@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 (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,
@@ -32205,6 +32205,13 @@ function agentLog(level, message, fieldsOrSessionId, sessionId) {
32205
32205
  }
32206
32206
  sendParentMessage(buildAgentLogPayload(level, message, fields, resolvedSessionId));
32207
32207
  }
32208
+ function disconnectClient(sessionId, options) {
32209
+ sendParentMessage({
32210
+ type: "disconnect_client",
32211
+ sessionId,
32212
+ reason: options?.reason
32213
+ });
32214
+ }
32208
32215
  function parseChatText(message) {
32209
32216
  if (!message || typeof message !== "object")
32210
32217
  return null;
@@ -34208,6 +34215,53 @@ function endUserTools(load = getUserHistory) {
34208
34215
  }
34209
34216
 
34210
34217
  // dist/llm/agent/config.js
34218
+ var VENDOR_CONFIG_RESERVED_KEYS = {
34219
+ openai: [
34220
+ "model",
34221
+ "messages",
34222
+ "stream",
34223
+ "stream_options",
34224
+ "tools",
34225
+ "tool_choice",
34226
+ "temperature",
34227
+ "max_tokens",
34228
+ "max_completion_tokens"
34229
+ ],
34230
+ openrouter: [
34231
+ "model",
34232
+ "messages",
34233
+ "stream",
34234
+ "stream_options",
34235
+ "tools",
34236
+ "tool_choice",
34237
+ "temperature",
34238
+ "max_tokens",
34239
+ "max_completion_tokens"
34240
+ ],
34241
+ anthropic: [
34242
+ "model",
34243
+ "messages",
34244
+ "system",
34245
+ "stream",
34246
+ "tools",
34247
+ "tool_choice",
34248
+ "temperature",
34249
+ "max_tokens",
34250
+ "output_config"
34251
+ ],
34252
+ gemini: [
34253
+ "model",
34254
+ "contents",
34255
+ "systemInstruction",
34256
+ "tools",
34257
+ "toolConfig",
34258
+ "temperature",
34259
+ "maxOutputTokens",
34260
+ "abortSignal"
34261
+ ]
34262
+ };
34263
+ var VENDOR_CONFIG_MAX_BYTES = 8 * 1024;
34264
+ var FAILED_REPLIES_MAX_CLOSE_AFTER = 20;
34211
34265
  var LLM_AGENT_CONFIG_DEFAULTS = {
34212
34266
  maxOutputTokens: 512,
34213
34267
  greetingInterruptible: false,
@@ -34228,6 +34282,7 @@ var LLM_AGENT_CONFIG_DEFAULTS = {
34228
34282
  interruptPolicy: "cancel",
34229
34283
  serviceWaitMs: 2e4,
34230
34284
  fallbackText: "Sorry, something went wrong. Please try again.",
34285
+ failedRepliesMessage: "Sorry, I can't answer right now. Please try again later.",
34231
34286
  speakChatReplies: true,
34232
34287
  toolCallLogs: "redacted",
34233
34288
  linksMode: "extract",
@@ -34505,11 +34560,44 @@ var TOP_KEYS = [
34505
34560
  "interruptPolicy",
34506
34561
  "serviceWaitMs",
34507
34562
  "fallbackText",
34563
+ "failedReplies",
34564
+ "vendorConfig",
34508
34565
  "speakChatReplies",
34509
34566
  "toolCallLogs",
34510
34567
  "knowledge",
34511
34568
  "links"
34512
34569
  ];
34570
+ function checkVendorConfig(c, raw, provider) {
34571
+ if (!isRecord(raw) || Array.isArray(raw)) {
34572
+ c.err("vendorConfig", "must be a plain object");
34573
+ return void 0;
34574
+ }
34575
+ const proto = Object.getPrototypeOf(raw);
34576
+ if (proto !== Object.prototype && proto !== null) {
34577
+ c.err("vendorConfig", "must be a plain object");
34578
+ return void 0;
34579
+ }
34580
+ let size;
34581
+ try {
34582
+ size = Buffer.byteLength(JSON.stringify(raw), "utf8");
34583
+ } catch {
34584
+ c.err("vendorConfig", "must be JSON-serializable");
34585
+ return void 0;
34586
+ }
34587
+ if (size > VENDOR_CONFIG_MAX_BYTES) {
34588
+ c.err("vendorConfig", `must be at most ${VENDOR_CONFIG_MAX_BYTES} bytes serialized`);
34589
+ return void 0;
34590
+ }
34591
+ const reserved = provider ? VENDOR_CONFIG_RESERVED_KEYS[provider] : [];
34592
+ let ok = true;
34593
+ for (const key of Object.keys(raw)) {
34594
+ if (reserved.includes(key)) {
34595
+ c.err(`vendorConfig.${key}`, `is set by the agent for provider "${provider}" and cannot be overridden`);
34596
+ ok = false;
34597
+ }
34598
+ }
34599
+ return ok ? raw : void 0;
34600
+ }
34513
34601
  function validateKnowledge(c, raw, provider, topSecret) {
34514
34602
  const k = c.object(raw, "knowledge");
34515
34603
  if (!k)
@@ -34894,6 +34982,31 @@ function validateLlmAgentConfig(input) {
34894
34982
  if (s !== void 0)
34895
34983
  config2.fallbackText = s;
34896
34984
  }
34985
+ if (root.failedReplies !== void 0) {
34986
+ const f = c.object(root.failedReplies, "failedReplies");
34987
+ if (f) {
34988
+ c.unknownKeys(f, ["closeAfter", "message"], "failedReplies");
34989
+ const n = c.number(f.closeAfter, "failedReplies.closeAfter", 1, FAILED_REPLIES_MAX_CLOSE_AFTER, true);
34990
+ const fr = {};
34991
+ if (n !== void 0)
34992
+ fr.closeAfter = n;
34993
+ if (f.message !== void 0) {
34994
+ const m = c.string(f.message, "failedReplies.message", {
34995
+ min: 1,
34996
+ max: 300
34997
+ });
34998
+ if (m !== void 0)
34999
+ fr.message = m;
35000
+ }
35001
+ if (n !== void 0)
35002
+ config2.failedReplies = fr;
35003
+ }
35004
+ }
35005
+ if (root.vendorConfig !== void 0) {
35006
+ const vc = checkVendorConfig(c, root.vendorConfig, provider);
35007
+ if (vc)
35008
+ config2.vendorConfig = vc;
35009
+ }
34897
35010
  if (root.speakChatReplies !== void 0) {
34898
35011
  const b = c.boolean(root.speakChatReplies, "speakChatReplies");
34899
35012
  if (b !== void 0)
@@ -35214,6 +35327,26 @@ var LLM_AGENT_CONFIG_JSON_SCHEMA = {
35214
35327
  maxLength: 500,
35215
35328
  default: "Sorry, something went wrong. Please try again."
35216
35329
  },
35330
+ failedReplies: {
35331
+ type: "object",
35332
+ additionalProperties: false,
35333
+ required: ["closeAfter"],
35334
+ description: "Close the call after this many failed replies in a row. Off when absent. A completed reply resets the count.",
35335
+ properties: {
35336
+ closeAfter: { type: "integer", minimum: 1, maximum: 20 },
35337
+ message: {
35338
+ type: "string",
35339
+ minLength: 1,
35340
+ maxLength: 300,
35341
+ default: "Sorry, I can't answer right now. Please try again later.",
35342
+ description: "Spoken and sent to chat on the closing reply, instead of fallbackText."
35343
+ }
35344
+ }
35345
+ },
35346
+ vendorConfig: {
35347
+ type: "object",
35348
+ 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."
35349
+ },
35217
35350
  speakChatReplies: { type: "boolean", default: true },
35218
35351
  links: {
35219
35352
  type: "object",
@@ -35939,6 +36072,8 @@ function stripThinking(text) {
35939
36072
  // dist/llm/agent/create-llm-agent.js
35940
36073
  var D = LLM_AGENT_CONFIG_DEFAULTS;
35941
36074
  var CANCEL_SETTLE_MS = 2e3;
36075
+ var FAIL_CLOSE_SPEAK_WAIT_MS = 15e3;
36076
+ var FAIL_CLOSE_REASON = "llm_failed_replies";
35942
36077
  var MAX_FILLERS_PER_TURN = 3;
35943
36078
  var MAX_TOOL_WAIT_FILLERS = 2;
35944
36079
  var TOOL_CALL_GAP_MS = 2e3;
@@ -36012,6 +36147,8 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36012
36147
  const toolMaxBytes = config2.tools?.maxResultBytes ?? D.toolMaxResultBytes;
36013
36148
  const policy = config2.interruptPolicy ?? D.interruptPolicy;
36014
36149
  const fallbackText = config2.fallbackText ?? D.fallbackText;
36150
+ const closeAfter = config2.failedReplies?.closeAfter;
36151
+ const closeMessage = config2.failedReplies?.message ?? D.failedRepliesMessage;
36015
36152
  const speakChat = config2.speakChatReplies ?? D.speakChatReplies;
36016
36153
  const useNote = config2.history?.interruptionNote ?? D.interruptionNote;
36017
36154
  const historyTurns = config2.history?.turns ?? D.historyTurns;
@@ -36153,17 +36290,18 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36153
36290
  sendReplyDelta(s, t, text);
36154
36291
  if (t.source === "voice" || speakChat) {
36155
36292
  if (interruptible) {
36156
- trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36293
+ return trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36157
36294
  turnId: t.turnId,
36158
36295
  text,
36159
36296
  sentences: t.spoken,
36160
36297
  indexes: [t.spoken.length - 1]
36161
- });
36162
- } else {
36163
- const h = announce(s.sessionId, text, replyOpts(t));
36164
- trackAnnouncement(s, h.done);
36298
+ }).done;
36165
36299
  }
36300
+ const h = announce(s.sessionId, text, replyOpts(t));
36301
+ trackAnnouncement(s, h.done);
36302
+ return h.done;
36166
36303
  }
36304
+ return Promise.resolve();
36167
36305
  }
36168
36306
  function sendFillerMessage(s, turnId, text) {
36169
36307
  sendToClient(s.sessionId, { type: "assistant_filler", turnId, text });
@@ -36542,6 +36680,7 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36542
36680
  messages,
36543
36681
  tools: registry2 ? registry2.list({ user: getSessionUser(s.sessionId) }).map((e) => e.spec) : [],
36544
36682
  ...config2.toolChoice === "required-first-round" && t.llmCalls === 1 ? { toolChoice: "required" } : {},
36683
+ ...config2.vendorConfig ? { vendorConfig: config2.vendorConfig } : {},
36545
36684
  temperature: config2.temperature,
36546
36685
  maxOutputTokens: config2.maxOutputTokens ?? D.maxOutputTokens,
36547
36686
  signal
@@ -36618,8 +36757,44 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36618
36757
  if (t.fallbackSpoken)
36619
36758
  return;
36620
36759
  t.fallbackSpoken = true;
36760
+ if (closeAfter !== void 0 && s.failedStreak + 1 >= closeAfter) {
36761
+ t.closeSpeech = deliver(s, t, closeMessage);
36762
+ return;
36763
+ }
36621
36764
  deliver(s, t, fallbackText);
36622
36765
  }
36766
+ function trackFailedReplies(s, t, summary) {
36767
+ if (closeAfter === void 0 || s.closing || s.ended)
36768
+ return false;
36769
+ if (summary.status === "completed") {
36770
+ s.failedStreak = 0;
36771
+ return false;
36772
+ }
36773
+ if (summary.status !== "failed")
36774
+ return false;
36775
+ s.failedStreak += 1;
36776
+ if (s.failedStreak < closeAfter)
36777
+ return false;
36778
+ s.closing = true;
36779
+ t.closeSpeech ??= deliver(s, t, closeMessage);
36780
+ return true;
36781
+ }
36782
+ async function closeAfterFailedReplies(s, t) {
36783
+ const started = now();
36784
+ let timer;
36785
+ await Promise.race([
36786
+ t.closeSpeech,
36787
+ new Promise((resolve) => {
36788
+ timer = setTimeout(resolve, FAIL_CLOSE_SPEAK_WAIT_MS);
36789
+ })
36790
+ ]);
36791
+ if (timer)
36792
+ clearTimeout(timer);
36793
+ if (s.ended)
36794
+ return;
36795
+ agentLog("info", "closing call after failed replies", { closeAfter, waitedMs: now() - started }, s.sessionId);
36796
+ disconnectClient(s.sessionId, { reason: FAIL_CLOSE_REASON });
36797
+ }
36623
36798
  async function ensureService(t) {
36624
36799
  if (serviceWaited || !serviceSource || serviceWaitMs <= 0)
36625
36800
  return;
@@ -36801,6 +36976,7 @@ ${t.inputText}` : t.inputText, {
36801
36976
  });
36802
36977
  }
36803
36978
  setAgentState(s.sessionId, "listening");
36979
+ const closeNow = trackFailedReplies(s, t, summary);
36804
36980
  if (hooks.onTurnEnd) {
36805
36981
  await callHook("onTurnEnd", s.sessionId, () => hooks.onTurnEnd(summary, ctx));
36806
36982
  }
@@ -36818,6 +36994,8 @@ ${t.inputText}` : t.inputText, {
36818
36994
  linksSent: t.linkUrls.size,
36819
36995
  linksDropped: t.linksDropped
36820
36996
  }, s.sessionId);
36997
+ if (closeNow)
36998
+ await closeAfterFailedReplies(s, t);
36821
36999
  }
36822
37000
  function startTurn(s, text, source, utteranceId) {
36823
37001
  const turnId = randomUUID3();
@@ -36882,7 +37060,7 @@ ${t.inputText}` : t.inputText, {
36882
37060
  if (s.active === active)
36883
37061
  s.active = void 0;
36884
37062
  const q = s.queued;
36885
- if (q && !s.ended && !s.active) {
37063
+ if (q && !s.ended && !s.closing && !s.active) {
36886
37064
  s.queued = void 0;
36887
37065
  startTurn(s, q.text, q.source, q.utteranceId);
36888
37066
  }
@@ -36890,7 +37068,7 @@ ${t.inputText}` : t.inputText, {
36890
37068
  })();
36891
37069
  }
36892
37070
  async function handleInput(s, text, source, utteranceId) {
36893
- if (s.ended)
37071
+ if (s.ended || s.closing)
36894
37072
  return;
36895
37073
  if (source === "voice" && config2.speechDuringNonInterruptible === "discard" && s.announcing > 0) {
36896
37074
  agentLog("info", "final discarded during announcement", s.sessionId);
@@ -36986,7 +37164,9 @@ ${t.inputText}` : t.inputText, {
36986
37164
  speech: [],
36987
37165
  announcing: 0,
36988
37166
  chain: Promise.resolve(),
36989
- ended: false
37167
+ ended: false,
37168
+ failedStreak: 0,
37169
+ closing: false
36990
37170
  };
36991
37171
  sessions.set(ctx.sessionId, s);
36992
37172
  const mode = config2.bargeIn?.mode ?? D.bargeInMode;
@@ -60434,6 +60614,7 @@ function createOpenAiCompatibleProvider(config2) {
60434
60614
  }
60435
60615
  }));
60436
60616
  const params = {
60617
+ ...req.vendorConfig ?? {},
60437
60618
  model: req.model,
60438
60619
  messages: toMessages(req.system, req.messages),
60439
60620
  stream: true,