@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.cjs CHANGED
@@ -8614,7 +8614,7 @@ var AnthropicProvider = class extends BaseProvider {
8614
8614
  const { system, messages } = this.splitSystemMessages(request.messages);
8615
8615
  const body = {
8616
8616
  model: request.model || this.defaultModel,
8617
- messages: messages.map((m) => this.toAnthropicMessage(m)),
8617
+ messages: this.toAnthropicMessages(messages),
8618
8618
  max_tokens: request.maxTokens ?? 4096,
8619
8619
  stream
8620
8620
  };
@@ -8670,20 +8670,44 @@ var AnthropicProvider = class extends BaseProvider {
8670
8670
  messages: rest
8671
8671
  };
8672
8672
  }
8673
- toAnthropicMessage(m) {
8674
- if (m.role === "tool" && m.toolCallId) {
8675
- const content = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
8676
- return {
8677
- role: "user",
8678
- content: [
8679
- {
8680
- type: "tool_result",
8681
- tool_use_id: m.toolCallId,
8682
- content
8683
- }
8684
- ]
8685
- };
8673
+ /**
8674
+ * Converts unified messages to Anthropic format, coalescing consecutive
8675
+ * tool-result messages into a single `user` turn.
8676
+ *
8677
+ * The unified message list carries one `role: "tool"` entry per tool call
8678
+ * (mirroring OpenAI's shape). Anthropic's Messages API expects every
8679
+ * `tool_result` block answering a single assistant turn's `tool_use`
8680
+ * block(s) to be batched into ONE `user` message rather than sent as
8681
+ * separate consecutive `user` messages.
8682
+ */
8683
+ toAnthropicMessages(messages) {
8684
+ const result = [];
8685
+ let mergingToolResults = false;
8686
+ for (const m of messages) {
8687
+ if (m.role === "tool" && m.toolCallId) {
8688
+ const toolResult = this.toAnthropicToolResult(m);
8689
+ if (mergingToolResults) {
8690
+ result[result.length - 1].content.push(toolResult);
8691
+ } else {
8692
+ result.push({ role: "user", content: [toolResult] });
8693
+ mergingToolResults = true;
8694
+ }
8695
+ continue;
8696
+ }
8697
+ mergingToolResults = false;
8698
+ result.push(this.toAnthropicMessage(m));
8686
8699
  }
8700
+ return result;
8701
+ }
8702
+ toAnthropicToolResult(m) {
8703
+ const content = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
8704
+ return {
8705
+ type: "tool_result",
8706
+ tool_use_id: m.toolCallId,
8707
+ content
8708
+ };
8709
+ }
8710
+ toAnthropicMessage(m) {
8687
8711
  if (m.role === "assistant" && m.toolCalls?.length) {
8688
8712
  const content = [];
8689
8713
  appendReasoningBlocks(content, m.reasoning?.blocks);
@@ -9068,7 +9092,7 @@ var BedrockProvider = class extends BaseProvider {
9068
9092
  buildConverseBody(request) {
9069
9093
  const { system, messages } = this.splitSystemMessages(request.messages);
9070
9094
  const body = {
9071
- messages: messages.map((m) => this.toBedrockMessage(m))
9095
+ messages: this.toBedrockMessages(messages)
9072
9096
  };
9073
9097
  if (system.length > 0) {
9074
9098
  const systemBlocks = system.map((s) => ({ text: s }));
@@ -9118,22 +9142,47 @@ var BedrockProvider = class extends BaseProvider {
9118
9142
  }
9119
9143
  return { system, messages: rest };
9120
9144
  }
9121
- toBedrockMessage(m) {
9122
- if (m.role === "tool" && m.toolCallId) {
9123
- const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
9124
- return {
9125
- role: "user",
9126
- content: [
9127
- {
9128
- toolResult: {
9129
- toolUseId: m.toolCallId,
9130
- content: [{ text }],
9131
- status: "success"
9132
- }
9133
- }
9134
- ]
9135
- };
9145
+ /**
9146
+ * Converts unified messages to Bedrock Converse format, coalescing
9147
+ * consecutive tool-result messages into a single `user` turn.
9148
+ *
9149
+ * The unified message list carries one `role: "tool"` entry per tool call
9150
+ * (mirroring OpenAI's shape). Bedrock's Converse API requires every
9151
+ * `toolResult` block answering a single assistant turn's `toolUse`
9152
+ * block(s) to be batched into ONE `user` message — sending them as
9153
+ * separate consecutive `user` messages triggers: "Expected toolResult
9154
+ * blocks at messages.N.content for the following Ids: ...".
9155
+ */
9156
+ toBedrockMessages(messages) {
9157
+ const result = [];
9158
+ let mergingToolResults = false;
9159
+ for (const m of messages) {
9160
+ if (m.role === "tool" && m.toolCallId) {
9161
+ const toolResult = this.toBedrockToolResult(m);
9162
+ if (mergingToolResults) {
9163
+ result[result.length - 1].content.push(toolResult);
9164
+ } else {
9165
+ result.push({ role: "user", content: [toolResult] });
9166
+ mergingToolResults = true;
9167
+ }
9168
+ continue;
9169
+ }
9170
+ mergingToolResults = false;
9171
+ result.push(this.toBedrockMessage(m));
9136
9172
  }
9173
+ return result;
9174
+ }
9175
+ toBedrockToolResult(m) {
9176
+ const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
9177
+ return {
9178
+ toolResult: {
9179
+ toolUseId: m.toolCallId,
9180
+ content: [{ text }],
9181
+ status: "success"
9182
+ }
9183
+ };
9184
+ }
9185
+ toBedrockMessage(m) {
9137
9186
  if (m.role === "assistant" && m.toolCalls?.length) {
9138
9187
  const content2 = [];
9139
9188
  appendBedrockReasoning(content2, m.reasoning?.blocks);
@@ -9439,12 +9488,24 @@ var VertexProvider = class extends BaseProvider {
9439
9488
  buildContents(messages) {
9440
9489
  const systemParts = [];
9441
9490
  const contents = [];
9491
+ let mergingToolResults = false;
9442
9492
  for (const m of messages) {
9443
9493
  if (m.role === "system") {
9444
9494
  const text = typeof m.content === "string" ? m.content : m.content.filter((p) => p.type === "text").map((p) => p.text).join("\n");
9445
9495
  if (text) systemParts.push(text);
9446
9496
  continue;
9447
9497
  }
9498
+ if (m.role === "tool" && m.toolCallId) {
9499
+ const part = this.toGeminiFunctionResponsePart(m);
9500
+ if (mergingToolResults) {
9501
+ contents[contents.length - 1].parts.push(part);
9502
+ } else {
9503
+ contents.push({ role: "user", parts: [part] });
9504
+ mergingToolResults = true;
9505
+ }
9506
+ continue;
9507
+ }
9508
+ mergingToolResults = false;
9448
9509
  contents.push(this.toGeminiContent(m));
9449
9510
  }
9450
9511
  return {
@@ -9452,27 +9513,22 @@ var VertexProvider = class extends BaseProvider {
9452
9513
  contents
9453
9514
  };
9454
9515
  }
9455
- toGeminiContent(m) {
9456
- if (m.role === "tool" && m.toolCallId) {
9457
- const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
9458
- let response;
9459
- try {
9460
- response = JSON.parse(text);
9461
- } catch {
9462
- response = { result: text };
9463
- }
9464
- return {
9465
- role: "user",
9466
- parts: [
9467
- {
9468
- functionResponse: {
9469
- name: m.name ?? m.toolCallId,
9470
- response
9471
- }
9472
- }
9473
- ]
9474
- };
9516
+ toGeminiFunctionResponsePart(m) {
9517
+ const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
9518
+ let response;
9519
+ try {
9520
+ response = JSON.parse(text);
9521
+ } catch {
9522
+ response = { result: text };
9475
9523
  }
9524
+ return {
9525
+ functionResponse: {
9526
+ name: m.name ?? m.toolCallId,
9527
+ response
9528
+ }
9529
+ };
9530
+ }
9531
+ toGeminiContent(m) {
9476
9532
  if (m.role === "assistant" && m.toolCalls?.length) {
9477
9533
  const parts2 = [];
9478
9534
  if (typeof m.content === "string" && m.content) {