@comchatx/cx-topics 0.1.7 → 0.1.8

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.
@@ -3,6 +3,51 @@ import { readFileSync } from "node:fs";
3
3
  import { join, resolve } from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
5
 
6
+ // node_modules/@comchatx/providers/src/index.ts
7
+ import { getAllBuiltinModels } from "@earendil-works/pi-ai/providers/all";
8
+
9
+ // node_modules/@comchatx/typesafe/src/index.ts
10
+ var JEV_PROVIDER = "typesafe";
11
+ var JEV_MODEL = "jev-latest";
12
+ var record = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
13
+ var GATE_CODES = /\b(membership_required|debt_owed|model_not_served|sign_in_again)\b/;
14
+ var statusOf = (message) => {
15
+ const found = /^(\d{3})\b/.exec(message) ?? /^[^()\n{}":]* error \((\d{3})\)(?::|$)/.exec(message);
16
+ return found === null ? void 0 : Number(found[1]);
17
+ };
18
+ var billed = (usage) => record(usage) && ["input", "output", "totalTokens"].some((key) => typeof usage[key] === "number" && usage[key] > 0);
19
+ function failureOf(raw) {
20
+ if (raw["stopReason"] === "aborted") return "timeout";
21
+ if (raw["stopReason"] === "length" || raw["stopReason"] === "toolUse" || billed(raw["usage"])) return "malformed";
22
+ const message = typeof raw["errorMessage"] === "string" ? raw["errorMessage"] : "";
23
+ if (/timed out/i.test(message)) return "timeout";
24
+ const status = statusOf(message);
25
+ return status === 400 || status === 413 || status === 422 || status === 403 && !GATE_CODES.test(message) ? "rejected" : "refused";
26
+ }
27
+ function read(raw, questions) {
28
+ if (!record(raw)) return { failure: "malformed" };
29
+ if (raw["stopReason"] !== "stop") return { failure: failureOf(raw) };
30
+ const given = raw["answers"];
31
+ if (!record(given)) return { failure: "malformed" };
32
+ const answers = {};
33
+ for (const key of Object.keys(questions)) {
34
+ if (!Object.hasOwn(given, key)) return { failure: "missing-answers" };
35
+ const answer = given[key];
36
+ if (!record(answer) || answer["type"] !== "bool" || typeof answer["probability"] !== "number" || !Number.isFinite(answer["probability"]) || answer["probability"] < 0 || answer["probability"] > 1) return { failure: "malformed" };
37
+ answers[key] = answer["probability"];
38
+ }
39
+ return { answers: Object.freeze(answers) };
40
+ }
41
+ async function askJev(registry, state, questions, options) {
42
+ const jev = registry.findOfType("classifier", JEV_PROVIDER, JEV_MODEL);
43
+ if (jev === void 0) return { failure: "no-model" };
44
+ try {
45
+ return read(await registry.classify(jev, { state, questions }, { maxRetries: 0, timeoutMs: options.timeoutMs, ...options.signal === void 0 ? {} : { signal: options.signal } }), questions);
46
+ } catch (error) {
47
+ return { failure: error instanceof Error && /timed out|abort/i.test(error.message) ? "timeout" : "refused" };
48
+ }
49
+ }
50
+
6
51
  // src/core/protocol.ts
7
52
  var STOPPING = /* @__PURE__ */ new Set(["refused", "timeout", "no-model"]);
8
53
  var ReviewFailure = class extends Error {
@@ -11,10 +56,10 @@ var ReviewFailure = class extends Error {
11
56
  this.kind = kind;
12
57
  }
13
58
  };
14
- var record = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
59
+ var record2 = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
15
60
  var bounded = (value, max) => typeof value === "string" && value.length <= max * 2 && value.trim() !== "" && [...value].length <= max;
16
61
  var validTitle = (value) => bounded(value, 80) && !/[\r\n]/.test(value) && value.trim().split(/\s+/).length <= 8;
17
- var pick = (value, keys) => record(value) ? Object.fromEntries(keys.map((key) => [key, value[key]])) : void 0;
62
+ var pick = (value, keys) => record2(value) ? Object.fromEntries(keys.map((key) => [key, value[key]])) : void 0;
18
63
  function list(value, max, copy) {
19
64
  if (!Array.isArray(value)) return void 0;
20
65
  const length = value.length;
@@ -23,33 +68,19 @@ function list(value, max, copy) {
23
68
  for (let i = 0; i < length; i++) items.push(copy(value[i]));
24
69
  return items;
25
70
  }
26
- var GATE_CODES = /\b(membership_required|debt_owed|model_not_served|sign_in_again)\b/;
27
- var statusOf = (message) => {
71
+ var GATE_CODES2 = /\b(membership_required|debt_owed|model_not_served|sign_in_again)\b/;
72
+ var statusOf2 = (message) => {
28
73
  const found = /^(\d{3})\b/.exec(message) ?? /^[^()\n{}":]* error \((\d{3})\)(?::|$)/.exec(message);
29
74
  return found === null ? void 0 : Number(found[1]);
30
75
  };
31
- var billed = (usage) => record(usage) && ["input", "output", "totalTokens"].some((key) => typeof usage[key] === "number" && usage[key] > 0);
32
- function failureOf(raw) {
76
+ var billed2 = (usage) => record2(usage) && ["input", "output", "totalTokens"].some((key) => typeof usage[key] === "number" && usage[key] > 0);
77
+ function failureOf2(raw) {
33
78
  if (raw.stopReason === "aborted") return "timeout";
34
- if (raw.stopReason === "length" || raw.stopReason === "toolUse" || billed(raw.usage)) return "malformed";
79
+ if (raw.stopReason === "length" || raw.stopReason === "toolUse" || billed2(raw.usage)) return "malformed";
35
80
  const message = typeof raw.errorMessage === "string" ? raw.errorMessage : "";
36
81
  if (/timed out/i.test(message)) return "timeout";
37
- const status = statusOf(message);
38
- return status === 400 || status === 413 || status === 422 || status === 403 && !GATE_CODES.test(message) ? "rejected" : "refused";
39
- }
40
- function parseAnswers(raw, questions) {
41
- if (raw === void 0) return "no-model";
42
- if (!record(raw)) return "malformed";
43
- if (raw.stopReason !== "stop") return failureOf(raw);
44
- if (!record(raw.answers)) return "malformed";
45
- const answers = {};
46
- for (const key of Object.keys(questions)) {
47
- if (!Object.hasOwn(raw.answers, key)) return "missing-answers";
48
- const answer = raw.answers[key];
49
- if (!record(answer) || answer.type !== "bool" || typeof answer.probability !== "number" || !Number.isFinite(answer.probability) || answer.probability < 0 || answer.probability > 1) return "malformed";
50
- answers[key] = answer.probability;
51
- }
52
- return Object.freeze(answers);
82
+ const status = statusOf2(message);
83
+ return status === 400 || status === 413 || status === 422 || status === 403 && !GATE_CODES2.test(message) ? "rejected" : "refused";
53
84
  }
54
85
  async function askQuestions(state, questions, ask, timeoutMs) {
55
86
  const controller = new AbortController();
@@ -61,12 +92,8 @@ async function askQuestions(state, questions, ask, timeoutMs) {
61
92
  controller.abort();
62
93
  }, timeoutMs);
63
94
  });
64
- const raw = await Promise.race([ask(state, questions, controller.signal), deadline]);
65
- try {
66
- return parseAnswers(raw, questions);
67
- } catch {
68
- return "malformed";
69
- }
95
+ const answer = await Promise.race([ask(state, questions, controller.signal), deadline]);
96
+ return "failure" in answer ? answer.failure : answer.answers;
70
97
  } catch (error) {
71
98
  return error instanceof ReviewFailure && error.kind === "timeout" ? "timeout" : "refused";
72
99
  } finally {
@@ -124,7 +151,7 @@ function copied(input) {
124
151
  return { ...envelope, latestUserMessage: cutText(envelope.latestUserMessage, ROUTING_LIMITS.latestChars), current: copyTopic(envelope.current) };
125
152
  }
126
153
  function validTopic(topic) {
127
- return record(topic) && (topic.title === void 0 || validTitle(topic.title)) && Array.isArray(topic.messages) && (topic.title !== void 0 || topic.messages.length > 0) && topic.messages.every(filled);
154
+ return record2(topic) && (topic.title === void 0 || validTitle(topic.title)) && Array.isArray(topic.messages) && (topic.title !== void 0 || topic.messages.length > 0) && topic.messages.every(filled);
128
155
  }
129
156
  function valid(input) {
130
157
  return (input.baselineDecision === "new-topic" || input.baselineDecision === "continuation") && filled(input.latestUserMessage) && validTopic(input.current) && (input.currentLength === void 0 || Number.isSafeInteger(input.currentLength) && input.currentLength >= input.current.messages.length);
@@ -213,7 +240,7 @@ function titlePrompt(request) {
213
240
  }
214
241
 
215
242
  // src/extensions/judge.ts
216
- var JEV = Object.freeze({ provider: "typesafe", model: "jev-latest" });
243
+ var JEV = Object.freeze({ provider: JEV_PROVIDER, model: JEV_MODEL });
217
244
  var VIRTUAL = "pi-virtual";
218
245
  var CALL_MS = 2e4;
219
246
  var isSellers = (entry) => entry.type === "message" && entry.message.role === "user";
@@ -254,11 +281,7 @@ function verdictOf(advisory) {
254
281
  if (advisory.status !== "available") return void 0;
255
282
  return Object.freeze(advisory.decision === "continue-current" ? { decision: "continue", stale: advisory.titleFlag === true } : { decision: "new" });
256
283
  }
257
- var asker = (ctx) => async (state, questions, signal) => {
258
- const jev = ctx.modelRegistry.findOfType("classifier", JEV.provider, JEV.model);
259
- if (jev === void 0) return void 0;
260
- return ctx.modelRegistry.classify(jev, { state, questions }, { maxRetries: 0, timeoutMs: CALL_MS, signal });
261
- };
284
+ var asker = (ctx) => (state, questions, signal) => askJev(ctx.modelRegistry, state, questions, { timeoutMs: CALL_MS, signal });
262
285
  var titler = (ctx) => async (request) => {
263
286
  const model = ctx.model;
264
287
  if (model === void 0) return { failure: "no-model" };
@@ -266,7 +289,7 @@ var titler = (ctx) => async (request) => {
266
289
  const context = { systemPrompt: TITLE_SYSTEM, messages: [{ role: "user", content: [{ type: "text", text: titlePrompt(request) }], timestamp: Date.now() }] };
267
290
  const options = { maxTokens: ROUTING_LIMITS.titleTokens, signal: AbortSignal.timeout(ROUTING_LIMITS.titleMs), cacheRetention: "none" };
268
291
  const answer = model.api === VIRTUAL ? await ctx.modelRegistry.streamSimple(model, context, options).result() : await ctx.modelRegistry.complete(model, context, options);
269
- if (answer.stopReason !== "stop") return { failure: failureOf(answer) };
292
+ if (answer.stopReason !== "stop") return { failure: failureOf2(answer) };
270
293
  return { text: answer.content.flatMap((block) => block.type === "text" ? [block.text] : []).join(" ") };
271
294
  } catch {
272
295
  return { failure: "refused" };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@comchatx/cx-topics",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Titles each piece of work, and reads quotes in context.",
5
5
  "license": "MIT",
6
6
  "author": "ComChatX",
@@ -40,11 +40,14 @@
40
40
  "jev"
41
41
  ]
42
42
  },
43
+ "dependencies": {
44
+ "@comchatx/typesafe": "^0.1.3"
45
+ },
43
46
  "peerDependencies": {
44
47
  "@earendil-works/pi-coding-agent": "*"
45
48
  },
46
49
  "devDependencies": {
47
- "@earendil-works/pi-coding-agent": "1.4.0",
50
+ "@earendil-works/pi-coding-agent": "1.5.2",
48
51
  "@types/node": "26.2.0",
49
52
  "esbuild": "0.27.2",
50
53
  "typescript": "7.0.2",