billion-context 0.1.70 → 0.1.71-pr.461.277
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 +188 -14
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -50369,6 +50369,50 @@ function stripItemContent(it2) {
|
|
|
50369
50369
|
}
|
|
50370
50370
|
return it2;
|
|
50371
50371
|
}
|
|
50372
|
+
function stripIfString(v2) {
|
|
50373
|
+
return typeof v2 === "string" ? stripAcpTags(v2) : v2;
|
|
50374
|
+
}
|
|
50375
|
+
function stripOpenaiChatText(obj) {
|
|
50376
|
+
if (!obj || typeof obj !== "object") return obj;
|
|
50377
|
+
const o = obj;
|
|
50378
|
+
if (!Array.isArray(o["choices"])) return obj;
|
|
50379
|
+
o["choices"] = o["choices"].map((c) => {
|
|
50380
|
+
if (!c || typeof c !== "object") return c;
|
|
50381
|
+
const ch = c;
|
|
50382
|
+
for (const holder of ["delta", "message"]) {
|
|
50383
|
+
const h = ch[holder];
|
|
50384
|
+
if (h && typeof h === "object") {
|
|
50385
|
+
const hh = { ...h };
|
|
50386
|
+
hh["content"] = stripIfString(hh["content"]);
|
|
50387
|
+
hh["reasoning_content"] = stripIfString(hh["reasoning_content"]);
|
|
50388
|
+
hh["reasoning"] = stripIfString(hh["reasoning"]);
|
|
50389
|
+
ch[holder] = hh;
|
|
50390
|
+
}
|
|
50391
|
+
}
|
|
50392
|
+
return ch;
|
|
50393
|
+
});
|
|
50394
|
+
return obj;
|
|
50395
|
+
}
|
|
50396
|
+
function stripAnthropicText(obj) {
|
|
50397
|
+
if (!obj || typeof obj !== "object") return obj;
|
|
50398
|
+
const o = obj;
|
|
50399
|
+
const d = o["delta"];
|
|
50400
|
+
if (d && typeof d === "object") {
|
|
50401
|
+
const dd = { ...d };
|
|
50402
|
+
dd["text"] = stripIfString(dd["text"]);
|
|
50403
|
+
dd["thinking"] = stripIfString(dd["thinking"]);
|
|
50404
|
+
o["delta"] = dd;
|
|
50405
|
+
}
|
|
50406
|
+
if (Array.isArray(o["content"])) {
|
|
50407
|
+
o["content"] = o["content"].map((c) => {
|
|
50408
|
+
if (!c || typeof c !== "object") return c;
|
|
50409
|
+
const cc = c;
|
|
50410
|
+
if (typeof cc["text"] !== "string" && typeof cc["thinking"] !== "string") return c;
|
|
50411
|
+
return { ...cc, text: stripIfString(cc["text"]), thinking: stripIfString(cc["thinking"]) };
|
|
50412
|
+
});
|
|
50413
|
+
}
|
|
50414
|
+
return obj;
|
|
50415
|
+
}
|
|
50372
50416
|
function stripResponsesText(obj) {
|
|
50373
50417
|
if (!obj || typeof obj !== "object") return obj;
|
|
50374
50418
|
const o = obj;
|
|
@@ -54174,19 +54218,125 @@ function mergeUsageSample(acc, sample) {
|
|
|
54174
54218
|
if (sample.cachedTokens !== void 0) acc.cachedTokens = sample.cachedTokens;
|
|
54175
54219
|
if (sample.outputTokens !== void 0) acc.outputTokens = sample.outputTokens;
|
|
54176
54220
|
}
|
|
54177
|
-
async function
|
|
54221
|
+
async function pipePluginChatWithStrip(stream2, res, session, protocol, log2) {
|
|
54178
54222
|
const reader = stream2.getReader();
|
|
54179
54223
|
const decoder = new TextDecoder("utf-8");
|
|
54180
54224
|
let buf = "";
|
|
54181
54225
|
const acc = {};
|
|
54226
|
+
const onDrop = (snippet) => {
|
|
54227
|
+
log("warn", `[tag-echo] stripped model-emitted render tag (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
|
|
54228
|
+
log2?.(`[tag-echo] stripped model-emitted render tag from plugin passthrough text`);
|
|
54229
|
+
};
|
|
54230
|
+
const streams = /* @__PURE__ */ new Map();
|
|
54231
|
+
const filterFor = (field, index) => {
|
|
54232
|
+
const key = `${field}:${index}`;
|
|
54233
|
+
let s3 = streams.get(key);
|
|
54234
|
+
if (!s3) {
|
|
54235
|
+
s3 = { filter: createTagEchoFilter(onDrop), field, index };
|
|
54236
|
+
streams.set(key, s3);
|
|
54237
|
+
}
|
|
54238
|
+
return s3;
|
|
54239
|
+
};
|
|
54240
|
+
const anyPending = () => {
|
|
54241
|
+
for (const s3 of streams.values()) if (s3.filter.pending()) return true;
|
|
54242
|
+
return false;
|
|
54243
|
+
};
|
|
54244
|
+
let lastChunkMeta = {};
|
|
54245
|
+
const syntheticTail = (field, index, tail) => {
|
|
54246
|
+
if (protocol === "anthropic") {
|
|
54247
|
+
const deltaType = field === "text" ? "text_delta" : "thinking_delta";
|
|
54248
|
+
return `event: content_block_delta
|
|
54249
|
+
data: ${JSON.stringify({ type: "content_block_delta", index, delta: { type: deltaType, [field]: tail } })}
|
|
54250
|
+
|
|
54251
|
+
`;
|
|
54252
|
+
}
|
|
54253
|
+
return `data: ${JSON.stringify({ ...lastChunkMeta, object: "chat.completion.chunk", choices: [{ index, delta: { [field]: tail } }] })}
|
|
54254
|
+
|
|
54255
|
+
`;
|
|
54256
|
+
};
|
|
54257
|
+
const flushTails = () => {
|
|
54258
|
+
let out = "";
|
|
54259
|
+
for (const s3 of streams.values()) {
|
|
54260
|
+
const tail = s3.filter.flush();
|
|
54261
|
+
if (tail.length > 0) out += syntheticTail(s3.field, s3.index, tail);
|
|
54262
|
+
}
|
|
54263
|
+
return out;
|
|
54264
|
+
};
|
|
54265
|
+
const write = (s3) => {
|
|
54266
|
+
if (!res.write(Buffer.from(s3, "utf8"))) {
|
|
54267
|
+
return new Promise((r) => res.once("drain", () => r()));
|
|
54268
|
+
}
|
|
54269
|
+
};
|
|
54270
|
+
const pushField = (field, index, text) => {
|
|
54271
|
+
const s3 = filterFor(field, index);
|
|
54272
|
+
const clean = s3.filter.push(text);
|
|
54273
|
+
return [clean, clean !== text];
|
|
54274
|
+
};
|
|
54275
|
+
const processOpenai = (ev, rawEvent) => {
|
|
54276
|
+
if (typeof ev["id"] === "string" || typeof ev["model"] === "string") {
|
|
54277
|
+
lastChunkMeta = { id: ev["id"], created: ev["created"], model: ev["model"] };
|
|
54278
|
+
}
|
|
54279
|
+
const choices = ev["choices"];
|
|
54280
|
+
if (!Array.isArray(choices)) {
|
|
54281
|
+
return anyPending() ? flushTails() + rawEvent + "\n\n" : rawEvent + "\n\n";
|
|
54282
|
+
}
|
|
54283
|
+
let rebuilt = null;
|
|
54284
|
+
let droppedText = false;
|
|
54285
|
+
let hadText = false;
|
|
54286
|
+
for (let ci2 = 0; ci2 < choices.length; ci2++) {
|
|
54287
|
+
const ch = choices[ci2];
|
|
54288
|
+
const d = ch?.["delta"];
|
|
54289
|
+
if (!d || typeof d !== "object") continue;
|
|
54290
|
+
const dd = d;
|
|
54291
|
+
for (const field of ["content", "reasoning_content", "reasoning"]) {
|
|
54292
|
+
const v2 = dd[field];
|
|
54293
|
+
if (typeof v2 !== "string") continue;
|
|
54294
|
+
hadText = true;
|
|
54295
|
+
if (!containsRenderTagText(v2) && !anyPending()) continue;
|
|
54296
|
+
const index = typeof ch?.["index"] === "number" ? ch["index"] : ci2;
|
|
54297
|
+
const [clean, changed] = pushField(field, index, v2);
|
|
54298
|
+
if (clean.length === 0) droppedText = true;
|
|
54299
|
+
if (changed) {
|
|
54300
|
+
if (!rebuilt) {
|
|
54301
|
+
rebuilt = { ...ev, choices: choices.map((c) => ({ ...c, delta: { ...c["delta"] } })) };
|
|
54302
|
+
}
|
|
54303
|
+
rebuilt["choices"][ci2]["delta"] = { ...rebuilt["choices"][ci2]["delta"], [field]: clean };
|
|
54304
|
+
}
|
|
54305
|
+
}
|
|
54306
|
+
}
|
|
54307
|
+
if (rebuilt) {
|
|
54308
|
+
if (droppedText && !hadTextOtherThanTextFields(rebuilt["choices"])) {
|
|
54309
|
+
return "";
|
|
54310
|
+
}
|
|
54311
|
+
return rebuildEvent(rawEvent, rebuilt);
|
|
54312
|
+
}
|
|
54313
|
+
if (!hadText && anyPending()) return flushTails() + rawEvent + "\n\n";
|
|
54314
|
+
return rawEvent + "\n\n";
|
|
54315
|
+
};
|
|
54316
|
+
const processAnthropic = (ev, rawEvent) => {
|
|
54317
|
+
if (ev["type"] !== "content_block_delta") {
|
|
54318
|
+
return anyPending() ? flushTails() + rawEvent + "\n\n" : rawEvent + "\n\n";
|
|
54319
|
+
}
|
|
54320
|
+
const d = ev["delta"];
|
|
54321
|
+
const index = typeof ev["index"] === "number" ? ev["index"] : 0;
|
|
54322
|
+
const field = d?.["type"] === "thinking_delta" ? "thinking" : d?.["type"] === "text_delta" ? "text" : null;
|
|
54323
|
+
if (field === null || typeof d?.[field] !== "string") {
|
|
54324
|
+
return rawEvent + "\n\n";
|
|
54325
|
+
}
|
|
54326
|
+
const raw = d[field];
|
|
54327
|
+
if (!containsRenderTagText(raw) && !anyPending()) {
|
|
54328
|
+
return rawEvent + "\n\n";
|
|
54329
|
+
}
|
|
54330
|
+
const [clean, changed] = pushField(field, index, raw);
|
|
54331
|
+
if (!changed) return rawEvent + "\n\n";
|
|
54332
|
+
if (clean.length === 0 && Object.keys(d ?? {}).length <= 2) return "";
|
|
54333
|
+
return rebuildEvent(rawEvent, { ...ev, delta: { ...d, [field]: clean } });
|
|
54334
|
+
};
|
|
54182
54335
|
try {
|
|
54183
54336
|
for (; ; ) {
|
|
54184
54337
|
const { done, value } = await reader.read();
|
|
54185
54338
|
if (done) break;
|
|
54186
54339
|
if (value && value.length > 0) {
|
|
54187
|
-
if (!res.write(Buffer.from(value))) {
|
|
54188
|
-
await new Promise((r) => res.once("drain", () => r()));
|
|
54189
|
-
}
|
|
54190
54340
|
buf = normalizeSseLineEndings(buf + decoder.decode(value, { stream: true }));
|
|
54191
54341
|
let idx;
|
|
54192
54342
|
while ((idx = buf.indexOf("\n\n")) !== -1) {
|
|
@@ -54195,17 +54345,28 @@ async function pipeThroughWithUsage(stream2, res, session, protocol) {
|
|
|
54195
54345
|
const dataLines = rawEvent.split("\n").filter((l) => l.startsWith("data:"));
|
|
54196
54346
|
if (dataLines.length === 0) continue;
|
|
54197
54347
|
const jsonStr = dataLines.map((l) => l.slice(5).replace(/^ /, "")).join("\n").trim();
|
|
54198
|
-
if (!jsonStr
|
|
54348
|
+
if (!jsonStr) continue;
|
|
54349
|
+
if (jsonStr === "[DONE]") {
|
|
54350
|
+
await write(flushTails() + rawEvent + "\n\n");
|
|
54351
|
+
continue;
|
|
54352
|
+
}
|
|
54353
|
+
let ev;
|
|
54199
54354
|
try {
|
|
54200
|
-
|
|
54201
|
-
const sample = usageFromSseEvent(ev);
|
|
54202
|
-
if (sample) mergeUsageSample(acc, sample);
|
|
54355
|
+
ev = JSON.parse(jsonStr);
|
|
54203
54356
|
} catch {
|
|
54357
|
+
await write(rawEvent + "\n\n");
|
|
54358
|
+
continue;
|
|
54204
54359
|
}
|
|
54360
|
+
const sample = usageFromSseEvent(ev);
|
|
54361
|
+
if (sample) mergeUsageSample(acc, sample);
|
|
54362
|
+
const out = protocol === "anthropic" ? processAnthropic(ev, rawEvent) : processOpenai(ev, rawEvent);
|
|
54363
|
+
if (out.length > 0) await write(out);
|
|
54205
54364
|
}
|
|
54206
54365
|
}
|
|
54207
54366
|
if (res.destroyed || res.writableEnded) break;
|
|
54208
54367
|
}
|
|
54368
|
+
const rest = flushTails();
|
|
54369
|
+
if (rest.length > 0 && !res.destroyed && !res.writableEnded) await write(rest);
|
|
54209
54370
|
if (acc.inputTokens !== void 0 || acc.outputTokens !== void 0 || acc.cachedTokens !== void 0) {
|
|
54210
54371
|
applyUsageSample(session, acc, protocol);
|
|
54211
54372
|
markDirty(session);
|
|
@@ -54215,6 +54376,20 @@ async function pipeThroughWithUsage(stream2, res, session, protocol) {
|
|
|
54215
54376
|
res.end();
|
|
54216
54377
|
}
|
|
54217
54378
|
}
|
|
54379
|
+
function hadTextOtherThanTextFields(choices) {
|
|
54380
|
+
if (!Array.isArray(choices)) return true;
|
|
54381
|
+
for (const c of choices) {
|
|
54382
|
+
if (!c || typeof c !== "object") continue;
|
|
54383
|
+
const ch = c;
|
|
54384
|
+
if (typeof ch["finish_reason"] === "string") return true;
|
|
54385
|
+
const d = ch["delta"];
|
|
54386
|
+
if (!d) continue;
|
|
54387
|
+
for (const k2 of Object.keys(d)) {
|
|
54388
|
+
if (k2 !== "content" && k2 !== "reasoning_content" && k2 !== "reasoning") return true;
|
|
54389
|
+
}
|
|
54390
|
+
}
|
|
54391
|
+
return false;
|
|
54392
|
+
}
|
|
54218
54393
|
async function pipePluginResponsesWithStrip(stream2, res, session, log2) {
|
|
54219
54394
|
const reader = stream2.getReader();
|
|
54220
54395
|
const decoder = new TextDecoder("utf-8");
|
|
@@ -54353,13 +54528,12 @@ async function pipePluginJson(stream2, res, session, protocol) {
|
|
|
54353
54528
|
}
|
|
54354
54529
|
} catch {
|
|
54355
54530
|
}
|
|
54356
|
-
if (
|
|
54531
|
+
if (containsRenderTagText(text)) {
|
|
54357
54532
|
try {
|
|
54358
54533
|
const json = JSON.parse(text);
|
|
54359
|
-
|
|
54360
|
-
|
|
54361
|
-
|
|
54362
|
-
}
|
|
54534
|
+
const stripped = protocol === "responses" ? stripResponsesText(json) : protocol === "anthropic" ? stripAnthropicText(json) : stripOpenaiChatText(json);
|
|
54535
|
+
res.end(Buffer.from(JSON.stringify(stripped), "utf8"));
|
|
54536
|
+
return;
|
|
54363
54537
|
} catch {
|
|
54364
54538
|
}
|
|
54365
54539
|
}
|
|
@@ -57329,7 +57503,7 @@ ${hdrText}
|
|
|
57329
57503
|
if (prepared.protocol === "responses") {
|
|
57330
57504
|
await pipePluginResponsesWithStrip(upstream.body, res, prepared.session, (msg) => log2("info", `[${prepared.session.id}] ${msg}`));
|
|
57331
57505
|
} else {
|
|
57332
|
-
await
|
|
57506
|
+
await pipePluginChatWithStrip(upstream.body, res, prepared.session, prepared.protocol, (msg) => log2("info", `[${prepared.session.id}] ${msg}`));
|
|
57333
57507
|
}
|
|
57334
57508
|
} else {
|
|
57335
57509
|
await pipePluginJson(upstream.body, res, prepared.session, prepared.protocol);
|