@codeworksh/harness 0.5.0-canary.90.1.gec09b058 → 0.5.0-canary.97.1.gaba2d655

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/effect.d.mts CHANGED
@@ -29292,6 +29292,20 @@ declare const StdioError_base: Schema.Class<StdioError, Schema.TaggedStruct<"Mcp
29292
29292
  }>, import("effect/Cause").YieldableError>;
29293
29293
  /** A failure inside the stdio transport, reported to the SDK through `onerror` or a rejected promise. */
29294
29294
  declare class StdioError extends StdioError_base {}
29295
+ //#endregion
29296
+ //#region src/tool/offer.d.ts
29297
+ /** A deferred tool as searched. */
29298
+ interface DeferredTool extends Message.Tool {
29299
+ /** Text of the namespace it belongs to, such as its MCP server's name, description and instructions. */
29300
+ readonly namespace?: string;
29301
+ }
29302
+ interface Offer {
29303
+ /**
29304
+ * Deferred tools the model may call and is not offered yet: the registry's, in its order, then
29305
+ * any an MCP server offers since the turn started. Waits, bounded, for servers still connecting.
29306
+ */
29307
+ readonly deferred: Effect.Effect<ReadonlyArray<DeferredTool>>;
29308
+ }
29295
29309
  import * as import__codeworksh_plugin_tool from "@codeworksh/plugin/tool";
29296
29310
  declare namespace mcp_d_exports {
29297
29311
  export { Input$1 as Input, Interface$7 as Interface, mcp_d_exports as Mcp, Reconciled, Service$7 as Service, State$1 as State, Status$1 as Status, check$1 as check, layer$7 as layer };
@@ -29348,7 +29362,7 @@ interface Reconciled {
29348
29362
  * The thread's deferred tools as they stand when it runs, under the names they are registered by.
29349
29363
  * Waits first, within {@link STARTUP_WAIT}, for those servers' connect attempts in progress.
29350
29364
  */
29351
- readonly deferred: Effect.Effect<ReadonlyArray<Message.Tool>>;
29365
+ readonly deferred: Effect.Effect<ReadonlyArray<DeferredTool>>;
29352
29366
  /** The `mcpservers` prompt section, as the servers stand once the ones with declared tools were waited for. */
29353
29367
  readonly section: string | undefined;
29354
29368
  }
@@ -29421,15 +29435,6 @@ interface Rendered {
29421
29435
  readonly sections: Readonly<Record<string, string>>;
29422
29436
  }
29423
29437
  import * as import__codeworksh_plugin_plugin_tool from "@codeworksh/plugin/plugin/tool";
29424
- //#endregion
29425
- //#region src/tool/offer.d.ts
29426
- interface Offer {
29427
- /**
29428
- * Deferred tools the model may call and is not offered yet: the registry's, in its order, then
29429
- * any an MCP server offers since the turn started. Waits, bounded, for servers still connecting.
29430
- */
29431
- readonly deferred: Effect.Effect<ReadonlyArray<Message.Tool>>;
29432
- }
29433
29438
  import * as import__codeworksh_plugin_tool_progress from "@codeworksh/plugin/tool/progress";
29434
29439
  declare namespace executor_d_exports {
29435
29440
  export { Executor, HandleOptions, MakeOptions, ProgressEvent, ToolOutcome, make$3 as make };
package/effect.mjs CHANGED
@@ -5433,40 +5433,85 @@ const stem = (term) => {
5433
5433
  if (term.length > 3 && term.endsWith("s") && !term.endsWith("ss")) return term.slice(0, -1);
5434
5434
  return term;
5435
5435
  };
5436
- /** Lowercase terms, split at camelCase boundaries and non-alphanumerics, without stop words. */
5437
- const tokenize = (text) => text.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").toLowerCase().split(/[^a-z0-9]+/).filter((term) => term.length > 0 && !STOP_WORDS.has(term)).map(stem);
5438
- /** Okapi BM25 (k1 1.2, b 0.75): the names of the best matches, best first; ties keep document order. */
5436
+ const wordSegmenter = new Intl.Segmenter(void 0, { granularity: "word" });
5437
+ /** Unicode words, split at camelCase boundaries, lowercase, without stop words; ASCII words in singular form. */
5438
+ const tokenize = (text) => {
5439
+ const normalized = text.normalize("NFC").replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").toLowerCase().replace(/[^\p{L}\p{M}\p{N}]+/gu, " ");
5440
+ return (/^[a-z0-9 ]*$/.test(normalized) ? normalized.split(" ") : Array.from(wordSegmenter.segment(normalized), (segment) => segment.isWordLike ? segment.segment : "")).filter((word) => word.length > 0 && !STOP_WORDS.has(word)).map((word) => /^[a-z0-9]+$/.test(word) ? stem(word) : word);
5441
+ };
5442
+ const K1 = 1.2;
5443
+ const B = .75;
5444
+ /**
5445
+ * A query term in the name counts this many times its IDF. The text part of a term is at most `K1 + 1` (2.2) times
5446
+ * its IDF and about 1 for a single mention, so a name match beats any number of mentions in the text.
5447
+ */
5448
+ const NAME_WEIGHT = 3;
5449
+ /** Every tool of a namespace shares its text, so it helps find the namespace but not the tool in it. */
5450
+ const NAMESPACE_WEIGHT = .5;
5451
+ /** Lowercase with characters invalid in identifiers as `_`, so `dev-radius` matches `dev_radius`. */
5452
+ const normalizeName = (name) => name.trim().toLowerCase().replace(/[^a-z0-9_$]/g, "_");
5453
+ /** The whole query is the tool's full name or its name after a `__`: `thread_read` names `mcp__app__thread_read`. */
5454
+ const isExactName = (query, name) => {
5455
+ const normalized = normalizeName(name);
5456
+ return normalized === query || normalized.endsWith(`__${query}`);
5457
+ };
5458
+ const indexDocument = (document) => {
5459
+ const textCounts = /* @__PURE__ */ new Map();
5460
+ for (const term of tokenize(document.text)) textCounts.set(term, (textCounts.get(term) ?? 0) + 1);
5461
+ const segments = document.name.split("__");
5462
+ return {
5463
+ name: document.name,
5464
+ nameTerms: new Set(tokenize(document.name)),
5465
+ localNameTerms: [...new Set(tokenize(segments[segments.length - 1]))],
5466
+ textCounts,
5467
+ textLength: [...textCounts.values()].reduce((sum, count) => sum + count, 0),
5468
+ namespaceTerms: new Set(tokenize(document.namespaceText ?? ""))
5469
+ };
5470
+ };
5471
+ /**
5472
+ * Okapi BM25 over `text`, with the name and the namespace as separate fields: the names of the best matches, best
5473
+ * first; ties keep document order.
5474
+ * - A query that is exactly a tool's name, or its name after a `__`, ranks that tool first.
5475
+ * - Then a tool matching more of the query's terms, in any field, ranks above one matching fewer, whatever their
5476
+ * scores: for `screenshot window`, a tool whose description has both words beats `window`, whose name has one.
5477
+ * - A tool whose whole local name is in the query gets a bonus worth one more match of every query term, so
5478
+ * `peekaboo app` finds `mcp__peekaboo__app` over tools that only mention the words.
5479
+ *
5480
+ * Only `text` is length-normalized, so a long name or namespace text does not lower a tool's score.
5481
+ */
5439
5482
  const rank$2 = (query, documents, limit) => {
5440
- const k1 = 1.2;
5441
- const b = .75;
5442
- const queryTerms = [...new Set(tokenize(query))];
5443
- if (queryTerms.length === 0 || documents.length === 0 || limit <= 0) return [];
5444
- const termCounts = documents.map((document) => {
5445
- const counts = /* @__PURE__ */ new Map();
5446
- for (const term of tokenize(document.text)) counts.set(term, (counts.get(term) ?? 0) + 1);
5447
- return counts;
5448
- });
5449
- const lengths = termCounts.map((counts) => [...counts.values()].reduce((sum, count) => sum + count, 0));
5450
- const averageLength = lengths.reduce((sum, length) => sum + length, 0) / documents.length || 1;
5451
- const idf = new Map(queryTerms.map((term) => {
5452
- const frequency = termCounts.filter((counts) => counts.has(term)).length;
5453
- return [term, Math.log(1 + (documents.length - frequency + .5) / (frequency + .5))];
5483
+ const queryTerms = new Set(tokenize(query));
5484
+ if (queryTerms.size === 0 || documents.length === 0 || limit <= 0) return [];
5485
+ const indexed = documents.map(indexDocument);
5486
+ const averageLength = indexed.reduce((sum, document) => sum + document.textLength, 0) / indexed.length || 1;
5487
+ const idf = new Map([...queryTerms].map((term) => {
5488
+ const frequency = indexed.filter((document) => document.nameTerms.has(term) || document.textCounts.has(term) || document.namespaceTerms.has(term)).length;
5489
+ return [term, Math.log(1 + (indexed.length - frequency + .5) / (frequency + .5))];
5454
5490
  }));
5491
+ const localNameBonus = [...idf.values()].reduce((sum, value) => sum + value, 0);
5492
+ const exactQuery = normalizeName(query);
5455
5493
  const matches = [];
5456
- documents.forEach((document, index) => {
5494
+ for (const document of indexed) {
5495
+ const norm = K1 * (.25 + B * document.textLength / averageLength);
5457
5496
  let score = 0;
5497
+ let covered = 0;
5458
5498
  for (const term of queryTerms) {
5459
- const count = termCounts[index].get(term);
5460
- if (!count) continue;
5461
- const norm = k1 * (.25 + b * lengths[index] / averageLength);
5462
- score += (idf.get(term) ?? 0) * (count * 2.2 / (count + norm));
5499
+ const count = document.textCounts.get(term) ?? 0;
5500
+ const weight = (document.nameTerms.has(term) ? NAME_WEIGHT : 0) + (document.namespaceTerms.has(term) ? NAMESPACE_WEIGHT : 0) + count * 2.2 / (count + norm);
5501
+ if (weight > 0) covered++;
5502
+ score += (idf.get(term) ?? 0) * weight;
5463
5503
  }
5464
- if (score > 0) matches.push({
5504
+ if (score === 0) continue;
5505
+ const local = document.localNameTerms;
5506
+ if (local.length > 0 && local.every((term) => queryTerms.has(term))) score += localNameBonus;
5507
+ matches.push({
5465
5508
  name: document.name,
5466
- score
5509
+ score,
5510
+ covered,
5511
+ exact: isExactName(exactQuery, document.name)
5467
5512
  });
5468
- });
5469
- return matches.sort((left, right) => right.score - left.score).slice(0, limit).map((match) => match.name);
5513
+ }
5514
+ return matches.sort((left, right) => Number(right.exact) - Number(left.exact) || right.covered - left.covered || right.score - left.score).slice(0, limit).map((match) => match.name);
5470
5515
  };
5471
5516
  const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
5472
5517
  /** Schema descriptions and property names, recursively. */
@@ -5487,13 +5532,9 @@ const schemaText = (schema, parts) => {
5487
5532
  if (Array.isArray(variants)) for (const variant of variants) schemaText(variant, parts);
5488
5533
  }
5489
5534
  };
5490
- /** Search text of a tool: its name, the name with `_` as spaces, its description, and its parameters' text. */
5535
+ /** Search text of a tool: its description and its parameters' text. The ranker reads the name from `name`. */
5491
5536
  const text$1 = (tool) => {
5492
- const parts = [
5493
- tool.name,
5494
- tool.name.replaceAll("_", " "),
5495
- tool.description
5496
- ];
5537
+ const parts = [tool.description];
5497
5538
  schemaText(tool.parameters, parts);
5498
5539
  return parts.filter((part) => part.trim() !== "").join(" ");
5499
5540
  };
@@ -9245,7 +9286,23 @@ const layer$11 = (options) => Layer.effect(Service$8, Effect.gen(function* () {
9245
9286
  concurrency: "unbounded",
9246
9287
  discard: true
9247
9288
  }).pipe(Effect.timeoutOption(STARTUP_WAIT));
9248
- return toolsOf(thread).flatMap((tool) => tool.definition.exposure === "deferred" ? [(0, tool_exports.toAikitTool)(tool.definition)] : []);
9289
+ const namespaces = new Map([...thread.servers].flatMap(([name, { config, server }]) => {
9290
+ if (server === void 0) return [];
9291
+ const text = [
9292
+ namespace$1(name),
9293
+ config.description ?? "",
9294
+ server.instructions ?? ""
9295
+ ].join(" ");
9296
+ return [...thread.names(name, server.tools.map((tool) => tool.name)).values()].map((registered) => [registered, text]);
9297
+ }));
9298
+ return toolsOf(thread).flatMap((tool) => {
9299
+ if (tool.definition.exposure !== "deferred") return [];
9300
+ const text = namespaces.get(tool.definition.name);
9301
+ return [{
9302
+ ...(0, tool_exports.toAikitTool)(tool.definition),
9303
+ ...text === void 0 ? {} : { namespace: text }
9304
+ }];
9305
+ });
9249
9306
  });
9250
9307
  const reconcile = Effect.fn("Mcp.reconcile")(function* (input) {
9251
9308
  const thread = threadOf(input.threadId, input.sessionId);
@@ -12171,7 +12228,8 @@ const toolSearchHandler = Effect.fn("ToolSearch.handler")(function* ({ query, li
12171
12228
  const deferred = yield* (yield* ToolOffer).deferred;
12172
12229
  const loaded = rank$2(query, deferred.map((tool) => ({
12173
12230
  name: tool.name,
12174
- text: text$1(tool)
12231
+ text: text$1(tool),
12232
+ namespaceText: tool.namespace
12175
12233
  })), max);
12176
12234
  const listed = loaded.map((name) => {
12177
12235
  return `- ${name}: ${(deferred.find((tool) => tool.name === name)?.description ?? "").trim().split(/\r?\n/)[0]}`;