billion-context 0.1.70-pr.459.276 → 0.1.70
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/dist/index.js +10 -127
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -50393,44 +50393,6 @@ function stripResponsesText(obj) {
|
|
|
50393
50393
|
}
|
|
50394
50394
|
return obj;
|
|
50395
50395
|
}
|
|
50396
|
-
function stripOpenAIText(obj) {
|
|
50397
|
-
if (!obj || typeof obj !== "object") return obj;
|
|
50398
|
-
const o = obj;
|
|
50399
|
-
const choices = o["choices"];
|
|
50400
|
-
if (!Array.isArray(choices)) return obj;
|
|
50401
|
-
o["choices"] = choices.map((c) => {
|
|
50402
|
-
if (!c || typeof c !== "object") return c;
|
|
50403
|
-
const choice = { ...c };
|
|
50404
|
-
const msg = choice["message"];
|
|
50405
|
-
if (!msg || typeof msg !== "object") return choice;
|
|
50406
|
-
const message = { ...msg };
|
|
50407
|
-
const content = message["content"];
|
|
50408
|
-
if (typeof content === "string" && containsRenderTagText(content)) {
|
|
50409
|
-
message["content"] = stripAcpTags(content);
|
|
50410
|
-
} else if (Array.isArray(content)) {
|
|
50411
|
-
message["content"] = stripParts(content);
|
|
50412
|
-
}
|
|
50413
|
-
choice["message"] = message;
|
|
50414
|
-
return choice;
|
|
50415
|
-
});
|
|
50416
|
-
return obj;
|
|
50417
|
-
}
|
|
50418
|
-
function stripAnthropicText(obj) {
|
|
50419
|
-
if (!obj || typeof obj !== "object") return obj;
|
|
50420
|
-
const o = obj;
|
|
50421
|
-
const content = o["content"];
|
|
50422
|
-
if (!Array.isArray(content)) return obj;
|
|
50423
|
-
o["content"] = content.map((blk) => {
|
|
50424
|
-
if (blk && typeof blk === "object" && blk.type === "text" && typeof blk.text === "string") {
|
|
50425
|
-
const b2 = blk;
|
|
50426
|
-
if (containsRenderTagText(b2.text)) {
|
|
50427
|
-
return { ...b2, text: stripAcpTags(b2.text) };
|
|
50428
|
-
}
|
|
50429
|
-
}
|
|
50430
|
-
return blk;
|
|
50431
|
-
});
|
|
50432
|
-
return obj;
|
|
50433
|
-
}
|
|
50434
50396
|
|
|
50435
50397
|
// src/stream.ts
|
|
50436
50398
|
function executeAnthropicProxyTool(toolName, args, ctx) {
|
|
@@ -54217,111 +54179,33 @@ async function pipeThroughWithUsage(stream2, res, session, protocol) {
|
|
|
54217
54179
|
const decoder = new TextDecoder("utf-8");
|
|
54218
54180
|
let buf = "";
|
|
54219
54181
|
const acc = {};
|
|
54220
|
-
const stripText = protocol === "openai" || protocol === "anthropic";
|
|
54221
|
-
const tagFilter = createTagEchoFilter((snippet) => {
|
|
54222
|
-
log("warn", `[tag-echo] stripped model-emitted render tag (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
|
|
54223
|
-
});
|
|
54224
|
-
let lastChunkBase = null;
|
|
54225
|
-
let lastChoiceIndex = 0;
|
|
54226
|
-
let lastBlockIndex = 0;
|
|
54227
|
-
const write = async (s3) => {
|
|
54228
|
-
if (!res.write(Buffer.from(s3, "utf8"))) {
|
|
54229
|
-
await new Promise((r) => res.once("drain", () => r()));
|
|
54230
|
-
}
|
|
54231
|
-
};
|
|
54232
|
-
const flushTail = () => {
|
|
54233
|
-
const tail = tagFilter.flush();
|
|
54234
|
-
if (tail.length === 0) return "";
|
|
54235
|
-
if (protocol === "openai") {
|
|
54236
|
-
const base = lastChunkBase ? { ...lastChunkBase } : {};
|
|
54237
|
-
delete base["usage"];
|
|
54238
|
-
return `data: ${JSON.stringify({ ...base, choices: [{ index: lastChoiceIndex, delta: { content: tail }, finish_reason: null }] })}
|
|
54239
|
-
|
|
54240
|
-
`;
|
|
54241
|
-
}
|
|
54242
|
-
return `event: content_block_delta
|
|
54243
|
-
data: ${JSON.stringify({ type: "content_block_delta", index: lastBlockIndex, delta: { type: "text_delta", text: tail } })}
|
|
54244
|
-
|
|
54245
|
-
`;
|
|
54246
|
-
};
|
|
54247
54182
|
try {
|
|
54248
54183
|
for (; ; ) {
|
|
54249
54184
|
const { done, value } = await reader.read();
|
|
54250
54185
|
if (done) break;
|
|
54251
54186
|
if (value && value.length > 0) {
|
|
54187
|
+
if (!res.write(Buffer.from(value))) {
|
|
54188
|
+
await new Promise((r) => res.once("drain", () => r()));
|
|
54189
|
+
}
|
|
54252
54190
|
buf = normalizeSseLineEndings(buf + decoder.decode(value, { stream: true }));
|
|
54253
54191
|
let idx;
|
|
54254
54192
|
while ((idx = buf.indexOf("\n\n")) !== -1) {
|
|
54255
54193
|
const rawEvent = buf.slice(0, idx);
|
|
54256
54194
|
buf = buf.slice(idx + 2);
|
|
54257
54195
|
const dataLines = rawEvent.split("\n").filter((l) => l.startsWith("data:"));
|
|
54258
|
-
if (dataLines.length === 0)
|
|
54259
|
-
await write(rawEvent + "\n\n");
|
|
54260
|
-
continue;
|
|
54261
|
-
}
|
|
54196
|
+
if (dataLines.length === 0) continue;
|
|
54262
54197
|
const jsonStr = dataLines.map((l) => l.slice(5).replace(/^ /, "")).join("\n").trim();
|
|
54263
|
-
if (!jsonStr || jsonStr === "[DONE]")
|
|
54264
|
-
await write(flushTail() + rawEvent + "\n\n");
|
|
54265
|
-
continue;
|
|
54266
|
-
}
|
|
54267
|
-
let ev;
|
|
54198
|
+
if (!jsonStr || jsonStr === "[DONE]") continue;
|
|
54268
54199
|
try {
|
|
54269
|
-
ev = JSON.parse(jsonStr);
|
|
54200
|
+
const ev = JSON.parse(jsonStr);
|
|
54201
|
+
const sample = usageFromSseEvent(ev);
|
|
54202
|
+
if (sample) mergeUsageSample(acc, sample);
|
|
54270
54203
|
} catch {
|
|
54271
|
-
await write(rawEvent + "\n\n");
|
|
54272
|
-
continue;
|
|
54273
54204
|
}
|
|
54274
|
-
const sample = usageFromSseEvent(ev);
|
|
54275
|
-
if (sample) mergeUsageSample(acc, sample);
|
|
54276
|
-
if (!stripText) {
|
|
54277
|
-
await write(rawEvent + "\n\n");
|
|
54278
|
-
continue;
|
|
54279
|
-
}
|
|
54280
|
-
let dirty = false;
|
|
54281
|
-
let terminal = false;
|
|
54282
|
-
if (protocol === "openai") {
|
|
54283
|
-
const choices = ev["choices"];
|
|
54284
|
-
if (Array.isArray(choices)) {
|
|
54285
|
-
for (let i = 0; i < choices.length; i++) {
|
|
54286
|
-
const choice = choices[i] ?? {};
|
|
54287
|
-
const fr2 = choice["finish_reason"];
|
|
54288
|
-
if (typeof fr2 === "string" && fr2 !== "null") terminal = true;
|
|
54289
|
-
const delta = choice["delta"];
|
|
54290
|
-
const content = delta?.["content"];
|
|
54291
|
-
if (delta && typeof content === "string" && content.length > 0) {
|
|
54292
|
-
lastChunkBase = ev;
|
|
54293
|
-
lastChoiceIndex = i;
|
|
54294
|
-
if (!containsRenderTagText(content) && !tagFilter.pending()) continue;
|
|
54295
|
-
delta["content"] = tagFilter.push(content);
|
|
54296
|
-
dirty = true;
|
|
54297
|
-
}
|
|
54298
|
-
}
|
|
54299
|
-
}
|
|
54300
|
-
} else {
|
|
54301
|
-
const t = ev["type"];
|
|
54302
|
-
if (t === "message_delta" || t === "message_stop") terminal = true;
|
|
54303
|
-
if (t === "content_block_delta") {
|
|
54304
|
-
const delta = ev["delta"];
|
|
54305
|
-
const text = delta?.["text"];
|
|
54306
|
-
if (delta?.["type"] === "text_delta" && typeof text === "string" && text.length > 0) {
|
|
54307
|
-
lastBlockIndex = typeof ev["index"] === "number" ? ev["index"] : 0;
|
|
54308
|
-
if (containsRenderTagText(text) || tagFilter.pending()) {
|
|
54309
|
-
delta["text"] = tagFilter.push(text);
|
|
54310
|
-
dirty = true;
|
|
54311
|
-
}
|
|
54312
|
-
}
|
|
54313
|
-
}
|
|
54314
|
-
}
|
|
54315
|
-
if (terminal) await write(flushTail());
|
|
54316
|
-
await write(dirty ? rebuildEvent(rawEvent, ev) : rawEvent + "\n\n");
|
|
54317
54205
|
}
|
|
54318
54206
|
}
|
|
54319
54207
|
if (res.destroyed || res.writableEnded) break;
|
|
54320
54208
|
}
|
|
54321
|
-
if (!res.destroyed && !res.writableEnded) {
|
|
54322
|
-
const rest = flushTail();
|
|
54323
|
-
if (rest.length > 0) await write(rest);
|
|
54324
|
-
}
|
|
54325
54209
|
if (acc.inputTokens !== void 0 || acc.outputTokens !== void 0 || acc.cachedTokens !== void 0) {
|
|
54326
54210
|
applyUsageSample(session, acc, protocol);
|
|
54327
54211
|
markDirty(session);
|
|
@@ -54469,12 +54353,11 @@ async function pipePluginJson(stream2, res, session, protocol) {
|
|
|
54469
54353
|
}
|
|
54470
54354
|
} catch {
|
|
54471
54355
|
}
|
|
54472
|
-
if (protocol
|
|
54356
|
+
if (protocol === "responses") {
|
|
54473
54357
|
try {
|
|
54474
54358
|
const json = JSON.parse(text);
|
|
54475
54359
|
if (containsRenderTagText(text)) {
|
|
54476
|
-
|
|
54477
|
-
res.end(Buffer.from(JSON.stringify(stripped), "utf8"));
|
|
54360
|
+
res.end(Buffer.from(JSON.stringify(stripResponsesText(json)), "utf8"));
|
|
54478
54361
|
return;
|
|
54479
54362
|
}
|
|
54480
54363
|
} catch {
|