@codemem/core 0.21.1 → 0.21.2
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.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +758 -682
- package/dist/index.js.map +1 -1
- package/dist/ingest-pipeline.d.ts.map +1 -1
- package/dist/observer-client.d.ts.map +1 -1
- package/dist/raw-event-flush.d.ts +0 -1
- package/dist/raw-event-flush.d.ts.map +1 -1
- package/dist/raw-event-sweeper.d.ts.map +1 -1
- package/dist/sync-replication.d.ts +21 -0
- package/dist/sync-replication.d.ts.map +1 -1
- package/dist/sync-retention-runner.d.ts +11 -0
- package/dist/sync-retention-runner.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4909,7 +4909,34 @@ function ts(e, t) {
|
|
|
4909
4909
|
}
|
|
4910
4910
|
return n;
|
|
4911
4911
|
}
|
|
4912
|
-
|
|
4912
|
+
function ns(e) {
|
|
4913
|
+
return e.observations.length > 0 || e.summary !== null || e.skipSummaryReason !== null;
|
|
4914
|
+
}
|
|
4915
|
+
async function rs(e, t, n) {
|
|
4916
|
+
let r = await e.observe(t, n), i = r.raw ? Ro(r.raw) : {
|
|
4917
|
+
observations: [],
|
|
4918
|
+
summary: null,
|
|
4919
|
+
skipSummaryReason: null
|
|
4920
|
+
};
|
|
4921
|
+
if (!r.raw || ns(i)) return {
|
|
4922
|
+
raw: r.raw,
|
|
4923
|
+
parsed: i,
|
|
4924
|
+
provider: r.provider,
|
|
4925
|
+
model: r.model
|
|
4926
|
+
};
|
|
4927
|
+
let a = `${t}\n\nYour previous reply was invalid because it did not follow the required XML-only schema. Rewrite the same analysis as valid XML only. Do not include prose outside XML.`, o = `${n}\n\nPrevious invalid response to rewrite as valid XML:\n${r.raw}`, s = await e.observe(a, o), c = s.raw ? Ro(s.raw) : {
|
|
4928
|
+
observations: [],
|
|
4929
|
+
summary: null,
|
|
4930
|
+
skipSummaryReason: null
|
|
4931
|
+
};
|
|
4932
|
+
return {
|
|
4933
|
+
raw: s.raw,
|
|
4934
|
+
parsed: c,
|
|
4935
|
+
provider: s.provider,
|
|
4936
|
+
model: s.model
|
|
4937
|
+
};
|
|
4938
|
+
}
|
|
4939
|
+
async function is(e, t, n) {
|
|
4913
4940
|
let r = e.cwd ?? process.cwd(), i = e.events ?? [];
|
|
4914
4941
|
if (!Array.isArray(i) || i.length === 0) return;
|
|
4915
4942
|
let a = e.sessionContext ?? {}, o = n.storeSummary ?? !0, s = n.storeTyped ?? !0, c = n.maxChars ?? 12e3, l = n.observerMaxChars ?? 12e3, u = N(t.db, { schema: H }), d = (/* @__PURE__ */ new Date()).toISOString(), f = Ce(e.project) ?? Qt(r) ?? null, p = u.insert(U).values({
|
|
@@ -4931,7 +4958,7 @@ async function ns(e, t, n) {
|
|
|
4931
4958
|
h = ao(h, g, 30);
|
|
4932
4959
|
let _ = To(e), v = Eo(e), y = _.at(-1) ?? null, b = a.firstPrompt ?? (d.length > 0 ? d[d.length - 1]?.promptText : null) ?? null, x = h.length > 0 || !!b || o && !!y;
|
|
4933
4960
|
if (b && xo(b) && h.length === 0 && !y && (x = !1), !x) {
|
|
4934
|
-
|
|
4961
|
+
as(t, p, i.length, a);
|
|
4935
4962
|
return;
|
|
4936
4963
|
}
|
|
4937
4964
|
let S = wo(e), C = [];
|
|
@@ -4951,14 +4978,14 @@ async function ns(e, t, n) {
|
|
|
4951
4978
|
includeSummary: o,
|
|
4952
4979
|
diffSummary: "",
|
|
4953
4980
|
recentFiles: ""
|
|
4954
|
-
}), te = await n.observer
|
|
4981
|
+
}), te = await rs(n.observer, E, ee);
|
|
4955
4982
|
if (!te.raw) {
|
|
4956
4983
|
if (a?.flusher === "raw_events") throw Error("observer failed during raw-event flush");
|
|
4957
4984
|
let e = n.observer.getStatus();
|
|
4958
|
-
console.warn(`[codemem] Observer returned no output (provider=${te.provider}, model=${te.model}${e.lastError ? `, error=${e.lastError}` : ""}). No memories will be created for this session.`),
|
|
4985
|
+
console.warn(`[codemem] Observer returned no output (provider=${te.provider}, model=${te.model}${e.lastError ? `, error=${e.lastError}` : ""}). No memories will be created for this session.`), as(t, p, i.length, a);
|
|
4959
4986
|
return;
|
|
4960
4987
|
}
|
|
4961
|
-
let D = te.raw, O =
|
|
4988
|
+
let D = te.raw, O = te.parsed, k = [];
|
|
4962
4989
|
if (s && zo(O.observations)) for (let e of O.observations) {
|
|
4963
4990
|
let t = e.kind.trim().toLowerCase();
|
|
4964
4991
|
Zo.has(t) && (!e.title && !e.narrative || Kt(e.title) || Kt(e.narrative) || (e.filesRead = $o(e.filesRead, r), e.filesModified = $o(e.filesModified, r), k.push(e)));
|
|
@@ -5053,15 +5080,15 @@ async function ns(e, t, n) {
|
|
|
5053
5080
|
for (let e of ne) try {
|
|
5054
5081
|
await Jo(t.db, e.memoryId, e.title, e.bodyText);
|
|
5055
5082
|
} catch {}
|
|
5056
|
-
|
|
5083
|
+
as(t, p, i.length, a);
|
|
5057
5084
|
} catch (e) {
|
|
5058
5085
|
try {
|
|
5059
|
-
|
|
5086
|
+
as(t, p, i.length, a);
|
|
5060
5087
|
} catch {}
|
|
5061
5088
|
throw e;
|
|
5062
5089
|
}
|
|
5063
5090
|
}
|
|
5064
|
-
function
|
|
5091
|
+
function as(e, t, n, r) {
|
|
5065
5092
|
e.endSession(t, {
|
|
5066
5093
|
post: {},
|
|
5067
5094
|
source: "plugin",
|
|
@@ -5069,11 +5096,11 @@ function rs(e, t, n, r) {
|
|
|
5069
5096
|
session_context: r
|
|
5070
5097
|
});
|
|
5071
5098
|
}
|
|
5072
|
-
function
|
|
5099
|
+
function os(e, t = 24) {
|
|
5073
5100
|
let n = N(e.db, { schema: H }), r = (/* @__PURE__ */ new Date(Date.now() - t * 36e5)).toISOString();
|
|
5074
5101
|
return n.update(U).set({ ended_at: r }).where(D(ie(U.ended_at), oe(U.started_at, r))).run().changes;
|
|
5075
5102
|
}
|
|
5076
|
-
async function
|
|
5103
|
+
async function ss(e, t) {
|
|
5077
5104
|
let n = [];
|
|
5078
5105
|
for await (let e of process.stdin) n.push(String(e));
|
|
5079
5106
|
let r = n.join("");
|
|
@@ -5084,30 +5111,30 @@ async function as(e, t) {
|
|
|
5084
5111
|
} catch (e) {
|
|
5085
5112
|
throw Error(`codemem: invalid payload: ${e}`);
|
|
5086
5113
|
}
|
|
5087
|
-
await
|
|
5114
|
+
await is(i, e, { observer: t });
|
|
5088
5115
|
}
|
|
5089
5116
|
//#endregion
|
|
5090
5117
|
//#region src/integers.ts
|
|
5091
|
-
var
|
|
5092
|
-
function
|
|
5118
|
+
var cs = BigInt(2 ** 53 - 1), ls = BigInt(-(2 ** 53 - 1));
|
|
5119
|
+
function us(e) {
|
|
5093
5120
|
if (e == null) return null;
|
|
5094
5121
|
let t = e.trim();
|
|
5095
5122
|
if (!/^[+-]?\d+$/.test(t)) return null;
|
|
5096
5123
|
try {
|
|
5097
5124
|
let e = BigInt(t);
|
|
5098
|
-
return e <
|
|
5125
|
+
return e < ls || e > cs ? null : Number(e);
|
|
5099
5126
|
} catch {
|
|
5100
5127
|
return null;
|
|
5101
5128
|
}
|
|
5102
5129
|
}
|
|
5103
|
-
function
|
|
5130
|
+
function ds(e) {
|
|
5104
5131
|
if (typeof e == "boolean") return null;
|
|
5105
5132
|
if (typeof e == "number") return !Number.isInteger(e) || !Number.isSafeInteger(e) || e <= 0 ? null : e;
|
|
5106
5133
|
if (typeof e == "string") {
|
|
5107
5134
|
if (!/^\d+$/.test(e)) return null;
|
|
5108
5135
|
try {
|
|
5109
5136
|
let t = BigInt(e);
|
|
5110
|
-
return t <= 0n || t >
|
|
5137
|
+
return t <= 0n || t > cs ? null : Number(t);
|
|
5111
5138
|
} catch {
|
|
5112
5139
|
return null;
|
|
5113
5140
|
}
|
|
@@ -5116,24 +5143,24 @@ function ls(e) {
|
|
|
5116
5143
|
}
|
|
5117
5144
|
//#endregion
|
|
5118
5145
|
//#region src/observer-auth.ts
|
|
5119
|
-
var
|
|
5120
|
-
function
|
|
5146
|
+
var fs = "0.0.1", ps = [/sk-[A-Za-z0-9]{10,}/g, /Bearer\s+[A-Za-z0-9._-]{10,}/g];
|
|
5147
|
+
function ms(e, t = 400) {
|
|
5121
5148
|
let n = e;
|
|
5122
|
-
for (let e of
|
|
5149
|
+
for (let e of ps) n = n.replace(new RegExp(e.source, e.flags), "[redacted]");
|
|
5123
5150
|
return n.length > t ? `${n.slice(0, t)}…` : n;
|
|
5124
5151
|
}
|
|
5125
|
-
function
|
|
5152
|
+
function hs() {
|
|
5126
5153
|
return {
|
|
5127
5154
|
token: null,
|
|
5128
5155
|
authType: "none",
|
|
5129
5156
|
source: "none"
|
|
5130
5157
|
};
|
|
5131
5158
|
}
|
|
5132
|
-
function
|
|
5159
|
+
function gs() {
|
|
5133
5160
|
return T(v(), ".local", "share", "opencode", "auth.json");
|
|
5134
5161
|
}
|
|
5135
|
-
function
|
|
5136
|
-
let e =
|
|
5162
|
+
function _s() {
|
|
5163
|
+
let e = gs();
|
|
5137
5164
|
if (!l(e)) return {};
|
|
5138
5165
|
try {
|
|
5139
5166
|
let t = JSON.parse(f(e, "utf-8"));
|
|
@@ -5142,55 +5169,55 @@ function hs() {
|
|
|
5142
5169
|
return {};
|
|
5143
5170
|
}
|
|
5144
5171
|
}
|
|
5145
|
-
function
|
|
5172
|
+
function vs(e, t) {
|
|
5146
5173
|
return e && ["openai", "anthropic"].includes(e.toLowerCase()) ? e.toLowerCase() : t.toLowerCase().startsWith("claude") ? "anthropic" : "openai";
|
|
5147
5174
|
}
|
|
5148
|
-
function
|
|
5175
|
+
function ys(e, t) {
|
|
5149
5176
|
let n = e[t];
|
|
5150
5177
|
return typeof n == "object" && n && !Array.isArray(n) ? n : null;
|
|
5151
5178
|
}
|
|
5152
|
-
function
|
|
5153
|
-
let n =
|
|
5179
|
+
function bs(e, t) {
|
|
5180
|
+
let n = ys(e, t);
|
|
5154
5181
|
if (!n) return null;
|
|
5155
5182
|
let r = n.access;
|
|
5156
5183
|
return typeof r == "string" && r ? r : null;
|
|
5157
5184
|
}
|
|
5158
|
-
function
|
|
5159
|
-
let n =
|
|
5185
|
+
function xs(e, t) {
|
|
5186
|
+
let n = ys(e, t);
|
|
5160
5187
|
if (!n) return null;
|
|
5161
5188
|
let r = n.key;
|
|
5162
5189
|
return typeof r == "string" && r ? r : null;
|
|
5163
5190
|
}
|
|
5164
|
-
function
|
|
5165
|
-
let e =
|
|
5191
|
+
function Ss() {
|
|
5192
|
+
let e = _s(), t = Date.now(), n = !!process.env.CODEMEM_OBSERVER_API_KEY, r = [
|
|
5166
5193
|
["openai", "OPENAI_API_KEY"],
|
|
5167
5194
|
["anthropic", "ANTHROPIC_API_KEY"],
|
|
5168
5195
|
["opencode", null]
|
|
5169
5196
|
], i = {};
|
|
5170
5197
|
for (let [a, o] of r) {
|
|
5171
|
-
let r =
|
|
5198
|
+
let r = bs(e, a), s = ws(e, a);
|
|
5172
5199
|
i[a] = {
|
|
5173
|
-
oauth: a === "opencode" ? !!
|
|
5200
|
+
oauth: a === "opencode" ? !!xs(e, a) : !!r && (s == null || s > t),
|
|
5174
5201
|
api_key: n,
|
|
5175
5202
|
env_var: o ? !!process.env[o] : !1
|
|
5176
5203
|
};
|
|
5177
5204
|
}
|
|
5178
5205
|
return i;
|
|
5179
5206
|
}
|
|
5180
|
-
function
|
|
5181
|
-
let n =
|
|
5207
|
+
function Cs(e, t) {
|
|
5208
|
+
let n = ys(e, t);
|
|
5182
5209
|
if (!n) return null;
|
|
5183
5210
|
let r = n.accountId;
|
|
5184
5211
|
return typeof r == "string" && r ? r : null;
|
|
5185
5212
|
}
|
|
5186
|
-
function
|
|
5187
|
-
let n =
|
|
5213
|
+
function ws(e, t) {
|
|
5214
|
+
let n = ys(e, t);
|
|
5188
5215
|
if (!n) return null;
|
|
5189
5216
|
let r = n.expires;
|
|
5190
5217
|
return typeof r == "number" ? r : null;
|
|
5191
5218
|
}
|
|
5192
|
-
function
|
|
5193
|
-
let n = process.env.CODEMEM_CODEX_ORIGINATOR ?? "opencode", r = process.env.CODEMEM_CODEX_USER_AGENT ?? `codemem/${
|
|
5219
|
+
function Ts(e, t) {
|
|
5220
|
+
let n = process.env.CODEMEM_CODEX_ORIGINATOR ?? "opencode", r = process.env.CODEMEM_CODEX_USER_AGENT ?? `codemem/${fs} (${b()} ${x()}; ${_()})`, i = {
|
|
5194
5221
|
authorization: `Bearer ${e}`,
|
|
5195
5222
|
originator: n,
|
|
5196
5223
|
"User-Agent": r,
|
|
@@ -5198,7 +5225,7 @@ function Cs(e, t) {
|
|
|
5198
5225
|
};
|
|
5199
5226
|
return t && (i["ChatGPT-Account-Id"] = t), i;
|
|
5200
5227
|
}
|
|
5201
|
-
function
|
|
5228
|
+
function Es(e, t) {
|
|
5202
5229
|
let n = e[0];
|
|
5203
5230
|
if (!n) return null;
|
|
5204
5231
|
let r = Math.max(100, t);
|
|
@@ -5216,7 +5243,7 @@ function ws(e, t) {
|
|
|
5216
5243
|
return null;
|
|
5217
5244
|
}
|
|
5218
5245
|
}
|
|
5219
|
-
function
|
|
5246
|
+
function Ds(e) {
|
|
5220
5247
|
if (!e) return null;
|
|
5221
5248
|
let t = e.replace(/^~/, v());
|
|
5222
5249
|
t = t.replace(/\$\{([^}]+)\}|\$([A-Za-z_][A-Za-z0-9_]*)/g, (e, t, n) => {
|
|
@@ -5229,7 +5256,7 @@ function Ts(e) {
|
|
|
5229
5256
|
return null;
|
|
5230
5257
|
}
|
|
5231
5258
|
}
|
|
5232
|
-
var
|
|
5259
|
+
var Os = new Set([
|
|
5233
5260
|
"",
|
|
5234
5261
|
"auto",
|
|
5235
5262
|
"env",
|
|
@@ -5237,47 +5264,47 @@ var Es = new Set([
|
|
|
5237
5264
|
"command",
|
|
5238
5265
|
"none"
|
|
5239
5266
|
]);
|
|
5240
|
-
function
|
|
5267
|
+
function ks(e) {
|
|
5241
5268
|
let t = (e ?? "").trim().toLowerCase();
|
|
5242
|
-
return
|
|
5269
|
+
return Os.has(t) && t || "auto";
|
|
5243
5270
|
}
|
|
5244
|
-
var
|
|
5271
|
+
var As = class {
|
|
5245
5272
|
source;
|
|
5246
5273
|
filePath;
|
|
5247
5274
|
command;
|
|
5248
5275
|
timeoutMs;
|
|
5249
5276
|
cacheTtlS;
|
|
5250
|
-
cached =
|
|
5277
|
+
cached = hs();
|
|
5251
5278
|
cachedAtMs = 0;
|
|
5252
5279
|
constructor(e) {
|
|
5253
5280
|
this.source = e?.source ?? "auto", this.filePath = e?.filePath ?? null, this.command = e?.command ?? [], this.timeoutMs = e?.timeoutMs ?? 1500, this.cacheTtlS = e?.cacheTtlS ?? 300;
|
|
5254
5281
|
}
|
|
5255
5282
|
resolve(e) {
|
|
5256
|
-
let t =
|
|
5257
|
-
if (t === "none") return
|
|
5283
|
+
let t = ks(this.source), n = e?.explicitToken ?? null, r = e?.envTokens ?? [], i = e?.oauthToken ?? null, a = e?.forceRefresh ?? !1;
|
|
5284
|
+
if (t === "none") return hs();
|
|
5258
5285
|
if (!a && (t === "command" || t === "file") && this.cacheTtlS > 0) {
|
|
5259
5286
|
let e = performance.now() - this.cachedAtMs;
|
|
5260
5287
|
if (this.cachedAtMs > 0 && e <= this.cacheTtlS * 1e3) return this.cached;
|
|
5261
5288
|
}
|
|
5262
5289
|
let o = null, s = "none";
|
|
5263
|
-
t === "auto" ? (n && (o = n, s = "explicit"), o || (o = r.find((e) => !!e) ?? null, o && (s = "env")), !o && i && (o = i, s = "oauth")) : t === "env" && (o = r.find((e) => !!e) ?? null, o && (s = "env")), (t === "auto" || t === "file") && !o && (o =
|
|
5290
|
+
t === "auto" ? (n && (o = n, s = "explicit"), o || (o = r.find((e) => !!e) ?? null, o && (s = "env")), !o && i && (o = i, s = "oauth")) : t === "env" && (o = r.find((e) => !!e) ?? null, o && (s = "env")), (t === "auto" || t === "file") && !o && (o = Ds(this.filePath), o && (s = "file")), (t === "auto" || t === "command") && !o && (o = Es(this.command, this.timeoutMs), o && (s = "command"));
|
|
5264
5291
|
let c = o ? {
|
|
5265
5292
|
token: o,
|
|
5266
5293
|
authType: "bearer",
|
|
5267
5294
|
source: s
|
|
5268
|
-
} :
|
|
5295
|
+
} : hs(), l = t === "command" || t === "file";
|
|
5269
5296
|
return l && c.token ? (this.cached = c, this.cachedAtMs = performance.now()) : l && this.invalidateCache(), c;
|
|
5270
5297
|
}
|
|
5271
5298
|
invalidateCache() {
|
|
5272
|
-
this.cached =
|
|
5299
|
+
this.cached = hs(), this.cachedAtMs = 0;
|
|
5273
5300
|
}
|
|
5274
|
-
},
|
|
5275
|
-
function
|
|
5301
|
+
}, js = /\$\{auth\.token\}/g, Ms = /\$\{auth\.type\}/g, Ns = /\$\{auth\.source\}/g;
|
|
5302
|
+
function Ps(e, t) {
|
|
5276
5303
|
let n = t.token ?? "", r = {};
|
|
5277
5304
|
for (let [i, a] of Object.entries(e)) {
|
|
5278
5305
|
if (typeof i != "string" || typeof a != "string") continue;
|
|
5279
|
-
let e = a.replace(
|
|
5280
|
-
if (e = e.replace(
|
|
5306
|
+
let e = a.replace(js, n);
|
|
5307
|
+
if (e = e.replace(Ms, t.authType), e = e.replace(Ns, t.source), a.includes("${auth.token}") && !n) continue;
|
|
5281
5308
|
let o = e.trim();
|
|
5282
5309
|
o && (r[i] = o);
|
|
5283
5310
|
}
|
|
@@ -5285,7 +5312,7 @@ function Ms(e, t) {
|
|
|
5285
5312
|
}
|
|
5286
5313
|
//#endregion
|
|
5287
5314
|
//#region src/observer-client.ts
|
|
5288
|
-
var
|
|
5315
|
+
var Fs = "claude-sonnet-4-20250514", Is = "gpt-5.1-codex-mini", Ls = "https://api.anthropic.com/v1/messages", Rs = "2023-06-01", zs = "https://chatgpt.com/backend-api/codex/responses", Bs = 6e4, Vs = {
|
|
5289
5316
|
"claude-4.5-haiku": "claude-haiku-4-5",
|
|
5290
5317
|
"claude-4.5-sonnet": "claude-sonnet-4-5",
|
|
5291
5318
|
"claude-4.5-opus": "claude-opus-4-5",
|
|
@@ -5295,12 +5322,12 @@ var Ns = "claude-sonnet-4-20250514", Ps = "gpt-5.1-codex-mini", Fs = "https://ap
|
|
|
5295
5322
|
"claude-4.0-sonnet": "claude-sonnet-4-0",
|
|
5296
5323
|
"claude-4.0-opus": "claude-opus-4-0"
|
|
5297
5324
|
};
|
|
5298
|
-
function
|
|
5325
|
+
function Hs(e, t) {
|
|
5299
5326
|
if (e == null) return t;
|
|
5300
5327
|
let n = typeof e == "string" ? Number.parseInt(e, 10) : Number(e);
|
|
5301
5328
|
return Number.isFinite(n) ? n : t;
|
|
5302
5329
|
}
|
|
5303
|
-
function
|
|
5330
|
+
function Us(e) {
|
|
5304
5331
|
if (e == null) return null;
|
|
5305
5332
|
if (typeof e == "string") {
|
|
5306
5333
|
let t = e.trim();
|
|
@@ -5314,7 +5341,7 @@ function Vs(e) {
|
|
|
5314
5341
|
}
|
|
5315
5342
|
return typeof e == "object" && !Array.isArray(e) ? e : null;
|
|
5316
5343
|
}
|
|
5317
|
-
function
|
|
5344
|
+
function Ws(e) {
|
|
5318
5345
|
if (e == null) return null;
|
|
5319
5346
|
if (Array.isArray(e)) return e.every((e) => typeof e == "string") ? e : null;
|
|
5320
5347
|
if (typeof e == "string") {
|
|
@@ -5328,7 +5355,7 @@ function Hs(e) {
|
|
|
5328
5355
|
}
|
|
5329
5356
|
return null;
|
|
5330
5357
|
}
|
|
5331
|
-
function
|
|
5358
|
+
function Gs() {
|
|
5332
5359
|
let e = {
|
|
5333
5360
|
observerProvider: null,
|
|
5334
5361
|
observerModel: null,
|
|
@@ -5360,41 +5387,41 @@ function Us() {
|
|
|
5360
5387
|
i = {};
|
|
5361
5388
|
}
|
|
5362
5389
|
let a = { ...e };
|
|
5363
|
-
typeof i.observer_provider == "string" && (a.observerProvider = i.observer_provider), typeof i.observer_model == "string" && (a.observerModel = i.observer_model), typeof i.observer_runtime == "string" && (a.observerRuntime = i.observer_runtime), typeof i.observer_api_key == "string" && (a.observerApiKey = i.observer_api_key), typeof i.observer_base_url == "string" && (a.observerBaseUrl = i.observer_base_url), a.observerMaxChars =
|
|
5364
|
-
let o =
|
|
5390
|
+
typeof i.observer_provider == "string" && (a.observerProvider = i.observer_provider), typeof i.observer_model == "string" && (a.observerModel = i.observer_model), typeof i.observer_runtime == "string" && (a.observerRuntime = i.observer_runtime), typeof i.observer_api_key == "string" && (a.observerApiKey = i.observer_api_key), typeof i.observer_base_url == "string" && (a.observerBaseUrl = i.observer_base_url), a.observerMaxChars = Hs(i.observer_max_chars, a.observerMaxChars), a.observerMaxTokens = Hs(i.observer_max_tokens, a.observerMaxTokens), typeof i.observer_auth_source == "string" && (a.observerAuthSource = i.observer_auth_source), typeof i.observer_auth_file == "string" && (a.observerAuthFile = i.observer_auth_file), a.observerAuthTimeoutMs = Hs(i.observer_auth_timeout_ms, a.observerAuthTimeoutMs), a.observerAuthCacheTtlS = Hs(i.observer_auth_cache_ttl_s, a.observerAuthCacheTtlS);
|
|
5391
|
+
let o = Us(i.observer_headers);
|
|
5365
5392
|
o && (a.observerHeaders = o);
|
|
5366
|
-
let s =
|
|
5367
|
-
s && (a.observerAuthCommand = s), a.observerProvider = process.env.CODEMEM_OBSERVER_PROVIDER ?? a.observerProvider, a.observerModel = process.env.CODEMEM_OBSERVER_MODEL ?? a.observerModel, a.observerRuntime = process.env.CODEMEM_OBSERVER_RUNTIME ?? a.observerRuntime, a.observerApiKey = process.env.CODEMEM_OBSERVER_API_KEY ?? a.observerApiKey, a.observerBaseUrl = process.env.CODEMEM_OBSERVER_BASE_URL ?? a.observerBaseUrl, a.observerAuthSource = process.env.CODEMEM_OBSERVER_AUTH_SOURCE ?? a.observerAuthSource, a.observerAuthFile = process.env.CODEMEM_OBSERVER_AUTH_FILE ?? a.observerAuthFile, a.observerMaxChars =
|
|
5368
|
-
let c =
|
|
5393
|
+
let s = Ws(i.observer_auth_command);
|
|
5394
|
+
s && (a.observerAuthCommand = s), a.observerProvider = process.env.CODEMEM_OBSERVER_PROVIDER ?? a.observerProvider, a.observerModel = process.env.CODEMEM_OBSERVER_MODEL ?? a.observerModel, a.observerRuntime = process.env.CODEMEM_OBSERVER_RUNTIME ?? a.observerRuntime, a.observerApiKey = process.env.CODEMEM_OBSERVER_API_KEY ?? a.observerApiKey, a.observerBaseUrl = process.env.CODEMEM_OBSERVER_BASE_URL ?? a.observerBaseUrl, a.observerAuthSource = process.env.CODEMEM_OBSERVER_AUTH_SOURCE ?? a.observerAuthSource, a.observerAuthFile = process.env.CODEMEM_OBSERVER_AUTH_FILE ?? a.observerAuthFile, a.observerMaxChars = Hs(process.env.CODEMEM_OBSERVER_MAX_CHARS, a.observerMaxChars), a.observerMaxTokens = Hs(process.env.CODEMEM_OBSERVER_MAX_TOKENS, a.observerMaxTokens), a.observerAuthTimeoutMs = Hs(process.env.CODEMEM_OBSERVER_AUTH_TIMEOUT_MS, a.observerAuthTimeoutMs), a.observerAuthCacheTtlS = Hs(process.env.CODEMEM_OBSERVER_AUTH_CACHE_TTL_S, a.observerAuthCacheTtlS);
|
|
5395
|
+
let c = Us(process.env.CODEMEM_OBSERVER_HEADERS);
|
|
5369
5396
|
c && (a.observerHeaders = c);
|
|
5370
|
-
let u =
|
|
5397
|
+
let u = Ws(process.env.CODEMEM_OBSERVER_AUTH_COMMAND);
|
|
5371
5398
|
return u && (a.observerAuthCommand = u), a;
|
|
5372
5399
|
}
|
|
5373
|
-
var
|
|
5400
|
+
var Ks = class extends Error {
|
|
5374
5401
|
constructor(e) {
|
|
5375
5402
|
super(e), this.name = "ObserverAuthError";
|
|
5376
5403
|
}
|
|
5377
5404
|
};
|
|
5378
|
-
function
|
|
5405
|
+
function qs(e) {
|
|
5379
5406
|
return e === 401 || e === 403;
|
|
5380
5407
|
}
|
|
5381
|
-
function
|
|
5408
|
+
function Js(e) {
|
|
5382
5409
|
let t = e.trim();
|
|
5383
|
-
return t && (
|
|
5410
|
+
return t && (Vs[t.toLowerCase()] ?? t);
|
|
5384
5411
|
}
|
|
5385
|
-
function
|
|
5386
|
-
return process.env.CODEMEM_ANTHROPIC_ENDPOINT ??
|
|
5412
|
+
function Ys() {
|
|
5413
|
+
return process.env.CODEMEM_ANTHROPIC_ENDPOINT ?? Ls;
|
|
5387
5414
|
}
|
|
5388
|
-
function
|
|
5415
|
+
function Xs(e, t) {
|
|
5389
5416
|
let n = {
|
|
5390
|
-
"anthropic-version":
|
|
5417
|
+
"anthropic-version": Rs,
|
|
5391
5418
|
"content-type": "application/json"
|
|
5392
5419
|
};
|
|
5393
5420
|
return t ? (n.authorization = `Bearer ${e}`, n["anthropic-beta"] = "oauth-2025-04-20") : n["x-api-key"] = e, n;
|
|
5394
5421
|
}
|
|
5395
|
-
function
|
|
5422
|
+
function Zs(e, t, n, r) {
|
|
5396
5423
|
return {
|
|
5397
|
-
model:
|
|
5424
|
+
model: Js(e),
|
|
5398
5425
|
max_tokens: r,
|
|
5399
5426
|
system: t,
|
|
5400
5427
|
messages: [{
|
|
@@ -5403,7 +5430,7 @@ function Ys(e, t, n, r) {
|
|
|
5403
5430
|
}]
|
|
5404
5431
|
};
|
|
5405
5432
|
}
|
|
5406
|
-
function
|
|
5433
|
+
function Qs(e) {
|
|
5407
5434
|
let t = e.content;
|
|
5408
5435
|
if (!Array.isArray(t)) return console.warn(`[codemem] Anthropic response has no content array (stop_reason=${e.stop_reason ?? "unknown"}, keys=${Object.keys(e).join(",")})`), null;
|
|
5409
5436
|
let n = [];
|
|
@@ -5417,13 +5444,13 @@ function Xs(e) {
|
|
|
5417
5444
|
}
|
|
5418
5445
|
return n.length > 0 ? n.join("") : null;
|
|
5419
5446
|
}
|
|
5420
|
-
function
|
|
5447
|
+
function $s(e) {
|
|
5421
5448
|
return {
|
|
5422
5449
|
authorization: `Bearer ${e}`,
|
|
5423
5450
|
"content-type": "application/json"
|
|
5424
5451
|
};
|
|
5425
5452
|
}
|
|
5426
|
-
function
|
|
5453
|
+
function ec(e, t) {
|
|
5427
5454
|
let n = { ...e };
|
|
5428
5455
|
for (let [e, r] of Object.entries(t)) {
|
|
5429
5456
|
let t = e.toLowerCase();
|
|
@@ -5432,12 +5459,12 @@ function Qs(e, t) {
|
|
|
5432
5459
|
}
|
|
5433
5460
|
return n;
|
|
5434
5461
|
}
|
|
5435
|
-
function
|
|
5436
|
-
let n =
|
|
5462
|
+
function tc(e, t) {
|
|
5463
|
+
let n = ec(e, t);
|
|
5437
5464
|
for (let t of Object.keys(e)) delete e[t];
|
|
5438
5465
|
Object.assign(e, n);
|
|
5439
5466
|
}
|
|
5440
|
-
function
|
|
5467
|
+
function nc(e, t, n, r) {
|
|
5441
5468
|
return {
|
|
5442
5469
|
model: e,
|
|
5443
5470
|
max_tokens: r,
|
|
@@ -5451,7 +5478,7 @@ function ec(e, t, n, r) {
|
|
|
5451
5478
|
}]
|
|
5452
5479
|
};
|
|
5453
5480
|
}
|
|
5454
|
-
function
|
|
5481
|
+
function rc(e) {
|
|
5455
5482
|
let t = e.choices;
|
|
5456
5483
|
if (!Array.isArray(t) || t.length === 0) return null;
|
|
5457
5484
|
let n = t[0];
|
|
@@ -5461,25 +5488,25 @@ function tc(e) {
|
|
|
5461
5488
|
let i = r.content;
|
|
5462
5489
|
return typeof i == "string" ? i : null;
|
|
5463
5490
|
}
|
|
5464
|
-
function
|
|
5465
|
-
return process.env.CODEMEM_CODEX_ENDPOINT ??
|
|
5491
|
+
function ic() {
|
|
5492
|
+
return process.env.CODEMEM_CODEX_ENDPOINT ?? zs;
|
|
5466
5493
|
}
|
|
5467
|
-
function
|
|
5494
|
+
function ac(e, t, n) {
|
|
5468
5495
|
return {
|
|
5469
5496
|
model: e,
|
|
5470
|
-
instructions:
|
|
5497
|
+
instructions: t,
|
|
5471
5498
|
input: [{
|
|
5472
5499
|
role: "user",
|
|
5473
5500
|
content: [{
|
|
5474
5501
|
type: "input_text",
|
|
5475
|
-
text:
|
|
5502
|
+
text: n
|
|
5476
5503
|
}]
|
|
5477
5504
|
}],
|
|
5478
5505
|
store: !1,
|
|
5479
5506
|
stream: !0
|
|
5480
5507
|
};
|
|
5481
5508
|
}
|
|
5482
|
-
function
|
|
5509
|
+
function oc(e, t) {
|
|
5483
5510
|
let n = [];
|
|
5484
5511
|
for (let r of e.split("\n")) {
|
|
5485
5512
|
if (!r.startsWith("data:")) continue;
|
|
@@ -5491,14 +5518,14 @@ function ic(e, t) {
|
|
|
5491
5518
|
}
|
|
5492
5519
|
return n.length > 0 ? n.join("").trim() : null;
|
|
5493
5520
|
}
|
|
5494
|
-
function
|
|
5521
|
+
function sc(e) {
|
|
5495
5522
|
if (e.type === "response.output_text.delta") {
|
|
5496
5523
|
let t = e.delta;
|
|
5497
5524
|
return typeof t == "string" && t ? t : null;
|
|
5498
5525
|
}
|
|
5499
5526
|
return null;
|
|
5500
5527
|
}
|
|
5501
|
-
function
|
|
5528
|
+
function cc(e) {
|
|
5502
5529
|
if (e.type === "content_block_delta") {
|
|
5503
5530
|
let t = e.delta;
|
|
5504
5531
|
if (t && t.type === "text_delta") {
|
|
@@ -5508,10 +5535,10 @@ function oc(e) {
|
|
|
5508
5535
|
}
|
|
5509
5536
|
return null;
|
|
5510
5537
|
}
|
|
5511
|
-
function
|
|
5538
|
+
function lc() {
|
|
5512
5539
|
return Date.now();
|
|
5513
5540
|
}
|
|
5514
|
-
var
|
|
5541
|
+
var uc = class {
|
|
5515
5542
|
provider;
|
|
5516
5543
|
model;
|
|
5517
5544
|
runtime;
|
|
@@ -5528,7 +5555,7 @@ var cc = class {
|
|
|
5528
5555
|
_lastErrorCode = null;
|
|
5529
5556
|
_lastErrorMessage = null;
|
|
5530
5557
|
constructor(e) {
|
|
5531
|
-
let t = e ??
|
|
5558
|
+
let t = e ?? Gs(), n = (t.observerProvider ?? "").toLowerCase(), r = (t.observerModel ?? "").trim(), i = lt();
|
|
5532
5559
|
n && n !== "openai" && n !== "anthropic" && i.add(n);
|
|
5533
5560
|
let a = n;
|
|
5534
5561
|
if (!a) {
|
|
@@ -5539,11 +5566,11 @@ var cc = class {
|
|
|
5539
5566
|
let e = dt(r);
|
|
5540
5567
|
e && (a = e);
|
|
5541
5568
|
}
|
|
5542
|
-
a ||=
|
|
5569
|
+
a ||= vs(null, r || Is), a !== "openai" && a !== "anthropic" && a !== "opencode" && !i.has(a) && (a = "openai"), this.provider = a;
|
|
5543
5570
|
let o = t.observerRuntime;
|
|
5544
|
-
this.runtime = (typeof o == "string" && o.trim().toLowerCase(), "api_http"), r ? this.model = r : a === "anthropic" ? this.model =
|
|
5571
|
+
this.runtime = (typeof o == "string" && o.trim().toLowerCase(), "api_http"), r ? this.model = r : a === "anthropic" ? this.model = Fs : a === "openai" ? this.model = Is : this.model = ft(a) ?? St(a) ?? "", this.maxChars = t.observerMaxChars, this.maxTokens = t.observerMaxTokens, this._observerHeaders = { ...t.observerHeaders }, this._apiKey = t.observerApiKey ?? null;
|
|
5545
5572
|
let s = t.observerBaseUrl;
|
|
5546
|
-
this._customBaseUrl = typeof s == "string" && s.trim() ? s.trim() : null, this.authAdapter = new
|
|
5573
|
+
this._customBaseUrl = typeof s == "string" && s.trim() ? s.trim() : null, this.authAdapter = new As({
|
|
5547
5574
|
source: t.observerAuthSource,
|
|
5548
5575
|
filePath: t.observerAuthFile,
|
|
5549
5576
|
command: t.observerAuthCommand,
|
|
@@ -5582,18 +5609,18 @@ var cc = class {
|
|
|
5582
5609
|
let e = await this._callOnce(a, s);
|
|
5583
5610
|
return e && this._clearLastError(), {
|
|
5584
5611
|
raw: e,
|
|
5585
|
-
parsed: e ?
|
|
5612
|
+
parsed: e ? fc(e) : null,
|
|
5586
5613
|
provider: this.provider,
|
|
5587
5614
|
model: this.model
|
|
5588
5615
|
};
|
|
5589
5616
|
} catch (e) {
|
|
5590
|
-
if (e instanceof
|
|
5617
|
+
if (e instanceof Ks) {
|
|
5591
5618
|
if (this.refreshAuth(), !this.auth.token) throw e;
|
|
5592
5619
|
try {
|
|
5593
5620
|
let e = await this._callOnce(a, s);
|
|
5594
5621
|
return e && this._clearLastError(), {
|
|
5595
5622
|
raw: e,
|
|
5596
|
-
parsed: e ?
|
|
5623
|
+
parsed: e ? fc(e) : null,
|
|
5597
5624
|
provider: this.provider,
|
|
5598
5625
|
model: this.model
|
|
5599
5626
|
};
|
|
@@ -5606,11 +5633,11 @@ var cc = class {
|
|
|
5606
5633
|
}
|
|
5607
5634
|
_initProvider(e) {
|
|
5608
5635
|
this._codexAccess = null, this._codexAccountId = null, this._anthropicOAuthAccess = null;
|
|
5609
|
-
let t =
|
|
5636
|
+
let t = _s(), n = null, r = null;
|
|
5610
5637
|
if (this.provider === "openai" || this.provider === "anthropic") {
|
|
5611
|
-
r =
|
|
5612
|
-
let e =
|
|
5613
|
-
n && e != null && e <=
|
|
5638
|
+
r = vs(this.provider, this.model), n = bs(t, r);
|
|
5639
|
+
let e = ws(t, r);
|
|
5640
|
+
n && e != null && e <= lc() && (n = null);
|
|
5614
5641
|
}
|
|
5615
5642
|
if (this.provider !== "openai" && this.provider !== "anthropic") {
|
|
5616
5643
|
let n = at(this.provider), [r, i, a] = Object.keys(n).length > 0 ? Ct(this.provider, this.model) : pt(this.provider, this.model);
|
|
@@ -5618,7 +5645,7 @@ var cc = class {
|
|
|
5618
5645
|
...this._observerHeaders,
|
|
5619
5646
|
...a
|
|
5620
5647
|
}), !this._customBaseUrl) return;
|
|
5621
|
-
let o = this.provider === "opencode" ?
|
|
5648
|
+
let o = this.provider === "opencode" ? xs(t, this.provider) : null, s = xt(n) || this._apiKey || o;
|
|
5622
5649
|
this.auth = this.authAdapter.resolve({
|
|
5623
5650
|
explicitToken: s,
|
|
5624
5651
|
envTokens: [process.env.CODEMEM_OBSERVER_API_KEY ?? ""],
|
|
@@ -5638,67 +5665,67 @@ var cc = class {
|
|
|
5638
5665
|
],
|
|
5639
5666
|
oauthToken: n,
|
|
5640
5667
|
forceRefresh: e
|
|
5641
|
-
}), this.auth.source === "oauth" && n && (this._codexAccess = n, this._codexAccountId =
|
|
5668
|
+
}), this.auth.source === "oauth" && n && (this._codexAccess = n, this._codexAccountId = Cs(t, r ?? "openai")));
|
|
5642
5669
|
}
|
|
5643
5670
|
async _callOnce(e, t) {
|
|
5644
|
-
if (this._codexAccess) return this._callCodexConsumer(t);
|
|
5671
|
+
if (this._codexAccess) return this._callCodexConsumer(e, t);
|
|
5645
5672
|
if (this._anthropicOAuthAccess) return this._callAnthropicConsumer(e, t);
|
|
5646
5673
|
if (!this.auth.token) {
|
|
5647
|
-
if (this._initProvider(!0), this._codexAccess) return this._callCodexConsumer(t);
|
|
5674
|
+
if (this._initProvider(!0), this._codexAccess) return this._callCodexConsumer(e, t);
|
|
5648
5675
|
if (this._anthropicOAuthAccess) return this._callAnthropicConsumer(e, t);
|
|
5649
|
-
if (!this.auth.token) return this._setLastError(`${
|
|
5676
|
+
if (!this.auth.token) return this._setLastError(`${dc(this.provider)} credentials are missing.`, "auth_missing"), null;
|
|
5650
5677
|
}
|
|
5651
5678
|
return this.provider === "anthropic" ? this._callAnthropicDirect(e, t) : this._callOpenAIDirect(e, t);
|
|
5652
5679
|
}
|
|
5653
5680
|
async _callAnthropicDirect(e, t) {
|
|
5654
|
-
let n =
|
|
5681
|
+
let n = Ys(), r = ec(Xs(this.auth.token ?? "", !1), Ps(this._observerHeaders, this.auth)), i = Zs(this.model, e, t, this.maxTokens);
|
|
5655
5682
|
return this._fetchJSON(n, r, i, {
|
|
5656
|
-
parseResponse:
|
|
5683
|
+
parseResponse: Qs,
|
|
5657
5684
|
providerLabel: "Anthropic"
|
|
5658
5685
|
});
|
|
5659
5686
|
}
|
|
5660
5687
|
async _callOpenAIDirect(e, t) {
|
|
5661
5688
|
let n;
|
|
5662
5689
|
n = this._customBaseUrl ? `${this._customBaseUrl.replace(/\/+$/, "")}/chat/completions` : "https://api.openai.com/v1/chat/completions";
|
|
5663
|
-
let r =
|
|
5690
|
+
let r = ec($s(this.auth.token ?? ""), Ps(this._observerHeaders, this.auth)), i = nc(this.model, e, t, this.maxTokens);
|
|
5664
5691
|
return this._fetchJSON(n, r, i, {
|
|
5665
|
-
parseResponse:
|
|
5666
|
-
providerLabel:
|
|
5692
|
+
parseResponse: rc,
|
|
5693
|
+
providerLabel: dc(this.provider)
|
|
5667
5694
|
});
|
|
5668
5695
|
}
|
|
5669
|
-
async _callCodexConsumer(e) {
|
|
5696
|
+
async _callCodexConsumer(e, t) {
|
|
5670
5697
|
if (!this._codexAccess) return null;
|
|
5671
|
-
let
|
|
5698
|
+
let n = Ts(this._codexAccess, this._codexAccountId);
|
|
5672
5699
|
if (Object.keys(this._observerHeaders).length > 0) {
|
|
5673
5700
|
let e = {
|
|
5674
5701
|
token: this._codexAccess,
|
|
5675
5702
|
authType: "bearer",
|
|
5676
5703
|
source: this.auth.source
|
|
5677
5704
|
};
|
|
5678
|
-
|
|
5705
|
+
tc(n, Ps(this._observerHeaders, e));
|
|
5679
5706
|
}
|
|
5680
|
-
|
|
5681
|
-
let
|
|
5682
|
-
return this._fetchSSE(
|
|
5707
|
+
n["content-type"] = "application/json";
|
|
5708
|
+
let r = ac(this.model, e, t), i = ic();
|
|
5709
|
+
return this._fetchSSE(i, n, r, sc, {
|
|
5683
5710
|
providerLabel: "OpenAI",
|
|
5684
5711
|
authErrorMessage: "OpenAI authentication failed. Refresh credentials and retry."
|
|
5685
5712
|
});
|
|
5686
5713
|
}
|
|
5687
5714
|
async _callAnthropicConsumer(e, t) {
|
|
5688
5715
|
if (!this._anthropicOAuthAccess) return null;
|
|
5689
|
-
let n =
|
|
5716
|
+
let n = Xs(this._anthropicOAuthAccess, !0);
|
|
5690
5717
|
if (Object.keys(this._observerHeaders).length > 0) {
|
|
5691
5718
|
let e = {
|
|
5692
5719
|
token: this._anthropicOAuthAccess,
|
|
5693
5720
|
authType: "bearer",
|
|
5694
5721
|
source: this.auth.source
|
|
5695
5722
|
};
|
|
5696
|
-
|
|
5723
|
+
tc(n, Ps(this._observerHeaders, e));
|
|
5697
5724
|
}
|
|
5698
|
-
let r =
|
|
5725
|
+
let r = Ys(), i = new URL(r);
|
|
5699
5726
|
i.searchParams.set("beta", "true");
|
|
5700
5727
|
let a = i.toString(), o = {
|
|
5701
|
-
model:
|
|
5728
|
+
model: Js(this.model),
|
|
5702
5729
|
max_tokens: this.maxTokens,
|
|
5703
5730
|
stream: !0,
|
|
5704
5731
|
messages: [{
|
|
@@ -5707,7 +5734,7 @@ var cc = class {
|
|
|
5707
5734
|
}],
|
|
5708
5735
|
system: e
|
|
5709
5736
|
};
|
|
5710
|
-
return this._fetchSSE(a, n, o,
|
|
5737
|
+
return this._fetchSSE(a, n, o, cc, {
|
|
5711
5738
|
providerLabel: "Anthropic",
|
|
5712
5739
|
authErrorMessage: "Anthropic authentication failed. Refresh credentials and retry."
|
|
5713
5740
|
});
|
|
@@ -5718,7 +5745,7 @@ var cc = class {
|
|
|
5718
5745
|
method: "POST",
|
|
5719
5746
|
headers: t,
|
|
5720
5747
|
body: JSON.stringify(n),
|
|
5721
|
-
signal: AbortSignal.timeout(
|
|
5748
|
+
signal: AbortSignal.timeout(Bs)
|
|
5722
5749
|
});
|
|
5723
5750
|
if (!i.ok) {
|
|
5724
5751
|
let e = await i.text().catch(() => "");
|
|
@@ -5727,7 +5754,7 @@ var cc = class {
|
|
|
5727
5754
|
let a = await i.json(), o = r.parseResponse(a);
|
|
5728
5755
|
return o === null && this._setLastError(`${r.providerLabel} returned 200 but response contained no extractable text.`, "empty_response"), o;
|
|
5729
5756
|
} catch (e) {
|
|
5730
|
-
if (e instanceof
|
|
5757
|
+
if (e instanceof Ks) throw e;
|
|
5731
5758
|
return this._setLastError(`${r.providerLabel} processing failed during observer inference.`, "observer_call_failed"), null;
|
|
5732
5759
|
}
|
|
5733
5760
|
}
|
|
@@ -5737,21 +5764,21 @@ var cc = class {
|
|
|
5737
5764
|
method: "POST",
|
|
5738
5765
|
headers: t,
|
|
5739
5766
|
body: JSON.stringify(n),
|
|
5740
|
-
signal: AbortSignal.timeout(
|
|
5767
|
+
signal: AbortSignal.timeout(Bs)
|
|
5741
5768
|
});
|
|
5742
5769
|
if (!a.ok) {
|
|
5743
|
-
if (await a.text().catch(() => ""),
|
|
5770
|
+
if (await a.text().catch(() => ""), qs(a.status)) throw this._setLastError(i.authErrorMessage, "auth_failed"), new Ks(`${i.providerLabel} auth error: ${a.status}`);
|
|
5744
5771
|
return this._setLastError(`${i.providerLabel} request failed during observer processing.`, "provider_request_failed"), null;
|
|
5745
5772
|
}
|
|
5746
|
-
return
|
|
5773
|
+
return oc(await a.text(), r);
|
|
5747
5774
|
} catch (e) {
|
|
5748
|
-
if (e instanceof
|
|
5775
|
+
if (e instanceof Ks) throw e;
|
|
5749
5776
|
return this._setLastError(`${i.providerLabel} processing failed during observer inference.`, "observer_call_failed"), null;
|
|
5750
5777
|
}
|
|
5751
5778
|
}
|
|
5752
5779
|
_handleHttpError(e, t, n) {
|
|
5753
|
-
let r =
|
|
5754
|
-
if (
|
|
5780
|
+
let r = ms(t);
|
|
5781
|
+
if (qs(e)) throw this._setLastError(`${n} authentication failed. Refresh credentials and retry.`, "auth_failed"), new Ks(`${n} auth error: ${e}: ${r}`);
|
|
5755
5782
|
if (e === 429) {
|
|
5756
5783
|
this._setLastError(`${n} rate limited. Retry later.`, "rate_limited");
|
|
5757
5784
|
return;
|
|
@@ -5776,10 +5803,10 @@ var cc = class {
|
|
|
5776
5803
|
this._lastErrorCode = null, this._lastErrorMessage = null;
|
|
5777
5804
|
}
|
|
5778
5805
|
};
|
|
5779
|
-
function
|
|
5806
|
+
function dc(e) {
|
|
5780
5807
|
return e.charAt(0).toUpperCase() + e.slice(1);
|
|
5781
5808
|
}
|
|
5782
|
-
function
|
|
5809
|
+
function fc(e) {
|
|
5783
5810
|
try {
|
|
5784
5811
|
let t = JSON.parse(e);
|
|
5785
5812
|
return typeof t == "object" && t && !Array.isArray(t) ? t : null;
|
|
@@ -5789,10 +5816,10 @@ function uc(e) {
|
|
|
5789
5816
|
}
|
|
5790
5817
|
//#endregion
|
|
5791
5818
|
//#region src/search.ts
|
|
5792
|
-
function
|
|
5819
|
+
function pc(e) {
|
|
5793
5820
|
return N(e, { schema: H });
|
|
5794
5821
|
}
|
|
5795
|
-
var
|
|
5822
|
+
var mc = {
|
|
5796
5823
|
session_summary: .25,
|
|
5797
5824
|
decision: .2,
|
|
5798
5825
|
feature: .18,
|
|
@@ -5804,32 +5831,32 @@ var fc = {
|
|
|
5804
5831
|
observation: .1,
|
|
5805
5832
|
exploration: .1,
|
|
5806
5833
|
entities: .05
|
|
5807
|
-
},
|
|
5834
|
+
}, hc = .45, gc = .18, _c = .12, vc = 3, yc = 0, bc = 2, xc = [
|
|
5808
5835
|
/\bwhat did i\b/i,
|
|
5809
5836
|
/\bmy notes\b/i,
|
|
5810
5837
|
/\bmy last session\b/i,
|
|
5811
5838
|
/\bmy machine\b/i
|
|
5812
|
-
],
|
|
5839
|
+
], Sc = new Set([
|
|
5813
5840
|
"or",
|
|
5814
5841
|
"and",
|
|
5815
5842
|
"not",
|
|
5816
5843
|
"near",
|
|
5817
5844
|
"phrase"
|
|
5818
5845
|
]);
|
|
5819
|
-
function
|
|
5846
|
+
function Cc(e) {
|
|
5820
5847
|
let t = e.match(/[A-Za-z0-9_]+/g);
|
|
5821
5848
|
if (!t) return "";
|
|
5822
|
-
let n = t.filter((e) => !
|
|
5849
|
+
let n = t.filter((e) => !Sc.has(e.toLowerCase()));
|
|
5823
5850
|
return n.length === 0 ? "" : n.length === 1 ? n[0] : n.join(" OR ");
|
|
5824
5851
|
}
|
|
5825
|
-
function
|
|
5852
|
+
function wc(e, t) {
|
|
5826
5853
|
let n = Date.parse(e);
|
|
5827
5854
|
return Number.isNaN(n) ? 0 : 1 / (1 + Math.max(0, Math.floor(((t ?? /* @__PURE__ */ new Date()).getTime() - n) / 864e5)) / 7);
|
|
5828
5855
|
}
|
|
5829
|
-
function
|
|
5830
|
-
return e ?
|
|
5856
|
+
function Tc(e) {
|
|
5857
|
+
return e ? mc[e.trim().toLowerCase()] ?? 0 : 0;
|
|
5831
5858
|
}
|
|
5832
|
-
function
|
|
5859
|
+
function Ec(e) {
|
|
5833
5860
|
if (!e || e.personal_first === void 0) return !0;
|
|
5834
5861
|
let t = e.personal_first;
|
|
5835
5862
|
if (typeof t == "string") {
|
|
@@ -5849,10 +5876,10 @@ function wc(e) {
|
|
|
5849
5876
|
}
|
|
5850
5877
|
return !!t;
|
|
5851
5878
|
}
|
|
5852
|
-
function
|
|
5879
|
+
function Dc(e) {
|
|
5853
5880
|
return String(e?.trust_bias ?? "off").trim().toLowerCase() === "soft" ? "soft" : "off";
|
|
5854
5881
|
}
|
|
5855
|
-
function
|
|
5882
|
+
function Oc(e) {
|
|
5856
5883
|
if (!e || e.widen_shared_when_weak === void 0) return !1;
|
|
5857
5884
|
let t = e.widen_shared_when_weak;
|
|
5858
5885
|
if (typeof t == "string") {
|
|
@@ -5872,25 +5899,25 @@ function Ec(e) {
|
|
|
5872
5899
|
}
|
|
5873
5900
|
return !!t;
|
|
5874
5901
|
}
|
|
5875
|
-
function
|
|
5902
|
+
function kc(e) {
|
|
5876
5903
|
let t = e?.widen_shared_min_personal_results;
|
|
5877
|
-
return typeof t != "number" || !Number.isFinite(t) ?
|
|
5904
|
+
return typeof t != "number" || !Number.isFinite(t) ? vc : Math.max(1, Math.trunc(t));
|
|
5878
5905
|
}
|
|
5879
|
-
function
|
|
5906
|
+
function Ac(e) {
|
|
5880
5907
|
let t = e?.widen_shared_min_personal_score;
|
|
5881
|
-
return typeof t != "number" || !Number.isFinite(t) ?
|
|
5908
|
+
return typeof t != "number" || !Number.isFinite(t) ? yc : Math.max(0, t);
|
|
5882
5909
|
}
|
|
5883
|
-
function
|
|
5884
|
-
return
|
|
5910
|
+
function jc(e) {
|
|
5911
|
+
return xc.some((t) => t.test(e));
|
|
5885
5912
|
}
|
|
5886
|
-
function
|
|
5913
|
+
function Mc(e) {
|
|
5887
5914
|
if (!e) return !1;
|
|
5888
5915
|
let t = String(e.ownership_scope ?? "").trim().toLowerCase();
|
|
5889
5916
|
if (t === "mine" || t === "theirs") return !0;
|
|
5890
5917
|
let n = ka(e.include_visibility ?? e.visibility), r = ka(e.exclude_visibility), i = Da(e.include_workspace_ids), a = Da(e.exclude_workspace_ids), o = Oa(e.include_workspace_kinds), s = Oa(e.exclude_workspace_kinds);
|
|
5891
5918
|
return n.length || i.length || o.length || r.includes("private") || r.includes("shared") || s.includes("shared") || s.includes("personal") ? !0 : a.some((e) => e.startsWith("personal:") || e.startsWith("shared:"));
|
|
5892
5919
|
}
|
|
5893
|
-
function
|
|
5920
|
+
function Nc(e) {
|
|
5894
5921
|
return {
|
|
5895
5922
|
...e ?? {},
|
|
5896
5923
|
visibility: void 0,
|
|
@@ -5901,7 +5928,7 @@ function jc(e) {
|
|
|
5901
5928
|
widen_shared_when_weak: !1
|
|
5902
5929
|
};
|
|
5903
5930
|
}
|
|
5904
|
-
function
|
|
5931
|
+
function Pc(e) {
|
|
5905
5932
|
return e.map((e) => ({
|
|
5906
5933
|
...e,
|
|
5907
5934
|
metadata: {
|
|
@@ -5910,83 +5937,83 @@ function Mc(e) {
|
|
|
5910
5937
|
}
|
|
5911
5938
|
}));
|
|
5912
5939
|
}
|
|
5913
|
-
function
|
|
5914
|
-
return
|
|
5940
|
+
function Fc(e, t, n) {
|
|
5941
|
+
return Ec(n) && e.memoryOwnedBySelf(t) ? hc : 0;
|
|
5915
5942
|
}
|
|
5916
|
-
function
|
|
5917
|
-
if (
|
|
5943
|
+
function Ic(e, t, n) {
|
|
5944
|
+
if (Dc(n) !== "soft" || e.memoryOwnedBySelf(t)) return 0;
|
|
5918
5945
|
let r = t.metadata ?? {}, i = String(r.visibility ?? "").trim().toLowerCase(), a = String(r.workspace_kind ?? "").trim().toLowerCase();
|
|
5919
5946
|
if (i !== "shared" && a !== "shared") return 0;
|
|
5920
5947
|
let o = String(r.trust_state ?? "trusted").trim().toLowerCase();
|
|
5921
|
-
return o === "legacy_unknown" ?
|
|
5948
|
+
return o === "legacy_unknown" ? gc : o === "unreviewed" ? _c : 0;
|
|
5922
5949
|
}
|
|
5923
|
-
function
|
|
5950
|
+
function Lc(e) {
|
|
5924
5951
|
let t = e.trim().replaceAll("\\", "/");
|
|
5925
5952
|
t.startsWith("./") && (t = t.slice(2));
|
|
5926
5953
|
let n = t.split("/").filter((e) => e && e !== ".");
|
|
5927
5954
|
return n.length === 0 ? "" : n.join("/").toLowerCase();
|
|
5928
5955
|
}
|
|
5929
|
-
function
|
|
5930
|
-
let t =
|
|
5956
|
+
function Rc(e) {
|
|
5957
|
+
let t = Lc(e);
|
|
5931
5958
|
return t ? t.split("/") : [];
|
|
5932
5959
|
}
|
|
5933
|
-
function
|
|
5934
|
-
let t =
|
|
5960
|
+
function zc(e) {
|
|
5961
|
+
let t = Rc(e);
|
|
5935
5962
|
return t.length === 0 ? "" : t[t.length - 1] ?? "";
|
|
5936
5963
|
}
|
|
5937
|
-
function
|
|
5964
|
+
function Bc(e, t) {
|
|
5938
5965
|
return e.length === 0 || t.length === 0 ? !1 : e.length <= t.length ? t.slice(t.length - e.length).join("/") === e.join("/") : e.slice(e.length - t.length).join("/") === t.join("/");
|
|
5939
5966
|
}
|
|
5940
|
-
function
|
|
5967
|
+
function Vc(e) {
|
|
5941
5968
|
if (!Array.isArray(e)) return [];
|
|
5942
5969
|
let t = [], n = /* @__PURE__ */ new Set();
|
|
5943
5970
|
for (let r of e) {
|
|
5944
5971
|
if (typeof r != "string") continue;
|
|
5945
|
-
let e =
|
|
5972
|
+
let e = Lc(r);
|
|
5946
5973
|
!e || n.has(e) || (n.add(e), t.push(e));
|
|
5947
5974
|
}
|
|
5948
5975
|
return t;
|
|
5949
5976
|
}
|
|
5950
|
-
function
|
|
5977
|
+
function Hc(e) {
|
|
5951
5978
|
let t = (e.metadata ?? {}).files_modified;
|
|
5952
5979
|
if (!Array.isArray(t)) return [];
|
|
5953
5980
|
let n = [];
|
|
5954
5981
|
for (let e of t) {
|
|
5955
5982
|
if (typeof e != "string") continue;
|
|
5956
|
-
let t =
|
|
5983
|
+
let t = Lc(e);
|
|
5957
5984
|
t && n.push(t);
|
|
5958
5985
|
}
|
|
5959
5986
|
return n;
|
|
5960
5987
|
}
|
|
5961
|
-
function
|
|
5988
|
+
function Uc(e, t) {
|
|
5962
5989
|
if (t.length === 0) return 0;
|
|
5963
|
-
let n =
|
|
5990
|
+
let n = Hc(e);
|
|
5964
5991
|
if (n.length === 0) return 0;
|
|
5965
|
-
let r = [...new Set(n)].map((e) =>
|
|
5966
|
-
for (let e of r) i.some((t) =>
|
|
5992
|
+
let r = [...new Set(n)].map((e) => Rc(e)), i = [...new Set(t)].map((e) => Rc(e)), a = new Set(n.map((e) => zc(e)).filter(Boolean)), o = new Set(t.map((e) => zc(e)).filter(Boolean)), s = 0;
|
|
5993
|
+
for (let e of r) i.some((t) => Bc(e, t)) && (s += 1);
|
|
5967
5994
|
let c = 0;
|
|
5968
5995
|
for (let e of a) o.has(e) && (c += 1);
|
|
5969
5996
|
let l = s * .16 + c * .06;
|
|
5970
5997
|
return Math.min(.32, l);
|
|
5971
5998
|
}
|
|
5972
|
-
function
|
|
5973
|
-
let i = /* @__PURE__ */ new Date(), a =
|
|
5999
|
+
function Wc(e, t, n, r) {
|
|
6000
|
+
let i = /* @__PURE__ */ new Date(), a = Vc(r?.working_set_paths), o = t.map((t) => ({
|
|
5974
6001
|
item: t,
|
|
5975
|
-
combinedScore: t.score * 1.5 +
|
|
6002
|
+
combinedScore: t.score * 1.5 + wc(t.created_at, i) + Tc(t.kind) + Uc(t, a) + Fc(e, t, r) - Ic(e, t, r)
|
|
5976
6003
|
}));
|
|
5977
6004
|
return o.sort((e, t) => t.combinedScore - e.combinedScore), o.slice(0, n).map((e) => e.item);
|
|
5978
6005
|
}
|
|
5979
|
-
function
|
|
5980
|
-
let i =
|
|
5981
|
-
if (!
|
|
6006
|
+
function Gc(e, t, n = 10, r) {
|
|
6007
|
+
let i = Kc(e, t, n, r);
|
|
6008
|
+
if (!Oc(r) || !t || jc(t) || Mc(r)) return i;
|
|
5982
6009
|
let a = i.filter((t) => e.memoryOwnedBySelf(t)), o = a[0]?.score ?? -Infinity;
|
|
5983
|
-
if (a.length >=
|
|
5984
|
-
let s =
|
|
5985
|
-
for (let e of s) if (!c.has(e.id) && (c.add(e.id), l.push(e), u += 1, u >=
|
|
6010
|
+
if (a.length >= kc(r) && o >= Ac(r)) return i;
|
|
6011
|
+
let s = Pc(Kc(e, t, bc, Nc(r)).filter((t) => !e.memoryOwnedBySelf(t))), c = new Set(i.map((e) => e.id)), l = [...i], u = 0;
|
|
6012
|
+
for (let e of s) if (!c.has(e.id) && (c.add(e.id), l.push(e), u += 1, u >= bc)) break;
|
|
5986
6013
|
return l;
|
|
5987
6014
|
}
|
|
5988
|
-
function
|
|
5989
|
-
let i = Math.max(1, Math.trunc(n)), a =
|
|
6015
|
+
function Kc(e, t, n = 10, r) {
|
|
6016
|
+
let i = Math.max(1, Math.trunc(n)), a = Cc(t);
|
|
5990
6017
|
if (!a) return [];
|
|
5991
6018
|
let o = Math.min(Math.max(i * 4, i + 8), 200), s = [a], c = ["memory_items.active = 1", "memory_fts MATCH ?"], l = Na(r, {
|
|
5992
6019
|
actorId: e.actorId,
|
|
@@ -6004,7 +6031,7 @@ function Wc(e, t, n = 10, r) {
|
|
|
6004
6031
|
ORDER BY (score * 1.5 + recency) DESC, memory_items.created_at DESC, memory_items.id DESC
|
|
6005
6032
|
LIMIT ?
|
|
6006
6033
|
`;
|
|
6007
|
-
return s.push(o),
|
|
6034
|
+
return s.push(o), Wc(e, e.db.prepare(d).all(...s).map((e) => {
|
|
6008
6035
|
let t = { ...z(e.metadata_json) };
|
|
6009
6036
|
for (let n of [
|
|
6010
6037
|
"actor_id",
|
|
@@ -6038,7 +6065,7 @@ function Wc(e, t, n = 10, r) {
|
|
|
6038
6065
|
};
|
|
6039
6066
|
}), i, r);
|
|
6040
6067
|
}
|
|
6041
|
-
function
|
|
6068
|
+
function qc(e, t, n, r = 3, i = 3, a) {
|
|
6042
6069
|
let o = null;
|
|
6043
6070
|
if (n != null) {
|
|
6044
6071
|
let t = e.get(n);
|
|
@@ -6049,7 +6076,7 @@ function Gc(e, t, n, r = 3, i = 3, a) {
|
|
|
6049
6076
|
});
|
|
6050
6077
|
}
|
|
6051
6078
|
if (o == null && t) {
|
|
6052
|
-
let n =
|
|
6079
|
+
let n = Gc(e, t, 1, a ?? void 0);
|
|
6053
6080
|
if (n.length > 0) {
|
|
6054
6081
|
let e = n[0];
|
|
6055
6082
|
o = {
|
|
@@ -6059,9 +6086,9 @@ function Gc(e, t, n, r = 3, i = 3, a) {
|
|
|
6059
6086
|
};
|
|
6060
6087
|
}
|
|
6061
6088
|
}
|
|
6062
|
-
return o == null ? [] :
|
|
6089
|
+
return o == null ? [] : Jc(e, o, r, i, a);
|
|
6063
6090
|
}
|
|
6064
|
-
function
|
|
6091
|
+
function Jc(e, t, n, r, i) {
|
|
6065
6092
|
let a = t.id, o = t.created_at, s = t.session_id;
|
|
6066
6093
|
if (!a || !o) return [];
|
|
6067
6094
|
let c = Na(i, {
|
|
@@ -6079,7 +6106,7 @@ function Kc(e, t, n, r, i) {
|
|
|
6079
6106
|
${f}
|
|
6080
6107
|
WHERE ${d} AND memory_items.created_at > ?
|
|
6081
6108
|
ORDER BY memory_items.created_at ASC
|
|
6082
|
-
LIMIT ?`).all(...u, o, r), h =
|
|
6109
|
+
LIMIT ?`).all(...u, o, r), h = pc(e.db).select().from(W).where(D(k(W.id, a), k(W.active, 1))).get(), g = [...p.reverse()];
|
|
6083
6110
|
return h && g.push(h), g.push(...m), g.map((e) => {
|
|
6084
6111
|
let { metadata_json: t, ...n } = e;
|
|
6085
6112
|
return {
|
|
@@ -6089,10 +6116,10 @@ function Kc(e, t, n, r, i) {
|
|
|
6089
6116
|
};
|
|
6090
6117
|
});
|
|
6091
6118
|
}
|
|
6092
|
-
function
|
|
6119
|
+
function Yc(e) {
|
|
6093
6120
|
let t = /* @__PURE__ */ new Set(), n = [], r = [];
|
|
6094
6121
|
for (let i of e) {
|
|
6095
|
-
let e =
|
|
6122
|
+
let e = ds(i);
|
|
6096
6123
|
if (e == null) {
|
|
6097
6124
|
r.push(String(i));
|
|
6098
6125
|
continue;
|
|
@@ -6104,8 +6131,8 @@ function qc(e) {
|
|
|
6104
6131
|
invalid: r
|
|
6105
6132
|
};
|
|
6106
6133
|
}
|
|
6107
|
-
function
|
|
6108
|
-
let c = `${e.title} ${e.body_text} ${e.tags_text}`.toLowerCase(), l = r.filter((e) => c.includes(e)), u = t === "query" || t === "query+id_lookup" ? e.score : null, d =
|
|
6134
|
+
function Xc(e, t, n, r, i, a, o, s) {
|
|
6135
|
+
let c = `${e.title} ${e.body_text} ${e.tags_text}`.toLowerCase(), l = r.filter((e) => c.includes(e)), u = t === "query" || t === "query+id_lookup" ? e.score : null, d = wc(e.created_at, s), f = Tc(e.kind), p = null;
|
|
6109
6136
|
return u != null && (p = u * 1.5 + d + f), {
|
|
6110
6137
|
id: e.id,
|
|
6111
6138
|
kind: e.kind,
|
|
@@ -6136,14 +6163,14 @@ function Jc(e, t, n, r, i, a, o, s) {
|
|
|
6136
6163
|
} : null
|
|
6137
6164
|
};
|
|
6138
6165
|
}
|
|
6139
|
-
function
|
|
6166
|
+
function Zc(e, t, n) {
|
|
6140
6167
|
if (t.length === 0) return {
|
|
6141
6168
|
items: [],
|
|
6142
6169
|
missingNotFound: [],
|
|
6143
6170
|
missingProjectMismatch: [],
|
|
6144
6171
|
missingFilterMismatch: []
|
|
6145
6172
|
};
|
|
6146
|
-
let r =
|
|
6173
|
+
let r = pc(e.db).select().from(W).where(D(k(W.active, 1), j(W.id, t))).all(), i = new Set(r.map((e) => e.id)), a = t.map(() => "?").join(", "), o = r, s = new Set(i);
|
|
6147
6174
|
if (n.project) {
|
|
6148
6175
|
let r = Na({ project: n.project }, {
|
|
6149
6176
|
actorId: e.actorId,
|
|
@@ -6189,16 +6216,16 @@ function Yc(e, t, n) {
|
|
|
6189
6216
|
missingFilterMismatch: m
|
|
6190
6217
|
};
|
|
6191
6218
|
}
|
|
6192
|
-
function
|
|
6219
|
+
function Qc(e, t) {
|
|
6193
6220
|
if (t.size === 0) return /* @__PURE__ */ new Map();
|
|
6194
|
-
let n = [...t].sort((e, t) => e - t), r =
|
|
6221
|
+
let n = [...t].sort((e, t) => e - t), r = pc(e.db).select({
|
|
6195
6222
|
id: U.id,
|
|
6196
6223
|
project: U.project
|
|
6197
6224
|
}).from(U).where(j(U.id, n)).all();
|
|
6198
6225
|
return new Map(r.map((e) => [e.id, e.project]));
|
|
6199
6226
|
}
|
|
6200
|
-
function
|
|
6201
|
-
let o = a?.includePackContext ?? !1, s = (t ?? "").trim(), { ordered: c, invalid: l } =
|
|
6227
|
+
function $c(e, t, n, r = 10, i, a) {
|
|
6228
|
+
let o = a?.includePackContext ?? !1, s = (t ?? "").trim(), { ordered: c, invalid: l } = Yc(n ?? []), u = [];
|
|
6202
6229
|
if (l.length > 0 && u.push({
|
|
6203
6230
|
code: "INVALID_ARGUMENT",
|
|
6204
6231
|
field: "ids",
|
|
@@ -6221,10 +6248,10 @@ function Zc(e, t, n, r = 10, i, a) {
|
|
|
6221
6248
|
}
|
|
6222
6249
|
};
|
|
6223
6250
|
let d = [];
|
|
6224
|
-
s && (d =
|
|
6251
|
+
s && (d = Gc(e, s, Math.max(1, Math.trunc(r)), i ?? void 0));
|
|
6225
6252
|
let f = /* @__PURE__ */ new Map();
|
|
6226
6253
|
for (let e = 0; e < d.length; e++) f.set(d[e].id, e + 1);
|
|
6227
|
-
let { items: p, missingNotFound: m, missingProjectMismatch: h, missingFilterMismatch: g } =
|
|
6254
|
+
let { items: p, missingNotFound: m, missingProjectMismatch: h, missingFilterMismatch: g } = Zc(e, c, i ?? {}), _ = new Map(p.map((e) => [e.id, e])), v = new Set(c), y = /* @__PURE__ */ new Set(), b = [];
|
|
6228
6255
|
for (let e of d) {
|
|
6229
6256
|
y.add(e.id);
|
|
6230
6257
|
let t = v.has(e.id) ? "query+id_lookup" : "query";
|
|
@@ -6243,7 +6270,7 @@ function Zc(e, t, n, r = 10, i, a) {
|
|
|
6243
6270
|
rank: null
|
|
6244
6271
|
}), y.add(e));
|
|
6245
6272
|
}
|
|
6246
|
-
let x = s ? (s.match(/[A-Za-z0-9_]+/g) ?? []).map((e) => e.toLowerCase()) : [], S =
|
|
6273
|
+
let x = s ? (s.match(/[A-Za-z0-9_]+/g) ?? []).map((e) => e.toLowerCase()) : [], S = Qc(e, new Set(b.map(({ item: e }) => e.session_id).filter((e) => e > 0))), C = /* @__PURE__ */ new Date(), w = b.map(({ item: e, source: t, rank: n }) => Xc(e, t, n, x, i?.project ?? null, S.get(e.session_id) ?? null, o, C)), T = c.filter((e) => !y.has(e));
|
|
6247
6274
|
return m.length > 0 && u.push({
|
|
6248
6275
|
code: "NOT_FOUND",
|
|
6249
6276
|
field: "ids",
|
|
@@ -6274,7 +6301,7 @@ function Zc(e, t, n, r = 10, i, a) {
|
|
|
6274
6301
|
}
|
|
6275
6302
|
//#endregion
|
|
6276
6303
|
//#region src/pack.ts
|
|
6277
|
-
var
|
|
6304
|
+
var el = {
|
|
6278
6305
|
decision: 0,
|
|
6279
6306
|
feature: 1,
|
|
6280
6307
|
bugfix: 2,
|
|
@@ -6283,11 +6310,11 @@ var Qc = {
|
|
|
6283
6310
|
discovery: 5,
|
|
6284
6311
|
exploration: 6,
|
|
6285
6312
|
note: 7
|
|
6286
|
-
},
|
|
6287
|
-
function
|
|
6313
|
+
}, tl = 365, nl = "todo todos task tasks pending follow up follow-up next resume continue backlog pick up pick-up", rl = "session summary recap remember last time previous work";
|
|
6314
|
+
function il(e) {
|
|
6288
6315
|
return Math.ceil(e.length / 4);
|
|
6289
6316
|
}
|
|
6290
|
-
function
|
|
6317
|
+
function al(e) {
|
|
6291
6318
|
let t = [
|
|
6292
6319
|
`[${e.id}]`,
|
|
6293
6320
|
`(${e.kind})`,
|
|
@@ -6295,11 +6322,11 @@ function rl(e) {
|
|
|
6295
6322
|
];
|
|
6296
6323
|
return e.body_text && t.push("-", e.body_text), t.join(" ");
|
|
6297
6324
|
}
|
|
6298
|
-
function
|
|
6325
|
+
function ol(e, t) {
|
|
6299
6326
|
let n = `## ${e}`;
|
|
6300
|
-
return t.length === 0 ? `${n}\n` : [n, ...t.map(
|
|
6327
|
+
return t.length === 0 ? `${n}\n` : [n, ...t.map(al)].join("\n");
|
|
6301
6328
|
}
|
|
6302
|
-
function
|
|
6329
|
+
function sl(e, t) {
|
|
6303
6330
|
let n = t?.duplicateIds.get(e.id), r = {
|
|
6304
6331
|
id: e.id,
|
|
6305
6332
|
kind: e.kind,
|
|
@@ -6311,18 +6338,18 @@ function al(e, t) {
|
|
|
6311
6338
|
};
|
|
6312
6339
|
return n && n.size > 0 && (r.support_count = 1 + n.size, r.duplicate_ids = [...n].sort((e, t) => e - t)), r;
|
|
6313
6340
|
}
|
|
6314
|
-
function
|
|
6341
|
+
function cl(e) {
|
|
6315
6342
|
return e.trim().toLowerCase().split(/\s+/).join(" ");
|
|
6316
6343
|
}
|
|
6317
|
-
function
|
|
6344
|
+
function ll(e) {
|
|
6318
6345
|
if (e.kind === "session_summary") return null;
|
|
6319
|
-
let t =
|
|
6346
|
+
let t = cl(e.title), n = cl(e.body_text);
|
|
6320
6347
|
return !t && !n ? null : `${e.kind.length}:${e.kind}|${t.length}:${t}|${n.length}:${n}`;
|
|
6321
6348
|
}
|
|
6322
|
-
function
|
|
6349
|
+
function ul(e, t) {
|
|
6323
6350
|
let n = [];
|
|
6324
6351
|
for (let r of e) {
|
|
6325
|
-
let e =
|
|
6352
|
+
let e = ll(r);
|
|
6326
6353
|
if (e === null) {
|
|
6327
6354
|
n.push(r);
|
|
6328
6355
|
continue;
|
|
@@ -6341,19 +6368,19 @@ function cl(e, t) {
|
|
|
6341
6368
|
}
|
|
6342
6369
|
return n;
|
|
6343
6370
|
}
|
|
6344
|
-
function
|
|
6371
|
+
function dl(e, t) {
|
|
6345
6372
|
let n = new Set((t.toLowerCase().match(/[a-z0-9_]+/g) ?? []).filter(Boolean));
|
|
6346
6373
|
return n.size === 0 ? e : [...e].sort((e, t) => {
|
|
6347
|
-
let r =
|
|
6374
|
+
let r = fl(e.tags_text, n), i = fl(t.tags_text, n);
|
|
6348
6375
|
return i === r ? (t.created_at ?? "").localeCompare(e.created_at ?? "") : i - r;
|
|
6349
6376
|
});
|
|
6350
6377
|
}
|
|
6351
|
-
function
|
|
6378
|
+
function fl(e, t) {
|
|
6352
6379
|
let n = new Set(e.split(/\s+/).filter(Boolean)), r = 0;
|
|
6353
6380
|
for (let e of t) n.has(e) && r++;
|
|
6354
6381
|
return r;
|
|
6355
6382
|
}
|
|
6356
|
-
function
|
|
6383
|
+
function pl(e) {
|
|
6357
6384
|
let t = e.toLowerCase();
|
|
6358
6385
|
for (let e of [
|
|
6359
6386
|
"todo",
|
|
@@ -6380,7 +6407,7 @@ function dl(e) {
|
|
|
6380
6407
|
]) if (t.includes(e)) return !0;
|
|
6381
6408
|
return !1;
|
|
6382
6409
|
}
|
|
6383
|
-
function
|
|
6410
|
+
function ml(e) {
|
|
6384
6411
|
let t = e.toLowerCase();
|
|
6385
6412
|
for (let e of [
|
|
6386
6413
|
"remember",
|
|
@@ -6404,7 +6431,7 @@ function fl(e) {
|
|
|
6404
6431
|
]) if (t.includes(e)) return !0;
|
|
6405
6432
|
return !1;
|
|
6406
6433
|
}
|
|
6407
|
-
function
|
|
6434
|
+
function hl(e) {
|
|
6408
6435
|
return {
|
|
6409
6436
|
id: e.id,
|
|
6410
6437
|
kind: e.kind,
|
|
@@ -6419,57 +6446,57 @@ function pl(e) {
|
|
|
6419
6446
|
metadata: e.metadata_json
|
|
6420
6447
|
};
|
|
6421
6448
|
}
|
|
6422
|
-
function
|
|
6449
|
+
function gl(e) {
|
|
6423
6450
|
let t = Date.parse(e);
|
|
6424
6451
|
return Number.isNaN(t) ? -Infinity : t;
|
|
6425
6452
|
}
|
|
6426
|
-
function
|
|
6453
|
+
function _l(e, t) {
|
|
6427
6454
|
let n = Date.now() - t * 864e5;
|
|
6428
|
-
return e.filter((e) =>
|
|
6455
|
+
return e.filter((e) => gl(e.created_at) >= n);
|
|
6429
6456
|
}
|
|
6430
|
-
function
|
|
6457
|
+
function vl(e, t) {
|
|
6431
6458
|
let n = [...e].sort((e, t) => (t.created_at ?? "").localeCompare(e.created_at ?? ""));
|
|
6432
6459
|
return n.sort((e, t) => {
|
|
6433
6460
|
let n = (e) => e === "note" ? 0 : e === "decision" ? 1 : e === "observation" ? 2 : 3;
|
|
6434
6461
|
return n(e.kind) - n(t.kind);
|
|
6435
6462
|
}), n.slice(0, t);
|
|
6436
6463
|
}
|
|
6437
|
-
function
|
|
6464
|
+
function yl(e, t) {
|
|
6438
6465
|
let n = [...e].sort((e, t) => (t.created_at ?? "").localeCompare(e.created_at ?? ""));
|
|
6439
6466
|
return n.sort((e, t) => {
|
|
6440
6467
|
let n = (e) => e === "session_summary" ? 0 : e === "decision" ? 1 : e === "note" ? 2 : e === "observation" ? 3 : e === "entities" ? 4 : 5;
|
|
6441
6468
|
return n(e.kind) - n(t.kind);
|
|
6442
6469
|
}), n.slice(0, t);
|
|
6443
6470
|
}
|
|
6444
|
-
function
|
|
6471
|
+
function bl(e, t, n) {
|
|
6445
6472
|
let r = Math.max(t * 3, t);
|
|
6446
|
-
return
|
|
6473
|
+
return vl(e.recent(r, n ?? null).map(hl), t);
|
|
6447
6474
|
}
|
|
6448
|
-
function
|
|
6475
|
+
function xl(e, t, n) {
|
|
6449
6476
|
let r = {
|
|
6450
6477
|
...n ?? {},
|
|
6451
6478
|
kind: "session_summary"
|
|
6452
|
-
}, i = e.recent(t, r).map(
|
|
6479
|
+
}, i = e.recent(t, r).map(hl);
|
|
6453
6480
|
if (i.length >= t) return i.slice(0, t);
|
|
6454
|
-
let a = Math.max(t * 3, t), o = e.recent(a, n ?? null).map(
|
|
6481
|
+
let a = Math.max(t * 3, t), o = e.recent(a, n ?? null).map(hl), s = new Set(i.map((e) => e.id)), c = vl(o.filter((e) => !s.has(e.id)), t - i.length);
|
|
6455
6482
|
return [...i, ...c];
|
|
6456
6483
|
}
|
|
6457
|
-
function
|
|
6484
|
+
function Sl(e) {
|
|
6458
6485
|
if (e == null || typeof e == "boolean") return null;
|
|
6459
6486
|
let t = Number(e);
|
|
6460
6487
|
if (!Number.isFinite(t)) return null;
|
|
6461
6488
|
let n = Math.trunc(t);
|
|
6462
6489
|
return n < 0 ? null : n;
|
|
6463
6490
|
}
|
|
6464
|
-
function
|
|
6491
|
+
function Cl(e) {
|
|
6465
6492
|
let t = [], n = /* @__PURE__ */ new Set();
|
|
6466
6493
|
for (let r of e) {
|
|
6467
|
-
let e =
|
|
6494
|
+
let e = Sl(r);
|
|
6468
6495
|
e == null || e <= 0 || n.has(e) || (n.add(e), t.push(e));
|
|
6469
6496
|
}
|
|
6470
6497
|
return t;
|
|
6471
6498
|
}
|
|
6472
|
-
function
|
|
6499
|
+
function wl(e) {
|
|
6473
6500
|
if (!Array.isArray(e)) return {
|
|
6474
6501
|
ids: [],
|
|
6475
6502
|
valid: !1
|
|
@@ -6479,11 +6506,11 @@ function Sl(e) {
|
|
|
6479
6506
|
valid: !1
|
|
6480
6507
|
};
|
|
6481
6508
|
return {
|
|
6482
|
-
ids:
|
|
6509
|
+
ids: Cl(e),
|
|
6483
6510
|
valid: !0
|
|
6484
6511
|
};
|
|
6485
6512
|
}
|
|
6486
|
-
function
|
|
6513
|
+
function Tl(e) {
|
|
6487
6514
|
if (!e) return {};
|
|
6488
6515
|
if (typeof e == "string") try {
|
|
6489
6516
|
let t = JSON.parse(e);
|
|
@@ -6493,7 +6520,7 @@ function Cl(e) {
|
|
|
6493
6520
|
}
|
|
6494
6521
|
return typeof e == "object" && !Array.isArray(e) ? e : {};
|
|
6495
6522
|
}
|
|
6496
|
-
function
|
|
6523
|
+
function El(e, t) {
|
|
6497
6524
|
let n = t ? qt(t) : null, r = "CASE WHEN json_valid(metadata_json) = 1 THEN json_extract(metadata_json, '$.project') ELSE NULL END", i = t ? e.db.prepare(`SELECT metadata_json, tokens_read
|
|
6498
6525
|
FROM usage_events
|
|
6499
6526
|
WHERE event = 'pack'
|
|
@@ -6501,15 +6528,15 @@ function wl(e, t) {
|
|
|
6501
6528
|
ORDER BY created_at DESC
|
|
6502
6529
|
LIMIT 25`).all(t, n ?? t) : e.db.prepare("SELECT metadata_json, tokens_read\n FROM usage_events\n WHERE event = 'pack'\n ORDER BY created_at DESC\n LIMIT 25").all();
|
|
6503
6530
|
for (let e of i) {
|
|
6504
|
-
let r =
|
|
6531
|
+
let r = Tl(e.metadata_json);
|
|
6505
6532
|
if (t != null) {
|
|
6506
6533
|
let e = typeof r.project == "string" ? r.project : null;
|
|
6507
6534
|
if (e !== t && e !== n) continue;
|
|
6508
6535
|
}
|
|
6509
6536
|
if (!("pack_item_ids" in r)) continue;
|
|
6510
|
-
let { ids: i, valid: a } =
|
|
6537
|
+
let { ids: i, valid: a } = wl(r.pack_item_ids);
|
|
6511
6538
|
if (!a) continue;
|
|
6512
|
-
let o =
|
|
6539
|
+
let o = Sl(r.pack_tokens) ?? Sl(e.tokens_read);
|
|
6513
6540
|
if (o != null) return {
|
|
6514
6541
|
previousPackIds: i,
|
|
6515
6542
|
previousPackTokens: o
|
|
@@ -6520,20 +6547,20 @@ function wl(e, t) {
|
|
|
6520
6547
|
previousPackTokens: null
|
|
6521
6548
|
};
|
|
6522
6549
|
}
|
|
6523
|
-
function
|
|
6550
|
+
function Dl(e, t) {
|
|
6524
6551
|
if (!t) return null;
|
|
6525
6552
|
let n = qt(t);
|
|
6526
6553
|
return e.db.prepare("SELECT id\n FROM sessions\n WHERE project = ? OR project = ?\n ORDER BY started_at DESC, id DESC\n LIMIT 1").get(t, n)?.id ?? null;
|
|
6527
6554
|
}
|
|
6528
|
-
function
|
|
6529
|
-
return
|
|
6555
|
+
function Ol(e) {
|
|
6556
|
+
return Sl(Tl(e.metadata).discovery_tokens) ?? Math.max(2e3, il(`${e.title} ${e.body_text}`.trim()));
|
|
6530
6557
|
}
|
|
6531
|
-
function
|
|
6532
|
-
let t =
|
|
6558
|
+
function kl(e) {
|
|
6559
|
+
let t = Tl(e.metadata).discovery_group;
|
|
6533
6560
|
return typeof t == "string" && t.trim().length > 0 ? t.trim() : `memory:${e.id}`;
|
|
6534
6561
|
}
|
|
6535
|
-
function
|
|
6536
|
-
let t =
|
|
6562
|
+
function Al(e) {
|
|
6563
|
+
let t = Tl(e.metadata), n = Sl(t.discovery_tokens);
|
|
6537
6564
|
return n != null && n > 0 ? {
|
|
6538
6565
|
tokens: n,
|
|
6539
6566
|
source: typeof t.discovery_source == "string" && t.discovery_source ? t.discovery_source : "known"
|
|
@@ -6542,16 +6569,16 @@ function Ol(e) {
|
|
|
6542
6569
|
source: "unknown"
|
|
6543
6570
|
};
|
|
6544
6571
|
}
|
|
6545
|
-
function
|
|
6546
|
-
return
|
|
6572
|
+
function jl(e) {
|
|
6573
|
+
return Tl(e.metadata).discovery_source === "usage" ? "usage" : "estimate";
|
|
6547
6574
|
}
|
|
6548
|
-
function
|
|
6549
|
-
let n = (/* @__PURE__ */ new Date()).toISOString(), r =
|
|
6575
|
+
function Ml(e, t) {
|
|
6576
|
+
let n = (/* @__PURE__ */ new Date()).toISOString(), r = Sl(t.pack_tokens) ?? 0, i = Sl(t.tokens_saved) ?? 0, a = Dl(e, typeof t.project == "string" ? t.project : null);
|
|
6550
6577
|
try {
|
|
6551
6578
|
e.db.prepare("INSERT INTO usage_events(session_id, event, tokens_read, tokens_written, tokens_saved, created_at, metadata_json)\n VALUES (?, 'pack', ?, 0, ?, ?, ?)").run(a, r, i, n, JSON.stringify(t));
|
|
6552
6579
|
} catch {}
|
|
6553
6580
|
}
|
|
6554
|
-
function
|
|
6581
|
+
function Nl(e, t, n, r, i) {
|
|
6555
6582
|
let a = /* @__PURE__ */ new Map();
|
|
6556
6583
|
for (let e of t) {
|
|
6557
6584
|
let t = a.get(e.id);
|
|
@@ -6564,46 +6591,46 @@ function jl(e, t, n, r, i) {
|
|
|
6564
6591
|
(!t || e.score > t.score) && a.set(e.id, e);
|
|
6565
6592
|
}
|
|
6566
6593
|
return {
|
|
6567
|
-
merged:
|
|
6594
|
+
merged: Wc(e, [...a.values()], r, i),
|
|
6568
6595
|
ftsCount: t.length,
|
|
6569
6596
|
semanticCount: o
|
|
6570
6597
|
};
|
|
6571
6598
|
}
|
|
6572
|
-
function
|
|
6573
|
-
let o = Math.max(1, Math.trunc(n)), s = !1, c = 0, l = 0, u, d =
|
|
6599
|
+
function Pl(e, t, n = 10, r = null, i, a) {
|
|
6600
|
+
let o = Math.max(1, Math.trunc(n)), s = !1, c = 0, l = 0, u, d = pl(t), f = !d && ml(t);
|
|
6574
6601
|
if (d) {
|
|
6575
|
-
let n =
|
|
6602
|
+
let n = Gc(e, `${t} ${nl}`.trim(), o, i);
|
|
6576
6603
|
if (c = n.length, a && a.length > 0) {
|
|
6577
|
-
let t =
|
|
6604
|
+
let t = Nl(e, n, a, o, i);
|
|
6578
6605
|
n = t.merged, l = t.semanticCount;
|
|
6579
6606
|
}
|
|
6580
|
-
if (n.length === 0) s = !0, u =
|
|
6607
|
+
if (n.length === 0) s = !0, u = bl(e, o, i);
|
|
6581
6608
|
else {
|
|
6582
|
-
let e =
|
|
6583
|
-
u =
|
|
6609
|
+
let e = _l(n, tl);
|
|
6610
|
+
u = vl(e.length > 0 ? e : n, o);
|
|
6584
6611
|
}
|
|
6585
6612
|
} else if (f) {
|
|
6586
|
-
let n =
|
|
6587
|
-
if (c = n.length, n.length === 0 && (n =
|
|
6613
|
+
let n = Gc(e, t.trim().length > 0 ? t : rl, o, i);
|
|
6614
|
+
if (c = n.length, n.length === 0 && (n = Gc(e, rl, o, {
|
|
6588
6615
|
...i ?? {},
|
|
6589
6616
|
kind: "session_summary"
|
|
6590
6617
|
}), c = n.length), a && a.length > 0) {
|
|
6591
|
-
let t =
|
|
6618
|
+
let t = Nl(e, n, a, o, i);
|
|
6592
6619
|
n = t.merged, l = t.semanticCount;
|
|
6593
6620
|
}
|
|
6594
|
-
u =
|
|
6621
|
+
u = yl(n, o), u.length === 0 && (s = !0, u = xl(e, o, i));
|
|
6595
6622
|
let r = u[0]?.id;
|
|
6596
6623
|
if (r != null) {
|
|
6597
|
-
let t = Math.max(0, Math.floor(o / 2)), n =
|
|
6598
|
-
n.length > 0 && (u = n.map(
|
|
6624
|
+
let t = Math.max(0, Math.floor(o / 2)), n = qc(e, void 0, r, t, Math.max(0, o - t - 1), i ?? null);
|
|
6625
|
+
n.length > 0 && (u = n.map(hl));
|
|
6599
6626
|
}
|
|
6600
6627
|
} else {
|
|
6601
|
-
let n =
|
|
6628
|
+
let n = Gc(e, t, o, i);
|
|
6602
6629
|
if (a && a.length > 0) {
|
|
6603
|
-
let t =
|
|
6630
|
+
let t = Nl(e, n, a, o, i);
|
|
6604
6631
|
u = t.merged, c = t.ftsCount, l = t.semanticCount;
|
|
6605
6632
|
} else u = n, c = u.length;
|
|
6606
|
-
u.length === 0 && (s = !0, u = e.recent(o, i ?? null).map(
|
|
6633
|
+
u.length === 0 && (s = !0, u = e.recent(o, i ?? null).map(hl));
|
|
6607
6634
|
}
|
|
6608
6635
|
let p = u.filter((e) => e.kind === "session_summary").slice(0, 1);
|
|
6609
6636
|
if (p.length === 0) {
|
|
@@ -6628,7 +6655,7 @@ function Ml(e, t, n = 10, r = null, i, a) {
|
|
|
6628
6655
|
}];
|
|
6629
6656
|
}
|
|
6630
6657
|
}
|
|
6631
|
-
let m = u.filter((e) => e.kind !== "session_summary").slice(0, 3), h = new Set(m.map((e) => e.id)), g = Object.keys(
|
|
6658
|
+
let m = u.filter((e) => e.kind !== "session_summary").slice(0, 3), h = new Set(m.map((e) => e.id)), g = Object.keys(el), _ = [...u].filter((e) => e.kind !== "session_summary" && !h.has(e.id)).sort((e, t) => (el[e.kind] ?? 99) - (el[t.kind] ?? 99));
|
|
6632
6659
|
f && _.length === 0 && (_ = u.filter((e) => e.kind !== "session_summary")), _.length === 0 && (_ = e.recentByKinds(g, Math.max(o * 3, 10), i ?? null).map((e) => ({
|
|
6633
6660
|
id: e.id,
|
|
6634
6661
|
kind: e.kind,
|
|
@@ -6641,55 +6668,55 @@ function Ml(e, t, n = 10, r = null, i, a) {
|
|
|
6641
6668
|
score: 0,
|
|
6642
6669
|
session_id: e.session_id,
|
|
6643
6670
|
metadata: e.metadata_json
|
|
6644
|
-
}))), _.length === 0 && (_ = [...m]), _ =
|
|
6671
|
+
}))), _.length === 0 && (_ = [...m]), _ = dl(_, t);
|
|
6645
6672
|
let v = {
|
|
6646
6673
|
canonicalByKey: /* @__PURE__ */ new Map(),
|
|
6647
6674
|
duplicateIds: /* @__PURE__ */ new Map()
|
|
6648
6675
|
};
|
|
6649
|
-
p =
|
|
6676
|
+
p = ul(p, v), m = ul(m, v), _ = ul(_, v);
|
|
6650
6677
|
let y = p, b = m, x = _;
|
|
6651
6678
|
if (r != null && r > 0) {
|
|
6652
6679
|
let e = 0;
|
|
6653
6680
|
y = [];
|
|
6654
6681
|
for (let t of p) {
|
|
6655
|
-
let n =
|
|
6682
|
+
let n = il(al(t));
|
|
6656
6683
|
if (e + n > r) break;
|
|
6657
6684
|
e += n, y.push(t);
|
|
6658
6685
|
}
|
|
6659
6686
|
b = [];
|
|
6660
6687
|
for (let t of m) {
|
|
6661
|
-
let n =
|
|
6688
|
+
let n = il(al(t));
|
|
6662
6689
|
if (e + n > r) break;
|
|
6663
6690
|
e += n, b.push(t);
|
|
6664
6691
|
}
|
|
6665
6692
|
x = [];
|
|
6666
6693
|
for (let t of _) {
|
|
6667
|
-
let n =
|
|
6694
|
+
let n = il(al(t));
|
|
6668
6695
|
if (e + n > r) break;
|
|
6669
6696
|
e += n, x.push(t);
|
|
6670
6697
|
}
|
|
6671
6698
|
}
|
|
6672
6699
|
let S = [
|
|
6673
|
-
|
|
6674
|
-
|
|
6675
|
-
|
|
6676
|
-
].join("\n\n"), C =
|
|
6700
|
+
ol("Summary", y),
|
|
6701
|
+
ol("Timeline", b),
|
|
6702
|
+
ol("Observations", x)
|
|
6703
|
+
].join("\n\n"), C = il(S), w = /* @__PURE__ */ new Set(), T = [], E = [], ee = [];
|
|
6677
6704
|
for (let e of [
|
|
6678
6705
|
...y,
|
|
6679
6706
|
...b,
|
|
6680
6707
|
...x
|
|
6681
|
-
]) w.has(e.id) || (w.add(e.id), E.push(e), T.push(
|
|
6682
|
-
let { previousPackIds: te, previousPackTokens: D } =
|
|
6708
|
+
]) w.has(e.id) || (w.add(e.id), E.push(e), T.push(sl(e, v)), ee.push(e.id));
|
|
6709
|
+
let { previousPackIds: te, previousPackTokens: D } = El(e, i?.project ?? null), O = te != null && D != null, k = new Set(te ?? []), A = new Set(ee), ne = O ? ee.filter((e) => !k.has(e)) : [], j = O ? (te ?? []).filter((e) => !A.has(e)) : [], re = O ? ee.filter((e) => k.has(e)) : [], ie = O ? C - (D ?? 0) : 0, ae = E.reduce((e, t) => e + Ol(t), 0), oe = /* @__PURE__ */ new Map();
|
|
6683
6710
|
for (let e of E) {
|
|
6684
|
-
let t =
|
|
6711
|
+
let t = kl(e), n = Ol(e);
|
|
6685
6712
|
n > (oe.get(t) ?? 0) && oe.set(t, n);
|
|
6686
6713
|
}
|
|
6687
6714
|
let se = [...oe.values()].reduce((e, t) => e + t, 0), ce = Math.max(0, se - C), M = 0, N = 0, P = 0, F = {};
|
|
6688
6715
|
for (let e of E) {
|
|
6689
|
-
let t =
|
|
6716
|
+
let t = Al(e);
|
|
6690
6717
|
t.tokens > 0 ? (M += t.tokens, N += 1, F[t.source] = (F[t.source] ?? 0) + 1) : P += 1;
|
|
6691
6718
|
}
|
|
6692
|
-
let le = Math.max(0, M - C), ue = M > 0 ? M / Math.max(C, 1) : null, I = E.map(
|
|
6719
|
+
let le = Math.max(0, M - C), ue = M > 0 ? M / Math.max(C, 1) : null, I = E.map(jl), L = I.filter((e) => e === "usage").length, de = I.length - L, fe = L > 0 && de > 0 ? "mixed" : L > 0 ? "usage" : "estimate", pe = se > 0 ? C / se : null, me = se > 0 ? C - se : null, he = s ? "recent" : null, ge = d ? "task" : f ? "recall" : "default", _e = {
|
|
6693
6720
|
total_items: T.length,
|
|
6694
6721
|
pack_tokens: C,
|
|
6695
6722
|
fallback_used: s,
|
|
@@ -6725,7 +6752,7 @@ function Ml(e, t, n = 10, r = null, i, a) {
|
|
|
6725
6752
|
fuzzy: 0
|
|
6726
6753
|
}
|
|
6727
6754
|
};
|
|
6728
|
-
return
|
|
6755
|
+
return Ml(e, _e), {
|
|
6729
6756
|
context: t,
|
|
6730
6757
|
items: T,
|
|
6731
6758
|
item_ids: ee,
|
|
@@ -6733,7 +6760,7 @@ function Ml(e, t, n = 10, r = null, i, a) {
|
|
|
6733
6760
|
metrics: _e
|
|
6734
6761
|
};
|
|
6735
6762
|
}
|
|
6736
|
-
async function
|
|
6763
|
+
async function Fl(e, t, n = 10, r = null, i) {
|
|
6737
6764
|
let a = [];
|
|
6738
6765
|
try {
|
|
6739
6766
|
a = (await Xo(e.db, t, n, { project: i?.project })).map((e) => {
|
|
@@ -6757,24 +6784,24 @@ async function Nl(e, t, n = 10, r = null, i) {
|
|
|
6757
6784
|
};
|
|
6758
6785
|
});
|
|
6759
6786
|
} catch {}
|
|
6760
|
-
return
|
|
6787
|
+
return Pl(e, t, n, r, i, a);
|
|
6761
6788
|
}
|
|
6762
6789
|
//#endregion
|
|
6763
6790
|
//#region src/raw-event-flush.ts
|
|
6764
|
-
var
|
|
6765
|
-
function
|
|
6791
|
+
var Il = "raw_events_v1";
|
|
6792
|
+
function Ll(e, t = 280) {
|
|
6766
6793
|
let n = e.replace(/\s+/g, " ").trim();
|
|
6767
6794
|
return n.length <= t ? n : `${n.slice(0, t - 3).trimEnd()}...`;
|
|
6768
6795
|
}
|
|
6769
|
-
function
|
|
6796
|
+
function Rl(e) {
|
|
6770
6797
|
let t = (e ?? "").trim().toLowerCase();
|
|
6771
6798
|
return t === "openai" ? "OpenAI" : t === "anthropic" ? "Anthropic" : t ? t.charAt(0).toUpperCase() + t.slice(1) : "Observer";
|
|
6772
6799
|
}
|
|
6773
|
-
function
|
|
6774
|
-
let n =
|
|
6775
|
-
return e instanceof
|
|
6800
|
+
function zl(e, t) {
|
|
6801
|
+
let n = Rl(t), r = String(e.message ?? "").trim().toLowerCase();
|
|
6802
|
+
return e instanceof Ks ? `${n} authentication failed. Refresh credentials and retry.` : e.name === "TimeoutError" || r.includes("timeout") ? `${n} request timed out during raw-event processing.` : r === "observer failed during raw-event flush" || r === "observer produced no storable output for raw-event flush" ? `${n} returned no usable output for raw-event processing.` : /parse|xml|json/i.test(r) ? `${n} response could not be processed.` : `${n} processing failed during raw-event ingestion.`;
|
|
6776
6803
|
}
|
|
6777
|
-
function
|
|
6804
|
+
function Bl(e) {
|
|
6778
6805
|
let t = 0, n = 0;
|
|
6779
6806
|
for (let r of e) r.type === "user_prompt" && t++, r.type === "tool.execute.after" && n++;
|
|
6780
6807
|
let r = [];
|
|
@@ -6786,12 +6813,11 @@ function Rl(e) {
|
|
|
6786
6813
|
}
|
|
6787
6814
|
let i = 0;
|
|
6788
6815
|
if (r.length > 0) {
|
|
6789
|
-
let e = r[0]
|
|
6790
|
-
|
|
6791
|
-
|
|
6792
|
-
|
|
6793
|
-
|
|
6794
|
-
i = Math.max(0, t - e);
|
|
6816
|
+
let e = r[0];
|
|
6817
|
+
if (e == null) throw Error("Expected timestamp when tsValues is non-empty");
|
|
6818
|
+
let t = r.slice(1), n = e, a = e;
|
|
6819
|
+
for (let e of t) e < n && (n = e), e > a && (a = e);
|
|
6820
|
+
i = Math.max(0, a - n);
|
|
6795
6821
|
}
|
|
6796
6822
|
let a = /* @__PURE__ */ new Set(), o = /* @__PURE__ */ new Set();
|
|
6797
6823
|
for (let t of e) {
|
|
@@ -6819,94 +6845,117 @@ function Rl(e) {
|
|
|
6819
6845
|
filesRead: [...o].sort()
|
|
6820
6846
|
};
|
|
6821
6847
|
}
|
|
6822
|
-
|
|
6823
|
-
let
|
|
6848
|
+
function Vl(e, t) {
|
|
6849
|
+
let n = t.promptCount ?? 0, r = t.toolCount ?? 0;
|
|
6850
|
+
if (n > 0 || r > 0 || t.firstPrompt || e.length > 4 || Oo(e).some((e) => {
|
|
6851
|
+
let t = String(e.type ?? "");
|
|
6852
|
+
return t === "user_prompt" || t === "assistant_message";
|
|
6853
|
+
}) || e.some((e) => {
|
|
6854
|
+
let t = eo(e);
|
|
6855
|
+
return t ? to(t, e) != null : String(e.type ?? "") === "tool.execute.after";
|
|
6856
|
+
})) return !1;
|
|
6857
|
+
let i = new Set([
|
|
6858
|
+
"session.started",
|
|
6859
|
+
"session.idle",
|
|
6860
|
+
"session.ended"
|
|
6861
|
+
]), a = new Set([
|
|
6862
|
+
"session_start",
|
|
6863
|
+
"session_end",
|
|
6864
|
+
"error"
|
|
6865
|
+
]);
|
|
6866
|
+
return e.every((e) => {
|
|
6867
|
+
let t = eo(e);
|
|
6868
|
+
return t ? a.has(String(t.event_type ?? "")) : i.has(String(e.type ?? ""));
|
|
6869
|
+
});
|
|
6870
|
+
}
|
|
6871
|
+
async function Hl(e, t, n) {
|
|
6872
|
+
let { source: r = "opencode", cwd: i, project: a, startedAt: o } = n, { opencodeSessionId: s, maxEvents: c } = n;
|
|
6824
6873
|
r = (r ?? "").trim().toLowerCase() || "opencode";
|
|
6825
|
-
let
|
|
6826
|
-
i ??=
|
|
6827
|
-
let
|
|
6828
|
-
if (
|
|
6874
|
+
let l = e.rawEventSessionMeta(s, r);
|
|
6875
|
+
i ??= l.cwd ?? process.cwd(), a ??= l.project ?? null, o ??= l.started_at ?? null;
|
|
6876
|
+
let u = e.rawEventFlushState(s, r), d = e.rawEventsSinceBySeq(s, r, u, c);
|
|
6877
|
+
if (d.length === 0) return {
|
|
6829
6878
|
flushed: 0,
|
|
6830
6879
|
updatedState: 0
|
|
6831
6880
|
};
|
|
6832
|
-
let
|
|
6833
|
-
for (let e of
|
|
6881
|
+
let f = [];
|
|
6882
|
+
for (let e of d) {
|
|
6834
6883
|
let t = e.event_seq;
|
|
6835
6884
|
if (t == null) continue;
|
|
6836
6885
|
let n = Number(t);
|
|
6837
|
-
Number.isFinite(n) &&
|
|
6886
|
+
Number.isFinite(n) && f.push(n);
|
|
6838
6887
|
}
|
|
6839
|
-
if (
|
|
6888
|
+
if (f.length === 0) return {
|
|
6840
6889
|
flushed: 0,
|
|
6841
6890
|
updatedState: 0
|
|
6842
6891
|
};
|
|
6843
|
-
let
|
|
6844
|
-
|
|
6845
|
-
let t = p[e];
|
|
6846
|
-
t < m && (m = t), t > h && (h = t);
|
|
6847
|
-
}
|
|
6848
|
-
if (h < m) return {
|
|
6892
|
+
let p = f[0];
|
|
6893
|
+
if (p == null) return {
|
|
6849
6894
|
flushed: 0,
|
|
6850
6895
|
updatedState: 0
|
|
6851
6896
|
};
|
|
6852
|
-
let
|
|
6853
|
-
|
|
6897
|
+
let m = f.slice(1), h = p, g = p;
|
|
6898
|
+
for (let e of m) e < h && (h = e), e > g && (g = e);
|
|
6899
|
+
if (g < h) return {
|
|
6900
|
+
flushed: 0,
|
|
6901
|
+
updatedState: 0
|
|
6902
|
+
};
|
|
6903
|
+
let { batchId: _, status: v } = e.getOrCreateRawEventFlushBatch(s, r, h, g, Il);
|
|
6904
|
+
if (v === "completed") return e.updateRawEventFlushState(s, g, r), {
|
|
6854
6905
|
flushed: 0,
|
|
6855
6906
|
updatedState: 1
|
|
6856
6907
|
};
|
|
6857
|
-
if (!e.claimRawEventFlushBatch(
|
|
6908
|
+
if (!e.claimRawEventFlushBatch(_)) return {
|
|
6858
6909
|
flushed: 0,
|
|
6859
6910
|
updatedState: 0
|
|
6860
6911
|
};
|
|
6861
|
-
let
|
|
6862
|
-
|
|
6863
|
-
batch_id:
|
|
6864
|
-
start_event_seq:
|
|
6865
|
-
end_event_seq:
|
|
6912
|
+
let y = Bl(d);
|
|
6913
|
+
if (y.opencodeSessionId = s, y.source = r, y.streamId = s, y.flusher = "raw_events", y.flushBatch = {
|
|
6914
|
+
batch_id: _,
|
|
6915
|
+
start_event_seq: h,
|
|
6916
|
+
end_event_seq: g
|
|
6917
|
+
}, Vl(d, y)) return e.updateRawEventFlushBatchStatus(_, "completed"), e.updateRawEventFlushState(s, g, r), {
|
|
6918
|
+
flushed: d.length,
|
|
6919
|
+
updatedState: 1
|
|
6866
6920
|
};
|
|
6867
|
-
let
|
|
6921
|
+
let b = {
|
|
6868
6922
|
cwd: i ?? void 0,
|
|
6869
6923
|
project: a ?? void 0,
|
|
6870
6924
|
startedAt: o ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
6871
|
-
events:
|
|
6872
|
-
sessionContext:
|
|
6925
|
+
events: d,
|
|
6926
|
+
sessionContext: y
|
|
6873
6927
|
};
|
|
6874
6928
|
try {
|
|
6875
|
-
await
|
|
6929
|
+
await is(b, e, t);
|
|
6876
6930
|
} catch (n) {
|
|
6877
|
-
let
|
|
6878
|
-
|
|
6879
|
-
|
|
6880
|
-
|
|
6881
|
-
|
|
6882
|
-
let a = t.observer?.getStatus?.()?.provider, o = Fl(Ll(i, a));
|
|
6883
|
-
throw e.recordRawEventFlushBatchFailure(g, {
|
|
6884
|
-
message: o,
|
|
6885
|
-
errorType: i instanceof Ws ? "ObserverAuthError" : i.name,
|
|
6886
|
-
observerProvider: a ?? null,
|
|
6931
|
+
let r = n instanceof Error ? n : Error(String(n)), i = t.observer?.getStatus?.()?.provider, a = Ll(zl(r, i));
|
|
6932
|
+
throw e.recordRawEventFlushBatchFailure(_, {
|
|
6933
|
+
message: a,
|
|
6934
|
+
errorType: r instanceof Ks ? "ObserverAuthError" : r.name,
|
|
6935
|
+
observerProvider: i ?? null,
|
|
6887
6936
|
observerModel: t.observer?.getStatus?.()?.model ?? null,
|
|
6888
6937
|
observerRuntime: null
|
|
6889
6938
|
}), n;
|
|
6890
6939
|
}
|
|
6891
|
-
return e.updateRawEventFlushBatchStatus(
|
|
6892
|
-
flushed:
|
|
6940
|
+
return e.updateRawEventFlushBatchStatus(_, "completed"), e.updateRawEventFlushState(s, g, r), {
|
|
6941
|
+
flushed: d.length,
|
|
6893
6942
|
updatedState: 1
|
|
6894
6943
|
};
|
|
6895
6944
|
}
|
|
6896
6945
|
//#endregion
|
|
6897
6946
|
//#region src/raw-event-sweeper.ts
|
|
6898
|
-
var
|
|
6899
|
-
function
|
|
6947
|
+
var Ul = 60;
|
|
6948
|
+
function Wl(e, t) {
|
|
6900
6949
|
let n = process.env[e];
|
|
6901
6950
|
if (n == null) return t;
|
|
6902
6951
|
let r = Number.parseInt(n, 10);
|
|
6903
6952
|
return Number.isFinite(r) ? r : t;
|
|
6904
6953
|
}
|
|
6905
|
-
function
|
|
6954
|
+
function Gl(e) {
|
|
6906
6955
|
let t = (process.env[e] ?? "1").trim().toLowerCase();
|
|
6907
6956
|
return t === "0" || t === "false" || t === "off";
|
|
6908
6957
|
}
|
|
6909
|
-
var
|
|
6958
|
+
var Kl = class {
|
|
6910
6959
|
store;
|
|
6911
6960
|
ingestOpts;
|
|
6912
6961
|
active = !1;
|
|
@@ -6924,7 +6973,7 @@ var Ul = class {
|
|
|
6924
6973
|
this.store = e, this.ingestOpts = t;
|
|
6925
6974
|
}
|
|
6926
6975
|
enabled() {
|
|
6927
|
-
return !
|
|
6976
|
+
return !Gl("CODEMEM_RAW_EVENTS_SWEEPER");
|
|
6928
6977
|
}
|
|
6929
6978
|
intervalMs() {
|
|
6930
6979
|
let e = process.env.CODEMEM_RAW_EVENTS_SWEEPER_INTERVAL_MS;
|
|
@@ -6936,31 +6985,31 @@ var Ul = class {
|
|
|
6936
6985
|
return Number.isFinite(n) && n > 0 ? Math.max(1e3, n * 1e3) : 3e4;
|
|
6937
6986
|
}
|
|
6938
6987
|
idleMs() {
|
|
6939
|
-
return
|
|
6988
|
+
return Wl("CODEMEM_RAW_EVENTS_SWEEPER_IDLE_MS", 12e4);
|
|
6940
6989
|
}
|
|
6941
6990
|
limit() {
|
|
6942
|
-
return
|
|
6991
|
+
return Wl("CODEMEM_RAW_EVENTS_SWEEPER_LIMIT", 25);
|
|
6943
6992
|
}
|
|
6944
6993
|
workerMaxEvents() {
|
|
6945
|
-
let e =
|
|
6994
|
+
let e = Wl("CODEMEM_RAW_EVENTS_WORKER_MAX_EVENTS", 250);
|
|
6946
6995
|
return e <= 0 ? null : e;
|
|
6947
6996
|
}
|
|
6948
6997
|
retentionMs() {
|
|
6949
|
-
return
|
|
6998
|
+
return Wl("CODEMEM_RAW_EVENTS_RETENTION_MS", 0);
|
|
6950
6999
|
}
|
|
6951
7000
|
autoFlushEnabled() {
|
|
6952
7001
|
return (process.env.CODEMEM_RAW_EVENTS_AUTO_FLUSH ?? "").trim() === "1";
|
|
6953
7002
|
}
|
|
6954
7003
|
debounceMs() {
|
|
6955
|
-
return
|
|
7004
|
+
return Wl("CODEMEM_RAW_EVENTS_DEBOUNCE_MS", 6e4);
|
|
6956
7005
|
}
|
|
6957
7006
|
stuckBatchMs() {
|
|
6958
|
-
return
|
|
7007
|
+
return Wl("CODEMEM_RAW_EVENTS_STUCK_BATCH_MS", 3e5);
|
|
6959
7008
|
}
|
|
6960
7009
|
handleAuthError(e) {
|
|
6961
|
-
if (this.authBackoffUntil = Date.now() / 1e3 +
|
|
7010
|
+
if (this.authBackoffUntil = Date.now() / 1e3 + Ul, !this.authErrorLogged) {
|
|
6962
7011
|
this.authErrorLogged = !0;
|
|
6963
|
-
let t = `codemem: observer auth error — backing off for ${
|
|
7012
|
+
let t = `codemem: observer auth error — backing off for ${Ul}s. Refresh your provider credentials or update observer_provider in settings. (${e.message})`;
|
|
6964
7013
|
console.error(t);
|
|
6965
7014
|
}
|
|
6966
7015
|
}
|
|
@@ -7040,17 +7089,16 @@ var Ul = class {
|
|
|
7040
7089
|
let r = this.autoFlushTimers.get(n);
|
|
7041
7090
|
r && (clearTimeout(r), this.autoFlushTimers.delete(n));
|
|
7042
7091
|
try {
|
|
7043
|
-
await
|
|
7092
|
+
await Hl(this.store, this.ingestOpts, {
|
|
7044
7093
|
opencodeSessionId: e,
|
|
7045
7094
|
source: t,
|
|
7046
7095
|
cwd: null,
|
|
7047
7096
|
project: null,
|
|
7048
7097
|
startedAt: null,
|
|
7049
|
-
maxEvents: null
|
|
7050
|
-
allowEmptyFlush: !0
|
|
7098
|
+
maxEvents: null
|
|
7051
7099
|
});
|
|
7052
7100
|
} catch (t) {
|
|
7053
|
-
if (t instanceof
|
|
7101
|
+
if (t instanceof Ks) {
|
|
7054
7102
|
this.handleAuthError(t);
|
|
7055
7103
|
return;
|
|
7056
7104
|
}
|
|
@@ -7098,17 +7146,16 @@ var Ul = class {
|
|
|
7098
7146
|
if (!this.sessionFlushing.has(r)) {
|
|
7099
7147
|
this.sessionFlushing.add(r);
|
|
7100
7148
|
try {
|
|
7101
|
-
await
|
|
7149
|
+
await Hl(this.store, this.ingestOpts, {
|
|
7102
7150
|
opencodeSessionId: n,
|
|
7103
7151
|
source: t,
|
|
7104
7152
|
cwd: null,
|
|
7105
7153
|
project: null,
|
|
7106
7154
|
startedAt: null,
|
|
7107
|
-
maxEvents: i
|
|
7108
|
-
allowEmptyFlush: !0
|
|
7155
|
+
maxEvents: i
|
|
7109
7156
|
}), o.add(`${t}:${n}`);
|
|
7110
7157
|
} catch (e) {
|
|
7111
|
-
if (e instanceof
|
|
7158
|
+
if (e instanceof Ks) {
|
|
7112
7159
|
this.handleAuthError(e);
|
|
7113
7160
|
return;
|
|
7114
7161
|
}
|
|
@@ -7126,17 +7173,16 @@ var Ul = class {
|
|
|
7126
7173
|
if (!this.sessionFlushing.has(r)) {
|
|
7127
7174
|
this.sessionFlushing.add(r);
|
|
7128
7175
|
try {
|
|
7129
|
-
await
|
|
7176
|
+
await Hl(this.store, this.ingestOpts, {
|
|
7130
7177
|
opencodeSessionId: n,
|
|
7131
7178
|
source: t,
|
|
7132
7179
|
cwd: null,
|
|
7133
7180
|
project: null,
|
|
7134
7181
|
startedAt: null,
|
|
7135
|
-
maxEvents: i
|
|
7136
|
-
allowEmptyFlush: !0
|
|
7182
|
+
maxEvents: i
|
|
7137
7183
|
});
|
|
7138
7184
|
} catch (e) {
|
|
7139
|
-
if (e instanceof
|
|
7185
|
+
if (e instanceof Ks) {
|
|
7140
7186
|
this.handleAuthError(e);
|
|
7141
7187
|
return;
|
|
7142
7188
|
}
|
|
@@ -7150,7 +7196,7 @@ var Ul = class {
|
|
|
7150
7196
|
};
|
|
7151
7197
|
//#endregion
|
|
7152
7198
|
//#region src/sync-replication.ts
|
|
7153
|
-
function
|
|
7199
|
+
function ql(e) {
|
|
7154
7200
|
if (e == null) return null;
|
|
7155
7201
|
if (typeof e == "number" && Number.isFinite(e)) return e;
|
|
7156
7202
|
if (typeof e == "string" && e.trim().length > 0) {
|
|
@@ -7162,7 +7208,7 @@ function Wl(e) {
|
|
|
7162
7208
|
function Q(e) {
|
|
7163
7209
|
return e == null ? null : String(e);
|
|
7164
7210
|
}
|
|
7165
|
-
function
|
|
7211
|
+
function Jl(e, t) {
|
|
7166
7212
|
if (!e.payload_json) return null;
|
|
7167
7213
|
let n;
|
|
7168
7214
|
try {
|
|
@@ -7172,14 +7218,14 @@ function Gl(e, t) {
|
|
|
7172
7218
|
}
|
|
7173
7219
|
let r = n.metadata_json, i = r && typeof r == "object" && !Array.isArray(r) ? { ...r } : {};
|
|
7174
7220
|
return {
|
|
7175
|
-
session_id:
|
|
7221
|
+
session_id: ql(n.session_id),
|
|
7176
7222
|
kind: Q(n.kind),
|
|
7177
7223
|
title: Q(n.title),
|
|
7178
7224
|
subtitle: Q(n.subtitle),
|
|
7179
7225
|
body_text: Q(n.body_text),
|
|
7180
|
-
confidence:
|
|
7226
|
+
confidence: ql(n.confidence),
|
|
7181
7227
|
tags_text: Q(n.tags_text),
|
|
7182
|
-
active:
|
|
7228
|
+
active: ql(n.active),
|
|
7183
7229
|
created_at: Q(n.created_at),
|
|
7184
7230
|
updated_at: Q(n.updated_at),
|
|
7185
7231
|
metadata_json: i,
|
|
@@ -7196,19 +7242,19 @@ function Gl(e, t) {
|
|
|
7196
7242
|
concepts: n.concepts ?? null,
|
|
7197
7243
|
files_read: n.files_read ?? null,
|
|
7198
7244
|
files_modified: n.files_modified ?? null,
|
|
7199
|
-
user_prompt_id:
|
|
7200
|
-
prompt_number:
|
|
7245
|
+
user_prompt_id: ql(n.user_prompt_id),
|
|
7246
|
+
prompt_number: ql(n.prompt_number),
|
|
7201
7247
|
deleted_at: Q(n.deleted_at),
|
|
7202
|
-
rev:
|
|
7248
|
+
rev: ql(n.rev) ?? 0,
|
|
7203
7249
|
import_key: Q(n.import_key)
|
|
7204
7250
|
};
|
|
7205
7251
|
}
|
|
7206
|
-
function
|
|
7252
|
+
function Yl(e) {
|
|
7207
7253
|
let t = String(e ?? "").trim();
|
|
7208
7254
|
return t.length > 0 ? t : null;
|
|
7209
7255
|
}
|
|
7210
|
-
function
|
|
7211
|
-
let t =
|
|
7256
|
+
function Xl(e) {
|
|
7257
|
+
let t = Yl(e);
|
|
7212
7258
|
if (!t) return null;
|
|
7213
7259
|
let n = t.lastIndexOf("|");
|
|
7214
7260
|
if (n <= 0) return null;
|
|
@@ -7218,20 +7264,20 @@ function ql(e) {
|
|
|
7218
7264
|
id: i
|
|
7219
7265
|
};
|
|
7220
7266
|
}
|
|
7221
|
-
function
|
|
7267
|
+
function Zl(e, t) {
|
|
7222
7268
|
return `${e}|${t}`;
|
|
7223
7269
|
}
|
|
7224
|
-
function
|
|
7270
|
+
function Ql(e) {
|
|
7225
7271
|
e.exec("\n CREATE TABLE IF NOT EXISTS sync_reset_state (\n id INTEGER PRIMARY KEY,\n generation INTEGER NOT NULL,\n snapshot_id TEXT NOT NULL,\n baseline_cursor TEXT,\n retained_floor_cursor TEXT,\n updated_at TEXT NOT NULL\n )\n ");
|
|
7226
7272
|
}
|
|
7227
|
-
function
|
|
7228
|
-
|
|
7273
|
+
function $l(e) {
|
|
7274
|
+
Ql(e);
|
|
7229
7275
|
let t = N(e, { schema: H }), n = t.select().from(cn).where(k(cn.id, 1)).get();
|
|
7230
7276
|
if (n) return {
|
|
7231
7277
|
generation: Number(n.generation ?? 1),
|
|
7232
7278
|
snapshot_id: String(n.snapshot_id),
|
|
7233
|
-
baseline_cursor:
|
|
7234
|
-
retained_floor_cursor:
|
|
7279
|
+
baseline_cursor: Yl(n.baseline_cursor),
|
|
7280
|
+
retained_floor_cursor: Yl(n.retained_floor_cursor)
|
|
7235
7281
|
};
|
|
7236
7282
|
let r = (/* @__PURE__ */ new Date()).toISOString(), i = {
|
|
7237
7283
|
id: 1,
|
|
@@ -7248,12 +7294,12 @@ function Xl(e) {
|
|
|
7248
7294
|
retained_floor_cursor: i.retained_floor_cursor
|
|
7249
7295
|
};
|
|
7250
7296
|
}
|
|
7251
|
-
function
|
|
7252
|
-
let n =
|
|
7297
|
+
function eu(e, t) {
|
|
7298
|
+
let n = $l(e), r = (/* @__PURE__ */ new Date()).toISOString(), i = {
|
|
7253
7299
|
generation: t.generation ?? n.generation,
|
|
7254
7300
|
snapshot_id: t.snapshot_id ?? n.snapshot_id,
|
|
7255
|
-
baseline_cursor: t.baseline_cursor === void 0 ? n.baseline_cursor :
|
|
7256
|
-
retained_floor_cursor: t.retained_floor_cursor === void 0 ? n.retained_floor_cursor :
|
|
7301
|
+
baseline_cursor: t.baseline_cursor === void 0 ? n.baseline_cursor : Yl(t.baseline_cursor),
|
|
7302
|
+
retained_floor_cursor: t.retained_floor_cursor === void 0 ? n.retained_floor_cursor : Yl(t.retained_floor_cursor)
|
|
7257
7303
|
};
|
|
7258
7304
|
return N(e, { schema: H }).insert(cn).values({
|
|
7259
7305
|
id: 1,
|
|
@@ -7270,7 +7316,7 @@ function Zl(e, t) {
|
|
|
7270
7316
|
}
|
|
7271
7317
|
}).run(), i;
|
|
7272
7318
|
}
|
|
7273
|
-
function
|
|
7319
|
+
function tu(e, t) {
|
|
7274
7320
|
return {
|
|
7275
7321
|
reset_required: !0,
|
|
7276
7322
|
reset: {
|
|
@@ -7280,17 +7326,17 @@ function Ql(e, t) {
|
|
|
7280
7326
|
}
|
|
7281
7327
|
};
|
|
7282
7328
|
}
|
|
7283
|
-
function
|
|
7329
|
+
function nu(e, t) {
|
|
7284
7330
|
return {
|
|
7285
7331
|
...e,
|
|
7286
7332
|
clock_device_id: t
|
|
7287
7333
|
};
|
|
7288
7334
|
}
|
|
7289
|
-
function
|
|
7335
|
+
function ru(e) {
|
|
7290
7336
|
let t = z(e);
|
|
7291
7337
|
return typeof t.clock_device_id == "string" ? t.clock_device_id : "";
|
|
7292
7338
|
}
|
|
7293
|
-
function
|
|
7339
|
+
function iu(e, t) {
|
|
7294
7340
|
let n = new TextEncoder(), r = [], i = [], a = 9;
|
|
7295
7341
|
for (let o of e) {
|
|
7296
7342
|
let e = n.encode(JSON.stringify(o)).byteLength, s = i.length === 0 ? e : e + 1;
|
|
@@ -7302,14 +7348,14 @@ function tu(e, t) {
|
|
|
7302
7348
|
}
|
|
7303
7349
|
return i.length > 0 && r.push(i), r;
|
|
7304
7350
|
}
|
|
7305
|
-
function
|
|
7351
|
+
function au(e, t) {
|
|
7306
7352
|
let n = N(e, { schema: H }).select({
|
|
7307
7353
|
last_applied_cursor: an.last_applied_cursor,
|
|
7308
7354
|
last_acked_cursor: an.last_acked_cursor
|
|
7309
7355
|
}).from(an).where(k(an.peer_device_id, t)).get();
|
|
7310
7356
|
return n ? [n.last_applied_cursor, n.last_acked_cursor] : [null, null];
|
|
7311
7357
|
}
|
|
7312
|
-
function
|
|
7358
|
+
function ou(e, t, n = {}) {
|
|
7313
7359
|
let r = (/* @__PURE__ */ new Date()).toISOString(), i = n.lastApplied ?? null, a = n.lastAcked ?? null;
|
|
7314
7360
|
N(e, { schema: H }).insert(an).values({
|
|
7315
7361
|
peer_device_id: t,
|
|
@@ -7325,7 +7371,7 @@ function ru(e, t, n = {}) {
|
|
|
7325
7371
|
}
|
|
7326
7372
|
}).run();
|
|
7327
7373
|
}
|
|
7328
|
-
var
|
|
7374
|
+
var su = [
|
|
7329
7375
|
"op_id",
|
|
7330
7376
|
"entity_type",
|
|
7331
7377
|
"entity_id",
|
|
@@ -7336,26 +7382,26 @@ var iu = [
|
|
|
7336
7382
|
"device_id",
|
|
7337
7383
|
"created_at"
|
|
7338
7384
|
];
|
|
7339
|
-
function
|
|
7385
|
+
function cu(e) {
|
|
7340
7386
|
if (typeof e != "object" || !e) return [];
|
|
7341
7387
|
let t = e.ops;
|
|
7342
7388
|
return Array.isArray(t) ? t.filter((e) => {
|
|
7343
7389
|
if (typeof e != "object" || !e) return !1;
|
|
7344
7390
|
let t = e;
|
|
7345
|
-
return
|
|
7391
|
+
return su.every((e) => t[e] !== void 0 && t[e] !== null);
|
|
7346
7392
|
}) : [];
|
|
7347
7393
|
}
|
|
7348
|
-
function
|
|
7394
|
+
function lu(e, t, n) {
|
|
7349
7395
|
return [
|
|
7350
7396
|
e,
|
|
7351
7397
|
t,
|
|
7352
7398
|
n
|
|
7353
7399
|
];
|
|
7354
7400
|
}
|
|
7355
|
-
function
|
|
7401
|
+
function uu(e, t) {
|
|
7356
7402
|
return e[0] === t[0] ? e[1] === t[1] ? e[2] > t[2] : e[1] > t[1] : e[0] > t[0];
|
|
7357
7403
|
}
|
|
7358
|
-
function
|
|
7404
|
+
function du(e, t) {
|
|
7359
7405
|
let n = N(e, { schema: H }), r = a(), i = (/* @__PURE__ */ new Date()).toISOString(), o = n.select().from(W).where(k(W.id, t.memoryId)).get(), s = Number(o?.rev ?? 0), c = o?.updated_at ?? i, l = o?.import_key ?? String(t.memoryId), u = z(o?.metadata_json), d = typeof u.clock_device_id == "string" && u.clock_device_id.trim().length > 0 ? u.clock_device_id : t.deviceId, f;
|
|
7360
7406
|
if (t.payload) f = B(t.payload);
|
|
7361
7407
|
else if (o && t.opType === "upsert") {
|
|
@@ -7410,17 +7456,17 @@ function cu(e, t) {
|
|
|
7410
7456
|
created_at: i
|
|
7411
7457
|
}).run(), r;
|
|
7412
7458
|
}
|
|
7413
|
-
function
|
|
7459
|
+
function fu(e) {
|
|
7414
7460
|
let t = e.indexOf("|");
|
|
7415
7461
|
return t < 0 ? null : [e.slice(0, t), e.slice(t + 1)];
|
|
7416
7462
|
}
|
|
7417
|
-
function
|
|
7463
|
+
function pu(e, t) {
|
|
7418
7464
|
return `${e}|${t}`;
|
|
7419
7465
|
}
|
|
7420
|
-
function
|
|
7466
|
+
function mu(e, t, n = 100, r) {
|
|
7421
7467
|
let i = N(e, { schema: H }), a = J, o = [];
|
|
7422
7468
|
if (t) {
|
|
7423
|
-
let e =
|
|
7469
|
+
let e = fu(t);
|
|
7424
7470
|
if (e) {
|
|
7425
7471
|
let [t, n] = e;
|
|
7426
7472
|
o.push(ce(A(a.created_at, t), D(k(a.created_at, t), A(a.op_id, n))));
|
|
@@ -7441,17 +7487,17 @@ function du(e, t, n = 100, r) {
|
|
|
7441
7487
|
})), u = null;
|
|
7442
7488
|
if (c.length > 0) {
|
|
7443
7489
|
let e = c.at(-1);
|
|
7444
|
-
e && (u =
|
|
7490
|
+
e && (u = pu(e.created_at, e.op_id));
|
|
7445
7491
|
}
|
|
7446
7492
|
return [l, u];
|
|
7447
7493
|
}
|
|
7448
|
-
function
|
|
7449
|
-
let n =
|
|
7450
|
-
if (i != null && i !== n.generation) return
|
|
7494
|
+
function hu(e, t) {
|
|
7495
|
+
let n = $l(e), r = Yl(t.since), i = t.generation ?? null, a = Yl(t.snapshotId), o = Yl(t.baselineCursor), s = i != null || a != null || o != null;
|
|
7496
|
+
if (i != null && i !== n.generation) return tu(n, "generation_mismatch");
|
|
7451
7497
|
let c = i != null && a != null && o === n.baseline_cursor;
|
|
7452
|
-
if (!s || !c || a !== n.snapshot_id || o !== n.baseline_cursor) return
|
|
7453
|
-
if (r != null && n.retained_floor_cursor != null && r < n.retained_floor_cursor) return
|
|
7454
|
-
let [l, u] =
|
|
7498
|
+
if (!s || !c || a !== n.snapshot_id || o !== n.baseline_cursor) return tu(n, "boundary_mismatch");
|
|
7499
|
+
if (r != null && n.retained_floor_cursor != null && r < n.retained_floor_cursor) return tu(n, "stale_cursor");
|
|
7500
|
+
let [l, u] = mu(e, r, t.limit, t.deviceId);
|
|
7455
7501
|
return {
|
|
7456
7502
|
reset_required: !1,
|
|
7457
7503
|
boundary: n,
|
|
@@ -7459,11 +7505,11 @@ function fu(e, t) {
|
|
|
7459
7505
|
nextCursor: u
|
|
7460
7506
|
};
|
|
7461
7507
|
}
|
|
7462
|
-
function
|
|
7463
|
-
let n =
|
|
7508
|
+
function gu(e, t) {
|
|
7509
|
+
let n = $l(e);
|
|
7464
7510
|
if (t.generation !== n.generation) throw Error("generation_mismatch");
|
|
7465
|
-
if (
|
|
7466
|
-
let r = Math.max(1, Math.min(t.limit ?? 200, 1e3)), i =
|
|
7511
|
+
if (Yl(t.snapshotId) !== n.snapshot_id || Yl(t.baselineCursor) !== n.baseline_cursor) throw Error("boundary_mismatch");
|
|
7512
|
+
let r = Math.max(1, Math.min(t.limit ?? 200, 1e3)), i = Xl(t.pageToken), a = N(e, { schema: H }), o = Math.min(Math.max(r * 3, r), 1e3), s = (e, t) => a.select({
|
|
7467
7513
|
memory: W,
|
|
7468
7514
|
sessionProject: U.project
|
|
7469
7515
|
}).from(W).innerJoin(U, k(U.id, W.session_id)).where(D(re(W.import_key), e ? ce(A(W.import_key, e.importKey), D(k(W.import_key, e.importKey), A(W.id, e.id))) : void 0)).orderBy(W.import_key, W.id).limit(t).all(), c = [], l = i, u = null, d = !1;
|
|
@@ -7475,12 +7521,12 @@ function pu(e, t) {
|
|
|
7475
7521
|
if (!a) continue;
|
|
7476
7522
|
let o = String(a.memory.import_key ?? "").trim();
|
|
7477
7523
|
if (!o) continue;
|
|
7478
|
-
u =
|
|
7524
|
+
u = Zl(o, Number(a.memory.id)), l = {
|
|
7479
7525
|
importKey: o,
|
|
7480
7526
|
id: Number(a.memory.id)
|
|
7481
7527
|
};
|
|
7482
|
-
let f =
|
|
7483
|
-
if (!
|
|
7528
|
+
let f = Ru(a.memory);
|
|
7529
|
+
if (!ku(f) && !Au(e, t.peerDeviceId ?? null) || !Ou(e, a.sessionProject, t.peerDeviceId ?? null)) continue;
|
|
7484
7530
|
let p = f.metadata_json && typeof f.metadata_json == "object" ? f.metadata_json : {}, m = typeof p.clock_device_id == "string" && p.clock_device_id.trim() ? p.clock_device_id.trim() : String(a.memory.origin_device_id ?? "local");
|
|
7485
7531
|
if (c.push({
|
|
7486
7532
|
entity_id: o,
|
|
@@ -7503,10 +7549,10 @@ function pu(e, t) {
|
|
|
7503
7549
|
hasMore: d && u != null
|
|
7504
7550
|
};
|
|
7505
7551
|
}
|
|
7506
|
-
function
|
|
7552
|
+
function _u(e, t = 25) {
|
|
7507
7553
|
let n = N(e, { schema: H }), r = n.select().from(W).orderBy(W.updated_at).all(), i = 0;
|
|
7508
7554
|
for (let e of r) {
|
|
7509
|
-
if (!e.import_key || !
|
|
7555
|
+
if (!e.import_key || !ku(Ru(e))) continue;
|
|
7510
7556
|
let r = e.deleted_at || e.active === 0 ? "delete" : "upsert";
|
|
7511
7557
|
if (!n.select({ one: M`1` }).from(J).where(D(k(J.entity_type, "memory_item"), k(J.entity_id, e.import_key), k(J.op_type, r), k(J.clock_rev, Number(e.rev ?? 0)))).limit(1).get() && (i += 1, i >= t)) break;
|
|
7512
7558
|
}
|
|
@@ -7515,10 +7561,8 @@ function mu(e, t = 25) {
|
|
|
7515
7561
|
count: i
|
|
7516
7562
|
};
|
|
7517
7563
|
}
|
|
7518
|
-
function
|
|
7519
|
-
let n = Math.max(1, Math.floor(t?.maxAgeDays ?? 30)), r = Math.max(1, Math.floor(t?.maxSizeBytes ?? 512 * 1024 * 1024)), i = Math.max(1, Math.floor(t?.maxDeleteOps ?? 2 ** 53 - 1)), a = Math.max(1, Math.floor(t?.maxRuntimeMs ?? 2 ** 53 - 1)), o = N(e, { schema: H }), s = (/* @__PURE__ */ new Date(Date.now() - n * 24 * 60 * 60 * 1e3)).toISOString(), c = Date.now(), l = 0, u = null;
|
|
7520
|
-
e.prepare("DELETE FROM replication_ops WHERE op_id = ?");
|
|
7521
|
-
let d = e.prepare("DELETE FROM replication_ops WHERE created_at < ? OR (created_at = ? AND op_id <= ?)"), f = vu(e);
|
|
7564
|
+
function vu(e, t) {
|
|
7565
|
+
let n = Math.max(1, Math.floor(t?.maxAgeDays ?? 30)), r = Math.max(1, Math.floor(t?.maxSizeBytes ?? 512 * 1024 * 1024)), i = Math.max(1, Math.floor(t?.maxDeleteOps ?? 2 ** 53 - 1)), a = Math.max(1, Math.floor(t?.maxRuntimeMs ?? 2 ** 53 - 1)), o = N(e, { schema: H }), s = (/* @__PURE__ */ new Date(Date.now() - n * 24 * 60 * 60 * 1e3)).toISOString(), c = Date.now(), l = 0, u = null, d = e.prepare("DELETE FROM replication_ops WHERE created_at < ? OR (created_at = ? AND op_id <= ?)"), f = Su(e);
|
|
7522
7566
|
if (f == null) throw Error("replication_ops_size_unavailable");
|
|
7523
7567
|
let p = !1, m = () => l >= i || Date.now() - c >= a;
|
|
7524
7568
|
for (;;) {
|
|
@@ -7533,7 +7577,7 @@ function hu(e, t) {
|
|
|
7533
7577
|
if (n.length === 0) break;
|
|
7534
7578
|
let r = n[n.length - 1];
|
|
7535
7579
|
if (!r) break;
|
|
7536
|
-
if (u =
|
|
7580
|
+
if (u = pu(String(r.created_at), String(r.op_id)), l += d.run(r.created_at, r.created_at, r.op_id).changes ?? 0, m()) {
|
|
7537
7581
|
p = !0;
|
|
7538
7582
|
break;
|
|
7539
7583
|
}
|
|
@@ -7550,13 +7594,13 @@ function hu(e, t) {
|
|
|
7550
7594
|
created_at: J.created_at
|
|
7551
7595
|
}).from(J).orderBy(J.created_at, J.op_id).limit(n).all(), a = r[r.length - 1];
|
|
7552
7596
|
if (!a) break;
|
|
7553
|
-
u =
|
|
7554
|
-
let s =
|
|
7597
|
+
u = pu(String(a.created_at), String(a.op_id)), l += d.run(a.created_at, a.created_at, a.op_id).changes ?? 0;
|
|
7598
|
+
let s = Su(e);
|
|
7555
7599
|
if (s == null) throw Error("replication_ops_size_unavailable");
|
|
7556
7600
|
h = s;
|
|
7557
7601
|
}
|
|
7558
|
-
let g =
|
|
7559
|
-
return
|
|
7602
|
+
let g = $l(e), _ = l > 0 ? u : g.retained_floor_cursor;
|
|
7603
|
+
return eu(e, { retained_floor_cursor: _ }), {
|
|
7560
7604
|
deleted: l,
|
|
7561
7605
|
retained_floor_cursor: _,
|
|
7562
7606
|
estimated_bytes_before: f,
|
|
@@ -7564,8 +7608,31 @@ function hu(e, t) {
|
|
|
7564
7608
|
stopped_by_budget: p
|
|
7565
7609
|
};
|
|
7566
7610
|
}
|
|
7567
|
-
function
|
|
7568
|
-
let
|
|
7611
|
+
function yu(e, t) {
|
|
7612
|
+
let n = Math.max(1, Math.floor(t.maxPasses ?? 25)), r = 0, i = 0, a = null, o = Su(e) ?? 0, s = !1;
|
|
7613
|
+
for (;;) {
|
|
7614
|
+
if (t.signal?.aborted || i >= n) {
|
|
7615
|
+
s = !0;
|
|
7616
|
+
break;
|
|
7617
|
+
}
|
|
7618
|
+
let c = vu(e, t);
|
|
7619
|
+
if (i += 1, r += c.deleted, a = c.retained_floor_cursor, o = c.estimated_bytes_after ?? Su(e) ?? 0, s = c.stopped_by_budget ?? !1, t.onPass?.({
|
|
7620
|
+
passNumber: i,
|
|
7621
|
+
deleted: c.deleted,
|
|
7622
|
+
afterBytes: o,
|
|
7623
|
+
stoppedByBudget: s
|
|
7624
|
+
}), c.deleted === 0 || !c.stopped_by_budget) break;
|
|
7625
|
+
}
|
|
7626
|
+
return {
|
|
7627
|
+
totalDeleted: r,
|
|
7628
|
+
passes: i,
|
|
7629
|
+
lastFloor: a,
|
|
7630
|
+
afterBytes: o,
|
|
7631
|
+
stoppedByBudget: s
|
|
7632
|
+
};
|
|
7633
|
+
}
|
|
7634
|
+
function bu(e, t, n = 2 ** 53 - 1) {
|
|
7635
|
+
let r = N(e, { schema: H }), i = (/* @__PURE__ */ new Date(Date.now() - Math.max(1, Math.floor(t)) * 24 * 60 * 60 * 1e3)).toISOString(), a = Su(e);
|
|
7569
7636
|
if (a == null) throw Error("replication_ops_size_unavailable");
|
|
7570
7637
|
let o = r.select({
|
|
7571
7638
|
op_id: J.op_id,
|
|
@@ -7573,14 +7640,14 @@ function gu(e, t, n = 2 ** 53 - 1) {
|
|
|
7573
7640
|
}).from(J).where(M`${J.created_at} < ${i}`).orderBy(J.created_at, J.op_id).limit(Math.max(1, n)).all(), s = o[o.length - 1];
|
|
7574
7641
|
if (!s) return {
|
|
7575
7642
|
deleted: 0,
|
|
7576
|
-
retained_floor_cursor:
|
|
7643
|
+
retained_floor_cursor: $l(e).retained_floor_cursor,
|
|
7577
7644
|
estimated_bytes_before: a,
|
|
7578
7645
|
estimated_bytes_after: a,
|
|
7579
7646
|
stopped_by_budget: !1
|
|
7580
7647
|
};
|
|
7581
|
-
let c = e.prepare("DELETE FROM replication_ops WHERE created_at < ? OR (created_at = ? AND op_id <= ?)").run(s.created_at, s.created_at, s.op_id).changes ?? 0, l =
|
|
7582
|
-
|
|
7583
|
-
let u =
|
|
7648
|
+
let c = e.prepare("DELETE FROM replication_ops WHERE created_at < ? OR (created_at = ? AND op_id <= ?)").run(s.created_at, s.created_at, s.op_id).changes ?? 0, l = pu(String(s.created_at), String(s.op_id));
|
|
7649
|
+
eu(e, { retained_floor_cursor: l });
|
|
7650
|
+
let u = Su(e);
|
|
7584
7651
|
if (u == null) throw Error("replication_ops_size_unavailable");
|
|
7585
7652
|
return {
|
|
7586
7653
|
deleted: c,
|
|
@@ -7590,19 +7657,19 @@ function gu(e, t, n = 2 ** 53 - 1) {
|
|
|
7590
7657
|
stopped_by_budget: !1
|
|
7591
7658
|
};
|
|
7592
7659
|
}
|
|
7593
|
-
function
|
|
7660
|
+
function xu(e, t, n) {
|
|
7594
7661
|
let r = N(e, { schema: H }), i = (/* @__PURE__ */ new Date(Date.now() - Math.max(1, Math.floor(t)) * 24 * 60 * 60 * 1e3)).toISOString(), a = r.select({ total: M`COUNT(*)` }).from(J).where(M`${J.created_at} < ${i}`).get(), o = r.select({ total: M`COUNT(*)` }).from(J).get(), s = r.select({
|
|
7595
7662
|
op_id: J.op_id,
|
|
7596
7663
|
created_at: J.created_at
|
|
7597
|
-
}).from(J).where(M`${J.created_at} < ${i}`).orderBy(O(J.created_at), O(J.op_id)).limit(1).get(), c = Number(a?.total ?? 0), l = Number(o?.total ?? 0), u =
|
|
7664
|
+
}).from(J).where(M`${J.created_at} < ${i}`).orderBy(O(J.created_at), O(J.op_id)).limit(1).get(), c = Number(a?.total ?? 0), l = Number(o?.total ?? 0), u = Su(e) ?? 0, d = l > 0 ? Math.round(u * c / l) : 0;
|
|
7598
7665
|
return {
|
|
7599
7666
|
candidate_ops: c,
|
|
7600
7667
|
estimated_candidate_bytes: Math.max(0, d),
|
|
7601
|
-
cutoff_cursor: s ?
|
|
7668
|
+
cutoff_cursor: s ? pu(String(s.created_at), String(s.op_id)) : null,
|
|
7602
7669
|
estimated_batches: c > 0 ? Math.ceil(c / Math.max(1, n)) : 0
|
|
7603
7670
|
};
|
|
7604
7671
|
}
|
|
7605
|
-
function
|
|
7672
|
+
function Su(e) {
|
|
7606
7673
|
try {
|
|
7607
7674
|
let t = e.prepare("SELECT COALESCE(SUM(pgsize), 0) AS total_bytes\n FROM dbstat\n WHERE name = 'replication_ops'\n OR name LIKE 'idx_replication_ops_%'\n OR name LIKE 'sqlite_autoindex_replication_ops_%'").get(), n = Number(t?.total_bytes ?? 0);
|
|
7608
7675
|
return Number.isFinite(n) && n >= 0 ? n : null;
|
|
@@ -7610,7 +7677,7 @@ function vu(e) {
|
|
|
7610
7677
|
return null;
|
|
7611
7678
|
}
|
|
7612
7679
|
}
|
|
7613
|
-
function
|
|
7680
|
+
function Cu(e) {
|
|
7614
7681
|
if (!Array.isArray(e)) return [];
|
|
7615
7682
|
let t = [];
|
|
7616
7683
|
for (let n of e) {
|
|
@@ -7619,25 +7686,25 @@ function yu(e) {
|
|
|
7619
7686
|
}
|
|
7620
7687
|
return t;
|
|
7621
7688
|
}
|
|
7622
|
-
function
|
|
7623
|
-
return Array.isArray(e) ?
|
|
7689
|
+
function wu(e) {
|
|
7690
|
+
return Array.isArray(e) ? Cu(e) : typeof e == "string" ? e.split(",").map((e) => e.trim()).filter(Boolean) : [];
|
|
7624
7691
|
}
|
|
7625
|
-
function
|
|
7692
|
+
function Tu(e) {
|
|
7626
7693
|
if (!e) return [];
|
|
7627
7694
|
try {
|
|
7628
|
-
return
|
|
7695
|
+
return Cu(JSON.parse(e));
|
|
7629
7696
|
} catch {
|
|
7630
7697
|
return [];
|
|
7631
7698
|
}
|
|
7632
7699
|
}
|
|
7633
|
-
function
|
|
7700
|
+
function Eu(e) {
|
|
7634
7701
|
let t = String(e ?? "").trim().replaceAll("\\", "/");
|
|
7635
7702
|
if (!t) return "";
|
|
7636
7703
|
let n = t.split("/").filter(Boolean);
|
|
7637
7704
|
return n.length > 0 ? n[n.length - 1] ?? "" : "";
|
|
7638
7705
|
}
|
|
7639
|
-
function
|
|
7640
|
-
let n = N(e, { schema: H }), r = tt(), i = process.env.CODEMEM_SYNC_PROJECTS_INCLUDE, a = process.env.CODEMEM_SYNC_PROJECTS_EXCLUDE, o =
|
|
7706
|
+
function Du(e, t) {
|
|
7707
|
+
let n = N(e, { schema: H }), r = tt(), i = process.env.CODEMEM_SYNC_PROJECTS_INCLUDE, a = process.env.CODEMEM_SYNC_PROJECTS_EXCLUDE, o = wu(i === void 0 ? r.sync_projects_include : i), s = wu(a === void 0 ? r.sync_projects_exclude : a);
|
|
7641
7708
|
if (!t) return {
|
|
7642
7709
|
include: o,
|
|
7643
7710
|
exclude: s
|
|
@@ -7650,15 +7717,15 @@ function Cu(e, t) {
|
|
|
7650
7717
|
include: o,
|
|
7651
7718
|
exclude: s
|
|
7652
7719
|
} : {
|
|
7653
|
-
include:
|
|
7654
|
-
exclude:
|
|
7720
|
+
include: Tu(c.projects_include_json),
|
|
7721
|
+
exclude: Tu(c.projects_exclude_json)
|
|
7655
7722
|
};
|
|
7656
7723
|
}
|
|
7657
|
-
function
|
|
7658
|
-
let { include: r, exclude: i } =
|
|
7724
|
+
function Ou(e, t, n) {
|
|
7725
|
+
let { include: r, exclude: i } = Du(e, n), a = String(t ?? "").trim(), o = Eu(a);
|
|
7659
7726
|
return i.some((e) => e === a || e === o) ? !1 : r.length === 0 ? !0 : r.some((e) => e === a || e === o);
|
|
7660
7727
|
}
|
|
7661
|
-
function
|
|
7728
|
+
function ku(e) {
|
|
7662
7729
|
if (!e) return !1;
|
|
7663
7730
|
let t = String(e.visibility ?? "").trim().toLowerCase(), n = e.metadata_json && typeof e.metadata_json == "object" && !Array.isArray(e.metadata_json) ? e.metadata_json : {}, r = String(n.visibility ?? "").trim().toLowerCase();
|
|
7664
7731
|
if (!t && r && (t = r), !t) {
|
|
@@ -7668,10 +7735,10 @@ function Tu(e) {
|
|
|
7668
7735
|
}
|
|
7669
7736
|
return t === "shared";
|
|
7670
7737
|
}
|
|
7671
|
-
function
|
|
7738
|
+
function Au(e, t) {
|
|
7672
7739
|
return t ? !!N(e, { schema: H }).select({ claimed_local_actor: Y.claimed_local_actor }).from(Y).where(k(Y.peer_device_id, t)).limit(1).get()?.claimed_local_actor : !1;
|
|
7673
7740
|
}
|
|
7674
|
-
function
|
|
7741
|
+
function ju(e) {
|
|
7675
7742
|
if (!e || !e.trim()) return null;
|
|
7676
7743
|
try {
|
|
7677
7744
|
let t = JSON.parse(e);
|
|
@@ -7680,16 +7747,16 @@ function Du(e) {
|
|
|
7680
7747
|
return null;
|
|
7681
7748
|
}
|
|
7682
7749
|
}
|
|
7683
|
-
function
|
|
7750
|
+
function Mu(e, t, n) {
|
|
7684
7751
|
let r = [], i = null, a = 0, o = null;
|
|
7685
7752
|
for (let s of t) {
|
|
7686
7753
|
if (s.entity_type === "memory_item") {
|
|
7687
|
-
let t =
|
|
7754
|
+
let t = ju(s.payload_json);
|
|
7688
7755
|
if (s.op_type === "delete" && t == null) {
|
|
7689
|
-
r.push(s), i =
|
|
7756
|
+
r.push(s), i = pu(s.created_at, s.op_id);
|
|
7690
7757
|
continue;
|
|
7691
7758
|
}
|
|
7692
|
-
if (!
|
|
7759
|
+
if (!ku(t) && !Au(e, n)) {
|
|
7693
7760
|
a += 1, o ||= {
|
|
7694
7761
|
reason: "visibility_filter",
|
|
7695
7762
|
op_id: s.op_id,
|
|
@@ -7698,11 +7765,11 @@ function Ou(e, t, n) {
|
|
|
7698
7765
|
entity_id: s.entity_id,
|
|
7699
7766
|
visibility: typeof t?.visibility == "string" ? String(t.visibility) : null,
|
|
7700
7767
|
skipped_count: 0
|
|
7701
|
-
}, i =
|
|
7768
|
+
}, i = pu(s.created_at, s.op_id);
|
|
7702
7769
|
continue;
|
|
7703
7770
|
}
|
|
7704
7771
|
let c = typeof t?.project == "string" && t.project.trim() ? t.project.trim() : null;
|
|
7705
|
-
if (!
|
|
7772
|
+
if (!Ou(e, c, n)) {
|
|
7706
7773
|
a += 1, o ||= {
|
|
7707
7774
|
reason: "project_filter",
|
|
7708
7775
|
op_id: s.op_id,
|
|
@@ -7711,11 +7778,11 @@ function Ou(e, t, n) {
|
|
|
7711
7778
|
entity_id: s.entity_id,
|
|
7712
7779
|
project: c,
|
|
7713
7780
|
skipped_count: 0
|
|
7714
|
-
}, i =
|
|
7781
|
+
}, i = pu(s.created_at, s.op_id);
|
|
7715
7782
|
continue;
|
|
7716
7783
|
}
|
|
7717
7784
|
}
|
|
7718
|
-
r.push(s), i =
|
|
7785
|
+
r.push(s), i = pu(s.created_at, s.op_id);
|
|
7719
7786
|
}
|
|
7720
7787
|
return o && (o.skipped_count = a), [
|
|
7721
7788
|
r,
|
|
@@ -7723,12 +7790,12 @@ function Ou(e, t, n) {
|
|
|
7723
7790
|
o
|
|
7724
7791
|
];
|
|
7725
7792
|
}
|
|
7726
|
-
function
|
|
7727
|
-
let [r, i] =
|
|
7793
|
+
function Nu(e, t, n) {
|
|
7794
|
+
let [r, i] = Mu(e, t, n);
|
|
7728
7795
|
return [r, i];
|
|
7729
7796
|
}
|
|
7730
|
-
var
|
|
7731
|
-
function
|
|
7797
|
+
var Pu = /^legacy:memory_item:(.+)$/;
|
|
7798
|
+
function Fu(e, t = 2e3) {
|
|
7732
7799
|
let n = N(e, { schema: H }), r = n.select({ device_id: X.device_id }).from(X).limit(1).get(), i = String(r?.device_id ?? "").trim();
|
|
7733
7800
|
if (!i) return 0;
|
|
7734
7801
|
let a = n.select({
|
|
@@ -7742,7 +7809,7 @@ function ju(e, t = 2e3) {
|
|
|
7742
7809
|
let t = Number(e.id), r = String(e.import_key ?? "").trim(), a = z(e.metadata_json), s = typeof a.clock_device_id == "string" ? a.clock_device_id.trim() : "", c = "";
|
|
7743
7810
|
if (!r) c = `legacy:${i}:memory_item:${t}`;
|
|
7744
7811
|
else {
|
|
7745
|
-
let e = r.match(
|
|
7812
|
+
let e = r.match(Pu);
|
|
7746
7813
|
if (!e) continue;
|
|
7747
7814
|
let t = e[1] ?? "", n = s && s !== "local" ? s : i;
|
|
7748
7815
|
c = n ? `legacy:${n}:memory_item:${t}` : "";
|
|
@@ -7753,9 +7820,9 @@ function ju(e, t = 2e3) {
|
|
|
7753
7820
|
}
|
|
7754
7821
|
return o;
|
|
7755
7822
|
}
|
|
7756
|
-
function
|
|
7823
|
+
function Iu(e, t = 200) {
|
|
7757
7824
|
if (t <= 0) return 0;
|
|
7758
|
-
|
|
7825
|
+
Fu(e, 2e3);
|
|
7759
7826
|
let n = N(e, { schema: H }), r = n.select({ device_id: X.device_id }).from(X).limit(1).get(), i = String(r?.device_id ?? "").trim(), a = n.select().from(W).where(D(ce(re(W.deleted_at), k(W.active, 0)), M`NOT EXISTS (
|
|
7760
7827
|
SELECT 1
|
|
7761
7828
|
FROM replication_ops ro
|
|
@@ -7780,7 +7847,7 @@ function Mu(e, t = 200) {
|
|
|
7780
7847
|
for (let e of s) {
|
|
7781
7848
|
let t = Number(e.id ?? 0);
|
|
7782
7849
|
if (!t) continue;
|
|
7783
|
-
let r = typeof e.metadata_json == "string" ? z(e.metadata_json) :
|
|
7850
|
+
let r = typeof e.metadata_json == "string" ? z(e.metadata_json) : Lu(e.metadata_json), a = typeof r.clock_device_id == "string" ? r.clock_device_id.trim() : "", o = a && a !== "local" ? a : i, s = String(e.import_key ?? "").trim();
|
|
7784
7851
|
if (!s) {
|
|
7785
7852
|
if (!o) continue;
|
|
7786
7853
|
s = `legacy:${o}:memory_item:${t}`, n.update(W).set({ import_key: s }).where(k(W.id, t)).run();
|
|
@@ -7789,7 +7856,7 @@ function Mu(e, t = 200) {
|
|
|
7789
7856
|
if (n.select({ one: M`1` }).from(J).where(k(J.op_id, p)).limit(1).get()) continue;
|
|
7790
7857
|
let m = o;
|
|
7791
7858
|
if (!m) continue;
|
|
7792
|
-
let h =
|
|
7859
|
+
let h = Ru(e);
|
|
7793
7860
|
n.insert(J).values({
|
|
7794
7861
|
op_id: p,
|
|
7795
7862
|
entity_type: "memory_item",
|
|
@@ -7805,10 +7872,10 @@ function Mu(e, t = 200) {
|
|
|
7805
7872
|
}
|
|
7806
7873
|
return l;
|
|
7807
7874
|
}
|
|
7808
|
-
function
|
|
7875
|
+
function Lu(e) {
|
|
7809
7876
|
return typeof e == "string" ? z(e) : e && typeof e == "object" ? e : {};
|
|
7810
7877
|
}
|
|
7811
|
-
function
|
|
7878
|
+
function Ru(e) {
|
|
7812
7879
|
let t = (e) => {
|
|
7813
7880
|
if (typeof e != "string") return e ?? null;
|
|
7814
7881
|
try {
|
|
@@ -7849,7 +7916,7 @@ function Pu(e) {
|
|
|
7849
7916
|
import_key: e.import_key ?? null
|
|
7850
7917
|
};
|
|
7851
7918
|
}
|
|
7852
|
-
function
|
|
7919
|
+
function zu(e, t, n) {
|
|
7853
7920
|
let r = N(e, { schema: H }), i = {
|
|
7854
7921
|
applied: 0,
|
|
7855
7922
|
skipped: 0,
|
|
@@ -7874,14 +7941,14 @@ function Fu(e, t, n) {
|
|
|
7874
7941
|
metadata_json: W.metadata_json
|
|
7875
7942
|
}).from(W).where(k(W.import_key, t)).get();
|
|
7876
7943
|
if (n) {
|
|
7877
|
-
let a =
|
|
7878
|
-
if (!
|
|
7879
|
-
i.conflicts++,
|
|
7944
|
+
let a = ru(n.metadata_json), o = lu(n.rev ?? 0, n.updated_at ?? "", a);
|
|
7945
|
+
if (!uu(lu(e.clock_rev, e.clock_updated_at, e.clock_device_id), o)) {
|
|
7946
|
+
i.conflicts++, Bu(r, e);
|
|
7880
7947
|
continue;
|
|
7881
7948
|
}
|
|
7882
|
-
let s =
|
|
7949
|
+
let s = Jl(e, i.errors);
|
|
7883
7950
|
if (!s) continue;
|
|
7884
|
-
let c =
|
|
7951
|
+
let c = nu(s.metadata_json, e.clock_device_id);
|
|
7885
7952
|
r.update(W).set({
|
|
7886
7953
|
kind: M`COALESCE(${s.kind}, ${W.kind})`,
|
|
7887
7954
|
title: M`COALESCE(${s.title}, ${W.title})`,
|
|
@@ -7909,9 +7976,9 @@ function Fu(e, t, n) {
|
|
|
7909
7976
|
files_modified: V(s.files_modified)
|
|
7910
7977
|
}).where(k(W.import_key, t)).run();
|
|
7911
7978
|
} else {
|
|
7912
|
-
let n =
|
|
7979
|
+
let n = Jl(e, i.errors);
|
|
7913
7980
|
if (!n) continue;
|
|
7914
|
-
let a =
|
|
7981
|
+
let a = Vu(r, null, e.clock_updated_at), o = nu(n.metadata_json, e.clock_device_id);
|
|
7915
7982
|
r.insert(W).values({
|
|
7916
7983
|
session_id: a,
|
|
7917
7984
|
kind: n.kind ?? "discovery",
|
|
@@ -7950,9 +8017,9 @@ function Fu(e, t, n) {
|
|
|
7950
8017
|
metadata_json: W.metadata_json
|
|
7951
8018
|
}).from(W).where(k(W.import_key, t)).limit(1).get();
|
|
7952
8019
|
if (n) {
|
|
7953
|
-
let t =
|
|
7954
|
-
if (!
|
|
7955
|
-
i.conflicts++,
|
|
8020
|
+
let t = lu(n.rev ?? 1, n.updated_at ?? "", ru(n.metadata_json));
|
|
8021
|
+
if (!uu(lu(e.clock_rev, e.clock_updated_at, e.clock_device_id), t)) {
|
|
8022
|
+
i.conflicts++, Bu(r, e);
|
|
7956
8023
|
continue;
|
|
7957
8024
|
}
|
|
7958
8025
|
let a = (/* @__PURE__ */ new Date()).toISOString();
|
|
@@ -7964,16 +8031,16 @@ function Fu(e, t, n) {
|
|
|
7964
8031
|
}).where(k(W.id, n.id)).run();
|
|
7965
8032
|
}
|
|
7966
8033
|
} else {
|
|
7967
|
-
i.skipped++,
|
|
8034
|
+
i.skipped++, Bu(r, e);
|
|
7968
8035
|
continue;
|
|
7969
8036
|
}
|
|
7970
|
-
|
|
8037
|
+
Bu(r, e), i.applied++;
|
|
7971
8038
|
} catch (t) {
|
|
7972
8039
|
i.errors.push(`op ${e.op_id}: ${t instanceof Error ? t.message : String(t)}`);
|
|
7973
8040
|
}
|
|
7974
8041
|
})(), i;
|
|
7975
8042
|
}
|
|
7976
|
-
function
|
|
8043
|
+
function Bu(e, t) {
|
|
7977
8044
|
e.insert(J).values({
|
|
7978
8045
|
op_id: t.op_id,
|
|
7979
8046
|
entity_type: t.entity_type,
|
|
@@ -7987,7 +8054,7 @@ function Iu(e, t) {
|
|
|
7987
8054
|
created_at: t.created_at
|
|
7988
8055
|
}).onConflictDoNothing().run();
|
|
7989
8056
|
}
|
|
7990
|
-
function
|
|
8057
|
+
function Vu(e, t, n) {
|
|
7991
8058
|
if (t != null && e.select({ id: U.id }).from(U).where(k(U.id, t)).get()) return t;
|
|
7992
8059
|
let r = n || (/* @__PURE__ */ new Date()).toISOString(), i = e.insert(U).values({
|
|
7993
8060
|
started_at: r,
|
|
@@ -8000,7 +8067,7 @@ function Lu(e, t, n) {
|
|
|
8000
8067
|
}
|
|
8001
8068
|
//#endregion
|
|
8002
8069
|
//#region src/store.ts
|
|
8003
|
-
var
|
|
8070
|
+
var Hu = new Set([
|
|
8004
8071
|
"discovery",
|
|
8005
8072
|
"change",
|
|
8006
8073
|
"feature",
|
|
@@ -8009,27 +8076,27 @@ var Ru = new Set([
|
|
|
8009
8076
|
"decision",
|
|
8010
8077
|
"exploration"
|
|
8011
8078
|
]);
|
|
8012
|
-
function
|
|
8079
|
+
function Uu(e) {
|
|
8013
8080
|
let t = e.trim().toLowerCase();
|
|
8014
|
-
if (!
|
|
8081
|
+
if (!Hu.has(t)) throw Error(`Invalid memory kind "${e}". Allowed: ${[...Hu].join(", ")}`);
|
|
8015
8082
|
return t;
|
|
8016
8083
|
}
|
|
8017
|
-
function
|
|
8084
|
+
function Wu() {
|
|
8018
8085
|
return (/* @__PURE__ */ new Date()).toISOString();
|
|
8019
8086
|
}
|
|
8020
|
-
function
|
|
8087
|
+
function Gu(e) {
|
|
8021
8088
|
return (e.match(/\?/g) ?? []).length;
|
|
8022
8089
|
}
|
|
8023
|
-
function
|
|
8090
|
+
function Ku(e, t) {
|
|
8024
8091
|
let n = e.split("?"), r = M.raw(n[0] ?? "");
|
|
8025
8092
|
for (let e = 1; e < n.length; e++) r = M`${r}${t[e - 1]}${M.raw(n[e] ?? "")}`;
|
|
8026
8093
|
return r;
|
|
8027
8094
|
}
|
|
8028
|
-
function
|
|
8095
|
+
function qu(e, t) {
|
|
8029
8096
|
let n = [], r = 0;
|
|
8030
8097
|
for (let i of e) {
|
|
8031
|
-
let e =
|
|
8032
|
-
n.push(
|
|
8098
|
+
let e = Gu(i), a = t.slice(r, r + e);
|
|
8099
|
+
n.push(Ku(i, a)), r += e;
|
|
8033
8100
|
}
|
|
8034
8101
|
if (r !== t.length) throw Error("filter parameter mismatch while building SQL clauses");
|
|
8035
8102
|
if (n.length === 1) return n[0] ?? M`1=1`;
|
|
@@ -8042,7 +8109,7 @@ function $(e) {
|
|
|
8042
8109
|
let t = e.trim();
|
|
8043
8110
|
return t.length > 0 ? t : null;
|
|
8044
8111
|
}
|
|
8045
|
-
function
|
|
8112
|
+
function Ju(e) {
|
|
8046
8113
|
if (typeof e != "string" || !e.trim()) return [];
|
|
8047
8114
|
try {
|
|
8048
8115
|
let t = JSON.parse(e);
|
|
@@ -8051,21 +8118,21 @@ function Wu(e) {
|
|
|
8051
8118
|
return [];
|
|
8052
8119
|
}
|
|
8053
8120
|
}
|
|
8054
|
-
function
|
|
8121
|
+
function Yu(e) {
|
|
8055
8122
|
let t = $(e);
|
|
8056
8123
|
if (!t) return "";
|
|
8057
8124
|
let n = t.replace(/\\/g, "/").split("/").filter(Boolean);
|
|
8058
8125
|
return n[n.length - 1] ?? t;
|
|
8059
8126
|
}
|
|
8060
|
-
var
|
|
8061
|
-
function
|
|
8127
|
+
var Xu = "Legacy synced peer", Zu = "shared:legacy";
|
|
8128
|
+
function Qu(e) {
|
|
8062
8129
|
let { metadata_json: t, ...n } = e;
|
|
8063
8130
|
return {
|
|
8064
8131
|
...n,
|
|
8065
8132
|
metadata_json: z(t)
|
|
8066
8133
|
};
|
|
8067
8134
|
}
|
|
8068
|
-
var
|
|
8135
|
+
var $u = class {
|
|
8069
8136
|
db;
|
|
8070
8137
|
dbPath;
|
|
8071
8138
|
deviceId;
|
|
@@ -8107,10 +8174,10 @@ var Yu = class {
|
|
|
8107
8174
|
}
|
|
8108
8175
|
get(e) {
|
|
8109
8176
|
let t = this.d.select().from(W).where(k(W.id, e)).get();
|
|
8110
|
-
return t ?
|
|
8177
|
+
return t ? Qu(t) : null;
|
|
8111
8178
|
}
|
|
8112
8179
|
startSession(e) {
|
|
8113
|
-
let t =
|
|
8180
|
+
let t = Wu(), n = this.d.insert(U).values({
|
|
8114
8181
|
started_at: t,
|
|
8115
8182
|
cwd: e.cwd ?? process.cwd(),
|
|
8116
8183
|
project: e.project ?? null,
|
|
@@ -8124,7 +8191,7 @@ var Yu = class {
|
|
|
8124
8191
|
return n;
|
|
8125
8192
|
}
|
|
8126
8193
|
endSession(e, t) {
|
|
8127
|
-
let n =
|
|
8194
|
+
let n = Wu();
|
|
8128
8195
|
if (t) {
|
|
8129
8196
|
let r = {
|
|
8130
8197
|
...z(this.d.select({ metadata_json: U.metadata_json }).from(U).where(k(U.id, e)).get()?.metadata_json),
|
|
@@ -8137,7 +8204,7 @@ var Yu = class {
|
|
|
8137
8204
|
} else this.d.update(U).set({ ended_at: n }).where(k(U.id, e)).run();
|
|
8138
8205
|
}
|
|
8139
8206
|
remember(e, t, n, r, i = .5, o, s) {
|
|
8140
|
-
let c =
|
|
8207
|
+
let c = Uu(t), l = Wu(), u = o ? [...new Set(o)].sort().join(" ") : "", d = { ...s ?? {} };
|
|
8141
8208
|
d.clock_device_id ??= this.deviceId;
|
|
8142
8209
|
let f = d.import_key || a();
|
|
8143
8210
|
d.import_key = f;
|
|
@@ -8174,7 +8241,7 @@ var Yu = class {
|
|
|
8174
8241
|
}).returning({ id: W.id }).all()[0]?.id;
|
|
8175
8242
|
if (S == null) throw Error("memory insert returned no id");
|
|
8176
8243
|
try {
|
|
8177
|
-
|
|
8244
|
+
du(this.db, {
|
|
8178
8245
|
memoryId: S,
|
|
8179
8246
|
opType: "upsert",
|
|
8180
8247
|
deviceId: this.deviceId
|
|
@@ -8222,7 +8289,7 @@ var Yu = class {
|
|
|
8222
8289
|
if (!t) return;
|
|
8223
8290
|
let n = z(t.metadata_json);
|
|
8224
8291
|
n.clock_device_id = this.deviceId;
|
|
8225
|
-
let r =
|
|
8292
|
+
let r = Wu(), i = (t.rev ?? 0) + 1;
|
|
8226
8293
|
this.d.update(W).set({
|
|
8227
8294
|
active: 0,
|
|
8228
8295
|
deleted_at: r,
|
|
@@ -8231,7 +8298,7 @@ var Yu = class {
|
|
|
8231
8298
|
rev: i
|
|
8232
8299
|
}).where(k(W.id, e)).run();
|
|
8233
8300
|
try {
|
|
8234
|
-
|
|
8301
|
+
du(this.db, {
|
|
8235
8302
|
memoryId: e,
|
|
8236
8303
|
opType: "delete",
|
|
8237
8304
|
deviceId: this.deviceId
|
|
@@ -8240,20 +8307,20 @@ var Yu = class {
|
|
|
8240
8307
|
}).immediate();
|
|
8241
8308
|
}
|
|
8242
8309
|
recent(e = 10, t, n = 0) {
|
|
8243
|
-
let r = ["memory_items.active = 1"], i = Ma(t), a =
|
|
8310
|
+
let r = ["memory_items.active = 1"], i = Ma(t), a = qu([...r, ...i.clauses], i.params), o = i.joinSessions ? M.raw("memory_items JOIN sessions ON sessions.id = memory_items.session_id") : M.raw("memory_items");
|
|
8244
8311
|
return this.d.all(M`SELECT memory_items.* FROM ${o}
|
|
8245
8312
|
WHERE ${a}
|
|
8246
8313
|
ORDER BY created_at DESC
|
|
8247
|
-
LIMIT ${e} OFFSET ${Math.max(n, 0)}`).map((e) =>
|
|
8314
|
+
LIMIT ${e} OFFSET ${Math.max(n, 0)}`).map((e) => Qu(e));
|
|
8248
8315
|
}
|
|
8249
8316
|
recentByKinds(e, t = 10, n, r = 0) {
|
|
8250
8317
|
let i = e.filter((e) => e.length > 0);
|
|
8251
8318
|
if (i.length === 0) return [];
|
|
8252
|
-
let a = ["memory_items.active = 1", `memory_items.kind IN (${i.map(() => "?").join(", ")})`], o = Ma(n), s =
|
|
8319
|
+
let a = ["memory_items.active = 1", `memory_items.kind IN (${i.map(() => "?").join(", ")})`], o = Ma(n), s = qu([...a, ...o.clauses], [...i, ...o.params]), c = o.joinSessions ? M.raw("memory_items JOIN sessions ON sessions.id = memory_items.session_id") : M.raw("memory_items");
|
|
8253
8320
|
return this.d.all(M`SELECT memory_items.* FROM ${c}
|
|
8254
8321
|
WHERE ${s}
|
|
8255
8322
|
ORDER BY created_at DESC
|
|
8256
|
-
LIMIT ${t} OFFSET ${Math.max(r, 0)}`).map((e) =>
|
|
8323
|
+
LIMIT ${t} OFFSET ${Math.max(r, 0)}`).map((e) => Qu(e));
|
|
8257
8324
|
}
|
|
8258
8325
|
stats() {
|
|
8259
8326
|
let e = (e) => this.d.select({ c: M`COUNT(*)` }).from(e).get()?.c ?? 0, t = e(W), n = this.d.select({ c: M`COUNT(*)` }).from(W).where(k(W.active, 1)).get()?.c ?? 0, r = e(U), i = e($t), a = e(G), o = 0;
|
|
@@ -8318,7 +8385,7 @@ var Yu = class {
|
|
|
8318
8385
|
if (!this.memoryOwnedBySelf(t)) throw Error("memory not owned by this device");
|
|
8319
8386
|
let r = $(t.actor_id) ?? this.actorId, i = n === "shared" ? "shared" : "personal", a = n === "shared" && t.workspace_id?.startsWith("shared:") ? t.workspace_id : i === "personal" ? `personal:${r}` : "shared:default", o = z(t.metadata_json);
|
|
8320
8387
|
o.visibility = n, o.workspace_kind = i, o.workspace_id = a, o.clock_device_id = this.deviceId;
|
|
8321
|
-
let s =
|
|
8388
|
+
let s = Wu(), c = (t.rev ?? 0) + 1;
|
|
8322
8389
|
this.d.update(W).set({
|
|
8323
8390
|
visibility: n,
|
|
8324
8391
|
workspace_kind: i,
|
|
@@ -8328,7 +8395,7 @@ var Yu = class {
|
|
|
8328
8395
|
rev: c
|
|
8329
8396
|
}).where(k(W.id, e)).run();
|
|
8330
8397
|
try {
|
|
8331
|
-
|
|
8398
|
+
du(this.db, {
|
|
8332
8399
|
memoryId: e,
|
|
8333
8400
|
opType: "upsert",
|
|
8334
8401
|
deviceId: this.deviceId
|
|
@@ -8340,19 +8407,19 @@ var Yu = class {
|
|
|
8340
8407
|
}).immediate();
|
|
8341
8408
|
}
|
|
8342
8409
|
search(e, t = 10, n) {
|
|
8343
|
-
return
|
|
8410
|
+
return Gc(this, e, t, n);
|
|
8344
8411
|
}
|
|
8345
8412
|
timeline(e, t, n = 3, r = 3, i) {
|
|
8346
|
-
return
|
|
8413
|
+
return qc(this, e, t, n, r, i);
|
|
8347
8414
|
}
|
|
8348
8415
|
explain(e, t, n = 10, r, i) {
|
|
8349
|
-
return
|
|
8416
|
+
return $c(this, e, t, n, r, i);
|
|
8350
8417
|
}
|
|
8351
8418
|
buildMemoryPack(e, t, n, r) {
|
|
8352
|
-
return
|
|
8419
|
+
return Pl(this, e, t, n ?? null, r);
|
|
8353
8420
|
}
|
|
8354
8421
|
async buildMemoryPackAsync(e, t, n, r) {
|
|
8355
|
-
return
|
|
8422
|
+
return Fl(this, e, t, n ?? null, r);
|
|
8356
8423
|
}
|
|
8357
8424
|
normalizeStreamIdentity(e, t) {
|
|
8358
8425
|
let n = e.trim().toLowerCase() || "opencode", r = t.trim();
|
|
@@ -8536,7 +8603,7 @@ var Yu = class {
|
|
|
8536
8603
|
}).run();
|
|
8537
8604
|
}
|
|
8538
8605
|
updateRawEventIngestStats(e, t, n, r) {
|
|
8539
|
-
let i =
|
|
8606
|
+
let i = Wu(), a = t + n + r;
|
|
8540
8607
|
this.d.insert(un).values({
|
|
8541
8608
|
created_at: i,
|
|
8542
8609
|
inserted_events: e,
|
|
@@ -8569,7 +8636,7 @@ var Yu = class {
|
|
|
8569
8636
|
if (!e.eventType.trim()) throw Error("event_type is required");
|
|
8570
8637
|
let [t, n] = this.normalizeStreamIdentity(e.source ?? "opencode", e.opencodeSessionId);
|
|
8571
8638
|
return this.db.transaction(() => {
|
|
8572
|
-
let r =
|
|
8639
|
+
let r = Wu();
|
|
8573
8640
|
if (this.d.select({ one: M`1` }).from(G).where(D(k(G.source, t), k(G.stream_id, n), k(G.event_id, e.eventId))).get() != null) return this.updateRawEventIngestStats(0, 0, 1, 0), !1;
|
|
8574
8641
|
this.d.select({ one: M`1` }).from(K).where(D(k(K.source, t), k(K.stream_id, n))).get() ?? this.d.insert(K).values({
|
|
8575
8642
|
opencode_session_id: n,
|
|
@@ -8601,7 +8668,7 @@ var Yu = class {
|
|
|
8601
8668
|
if (!e.trim()) throw Error("opencode_session_id is required");
|
|
8602
8669
|
let [n, r] = this.normalizeStreamIdentity("opencode", e);
|
|
8603
8670
|
return this.db.transaction(() => {
|
|
8604
|
-
let e =
|
|
8671
|
+
let e = Wu();
|
|
8605
8672
|
this.d.select({ one: M`1` }).from(K).where(D(k(K.source, n), k(K.stream_id, r))).get() ?? this.d.insert(K).values({
|
|
8606
8673
|
opencode_session_id: r,
|
|
8607
8674
|
source: n,
|
|
@@ -8670,7 +8737,7 @@ var Yu = class {
|
|
|
8670
8737
|
})();
|
|
8671
8738
|
}
|
|
8672
8739
|
updateRawEventSessionMeta(e) {
|
|
8673
|
-
let [t, n] = this.normalizeStreamIdentity(e.source ?? "opencode", e.opencodeSessionId), r =
|
|
8740
|
+
let [t, n] = this.normalizeStreamIdentity(e.source ?? "opencode", e.opencodeSessionId), r = Wu(), i = K;
|
|
8674
8741
|
this.d.insert(i).values({
|
|
8675
8742
|
opencode_session_id: n,
|
|
8676
8743
|
source: t,
|
|
@@ -8770,8 +8837,8 @@ var Yu = class {
|
|
|
8770
8837
|
${W.actor_id} IS NULL
|
|
8771
8838
|
OR TRIM(${W.actor_id}) = ''
|
|
8772
8839
|
OR ${W.actor_id} LIKE 'legacy-sync:%'
|
|
8773
|
-
OR ${W.actor_display_name} = ${
|
|
8774
|
-
OR ${W.workspace_id} = ${
|
|
8840
|
+
OR ${W.actor_display_name} = ${Xu}
|
|
8841
|
+
OR ${W.workspace_id} = ${Zu}
|
|
8775
8842
|
OR ${W.trust_state} = 'legacy_unknown'
|
|
8776
8843
|
)
|
|
8777
8844
|
)`, M`${W.origin_device_id} != ${this.deviceId}`, M`${W.origin_device_id} NOT IN (
|
|
@@ -8785,13 +8852,13 @@ var Yu = class {
|
|
|
8785
8852
|
}));
|
|
8786
8853
|
}
|
|
8787
8854
|
effectiveSyncProjectFilters(e) {
|
|
8788
|
-
let t = tt(), n =
|
|
8855
|
+
let t = tt(), n = Ju(JSON.stringify(t.sync_projects_include ?? [])), r = Ju(JSON.stringify(t.sync_projects_exclude ?? []));
|
|
8789
8856
|
if (e) {
|
|
8790
8857
|
let t = this.d.select({
|
|
8791
8858
|
projects_include_json: Y.projects_include_json,
|
|
8792
8859
|
projects_exclude_json: Y.projects_exclude_json
|
|
8793
8860
|
}).from(Y).where(k(Y.peer_device_id, e)).limit(1).get();
|
|
8794
|
-
t && (t.projects_include_json != null && (n =
|
|
8861
|
+
t && (t.projects_include_json != null && (n = Ju(t.projects_include_json)), t.projects_exclude_json != null && (r = Ju(t.projects_exclude_json)));
|
|
8795
8862
|
}
|
|
8796
8863
|
return {
|
|
8797
8864
|
include: n,
|
|
@@ -8799,7 +8866,7 @@ var Yu = class {
|
|
|
8799
8866
|
};
|
|
8800
8867
|
}
|
|
8801
8868
|
syncProjectAllowed(e, t) {
|
|
8802
|
-
let { include: n, exclude: r } = this.effectiveSyncProjectFilters(t), i = $(e), a =
|
|
8869
|
+
let { include: n, exclude: r } = this.effectiveSyncProjectFilters(t), i = $(e), a = Yu(i);
|
|
8803
8870
|
return !(n.length > 0 && !n.some((e) => e === i || e === a) || r.some((e) => e === i || e === a));
|
|
8804
8871
|
}
|
|
8805
8872
|
sharingReviewSummary(e) {
|
|
@@ -8819,7 +8886,7 @@ var Yu = class {
|
|
|
8819
8886
|
if (!n || !r || r === this.actorId) continue;
|
|
8820
8887
|
let i = $(e.project);
|
|
8821
8888
|
if (t) {
|
|
8822
|
-
let e =
|
|
8889
|
+
let e = Yu(t), n = Yu(i);
|
|
8823
8890
|
if (i !== t && n !== e) continue;
|
|
8824
8891
|
}
|
|
8825
8892
|
if (!this.syncProjectAllowed(i, n)) continue;
|
|
@@ -8843,11 +8910,11 @@ var Yu = class {
|
|
|
8843
8910
|
close() {
|
|
8844
8911
|
this.db.pragma("optimize"), this.db.close();
|
|
8845
8912
|
}
|
|
8846
|
-
},
|
|
8847
|
-
function
|
|
8913
|
+
}, ed = "_codemem._tcp.local.";
|
|
8914
|
+
function td(e) {
|
|
8848
8915
|
return e.mdns.length === 0 ? Ii(e.stored, []) : Ii(e.mdns, e.stored);
|
|
8849
8916
|
}
|
|
8850
|
-
function
|
|
8917
|
+
function nd(e, t) {
|
|
8851
8918
|
let n = N(e, { schema: H }).select({ addresses_json: Y.addresses_json }).from(Y).where(k(Y.peer_device_id, t)).get();
|
|
8852
8919
|
if (!n?.addresses_json) return [];
|
|
8853
8920
|
try {
|
|
@@ -8857,8 +8924,8 @@ function Qu(e, t) {
|
|
|
8857
8924
|
return [];
|
|
8858
8925
|
}
|
|
8859
8926
|
}
|
|
8860
|
-
function
|
|
8861
|
-
let i = Ii(
|
|
8927
|
+
function rd(e, t, n, r) {
|
|
8928
|
+
let i = Ii(nd(e, t), n), a = (/* @__PURE__ */ new Date()).toISOString(), o = JSON.stringify(i);
|
|
8862
8929
|
return N(e, { schema: H }).insert(Y).values({
|
|
8863
8930
|
peer_device_id: t,
|
|
8864
8931
|
name: r?.name ?? null,
|
|
@@ -8878,7 +8945,7 @@ function $u(e, t, n, r) {
|
|
|
8878
8945
|
}
|
|
8879
8946
|
}).run(), i;
|
|
8880
8947
|
}
|
|
8881
|
-
function
|
|
8948
|
+
function id(e, t, n) {
|
|
8882
8949
|
let r = N(e, { schema: H }), i = (/* @__PURE__ */ new Date()).toISOString();
|
|
8883
8950
|
r.insert(Z).values({
|
|
8884
8951
|
peer_device_id: t,
|
|
@@ -8893,32 +8960,32 @@ function ed(e, t, n) {
|
|
|
8893
8960
|
last_error: null
|
|
8894
8961
|
}).where(k(Y.peer_device_id, t)).run() : r.update(Y).set({ last_error: n.error ?? null }).where(k(Y.peer_device_id, t)).run();
|
|
8895
8962
|
}
|
|
8896
|
-
function
|
|
8897
|
-
let r =
|
|
8963
|
+
function ad(e, t, n) {
|
|
8964
|
+
let r = nd(e, t), i = Pi(n ?? ""), a = r;
|
|
8898
8965
|
return i && (a = [i, ...r.filter((e) => Pi(e) !== i)], N(e, { schema: H }).update(Y).set({
|
|
8899
8966
|
addresses_json: JSON.stringify(a),
|
|
8900
8967
|
last_sync_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
8901
8968
|
last_error: null
|
|
8902
8969
|
}).where(k(Y.peer_device_id, t)).run()), a;
|
|
8903
8970
|
}
|
|
8904
|
-
function
|
|
8971
|
+
function od(e, t, n) {
|
|
8905
8972
|
let r = n.include === null ? null : JSON.stringify(n.include), i = n.exclude === null ? null : JSON.stringify(n.exclude);
|
|
8906
8973
|
N(e, { schema: H }).update(Y).set({
|
|
8907
8974
|
projects_include_json: r,
|
|
8908
8975
|
projects_exclude_json: i
|
|
8909
8976
|
}).where(k(Y.peer_device_id, t)).run();
|
|
8910
8977
|
}
|
|
8911
|
-
function
|
|
8978
|
+
function sd(e, t, n) {
|
|
8912
8979
|
N(e, { schema: H }).update(Y).set({ claimed_local_actor: n ? 1 : 0 }).where(k(Y.peer_device_id, t)).run();
|
|
8913
8980
|
}
|
|
8914
|
-
function
|
|
8981
|
+
function cd(e) {
|
|
8915
8982
|
try {
|
|
8916
8983
|
return fe("which", [e], { stdio: "pipe" }), !0;
|
|
8917
8984
|
} catch {
|
|
8918
8985
|
return !1;
|
|
8919
8986
|
}
|
|
8920
8987
|
}
|
|
8921
|
-
function
|
|
8988
|
+
function ld(e, t = 1200) {
|
|
8922
8989
|
try {
|
|
8923
8990
|
return fe("dns-sd", e, {
|
|
8924
8991
|
encoding: "utf8",
|
|
@@ -8937,8 +9004,8 @@ function ad(e, t = 1200) {
|
|
|
8937
9004
|
return "";
|
|
8938
9005
|
}
|
|
8939
9006
|
}
|
|
8940
|
-
function
|
|
8941
|
-
let e =
|
|
9007
|
+
function ud() {
|
|
9008
|
+
let e = ld([
|
|
8942
9009
|
"-B",
|
|
8943
9010
|
"_codemem._tcp",
|
|
8944
9011
|
"local."
|
|
@@ -8957,8 +9024,8 @@ function od() {
|
|
|
8957
9024
|
}
|
|
8958
9025
|
return [...t];
|
|
8959
9026
|
}
|
|
8960
|
-
function
|
|
8961
|
-
let t =
|
|
9027
|
+
function dd(e) {
|
|
9028
|
+
let t = ld([
|
|
8962
9029
|
"-L",
|
|
8963
9030
|
e,
|
|
8964
9031
|
"_codemem._tcp",
|
|
@@ -8975,12 +9042,12 @@ function sd(e) {
|
|
|
8975
9042
|
properties: s
|
|
8976
9043
|
};
|
|
8977
9044
|
}
|
|
8978
|
-
function
|
|
9045
|
+
function fd() {
|
|
8979
9046
|
let e = process.env.CODEMEM_SYNC_MDNS;
|
|
8980
9047
|
return e ? e === "1" || e.toLowerCase() === "true" : !1;
|
|
8981
9048
|
}
|
|
8982
|
-
function
|
|
8983
|
-
if (!
|
|
9049
|
+
function pd(e, t) {
|
|
9050
|
+
if (!fd() || process.platform !== "darwin" || !cd("dns-sd")) return { close() {} };
|
|
8984
9051
|
let n = pe("dns-sd", [
|
|
8985
9052
|
"-R",
|
|
8986
9053
|
`codemem-${e.slice(0, 12)}`,
|
|
@@ -8998,18 +9065,18 @@ function ld(e, t) {
|
|
|
8998
9065
|
} catch {}
|
|
8999
9066
|
} };
|
|
9000
9067
|
}
|
|
9001
|
-
function
|
|
9002
|
-
if (!
|
|
9003
|
-
let e =
|
|
9068
|
+
function md() {
|
|
9069
|
+
if (!fd() || process.platform !== "darwin" || !cd("dns-sd")) return [];
|
|
9070
|
+
let e = ud();
|
|
9004
9071
|
if (e.length === 0) return [];
|
|
9005
9072
|
let t = [];
|
|
9006
9073
|
for (let n of e) {
|
|
9007
|
-
let e =
|
|
9074
|
+
let e = dd(n);
|
|
9008
9075
|
e && t.push(e);
|
|
9009
9076
|
}
|
|
9010
9077
|
return t;
|
|
9011
9078
|
}
|
|
9012
|
-
function
|
|
9079
|
+
function hd(e, t) {
|
|
9013
9080
|
let n = [];
|
|
9014
9081
|
for (let r of t) {
|
|
9015
9082
|
let t = r.properties ?? {}, i = t.device_id ?? t.device_id;
|
|
@@ -9023,40 +9090,40 @@ function dd(e, t) {
|
|
|
9023
9090
|
}
|
|
9024
9091
|
//#endregion
|
|
9025
9092
|
//#region src/sync-pass.ts
|
|
9026
|
-
var
|
|
9027
|
-
function
|
|
9093
|
+
var gd = 1048576, _d = 200, vd = "2";
|
|
9094
|
+
function yd(e, t) {
|
|
9028
9095
|
return !t || !t.includes("|") ? !1 : e ? t > e : !0;
|
|
9029
9096
|
}
|
|
9030
|
-
function
|
|
9097
|
+
function bd(e) {
|
|
9031
9098
|
if (!e) return null;
|
|
9032
9099
|
let t = e.error, n = e.reason;
|
|
9033
9100
|
return typeof t == "string" && typeof n == "string" ? `${t}:${n}` : typeof t == "string" ? t : null;
|
|
9034
9101
|
}
|
|
9035
|
-
function
|
|
9102
|
+
function xd(e) {
|
|
9036
9103
|
return e.length === 0 ? null : `all addresses failed | ${e.map((e) => `${e.address}: ${e.error}`).join(" || ")}`;
|
|
9037
9104
|
}
|
|
9038
|
-
function
|
|
9105
|
+
function Sd(e) {
|
|
9039
9106
|
if (typeof e != "string") return null;
|
|
9040
9107
|
let t = e.trim();
|
|
9041
9108
|
return t.length > 0 ? t : null;
|
|
9042
9109
|
}
|
|
9043
|
-
function
|
|
9110
|
+
function Cd(e) {
|
|
9044
9111
|
let t = e?.sync_reset;
|
|
9045
9112
|
if (!t || typeof t != "object" || Array.isArray(t)) return null;
|
|
9046
9113
|
let n = t;
|
|
9047
9114
|
if (!Number.isFinite(n.generation) || typeof n.snapshot_id != "string" || n.snapshot_id.trim().length === 0) return null;
|
|
9048
|
-
let r =
|
|
9115
|
+
let r = Sd(n.baseline_cursor);
|
|
9049
9116
|
return {
|
|
9050
9117
|
generation: Number(n.generation),
|
|
9051
9118
|
snapshot_id: n.snapshot_id.trim(),
|
|
9052
9119
|
baseline_cursor: r,
|
|
9053
|
-
retained_floor_cursor:
|
|
9120
|
+
retained_floor_cursor: Sd(n.retained_floor_cursor)
|
|
9054
9121
|
};
|
|
9055
9122
|
}
|
|
9056
|
-
function
|
|
9123
|
+
function wd(e) {
|
|
9057
9124
|
return !(!e || e.reset_required !== !1 || !Number.isFinite(e.generation) || typeof e.snapshot_id != "string" || e.snapshot_id.trim().length === 0 || !(e.baseline_cursor == null || typeof e.baseline_cursor == "string") || !(e.retained_floor_cursor == null || typeof e.retained_floor_cursor == "string") || !Array.isArray(e.ops) || !(e.next_cursor == null || typeof e.next_cursor == "string") || !Number.isFinite(e.skipped));
|
|
9058
9125
|
}
|
|
9059
|
-
function
|
|
9126
|
+
function Td(e, t, n) {
|
|
9060
9127
|
let r = N(e, { schema: H }), i = (/* @__PURE__ */ new Date()).toISOString();
|
|
9061
9128
|
r.insert(Z).values({
|
|
9062
9129
|
peer_device_id: t,
|
|
@@ -9068,7 +9135,7 @@ function xd(e, t, n) {
|
|
|
9068
9135
|
error: n.error ?? null
|
|
9069
9136
|
}).run();
|
|
9070
9137
|
}
|
|
9071
|
-
function
|
|
9138
|
+
function Ed(e, t) {
|
|
9072
9139
|
let n = N(e, { schema: H }), r = (/* @__PURE__ */ new Date()).toISOString();
|
|
9073
9140
|
n.update(Y).set({
|
|
9074
9141
|
last_sync_at: r,
|
|
@@ -9076,7 +9143,7 @@ function Sd(e, t) {
|
|
|
9076
9143
|
last_error: null
|
|
9077
9144
|
}).where(k(Y.peer_device_id, t)).run();
|
|
9078
9145
|
}
|
|
9079
|
-
function
|
|
9146
|
+
function Dd(e, t, n, r) {
|
|
9080
9147
|
let i = N(e, { schema: H }), { replicationOps: a } = H, o;
|
|
9081
9148
|
if (t) {
|
|
9082
9149
|
let e = t.indexOf("|"), s = e >= 0 ? t.slice(0, e) : t, c = e >= 0 ? t.slice(e + 1) : "";
|
|
@@ -9088,8 +9155,8 @@ function Cd(e, t, n, r) {
|
|
|
9088
9155
|
let c = `${s.created_at}|${s.op_id}`;
|
|
9089
9156
|
return [o, c];
|
|
9090
9157
|
}
|
|
9091
|
-
var
|
|
9092
|
-
async function
|
|
9158
|
+
var Od = 8;
|
|
9159
|
+
async function kd(e, t, n, r, i = 0) {
|
|
9093
9160
|
if (n.length === 0) return;
|
|
9094
9161
|
let a = { ops: n }, o = Buffer.from(JSON.stringify(a), "utf-8"), [s, c] = await jn("POST", e, {
|
|
9095
9162
|
headers: xi({
|
|
@@ -9103,20 +9170,20 @@ async function Td(e, t, n, r, i = 0) {
|
|
|
9103
9170
|
bodyBytes: o
|
|
9104
9171
|
});
|
|
9105
9172
|
if (s === 200 && c != null) return;
|
|
9106
|
-
let l =
|
|
9107
|
-
if (s === 413 && n.length > 1 && i <
|
|
9173
|
+
let l = bd(c);
|
|
9174
|
+
if (s === 413 && n.length > 1 && i < Od && (l === "payload_too_large" || l === "too_many_ops")) {
|
|
9108
9175
|
let a = Math.floor(n.length / 2);
|
|
9109
|
-
await
|
|
9176
|
+
await kd(e, t, n.slice(0, a), r, i + 1), await kd(e, t, n.slice(a), r, i + 1);
|
|
9110
9177
|
return;
|
|
9111
9178
|
}
|
|
9112
9179
|
let u = l ? ` (${s}: ${l})` : ` (${s})`;
|
|
9113
9180
|
throw Error(`peer ops push failed${u}`);
|
|
9114
9181
|
}
|
|
9115
|
-
function
|
|
9116
|
-
|
|
9182
|
+
function Ad(e) {
|
|
9183
|
+
Fu(e, 2e3), Iu(e, 200);
|
|
9117
9184
|
}
|
|
9118
|
-
async function
|
|
9119
|
-
let i = r?.limit ??
|
|
9185
|
+
async function jd(e, t, n, r) {
|
|
9186
|
+
let i = r?.limit ?? _d, a = r?.keysDir, o = N(e, { schema: H }).select({ pinned_fingerprint: Y.pinned_fingerprint }).from(Y).where(k(Y.peer_device_id, t)).get()?.pinned_fingerprint ?? "";
|
|
9120
9187
|
if (!o) return {
|
|
9121
9188
|
ok: !1,
|
|
9122
9189
|
error: "peer not pinned",
|
|
@@ -9124,12 +9191,12 @@ async function Dd(e, t, n, r) {
|
|
|
9124
9191
|
opsOut: 0,
|
|
9125
9192
|
addressErrors: []
|
|
9126
9193
|
};
|
|
9127
|
-
let [s, c] =
|
|
9194
|
+
let [s, c] = au(e, t), l;
|
|
9128
9195
|
try {
|
|
9129
9196
|
[l] = Qn(e, { keysDir: a });
|
|
9130
9197
|
} catch (n) {
|
|
9131
9198
|
let r = `device identity unavailable: ${n instanceof Error ? n.message.trim() || n.constructor.name : "unknown"}`;
|
|
9132
|
-
return
|
|
9199
|
+
return Td(e, t, {
|
|
9133
9200
|
ok: !1,
|
|
9134
9201
|
error: r
|
|
9135
9202
|
}), {
|
|
@@ -9154,12 +9221,12 @@ async function Dd(e, t, n, r) {
|
|
|
9154
9221
|
keysDir: a
|
|
9155
9222
|
}) });
|
|
9156
9223
|
if (u !== 200 || !d) {
|
|
9157
|
-
let e =
|
|
9224
|
+
let e = bd(d), t = e ? ` (${u}: ${e})` : ` (${u})`;
|
|
9158
9225
|
throw Error(`peer status failed${t}`);
|
|
9159
9226
|
}
|
|
9160
9227
|
if (d.fingerprint !== o) throw Error("peer fingerprint mismatch");
|
|
9161
|
-
if (String(d.protocol_version ?? "") !==
|
|
9162
|
-
let f =
|
|
9228
|
+
if (String(d.protocol_version ?? "") !== vd) throw Error(`peer protocol mismatch (expected ${vd}, got ${String(d.protocol_version ?? "missing")})`);
|
|
9229
|
+
let f = Cd(d);
|
|
9163
9230
|
if (!f) throw Error("peer status missing sync_reset boundary");
|
|
9164
9231
|
let p = new URLSearchParams({
|
|
9165
9232
|
since: s ?? "",
|
|
@@ -9176,7 +9243,7 @@ async function Dd(e, t, n, r) {
|
|
|
9176
9243
|
keysDir: a
|
|
9177
9244
|
}) });
|
|
9178
9245
|
if (h === 409 && g?.reset_required === !0) {
|
|
9179
|
-
let r =
|
|
9246
|
+
let r = _u(e), i = {
|
|
9180
9247
|
reset_required: !0,
|
|
9181
9248
|
reason: g.reason === "generation_mismatch" || g.reason === "boundary_mismatch" ? g.reason : "stale_cursor",
|
|
9182
9249
|
generation: Number(g.generation ?? 1),
|
|
@@ -9184,7 +9251,7 @@ async function Dd(e, t, n, r) {
|
|
|
9184
9251
|
baseline_cursor: typeof g.baseline_cursor == "string" && g.baseline_cursor.trim() ? g.baseline_cursor.trim() : null,
|
|
9185
9252
|
retained_floor_cursor: typeof g.retained_floor_cursor == "string" && g.retained_floor_cursor.trim() ? g.retained_floor_cursor.trim() : null
|
|
9186
9253
|
};
|
|
9187
|
-
return
|
|
9254
|
+
return Td(e, t, {
|
|
9188
9255
|
ok: !1,
|
|
9189
9256
|
error: r.dirty ? `needs_attention:local_unsynced_shared_memory:${r.count}` : `reset_required:${i.reason}`
|
|
9190
9257
|
}), {
|
|
@@ -9198,19 +9265,19 @@ async function Dd(e, t, n, r) {
|
|
|
9198
9265
|
};
|
|
9199
9266
|
}
|
|
9200
9267
|
if (h !== 200 || g == null) {
|
|
9201
|
-
let e =
|
|
9268
|
+
let e = bd(g), t = e ? ` (${h}: ${e})` : ` (${h})`;
|
|
9202
9269
|
throw Error(`peer ops fetch failed${t}`);
|
|
9203
9270
|
}
|
|
9204
|
-
if (!
|
|
9205
|
-
let [_, v] =
|
|
9206
|
-
|
|
9207
|
-
let [x, S] =
|
|
9271
|
+
if (!wd(g)) throw Error("invalid ops response");
|
|
9272
|
+
let [_, v] = Mu(e, cu(g), t), y = zu(e, _, l), b = v || Sd(g.next_cursor);
|
|
9273
|
+
yd(s, b) && (ou(e, t, { lastApplied: b }), s = b);
|
|
9274
|
+
let [x, S] = Dd(e, c, l, i), [C, w] = Nu(e, x, t), T = `${n}/v1/ops`;
|
|
9208
9275
|
if (C.length > 0) {
|
|
9209
|
-
let e =
|
|
9210
|
-
for (let t of e) await
|
|
9276
|
+
let e = iu(C, gd);
|
|
9277
|
+
for (let t of e) await kd(T, l, t, a);
|
|
9211
9278
|
}
|
|
9212
9279
|
let E = w ?? S;
|
|
9213
|
-
return E &&
|
|
9280
|
+
return E && yd(c, E) && (ou(e, t, { lastAcked: E }), c = E), Ed(e, t), Td(e, t, {
|
|
9214
9281
|
ok: !0,
|
|
9215
9282
|
opsIn: y.applied,
|
|
9216
9283
|
opsOut: C.length
|
|
@@ -9230,8 +9297,8 @@ async function Dd(e, t, n, r) {
|
|
|
9230
9297
|
}
|
|
9231
9298
|
}
|
|
9232
9299
|
}
|
|
9233
|
-
let f =
|
|
9234
|
-
return d || (f = "no dialable peer addresses"), f ||= "sync failed without diagnostic detail",
|
|
9300
|
+
let f = xd(u);
|
|
9301
|
+
return d || (f = "no dialable peer addresses"), f ||= "sync failed without diagnostic detail", Td(e, t, {
|
|
9235
9302
|
ok: !1,
|
|
9236
9303
|
error: f
|
|
9237
9304
|
}), {
|
|
@@ -9242,15 +9309,15 @@ async function Dd(e, t, n, r) {
|
|
|
9242
9309
|
addressErrors: u
|
|
9243
9310
|
};
|
|
9244
9311
|
}
|
|
9245
|
-
async function
|
|
9312
|
+
async function Md(e, t, n) {
|
|
9246
9313
|
let r = N(e, { schema: H }).select({ addresses_json: Y.addresses_json }).from(Y).where(k(Y.peer_device_id, t)).get(), i = [];
|
|
9247
9314
|
if (r?.addresses_json) try {
|
|
9248
9315
|
let e = JSON.parse(r.addresses_json);
|
|
9249
9316
|
Array.isArray(e) && (i = e.filter((e) => typeof e == "string"));
|
|
9250
9317
|
} catch {}
|
|
9251
|
-
return
|
|
9318
|
+
return jd(e, t, i, n);
|
|
9252
9319
|
}
|
|
9253
|
-
var
|
|
9320
|
+
var Nd = [
|
|
9254
9321
|
"no route to host",
|
|
9255
9322
|
"connection refused",
|
|
9256
9323
|
"network is unreachable",
|
|
@@ -9261,33 +9328,33 @@ var kd = [
|
|
|
9261
9328
|
"errno 61",
|
|
9262
9329
|
"errno 60",
|
|
9263
9330
|
"errno 111"
|
|
9264
|
-
],
|
|
9265
|
-
function
|
|
9331
|
+
], Pd = 120, Fd = 1800;
|
|
9332
|
+
function Id(e) {
|
|
9266
9333
|
if (!e) return !1;
|
|
9267
9334
|
let t = e.toLowerCase();
|
|
9268
|
-
return
|
|
9335
|
+
return Nd.some((e) => t.includes(e));
|
|
9269
9336
|
}
|
|
9270
|
-
function
|
|
9337
|
+
function Ld(e, t, n = 10) {
|
|
9271
9338
|
let r = N(e, { schema: H }).select({
|
|
9272
9339
|
ok: Z.ok,
|
|
9273
9340
|
error: Z.error
|
|
9274
9341
|
}).from(Z).where(k(Z.peer_device_id, t)).orderBy(O(Z.started_at)).limit(n).all(), i = 0;
|
|
9275
9342
|
for (let e of r) {
|
|
9276
9343
|
if (e.ok) break;
|
|
9277
|
-
if (
|
|
9344
|
+
if (Id(e.error)) i++;
|
|
9278
9345
|
else break;
|
|
9279
9346
|
}
|
|
9280
9347
|
return i;
|
|
9281
9348
|
}
|
|
9282
|
-
function
|
|
9349
|
+
function Rd(e) {
|
|
9283
9350
|
if (e <= 1) return 0;
|
|
9284
9351
|
let t = Math.min(e - 1, 8);
|
|
9285
|
-
return Math.min(
|
|
9352
|
+
return Math.min(Pd * 2 ** (t - 1), Fd) * (.5 + Math.random() * .5);
|
|
9286
9353
|
}
|
|
9287
|
-
function
|
|
9288
|
-
let n =
|
|
9354
|
+
function zd(e, t) {
|
|
9355
|
+
let n = Ld(e, t);
|
|
9289
9356
|
if (n < 2) return !1;
|
|
9290
|
-
let r =
|
|
9357
|
+
let r = Rd(n);
|
|
9291
9358
|
if (r <= 0) return !1;
|
|
9292
9359
|
let i = N(e, { schema: H }).select({ started_at: Z.started_at }).from(Z).where(k(Z.peer_device_id, t)).orderBy(O(Z.started_at)).limit(1).get();
|
|
9293
9360
|
if (!i?.started_at) return !1;
|
|
@@ -9300,7 +9367,7 @@ function Fd(e, t) {
|
|
|
9300
9367
|
}
|
|
9301
9368
|
//#endregion
|
|
9302
9369
|
//#region src/sync-daemon.ts
|
|
9303
|
-
function
|
|
9370
|
+
function Bd(e) {
|
|
9304
9371
|
let t = N(e, { schema: H }), n = (/* @__PURE__ */ new Date()).toISOString();
|
|
9305
9372
|
t.insert(sn).values({
|
|
9306
9373
|
id: 1,
|
|
@@ -9310,7 +9377,7 @@ function Id(e) {
|
|
|
9310
9377
|
set: { last_ok_at: M`excluded.last_ok_at` }
|
|
9311
9378
|
}).run();
|
|
9312
9379
|
}
|
|
9313
|
-
function
|
|
9380
|
+
function Vd(e, t, n) {
|
|
9314
9381
|
let r = N(e, { schema: H }), i = (/* @__PURE__ */ new Date()).toISOString();
|
|
9315
9382
|
r.insert(sn).values({
|
|
9316
9383
|
id: 1,
|
|
@@ -9326,14 +9393,14 @@ function Ld(e, t, n) {
|
|
|
9326
9393
|
}
|
|
9327
9394
|
}).run();
|
|
9328
9395
|
}
|
|
9329
|
-
async function
|
|
9396
|
+
async function Hd(e, t) {
|
|
9330
9397
|
let n = N(e, { schema: H }).select({ peer_device_id: Y.peer_device_id }).from(Y).all();
|
|
9331
9398
|
if (n.length === 0) return [];
|
|
9332
|
-
|
|
9399
|
+
Ad(e);
|
|
9333
9400
|
let r = [];
|
|
9334
9401
|
for (let i of n) {
|
|
9335
9402
|
let n = i.peer_device_id;
|
|
9336
|
-
if (
|
|
9403
|
+
if (zd(e, n)) {
|
|
9337
9404
|
r.push({
|
|
9338
9405
|
ok: !1,
|
|
9339
9406
|
skipped: !0,
|
|
@@ -9341,7 +9408,7 @@ async function Rd(e, t) {
|
|
|
9341
9408
|
});
|
|
9342
9409
|
continue;
|
|
9343
9410
|
}
|
|
9344
|
-
let a = await
|
|
9411
|
+
let a = await Md(e, n, { keysDir: t });
|
|
9345
9412
|
r.push({
|
|
9346
9413
|
ok: a.ok,
|
|
9347
9414
|
error: a.error,
|
|
@@ -9351,13 +9418,13 @@ async function Rd(e, t) {
|
|
|
9351
9418
|
}
|
|
9352
9419
|
return r;
|
|
9353
9420
|
}
|
|
9354
|
-
async function
|
|
9421
|
+
async function Ud(e) {
|
|
9355
9422
|
let t = e?.intervalS ?? 120, n = Ot(e?.dbPath), r = e?.keysDir, i = e?.signal, a = e?.onPhaseChange;
|
|
9356
9423
|
a?.("starting");
|
|
9357
9424
|
let o = Nt(n), s = null;
|
|
9358
9425
|
try {
|
|
9359
9426
|
let [t] = Qn(o, { keysDir: r });
|
|
9360
|
-
|
|
9427
|
+
fd() && e?.port && (s = pd(t, e.port));
|
|
9361
9428
|
} finally {
|
|
9362
9429
|
o.close();
|
|
9363
9430
|
}
|
|
@@ -9367,7 +9434,7 @@ async function zd(e) {
|
|
|
9367
9434
|
}
|
|
9368
9435
|
return new Promise((e) => {
|
|
9369
9436
|
let o = !1, c = !1, l = () => {
|
|
9370
|
-
o || (o = !0,
|
|
9437
|
+
o || (o = !0, Wd(n, r).finally(() => {
|
|
9371
9438
|
o = !1, c || (c = !0, a?.("running"));
|
|
9372
9439
|
}));
|
|
9373
9440
|
}, u = setInterval(l, t * 1e3);
|
|
@@ -9384,19 +9451,81 @@ async function zd(e) {
|
|
|
9384
9451
|
}
|
|
9385
9452
|
});
|
|
9386
9453
|
}
|
|
9387
|
-
async function
|
|
9454
|
+
async function Wd(e, t) {
|
|
9388
9455
|
let n = Nt(e);
|
|
9389
9456
|
try {
|
|
9390
|
-
await
|
|
9457
|
+
await Hd(n, t), Bd(n);
|
|
9391
9458
|
} catch (e) {
|
|
9392
|
-
|
|
9459
|
+
Vd(n, e instanceof Error ? e.message : String(e), e instanceof Error ? e.stack ?? "" : "");
|
|
9393
9460
|
} finally {
|
|
9394
9461
|
n.close();
|
|
9395
9462
|
}
|
|
9396
9463
|
}
|
|
9397
9464
|
//#endregion
|
|
9398
9465
|
//#region src/sync-retention-runner.ts
|
|
9399
|
-
|
|
9466
|
+
async function Gd(e, t) {
|
|
9467
|
+
if (!t.syncRetentionEnabled) return;
|
|
9468
|
+
e.exec("\n CREATE TABLE IF NOT EXISTS sync_retention_state (\n id INTEGER PRIMARY KEY,\n last_run_at TEXT,\n last_duration_ms INTEGER,\n last_deleted_ops INTEGER NOT NULL DEFAULT 0,\n last_estimated_bytes_before INTEGER,\n last_estimated_bytes_after INTEGER,\n retained_floor_cursor TEXT,\n last_error TEXT,\n last_error_at TEXT\n )\n ");
|
|
9469
|
+
let n = N(e, { schema: H }), r = (/* @__PURE__ */ new Date()).toISOString();
|
|
9470
|
+
try {
|
|
9471
|
+
let i = e.prepare("SELECT COALESCE(SUM(pgsize), 0) AS total_bytes\n FROM dbstat\n WHERE name = 'replication_ops'\n OR name LIKE 'idx_replication_ops_%'\n OR name LIKE 'sqlite_autoindex_replication_ops_%'").get(), a = yu(e, {
|
|
9472
|
+
maxAgeDays: t.syncRetentionMaxAgeDays,
|
|
9473
|
+
maxSizeBytes: t.syncRetentionMaxSizeMb * 1024 * 1024,
|
|
9474
|
+
maxDeleteOps: t.syncRetentionMaxOpsPerPass,
|
|
9475
|
+
maxRuntimeMs: t.syncRetentionMaxRuntimeMs,
|
|
9476
|
+
maxPasses: t.maxCatchUpPasses,
|
|
9477
|
+
signal: t.signal
|
|
9478
|
+
});
|
|
9479
|
+
n.insert(ln).values({
|
|
9480
|
+
id: 1,
|
|
9481
|
+
last_run_at: r,
|
|
9482
|
+
last_duration_ms: Date.now() - Date.parse(r),
|
|
9483
|
+
last_deleted_ops: a.totalDeleted,
|
|
9484
|
+
last_estimated_bytes_before: i?.total_bytes ?? null,
|
|
9485
|
+
last_estimated_bytes_after: a.afterBytes,
|
|
9486
|
+
retained_floor_cursor: a.lastFloor,
|
|
9487
|
+
last_error: null,
|
|
9488
|
+
last_error_at: null
|
|
9489
|
+
}).onConflictDoUpdate({
|
|
9490
|
+
target: ln.id,
|
|
9491
|
+
set: {
|
|
9492
|
+
last_run_at: r,
|
|
9493
|
+
last_duration_ms: Date.now() - Date.parse(r),
|
|
9494
|
+
last_deleted_ops: a.totalDeleted,
|
|
9495
|
+
last_estimated_bytes_before: i?.total_bytes ?? null,
|
|
9496
|
+
last_estimated_bytes_after: a.afterBytes,
|
|
9497
|
+
retained_floor_cursor: a.lastFloor,
|
|
9498
|
+
last_error: null,
|
|
9499
|
+
last_error_at: null
|
|
9500
|
+
}
|
|
9501
|
+
}).run();
|
|
9502
|
+
} catch (e) {
|
|
9503
|
+
n.insert(ln).values({
|
|
9504
|
+
id: 1,
|
|
9505
|
+
last_run_at: r,
|
|
9506
|
+
last_duration_ms: Date.now() - Date.parse(r),
|
|
9507
|
+
last_deleted_ops: 0,
|
|
9508
|
+
last_estimated_bytes_before: null,
|
|
9509
|
+
last_estimated_bytes_after: null,
|
|
9510
|
+
retained_floor_cursor: null,
|
|
9511
|
+
last_error: e instanceof Error ? e.message : String(e),
|
|
9512
|
+
last_error_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
9513
|
+
}).onConflictDoUpdate({
|
|
9514
|
+
target: ln.id,
|
|
9515
|
+
set: {
|
|
9516
|
+
last_run_at: r,
|
|
9517
|
+
last_duration_ms: Date.now() - Date.parse(r),
|
|
9518
|
+
last_deleted_ops: 0,
|
|
9519
|
+
last_estimated_bytes_before: null,
|
|
9520
|
+
last_estimated_bytes_after: null,
|
|
9521
|
+
retained_floor_cursor: null,
|
|
9522
|
+
last_error: e instanceof Error ? e.message : String(e),
|
|
9523
|
+
last_error_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
9524
|
+
}
|
|
9525
|
+
}).run();
|
|
9526
|
+
}
|
|
9527
|
+
}
|
|
9528
|
+
var Kd = class {
|
|
9400
9529
|
dbPath;
|
|
9401
9530
|
signal;
|
|
9402
9531
|
active = !1;
|
|
@@ -9422,81 +9551,28 @@ var Vd = class {
|
|
|
9422
9551
|
}
|
|
9423
9552
|
async runOnce() {
|
|
9424
9553
|
if (!this.active || this.signal?.aborted) return;
|
|
9425
|
-
let e = Gi(tt());
|
|
9426
|
-
if (!e.syncRetentionEnabled) return;
|
|
9427
|
-
let t = Nt(this.dbPath);
|
|
9428
|
-
t.exec("\n CREATE TABLE IF NOT EXISTS sync_retention_state (\n id INTEGER PRIMARY KEY,\n last_run_at TEXT,\n last_duration_ms INTEGER,\n last_deleted_ops INTEGER NOT NULL DEFAULT 0,\n last_estimated_bytes_before INTEGER,\n last_estimated_bytes_after INTEGER,\n retained_floor_cursor TEXT,\n last_error TEXT,\n last_error_at TEXT\n )\n ");
|
|
9429
|
-
let n = N(t, { schema: H }), r = (/* @__PURE__ */ new Date()).toISOString();
|
|
9554
|
+
let e = Gi(tt()), t = Nt(this.dbPath);
|
|
9430
9555
|
try {
|
|
9431
|
-
|
|
9432
|
-
|
|
9433
|
-
|
|
9434
|
-
maxDeleteOps: e.syncRetentionMaxOpsPerPass,
|
|
9435
|
-
maxRuntimeMs: e.syncRetentionMaxRuntimeMs
|
|
9556
|
+
await Gd(t, {
|
|
9557
|
+
...e,
|
|
9558
|
+
signal: this.signal
|
|
9436
9559
|
});
|
|
9437
|
-
n.insert(ln).values({
|
|
9438
|
-
id: 1,
|
|
9439
|
-
last_run_at: r,
|
|
9440
|
-
last_duration_ms: Date.now() - Date.parse(r),
|
|
9441
|
-
last_deleted_ops: i.deleted,
|
|
9442
|
-
last_estimated_bytes_before: i.estimated_bytes_before ?? null,
|
|
9443
|
-
last_estimated_bytes_after: i.estimated_bytes_after ?? null,
|
|
9444
|
-
retained_floor_cursor: i.retained_floor_cursor,
|
|
9445
|
-
last_error: null,
|
|
9446
|
-
last_error_at: null
|
|
9447
|
-
}).onConflictDoUpdate({
|
|
9448
|
-
target: ln.id,
|
|
9449
|
-
set: {
|
|
9450
|
-
last_run_at: r,
|
|
9451
|
-
last_duration_ms: Date.now() - Date.parse(r),
|
|
9452
|
-
last_deleted_ops: i.deleted,
|
|
9453
|
-
last_estimated_bytes_before: i.estimated_bytes_before ?? null,
|
|
9454
|
-
last_estimated_bytes_after: i.estimated_bytes_after ?? null,
|
|
9455
|
-
retained_floor_cursor: i.retained_floor_cursor,
|
|
9456
|
-
last_error: null,
|
|
9457
|
-
last_error_at: null
|
|
9458
|
-
}
|
|
9459
|
-
}).run();
|
|
9460
|
-
} catch (e) {
|
|
9461
|
-
n.insert(ln).values({
|
|
9462
|
-
id: 1,
|
|
9463
|
-
last_run_at: r,
|
|
9464
|
-
last_duration_ms: Date.now() - Date.parse(r),
|
|
9465
|
-
last_deleted_ops: 0,
|
|
9466
|
-
last_estimated_bytes_before: null,
|
|
9467
|
-
last_estimated_bytes_after: null,
|
|
9468
|
-
retained_floor_cursor: null,
|
|
9469
|
-
last_error: e instanceof Error ? e.message : String(e),
|
|
9470
|
-
last_error_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
9471
|
-
}).onConflictDoUpdate({
|
|
9472
|
-
target: ln.id,
|
|
9473
|
-
set: {
|
|
9474
|
-
last_run_at: r,
|
|
9475
|
-
last_duration_ms: Date.now() - Date.parse(r),
|
|
9476
|
-
last_deleted_ops: 0,
|
|
9477
|
-
last_estimated_bytes_before: null,
|
|
9478
|
-
last_estimated_bytes_after: null,
|
|
9479
|
-
retained_floor_cursor: null,
|
|
9480
|
-
last_error: e instanceof Error ? e.message : String(e),
|
|
9481
|
-
last_error_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
9482
|
-
}
|
|
9483
|
-
}).run();
|
|
9484
9560
|
} finally {
|
|
9485
9561
|
t.close();
|
|
9486
9562
|
}
|
|
9487
9563
|
}
|
|
9488
|
-
}, Hd = "CREATE TABLE IF NOT EXISTS `sessions` (\n `id` integer PRIMARY KEY NOT NULL,\n `started_at` text NOT NULL,\n `ended_at` text,\n `cwd` text,\n `project` text,\n `git_remote` text,\n `git_branch` text,\n `user` text,\n `tool_version` text,\n `metadata_json` text,\n `import_key` text\n);\n\nCREATE TABLE IF NOT EXISTS `artifacts` (\n `id` integer PRIMARY KEY NOT NULL,\n `session_id` integer NOT NULL,\n `kind` text NOT NULL,\n `path` text,\n `content_text` text,\n `content_hash` text,\n `created_at` text NOT NULL,\n `metadata_json` text,\n FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE cascade\n);\n\nCREATE INDEX IF NOT EXISTS `idx_artifacts_session_kind` ON `artifacts` (`session_id`,`kind`);\nCREATE TABLE IF NOT EXISTS `memory_items` (\n `id` integer PRIMARY KEY NOT NULL,\n `session_id` integer NOT NULL,\n `kind` text NOT NULL,\n `title` text NOT NULL,\n `subtitle` text,\n `body_text` text NOT NULL,\n `confidence` real DEFAULT 0.5,\n `tags_text` text DEFAULT '',\n `active` integer DEFAULT 1,\n `created_at` text NOT NULL,\n `updated_at` text NOT NULL,\n `metadata_json` text,\n `actor_id` text,\n `actor_display_name` text,\n `visibility` text,\n `workspace_id` text,\n `workspace_kind` text,\n `origin_device_id` text,\n `origin_source` text,\n `trust_state` text,\n `facts` text,\n `narrative` text,\n `concepts` text,\n `files_read` text,\n `files_modified` text,\n `user_prompt_id` integer,\n `prompt_number` integer,\n `deleted_at` text,\n `rev` integer DEFAULT 0,\n `import_key` text,\n FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE cascade\n);\n\nCREATE INDEX IF NOT EXISTS `idx_memory_items_active_created` ON `memory_items` (`active`,`created_at`);\nCREATE INDEX IF NOT EXISTS `idx_memory_items_session` ON `memory_items` (`session_id`);\nCREATE TABLE IF NOT EXISTS `usage_events` (\n `id` integer PRIMARY KEY NOT NULL,\n `session_id` integer,\n `event` text NOT NULL,\n `tokens_read` integer DEFAULT 0,\n `tokens_written` integer DEFAULT 0,\n `tokens_saved` integer DEFAULT 0,\n `created_at` text NOT NULL,\n `metadata_json` text,\n FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE set null\n);\n\nCREATE INDEX IF NOT EXISTS `idx_usage_events_event_created` ON `usage_events` (`event`,`created_at`);\nCREATE INDEX IF NOT EXISTS `idx_usage_events_session` ON `usage_events` (`session_id`);\nCREATE TABLE IF NOT EXISTS `raw_events` (\n `id` integer PRIMARY KEY NOT NULL,\n `source` text DEFAULT 'opencode' NOT NULL,\n `stream_id` text DEFAULT '' NOT NULL,\n `opencode_session_id` text NOT NULL,\n `event_id` text,\n `event_seq` integer NOT NULL,\n `event_type` text NOT NULL,\n `ts_wall_ms` integer,\n `ts_mono_ms` real,\n `payload_json` text NOT NULL,\n `created_at` text NOT NULL\n);\n\nCREATE UNIQUE INDEX IF NOT EXISTS `idx_raw_events_source_stream_seq` ON `raw_events` (`source`,`stream_id`,`event_seq`);\nCREATE UNIQUE INDEX IF NOT EXISTS `idx_raw_events_source_stream_event_id` ON `raw_events` (`source`,`stream_id`,`event_id`);\nCREATE INDEX IF NOT EXISTS `idx_raw_events_session_seq` ON `raw_events` (`opencode_session_id`,`event_seq`);\nCREATE INDEX IF NOT EXISTS `idx_raw_events_created` ON `raw_events` (`created_at`);\nCREATE TABLE IF NOT EXISTS `raw_event_sessions` (\n `source` text DEFAULT 'opencode' NOT NULL,\n `stream_id` text DEFAULT '' NOT NULL,\n `opencode_session_id` text NOT NULL,\n `cwd` text,\n `project` text,\n `started_at` text,\n `last_seen_ts_wall_ms` integer,\n `last_received_event_seq` integer DEFAULT -1 NOT NULL,\n `last_flushed_event_seq` integer DEFAULT -1 NOT NULL,\n `updated_at` text NOT NULL,\n PRIMARY KEY(`source`, `stream_id`)\n);\n\nCREATE TABLE IF NOT EXISTS `opencode_sessions` (\n `source` text DEFAULT 'opencode' NOT NULL,\n `stream_id` text DEFAULT '' NOT NULL,\n `opencode_session_id` text NOT NULL,\n `session_id` integer,\n `created_at` text NOT NULL,\n PRIMARY KEY(`source`, `stream_id`),\n FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE cascade\n);\n\nCREATE INDEX IF NOT EXISTS `idx_opencode_sessions_session` ON `opencode_sessions` (`session_id`);\nCREATE TABLE IF NOT EXISTS `raw_event_flush_batches` (\n `id` integer PRIMARY KEY NOT NULL,\n `source` text DEFAULT 'opencode' NOT NULL,\n `stream_id` text DEFAULT '' NOT NULL,\n `opencode_session_id` text NOT NULL,\n `start_event_seq` integer NOT NULL,\n `end_event_seq` integer NOT NULL,\n `extractor_version` text NOT NULL,\n `status` text NOT NULL,\n `error_message` text,\n `error_type` text,\n `observer_provider` text,\n `observer_model` text,\n `observer_runtime` text,\n `attempt_count` integer DEFAULT 0 NOT NULL,\n `created_at` text NOT NULL,\n `updated_at` text NOT NULL\n);\n\nCREATE UNIQUE INDEX IF NOT EXISTS `idx_flush_batches_source_stream_seq_ver` ON `raw_event_flush_batches` (`source`,`stream_id`,`start_event_seq`,`end_event_seq`,`extractor_version`);\nCREATE INDEX IF NOT EXISTS `idx_flush_batches_session_created` ON `raw_event_flush_batches` (`opencode_session_id`,`created_at`);\nCREATE INDEX IF NOT EXISTS `idx_flush_batches_status_updated` ON `raw_event_flush_batches` (`status`,`updated_at`);\nCREATE TABLE IF NOT EXISTS `user_prompts` (\n `id` integer PRIMARY KEY NOT NULL,\n `session_id` integer,\n `project` text,\n `prompt_text` text NOT NULL,\n `prompt_number` integer,\n `created_at` text NOT NULL,\n `created_at_epoch` integer NOT NULL,\n `metadata_json` text,\n `import_key` text,\n FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE cascade\n);\n\nCREATE INDEX IF NOT EXISTS `idx_user_prompts_session` ON `user_prompts` (`session_id`);\nCREATE INDEX IF NOT EXISTS `idx_user_prompts_project` ON `user_prompts` (`project`);\nCREATE INDEX IF NOT EXISTS `idx_user_prompts_epoch` ON `user_prompts` (`created_at_epoch`);\nCREATE TABLE IF NOT EXISTS `session_summaries` (\n `id` integer PRIMARY KEY NOT NULL,\n `session_id` integer,\n `project` text,\n `request` text,\n `investigated` text,\n `learned` text,\n `completed` text,\n `next_steps` text,\n `notes` text,\n `files_read` text,\n `files_edited` text,\n `prompt_number` integer,\n `created_at` text NOT NULL,\n `created_at_epoch` integer NOT NULL,\n `metadata_json` text,\n `import_key` text,\n FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE cascade\n);\n\nCREATE INDEX IF NOT EXISTS `idx_session_summaries_session` ON `session_summaries` (`session_id`);\nCREATE INDEX IF NOT EXISTS `idx_session_summaries_project` ON `session_summaries` (`project`);\nCREATE INDEX IF NOT EXISTS `idx_session_summaries_epoch` ON `session_summaries` (`created_at_epoch`);\nCREATE TABLE IF NOT EXISTS `replication_ops` (\n `op_id` text PRIMARY KEY NOT NULL,\n `entity_type` text NOT NULL,\n `entity_id` text NOT NULL,\n `op_type` text NOT NULL,\n `payload_json` text,\n `clock_rev` integer NOT NULL,\n `clock_updated_at` text NOT NULL,\n `clock_device_id` text NOT NULL,\n `device_id` text NOT NULL,\n `created_at` text NOT NULL\n);\n\nCREATE INDEX IF NOT EXISTS `idx_replication_ops_created` ON `replication_ops` (`created_at`,`op_id`);\nCREATE INDEX IF NOT EXISTS `idx_replication_ops_entity` ON `replication_ops` (`entity_type`,`entity_id`);\nCREATE TABLE IF NOT EXISTS `replication_cursors` (\n `peer_device_id` text PRIMARY KEY NOT NULL,\n `last_applied_cursor` text,\n `last_acked_cursor` text,\n `updated_at` text NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS `sync_peers` (\n `peer_device_id` text PRIMARY KEY NOT NULL,\n `name` text,\n `pinned_fingerprint` text,\n `public_key` text,\n `addresses_json` text,\n `claimed_local_actor` integer DEFAULT 0 NOT NULL,\n `actor_id` text,\n `projects_include_json` text,\n `projects_exclude_json` text,\n `created_at` text NOT NULL,\n `last_seen_at` text,\n `last_sync_at` text,\n `last_error` text\n);\n\nCREATE TABLE IF NOT EXISTS `sync_nonces` (\n `nonce` text PRIMARY KEY NOT NULL,\n `device_id` text NOT NULL,\n `created_at` text NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS `sync_device` (\n `device_id` text PRIMARY KEY NOT NULL,\n `public_key` text NOT NULL,\n `fingerprint` text NOT NULL,\n `created_at` text NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS `sync_attempts` (\n `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,\n `peer_device_id` text NOT NULL,\n `started_at` text NOT NULL,\n `finished_at` text,\n `ok` integer NOT NULL,\n `ops_in` integer NOT NULL,\n `ops_out` integer NOT NULL,\n `error` text\n);\n\nCREATE INDEX IF NOT EXISTS `idx_sync_attempts_peer_started` ON `sync_attempts` (`peer_device_id`,`started_at`);\nCREATE TABLE IF NOT EXISTS `sync_daemon_state` (\n `id` integer PRIMARY KEY NOT NULL,\n `last_error` text,\n `last_traceback` text,\n `last_error_at` text,\n `last_ok_at` text\n);\n\nCREATE TABLE IF NOT EXISTS `sync_reset_state` (\n `id` integer PRIMARY KEY NOT NULL,\n `generation` integer NOT NULL,\n `snapshot_id` text NOT NULL,\n `baseline_cursor` text,\n `retained_floor_cursor` text,\n `updated_at` text NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS `raw_event_ingest_samples` (\n `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,\n `created_at` text NOT NULL,\n `inserted_events` integer DEFAULT 0 NOT NULL,\n `skipped_invalid` integer DEFAULT 0 NOT NULL,\n `skipped_duplicate` integer DEFAULT 0 NOT NULL,\n `skipped_conflict` integer DEFAULT 0 NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS `raw_event_ingest_stats` (\n `id` integer PRIMARY KEY NOT NULL,\n `inserted_events` integer DEFAULT 0 NOT NULL,\n `skipped_events` integer DEFAULT 0 NOT NULL,\n `skipped_invalid` integer DEFAULT 0 NOT NULL,\n `skipped_duplicate` integer DEFAULT 0 NOT NULL,\n `skipped_conflict` integer DEFAULT 0 NOT NULL,\n `updated_at` text NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS `actors` (\n `actor_id` text PRIMARY KEY NOT NULL,\n `display_name` text NOT NULL,\n `is_local` integer DEFAULT 0 NOT NULL,\n `status` text DEFAULT 'active' NOT NULL,\n `merged_into_actor_id` text,\n `created_at` text NOT NULL,\n `updated_at` text NOT NULL\n);\n\nCREATE INDEX IF NOT EXISTS `idx_actors_is_local` ON `actors` (`is_local`);\nCREATE INDEX IF NOT EXISTS `idx_actors_status` ON `actors` (`status`);", Ud = "\nCREATE INDEX IF NOT EXISTS idx_sync_peers_actor_id ON sync_peers(actor_id);\n\nCREATE TABLE IF NOT EXISTS sync_retention_state (\n id INTEGER PRIMARY KEY,\n last_run_at TEXT,\n last_duration_ms INTEGER,\n last_deleted_ops INTEGER NOT NULL DEFAULT 0,\n last_estimated_bytes_before INTEGER,\n last_estimated_bytes_after INTEGER,\n retained_floor_cursor TEXT,\n last_error TEXT,\n last_error_at TEXT\n);\n\nCREATE VIRTUAL TABLE IF NOT EXISTS memory_fts USING fts5(\n title, body_text, tags_text,\n content='memory_items',\n content_rowid='id'\n);\n\nCREATE TRIGGER IF NOT EXISTS memory_items_ai AFTER INSERT ON memory_items BEGIN\n INSERT INTO memory_fts(rowid, title, body_text, tags_text)\n VALUES (new.id, new.title, new.body_text, new.tags_text);\nEND;\n\nDROP TRIGGER IF EXISTS memory_items_au;\nCREATE TRIGGER memory_items_au AFTER UPDATE ON memory_items BEGIN\n INSERT INTO memory_fts(memory_fts, rowid, title, body_text, tags_text)\n VALUES('delete', old.id, old.title, old.body_text, old.tags_text);\n INSERT INTO memory_fts(rowid, title, body_text, tags_text)\n VALUES (new.id, new.title, new.body_text, new.tags_text);\nEND;\n\nDROP TRIGGER IF EXISTS memory_items_ad;\nCREATE TRIGGER memory_items_ad AFTER DELETE ON memory_items BEGIN\n INSERT INTO memory_fts(memory_fts, rowid, title, body_text, tags_text)\n VALUES('delete', old.id, old.title, old.body_text, old.tags_text);\nEND;\n";
|
|
9489
|
-
function
|
|
9490
|
-
e.exec(
|
|
9564
|
+
}, qd = "CREATE TABLE IF NOT EXISTS `sessions` (\n `id` integer PRIMARY KEY NOT NULL,\n `started_at` text NOT NULL,\n `ended_at` text,\n `cwd` text,\n `project` text,\n `git_remote` text,\n `git_branch` text,\n `user` text,\n `tool_version` text,\n `metadata_json` text,\n `import_key` text\n);\n\nCREATE TABLE IF NOT EXISTS `artifacts` (\n `id` integer PRIMARY KEY NOT NULL,\n `session_id` integer NOT NULL,\n `kind` text NOT NULL,\n `path` text,\n `content_text` text,\n `content_hash` text,\n `created_at` text NOT NULL,\n `metadata_json` text,\n FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE cascade\n);\n\nCREATE INDEX IF NOT EXISTS `idx_artifacts_session_kind` ON `artifacts` (`session_id`,`kind`);\nCREATE TABLE IF NOT EXISTS `memory_items` (\n `id` integer PRIMARY KEY NOT NULL,\n `session_id` integer NOT NULL,\n `kind` text NOT NULL,\n `title` text NOT NULL,\n `subtitle` text,\n `body_text` text NOT NULL,\n `confidence` real DEFAULT 0.5,\n `tags_text` text DEFAULT '',\n `active` integer DEFAULT 1,\n `created_at` text NOT NULL,\n `updated_at` text NOT NULL,\n `metadata_json` text,\n `actor_id` text,\n `actor_display_name` text,\n `visibility` text,\n `workspace_id` text,\n `workspace_kind` text,\n `origin_device_id` text,\n `origin_source` text,\n `trust_state` text,\n `facts` text,\n `narrative` text,\n `concepts` text,\n `files_read` text,\n `files_modified` text,\n `user_prompt_id` integer,\n `prompt_number` integer,\n `deleted_at` text,\n `rev` integer DEFAULT 0,\n `import_key` text,\n FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE cascade\n);\n\nCREATE INDEX IF NOT EXISTS `idx_memory_items_active_created` ON `memory_items` (`active`,`created_at`);\nCREATE INDEX IF NOT EXISTS `idx_memory_items_session` ON `memory_items` (`session_id`);\nCREATE TABLE IF NOT EXISTS `usage_events` (\n `id` integer PRIMARY KEY NOT NULL,\n `session_id` integer,\n `event` text NOT NULL,\n `tokens_read` integer DEFAULT 0,\n `tokens_written` integer DEFAULT 0,\n `tokens_saved` integer DEFAULT 0,\n `created_at` text NOT NULL,\n `metadata_json` text,\n FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE set null\n);\n\nCREATE INDEX IF NOT EXISTS `idx_usage_events_event_created` ON `usage_events` (`event`,`created_at`);\nCREATE INDEX IF NOT EXISTS `idx_usage_events_session` ON `usage_events` (`session_id`);\nCREATE TABLE IF NOT EXISTS `raw_events` (\n `id` integer PRIMARY KEY NOT NULL,\n `source` text DEFAULT 'opencode' NOT NULL,\n `stream_id` text DEFAULT '' NOT NULL,\n `opencode_session_id` text NOT NULL,\n `event_id` text,\n `event_seq` integer NOT NULL,\n `event_type` text NOT NULL,\n `ts_wall_ms` integer,\n `ts_mono_ms` real,\n `payload_json` text NOT NULL,\n `created_at` text NOT NULL\n);\n\nCREATE UNIQUE INDEX IF NOT EXISTS `idx_raw_events_source_stream_seq` ON `raw_events` (`source`,`stream_id`,`event_seq`);\nCREATE UNIQUE INDEX IF NOT EXISTS `idx_raw_events_source_stream_event_id` ON `raw_events` (`source`,`stream_id`,`event_id`);\nCREATE INDEX IF NOT EXISTS `idx_raw_events_session_seq` ON `raw_events` (`opencode_session_id`,`event_seq`);\nCREATE INDEX IF NOT EXISTS `idx_raw_events_created` ON `raw_events` (`created_at`);\nCREATE TABLE IF NOT EXISTS `raw_event_sessions` (\n `source` text DEFAULT 'opencode' NOT NULL,\n `stream_id` text DEFAULT '' NOT NULL,\n `opencode_session_id` text NOT NULL,\n `cwd` text,\n `project` text,\n `started_at` text,\n `last_seen_ts_wall_ms` integer,\n `last_received_event_seq` integer DEFAULT -1 NOT NULL,\n `last_flushed_event_seq` integer DEFAULT -1 NOT NULL,\n `updated_at` text NOT NULL,\n PRIMARY KEY(`source`, `stream_id`)\n);\n\nCREATE TABLE IF NOT EXISTS `opencode_sessions` (\n `source` text DEFAULT 'opencode' NOT NULL,\n `stream_id` text DEFAULT '' NOT NULL,\n `opencode_session_id` text NOT NULL,\n `session_id` integer,\n `created_at` text NOT NULL,\n PRIMARY KEY(`source`, `stream_id`),\n FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE cascade\n);\n\nCREATE INDEX IF NOT EXISTS `idx_opencode_sessions_session` ON `opencode_sessions` (`session_id`);\nCREATE TABLE IF NOT EXISTS `raw_event_flush_batches` (\n `id` integer PRIMARY KEY NOT NULL,\n `source` text DEFAULT 'opencode' NOT NULL,\n `stream_id` text DEFAULT '' NOT NULL,\n `opencode_session_id` text NOT NULL,\n `start_event_seq` integer NOT NULL,\n `end_event_seq` integer NOT NULL,\n `extractor_version` text NOT NULL,\n `status` text NOT NULL,\n `error_message` text,\n `error_type` text,\n `observer_provider` text,\n `observer_model` text,\n `observer_runtime` text,\n `attempt_count` integer DEFAULT 0 NOT NULL,\n `created_at` text NOT NULL,\n `updated_at` text NOT NULL\n);\n\nCREATE UNIQUE INDEX IF NOT EXISTS `idx_flush_batches_source_stream_seq_ver` ON `raw_event_flush_batches` (`source`,`stream_id`,`start_event_seq`,`end_event_seq`,`extractor_version`);\nCREATE INDEX IF NOT EXISTS `idx_flush_batches_session_created` ON `raw_event_flush_batches` (`opencode_session_id`,`created_at`);\nCREATE INDEX IF NOT EXISTS `idx_flush_batches_status_updated` ON `raw_event_flush_batches` (`status`,`updated_at`);\nCREATE TABLE IF NOT EXISTS `user_prompts` (\n `id` integer PRIMARY KEY NOT NULL,\n `session_id` integer,\n `project` text,\n `prompt_text` text NOT NULL,\n `prompt_number` integer,\n `created_at` text NOT NULL,\n `created_at_epoch` integer NOT NULL,\n `metadata_json` text,\n `import_key` text,\n FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE cascade\n);\n\nCREATE INDEX IF NOT EXISTS `idx_user_prompts_session` ON `user_prompts` (`session_id`);\nCREATE INDEX IF NOT EXISTS `idx_user_prompts_project` ON `user_prompts` (`project`);\nCREATE INDEX IF NOT EXISTS `idx_user_prompts_epoch` ON `user_prompts` (`created_at_epoch`);\nCREATE TABLE IF NOT EXISTS `session_summaries` (\n `id` integer PRIMARY KEY NOT NULL,\n `session_id` integer,\n `project` text,\n `request` text,\n `investigated` text,\n `learned` text,\n `completed` text,\n `next_steps` text,\n `notes` text,\n `files_read` text,\n `files_edited` text,\n `prompt_number` integer,\n `created_at` text NOT NULL,\n `created_at_epoch` integer NOT NULL,\n `metadata_json` text,\n `import_key` text,\n FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE cascade\n);\n\nCREATE INDEX IF NOT EXISTS `idx_session_summaries_session` ON `session_summaries` (`session_id`);\nCREATE INDEX IF NOT EXISTS `idx_session_summaries_project` ON `session_summaries` (`project`);\nCREATE INDEX IF NOT EXISTS `idx_session_summaries_epoch` ON `session_summaries` (`created_at_epoch`);\nCREATE TABLE IF NOT EXISTS `replication_ops` (\n `op_id` text PRIMARY KEY NOT NULL,\n `entity_type` text NOT NULL,\n `entity_id` text NOT NULL,\n `op_type` text NOT NULL,\n `payload_json` text,\n `clock_rev` integer NOT NULL,\n `clock_updated_at` text NOT NULL,\n `clock_device_id` text NOT NULL,\n `device_id` text NOT NULL,\n `created_at` text NOT NULL\n);\n\nCREATE INDEX IF NOT EXISTS `idx_replication_ops_created` ON `replication_ops` (`created_at`,`op_id`);\nCREATE INDEX IF NOT EXISTS `idx_replication_ops_entity` ON `replication_ops` (`entity_type`,`entity_id`);\nCREATE TABLE IF NOT EXISTS `replication_cursors` (\n `peer_device_id` text PRIMARY KEY NOT NULL,\n `last_applied_cursor` text,\n `last_acked_cursor` text,\n `updated_at` text NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS `sync_peers` (\n `peer_device_id` text PRIMARY KEY NOT NULL,\n `name` text,\n `pinned_fingerprint` text,\n `public_key` text,\n `addresses_json` text,\n `claimed_local_actor` integer DEFAULT 0 NOT NULL,\n `actor_id` text,\n `projects_include_json` text,\n `projects_exclude_json` text,\n `created_at` text NOT NULL,\n `last_seen_at` text,\n `last_sync_at` text,\n `last_error` text\n);\n\nCREATE TABLE IF NOT EXISTS `sync_nonces` (\n `nonce` text PRIMARY KEY NOT NULL,\n `device_id` text NOT NULL,\n `created_at` text NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS `sync_device` (\n `device_id` text PRIMARY KEY NOT NULL,\n `public_key` text NOT NULL,\n `fingerprint` text NOT NULL,\n `created_at` text NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS `sync_attempts` (\n `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,\n `peer_device_id` text NOT NULL,\n `started_at` text NOT NULL,\n `finished_at` text,\n `ok` integer NOT NULL,\n `ops_in` integer NOT NULL,\n `ops_out` integer NOT NULL,\n `error` text\n);\n\nCREATE INDEX IF NOT EXISTS `idx_sync_attempts_peer_started` ON `sync_attempts` (`peer_device_id`,`started_at`);\nCREATE TABLE IF NOT EXISTS `sync_daemon_state` (\n `id` integer PRIMARY KEY NOT NULL,\n `last_error` text,\n `last_traceback` text,\n `last_error_at` text,\n `last_ok_at` text\n);\n\nCREATE TABLE IF NOT EXISTS `sync_reset_state` (\n `id` integer PRIMARY KEY NOT NULL,\n `generation` integer NOT NULL,\n `snapshot_id` text NOT NULL,\n `baseline_cursor` text,\n `retained_floor_cursor` text,\n `updated_at` text NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS `raw_event_ingest_samples` (\n `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,\n `created_at` text NOT NULL,\n `inserted_events` integer DEFAULT 0 NOT NULL,\n `skipped_invalid` integer DEFAULT 0 NOT NULL,\n `skipped_duplicate` integer DEFAULT 0 NOT NULL,\n `skipped_conflict` integer DEFAULT 0 NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS `raw_event_ingest_stats` (\n `id` integer PRIMARY KEY NOT NULL,\n `inserted_events` integer DEFAULT 0 NOT NULL,\n `skipped_events` integer DEFAULT 0 NOT NULL,\n `skipped_invalid` integer DEFAULT 0 NOT NULL,\n `skipped_duplicate` integer DEFAULT 0 NOT NULL,\n `skipped_conflict` integer DEFAULT 0 NOT NULL,\n `updated_at` text NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS `actors` (\n `actor_id` text PRIMARY KEY NOT NULL,\n `display_name` text NOT NULL,\n `is_local` integer DEFAULT 0 NOT NULL,\n `status` text DEFAULT 'active' NOT NULL,\n `merged_into_actor_id` text,\n `created_at` text NOT NULL,\n `updated_at` text NOT NULL\n);\n\nCREATE INDEX IF NOT EXISTS `idx_actors_is_local` ON `actors` (`is_local`);\nCREATE INDEX IF NOT EXISTS `idx_actors_status` ON `actors` (`status`);", Jd = "\nCREATE INDEX IF NOT EXISTS idx_sync_peers_actor_id ON sync_peers(actor_id);\n\nCREATE TABLE IF NOT EXISTS sync_retention_state (\n id INTEGER PRIMARY KEY,\n last_run_at TEXT,\n last_duration_ms INTEGER,\n last_deleted_ops INTEGER NOT NULL DEFAULT 0,\n last_estimated_bytes_before INTEGER,\n last_estimated_bytes_after INTEGER,\n retained_floor_cursor TEXT,\n last_error TEXT,\n last_error_at TEXT\n);\n\nCREATE VIRTUAL TABLE IF NOT EXISTS memory_fts USING fts5(\n title, body_text, tags_text,\n content='memory_items',\n content_rowid='id'\n);\n\nCREATE TRIGGER IF NOT EXISTS memory_items_ai AFTER INSERT ON memory_items BEGIN\n INSERT INTO memory_fts(rowid, title, body_text, tags_text)\n VALUES (new.id, new.title, new.body_text, new.tags_text);\nEND;\n\nDROP TRIGGER IF EXISTS memory_items_au;\nCREATE TRIGGER memory_items_au AFTER UPDATE ON memory_items BEGIN\n INSERT INTO memory_fts(memory_fts, rowid, title, body_text, tags_text)\n VALUES('delete', old.id, old.title, old.body_text, old.tags_text);\n INSERT INTO memory_fts(rowid, title, body_text, tags_text)\n VALUES (new.id, new.title, new.body_text, new.tags_text);\nEND;\n\nDROP TRIGGER IF EXISTS memory_items_ad;\nCREATE TRIGGER memory_items_ad AFTER DELETE ON memory_items BEGIN\n INSERT INTO memory_fts(memory_fts, rowid, title, body_text, tags_text)\n VALUES('delete', old.id, old.title, old.body_text, old.tags_text);\nEND;\n";
|
|
9565
|
+
function Yd(e) {
|
|
9566
|
+
e.exec(qd), e.exec(Jd), e.pragma("user_version = 6");
|
|
9491
9567
|
}
|
|
9492
|
-
function
|
|
9568
|
+
function Xd(e) {
|
|
9493
9569
|
let t = (/* @__PURE__ */ new Date()).toISOString(), n = e.prepare("INSERT INTO sessions(started_at, cwd, project, user, tool_version) VALUES (?, ?, ?, ?, ?)").run(t, "/tmp/test", "test-project", "test-user", "test");
|
|
9494
9570
|
return Number(n.lastInsertRowid);
|
|
9495
9571
|
}
|
|
9496
9572
|
//#endregion
|
|
9497
9573
|
//#region src/index.ts
|
|
9498
|
-
var
|
|
9574
|
+
var Zd = "0.21.2";
|
|
9499
9575
|
//#endregion
|
|
9500
|
-
export { ge as Api, $e as CODEMEM_CONFIG_ENV_OVERRIDES, R as CoordinatorStore, Be as DEFAULT_COORDINATOR_DB_PATH, Dt as DEFAULT_DB_PATH,
|
|
9576
|
+
export { ge as Api, $e as CODEMEM_CONFIG_ENV_OVERRIDES, R as CoordinatorStore, Be as DEFAULT_COORDINATOR_DB_PATH, Dt as DEFAULT_DB_PATH, ed as DEFAULT_SERVICE_TYPE, gi as DEFAULT_TIME_WINDOW_S, qa as LOW_SIGNAL_TOOLS, ve as MAPPABLE_CLAUDE_HOOK_EVENTS, Tt as MIN_COMPATIBLE_SCHEMA, $u as MemoryStore, As as ObserverAuthAdapter, Ks as ObserverAuthError, uc as ObserverClient, Kl as RawEventSweeper, wt as SCHEMA_VERSION, hi as SIGNATURE_VERSION, Kd as SyncRetentionRunner, yo as TRIVIAL_REQUESTS, Zd as VERSION, ta as _resetEmbeddingClient, Fi as addressDedupeKey, pd as advertiseMdns, zu as applyReplicationOps, zt as assertSchemaReady, Iu as backfillReplicationOps, Tn as backfillTagsText, Yo as backfillVectors, ao as budgetToolEvents, xi as buildAuthHeaders, An as buildBaseUrl, _i as buildCanonicalRequest, Ts as buildCodexHeaders, Ma as buildFilterClauses, Na as buildFilterClausesWithContext, fa as buildImportKey, Fe as buildIngestPayloadFromHook, Pl as buildMemoryPack, Fl as buildMemoryPackAsync, vo as buildObserverPrompt, Pe as buildRawEventEnvelopeFromHook, Bl as buildSessionContext, wo as buildTranscript, bu as bulkPruneReplicationOpsByAgeCutoff, iu as chunkOpsBySize, $i as chunkText, os as cleanOrphanSessions, Ci as cleanupNonces, lu as clockTuple, Nt as connect, Je as connectCoordinator, Ld as consecutiveConnectivityFailures, nr as coordinatorCreateInviteAction, Ki as coordinatorEnabled, rr as coordinatorImportInviteAction, ir as coordinatorListJoinRequestsAction, ar as coordinatorReviewJoinRequestAction, Xi as coordinatorStatusSnapshot, Mi as createCoordinatorApp, yd as cursorAdvances, kn as deactivateLowSignalMemories, On as deactivateLowSignalObservations, Le as decodeInvitePayload, Yc as dedupeOrderedIds, Co as deriveRequest, Uo as deriveTags, md as discoverPeersViaMdns, ra as embedTexts, Ie as encodeInvitePayload, Qn as ensureDeviceIdentity, il as estimateTokens, Qa as eventToToolEvent, Cc as expandQuery, $c as explain, va as exportMemories, eo as extractAdapterEvent, To as extractAssistantMessages, Eo as extractAssistantUsage, ze as extractInvitePayload, bs as extractOAuthAccess, Cs as extractOAuthAccountId, ws as extractOAuthExpires, Do as extractPrompts, xs as extractProviderApiKey, cu as extractReplicationOps, $a as extractToolEvents, Ho as fileTags, Nu as filterReplicationOpsForSync, Mu as filterReplicationOpsForSyncWithStatus, In as fingerprintPublicKey, So as firstSentence, Hl as flushRawEvents, z as fromJson, Ut as fromJsonStrict, Jn as generateKeypair, et as getCodememConfigPath, rt as getCodememEnvOverrides, na as getEmbeddingClient, at as getOpenCodeProviderConfig, xt as getProviderApiKey, yt as getProviderBaseUrl, bt as getProviderHeaders, vt as getProviderOptions, _n as getRawEventStatus, vn as getReliabilityMetrics, au as getReplicationCursor, Rt as getSchemaVersion, $l as getSyncResetState, zo as hasMeaningfulObservation, _u as hasUnsyncedSharedMemoryChanges, Qi as hashText, Ea as importMemories, is as ingest, ss as ingestMain, hn as initDatabase, Yd as initTestSchema, Xd as insertTestSession, Re as inviteLink, Id as isConnectivityError, Lt as isEmbeddingDisabled, Ja as isInternalMemoryTool, Kt as isLowSignalObservation, uu as isNewerClock, Ba as isSensitiveFieldName, xo as isTrivialRequest, Tc as kindBonus, lt as listConfiguredOpenCodeProviders, Zi as listCoordinatorJoinRequests, ot as listCustomProviders, ut as listObserverProviderOptions, gu as loadMemorySnapshotPageForPeer, Gs as loadObserverConfig, Ze as loadOpenCodeConfig, _s as loadOpenCodeOAuthCache, nd as loadPeerAddresses, Wn as loadPrivateKey, Vn as loadPrivateKeyKeychain, Un as loadPublicKey, hu as loadReplicationOpsForPeer, mu as loadReplicationOpsSince, It as loadSqliteVec, Yi as lookupCoordinatorPeers, Me as mapClaudeHookPayload, hd as mdnsAddressesForPeer, fd as mdnsEnabled, Ii as mergeAddresses, pa as mergeSummaryMetadata, Mt as migrateLegacyDbPath, Fu as migrateLegacyImportKeys, Oo as normalizeAdapterEvents, Pi as normalizeAddress, Gt as normalizeObservation, Ce as normalizeProjectLabel, bo as normalizeRequestText, Vo as normalizeTag, Ya as normalizeToolName, Ro as parseObserverResponse, ds as parsePositiveMemoryId, us as parseStrictInteger, Rd as peerBackoffSeconds, xu as planReplicationOpsAgePrune, Ss as probeAvailableCredentials, to as projectAdapterToolEvent, qt as projectBasename, Yt as projectClause, Jt as projectColumnClause, Xt as projectMatchesFilter, vu as pruneReplicationOps, yu as pruneReplicationOpsUntilCaughtUp, yn as rawEventsGate, Ds as readAuthFile, tt as readCodememConfigFile, Gi as readCoordinatorSyncConfig, ya as readImportPayload, wc as recencyScore, Si as recordNonce, ad as recordPeerSuccess, du as recordReplicationOp, id as recordSyncAttempt, ms as redactText, Ji as registerCoordinatorPresence, Ps as renderObserverHeaders, jn as requestJson, Wc as rerankResults, ft as resolveBuiltInProviderDefaultModel, dt as resolveBuiltInProviderFromModel, pt as resolveBuiltInProviderModel, St as resolveCustomProviderDefaultModel, mt as resolveCustomProviderFromModel, Ct as resolveCustomProviderModel, Ot as resolveDbPath, Ee as resolveHookProject, Hn as resolveKeyPaths, vs as resolveOAuthProvider, ht as resolvePlaceholder, Qt as resolveProject, bn as retryRawEventFailures, Es as runAuthCommand, Ud as runSyncDaemon, Md as runSyncPass, Ua as sanitizePayload, Ka as sanitizeToolOutput, H as schema, Gc as search, td as selectDialAddresses, Xo as semanticSearch, aa as serializeFloat32, sd as setPeerLocalActorClaim, od as setPeerProjectFilter, ou as setReplicationCursor, Vd as setSyncDaemonError, Bd as setSyncDaemonOk, eu as setSyncResetState, zd as shouldSkipOfflinePeer, vi as signRequest, Bn as storePrivateKeyKeychain, Jo as storeVectors, Ye as stripJsonComments, La as stripPrivate, Va as stripPrivateObj, Xe as stripTrailingCommas, Hd as syncDaemonTick, jd as syncOnce, Ad as syncPassPreflight, Bt as tableExists, qc as timeline, B as toJson, V as toJsonNullable, rd as updatePeerAddresses, gn as vacuumDatabase, Zn as validateExistingKeypair, yi as verifySignature, nt as writeCodememConfigFile };
|
|
9501
9577
|
|
|
9502
9578
|
//# sourceMappingURL=index.js.map
|