@cortexkit/opencode-magic-context 0.17.1 → 0.17.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.
@@ -25,6 +25,17 @@
25
25
  * If `setDatabase()` hasn't been called yet (cold path before openDatabase
26
26
  * completes), `recordToolDefinition` still updates the in-memory map and
27
27
  * silently skips persistence — first measurement after init lands both.
28
+ *
29
+ * Hot-path optimization: `tool.definition` fires once per tool per LLM
30
+ * flight (~58 tools × 5–18ms SQLite write = ~1.4s of redundant work per
31
+ * flight on large MC databases). Tool descriptions and parameters almost
32
+ * never change between flights, so we keep a per-key content-fingerprint
33
+ * Map and bail out at the top of `recordToolDefinition` when the new fire
34
+ * carries the same fingerprint as the previous one. This collapses
35
+ * steady-state hook overhead from ~1.4s to <1ms while still re-measuring
36
+ * any tool whose description/schema actually changed (e.g. MCP server
37
+ * restart, OpenCode upgrade). Cached prepared statement avoids repeated
38
+ * `db.prepare()` compile cost on first-flight rebuilds.
28
39
  */
29
40
  import type { Database } from "../../shared/sqlite";
30
41
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"tool-definition-tokens.d.ts","sourceRoot":"","sources":["../../../src/features/magic-context/tool-definition-tokens.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAGH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAiBpD;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,QAAQ,GAAG,IAAI,CAE9C;AAED;;;;;;;;;GASG;AACH,wBAAgB,8BAA8B,CAAC,EAAE,EAAE,QAAQ,GAAG,IAAI,CA4BjE;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAChC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,OAAO,GACpB,IAAI,CAgDN;AAED;;;GAGG;AACH,wBAAgB,+BAA+B,CAC3C,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,GAAG,SAAS,GAC9B,MAAM,GAAG,SAAS,CAOpB;AAED,sEAAsE;AACtE,wBAAgB,iCAAiC,IAAI,IAAI,CAGxD;AAED,+EAA+E;AAC/E,wBAAgB,yBAAyB,IAAI,KAAK,CAAC;IAC/C,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;CACrB,CAAC,CAMD"}
1
+ {"version":3,"file":"tool-definition-tokens.d.ts","sourceRoot":"","sources":["../../../src/features/magic-context/tool-definition-tokens.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAGH,OAAO,KAAK,EAAE,QAAQ,EAAa,MAAM,qBAAqB,CAAC;AAmD/D;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,QAAQ,GAAG,IAAI,CAK9C;AAED;;;;;;;;;GASG;AACH,wBAAgB,8BAA8B,CAAC,EAAE,EAAE,QAAQ,GAAG,IAAI,CAkCjE;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAChC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,OAAO,GACpB,IAAI,CAwEN;AAED;;;GAGG;AACH,wBAAgB,+BAA+B,CAC3C,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,GAAG,SAAS,GAC9B,MAAM,GAAG,SAAS,CAOpB;AAED,sEAAsE;AACtE,wBAAgB,iCAAiC,IAAI,IAAI,CAKxD;AAED,+EAA+E;AAC/E,wBAAgB,yBAAyB,IAAI,KAAK,CAAC;IAC/C,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;CACrB,CAAC,CAMD"}
package/dist/index.js CHANGED
@@ -147913,8 +147913,20 @@ function keyFor(providerID, modelID, agentName) {
147913
147913
  const agent = agentName && agentName.length > 0 ? agentName : "default";
147914
147914
  return `${providerID}/${modelID}/${agent}`;
147915
147915
  }
147916
+ function fingerprintFor(description, parameters) {
147917
+ const descLen = description.length;
147918
+ if (parameters === undefined)
147919
+ return `${descLen}:none`;
147920
+ if (parameters === null)
147921
+ return `${descLen}:null`;
147922
+ if (typeof parameters !== "object")
147923
+ return `${descLen}:${typeof parameters}`;
147924
+ const keys = Object.keys(parameters);
147925
+ return `${descLen}:obj:${keys.length}:${keys.sort().join(",")}`;
147926
+ }
147916
147927
  function setDatabase(db) {
147917
147928
  persistenceDb = db;
147929
+ cachedInsertStmt = null;
147918
147930
  }
147919
147931
  function loadToolDefinitionMeasurements(db) {
147920
147932
  let rows = [];
@@ -147937,6 +147949,10 @@ function recordToolDefinition(providerID, modelID, agentName, toolID, descriptio
147937
147949
  if (!providerID || !modelID || !toolID)
147938
147950
  return;
147939
147951
  const key = keyFor(providerID, modelID, agentName);
147952
+ const fp = fingerprintFor(description ?? "", parameters);
147953
+ let innerFp = fingerprints.get(key);
147954
+ if (innerFp && innerFp.get(toolID) === fp)
147955
+ return;
147940
147956
  let paramsText = "";
147941
147957
  try {
147942
147958
  paramsText = parameters === undefined ? "" : JSON.stringify(parameters);
@@ -147950,13 +147966,23 @@ function recordToolDefinition(providerID, modelID, agentName, toolID, descriptio
147950
147966
  measurements.set(key, inner);
147951
147967
  }
147952
147968
  inner.set(toolID, tokens);
147969
+ if (!innerFp) {
147970
+ innerFp = new Map;
147971
+ fingerprints.set(key, innerFp);
147972
+ }
147973
+ innerFp.set(toolID, fp);
147953
147974
  if (persistenceDb) {
147954
147975
  try {
147955
147976
  const agent = agentName && agentName.length > 0 ? agentName : "default";
147956
- persistenceDb.prepare(`INSERT OR REPLACE INTO tool_definition_measurements
147977
+ if (!cachedInsertStmt) {
147978
+ cachedInsertStmt = persistenceDb.prepare(`INSERT OR REPLACE INTO tool_definition_measurements
147957
147979
  (provider_id, model_id, agent_name, tool_id, token_count, recorded_at)
147958
- VALUES (?, ?, ?, ?, ?, ?)`).run(providerID, modelID, agent, toolID, tokens, Date.now());
147959
- } catch {}
147980
+ VALUES (?, ?, ?, ?, ?, ?)`);
147981
+ }
147982
+ cachedInsertStmt.run(providerID, modelID, agent, toolID, tokens, Date.now());
147983
+ } catch {
147984
+ cachedInsertStmt = null;
147985
+ }
147960
147986
  }
147961
147987
  }
147962
147988
  function getMeasuredToolDefinitionTokens(providerID, modelID, agentName) {
@@ -147970,10 +147996,11 @@ function getMeasuredToolDefinitionTokens(providerID, modelID, agentName) {
147970
147996
  total += tokens;
147971
147997
  return total;
147972
147998
  }
147973
- var measurements, persistenceDb = null;
147999
+ var measurements, fingerprints, persistenceDb = null, cachedInsertStmt = null;
147974
148000
  var init_tool_definition_tokens = __esm(() => {
147975
148001
  init_read_session_formatting();
147976
148002
  measurements = new Map;
148003
+ fingerprints = new Map;
147977
148004
  });
147978
148005
 
147979
148006
  // ../../node_modules/.bun/esprima@4.0.1/node_modules/esprima/dist/esprima.js
@@ -172140,7 +172167,7 @@ function extractToolInfo(part) {
172140
172167
  return null;
172141
172168
  }
172142
172169
  function buildToolFingerprints(messages) {
172143
- const fingerprints = new Map;
172170
+ const fingerprints2 = new Map;
172144
172171
  for (const message of messages) {
172145
172172
  if (message.info.role !== "assistant")
172146
172173
  continue;
@@ -172158,11 +172185,11 @@ function buildToolFingerprints(messages) {
172158
172185
  try {
172159
172186
  const fingerprint = `${ownerMsgId}:${info.toolName}:${JSON.stringify(info.args)}`;
172160
172187
  const compositeKey = `${ownerMsgId}\x00${callId}`;
172161
- fingerprints.set(compositeKey, fingerprint);
172188
+ fingerprints2.set(compositeKey, fingerprint);
172162
172189
  } catch {}
172163
172190
  }
172164
172191
  }
172165
- return fingerprints;
172192
+ return fingerprints2;
172166
172193
  }
172167
172194
  function extractCallId(part) {
172168
172195
  if (part.type === "tool" && typeof part.callID === "string")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/opencode-magic-context",
3
- "version": "0.17.1",
3
+ "version": "0.17.2",
4
4
  "type": "module",
5
5
  "description": "OpenCode plugin for Magic Context — cross-session memory and context management",
6
6
  "main": "dist/index.js",