diff --git a/apps/android/src/ui/heartbeat-message.ts b/apps/android/src/ui/heartbeat-message.ts new file mode 100644 index 0000000..c68e56f --- /dev/null +++ b/apps/android/src/ui/heartbeat-message.ts @@ -0,0 +1,20 @@ +/** Presentation only: never change the stored message or interpret arbitrary XML. */ +export function heartbeatMessageBody(text: string, role: 'user' | 'assistant'): string | undefined { + const envelope = /^\s*\s*([\s\S]*?)\s*<\/heartbeat>\s*$/.exec(text) + if (!envelope) return undefined + const fields = new Map() + let rest = envelope[1]! + while (rest.trim()) { + const field = /^\s*<(automation_id|decision|message|instructions)>([\s\S]*?)<\/\1>/.exec(rest) + if (!field || fields.has(field[1]!)) return undefined + fields.set(field[1]!, field[2]!.trim()) + rest = rest.slice(field[0].length) + } + if (!fields.get('automation_id')) return undefined + if (role === 'assistant') { + if (!fields.has('decision') || fields.has('instructions')) return undefined + return fields.get('message') || undefined + } + if (fields.has('decision') || fields.has('message')) return undefined + return fields.get('instructions') || undefined +}