@cognipeer/agent-sdk 0.8.0 → 0.8.1

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/index.d.cts CHANGED
@@ -2589,6 +2589,18 @@ declare class AnthropicProvider extends BaseProvider {
2589
2589
  completeStream(request: ChatCompletionRequest): AsyncGenerator<ChatCompletionChunk, void, unknown>;
2590
2590
  private buildRequestBody;
2591
2591
  private splitSystemMessages;
2592
+ /**
2593
+ * Converts unified messages to Anthropic format, coalescing consecutive
2594
+ * tool-result messages into a single `user` turn.
2595
+ *
2596
+ * The unified message list carries one `role: "tool"` entry per tool call
2597
+ * (mirroring OpenAI's shape). Anthropic's Messages API expects every
2598
+ * `tool_result` block answering a single assistant turn's `tool_use`
2599
+ * block(s) to be batched into ONE `user` message rather than sent as
2600
+ * separate consecutive `user` messages.
2601
+ */
2602
+ private toAnthropicMessages;
2603
+ private toAnthropicToolResult;
2592
2604
  private toAnthropicMessage;
2593
2605
  private toAnthropicTool;
2594
2606
  private doFetch;
@@ -2626,6 +2638,19 @@ declare class BedrockProvider extends BaseProvider {
2626
2638
  completeStream(request: ChatCompletionRequest): AsyncGenerator<ChatCompletionChunk, void, unknown>;
2627
2639
  private buildConverseBody;
2628
2640
  private splitSystemMessages;
2641
+ /**
2642
+ * Converts unified messages to Bedrock Converse format, coalescing
2643
+ * consecutive tool-result messages into a single `user` turn.
2644
+ *
2645
+ * The unified message list carries one `role: "tool"` entry per tool call
2646
+ * (mirroring OpenAI's shape). Bedrock's Converse API requires every
2647
+ * `toolResult` block answering a single assistant turn's `toolUse`
2648
+ * block(s) to be batched into ONE `user` message — sending them as
2649
+ * separate consecutive `user` messages triggers: "Expected toolResult
2650
+ * blocks at messages.N.content for the following Ids: ...".
2651
+ */
2652
+ private toBedrockMessages;
2653
+ private toBedrockToolResult;
2629
2654
  private toBedrockMessage;
2630
2655
  private toBedrockTool;
2631
2656
  private doFetch;
@@ -2647,6 +2672,7 @@ declare class VertexProvider extends BaseProvider {
2647
2672
  private buildUrl;
2648
2673
  private buildGeminiBody;
2649
2674
  private buildContents;
2675
+ private toGeminiFunctionResponsePart;
2650
2676
  private toGeminiContent;
2651
2677
  private toGeminiTool;
2652
2678
  private getAccessToken;
package/dist/index.d.ts CHANGED
@@ -2589,6 +2589,18 @@ declare class AnthropicProvider extends BaseProvider {
2589
2589
  completeStream(request: ChatCompletionRequest): AsyncGenerator<ChatCompletionChunk, void, unknown>;
2590
2590
  private buildRequestBody;
2591
2591
  private splitSystemMessages;
2592
+ /**
2593
+ * Converts unified messages to Anthropic format, coalescing consecutive
2594
+ * tool-result messages into a single `user` turn.
2595
+ *
2596
+ * The unified message list carries one `role: "tool"` entry per tool call
2597
+ * (mirroring OpenAI's shape). Anthropic's Messages API expects every
2598
+ * `tool_result` block answering a single assistant turn's `tool_use`
2599
+ * block(s) to be batched into ONE `user` message rather than sent as
2600
+ * separate consecutive `user` messages.
2601
+ */
2602
+ private toAnthropicMessages;
2603
+ private toAnthropicToolResult;
2592
2604
  private toAnthropicMessage;
2593
2605
  private toAnthropicTool;
2594
2606
  private doFetch;
@@ -2626,6 +2638,19 @@ declare class BedrockProvider extends BaseProvider {
2626
2638
  completeStream(request: ChatCompletionRequest): AsyncGenerator<ChatCompletionChunk, void, unknown>;
2627
2639
  private buildConverseBody;
2628
2640
  private splitSystemMessages;
2641
+ /**
2642
+ * Converts unified messages to Bedrock Converse format, coalescing
2643
+ * consecutive tool-result messages into a single `user` turn.
2644
+ *
2645
+ * The unified message list carries one `role: "tool"` entry per tool call
2646
+ * (mirroring OpenAI's shape). Bedrock's Converse API requires every
2647
+ * `toolResult` block answering a single assistant turn's `toolUse`
2648
+ * block(s) to be batched into ONE `user` message — sending them as
2649
+ * separate consecutive `user` messages triggers: "Expected toolResult
2650
+ * blocks at messages.N.content for the following Ids: ...".
2651
+ */
2652
+ private toBedrockMessages;
2653
+ private toBedrockToolResult;
2629
2654
  private toBedrockMessage;
2630
2655
  private toBedrockTool;
2631
2656
  private doFetch;
@@ -2647,6 +2672,7 @@ declare class VertexProvider extends BaseProvider {
2647
2672
  private buildUrl;
2648
2673
  private buildGeminiBody;
2649
2674
  private buildContents;
2675
+ private toGeminiFunctionResponsePart;
2650
2676
  private toGeminiContent;
2651
2677
  private toGeminiTool;
2652
2678
  private getAccessToken;
package/dist/index.mjs CHANGED
@@ -8604,7 +8604,7 @@ var AnthropicProvider = class extends BaseProvider {
8604
8604
  const { system, messages } = this.splitSystemMessages(request.messages);
8605
8605
  const body = {
8606
8606
  model: request.model || this.defaultModel,
8607
- messages: messages.map((m) => this.toAnthropicMessage(m)),
8607
+ messages: this.toAnthropicMessages(messages),
8608
8608
  max_tokens: request.maxTokens ?? 4096,
8609
8609
  stream
8610
8610
  };
@@ -8660,20 +8660,44 @@ var AnthropicProvider = class extends BaseProvider {
8660
8660
  messages: rest
8661
8661
  };
8662
8662
  }
8663
- toAnthropicMessage(m) {
8664
- if (m.role === "tool" && m.toolCallId) {
8665
- const content = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
8666
- return {
8667
- role: "user",
8668
- content: [
8669
- {
8670
- type: "tool_result",
8671
- tool_use_id: m.toolCallId,
8672
- content
8673
- }
8674
- ]
8675
- };
8663
+ /**
8664
+ * Converts unified messages to Anthropic format, coalescing consecutive
8665
+ * tool-result messages into a single `user` turn.
8666
+ *
8667
+ * The unified message list carries one `role: "tool"` entry per tool call
8668
+ * (mirroring OpenAI's shape). Anthropic's Messages API expects every
8669
+ * `tool_result` block answering a single assistant turn's `tool_use`
8670
+ * block(s) to be batched into ONE `user` message rather than sent as
8671
+ * separate consecutive `user` messages.
8672
+ */
8673
+ toAnthropicMessages(messages) {
8674
+ const result = [];
8675
+ let mergingToolResults = false;
8676
+ for (const m of messages) {
8677
+ if (m.role === "tool" && m.toolCallId) {
8678
+ const toolResult = this.toAnthropicToolResult(m);
8679
+ if (mergingToolResults) {
8680
+ result[result.length - 1].content.push(toolResult);
8681
+ } else {
8682
+ result.push({ role: "user", content: [toolResult] });
8683
+ mergingToolResults = true;
8684
+ }
8685
+ continue;
8686
+ }
8687
+ mergingToolResults = false;
8688
+ result.push(this.toAnthropicMessage(m));
8676
8689
  }
8690
+ return result;
8691
+ }
8692
+ toAnthropicToolResult(m) {
8693
+ const content = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
8694
+ return {
8695
+ type: "tool_result",
8696
+ tool_use_id: m.toolCallId,
8697
+ content
8698
+ };
8699
+ }
8700
+ toAnthropicMessage(m) {
8677
8701
  if (m.role === "assistant" && m.toolCalls?.length) {
8678
8702
  const content = [];
8679
8703
  appendReasoningBlocks(content, m.reasoning?.blocks);
@@ -9058,7 +9082,7 @@ var BedrockProvider = class extends BaseProvider {
9058
9082
  buildConverseBody(request) {
9059
9083
  const { system, messages } = this.splitSystemMessages(request.messages);
9060
9084
  const body = {
9061
- messages: messages.map((m) => this.toBedrockMessage(m))
9085
+ messages: this.toBedrockMessages(messages)
9062
9086
  };
9063
9087
  if (system.length > 0) {
9064
9088
  const systemBlocks = system.map((s) => ({ text: s }));
@@ -9108,22 +9132,47 @@ var BedrockProvider = class extends BaseProvider {
9108
9132
  }
9109
9133
  return { system, messages: rest };
9110
9134
  }
9111
- toBedrockMessage(m) {
9112
- if (m.role === "tool" && m.toolCallId) {
9113
- const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
9114
- return {
9115
- role: "user",
9116
- content: [
9117
- {
9118
- toolResult: {
9119
- toolUseId: m.toolCallId,
9120
- content: [{ text }],
9121
- status: "success"
9122
- }
9123
- }
9124
- ]
9125
- };
9135
+ /**
9136
+ * Converts unified messages to Bedrock Converse format, coalescing
9137
+ * consecutive tool-result messages into a single `user` turn.
9138
+ *
9139
+ * The unified message list carries one `role: "tool"` entry per tool call
9140
+ * (mirroring OpenAI's shape). Bedrock's Converse API requires every
9141
+ * `toolResult` block answering a single assistant turn's `toolUse`
9142
+ * block(s) to be batched into ONE `user` message — sending them as
9143
+ * separate consecutive `user` messages triggers: "Expected toolResult
9144
+ * blocks at messages.N.content for the following Ids: ...".
9145
+ */
9146
+ toBedrockMessages(messages) {
9147
+ const result = [];
9148
+ let mergingToolResults = false;
9149
+ for (const m of messages) {
9150
+ if (m.role === "tool" && m.toolCallId) {
9151
+ const toolResult = this.toBedrockToolResult(m);
9152
+ if (mergingToolResults) {
9153
+ result[result.length - 1].content.push(toolResult);
9154
+ } else {
9155
+ result.push({ role: "user", content: [toolResult] });
9156
+ mergingToolResults = true;
9157
+ }
9158
+ continue;
9159
+ }
9160
+ mergingToolResults = false;
9161
+ result.push(this.toBedrockMessage(m));
9126
9162
  }
9163
+ return result;
9164
+ }
9165
+ toBedrockToolResult(m) {
9166
+ const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
9167
+ return {
9168
+ toolResult: {
9169
+ toolUseId: m.toolCallId,
9170
+ content: [{ text }],
9171
+ status: "success"
9172
+ }
9173
+ };
9174
+ }
9175
+ toBedrockMessage(m) {
9127
9176
  if (m.role === "assistant" && m.toolCalls?.length) {
9128
9177
  const content2 = [];
9129
9178
  appendBedrockReasoning(content2, m.reasoning?.blocks);
@@ -9429,12 +9478,24 @@ var VertexProvider = class extends BaseProvider {
9429
9478
  buildContents(messages) {
9430
9479
  const systemParts = [];
9431
9480
  const contents = [];
9481
+ let mergingToolResults = false;
9432
9482
  for (const m of messages) {
9433
9483
  if (m.role === "system") {
9434
9484
  const text = typeof m.content === "string" ? m.content : m.content.filter((p) => p.type === "text").map((p) => p.text).join("\n");
9435
9485
  if (text) systemParts.push(text);
9436
9486
  continue;
9437
9487
  }
9488
+ if (m.role === "tool" && m.toolCallId) {
9489
+ const part = this.toGeminiFunctionResponsePart(m);
9490
+ if (mergingToolResults) {
9491
+ contents[contents.length - 1].parts.push(part);
9492
+ } else {
9493
+ contents.push({ role: "user", parts: [part] });
9494
+ mergingToolResults = true;
9495
+ }
9496
+ continue;
9497
+ }
9498
+ mergingToolResults = false;
9438
9499
  contents.push(this.toGeminiContent(m));
9439
9500
  }
9440
9501
  return {
@@ -9442,27 +9503,22 @@ var VertexProvider = class extends BaseProvider {
9442
9503
  contents
9443
9504
  };
9444
9505
  }
9445
- toGeminiContent(m) {
9446
- if (m.role === "tool" && m.toolCallId) {
9447
- const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
9448
- let response;
9449
- try {
9450
- response = JSON.parse(text);
9451
- } catch {
9452
- response = { result: text };
9453
- }
9454
- return {
9455
- role: "user",
9456
- parts: [
9457
- {
9458
- functionResponse: {
9459
- name: m.name ?? m.toolCallId,
9460
- response
9461
- }
9462
- }
9463
- ]
9464
- };
9506
+ toGeminiFunctionResponsePart(m) {
9507
+ const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
9508
+ let response;
9509
+ try {
9510
+ response = JSON.parse(text);
9511
+ } catch {
9512
+ response = { result: text };
9465
9513
  }
9514
+ return {
9515
+ functionResponse: {
9516
+ name: m.name ?? m.toolCallId,
9517
+ response
9518
+ }
9519
+ };
9520
+ }
9521
+ toGeminiContent(m) {
9466
9522
  if (m.role === "assistant" && m.toolCalls?.length) {
9467
9523
  const parts2 = [];
9468
9524
  if (typeof m.content === "string" && m.content) {