@voicethere/agent 0.33.0 → 0.33.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.
@@ -32215,31 +32215,54 @@ function parseChatText(message) {
32215
32215
  var LlmProviderError = class extends Error {
32216
32216
  code;
32217
32217
  status;
32218
- constructor(code, message, status) {
32218
+ vendorCode;
32219
+ stream;
32220
+ constructor(code, message, status, detail = {}) {
32219
32221
  super(message);
32220
32222
  this.code = code;
32221
32223
  this.status = status;
32222
32224
  this.name = "LlmProviderError";
32225
+ if (detail.vendorCode !== void 0)
32226
+ this.vendorCode = detail.vendorCode;
32227
+ if (detail.stream)
32228
+ this.stream = true;
32223
32229
  }
32224
32230
  };
32225
32231
 
32226
32232
  // dist/llm/bounded-provider.js
32233
+ function vendorCodeOf(err) {
32234
+ const body = err?.error;
32235
+ if (!body || typeof body !== "object")
32236
+ return void 0;
32237
+ const code = body.code;
32238
+ if (typeof code === "number" && Number.isFinite(code))
32239
+ return code;
32240
+ if (typeof code === "string" && code.length > 0)
32241
+ return code.slice(0, 64);
32242
+ return void 0;
32243
+ }
32227
32244
  function toLlmError(err, providerId) {
32228
32245
  if (err instanceof LlmProviderError)
32229
32246
  return err;
32230
32247
  const raw = err?.status;
32231
32248
  const status = typeof raw === "number" ? raw : void 0;
32232
- const message = `${providerId} ${status ?? "network error"}`;
32249
+ const vendorCode = vendorCodeOf(err);
32250
+ const stream = status === void 0 && vendorCode !== void 0;
32251
+ const detail = {
32252
+ ...vendorCode !== void 0 ? { vendorCode } : {},
32253
+ ...stream ? { stream } : {}
32254
+ };
32255
+ const message = `${providerId} ${status ?? (stream ? "stream error" : "network error")}`;
32233
32256
  if (status === 401 || status === 403) {
32234
- return new LlmProviderError("LLM_AUTH_FAILED", message, status);
32257
+ return new LlmProviderError("LLM_AUTH_FAILED", message, status, detail);
32235
32258
  }
32236
32259
  if (status === 429) {
32237
- return new LlmProviderError("LLM_RATE_LIMITED", message, status);
32260
+ return new LlmProviderError("LLM_RATE_LIMITED", message, status, detail);
32238
32261
  }
32239
32262
  if (status === 400 || status === 404 || status === 422) {
32240
- return new LlmProviderError("LLM_BAD_REQUEST", message, status);
32263
+ return new LlmProviderError("LLM_BAD_REQUEST", message, status, detail);
32241
32264
  }
32242
- return new LlmProviderError("LLM_PROVIDER_ERROR", message, status);
32265
+ return new LlmProviderError("LLM_PROVIDER_ERROR", message, status, detail);
32243
32266
  }
32244
32267
  function sleep(ms, signal) {
32245
32268
  return new Promise((resolve) => {
@@ -35371,14 +35394,25 @@ function buildToolRegistry(o) {
35371
35394
  }
35372
35395
 
35373
35396
  // dist/llm/agent/history.js
35397
+ var MAX_CLOSED_IDS = 256;
35374
35398
  var ConversationHistory = class {
35375
35399
  maxTurns;
35400
+ onLateWrite;
35376
35401
  turns = [];
35377
35402
  pendingNote;
35378
- constructor(maxTurns) {
35403
+ closed = /* @__PURE__ */ new Set();
35404
+ reported = /* @__PURE__ */ new Set();
35405
+ constructor(maxTurns, onLateWrite) {
35379
35406
  this.maxTurns = maxTurns;
35407
+ this.onLateWrite = onLateWrite;
35380
35408
  }
35381
35409
  addUser(text, meta2) {
35410
+ for (const t of this.turns) {
35411
+ if (this.closed.has(t.turnId))
35412
+ continue;
35413
+ closeOpenToolRound(t);
35414
+ this.markClosed(t.turnId);
35415
+ }
35382
35416
  this.turns.push({
35383
35417
  turnId: meta2?.turnId ?? `turn-${this.turns.length + 1}`,
35384
35418
  messages: [{ role: "user", text }]
@@ -35387,11 +35421,23 @@ var ConversationHistory = class {
35387
35421
  this.turns.splice(0, this.turns.length - this.maxTurns);
35388
35422
  }
35389
35423
  }
35424
+ /** False when the turn is already closed and the message was dropped. */
35390
35425
  addAssistant(msg, meta2) {
35426
+ if (this.closed.has(meta2.turnId)) {
35427
+ this.reportLate(meta2.turnId, "assistant", 1);
35428
+ return false;
35429
+ }
35391
35430
  this.turnFor(meta2.turnId).messages.push(msg);
35431
+ return true;
35392
35432
  }
35433
+ /** False when the turn is already closed and the results were dropped. */
35393
35434
  addToolResults(results, meta2) {
35435
+ if (this.closed.has(meta2.turnId)) {
35436
+ this.reportLate(meta2.turnId, "tool", results.length);
35437
+ return false;
35438
+ }
35394
35439
  this.turnFor(meta2.turnId).messages.push({ role: "tool", results });
35440
+ return true;
35395
35441
  }
35396
35442
  /**
35397
35443
  * The caller heard only `playedText` of the last assistant message of the turn.
@@ -35407,7 +35453,8 @@ var ConversationHistory = class {
35407
35453
  if (m.toolCalls && m.toolCalls.length > 0) {
35408
35454
  m.text = playedText;
35409
35455
  } else if (playedText.length === 0) {
35410
- turn.messages.splice(i, 1);
35456
+ if (turn.messages[i - 1]?.role !== "tool")
35457
+ turn.messages.splice(i, 1);
35411
35458
  } else {
35412
35459
  m.text = playedText;
35413
35460
  delete m.providerState;
@@ -35429,6 +35476,26 @@ var ConversationHistory = class {
35429
35476
  messages() {
35430
35477
  return this.turns.flatMap((t) => t.messages);
35431
35478
  }
35479
+ reportLate(turnId, kind, count2) {
35480
+ const key = `${turnId}:${kind}`;
35481
+ if (this.reported.has(key))
35482
+ return;
35483
+ this.reported.add(key);
35484
+ if (this.reported.size > MAX_CLOSED_IDS) {
35485
+ const oldest = this.reported.values().next().value;
35486
+ if (oldest !== void 0)
35487
+ this.reported.delete(oldest);
35488
+ }
35489
+ this.onLateWrite?.({ turnId, kind, count: count2 });
35490
+ }
35491
+ markClosed(turnId) {
35492
+ this.closed.add(turnId);
35493
+ if (this.closed.size > MAX_CLOSED_IDS) {
35494
+ const oldest = this.closed.values().next().value;
35495
+ if (oldest !== void 0)
35496
+ this.closed.delete(oldest);
35497
+ }
35498
+ }
35432
35499
  turnFor(turnId) {
35433
35500
  let turn = this.turns.find((t) => t.turnId === turnId);
35434
35501
  if (!turn) {
@@ -35441,6 +35508,19 @@ var ConversationHistory = class {
35441
35508
  return turn;
35442
35509
  }
35443
35510
  };
35511
+ function closeOpenToolRound(turn) {
35512
+ const msgs = turn.messages;
35513
+ while (msgs.length > 0 && msgs[msgs.length - 1].role === "tool")
35514
+ msgs.pop();
35515
+ const i = msgs.length - 1;
35516
+ const m = msgs[i];
35517
+ if (!m || m.role !== "assistant" || !m.toolCalls || m.toolCalls.length === 0)
35518
+ return;
35519
+ delete m.toolCalls;
35520
+ delete m.providerState;
35521
+ if (!m.text)
35522
+ msgs.splice(i, 1);
35523
+ }
35444
35524
 
35445
35525
  // dist/llm/agent/link-extractor.js
35446
35526
  var LINK_MARK = "\uE000";
@@ -36649,7 +36729,7 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36649
36729
  }
36650
36730
  const code = err instanceof LlmProviderError ? err.code : "LLM_TURN_ERROR";
36651
36731
  t.errorCode = code;
36652
- agentLog("error", `llm turn failed: ${code}`, s.sessionId);
36732
+ agentLog("error", `llm turn failed: ${code}`, err instanceof LlmProviderError ? providerErrorFields(err) : {}, s.sessionId);
36653
36733
  speakFallback(s, t);
36654
36734
  t.status = "failed";
36655
36735
  return summarize(t);
@@ -36898,7 +36978,7 @@ ${t.inputText}` : t.inputText, {
36898
36978
  async onSessionStart(ctx) {
36899
36979
  const s = {
36900
36980
  sessionId: ctx.sessionId,
36901
- history: new ConversationHistory(historyTurns),
36981
+ history: new ConversationHistory(historyTurns, (fields) => agentLog("info", "history late write dropped", fields, ctx.sessionId)),
36902
36982
  speech: [],
36903
36983
  announcing: 0,
36904
36984
  chain: Promise.resolve(),
@@ -36979,6 +37059,13 @@ ${t.inputText}` : t.inputText, {
36979
37059
  }
36980
37060
  };
36981
37061
  }
37062
+ function providerErrorFields(err) {
37063
+ return {
37064
+ ...err.status !== void 0 ? { status: err.status } : {},
37065
+ ...err.vendorCode !== void 0 ? { vendorCode: err.vendorCode } : {},
37066
+ ...err.stream ? { stream: true } : {}
37067
+ };
37068
+ }
36982
37069
 
36983
37070
  // node_modules/openai/internal/tslib.mjs
36984
37071
  function __classPrivateFieldSet(receiver, state2, value, kind, f) {
@@ -32215,31 +32215,54 @@ function parseChatText(message) {
32215
32215
  var LlmProviderError = class extends Error {
32216
32216
  code;
32217
32217
  status;
32218
- constructor(code, message, status) {
32218
+ vendorCode;
32219
+ stream;
32220
+ constructor(code, message, status, detail = {}) {
32219
32221
  super(message);
32220
32222
  this.code = code;
32221
32223
  this.status = status;
32222
32224
  this.name = "LlmProviderError";
32225
+ if (detail.vendorCode !== void 0)
32226
+ this.vendorCode = detail.vendorCode;
32227
+ if (detail.stream)
32228
+ this.stream = true;
32223
32229
  }
32224
32230
  };
32225
32231
 
32226
32232
  // dist/llm/bounded-provider.js
32233
+ function vendorCodeOf(err) {
32234
+ const body = err?.error;
32235
+ if (!body || typeof body !== "object")
32236
+ return void 0;
32237
+ const code = body.code;
32238
+ if (typeof code === "number" && Number.isFinite(code))
32239
+ return code;
32240
+ if (typeof code === "string" && code.length > 0)
32241
+ return code.slice(0, 64);
32242
+ return void 0;
32243
+ }
32227
32244
  function toLlmError(err, providerId) {
32228
32245
  if (err instanceof LlmProviderError)
32229
32246
  return err;
32230
32247
  const raw = err?.status;
32231
32248
  const status = typeof raw === "number" ? raw : void 0;
32232
- const message = `${providerId} ${status ?? "network error"}`;
32249
+ const vendorCode = vendorCodeOf(err);
32250
+ const stream = status === void 0 && vendorCode !== void 0;
32251
+ const detail = {
32252
+ ...vendorCode !== void 0 ? { vendorCode } : {},
32253
+ ...stream ? { stream } : {}
32254
+ };
32255
+ const message = `${providerId} ${status ?? (stream ? "stream error" : "network error")}`;
32233
32256
  if (status === 401 || status === 403) {
32234
- return new LlmProviderError("LLM_AUTH_FAILED", message, status);
32257
+ return new LlmProviderError("LLM_AUTH_FAILED", message, status, detail);
32235
32258
  }
32236
32259
  if (status === 429) {
32237
- return new LlmProviderError("LLM_RATE_LIMITED", message, status);
32260
+ return new LlmProviderError("LLM_RATE_LIMITED", message, status, detail);
32238
32261
  }
32239
32262
  if (status === 400 || status === 404 || status === 422) {
32240
- return new LlmProviderError("LLM_BAD_REQUEST", message, status);
32263
+ return new LlmProviderError("LLM_BAD_REQUEST", message, status, detail);
32241
32264
  }
32242
- return new LlmProviderError("LLM_PROVIDER_ERROR", message, status);
32265
+ return new LlmProviderError("LLM_PROVIDER_ERROR", message, status, detail);
32243
32266
  }
32244
32267
  function sleep(ms, signal) {
32245
32268
  return new Promise((resolve) => {
@@ -35371,14 +35394,25 @@ function buildToolRegistry(o) {
35371
35394
  }
35372
35395
 
35373
35396
  // dist/llm/agent/history.js
35397
+ var MAX_CLOSED_IDS = 256;
35374
35398
  var ConversationHistory = class {
35375
35399
  maxTurns;
35400
+ onLateWrite;
35376
35401
  turns = [];
35377
35402
  pendingNote;
35378
- constructor(maxTurns) {
35403
+ closed = /* @__PURE__ */ new Set();
35404
+ reported = /* @__PURE__ */ new Set();
35405
+ constructor(maxTurns, onLateWrite) {
35379
35406
  this.maxTurns = maxTurns;
35407
+ this.onLateWrite = onLateWrite;
35380
35408
  }
35381
35409
  addUser(text, meta2) {
35410
+ for (const t of this.turns) {
35411
+ if (this.closed.has(t.turnId))
35412
+ continue;
35413
+ closeOpenToolRound(t);
35414
+ this.markClosed(t.turnId);
35415
+ }
35382
35416
  this.turns.push({
35383
35417
  turnId: meta2?.turnId ?? `turn-${this.turns.length + 1}`,
35384
35418
  messages: [{ role: "user", text }]
@@ -35387,11 +35421,23 @@ var ConversationHistory = class {
35387
35421
  this.turns.splice(0, this.turns.length - this.maxTurns);
35388
35422
  }
35389
35423
  }
35424
+ /** False when the turn is already closed and the message was dropped. */
35390
35425
  addAssistant(msg, meta2) {
35426
+ if (this.closed.has(meta2.turnId)) {
35427
+ this.reportLate(meta2.turnId, "assistant", 1);
35428
+ return false;
35429
+ }
35391
35430
  this.turnFor(meta2.turnId).messages.push(msg);
35431
+ return true;
35392
35432
  }
35433
+ /** False when the turn is already closed and the results were dropped. */
35393
35434
  addToolResults(results, meta2) {
35435
+ if (this.closed.has(meta2.turnId)) {
35436
+ this.reportLate(meta2.turnId, "tool", results.length);
35437
+ return false;
35438
+ }
35394
35439
  this.turnFor(meta2.turnId).messages.push({ role: "tool", results });
35440
+ return true;
35395
35441
  }
35396
35442
  /**
35397
35443
  * The caller heard only `playedText` of the last assistant message of the turn.
@@ -35407,7 +35453,8 @@ var ConversationHistory = class {
35407
35453
  if (m.toolCalls && m.toolCalls.length > 0) {
35408
35454
  m.text = playedText;
35409
35455
  } else if (playedText.length === 0) {
35410
- turn.messages.splice(i, 1);
35456
+ if (turn.messages[i - 1]?.role !== "tool")
35457
+ turn.messages.splice(i, 1);
35411
35458
  } else {
35412
35459
  m.text = playedText;
35413
35460
  delete m.providerState;
@@ -35429,6 +35476,26 @@ var ConversationHistory = class {
35429
35476
  messages() {
35430
35477
  return this.turns.flatMap((t) => t.messages);
35431
35478
  }
35479
+ reportLate(turnId, kind, count2) {
35480
+ const key = `${turnId}:${kind}`;
35481
+ if (this.reported.has(key))
35482
+ return;
35483
+ this.reported.add(key);
35484
+ if (this.reported.size > MAX_CLOSED_IDS) {
35485
+ const oldest = this.reported.values().next().value;
35486
+ if (oldest !== void 0)
35487
+ this.reported.delete(oldest);
35488
+ }
35489
+ this.onLateWrite?.({ turnId, kind, count: count2 });
35490
+ }
35491
+ markClosed(turnId) {
35492
+ this.closed.add(turnId);
35493
+ if (this.closed.size > MAX_CLOSED_IDS) {
35494
+ const oldest = this.closed.values().next().value;
35495
+ if (oldest !== void 0)
35496
+ this.closed.delete(oldest);
35497
+ }
35498
+ }
35432
35499
  turnFor(turnId) {
35433
35500
  let turn = this.turns.find((t) => t.turnId === turnId);
35434
35501
  if (!turn) {
@@ -35441,6 +35508,19 @@ var ConversationHistory = class {
35441
35508
  return turn;
35442
35509
  }
35443
35510
  };
35511
+ function closeOpenToolRound(turn) {
35512
+ const msgs = turn.messages;
35513
+ while (msgs.length > 0 && msgs[msgs.length - 1].role === "tool")
35514
+ msgs.pop();
35515
+ const i = msgs.length - 1;
35516
+ const m = msgs[i];
35517
+ if (!m || m.role !== "assistant" || !m.toolCalls || m.toolCalls.length === 0)
35518
+ return;
35519
+ delete m.toolCalls;
35520
+ delete m.providerState;
35521
+ if (!m.text)
35522
+ msgs.splice(i, 1);
35523
+ }
35444
35524
 
35445
35525
  // dist/llm/agent/link-extractor.js
35446
35526
  var LINK_MARK = "\uE000";
@@ -36649,7 +36729,7 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36649
36729
  }
36650
36730
  const code = err instanceof LlmProviderError ? err.code : "LLM_TURN_ERROR";
36651
36731
  t.errorCode = code;
36652
- agentLog("error", `llm turn failed: ${code}`, s.sessionId);
36732
+ agentLog("error", `llm turn failed: ${code}`, err instanceof LlmProviderError ? providerErrorFields(err) : {}, s.sessionId);
36653
36733
  speakFallback(s, t);
36654
36734
  t.status = "failed";
36655
36735
  return summarize(t);
@@ -36898,7 +36978,7 @@ ${t.inputText}` : t.inputText, {
36898
36978
  async onSessionStart(ctx) {
36899
36979
  const s = {
36900
36980
  sessionId: ctx.sessionId,
36901
- history: new ConversationHistory(historyTurns),
36981
+ history: new ConversationHistory(historyTurns, (fields) => agentLog("info", "history late write dropped", fields, ctx.sessionId)),
36902
36982
  speech: [],
36903
36983
  announcing: 0,
36904
36984
  chain: Promise.resolve(),
@@ -36979,6 +37059,13 @@ ${t.inputText}` : t.inputText, {
36979
37059
  }
36980
37060
  };
36981
37061
  }
37062
+ function providerErrorFields(err) {
37063
+ return {
37064
+ ...err.status !== void 0 ? { status: err.status } : {},
37065
+ ...err.vendorCode !== void 0 ? { vendorCode: err.vendorCode } : {},
37066
+ ...err.stream ? { stream: true } : {}
37067
+ };
37068
+ }
36982
37069
 
36983
37070
  // node_modules/openai/internal/tslib.mjs
36984
37071
  function __classPrivateFieldSet(receiver, state2, value, kind, f) {
package/eject/llm/loop.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Copied from @voicethere/agent 0.33.0. Package updates no longer change this file.
1
+ // Copied from @voicethere/agent 0.33.2. Package updates no longer change this file.
2
2
  import { randomUUID } from "node:crypto";
3
3
  import {
4
4
  type AgentHandlers,
@@ -1298,7 +1298,12 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
1298
1298
  const code =
1299
1299
  err instanceof LlmProviderError ? err.code : "LLM_TURN_ERROR";
1300
1300
  t.errorCode = code;
1301
- agentLog("error", `llm turn failed: ${code}`, s.sessionId);
1301
+ agentLog(
1302
+ "error",
1303
+ `llm turn failed: ${code}`,
1304
+ err instanceof LlmProviderError ? providerErrorFields(err) : {},
1305
+ s.sessionId,
1306
+ );
1302
1307
  speakFallback(s, t);
1303
1308
  t.status = "failed";
1304
1309
  return summarize(t);
@@ -1599,7 +1604,9 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
1599
1604
  async onSessionStart(ctx: SessionContext) {
1600
1605
  const s: SessionState = {
1601
1606
  sessionId: ctx.sessionId,
1602
- history: new ConversationHistory(historyTurns),
1607
+ history: new ConversationHistory(historyTurns, (fields) =>
1608
+ agentLog("info", "history late write dropped", fields, ctx.sessionId),
1609
+ ),
1603
1610
  speech: [],
1604
1611
  announcing: 0,
1605
1612
  chain: Promise.resolve(),
@@ -1692,3 +1699,14 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
1692
1699
  },
1693
1700
  };
1694
1701
  }
1702
+
1703
+ /** Safe metadata for the `llm turn failed` row: no vendor message, no body. */
1704
+ export function providerErrorFields(
1705
+ err: LlmProviderError,
1706
+ ): Record<string, unknown> {
1707
+ return {
1708
+ ...(err.status !== undefined ? { status: err.status } : {}),
1709
+ ...(err.vendorCode !== undefined ? { vendorCode: err.vendorCode } : {}),
1710
+ ...(err.stream ? { stream: true } : {}),
1711
+ };
1712
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voicethere/agent",
3
- "version": "0.33.0",
3
+ "version": "0.33.2",
4
4
  "description": "VoiceThere customer agent SDK — IPC types and runtime helpers for sandboxed child bundles",
5
5
  "type": "module",
6
6
  "exports": {
@@ -115,7 +115,7 @@
115
115
  "build:templates": "node dist/templates/build-seed-bundles-cli.js",
116
116
  "build:example": "node dist/cli.js build --entry examples/agent.ts --outfile dist/agent.js",
117
117
  "build": "npm run build:lib && npm run build:schema && npm run build:eject && npm run build:templates && npm run build:example",
118
- "typecheck": "tsc --noEmit -p tsconfig.json && tsc --noEmit -p tsconfig.test.json",
118
+ "typecheck": "tsc --noEmit -p tsconfig.json && tsc --noEmit -p tsconfig.test.json && tsc --noEmit -p tsconfig.native-ts.json",
119
119
  "test": "vitest run",
120
120
  "test:ci": "npm run typecheck && npm run build && npm run test && node scripts/ci/verify-package-exports.mjs",
121
121
  "check:openrouter-model": "node scripts/ci/check-openrouter-default-model.mjs",