@sandagent/runner-cli 0.2.12 → 0.2.13
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.
- package/dist/bundle.mjs +60 -30
- package/package.json +1 -1
package/dist/bundle.mjs
CHANGED
|
@@ -131,8 +131,23 @@ CMD `);
|
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
// ../../packages/runner-claude/dist/ai-sdk-stream.js
|
|
134
|
-
import {
|
|
134
|
+
import { appendFileSync, existsSync as existsSync2, unlinkSync } from "node:fs";
|
|
135
135
|
import { join as join2 } from "node:path";
|
|
136
|
+
function trace(data, reset = false) {
|
|
137
|
+
if (process.env.DEBUG !== "true")
|
|
138
|
+
return;
|
|
139
|
+
try {
|
|
140
|
+
const file = join2(process.cwd(), "claude-message-stream-debug.json");
|
|
141
|
+
if (reset && existsSync2(file))
|
|
142
|
+
unlinkSync(file);
|
|
143
|
+
const entry = {
|
|
144
|
+
_t: (/* @__PURE__ */ new Date()).toISOString(),
|
|
145
|
+
...typeof data === "object" && data !== null ? data : { value: data }
|
|
146
|
+
};
|
|
147
|
+
appendFileSync(file, JSON.stringify(entry, null, 2) + ",\n");
|
|
148
|
+
} catch {
|
|
149
|
+
}
|
|
150
|
+
}
|
|
136
151
|
var UNKNOWN_TOOL_NAME = "unknown-tool";
|
|
137
152
|
function formatDataStream(data) {
|
|
138
153
|
return `data: ${JSON.stringify(data)}
|
|
@@ -142,6 +157,16 @@ function formatDataStream(data) {
|
|
|
142
157
|
function generateId() {
|
|
143
158
|
return `${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
144
159
|
}
|
|
160
|
+
function isAbortError(err) {
|
|
161
|
+
if (err && typeof err === "object") {
|
|
162
|
+
const e = err;
|
|
163
|
+
if (typeof e.name === "string" && e.name === "AbortError")
|
|
164
|
+
return true;
|
|
165
|
+
if (typeof e.code === "string" && e.code.toUpperCase() === "ABORT_ERR")
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
145
170
|
function convertUsageToAISDK(usage) {
|
|
146
171
|
const inputTokens = usage.input_tokens ?? 0;
|
|
147
172
|
const outputTokens = usage.output_tokens ?? 0;
|
|
@@ -194,11 +219,6 @@ function extractToolUses(content) {
|
|
|
194
219
|
var AISDKStreamConverter = class {
|
|
195
220
|
systemMessage;
|
|
196
221
|
hasEmittedStart = false;
|
|
197
|
-
accumulatedText = "";
|
|
198
|
-
textPartId;
|
|
199
|
-
streamedTextLength = 0;
|
|
200
|
-
// Track text already emitted via stream_events
|
|
201
|
-
hasReceivedStreamEvents = false;
|
|
202
222
|
sessionId;
|
|
203
223
|
partIdMap = /* @__PURE__ */ new Map();
|
|
204
224
|
/**
|
|
@@ -277,15 +297,15 @@ var AISDKStreamConverter = class {
|
|
|
277
297
|
/**
|
|
278
298
|
* Stream SDK messages and convert to AI SDK UI Data Stream format
|
|
279
299
|
*/
|
|
280
|
-
async *stream(messageIterator
|
|
281
|
-
const debugMessages = [];
|
|
300
|
+
async *stream(messageIterator) {
|
|
282
301
|
try {
|
|
283
302
|
for await (const message of messageIterator) {
|
|
284
|
-
debugMessages.push(message);
|
|
285
303
|
if (message.type === "system" && message.subtype === "init") {
|
|
304
|
+
trace(null, true);
|
|
286
305
|
this.systemMessage = message;
|
|
287
306
|
this.sessionId = this.systemMessage.session_id;
|
|
288
307
|
}
|
|
308
|
+
trace(message);
|
|
289
309
|
if (message.type === "stream_event") {
|
|
290
310
|
const streamEvent = message;
|
|
291
311
|
const event = streamEvent.event;
|
|
@@ -340,9 +360,14 @@ var AISDKStreamConverter = class {
|
|
|
340
360
|
}
|
|
341
361
|
if (message.type === "result") {
|
|
342
362
|
const resultMsg = message;
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
363
|
+
if (resultMsg.is_error) {
|
|
364
|
+
const errorText = resultMsg.result || "Unknown error";
|
|
365
|
+
yield this.emit({
|
|
366
|
+
type: "error",
|
|
367
|
+
errorText
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
yield this.emit({
|
|
346
371
|
type: "finish",
|
|
347
372
|
finishReason: mapFinishReason(resultMsg.subtype, resultMsg.is_error),
|
|
348
373
|
messageMetadata: {
|
|
@@ -350,29 +375,34 @@ var AISDKStreamConverter = class {
|
|
|
350
375
|
sessionId: this.sessionId
|
|
351
376
|
}
|
|
352
377
|
});
|
|
353
|
-
console.error(`[AISDKStream] Emitting finish event at ${(/* @__PURE__ */ new Date()).toISOString()}`);
|
|
354
|
-
yield finishEvent;
|
|
355
|
-
console.error(`[AISDKStream] Emitted [DONE] at ${(/* @__PURE__ */ new Date()).toISOString()}`);
|
|
356
378
|
}
|
|
357
379
|
}
|
|
358
380
|
} catch (error) {
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
const
|
|
364
|
-
|
|
365
|
-
|
|
381
|
+
trace({ error: String(error) });
|
|
382
|
+
if (isAbortError(error)) {
|
|
383
|
+
console.error("[AISDKStream] Operation aborted");
|
|
384
|
+
} else {
|
|
385
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
386
|
+
console.error("[AISDKStream] Error:", errorMessage);
|
|
387
|
+
yield this.emit({ type: "error", errorText: errorMessage });
|
|
388
|
+
yield this.emit({
|
|
389
|
+
type: "finish",
|
|
390
|
+
finishReason: mapFinishReason("error_during_execution", true),
|
|
391
|
+
messageMetadata: {
|
|
392
|
+
usage: convertUsageToAISDK({}),
|
|
393
|
+
sessionId: this.sessionId
|
|
394
|
+
}
|
|
366
395
|
});
|
|
367
396
|
}
|
|
397
|
+
} finally {
|
|
368
398
|
yield `data: [DONE]
|
|
369
399
|
|
|
370
400
|
`;
|
|
371
401
|
}
|
|
372
402
|
}
|
|
373
403
|
};
|
|
374
|
-
function streamSDKMessagesToAISDKUI(messageIterator
|
|
375
|
-
return new AISDKStreamConverter().stream(messageIterator
|
|
404
|
+
function streamSDKMessagesToAISDKUI(messageIterator) {
|
|
405
|
+
return new AISDKStreamConverter().stream(messageIterator);
|
|
376
406
|
}
|
|
377
407
|
|
|
378
408
|
// ../../packages/runner-claude/dist/claude-runner.js
|
|
@@ -538,7 +568,7 @@ function setupAbortHandler(queryIterator, signal) {
|
|
|
538
568
|
}
|
|
539
569
|
};
|
|
540
570
|
}
|
|
541
|
-
async function* runWithTextOutput(sdk, options, userInput
|
|
571
|
+
async function* runWithTextOutput(sdk, options, userInput) {
|
|
542
572
|
const sdkOptions = createSDKOptions(options);
|
|
543
573
|
const queryIterator = sdk.query({ prompt: userInput, options: sdkOptions });
|
|
544
574
|
const cleanup = setupAbortHandler(queryIterator, options.abortController?.signal);
|
|
@@ -557,10 +587,10 @@ async function* runWithTextOutput(sdk, options, userInput, signal) {
|
|
|
557
587
|
cleanup();
|
|
558
588
|
}
|
|
559
589
|
}
|
|
560
|
-
async function* runWithJSONOutput(sdk, options, userInput
|
|
590
|
+
async function* runWithJSONOutput(sdk, options, userInput) {
|
|
561
591
|
const sdkOptions = createSDKOptions(options);
|
|
562
592
|
const queryIterator = sdk.query({ prompt: userInput, options: sdkOptions });
|
|
563
|
-
const cleanup = setupAbortHandler(queryIterator, signal);
|
|
593
|
+
const cleanup = setupAbortHandler(queryIterator, options.abortController?.signal);
|
|
564
594
|
try {
|
|
565
595
|
let resultMessage = null;
|
|
566
596
|
for await (const message of queryIterator) {
|
|
@@ -575,10 +605,10 @@ async function* runWithJSONOutput(sdk, options, userInput, signal) {
|
|
|
575
605
|
cleanup();
|
|
576
606
|
}
|
|
577
607
|
}
|
|
578
|
-
async function* runWithStreamJSONOutput(sdk, options, userInput
|
|
608
|
+
async function* runWithStreamJSONOutput(sdk, options, userInput) {
|
|
579
609
|
const sdkOptions = createSDKOptions(options);
|
|
580
610
|
const queryIterator = sdk.query({ prompt: userInput, options: sdkOptions });
|
|
581
|
-
const cleanup = setupAbortHandler(queryIterator, signal);
|
|
611
|
+
const cleanup = setupAbortHandler(queryIterator, options.abortController?.signal);
|
|
582
612
|
try {
|
|
583
613
|
for await (const message of queryIterator) {
|
|
584
614
|
yield JSON.stringify(message) + "\n";
|
|
@@ -595,7 +625,7 @@ async function* runWithAISDKUIOutput(sdk, options, userInput) {
|
|
|
595
625
|
const queryIterator = sdk.query({ prompt: userInput, options: sdkOptions });
|
|
596
626
|
const cleanup = setupAbortHandler(queryIterator, options.abortController?.signal);
|
|
597
627
|
try {
|
|
598
|
-
yield* streamSDKMessagesToAISDKUI(queryIterator
|
|
628
|
+
yield* streamSDKMessagesToAISDKUI(queryIterator);
|
|
599
629
|
} finally {
|
|
600
630
|
cleanup();
|
|
601
631
|
}
|
package/package.json
CHANGED