billion-context 0.1.13 → 0.1.15
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 +81 -81
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -38,6 +38,65 @@ function defaultLogFile() {
|
|
|
38
38
|
return path.join(stateDir(), "bili.log");
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
// src/logger.ts
|
|
42
|
+
import { createWriteStream, mkdirSync, statSync, renameSync } from "fs";
|
|
43
|
+
import path2 from "path";
|
|
44
|
+
var MAX_BYTES = 10 * 1024 * 1024;
|
|
45
|
+
var stream;
|
|
46
|
+
var logPath;
|
|
47
|
+
var bytesWritten = 0;
|
|
48
|
+
function open(pathStr) {
|
|
49
|
+
mkdirSync(path2.dirname(pathStr), { recursive: true });
|
|
50
|
+
let existingSize = 0;
|
|
51
|
+
try {
|
|
52
|
+
existingSize = statSync(pathStr).size;
|
|
53
|
+
if (existingSize >= MAX_BYTES) {
|
|
54
|
+
rotate(pathStr);
|
|
55
|
+
existingSize = 0;
|
|
56
|
+
}
|
|
57
|
+
} catch {
|
|
58
|
+
}
|
|
59
|
+
const s = createWriteStream(pathStr, { flags: "a" });
|
|
60
|
+
bytesWritten = existingSize;
|
|
61
|
+
return s;
|
|
62
|
+
}
|
|
63
|
+
function rotate(pathStr) {
|
|
64
|
+
try {
|
|
65
|
+
renameSync(pathStr, pathStr + ".old");
|
|
66
|
+
} catch {
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function configureLogger(file) {
|
|
70
|
+
if (!file || file === "off") {
|
|
71
|
+
logPath = void 0;
|
|
72
|
+
stream = void 0;
|
|
73
|
+
return void 0;
|
|
74
|
+
}
|
|
75
|
+
logPath = file;
|
|
76
|
+
stream = open(file);
|
|
77
|
+
return file;
|
|
78
|
+
}
|
|
79
|
+
var log = (level, msg2) => {
|
|
80
|
+
const ts = (/* @__PURE__ */ new Date()).toISOString();
|
|
81
|
+
const line = `${ts} [${level}] ${msg2}
|
|
82
|
+
`;
|
|
83
|
+
process.stderr.write(line);
|
|
84
|
+
if (stream && logPath) {
|
|
85
|
+
if (bytesWritten >= MAX_BYTES) {
|
|
86
|
+
stream.end();
|
|
87
|
+
rotate(logPath);
|
|
88
|
+
stream = open(logPath);
|
|
89
|
+
}
|
|
90
|
+
stream.write(line);
|
|
91
|
+
bytesWritten += Buffer.byteLength(line);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
function closeLogger() {
|
|
95
|
+
stream?.end();
|
|
96
|
+
stream = void 0;
|
|
97
|
+
bytesWritten = 0;
|
|
98
|
+
}
|
|
99
|
+
|
|
41
100
|
// src/config.ts
|
|
42
101
|
function safeReadJson(path6) {
|
|
43
102
|
try {
|
|
@@ -45,7 +104,7 @@ function safeReadJson(path6) {
|
|
|
45
104
|
return JSON.parse(raw);
|
|
46
105
|
} catch (e) {
|
|
47
106
|
if (e.code !== "ENOENT") {
|
|
48
|
-
|
|
107
|
+
log("error", `[acp-config] failed to parse ${path6}: ${String(e)}`);
|
|
49
108
|
}
|
|
50
109
|
return void 0;
|
|
51
110
|
}
|
|
@@ -684,9 +743,9 @@ import { createInitialState as createInitialState2 } from "acp-kernel";
|
|
|
684
743
|
|
|
685
744
|
// src/persist.ts
|
|
686
745
|
import { promises as fs } from "fs";
|
|
687
|
-
import { existsSync, mkdirSync, readFileSync as readFileSync2, renameSync, unlinkSync, writeFileSync } from "fs";
|
|
746
|
+
import { existsSync, mkdirSync as mkdirSync2, readFileSync as readFileSync2, renameSync as renameSync2, unlinkSync, writeFileSync } from "fs";
|
|
688
747
|
import { createHash as createHash2 } from "crypto";
|
|
689
|
-
import * as
|
|
748
|
+
import * as path3 from "path";
|
|
690
749
|
import { createInitialState } from "acp-kernel";
|
|
691
750
|
var PERSIST_VERSION = 1;
|
|
692
751
|
function mergeState(parsed) {
|
|
@@ -712,7 +771,7 @@ function hostLabel(upstreamOrigin) {
|
|
|
712
771
|
function relPathFor(id, protocol, upstreamOrigin) {
|
|
713
772
|
const proto = protocol ?? "_unknown";
|
|
714
773
|
const host = protocol ? hostLabel(upstreamOrigin) + "_" : "";
|
|
715
|
-
return
|
|
774
|
+
return path3.join(proto, `${host}${createHash2("sha256").update(id, "utf8").digest("hex").slice(0, 24)}.json`);
|
|
716
775
|
}
|
|
717
776
|
function legacyFileNameFor(id) {
|
|
718
777
|
return createHash2("sha256").update(id, "utf8").digest("hex").slice(0, 24) + ".json";
|
|
@@ -732,13 +791,13 @@ var SessionStore = class {
|
|
|
732
791
|
this.log = opts?.log ?? defaultLogger;
|
|
733
792
|
}
|
|
734
793
|
filePath(id, protocol, upstreamOrigin) {
|
|
735
|
-
return
|
|
794
|
+
return path3.join(this.dir, relPathFor(id, protocol, upstreamOrigin));
|
|
736
795
|
}
|
|
737
796
|
/** A unique temp path per write (per process). Two overlapping writes for
|
|
738
797
|
* the same session must not share a temp file, or one rename invalidates
|
|
739
798
|
* the other. */
|
|
740
799
|
tempPath(id) {
|
|
741
|
-
return
|
|
800
|
+
return path3.join(this.dir, `.tmp-${legacyFileNameFor(id)}-${process.pid}-${this.tmpSeq++}`);
|
|
742
801
|
}
|
|
743
802
|
/** Bulk-load every persisted session from disk into a map keyed by the
|
|
744
803
|
* REAL session id (read from the file body, not the filename). Called once
|
|
@@ -762,7 +821,7 @@ var SessionStore = class {
|
|
|
762
821
|
}
|
|
763
822
|
for (const e of entries) {
|
|
764
823
|
if (e.name.startsWith(".tmp-")) continue;
|
|
765
|
-
const full =
|
|
824
|
+
const full = path3.join(dir, e.name);
|
|
766
825
|
if (e.isDirectory()) {
|
|
767
826
|
await walk(full);
|
|
768
827
|
} else if (e.isFile() && e.name.endsWith(".json")) {
|
|
@@ -772,13 +831,13 @@ var SessionStore = class {
|
|
|
772
831
|
};
|
|
773
832
|
await walk(this.dir);
|
|
774
833
|
for (const full of files) {
|
|
775
|
-
const name =
|
|
834
|
+
const name = path3.basename(full);
|
|
776
835
|
try {
|
|
777
836
|
const parsed = JSON.parse(await fs.readFile(full, "utf8"));
|
|
778
837
|
if (!isValidRecord(parsed)) continue;
|
|
779
838
|
const proto = parsed.protocol;
|
|
780
839
|
const origin = parsed.upstreamOrigin;
|
|
781
|
-
const expectedNamespaced =
|
|
840
|
+
const expectedNamespaced = path3.basename(relPathFor(parsed.id, proto, origin));
|
|
782
841
|
const expectedLegacy = legacyFileNameFor(parsed.id);
|
|
783
842
|
if (name !== expectedNamespaced && name !== expectedLegacy) {
|
|
784
843
|
this.log("warn", `[persist] skipping ${full}: filename does not match body id (expected ${expectedNamespaced})`);
|
|
@@ -833,7 +892,7 @@ var SessionStore = class {
|
|
|
833
892
|
const record = buildRecord(session);
|
|
834
893
|
const file = this.filePath(session.id, session.protocol, session.upstreamOrigin);
|
|
835
894
|
try {
|
|
836
|
-
await fs.mkdir(
|
|
895
|
+
await fs.mkdir(path3.dirname(file), { recursive: true });
|
|
837
896
|
} catch (e) {
|
|
838
897
|
this.log("warn", `[persist] could not create session dir ${this.dir}: ${msg(e)}`);
|
|
839
898
|
}
|
|
@@ -867,14 +926,14 @@ var SessionStore = class {
|
|
|
867
926
|
const record = buildRecord(session);
|
|
868
927
|
const file = this.filePath(session.id, session.protocol, session.upstreamOrigin);
|
|
869
928
|
try {
|
|
870
|
-
|
|
929
|
+
mkdirSync2(path3.dirname(file), { recursive: true });
|
|
871
930
|
} catch (e) {
|
|
872
931
|
this.log("warn", `[persist] could not create session dir ${this.dir}: ${msg(e)}`);
|
|
873
932
|
}
|
|
874
933
|
const tmp = this.tempPath(session.id);
|
|
875
934
|
try {
|
|
876
935
|
writeFileSync(tmp, JSON.stringify(record), "utf8");
|
|
877
|
-
|
|
936
|
+
renameSync2(tmp, file);
|
|
878
937
|
return true;
|
|
879
938
|
} catch (e) {
|
|
880
939
|
this.log("error", `[persist] flushSync FAILED for ${session.id}: ${msg(e)} \u2014 session NOT evicted to prevent loss`);
|
|
@@ -972,12 +1031,12 @@ function persistEnabled() {
|
|
|
972
1031
|
return true;
|
|
973
1032
|
}
|
|
974
1033
|
function defaultLogger(level, m) {
|
|
975
|
-
|
|
1034
|
+
log(level, m);
|
|
976
1035
|
}
|
|
977
1036
|
var _store = null;
|
|
978
1037
|
function getStore() {
|
|
979
1038
|
if (!_store) {
|
|
980
|
-
_store = new SessionStore({ enabled: persistEnabled() });
|
|
1039
|
+
_store = new SessionStore({ enabled: persistEnabled(), log: defaultLogger });
|
|
981
1040
|
}
|
|
982
1041
|
return _store;
|
|
983
1042
|
}
|
|
@@ -1117,17 +1176,17 @@ var COMPRESS_TOOL = {
|
|
|
1117
1176
|
};
|
|
1118
1177
|
function parseCompressInput(input) {
|
|
1119
1178
|
if (!input || typeof input !== "object") {
|
|
1120
|
-
|
|
1179
|
+
log("warn", `[acp-compress-input] rejected: not object (${typeof input})`);
|
|
1121
1180
|
return [];
|
|
1122
1181
|
}
|
|
1123
1182
|
const obj = input;
|
|
1124
1183
|
if (Array.isArray(obj.content)) {
|
|
1125
1184
|
const out = obj.content.map((r) => toRange(r)).filter((r) => r !== null);
|
|
1126
|
-
if (out.length === 0)
|
|
1185
|
+
if (out.length === 0) log("warn", `[acp-compress-input] content array but 0 valid ranges. keys per item: ${obj.content.map((c) => Object.keys(c ?? {}).join(",")).join(" | ")}`);
|
|
1127
1186
|
return out;
|
|
1128
1187
|
}
|
|
1129
1188
|
const single = toRange(obj);
|
|
1130
|
-
if (!single)
|
|
1189
|
+
if (!single) log("warn", `[acp-compress-input] no content array, single-parse failed. top keys: ${Object.keys(obj).join(",")}`);
|
|
1131
1190
|
return single ? [single] : [];
|
|
1132
1191
|
}
|
|
1133
1192
|
function toRange(r) {
|
|
@@ -1549,7 +1608,7 @@ import {
|
|
|
1549
1608
|
collectBlockContent as collectBlockContent2,
|
|
1550
1609
|
deactivateBlock
|
|
1551
1610
|
} from "acp-kernel";
|
|
1552
|
-
import { mkdirSync as
|
|
1611
|
+
import { mkdirSync as mkdirSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
1553
1612
|
import { dirname as dirname2, join as join2 } from "path";
|
|
1554
1613
|
import { tmpdir } from "os";
|
|
1555
1614
|
function resolveDecompress(args, ctx) {
|
|
@@ -1582,7 +1641,7 @@ function resolveDecompress(args, ctx) {
|
|
|
1582
1641
|
const outPath = body.length > 1e4 ? join2(tmpdir(), `acp-decompress-${safeBlockId}-${Date.now()}.txt`) : null;
|
|
1583
1642
|
if (outPath) {
|
|
1584
1643
|
try {
|
|
1585
|
-
|
|
1644
|
+
mkdirSync3(dirname2(outPath), { recursive: true });
|
|
1586
1645
|
writeFileSync2(outPath, body, "utf8");
|
|
1587
1646
|
return `${header}
|
|
1588
1647
|
Content (${body.length} chars) written to: ${outPath}
|
|
@@ -1597,65 +1656,6 @@ ${body.slice(0, 4e3)}...`;
|
|
|
1597
1656
|
${body}`;
|
|
1598
1657
|
}
|
|
1599
1658
|
|
|
1600
|
-
// src/logger.ts
|
|
1601
|
-
import { createWriteStream, mkdirSync as mkdirSync3, statSync, renameSync as renameSync2 } from "fs";
|
|
1602
|
-
import path3 from "path";
|
|
1603
|
-
var MAX_BYTES = 10 * 1024 * 1024;
|
|
1604
|
-
var stream;
|
|
1605
|
-
var logPath;
|
|
1606
|
-
var bytesWritten = 0;
|
|
1607
|
-
function open(pathStr) {
|
|
1608
|
-
mkdirSync3(path3.dirname(pathStr), { recursive: true });
|
|
1609
|
-
let existingSize = 0;
|
|
1610
|
-
try {
|
|
1611
|
-
existingSize = statSync(pathStr).size;
|
|
1612
|
-
if (existingSize >= MAX_BYTES) {
|
|
1613
|
-
rotate(pathStr);
|
|
1614
|
-
existingSize = 0;
|
|
1615
|
-
}
|
|
1616
|
-
} catch {
|
|
1617
|
-
}
|
|
1618
|
-
const s = createWriteStream(pathStr, { flags: "a" });
|
|
1619
|
-
bytesWritten = existingSize;
|
|
1620
|
-
return s;
|
|
1621
|
-
}
|
|
1622
|
-
function rotate(pathStr) {
|
|
1623
|
-
try {
|
|
1624
|
-
renameSync2(pathStr, pathStr + ".old");
|
|
1625
|
-
} catch {
|
|
1626
|
-
}
|
|
1627
|
-
}
|
|
1628
|
-
function configureLogger(file) {
|
|
1629
|
-
if (!file || file === "off") {
|
|
1630
|
-
logPath = void 0;
|
|
1631
|
-
stream = void 0;
|
|
1632
|
-
return void 0;
|
|
1633
|
-
}
|
|
1634
|
-
logPath = file;
|
|
1635
|
-
stream = open(file);
|
|
1636
|
-
return file;
|
|
1637
|
-
}
|
|
1638
|
-
var log = (level, msg2) => {
|
|
1639
|
-
const ts = (/* @__PURE__ */ new Date()).toISOString();
|
|
1640
|
-
const line = `${ts} [${level}] ${msg2}
|
|
1641
|
-
`;
|
|
1642
|
-
process.stderr.write(line);
|
|
1643
|
-
if (stream && logPath) {
|
|
1644
|
-
if (bytesWritten >= MAX_BYTES) {
|
|
1645
|
-
stream.end();
|
|
1646
|
-
rotate(logPath);
|
|
1647
|
-
stream = open(logPath);
|
|
1648
|
-
}
|
|
1649
|
-
stream.write(line);
|
|
1650
|
-
bytesWritten += Buffer.byteLength(line);
|
|
1651
|
-
}
|
|
1652
|
-
};
|
|
1653
|
-
function closeLogger() {
|
|
1654
|
-
stream?.end();
|
|
1655
|
-
stream = void 0;
|
|
1656
|
-
bytesWritten = 0;
|
|
1657
|
-
}
|
|
1658
|
-
|
|
1659
1659
|
// src/compress-loop.ts
|
|
1660
1660
|
function executeProxyTool(toolName, args, ctx) {
|
|
1661
1661
|
if (toolName === "compress") {
|
|
@@ -2350,7 +2350,7 @@ async function* compressLoopResponsesStream(initialUpstream, ctx, requestBody, r
|
|
|
2350
2350
|
const allCalls = [...fcByItemId.values()].filter((c) => c.name.length > 0);
|
|
2351
2351
|
const proxyCalls = allCalls.filter((c) => PROXY_TOOL_NAMES.has(c.name));
|
|
2352
2352
|
const realCalls = allCalls.filter((c) => !PROXY_TOOL_NAMES.has(c.name));
|
|
2353
|
-
|
|
2353
|
+
log("debug", `[acp-diag] round ${loopCount} allCalls=[${allCalls.map((c) => c.name).join(",")}] realCalls=[${realCalls.map((c) => c.name).join(",")}] text=${JSON.stringify(contentText.slice(0, 120))}`);
|
|
2354
2354
|
const hasOnlyProxy = proxyCalls.length > 0 && realCalls.length === 0;
|
|
2355
2355
|
if (!hasOnlyProxy) {
|
|
2356
2356
|
let oi = nextOutputIndex;
|
|
@@ -2393,11 +2393,11 @@ async function* compressLoopResponsesStream(initialUpstream, ctx, requestBody, r
|
|
|
2393
2393
|
try {
|
|
2394
2394
|
args = JSON.parse(fc.arguments);
|
|
2395
2395
|
} catch (e) {
|
|
2396
|
-
|
|
2396
|
+
log("warn", `[acp-compress-args] ${fc.name} JSON.parse failed: ${String(e)}. raw arguments (len=${fc.arguments.length}): ${fc.arguments.slice(0, 300)}`);
|
|
2397
2397
|
args = {};
|
|
2398
2398
|
}
|
|
2399
2399
|
if (fc.name === "compress") {
|
|
2400
|
-
|
|
2400
|
+
log("debug", `[acp-compress-args] compress args parsed: ${JSON.stringify(args).slice(0, 400)}`);
|
|
2401
2401
|
}
|
|
2402
2402
|
const result = executeProxyTool2(fc.name, args, ctx);
|
|
2403
2403
|
const preview = result.length > 120 ? result.slice(0, 120) + "..." : result;
|