graphlit-client 1.0.20260402001 → 1.0.20260402002

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 (2) hide show
  1. package/dist/client.js +94 -59
  2. package/package.json +1 -1
package/dist/client.js CHANGED
@@ -200,6 +200,8 @@ catch (e) {
200
200
  }
201
201
  }
202
202
  const DEFAULT_MAX_TOOL_ROUNDS = 100;
203
+ /** Maximum number of tool calls to execute concurrently within a single streaming round. */
204
+ const DEFAULT_MAX_PARALLEL_TOOL_CALLS = 4;
203
205
  /** Maximum number of retries for transient provider errors (5xx, network, overloaded). */
204
206
  const DEFAULT_PROVIDER_RETRIES = 3;
205
207
  /** Base delay in ms for exponential backoff between provider retries. */
@@ -6330,8 +6332,8 @@ class Graphlit {
6330
6332
  roundToolCalls.reduce((sum, tc) => sum + estimateTokens(tc.arguments), 0);
6331
6333
  budgetTracker.addMessage("", assistantTokens);
6332
6334
  }
6333
- // Execute tools and add responses
6334
- for (const toolCall of roundToolCalls) {
6335
+ const toolExecutionResults = new Array(roundToolCalls.length);
6336
+ const executeStreamingToolCall = async (toolCall, index) => {
6335
6337
  if (abortSignal?.aborted) {
6336
6338
  throw new Error("Operation aborted");
6337
6339
  }
@@ -6359,20 +6361,21 @@ class Graphlit {
6359
6361
  },
6360
6362
  error: errorMessage,
6361
6363
  });
6362
- const errorToolMessage = {
6363
- __typename: "ConversationMessage",
6364
- role: Types.ConversationRoleTypes.Tool,
6365
- message: `Error: ${errorMessage}`,
6366
- toolCallId: toolCall.id,
6367
- timestamp: terminalAt,
6368
- toolCallResponse: toolCall.name,
6364
+ const errorText = `Error: ${errorMessage}`;
6365
+ toolExecutionResults[index] = {
6366
+ index,
6367
+ toolMessage: {
6368
+ __typename: "ConversationMessage",
6369
+ role: Types.ConversationRoleTypes.Tool,
6370
+ message: errorText,
6371
+ toolCallId: toolCall.id,
6372
+ timestamp: terminalAt,
6373
+ toolCallResponse: toolCall.name,
6374
+ },
6375
+ budgetText: errorText,
6376
+ errorEntry: `${toolCall.name}: ${errorMessage}`,
6369
6377
  };
6370
- messages.push(errorToolMessage);
6371
- errors.push(`${toolCall.name}: ${errorMessage}`);
6372
- if (budgetTracker) {
6373
- budgetTracker.addMessage(errorToolMessage.message || "");
6374
- }
6375
- continue;
6378
+ return;
6376
6379
  }
6377
6380
  try {
6378
6381
  let args;
@@ -6453,20 +6456,21 @@ class Graphlit {
6453
6456
  },
6454
6457
  error: parseErrorText,
6455
6458
  });
6456
- const errorToolMessage = {
6457
- __typename: "ConversationMessage",
6458
- role: Types.ConversationRoleTypes.Tool,
6459
- message: `Error: ${parseErrorText}`,
6460
- toolCallId: toolCall.id,
6461
- timestamp: terminalAt,
6462
- toolCallResponse: toolCall.name,
6459
+ const errorText = `Error: ${parseErrorText}`;
6460
+ toolExecutionResults[index] = {
6461
+ index,
6462
+ toolMessage: {
6463
+ __typename: "ConversationMessage",
6464
+ role: Types.ConversationRoleTypes.Tool,
6465
+ message: errorText,
6466
+ toolCallId: toolCall.id,
6467
+ timestamp: terminalAt,
6468
+ toolCallResponse: toolCall.name,
6469
+ },
6470
+ budgetText: errorText,
6471
+ errorEntry: parseErrorText,
6463
6472
  };
6464
- messages.push(errorToolMessage);
6465
- errors.push(parseErrorText);
6466
- if (budgetTracker) {
6467
- budgetTracker.addMessage(errorToolMessage.message || "");
6468
- }
6469
- continue;
6473
+ return;
6470
6474
  }
6471
6475
  }
6472
6476
  const executionStartMs = Date.now();
@@ -6504,44 +6508,36 @@ class Graphlit {
6504
6508
  });
6505
6509
  const rawResult = typeof result === "string" ? result : JSON.stringify(result);
6506
6510
  const truncatedResult = truncateToolResult(rawResult, toolResultTokenLimit, toolCall.name);
6511
+ let contextAction;
6507
6512
  if (truncatedResult.length < rawResult.length) {
6508
- const action = {
6513
+ contextAction = {
6509
6514
  type: "truncated_tool_result",
6510
6515
  toolName: toolCall.name,
6511
6516
  originalTokens: estimateTokens(rawResult),
6512
6517
  truncatedTokens: estimateTokens(truncatedResult),
6513
6518
  };
6514
- contextActions.push(action);
6515
- if (budgetTracker) {
6516
- uiAdapter.handleEvent({
6517
- type: "context_management",
6518
- action,
6519
- usage: budgetTracker.getUsageSnapshot(),
6520
- timestamp: new Date(),
6521
- });
6522
- }
6523
6519
  if (process.env.DEBUG_GRAPHLIT_SDK_STREAMING) {
6524
6520
  console.log(`📊 [Context Management] Truncated tool result for ${toolCall.name}: ` +
6525
6521
  `${estimateTokens(rawResult)} → ${estimateTokens(truncatedResult)} tokens`);
6526
6522
  }
6527
6523
  }
6528
- const toolMessage = {
6529
- __typename: "ConversationMessage",
6530
- role: Types.ConversationRoleTypes.Tool,
6531
- message: truncatedResult,
6532
- toolCallId: toolCall.id,
6533
- timestamp: new Date().toISOString(),
6534
- toolCallResponse: toolCall.name,
6524
+ toolExecutionResults[index] = {
6525
+ index,
6526
+ toolMessage: {
6527
+ __typename: "ConversationMessage",
6528
+ role: Types.ConversationRoleTypes.Tool,
6529
+ message: truncatedResult,
6530
+ toolCallId: toolCall.id,
6531
+ timestamp: new Date().toISOString(),
6532
+ toolCallResponse: toolCall.name,
6533
+ },
6534
+ budgetText: truncatedResult,
6535
+ contextAction,
6535
6536
  };
6536
- messages.push(toolMessage);
6537
- if (budgetTracker) {
6538
- budgetTracker.addMessage(truncatedResult);
6539
- }
6540
6537
  }
6541
6538
  catch (error) {
6542
6539
  const errorMessage = error instanceof Error ? error.message : "Unknown error";
6543
6540
  console.error(`Tool execution error for ${toolCall.name}:`, error);
6544
- errors.push(`${toolCall.name}: ${errorMessage}`);
6545
6541
  const completedAt = nowIsoString();
6546
6542
  if (!toolCall.startedAt) {
6547
6543
  toolCall.startedAt = completedAt;
@@ -6567,19 +6563,58 @@ class Graphlit {
6567
6563
  error: errorMessage,
6568
6564
  });
6569
6565
  const errorText = `Error: ${errorMessage}`;
6570
- const errorToolMessage = {
6571
- __typename: "ConversationMessage",
6572
- role: Types.ConversationRoleTypes.Tool,
6573
- message: errorText,
6574
- toolCallId: toolCall.id,
6575
- timestamp: new Date().toISOString(),
6576
- toolCallResponse: toolCall.name,
6566
+ toolExecutionResults[index] = {
6567
+ index,
6568
+ toolMessage: {
6569
+ __typename: "ConversationMessage",
6570
+ role: Types.ConversationRoleTypes.Tool,
6571
+ message: errorText,
6572
+ toolCallId: toolCall.id,
6573
+ timestamp: new Date().toISOString(),
6574
+ toolCallResponse: toolCall.name,
6575
+ },
6576
+ budgetText: errorText,
6577
+ errorEntry: `${toolCall.name}: ${errorMessage}`,
6577
6578
  };
6578
- messages.push(errorToolMessage);
6579
+ }
6580
+ };
6581
+ const workerCount = Math.min(DEFAULT_MAX_PARALLEL_TOOL_CALLS, roundToolCalls.length);
6582
+ let nextToolIndex = 0;
6583
+ await Promise.all(Array.from({ length: workerCount }, async () => {
6584
+ while (true) {
6585
+ if (abortSignal?.aborted) {
6586
+ throw new Error("Operation aborted");
6587
+ }
6588
+ const currentIndex = nextToolIndex;
6589
+ nextToolIndex += 1;
6590
+ if (currentIndex >= roundToolCalls.length) {
6591
+ break;
6592
+ }
6593
+ await executeStreamingToolCall(roundToolCalls[currentIndex], currentIndex);
6594
+ }
6595
+ }));
6596
+ for (const executionResult of toolExecutionResults) {
6597
+ if (!executionResult) {
6598
+ continue;
6599
+ }
6600
+ if (executionResult.contextAction) {
6601
+ contextActions.push(executionResult.contextAction);
6579
6602
  if (budgetTracker) {
6580
- budgetTracker.addMessage(errorText);
6603
+ uiAdapter.handleEvent({
6604
+ type: "context_management",
6605
+ action: executionResult.contextAction,
6606
+ usage: budgetTracker.getUsageSnapshot(),
6607
+ timestamp: new Date(),
6608
+ });
6581
6609
  }
6582
6610
  }
6611
+ messages.push(executionResult.toolMessage);
6612
+ if (executionResult.errorEntry) {
6613
+ errors.push(executionResult.errorEntry);
6614
+ }
6615
+ if (budgetTracker) {
6616
+ budgetTracker.addMessage(executionResult.budgetText);
6617
+ }
6583
6618
  }
6584
6619
  }
6585
6620
  // Emit context window usage after each tool round
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphlit-client",
3
- "version": "1.0.20260402001",
3
+ "version": "1.0.20260402002",
4
4
  "description": "Graphlit API Client for TypeScript",
5
5
  "type": "module",
6
6
  "main": "./dist/client.js",