(m: Message)
| 26 | /** Canonical, stable serialization of a single message for prefix comparison. |
| 27 | * Includes exactly the fields that go on the wire and affect the KV cache. */ |
| 28 | export function serializeMessage(m: Message): string { |
| 29 | const parts: string[] = [`role:${m.role}`]; |
| 30 | parts.push(`content:${m.content ?? ''}`); |
| 31 | if (m.tool_call_id) parts.push(`tcid:${m.tool_call_id}`); |
| 32 | if (m.name) parts.push(`name:${m.name}`); |
| 33 | if (m.tool_calls && m.tool_calls.length) { |
| 34 | for (const tc of m.tool_calls) { |
| 35 | parts.push(`tc:${tc.function.name}(${tc.function.arguments})`); |
| 36 | } |
| 37 | } |
| 38 | return parts.join(''); |
| 39 | } |
| 40 | |
| 41 | /** Number of leading messages that are byte-identical between two message arrays. |
| 42 | * This is the prefix the inference server can serve from KV cache. */ |
no test coverage detected