Files
hapi/shared/src/messages.ts
T

74 lines
2.3 KiB
TypeScript

import { isObject } from './utils'
type RoleWrappedRecord = {
role: string
content: unknown
meta?: unknown
}
const VISIBLE_CLAUDE_SYSTEM_SUBTYPES = new Set([
'api_error',
'turn_duration',
'microcompact_boundary',
'compact_boundary'
])
export function isRoleWrappedRecord(value: unknown): value is RoleWrappedRecord {
if (!isObject(value)) return false
return typeof value.role === 'string' && 'content' in value
}
export function unwrapRoleWrappedRecordEnvelope(value: unknown): RoleWrappedRecord | null {
if (isRoleWrappedRecord(value)) return value
if (!isObject(value)) return null
const direct = value.message
if (isRoleWrappedRecord(direct)) return direct
const data = value.data
if (isObject(data) && isRoleWrappedRecord(data.message)) return data.message as RoleWrappedRecord
const payload = value.payload
if (isObject(payload) && isRoleWrappedRecord(payload.message)) return payload.message as RoleWrappedRecord
return null
}
export function isClaudeChatVisibleSystemSubtype(subtype: unknown): subtype is string {
return typeof subtype === 'string' && VISIBLE_CLAUDE_SYSTEM_SUBTYPES.has(subtype)
}
export function isClaudeChatVisibleMessage(message: { type: unknown; subtype?: unknown }): boolean {
if (message.type === 'rate_limit_event') {
return false
}
if (message.type !== 'system') {
return true
}
return isClaudeChatVisibleSystemSubtype(message.subtype)
}
export function isRedundantGoalStatusMessageText(value: unknown): boolean {
if (typeof value !== 'string') return false
const message = value.trim()
return message === 'Goal cleared'
|| /^Goal (active|paused|complete|limited by budget)(?:$|\s+·\s+)/.test(message)
}
export function isRedundantGoalStatusEventContent(value: unknown): boolean {
const record = unwrapRoleWrappedRecordEnvelope(value)
if (record?.role !== 'agent') return false
const eventContent = record.content
if (!isObject(eventContent) || eventContent.type !== 'event') return false
const data = isObject(eventContent.data) ? eventContent.data : null
if (!data || data.type !== 'message') return false
return isRedundantGoalStatusMessageText(data.message)
}
export type { RoleWrappedRecord }