@yourgpt/copilot-sdk 2.1.5-alpha.7 → 2.1.6

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.
@@ -305,8 +305,16 @@ var HttpTransport = class {
305
305
  signal: this.abortController.signal
306
306
  });
307
307
  if (!response.ok) {
308
- const error = await response.text();
309
- throw new Error(`HTTP ${response.status}: ${error}`);
308
+ let errorMessage = `HTTP ${response.status}`;
309
+ try {
310
+ const errorBody = await response.json();
311
+ const custom = this.config.parseError?.(response.status, errorBody);
312
+ errorMessage = custom ?? errorBody?.message ?? errorBody?.error ?? JSON.stringify(errorBody);
313
+ } catch {
314
+ const text = await response.text();
315
+ if (text) errorMessage = text;
316
+ }
317
+ throw new Error(errorMessage);
310
318
  }
311
319
  const contentType = response.headers.get("content-type") || "";
312
320
  if (contentType.includes("application/json")) {
@@ -1491,6 +1499,11 @@ var AbstractChat = class {
1491
1499
  this.eventHandlers = /* @__PURE__ */ new Map();
1492
1500
  // Current streaming state
1493
1501
  this.streamState = null;
1502
+ // ID of the pending assistant placeholder pushed before a request.
1503
+ // Used by handleError() to pop (remove) the placeholder on failure so it
1504
+ // doesn't stay frozen. Error is surfaced via state.error only — never written
1505
+ // into message history.
1506
+ this._activePlaceholderMessageId = void 0;
1494
1507
  /**
1495
1508
  * Inline skills from the client (sent on every request for server to merge)
1496
1509
  */
@@ -1523,7 +1536,8 @@ var AbstractChat = class {
1523
1536
  url: init.runtimeUrl,
1524
1537
  headers: init.headers,
1525
1538
  body: init.body,
1526
- streaming: init.streaming ?? true
1539
+ streaming: init.streaming ?? true,
1540
+ parseError: init.parseError
1527
1541
  });
1528
1542
  this.callbacks = init.callbacks ?? {};
1529
1543
  this.optimizer = new ChatContextOptimizer(init.optimization);
@@ -1603,6 +1617,7 @@ var AbstractChat = class {
1603
1617
  });
1604
1618
  this.state.pushMessage(preMsg);
1605
1619
  preCreatedMessageId = preMsg.id;
1620
+ this._activePlaceholderMessageId = preMsg.id;
1606
1621
  }
1607
1622
  this.callbacks.onMessagesChange?.(this._allMessages());
1608
1623
  this.callbacks.onStatusChange?.("submitted");
@@ -1736,6 +1751,7 @@ var AbstractChat = class {
1736
1751
  this.state.pushMessage(userMessage);
1737
1752
  }
1738
1753
  this.state.status = "submitted";
1754
+ this.state.error = void 0;
1739
1755
  this.callbacks.onMessagesChange?.(this._allMessages());
1740
1756
  this.callbacks.onStatusChange?.("submitted");
1741
1757
  await new Promise((resolve) => setTimeout(resolve, 0));
@@ -1898,6 +1914,7 @@ var AbstractChat = class {
1898
1914
  this.state.pushMessage(preMsg);
1899
1915
  this.callbacks.onMessagesChange?.(this._allMessages());
1900
1916
  preCreatedMessageId = preMsg.id;
1917
+ this._activePlaceholderMessageId = preMsg.id;
1901
1918
  }
1902
1919
  const response = await this.transport.send(request);
1903
1920
  if (this.isAsyncIterable(response)) {
@@ -1914,6 +1931,7 @@ var AbstractChat = class {
1914
1931
  this.state.setCurrentLeaf(intendedLeafId);
1915
1932
  }
1916
1933
  }
1934
+ this._activePlaceholderMessageId = void 0;
1917
1935
  this.handleJsonResponse(response);
1918
1936
  }
1919
1937
  }
@@ -2199,7 +2217,7 @@ ${this.dynamicContext}`.trim() : this.config.systemPrompt;
2199
2217
  this.handleError(error);
2200
2218
  return;
2201
2219
  }
2202
- if (chunk.type === "message:end" && this.streamState?.content) {
2220
+ if (chunk.type === "message:end" && this.streamState !== null && (this.streamState.content || (this.streamState.toolResults?.size ?? 0) > 0)) {
2203
2221
  this.debug("message:end mid-stream", {
2204
2222
  messageId: this.streamState.messageId,
2205
2223
  contentLength: this.streamState.content.length,
@@ -2274,6 +2292,8 @@ ${this.dynamicContext}`.trim() : this.config.systemPrompt;
2274
2292
  continue;
2275
2293
  }
2276
2294
  if (msg.role === "assistant" && !msg.tool_calls?.length) continue;
2295
+ if (msg.role === "assistant" && msg.tool_calls?.length && pendingIds.size === 0)
2296
+ continue;
2277
2297
  messagesToInsert.push({
2278
2298
  id: generateMessageId(),
2279
2299
  role: msg.role,
@@ -2552,6 +2572,7 @@ ${this.dynamicContext}`.trim() : this.config.systemPrompt;
2552
2572
  }
2553
2573
  }
2554
2574
  this.callbacks.onMessagesChange?.(this._allMessages());
2575
+ this._activePlaceholderMessageId = void 0;
2555
2576
  this.debugGroupEnd();
2556
2577
  this.debug("stream end", {
2557
2578
  toolCallsEmitted,
@@ -2632,6 +2653,11 @@ ${this.dynamicContext}`.trim() : this.config.systemPrompt;
2632
2653
  */
2633
2654
  handleError(error) {
2634
2655
  this.debug("error", error);
2656
+ if (this._activePlaceholderMessageId) {
2657
+ this.state.popMessage();
2658
+ this._activePlaceholderMessageId = void 0;
2659
+ this.callbacks.onMessagesChange?.(this._allMessages());
2660
+ }
2635
2661
  this.state.error = error;
2636
2662
  this.state.status = "error";
2637
2663
  this.callbacks.onError?.(error);
@@ -3505,6 +3531,7 @@ var ChatWithTools = class {
3505
3531
  initialMessages: config.initialMessages,
3506
3532
  state: config.state,
3507
3533
  transport: config.transport,
3534
+ parseError: config.parseError,
3508
3535
  callbacks: {
3509
3536
  onMessagesChange: callbacks.onMessagesChange,
3510
3537
  onStatusChange: callbacks.onStatusChange,
@@ -5378,6 +5405,7 @@ function CopilotProvider(props) {
5378
5405
  initialMessages,
5379
5406
  onMessagesChange,
5380
5407
  onError,
5408
+ parseError,
5381
5409
  streaming,
5382
5410
  headers,
5383
5411
  body,
@@ -5443,6 +5471,7 @@ function CopilotProvider(props) {
5443
5471
  streaming,
5444
5472
  headers,
5445
5473
  body,
5474
+ parseError,
5446
5475
  debug,
5447
5476
  maxIterations,
5448
5477
  maxIterationsMessage,
@@ -5942,5 +5971,5 @@ exports.useMessageHistoryContext = useMessageHistoryContext;
5942
5971
  exports.useSkillContext = useSkillContext;
5943
5972
  exports.useTool = useTool;
5944
5973
  exports.useTools = useTools;
5945
- //# sourceMappingURL=chunk-NPBOKT63.cjs.map
5946
- //# sourceMappingURL=chunk-NPBOKT63.cjs.map
5974
+ //# sourceMappingURL=chunk-6ZTITCKU.cjs.map
5975
+ //# sourceMappingURL=chunk-6ZTITCKU.cjs.map