@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.d.cts CHANGED
@@ -2239,6 +2239,18 @@ declare class AnthropicProvider extends BaseProvider {
2239
2239
  completeStream(request: ChatCompletionRequest): AsyncGenerator<ChatCompletionChunk, void, unknown>;
2240
2240
  private buildRequestBody;
2241
2241
  private splitSystemMessages;
2242
+ /**
2243
+ * Converts unified messages to Anthropic format, coalescing consecutive
2244
+ * tool-result messages into a single `user` turn.
2245
+ *
2246
+ * The unified message list carries one `role: "tool"` entry per tool call
2247
+ * (mirroring OpenAI's shape). Anthropic's Messages API expects every
2248
+ * `tool_result` block answering a single assistant turn's `tool_use`
2249
+ * block(s) to be batched into ONE `user` message rather than sent as
2250
+ * separate consecutive `user` messages.
2251
+ */
2252
+ private toAnthropicMessages;
2253
+ private toAnthropicToolResult;
2242
2254
  private toAnthropicMessage;
2243
2255
  private toAnthropicTool;
2244
2256
  private doFetch;
@@ -2276,6 +2288,19 @@ declare class BedrockProvider extends BaseProvider {
2276
2288
  completeStream(request: ChatCompletionRequest): AsyncGenerator<ChatCompletionChunk, void, unknown>;
2277
2289
  private buildConverseBody;
2278
2290
  private splitSystemMessages;
2291
+ /**
2292
+ * Converts unified messages to Bedrock Converse format, coalescing
2293
+ * consecutive tool-result messages into a single `user` turn.
2294
+ *
2295
+ * The unified message list carries one `role: "tool"` entry per tool call
2296
+ * (mirroring OpenAI's shape). Bedrock's Converse API requires every
2297
+ * `toolResult` block answering a single assistant turn's `toolUse`
2298
+ * block(s) to be batched into ONE `user` message — sending them as
2299
+ * separate consecutive `user` messages triggers: "Expected toolResult
2300
+ * blocks at messages.N.content for the following Ids: ...".
2301
+ */
2302
+ private toBedrockMessages;
2303
+ private toBedrockToolResult;
2279
2304
  private toBedrockMessage;
2280
2305
  private toBedrockTool;
2281
2306
  private doFetch;
@@ -2297,6 +2322,7 @@ declare class VertexProvider extends BaseProvider {
2297
2322
  private buildUrl;
2298
2323
  private buildGeminiBody;
2299
2324
  private buildContents;
2325
+ private toGeminiFunctionResponsePart;
2300
2326
  private toGeminiContent;
2301
2327
  private toGeminiTool;
2302
2328
  private getAccessToken;
package/dist/index.d.ts CHANGED
@@ -2239,6 +2239,18 @@ declare class AnthropicProvider extends BaseProvider {
2239
2239
  completeStream(request: ChatCompletionRequest): AsyncGenerator<ChatCompletionChunk, void, unknown>;
2240
2240
  private buildRequestBody;
2241
2241
  private splitSystemMessages;
2242
+ /**
2243
+ * Converts unified messages to Anthropic format, coalescing consecutive
2244
+ * tool-result messages into a single `user` turn.
2245
+ *
2246
+ * The unified message list carries one `role: "tool"` entry per tool call
2247
+ * (mirroring OpenAI's shape). Anthropic's Messages API expects every
2248
+ * `tool_result` block answering a single assistant turn's `tool_use`
2249
+ * block(s) to be batched into ONE `user` message rather than sent as
2250
+ * separate consecutive `user` messages.
2251
+ */
2252
+ private toAnthropicMessages;
2253
+ private toAnthropicToolResult;
2242
2254
  private toAnthropicMessage;
2243
2255
  private toAnthropicTool;
2244
2256
  private doFetch;
@@ -2276,6 +2288,19 @@ declare class BedrockProvider extends BaseProvider {
2276
2288
  completeStream(request: ChatCompletionRequest): AsyncGenerator<ChatCompletionChunk, void, unknown>;
2277
2289
  private buildConverseBody;
2278
2290
  private splitSystemMessages;
2291
+ /**
2292
+ * Converts unified messages to Bedrock Converse format, coalescing
2293
+ * consecutive tool-result messages into a single `user` turn.
2294
+ *
2295
+ * The unified message list carries one `role: "tool"` entry per tool call
2296
+ * (mirroring OpenAI's shape). Bedrock's Converse API requires every
2297
+ * `toolResult` block answering a single assistant turn's `toolUse`
2298
+ * block(s) to be batched into ONE `user` message — sending them as
2299
+ * separate consecutive `user` messages triggers: "Expected toolResult
2300
+ * blocks at messages.N.content for the following Ids: ...".
2301
+ */
2302
+ private toBedrockMessages;
2303
+ private toBedrockToolResult;
2279
2304
  private toBedrockMessage;
2280
2305
  private toBedrockTool;
2281
2306
  private doFetch;
@@ -2297,6 +2322,7 @@ declare class VertexProvider extends BaseProvider {
2297
2322
  private buildUrl;
2298
2323
  private buildGeminiBody;
2299
2324
  private buildContents;
2325
+ private toGeminiFunctionResponsePart;
2300
2326
  private toGeminiContent;
2301
2327
  private toGeminiTool;
2302
2328
  private getAccessToken;
package/dist/index.mjs CHANGED
@@ -7818,7 +7818,7 @@ var AnthropicProvider = class extends BaseProvider {
7818
7818
  const { system, messages } = this.splitSystemMessages(request.messages);
7819
7819
  const body = {
7820
7820
  model: request.model || this.defaultModel,
7821
- messages: messages.map((m) => this.toAnthropicMessage(m)),
7821
+ messages: this.toAnthropicMessages(messages),
7822
7822
  max_tokens: request.maxTokens ?? 4096,
7823
7823
  stream
7824
7824
  };
@@ -7874,20 +7874,44 @@ var AnthropicProvider = class extends BaseProvider {
7874
7874
  messages: rest
7875
7875
  };
7876
7876
  }
7877
- toAnthropicMessage(m) {
7878
- if (m.role === "tool" && m.toolCallId) {
7879
- const content = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
7880
- return {
7881
- role: "user",
7882
- content: [
7883
- {
7884
- type: "tool_result",
7885
- tool_use_id: m.toolCallId,
7886
- content
7887
- }
7888
- ]
7889
- };
7877
+ /**
7878
+ * Converts unified messages to Anthropic format, coalescing consecutive
7879
+ * tool-result messages into a single `user` turn.
7880
+ *
7881
+ * The unified message list carries one `role: "tool"` entry per tool call
7882
+ * (mirroring OpenAI's shape). Anthropic's Messages API expects every
7883
+ * `tool_result` block answering a single assistant turn's `tool_use`
7884
+ * block(s) to be batched into ONE `user` message rather than sent as
7885
+ * separate consecutive `user` messages.
7886
+ */
7887
+ toAnthropicMessages(messages) {
7888
+ const result = [];
7889
+ let mergingToolResults = false;
7890
+ for (const m of messages) {
7891
+ if (m.role === "tool" && m.toolCallId) {
7892
+ const toolResult = this.toAnthropicToolResult(m);
7893
+ if (mergingToolResults) {
7894
+ result[result.length - 1].content.push(toolResult);
7895
+ } else {
7896
+ result.push({ role: "user", content: [toolResult] });
7897
+ mergingToolResults = true;
7898
+ }
7899
+ continue;
7900
+ }
7901
+ mergingToolResults = false;
7902
+ result.push(this.toAnthropicMessage(m));
7890
7903
  }
7904
+ return result;
7905
+ }
7906
+ toAnthropicToolResult(m) {
7907
+ const content = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
7908
+ return {
7909
+ type: "tool_result",
7910
+ tool_use_id: m.toolCallId,
7911
+ content
7912
+ };
7913
+ }
7914
+ toAnthropicMessage(m) {
7891
7915
  if (m.role === "assistant" && m.toolCalls?.length) {
7892
7916
  const content = [];
7893
7917
  appendReasoningBlocks(content, m.reasoning?.blocks);
@@ -8272,7 +8296,7 @@ var BedrockProvider = class extends BaseProvider {
8272
8296
  buildConverseBody(request) {
8273
8297
  const { system, messages } = this.splitSystemMessages(request.messages);
8274
8298
  const body = {
8275
- messages: messages.map((m) => this.toBedrockMessage(m))
8299
+ messages: this.toBedrockMessages(messages)
8276
8300
  };
8277
8301
  if (system.length > 0) {
8278
8302
  const systemBlocks = system.map((s) => ({ text: s }));
@@ -8322,22 +8346,47 @@ var BedrockProvider = class extends BaseProvider {
8322
8346
  }
8323
8347
  return { system, messages: rest };
8324
8348
  }
8325
- toBedrockMessage(m) {
8326
- if (m.role === "tool" && m.toolCallId) {
8327
- const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
8328
- return {
8329
- role: "user",
8330
- content: [
8331
- {
8332
- toolResult: {
8333
- toolUseId: m.toolCallId,
8334
- content: [{ text }],
8335
- status: "success"
8336
- }
8337
- }
8338
- ]
8339
- };
8349
+ /**
8350
+ * Converts unified messages to Bedrock Converse format, coalescing
8351
+ * consecutive tool-result messages into a single `user` turn.
8352
+ *
8353
+ * The unified message list carries one `role: "tool"` entry per tool call
8354
+ * (mirroring OpenAI's shape). Bedrock's Converse API requires every
8355
+ * `toolResult` block answering a single assistant turn's `toolUse`
8356
+ * block(s) to be batched into ONE `user` message — sending them as
8357
+ * separate consecutive `user` messages triggers: "Expected toolResult
8358
+ * blocks at messages.N.content for the following Ids: ...".
8359
+ */
8360
+ toBedrockMessages(messages) {
8361
+ const result = [];
8362
+ let mergingToolResults = false;
8363
+ for (const m of messages) {
8364
+ if (m.role === "tool" && m.toolCallId) {
8365
+ const toolResult = this.toBedrockToolResult(m);
8366
+ if (mergingToolResults) {
8367
+ result[result.length - 1].content.push(toolResult);
8368
+ } else {
8369
+ result.push({ role: "user", content: [toolResult] });
8370
+ mergingToolResults = true;
8371
+ }
8372
+ continue;
8373
+ }
8374
+ mergingToolResults = false;
8375
+ result.push(this.toBedrockMessage(m));
8340
8376
  }
8377
+ return result;
8378
+ }
8379
+ toBedrockToolResult(m) {
8380
+ const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
8381
+ return {
8382
+ toolResult: {
8383
+ toolUseId: m.toolCallId,
8384
+ content: [{ text }],
8385
+ status: "success"
8386
+ }
8387
+ };
8388
+ }
8389
+ toBedrockMessage(m) {
8341
8390
  if (m.role === "assistant" && m.toolCalls?.length) {
8342
8391
  const content2 = [];
8343
8392
  appendBedrockReasoning(content2, m.reasoning?.blocks);
@@ -8643,12 +8692,24 @@ var VertexProvider = class extends BaseProvider {
8643
8692
  buildContents(messages) {
8644
8693
  const systemParts = [];
8645
8694
  const contents = [];
8695
+ let mergingToolResults = false;
8646
8696
  for (const m of messages) {
8647
8697
  if (m.role === "system") {
8648
8698
  const text = typeof m.content === "string" ? m.content : m.content.filter((p) => p.type === "text").map((p) => p.text).join("\n");
8649
8699
  if (text) systemParts.push(text);
8650
8700
  continue;
8651
8701
  }
8702
+ if (m.role === "tool" && m.toolCallId) {
8703
+ const part = this.toGeminiFunctionResponsePart(m);
8704
+ if (mergingToolResults) {
8705
+ contents[contents.length - 1].parts.push(part);
8706
+ } else {
8707
+ contents.push({ role: "user", parts: [part] });
8708
+ mergingToolResults = true;
8709
+ }
8710
+ continue;
8711
+ }
8712
+ mergingToolResults = false;
8652
8713
  contents.push(this.toGeminiContent(m));
8653
8714
  }
8654
8715
  return {
@@ -8656,27 +8717,22 @@ var VertexProvider = class extends BaseProvider {
8656
8717
  contents
8657
8718
  };
8658
8719
  }
8659
- toGeminiContent(m) {
8660
- if (m.role === "tool" && m.toolCallId) {
8661
- const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
8662
- let response;
8663
- try {
8664
- response = JSON.parse(text);
8665
- } catch {
8666
- response = { result: text };
8667
- }
8668
- return {
8669
- role: "user",
8670
- parts: [
8671
- {
8672
- functionResponse: {
8673
- name: m.name ?? m.toolCallId,
8674
- response
8675
- }
8676
- }
8677
- ]
8678
- };
8720
+ toGeminiFunctionResponsePart(m) {
8721
+ const text = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
8722
+ let response;
8723
+ try {
8724
+ response = JSON.parse(text);
8725
+ } catch {
8726
+ response = { result: text };
8679
8727
  }
8728
+ return {
8729
+ functionResponse: {
8730
+ name: m.name ?? m.toolCallId,
8731
+ response
8732
+ }
8733
+ };
8734
+ }
8735
+ toGeminiContent(m) {
8680
8736
  if (m.role === "assistant" && m.toolCalls?.length) {
8681
8737
  const parts2 = [];
8682
8738
  if (typeof m.content === "string" && m.content) {