@cortexkit/pi-magic-context 0.47.0 → 0.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auto-search-worker.js +27 -22
- package/dist/embedding-activity-ew6dshfd.js +10 -0
- package/dist/embedding-backfill-b71dvfs8.js +20 -0
- package/dist/embedding-cache-7rp4gqzr.js +16 -0
- package/dist/embedding-worker.js +11 -8
- package/dist/harness-9872e0ym.js +10 -0
- package/dist/historian-calibration-extension.js +30 -4
- package/dist/index-041ex0cj.js +519 -0
- package/dist/index-0h2mkab2.js +17 -0
- package/dist/{index-2p1e8706.js → index-0vrkp1jq.js} +3 -3
- package/dist/index-135e8hvg.js +43 -0
- package/dist/{index-vgcemg0n.js → index-1evzceyw.js} +151 -2452
- package/dist/index-21v1kfs7.js +2309 -0
- package/dist/{index-n3g5mwa8.js → index-290tzwp1.js} +3 -3
- package/dist/{index-0qejsd7f.js → index-3cgbv47m.js} +289 -45
- package/dist/{index-xpbh1ypk.js → index-3mgf68pn.js} +38 -741
- package/dist/index-3rea6zd9.js +2396 -0
- package/dist/index-674f6qs2.js +16 -0
- package/dist/index-8976mezv.js +209 -0
- package/dist/index-904reqtg.js +152 -0
- package/dist/index-9p40dgkm.js +22 -0
- package/dist/index-9x1f96r8.js +11 -0
- package/dist/index-bejpsj10.js +1060 -0
- package/dist/{index-spk5vrhy.js → index-bgmv2kaf.js} +1 -16
- package/dist/{index-k3ezsjng.js → index-ehybv5nr.js} +14657 -13951
- package/dist/index-fapenpch.js +13 -0
- package/dist/index-hwqb3s7f.js +3804 -0
- package/dist/index-kjd1fj55.js +3106 -0
- package/dist/{index-td885aah.js → index-mcgehvgw.js} +181 -230
- package/dist/index-r024r8da.js +171 -0
- package/dist/{index-266qrgss.js → index-r56xgha8.js} +301 -27
- package/dist/{index-kztsbkqq.js → index-t7xfqtg6.js} +171 -168
- package/dist/index-vakawmbc.js +1262 -0
- package/dist/{index-7n8gb6j3.js → index-x3dcnxh0.js} +34 -33
- package/dist/index-xjqheg7a.js +931 -0
- package/dist/index-yarjn32j.js +3026 -0
- package/dist/index-yhzhdp3m.js +143 -0
- package/dist/index-yzcjxnt2.js +787 -0
- package/dist/{index-w76x3af7.js → index-zh1jw8qp.js} +107 -4
- package/dist/index.js +4926 -2437
- package/dist/logger-j6tecd1h.js +19 -0
- package/dist/message-index-bounded-qz5dz2se.js +120 -0
- package/dist/{message-time-backfill-d3db2etr.js → message-time-backfill-05ppypkz.js} +7 -6
- package/dist/migration-worker.js +20 -14
- package/dist/project-embedding-registry-ky7wrkr1.js +85 -0
- package/dist/raw-ordinal-warmup-worker.js +60 -0
- package/dist/{read-session-chunk-atxsyfqv.js → read-session-chunk-24sd89tf.js} +14 -6
- package/dist/read-session-raw-npx3920c.js +57 -0
- package/dist/{safe-notification-target-6cn3z99t.js → safe-notification-target-vn9kpmv4.js} +3 -2
- package/dist/search-jbh28nwy.js +40 -0
- package/dist/sqlite-3yye76ht.js +44 -0
- package/dist/sqlite-context-pragmas-gcgch6qz.js +6 -0
- package/dist/storage-memory-1z20y55x.js +71 -0
- package/dist/storage-tags-kjnsqbdb.js +127 -0
- package/dist/subagent-entry.js +35 -17
- package/dist/transformers-node-wasm.js +18415 -18415
- package/package.json +3 -3
- package/dist/index-7s8vw2y0.js +0 -5207
- package/dist/index-qaa5j277.js +0 -8544
- /package/dist/{index-en1k8h8m.js → index-5prbx026.js} +0 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// ../plugin/src/shared/protected-tools-policy.ts
|
|
2
|
+
var DEFAULT_PROTECTED_TOOLS = {
|
|
3
|
+
todowrite: 1,
|
|
4
|
+
ctx_reduce: 3
|
|
5
|
+
};
|
|
6
|
+
function normalizeProtectedToolName(name) {
|
|
7
|
+
return (name ?? "").toLowerCase().replace(/^mcp_/, "");
|
|
8
|
+
}
|
|
9
|
+
function mergeProtectedTools(...maps) {
|
|
10
|
+
return Object.fromEntries([DEFAULT_PROTECTED_TOOLS, ...maps].flatMap((map) => Object.entries(map ?? {}).map(([name, count]) => [
|
|
11
|
+
normalizeProtectedToolName(name),
|
|
12
|
+
count
|
|
13
|
+
])));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { DEFAULT_PROTECTED_TOOLS, normalizeProtectedToolName, mergeProtectedTools };
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// ../plugin/src/shared/opencode-db-path.ts
|
|
2
|
+
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
5
|
+
var cachedResolution = null;
|
|
6
|
+
var lastReadFailure = null;
|
|
7
|
+
var claimedDiagnostics = new Set;
|
|
8
|
+
function openCodeDataDir(env = process.env, dataHome) {
|
|
9
|
+
return join(dataHome ?? env.XDG_DATA_HOME ?? join(homedir(), ".local", "share"), "opencode");
|
|
10
|
+
}
|
|
11
|
+
function environmentKey(dataDir, hostGeneration, channel, env) {
|
|
12
|
+
return [
|
|
13
|
+
hostGeneration,
|
|
14
|
+
dataDir,
|
|
15
|
+
env.OPENCODE_DB ?? "",
|
|
16
|
+
env.OPENCODE_DISABLE_CHANNEL_DB ?? "",
|
|
17
|
+
channel ?? env.OPENCODE_CHANNEL ?? ""
|
|
18
|
+
].join("\x00");
|
|
19
|
+
}
|
|
20
|
+
function channelPath(dataDir, channel) {
|
|
21
|
+
return ["latest", "beta", "prod"].includes(channel) ? join(dataDir, "opencode.db") : join(dataDir, `opencode-${channel}.db`);
|
|
22
|
+
}
|
|
23
|
+
function discoveredCandidateNames(dataDir) {
|
|
24
|
+
const names = ["opencode.db", "opencode-local.db", "opencode-dev.db"];
|
|
25
|
+
try {
|
|
26
|
+
const discovered = readdirSync(dataDir, { withFileTypes: true }).filter((entry) => /^opencode-.+\.db$/.test(entry.name) && !names.includes(entry.name)).map((entry) => entry.name).sort();
|
|
27
|
+
names.push(...discovered);
|
|
28
|
+
} catch {}
|
|
29
|
+
return names;
|
|
30
|
+
}
|
|
31
|
+
function discoverOpenCodeDb(dataDir) {
|
|
32
|
+
const candidates = discoveredCandidateNames(dataDir).map((name, order) => {
|
|
33
|
+
const path = join(dataDir, name);
|
|
34
|
+
try {
|
|
35
|
+
const metadata = statSync(path);
|
|
36
|
+
return metadata.isFile() ? { path, order, mtimeMs: metadata.mtimeMs } : null;
|
|
37
|
+
} catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
const existing = candidates.filter((candidate) => candidate !== null).sort((left, right) => right.mtimeMs - left.mtimeMs || left.order - right.order)[0];
|
|
42
|
+
if (!existing) {
|
|
43
|
+
return { path: join(dataDir, "opencode.db"), source: "default", channel: null };
|
|
44
|
+
}
|
|
45
|
+
const name = existing.path.slice(dataDir.length + 1);
|
|
46
|
+
const channel = name === "opencode.db" ? null : name.slice("opencode-".length, -".db".length) || null;
|
|
47
|
+
return { path: existing.path, source: "discovered", channel };
|
|
48
|
+
}
|
|
49
|
+
function resolveV1Fresh(dataDir, env = process.env) {
|
|
50
|
+
const explicit = env.OPENCODE_DB;
|
|
51
|
+
if (explicit !== undefined && explicit.length > 0) {
|
|
52
|
+
if (explicit === ":memory:") {
|
|
53
|
+
return { path: explicit, source: "OPENCODE_DB", channel: null };
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
path: isAbsolute(explicit) ? explicit : join(dataDir, explicit),
|
|
57
|
+
source: "OPENCODE_DB",
|
|
58
|
+
channel: null
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const disableChannelDb = env.OPENCODE_DISABLE_CHANNEL_DB;
|
|
62
|
+
if (disableChannelDb === "1" || disableChannelDb === "true") {
|
|
63
|
+
return { path: join(dataDir, "opencode.db"), source: "default", channel: null };
|
|
64
|
+
}
|
|
65
|
+
const channel = env.OPENCODE_CHANNEL;
|
|
66
|
+
if (channel !== undefined && channel.length > 0) {
|
|
67
|
+
return { path: channelPath(dataDir, channel), source: "channel", channel };
|
|
68
|
+
}
|
|
69
|
+
return discoverOpenCodeDb(dataDir);
|
|
70
|
+
}
|
|
71
|
+
function sourceOpenCodeDatabaseFilename(hostGeneration, channel, env = process.env) {
|
|
72
|
+
if (hostGeneration === "v1") {
|
|
73
|
+
const explicit = env.OPENCODE_DB;
|
|
74
|
+
if (explicit !== undefined && explicit.length > 0)
|
|
75
|
+
return explicit;
|
|
76
|
+
if (env.OPENCODE_DISABLE_CHANNEL_DB === "1" || env.OPENCODE_DISABLE_CHANNEL_DB === "true") {
|
|
77
|
+
return "opencode.db";
|
|
78
|
+
}
|
|
79
|
+
return ["latest", "beta", "prod"].includes(channel) ? "opencode.db" : `opencode-${channel}.db`;
|
|
80
|
+
}
|
|
81
|
+
return env.OPENCODE_DB ?? (["latest", "dev", "beta", "next", "prod"].includes(channel) || env.OPENCODE_DISABLE_CHANNEL_DB === "1" || env.OPENCODE_DISABLE_CHANNEL_DB === "true" ? "opencode.db" : `opencode-${channel.replace(/[^a-zA-Z0-9._-]/g, "")}.db`);
|
|
82
|
+
}
|
|
83
|
+
function resolveV2Fresh(dataDir, channel, env) {
|
|
84
|
+
const filename = sourceOpenCodeDatabaseFilename("v2", channel, env);
|
|
85
|
+
const explicit = env.OPENCODE_DB !== undefined;
|
|
86
|
+
return {
|
|
87
|
+
path: filename === ":memory:" ? filename : resolve(dataDir, filename),
|
|
88
|
+
source: explicit ? "OPENCODE_DB" : env.OPENCODE_CHANNEL ? "channel" : "default",
|
|
89
|
+
channel: explicit ? null : channel
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function resolveOpenCodeDbPath(hostGeneration = "v1", options = {}) {
|
|
93
|
+
const env = options.env ?? process.env;
|
|
94
|
+
const dataDir = openCodeDataDir(env, options.dataHome);
|
|
95
|
+
const channel = options.channel ?? env.OPENCODE_CHANNEL;
|
|
96
|
+
const key = environmentKey(dataDir, hostGeneration, channel, env);
|
|
97
|
+
if (cachedResolution?.key === key && (!cachedResolution.existed || existsSync(cachedResolution.resolution.path))) {
|
|
98
|
+
if (cachedResolution.existed)
|
|
99
|
+
return cachedResolution.resolution;
|
|
100
|
+
}
|
|
101
|
+
const resolution = hostGeneration === "v2" ? resolveV2Fresh(dataDir, channel ?? "latest", env) : resolveV1Fresh(dataDir, env);
|
|
102
|
+
cachedResolution = {
|
|
103
|
+
key,
|
|
104
|
+
resolution,
|
|
105
|
+
existed: resolution.path !== ":memory:" && existsSync(resolution.path)
|
|
106
|
+
};
|
|
107
|
+
return resolution;
|
|
108
|
+
}
|
|
109
|
+
function schemaTableNames(db, schema = "main") {
|
|
110
|
+
const rows = db.prepare(`SELECT name FROM ${schema}.sqlite_master WHERE type = 'table' AND name IN ('message', 'part', 'session', 'project', 'session_message', 'session_v2')`).all();
|
|
111
|
+
return new Set(rows.flatMap((row) => typeof row.name === "string" ? [row.name] : []));
|
|
112
|
+
}
|
|
113
|
+
function hasV1MessageTables(db, schema = "main") {
|
|
114
|
+
const tables = schemaTableNames(db, schema);
|
|
115
|
+
return tables.has("message") && tables.has("part");
|
|
116
|
+
}
|
|
117
|
+
function detectOpenCodeStoreGeneration(db, schema = "main") {
|
|
118
|
+
const tables = schemaTableNames(db, schema);
|
|
119
|
+
const hasV1Messages = tables.has("message") && tables.has("part");
|
|
120
|
+
if (hasV1Messages)
|
|
121
|
+
return "v1";
|
|
122
|
+
if (tables.has("session_message"))
|
|
123
|
+
return "v2";
|
|
124
|
+
if (tables.has("session") || tables.has("project"))
|
|
125
|
+
return "v1";
|
|
126
|
+
return "unknown";
|
|
127
|
+
}
|
|
128
|
+
function hasMigratedV2Schema(tables) {
|
|
129
|
+
return tables.has("session_message") && tables.has("session_v2");
|
|
130
|
+
}
|
|
131
|
+
function isOpenCodeV2Store(db, schema = "main") {
|
|
132
|
+
return detectOpenCodeStoreGeneration(db, schema) === "v2" || hasMigratedV2Schema(schemaTableNames(db, schema));
|
|
133
|
+
}
|
|
134
|
+
function assertOpenCodeStoreGeneration(db, expected, path, schema = "main") {
|
|
135
|
+
const actual = detectOpenCodeStoreGeneration(db, schema);
|
|
136
|
+
if (actual === expected)
|
|
137
|
+
return;
|
|
138
|
+
if (expected === "v2" && isOpenCodeV2Store(db, schema))
|
|
139
|
+
return;
|
|
140
|
+
if (actual === "unknown")
|
|
141
|
+
return;
|
|
142
|
+
throw new Error(`OpenCode store generation mismatch at ${path}: expected ${expected}, found ${actual}; refusing generation-specific database access`);
|
|
143
|
+
}
|
|
144
|
+
function openCodeDbPathExists(resolution = resolveOpenCodeDbPath()) {
|
|
145
|
+
return resolution.path !== ":memory:" && existsSync(resolution.path);
|
|
146
|
+
}
|
|
147
|
+
function getOpenCodeDbProbeDescriptions(resolution = resolveOpenCodeDbPath()) {
|
|
148
|
+
if (resolution.source === "OPENCODE_DB")
|
|
149
|
+
return [resolution.path];
|
|
150
|
+
if (resolution.source === "channel" || process.env.OPENCODE_DISABLE_CHANNEL_DB === "1" || process.env.OPENCODE_DISABLE_CHANNEL_DB === "true") {
|
|
151
|
+
return [resolution.path];
|
|
152
|
+
}
|
|
153
|
+
const dataDir = openCodeDataDir();
|
|
154
|
+
return [
|
|
155
|
+
join(dataDir, "opencode.db"),
|
|
156
|
+
join(dataDir, "opencode-local.db"),
|
|
157
|
+
join(dataDir, "opencode-dev.db"),
|
|
158
|
+
join(dataDir, "opencode-<channel>.db")
|
|
159
|
+
];
|
|
160
|
+
}
|
|
161
|
+
function recordOpenCodeDbReadFailure(resolution, error) {
|
|
162
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
163
|
+
lastReadFailure = { ...resolution, message };
|
|
164
|
+
return lastReadFailure;
|
|
165
|
+
}
|
|
166
|
+
function clearOpenCodeDbReadFailure(path) {
|
|
167
|
+
if (path === undefined || lastReadFailure?.path === path)
|
|
168
|
+
lastReadFailure = null;
|
|
169
|
+
}
|
|
170
|
+
function claimOpenCodeDbDiagnosticOnce(surface, resolution) {
|
|
171
|
+
const key = `${surface}\x00${resolution.path}\x00${resolution.source}`;
|
|
172
|
+
if (claimedDiagnostics.has(key))
|
|
173
|
+
return false;
|
|
174
|
+
claimedDiagnostics.add(key);
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// ../plugin/src/features/magic-context/tool-definition-tokens.ts
|
|
179
|
+
var measurements = new Map;
|
|
180
|
+
var fingerprints = new Map;
|
|
181
|
+
var persistenceDb = null;
|
|
182
|
+
var cachedInsertStmt = null;
|
|
183
|
+
function keyFor(providerID, modelID, agentName) {
|
|
184
|
+
const agent = agentName && agentName.length > 0 ? agentName : "default";
|
|
185
|
+
return `${providerID}/${modelID}/${agent}`;
|
|
186
|
+
}
|
|
187
|
+
function setDatabase(db) {
|
|
188
|
+
persistenceDb = db;
|
|
189
|
+
cachedInsertStmt = null;
|
|
190
|
+
}
|
|
191
|
+
function loadToolDefinitionMeasurements(db) {
|
|
192
|
+
let rows = [];
|
|
193
|
+
try {
|
|
194
|
+
rows = db.prepare("SELECT provider_id, model_id, agent_name, tool_id, token_count FROM tool_definition_measurements").all();
|
|
195
|
+
} catch {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
for (const row of rows) {
|
|
199
|
+
const key = keyFor(row.provider_id, row.model_id, row.agent_name);
|
|
200
|
+
let inner = measurements.get(key);
|
|
201
|
+
if (!inner) {
|
|
202
|
+
inner = new Map;
|
|
203
|
+
measurements.set(key, inner);
|
|
204
|
+
}
|
|
205
|
+
inner.set(row.tool_id, row.token_count);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export { resolveOpenCodeDbPath, hasV1MessageTables, assertOpenCodeStoreGeneration, openCodeDbPathExists, getOpenCodeDbProbeDescriptions, recordOpenCodeDbReadFailure, clearOpenCodeDbReadFailure, claimOpenCodeDbDiagnosticOnce, setDatabase, loadToolDefinitionMeasurements };
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_LOCAL_EMBEDDING_MODEL,
|
|
3
|
+
LocalEmbeddingProvider
|
|
4
|
+
} from "./index-1evzceyw.js";
|
|
5
|
+
import {
|
|
6
|
+
log2
|
|
7
|
+
} from "./index-x3dcnxh0.js";
|
|
8
|
+
import {
|
|
9
|
+
isSynapseEmbeddingTruncated,
|
|
10
|
+
SynapseEmbeddingProvider
|
|
11
|
+
} from "./index-21v1kfs7.js";
|
|
12
|
+
import {
|
|
13
|
+
OpenAICompatibleEmbeddingProvider,
|
|
14
|
+
getProjectEmbeddingSnapshot2,
|
|
15
|
+
embedBatchForProject2
|
|
16
|
+
} from "./index-yarjn32j.js";
|
|
17
|
+
import {
|
|
18
|
+
isEmbeddingHostBusy2
|
|
19
|
+
} from "./index-9p40dgkm.js";
|
|
20
|
+
import {
|
|
21
|
+
saveEmbeddingIfHashMatches
|
|
22
|
+
} from "./index-yhzhdp3m.js";
|
|
23
|
+
|
|
24
|
+
// ../plugin/src/features/magic-context/memory/cosine-similarity.ts
|
|
25
|
+
function cosineSimilarity(a, b) {
|
|
26
|
+
if (a.length !== b.length) {
|
|
27
|
+
return 0;
|
|
28
|
+
}
|
|
29
|
+
let dotProduct = 0;
|
|
30
|
+
let normA = 0;
|
|
31
|
+
let normB = 0;
|
|
32
|
+
for (let index = 0;index < a.length; index++) {
|
|
33
|
+
dotProduct += a[index] * b[index];
|
|
34
|
+
normA += a[index] * a[index];
|
|
35
|
+
normB += b[index] * b[index];
|
|
36
|
+
}
|
|
37
|
+
const denominator = Math.sqrt(normA) * Math.sqrt(normB);
|
|
38
|
+
return denominator === 0 ? 0 : dotProduct / denominator;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ../plugin/src/features/magic-context/memory/embedding.ts
|
|
42
|
+
var DEFAULT_EMBEDDING_CONFIG = {
|
|
43
|
+
provider: "local",
|
|
44
|
+
model: DEFAULT_LOCAL_EMBEDDING_MODEL,
|
|
45
|
+
local_runtime: "auto"
|
|
46
|
+
};
|
|
47
|
+
var embeddingConfig = DEFAULT_EMBEDDING_CONFIG;
|
|
48
|
+
var provider = null;
|
|
49
|
+
function createProvider(config) {
|
|
50
|
+
if (config.provider === "off") {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
if (config.provider === "openai-compatible") {
|
|
54
|
+
return new OpenAICompatibleEmbeddingProvider({
|
|
55
|
+
endpoint: config.endpoint,
|
|
56
|
+
model: config.model,
|
|
57
|
+
apiKey: config.api_key,
|
|
58
|
+
inputType: config.input_type,
|
|
59
|
+
queryInputType: config.query_input_type,
|
|
60
|
+
queryInstruction: config.query_instruction,
|
|
61
|
+
documentPrefix: config.document_prefix,
|
|
62
|
+
truncate: config.truncate,
|
|
63
|
+
maxInputTokens: config.max_input_tokens
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (config.provider === "local") {
|
|
67
|
+
return new LocalEmbeddingProvider(config.model, config.max_input_tokens, config.local_dtype, config.local_runtime);
|
|
68
|
+
}
|
|
69
|
+
if (config.provider === "synapse") {
|
|
70
|
+
const synapse = config;
|
|
71
|
+
return new SynapseEmbeddingProvider({
|
|
72
|
+
connectionFile: synapse.synapse_connection_file ?? "",
|
|
73
|
+
projectRoot: "",
|
|
74
|
+
session: "embedding",
|
|
75
|
+
model: synapse.model,
|
|
76
|
+
fingerprint: synapse.synapse_fingerprint,
|
|
77
|
+
tableEpoch: synapse.synapse_table_epoch,
|
|
78
|
+
dims: synapse.synapse_dims,
|
|
79
|
+
recommendedBatch: synapse.synapse_recommended_batch,
|
|
80
|
+
recommendedTokenBudget: synapse.synapse_recommended_token_budget,
|
|
81
|
+
descriptor: synapse.synapse_descriptor,
|
|
82
|
+
provenance: synapse.synapse_provenance
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
throw new Error("Unknown embedding provider");
|
|
86
|
+
}
|
|
87
|
+
function getOrCreateProvider() {
|
|
88
|
+
if (provider) {
|
|
89
|
+
return provider;
|
|
90
|
+
}
|
|
91
|
+
provider = createProvider(embeddingConfig);
|
|
92
|
+
return provider;
|
|
93
|
+
}
|
|
94
|
+
function isEmbeddingEnabled() {
|
|
95
|
+
return embeddingConfig.provider !== "off";
|
|
96
|
+
}
|
|
97
|
+
async function embedText(text, signal) {
|
|
98
|
+
const currentProvider = getOrCreateProvider();
|
|
99
|
+
if (!currentProvider) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
if (!await currentProvider.initialize()) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
const vector = await currentProvider.embed(text, signal);
|
|
106
|
+
return vector && !isSynapseEmbeddingTruncated(vector) ? vector : null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// ../plugin/src/features/magic-context/memory/embedding-backfill.ts
|
|
110
|
+
async function ensureMemoryEmbeddings2(args) {
|
|
111
|
+
if (isEmbeddingHostBusy2())
|
|
112
|
+
return args.existingEmbeddings;
|
|
113
|
+
const snapshot = getProjectEmbeddingSnapshot2(args.projectIdentity);
|
|
114
|
+
if (!snapshot?.enabled) {
|
|
115
|
+
return args.existingEmbeddings;
|
|
116
|
+
}
|
|
117
|
+
const missingMemories = args.memories.filter((memory) => !args.existingEmbeddings.has(memory.id));
|
|
118
|
+
if (missingMemories.length === 0) {
|
|
119
|
+
return args.existingEmbeddings;
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
const result = await embedBatchForProject2(args.projectIdentity, missingMemories.map((memory) => memory.content));
|
|
123
|
+
if (!result) {
|
|
124
|
+
return args.existingEmbeddings;
|
|
125
|
+
}
|
|
126
|
+
const staged = new Map;
|
|
127
|
+
args.db.transaction(() => {
|
|
128
|
+
for (const [index, memory] of missingMemories.entries()) {
|
|
129
|
+
const embedding = result.vectors[index];
|
|
130
|
+
if (!embedding) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const saved = saveEmbeddingIfHashMatches(args.db, memory.id, embedding, result.modelId, memory.normalizedHash);
|
|
134
|
+
if (saved) {
|
|
135
|
+
staged.set(memory.id, { embedding, modelId: result.modelId });
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}).immediate();
|
|
139
|
+
const currentSnapshot = getProjectEmbeddingSnapshot2(args.projectIdentity);
|
|
140
|
+
if (!currentSnapshot || currentSnapshot.generation !== result.generation) {
|
|
141
|
+
return args.existingEmbeddings;
|
|
142
|
+
}
|
|
143
|
+
for (const [id, embedding] of staged) {
|
|
144
|
+
args.existingEmbeddings.set(id, embedding);
|
|
145
|
+
}
|
|
146
|
+
} catch (error) {
|
|
147
|
+
log2("[magic-context] failed to backfill memory embeddings:", error);
|
|
148
|
+
}
|
|
149
|
+
return args.existingEmbeddings;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export { cosineSimilarity, isEmbeddingEnabled, embedText, ensureMemoryEmbeddings2 };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// ../plugin/src/shared/embedding-activity.ts
|
|
2
|
+
var busySessions = new Set;
|
|
3
|
+
function setEmbeddingSessionBusy2(sessionId, busy) {
|
|
4
|
+
if (busy)
|
|
5
|
+
busySessions.add(sessionId);
|
|
6
|
+
else
|
|
7
|
+
busySessions.delete(sessionId);
|
|
8
|
+
}
|
|
9
|
+
function observeEmbeddingActivity2(event) {
|
|
10
|
+
const props = event.properties;
|
|
11
|
+
if (!props || typeof props.sessionID !== "string")
|
|
12
|
+
return;
|
|
13
|
+
if (event.type === "session.idle" || event.type === "session.deleted" || event.type === "session.status" && props.status?.type === "idle") {
|
|
14
|
+
setEmbeddingSessionBusy2(props.sessionID, false);
|
|
15
|
+
} else if (event.type === "session.status" && props.status?.type) {
|
|
16
|
+
setEmbeddingSessionBusy2(props.sessionID, true);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function isEmbeddingHostBusy2() {
|
|
20
|
+
return busySessions.size > 0;
|
|
21
|
+
}
|
|
22
|
+
export { setEmbeddingSessionBusy2, observeEmbeddingActivity2, isEmbeddingHostBusy2 };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// ../plugin/src/features/magic-context/memory/normalize-hash.ts
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
function normalizeMemoryContent(content) {
|
|
4
|
+
return content.toLowerCase().replace(/\s+/g, " ").trim();
|
|
5
|
+
}
|
|
6
|
+
function computeNormalizedHash(content) {
|
|
7
|
+
const normalized = normalizeMemoryContent(content);
|
|
8
|
+
return createHash("md5").update(normalized).digest("hex");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { computeNormalizedHash };
|