@superdurable/dex-agents 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (168) hide show
  1. package/README.md +1608 -64
  2. package/dist/src/agent-built-in-tools.d.ts +81 -0
  3. package/dist/src/agent-built-in-tools.js +246 -0
  4. package/dist/src/agent-built-in-tools.js.map +1 -0
  5. package/dist/src/agent-context-compaction.d.ts +254 -0
  6. package/dist/src/agent-context-compaction.js +287 -0
  7. package/dist/src/agent-context-compaction.js.map +1 -0
  8. package/dist/src/agent-framework-runner.d.ts +456 -16
  9. package/dist/src/agent-framework-runner.js +4 -3
  10. package/dist/src/agent-framework-runner.js.map +1 -1
  11. package/dist/src/agent-plans.d.ts +48 -0
  12. package/dist/src/agent-plans.js +86 -0
  13. package/dist/src/agent-plans.js.map +1 -0
  14. package/dist/src/agent-session-cancellation-steps.d.ts +28 -0
  15. package/dist/src/agent-session-cancellation-steps.js +154 -0
  16. package/dist/src/agent-session-cancellation-steps.js.map +1 -0
  17. package/dist/src/agent-session-codecs.d.ts +65 -4
  18. package/dist/src/agent-session-codecs.js +343 -8
  19. package/dist/src/agent-session-codecs.js.map +1 -1
  20. package/dist/src/agent-session-failure.d.ts +58 -0
  21. package/dist/src/agent-session-failure.js +126 -0
  22. package/dist/src/agent-session-failure.js.map +1 -0
  23. package/dist/src/agent-session-flow-graph-cli.d.ts +2 -0
  24. package/dist/src/agent-session-flow-graph-cli.js +47 -0
  25. package/dist/src/agent-session-flow-graph-cli.js.map +1 -0
  26. package/dist/src/agent-session-flow-graph.d.ts +38 -0
  27. package/dist/src/agent-session-flow-graph.js +54 -0
  28. package/dist/src/agent-session-flow-graph.js.map +1 -0
  29. package/dist/src/agent-session-flow-graph.json +2187 -0
  30. package/dist/src/agent-session-flow.d.ts +317 -28
  31. package/dist/src/agent-session-flow.js +373 -21
  32. package/dist/src/agent-session-flow.js.map +1 -1
  33. package/dist/src/agent-session-persistence.d.ts +81 -4
  34. package/dist/src/agent-session-persistence.js +33 -2
  35. package/dist/src/agent-session-persistence.js.map +1 -1
  36. package/dist/src/agent-session-settings.d.ts +58 -0
  37. package/dist/src/agent-session-settings.js +81 -0
  38. package/dist/src/agent-session-settings.js.map +1 -0
  39. package/dist/src/agent-session-spend-limit.d.ts +156 -0
  40. package/dist/src/agent-session-spend-limit.js +42 -0
  41. package/dist/src/agent-session-spend-limit.js.map +1 -0
  42. package/dist/src/agent-session-start.d.ts +49 -0
  43. package/dist/src/agent-session-start.js +67 -0
  44. package/dist/src/agent-session-start.js.map +1 -0
  45. package/dist/src/agent-session-step-options.d.ts +78 -0
  46. package/dist/src/agent-session-step-options.js +71 -0
  47. package/dist/src/agent-session-step-options.js.map +1 -0
  48. package/dist/src/agent-session-step-types.d.ts +20 -0
  49. package/dist/src/agent-session-step-types.js +28 -0
  50. package/dist/src/agent-session-step-types.js.map +1 -0
  51. package/dist/src/agent-session-steps.d.ts +103 -8
  52. package/dist/src/agent-session-steps.js +997 -197
  53. package/dist/src/agent-session-steps.js.map +1 -1
  54. package/dist/src/agent-session-tracer.d.ts +61 -0
  55. package/dist/src/agent-session-tracer.js +608 -0
  56. package/dist/src/agent-session-tracer.js.map +1 -0
  57. package/dist/src/agent-session-transitions.d.ts +200 -26
  58. package/dist/src/agent-session-transitions.js +732 -32
  59. package/dist/src/agent-session-transitions.js.map +1 -1
  60. package/dist/src/agent-session-values.d.ts +696 -30
  61. package/dist/src/agent-session-values.js +39 -4
  62. package/dist/src/agent-session-values.js.map +1 -1
  63. package/dist/src/agent-session-wait-steps.d.ts +53 -0
  64. package/dist/src/agent-session-wait-steps.js +389 -0
  65. package/dist/src/agent-session-wait-steps.js.map +1 -0
  66. package/dist/src/agent-tracing-options.d.ts +107 -0
  67. package/dist/src/agent-tracing-options.js +20 -0
  68. package/dist/src/agent-tracing-options.js.map +1 -0
  69. package/dist/src/ai-sdk/ai-sdk-model-client.d.ts +40 -0
  70. package/dist/src/ai-sdk/ai-sdk-model-client.js +235 -0
  71. package/dist/src/ai-sdk/ai-sdk-model-client.js.map +1 -0
  72. package/dist/src/ai-sdk/index.d.ts +1 -0
  73. package/dist/src/ai-sdk/index.js +9 -0
  74. package/dist/src/ai-sdk/index.js.map +1 -0
  75. package/dist/src/index.d.ts +14 -3
  76. package/dist/src/index.js +12 -2
  77. package/dist/src/index.js.map +1 -1
  78. package/dist/src/mcp/index.d.ts +3 -0
  79. package/dist/src/mcp/index.js +10 -0
  80. package/dist/src/mcp/index.js.map +1 -0
  81. package/dist/src/mcp/mcp-resource-and-prompt-tools.d.ts +29 -0
  82. package/dist/src/mcp/mcp-resource-and-prompt-tools.js +185 -0
  83. package/dist/src/mcp/mcp-resource-and-prompt-tools.js.map +1 -0
  84. package/dist/src/mcp/mcp-server-config.d.ts +86 -0
  85. package/dist/src/mcp/mcp-server-config.js +38 -0
  86. package/dist/src/mcp/mcp-server-config.js.map +1 -0
  87. package/dist/src/mcp/mcp-tool-source.d.ts +470 -0
  88. package/dist/src/mcp/mcp-tool-source.js +1204 -0
  89. package/dist/src/mcp/mcp-tool-source.js.map +1 -0
  90. package/dist/src/stepwise/agent-definition.d.ts +166 -0
  91. package/dist/src/stepwise/agent-definition.js +9 -0
  92. package/dist/src/stepwise/agent-definition.js.map +1 -0
  93. package/dist/src/stepwise/function-tool.d.ts +130 -0
  94. package/dist/src/stepwise/function-tool.js +75 -0
  95. package/dist/src/stepwise/function-tool.js.map +1 -0
  96. package/dist/src/stepwise/index.d.ts +8 -0
  97. package/dist/src/stepwise/index.js +13 -0
  98. package/dist/src/stepwise/index.js.map +1 -0
  99. package/dist/src/stepwise/json-schema-validation.d.ts +9 -0
  100. package/dist/src/stepwise/json-schema-validation.js +141 -0
  101. package/dist/src/stepwise/json-schema-validation.js.map +1 -0
  102. package/dist/src/stepwise/model-client.d.ts +194 -0
  103. package/dist/src/stepwise/model-client.js +9 -0
  104. package/dist/src/stepwise/model-client.js.map +1 -0
  105. package/dist/src/stepwise/scripted-model-client.d.ts +99 -0
  106. package/dist/src/stepwise/scripted-model-client.js +156 -0
  107. package/dist/src/stepwise/scripted-model-client.js.map +1 -0
  108. package/dist/src/stepwise/stepwise-agent-runner.d.ts +171 -0
  109. package/dist/src/stepwise/stepwise-agent-runner.js +777 -0
  110. package/dist/src/stepwise/stepwise-agent-runner.js.map +1 -0
  111. package/dist/src/stepwise/stepwise-agent-session-flow.d.ts +35 -0
  112. package/dist/src/stepwise/stepwise-agent-session-flow.js +57 -0
  113. package/dist/src/stepwise/stepwise-agent-session-flow.js.map +1 -0
  114. package/dist/src/stepwise/stepwise-input-items.d.ts +12 -0
  115. package/dist/src/stepwise/stepwise-input-items.js +172 -0
  116. package/dist/src/stepwise/stepwise-input-items.js.map +1 -0
  117. package/dist/src/stepwise/tool-source.d.ts +103 -0
  118. package/dist/src/stepwise/tool-source.js +9 -0
  119. package/dist/src/stepwise/tool-source.js.map +1 -0
  120. package/dist/src/testing/agent-session-test-client.d.ts +65 -8
  121. package/dist/src/testing/agent-session-test-client.js +92 -22
  122. package/dist/src/testing/agent-session-test-client.js.map +1 -1
  123. package/dist/src/testing/context-window.d.ts +194 -0
  124. package/dist/src/testing/context-window.js +360 -0
  125. package/dist/src/testing/context-window.js.map +1 -0
  126. package/dist/src/testing/index.d.ts +2 -0
  127. package/dist/src/testing/index.js +2 -0
  128. package/dist/src/testing/index.js.map +1 -1
  129. package/dist/src/testing/private-dex-engine.d.ts +9 -0
  130. package/dist/src/testing/private-dex-engine.js +9 -1
  131. package/dist/src/testing/private-dex-engine.js.map +1 -1
  132. package/dist/src/testing/scripted-model-client.d.ts +1 -0
  133. package/dist/src/testing/scripted-model-client.js +10 -0
  134. package/dist/src/testing/scripted-model-client.js.map +1 -0
  135. package/dist/src/ui-stream/agent-event-json.d.ts +30 -0
  136. package/dist/src/ui-stream/agent-event-json.js +64 -0
  137. package/dist/src/ui-stream/agent-event-json.js.map +1 -0
  138. package/dist/src/ui-stream/agent-event-sse.d.ts +155 -0
  139. package/dist/src/ui-stream/agent-event-sse.js +167 -0
  140. package/dist/src/ui-stream/agent-event-sse.js.map +1 -0
  141. package/dist/src/ui-stream/agent-event-stream-positions.d.ts +42 -0
  142. package/dist/src/ui-stream/agent-event-stream-positions.js +153 -0
  143. package/dist/src/ui-stream/agent-event-stream-positions.js.map +1 -0
  144. package/dist/src/ui-stream/agent-session-record-reader.d.ts +74 -0
  145. package/dist/src/ui-stream/agent-session-record-reader.js +246 -0
  146. package/dist/src/ui-stream/agent-session-record-reader.js.map +1 -0
  147. package/dist/src/ui-stream/agent-ui-message-stream-responses.d.ts +219 -0
  148. package/dist/src/ui-stream/agent-ui-message-stream-responses.js +441 -0
  149. package/dist/src/ui-stream/agent-ui-message-stream-responses.js.map +1 -0
  150. package/dist/src/ui-stream/agent-ui-message-stream.d.ts +138 -0
  151. package/dist/src/ui-stream/agent-ui-message-stream.js +695 -0
  152. package/dist/src/ui-stream/agent-ui-message-stream.js.map +1 -0
  153. package/dist/src/ui-stream/agent-ui-message-types.d.ts +238 -0
  154. package/dist/src/ui-stream/agent-ui-message-types.js +57 -0
  155. package/dist/src/ui-stream/agent-ui-message-types.js.map +1 -0
  156. package/dist/src/ui-stream/agent-ui-messages.d.ts +68 -0
  157. package/dist/src/ui-stream/agent-ui-messages.js +222 -0
  158. package/dist/src/ui-stream/agent-ui-messages.js.map +1 -0
  159. package/dist/src/ui-stream/agent-ui-stream-cursor.d.ts +64 -0
  160. package/dist/src/ui-stream/agent-ui-stream-cursor.js +186 -0
  161. package/dist/src/ui-stream/agent-ui-stream-cursor.js.map +1 -0
  162. package/dist/src/ui-stream/index.d.ts +14 -0
  163. package/dist/src/ui-stream/index.js +22 -0
  164. package/dist/src/ui-stream/index.js.map +1 -0
  165. package/dist/src/ui-stream/server-sent-event-responses.d.ts +6 -0
  166. package/dist/src/ui-stream/server-sent-event-responses.js +34 -0
  167. package/dist/src/ui-stream/server-sent-event-responses.js.map +1 -0
  168. package/package.json +44 -5
@@ -0,0 +1,246 @@
1
+ // Copyright (c) 2026 Super Durable, Inc.
2
+ //
3
+ // Licensed under the Sustainable Use License 1.0.
4
+ // You may not use this file except in compliance with the License.
5
+ // See the LICENSE file in the repository root.
6
+ //
7
+ // SPDX-License-Identifier: LicenseRef-Sustainable-Use-1.0
8
+ import { setTimeout as delay } from "node:timers/promises";
9
+ import { FlowNotFoundError } from "@superdurable/dex";
10
+ import { readSessionFailure } from "../agent-session-failure.js";
11
+ import { messageSequenceKey } from "../agent-session-persistence.js";
12
+ import { findMessageChunkRange } from "../agent-session-transitions.js";
13
+ import { parseTurnNumber } from "./agent-event-stream-positions.js";
14
+ const committedRecordWaitMs = 3_000;
15
+ const committedRecordPollIntervalMs = 25;
16
+ const storedMessagePageSize = 50;
17
+ const recentStoredMessageCount = 200;
18
+ const maximumStoredMessageReadAttempts = 3;
19
+ /** Reads committed session state for the UI streams; Attribute reads need no running Worker. */
20
+ export class AgentSessionRecordReader {
21
+ client;
22
+ flow;
23
+ flowId;
24
+ constructor(client, flow, flowId) {
25
+ this.client = client;
26
+ this.flow = flow;
27
+ this.flowId = flowId;
28
+ }
29
+ async readAgentState() {
30
+ const [state] = await this.client.getAttributes(this.flowId, this.flow.agentState);
31
+ return state;
32
+ }
33
+ /**
34
+ * Waits until a turn's end committed, because its end event reaches the Stream before the commit.
35
+ * @returns `true` once the turn is no longer active; `false` after 3 seconds without that commit.
36
+ */
37
+ async waitForTurnToEnd(turnId) {
38
+ const deadline = Date.now() + committedRecordWaitMs;
39
+ for (;;) {
40
+ const state = await this.readAgentState();
41
+ if (state?.activeTurnId !== turnId) {
42
+ return true;
43
+ }
44
+ if (Date.now() >= deadline) {
45
+ return false;
46
+ }
47
+ await delay(committedRecordPollIntervalMs);
48
+ }
49
+ }
50
+ /**
51
+ * Reads the IDs of the tool calls whose approval `resolve_interrupt` still accepts.
52
+ * @returns The tool call IDs of the pending `tool_approval` interrupts.
53
+ */
54
+ async readPendingApprovalToolCallIds() {
55
+ const [interrupts] = await this.client.getAttributes(this.flowId, this.flow.pendingInterrupts);
56
+ return new Set((interrupts ?? [])
57
+ .filter((interrupt) => interrupt.kind === "tool_approval")
58
+ .map((interrupt) => interrupt.toolCallId));
59
+ }
60
+ async isFlowRunning() {
61
+ try {
62
+ return (await this.client.describeFlow(this.flowId)).status === "running";
63
+ }
64
+ catch (error) {
65
+ if (error instanceof FlowNotFoundError) {
66
+ return false;
67
+ }
68
+ throw error;
69
+ }
70
+ }
71
+ /** Returns why the session's Flow failed, or `undefined` when it did not. */
72
+ readFlowFailure() {
73
+ return readSessionFailure(this.client, this.flowId);
74
+ }
75
+ /**
76
+ * Reads what the session holds about one tool call of a turn.
77
+ *
78
+ * Events reach the Stream before their Step commits, so it polls the record until `isReady` holds, for
79
+ * up to 3 seconds. A turn that ended deleted its records; then it reads the turn's stored messages.
80
+ * @returns The record when it exists, plus the tool name, arguments, and stored result text from the
81
+ * record or the stored messages; each is `undefined` when neither holds it.
82
+ */
83
+ async readToolCallDetails(turnId, toolCallId, isReady) {
84
+ const record = await this.waitForToolCall(turnId, toolCallId, isReady);
85
+ if (record !== undefined) {
86
+ return { record, toolName: record.toolName, toolArguments: record.arguments, storedToolOutput: undefined };
87
+ }
88
+ const storedMessages = await this.readStoredToolMessages(turnId, toolCallId);
89
+ return {
90
+ record: undefined,
91
+ toolName: storedMessages.toolCall?.toolName ?? storedMessages.toolResult?.toolName ?? undefined,
92
+ toolArguments: storedMessages.toolCall?.toolArguments ?? undefined,
93
+ storedToolOutput: storedMessages.toolResult?.toolOutput ?? undefined,
94
+ };
95
+ }
96
+ async waitForToolCall(turnId, toolCallId, isReady) {
97
+ const deadline = Date.now() + committedRecordWaitMs;
98
+ for (;;) {
99
+ const [record] = await this.client.getAttributes(this.flowId, this.flow.agentToolCalls.instance(toolCallId));
100
+ const turnRecord = record?.turnId === turnId ? record : undefined;
101
+ if ((turnRecord !== undefined && isReady(turnRecord)) || Date.now() >= deadline) {
102
+ return turnRecord;
103
+ }
104
+ if (turnRecord === undefined && (await this.readAgentState())?.activeTurnId !== turnId) {
105
+ return undefined;
106
+ }
107
+ await delay(committedRecordPollIntervalMs);
108
+ }
109
+ }
110
+ /**
111
+ * Waits up to 3 seconds for a tool call record to be committed, because events precede their commit.
112
+ * @returns The record, or `undefined` when it never appeared or the turn is no longer active.
113
+ */
114
+ async readCommittedToolCall(turnId, toolCallId) {
115
+ return this.waitForToolCall(turnId, toolCallId, () => true);
116
+ }
117
+ /**
118
+ * Reads one tool call record once, without waiting for a commit.
119
+ * @returns The record when it exists and belongs to `turnId`, otherwise `undefined`.
120
+ */
121
+ async readToolCall(turnId, toolCallId) {
122
+ const [record] = await this.client.getAttributes(this.flowId, this.flow.agentToolCalls.instance(toolCallId));
123
+ return record?.turnId === turnId ? record : undefined;
124
+ }
125
+ /**
126
+ * Reads the stored tool results among the last 200 stored messages of any turn.
127
+ * @returns The results in sequence order, each with its turn, tool call ID, and the text the model saw.
128
+ */
129
+ async readRecentStoredToolResults() {
130
+ const state = await this.readAgentState();
131
+ const endSequence = state?.nextMessageSequence ?? 1;
132
+ const startSequence = Math.max(1, endSequence - recentStoredMessageCount);
133
+ if (startSequence >= endSequence) {
134
+ return [];
135
+ }
136
+ const results = [];
137
+ for (const message of (await this.readStoredMessages(startSequence, endSequence)).messages) {
138
+ const toolOutput = message?.toolOutput ?? message?.text;
139
+ if (message?.role === "tool" && message.toolCallId !== null && toolOutput != null) {
140
+ results.push({ turnId: message.turnId, toolCallId: message.toolCallId, toolOutput });
141
+ }
142
+ }
143
+ return results;
144
+ }
145
+ /** Finds a tool call's stored request and result messages, newest pages first, within its turn. */
146
+ async readStoredToolMessages(turnId, toolCallId) {
147
+ const state = await this.readAgentState();
148
+ const turnNumber = parseTurnNumber(turnId);
149
+ const found = {};
150
+ let endSequence = state?.nextMessageSequence ?? 1;
151
+ let firstSequence = 1;
152
+ while (endSequence > firstSequence) {
153
+ const startSequence = Math.max(firstSequence, endSequence - storedMessagePageSize);
154
+ const page = await this.readStoredMessages(startSequence, endSequence);
155
+ const messages = page.messages;
156
+ firstSequence = page.firstSequence;
157
+ let hasEarlierTurn = false;
158
+ for (const message of messages) {
159
+ if (message === undefined) {
160
+ continue;
161
+ }
162
+ if (message.turnId !== turnId) {
163
+ hasEarlierTurn ||= (parseTurnNumber(message.turnId) ?? 0) < (turnNumber ?? 0);
164
+ continue;
165
+ }
166
+ if (message.toolCallId === toolCallId) {
167
+ if (message.role === "tool") {
168
+ found.toolResult = message;
169
+ }
170
+ else {
171
+ found.toolCall = message;
172
+ }
173
+ }
174
+ }
175
+ if (hasEarlierTurn || (found.toolCall !== undefined && found.toolResult !== undefined)) {
176
+ return found;
177
+ }
178
+ endSequence = startSequence;
179
+ }
180
+ return found;
181
+ }
182
+ /**
183
+ * Reads stored messages with sequences in `[startSequence, endSequence)`, in sequence order, from the
184
+ * `AgentMessages` window and the `AgentMessageChunks` instances that `AgentMessageIndex` names. A message
185
+ * that retention deleted reads as `undefined`. A commit between the index read and the instance reads moves
186
+ * messages, so the read repeats with the new index.
187
+ * @returns The messages and the sequence of the oldest stored message.
188
+ * @throws Error when the index keeps changing for every attempt, or a stored message is missing.
189
+ */
190
+ async readStoredMessages(startSequence, endSequence) {
191
+ let [index] = await this.client.getAttributes(this.flowId, this.flow.agentMessageIndex);
192
+ for (let attempt = 1;; attempt += 1) {
193
+ const firstSequence = index?.firstSequence ?? 1;
194
+ const chunkStarts = new Set();
195
+ const windowSequences = [];
196
+ for (let sequence = Math.max(firstSequence, startSequence); sequence < endSequence; sequence += 1) {
197
+ const range = index === undefined ? undefined : findMessageChunkRange(index, sequence);
198
+ if (range === undefined) {
199
+ windowSequences.push(sequence);
200
+ }
201
+ else {
202
+ chunkStarts.add(range.firstSequence);
203
+ }
204
+ }
205
+ const chunkReads = [...chunkStarts].map((chunkStart) => this.flow.agentMessageChunks.instance(messageSequenceKey(chunkStart)));
206
+ const windowReads = windowSequences.map((sequence) => this.flow.agentMessages.instance(messageSequenceKey(sequence)));
207
+ const values = (await this.client.getAttributes(this.flowId, ...[this.flow.agentMessageIndex, ...chunkReads, ...windowReads]));
208
+ const readIndex = values[0];
209
+ // A rollover or retention between the two reads moved messages, so read again with the new index.
210
+ if (!isSameMessageLayout(index, readIndex)) {
211
+ if (attempt >= maximumStoredMessageReadAttempts) {
212
+ throw new Error(`the stored messages of session ${this.flowId} moved during ${maximumStoredMessageReadAttempts} reads`);
213
+ }
214
+ index = readIndex;
215
+ continue;
216
+ }
217
+ const bySequence = new Map();
218
+ for (const value of values.slice(1, 1 + chunkReads.length)) {
219
+ for (const message of value?.messages ?? []) {
220
+ bySequence.set(message.sequence, message);
221
+ }
222
+ }
223
+ for (const [offset, value] of values.slice(1 + chunkReads.length).entries()) {
224
+ const sequence = windowSequences[offset];
225
+ if (sequence !== undefined && value !== undefined) {
226
+ bySequence.set(sequence, value);
227
+ }
228
+ }
229
+ const messages = [];
230
+ for (let sequence = startSequence; sequence < endSequence; sequence += 1) {
231
+ const message = bySequence.get(sequence);
232
+ if (message === undefined && sequence >= firstSequence) {
233
+ throw new Error(`session ${this.flowId} has no stored message ${sequence}`);
234
+ }
235
+ messages.push(message);
236
+ }
237
+ return { messages, firstSequence };
238
+ }
239
+ }
240
+ }
241
+ function isSameMessageLayout(left, right) {
242
+ return (left?.firstSequence === right?.firstSequence &&
243
+ left?.windowFirstSequence === right?.windowFirstSequence &&
244
+ left?.chunks.length === right?.chunks.length);
245
+ }
246
+ //# sourceMappingURL=agent-session-record-reader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-session-record-reader.js","sourceRoot":"","sources":["../../../src/ui-stream/agent-session-record-reader.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,EAAE;AACF,kDAAkD;AAClD,mEAAmE;AACnE,+CAA+C;AAC/C,EAAE;AACF,0DAA0D;AAE1D,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE3D,OAAO,EAAE,iBAAiB,EAAmC,MAAM,mBAAmB,CAAC;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AC7C,CAAC;AACJ,CAAC"}
@@ -0,0 +1,219 @@
1
+ import { type Client } from "@superdurable/dex";
2
+ import type { AgentFrameworkRunner, DeferredToolOutcome } from "../agent-framework-runner.js";
3
+ import type { AgentSessionFlow } from "../agent-session-flow.js";
4
+ import { type AgentUIMessageStreamOptions } from "./agent-ui-message-stream.js";
5
+ /**
6
+ * Request header name for a UI stream resume token.
7
+ *
8
+ * Send the token in this header rather than the URL: a token can reach 64 KiB, beyond many URL limits.
9
+ */
10
+ export declare const agentResumeTokenHeaderName = "x-agent-resume-token";
11
+ /** {@link AgentUIMessageStreamOptions} plus headers to add to the Response. */
12
+ export interface AgentUIMessageStreamResponseOptions extends AgentUIMessageStreamOptions {
13
+ /** Extra response headers; they override the protocol headers with the same name. */
14
+ readonly headers?: Readonly<Record<string, string>>;
15
+ }
16
+ /** Options of {@link sendAgentUserMessageWithUIMessageStream}. */
17
+ export interface AgentUserMessageUIStreamOptions extends Omit<AgentUIMessageStreamResponseOptions, "resumeToken"> {
18
+ /** Text of the user message. */
19
+ readonly text: string;
20
+ }
21
+ /**
22
+ * One tool approval decision, the content of an AI SDK `tool-approval-response`.
23
+ *
24
+ * `useChat` records it on a tool part when the application calls `addToolApprovalResponse`.
25
+ */
26
+ export interface AgentToolApprovalResponse {
27
+ /** Approval ID from the `tool-approval-request` part: the `interrupt_id` of the pending interrupt. */
28
+ readonly approvalId: string;
29
+ /** Whether the tool call may run. */
30
+ readonly approved: boolean;
31
+ /** Message the model sees after a rejection. Ignored on approval. */
32
+ readonly reason?: string | undefined;
33
+ /**
34
+ * Turn whose assistant message holds the decision, such as `"turn-3"`, or `undefined` when unknown.
35
+ * {@link resolveAgentToolApprovalsWithUIMessageStream} treats a decision bound to another turn than the
36
+ * active one as stale, so a stale browser tab cannot answer a later turn's call that reuses a tool call
37
+ * ID. {@link readAgentToolApprovalResponses} sets it from the message ID.
38
+ */
39
+ readonly turnId?: string | undefined;
40
+ }
41
+ /** Options of {@link resolveAgentToolApproval}. */
42
+ export interface AgentToolApprovalOptions {
43
+ /** Client used for the RPC. */
44
+ readonly client: Client;
45
+ /** The agent session Flow. */
46
+ readonly flow: AgentSessionFlow<AgentFrameworkRunner<DeferredToolOutcome>>;
47
+ /** Session ID: the Flow ID of the agent session. */
48
+ readonly flowId: string;
49
+ /** The decision to send. */
50
+ readonly approval: AgentToolApprovalResponse;
51
+ }
52
+ /** Options of {@link resolveAgentToolApprovalsWithUIMessageStream}. */
53
+ export interface AgentToolApprovalsUIStreamOptions extends AgentUIMessageStreamResponseOptions {
54
+ /**
55
+ * Decisions from the browser, such as the result of {@link readAgentToolApprovalResponses}. They are
56
+ * untrusted: only the first decision per approval ID counts, at most 100 are read, and only interrupts
57
+ * pending in the session's active turn are resolved, by decisions bound to that turn or to none.
58
+ */
59
+ readonly approvals: readonly AgentToolApprovalResponse[];
60
+ }
61
+ /**
62
+ * Returns a streaming AI SDK UI message stream Response for one agent turn, or 204 when there is none.
63
+ *
64
+ * Mount it as the reconnect endpoint of `useChat`, `GET <api>/<chatId>/stream` by default, with the chat ID
65
+ * as the session ID; the route must check that the caller may read that session. It writes
66
+ * {@link streamAgentUIMessageChunks} as Server-Sent Events with the protocol headers, a keep-alive
67
+ * comment after each empty long poll, and `data: [DONE]` at the end. It stops reading when the browser
68
+ * closes the response.
69
+ *
70
+ * Use the default `DefaultChatTransport`, whose resumed streams count as replays. A stream started with a
71
+ * token replays the token's turn, and one started without a token replays the active turn; either way
72
+ * its `start` part makes the browser reset the message and rebuild it, whatever it held. With
73
+ * `continueAfterResumeToken`, a stream continues exactly after the last part the browser received and
74
+ * sends no `start` part; use it only while the browser still holds that message. Before it answers 200,
75
+ * the helper reads once to check the token's Dex position; a position Dex rejects restarts the turn from
76
+ * its first retained event.
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * // Server: GET /api/chat/:id/stream
81
+ * export async function GET(request: Request, sessionId: string): Promise<Response> {
82
+ * await authorizeSession(request, sessionId);
83
+ * return createAgentUIMessageStreamResponse({
84
+ * client, flow, flowId: sessionId,
85
+ * resumeToken: request.headers.get(agentResumeTokenHeaderName) ?? undefined,
86
+ * signal: request.signal,
87
+ * });
88
+ * }
89
+ *
90
+ * // Browser
91
+ * let resumeToken: string | undefined = initial.resumeToken ?? undefined;
92
+ * const chat = useChat({
93
+ * id: sessionId,
94
+ * messages: initial.messages,
95
+ * resume: resumeToken !== undefined,
96
+ * onData: (part) => {
97
+ * if (part.type === "data-agent-resume-token") resumeToken = part.data as string;
98
+ * },
99
+ * transport: new DefaultChatTransport({
100
+ * api: "/api/chat",
101
+ * prepareReconnectToStreamRequest: ({ id }) => ({
102
+ * api: `/api/chat/${id}/stream`,
103
+ * headers: resumeToken === undefined ? {} : { "x-agent-resume-token": resumeToken },
104
+ * }),
105
+ * }),
106
+ * sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses,
107
+ * });
108
+ * ```
109
+ * @param options - Session, start position, polling, and extra headers.
110
+ * @returns 200 with the stream; 204 when the token's turn already finished, or when the turn to replay is
111
+ * not active or not on the Stream; 400 with a text body when `resumeToken` is invalid.
112
+ * @throws {RangeError} When `pollTimeoutMs` is not a positive whole number of seconds.
113
+ * @throws {DexServiceError} When reading the start position fails.
114
+ */
115
+ export declare function createAgentUIMessageStreamResponse(options: AgentUIMessageStreamResponseOptions): Promise<Response>;
116
+ /**
117
+ * Queues a user message and returns the AI SDK UI message stream Response of the turn it starts.
118
+ *
119
+ * Mount it as the send endpoint of `useChat`, `POST <api>` by default, when the last message in the
120
+ * request body is a user message. The chat ID in the body comes from the browser, so the route must check
121
+ * that the caller may write to that session. The helper reads the session's turn count and the Stream
122
+ * position first and then calls `send_user_message`, so the stream follows the next turn that starts
123
+ * after the message was queued. When an earlier turn is still running, the stream waits for it to end.
124
+ * When other messages were queued before this one, the stream follows the turn of the oldest of them.
125
+ *
126
+ * The session's committed state ends the stream even when no event of the turn reaches the Stream, such
127
+ * as with the Server's Stream Store disabled: once that turn ended, the stream finishes with reason
128
+ * `other`, and the application reloads the stored messages. The RPC needs a running Worker.
129
+ *
130
+ * @example
131
+ * ```ts
132
+ * // POST /api/chat
133
+ * export async function POST(request: Request): Promise<Response> {
134
+ * const { id, messages } = await request.json();
135
+ * await authorizeSession(request, id);
136
+ * const lastMessage = messages.at(-1);
137
+ * if (lastMessage.role === "assistant") {
138
+ * return resolveAgentToolApprovalsWithUIMessageStream({
139
+ * client, flow, flowId: id, approvals: readAgentToolApprovalResponses(lastMessage) });
140
+ * }
141
+ * const text = lastMessage.parts.filter((part) => part.type === "text").map((part) => part.text).join("");
142
+ * return sendAgentUserMessageWithUIMessageStream({ client, flow, flowId: id, text, signal: request.signal });
143
+ * }
144
+ * ```
145
+ * @param options - Session, message text, polling, and extra headers.
146
+ * @returns 200 with the stream; 400 with a text body when the text is blank.
147
+ * @throws {RangeError} When `pollTimeoutMs` is not a positive whole number of seconds.
148
+ * @throws {FlowNotActiveOrNotFoundError} When the session is not running.
149
+ * @throws {DexServiceError} When a read or the RPC fails.
150
+ */
151
+ export declare function sendAgentUserMessageWithUIMessageStream(options: AgentUserMessageUIStreamOptions): Promise<Response>;
152
+ /**
153
+ * Sends one tool approval decision to the session's `resolve_interrupt` RPC.
154
+ *
155
+ * Call it with each AI SDK `tool-approval-response`: its `approvalId` is the `interrupt_id`, and its
156
+ * `reason` becomes the rejection message the model sees. It retries `RpcLockConflictError`, which a call
157
+ * that races the approval Step can raise, for up to 5 seconds. It resolves any pending interrupt of the
158
+ * session, so the route must check that the caller may decide for that session. The RPC needs a running
159
+ * Worker.
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * const isResolved = await resolveAgentToolApproval({
164
+ * client, flow, flowId: sessionId,
165
+ * approval: { approvalId: "call_42", approved: false, reason: "Use the sandbox account" },
166
+ * });
167
+ * ```
168
+ * @param options - Session and decision.
169
+ * @returns `true` when the interrupt was pending and is now resolved; `false` when it is unknown, already
170
+ * resolved, or past its deadline.
171
+ * @throws {RpcLockConflictError} When the lock stays busy for 5 seconds.
172
+ * @throws {FlowNotActiveOrNotFoundError} When the session is not running.
173
+ */
174
+ export declare function resolveAgentToolApproval(options: AgentToolApprovalOptions): Promise<boolean>;
175
+ /**
176
+ * Returns the tool approval decisions recorded on an AI SDK assistant message.
177
+ *
178
+ * `useChat` sets a tool part to `approval-responded` when the application calls
179
+ * `addToolApprovalResponse`, then sends the message list with `sendAutomaticallyWhen`. Pass the last
180
+ * message of that request body. The message comes from the browser, so every field is checked; parts
181
+ * that are not well-formed responded approvals are skipped.
182
+ *
183
+ * Each decision is bound to the message's turn when the message ID is `<turnId>-assistant`, as the agent UI
184
+ * streams and snapshots name assistant messages; a message with another ID gives unbound decisions.
185
+ *
186
+ * @param message - An AI SDK `UIMessage`, typically the last message of a `useChat` request body.
187
+ * @returns One decision per `approval-responded` tool part, in part order; empty for anything else.
188
+ */
189
+ export declare function readAgentToolApprovalResponses(message: unknown): AgentToolApprovalResponse[];
190
+ /**
191
+ * Sends tool approval decisions and returns the AI SDK UI message stream Response that continues the turn.
192
+ *
193
+ * Mount it in the send endpoint of `useChat` for requests whose last message is an assistant message,
194
+ * which `sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses` sends after the
195
+ * application answered every approval. The decisions and the chat ID come from the browser, so the route
196
+ * must check that the caller may decide for that session. Only interrupts that are pending in the
197
+ * session's active turn are resolved.
198
+ *
199
+ * The stream continues the same assistant message. A decision that is no longer pending, because another
200
+ * client or a deadline settled the call or its turn ended, gets the call's known outcome, or an error
201
+ * part when none is known, so the browser leaves `approval-responded` and does not send the decision
202
+ * again. Decisions bound to another turn than the active one, such as from a stale browser tab, are all
203
+ * stale, and the stream then ends after their outcomes. With `resumeToken`, the latest `data-agent-resume-token`, the stream continues exactly after the
204
+ * paused stream. Without one it reads the Stream position and the active turn's tool calls before
205
+ * sending the decisions. The RPCs need a running Worker.
206
+ *
207
+ * @example
208
+ * ```ts
209
+ * return resolveAgentToolApprovalsWithUIMessageStream({
210
+ * client, flow, flowId: id, approvals: readAgentToolApprovalResponses(messages.at(-1)) });
211
+ * ```
212
+ * @param options - Session, decisions, optional resume token, polling, and extra headers.
213
+ * @returns 200 with the stream, which holds only the stale decisions' outcomes and a `finish` part when
214
+ * no turn is active; 400 with a text body when `resumeToken` is invalid.
215
+ * @throws {RangeError} When `pollTimeoutMs` is not a positive whole number of seconds.
216
+ * @throws {RpcLockConflictError} When a decision's lock stays busy for 5 seconds.
217
+ * @throws {DexServiceError} When a read or an RPC fails.
218
+ */
219
+ export declare function resolveAgentToolApprovalsWithUIMessageStream(options: AgentToolApprovalsUIStreamOptions): Promise<Response>;