@cortexkit/pi-magic-context 0.44.3 → 0.45.0
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/embedding-worker.js +47 -0
- package/dist/emscripten-module-0g8f45e6.js +602 -0
- package/dist/emscripten-module-0pekdjxz.js +1532 -0
- package/dist/emscripten-module-e67xwsp3.js +503 -0
- package/dist/emscripten-module-pj1vyb44.js +4559 -0
- package/dist/ffi-3cx1cxpk.js +78 -0
- package/dist/ffi-7bqqe4cd.js +91 -0
- package/dist/ffi-7e6gryj5.js +91 -0
- package/dist/ffi-bpvkebwr.js +78 -0
- package/dist/historian-calibration-extension.js +7 -4
- package/dist/{index-atb1ecv9.js → index-2bw9k8nx.js} +7930 -23026
- package/dist/index-2pbbk3en.js +410 -0
- package/dist/index-4d3cba70.js +10 -0
- package/dist/{index-59dmj4zs.js → index-7n8gb6j3.js} +165 -3
- package/dist/{index-97e5fzy3.js → index-7s5yk3za.js} +1 -1
- package/dist/index-901ekbga.js +244 -0
- package/dist/index-e16q5sd7.js +240 -0
- package/dist/index-g0xfx1c2.js +275 -0
- package/dist/{module-asyncify-2EFITU5U-rkqjhjxd.js → index-g20jdh0p.js} +114 -105
- package/dist/index-j0mbc5vj.js +359 -0
- package/dist/index-k0yd1ph9.js +7774 -0
- package/dist/{index-5fw91enm.js → index-n3g5mwa8.js} +1 -1
- package/dist/index-q1yc6bs0.js +104 -0
- package/dist/index-ttf0cqq8.js +8938 -0
- package/dist/{index-tckvezry.js → index-vxjgsr9m.js} +797 -914
- package/dist/{index-n8kg008j.js → index-w76x3af7.js} +28 -18
- package/dist/index.js +4060 -1695
- package/dist/{message-time-backfill-5yrp95cx.js → message-time-backfill-g83dkh8m.js} +19 -4
- package/dist/migration-worker.js +51 -0
- package/dist/module-ES6BEMUI-3cet1pyp.js +12 -0
- package/dist/module-asyncify-2EFITU5U-c2razzd9.js +6 -0
- package/dist/{read-session-chunk-z2weg25w.js → read-session-chunk-kmt7a6zj.js} +32 -5
- package/dist/rpc-notifications-gm1mdpwh.js +16 -0
- package/dist/{safe-notification-target-y3hg2m7p.js → safe-notification-target-6cn3z99t.js} +2 -2
- package/dist/subagent-entry.js +15 -6
- package/package.json +4 -4
- package/dist/index-b3eqj1g6.js +0 -51
- package/dist/index-rn7gafjr.js +0 -8
- package/dist/index-v6t0tg85.js +0 -13
- package/dist/rpc-notifications-jxy30tgz.js +0 -8
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertSync2,
|
|
3
|
+
EvalFlags2,
|
|
4
|
+
IntrinsicsFlags2,
|
|
5
|
+
JSPromiseStateEnum2,
|
|
6
|
+
GetOwnPropertyNamesFlags2,
|
|
7
|
+
IsEqualOp2,
|
|
8
|
+
setDebugMode2,
|
|
9
|
+
debugLog2,
|
|
10
|
+
errors_exports,
|
|
11
|
+
QuickJSMemoryLeakDetected,
|
|
12
|
+
QuickJSEmscriptenModuleError,
|
|
13
|
+
UsingDisposable2,
|
|
14
|
+
Lifetime2,
|
|
15
|
+
StaticLifetime2,
|
|
16
|
+
WeakLifetime2,
|
|
17
|
+
Scope2,
|
|
18
|
+
createDisposableArray2,
|
|
19
|
+
DisposableSuccess2,
|
|
20
|
+
DisposableFail2,
|
|
21
|
+
DisposableResult2,
|
|
22
|
+
QuickJSDeferredPromise2,
|
|
23
|
+
DefaultIntrinsics2,
|
|
24
|
+
QuickJSContext2,
|
|
25
|
+
QuickJSRuntime2,
|
|
26
|
+
QuickJSWASMModule2,
|
|
27
|
+
QuickJSAsyncContext2,
|
|
28
|
+
QuickJSAsyncRuntime2,
|
|
29
|
+
QuickJSAsyncWASMModule2
|
|
30
|
+
} from "./index-g20jdh0p.js";
|
|
31
|
+
|
|
32
|
+
// ../../node_modules/.bun/quickjs-emscripten-core@0.32.0/node_modules/quickjs-emscripten-core/dist/index.mjs
|
|
33
|
+
async function newQuickJSWASMModuleFromVariant(variantOrPromise) {
|
|
34
|
+
let variant = smartUnwrap(await variantOrPromise), [wasmModuleLoader, QuickJSFFI, { QuickJSWASMModule: QuickJSWASMModule2 }] = await Promise.all([variant.importModuleLoader().then(smartUnwrap), variant.importFFI(), import("./module-ES6BEMUI-3cet1pyp.js").then(smartUnwrap)]), wasmModule = await wasmModuleLoader();
|
|
35
|
+
wasmModule.type = "sync";
|
|
36
|
+
let ffi = new QuickJSFFI(wasmModule);
|
|
37
|
+
return new QuickJSWASMModule2(wasmModule, ffi);
|
|
38
|
+
}
|
|
39
|
+
async function newQuickJSAsyncWASMModuleFromVariant(variantOrPromise) {
|
|
40
|
+
let variant = smartUnwrap(await variantOrPromise), [wasmModuleLoader, QuickJSAsyncFFI, { QuickJSAsyncWASMModule: QuickJSAsyncWASMModule2 }] = await Promise.all([variant.importModuleLoader().then(smartUnwrap), variant.importFFI(), import("./module-asyncify-2EFITU5U-c2razzd9.js").then(smartUnwrap)]), wasmModule = await wasmModuleLoader();
|
|
41
|
+
wasmModule.type = "async";
|
|
42
|
+
let ffi = new QuickJSAsyncFFI(wasmModule);
|
|
43
|
+
return new QuickJSAsyncWASMModule2(wasmModule, ffi);
|
|
44
|
+
}
|
|
45
|
+
function memoizePromiseFactory(fn) {
|
|
46
|
+
let promise;
|
|
47
|
+
return () => promise ?? (promise = fn());
|
|
48
|
+
}
|
|
49
|
+
function smartUnwrap(val) {
|
|
50
|
+
return val && "default" in val && val.default ? val.default && "default" in val.default && val.default.default ? val.default.default : val.default : val;
|
|
51
|
+
}
|
|
52
|
+
function newVariant(baseVariant, options) {
|
|
53
|
+
return { ...baseVariant, async importModuleLoader() {
|
|
54
|
+
let moduleLoader = smartUnwrap(await baseVariant.importModuleLoader());
|
|
55
|
+
return async function() {
|
|
56
|
+
let moduleLoaderArg = options.emscriptenModule ? { ...options.emscriptenModule } : {}, log = options.log ?? ((...args) => debugLog2("newVariant moduleLoader:", ...args)), tapValue = (message, val) => (log(...message, val), val), force = (val) => typeof val == "function" ? val() : val;
|
|
57
|
+
(options.wasmLocation || options.wasmSourceMapLocation || options.locateFile) && (moduleLoaderArg.locateFile = (fileName, relativeTo) => {
|
|
58
|
+
let args = { fileName, relativeTo };
|
|
59
|
+
if (fileName.endsWith(".wasm") && options.wasmLocation !== undefined)
|
|
60
|
+
return tapValue(["locateFile .wasm: provide wasmLocation", args], options.wasmLocation);
|
|
61
|
+
if (fileName.endsWith(".map")) {
|
|
62
|
+
if (options.wasmSourceMapLocation !== undefined)
|
|
63
|
+
return tapValue(["locateFile .map: provide wasmSourceMapLocation", args], options.wasmSourceMapLocation);
|
|
64
|
+
if (options.wasmLocation && !options.locateFile)
|
|
65
|
+
return tapValue(["locateFile .map: infer from wasmLocation", args], options.wasmLocation + ".map");
|
|
66
|
+
}
|
|
67
|
+
return options.locateFile ? tapValue(["locateFile: use provided fn", args], options.locateFile(fileName, relativeTo)) : tapValue(["locateFile: unhandled, passthrough", args], fileName);
|
|
68
|
+
}), options.wasmBinary && (moduleLoaderArg.wasmBinary = await force(options.wasmBinary)), options.wasmMemory && (moduleLoaderArg.wasmMemory = await force(options.wasmMemory));
|
|
69
|
+
let optionsWasmModule = options.wasmModule, modulePromise;
|
|
70
|
+
optionsWasmModule && (moduleLoaderArg.instantiateWasm = async (imports, onSuccess) => {
|
|
71
|
+
modulePromise ?? (modulePromise = Promise.resolve(force(optionsWasmModule)));
|
|
72
|
+
let wasmModule = await modulePromise;
|
|
73
|
+
if (!wasmModule)
|
|
74
|
+
throw new QuickJSEmscriptenModuleError(`options.wasmModule returned ${String(wasmModule)}`);
|
|
75
|
+
let instance = await WebAssembly.instantiate(wasmModule, imports);
|
|
76
|
+
return onSuccess(instance), instance.exports;
|
|
77
|
+
}), moduleLoaderArg.monitorRunDependencies = (left) => {
|
|
78
|
+
log("monitorRunDependencies:", left);
|
|
79
|
+
}, moduleLoaderArg.quickjsEmscriptenInit = () => newMockExtensions(log);
|
|
80
|
+
let resultPromise = moduleLoader(moduleLoaderArg), extensions = moduleLoaderArg.quickjsEmscriptenInit?.(log);
|
|
81
|
+
if (optionsWasmModule && extensions?.receiveWasmOffsetConverter && !extensions.existingWasmOffsetConverter) {
|
|
82
|
+
let wasmBinary = await force(options.wasmBinary) ?? new ArrayBuffer(0);
|
|
83
|
+
modulePromise ?? (modulePromise = Promise.resolve(force(optionsWasmModule)));
|
|
84
|
+
let wasmModule = await modulePromise;
|
|
85
|
+
if (!wasmModule)
|
|
86
|
+
throw new QuickJSEmscriptenModuleError(`options.wasmModule returned ${String(wasmModule)}`);
|
|
87
|
+
extensions.receiveWasmOffsetConverter(wasmBinary, wasmModule);
|
|
88
|
+
}
|
|
89
|
+
if (extensions?.receiveSourceMapJSON) {
|
|
90
|
+
let loadedSourceMapData = await force(options.wasmSourceMapData);
|
|
91
|
+
typeof loadedSourceMapData == "string" ? extensions.receiveSourceMapJSON(JSON.parse(loadedSourceMapData)) : loadedSourceMapData ? extensions.receiveSourceMapJSON(loadedSourceMapData) : extensions.receiveSourceMapJSON({ version: 3, names: [], sources: [], mappings: "" });
|
|
92
|
+
}
|
|
93
|
+
return resultPromise;
|
|
94
|
+
};
|
|
95
|
+
} };
|
|
96
|
+
}
|
|
97
|
+
function newMockExtensions(log) {
|
|
98
|
+
let mockMessage = "mock called, emscripten module may not be initialized yet";
|
|
99
|
+
return { mock: true, removeRunDependency(name) {
|
|
100
|
+
log(`${mockMessage}: removeRunDependency called:`, name);
|
|
101
|
+
}, receiveSourceMapJSON(data) {
|
|
102
|
+
log(`${mockMessage}: receiveSourceMapJSON called:`, data);
|
|
103
|
+
}, WasmOffsetConverter: undefined, receiveWasmOffsetConverter(bytes, mod) {
|
|
104
|
+
log(`${mockMessage}: receiveWasmOffsetConverter called:`, bytes, mod);
|
|
105
|
+
} };
|
|
106
|
+
}
|
|
107
|
+
function isSuccess(successOrFail) {
|
|
108
|
+
return !("error" in successOrFail);
|
|
109
|
+
}
|
|
110
|
+
function isFail(successOrFail) {
|
|
111
|
+
return "error" in successOrFail;
|
|
112
|
+
}
|
|
113
|
+
function shouldInterruptAfterDeadline(deadline) {
|
|
114
|
+
let deadlineAsNumber = typeof deadline == "number" ? deadline : deadline.getTime();
|
|
115
|
+
return function() {
|
|
116
|
+
return Date.now() > deadlineAsNumber;
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
var TestQuickJSWASMModule = class {
|
|
120
|
+
constructor(parent) {
|
|
121
|
+
this.parent = parent;
|
|
122
|
+
this.contexts = new Set;
|
|
123
|
+
this.runtimes = new Set;
|
|
124
|
+
}
|
|
125
|
+
newRuntime(options) {
|
|
126
|
+
let runtime = this.parent.newRuntime({ ...options, ownedLifetimes: [new Lifetime2(undefined, undefined, () => this.runtimes.delete(runtime)), ...options?.ownedLifetimes ?? []] });
|
|
127
|
+
return this.runtimes.add(runtime), runtime;
|
|
128
|
+
}
|
|
129
|
+
newContext(options) {
|
|
130
|
+
let context = this.parent.newContext({ ...options, ownedLifetimes: [new Lifetime2(undefined, undefined, () => this.contexts.delete(context)), ...options?.ownedLifetimes ?? []] });
|
|
131
|
+
return this.contexts.add(context), context;
|
|
132
|
+
}
|
|
133
|
+
evalCode(code, options) {
|
|
134
|
+
return this.parent.evalCode(code, options);
|
|
135
|
+
}
|
|
136
|
+
disposeAll() {
|
|
137
|
+
let allDisposables = [...this.contexts, ...this.runtimes];
|
|
138
|
+
this.runtimes.clear(), this.contexts.clear(), allDisposables.forEach((d) => {
|
|
139
|
+
d.alive && d.dispose();
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
assertNoMemoryAllocated() {
|
|
143
|
+
if (this.getFFI().QTS_RecoverableLeakCheck())
|
|
144
|
+
throw new QuickJSMemoryLeakDetected("Leak sanitizer detected un-freed memory");
|
|
145
|
+
if (this.contexts.size > 0)
|
|
146
|
+
throw new QuickJSMemoryLeakDetected(`${this.contexts.size} contexts leaked`);
|
|
147
|
+
if (this.runtimes.size > 0)
|
|
148
|
+
throw new QuickJSMemoryLeakDetected(`${this.runtimes.size} runtimes leaked`);
|
|
149
|
+
}
|
|
150
|
+
getWasmMemory() {
|
|
151
|
+
return this.parent.getWasmMemory();
|
|
152
|
+
}
|
|
153
|
+
getFFI() {
|
|
154
|
+
return this.parent.getFFI();
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// ../../node_modules/.bun/@jitl+quickjs-wasmfile-debug-sync@0.32.0/node_modules/@jitl/quickjs-wasmfile-debug-sync/dist/index.mjs
|
|
159
|
+
var variant = { type: "sync", importFFI: () => import("./ffi-bpvkebwr.js").then((mod) => mod.QuickJSFFI), importModuleLoader: () => import("./emscripten-module-pj1vyb44.js").then((mod) => mod.default) };
|
|
160
|
+
var src_default = variant;
|
|
161
|
+
|
|
162
|
+
// ../../node_modules/.bun/@jitl+quickjs-wasmfile-release-sync@0.32.0/node_modules/@jitl/quickjs-wasmfile-release-sync/dist/index.mjs
|
|
163
|
+
var variant2 = { type: "sync", importFFI: () => import("./ffi-3cx1cxpk.js").then((mod) => mod.QuickJSFFI), importModuleLoader: () => import("./emscripten-module-e67xwsp3.js").then((mod) => mod.default) };
|
|
164
|
+
var src_default2 = variant2;
|
|
165
|
+
|
|
166
|
+
// ../../node_modules/.bun/@jitl+quickjs-wasmfile-debug-asyncify@0.32.0/node_modules/@jitl/quickjs-wasmfile-debug-asyncify/dist/index.mjs
|
|
167
|
+
var variant3 = { type: "async", importFFI: () => import("./ffi-7e6gryj5.js").then((mod) => mod.QuickJSAsyncFFI), importModuleLoader: () => import("./emscripten-module-0pekdjxz.js").then((mod) => mod.default) };
|
|
168
|
+
var src_default3 = variant3;
|
|
169
|
+
|
|
170
|
+
// ../../node_modules/.bun/@jitl+quickjs-wasmfile-release-asyncify@0.32.0/node_modules/@jitl/quickjs-wasmfile-release-asyncify/dist/index.mjs
|
|
171
|
+
var variant4 = { type: "async", importFFI: () => import("./ffi-7bqqe4cd.js").then((mod) => mod.QuickJSAsyncFFI), importModuleLoader: () => import("./emscripten-module-0g8f45e6.js").then((mod) => mod.default) };
|
|
172
|
+
var src_default4 = variant4;
|
|
173
|
+
|
|
174
|
+
// ../../node_modules/.bun/quickjs-emscripten@0.32.0/node_modules/quickjs-emscripten/dist/chunk-OHAYRCBA.mjs
|
|
175
|
+
async function newQuickJSWASMModule(variantOrPromise = src_default2) {
|
|
176
|
+
return newQuickJSWASMModuleFromVariant(variantOrPromise);
|
|
177
|
+
}
|
|
178
|
+
async function newQuickJSAsyncWASMModule(variantOrPromise = src_default4) {
|
|
179
|
+
return newQuickJSAsyncWASMModuleFromVariant(variantOrPromise);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ../../node_modules/.bun/quickjs-emscripten@0.32.0/node_modules/quickjs-emscripten/dist/index.mjs
|
|
183
|
+
var singleton;
|
|
184
|
+
var singletonPromise;
|
|
185
|
+
async function getQuickJS() {
|
|
186
|
+
return singletonPromise ?? (singletonPromise = newQuickJSWASMModule().then((instance) => (singleton = instance, instance))), await singletonPromise;
|
|
187
|
+
}
|
|
188
|
+
function getQuickJSSync() {
|
|
189
|
+
if (!singleton)
|
|
190
|
+
throw new Error("QuickJS not initialized. Await getQuickJS() at least once.");
|
|
191
|
+
return singleton;
|
|
192
|
+
}
|
|
193
|
+
async function newAsyncRuntime(options) {
|
|
194
|
+
return (await newQuickJSAsyncWASMModule()).newRuntime(options);
|
|
195
|
+
}
|
|
196
|
+
async function newAsyncContext(options) {
|
|
197
|
+
return (await newQuickJSAsyncWASMModule()).newContext(options);
|
|
198
|
+
}
|
|
199
|
+
export {
|
|
200
|
+
src_default3 as DEBUG_ASYNC,
|
|
201
|
+
src_default as DEBUG_SYNC,
|
|
202
|
+
DefaultIntrinsics2 as DefaultIntrinsics,
|
|
203
|
+
DisposableFail2 as DisposableFail,
|
|
204
|
+
DisposableResult2 as DisposableResult,
|
|
205
|
+
DisposableSuccess2 as DisposableSuccess,
|
|
206
|
+
EvalFlags2 as EvalFlags,
|
|
207
|
+
GetOwnPropertyNamesFlags2 as GetOwnPropertyNamesFlags,
|
|
208
|
+
IntrinsicsFlags2 as IntrinsicsFlags,
|
|
209
|
+
IsEqualOp2 as IsEqualOp,
|
|
210
|
+
JSPromiseStateEnum2 as JSPromiseStateEnum,
|
|
211
|
+
Lifetime2 as Lifetime,
|
|
212
|
+
QuickJSAsyncContext2 as QuickJSAsyncContext,
|
|
213
|
+
QuickJSAsyncRuntime2 as QuickJSAsyncRuntime,
|
|
214
|
+
QuickJSAsyncWASMModule2 as QuickJSAsyncWASMModule,
|
|
215
|
+
QuickJSContext2 as QuickJSContext,
|
|
216
|
+
QuickJSDeferredPromise2 as QuickJSDeferredPromise,
|
|
217
|
+
QuickJSRuntime2 as QuickJSRuntime,
|
|
218
|
+
QuickJSWASMModule2 as QuickJSWASMModule,
|
|
219
|
+
src_default4 as RELEASE_ASYNC,
|
|
220
|
+
src_default2 as RELEASE_SYNC,
|
|
221
|
+
Scope2 as Scope,
|
|
222
|
+
StaticLifetime2 as StaticLifetime,
|
|
223
|
+
TestQuickJSWASMModule,
|
|
224
|
+
UsingDisposable2 as UsingDisposable,
|
|
225
|
+
WeakLifetime2 as WeakLifetime,
|
|
226
|
+
assertSync2 as assertSync,
|
|
227
|
+
createDisposableArray2 as createDisposableArray,
|
|
228
|
+
debugLog2 as debugLog,
|
|
229
|
+
errors_exports as errors,
|
|
230
|
+
getQuickJS,
|
|
231
|
+
getQuickJSSync,
|
|
232
|
+
isFail,
|
|
233
|
+
isSuccess,
|
|
234
|
+
memoizePromiseFactory,
|
|
235
|
+
newAsyncContext,
|
|
236
|
+
newAsyncRuntime,
|
|
237
|
+
newQuickJSAsyncWASMModule,
|
|
238
|
+
newQuickJSAsyncWASMModuleFromVariant,
|
|
239
|
+
newQuickJSWASMModule,
|
|
240
|
+
newQuickJSWASMModuleFromVariant,
|
|
241
|
+
newVariant,
|
|
242
|
+
setDebugMode2 as setDebugMode,
|
|
243
|
+
shouldInterruptAfterDeadline
|
|
244
|
+
};
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
// ../plugin/src/shared/sqlite-helpers.ts
|
|
2
|
+
function closeQuietly(db) {
|
|
3
|
+
if (!db)
|
|
4
|
+
return;
|
|
5
|
+
try {
|
|
6
|
+
db.close();
|
|
7
|
+
} catch {}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// ../plugin/src/shared/stable-json.ts
|
|
11
|
+
function stableStringify(value, seen = new WeakSet) {
|
|
12
|
+
if (value === undefined)
|
|
13
|
+
return "undefined";
|
|
14
|
+
if (value === null || typeof value !== "object")
|
|
15
|
+
return JSON.stringify(value) ?? String(value);
|
|
16
|
+
if (seen.has(value))
|
|
17
|
+
return '"[Circular]"';
|
|
18
|
+
seen.add(value);
|
|
19
|
+
if (Array.isArray(value)) {
|
|
20
|
+
return `[${value.map((item) => stableStringify(item, seen)).join(",")}]`;
|
|
21
|
+
}
|
|
22
|
+
const entries = Object.entries(value).sort(([a], [b]) => {
|
|
23
|
+
if (a < b)
|
|
24
|
+
return -1;
|
|
25
|
+
if (a > b)
|
|
26
|
+
return 1;
|
|
27
|
+
return 0;
|
|
28
|
+
});
|
|
29
|
+
return `{${entries.map(([key, child]) => `${JSON.stringify(key)}:${stableStringify(child, seen)}`).join(",")}}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ../plugin/src/shared/opencode-db-path.ts
|
|
33
|
+
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
34
|
+
import { homedir } from "node:os";
|
|
35
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
36
|
+
var cachedResolution = null;
|
|
37
|
+
var lastReadFailure = null;
|
|
38
|
+
var claimedDiagnostics = new Set;
|
|
39
|
+
function openCodeDataDir(env = process.env, dataHome) {
|
|
40
|
+
return join(dataHome ?? env.XDG_DATA_HOME ?? join(homedir(), ".local", "share"), "opencode");
|
|
41
|
+
}
|
|
42
|
+
function environmentKey(dataDir, hostGeneration, channel, env) {
|
|
43
|
+
return [
|
|
44
|
+
hostGeneration,
|
|
45
|
+
dataDir,
|
|
46
|
+
env.OPENCODE_DB ?? "",
|
|
47
|
+
env.OPENCODE_DISABLE_CHANNEL_DB ?? "",
|
|
48
|
+
channel ?? env.OPENCODE_CHANNEL ?? ""
|
|
49
|
+
].join("\x00");
|
|
50
|
+
}
|
|
51
|
+
function channelPath(dataDir, channel) {
|
|
52
|
+
return ["latest", "beta", "prod"].includes(channel) ? join(dataDir, "opencode.db") : join(dataDir, `opencode-${channel}.db`);
|
|
53
|
+
}
|
|
54
|
+
function discoveredCandidateNames(dataDir) {
|
|
55
|
+
const names = ["opencode.db", "opencode-local.db", "opencode-dev.db"];
|
|
56
|
+
try {
|
|
57
|
+
const discovered = readdirSync(dataDir, { withFileTypes: true }).filter((entry) => /^opencode-.+\.db$/.test(entry.name) && !names.includes(entry.name)).map((entry) => entry.name).sort();
|
|
58
|
+
names.push(...discovered);
|
|
59
|
+
} catch {}
|
|
60
|
+
return names;
|
|
61
|
+
}
|
|
62
|
+
function discoverOpenCodeDb(dataDir) {
|
|
63
|
+
const candidates = discoveredCandidateNames(dataDir).map((name, order) => {
|
|
64
|
+
const path = join(dataDir, name);
|
|
65
|
+
try {
|
|
66
|
+
const metadata = statSync(path);
|
|
67
|
+
return metadata.isFile() ? { path, order, mtimeMs: metadata.mtimeMs } : null;
|
|
68
|
+
} catch {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
const existing = candidates.filter((candidate) => candidate !== null).sort((left, right) => right.mtimeMs - left.mtimeMs || left.order - right.order)[0];
|
|
73
|
+
if (!existing) {
|
|
74
|
+
return { path: join(dataDir, "opencode.db"), source: "default", channel: null };
|
|
75
|
+
}
|
|
76
|
+
const name = existing.path.slice(dataDir.length + 1);
|
|
77
|
+
const channel = name === "opencode.db" ? null : name.slice("opencode-".length, -".db".length) || null;
|
|
78
|
+
return { path: existing.path, source: "discovered", channel };
|
|
79
|
+
}
|
|
80
|
+
function resolveV1Fresh(dataDir, env = process.env) {
|
|
81
|
+
const explicit = env.OPENCODE_DB;
|
|
82
|
+
if (explicit !== undefined && explicit.length > 0) {
|
|
83
|
+
if (explicit === ":memory:") {
|
|
84
|
+
return { path: explicit, source: "OPENCODE_DB", channel: null };
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
path: isAbsolute(explicit) ? explicit : join(dataDir, explicit),
|
|
88
|
+
source: "OPENCODE_DB",
|
|
89
|
+
channel: null
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const disableChannelDb = env.OPENCODE_DISABLE_CHANNEL_DB;
|
|
93
|
+
if (disableChannelDb === "1" || disableChannelDb === "true") {
|
|
94
|
+
return { path: join(dataDir, "opencode.db"), source: "default", channel: null };
|
|
95
|
+
}
|
|
96
|
+
const channel = env.OPENCODE_CHANNEL;
|
|
97
|
+
if (channel !== undefined && channel.length > 0) {
|
|
98
|
+
return { path: channelPath(dataDir, channel), source: "channel", channel };
|
|
99
|
+
}
|
|
100
|
+
return discoverOpenCodeDb(dataDir);
|
|
101
|
+
}
|
|
102
|
+
function sourceOpenCodeDatabaseFilename(hostGeneration, channel, env = process.env) {
|
|
103
|
+
if (hostGeneration === "v1") {
|
|
104
|
+
const explicit = env.OPENCODE_DB;
|
|
105
|
+
if (explicit !== undefined && explicit.length > 0)
|
|
106
|
+
return explicit;
|
|
107
|
+
if (env.OPENCODE_DISABLE_CHANNEL_DB === "1" || env.OPENCODE_DISABLE_CHANNEL_DB === "true") {
|
|
108
|
+
return "opencode.db";
|
|
109
|
+
}
|
|
110
|
+
return ["latest", "beta", "prod"].includes(channel) ? "opencode.db" : `opencode-${channel}.db`;
|
|
111
|
+
}
|
|
112
|
+
return env.OPENCODE_DB ?? (["latest", "dev", "beta", "next", "prod"].includes(channel) || env.OPENCODE_DISABLE_CHANNEL_DB === "1" || env.OPENCODE_DISABLE_CHANNEL_DB === "true" ? "opencode.db" : `opencode-${channel.replace(/[^a-zA-Z0-9._-]/g, "")}.db`);
|
|
113
|
+
}
|
|
114
|
+
function resolveV2Fresh(dataDir, channel, env) {
|
|
115
|
+
const filename = sourceOpenCodeDatabaseFilename("v2", channel, env);
|
|
116
|
+
const explicit = env.OPENCODE_DB !== undefined;
|
|
117
|
+
return {
|
|
118
|
+
path: filename === ":memory:" ? filename : resolve(dataDir, filename),
|
|
119
|
+
source: explicit ? "OPENCODE_DB" : env.OPENCODE_CHANNEL ? "channel" : "default",
|
|
120
|
+
channel: explicit ? null : channel
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function resolveOpenCodeDbPath(hostGeneration = "v1", options = {}) {
|
|
124
|
+
const env = options.env ?? process.env;
|
|
125
|
+
const dataDir = openCodeDataDir(env, options.dataHome);
|
|
126
|
+
const channel = options.channel ?? env.OPENCODE_CHANNEL;
|
|
127
|
+
const key = environmentKey(dataDir, hostGeneration, channel, env);
|
|
128
|
+
if (cachedResolution?.key === key && (!cachedResolution.existed || existsSync(cachedResolution.resolution.path))) {
|
|
129
|
+
if (cachedResolution.existed)
|
|
130
|
+
return cachedResolution.resolution;
|
|
131
|
+
}
|
|
132
|
+
const resolution = hostGeneration === "v2" ? resolveV2Fresh(dataDir, channel ?? "latest", env) : resolveV1Fresh(dataDir, env);
|
|
133
|
+
cachedResolution = {
|
|
134
|
+
key,
|
|
135
|
+
resolution,
|
|
136
|
+
existed: resolution.path !== ":memory:" && existsSync(resolution.path)
|
|
137
|
+
};
|
|
138
|
+
return resolution;
|
|
139
|
+
}
|
|
140
|
+
function schemaTableNames(db, schema = "main") {
|
|
141
|
+
const rows = db.prepare(`SELECT name FROM ${schema}.sqlite_master WHERE type = 'table' AND name IN ('message', 'part', 'session', 'project', 'session_message', 'session_v2')`).all();
|
|
142
|
+
return new Set(rows.flatMap((row) => typeof row.name === "string" ? [row.name] : []));
|
|
143
|
+
}
|
|
144
|
+
function hasV1MessageTables(db, schema = "main") {
|
|
145
|
+
const tables = schemaTableNames(db, schema);
|
|
146
|
+
return tables.has("message") && tables.has("part");
|
|
147
|
+
}
|
|
148
|
+
function detectOpenCodeStoreGeneration(db, schema = "main") {
|
|
149
|
+
const tables = schemaTableNames(db, schema);
|
|
150
|
+
const hasV1Messages = tables.has("message") && tables.has("part");
|
|
151
|
+
if (hasV1Messages)
|
|
152
|
+
return "v1";
|
|
153
|
+
if (tables.has("session_message"))
|
|
154
|
+
return "v2";
|
|
155
|
+
if (tables.has("session") || tables.has("project"))
|
|
156
|
+
return "v1";
|
|
157
|
+
return "unknown";
|
|
158
|
+
}
|
|
159
|
+
function hasMigratedV2Schema(tables) {
|
|
160
|
+
return tables.has("session_message") && tables.has("session_v2");
|
|
161
|
+
}
|
|
162
|
+
function isOpenCodeV2Store(db, schema = "main") {
|
|
163
|
+
return detectOpenCodeStoreGeneration(db, schema) === "v2" || hasMigratedV2Schema(schemaTableNames(db, schema));
|
|
164
|
+
}
|
|
165
|
+
function assertOpenCodeStoreGeneration(db, expected, path, schema = "main") {
|
|
166
|
+
const actual = detectOpenCodeStoreGeneration(db, schema);
|
|
167
|
+
if (actual === expected)
|
|
168
|
+
return;
|
|
169
|
+
if (expected === "v2" && isOpenCodeV2Store(db, schema))
|
|
170
|
+
return;
|
|
171
|
+
if (actual === "unknown")
|
|
172
|
+
return;
|
|
173
|
+
throw new Error(`OpenCode store generation mismatch at ${path}: expected ${expected}, found ${actual}; refusing generation-specific database access`);
|
|
174
|
+
}
|
|
175
|
+
function openCodeDbPathExists(resolution = resolveOpenCodeDbPath()) {
|
|
176
|
+
return resolution.path !== ":memory:" && existsSync(resolution.path);
|
|
177
|
+
}
|
|
178
|
+
function getOpenCodeDbProbeDescriptions(resolution = resolveOpenCodeDbPath()) {
|
|
179
|
+
if (resolution.source === "OPENCODE_DB")
|
|
180
|
+
return [resolution.path];
|
|
181
|
+
if (resolution.source === "channel" || process.env.OPENCODE_DISABLE_CHANNEL_DB === "1" || process.env.OPENCODE_DISABLE_CHANNEL_DB === "true") {
|
|
182
|
+
return [resolution.path];
|
|
183
|
+
}
|
|
184
|
+
const dataDir = openCodeDataDir();
|
|
185
|
+
return [
|
|
186
|
+
join(dataDir, "opencode.db"),
|
|
187
|
+
join(dataDir, "opencode-local.db"),
|
|
188
|
+
join(dataDir, "opencode-dev.db"),
|
|
189
|
+
join(dataDir, "opencode-<channel>.db")
|
|
190
|
+
];
|
|
191
|
+
}
|
|
192
|
+
function recordOpenCodeDbReadFailure(resolution, error) {
|
|
193
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
194
|
+
lastReadFailure = { ...resolution, message };
|
|
195
|
+
return lastReadFailure;
|
|
196
|
+
}
|
|
197
|
+
function clearOpenCodeDbReadFailure(path) {
|
|
198
|
+
if (path === undefined || lastReadFailure?.path === path)
|
|
199
|
+
lastReadFailure = null;
|
|
200
|
+
}
|
|
201
|
+
function claimOpenCodeDbDiagnosticOnce(surface, resolution) {
|
|
202
|
+
const key = `${surface}\x00${resolution.path}\x00${resolution.source}`;
|
|
203
|
+
if (claimedDiagnostics.has(key))
|
|
204
|
+
return false;
|
|
205
|
+
claimedDiagnostics.add(key);
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// ../plugin/src/features/magic-context/tool-definition-tokens.ts
|
|
210
|
+
var measurements = new Map;
|
|
211
|
+
var fingerprints = new Map;
|
|
212
|
+
var persistenceDb = null;
|
|
213
|
+
var cachedInsertStmt = null;
|
|
214
|
+
function keyFor(providerID, modelID, agentName) {
|
|
215
|
+
const agent = agentName && agentName.length > 0 ? agentName : "default";
|
|
216
|
+
return `${providerID}/${modelID}/${agent}`;
|
|
217
|
+
}
|
|
218
|
+
function setDatabase(db) {
|
|
219
|
+
persistenceDb = db;
|
|
220
|
+
cachedInsertStmt = null;
|
|
221
|
+
}
|
|
222
|
+
function loadToolDefinitionMeasurements(db) {
|
|
223
|
+
let rows = [];
|
|
224
|
+
try {
|
|
225
|
+
rows = db.prepare("SELECT provider_id, model_id, agent_name, tool_id, token_count FROM tool_definition_measurements").all();
|
|
226
|
+
} catch {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
for (const row of rows) {
|
|
230
|
+
const key = keyFor(row.provider_id, row.model_id, row.agent_name);
|
|
231
|
+
let inner = measurements.get(key);
|
|
232
|
+
if (!inner) {
|
|
233
|
+
inner = new Map;
|
|
234
|
+
measurements.set(key, inner);
|
|
235
|
+
}
|
|
236
|
+
inner.set(row.tool_id, row.token_count);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export { stableStringify, resolveOpenCodeDbPath, hasV1MessageTables, assertOpenCodeStoreGeneration, openCodeDbPathExists, getOpenCodeDbProbeDescriptions, recordOpenCodeDbReadFailure, clearOpenCodeDbReadFailure, claimOpenCodeDbDiagnosticOnce, closeQuietly, setDatabase, loadToolDefinitionMeasurements };
|