bs-agent 0.0.10 → 0.0.12

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.
@@ -197,6 +197,20 @@ var createDebugHandlers = (setDebugData) => {
197
197
  }));
198
198
  break;
199
199
  }
200
+ case "run_error": {
201
+ setDebugData((prev) => ({
202
+ ...prev,
203
+ [executionId]: [
204
+ ...prev[executionId] || [],
205
+ {
206
+ itemType: "run_error",
207
+ message: event.data.message,
208
+ code: event.data.code
209
+ }
210
+ ]
211
+ }));
212
+ break;
213
+ }
200
214
  }
201
215
  };
202
216
  return {
@@ -293,6 +307,16 @@ function useClientToolHelpers(agentId, toolContext) {
293
307
  }
294
308
 
295
309
  // src/core/stream.ts
310
+ function tryParseJSON2(value) {
311
+ if (typeof value === "string") {
312
+ try {
313
+ return JSON.parse(value);
314
+ } catch {
315
+ return value;
316
+ }
317
+ }
318
+ return value;
319
+ }
296
320
  var FatalStreamError = class extends Error {
297
321
  constructor(message, statusCode) {
298
322
  super(message);
@@ -327,7 +351,8 @@ async function executeStream(options) {
327
351
  });
328
352
  const sessionId = response.headers.get("X-BuildShip-Agent-Session-ID");
329
353
  if (sessionId && onSessionId) {
330
- onSessionId(sessionId);
354
+ const sessionName = response.headers.get("X-BuildShip-Agent-Session-Name") || void 0;
355
+ onSessionId(sessionId, sessionName);
331
356
  }
332
357
  onResponse?.(response);
333
358
  if (!response.ok) {
@@ -344,9 +369,8 @@ async function executeStream(options) {
344
369
  throw error;
345
370
  }
346
371
  if (!response.body) {
347
- const error = new Error("Response body is null \u2014 streaming not supported");
348
- callbacks.onError?.(error);
349
- throw error;
372
+ callbacks.onComplete?.(fullText);
373
+ return;
350
374
  }
351
375
  const reader = response.body.getReader();
352
376
  const decoder = new TextDecoder();
@@ -359,9 +383,17 @@ async function executeStream(options) {
359
383
  const parts = buffer.split("\n\n");
360
384
  buffer = parts.pop() || "";
361
385
  for (const part of parts) {
362
- const dataLine = part.split("\n").find((line) => line.startsWith("data:"));
363
- if (!dataLine) continue;
364
- const jsonStr = dataLine.slice(5).trim();
386
+ const trimmedPart = part.trim();
387
+ if (!trimmedPart) continue;
388
+ let jsonStr = "";
389
+ for (const line of trimmedPart.split("\n")) {
390
+ if (line.startsWith("data: ")) {
391
+ jsonStr += line.slice(6);
392
+ } else if (line.startsWith("data:")) {
393
+ jsonStr += line.slice(5);
394
+ }
395
+ }
396
+ jsonStr = jsonStr.trim();
365
397
  if (!jsonStr) continue;
366
398
  let raw;
367
399
  try {
@@ -369,9 +401,9 @@ async function executeStream(options) {
369
401
  } catch {
370
402
  continue;
371
403
  }
372
- const event = normalizeEvent(raw);
404
+ const streamEvent = normalizeEvent(raw);
373
405
  handleEvent(
374
- event,
406
+ streamEvent,
375
407
  fullText,
376
408
  callbacks,
377
409
  clientTools,
@@ -379,39 +411,41 @@ async function executeStream(options) {
379
411
  onAutoResume,
380
412
  pendingAutoResumes
381
413
  );
382
- if (event.type === "text_delta") {
383
- fullText += event.data;
414
+ if (streamEvent.type === "text_delta") {
415
+ fullText += streamEvent.data;
384
416
  }
385
417
  }
386
418
  }
387
419
  if (buffer.trim()) {
388
- const dataLine = buffer.split("\n").find((line) => line.startsWith("data:"));
389
- if (dataLine) {
390
- const jsonStr = dataLine.slice(5).trim();
391
- if (jsonStr) {
392
- try {
393
- const event = normalizeEvent(JSON.parse(jsonStr));
394
- handleEvent(
395
- event,
396
- fullText,
397
- callbacks,
398
- clientTools,
399
- onPaused,
400
- onAutoResume,
401
- pendingAutoResumes
402
- );
403
- if (event.type === "text_delta") {
404
- fullText += event.data;
405
- }
406
- } catch {
420
+ let jsonStr = "";
421
+ for (const line of buffer.trim().split("\n")) {
422
+ if (line.startsWith("data: ")) {
423
+ jsonStr += line.slice(6);
424
+ } else if (line.startsWith("data:")) {
425
+ jsonStr += line.slice(5);
426
+ }
427
+ }
428
+ jsonStr = jsonStr.trim();
429
+ if (jsonStr) {
430
+ try {
431
+ const raw = JSON.parse(jsonStr);
432
+ const streamEvent = normalizeEvent(raw);
433
+ handleEvent(
434
+ streamEvent,
435
+ fullText,
436
+ callbacks,
437
+ clientTools,
438
+ onPaused,
439
+ onAutoResume,
440
+ pendingAutoResumes
441
+ );
442
+ if (streamEvent.type === "text_delta") {
443
+ fullText += streamEvent.data;
407
444
  }
445
+ } catch {
408
446
  }
409
447
  }
410
448
  }
411
- if (pendingAutoResumes.length > 0) {
412
- await Promise.allSettled(pendingAutoResumes);
413
- }
414
- callbacks.onComplete?.(fullText);
415
449
  } catch (err) {
416
450
  if (err instanceof Error && err.name === "AbortError") {
417
451
  throw err;
@@ -420,6 +454,10 @@ async function executeStream(options) {
420
454
  callbacks.onError?.(error);
421
455
  throw error;
422
456
  }
457
+ if (pendingAutoResumes.length > 0) {
458
+ await Promise.allSettled(pendingAutoResumes);
459
+ }
460
+ callbacks.onComplete?.(fullText);
423
461
  }
424
462
  function normalizeEvent(raw) {
425
463
  const typeMap = {
@@ -447,7 +485,8 @@ function handleEvent(event, _fullText, callbacks, clientTools, onPaused, onAutoR
447
485
  break;
448
486
  }
449
487
  case "tool_call_start": {
450
- const { callId, toolName, toolType, inputs, paused } = event.data;
488
+ const { callId, toolName, toolType, inputs: rawInputs, paused } = event.data;
489
+ const inputs = tryParseJSON2(rawInputs);
451
490
  callbacks.onToolStart?.(toolName, toolType);
452
491
  if (toolType === "client") {
453
492
  const tool = clientTools.get(toolName);
@@ -484,6 +523,13 @@ function handleEvent(event, _fullText, callbacks, clientTools, onPaused, onAutoR
484
523
  callbacks.onToolEnd?.(toolName, result, error);
485
524
  break;
486
525
  }
526
+ case "run_error": {
527
+ const { message, code } = event.data;
528
+ const error = new Error(message);
529
+ if (code != null) error.code = code;
530
+ callbacks.onError?.(error);
531
+ break;
532
+ }
487
533
  }
488
534
  }
489
535
 
@@ -575,10 +621,14 @@ async function executeAgentStream(deps, body, headers, callbacks, debugKey) {
575
621
  callbacks,
576
622
  clientTools: deps.getClientToolsMap(),
577
623
  signal,
578
- onSessionId: (sessionId) => {
624
+ onSessionId: (sessionId, sessionName) => {
579
625
  lastRequestContextRef.current.sessionId = sessionId;
580
626
  if (!currentSessionId || currentSessionId === TEMPORARY_SESSION_ID2) {
581
- createSessionFromResponse(sessionId, DEFAULT_SESSION_NAME2, messagesRef.current);
627
+ createSessionFromResponse(
628
+ sessionId,
629
+ sessionName || DEFAULT_SESSION_NAME2,
630
+ messagesRef.current
631
+ );
582
632
  deps.setCurrentSessionId(sessionId);
583
633
  }
584
634
  },