@statelyai/agent 2.0.0-alpha.32 → 2.0.0-alpha.34

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 (66) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/dist/ai-sdk.cjs +114 -63
  3. package/dist/ai-sdk.d.cts +11 -2
  4. package/dist/ai-sdk.d.mts +11 -2
  5. package/dist/ai-sdk.mjs +115 -64
  6. package/dist/{decision-BPdEaxPf.cjs → decision-BEsATcr1.cjs} +18 -2
  7. package/dist/{decision-CV7WiV5C.mjs → decision-COkJNjOW.mjs} +18 -2
  8. package/dist/{event-log-CKWeC7H-.cjs → event-log-BWSSLUff.cjs} +139 -10
  9. package/dist/{event-log-DAqsalT1.mjs → event-log-C7jeH0y8.mjs} +123 -12
  10. package/dist/{event-log-store-C2NjVlWU.d.mts → event-log-store-BzXehP7R.d.cts} +28 -3
  11. package/dist/{event-log-store-s73r1Hxf.d.cts → event-log-store-CWjN6lmM.d.mts} +28 -3
  12. package/dist/{event-log-store-DKyE7JKr.cjs → event-log-store-CleWpsp5.cjs} +1 -1
  13. package/dist/{event-log-store-BGehvVTb.mjs → event-log-store-W49TCJXi.mjs} +1 -1
  14. package/dist/index.cjs +5 -5
  15. package/dist/index.d.cts +7 -7
  16. package/dist/index.d.mts +7 -7
  17. package/dist/index.mjs +5 -5
  18. package/dist/log.cjs +2 -2
  19. package/dist/log.d.cts +1 -1
  20. package/dist/log.d.mts +1 -1
  21. package/dist/log.mjs +2 -2
  22. package/dist/machines.cjs +1 -1
  23. package/dist/machines.mjs +1 -1
  24. package/dist/{model-source-BilDEyqk.mjs → model-source-CK3F4YuU.mjs} +1 -1
  25. package/dist/{model-source-BQ97_l1-.cjs → model-source-DmhTMK39.cjs} +1 -1
  26. package/dist/openai.cjs +377 -95
  27. package/dist/openai.d.cts +55 -20
  28. package/dist/openai.d.mts +55 -20
  29. package/dist/openai.mjs +375 -96
  30. package/dist/otel.d.cts +1 -1
  31. package/dist/otel.d.mts +1 -1
  32. package/dist/{run-agent-B5QhabdY.d.cts → run-agent-B1-Eqviq.d.cts} +13 -5
  33. package/dist/{run-agent-Atu-wEk7.d.mts → run-agent-BGdx5913.d.mts} +13 -5
  34. package/dist/{run-agent-2UnSUhDb.mjs → run-agent-Ct_GZw_f.mjs} +124 -38
  35. package/dist/{run-agent-Cg0xd3K-.cjs → run-agent-s5D32w99.cjs} +123 -37
  36. package/dist/{setup-agent-CmhB-U5P.mjs → setup-agent-05uw4dPZ.mjs} +2 -2
  37. package/dist/{setup-agent-BiF1N3td.d.cts → setup-agent-BtfeFfF6.d.cts} +2 -2
  38. package/dist/{setup-agent-J9f_Wu5T.d.mts → setup-agent-C6nqzv_X.d.mts} +2 -2
  39. package/dist/{setup-agent-CwUJyqKB.cjs → setup-agent-CXEBURdo.cjs} +2 -2
  40. package/dist/tanstack-ai.cjs +91 -59
  41. package/dist/tanstack-ai.d.cts +13 -2
  42. package/dist/tanstack-ai.d.mts +13 -2
  43. package/dist/tanstack-ai.mjs +91 -59
  44. package/dist/testing.cjs +1026 -10
  45. package/dist/testing.d.cts +585 -5
  46. package/dist/testing.d.mts +585 -5
  47. package/dist/testing.mjs +1018 -11
  48. package/dist/{text-logic-CYP502VT.d.cts → text-logic-CV_4KGr6.d.cts} +28 -3
  49. package/dist/{text-logic-DuK9HIFM.d.mts → text-logic-Ckh-rJFV.d.mts} +28 -3
  50. package/dist/{usage-D7JKAxLc.d.mts → usage-BDiJKyv9.d.cts} +11 -1
  51. package/dist/{usage-DVw-646x.d.cts → usage-DOoVUGVq.d.mts} +11 -1
  52. package/dist/validate.cjs +3 -3
  53. package/dist/validate.d.cts +2 -2
  54. package/dist/validate.d.mts +2 -2
  55. package/dist/validate.mjs +3 -3
  56. package/dist/{verify-Dk3Vwnha.d.cts → verify-BQpE8hUz.d.cts} +1 -1
  57. package/dist/{verify-CoCvR1eM.mjs → verify-Bpahmr34.mjs} +14 -4
  58. package/dist/{verify-BVz4cURF.d.mts → verify-CqeuEmIk.d.mts} +1 -1
  59. package/dist/{verify-D_llRCTr.cjs → verify-DCLZa9R0.cjs} +19 -3
  60. package/dist/with-model-calls-BXYdBdcu.cjs +45 -0
  61. package/dist/with-model-calls-Bv4V4EHC.mjs +34 -0
  62. package/package.json +5 -5
  63. package/readme.md +14 -13
  64. package/skills/add-evals/SKILL.md +8 -2
  65. package/skills/improve-machine/SKILL.md +1 -1
  66. package/skills/loop-to-machine/SKILL.md +40 -2
@@ -1,5 +1,5 @@
1
1
  import { i as AgentToolChoice, n as AgentMessage, s as AgentTools, u as ChosenEvent } from "./types-Dp5_Ru4R.mjs";
2
- import { N as AgentDecisionRequest, W as AgentEventDescriptor, c as AgentRequestExecutorInfo, f as AgentTextRequest, r as AgentFinishReason, t as AgentCallUsage } from "./text-logic-DuK9HIFM.mjs";
2
+ import { G as AgentEventDescriptor, N as AgentDecisionRejection, P as AgentDecisionRequest, c as AgentRequestExecutorInfo, f as AgentTextRequest, r as AgentFinishReason, t as AgentCallUsage } from "./text-logic-Ckh-rJFV.mjs";
3
3
  import { AnyStateMachine } from "xstate";
4
4
  import { AnyTextAdapter, ModelMessage, StreamChunk, TokenUsage, Tool } from "@tanstack/ai";
5
5
  import { TextActivityOptions } from "@tanstack/ai/adapters";
@@ -72,6 +72,8 @@ export type TanStackAiToolCall = {
72
72
  toolCallId: string;
73
73
  toolName: string;
74
74
  input: unknown;
75
+ /** The arguments were not JSON; `input` is their raw text. */
76
+ invalid?: true;
75
77
  };
76
78
  /**
77
79
  * Per-call `chat()` options a host can apply on top of what the machine asked
@@ -132,6 +134,8 @@ export type TanStackAiGenerateResult = {
132
134
  usage?: AgentCallUsage;
133
135
  /** Why the run stopped, normalized to the portable {@link AgentFinishReason}. */
134
136
  finishReason: AgentFinishReason;
137
+ /** Model turns the run made: one per tool-loop iteration. */
138
+ modelCalls: number;
135
139
  /** Every tool call the model made during the run. */
136
140
  toolCalls: TanStackAiToolCall[];
137
141
  /** The run's response messages — each turn's assistant text and tool
@@ -148,14 +152,21 @@ export type TanStackAiStreamResult = {
148
152
  result: string;
149
153
  usage?: AgentCallUsage;
150
154
  finishReason: AgentFinishReason;
155
+ /** Model turns the run made, as on {@link TanStackAiGenerateResult}. */
156
+ modelCalls: number;
151
157
  /** The run's response messages, as on {@link TanStackAiGenerateResult}. */
152
158
  messages: AgentMessage[];
153
159
  raw: StreamChunk[];
154
160
  };
155
161
  /** Raw result shape from {@link TanStackAiExecutors.decide} — the chosen event
156
162
  * plus the call metadata, delivered per decision attempt to `onResult`. */
157
- export type TanStackAiDecideResult = {
163
+ export type TanStackAiDecideResult = ({
158
164
  event: ChosenEvent;
165
+ rejected?: undefined;
166
+ } | {
167
+ event?: undefined;
168
+ rejected: AgentDecisionRejection;
169
+ }) & {
159
170
  usage?: AgentCallUsage;
160
171
  finishReason: AgentFinishReason;
161
172
  raw: StreamChunk[];
@@ -1,6 +1,7 @@
1
- import { L as parseProviderOutput, R as providerOutputSchema, X as getJsonSchemaSync, Y as getJsonSchema, dt as AgentTruncatedError, j as getAgentOutputMode, tt as isStandardSchema, w as AGENT_USAGE_TOKEN_FIELDS } from "./event-log-DAqsalT1.mjs";
2
- import { c as renderDecisionAttempts } from "./decision-CV7WiV5C.mjs";
3
- import { t as resolveModelSource } from "./model-source-BilDEyqk.mjs";
1
+ import { B as providerOutputSchema, M as getAgentOutputMode, Q as getJsonSchemaSync, T as AGENT_USAGE_TOKEN_FIELDS, Z as getJsonSchema, mt as AgentTruncatedError, rt as isStandardSchema, z as parseProviderOutput } from "./event-log-C7jeH0y8.mjs";
2
+ import { c as renderDecisionAttempts } from "./decision-COkJNjOW.mjs";
3
+ import { t as resolveModelSource } from "./model-source-CK3F4YuU.mjs";
4
+ import { n as rejectedAnswer, t as withModelCalls } from "./with-model-calls-Bv4V4EHC.mjs";
4
5
  import { chat, maxIterations } from "@tanstack/ai";
5
6
  //#region src/tanstack-ai/index.ts
6
7
  /**
@@ -323,6 +324,7 @@ async function readRun(stream, onDelta) {
323
324
  structured: void 0,
324
325
  usage: void 0,
325
326
  finishReason: "other",
327
+ modelCalls: 0,
326
328
  toolCalls: [],
327
329
  messages: [],
328
330
  chunks: []
@@ -348,66 +350,78 @@ async function readRun(stream, onDelta) {
348
350
  flushResults();
349
351
  return assistant ??= [];
350
352
  };
351
- for await (const chunk of stream) {
352
- summary.chunks.push(chunk);
353
- switch (chunk.type) {
354
- case "TEXT_MESSAGE_CONTENT": {
355
- summary.text += chunk.delta;
356
- onDelta?.(chunk.delta);
357
- const parts = assistantParts();
358
- const last = parts.at(-1);
359
- if (last?.type === "text") last.text += chunk.delta;
360
- else parts.push({
361
- type: "text",
362
- text: chunk.delta
363
- });
364
- break;
365
- }
366
- case "TOOL_CALL_START": {
367
- const part = {
368
- type: "tool-call",
369
- toolCallId: chunk.toolCallId,
370
- toolName: chunk.toolCallName,
371
- input: {}
372
- };
373
- assistantParts().push(part);
374
- args.set(chunk.toolCallId, {
375
- part,
376
- json: ""
377
- });
378
- break;
379
- }
380
- case "TOOL_CALL_ARGS": {
381
- const call = args.get(chunk.toolCallId);
382
- if (call) call.json += chunk.delta;
383
- break;
353
+ try {
354
+ for await (const chunk of stream) {
355
+ summary.chunks.push(chunk);
356
+ switch (chunk.type) {
357
+ case "TEXT_MESSAGE_CONTENT": {
358
+ summary.text += chunk.delta;
359
+ onDelta?.(chunk.delta);
360
+ const parts = assistantParts();
361
+ const last = parts.at(-1);
362
+ if (last?.type === "text") last.text += chunk.delta;
363
+ else parts.push({
364
+ type: "text",
365
+ text: chunk.delta
366
+ });
367
+ break;
368
+ }
369
+ case "TOOL_CALL_START": {
370
+ const part = {
371
+ type: "tool-call",
372
+ toolCallId: chunk.toolCallId,
373
+ toolName: chunk.toolCallName,
374
+ input: {}
375
+ };
376
+ assistantParts().push(part);
377
+ args.set(chunk.toolCallId, {
378
+ part,
379
+ json: ""
380
+ });
381
+ break;
382
+ }
383
+ case "TOOL_CALL_ARGS": {
384
+ const call = args.get(chunk.toolCallId);
385
+ if (call) call.json += chunk.delta;
386
+ break;
387
+ }
388
+ case "TOOL_CALL_RESULT":
389
+ flushAssistant();
390
+ (results ??= []).push({
391
+ type: "tool-result",
392
+ toolCallId: chunk.toolCallId,
393
+ toolName: args.get(chunk.toolCallId)?.part.toolName ?? "",
394
+ output: toToolResultOutput(chunk.content)
395
+ });
396
+ break;
397
+ case "CUSTOM":
398
+ if (chunk.name === "structured-output.complete") summary.structured = chunk.value?.object;
399
+ break;
400
+ case "RUN_FINISHED":
401
+ summary.finishReason = toAgentFinishReason(chunk.finishReason ?? (chunk.metadata?.tanstack)?.finishReason);
402
+ summary.modelCalls++;
403
+ if (chunk.usage && !Array.isArray(chunk.usage)) summary.usage = addUsage(summary.usage, toAgentCallUsage(chunk.usage));
404
+ break;
405
+ case "RUN_ERROR": throw new TanStackAiRunError(chunk.message, chunk.code ?? chunk.error?.code);
384
406
  }
385
- case "TOOL_CALL_RESULT":
386
- flushAssistant();
387
- (results ??= []).push({
388
- type: "tool-result",
389
- toolCallId: chunk.toolCallId,
390
- toolName: args.get(chunk.toolCallId)?.part.toolName ?? "",
391
- output: toToolResultOutput(chunk.content)
392
- });
393
- break;
394
- case "CUSTOM":
395
- if (chunk.name === "structured-output.complete") summary.structured = chunk.value?.object;
396
- break;
397
- case "RUN_FINISHED":
398
- summary.finishReason = toAgentFinishReason(chunk.finishReason ?? (chunk.metadata?.tanstack)?.finishReason);
399
- if (chunk.usage && !Array.isArray(chunk.usage)) summary.usage = addUsage(summary.usage, toAgentCallUsage(chunk.usage));
400
- break;
401
- case "RUN_ERROR": throw new TanStackAiRunError(chunk.message, chunk.code ?? chunk.error?.code);
402
407
  }
408
+ } catch (error) {
409
+ throw withModelCalls(error, summary.modelCalls + 1);
410
+ }
411
+ const invalid = /* @__PURE__ */ new Set();
412
+ for (const { part, json } of args.values()) try {
413
+ part.input = json ? JSON.parse(json) : {};
414
+ } catch {
415
+ part.input = json;
416
+ invalid.add(part.toolCallId);
403
417
  }
404
- for (const { part, json } of args.values()) part.input = json ? JSON.parse(json) : {};
405
418
  flushAssistant();
406
419
  flushResults();
407
420
  summary.toolCalls = [...args.values()].map(({ part }) => ({
408
421
  toolCallId: part.toolCallId,
409
422
  toolName: part.toolName,
410
- input: part.input
423
+ input: part.input,
424
+ ...invalid.has(part.toolCallId) ? { invalid: true } : {}
411
425
  }));
412
426
  return summary;
413
427
  }
@@ -542,13 +556,19 @@ function createTanStackAiExecutors(source) {
542
556
  throw structured && isTruncationError(error) ? truncated(request, info, void 0, error) : error;
543
557
  }
544
558
  if (outputSchema) {
545
- if (run.finishReason === "length") throw truncated(request, info, run.structured ?? run.text);
546
- const parsed = parseProviderOutput(request, run.structured !== void 0 ? run.structured : JSON.parse(run.text));
559
+ if (run.finishReason === "length") throw withModelCalls(truncated(request, info, run.structured ?? run.text), run.modelCalls);
560
+ let parsed;
561
+ try {
562
+ parsed = parseProviderOutput(request, run.structured !== void 0 ? run.structured : JSON.parse(run.text));
563
+ } catch (error) {
564
+ throw withModelCalls(error, run.modelCalls);
565
+ }
547
566
  return {
548
567
  result: parsed.result,
549
568
  ...typeof parsed.reasoning === "string" ? { reasoning: parsed.reasoning } : {},
550
569
  ...usageField(run.usage),
551
570
  finishReason: run.finishReason,
571
+ modelCalls: run.modelCalls,
552
572
  toolCalls: run.toolCalls,
553
573
  messages: run.messages,
554
574
  raw: run.chunks
@@ -558,6 +578,7 @@ function createTanStackAiExecutors(source) {
558
578
  result: run.text,
559
579
  ...usageField(run.usage),
560
580
  finishReason: run.finishReason,
581
+ modelCalls: run.modelCalls,
561
582
  toolCalls: run.toolCalls,
562
583
  messages: run.messages,
563
584
  raw: run.chunks
@@ -578,6 +599,7 @@ function createTanStackAiExecutors(source) {
578
599
  result: run.text,
579
600
  ...usageField(run.usage),
580
601
  finishReason: run.finishReason,
602
+ modelCalls: run.modelCalls,
581
603
  messages: run.messages,
582
604
  raw: run.chunks
583
605
  };
@@ -595,9 +617,19 @@ function createTanStackAiExecutors(source) {
595
617
  agentLoopStrategy: maxIterations(1)
596
618
  }, info?.signal);
597
619
  const toolCall = run.toolCalls[0];
598
- if (!toolCall) throw new Error("createTanStackAiExecutors: decide — model did not call an event tool.");
620
+ const unnamed = (call) => ({
621
+ rejected: rejectedAnswer(call, request.events.map((event) => event.toolName)),
622
+ ...usageField(run.usage),
623
+ finishReason: run.finishReason,
624
+ raw: run.chunks
625
+ });
626
+ if (!toolCall) return unnamed();
599
627
  const chosenEvent = request.events.find((event) => event.toolName === toolCall.toolName);
600
- if (!chosenEvent) throw new Error(`createTanStackAiExecutors: decide — model called unknown tool '${toolCall.toolName}'.`);
628
+ if (!chosenEvent) return unnamed(toolCall);
629
+ if (toolCall.invalid) return unnamed({
630
+ toolName: toolCall.toolName,
631
+ invalidJson: true
632
+ });
601
633
  return {
602
634
  event: {
603
635
  ...toolCall.input && typeof toolCall.input === "object" ? toolCall.input : {},