johns-harness 2026.10.21 → 2026.10.23
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.
- package/CHANGELOG.md +15 -0
- package/README.md +3 -0
- package/dist/anthropic-auth-runtime.js +62 -0
- package/dist/auth-profiles-5CHn7vq1.js +9 -4
- package/dist/compact-B247y5Qt.js +41 -74
- package/dist/config-koj5M_EO.js +4 -2
- package/dist/daemon-cli.js +4 -2
- package/dist/model-selection-BGlGpPgM.js +9 -4
- package/dist/model-selection-BU6wl1le.js +9 -4
- package/dist/model-selection-L7RMwsG-.js +9 -4
- package/dist/pi-embedded-C6ITuRXf.js +42 -89
- package/dist/pi-embedded-DoQsYfIY.js +42 -89
- package/dist/plugin-sdk/bluebubbles.js +2 -1
- package/dist/plugin-sdk/config/types.models.d.ts +1 -0
- package/dist/plugin-sdk/config/zod-schema.core.d.ts +3 -0
- package/dist/plugin-sdk/config/zod-schema.d.ts +1 -0
- package/dist/plugin-sdk/config-4Fgm-yEH.js +9 -4
- package/dist/plugin-sdk/config-Di-lyCdh.js +9 -4
- package/dist/plugin-sdk/discord.js +4 -2
- package/dist/plugin-sdk/dispatch-BP0viZiL.js +42 -89
- package/dist/plugin-sdk/dispatch-CQsjmw7g.js +42 -89
- package/dist/plugin-sdk/dispatch-Cerq29sy.js +42 -89
- package/dist/plugin-sdk/dispatch-Cndjtt0g.js +42 -89
- package/dist/plugin-sdk/dispatch-UogiJYul.js +42 -89
- package/dist/plugin-sdk/feishu.js +2 -1
- package/dist/plugin-sdk/googlechat.js +2 -1
- package/dist/plugin-sdk/imessage.js +2 -1
- package/dist/plugin-sdk/model-auth-CX9cPHdC.js +9 -4
- package/dist/plugin-sdk/model-auth-Ciehz0x5.js +9 -4
- package/dist/plugin-sdk/model-auth-DVyo6JSX.js +9 -4
- package/dist/plugin-sdk/model-auth-kiHYHGrs.js +9 -4
- package/dist/plugin-sdk/model-selection-Dbj4jDYM.js +8 -3
- package/dist/plugin-sdk/nostr.js +2 -1
- package/dist/plugin-sdk/reply-DbZnH8-h.js +42 -89
- package/dist/plugin-sdk/signal.js +2 -1
- package/dist/plugin-sdk/slack.js +2 -1
- package/dist/plugin-sdk/telegram.js +2 -1
- package/dist/plugin-sdk/tlon.js +2 -1
- package/dist/plugin-sdk/twitch.js +2 -1
- package/dist/plugin-sdk/utils/usage-format.d.ts +1 -0
- package/dist/plugin-sdk/voice-call.js +2 -1
- package/dist/plugin-sdk/zalo.js +2 -1
- package/dist/plugin-sdk/zalouser.js +2 -1
- package/dist/reply-C5LKjXcC.js +41 -74
- package/dist/session-cost-usage-5CCZbVhO.js +4 -19
- package/dist/session-cost-usage-CkCo2kVx.js +4 -19
- package/dist/usage-billing.js +88 -0
- package/dist/usage-format-CNGi1eHQ.js +6 -11
- package/dist/usage-format-CvaU1ntA.js +6 -11
- package/docs/PATCHES.md +54 -1
- package/docs/providers/anthropic.md +23 -2
- package/docs/reference/token-use.md +11 -1
- package/models/models.json +33 -33
- package/node_modules/@mariozechner/pi-ai/dist/models.d.ts +1 -0
- package/node_modules/@mariozechner/pi-ai/dist/models.generated.js +32 -32
- package/node_modules/@mariozechner/pi-ai/dist/models.js +16 -5
- package/node_modules/@mariozechner/pi-ai/dist/providers/anthropic.js +15 -0
- package/node_modules/@mariozechner/pi-ai/dist/providers/google-vertex.js +1 -1
- package/node_modules/@mariozechner/pi-ai/dist/providers/google.js +1 -1
- package/node_modules/@mariozechner/pi-ai/dist/providers/openai-completions.js +5 -5
- package/node_modules/@mariozechner/pi-ai/dist/types.d.ts +3 -0
- package/node_modules/@mariozechner/pi-coding-agent/dist/core/model-registry.js +3 -0
- package/package.json +1 -1
- package/scripts/models-sync.mjs +1 -0
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/models.d.ts +25 -0
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/models.generated.js +32 -32
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/models.js +16 -5
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/anthropic.js +15 -0
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/google-vertex.js +371 -0
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/google.js +1 -1
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/openai-completions.js +5 -5
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/types.d.ts +282 -0
- package/vendor/patched-deps/@mariozechner/pi-coding-agent/dist/core/model-registry.js +3 -0
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import { GoogleGenAI, ThinkingLevel, } from "@google/genai";
|
|
2
|
+
import { calculateCost } from "../models.js";
|
|
3
|
+
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
4
|
+
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
|
|
5
|
+
import { convertMessages, convertTools, isThinkingPart, mapStopReason, mapToolChoice, retainThoughtSignature, } from "./google-shared.js";
|
|
6
|
+
import { buildBaseOptions, clampReasoning } from "./simple-options.js";
|
|
7
|
+
const API_VERSION = "v1";
|
|
8
|
+
const THINKING_LEVEL_MAP = {
|
|
9
|
+
THINKING_LEVEL_UNSPECIFIED: ThinkingLevel.THINKING_LEVEL_UNSPECIFIED,
|
|
10
|
+
MINIMAL: ThinkingLevel.MINIMAL,
|
|
11
|
+
LOW: ThinkingLevel.LOW,
|
|
12
|
+
MEDIUM: ThinkingLevel.MEDIUM,
|
|
13
|
+
HIGH: ThinkingLevel.HIGH,
|
|
14
|
+
};
|
|
15
|
+
// Counter for generating unique tool call IDs
|
|
16
|
+
let toolCallCounter = 0;
|
|
17
|
+
export const streamGoogleVertex = (model, context, options) => {
|
|
18
|
+
const stream = new AssistantMessageEventStream();
|
|
19
|
+
(async () => {
|
|
20
|
+
const output = {
|
|
21
|
+
role: "assistant",
|
|
22
|
+
content: [],
|
|
23
|
+
api: "google-vertex",
|
|
24
|
+
provider: model.provider,
|
|
25
|
+
model: model.id,
|
|
26
|
+
usage: {
|
|
27
|
+
input: 0,
|
|
28
|
+
output: 0,
|
|
29
|
+
cacheRead: 0,
|
|
30
|
+
cacheWrite: 0,
|
|
31
|
+
totalTokens: 0,
|
|
32
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
33
|
+
},
|
|
34
|
+
stopReason: "stop",
|
|
35
|
+
timestamp: Date.now(),
|
|
36
|
+
};
|
|
37
|
+
try {
|
|
38
|
+
const project = resolveProject(options);
|
|
39
|
+
const location = resolveLocation(options);
|
|
40
|
+
const client = createClient(model, project, location, options?.headers);
|
|
41
|
+
const params = buildParams(model, context, options);
|
|
42
|
+
options?.onPayload?.(params);
|
|
43
|
+
const googleStream = await client.models.generateContentStream(params);
|
|
44
|
+
stream.push({ type: "start", partial: output });
|
|
45
|
+
let currentBlock = null;
|
|
46
|
+
const blocks = output.content;
|
|
47
|
+
const blockIndex = () => blocks.length - 1;
|
|
48
|
+
for await (const chunk of googleStream) {
|
|
49
|
+
const candidate = chunk.candidates?.[0];
|
|
50
|
+
if (candidate?.content?.parts) {
|
|
51
|
+
for (const part of candidate.content.parts) {
|
|
52
|
+
if (part.text !== undefined) {
|
|
53
|
+
const isThinking = isThinkingPart(part);
|
|
54
|
+
if (!currentBlock ||
|
|
55
|
+
(isThinking && currentBlock.type !== "thinking") ||
|
|
56
|
+
(!isThinking && currentBlock.type !== "text")) {
|
|
57
|
+
if (currentBlock) {
|
|
58
|
+
if (currentBlock.type === "text") {
|
|
59
|
+
stream.push({
|
|
60
|
+
type: "text_end",
|
|
61
|
+
contentIndex: blocks.length - 1,
|
|
62
|
+
content: currentBlock.text,
|
|
63
|
+
partial: output,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
stream.push({
|
|
68
|
+
type: "thinking_end",
|
|
69
|
+
contentIndex: blockIndex(),
|
|
70
|
+
content: currentBlock.thinking,
|
|
71
|
+
partial: output,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (isThinking) {
|
|
76
|
+
currentBlock = { type: "thinking", thinking: "", thinkingSignature: undefined };
|
|
77
|
+
output.content.push(currentBlock);
|
|
78
|
+
stream.push({ type: "thinking_start", contentIndex: blockIndex(), partial: output });
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
currentBlock = { type: "text", text: "" };
|
|
82
|
+
output.content.push(currentBlock);
|
|
83
|
+
stream.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (currentBlock.type === "thinking") {
|
|
87
|
+
currentBlock.thinking += part.text;
|
|
88
|
+
currentBlock.thinkingSignature = retainThoughtSignature(currentBlock.thinkingSignature, part.thoughtSignature);
|
|
89
|
+
stream.push({
|
|
90
|
+
type: "thinking_delta",
|
|
91
|
+
contentIndex: blockIndex(),
|
|
92
|
+
delta: part.text,
|
|
93
|
+
partial: output,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
currentBlock.text += part.text;
|
|
98
|
+
currentBlock.textSignature = retainThoughtSignature(currentBlock.textSignature, part.thoughtSignature);
|
|
99
|
+
stream.push({
|
|
100
|
+
type: "text_delta",
|
|
101
|
+
contentIndex: blockIndex(),
|
|
102
|
+
delta: part.text,
|
|
103
|
+
partial: output,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (part.functionCall) {
|
|
108
|
+
if (currentBlock) {
|
|
109
|
+
if (currentBlock.type === "text") {
|
|
110
|
+
stream.push({
|
|
111
|
+
type: "text_end",
|
|
112
|
+
contentIndex: blockIndex(),
|
|
113
|
+
content: currentBlock.text,
|
|
114
|
+
partial: output,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
stream.push({
|
|
119
|
+
type: "thinking_end",
|
|
120
|
+
contentIndex: blockIndex(),
|
|
121
|
+
content: currentBlock.thinking,
|
|
122
|
+
partial: output,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
currentBlock = null;
|
|
126
|
+
}
|
|
127
|
+
const providedId = part.functionCall.id;
|
|
128
|
+
const needsNewId = !providedId || output.content.some((b) => b.type === "toolCall" && b.id === providedId);
|
|
129
|
+
const toolCallId = needsNewId
|
|
130
|
+
? `${part.functionCall.name}_${Date.now()}_${++toolCallCounter}`
|
|
131
|
+
: providedId;
|
|
132
|
+
const toolCall = {
|
|
133
|
+
type: "toolCall",
|
|
134
|
+
id: toolCallId,
|
|
135
|
+
name: part.functionCall.name || "",
|
|
136
|
+
arguments: part.functionCall.args ?? {},
|
|
137
|
+
...(part.thoughtSignature && { thoughtSignature: part.thoughtSignature }),
|
|
138
|
+
};
|
|
139
|
+
output.content.push(toolCall);
|
|
140
|
+
stream.push({ type: "toolcall_start", contentIndex: blockIndex(), partial: output });
|
|
141
|
+
stream.push({
|
|
142
|
+
type: "toolcall_delta",
|
|
143
|
+
contentIndex: blockIndex(),
|
|
144
|
+
delta: JSON.stringify(toolCall.arguments),
|
|
145
|
+
partial: output,
|
|
146
|
+
});
|
|
147
|
+
stream.push({ type: "toolcall_end", contentIndex: blockIndex(), toolCall, partial: output });
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (candidate?.finishReason) {
|
|
152
|
+
output.stopReason = mapStopReason(candidate.finishReason);
|
|
153
|
+
if (output.content.some((b) => b.type === "toolCall")) {
|
|
154
|
+
output.stopReason = "toolUse";
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (chunk.usageMetadata) {
|
|
158
|
+
output.usage = {
|
|
159
|
+
input: Math.max(0, (chunk.usageMetadata.promptTokenCount || 0) - (chunk.usageMetadata.cachedContentTokenCount || 0)), // JOHNNESS_PATCH_USAGE_BILLING_91
|
|
160
|
+
output: (chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),
|
|
161
|
+
cacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,
|
|
162
|
+
cacheWrite: 0,
|
|
163
|
+
totalTokens: chunk.usageMetadata.totalTokenCount || 0,
|
|
164
|
+
cost: {
|
|
165
|
+
input: 0,
|
|
166
|
+
output: 0,
|
|
167
|
+
cacheRead: 0,
|
|
168
|
+
cacheWrite: 0,
|
|
169
|
+
total: 0,
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
calculateCost(model, output.usage);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (currentBlock) {
|
|
176
|
+
if (currentBlock.type === "text") {
|
|
177
|
+
stream.push({
|
|
178
|
+
type: "text_end",
|
|
179
|
+
contentIndex: blockIndex(),
|
|
180
|
+
content: currentBlock.text,
|
|
181
|
+
partial: output,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
stream.push({
|
|
186
|
+
type: "thinking_end",
|
|
187
|
+
contentIndex: blockIndex(),
|
|
188
|
+
content: currentBlock.thinking,
|
|
189
|
+
partial: output,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (options?.signal?.aborted) {
|
|
194
|
+
throw new Error("Request was aborted");
|
|
195
|
+
}
|
|
196
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") {
|
|
197
|
+
throw new Error("An unknown error occurred");
|
|
198
|
+
}
|
|
199
|
+
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
200
|
+
stream.end();
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
// Remove internal index property used during streaming
|
|
204
|
+
for (const block of output.content) {
|
|
205
|
+
if ("index" in block) {
|
|
206
|
+
delete block.index;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
210
|
+
output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
|
|
211
|
+
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
212
|
+
stream.end();
|
|
213
|
+
}
|
|
214
|
+
})();
|
|
215
|
+
return stream;
|
|
216
|
+
};
|
|
217
|
+
export const streamSimpleGoogleVertex = (model, context, options) => {
|
|
218
|
+
const base = buildBaseOptions(model, options, undefined);
|
|
219
|
+
if (!options?.reasoning) {
|
|
220
|
+
return streamGoogleVertex(model, context, {
|
|
221
|
+
...base,
|
|
222
|
+
thinking: { enabled: false },
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
const effort = clampReasoning(options.reasoning);
|
|
226
|
+
const geminiModel = model;
|
|
227
|
+
if (isGemini3ProModel(geminiModel) || isGemini3FlashModel(geminiModel)) {
|
|
228
|
+
return streamGoogleVertex(model, context, {
|
|
229
|
+
...base,
|
|
230
|
+
thinking: {
|
|
231
|
+
enabled: true,
|
|
232
|
+
level: getGemini3ThinkingLevel(effort, geminiModel),
|
|
233
|
+
},
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
return streamGoogleVertex(model, context, {
|
|
237
|
+
...base,
|
|
238
|
+
thinking: {
|
|
239
|
+
enabled: true,
|
|
240
|
+
budgetTokens: getGoogleBudget(geminiModel, effort, options.thinkingBudgets),
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
};
|
|
244
|
+
function createClient(model, project, location, optionsHeaders) {
|
|
245
|
+
const httpOptions = {};
|
|
246
|
+
if (model.headers || optionsHeaders) {
|
|
247
|
+
httpOptions.headers = { ...model.headers, ...optionsHeaders };
|
|
248
|
+
}
|
|
249
|
+
const hasHttpOptions = Object.values(httpOptions).some(Boolean);
|
|
250
|
+
return new GoogleGenAI({
|
|
251
|
+
vertexai: true,
|
|
252
|
+
project,
|
|
253
|
+
location,
|
|
254
|
+
apiVersion: API_VERSION,
|
|
255
|
+
httpOptions: hasHttpOptions ? httpOptions : undefined,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
function resolveProject(options) {
|
|
259
|
+
const project = options?.project || process.env.GOOGLE_CLOUD_PROJECT || process.env.GCLOUD_PROJECT;
|
|
260
|
+
if (!project) {
|
|
261
|
+
throw new Error("Vertex AI requires a project ID. Set GOOGLE_CLOUD_PROJECT/GCLOUD_PROJECT or pass project in options.");
|
|
262
|
+
}
|
|
263
|
+
return project;
|
|
264
|
+
}
|
|
265
|
+
function resolveLocation(options) {
|
|
266
|
+
const location = options?.location || process.env.GOOGLE_CLOUD_LOCATION;
|
|
267
|
+
if (!location) {
|
|
268
|
+
throw new Error("Vertex AI requires a location. Set GOOGLE_CLOUD_LOCATION or pass location in options.");
|
|
269
|
+
}
|
|
270
|
+
return location;
|
|
271
|
+
}
|
|
272
|
+
function buildParams(model, context, options = {}) {
|
|
273
|
+
const contents = convertMessages(model, context);
|
|
274
|
+
const generationConfig = {};
|
|
275
|
+
if (options.temperature !== undefined) {
|
|
276
|
+
generationConfig.temperature = options.temperature;
|
|
277
|
+
}
|
|
278
|
+
if (options.maxTokens !== undefined) {
|
|
279
|
+
generationConfig.maxOutputTokens = options.maxTokens;
|
|
280
|
+
}
|
|
281
|
+
const config = {
|
|
282
|
+
...(Object.keys(generationConfig).length > 0 && generationConfig),
|
|
283
|
+
...(context.systemPrompt && { systemInstruction: sanitizeSurrogates(context.systemPrompt) }),
|
|
284
|
+
...(context.tools && context.tools.length > 0 && { tools: convertTools(context.tools) }),
|
|
285
|
+
};
|
|
286
|
+
if (context.tools && context.tools.length > 0 && options.toolChoice) {
|
|
287
|
+
config.toolConfig = {
|
|
288
|
+
functionCallingConfig: {
|
|
289
|
+
mode: mapToolChoice(options.toolChoice),
|
|
290
|
+
},
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
config.toolConfig = undefined;
|
|
295
|
+
}
|
|
296
|
+
if (options.thinking?.enabled && model.reasoning) {
|
|
297
|
+
const thinkingConfig = { includeThoughts: true };
|
|
298
|
+
if (options.thinking.level !== undefined) {
|
|
299
|
+
thinkingConfig.thinkingLevel = THINKING_LEVEL_MAP[options.thinking.level];
|
|
300
|
+
}
|
|
301
|
+
else if (options.thinking.budgetTokens !== undefined) {
|
|
302
|
+
thinkingConfig.thinkingBudget = options.thinking.budgetTokens;
|
|
303
|
+
}
|
|
304
|
+
config.thinkingConfig = thinkingConfig;
|
|
305
|
+
}
|
|
306
|
+
if (options.signal) {
|
|
307
|
+
if (options.signal.aborted) {
|
|
308
|
+
throw new Error("Request aborted");
|
|
309
|
+
}
|
|
310
|
+
config.abortSignal = options.signal;
|
|
311
|
+
}
|
|
312
|
+
const params = {
|
|
313
|
+
model: model.id,
|
|
314
|
+
contents,
|
|
315
|
+
config,
|
|
316
|
+
};
|
|
317
|
+
return params;
|
|
318
|
+
}
|
|
319
|
+
function isGemini3ProModel(model) {
|
|
320
|
+
return model.id.includes("3-pro");
|
|
321
|
+
}
|
|
322
|
+
function isGemini3FlashModel(model) {
|
|
323
|
+
return model.id.includes("3-flash");
|
|
324
|
+
}
|
|
325
|
+
function getGemini3ThinkingLevel(effort, model) {
|
|
326
|
+
if (isGemini3ProModel(model)) {
|
|
327
|
+
switch (effort) {
|
|
328
|
+
case "minimal":
|
|
329
|
+
case "low":
|
|
330
|
+
return "LOW";
|
|
331
|
+
case "medium":
|
|
332
|
+
case "high":
|
|
333
|
+
return "HIGH";
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
switch (effort) {
|
|
337
|
+
case "minimal":
|
|
338
|
+
return "MINIMAL";
|
|
339
|
+
case "low":
|
|
340
|
+
return "LOW";
|
|
341
|
+
case "medium":
|
|
342
|
+
return "MEDIUM";
|
|
343
|
+
case "high":
|
|
344
|
+
return "HIGH";
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
function getGoogleBudget(model, effort, customBudgets) {
|
|
348
|
+
if (customBudgets?.[effort] !== undefined) {
|
|
349
|
+
return customBudgets[effort];
|
|
350
|
+
}
|
|
351
|
+
if (model.id.includes("2.5-pro")) {
|
|
352
|
+
const budgets = {
|
|
353
|
+
minimal: 128,
|
|
354
|
+
low: 2048,
|
|
355
|
+
medium: 8192,
|
|
356
|
+
high: 32768,
|
|
357
|
+
};
|
|
358
|
+
return budgets[effort];
|
|
359
|
+
}
|
|
360
|
+
if (model.id.includes("2.5-flash")) {
|
|
361
|
+
const budgets = {
|
|
362
|
+
minimal: 128,
|
|
363
|
+
low: 2048,
|
|
364
|
+
medium: 8192,
|
|
365
|
+
high: 24576,
|
|
366
|
+
};
|
|
367
|
+
return budgets[effort];
|
|
368
|
+
}
|
|
369
|
+
return -1;
|
|
370
|
+
}
|
|
371
|
+
//# sourceMappingURL=google-vertex.js.map
|
|
@@ -182,7 +182,7 @@ export const streamGoogle = (model, context, options) => {
|
|
|
182
182
|
}
|
|
183
183
|
if (chunk.usageMetadata) {
|
|
184
184
|
output.usage = {
|
|
185
|
-
input: chunk.usageMetadata.promptTokenCount || 0,
|
|
185
|
+
input: Math.max(0, (chunk.usageMetadata.promptTokenCount || 0) - (chunk.usageMetadata.cachedContentTokenCount || 0)), // JOHNNESS_PATCH_USAGE_BILLING_91
|
|
186
186
|
output: (chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),
|
|
187
187
|
cacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,
|
|
188
188
|
cacheWrite: 0,
|
|
@@ -105,18 +105,18 @@ export const streamOpenAICompletions = (model, context, options) => {
|
|
|
105
105
|
};
|
|
106
106
|
for await (const chunk of openaiStream) {
|
|
107
107
|
if (chunk.usage) {
|
|
108
|
-
const cachedTokens = chunk.usage.prompt_tokens_details?.cached_tokens
|
|
108
|
+
const cachedTokens = chunk.usage.prompt_tokens_details?.cached_tokens ?? chunk.usage.prompt_cache_hit_tokens ?? 0; // JOHNNESS_PATCH_USAGE_BILLING_91
|
|
109
109
|
const reasoningTokens = chunk.usage.completion_tokens_details?.reasoning_tokens || 0;
|
|
110
|
-
const input = (chunk.usage.prompt_tokens || 0) - cachedTokens;
|
|
111
|
-
|
|
110
|
+
const input = Math.max(0, (chunk.usage.prompt_tokens || 0) - cachedTokens);
|
|
111
|
+
// OpenAI-compatible completion_tokens includes reasoning. Groq reports it separately.
|
|
112
|
+
const outputTokens = (chunk.usage.completion_tokens || 0) + (model.provider === "groq" ? reasoningTokens : 0);
|
|
112
113
|
output.usage = {
|
|
113
114
|
// OpenAI includes cached tokens in prompt_tokens, so subtract to get non-cached input
|
|
114
115
|
input,
|
|
115
116
|
output: outputTokens,
|
|
116
117
|
cacheRead: cachedTokens,
|
|
117
118
|
cacheWrite: 0,
|
|
118
|
-
// Compute totalTokens
|
|
119
|
-
// and some providers (e.g., Groq) don't include them in total_tokens
|
|
119
|
+
// Compute totalTokens from disjoint buckets (Groq reasoning is separate).
|
|
120
120
|
totalTokens: input + outputTokens + cachedTokens,
|
|
121
121
|
cost: {
|
|
122
122
|
input: 0,
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import type { AssistantMessageEventStream } from "./utils/event-stream.js";
|
|
2
|
+
export type { AssistantMessageEventStream } from "./utils/event-stream.js";
|
|
3
|
+
export type KnownApi = "openai-completions" | "openai-responses" | "azure-openai-responses" | "openai-codex-responses" | "anthropic-messages" | "bedrock-converse-stream" | "google-generative-ai" | "google-gemini-cli" | "google-vertex";
|
|
4
|
+
export type Api = KnownApi | (string & {});
|
|
5
|
+
export type KnownProvider = "amazon-bedrock" | "anthropic" | "google" | "google-gemini-cli" | "google-antigravity" | "google-vertex" | "openai" | "azure-openai-responses" | "openai-codex" | "github-copilot" | "xai" | "groq" | "cerebras" | "openrouter" | "vercel-ai-gateway" | "zai" | "mistral" | "minimax" | "minimax-cn" | "huggingface" | "opencode" | "kimi-coding";
|
|
6
|
+
export type Provider = KnownProvider | string;
|
|
7
|
+
export type ThinkingLevel = "minimal" | "low" | "medium" | "high" | "xhigh";
|
|
8
|
+
/** Token budgets for each thinking level (token-based providers only) */
|
|
9
|
+
export interface ThinkingBudgets {
|
|
10
|
+
minimal?: number;
|
|
11
|
+
low?: number;
|
|
12
|
+
medium?: number;
|
|
13
|
+
high?: number;
|
|
14
|
+
}
|
|
15
|
+
export type CacheRetention = "none" | "short" | "long";
|
|
16
|
+
export type Transport = "sse" | "websocket" | "auto";
|
|
17
|
+
export interface StreamOptions {
|
|
18
|
+
temperature?: number;
|
|
19
|
+
maxTokens?: number;
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
apiKey?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Preferred transport for providers that support multiple transports.
|
|
24
|
+
* Providers that do not support this option ignore it.
|
|
25
|
+
*/
|
|
26
|
+
transport?: Transport;
|
|
27
|
+
/**
|
|
28
|
+
* Prompt cache retention preference. Providers map this to their supported values.
|
|
29
|
+
* Default: "short".
|
|
30
|
+
*/
|
|
31
|
+
cacheRetention?: CacheRetention;
|
|
32
|
+
/**
|
|
33
|
+
* Optional session identifier for providers that support session-based caching.
|
|
34
|
+
* Providers can use this to enable prompt caching, request routing, or other
|
|
35
|
+
* session-aware features. Ignored by providers that don't support it.
|
|
36
|
+
*/
|
|
37
|
+
sessionId?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Optional callback for inspecting provider payloads before sending.
|
|
40
|
+
*/
|
|
41
|
+
onPayload?: (payload: unknown) => void;
|
|
42
|
+
/**
|
|
43
|
+
* Optional custom HTTP headers to include in API requests.
|
|
44
|
+
* Merged with provider defaults; can override default headers.
|
|
45
|
+
* Not supported by all providers (e.g., AWS Bedrock uses SDK auth).
|
|
46
|
+
*/
|
|
47
|
+
headers?: Record<string, string>;
|
|
48
|
+
/**
|
|
49
|
+
* Maximum delay in milliseconds to wait for a retry when the server requests a long wait.
|
|
50
|
+
* If the server's requested delay exceeds this value, the request fails immediately
|
|
51
|
+
* with an error containing the requested delay, allowing higher-level retry logic
|
|
52
|
+
* to handle it with user visibility.
|
|
53
|
+
* Default: 60000 (60 seconds). Set to 0 to disable the cap.
|
|
54
|
+
*/
|
|
55
|
+
maxRetryDelayMs?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Optional metadata to include in API requests.
|
|
58
|
+
* Providers extract the fields they understand and ignore the rest.
|
|
59
|
+
* For example, Anthropic uses `user_id` for abuse tracking and rate limiting.
|
|
60
|
+
*/
|
|
61
|
+
metadata?: Record<string, unknown>;
|
|
62
|
+
}
|
|
63
|
+
export type ProviderStreamOptions = StreamOptions & Record<string, unknown>;
|
|
64
|
+
export interface SimpleStreamOptions extends StreamOptions {
|
|
65
|
+
reasoning?: ThinkingLevel;
|
|
66
|
+
/** Custom token budgets for thinking levels (token-based providers only) */
|
|
67
|
+
thinkingBudgets?: ThinkingBudgets;
|
|
68
|
+
}
|
|
69
|
+
export type StreamFunction<TApi extends Api = Api, TOptions extends StreamOptions = StreamOptions> = (model: Model<TApi>, context: Context, options?: TOptions) => AssistantMessageEventStream;
|
|
70
|
+
export interface TextContent {
|
|
71
|
+
type: "text";
|
|
72
|
+
text: string;
|
|
73
|
+
textSignature?: string;
|
|
74
|
+
}
|
|
75
|
+
export interface ThinkingContent {
|
|
76
|
+
type: "thinking";
|
|
77
|
+
thinking: string;
|
|
78
|
+
thinkingSignature?: string;
|
|
79
|
+
/** When true, the thinking content was redacted by safety filters. The opaque
|
|
80
|
+
* encrypted payload is stored in `thinkingSignature` so it can be passed back
|
|
81
|
+
* to the API for multi-turn continuity. */
|
|
82
|
+
redacted?: boolean;
|
|
83
|
+
}
|
|
84
|
+
export interface ImageContent {
|
|
85
|
+
type: "image";
|
|
86
|
+
data: string;
|
|
87
|
+
mimeType: string;
|
|
88
|
+
}
|
|
89
|
+
export interface ToolCall {
|
|
90
|
+
type: "toolCall";
|
|
91
|
+
id: string;
|
|
92
|
+
name: string;
|
|
93
|
+
arguments: Record<string, any>;
|
|
94
|
+
thoughtSignature?: string;
|
|
95
|
+
}
|
|
96
|
+
export interface Usage {
|
|
97
|
+
input: number;
|
|
98
|
+
output: number;
|
|
99
|
+
cacheRead: number;
|
|
100
|
+
cacheWrite: number;
|
|
101
|
+
cacheWrite5m?: number;
|
|
102
|
+
cacheWrite1h?: number;
|
|
103
|
+
totalTokens: number;
|
|
104
|
+
cost: {
|
|
105
|
+
input: number;
|
|
106
|
+
output: number;
|
|
107
|
+
cacheRead: number;
|
|
108
|
+
cacheWrite: number;
|
|
109
|
+
total: number;
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
export type StopReason = "stop" | "length" | "toolUse" | "error" | "aborted";
|
|
113
|
+
export interface UserMessage {
|
|
114
|
+
role: "user";
|
|
115
|
+
content: string | (TextContent | ImageContent)[];
|
|
116
|
+
timestamp: number;
|
|
117
|
+
}
|
|
118
|
+
export interface AssistantMessage {
|
|
119
|
+
role: "assistant";
|
|
120
|
+
content: (TextContent | ThinkingContent | ToolCall)[];
|
|
121
|
+
api: Api;
|
|
122
|
+
provider: Provider;
|
|
123
|
+
model: string;
|
|
124
|
+
usage: Usage;
|
|
125
|
+
stopReason: StopReason;
|
|
126
|
+
errorMessage?: string;
|
|
127
|
+
timestamp: number;
|
|
128
|
+
}
|
|
129
|
+
export interface ToolResultMessage<TDetails = any> {
|
|
130
|
+
role: "toolResult";
|
|
131
|
+
toolCallId: string;
|
|
132
|
+
toolName: string;
|
|
133
|
+
content: (TextContent | ImageContent)[];
|
|
134
|
+
details?: TDetails;
|
|
135
|
+
isError: boolean;
|
|
136
|
+
timestamp: number;
|
|
137
|
+
}
|
|
138
|
+
export type Message = UserMessage | AssistantMessage | ToolResultMessage;
|
|
139
|
+
import type { TSchema } from "@sinclair/typebox";
|
|
140
|
+
export interface Tool<TParameters extends TSchema = TSchema> {
|
|
141
|
+
name: string;
|
|
142
|
+
description: string;
|
|
143
|
+
parameters: TParameters;
|
|
144
|
+
}
|
|
145
|
+
export interface Context {
|
|
146
|
+
systemPrompt?: string;
|
|
147
|
+
messages: Message[];
|
|
148
|
+
tools?: Tool[];
|
|
149
|
+
}
|
|
150
|
+
export type AssistantMessageEvent = {
|
|
151
|
+
type: "start";
|
|
152
|
+
partial: AssistantMessage;
|
|
153
|
+
} | {
|
|
154
|
+
type: "text_start";
|
|
155
|
+
contentIndex: number;
|
|
156
|
+
partial: AssistantMessage;
|
|
157
|
+
} | {
|
|
158
|
+
type: "text_delta";
|
|
159
|
+
contentIndex: number;
|
|
160
|
+
delta: string;
|
|
161
|
+
partial: AssistantMessage;
|
|
162
|
+
} | {
|
|
163
|
+
type: "text_end";
|
|
164
|
+
contentIndex: number;
|
|
165
|
+
content: string;
|
|
166
|
+
partial: AssistantMessage;
|
|
167
|
+
} | {
|
|
168
|
+
type: "thinking_start";
|
|
169
|
+
contentIndex: number;
|
|
170
|
+
partial: AssistantMessage;
|
|
171
|
+
} | {
|
|
172
|
+
type: "thinking_delta";
|
|
173
|
+
contentIndex: number;
|
|
174
|
+
delta: string;
|
|
175
|
+
partial: AssistantMessage;
|
|
176
|
+
} | {
|
|
177
|
+
type: "thinking_end";
|
|
178
|
+
contentIndex: number;
|
|
179
|
+
content: string;
|
|
180
|
+
partial: AssistantMessage;
|
|
181
|
+
} | {
|
|
182
|
+
type: "toolcall_start";
|
|
183
|
+
contentIndex: number;
|
|
184
|
+
partial: AssistantMessage;
|
|
185
|
+
} | {
|
|
186
|
+
type: "toolcall_delta";
|
|
187
|
+
contentIndex: number;
|
|
188
|
+
delta: string;
|
|
189
|
+
partial: AssistantMessage;
|
|
190
|
+
} | {
|
|
191
|
+
type: "toolcall_end";
|
|
192
|
+
contentIndex: number;
|
|
193
|
+
toolCall: ToolCall;
|
|
194
|
+
partial: AssistantMessage;
|
|
195
|
+
} | {
|
|
196
|
+
type: "done";
|
|
197
|
+
reason: Extract<StopReason, "stop" | "length" | "toolUse">;
|
|
198
|
+
message: AssistantMessage;
|
|
199
|
+
} | {
|
|
200
|
+
type: "error";
|
|
201
|
+
reason: Extract<StopReason, "aborted" | "error">;
|
|
202
|
+
error: AssistantMessage;
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Compatibility settings for OpenAI-compatible completions APIs.
|
|
206
|
+
* Use this to override URL-based auto-detection for custom providers.
|
|
207
|
+
*/
|
|
208
|
+
export interface OpenAICompletionsCompat {
|
|
209
|
+
/** Whether the provider supports the `store` field. Default: auto-detected from URL. */
|
|
210
|
+
supportsStore?: boolean;
|
|
211
|
+
/** Whether the provider supports the `developer` role (vs `system`). Default: auto-detected from URL. */
|
|
212
|
+
supportsDeveloperRole?: boolean;
|
|
213
|
+
/** Whether the provider supports `reasoning_effort`. Default: auto-detected from URL. */
|
|
214
|
+
supportsReasoningEffort?: boolean;
|
|
215
|
+
/** Whether the provider supports `stream_options: { include_usage: true }` for token usage in streaming responses. Default: true. */
|
|
216
|
+
supportsUsageInStreaming?: boolean;
|
|
217
|
+
/** Which field to use for max tokens. Default: auto-detected from URL. */
|
|
218
|
+
maxTokensField?: "max_completion_tokens" | "max_tokens";
|
|
219
|
+
/** Whether tool results require the `name` field. Default: auto-detected from URL. */
|
|
220
|
+
requiresToolResultName?: boolean;
|
|
221
|
+
/** Whether a user message after tool results requires an assistant message in between. Default: auto-detected from URL. */
|
|
222
|
+
requiresAssistantAfterToolResult?: boolean;
|
|
223
|
+
/** Whether thinking blocks must be converted to text blocks with <thinking> delimiters. Default: auto-detected from URL. */
|
|
224
|
+
requiresThinkingAsText?: boolean;
|
|
225
|
+
/** Whether tool call IDs must be normalized to Mistral format (exactly 9 alphanumeric chars). Default: auto-detected from URL. */
|
|
226
|
+
requiresMistralToolIds?: boolean;
|
|
227
|
+
/** Format for reasoning/thinking parameter. "openai" uses reasoning_effort, "zai" uses thinking: { type: "enabled" }, "qwen" uses enable_thinking: boolean. Default: "openai". */
|
|
228
|
+
thinkingFormat?: "openai" | "zai" | "qwen";
|
|
229
|
+
/** OpenRouter-specific routing preferences. Only used when baseUrl points to OpenRouter. */
|
|
230
|
+
openRouterRouting?: OpenRouterRouting;
|
|
231
|
+
/** Vercel AI Gateway routing preferences. Only used when baseUrl points to Vercel AI Gateway. */
|
|
232
|
+
vercelGatewayRouting?: VercelGatewayRouting;
|
|
233
|
+
/** Whether the provider supports the `strict` field in tool definitions. Default: true. */
|
|
234
|
+
supportsStrictMode?: boolean;
|
|
235
|
+
}
|
|
236
|
+
/** Compatibility settings for OpenAI Responses APIs. */
|
|
237
|
+
export interface OpenAIResponsesCompat {
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* OpenRouter provider routing preferences.
|
|
241
|
+
* Controls which upstream providers OpenRouter routes requests to.
|
|
242
|
+
* @see https://openrouter.ai/docs/provider-routing
|
|
243
|
+
*/
|
|
244
|
+
export interface OpenRouterRouting {
|
|
245
|
+
/** List of provider slugs to exclusively use for this request (e.g., ["amazon-bedrock", "anthropic"]). */
|
|
246
|
+
only?: string[];
|
|
247
|
+
/** List of provider slugs to try in order (e.g., ["anthropic", "openai"]). */
|
|
248
|
+
order?: string[];
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Vercel AI Gateway routing preferences.
|
|
252
|
+
* Controls which upstream providers the gateway routes requests to.
|
|
253
|
+
* @see https://vercel.com/docs/ai-gateway/models-and-providers/provider-options
|
|
254
|
+
*/
|
|
255
|
+
export interface VercelGatewayRouting {
|
|
256
|
+
/** List of provider slugs to exclusively use for this request (e.g., ["bedrock", "anthropic"]). */
|
|
257
|
+
only?: string[];
|
|
258
|
+
/** List of provider slugs to try in order (e.g., ["anthropic", "openai"]). */
|
|
259
|
+
order?: string[];
|
|
260
|
+
}
|
|
261
|
+
export interface Model<TApi extends Api> {
|
|
262
|
+
id: string;
|
|
263
|
+
name: string;
|
|
264
|
+
api: TApi;
|
|
265
|
+
provider: Provider;
|
|
266
|
+
baseUrl: string;
|
|
267
|
+
reasoning: boolean;
|
|
268
|
+
input: ("text" | "image")[];
|
|
269
|
+
cost: {
|
|
270
|
+
input: number;
|
|
271
|
+
output: number;
|
|
272
|
+
cacheRead: number;
|
|
273
|
+
cacheWrite: number;
|
|
274
|
+
cacheWrite1h?: number;
|
|
275
|
+
};
|
|
276
|
+
contextWindow: number;
|
|
277
|
+
maxTokens: number;
|
|
278
|
+
headers?: Record<string, string>;
|
|
279
|
+
/** Compatibility overrides for OpenAI-compatible APIs. If not set, auto-detected from baseUrl. */
|
|
280
|
+
compat?: TApi extends "openai-completions" ? OpenAICompletionsCompat : TApi extends "openai-responses" ? OpenAIResponsesCompat : never;
|
|
281
|
+
}
|
|
282
|
+
//# sourceMappingURL=types.d.ts.map
|