@voicethere/agent 0.34.0 → 0.35.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.
Files changed (33) hide show
  1. package/dist/llm/agent/config.d.ts +47 -0
  2. package/dist/llm/agent/config.d.ts.map +1 -1
  3. package/dist/llm/agent/config.js +128 -0
  4. package/dist/llm/agent/config.js.map +1 -1
  5. package/dist/llm/agent/create-llm-agent.d.ts +4 -0
  6. package/dist/llm/agent/create-llm-agent.d.ts.map +1 -1
  7. package/dist/llm/agent/create-llm-agent.js +63 -9
  8. package/dist/llm/agent/create-llm-agent.js.map +1 -1
  9. package/dist/llm/anthropic-provider.d.ts.map +1 -1
  10. package/dist/llm/anthropic-provider.js +1 -0
  11. package/dist/llm/anthropic-provider.js.map +1 -1
  12. package/dist/llm/gemini-provider.d.ts.map +1 -1
  13. package/dist/llm/gemini-provider.js +1 -0
  14. package/dist/llm/gemini-provider.js.map +1 -1
  15. package/dist/llm/openai-provider.d.ts.map +1 -1
  16. package/dist/llm/openai-provider.js +1 -0
  17. package/dist/llm/openai-provider.js.map +1 -1
  18. package/dist/llm/types.d.ts +6 -0
  19. package/dist/llm/types.d.ts.map +1 -1
  20. package/dist/llm/types.js.map +1 -1
  21. package/dist/templates/flow-agent/agent.js +23 -0
  22. package/dist/templates/llm-agent/agent.js +189 -8
  23. package/dist/templates/llm-board/agent.js +189 -8
  24. package/dist/templates/llm-docs-rag/agent.js +189 -8
  25. package/dist/templates/llm-language-switch/agent.js +189 -8
  26. package/dist/templates/llm-language-tutor/agent.js +189 -8
  27. package/dist/templates/llm-mcp-service/agent.js +189 -8
  28. package/dist/templates/llm-store-support/agent.js +189 -8
  29. package/dist/templates/llm-support-sticky-user/agent.js +189 -8
  30. package/eject/llm/loop.ts +79 -9
  31. package/package.json +1 -1
  32. package/schema/voicethere.llm.schema.json +24 -0
  33. package/templates/llm-agent/README.md +25 -0
@@ -32201,6 +32201,13 @@ function agentLog(level, message, fieldsOrSessionId, sessionId) {
32201
32201
  }
32202
32202
  sendParentMessage(buildAgentLogPayload(level, message, fields, resolvedSessionId));
32203
32203
  }
32204
+ function disconnectClient(sessionId, options) {
32205
+ sendParentMessage({
32206
+ type: "disconnect_client",
32207
+ sessionId,
32208
+ reason: options?.reason
32209
+ });
32210
+ }
32204
32211
  function parseChatText(message) {
32205
32212
  if (!message || typeof message !== "object")
32206
32213
  return null;
@@ -34204,6 +34211,53 @@ function endUserTools(load = getUserHistory) {
34204
34211
  }
34205
34212
 
34206
34213
  // dist/llm/agent/config.js
34214
+ var VENDOR_CONFIG_RESERVED_KEYS = {
34215
+ openai: [
34216
+ "model",
34217
+ "messages",
34218
+ "stream",
34219
+ "stream_options",
34220
+ "tools",
34221
+ "tool_choice",
34222
+ "temperature",
34223
+ "max_tokens",
34224
+ "max_completion_tokens"
34225
+ ],
34226
+ openrouter: [
34227
+ "model",
34228
+ "messages",
34229
+ "stream",
34230
+ "stream_options",
34231
+ "tools",
34232
+ "tool_choice",
34233
+ "temperature",
34234
+ "max_tokens",
34235
+ "max_completion_tokens"
34236
+ ],
34237
+ anthropic: [
34238
+ "model",
34239
+ "messages",
34240
+ "system",
34241
+ "stream",
34242
+ "tools",
34243
+ "tool_choice",
34244
+ "temperature",
34245
+ "max_tokens",
34246
+ "output_config"
34247
+ ],
34248
+ gemini: [
34249
+ "model",
34250
+ "contents",
34251
+ "systemInstruction",
34252
+ "tools",
34253
+ "toolConfig",
34254
+ "temperature",
34255
+ "maxOutputTokens",
34256
+ "abortSignal"
34257
+ ]
34258
+ };
34259
+ var VENDOR_CONFIG_MAX_BYTES = 8 * 1024;
34260
+ var FAILED_REPLIES_MAX_CLOSE_AFTER = 20;
34207
34261
  var LLM_AGENT_CONFIG_DEFAULTS = {
34208
34262
  maxOutputTokens: 512,
34209
34263
  greetingInterruptible: false,
@@ -34224,6 +34278,7 @@ var LLM_AGENT_CONFIG_DEFAULTS = {
34224
34278
  interruptPolicy: "cancel",
34225
34279
  serviceWaitMs: 2e4,
34226
34280
  fallbackText: "Sorry, something went wrong. Please try again.",
34281
+ failedRepliesMessage: "Sorry, I can't answer right now. Please try again later.",
34227
34282
  speakChatReplies: true,
34228
34283
  toolCallLogs: "redacted",
34229
34284
  linksMode: "extract",
@@ -34501,11 +34556,44 @@ var TOP_KEYS = [
34501
34556
  "interruptPolicy",
34502
34557
  "serviceWaitMs",
34503
34558
  "fallbackText",
34559
+ "failedReplies",
34560
+ "vendorConfig",
34504
34561
  "speakChatReplies",
34505
34562
  "toolCallLogs",
34506
34563
  "knowledge",
34507
34564
  "links"
34508
34565
  ];
34566
+ function checkVendorConfig(c, raw, provider) {
34567
+ if (!isRecord(raw) || Array.isArray(raw)) {
34568
+ c.err("vendorConfig", "must be a plain object");
34569
+ return void 0;
34570
+ }
34571
+ const proto = Object.getPrototypeOf(raw);
34572
+ if (proto !== Object.prototype && proto !== null) {
34573
+ c.err("vendorConfig", "must be a plain object");
34574
+ return void 0;
34575
+ }
34576
+ let size;
34577
+ try {
34578
+ size = Buffer.byteLength(JSON.stringify(raw), "utf8");
34579
+ } catch {
34580
+ c.err("vendorConfig", "must be JSON-serializable");
34581
+ return void 0;
34582
+ }
34583
+ if (size > VENDOR_CONFIG_MAX_BYTES) {
34584
+ c.err("vendorConfig", `must be at most ${VENDOR_CONFIG_MAX_BYTES} bytes serialized`);
34585
+ return void 0;
34586
+ }
34587
+ const reserved = provider ? VENDOR_CONFIG_RESERVED_KEYS[provider] : [];
34588
+ let ok = true;
34589
+ for (const key of Object.keys(raw)) {
34590
+ if (reserved.includes(key)) {
34591
+ c.err(`vendorConfig.${key}`, `is set by the agent for provider "${provider}" and cannot be overridden`);
34592
+ ok = false;
34593
+ }
34594
+ }
34595
+ return ok ? raw : void 0;
34596
+ }
34509
34597
  function validateKnowledge(c, raw, provider, topSecret) {
34510
34598
  const k = c.object(raw, "knowledge");
34511
34599
  if (!k)
@@ -34890,6 +34978,31 @@ function validateLlmAgentConfig(input) {
34890
34978
  if (s !== void 0)
34891
34979
  config2.fallbackText = s;
34892
34980
  }
34981
+ if (root.failedReplies !== void 0) {
34982
+ const f = c.object(root.failedReplies, "failedReplies");
34983
+ if (f) {
34984
+ c.unknownKeys(f, ["closeAfter", "message"], "failedReplies");
34985
+ const n = c.number(f.closeAfter, "failedReplies.closeAfter", 1, FAILED_REPLIES_MAX_CLOSE_AFTER, true);
34986
+ const fr = {};
34987
+ if (n !== void 0)
34988
+ fr.closeAfter = n;
34989
+ if (f.message !== void 0) {
34990
+ const m = c.string(f.message, "failedReplies.message", {
34991
+ min: 1,
34992
+ max: 300
34993
+ });
34994
+ if (m !== void 0)
34995
+ fr.message = m;
34996
+ }
34997
+ if (n !== void 0)
34998
+ config2.failedReplies = fr;
34999
+ }
35000
+ }
35001
+ if (root.vendorConfig !== void 0) {
35002
+ const vc = checkVendorConfig(c, root.vendorConfig, provider);
35003
+ if (vc)
35004
+ config2.vendorConfig = vc;
35005
+ }
34893
35006
  if (root.speakChatReplies !== void 0) {
34894
35007
  const b = c.boolean(root.speakChatReplies, "speakChatReplies");
34895
35008
  if (b !== void 0)
@@ -35210,6 +35323,26 @@ var LLM_AGENT_CONFIG_JSON_SCHEMA = {
35210
35323
  maxLength: 500,
35211
35324
  default: "Sorry, something went wrong. Please try again."
35212
35325
  },
35326
+ failedReplies: {
35327
+ type: "object",
35328
+ additionalProperties: false,
35329
+ required: ["closeAfter"],
35330
+ description: "Close the call after this many failed replies in a row. Off when absent. A completed reply resets the count.",
35331
+ properties: {
35332
+ closeAfter: { type: "integer", minimum: 1, maximum: 20 },
35333
+ message: {
35334
+ type: "string",
35335
+ minLength: 1,
35336
+ maxLength: 300,
35337
+ default: "Sorry, I can't answer right now. Please try again later.",
35338
+ description: "Spoken and sent to chat on the closing reply, instead of fallbackText."
35339
+ }
35340
+ }
35341
+ },
35342
+ vendorConfig: {
35343
+ type: "object",
35344
+ description: "Extra request keys for the provider's own API, merged under the fields the agent sets itself (at most 8 KB serialized). Keys the agent sets itself (model, messages or contents, system, tools, tool choice, stream, max tokens, temperature) are rejected. See the provider's API reference for the keys it accepts."
35345
+ },
35213
35346
  speakChatReplies: { type: "boolean", default: true },
35214
35347
  links: {
35215
35348
  type: "object",
@@ -35935,6 +36068,8 @@ function stripThinking(text) {
35935
36068
  // dist/llm/agent/create-llm-agent.js
35936
36069
  var D = LLM_AGENT_CONFIG_DEFAULTS;
35937
36070
  var CANCEL_SETTLE_MS = 2e3;
36071
+ var FAIL_CLOSE_SPEAK_WAIT_MS = 15e3;
36072
+ var FAIL_CLOSE_REASON = "llm_failed_replies";
35938
36073
  var MAX_FILLERS_PER_TURN = 3;
35939
36074
  var MAX_TOOL_WAIT_FILLERS = 2;
35940
36075
  var TOOL_CALL_GAP_MS = 2e3;
@@ -36008,6 +36143,8 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36008
36143
  const toolMaxBytes = config2.tools?.maxResultBytes ?? D.toolMaxResultBytes;
36009
36144
  const policy = config2.interruptPolicy ?? D.interruptPolicy;
36010
36145
  const fallbackText = config2.fallbackText ?? D.fallbackText;
36146
+ const closeAfter = config2.failedReplies?.closeAfter;
36147
+ const closeMessage = config2.failedReplies?.message ?? D.failedRepliesMessage;
36011
36148
  const speakChat = config2.speakChatReplies ?? D.speakChatReplies;
36012
36149
  const useNote = config2.history?.interruptionNote ?? D.interruptionNote;
36013
36150
  const historyTurns = config2.history?.turns ?? D.historyTurns;
@@ -36149,17 +36286,18 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36149
36286
  sendReplyDelta(s, t, text);
36150
36287
  if (t.source === "voice" || speakChat) {
36151
36288
  if (interruptible) {
36152
- trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36289
+ return trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36153
36290
  turnId: t.turnId,
36154
36291
  text,
36155
36292
  sentences: t.spoken,
36156
36293
  indexes: [t.spoken.length - 1]
36157
- });
36158
- } else {
36159
- const h = announce(s.sessionId, text, replyOpts(t));
36160
- trackAnnouncement(s, h.done);
36294
+ }).done;
36161
36295
  }
36296
+ const h = announce(s.sessionId, text, replyOpts(t));
36297
+ trackAnnouncement(s, h.done);
36298
+ return h.done;
36162
36299
  }
36300
+ return Promise.resolve();
36163
36301
  }
36164
36302
  function sendFillerMessage(s, turnId, text) {
36165
36303
  sendToClient(s.sessionId, { type: "assistant_filler", turnId, text });
@@ -36538,6 +36676,7 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36538
36676
  messages,
36539
36677
  tools: registry2 ? registry2.list({ user: getSessionUser(s.sessionId) }).map((e) => e.spec) : [],
36540
36678
  ...config2.toolChoice === "required-first-round" && t.llmCalls === 1 ? { toolChoice: "required" } : {},
36679
+ ...config2.vendorConfig ? { vendorConfig: config2.vendorConfig } : {},
36541
36680
  temperature: config2.temperature,
36542
36681
  maxOutputTokens: config2.maxOutputTokens ?? D.maxOutputTokens,
36543
36682
  signal
@@ -36614,8 +36753,44 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36614
36753
  if (t.fallbackSpoken)
36615
36754
  return;
36616
36755
  t.fallbackSpoken = true;
36756
+ if (closeAfter !== void 0 && s.failedStreak + 1 >= closeAfter) {
36757
+ t.closeSpeech = deliver(s, t, closeMessage);
36758
+ return;
36759
+ }
36617
36760
  deliver(s, t, fallbackText);
36618
36761
  }
36762
+ function trackFailedReplies(s, t, summary) {
36763
+ if (closeAfter === void 0 || s.closing || s.ended)
36764
+ return false;
36765
+ if (summary.status === "completed") {
36766
+ s.failedStreak = 0;
36767
+ return false;
36768
+ }
36769
+ if (summary.status !== "failed")
36770
+ return false;
36771
+ s.failedStreak += 1;
36772
+ if (s.failedStreak < closeAfter)
36773
+ return false;
36774
+ s.closing = true;
36775
+ t.closeSpeech ??= deliver(s, t, closeMessage);
36776
+ return true;
36777
+ }
36778
+ async function closeAfterFailedReplies(s, t) {
36779
+ const started = now();
36780
+ let timer;
36781
+ await Promise.race([
36782
+ t.closeSpeech,
36783
+ new Promise((resolve) => {
36784
+ timer = setTimeout(resolve, FAIL_CLOSE_SPEAK_WAIT_MS);
36785
+ })
36786
+ ]);
36787
+ if (timer)
36788
+ clearTimeout(timer);
36789
+ if (s.ended)
36790
+ return;
36791
+ agentLog("info", "closing call after failed replies", { closeAfter, waitedMs: now() - started }, s.sessionId);
36792
+ disconnectClient(s.sessionId, { reason: FAIL_CLOSE_REASON });
36793
+ }
36619
36794
  async function ensureService(t) {
36620
36795
  if (serviceWaited || !serviceSource || serviceWaitMs <= 0)
36621
36796
  return;
@@ -36797,6 +36972,7 @@ ${t.inputText}` : t.inputText, {
36797
36972
  });
36798
36973
  }
36799
36974
  setAgentState(s.sessionId, "listening");
36975
+ const closeNow = trackFailedReplies(s, t, summary);
36800
36976
  if (hooks.onTurnEnd) {
36801
36977
  await callHook("onTurnEnd", s.sessionId, () => hooks.onTurnEnd(summary, ctx));
36802
36978
  }
@@ -36814,6 +36990,8 @@ ${t.inputText}` : t.inputText, {
36814
36990
  linksSent: t.linkUrls.size,
36815
36991
  linksDropped: t.linksDropped
36816
36992
  }, s.sessionId);
36993
+ if (closeNow)
36994
+ await closeAfterFailedReplies(s, t);
36817
36995
  }
36818
36996
  function startTurn(s, text, source, utteranceId) {
36819
36997
  const turnId = randomUUID3();
@@ -36878,7 +37056,7 @@ ${t.inputText}` : t.inputText, {
36878
37056
  if (s.active === active)
36879
37057
  s.active = void 0;
36880
37058
  const q = s.queued;
36881
- if (q && !s.ended && !s.active) {
37059
+ if (q && !s.ended && !s.closing && !s.active) {
36882
37060
  s.queued = void 0;
36883
37061
  startTurn(s, q.text, q.source, q.utteranceId);
36884
37062
  }
@@ -36886,7 +37064,7 @@ ${t.inputText}` : t.inputText, {
36886
37064
  })();
36887
37065
  }
36888
37066
  async function handleInput(s, text, source, utteranceId) {
36889
- if (s.ended)
37067
+ if (s.ended || s.closing)
36890
37068
  return;
36891
37069
  if (source === "voice" && config2.speechDuringNonInterruptible === "discard" && s.announcing > 0) {
36892
37070
  agentLog("info", "final discarded during announcement", s.sessionId);
@@ -36982,7 +37160,9 @@ ${t.inputText}` : t.inputText, {
36982
37160
  speech: [],
36983
37161
  announcing: 0,
36984
37162
  chain: Promise.resolve(),
36985
- ended: false
37163
+ ended: false,
37164
+ failedStreak: 0,
37165
+ closing: false
36986
37166
  };
36987
37167
  sessions.set(ctx.sessionId, s);
36988
37168
  const mode = config2.bargeIn?.mode ?? D.bargeInMode;
@@ -60430,6 +60610,7 @@ function createOpenAiCompatibleProvider(config2) {
60430
60610
  }
60431
60611
  }));
60432
60612
  const params = {
60613
+ ...req.vendorConfig ?? {},
60433
60614
  model: req.model,
60434
60615
  messages: toMessages(req.system, req.messages),
60435
60616
  stream: true,
@@ -32201,6 +32201,13 @@ function agentLog(level, message, fieldsOrSessionId, sessionId) {
32201
32201
  }
32202
32202
  sendParentMessage(buildAgentLogPayload(level, message, fields, resolvedSessionId));
32203
32203
  }
32204
+ function disconnectClient(sessionId, options) {
32205
+ sendParentMessage({
32206
+ type: "disconnect_client",
32207
+ sessionId,
32208
+ reason: options?.reason
32209
+ });
32210
+ }
32204
32211
  function parseChatText(message) {
32205
32212
  if (!message || typeof message !== "object")
32206
32213
  return null;
@@ -34204,6 +34211,53 @@ function endUserTools(load = getUserHistory) {
34204
34211
  }
34205
34212
 
34206
34213
  // dist/llm/agent/config.js
34214
+ var VENDOR_CONFIG_RESERVED_KEYS = {
34215
+ openai: [
34216
+ "model",
34217
+ "messages",
34218
+ "stream",
34219
+ "stream_options",
34220
+ "tools",
34221
+ "tool_choice",
34222
+ "temperature",
34223
+ "max_tokens",
34224
+ "max_completion_tokens"
34225
+ ],
34226
+ openrouter: [
34227
+ "model",
34228
+ "messages",
34229
+ "stream",
34230
+ "stream_options",
34231
+ "tools",
34232
+ "tool_choice",
34233
+ "temperature",
34234
+ "max_tokens",
34235
+ "max_completion_tokens"
34236
+ ],
34237
+ anthropic: [
34238
+ "model",
34239
+ "messages",
34240
+ "system",
34241
+ "stream",
34242
+ "tools",
34243
+ "tool_choice",
34244
+ "temperature",
34245
+ "max_tokens",
34246
+ "output_config"
34247
+ ],
34248
+ gemini: [
34249
+ "model",
34250
+ "contents",
34251
+ "systemInstruction",
34252
+ "tools",
34253
+ "toolConfig",
34254
+ "temperature",
34255
+ "maxOutputTokens",
34256
+ "abortSignal"
34257
+ ]
34258
+ };
34259
+ var VENDOR_CONFIG_MAX_BYTES = 8 * 1024;
34260
+ var FAILED_REPLIES_MAX_CLOSE_AFTER = 20;
34207
34261
  var LLM_AGENT_CONFIG_DEFAULTS = {
34208
34262
  maxOutputTokens: 512,
34209
34263
  greetingInterruptible: false,
@@ -34224,6 +34278,7 @@ var LLM_AGENT_CONFIG_DEFAULTS = {
34224
34278
  interruptPolicy: "cancel",
34225
34279
  serviceWaitMs: 2e4,
34226
34280
  fallbackText: "Sorry, something went wrong. Please try again.",
34281
+ failedRepliesMessage: "Sorry, I can't answer right now. Please try again later.",
34227
34282
  speakChatReplies: true,
34228
34283
  toolCallLogs: "redacted",
34229
34284
  linksMode: "extract",
@@ -34501,11 +34556,44 @@ var TOP_KEYS = [
34501
34556
  "interruptPolicy",
34502
34557
  "serviceWaitMs",
34503
34558
  "fallbackText",
34559
+ "failedReplies",
34560
+ "vendorConfig",
34504
34561
  "speakChatReplies",
34505
34562
  "toolCallLogs",
34506
34563
  "knowledge",
34507
34564
  "links"
34508
34565
  ];
34566
+ function checkVendorConfig(c, raw, provider) {
34567
+ if (!isRecord(raw) || Array.isArray(raw)) {
34568
+ c.err("vendorConfig", "must be a plain object");
34569
+ return void 0;
34570
+ }
34571
+ const proto = Object.getPrototypeOf(raw);
34572
+ if (proto !== Object.prototype && proto !== null) {
34573
+ c.err("vendorConfig", "must be a plain object");
34574
+ return void 0;
34575
+ }
34576
+ let size;
34577
+ try {
34578
+ size = Buffer.byteLength(JSON.stringify(raw), "utf8");
34579
+ } catch {
34580
+ c.err("vendorConfig", "must be JSON-serializable");
34581
+ return void 0;
34582
+ }
34583
+ if (size > VENDOR_CONFIG_MAX_BYTES) {
34584
+ c.err("vendorConfig", `must be at most ${VENDOR_CONFIG_MAX_BYTES} bytes serialized`);
34585
+ return void 0;
34586
+ }
34587
+ const reserved = provider ? VENDOR_CONFIG_RESERVED_KEYS[provider] : [];
34588
+ let ok = true;
34589
+ for (const key of Object.keys(raw)) {
34590
+ if (reserved.includes(key)) {
34591
+ c.err(`vendorConfig.${key}`, `is set by the agent for provider "${provider}" and cannot be overridden`);
34592
+ ok = false;
34593
+ }
34594
+ }
34595
+ return ok ? raw : void 0;
34596
+ }
34509
34597
  function validateKnowledge(c, raw, provider, topSecret) {
34510
34598
  const k = c.object(raw, "knowledge");
34511
34599
  if (!k)
@@ -34890,6 +34978,31 @@ function validateLlmAgentConfig(input) {
34890
34978
  if (s !== void 0)
34891
34979
  config2.fallbackText = s;
34892
34980
  }
34981
+ if (root.failedReplies !== void 0) {
34982
+ const f = c.object(root.failedReplies, "failedReplies");
34983
+ if (f) {
34984
+ c.unknownKeys(f, ["closeAfter", "message"], "failedReplies");
34985
+ const n = c.number(f.closeAfter, "failedReplies.closeAfter", 1, FAILED_REPLIES_MAX_CLOSE_AFTER, true);
34986
+ const fr = {};
34987
+ if (n !== void 0)
34988
+ fr.closeAfter = n;
34989
+ if (f.message !== void 0) {
34990
+ const m = c.string(f.message, "failedReplies.message", {
34991
+ min: 1,
34992
+ max: 300
34993
+ });
34994
+ if (m !== void 0)
34995
+ fr.message = m;
34996
+ }
34997
+ if (n !== void 0)
34998
+ config2.failedReplies = fr;
34999
+ }
35000
+ }
35001
+ if (root.vendorConfig !== void 0) {
35002
+ const vc = checkVendorConfig(c, root.vendorConfig, provider);
35003
+ if (vc)
35004
+ config2.vendorConfig = vc;
35005
+ }
34893
35006
  if (root.speakChatReplies !== void 0) {
34894
35007
  const b = c.boolean(root.speakChatReplies, "speakChatReplies");
34895
35008
  if (b !== void 0)
@@ -35210,6 +35323,26 @@ var LLM_AGENT_CONFIG_JSON_SCHEMA = {
35210
35323
  maxLength: 500,
35211
35324
  default: "Sorry, something went wrong. Please try again."
35212
35325
  },
35326
+ failedReplies: {
35327
+ type: "object",
35328
+ additionalProperties: false,
35329
+ required: ["closeAfter"],
35330
+ description: "Close the call after this many failed replies in a row. Off when absent. A completed reply resets the count.",
35331
+ properties: {
35332
+ closeAfter: { type: "integer", minimum: 1, maximum: 20 },
35333
+ message: {
35334
+ type: "string",
35335
+ minLength: 1,
35336
+ maxLength: 300,
35337
+ default: "Sorry, I can't answer right now. Please try again later.",
35338
+ description: "Spoken and sent to chat on the closing reply, instead of fallbackText."
35339
+ }
35340
+ }
35341
+ },
35342
+ vendorConfig: {
35343
+ type: "object",
35344
+ description: "Extra request keys for the provider's own API, merged under the fields the agent sets itself (at most 8 KB serialized). Keys the agent sets itself (model, messages or contents, system, tools, tool choice, stream, max tokens, temperature) are rejected. See the provider's API reference for the keys it accepts."
35345
+ },
35213
35346
  speakChatReplies: { type: "boolean", default: true },
35214
35347
  links: {
35215
35348
  type: "object",
@@ -35935,6 +36068,8 @@ function stripThinking(text) {
35935
36068
  // dist/llm/agent/create-llm-agent.js
35936
36069
  var D = LLM_AGENT_CONFIG_DEFAULTS;
35937
36070
  var CANCEL_SETTLE_MS = 2e3;
36071
+ var FAIL_CLOSE_SPEAK_WAIT_MS = 15e3;
36072
+ var FAIL_CLOSE_REASON = "llm_failed_replies";
35938
36073
  var MAX_FILLERS_PER_TURN = 3;
35939
36074
  var MAX_TOOL_WAIT_FILLERS = 2;
35940
36075
  var TOOL_CALL_GAP_MS = 2e3;
@@ -36008,6 +36143,8 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36008
36143
  const toolMaxBytes = config2.tools?.maxResultBytes ?? D.toolMaxResultBytes;
36009
36144
  const policy = config2.interruptPolicy ?? D.interruptPolicy;
36010
36145
  const fallbackText = config2.fallbackText ?? D.fallbackText;
36146
+ const closeAfter = config2.failedReplies?.closeAfter;
36147
+ const closeMessage = config2.failedReplies?.message ?? D.failedRepliesMessage;
36011
36148
  const speakChat = config2.speakChatReplies ?? D.speakChatReplies;
36012
36149
  const useNote = config2.history?.interruptionNote ?? D.interruptionNote;
36013
36150
  const historyTurns = config2.history?.turns ?? D.historyTurns;
@@ -36149,17 +36286,18 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36149
36286
  sendReplyDelta(s, t, text);
36150
36287
  if (t.source === "voice" || speakChat) {
36151
36288
  if (interruptible) {
36152
- trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36289
+ return trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36153
36290
  turnId: t.turnId,
36154
36291
  text,
36155
36292
  sentences: t.spoken,
36156
36293
  indexes: [t.spoken.length - 1]
36157
- });
36158
- } else {
36159
- const h = announce(s.sessionId, text, replyOpts(t));
36160
- trackAnnouncement(s, h.done);
36294
+ }).done;
36161
36295
  }
36296
+ const h = announce(s.sessionId, text, replyOpts(t));
36297
+ trackAnnouncement(s, h.done);
36298
+ return h.done;
36162
36299
  }
36300
+ return Promise.resolve();
36163
36301
  }
36164
36302
  function sendFillerMessage(s, turnId, text) {
36165
36303
  sendToClient(s.sessionId, { type: "assistant_filler", turnId, text });
@@ -36538,6 +36676,7 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36538
36676
  messages,
36539
36677
  tools: registry2 ? registry2.list({ user: getSessionUser(s.sessionId) }).map((e) => e.spec) : [],
36540
36678
  ...config2.toolChoice === "required-first-round" && t.llmCalls === 1 ? { toolChoice: "required" } : {},
36679
+ ...config2.vendorConfig ? { vendorConfig: config2.vendorConfig } : {},
36541
36680
  temperature: config2.temperature,
36542
36681
  maxOutputTokens: config2.maxOutputTokens ?? D.maxOutputTokens,
36543
36682
  signal
@@ -36614,8 +36753,44 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36614
36753
  if (t.fallbackSpoken)
36615
36754
  return;
36616
36755
  t.fallbackSpoken = true;
36756
+ if (closeAfter !== void 0 && s.failedStreak + 1 >= closeAfter) {
36757
+ t.closeSpeech = deliver(s, t, closeMessage);
36758
+ return;
36759
+ }
36617
36760
  deliver(s, t, fallbackText);
36618
36761
  }
36762
+ function trackFailedReplies(s, t, summary) {
36763
+ if (closeAfter === void 0 || s.closing || s.ended)
36764
+ return false;
36765
+ if (summary.status === "completed") {
36766
+ s.failedStreak = 0;
36767
+ return false;
36768
+ }
36769
+ if (summary.status !== "failed")
36770
+ return false;
36771
+ s.failedStreak += 1;
36772
+ if (s.failedStreak < closeAfter)
36773
+ return false;
36774
+ s.closing = true;
36775
+ t.closeSpeech ??= deliver(s, t, closeMessage);
36776
+ return true;
36777
+ }
36778
+ async function closeAfterFailedReplies(s, t) {
36779
+ const started = now();
36780
+ let timer;
36781
+ await Promise.race([
36782
+ t.closeSpeech,
36783
+ new Promise((resolve) => {
36784
+ timer = setTimeout(resolve, FAIL_CLOSE_SPEAK_WAIT_MS);
36785
+ })
36786
+ ]);
36787
+ if (timer)
36788
+ clearTimeout(timer);
36789
+ if (s.ended)
36790
+ return;
36791
+ agentLog("info", "closing call after failed replies", { closeAfter, waitedMs: now() - started }, s.sessionId);
36792
+ disconnectClient(s.sessionId, { reason: FAIL_CLOSE_REASON });
36793
+ }
36619
36794
  async function ensureService(t) {
36620
36795
  if (serviceWaited || !serviceSource || serviceWaitMs <= 0)
36621
36796
  return;
@@ -36797,6 +36972,7 @@ ${t.inputText}` : t.inputText, {
36797
36972
  });
36798
36973
  }
36799
36974
  setAgentState(s.sessionId, "listening");
36975
+ const closeNow = trackFailedReplies(s, t, summary);
36800
36976
  if (hooks2.onTurnEnd) {
36801
36977
  await callHook("onTurnEnd", s.sessionId, () => hooks2.onTurnEnd(summary, ctx));
36802
36978
  }
@@ -36814,6 +36990,8 @@ ${t.inputText}` : t.inputText, {
36814
36990
  linksSent: t.linkUrls.size,
36815
36991
  linksDropped: t.linksDropped
36816
36992
  }, s.sessionId);
36993
+ if (closeNow)
36994
+ await closeAfterFailedReplies(s, t);
36817
36995
  }
36818
36996
  function startTurn(s, text, source, utteranceId) {
36819
36997
  const turnId = randomUUID3();
@@ -36878,7 +37056,7 @@ ${t.inputText}` : t.inputText, {
36878
37056
  if (s.active === active)
36879
37057
  s.active = void 0;
36880
37058
  const q = s.queued;
36881
- if (q && !s.ended && !s.active) {
37059
+ if (q && !s.ended && !s.closing && !s.active) {
36882
37060
  s.queued = void 0;
36883
37061
  startTurn(s, q.text, q.source, q.utteranceId);
36884
37062
  }
@@ -36886,7 +37064,7 @@ ${t.inputText}` : t.inputText, {
36886
37064
  })();
36887
37065
  }
36888
37066
  async function handleInput(s, text, source, utteranceId) {
36889
- if (s.ended)
37067
+ if (s.ended || s.closing)
36890
37068
  return;
36891
37069
  if (source === "voice" && config2.speechDuringNonInterruptible === "discard" && s.announcing > 0) {
36892
37070
  agentLog("info", "final discarded during announcement", s.sessionId);
@@ -36982,7 +37160,9 @@ ${t.inputText}` : t.inputText, {
36982
37160
  speech: [],
36983
37161
  announcing: 0,
36984
37162
  chain: Promise.resolve(),
36985
- ended: false
37163
+ ended: false,
37164
+ failedStreak: 0,
37165
+ closing: false
36986
37166
  };
36987
37167
  sessions.set(ctx.sessionId, s);
36988
37168
  const mode = config2.bargeIn?.mode ?? D.bargeInMode;
@@ -60430,6 +60610,7 @@ function createOpenAiCompatibleProvider(config2) {
60430
60610
  }
60431
60611
  }));
60432
60612
  const params = {
60613
+ ...req.vendorConfig ?? {},
60433
60614
  model: req.model,
60434
60615
  messages: toMessages(req.system, req.messages),
60435
60616
  stream: true,