@claudexor/harness-raw-api 4.8.2 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,159 @@
1
+ import { catalogRowFor, readVerifiedAttachmentBytes } from "@claudexor/core";
2
+ /** Tokens kept free for the answer when the catalog declares no completion
3
+ * limit (#147). No `max_tokens` is sent: this only reserves room in the window. */
4
+ export const RAW_API_DEFAULT_OUTPUT_ALLOWANCE_TOKENS = 16_384;
5
+ /** Estimated tokens per inline image part. Images are not chars/4 text; this
6
+ * is an approximate, disclosed figure, never a provider's tokenizer count. */
7
+ export const RAW_API_IMAGE_TOKEN_ESTIMATE = 1_600;
8
+ function patchIntent(spec) {
9
+ return spec.intent === "implement" || spec.intent === "synthesize";
10
+ }
11
+ /** The patch protocol around the packet; `packetJson` is the packet as sent. */
12
+ function patchPrompt(prompt, packetJson) {
13
+ return [
14
+ prompt,
15
+ "",
16
+ 'Produce the requested change only by returning one JSON object: {"patch":"<complete textual git unified diff>"}.',
17
+ "Touch existing files only from editable_paths. New paths are allowed only below creatable_roots.",
18
+ "Do not calculate hashes or preimage evidence; the trusted local adapter derives them. Do not use Markdown fences or add prose. Binary patches are unsupported.",
19
+ "RAW_CONTEXT_PACKET:",
20
+ packetJson,
21
+ ].join("\n");
22
+ }
23
+ function attachmentHeader(a) {
24
+ return `Attached file ${a.name || a.resource_id} (${a.mime}, ${a.sha256}):\n`;
25
+ }
26
+ /** Build the chat-completions content from digest-bound immutable resources. */
27
+ function rawApiUserContent(spec) {
28
+ const prompt = patchIntent(spec) && spec.raw_context_packet
29
+ ? patchPrompt(spec.prompt, JSON.stringify(spec.raw_context_packet))
30
+ : spec.prompt;
31
+ const attachments = spec.attachments ?? [];
32
+ if (attachments.length === 0)
33
+ return prompt;
34
+ const parts = [{ type: "text", text: prompt }];
35
+ for (const a of attachments) {
36
+ const bytes = readVerifiedAttachmentBytes(a);
37
+ parts.push(a.kind === "image"
38
+ ? {
39
+ type: "image_url",
40
+ image_url: { url: `data:${a.mime};base64,${bytes.toString("base64")}` },
41
+ }
42
+ : { type: "text", text: `${attachmentHeader(a)}${bytes.toString("utf8")}` });
43
+ }
44
+ return parts.length > 1 ? parts : prompt;
45
+ }
46
+ /** The exact messages the request sends: one renderer for POST and estimates. */
47
+ export function rawApiMessages(spec) {
48
+ return [
49
+ // Per-run instructions ride the OpenAI-style `system` role (the engine
50
+ // already withheld them from synthesis/reviewers).
51
+ ...(spec.instructions && spec.instructions.trim()
52
+ ? [{ role: "system", content: spec.instructions }]
53
+ : []),
54
+ { role: "user", content: rawApiUserContent(spec) },
55
+ ];
56
+ }
57
+ function estimate(chars, images) {
58
+ return Math.ceil(chars / 4) + images * RAW_API_IMAGE_TOKEN_ESTIMATE;
59
+ }
60
+ /** Estimated tokens of the text the model receives (chars/4) plus images. */
61
+ export function estimateMessageTokens(messages) {
62
+ let chars = 0;
63
+ let images = 0;
64
+ for (const { content } of messages) {
65
+ if (typeof content === "string")
66
+ chars += content.length;
67
+ else
68
+ for (const part of content) {
69
+ if (typeof part["text"] === "string")
70
+ chars += part["text"].length;
71
+ else
72
+ images += 1;
73
+ }
74
+ }
75
+ return estimate(chars, images);
76
+ }
77
+ /** Estimated tokens the request spends outside its packet, read from the spec
78
+ * without the packet or attachment bytes: a text attachment's byte length bounds
79
+ * its decoded characters, so this never undercounts the final rendering. */
80
+ export function estimateFramingTokens(spec) {
81
+ const instructions = spec.instructions?.trim() ? spec.instructions.length : 0;
82
+ const prompt = patchIntent(spec) ? patchPrompt(spec.prompt, "").length : spec.prompt.length;
83
+ let chars = instructions + prompt;
84
+ let images = 0;
85
+ for (const a of spec.attachments ?? []) {
86
+ if (a.kind === "image")
87
+ images += 1;
88
+ else
89
+ chars += attachmentHeader(a).length + a.size_bytes;
90
+ }
91
+ return estimate(chars, images);
92
+ }
93
+ /** #147 output allowance: the catalog's declared completion limit when known,
94
+ * else the documented default, and never more than half a known window — a
95
+ * catalog that declares a completion limit equal to the window would otherwise
96
+ * leave no room for any input. */
97
+ export function rawApiOutputAllowance(row, contextWindow) {
98
+ const declared = row?.max_output_tokens ?? null;
99
+ const wanted = declared ?? RAW_API_DEFAULT_OUTPUT_ALLOWANCE_TOKENS;
100
+ return {
101
+ tokens: contextWindow === null ? wanted : Math.min(wanted, Math.floor(contextWindow / 2)),
102
+ source: declared === null ? "default" : "catalog",
103
+ };
104
+ }
105
+ /** Capacity of `spec`'s request from `catalog`, the rows the dispatch gate
106
+ * read under the credential identity `run()` uses. A failed read ([]) or a
107
+ * missing row leaves the window unknown; it never refuses anything by itself. */
108
+ export function rawApiRequestCapacity(spec, model, routingVariants, catalog) {
109
+ const row = catalogRowFor(model, catalog, routingVariants);
110
+ const contextWindow = row?.context_window ?? null;
111
+ const allowance = rawApiOutputAllowance(row, contextWindow);
112
+ return {
113
+ model,
114
+ contextWindow,
115
+ outputAllowance: allowance.tokens,
116
+ outputAllowanceSource: allowance.source,
117
+ framingTokens: estimateFramingTokens(spec),
118
+ };
119
+ }
120
+ /**
121
+ * Catalog input modalities are declared metadata that refuse no request,
122
+ * consistent with the #435 rule for model-operation catalogs (#146). When a
123
+ * successful catalog lists this model's modalities without "image" and an
124
+ * image is attached, the run says so once and the provider decides; unknown
125
+ * modalities say nothing. Refusing on an explicitly listed exclusion would be
126
+ * a change of this one function's verdict and its call site in `run()`.
127
+ */
128
+ export function imageInputDisclosure(model, row) {
129
+ const modalities = row?.input_modalities;
130
+ if (!modalities || modalities.includes("image"))
131
+ return null;
132
+ return `the model catalog does not list image input for "${model}" (input modalities: ${modalities.join(", ")}); the image is sent unchanged and the provider decides`;
133
+ }
134
+ /** The pre-POST window check against the budget the packet was sized under:
135
+ * a refusal text when the final rendering cannot fit that KNOWN window. */
136
+ export function finalWindowRefusal(budget, model, estimatedTokens) {
137
+ if (budget?.basis !== "model_context_window" || budget.model !== model)
138
+ return null;
139
+ const window = budget.context_window;
140
+ const allowance = budget.output_allowance_tokens;
141
+ if (window === null || allowance === null || estimatedTokens + allowance <= window)
142
+ return null;
143
+ return `raw_context_exceeds_window: the request for "${model}" is estimated at ${estimatedTokens} tokens (chars/4) and ${allowance} more are reserved for the answer, over its ${window}-token context window; this request was not sent`;
144
+ }
145
+ /** One status line saying how the packet was sized (estimates disclosed). */
146
+ export function rawContextDisclosure(budget, model, estimatedTokens) {
147
+ const omitted = budget.omitted_for_budget > 0
148
+ ? `; ${budget.omitted_for_budget} optional file(s) omitted for the budget are listed in the packet omissions`
149
+ : "";
150
+ const mandatory = budget.mandatory_paths.length > 0
151
+ ? `; mandatory files included complete: ${budget.mandatory_paths.join(", ")}`
152
+ : "";
153
+ if (budget.basis === "model_context_window" && budget.model !== model)
154
+ return `raw context was sized for "${budget.model}" (${budget.context_window}-token window), but this request names "${model}", whose window was not checked; request estimated at ${estimatedTokens} tokens (chars/4)${omitted}${mandatory}`;
155
+ return budget.basis === "model_context_window"
156
+ ? `raw context sized for "${budget.model}": request estimated at ${estimatedTokens} tokens (chars/4) with ${budget.output_allowance_tokens} reserved for the answer, within its ${budget.context_window}-token context window${omitted}${mandatory}`
157
+ : `raw context: no context window is known for ${budget.model ? `"${budget.model}"` : "this request"}, so optional file contents were selected under an assumed ${budget.selection_limit_tokens}-token ceiling that is not a model fact; request estimated at ${estimatedTokens} tokens (chars/4)${omitted}${mandatory}`;
158
+ }
159
+ //# sourceMappingURL=request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request.js","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,aAAa,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAC;AAE7E;mFACmF;AACnF,MAAM,CAAC,MAAM,uCAAuC,GAAG,MAAM,CAAC;AAC9D;8EAC8E;AAC9E,MAAM,CAAC,MAAM,4BAA4B,GAAG,KAAK,CAAC;AAKlD,SAAS,WAAW,CAAC,IAAoB;IACvC,OAAO,IAAI,CAAC,MAAM,KAAK,WAAW,IAAI,IAAI,CAAC,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CAAC;AAC7T,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@claudexor/harness-raw-api",
3
- "version": "4.8.2",
3
+ "version": "5.0.1",
4
4
  "license": "MIT",
5
5
  "description": "Raw-API harness: OpenAI-compatible chat completions with typed, isolated Git-patch implementation.",
6
6
  "type": "module",
@@ -16,10 +16,10 @@
16
16
  "dist"
17
17
  ],
18
18
  "dependencies": {
19
- "@claudexor/schema": "4.8.2",
20
- "@claudexor/secrets": "4.8.2",
21
- "@claudexor/core": "4.8.2",
22
- "@claudexor/util": "4.8.2"
19
+ "@claudexor/schema": "5.0.1",
20
+ "@claudexor/core": "5.0.1",
21
+ "@claudexor/secrets": "5.0.1",
22
+ "@claudexor/util": "5.0.1"
23
23
  },
24
24
  "repository": {
25
25
  "type": "git",