@tanstack/ai-acp 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,320 +1,298 @@
1
1
  import { EventType, buildBaseUsage } from "@tanstack/ai";
2
- const BRIDGED_MCP_SERVER_NAME = "tanstack";
2
+ //#region src/stream/translate.ts
3
+ var BRIDGED_MCP_SERVER_NAME = "tanstack";
3
4
  function matchBridgedToolName(title, bridgedToolNames) {
4
- if (!title || !bridgedToolNames) return void 0;
5
- if (bridgedToolNames.has(title)) return title;
6
- for (const name of bridgedToolNames) {
7
- if (title.startsWith(`${name} (`)) return name;
8
- }
9
- return void 0;
5
+ if (!title || !bridgedToolNames) return void 0;
6
+ if (bridgedToolNames.has(title)) return title;
7
+ for (const name of bridgedToolNames) if (title.startsWith(`${name} (`)) return name;
10
8
  }
11
9
  function resolveToolName(update, bridgedToolNames) {
12
- return matchBridgedToolName(update.title, bridgedToolNames) ?? update.kind ?? "tool";
10
+ return matchBridgedToolName(update.title, bridgedToolNames) ?? update.kind ?? "tool";
13
11
  }
14
12
  function stringifyToolOutput(update) {
15
- if (update.rawOutput !== void 0) {
16
- return typeof update.rawOutput === "string" ? update.rawOutput : JSON.stringify(update.rawOutput);
17
- }
18
- const blocks = update.content ?? [];
19
- const text = blocks.map(
20
- (block) => block.content && typeof block.content.text === "string" ? block.content.text : ""
21
- ).join("");
22
- const hasNonText = blocks.some(
23
- (block) => block.type !== "content" || block.content !== void 0 && block.content.type !== "text"
24
- );
25
- if (hasNonText) return JSON.stringify(blocks);
26
- if (text !== "") return text;
27
- return JSON.stringify({ status: update.status ?? "completed" });
13
+ if (update.rawOutput !== void 0) return typeof update.rawOutput === "string" ? update.rawOutput : JSON.stringify(update.rawOutput);
14
+ const blocks = update.content ?? [];
15
+ const text = blocks.map((block) => block.content && typeof block.content.text === "string" ? block.content.text : "").join("");
16
+ if (blocks.some((block) => block.type !== "content" || block.content !== void 0 && block.content.type !== "text")) return JSON.stringify(blocks);
17
+ if (text !== "") return text;
18
+ return JSON.stringify({ status: update.status ?? "completed" });
28
19
  }
29
20
  function buildUsage(usage) {
30
- if (!usage) return void 0;
31
- const promptTokens = usage.inputTokens ?? 0;
32
- const completionTokens = usage.outputTokens ?? 0;
33
- const result = buildBaseUsage({
34
- promptTokens,
35
- completionTokens,
36
- totalTokens: usage.totalTokens ?? promptTokens + completionTokens
37
- });
38
- if (usage.cachedReadTokens) {
39
- result.promptTokensDetails = { cachedTokens: usage.cachedReadTokens };
40
- }
41
- if (usage.thoughtTokens) {
42
- result.completionTokensDetails = { reasoningTokens: usage.thoughtTokens };
43
- }
44
- return result;
21
+ if (!usage) return void 0;
22
+ const promptTokens = usage.inputTokens ?? 0;
23
+ const completionTokens = usage.outputTokens ?? 0;
24
+ const result = buildBaseUsage({
25
+ promptTokens,
26
+ completionTokens,
27
+ totalTokens: usage.totalTokens ?? promptTokens + completionTokens
28
+ });
29
+ if (usage.cachedReadTokens) result.promptTokensDetails = { cachedTokens: usage.cachedReadTokens };
30
+ if (usage.thoughtTokens) result.completionTokensDetails = { reasoningTokens: usage.thoughtTokens };
31
+ return result;
45
32
  }
46
33
  async function* translateAcpStream(events, ctx) {
47
- const { model, runId, threadId, genId, labels } = ctx;
48
- const now = () => Date.now();
49
- let runStarted = false;
50
- const unresolvedToolCalls = /* @__PURE__ */ new Set();
51
- const knownToolCalls = /* @__PURE__ */ new Set();
52
- let textMessageId = null;
53
- let textContent = "";
54
- let reasoningId = null;
55
- function* startRun() {
56
- if (runStarted) return;
57
- runStarted = true;
58
- yield {
59
- type: EventType.RUN_STARTED,
60
- runId,
61
- threadId,
62
- model,
63
- timestamp: now(),
64
- ...ctx.parentRunId !== void 0 && { parentRunId: ctx.parentRunId }
65
- };
66
- }
67
- function* closeText() {
68
- if (textMessageId !== null) {
69
- yield {
70
- type: EventType.TEXT_MESSAGE_END,
71
- messageId: textMessageId,
72
- model,
73
- timestamp: now()
74
- };
75
- }
76
- textMessageId = null;
77
- textContent = "";
78
- }
79
- function* closeReasoning() {
80
- if (reasoningId !== null) {
81
- yield {
82
- type: EventType.REASONING_MESSAGE_END,
83
- messageId: reasoningId,
84
- model,
85
- timestamp: now()
86
- };
87
- yield {
88
- type: EventType.REASONING_END,
89
- messageId: reasoningId,
90
- model,
91
- timestamp: now()
92
- };
93
- }
94
- reasoningId = null;
95
- }
96
- function* synthesizeUnresolvedResults() {
97
- for (const toolCallId of unresolvedToolCalls) {
98
- yield {
99
- type: EventType.TOOL_CALL_RESULT,
100
- toolCallId,
101
- messageId: genId(),
102
- model,
103
- timestamp: now(),
104
- content: JSON.stringify({ status: "interrupted" })
105
- };
106
- }
107
- unresolvedToolCalls.clear();
108
- }
109
- function* openToolCall(update) {
110
- if (knownToolCalls.has(update.toolCallId)) return;
111
- knownToolCalls.add(update.toolCallId);
112
- const toolCallName = resolveToolName(update, ctx.bridgedToolNames);
113
- const input = {
114
- ...update.title != null && { title: update.title },
115
- ...update.rawInput !== void 0 && update.rawInput !== null ? typeof update.rawInput === "object" ? update.rawInput : { input: update.rawInput } : {}
116
- };
117
- const args = JSON.stringify(input);
118
- yield {
119
- type: EventType.TOOL_CALL_START,
120
- toolCallId: update.toolCallId,
121
- toolCallName,
122
- toolName: toolCallName,
123
- model,
124
- timestamp: now()
125
- };
126
- yield {
127
- type: EventType.TOOL_CALL_ARGS,
128
- toolCallId: update.toolCallId,
129
- model,
130
- timestamp: now(),
131
- delta: args,
132
- args
133
- };
134
- yield {
135
- type: EventType.TOOL_CALL_END,
136
- toolCallId: update.toolCallId,
137
- toolCallName,
138
- toolName: toolCallName,
139
- model,
140
- timestamp: now(),
141
- input
142
- };
143
- unresolvedToolCalls.add(update.toolCallId);
144
- }
145
- function* resolveToolCall(update) {
146
- yield* openToolCall(update);
147
- unresolvedToolCalls.delete(update.toolCallId);
148
- yield {
149
- type: EventType.TOOL_CALL_RESULT,
150
- toolCallId: update.toolCallId,
151
- messageId: genId(),
152
- model,
153
- timestamp: now(),
154
- content: stringifyToolOutput(update),
155
- ...update.status === "failed" && { state: "output-error" }
156
- };
157
- }
158
- function* handleUpdate(update) {
159
- if (update.sessionUpdate === "agent_message_chunk") {
160
- yield* closeReasoning();
161
- if (update.content.type !== "text") {
162
- if (labels.contentEvent !== void 0) {
163
- yield* closeText();
164
- yield {
165
- type: EventType.CUSTOM,
166
- model,
167
- timestamp: now(),
168
- name: labels.contentEvent,
169
- value: { content: update.content }
170
- };
171
- }
172
- return;
173
- }
174
- const text = typeof update.content.text === "string" ? update.content.text : "";
175
- if (text === "") return;
176
- if (textMessageId === null) {
177
- textMessageId = genId();
178
- yield {
179
- type: EventType.TEXT_MESSAGE_START,
180
- messageId: textMessageId,
181
- model,
182
- timestamp: now(),
183
- role: "assistant"
184
- };
185
- }
186
- textContent += text;
187
- yield {
188
- type: EventType.TEXT_MESSAGE_CONTENT,
189
- messageId: textMessageId,
190
- model,
191
- timestamp: now(),
192
- delta: text,
193
- content: textContent
194
- };
195
- } else if (update.sessionUpdate === "agent_thought_chunk") {
196
- yield* closeText();
197
- const thought = typeof update.content.text === "string" ? update.content.text : "";
198
- if (thought === "") return;
199
- if (reasoningId === null) {
200
- reasoningId = genId();
201
- yield {
202
- type: EventType.REASONING_START,
203
- messageId: reasoningId,
204
- model,
205
- timestamp: now()
206
- };
207
- yield {
208
- type: EventType.REASONING_MESSAGE_START,
209
- messageId: reasoningId,
210
- role: "reasoning",
211
- model,
212
- timestamp: now()
213
- };
214
- }
215
- yield {
216
- type: EventType.REASONING_MESSAGE_CONTENT,
217
- messageId: reasoningId,
218
- delta: thought,
219
- model,
220
- timestamp: now()
221
- };
222
- } else if (update.sessionUpdate === "tool_call") {
223
- yield* closeText();
224
- yield* closeReasoning();
225
- yield* openToolCall(update);
226
- if (update.status === "completed" || update.status === "failed") {
227
- yield* resolveToolCall(update);
228
- }
229
- } else if (update.sessionUpdate === "tool_call_update") {
230
- if (update.status === "completed" || update.status === "failed") {
231
- yield* resolveToolCall(update);
232
- } else if (update.status === "in_progress" && update.rawInput !== void 0) {
233
- yield* closeText();
234
- yield* closeReasoning();
235
- if (!knownToolCalls.has(update.toolCallId)) {
236
- yield* openToolCall(update);
237
- } else {
238
- const input = {
239
- ...update.title != null && { title: update.title },
240
- ...typeof update.rawInput === "object" && update.rawInput !== null ? update.rawInput : { input: update.rawInput }
241
- };
242
- const args = JSON.stringify(input);
243
- yield {
244
- type: EventType.TOOL_CALL_ARGS,
245
- toolCallId: update.toolCallId,
246
- model,
247
- timestamp: now(),
248
- delta: args,
249
- args
250
- };
251
- }
252
- }
253
- } else if (update.sessionUpdate === "plan" && labels.planEvent !== void 0) {
254
- yield {
255
- type: EventType.CUSTOM,
256
- model,
257
- timestamp: now(),
258
- name: labels.planEvent,
259
- value: { entries: update.entries }
260
- };
261
- }
262
- }
263
- try {
264
- for await (const event of events) {
265
- ctx.onAcpEvent?.(event);
266
- if (event.kind === "session") {
267
- yield* startRun();
268
- yield {
269
- type: EventType.CUSTOM,
270
- model,
271
- timestamp: now(),
272
- name: labels.sessionIdEvent,
273
- value: { sessionId: event.sessionId }
274
- };
275
- } else if (event.kind === "update") {
276
- yield* startRun();
277
- yield* handleUpdate(event.update);
278
- } else {
279
- yield* startRun();
280
- yield* closeText();
281
- yield* closeReasoning();
282
- yield* synthesizeUnresolvedResults();
283
- if (event.stopReason === "refusal") {
284
- const message = labels.refusalMessage ?? "The harness refused the request.";
285
- yield {
286
- type: EventType.RUN_ERROR,
287
- model,
288
- timestamp: now(),
289
- message,
290
- code: "refusal",
291
- error: { message, code: "refusal" }
292
- };
293
- } else {
294
- const usage = buildUsage(event.usage);
295
- const finishReason = event.stopReason === "max_tokens" || event.stopReason === "max_turn_requests" ? "length" : "stop";
296
- yield {
297
- type: EventType.RUN_FINISHED,
298
- runId,
299
- threadId,
300
- model,
301
- timestamp: now(),
302
- finishReason,
303
- ...usage !== void 0 && { usage }
304
- };
305
- }
306
- }
307
- }
308
- } catch (error) {
309
- yield* closeText();
310
- yield* closeReasoning();
311
- yield* synthesizeUnresolvedResults();
312
- throw error;
313
- }
34
+ const { model, runId, threadId, genId, labels } = ctx;
35
+ const now = () => Date.now();
36
+ let runStarted = false;
37
+ const unresolvedToolCalls = /* @__PURE__ */ new Set();
38
+ const knownToolCalls = /* @__PURE__ */ new Set();
39
+ let textMessageId = null;
40
+ let textContent = "";
41
+ let reasoningId = null;
42
+ function* startRun() {
43
+ if (runStarted) return;
44
+ runStarted = true;
45
+ yield {
46
+ type: EventType.RUN_STARTED,
47
+ runId,
48
+ threadId,
49
+ model,
50
+ timestamp: now(),
51
+ ...ctx.parentRunId !== void 0 && { parentRunId: ctx.parentRunId }
52
+ };
53
+ }
54
+ function* closeText() {
55
+ if (textMessageId !== null) yield {
56
+ type: EventType.TEXT_MESSAGE_END,
57
+ messageId: textMessageId,
58
+ model,
59
+ timestamp: now()
60
+ };
61
+ textMessageId = null;
62
+ textContent = "";
63
+ }
64
+ function* closeReasoning() {
65
+ if (reasoningId !== null) {
66
+ yield {
67
+ type: EventType.REASONING_MESSAGE_END,
68
+ messageId: reasoningId,
69
+ model,
70
+ timestamp: now()
71
+ };
72
+ yield {
73
+ type: EventType.REASONING_END,
74
+ messageId: reasoningId,
75
+ model,
76
+ timestamp: now()
77
+ };
78
+ }
79
+ reasoningId = null;
80
+ }
81
+ function* synthesizeUnresolvedResults() {
82
+ for (const toolCallId of unresolvedToolCalls) yield {
83
+ type: EventType.TOOL_CALL_RESULT,
84
+ toolCallId,
85
+ messageId: genId(),
86
+ model,
87
+ timestamp: now(),
88
+ content: JSON.stringify({ status: "interrupted" })
89
+ };
90
+ unresolvedToolCalls.clear();
91
+ }
92
+ function* openToolCall(update) {
93
+ if (knownToolCalls.has(update.toolCallId)) return;
94
+ knownToolCalls.add(update.toolCallId);
95
+ const toolCallName = resolveToolName(update, ctx.bridgedToolNames);
96
+ const input = {
97
+ ...update.title != null && { title: update.title },
98
+ ...update.rawInput !== void 0 && update.rawInput !== null ? typeof update.rawInput === "object" ? update.rawInput : { input: update.rawInput } : {}
99
+ };
100
+ const args = JSON.stringify(input);
101
+ yield {
102
+ type: EventType.TOOL_CALL_START,
103
+ toolCallId: update.toolCallId,
104
+ toolCallName,
105
+ toolName: toolCallName,
106
+ model,
107
+ timestamp: now()
108
+ };
109
+ yield {
110
+ type: EventType.TOOL_CALL_ARGS,
111
+ toolCallId: update.toolCallId,
112
+ model,
113
+ timestamp: now(),
114
+ delta: args,
115
+ args
116
+ };
117
+ yield {
118
+ type: EventType.TOOL_CALL_END,
119
+ toolCallId: update.toolCallId,
120
+ toolCallName,
121
+ toolName: toolCallName,
122
+ model,
123
+ timestamp: now(),
124
+ input
125
+ };
126
+ unresolvedToolCalls.add(update.toolCallId);
127
+ }
128
+ function* resolveToolCall(update) {
129
+ yield* openToolCall(update);
130
+ unresolvedToolCalls.delete(update.toolCallId);
131
+ yield {
132
+ type: EventType.TOOL_CALL_RESULT,
133
+ toolCallId: update.toolCallId,
134
+ messageId: genId(),
135
+ model,
136
+ timestamp: now(),
137
+ content: stringifyToolOutput(update),
138
+ ...update.status === "failed" && { state: "output-error" }
139
+ };
140
+ }
141
+ function* handleUpdate(update) {
142
+ if (update.sessionUpdate === "agent_message_chunk") {
143
+ yield* closeReasoning();
144
+ if (update.content.type !== "text") {
145
+ if (labels.contentEvent !== void 0) {
146
+ yield* closeText();
147
+ yield {
148
+ type: EventType.CUSTOM,
149
+ model,
150
+ timestamp: now(),
151
+ name: labels.contentEvent,
152
+ value: { content: update.content }
153
+ };
154
+ }
155
+ return;
156
+ }
157
+ const text = typeof update.content.text === "string" ? update.content.text : "";
158
+ if (text === "") return;
159
+ if (textMessageId === null) {
160
+ textMessageId = genId();
161
+ yield {
162
+ type: EventType.TEXT_MESSAGE_START,
163
+ messageId: textMessageId,
164
+ model,
165
+ timestamp: now(),
166
+ role: "assistant"
167
+ };
168
+ }
169
+ textContent += text;
170
+ yield {
171
+ type: EventType.TEXT_MESSAGE_CONTENT,
172
+ messageId: textMessageId,
173
+ model,
174
+ timestamp: now(),
175
+ delta: text,
176
+ content: textContent
177
+ };
178
+ } else if (update.sessionUpdate === "agent_thought_chunk") {
179
+ yield* closeText();
180
+ const thought = typeof update.content.text === "string" ? update.content.text : "";
181
+ if (thought === "") return;
182
+ if (reasoningId === null) {
183
+ reasoningId = genId();
184
+ yield {
185
+ type: EventType.REASONING_START,
186
+ messageId: reasoningId,
187
+ model,
188
+ timestamp: now()
189
+ };
190
+ yield {
191
+ type: EventType.REASONING_MESSAGE_START,
192
+ messageId: reasoningId,
193
+ role: "reasoning",
194
+ model,
195
+ timestamp: now()
196
+ };
197
+ }
198
+ yield {
199
+ type: EventType.REASONING_MESSAGE_CONTENT,
200
+ messageId: reasoningId,
201
+ delta: thought,
202
+ model,
203
+ timestamp: now()
204
+ };
205
+ } else if (update.sessionUpdate === "tool_call") {
206
+ yield* closeText();
207
+ yield* closeReasoning();
208
+ yield* openToolCall(update);
209
+ if (update.status === "completed" || update.status === "failed") yield* resolveToolCall(update);
210
+ } else if (update.sessionUpdate === "tool_call_update") {
211
+ if (update.status === "completed" || update.status === "failed") yield* resolveToolCall(update);
212
+ else if (update.status === "in_progress" && update.rawInput !== void 0) {
213
+ yield* closeText();
214
+ yield* closeReasoning();
215
+ if (!knownToolCalls.has(update.toolCallId)) yield* openToolCall(update);
216
+ else {
217
+ const input = {
218
+ ...update.title != null && { title: update.title },
219
+ ...typeof update.rawInput === "object" && update.rawInput !== null ? update.rawInput : { input: update.rawInput }
220
+ };
221
+ const args = JSON.stringify(input);
222
+ yield {
223
+ type: EventType.TOOL_CALL_ARGS,
224
+ toolCallId: update.toolCallId,
225
+ model,
226
+ timestamp: now(),
227
+ delta: args,
228
+ args
229
+ };
230
+ }
231
+ }
232
+ } else if (update.sessionUpdate === "plan" && labels.planEvent !== void 0) yield {
233
+ type: EventType.CUSTOM,
234
+ model,
235
+ timestamp: now(),
236
+ name: labels.planEvent,
237
+ value: { entries: update.entries }
238
+ };
239
+ }
240
+ try {
241
+ for await (const event of events) {
242
+ ctx.onAcpEvent?.(event);
243
+ if (event.kind === "session") {
244
+ yield* startRun();
245
+ yield {
246
+ type: EventType.CUSTOM,
247
+ model,
248
+ timestamp: now(),
249
+ name: labels.sessionIdEvent,
250
+ value: { sessionId: event.sessionId }
251
+ };
252
+ } else if (event.kind === "update") {
253
+ yield* startRun();
254
+ yield* handleUpdate(event.update);
255
+ } else {
256
+ yield* startRun();
257
+ yield* closeText();
258
+ yield* closeReasoning();
259
+ yield* synthesizeUnresolvedResults();
260
+ if (event.stopReason === "refusal") {
261
+ const message = labels.refusalMessage ?? "The harness refused the request.";
262
+ yield {
263
+ type: EventType.RUN_ERROR,
264
+ model,
265
+ timestamp: now(),
266
+ message,
267
+ code: "refusal",
268
+ error: {
269
+ message,
270
+ code: "refusal"
271
+ }
272
+ };
273
+ } else {
274
+ const usage = buildUsage(event.usage);
275
+ const finishReason = event.stopReason === "max_tokens" || event.stopReason === "max_turn_requests" ? "length" : "stop";
276
+ yield {
277
+ type: EventType.RUN_FINISHED,
278
+ runId,
279
+ threadId,
280
+ model,
281
+ timestamp: now(),
282
+ finishReason,
283
+ ...usage !== void 0 && { usage }
284
+ };
285
+ }
286
+ }
287
+ }
288
+ } catch (error) {
289
+ yield* closeText();
290
+ yield* closeReasoning();
291
+ yield* synthesizeUnresolvedResults();
292
+ throw error;
293
+ }
314
294
  }
315
- export {
316
- BRIDGED_MCP_SERVER_NAME,
317
- matchBridgedToolName,
318
- translateAcpStream
319
- };
320
- //# sourceMappingURL=translate.js.map
295
+ //#endregion
296
+ export { BRIDGED_MCP_SERVER_NAME, matchBridgedToolName, translateAcpStream };
297
+
298
+ //# sourceMappingURL=translate.js.map