@minpeter/pss-runtime 0.1.0-next.1 → 0.1.0-next.3

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 (133) hide show
  1. package/README.md +290 -61
  2. package/dist/agent-host-session-store.js +10 -0
  3. package/dist/agent-host-session-store.js.map +1 -0
  4. package/dist/agent-loop.js +57 -28
  5. package/dist/agent-loop.js.map +1 -1
  6. package/dist/agent-namespace.js +6 -3
  7. package/dist/agent-namespace.js.map +1 -1
  8. package/dist/agent-options.d.ts +29 -0
  9. package/dist/agent-options.js +16 -0
  10. package/dist/agent-options.js.map +1 -0
  11. package/dist/agent-resume.js +63 -0
  12. package/dist/agent-resume.js.map +1 -0
  13. package/dist/agent-session-entry.d.ts +13 -0
  14. package/dist/agent.d.ts +8 -44
  15. package/dist/agent.js +61 -83
  16. package/dist/agent.js.map +1 -1
  17. package/dist/cloudflare/cloudflare-agent-context.d.ts +40 -0
  18. package/dist/cloudflare/cloudflare-agent-context.js +37 -0
  19. package/dist/cloudflare/cloudflare-agent-context.js.map +1 -0
  20. package/dist/cloudflare/cloudflare-alarm-budget.d.ts +18 -0
  21. package/dist/cloudflare/cloudflare-alarm-budget.js +77 -0
  22. package/dist/cloudflare/cloudflare-alarm-budget.js.map +1 -0
  23. package/dist/cloudflare/cloudflare-alarm-drainer.d.ts +45 -0
  24. package/dist/cloudflare/cloudflare-alarm-drainer.js +103 -0
  25. package/dist/cloudflare/cloudflare-alarm-drainer.js.map +1 -0
  26. package/dist/cloudflare/cloudflare-alarm-run-drain.d.ts +13 -0
  27. package/dist/cloudflare/cloudflare-alarm-run-drain.js +81 -0
  28. package/dist/cloudflare/cloudflare-alarm-run-drain.js.map +1 -0
  29. package/dist/cloudflare/cloudflare-alarm-work.js +110 -0
  30. package/dist/cloudflare/cloudflare-alarm-work.js.map +1 -0
  31. package/dist/cloudflare/cloudflare-checkpoint-store.js +39 -0
  32. package/dist/cloudflare/cloudflare-checkpoint-store.js.map +1 -0
  33. package/dist/cloudflare/cloudflare-durable-object-fetch.d.ts +21 -0
  34. package/dist/cloudflare/cloudflare-durable-object-fetch.js +11 -0
  35. package/dist/cloudflare/cloudflare-durable-object-fetch.js.map +1 -0
  36. package/dist/cloudflare/cloudflare-event-store.js +33 -0
  37. package/dist/cloudflare/cloudflare-event-store.js.map +1 -0
  38. package/dist/cloudflare/cloudflare-execution-session-store.js +40 -0
  39. package/dist/cloudflare/cloudflare-execution-session-store.js.map +1 -0
  40. package/dist/cloudflare/cloudflare-execution-store.js +35 -0
  41. package/dist/cloudflare/cloudflare-execution-store.js.map +1 -0
  42. package/dist/cloudflare/cloudflare-host.d.ts +61 -0
  43. package/dist/cloudflare/cloudflare-host.js +113 -0
  44. package/dist/cloudflare/cloudflare-host.js.map +1 -0
  45. package/dist/cloudflare/cloudflare-notification-store.js +59 -0
  46. package/dist/cloudflare/cloudflare-notification-store.js.map +1 -0
  47. package/dist/cloudflare/cloudflare-run-store.js +81 -0
  48. package/dist/cloudflare/cloudflare-run-store.js.map +1 -0
  49. package/dist/cloudflare/cloudflare-store-utils.js +43 -0
  50. package/dist/cloudflare/cloudflare-store-utils.js.map +1 -0
  51. package/dist/cloudflare/durable-object-storage.d.ts +20 -0
  52. package/dist/cloudflare/durable-object-storage.js +76 -0
  53. package/dist/cloudflare/durable-object-storage.js.map +1 -0
  54. package/dist/cloudflare/index.d.ts +7 -0
  55. package/dist/cloudflare/index.js +6 -0
  56. package/dist/execution/capabilities.d.ts +40 -0
  57. package/dist/execution/host.d.ts +9 -0
  58. package/dist/execution/host.js +62 -0
  59. package/dist/execution/host.js.map +1 -0
  60. package/dist/execution/index.d.ts +6 -0
  61. package/dist/execution/index.js +4 -0
  62. package/dist/execution/memory-notifications.js +54 -0
  63. package/dist/execution/memory-notifications.js.map +1 -0
  64. package/dist/execution/memory-state.js +34 -0
  65. package/dist/execution/memory-state.js.map +1 -0
  66. package/dist/execution/memory-store.js +203 -0
  67. package/dist/execution/memory-store.js.map +1 -0
  68. package/dist/execution/memory.d.ts +7 -0
  69. package/dist/execution/memory.js +28 -0
  70. package/dist/execution/memory.js.map +1 -0
  71. package/dist/execution/types.d.ts +150 -0
  72. package/dist/index.d.ts +13 -6
  73. package/dist/index.js +6 -1
  74. package/dist/llm-tool-execution.d.ts +35 -0
  75. package/dist/llm-tool-execution.js +126 -0
  76. package/dist/llm-tool-execution.js.map +1 -0
  77. package/dist/llm.d.ts +11 -15
  78. package/dist/llm.js +5 -3
  79. package/dist/llm.js.map +1 -1
  80. package/dist/plugins.d.ts +42 -0
  81. package/dist/plugins.js +43 -0
  82. package/dist/plugins.js.map +1 -0
  83. package/dist/session/delegate-input.d.ts +9 -0
  84. package/dist/session/delegate-input.js +16 -0
  85. package/dist/session/delegate-input.js.map +1 -0
  86. package/dist/session/events.d.ts +43 -22
  87. package/dist/session/events.js +41 -0
  88. package/dist/session/events.js.map +1 -0
  89. package/dist/session/input-meta-types.d.ts +10 -0
  90. package/dist/session/input-meta.d.ts +13 -0
  91. package/dist/session/input-meta.js +45 -0
  92. package/dist/session/input-meta.js.map +1 -0
  93. package/dist/session/input.d.ts +4 -0
  94. package/dist/session/mapping.js +4 -2
  95. package/dist/session/mapping.js.map +1 -1
  96. package/dist/session/runtime-input-emit.js +41 -0
  97. package/dist/session/runtime-input-emit.js.map +1 -0
  98. package/dist/session/runtime-input.js +10 -24
  99. package/dist/session/runtime-input.js.map +1 -1
  100. package/dist/session/session-errors.js +1 -6
  101. package/dist/session/session-errors.js.map +1 -1
  102. package/dist/session/session-events.js +73 -0
  103. package/dist/session/session-events.js.map +1 -0
  104. package/dist/session/session-execution.js +88 -0
  105. package/dist/session/session-execution.js.map +1 -0
  106. package/dist/session/session-notification.js +59 -0
  107. package/dist/session/session-notification.js.map +1 -0
  108. package/dist/session/session-runtime-drain.js +3 -9
  109. package/dist/session/session-runtime-drain.js.map +1 -1
  110. package/dist/session/session-turn-processor.js +125 -0
  111. package/dist/session/session-turn-processor.js.map +1 -0
  112. package/dist/session/session.js +81 -102
  113. package/dist/session/session.js.map +1 -1
  114. package/dist/session/snapshot.js.map +1 -1
  115. package/package.json +16 -1
  116. package/dist/agent-validation.js +0 -35
  117. package/dist/agent-validation.js.map +0 -1
  118. package/dist/child-session-cleanups.js +0 -61
  119. package/dist/child-session-cleanups.js.map +0 -1
  120. package/dist/hooks.d.ts +0 -32
  121. package/dist/subagent-job-cancel.js +0 -28
  122. package/dist/subagent-job-cancel.js.map +0 -1
  123. package/dist/subagent-job-output.js +0 -63
  124. package/dist/subagent-job-output.js.map +0 -1
  125. package/dist/subagent-jobs.js +0 -151
  126. package/dist/subagent-jobs.js.map +0 -1
  127. package/dist/subagent-prompt-schema.js +0 -114
  128. package/dist/subagent-prompt-schema.js.map +0 -1
  129. package/dist/subagent-run.js +0 -111
  130. package/dist/subagent-run.js.map +0 -1
  131. package/dist/subagents.js +0 -92
  132. package/dist/subagents.js.map +0 -1
  133. /package/dist/session/{runtime-input.d.ts → session-execution.d.ts} +0 -0
package/dist/index.js CHANGED
@@ -1,3 +1,8 @@
1
+ import { executionHost } from "./execution/host.js";
1
2
  import { createLlm } from "./llm.js";
3
+ import { attachInputMeta, attachRuntimeInputMeta, stripEventMeta, stripInputMeta, userInputFromEvent } from "./session/input-meta.js";
4
+ import { runPluginsForEvent } from "./plugins.js";
2
5
  import { Agent } from "./agent.js";
3
- export { Agent, createLlm };
6
+ import { delegateUserInput } from "./session/delegate-input.js";
7
+ import { isControlAgentEvent, isLifecycleAgentEvent, isTelemetryAgentEvent, isToolAgentEvent, isVisibleAgentEvent } from "./session/events.js";
8
+ export { Agent, attachInputMeta, attachRuntimeInputMeta, createLlm, delegateUserInput, executionHost, isControlAgentEvent, isLifecycleAgentEvent, isTelemetryAgentEvent, isToolAgentEvent, isVisibleAgentEvent, runPluginsForEvent, stripEventMeta, stripInputMeta, userInputFromEvent };
@@ -0,0 +1,35 @@
1
+ import { ToolSet } from "ai";
2
+
3
+ //#region src/llm-tool-execution.d.ts
4
+ type RuntimeToolRetryPolicy = "idempotent" | "manual-recovery" | "pure";
5
+ interface RuntimeToolExecutionCheckpointMetadata {
6
+ readonly attempt: number;
7
+ readonly idempotencyKey: string;
8
+ readonly policy: RuntimeToolRetryPolicy;
9
+ readonly toolCallId: string;
10
+ readonly toolName: string;
11
+ }
12
+ interface RuntimeToolExecutionCheckpoint extends RuntimeToolExecutionCheckpointMetadata {
13
+ readonly input: unknown;
14
+ }
15
+ type RuntimeToolExecutionDecision = {
16
+ readonly status: "needs-recovery";
17
+ } | undefined;
18
+ interface RuntimeToolExecutionContext {
19
+ readonly afterTool?: (checkpoint: RuntimeToolExecutionCheckpoint & {
20
+ readonly output: unknown;
21
+ }) => Promise<void> | void;
22
+ readonly attempt: number;
23
+ readonly beforeTool?: (checkpoint: RuntimeToolExecutionCheckpoint) => Promise<RuntimeToolExecutionDecision> | RuntimeToolExecutionDecision;
24
+ readonly runId: string;
25
+ }
26
+ declare class ToolExecutionNeedsRecoveryError extends Error {
27
+ readonly idempotencyKey: string;
28
+ readonly status = "needs-recovery";
29
+ readonly toolCallId: string;
30
+ readonly toolName: string;
31
+ constructor(checkpoint: RuntimeToolExecutionCheckpointMetadata);
32
+ }
33
+ //#endregion
34
+ export { RuntimeToolExecutionCheckpoint, RuntimeToolExecutionCheckpointMetadata, RuntimeToolExecutionContext, RuntimeToolExecutionDecision, RuntimeToolRetryPolicy, ToolExecutionNeedsRecoveryError };
35
+ //# sourceMappingURL=llm-tool-execution.d.ts.map
@@ -0,0 +1,126 @@
1
+ //#region src/llm-tool-execution.ts
2
+ const toolCallIdPrefix = "call_";
3
+ const publicToolCallIdPattern = /^call[-_]/;
4
+ var ToolExecutionNeedsRecoveryError = class extends Error {
5
+ idempotencyKey;
6
+ status = "needs-recovery";
7
+ toolCallId;
8
+ toolName;
9
+ constructor(checkpoint) {
10
+ super(`Tool ${checkpoint.toolName} requires manual recovery for ${checkpoint.idempotencyKey}`);
11
+ this.idempotencyKey = checkpoint.idempotencyKey;
12
+ this.name = "ToolExecutionNeedsRecoveryError";
13
+ this.toolCallId = checkpoint.toolCallId;
14
+ this.toolName = checkpoint.toolName;
15
+ }
16
+ };
17
+ function persistedToolExecutionCheckpoint(checkpoint) {
18
+ return {
19
+ attempt: checkpoint.attempt,
20
+ idempotencyKey: checkpoint.idempotencyKey,
21
+ policy: checkpoint.policy,
22
+ toolCallId: checkpoint.toolCallId,
23
+ toolName: checkpoint.toolName
24
+ };
25
+ }
26
+ function normalizeToolCallIds(tools, toolCallIds, toolExecution) {
27
+ if (!tools) return;
28
+ return Object.fromEntries(Object.entries(tools).map(([name, candidate]) => [name, wrapToolExecute(name, candidate, toolCallIds, toolExecution)]));
29
+ }
30
+ function rewriteMessageToolCallIds(message, toolCallIds) {
31
+ if (message.role === "assistant") return rewriteAssistantToolCallIds(message, toolCallIds);
32
+ if (message.role === "tool") return rewriteToolResultCallIds(message, toolCallIds);
33
+ return message;
34
+ }
35
+ function wrapToolExecute(toolName, toolDefinition, toolCallIds, toolExecution) {
36
+ if (!isExecutableToolDefinition(toolDefinition)) return toolDefinition;
37
+ const { execute } = toolDefinition;
38
+ if (!toolExecution) return {
39
+ ...toolDefinition,
40
+ execute: (input, options) => execute(input, {
41
+ ...options,
42
+ toolCallId: publicToolCallId(options.toolCallId, toolCallIds)
43
+ })
44
+ };
45
+ return {
46
+ ...toolDefinition,
47
+ execute: async (input, options) => {
48
+ const toolCallId = publicToolCallId(options.toolCallId, toolCallIds);
49
+ const checkpoint = toolCheckpoint({
50
+ input,
51
+ policy: toolRetryPolicy(toolDefinition),
52
+ toolCallId,
53
+ toolExecution,
54
+ toolName
55
+ });
56
+ if ((await toolExecution.beforeTool?.(checkpoint))?.status === "needs-recovery") throw new ToolExecutionNeedsRecoveryError(checkpoint);
57
+ const output = await execute(input, {
58
+ ...options,
59
+ attempt: checkpoint.attempt,
60
+ idempotencyKey: checkpoint.idempotencyKey,
61
+ retryPolicy: checkpoint.policy,
62
+ ...options.abortSignal === void 0 ? {} : { signal: options.abortSignal },
63
+ toolCallId
64
+ });
65
+ await toolExecution.afterTool?.({
66
+ ...checkpoint,
67
+ output
68
+ });
69
+ return output;
70
+ }
71
+ };
72
+ }
73
+ function isExecutableToolDefinition(value) {
74
+ return typeof value === "object" && value !== null && "execute" in value && typeof value.execute === "function";
75
+ }
76
+ function toolCheckpoint({ input, policy, toolCallId, toolExecution, toolName }) {
77
+ return {
78
+ attempt: toolExecution.attempt,
79
+ idempotencyKey: `${toolExecution.runId}:${toolCallId}`,
80
+ input,
81
+ policy,
82
+ toolCallId,
83
+ toolName
84
+ };
85
+ }
86
+ function toolRetryPolicy(toolDefinition) {
87
+ if (typeof toolDefinition === "object" && toolDefinition !== null && "retryPolicy" in toolDefinition && isToolRetryPolicy(toolDefinition.retryPolicy)) return toolDefinition.retryPolicy;
88
+ return "manual-recovery";
89
+ }
90
+ function isToolRetryPolicy(value) {
91
+ return value === "idempotent" || value === "manual-recovery" || value === "pure";
92
+ }
93
+ function rewriteAssistantToolCallIds(message, toolCallIds) {
94
+ if (typeof message.content === "string") return message;
95
+ return {
96
+ ...message,
97
+ content: message.content.map((part) => "toolCallId" in part ? {
98
+ ...part,
99
+ toolCallId: publicToolCallId(part.toolCallId, toolCallIds)
100
+ } : part)
101
+ };
102
+ }
103
+ function rewriteToolResultCallIds(message, toolCallIds) {
104
+ return {
105
+ ...message,
106
+ content: message.content.map((part) => "toolCallId" in part ? {
107
+ ...part,
108
+ toolCallId: publicToolCallId(part.toolCallId, toolCallIds)
109
+ } : part)
110
+ };
111
+ }
112
+ function publicToolCallId(toolCallId, toolCallIds) {
113
+ if (publicToolCallIdPattern.test(toolCallId)) return toolCallId;
114
+ const existing = toolCallIds.get(toolCallId);
115
+ if (existing) return existing;
116
+ const generated = createToolCallId();
117
+ toolCallIds.set(toolCallId, generated);
118
+ return generated;
119
+ }
120
+ function createToolCallId() {
121
+ return `${toolCallIdPrefix}${crypto.randomUUID().replaceAll("-", "")}`;
122
+ }
123
+ //#endregion
124
+ export { ToolExecutionNeedsRecoveryError, normalizeToolCallIds, persistedToolExecutionCheckpoint, rewriteMessageToolCallIds };
125
+
126
+ //# sourceMappingURL=llm-tool-execution.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"llm-tool-execution.js","names":[],"sources":["../src/llm-tool-execution.ts"],"sourcesContent":["import type { AssistantModelMessage, ToolModelMessage, ToolSet } from \"ai\";\nimport type { RuntimeLlmOutput } from \"./llm\";\n\nconst toolCallIdPrefix = \"call_\";\nconst publicToolCallIdPattern = /^call[-_]/;\n\ntype RuntimeLlmMessage = RuntimeLlmOutput[number];\n\nexport type RuntimeToolRetryPolicy = \"idempotent\" | \"manual-recovery\" | \"pure\";\n\nexport interface RuntimeToolExecutionCheckpointMetadata {\n readonly attempt: number;\n readonly idempotencyKey: string;\n readonly policy: RuntimeToolRetryPolicy;\n readonly toolCallId: string;\n readonly toolName: string;\n}\n\nexport interface RuntimeToolExecutionCheckpoint\n extends RuntimeToolExecutionCheckpointMetadata {\n readonly input: unknown;\n}\n\nexport type RuntimePersistedToolExecutionCheckpoint =\n RuntimeToolExecutionCheckpointMetadata;\n\nexport type RuntimeToolExecutionDecision =\n | { readonly status: \"needs-recovery\" }\n | undefined;\n\nexport interface RuntimeToolExecutionContext {\n readonly afterTool?: (\n checkpoint: RuntimeToolExecutionCheckpoint & { readonly output: unknown }\n ) => Promise<void> | void;\n readonly attempt: number;\n readonly beforeTool?: (\n checkpoint: RuntimeToolExecutionCheckpoint\n ) => Promise<RuntimeToolExecutionDecision> | RuntimeToolExecutionDecision;\n readonly runId: string;\n}\n\nexport class ToolExecutionNeedsRecoveryError extends Error {\n readonly idempotencyKey: string;\n readonly status = \"needs-recovery\";\n readonly toolCallId: string;\n readonly toolName: string;\n\n constructor(checkpoint: RuntimeToolExecutionCheckpointMetadata) {\n super(\n `Tool ${checkpoint.toolName} requires manual recovery for ${checkpoint.idempotencyKey}`\n );\n this.idempotencyKey = checkpoint.idempotencyKey;\n this.name = \"ToolExecutionNeedsRecoveryError\";\n this.toolCallId = checkpoint.toolCallId;\n this.toolName = checkpoint.toolName;\n }\n}\n\nexport function persistedToolExecutionCheckpoint(\n checkpoint: RuntimeToolExecutionCheckpointMetadata\n): RuntimePersistedToolExecutionCheckpoint {\n return {\n attempt: checkpoint.attempt,\n idempotencyKey: checkpoint.idempotencyKey,\n policy: checkpoint.policy,\n toolCallId: checkpoint.toolCallId,\n toolName: checkpoint.toolName,\n };\n}\n\nexport function normalizeToolCallIds(\n tools: ToolSet | undefined,\n toolCallIds: Map<string, string>,\n toolExecution: RuntimeToolExecutionContext | undefined\n): ToolSet | undefined {\n if (!tools) {\n return;\n }\n\n return Object.fromEntries(\n Object.entries(tools).map(([name, candidate]) => [\n name,\n wrapToolExecute(name, candidate, toolCallIds, toolExecution),\n ])\n ) as ToolSet;\n}\n\nexport function rewriteMessageToolCallIds(\n message: RuntimeLlmMessage,\n toolCallIds: Map<string, string>\n): RuntimeLlmMessage {\n if (message.role === \"assistant\") {\n return rewriteAssistantToolCallIds(message, toolCallIds);\n }\n\n if (message.role === \"tool\") {\n return rewriteToolResultCallIds(message, toolCallIds);\n }\n\n return message;\n}\n\nfunction wrapToolExecute(\n toolName: string,\n toolDefinition: unknown,\n toolCallIds: Map<string, string>,\n toolExecution: RuntimeToolExecutionContext | undefined\n): unknown {\n if (!isExecutableToolDefinition(toolDefinition)) {\n return toolDefinition;\n }\n\n const { execute } = toolDefinition;\n if (!toolExecution) {\n return {\n ...toolDefinition,\n execute: (input: unknown, options: ToolExecutionOptionsLike) =>\n execute(input, {\n ...options,\n toolCallId: publicToolCallId(options.toolCallId, toolCallIds),\n }),\n };\n }\n\n return {\n ...toolDefinition,\n execute: async (input: unknown, options: ToolExecutionOptionsLike) => {\n const toolCallId = publicToolCallId(options.toolCallId, toolCallIds);\n const checkpoint = toolCheckpoint({\n input,\n policy: toolRetryPolicy(toolDefinition),\n toolCallId,\n toolExecution,\n toolName,\n });\n const decision = await toolExecution.beforeTool?.(checkpoint);\n if (decision?.status === \"needs-recovery\") {\n throw new ToolExecutionNeedsRecoveryError(checkpoint);\n }\n\n const output = await execute(input, {\n ...options,\n attempt: checkpoint.attempt,\n idempotencyKey: checkpoint.idempotencyKey,\n retryPolicy: checkpoint.policy,\n ...(options.abortSignal === undefined\n ? {}\n : { signal: options.abortSignal }),\n toolCallId,\n });\n await toolExecution.afterTool?.({ ...checkpoint, output });\n return output;\n },\n };\n}\n\nfunction isExecutableToolDefinition(value: unknown): value is {\n readonly execute: (\n input: unknown,\n options: RuntimeToolExecutionOptionsLike | ToolExecutionOptionsLike\n ) => unknown;\n} {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"execute\" in value &&\n typeof value.execute === \"function\"\n );\n}\n\ninterface ToolExecutionOptionsLike {\n readonly abortSignal?: AbortSignal;\n readonly toolCallId: string;\n}\n\ninterface RuntimeToolExecutionOptionsLike extends ToolExecutionOptionsLike {\n readonly attempt: number;\n readonly idempotencyKey: string;\n readonly retryPolicy: RuntimeToolRetryPolicy;\n readonly signal?: AbortSignal;\n}\n\nfunction toolCheckpoint({\n input,\n policy,\n toolCallId,\n toolExecution,\n toolName,\n}: {\n readonly input: unknown;\n readonly policy: RuntimeToolRetryPolicy;\n readonly toolCallId: string;\n readonly toolExecution: RuntimeToolExecutionContext;\n readonly toolName: string;\n}): RuntimeToolExecutionCheckpoint {\n return {\n attempt: toolExecution.attempt,\n idempotencyKey: `${toolExecution.runId}:${toolCallId}`,\n input,\n policy,\n toolCallId,\n toolName,\n };\n}\n\nfunction toolRetryPolicy(toolDefinition: unknown): RuntimeToolRetryPolicy {\n if (\n typeof toolDefinition === \"object\" &&\n toolDefinition !== null &&\n \"retryPolicy\" in toolDefinition &&\n isToolRetryPolicy(toolDefinition.retryPolicy)\n ) {\n return toolDefinition.retryPolicy;\n }\n\n return \"manual-recovery\";\n}\n\nfunction isToolRetryPolicy(value: unknown): value is RuntimeToolRetryPolicy {\n return (\n value === \"idempotent\" || value === \"manual-recovery\" || value === \"pure\"\n );\n}\n\nfunction rewriteAssistantToolCallIds(\n message: AssistantModelMessage,\n toolCallIds: Map<string, string>\n): AssistantModelMessage {\n if (typeof message.content === \"string\") {\n return message;\n }\n\n return {\n ...message,\n content: message.content.map((part) =>\n \"toolCallId\" in part\n ? {\n ...part,\n toolCallId: publicToolCallId(part.toolCallId, toolCallIds),\n }\n : part\n ),\n };\n}\n\nfunction rewriteToolResultCallIds(\n message: ToolModelMessage,\n toolCallIds: Map<string, string>\n): ToolModelMessage {\n return {\n ...message,\n content: message.content.map((part) =>\n \"toolCallId\" in part\n ? {\n ...part,\n toolCallId: publicToolCallId(part.toolCallId, toolCallIds),\n }\n : part\n ),\n };\n}\n\nfunction publicToolCallId(\n toolCallId: string,\n toolCallIds: Map<string, string>\n): string {\n if (publicToolCallIdPattern.test(toolCallId)) {\n return toolCallId;\n }\n\n const existing = toolCallIds.get(toolCallId);\n if (existing) {\n return existing;\n }\n\n const generated = createToolCallId();\n toolCallIds.set(toolCallId, generated);\n return generated;\n}\n\nfunction createToolCallId(): string {\n return `${toolCallIdPrefix}${crypto.randomUUID().replaceAll(\"-\", \"\")}`;\n}\n"],"mappings":"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"}
package/dist/llm.d.ts CHANGED
@@ -1,32 +1,28 @@
1
- import { LanguageModel, ModelMessage, Tool, ToolExecutionOptions, ToolSet, generateText } from "ai";
1
+ import { RuntimeToolExecutionCheckpoint, RuntimeToolExecutionCheckpointMetadata, RuntimeToolExecutionContext, RuntimeToolExecutionDecision, RuntimeToolRetryPolicy } from "./llm-tool-execution.js";
2
+ import { LanguageModel, ModelMessage, ToolChoice, ToolSet, generateText } from "ai";
2
3
 
3
4
  //#region src/llm.d.ts
4
- type AgentToolExecutionOptions = ToolExecutionOptions<unknown>;
5
- type AgentToolExecute = NonNullable<Tool["execute"]>;
6
- type AgentToolChoice = "auto" | "required";
7
- type LlmOutput = Awaited<ReturnType<typeof generateText>>["responseMessages"];
8
- type LlmOutputPart = LlmOutput[number];
9
- interface LlmContext {
5
+ type AgentToolChoice = ToolChoice<ToolSet>;
6
+ type RuntimeLlmOutput = Awaited<ReturnType<typeof generateText>>["responseMessages"];
7
+ type RuntimeLlmOutputPart = RuntimeLlmOutput[number];
8
+ interface RuntimeLlmContext {
10
9
  history: readonly ModelMessage[];
11
10
  signal: AbortSignal;
11
+ toolExecution?: RuntimeToolExecutionContext;
12
12
  }
13
- type Llm = (context: LlmContext) => Promise<LlmOutput>;
14
- interface CreateLlmOptions {
13
+ type RuntimeLlm = (context: RuntimeLlmContext) => Promise<RuntimeLlmOutput>;
14
+ interface RuntimeCreateLlmOptions {
15
15
  instructions?: string;
16
16
  model: LanguageModel;
17
17
  toolChoice?: AgentToolChoice;
18
18
  tools?: ToolSet;
19
19
  }
20
- type RuntimeCreateLlmOptions = CreateLlmOptions;
21
- type RuntimeLlm = Llm;
22
- type RuntimeLlmContext = LlmContext;
23
- type RuntimeLlmOutput = LlmOutput;
24
20
  declare function createLlm({
25
21
  model,
26
22
  instructions,
27
23
  toolChoice,
28
24
  tools
29
- }: CreateLlmOptions): Llm;
25
+ }: RuntimeCreateLlmOptions): RuntimeLlm;
30
26
  //#endregion
31
- export { AgentToolChoice, AgentToolExecute, AgentToolExecutionOptions, Llm, LlmOutputPart, RuntimeCreateLlmOptions, RuntimeLlm, RuntimeLlmContext, RuntimeLlmOutput, createLlm };
27
+ export { AgentToolChoice, RuntimeCreateLlmOptions, RuntimeLlm, RuntimeLlmContext, RuntimeLlmOutput, RuntimeLlmOutputPart, createLlm };
32
28
  //# sourceMappingURL=llm.d.ts.map
package/dist/llm.js CHANGED
@@ -1,16 +1,18 @@
1
+ import { normalizeToolCallIds, rewriteMessageToolCallIds } from "./llm-tool-execution.js";
1
2
  import { generateText } from "ai";
2
3
  //#region src/llm.ts
3
4
  function createLlm({ model, instructions, toolChoice, tools }) {
4
- return async ({ history, signal }) => {
5
+ return async ({ history, signal, toolExecution }) => {
6
+ const toolCallIds = /* @__PURE__ */ new Map();
5
7
  const { responseMessages } = await generateText({
6
8
  abortSignal: signal,
7
9
  instructions,
8
10
  messages: [...history],
9
11
  model,
10
12
  toolChoice,
11
- tools
13
+ tools: normalizeToolCallIds(tools, toolCallIds, toolExecution)
12
14
  });
13
- return responseMessages;
15
+ return responseMessages.map((message) => rewriteMessageToolCallIds(message, toolCallIds));
14
16
  };
15
17
  }
16
18
  //#endregion
package/dist/llm.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"llm.js","names":[],"sources":["../src/llm.ts"],"sourcesContent":["import type {\n LanguageModel,\n ModelMessage,\n Tool,\n ToolExecutionOptions,\n ToolSet,\n} from \"ai\";\nimport { generateText } from \"ai\";\n\nexport type AgentToolExecutionOptions = ToolExecutionOptions<unknown>;\nexport type AgentToolExecute = NonNullable<Tool[\"execute\"]>;\nexport type AgentToolChoice = \"auto\" | \"required\";\nexport type LlmOutput = Awaited<\n ReturnType<typeof generateText>\n>[\"responseMessages\"];\nexport type LlmOutputPart = LlmOutput[number];\n\nexport interface LlmContext {\n history: readonly ModelMessage[];\n signal: AbortSignal;\n}\n\nexport type Llm = (context: LlmContext) => Promise<LlmOutput>;\n\nexport interface CreateLlmOptions {\n instructions?: string;\n model: LanguageModel;\n toolChoice?: AgentToolChoice;\n tools?: ToolSet;\n}\n\nexport type RuntimeCreateLlmOptions = CreateLlmOptions;\nexport type RuntimeLlm = Llm;\nexport type RuntimeLlmContext = LlmContext;\nexport type RuntimeLlmOutput = LlmOutput;\n\nexport function createLlm({\n model,\n instructions,\n toolChoice,\n tools,\n}: CreateLlmOptions): Llm {\n return async ({ history, signal }) => {\n const { responseMessages } = await generateText({\n abortSignal: signal,\n instructions,\n messages: [...history],\n model,\n toolChoice,\n tools,\n });\n\n return responseMessages;\n };\n}\n"],"mappings":";;AAoCA,SAAgB,UAAU,EACxB,OACA,cACA,YACA,SACwB;CACxB,OAAO,OAAO,EAAE,SAAS,aAAa;EACpC,MAAM,EAAE,qBAAqB,MAAM,aAAa;GAC9C,aAAa;GACb;GACA,UAAU,CAAC,GAAG,OAAO;GACrB;GACA;GACA;EACF,CAAC;EAED,OAAO;CACT;AACF"}
1
+ {"version":3,"file":"llm.js","names":[],"sources":["../src/llm.ts"],"sourcesContent":["import type { LanguageModel, ModelMessage, ToolChoice, ToolSet } from \"ai\";\nimport { generateText } from \"ai\";\nimport type { RuntimeToolExecutionContext } from \"./llm-tool-execution\";\nimport {\n normalizeToolCallIds,\n rewriteMessageToolCallIds,\n} from \"./llm-tool-execution\";\n\nexport type {\n RuntimePersistedToolExecutionCheckpoint,\n RuntimeToolExecutionCheckpoint,\n RuntimeToolExecutionCheckpointMetadata,\n RuntimeToolExecutionContext,\n RuntimeToolExecutionDecision,\n RuntimeToolRetryPolicy,\n} from \"./llm-tool-execution\";\n\nexport type AgentToolChoice = ToolChoice<ToolSet>;\nexport type RuntimeLlmOutput = Awaited<\n ReturnType<typeof generateText>\n>[\"responseMessages\"];\nexport type RuntimeLlmOutputPart = RuntimeLlmOutput[number];\n\nexport interface RuntimeLlmContext {\n history: readonly ModelMessage[];\n signal: AbortSignal;\n toolExecution?: RuntimeToolExecutionContext;\n}\n\nexport type RuntimeLlm = (\n context: RuntimeLlmContext\n) => Promise<RuntimeLlmOutput>;\n\nexport interface RuntimeCreateLlmOptions {\n instructions?: string;\n model: LanguageModel;\n toolChoice?: AgentToolChoice;\n tools?: ToolSet;\n}\n\nexport function createLlm({\n model,\n instructions,\n toolChoice,\n tools,\n}: RuntimeCreateLlmOptions): RuntimeLlm {\n return async ({ history, signal, toolExecution }) => {\n const toolCallIds = new Map<string, string>();\n const { responseMessages } = await generateText({\n abortSignal: signal,\n instructions,\n messages: [...history],\n model,\n toolChoice,\n tools: normalizeToolCallIds(tools, toolCallIds, toolExecution),\n });\n\n return responseMessages.map((message) =>\n rewriteMessageToolCallIds(message, toolCallIds)\n );\n };\n}\n"],"mappings":";;;AAwCA,SAAgB,UAAU,EACxB,OACA,cACA,YACA,SACsC;CACtC,OAAO,OAAO,EAAE,SAAS,QAAQ,oBAAoB;EACnD,MAAM,8BAAc,IAAI,IAAoB;EAC5C,MAAM,EAAE,qBAAqB,MAAM,aAAa;GAC9C,aAAa;GACb;GACA,UAAU,CAAC,GAAG,OAAO;GACrB;GACA;GACA,OAAO,qBAAqB,OAAO,aAAa,aAAa;EAC/D,CAAC;EAED,OAAO,iBAAiB,KAAK,YAC3B,0BAA0B,SAAS,WAAW,CAChD;CACF;AACF"}
@@ -0,0 +1,42 @@
1
+ import { RuntimeLlmContext } from "./llm.js";
2
+ import { InputEventMeta, InputSource } from "./session/input-meta-types.js";
3
+ import { UserMessage, UserText } from "./session/input.js";
4
+ import { AgentEvent, RuntimeInput } from "./session/events.js";
5
+
6
+ //#region src/plugins.d.ts
7
+ type MaybePromise<T> = PromiseLike<T> | T;
8
+ type AgentEventHistory = RuntimeLlmContext["history"];
9
+ type InterceptableAgentEvent = RuntimeInput | UserMessage | UserText;
10
+ type AgentPluginInterceptResult = {
11
+ readonly action: "continue";
12
+ } | {
13
+ readonly action: "handled";
14
+ } | {
15
+ readonly action: "transform";
16
+ readonly event: InterceptableAgentEvent;
17
+ };
18
+ type AgentPluginResult = AgentPluginInterceptResult | undefined;
19
+ interface AgentEventContext {
20
+ readonly event: AgentEvent;
21
+ readonly history: AgentEventHistory;
22
+ readonly signal?: AbortSignal;
23
+ }
24
+ interface AgentPlugin {
25
+ readonly events?: {
26
+ /** @deprecated Use top-level `on`. */readonly on?: (context: AgentEventContext) => MaybePromise<void>;
27
+ };
28
+ readonly name?: string;
29
+ readonly on?: (context: AgentEventContext) => MaybePromise<AgentPluginResult>;
30
+ }
31
+ type PluginPipelineResult = {
32
+ readonly event: AgentEvent;
33
+ readonly kind: "emit";
34
+ } | {
35
+ readonly kind: "handled";
36
+ };
37
+ declare function runPluginsForEvent(plugins: readonly AgentPlugin[], context: AgentEventContext, options?: {
38
+ readonly observeOnly?: boolean;
39
+ }): Promise<PluginPipelineResult>;
40
+ //#endregion
41
+ export { AgentEventContext, AgentPlugin, AgentPluginInterceptResult, AgentPluginResult, InterceptableAgentEvent, PluginPipelineResult, runPluginsForEvent };
42
+ //# sourceMappingURL=plugins.d.ts.map
@@ -0,0 +1,43 @@
1
+ //#region src/plugins.ts
2
+ function runPluginsForEvent(plugins, context, options = {}) {
3
+ return runPluginPipeline(plugins, context, options.observeOnly === true);
4
+ }
5
+ function isInterceptableEvent(event) {
6
+ return event.type === "user-text" || event.type === "user-message" || event.type === "runtime-input";
7
+ }
8
+ function resolvePluginHandler(plugin) {
9
+ if (plugin.on) return plugin.on;
10
+ const legacyHandler = plugin.events?.on;
11
+ if (!legacyHandler) return;
12
+ return (legacyContext) => Promise.resolve(legacyHandler(legacyContext)).then(() => void 0);
13
+ }
14
+ function normalizeInterceptResult(result) {
15
+ if (result === void 0) return;
16
+ if (result.action === "continue") return result;
17
+ if (result.action === "handled") return result;
18
+ if (result.action === "transform") return result;
19
+ }
20
+ async function runPluginPipeline(plugins, context, observeOnly) {
21
+ let currentEvent = context.event;
22
+ for (const plugin of plugins) {
23
+ const handler = resolvePluginHandler(plugin);
24
+ if (!handler) continue;
25
+ const result = await handler({
26
+ ...context,
27
+ event: currentEvent
28
+ });
29
+ if (observeOnly || !isInterceptableEvent(currentEvent)) continue;
30
+ const intercept = normalizeInterceptResult(result);
31
+ if (!intercept || intercept.action === "continue") continue;
32
+ if (intercept.action === "handled") return { kind: "handled" };
33
+ currentEvent = intercept.event;
34
+ }
35
+ return {
36
+ kind: "emit",
37
+ event: currentEvent
38
+ };
39
+ }
40
+ //#endregion
41
+ export { runPluginsForEvent };
42
+
43
+ //# sourceMappingURL=plugins.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugins.js","names":[],"sources":["../src/plugins.ts"],"sourcesContent":["import type { RuntimeLlmContext } from \"./llm\";\nimport type {\n AgentEvent,\n RuntimeInput,\n UserMessage,\n UserText,\n} from \"./session/events\";\n\nexport type { InputEventMeta, InputSource } from \"./session/input-meta-types\";\n\ntype MaybePromise<T> = PromiseLike<T> | T;\ntype AgentEventHistory = RuntimeLlmContext[\"history\"];\n\nexport type InterceptableAgentEvent = RuntimeInput | UserMessage | UserText;\n\nexport type AgentPluginInterceptResult =\n | { readonly action: \"continue\" }\n | { readonly action: \"handled\" }\n | { readonly action: \"transform\"; readonly event: InterceptableAgentEvent };\n\nexport type AgentPluginResult = AgentPluginInterceptResult | undefined;\n\nexport interface AgentEventContext {\n readonly event: AgentEvent;\n readonly history: AgentEventHistory;\n readonly signal?: AbortSignal;\n}\n\nexport interface AgentPlugin {\n readonly events?: {\n /** @deprecated Use top-level `on`. */\n readonly on?: (context: AgentEventContext) => MaybePromise<void>;\n };\n readonly name?: string;\n readonly on?: (context: AgentEventContext) => MaybePromise<AgentPluginResult>;\n}\n\nexport type PluginPipelineResult =\n | { readonly event: AgentEvent; readonly kind: \"emit\" }\n | { readonly kind: \"handled\" };\n\nexport function runEventPlugins(\n plugins: readonly AgentPlugin[],\n context: AgentEventContext\n): Promise<void> {\n return runPluginsForEvent(plugins, context, { observeOnly: true }).then(\n () => undefined\n );\n}\n\nexport function runPluginsForEvent(\n plugins: readonly AgentPlugin[],\n context: AgentEventContext,\n options: { readonly observeOnly?: boolean } = {}\n): Promise<PluginPipelineResult> {\n return runPluginPipeline(plugins, context, options.observeOnly === true);\n}\n\nfunction isInterceptableEvent(\n event: AgentEvent\n): event is InterceptableAgentEvent {\n return (\n event.type === \"user-text\" ||\n event.type === \"user-message\" ||\n event.type === \"runtime-input\"\n );\n}\n\nfunction resolvePluginHandler(\n plugin: AgentPlugin\n):\n | ((context: AgentEventContext) => MaybePromise<AgentPluginResult>)\n | undefined {\n if (plugin.on) {\n return plugin.on;\n }\n\n const legacyHandler = plugin.events?.on;\n if (!legacyHandler) {\n return;\n }\n\n return (legacyContext) =>\n Promise.resolve(legacyHandler(legacyContext)).then(() => undefined);\n}\n\nfunction normalizeInterceptResult(\n result: AgentPluginResult | undefined\n): AgentPluginInterceptResult | undefined {\n if (result === undefined) {\n return;\n }\n\n if (result.action === \"continue\") {\n return result;\n }\n\n if (result.action === \"handled\") {\n return result;\n }\n\n if (result.action === \"transform\") {\n return result;\n }\n\n return;\n}\n\nasync function runPluginPipeline(\n plugins: readonly AgentPlugin[],\n context: AgentEventContext,\n observeOnly: boolean\n): Promise<PluginPipelineResult> {\n let currentEvent = context.event;\n\n for (const plugin of plugins) {\n const handler = resolvePluginHandler(plugin);\n if (!handler) {\n continue;\n }\n\n const result = await handler({ ...context, event: currentEvent });\n if (observeOnly || !isInterceptableEvent(currentEvent)) {\n continue;\n }\n\n const intercept = normalizeInterceptResult(result);\n if (!intercept || intercept.action === \"continue\") {\n continue;\n }\n\n if (intercept.action === \"handled\") {\n return { kind: \"handled\" };\n }\n\n currentEvent = intercept.event;\n }\n\n return { kind: \"emit\", event: currentEvent };\n}\n"],"mappings":";AAkDA,SAAgB,mBACd,SACA,SACA,UAA8C,CAAC,GAChB;CAC/B,OAAO,kBAAkB,SAAS,SAAS,QAAQ,gBAAgB,IAAI;AACzE;AAEA,SAAS,qBACP,OACkC;CAClC,OACE,MAAM,SAAS,eACf,MAAM,SAAS,kBACf,MAAM,SAAS;AAEnB;AAEA,SAAS,qBACP,QAGY;CACZ,IAAI,OAAO,IACT,OAAO,OAAO;CAGhB,MAAM,gBAAgB,OAAO,QAAQ;CACrC,IAAI,CAAC,eACH;CAGF,QAAQ,kBACN,QAAQ,QAAQ,cAAc,aAAa,CAAC,EAAE,WAAW,KAAA,CAAS;AACtE;AAEA,SAAS,yBACP,QACwC;CACxC,IAAI,WAAW,KAAA,GACb;CAGF,IAAI,OAAO,WAAW,YACpB,OAAO;CAGT,IAAI,OAAO,WAAW,WACpB,OAAO;CAGT,IAAI,OAAO,WAAW,aACpB,OAAO;AAIX;AAEA,eAAe,kBACb,SACA,SACA,aAC+B;CAC/B,IAAI,eAAe,QAAQ;CAE3B,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,UAAU,qBAAqB,MAAM;EAC3C,IAAI,CAAC,SACH;EAGF,MAAM,SAAS,MAAM,QAAQ;GAAE,GAAG;GAAS,OAAO;EAAa,CAAC;EAChE,IAAI,eAAe,CAAC,qBAAqB,YAAY,GACnD;EAGF,MAAM,YAAY,yBAAyB,MAAM;EACjD,IAAI,CAAC,aAAa,UAAU,WAAW,YACrC;EAGF,IAAI,UAAU,WAAW,WACvB,OAAO,EAAE,MAAM,UAAU;EAG3B,eAAe,UAAU;CAC3B;CAEA,OAAO;EAAE,MAAM;EAAQ,OAAO;CAAa;AAC7C"}
@@ -0,0 +1,9 @@
1
+ import { UserInput } from "./input.js";
2
+
3
+ //#region src/session/delegate-input.d.ts
4
+ declare function delegateUserInput(prompt: unknown, options?: {
5
+ readonly delegateToolName?: string;
6
+ }): UserInput;
7
+ //#endregion
8
+ export { delegateUserInput };
9
+ //# sourceMappingURL=delegate-input.d.ts.map
@@ -0,0 +1,16 @@
1
+ import { attachInputMeta } from "./input-meta.js";
2
+ //#region src/session/delegate-input.ts
3
+ function delegateUserInput(prompt, options = {}) {
4
+ if (typeof prompt !== "string") throw new TypeError("Delegate prompt must be a plain string.");
5
+ return attachInputMeta({
6
+ type: "user-text",
7
+ text: prompt
8
+ }, {
9
+ delegateToolName: options.delegateToolName,
10
+ source: "delegate"
11
+ });
12
+ }
13
+ //#endregion
14
+ export { delegateUserInput };
15
+
16
+ //# sourceMappingURL=delegate-input.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"delegate-input.js","names":[],"sources":["../../src/session/delegate-input.ts"],"sourcesContent":["import type { UserInput } from \"./input\";\nimport { attachInputMeta } from \"./input-meta\";\n\nexport function delegateUserInput(\n prompt: unknown,\n options: { readonly delegateToolName?: string } = {}\n): UserInput {\n if (typeof prompt !== \"string\") {\n throw new TypeError(\"Delegate prompt must be a plain string.\");\n }\n\n return attachInputMeta(\n {\n type: \"user-text\",\n text: prompt,\n },\n {\n delegateToolName: options.delegateToolName,\n source: \"delegate\",\n }\n );\n}\n"],"mappings":";;AAGA,SAAgB,kBACd,QACA,UAAkD,CAAC,GACxC;CACX,IAAI,OAAO,WAAW,UACpB,MAAM,IAAI,UAAU,yCAAyC;CAG/D,OAAO,gBACL;EACE,MAAM;EACN,MAAM;CACR,GACA;EACE,kBAAkB,QAAQ;EAC1B,QAAQ;CACV,CACF;AACF"}
@@ -1,3 +1,4 @@
1
+ import { InputEventMeta, InputSource } from "./input-meta-types.js";
1
2
  import { UserInput, UserMessage, UserMessageContent, UserMessageContentPart, UserMessageFileData, UserMessageFilePart, UserMessageImagePart, UserMessageTextPart, UserText, UserTextContent } from "./input.js";
2
3
 
3
4
  //#region src/session/events.d.ts
@@ -7,6 +8,7 @@ interface RuntimeInput {
7
8
  * This is distinct from human-originated user-text and user-message input.
8
9
  */
9
10
  input: UserInput;
11
+ meta?: InputEventMeta;
10
12
  placement: "turn-start" | "step-start" | "step-end";
11
13
  type: "runtime-input";
12
14
  }
@@ -30,7 +32,6 @@ interface ToolResult {
30
32
  toolName: string;
31
33
  type: "tool-result";
32
34
  }
33
- type SubagentJobStatus = "aborted" | "cancelled" | "completed" | "error" | "pending" | "running";
34
35
  type AgentEvent = /** User input was accepted into the session queue. */UserText /** User multipart input was accepted into the session queue. */ | UserMessage /** Runtime/API-originated input inserted into the current turn, not human input. */ | RuntimeInput /** A queued user input started running as a turn. */ | {
35
36
  type: "turn-start";
36
37
  } /** The active turn was interrupted before normal completion. */ | {
@@ -42,29 +43,49 @@ type AgentEvent = /** User input was accepted into the session queue. */UserText
42
43
  type: "turn-end";
43
44
  } /** One model/tool-loop iteration started within the active turn. */ | {
44
45
  type: "step-start";
45
- } /** The model produced reasoning content. */ | AssistantReasoning /** The model produced visible assistant text. */ | AssistantText /** The model requested a tool call. */ | ToolCall /** A tool call returned a result. */ | ToolResult | {
46
- description?: string;
47
- run_in_background: boolean;
48
- subagent: string;
49
- task_id?: string;
50
- type: "subagent-job-start";
51
- } | {
52
- eventType?: AgentEvent["type"];
53
- status: SubagentJobStatus;
54
- subagent: string;
55
- task_id: string;
56
- type: "subagent-job-update";
57
- } | {
58
- error?: string;
59
- eventCount: number;
60
- status: Exclude<SubagentJobStatus, "pending" | "running">;
61
- subagent: string;
62
- task_id?: string;
63
- type: "subagent-job-end";
64
- } /** One model/tool-loop iteration finished within the active turn. */ | {
46
+ } /** The model produced reasoning content. */ | AssistantReasoning /** The model produced visible assistant text. */ | AssistantText /** The model requested a tool call. */ | ToolCall /** A tool call returned a result. */ | ToolResult /** One model/tool-loop iteration finished within the active turn. */ | {
65
47
  type: "step-end";
66
48
  };
67
49
  type AgentEventListener = (event: AgentEvent) => void;
50
+ declare const visibleAgentEventTypes: {
51
+ "assistant-text": true;
52
+ "user-message": true;
53
+ "user-text": true;
54
+ };
55
+ declare const lifecycleAgentEventTypes: {
56
+ "step-end": true;
57
+ "step-start": true;
58
+ "turn-abort": true;
59
+ "turn-end": true;
60
+ "turn-error": true;
61
+ "turn-start": true;
62
+ };
63
+ declare const toolAgentEventTypes: {
64
+ "tool-call": true;
65
+ "tool-result": true;
66
+ };
67
+ declare const telemetryAgentEventTypes: {
68
+ "assistant-reasoning": true;
69
+ "runtime-input": true;
70
+ };
71
+ type VisibleAgentEvent = Extract<AgentEvent, {
72
+ type: keyof typeof visibleAgentEventTypes;
73
+ }>;
74
+ type LifecycleAgentEvent = Extract<AgentEvent, {
75
+ type: keyof typeof lifecycleAgentEventTypes;
76
+ }>;
77
+ type ToolAgentEvent = Extract<AgentEvent, {
78
+ type: keyof typeof toolAgentEventTypes;
79
+ }>;
80
+ type TelemetryAgentEvent = Extract<AgentEvent, {
81
+ type: keyof typeof telemetryAgentEventTypes;
82
+ }>;
83
+ type ControlAgentEvent = Exclude<AgentEvent, VisibleAgentEvent>;
84
+ declare function isVisibleAgentEvent(event: AgentEvent): event is VisibleAgentEvent;
85
+ declare function isLifecycleAgentEvent(event: AgentEvent): event is LifecycleAgentEvent;
86
+ declare function isToolAgentEvent(event: AgentEvent): event is ToolAgentEvent;
87
+ declare function isTelemetryAgentEvent(event: AgentEvent): event is TelemetryAgentEvent;
88
+ declare function isControlAgentEvent(event: AgentEvent): event is ControlAgentEvent;
68
89
  //#endregion
69
- export { AgentEvent, AgentEventListener, AssistantReasoning, AssistantText, RuntimeInput, ToolCall, ToolResult };
90
+ export { AgentEvent, AgentEventListener, AssistantReasoning, AssistantText, ControlAgentEvent, LifecycleAgentEvent, RuntimeInput, TelemetryAgentEvent, ToolAgentEvent, ToolCall, ToolResult, VisibleAgentEvent, isControlAgentEvent, isLifecycleAgentEvent, isTelemetryAgentEvent, isToolAgentEvent, isVisibleAgentEvent };
70
91
  //# sourceMappingURL=events.d.ts.map
@@ -0,0 +1,41 @@
1
+ //#region src/session/events.ts
2
+ const visibleAgentEventTypes = {
3
+ "assistant-text": true,
4
+ "user-message": true,
5
+ "user-text": true
6
+ };
7
+ const lifecycleAgentEventTypes = {
8
+ "step-end": true,
9
+ "step-start": true,
10
+ "turn-abort": true,
11
+ "turn-end": true,
12
+ "turn-error": true,
13
+ "turn-start": true
14
+ };
15
+ const toolAgentEventTypes = {
16
+ "tool-call": true,
17
+ "tool-result": true
18
+ };
19
+ const telemetryAgentEventTypes = {
20
+ "assistant-reasoning": true,
21
+ "runtime-input": true
22
+ };
23
+ function isVisibleAgentEvent(event) {
24
+ return event.type in visibleAgentEventTypes;
25
+ }
26
+ function isLifecycleAgentEvent(event) {
27
+ return event.type in lifecycleAgentEventTypes;
28
+ }
29
+ function isToolAgentEvent(event) {
30
+ return event.type in toolAgentEventTypes;
31
+ }
32
+ function isTelemetryAgentEvent(event) {
33
+ return event.type in telemetryAgentEventTypes;
34
+ }
35
+ function isControlAgentEvent(event) {
36
+ return !isVisibleAgentEvent(event);
37
+ }
38
+ //#endregion
39
+ export { isControlAgentEvent, isLifecycleAgentEvent, isTelemetryAgentEvent, isToolAgentEvent, isVisibleAgentEvent };
40
+
41
+ //# sourceMappingURL=events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.js","names":[],"sources":["../../src/session/events.ts"],"sourcesContent":["import type { UserInput, UserMessage, UserText } from \"./input\";\nimport type { InputEventMeta } from \"./input-meta-types\";\n\nexport type {\n UserInput,\n UserMessage,\n UserMessageContent,\n UserMessageContentPart,\n UserMessageFileData,\n UserMessageFilePart,\n UserMessageImagePart,\n UserMessageTextPart,\n UserText,\n UserTextContent,\n} from \"./input\";\nexport type { InputEventMeta, InputSource } from \"./input-meta-types\";\n\nexport interface RuntimeInput {\n /**\n * Runtime/API-originated model input inserted into the current turn.\n * This is distinct from human-originated user-text and user-message input.\n */\n input: UserInput;\n meta?: InputEventMeta;\n placement: \"turn-start\" | \"step-start\" | \"step-end\";\n type: \"runtime-input\";\n}\n\nexport interface AssistantText {\n text: string;\n type: \"assistant-text\";\n}\nexport interface AssistantReasoning {\n text: string;\n type: \"assistant-reasoning\";\n}\nexport interface ToolCall {\n input: unknown;\n toolCallId: string;\n toolName: string;\n type: \"tool-call\";\n}\nexport interface ToolResult {\n output: unknown;\n toolCallId: string;\n toolName: string;\n type: \"tool-result\";\n}\n\nexport type AgentEvent =\n /** User input was accepted into the session queue. */\n | UserText\n /** User multipart input was accepted into the session queue. */\n | UserMessage\n /** Runtime/API-originated input inserted into the current turn, not human input. */\n | RuntimeInput\n /** A queued user input started running as a turn. */\n | { type: \"turn-start\" }\n /** The active turn was interrupted before normal completion. */\n | { type: \"turn-abort\" }\n /** The active turn hit an unrecoverable runtime failure. */\n | { type: \"turn-error\"; message: string }\n /** The active turn completed normally. */\n | { type: \"turn-end\" }\n /** One model/tool-loop iteration started within the active turn. */\n | { type: \"step-start\" }\n /** The model produced reasoning content. */\n | AssistantReasoning\n /** The model produced visible assistant text. */\n | AssistantText\n /** The model requested a tool call. */\n | ToolCall\n /** A tool call returned a result. */\n | ToolResult\n /** One model/tool-loop iteration finished within the active turn. */\n | { type: \"step-end\" };\n\nexport type AgentEventListener = (event: AgentEvent) => void;\n\nconst visibleAgentEventTypes = {\n \"assistant-text\": true,\n \"user-message\": true,\n \"user-text\": true,\n} satisfies Partial<Record<AgentEvent[\"type\"], true>>;\n\nconst lifecycleAgentEventTypes = {\n \"step-end\": true,\n \"step-start\": true,\n \"turn-abort\": true,\n \"turn-end\": true,\n \"turn-error\": true,\n \"turn-start\": true,\n} satisfies Partial<Record<AgentEvent[\"type\"], true>>;\n\nconst toolAgentEventTypes = {\n \"tool-call\": true,\n \"tool-result\": true,\n} satisfies Partial<Record<AgentEvent[\"type\"], true>>;\n\nconst telemetryAgentEventTypes = {\n \"assistant-reasoning\": true,\n \"runtime-input\": true,\n} satisfies Partial<Record<AgentEvent[\"type\"], true>>;\n\nexport type VisibleAgentEvent = Extract<\n AgentEvent,\n { type: keyof typeof visibleAgentEventTypes }\n>;\nexport type LifecycleAgentEvent = Extract<\n AgentEvent,\n { type: keyof typeof lifecycleAgentEventTypes }\n>;\nexport type ToolAgentEvent = Extract<\n AgentEvent,\n { type: keyof typeof toolAgentEventTypes }\n>;\nexport type TelemetryAgentEvent = Extract<\n AgentEvent,\n { type: keyof typeof telemetryAgentEventTypes }\n>;\nexport type ControlAgentEvent = Exclude<AgentEvent, VisibleAgentEvent>;\n\nexport function isVisibleAgentEvent(\n event: AgentEvent\n): event is VisibleAgentEvent {\n return event.type in visibleAgentEventTypes;\n}\n\nexport function isLifecycleAgentEvent(\n event: AgentEvent\n): event is LifecycleAgentEvent {\n return event.type in lifecycleAgentEventTypes;\n}\n\nexport function isToolAgentEvent(event: AgentEvent): event is ToolAgentEvent {\n return event.type in toolAgentEventTypes;\n}\n\nexport function isTelemetryAgentEvent(\n event: AgentEvent\n): event is TelemetryAgentEvent {\n return event.type in telemetryAgentEventTypes;\n}\n\nexport function isControlAgentEvent(\n event: AgentEvent\n): event is ControlAgentEvent {\n return !isVisibleAgentEvent(event);\n}\n"],"mappings":";AA+EA,MAAM,yBAAyB;CAC7B,kBAAkB;CAClB,gBAAgB;CAChB,aAAa;AACf;AAEA,MAAM,2BAA2B;CAC/B,YAAY;CACZ,cAAc;CACd,cAAc;CACd,YAAY;CACZ,cAAc;CACd,cAAc;AAChB;AAEA,MAAM,sBAAsB;CAC1B,aAAa;CACb,eAAe;AACjB;AAEA,MAAM,2BAA2B;CAC/B,uBAAuB;CACvB,iBAAiB;AACnB;AAoBA,SAAgB,oBACd,OAC4B;CAC5B,OAAO,MAAM,QAAQ;AACvB;AAEA,SAAgB,sBACd,OAC8B;CAC9B,OAAO,MAAM,QAAQ;AACvB;AAEA,SAAgB,iBAAiB,OAA4C;CAC3E,OAAO,MAAM,QAAQ;AACvB;AAEA,SAAgB,sBACd,OAC8B;CAC9B,OAAO,MAAM,QAAQ;AACvB;AAEA,SAAgB,oBACd,OAC4B;CAC5B,OAAO,CAAC,oBAAoB,KAAK;AACnC"}
@@ -0,0 +1,10 @@
1
+ //#region src/session/input-meta-types.d.ts
2
+ type InputSource = "delegate" | "notify" | "send" | "steer";
3
+ interface InputEventMeta {
4
+ readonly delegateToolName?: string;
5
+ readonly source: InputSource;
6
+ readonly streaming?: "follow-up" | "steer";
7
+ }
8
+ //#endregion
9
+ export { InputEventMeta, InputSource };
10
+ //# sourceMappingURL=input-meta-types.d.ts.map
@@ -0,0 +1,13 @@
1
+ import { InputEventMeta, InputSource } from "./input-meta-types.js";
2
+ import { UserInput, UserMessage, UserText } from "./input.js";
3
+ import { AgentEvent, RuntimeInput } from "./events.js";
4
+
5
+ //#region src/session/input-meta.d.ts
6
+ declare function attachInputMeta(input: UserInput, meta: InputEventMeta): UserText | UserMessage;
7
+ declare function attachRuntimeInputMeta(input: UserInput, placement: RuntimeInput["placement"], meta: InputEventMeta): RuntimeInput;
8
+ declare function stripInputMeta(input: UserInput): UserInput;
9
+ declare function stripEventMeta(event: AgentEvent): AgentEvent;
10
+ declare function userInputFromEvent(event: UserText | UserMessage): UserInput;
11
+ //#endregion
12
+ export { attachInputMeta, attachRuntimeInputMeta, stripEventMeta, stripInputMeta, userInputFromEvent };
13
+ //# sourceMappingURL=input-meta.d.ts.map
@@ -0,0 +1,45 @@
1
+ //#region src/session/input-meta.ts
2
+ function attachInputMeta(input, meta) {
3
+ if (input.type === "user-text") return {
4
+ ...input,
5
+ meta
6
+ };
7
+ return {
8
+ ...input,
9
+ meta
10
+ };
11
+ }
12
+ function attachRuntimeInputMeta(input, placement, meta) {
13
+ return {
14
+ input: attachInputMeta(input, meta),
15
+ meta,
16
+ placement,
17
+ type: "runtime-input"
18
+ };
19
+ }
20
+ function stripInputMeta(input) {
21
+ if (input.type === "user-text") {
22
+ const { meta: _meta, ...rest } = input;
23
+ return rest;
24
+ }
25
+ const { meta: _meta, ...rest } = input;
26
+ return rest;
27
+ }
28
+ function stripEventMeta(event) {
29
+ if (event.type === "user-text" || event.type === "user-message") return stripInputMeta(event);
30
+ if (event.type === "runtime-input") {
31
+ const { meta: _meta, ...rest } = event;
32
+ return {
33
+ ...rest,
34
+ input: stripInputMeta(event.input)
35
+ };
36
+ }
37
+ return event;
38
+ }
39
+ function userInputFromEvent(event) {
40
+ return stripInputMeta(event);
41
+ }
42
+ //#endregion
43
+ export { attachInputMeta, attachRuntimeInputMeta, stripEventMeta, stripInputMeta, userInputFromEvent };
44
+
45
+ //# sourceMappingURL=input-meta.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"input-meta.js","names":[],"sources":["../../src/session/input-meta.ts"],"sourcesContent":["import type { AgentEvent, RuntimeInput } from \"./events\";\nimport type { UserInput, UserMessage, UserText } from \"./input\";\nimport type { InputEventMeta } from \"./input-meta-types\";\n\nexport type { InputEventMeta, InputSource } from \"./input-meta-types\";\n\nexport function attachInputMeta(\n input: UserInput,\n meta: InputEventMeta\n): UserText | UserMessage {\n if (input.type === \"user-text\") {\n return { ...input, meta };\n }\n\n return { ...input, meta };\n}\n\nexport function attachRuntimeInputMeta(\n input: UserInput,\n placement: RuntimeInput[\"placement\"],\n meta: InputEventMeta\n): RuntimeInput {\n return {\n input: attachInputMeta(input, meta),\n meta,\n placement,\n type: \"runtime-input\",\n };\n}\n\nexport function stripInputMeta(input: UserInput): UserInput {\n if (input.type === \"user-text\") {\n const { meta: _meta, ...rest } = input;\n return rest;\n }\n\n const { meta: _meta, ...rest } = input;\n return rest;\n}\n\nexport function stripEventMeta(event: AgentEvent): AgentEvent {\n if (event.type === \"user-text\" || event.type === \"user-message\") {\n return stripInputMeta(event);\n }\n\n if (event.type === \"runtime-input\") {\n const { meta: _meta, ...rest } = event;\n return {\n ...rest,\n input: stripInputMeta(event.input),\n };\n }\n\n return event;\n}\n\nexport function userInputFromEvent(event: UserText | UserMessage): UserInput {\n return stripInputMeta(event);\n}\n"],"mappings":";AAMA,SAAgB,gBACd,OACA,MACwB;CACxB,IAAI,MAAM,SAAS,aACjB,OAAO;EAAE,GAAG;EAAO;CAAK;CAG1B,OAAO;EAAE,GAAG;EAAO;CAAK;AAC1B;AAEA,SAAgB,uBACd,OACA,WACA,MACc;CACd,OAAO;EACL,OAAO,gBAAgB,OAAO,IAAI;EAClC;EACA;EACA,MAAM;CACR;AACF;AAEA,SAAgB,eAAe,OAA6B;CAC1D,IAAI,MAAM,SAAS,aAAa;EAC9B,MAAM,EAAE,MAAM,OAAO,GAAG,SAAS;EACjC,OAAO;CACT;CAEA,MAAM,EAAE,MAAM,OAAO,GAAG,SAAS;CACjC,OAAO;AACT;AAEA,SAAgB,eAAe,OAA+B;CAC5D,IAAI,MAAM,SAAS,eAAe,MAAM,SAAS,gBAC/C,OAAO,eAAe,KAAK;CAG7B,IAAI,MAAM,SAAS,iBAAiB;EAClC,MAAM,EAAE,MAAM,OAAO,GAAG,SAAS;EACjC,OAAO;GACL,GAAG;GACH,OAAO,eAAe,MAAM,KAAK;EACnC;CACF;CAEA,OAAO;AACT;AAEA,SAAgB,mBAAmB,OAA0C;CAC3E,OAAO,eAAe,KAAK;AAC7B"}