@cognipeer/agent-sdk 0.7.2 → 0.7.3

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
@@ -7828,7 +7828,7 @@ var AnthropicProvider = class extends BaseProvider {
7828
7828
  const { system, messages } = this.splitSystemMessages(request.messages);
7829
7829
  const body = {
7830
7830
  model: request.model || this.defaultModel,
7831
- messages: messages.map((m) => this.toAnthropicMessage(m)),
7831
+ messages: this.toAnthropicMessages(messages),
7832
7832
  max_tokens: request.maxTokens ?? 4096,
7833
7833
  stream
7834
7834
  };
@@ -7884,20 +7884,44 @@ var AnthropicProvider = class extends BaseProvider {
7884
7884
  messages: rest
7885
7885
  };
7886
7886
  }
7887
- toAnthropicMessage(m) {
7888
- if (m.role === "tool" && m.toolCallId) {
7889
- const content = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
7890
- return {
7891
- role: "user",
7892
- content: [
7893
- {
7894
- type: "tool_result",
7895
- tool_use_id: m.toolCallId,
7896
- content
7897
- }
7898
- ]
7899
- };
7887
+ /**
7888
+ * Converts unified messages to Anthropic format, coalescing consecutive
7889
+ * tool-result messages into a single `user` turn.
7890
+ *
7891
+ * The unified message list carries one `role: "tool"` entry per tool call
7892
+ * (mirroring OpenAI's shape). Anthropic's Messages API expects every
7893
+ * `tool_result` block answering a single assistant turn's `tool_use`
7894
+ * block(s) to be batched into ONE `user` message rather than sent as
7895
+ * separate consecutive `user` messages.
7896
+ */
7897
+ toAnthropicMessages(messages) {
7898
+ const result = [];
7899
+ let mergingToolResults = false;
7900
+ for (const m of messages) {
7901
+ if (m.role === "tool" && m.toolCallId) {
7902
+ const toolResult = this.toAnthropicToolResult(m);
7903
+ if (mergingToolResults) {
7904
+ result[result.length - 1].content.push(toolResult);
7905
+ } else {
7906
+ result.push({ role: "user", content: [toolResult] });
7907
+ mergingToolResults = true;
7908
+ }
7909
+ continue;
7910
+ }
7911
+ mergingToolResults = false;
7912
+ result.push(this.toAnthropicMessage(m));
7900
7913
  }
7914
+ return result;
7915
+ }
7916
+ toAnthropicToolResult(m) {
7917
+ const content = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
7918
+ return {
7919
+ type: "tool_result",
7920
+ tool_use_id: m.toolCallId,
7921
+ content
7922
+ };
7923
+ }
7924
+ toAnthropicMessage(m) {
7901
7925
  if (m.role === "assistant" && m.toolCalls?.length) {
7902
7926
  const content = [];
7903
7927
  appendReasoningBlocks(content, m.reasoning?.blocks);
@@ -8282,7 +8306,7 @@ var BedrockProvider = class extends BaseProvider {
8282
8306
  buildConverseBody(request) {
8283
8307
  const { system, messages } = this.splitSystemMessages(request.messages);
8284
8308
  const body = {
8285
- messages: messages.map((m) => this.toBedrockMessage(m))
8309
+ messages: this.toBedrockMessages(messages)
8286
8310
  };
8287
8311
  if (system.length > 0) {
8288
8312
  const systemBlocks = system.map((s) => ({ text: s }));
@@ -8332,22 +8356,47 @@ var BedrockProvider = class extends BaseProvider {
8332
8356
  }
8333
8357
  return { system, messages: rest };
8334
8358
  }
8335
- toBedrockMessage(m) {
8336
- if (m.role === "tool" && m.toolCallId) {
8337
- const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
8338
- return {
8339
- role: "user",
8340
- content: [
8341
- {
8342
- toolResult: {
8343
- toolUseId: m.toolCallId,
8344
- content: [{ text }],
8345
- status: "success"
8346
- }
8347
- }
8348
- ]
8349
- };
8359
+ /**
8360
+ * Converts unified messages to Bedrock Converse format, coalescing
8361
+ * consecutive tool-result messages into a single `user` turn.
8362
+ *
8363
+ * The unified message list carries one `role: "tool"` entry per tool call
8364
+ * (mirroring OpenAI's shape). Bedrock's Converse API requires every
8365
+ * `toolResult` block answering a single assistant turn's `toolUse`
8366
+ * block(s) to be batched into ONE `user` message — sending them as
8367
+ * separate consecutive `user` messages triggers: "Expected toolResult
8368
+ * blocks at messages.N.content for the following Ids: ...".
8369
+ */
8370
+ toBedrockMessages(messages) {
8371
+ const result = [];
8372
+ let mergingToolResults = false;
8373
+ for (const m of messages) {
8374
+ if (m.role === "tool" && m.toolCallId) {
8375
+ const toolResult = this.toBedrockToolResult(m);
8376
+ if (mergingToolResults) {
8377
+ result[result.length - 1].content.push(toolResult);
8378
+ } else {
8379
+ result.push({ role: "user", content: [toolResult] });
8380
+ mergingToolResults = true;
8381
+ }
8382
+ continue;
8383
+ }
8384
+ mergingToolResults = false;
8385
+ result.push(this.toBedrockMessage(m));
8350
8386
  }
8387
+ return result;
8388
+ }
8389
+ toBedrockToolResult(m) {
8390
+ const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
8391
+ return {
8392
+ toolResult: {
8393
+ toolUseId: m.toolCallId,
8394
+ content: [{ text }],
8395
+ status: "success"
8396
+ }
8397
+ };
8398
+ }
8399
+ toBedrockMessage(m) {
8351
8400
  if (m.role === "assistant" && m.toolCalls?.length) {
8352
8401
  const content2 = [];
8353
8402
  appendBedrockReasoning(content2, m.reasoning?.blocks);
@@ -8653,12 +8702,24 @@ var VertexProvider = class extends BaseProvider {
8653
8702
  buildContents(messages) {
8654
8703
  const systemParts = [];
8655
8704
  const contents = [];
8705
+ let mergingToolResults = false;
8656
8706
  for (const m of messages) {
8657
8707
  if (m.role === "system") {
8658
8708
  const text = typeof m.content === "string" ? m.content : m.content.filter((p) => p.type === "text").map((p) => p.text).join("\n");
8659
8709
  if (text) systemParts.push(text);
8660
8710
  continue;
8661
8711
  }
8712
+ if (m.role === "tool" && m.toolCallId) {
8713
+ const part = this.toGeminiFunctionResponsePart(m);
8714
+ if (mergingToolResults) {
8715
+ contents[contents.length - 1].parts.push(part);
8716
+ } else {
8717
+ contents.push({ role: "user", parts: [part] });
8718
+ mergingToolResults = true;
8719
+ }
8720
+ continue;
8721
+ }
8722
+ mergingToolResults = false;
8662
8723
  contents.push(this.toGeminiContent(m));
8663
8724
  }
8664
8725
  return {
@@ -8666,27 +8727,22 @@ var VertexProvider = class extends BaseProvider {
8666
8727
  contents
8667
8728
  };
8668
8729
  }
8669
- toGeminiContent(m) {
8670
- if (m.role === "tool" && m.toolCallId) {
8671
- const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
8672
- let response;
8673
- try {
8674
- response = JSON.parse(text);
8675
- } catch {
8676
- response = { result: text };
8677
- }
8678
- return {
8679
- role: "user",
8680
- parts: [
8681
- {
8682
- functionResponse: {
8683
- name: m.name ?? m.toolCallId,
8684
- response
8685
- }
8686
- }
8687
- ]
8688
- };
8730
+ toGeminiFunctionResponsePart(m) {
8731
+ const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
8732
+ let response;
8733
+ try {
8734
+ response = JSON.parse(text);
8735
+ } catch {
8736
+ response = { result: text };
8689
8737
  }
8738
+ return {
8739
+ functionResponse: {
8740
+ name: m.name ?? m.toolCallId,
8741
+ response
8742
+ }
8743
+ };
8744
+ }
8745
+ toGeminiContent(m) {
8690
8746
  if (m.role === "assistant" && m.toolCalls?.length) {
8691
8747
  const parts2 = [];
8692
8748
  if (typeof m.content === "string" && m.content) {