|
| 1 | +export * as ContextAccounting from "./context-accounting" |
| 2 | + |
| 3 | +import { Token } from "@opencode-ai/core/util/token" |
| 4 | +import type { LLMRequest } from "@opencode-ai/llm" |
| 5 | + |
| 6 | +/** |
| 7 | + * Per-source token/byte accounting for one composed LLMRequest -- the spec's "record context |
| 8 | + * bytes and tokens contributed by goal, ledger, profile, recent tail, and retrieved history" |
| 9 | + * requirement, and the harness's primary defense against "reliability bought by injecting the |
| 10 | + * full transcript" (making history_search results balloon the tail would show up here as a |
| 11 | + * jump in `tail`, not `retrievedHistory`, since a tool result IS part of the message tail). |
| 12 | + * |
| 13 | + * `system` in an LLMRequest is a flat array of parts with no source tags, so attribution here is |
| 14 | + * by TEXT MATCH against durable-state renderers' own delimiters (`<session_goal>`, |
| 15 | + * `<session_ledger>`) rather than by construction -- the harness observes the wire artifact, the |
| 16 | + * same discipline the rest of this fork uses (verify the artifact, not the mechanism that built |
| 17 | + * it). A goal/ledger renderer that changes its wrapper tag needs this updated alongside it; there |
| 18 | + * is no schema-level guarantee tying the two together. |
| 19 | + */ |
| 20 | +export interface Breakdown { |
| 21 | + readonly goal: { readonly bytes: number; readonly tokens: number } |
| 22 | + readonly ledger: { readonly bytes: number; readonly tokens: number } |
| 23 | + readonly otherSystem: { readonly bytes: number; readonly tokens: number } |
| 24 | + readonly tail: { readonly bytes: number; readonly tokens: number } |
| 25 | + readonly tools: { readonly bytes: number; readonly tokens: number } |
| 26 | + readonly total: { readonly bytes: number; readonly tokens: number } |
| 27 | +} |
| 28 | + |
| 29 | +const sizeOf = (text: string) => ({ bytes: Buffer.byteLength(text, "utf8"), tokens: Token.estimate(text) }) |
| 30 | + |
| 31 | +const add = (a: { bytes: number; tokens: number }, b: { bytes: number; tokens: number }) => ({ |
| 32 | + bytes: a.bytes + b.bytes, |
| 33 | + tokens: a.tokens + b.tokens, |
| 34 | +}) |
| 35 | + |
| 36 | +export const breakdown = (request: LLMRequest): Breakdown => { |
| 37 | + let goal = { bytes: 0, tokens: 0 } |
| 38 | + let ledger = { bytes: 0, tokens: 0 } |
| 39 | + let otherSystem = { bytes: 0, tokens: 0 } |
| 40 | + |
| 41 | + for (const part of request.system) { |
| 42 | + const size = sizeOf(part.text) |
| 43 | + if (part.text.includes("<session_goal>")) goal = add(goal, size) |
| 44 | + else if (part.text.includes("<session_ledger>")) ledger = add(ledger, size) |
| 45 | + else otherSystem = add(otherSystem, size) |
| 46 | + } |
| 47 | + |
| 48 | + const tail = sizeOf(JSON.stringify(request.messages)) |
| 49 | + const tools = sizeOf(JSON.stringify(request.tools ?? [])) |
| 50 | + const total = [goal, ledger, otherSystem, tail, tools].reduce(add, { bytes: 0, tokens: 0 }) |
| 51 | + |
| 52 | + return { goal, ledger, otherSystem, tail, tools, total } |
| 53 | +} |
0 commit comments