(body: MinimalRequest["body"])
| 107 | |
| 108 | /** Build the wire-order segment list: system blocks first, then every message. */ |
| 109 | export function buildSegments(body: MinimalRequest["body"]): WireSegment[] { |
| 110 | const segs: WireSegment[] = []; |
| 111 | const system = body.system; |
| 112 | const sysBlocks = Array.isArray(system) ? system : system != null ? [system] : []; |
| 113 | sysBlocks.forEach((b, i) => { |
| 114 | const raw = blockText(b); |
| 115 | segs.push({ |
| 116 | id: `system[${i}]`, |
| 117 | hash: sha(normalizeSystemText(raw)), |
| 118 | bytes: Buffer.byteLength(raw), |
| 119 | breakpoint: hasCacheControl(b), |
| 120 | }); |
| 121 | }); |
| 122 | const messages = Array.isArray(body.messages) ? (body.messages as Json[]) : []; |
| 123 | messages.forEach((m, i) => { |
| 124 | const norm = JSON.stringify({ role: m.role, content: stripCacheControl(m.content) }); |
| 125 | segs.push({ |
| 126 | id: `message[${i}](${String(m.role)})`, |
| 127 | hash: sha(norm), |
| 128 | bytes: Buffer.byteLength(JSON.stringify(m)), |
| 129 | breakpoint: messageHasBreakpoint(m), |
| 130 | }); |
| 131 | }); |
| 132 | return segs; |
| 133 | } |
| 134 | |
| 135 | /** First wire-order segment index where prev/cur diverge (added/removed/changed). */ |
| 136 | function firstDivergence(prev: WireSegment[], cur: WireSegment[]): number { |
no test coverage detected