MCPcopy Create free account
hub / github.com/cortexkit/magic-context / buildSegments

Function buildSegments

packages/e2e-tests/src/cache-analysis.ts:109–133  ·  view source on GitHub ↗
(body: MinimalRequest["body"])

Source from the content-addressed store, hash-verified

107
108/** Build the wire-order segment list: system blocks first, then every message. */
109export 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). */
136function firstDivergence(prev: WireSegment[], cur: WireSegment[]): number {

Callers 2

analyzePassesFunction · 0.70

Calls 6

blockTextFunction · 0.70
shaFunction · 0.70
normalizeSystemTextFunction · 0.70
hasCacheControlFunction · 0.70
stripCacheControlFunction · 0.70
messageHasBreakpointFunction · 0.70

Tested by

no test coverage detected