@meetsmore-oss/use-ai-server 1.9.4 → 1.10.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.
@@ -1,4 +1,4 @@
1
- import { LanguageModel } from 'ai';
1
+ import { LanguageModel, type JSONValue } from 'ai';
2
2
  import type { Agent, AgentInput, EventEmitter, AgentResult } from './types';
3
3
  import type { ToolDefinition } from '../types';
4
4
  import { type CacheBreakpointFn } from './anthropicCache';
@@ -147,6 +147,33 @@ export interface AISDKAgentConfig {
147
147
  * ```
148
148
  */
149
149
  cacheBreakpoint?: CacheBreakpointFn;
150
+ /**
151
+ * Provider-specific options passed directly to `streamText`.
152
+ * Can be used for AI Gateway features like model fallbacks, provider routing, etc.
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * // Model fallbacks via AI Gateway
157
+ * {
158
+ * providerOptions: {
159
+ * gateway: {
160
+ * models: ['anthropic/claude-opus-4.6', 'google/gemini-3.1-pro-preview'],
161
+ * },
162
+ * },
163
+ * }
164
+ *
165
+ * // Model fallbacks + provider routing
166
+ * {
167
+ * providerOptions: {
168
+ * gateway: {
169
+ * models: ['openai/gpt-5-nano', 'anthropic/claude-opus-4.6'],
170
+ * order: ['azure', 'openai'],
171
+ * },
172
+ * },
173
+ * }
174
+ * ```
175
+ */
176
+ providerOptions?: Record<string, Record<string, JSONValue>>;
150
177
  /**
151
178
  * Maximum number of tokens the model can output per response.
152
179
  * @default 4096
@@ -213,6 +240,7 @@ export interface AISDKAgentConfig {
213
240
  */
214
241
  export declare class AISDKAgent implements Agent {
215
242
  private model;
243
+ private providerOptions?;
216
244
  private name;
217
245
  private annotation?;
218
246
  private toolFilter?;
@@ -252,6 +280,16 @@ export declare class AISDKAgent implements Agent {
252
280
  * Each step runs ONE model invocation, then checks for updated tools/state.
253
281
  */
254
282
  private executeStepLoop;
283
+ /**
284
+ * Detects incomplete tool calls caused by maxOutputTokens truncation and injects
285
+ * synthetic error tool_results into ctx so the model can retry with shorter arguments.
286
+ *
287
+ * When the stream is cut mid-tool-input, tool-input-start fires but tool-call never fires.
288
+ *
289
+ * Mutates ctx.allResponseMessages and ctx.currentMessages as a side effect.
290
+ * @returns true if recovery messages were injected (caller should continue to next step)
291
+ */
292
+ private handleIncompleteToolCalls;
255
293
  /**
256
294
  * Processes a single stream chunk, emitting the appropriate AG-UI events.
257
295
  * Mutates stepCtx.stepHadToolCalls when a tool call is detected.
@@ -291,6 +329,12 @@ export declare class AISDKAgent implements Agent {
291
329
  * @returns A SystemModelMessage with the state, or undefined if no state
292
330
  */
293
331
  private buildStateMessage;
332
+ /**
333
+ * When maxSteps is exhausted mid-tool-call chain, overrides step config
334
+ * to strip tools and inject a summary prompt so the model can summarize progress.
335
+ * No-ops when isGracefulSummaryStep is false.
336
+ */
337
+ private applyGracefulSummaryOverrides;
294
338
  /**
295
339
  * Filters tools using the configured filter function.
296
340
  * If no filter is configured, returns all tools.
@@ -1 +1 @@
1
- {"version":3,"file":"AISDKAgent.d.ts","sourceRoot":"","sources":["../../../src/agents/AISDKAgent.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,aAAa,EAA2D,MAAM,IAAI,CAAC;AAKpH,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAiB,MAAM,SAAS,CAAC;AAC3F,OAAO,KAAK,EAAE,cAAc,EAAuB,MAAM,UAAU,CAAC;AA0BpE,OAAO,EAAyB,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AA8FjF;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;;;;;;;;;;;;;;;OAkBG;IACH,KAAK,EAAE,aAAa,CAAC;IAErB;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;;;;;;;;OAUG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,YAAY,CAAC,EAAE,MAA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M,CAAC,QAAQ,CAAC,iBAAiB,CAG9B;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,kBAAkB,CAG/B;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAI9B;IAEX;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,4BAA4B,CAWhD;IAEJ,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAOtC;IAEH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAM1B;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,mBAAmB,CAAqC;IAEhF;;;;;;;;OAQG;IACH,OAAO,CAAC,gBAAgB;CAgBzB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=AISDKAgent.perStepEvents.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AISDKAgent.perStepEvents.test.d.ts","sourceRoot":"","sources":["../../../src/agents/AISDKAgent.perStepEvents.test.ts"],"names":[],"mappings":""}
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export type { UseAIServerConfig, McpEndpointConfig, ToolDefinition, CorsOptions
3
3
  export type { ClientSession } from './server';
4
4
  export type { Agent, AgentInput, EventEmitter, AgentResult } from './agents';
5
5
  export { AISDKAgent, type AISDKAgentConfig, type MessageWithCacheContext, type CacheTtl, type CacheBreakpointResult, type CacheBreakpointFn } from './agents';
6
- export type { UseAIServerPlugin, MessageHandler } from './plugins';
6
+ export type { UseAIServerPlugin, MessageHandler, BeforeRunAgentResult } from './plugins';
7
7
  export { FeedbackPlugin } from './plugins';
8
8
  export type { SpanProcessor } from './instrumentation';
9
9
  export { logger } from './logger';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AACvC,YAAY,EAAE,iBAAiB,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AAS,CAAC;AACjG,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAG9C,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,KAAK,uBAAuB,EAAE,KAAK,QAAQ,EAAE,KAAK,qBAAqB,EAAE,KAAK,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAG9J,YAAY,EAAE,iBAAiB,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AACzF,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAG3C,YAAY,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAGvD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAGlC,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,KAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AAGzE,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAC3C,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAGzF,OAAO,EACL,uBAAuB,EACvB,KAAK,yBAAyB,EAC9B,KAAK,qBAAqB,EAC1B,yBAAyB,EACzB,KAAK,uBAAuB,GAC7B,MAAM,OAAO,CAAC;AAGf,OAAO,EACL,wBAAwB,EACxB,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAClB,gBAAgB,EAChB,GAAG,EACH,EAAE,EACF,GAAG,GACJ,MAAM,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -90464,16 +90464,30 @@ ${block.text}`
90464
90464
  this.sendEvent(session.socket, event);
90465
90465
  }
90466
90466
  };
90467
+ const agentInput = {
90468
+ session,
90469
+ runId,
90470
+ messages: incomingMessages,
90471
+ tools: session.tools,
90472
+ state,
90473
+ systemPrompt,
90474
+ originalInput: message.data
90475
+ };
90476
+ for (const plugin of this.plugins) {
90477
+ if (plugin.beforeRunAgent) {
90478
+ const result = await plugin.beforeRunAgent(agentInput);
90479
+ if (result?.abort) {
90480
+ this.sendEvent(session.socket, {
90481
+ type: export_EventType.RUN_ERROR,
90482
+ message: result.message,
90483
+ timestamp: Date.now()
90484
+ });
90485
+ return;
90486
+ }
90487
+ }
90488
+ }
90467
90489
  try {
90468
- await selectedAgent.run({
90469
- session,
90470
- runId,
90471
- messages: incomingMessages,
90472
- tools: session.tools,
90473
- state,
90474
- systemPrompt,
90475
- originalInput: message.data
90476
- }, eventEmitter);
90490
+ await selectedAgent.run(agentInput, eventEmitter);
90477
90491
  } finally {
90478
90492
  delete session.currentMcpHeaders;
90479
90493
  }
@@ -114134,6 +114148,7 @@ var DEFAULT_MAX_STEPS = 10;
114134
114148
 
114135
114149
  class AISDKAgent {
114136
114150
  model;
114151
+ providerOptions;
114137
114152
  name;
114138
114153
  annotation;
114139
114154
  toolFilter;
@@ -114144,6 +114159,7 @@ class AISDKAgent {
114144
114159
  maxSteps;
114145
114160
  constructor(config2) {
114146
114161
  this.model = config2.model;
114162
+ this.providerOptions = config2.providerOptions;
114147
114163
  this.name = config2.name || "ai-sdk";
114148
114164
  this.annotation = config2.annotation;
114149
114165
  this.toolFilter = config2.toolFilter;
@@ -114191,14 +114207,13 @@ class AISDKAgent {
114191
114207
  originalInput,
114192
114208
  staticSystemMessages,
114193
114209
  streamTextStarted: false,
114194
- messageId: null,
114195
- hasEmittedTextStart: false,
114196
114210
  finalText: "",
114197
114211
  currentStepNumber: 0,
114198
114212
  hasAnyContent: false,
114199
114213
  currentMessages: [...sanitizedInputMessages],
114200
114214
  allResponseMessages: [],
114201
- response: null
114215
+ response: null,
114216
+ lastStepHadToolCalls: false
114202
114217
  };
114203
114218
  }
114204
114219
  emitRunStartEvents(ctx, events) {
@@ -114245,46 +114260,52 @@ class AISDKAgent {
114245
114260
  });
114246
114261
  }
114247
114262
  async executeStepLoop(ctx, events, span) {
114248
- for (let stepIteration = 0;stepIteration < this.maxSteps; stepIteration++) {
114263
+ for (let stepIteration = 0;stepIteration <= this.maxSteps; stepIteration++) {
114264
+ const isGracefulSummaryStep = stepIteration === this.maxSteps;
114265
+ if (isGracefulSummaryStep && !ctx.lastStepHadToolCalls)
114266
+ break;
114249
114267
  const stepCtx = {
114250
114268
  currentTools: ctx.session.tools,
114251
114269
  activeToolCalls: new Map,
114252
- stepHadToolCalls: false
114270
+ completedToolCalls: new Set,
114271
+ stepHadToolCalls: false,
114272
+ messageId: null,
114273
+ hasEmittedTextStart: false,
114274
+ stepFinishReason: undefined
114253
114275
  };
114254
- logger2.debug("Starting step iteration", {
114255
- stepIteration,
114256
- toolCount: stepCtx.currentTools.length,
114257
- toolNames: stepCtx.currentTools.map((t) => t.name)
114258
- });
114259
114276
  const stateMessage = this.buildStateMessage(ctx.session.state);
114260
114277
  const messagesForStep = [
114261
114278
  ...ctx.staticSystemMessages || [],
114262
114279
  ...stateMessage ? [stateMessage] : [],
114263
114280
  ...ctx.currentMessages
114264
114281
  ];
114265
- const messagesWithCache = applyCacheBreakpoints(messagesForStep, this.cacheBreakpoint, this.model);
114282
+ const stepConfig = {
114283
+ messages: messagesForStep,
114284
+ tools: stepCtx.currentTools.length > 0 ? this.sanitizeToolsForAPI(this.filterTools(stepCtx.currentTools), ctx.session, events) : undefined,
114285
+ metadata: {
114286
+ sessionId: ctx.session.clientId,
114287
+ threadId: ctx.session.threadId,
114288
+ runId: ctx.runId,
114289
+ ipAddress: ctx.session.ipAddress,
114290
+ toolCount: stepCtx.currentTools.length,
114291
+ stepIteration,
114292
+ ...ctx.originalInput.forwardedProps?.telemetryMetadata || {}
114293
+ }
114294
+ };
114295
+ this.applyGracefulSummaryOverrides(isGracefulSummaryStep, stepConfig);
114296
+ logger2.debug("Starting step iteration", { stepIteration, ...stepConfig.metadata });
114297
+ const messagesWithCache = applyCacheBreakpoints(stepConfig.messages, this.cacheBreakpoint, this.model);
114266
114298
  ctx.streamTextStarted = true;
114267
114299
  const createStream = () => streamText({
114268
114300
  model: this.model,
114269
114301
  messages: messagesWithCache,
114270
- tools: stepCtx.currentTools.length > 0 ? this.sanitizeToolsForAPI(this.filterTools(stepCtx.currentTools), ctx.session, events) : undefined,
114302
+ tools: stepConfig.tools,
114271
114303
  stopWhen: stepCountIs(1),
114272
114304
  maxOutputTokens: this.maxOutputTokens,
114273
114305
  temperature: this.temperature,
114274
114306
  abortSignal: ctx.session.abortController?.signal,
114275
- experimental_telemetry: span.active ? {
114276
- isEnabled: true,
114277
- functionId: "use-ai",
114278
- metadata: {
114279
- sessionId: ctx.session.clientId,
114280
- threadId: ctx.session.threadId,
114281
- runId: ctx.runId,
114282
- ipAddress: ctx.session.ipAddress,
114283
- toolCount: stepCtx.currentTools.length,
114284
- stepIteration,
114285
- ...ctx.originalInput.forwardedProps?.telemetryMetadata || {}
114286
- }
114287
- } : undefined,
114307
+ providerOptions: this.providerOptions,
114308
+ experimental_telemetry: span.active ? { isEnabled: true, functionId: "use-ai", metadata: stepConfig.metadata } : undefined,
114288
114309
  onStepFinish: ({ usage, finishReason }) => {
114289
114310
  logger2.debug("Step finished", { usage, finishReason, stepIteration });
114290
114311
  }
@@ -114306,11 +114327,15 @@ class AISDKAgent {
114306
114327
  ctx.response = response;
114307
114328
  const stepMessages = this.sanitizeMessages(response.messages);
114308
114329
  ctx.allResponseMessages.push(...stepMessages);
114330
+ ctx.currentMessages = [...ctx.currentMessages, ...stepMessages];
114331
+ if (this.handleIncompleteToolCalls(ctx, stepCtx)) {
114332
+ continue;
114333
+ }
114334
+ ctx.lastStepHadToolCalls = stepCtx.stepHadToolCalls;
114309
114335
  if (!stepCtx.stepHadToolCalls) {
114310
114336
  logger2.debug("Step had no tool calls, finishing run", { stepIteration });
114311
114337
  break;
114312
114338
  }
114313
- ctx.currentMessages = [...ctx.currentMessages, ...stepMessages];
114314
114339
  logger2.debug("Continuing to next step after tool calls", {
114315
114340
  stepIteration,
114316
114341
  newMessageCount: ctx.currentMessages.length,
@@ -114318,6 +114343,20 @@ class AISDKAgent {
114318
114343
  });
114319
114344
  }
114320
114345
  }
114346
+ handleIncompleteToolCalls(ctx, stepCtx) {
114347
+ const incompleteToolCalls = [...stepCtx.activeToolCalls.entries()].filter(([id]) => !stepCtx.completedToolCalls.has(id)).map(([id, call]) => ({ id, ...call }));
114348
+ const recoveryMessages = buildRecoveryToolResults(incompleteToolCalls, stepCtx.stepFinishReason, this.maxOutputTokens);
114349
+ if (recoveryMessages.length === 0) {
114350
+ return false;
114351
+ }
114352
+ const sanitized = this.sanitizeMessages(recoveryMessages);
114353
+ ctx.allResponseMessages.push(...sanitized);
114354
+ ctx.currentMessages = [...ctx.currentMessages, ...sanitized];
114355
+ logger2.warn("Incomplete tool calls detected (likely maxOutputTokens exceeded)", {
114356
+ incompleteCount: incompleteToolCalls.length
114357
+ });
114358
+ return true;
114359
+ }
114321
114360
  processStreamChunk(chunk, ctx, stepCtx, events) {
114322
114361
  switch (chunk.type) {
114323
114362
  case "start-step": {
@@ -114330,19 +114369,19 @@ class AISDKAgent {
114330
114369
  }
114331
114370
  case "text-delta": {
114332
114371
  ctx.hasAnyContent = true;
114333
- if (!ctx.hasEmittedTextStart) {
114334
- ctx.messageId = v4_default();
114372
+ if (!stepCtx.hasEmittedTextStart) {
114373
+ stepCtx.messageId = v4_default();
114335
114374
  events.emit({
114336
114375
  type: export_EventType.TEXT_MESSAGE_START,
114337
- messageId: ctx.messageId,
114376
+ messageId: stepCtx.messageId,
114338
114377
  role: "assistant",
114339
114378
  timestamp: Date.now()
114340
114379
  });
114341
- ctx.hasEmittedTextStart = true;
114380
+ stepCtx.hasEmittedTextStart = true;
114342
114381
  }
114343
114382
  events.emit({
114344
114383
  type: export_EventType.TEXT_MESSAGE_CONTENT,
114345
- messageId: ctx.messageId,
114384
+ messageId: stepCtx.messageId,
114346
114385
  delta: chunk.text,
114347
114386
  timestamp: Date.now()
114348
114387
  });
@@ -114356,13 +114395,22 @@ class AISDKAgent {
114356
114395
  case "tool-input-start": {
114357
114396
  ctx.hasAnyContent = true;
114358
114397
  stepCtx.stepHadToolCalls = true;
114398
+ if (stepCtx.messageId) {
114399
+ events.emit({
114400
+ type: export_EventType.TEXT_MESSAGE_END,
114401
+ messageId: stepCtx.messageId,
114402
+ timestamp: Date.now()
114403
+ });
114404
+ }
114359
114405
  const toolDef = stepCtx.currentTools.find((t) => t.name === chunk.toolName);
114360
114406
  const annotations = getToolAnnotations(toolDef);
114407
+ const parentId = stepCtx.messageId ?? v4_default();
114408
+ stepCtx.messageId = null;
114361
114409
  const toolCallStartEvent = {
114362
114410
  type: export_EventType.TOOL_CALL_START,
114363
114411
  toolCallId: chunk.id,
114364
114412
  toolCallName: chunk.toolName,
114365
- parentMessageId: ctx.messageId ?? v4_default(),
114413
+ parentMessageId: parentId,
114366
114414
  timestamp: Date.now()
114367
114415
  };
114368
114416
  if (annotations) {
@@ -114387,6 +114435,7 @@ class AISDKAgent {
114387
114435
  }
114388
114436
  case "tool-call": {
114389
114437
  stepCtx.stepHadToolCalls = true;
114438
+ stepCtx.completedToolCalls.add(chunk.toolCallId);
114390
114439
  const toolCall = stepCtx.activeToolCalls.get(chunk.toolCallId);
114391
114440
  const finalArgs = JSON.stringify(chunk.input);
114392
114441
  if (toolCall && toolCall.args.length === 0) {
@@ -114417,7 +114466,19 @@ class AISDKAgent {
114417
114466
  });
114418
114467
  return;
114419
114468
  }
114469
+ case "finish": {
114470
+ stepCtx.stepFinishReason = chunk.finishReason;
114471
+ return;
114472
+ }
114420
114473
  case "finish-step": {
114474
+ if (stepCtx.messageId) {
114475
+ events.emit({
114476
+ type: export_EventType.TEXT_MESSAGE_END,
114477
+ messageId: stepCtx.messageId,
114478
+ timestamp: Date.now()
114479
+ });
114480
+ stepCtx.messageId = null;
114481
+ }
114421
114482
  events.emit({
114422
114483
  type: export_EventType.STEP_FINISHED,
114423
114484
  stepName: `step-${ctx.currentStepNumber - 1}`,
@@ -114444,13 +114505,6 @@ class AISDKAgent {
114444
114505
  }
114445
114506
  }
114446
114507
  finalizeRun(ctx, events, span) {
114447
- if (ctx.hasEmittedTextStart && ctx.messageId) {
114448
- events.emit({
114449
- type: export_EventType.TEXT_MESSAGE_END,
114450
- messageId: ctx.messageId,
114451
- timestamp: Date.now()
114452
- });
114453
- }
114454
114508
  if (!ctx.hasAnyContent) {
114455
114509
  span.endWithError("Empty response from AI");
114456
114510
  events.emit({
@@ -114548,6 +114602,13 @@ class AISDKAgent {
114548
114602
  ${JSON.stringify(state, null, 2)}`
114549
114603
  };
114550
114604
  }
114605
+ applyGracefulSummaryOverrides(isGracefulSummaryStep, stepConfig) {
114606
+ if (!isGracefulSummaryStep)
114607
+ return;
114608
+ stepConfig.messages.push({ role: "user", content: "max steps reached, summarize progress" });
114609
+ stepConfig.tools = undefined;
114610
+ Object.assign(stepConfig.metadata, { toolCount: 0, gracefulSummary: true });
114611
+ }
114551
114612
  filterTools(tools) {
114552
114613
  if (!this.toolFilter) {
114553
114614
  return tools;
@@ -114690,6 +114751,35 @@ ${val.text}`
114690
114751
  }
114691
114752
  }
114692
114753
  }
114754
+ function buildRecoveryToolResults(incompleteToolCalls, stepFinishReason, maxOutputTokens) {
114755
+ if (incompleteToolCalls.length === 0 || stepFinishReason !== "length") {
114756
+ return [];
114757
+ }
114758
+ const recoveryAssistantMessage = {
114759
+ role: "assistant",
114760
+ content: incompleteToolCalls.map((toolCall) => ({
114761
+ type: "tool-call",
114762
+ toolCallId: toolCall.id,
114763
+ toolName: toolCall.name,
114764
+ input: {}
114765
+ }))
114766
+ };
114767
+ const recoveryToolResults = incompleteToolCalls.map((toolCall) => ({
114768
+ role: "tool",
114769
+ content: [
114770
+ {
114771
+ type: "tool-result",
114772
+ toolCallId: toolCall.id,
114773
+ toolName: toolCall.name,
114774
+ output: {
114775
+ type: "text",
114776
+ value: `Error: Tool call "${toolCall.name}" failed because its arguments were cut off mid-stream by the output token limit (maxOutputTokens: ${maxOutputTokens}). This call was recorded with args={} as a placeholder — retry with shorter arguments. Truncated args (first 200 chars): ${toolCall.args.substring(0, 200)}`
114777
+ }
114778
+ }
114779
+ ]
114780
+ }));
114781
+ return [recoveryAssistantMessage, ...recoveryToolResults];
114782
+ }
114693
114783
  // ../../node_modules/.bun/zod-to-json-schema@3.25.1+27912429049419a2/node_modules/zod-to-json-schema/dist/esm/Options.js
114694
114784
  var ignoreOverride2 = Symbol("Let zodToJsonSchema decide on which parser to use");
114695
114785
  var defaultOptions2 = {
@@ -1,3 +1,3 @@
1
- export type { UseAIServerPlugin, MessageHandler } from './types';
1
+ export type { UseAIServerPlugin, MessageHandler, BeforeRunAgentResult } from './types';
2
2
  export { FeedbackPlugin } from './FeedbackPlugin';
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/plugins/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/plugins/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,iBAAiB,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AACvF,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC"}
@@ -1,4 +1,4 @@
1
- import type { ClientSession } from '../agents/types';
1
+ import type { ClientSession, AgentInput } from '../agents/types';
2
2
  import type { UseAIClientMessage } from '../types';
3
3
  /**
4
4
  * Message handler function type for plugin-registered message handlers.
@@ -7,6 +7,14 @@ import type { UseAIClientMessage } from '../types';
7
7
  * @param message - The incoming message from the client
8
8
  */
9
9
  export type MessageHandler = (session: ClientSession, message: UseAIClientMessage) => Promise<void> | void;
10
+ /**
11
+ * Result from a beforeRunAgent hook indicating the run should be aborted.
12
+ * Return this from beforeRunAgent to block the run and send the message to the client as RUN_ERROR.
13
+ */
14
+ export interface BeforeRunAgentResult {
15
+ abort: true;
16
+ message: string;
17
+ }
10
18
  /**
11
19
  * Plugin interface for extending UseAIServer functionality.
12
20
  *
@@ -65,6 +73,17 @@ export interface UseAIServerPlugin {
65
73
  * @param session - The disconnecting client session
66
74
  */
67
75
  onClientDisconnect?(session: ClientSession): void;
76
+ /**
77
+ * Optional hook called before an agent run starts.
78
+ * Use this to perform pre-run checks such as authentication, quota enforcement, etc.
79
+ *
80
+ * Return `{ abort: true, message: '...' }` to block the run — the message is sent
81
+ * to the client as a RUN_ERROR event. Return `void` (or `undefined`) to allow the run.
82
+ *
83
+ * @param input - The agent input containing session, messages, tools, and state
84
+ * @returns Promise resolving to an abort result or void
85
+ */
86
+ beforeRunAgent?(input: AgentInput): Promise<BeforeRunAgentResult | void>;
68
87
  /**
69
88
  * Optional cleanup hook called when the server is shutting down.
70
89
  * Use this to flush pending data, close connections, etc.
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/plugins/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAEnD;;;;;GAKG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,kBAAkB,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;AAE3G;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;;;OAKG;IACH,OAAO,IAAI,MAAM,CAAC;IAElB;;;;;OAKG;IACH,gBAAgB,CAAC,MAAM,EAAE;QACvB,sBAAsB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,GAAG,IAAI,CAAC;KACrE,GAAG,IAAI,CAAC;IAET;;;;OAIG;IACH,eAAe,CAAC,CAAC,OAAO,EAAE,aAA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AMG;IACI,sBAAsB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,GAAG,IAAI;IAS1E,OAAO,CAAC,mBAAmB;YAqGb,mBAAmB;YA8BnB,cAAc;IAkU5B,OAAO,CAAC,iBAAiB;IAKzB,OAAO,CAAC,gBAAgB;IAiDxB,OAAO,CAAC,0BAA0B;IAqBlC,OAAO,CAAC,cAAc;IAYtB,OAAO,CAAC,SAAS;IAMjB;;;;;;;;;;;OAWG;YACW,qBAAqB;IA+CnC;;;;;;;OAOG;IACH,OAAO,CAAC,oBAAoB;IAwC5B;;;;;OAKG;IACH,OAAO,CAAC,cAAc;IAoBtB;;;;;;OAMG;IACH,OAAO,CAAC,yBAAyB;IAYjC;;;OAGG;IACU,KAAK;CAsBnB"}
@@ -1,4 +1,4 @@
1
- import { LanguageModel } from 'ai';
1
+ import { LanguageModel, type JSONValue } from 'ai';
2
2
  import type { Agent, AgentInput, EventEmitter, AgentResult } from './types';
3
3
  import type { ToolDefinition } from '../types';
4
4
  import { type CacheBreakpointFn } from './anthropicCache';
@@ -147,6 +147,33 @@ export interface AISDKAgentConfig {
147
147
  * ```
148
148
  */
149
149
  cacheBreakpoint?: CacheBreakpointFn;
150
+ /**
151
+ * Provider-specific options passed directly to `streamText`.
152
+ * Can be used for AI Gateway features like model fallbacks, provider routing, etc.
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * // Model fallbacks via AI Gateway
157
+ * {
158
+ * providerOptions: {
159
+ * gateway: {
160
+ * models: ['anthropic/claude-opus-4.6', 'google/gemini-3.1-pro-preview'],
161
+ * },
162
+ * },
163
+ * }
164
+ *
165
+ * // Model fallbacks + provider routing
166
+ * {
167
+ * providerOptions: {
168
+ * gateway: {
169
+ * models: ['openai/gpt-5-nano', 'anthropic/claude-opus-4.6'],
170
+ * order: ['azure', 'openai'],
171
+ * },
172
+ * },
173
+ * }
174
+ * ```
175
+ */
176
+ providerOptions?: Record<string, Record<string, JSONValue>>;
150
177
  /**
151
178
  * Maximum number of tokens the model can output per response.
152
179
  * @default 4096
@@ -213,6 +240,7 @@ export interface AISDKAgentConfig {
213
240
  */
214
241
  export declare class AISDKAgent implements Agent {
215
242
  private model;
243
+ private providerOptions?;
216
244
  private name;
217
245
  private annotation?;
218
246
  private toolFilter?;
@@ -252,6 +280,16 @@ export declare class AISDKAgent implements Agent {
252
280
  * Each step runs ONE model invocation, then checks for updated tools/state.
253
281
  */
254
282
  private executeStepLoop;
283
+ /**
284
+ * Detects incomplete tool calls caused by maxOutputTokens truncation and injects
285
+ * synthetic error tool_results into ctx so the model can retry with shorter arguments.
286
+ *
287
+ * When the stream is cut mid-tool-input, tool-input-start fires but tool-call never fires.
288
+ *
289
+ * Mutates ctx.allResponseMessages and ctx.currentMessages as a side effect.
290
+ * @returns true if recovery messages were injected (caller should continue to next step)
291
+ */
292
+ private handleIncompleteToolCalls;
255
293
  /**
256
294
  * Processes a single stream chunk, emitting the appropriate AG-UI events.
257
295
  * Mutates stepCtx.stepHadToolCalls when a tool call is detected.
@@ -291,6 +329,12 @@ export declare class AISDKAgent implements Agent {
291
329
  * @returns A SystemModelMessage with the state, or undefined if no state
292
330
  */
293
331
  private buildStateMessage;
332
+ /**
333
+ * When maxSteps is exhausted mid-tool-call chain, overrides step config
334
+ * to strip tools and inject a summary prompt so the model can summarize progress.
335
+ * No-ops when isGracefulSummaryStep is false.
336
+ */
337
+ private applyGracefulSummaryOverrides;
294
338
  /**
295
339
  * Filters tools using the configured filter function.
296
340
  * If no filter is configured, returns all tools.
@@ -1 +1 @@
1
- {"version":3,"file":"AISDKAgent.d.ts","sourceRoot":"","sources":["../../../src/agents/AISDKAgent.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,aAAa,EAA2D,MAAM,IAAI,CAAC;AAKpH,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAiB,MAAM,SAAS,CAAC;AAC3F,OAAO,KAAK,EAAE,cAAc,EAAuB,MAAM,UA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BAAyB;IAgBjC;;;;;;OAMG;IACH,OAAO,CAAC,iBAAiB;IAQzB;;;;OAIG;IACH,OAAO,CAAC,6BAA6B;IAUrC;;;OAGG;IACH,OAAO,CAAC,WAAW;IAsBnB;;;;OAIG;IACH,OAAO,CAAC,qBAAqB;IAyD7B,OAAO,CAAC,mBAAmB;IAyC3B;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,uBAAuB,CAMpC;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,qBAAqB,CAKlC;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAG9B;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,kBAAkB,CAG/B;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAI9B;IAEX;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,4BAA4B,CAWhD;IAEJ,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAOtC;IAEH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAM1B;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,mBAAmB,CAAqC;IAEhF;;;;;;;;OAQG;IACH,OAAO,CAAC,gBAAgB;CAgBzB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=AISDKAgent.perStepEvents.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AISDKAgent.perStepEvents.test.d.ts","sourceRoot":"","sources":["../../../src/agents/AISDKAgent.perStepEvents.test.ts"],"names":[],"mappings":""}
@@ -3,7 +3,7 @@ export type { UseAIServerConfig, McpEndpointConfig, ToolDefinition, CorsOptions
3
3
  export type { ClientSession } from './server';
4
4
  export type { Agent, AgentInput, EventEmitter, AgentResult } from './agents';
5
5
  export { AISDKAgent, type AISDKAgentConfig, type MessageWithCacheContext, type CacheTtl, type CacheBreakpointResult, type CacheBreakpointFn } from './agents';
6
- export type { UseAIServerPlugin, MessageHandler } from './plugins';
6
+ export type { UseAIServerPlugin, MessageHandler, BeforeRunAgentResult } from './plugins';
7
7
  export { FeedbackPlugin } from './plugins';
8
8
  export type { SpanProcessor } from './instrumentation';
9
9
  export { logger } from './logger';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AACvC,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACjG,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAG9C,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,KAAK,uBAAuB,EAAE,KAAK,QAAQ,EAAE,KAAK,qBAAqB,EAAE,KAAK,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAG9J,YAAY,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAG3C,YAAY,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAGvD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAGlC,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,KAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AAGzE,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAC3C,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAGzF,OAAO,EACL,uBAAuB,EACvB,KAAK,yBAAyB,EAC9B,KAAK,qBAAqB,EAC1B,yBAAyB,EACzB,KAAK,uBAAuB,GAC7B,MAAM,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EACxB,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAClB,gBAAgB,EAChB,GAAG,EACH,EAAE,EACF,GAAG,GACJ,MAAM,SAAS,CAAC"}
@@ -1,3 +1,3 @@
1
- export type { UseAIServerPlugin, MessageHandler } from './types';
1
+ export type { UseAIServerPlugin, MessageHandler, BeforeRunAgentResult } from './types';
2
2
  export { FeedbackPlugin } from './FeedbackPlugin';
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/plugins/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/plugins/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,iBAAiB,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AACvF,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC"}