@langchain/anthropic 1.5.12 → 1.6.0

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 (70) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/chat_models.cjs +1 -1
  3. package/dist/chat_models.cjs.map +1 -1
  4. package/dist/chat_models.d.cts +12 -8
  5. package/dist/chat_models.d.cts.map +1 -1
  6. package/dist/chat_models.d.ts +12 -8
  7. package/dist/chat_models.d.ts.map +1 -1
  8. package/dist/chat_models.js +1 -1
  9. package/dist/chat_models.js.map +1 -1
  10. package/dist/profiles.cjs +87 -36
  11. package/dist/profiles.cjs.map +1 -1
  12. package/dist/profiles.js +87 -36
  13. package/dist/profiles.js.map +1 -1
  14. package/dist/tools/bash.d.cts +2 -3
  15. package/dist/tools/bash.d.cts.map +1 -1
  16. package/dist/tools/bash.d.ts +2 -3
  17. package/dist/tools/bash.d.ts.map +1 -1
  18. package/dist/tools/codeExecution.d.cts +2 -3
  19. package/dist/tools/codeExecution.d.cts.map +1 -1
  20. package/dist/tools/codeExecution.d.ts +2 -3
  21. package/dist/tools/codeExecution.d.ts.map +1 -1
  22. package/dist/tools/computer.d.cts +5 -6
  23. package/dist/tools/computer.d.cts.map +1 -1
  24. package/dist/tools/computer.d.ts +5 -6
  25. package/dist/tools/computer.d.ts.map +1 -1
  26. package/dist/tools/index.d.cts +2 -2
  27. package/dist/tools/index.d.cts.map +1 -1
  28. package/dist/tools/index.d.ts +2 -2
  29. package/dist/tools/index.d.ts.map +1 -1
  30. package/dist/tools/mcpToolset.d.cts +4 -5
  31. package/dist/tools/mcpToolset.d.cts.map +1 -1
  32. package/dist/tools/mcpToolset.d.ts +4 -5
  33. package/dist/tools/mcpToolset.d.ts.map +1 -1
  34. package/dist/tools/memory.d.cts +1 -2
  35. package/dist/tools/memory.d.cts.map +1 -1
  36. package/dist/tools/memory.d.ts +1 -2
  37. package/dist/tools/memory.d.ts.map +1 -1
  38. package/dist/tools/textEditor.d.cts +2 -3
  39. package/dist/tools/textEditor.d.cts.map +1 -1
  40. package/dist/tools/textEditor.d.ts +2 -3
  41. package/dist/tools/textEditor.d.ts.map +1 -1
  42. package/dist/tools/toolSearch.d.cts +3 -4
  43. package/dist/tools/toolSearch.d.cts.map +1 -1
  44. package/dist/tools/toolSearch.d.ts +3 -4
  45. package/dist/tools/toolSearch.d.ts.map +1 -1
  46. package/dist/tools/types.d.cts +68 -69
  47. package/dist/tools/types.d.cts.map +1 -1
  48. package/dist/tools/types.d.ts +68 -69
  49. package/dist/tools/types.d.ts.map +1 -1
  50. package/dist/tools/webFetch.d.cts +2 -3
  51. package/dist/tools/webFetch.d.cts.map +1 -1
  52. package/dist/tools/webFetch.d.ts +2 -3
  53. package/dist/tools/webFetch.d.ts.map +1 -1
  54. package/dist/tools/webSearch.d.cts +2 -3
  55. package/dist/tools/webSearch.d.cts.map +1 -1
  56. package/dist/tools/webSearch.d.ts +2 -3
  57. package/dist/tools/webSearch.d.ts.map +1 -1
  58. package/dist/types.d.cts +27 -28
  59. package/dist/types.d.cts.map +1 -1
  60. package/dist/types.d.ts +27 -28
  61. package/dist/types.d.ts.map +1 -1
  62. package/dist/utils/prompts.d.cts +1 -2
  63. package/dist/utils/prompts.d.cts.map +1 -1
  64. package/dist/utils/prompts.d.ts +1 -2
  65. package/dist/utils/prompts.d.ts.map +1 -1
  66. package/dist/utils/stream_events.cjs +27 -11
  67. package/dist/utils/stream_events.cjs.map +1 -1
  68. package/dist/utils/stream_events.js +27 -11
  69. package/dist/utils/stream_events.js.map +1 -1
  70. package/package.json +9 -9
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","names":[],"sources":["../../src/tools/types.ts"],"mappings":";;;;;;;;cAUa,iCAA+B,EAAA;;;GAG1C,EAAA,KAAA;cAEW,mCAAiC,EAAA;;;;GAI5C,EAAA,KAAA;cAEW,uCAAqC,EAAA;;;;;GAKhD,EAAA,KAAA;cAEW,mCAAiC,EAAA;;;;;GA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G,uBAAuB,EAAE,aAAa;YAEtC,qBAAqB,EAAE,aAAa;YAEpC,oBAAoB,EAAE,aAAa;YAEnC,0BAA0B,EAAE,aAC/B;YAGG,wBAAwB,EAAE,aAAa;YAEvC,qBAAqB,EAAE,aAAa;YAEpC,qBAAqB,EAAE,aAAa;;;;;qBAQnC,kCAAgC,EAAA;;GAE3C,EAAA,KAAA;qBAEW,kCAAgC,EAAA;;GAE3C,EAAA,KAAA;qBAGW,2BAAyB,EAAA,mBAAA,EAAA;;;;GAGpC,EAAA,KAAA;YAGU,6BAA6B,EAAE,aAClC;YAEG,6BAA6B,EAAE,aAClC;YAEG,sBAAsB,EAAE,aAAa"}
@@ -4,7 +4,7 @@ import { ServerTool } from "@langchain/core/tools";
4
4
  /**
5
5
  * Options for the web fetch tool.
6
6
  */
7
- interface WebFetch20250910Options {
7
+ export interface WebFetch20250910Options {
8
8
  /**
9
9
  * Maximum number of times the tool can be used in the API request.
10
10
  */
@@ -88,7 +88,6 @@ interface WebFetch20250910Options {
88
88
  * );
89
89
  * ```
90
90
  */
91
- declare function webFetch_20250910(options?: WebFetch20250910Options): ServerTool;
91
+ export declare function webFetch_20250910(options?: WebFetch20250910Options): ServerTool;
92
92
  //#endregion
93
- export { WebFetch20250910Options, webFetch_20250910 };
94
93
  //# sourceMappingURL=webFetch.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"webFetch.d.cts","names":[],"sources":["../../src/tools/webFetch.ts"],"mappings":";;;;;;UAMiB;;;;EAIf;;;;;EAKA;;;;;EAKA;;;;EAIA,eAAe,YAAU,KAAK;;;;;EAK9B;IACE;;;;;;EAMF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuDc,kBACd,UAAU,0BACT"}
1
+ {"version":3,"file":"webFetch.d.cts","names":["Anthropic"],"sources":["../../src/tools/webFetch.ts"],"mappings":";;;;;;iBAMiB;;;;EAIf;;;;;EAKA;;;;;EAKA;;;;EAIA,eAAeA,YAAU,KAAK;;;;;EAK9B;IACE;;;;;;EAMF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAuDc,kBACd,UAAU,0BACT"}
@@ -4,7 +4,7 @@ import { ServerTool } from "@langchain/core/tools";
4
4
  /**
5
5
  * Options for the web fetch tool.
6
6
  */
7
- interface WebFetch20250910Options {
7
+ export interface WebFetch20250910Options {
8
8
  /**
9
9
  * Maximum number of times the tool can be used in the API request.
10
10
  */
@@ -88,7 +88,6 @@ interface WebFetch20250910Options {
88
88
  * );
89
89
  * ```
90
90
  */
91
- declare function webFetch_20250910(options?: WebFetch20250910Options): ServerTool;
91
+ export declare function webFetch_20250910(options?: WebFetch20250910Options): ServerTool;
92
92
  //#endregion
93
- export { WebFetch20250910Options, webFetch_20250910 };
94
93
  //# sourceMappingURL=webFetch.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"webFetch.d.ts","names":[],"sources":["../../src/tools/webFetch.ts"],"mappings":";;;;;;UAMiB;;;;EAIf;;;;;EAKA;;;;;EAKA;;;;EAIA,eAAe,YAAU,KAAK;;;;;EAK9B;IACE;;;;;;EAMF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuDc,kBACd,UAAU,0BACT"}
1
+ {"version":3,"file":"webFetch.d.ts","names":["Anthropic"],"sources":["../../src/tools/webFetch.ts"],"mappings":";;;;;;iBAMiB;;;;EAIf;;;;;EAKA;;;;;EAKA;;;;EAIA,eAAeA,YAAU,KAAK;;;;;EAK9B;IACE;;;;;;EAMF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAuDc,kBACd,UAAU,0BACT"}
@@ -4,7 +4,7 @@ import { ServerTool } from "@langchain/core/tools";
4
4
  /**
5
5
  * Options for the web search tool.
6
6
  */
7
- interface WebSearch20250305Options {
7
+ export interface WebSearch20250305Options {
8
8
  /**
9
9
  * Maximum number of times the tool can be used in the API request.
10
10
  */
@@ -76,7 +76,6 @@ interface WebSearch20250305Options {
76
76
  * });
77
77
  * ```
78
78
  */
79
- declare function webSearch_20250305(options?: WebSearch20250305Options): ServerTool;
79
+ export declare function webSearch_20250305(options?: WebSearch20250305Options): ServerTool;
80
80
  //#endregion
81
- export { WebSearch20250305Options, webSearch_20250305 };
82
81
  //# sourceMappingURL=webSearch.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"webSearch.d.cts","names":[],"sources":["../../src/tools/webSearch.ts"],"mappings":";;;;;;UAMiB;;;;EAIf;;;;;EAKA;;;;;EAKA;;;;EAIA,eAAe,YAAU,KAAK;;;;;EAK9B,eAAe,YAAU,KAAK,0BAA0B;;;;;EAKxD;;;;EAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyCc,mBACd,UAAU,2BACT"}
1
+ {"version":3,"file":"webSearch.d.cts","names":["Anthropic"],"sources":["../../src/tools/webSearch.ts"],"mappings":";;;;;;iBAMiB;;;;EAIf;;;;;EAKA;;;;;EAKA;;;;EAIA,eAAeA,YAAU,KAAK;;;;;EAK9B,eAAeA,YAAU,KAAK,0BAA0B;;;;;EAKxD;;;;EAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAyCc,mBACd,UAAU,2BACT"}
@@ -4,7 +4,7 @@ import { ServerTool } from "@langchain/core/tools";
4
4
  /**
5
5
  * Options for the web search tool.
6
6
  */
7
- interface WebSearch20250305Options {
7
+ export interface WebSearch20250305Options {
8
8
  /**
9
9
  * Maximum number of times the tool can be used in the API request.
10
10
  */
@@ -76,7 +76,6 @@ interface WebSearch20250305Options {
76
76
  * });
77
77
  * ```
78
78
  */
79
- declare function webSearch_20250305(options?: WebSearch20250305Options): ServerTool;
79
+ export declare function webSearch_20250305(options?: WebSearch20250305Options): ServerTool;
80
80
  //#endregion
81
- export { WebSearch20250305Options, webSearch_20250305 };
82
81
  //# sourceMappingURL=webSearch.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"webSearch.d.ts","names":[],"sources":["../../src/tools/webSearch.ts"],"mappings":";;;;;;UAMiB;;;;EAIf;;;;;EAKA;;;;;EAKA;;;;EAIA,eAAe,YAAU,KAAK;;;;;EAK9B,eAAe,YAAU,KAAK,0BAA0B;;;;;EAKxD;;;;EAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyCc,mBACd,UAAU,2BACT"}
1
+ {"version":3,"file":"webSearch.d.ts","names":["Anthropic"],"sources":["../../src/tools/webSearch.ts"],"mappings":";;;;;;iBAMiB;;;;EAIf;;;;;EAKA;;;;;EAKA;;;;EAIA,eAAeA,YAAU,KAAK;;;;;EAK9B,eAAeA,YAAU,KAAK,0BAA0B;;;;;EAKxD;;;;EAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAyCc,mBACd,UAAU,2BACT"}
package/dist/types.d.cts CHANGED
@@ -1,40 +1,39 @@
1
1
  import Anthropic$1 from "@anthropic-ai/sdk";
2
2
  import { BindToolsInput } from "@langchain/core/language_models/chat_models";
3
3
  //#region src/types.d.ts
4
- type AnthropicMessageCreateParams = Anthropic$1.MessageCreateParamsNonStreaming;
5
- type AnthropicStreamingMessageCreateParams = Anthropic$1.MessageCreateParamsStreaming;
6
- type AnthropicThinkingConfigParam = Anthropic$1.ThinkingConfigParam;
7
- type AnthropicContextManagementConfigParam = Anthropic$1.Beta.BetaContextManagementConfig;
8
- type AnthropicMessageStreamEvent = Anthropic$1.MessageStreamEvent;
9
- type AnthropicRequestOptions = Anthropic$1.RequestOptions;
10
- type AnthropicToolChoice = {
4
+ export type AnthropicMessageCreateParams = Anthropic$1.MessageCreateParamsNonStreaming;
5
+ export type AnthropicStreamingMessageCreateParams = Anthropic$1.MessageCreateParamsStreaming;
6
+ export type AnthropicThinkingConfigParam = Anthropic$1.ThinkingConfigParam;
7
+ export type AnthropicContextManagementConfigParam = Anthropic$1.Beta.BetaContextManagementConfig;
8
+ export type AnthropicMessageStreamEvent = Anthropic$1.MessageStreamEvent;
9
+ export type AnthropicRequestOptions = Anthropic$1.RequestOptions;
10
+ export type AnthropicToolChoice = {
11
11
  type: "tool";
12
12
  name: string;
13
13
  } | "any" | "auto" | "none" | string;
14
- type ChatAnthropicToolType = Anthropic$1.Messages.Tool | BindToolsInput;
15
- type ChatAnthropicOutputFormat = Anthropic$1.Messages.JSONOutputFormat;
16
- type AnthropicOutputConfig = Anthropic$1.Messages.OutputConfig;
17
- type AnthropicTextBlockParam = Anthropic$1.Messages.TextBlockParam;
18
- type AnthropicImageBlockParam = Anthropic$1.Messages.ImageBlockParam;
19
- type AnthropicToolUseBlockParam = Anthropic$1.Messages.ToolUseBlockParam;
20
- type AnthropicToolResultBlockParam = Anthropic$1.Messages.ToolResultBlockParam;
21
- type AnthropicDocumentBlockParam = Anthropic$1.Messages.DocumentBlockParam;
22
- type AnthropicThinkingBlockParam = Anthropic$1.Messages.ThinkingBlockParam;
23
- type AnthropicRedactedThinkingBlockParam = Anthropic$1.Messages.RedactedThinkingBlockParam;
24
- type AnthropicServerToolUseBlockParam = Anthropic$1.Messages.ServerToolUseBlockParam;
25
- type AnthropicWebSearchToolResultBlockParam = Anthropic$1.Messages.WebSearchToolResultBlockParam;
26
- type AnthropicWebSearchResultBlockParam = Anthropic$1.Messages.WebSearchResultBlockParam;
27
- type AnthropicSearchResultBlockParam = Anthropic$1.SearchResultBlockParam;
28
- type AnthropicContainerUploadBlockParam = Anthropic$1.Beta.BetaContainerUploadBlockParam;
29
- type AnthropicCompactionBlockParam = Anthropic$1.Beta.BetaCompactionBlockParam;
30
- type AnthropicMCPServerURLDefinition = Anthropic$1.Beta.Messages.BetaRequestMCPServerURLDefinition;
31
- type ChatAnthropicContentBlock = AnthropicTextBlockParam | AnthropicImageBlockParam | AnthropicToolUseBlockParam | AnthropicToolResultBlockParam | AnthropicDocumentBlockParam | AnthropicThinkingBlockParam | AnthropicRedactedThinkingBlockParam | AnthropicServerToolUseBlockParam | AnthropicWebSearchToolResultBlockParam | AnthropicWebSearchResultBlockParam | AnthropicSearchResultBlockParam | AnthropicContainerUploadBlockParam | AnthropicCompactionBlockParam;
14
+ export type ChatAnthropicToolType = Anthropic$1.Messages.Tool | BindToolsInput;
15
+ export type ChatAnthropicOutputFormat = Anthropic$1.Messages.JSONOutputFormat;
16
+ export type AnthropicOutputConfig = Anthropic$1.Messages.OutputConfig;
17
+ export type AnthropicTextBlockParam = Anthropic$1.Messages.TextBlockParam;
18
+ export type AnthropicImageBlockParam = Anthropic$1.Messages.ImageBlockParam;
19
+ export type AnthropicToolUseBlockParam = Anthropic$1.Messages.ToolUseBlockParam;
20
+ export type AnthropicToolResultBlockParam = Anthropic$1.Messages.ToolResultBlockParam;
21
+ export type AnthropicDocumentBlockParam = Anthropic$1.Messages.DocumentBlockParam;
22
+ export type AnthropicThinkingBlockParam = Anthropic$1.Messages.ThinkingBlockParam;
23
+ export type AnthropicRedactedThinkingBlockParam = Anthropic$1.Messages.RedactedThinkingBlockParam;
24
+ export type AnthropicServerToolUseBlockParam = Anthropic$1.Messages.ServerToolUseBlockParam;
25
+ export type AnthropicWebSearchToolResultBlockParam = Anthropic$1.Messages.WebSearchToolResultBlockParam;
26
+ export type AnthropicWebSearchResultBlockParam = Anthropic$1.Messages.WebSearchResultBlockParam;
27
+ export type AnthropicSearchResultBlockParam = Anthropic$1.SearchResultBlockParam;
28
+ export type AnthropicContainerUploadBlockParam = Anthropic$1.Beta.BetaContainerUploadBlockParam;
29
+ export type AnthropicCompactionBlockParam = Anthropic$1.Beta.BetaCompactionBlockParam;
30
+ export type AnthropicMCPServerURLDefinition = Anthropic$1.Beta.Messages.BetaRequestMCPServerURLDefinition;
31
+ export type ChatAnthropicContentBlock = AnthropicTextBlockParam | AnthropicImageBlockParam | AnthropicToolUseBlockParam | AnthropicToolResultBlockParam | AnthropicDocumentBlockParam | AnthropicThinkingBlockParam | AnthropicRedactedThinkingBlockParam | AnthropicServerToolUseBlockParam | AnthropicWebSearchToolResultBlockParam | AnthropicWebSearchResultBlockParam | AnthropicSearchResultBlockParam | AnthropicContainerUploadBlockParam | AnthropicCompactionBlockParam;
32
32
  /**
33
33
  * A type representing additional parameters that can be passed to the
34
34
  * Anthropic API.
35
35
  */
36
- type Kwargs = Record<string, any>;
37
- type AnthropicInvocationParams = Omit<AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams, "messages"> & Kwargs;
36
+ export type Kwargs = Record<string, any>;
37
+ export type AnthropicInvocationParams = Omit<AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams, "messages"> & Kwargs;
38
38
  //#endregion
39
- export { AnthropicCompactionBlockParam, AnthropicContainerUploadBlockParam, AnthropicContextManagementConfigParam, AnthropicDocumentBlockParam, AnthropicImageBlockParam, AnthropicInvocationParams, AnthropicMCPServerURLDefinition, AnthropicMessageCreateParams, AnthropicMessageStreamEvent, AnthropicOutputConfig, AnthropicRedactedThinkingBlockParam, AnthropicRequestOptions, AnthropicSearchResultBlockParam, AnthropicServerToolUseBlockParam, AnthropicStreamingMessageCreateParams, AnthropicTextBlockParam, AnthropicThinkingBlockParam, AnthropicThinkingConfigParam, AnthropicToolChoice, AnthropicToolResultBlockParam, AnthropicToolUseBlockParam, AnthropicWebSearchResultBlockParam, AnthropicWebSearchToolResultBlockParam, ChatAnthropicContentBlock, ChatAnthropicOutputFormat, ChatAnthropicToolType, Kwargs };
40
39
  //# sourceMappingURL=types.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.cts","names":[],"sources":["../src/types.ts"],"mappings":";;;KAcY,+BACV,YAAU;KACA,wCACV,YAAU;KACA,+BAA+B,YAAU;KACzC,wCACV,YAAU,KAAK;KACL,8BAA8B,YAAU;KACxC,0BAA0B,YAAU;KACpC;EAEN;EACA;;KAMM,wBAAwB,YAAU,SAAS,OAAO;KAClD,4BAA4B,YAAU,SAAS;KAC/C,wBAAwB,YAAU,SAAS;KAE3C,0BAA0B,YAAU,SAAS;KAC7C,2BAA2B,YAAU,SAAS;KAC9C,6BAA6B,YAAU,SAAS;KAChD,gCACV,YAAU,SAAS;KACT,8BAA8B,YAAU,SAAS;KACjD,8BAA8B,YAAU,SAAS;KACjD,sCACV,YAAU,SAAS;KACT,mCACV,YAAU,SAAS;KACT,yCACV,YAAU,SAAS;KACT,qCACV,YAAU,SAAS;KACT,kCAAkC,YAAU;KAC5C,qCACV,YAAU,KAAK;KACL,gCACV,YAAU,KAAK;KACL,kCACV,YAAU,KAAK,SAAS;KAGd,4BACR,0BACA,2BACA,6BACA,gCACA,8BACA,8BACA,sCACA,mCACA,yCACA,qCACA,kCACA,qCACA;;;;;KAyEQ,SAAS;KAET,4BAA4B,KACtC,+BAA+B,qDAG/B"}
1
+ {"version":3,"file":"types.d.cts","names":["Anthropic"],"sources":["../src/types.ts"],"mappings":";;;YAcY,+BACVA,YAAU;YACA,wCACVA,YAAU;YACA,+BAA+BA,YAAU;YACzC,wCACVA,YAAU,KAAK;YACL,8BAA8BA,YAAU;YACxC,0BAA0BA,YAAU;YACpC;EAEN;EACA;;YAMM,wBAAwBA,YAAU,SAAS,OAAO;YAClD,4BAA4BA,YAAU,SAAS;YAC/C,wBAAwBA,YAAU,SAAS;YAE3C,0BAA0BA,YAAU,SAAS;YAC7C,2BAA2BA,YAAU,SAAS;YAC9C,6BAA6BA,YAAU,SAAS;YAChD,gCACVA,YAAU,SAAS;YACT,8BAA8BA,YAAU,SAAS;YACjD,8BAA8BA,YAAU,SAAS;YACjD,sCACVA,YAAU,SAAS;YACT,mCACVA,YAAU,SAAS;YACT,yCACVA,YAAU,SAAS;YACT,qCACVA,YAAU,SAAS;YACT,kCAAkCA,YAAU;YAC5C,qCACVA,YAAU,KAAK;YACL,gCACVA,YAAU,KAAK;YACL,kCACVA,YAAU,KAAK,SAAS;YAGd,4BACR,0BACA,2BACA,6BACA,gCACA,8BACA,8BACA,sCACA,mCACA,yCACA,qCACA,kCACA,qCACA;;;;;YAyEQ,SAAS;YAET,4BAA4B,KACtC,+BAA+B,qDAG/B"}
package/dist/types.d.ts CHANGED
@@ -1,40 +1,39 @@
1
1
  import Anthropic$1 from "@anthropic-ai/sdk";
2
2
  import { BindToolsInput } from "@langchain/core/language_models/chat_models";
3
3
  //#region src/types.d.ts
4
- type AnthropicMessageCreateParams = Anthropic$1.MessageCreateParamsNonStreaming;
5
- type AnthropicStreamingMessageCreateParams = Anthropic$1.MessageCreateParamsStreaming;
6
- type AnthropicThinkingConfigParam = Anthropic$1.ThinkingConfigParam;
7
- type AnthropicContextManagementConfigParam = Anthropic$1.Beta.BetaContextManagementConfig;
8
- type AnthropicMessageStreamEvent = Anthropic$1.MessageStreamEvent;
9
- type AnthropicRequestOptions = Anthropic$1.RequestOptions;
10
- type AnthropicToolChoice = {
4
+ export type AnthropicMessageCreateParams = Anthropic$1.MessageCreateParamsNonStreaming;
5
+ export type AnthropicStreamingMessageCreateParams = Anthropic$1.MessageCreateParamsStreaming;
6
+ export type AnthropicThinkingConfigParam = Anthropic$1.ThinkingConfigParam;
7
+ export type AnthropicContextManagementConfigParam = Anthropic$1.Beta.BetaContextManagementConfig;
8
+ export type AnthropicMessageStreamEvent = Anthropic$1.MessageStreamEvent;
9
+ export type AnthropicRequestOptions = Anthropic$1.RequestOptions;
10
+ export type AnthropicToolChoice = {
11
11
  type: "tool";
12
12
  name: string;
13
13
  } | "any" | "auto" | "none" | string;
14
- type ChatAnthropicToolType = Anthropic$1.Messages.Tool | BindToolsInput;
15
- type ChatAnthropicOutputFormat = Anthropic$1.Messages.JSONOutputFormat;
16
- type AnthropicOutputConfig = Anthropic$1.Messages.OutputConfig;
17
- type AnthropicTextBlockParam = Anthropic$1.Messages.TextBlockParam;
18
- type AnthropicImageBlockParam = Anthropic$1.Messages.ImageBlockParam;
19
- type AnthropicToolUseBlockParam = Anthropic$1.Messages.ToolUseBlockParam;
20
- type AnthropicToolResultBlockParam = Anthropic$1.Messages.ToolResultBlockParam;
21
- type AnthropicDocumentBlockParam = Anthropic$1.Messages.DocumentBlockParam;
22
- type AnthropicThinkingBlockParam = Anthropic$1.Messages.ThinkingBlockParam;
23
- type AnthropicRedactedThinkingBlockParam = Anthropic$1.Messages.RedactedThinkingBlockParam;
24
- type AnthropicServerToolUseBlockParam = Anthropic$1.Messages.ServerToolUseBlockParam;
25
- type AnthropicWebSearchToolResultBlockParam = Anthropic$1.Messages.WebSearchToolResultBlockParam;
26
- type AnthropicWebSearchResultBlockParam = Anthropic$1.Messages.WebSearchResultBlockParam;
27
- type AnthropicSearchResultBlockParam = Anthropic$1.SearchResultBlockParam;
28
- type AnthropicContainerUploadBlockParam = Anthropic$1.Beta.BetaContainerUploadBlockParam;
29
- type AnthropicCompactionBlockParam = Anthropic$1.Beta.BetaCompactionBlockParam;
30
- type AnthropicMCPServerURLDefinition = Anthropic$1.Beta.Messages.BetaRequestMCPServerURLDefinition;
31
- type ChatAnthropicContentBlock = AnthropicTextBlockParam | AnthropicImageBlockParam | AnthropicToolUseBlockParam | AnthropicToolResultBlockParam | AnthropicDocumentBlockParam | AnthropicThinkingBlockParam | AnthropicRedactedThinkingBlockParam | AnthropicServerToolUseBlockParam | AnthropicWebSearchToolResultBlockParam | AnthropicWebSearchResultBlockParam | AnthropicSearchResultBlockParam | AnthropicContainerUploadBlockParam | AnthropicCompactionBlockParam;
14
+ export type ChatAnthropicToolType = Anthropic$1.Messages.Tool | BindToolsInput;
15
+ export type ChatAnthropicOutputFormat = Anthropic$1.Messages.JSONOutputFormat;
16
+ export type AnthropicOutputConfig = Anthropic$1.Messages.OutputConfig;
17
+ export type AnthropicTextBlockParam = Anthropic$1.Messages.TextBlockParam;
18
+ export type AnthropicImageBlockParam = Anthropic$1.Messages.ImageBlockParam;
19
+ export type AnthropicToolUseBlockParam = Anthropic$1.Messages.ToolUseBlockParam;
20
+ export type AnthropicToolResultBlockParam = Anthropic$1.Messages.ToolResultBlockParam;
21
+ export type AnthropicDocumentBlockParam = Anthropic$1.Messages.DocumentBlockParam;
22
+ export type AnthropicThinkingBlockParam = Anthropic$1.Messages.ThinkingBlockParam;
23
+ export type AnthropicRedactedThinkingBlockParam = Anthropic$1.Messages.RedactedThinkingBlockParam;
24
+ export type AnthropicServerToolUseBlockParam = Anthropic$1.Messages.ServerToolUseBlockParam;
25
+ export type AnthropicWebSearchToolResultBlockParam = Anthropic$1.Messages.WebSearchToolResultBlockParam;
26
+ export type AnthropicWebSearchResultBlockParam = Anthropic$1.Messages.WebSearchResultBlockParam;
27
+ export type AnthropicSearchResultBlockParam = Anthropic$1.SearchResultBlockParam;
28
+ export type AnthropicContainerUploadBlockParam = Anthropic$1.Beta.BetaContainerUploadBlockParam;
29
+ export type AnthropicCompactionBlockParam = Anthropic$1.Beta.BetaCompactionBlockParam;
30
+ export type AnthropicMCPServerURLDefinition = Anthropic$1.Beta.Messages.BetaRequestMCPServerURLDefinition;
31
+ export type ChatAnthropicContentBlock = AnthropicTextBlockParam | AnthropicImageBlockParam | AnthropicToolUseBlockParam | AnthropicToolResultBlockParam | AnthropicDocumentBlockParam | AnthropicThinkingBlockParam | AnthropicRedactedThinkingBlockParam | AnthropicServerToolUseBlockParam | AnthropicWebSearchToolResultBlockParam | AnthropicWebSearchResultBlockParam | AnthropicSearchResultBlockParam | AnthropicContainerUploadBlockParam | AnthropicCompactionBlockParam;
32
32
  /**
33
33
  * A type representing additional parameters that can be passed to the
34
34
  * Anthropic API.
35
35
  */
36
- type Kwargs = Record<string, any>;
37
- type AnthropicInvocationParams = Omit<AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams, "messages"> & Kwargs;
36
+ export type Kwargs = Record<string, any>;
37
+ export type AnthropicInvocationParams = Omit<AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams, "messages"> & Kwargs;
38
38
  //#endregion
39
- export { AnthropicCompactionBlockParam, AnthropicContainerUploadBlockParam, AnthropicContextManagementConfigParam, AnthropicDocumentBlockParam, AnthropicImageBlockParam, AnthropicInvocationParams, AnthropicMCPServerURLDefinition, AnthropicMessageCreateParams, AnthropicMessageStreamEvent, AnthropicOutputConfig, AnthropicRedactedThinkingBlockParam, AnthropicRequestOptions, AnthropicSearchResultBlockParam, AnthropicServerToolUseBlockParam, AnthropicStreamingMessageCreateParams, AnthropicTextBlockParam, AnthropicThinkingBlockParam, AnthropicThinkingConfigParam, AnthropicToolChoice, AnthropicToolResultBlockParam, AnthropicToolUseBlockParam, AnthropicWebSearchResultBlockParam, AnthropicWebSearchToolResultBlockParam, ChatAnthropicContentBlock, ChatAnthropicOutputFormat, ChatAnthropicToolType, Kwargs };
40
39
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","names":[],"sources":["../src/types.ts"],"mappings":";;;KAcY,+BACV,YAAU;KACA,wCACV,YAAU;KACA,+BAA+B,YAAU;KACzC,wCACV,YAAU,KAAK;KACL,8BAA8B,YAAU;KACxC,0BAA0B,YAAU;KACpC;EAEN;EACA;;KAMM,wBAAwB,YAAU,SAAS,OAAO;KAClD,4BAA4B,YAAU,SAAS;KAC/C,wBAAwB,YAAU,SAAS;KAE3C,0BAA0B,YAAU,SAAS;KAC7C,2BAA2B,YAAU,SAAS;KAC9C,6BAA6B,YAAU,SAAS;KAChD,gCACV,YAAU,SAAS;KACT,8BAA8B,YAAU,SAAS;KACjD,8BAA8B,YAAU,SAAS;KACjD,sCACV,YAAU,SAAS;KACT,mCACV,YAAU,SAAS;KACT,yCACV,YAAU,SAAS;KACT,qCACV,YAAU,SAAS;KACT,kCAAkC,YAAU;KAC5C,qCACV,YAAU,KAAK;KACL,gCACV,YAAU,KAAK;KACL,kCACV,YAAU,KAAK,SAAS;KAGd,4BACR,0BACA,2BACA,6BACA,gCACA,8BACA,8BACA,sCACA,mCACA,yCACA,qCACA,kCACA,qCACA;;;;;KAyEQ,SAAS;KAET,4BAA4B,KACtC,+BAA+B,qDAG/B"}
1
+ {"version":3,"file":"types.d.ts","names":["Anthropic"],"sources":["../src/types.ts"],"mappings":";;;YAcY,+BACVA,YAAU;YACA,wCACVA,YAAU;YACA,+BAA+BA,YAAU;YACzC,wCACVA,YAAU,KAAK;YACL,8BAA8BA,YAAU;YACxC,0BAA0BA,YAAU;YACpC;EAEN;EACA;;YAMM,wBAAwBA,YAAU,SAAS,OAAO;YAClD,4BAA4BA,YAAU,SAAS;YAC/C,wBAAwBA,YAAU,SAAS;YAE3C,0BAA0BA,YAAU,SAAS;YAC7C,2BAA2BA,YAAU,SAAS;YAC9C,6BAA6BA,YAAU,SAAS;YAChD,gCACVA,YAAU,SAAS;YACT,8BAA8BA,YAAU,SAAS;YACjD,8BAA8BA,YAAU,SAAS;YACjD,sCACVA,YAAU,SAAS;YACT,mCACVA,YAAU,SAAS;YACT,yCACVA,YAAU,SAAS;YACT,qCACVA,YAAU,SAAS;YACT,kCAAkCA,YAAU;YAC5C,qCACVA,YAAU,KAAK;YACL,gCACVA,YAAU,KAAK;YACL,kCACVA,YAAU,KAAK,SAAS;YAGd,4BACR,0BACA,2BACA,6BACA,gCACA,8BACA,8BACA,sCACA,mCACA,yCACA,qCACA,kCACA,qCACA;;;;;YAyEQ,SAAS;YAET,4BAA4B,KACtC,+BAA+B,qDAG/B"}
@@ -37,7 +37,6 @@ import { BasePromptValue } from "@langchain/core/prompt_values";
37
37
  * @param formattedPrompt
38
38
  * @returns A partial Anthropic payload.
39
39
  */
40
- declare function convertPromptToAnthropic(formattedPrompt: BasePromptValue): Anthropic$1.Messages.MessageCreateParams;
40
+ export declare function convertPromptToAnthropic(formattedPrompt: BasePromptValue): Anthropic$1.Messages.MessageCreateParams;
41
41
  //#endregion
42
- export { convertPromptToAnthropic };
43
42
  //# sourceMappingURL=prompts.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"prompts.d.cts","names":[],"sources":["../../src/utils/prompts.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyCgB,yBACd,iBAAiB,kBAChB,YAAU,SAAS"}
1
+ {"version":3,"file":"prompts.d.cts","names":["Anthropic"],"sources":["../../src/utils/prompts.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAyCgB,yBACd,iBAAiB,kBAChBA,YAAU,SAAS"}
@@ -37,7 +37,6 @@ import { BasePromptValue } from "@langchain/core/prompt_values";
37
37
  * @param formattedPrompt
38
38
  * @returns A partial Anthropic payload.
39
39
  */
40
- declare function convertPromptToAnthropic(formattedPrompt: BasePromptValue): Anthropic$1.Messages.MessageCreateParams;
40
+ export declare function convertPromptToAnthropic(formattedPrompt: BasePromptValue): Anthropic$1.Messages.MessageCreateParams;
41
41
  //#endregion
42
- export { convertPromptToAnthropic };
43
42
  //# sourceMappingURL=prompts.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"prompts.d.ts","names":[],"sources":["../../src/utils/prompts.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyCgB,yBACd,iBAAiB,kBAChB,YAAU,SAAS"}
1
+ {"version":3,"file":"prompts.d.ts","names":["Anthropic"],"sources":["../../src/utils/prompts.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAyCgB,yBACd,iBAAiB,kBAChBA,YAAU,SAAS"}
@@ -81,7 +81,7 @@ async function* convertAnthropicStream(source, options = {}) {
81
81
  if (!acc) break;
82
82
  const { contentDelta, accumulated } = applyAnthropicDelta(acc, delta);
83
83
  blockAccumulators.set(index, accumulated);
84
- yield {
84
+ if (contentDelta) yield {
85
85
  event: "content-block-delta",
86
86
  index,
87
87
  delta: contentDelta
@@ -171,7 +171,8 @@ function mapBlockToContentBlock(block, index) {
171
171
  }
172
172
  /**
173
173
  * Map an Anthropic content_block_delta to a content block delta
174
- * and update the accumulated state.
174
+ * and update the accumulated state. `contentDelta` is undefined when
175
+ * there's nothing new to send.
175
176
  */
176
177
  function applyAnthropicDelta(accumulated, delta) {
177
178
  switch (delta.type) {
@@ -195,22 +196,37 @@ function applyAnthropicDelta(accumulated, delta) {
195
196
  reasoning: (accumulated.reasoning ?? "") + delta.thinking
196
197
  }
197
198
  };
198
- case "input_json_delta": {
199
- const newArgs = (accumulated.args ?? "") + delta.partial_json;
200
- return {
201
- contentDelta: {
202
- type: "block-delta",
203
- fields: {
204
- type: accumulated.type,
199
+ case "input_json_delta":
200
+ if (accumulated.type !== "tool_call_chunk" && accumulated.type !== "server_tool_call_chunk") {
201
+ const newArgs = (accumulated.args ?? "") + delta.partial_json;
202
+ return {
203
+ contentDelta: {
204
+ type: "block-delta",
205
+ fields: {
206
+ type: accumulated.type,
207
+ args: newArgs
208
+ }
209
+ },
210
+ accumulated: {
211
+ ...accumulated,
205
212
  args: newArgs
206
213
  }
214
+ };
215
+ }
216
+ if (!delta.partial_json) return {
217
+ contentDelta: void 0,
218
+ accumulated
219
+ };
220
+ return {
221
+ contentDelta: {
222
+ type: "tool-call-delta",
223
+ args: delta.partial_json
207
224
  },
208
225
  accumulated: {
209
226
  ...accumulated,
210
- args: newArgs
227
+ args: (accumulated.args ?? "") + delta.partial_json
211
228
  }
212
229
  };
213
- }
214
230
  case "citations_delta": {
215
231
  const annotations = [...accumulated.annotations ?? [], delta.citation];
216
232
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"stream_events.cjs","names":[],"sources":["../../src/utils/stream_events.ts"],"sourcesContent":["/**\n * Converts a raw Anthropic SSE event stream into LangChain ChatModelStreamEvents.\n *\n * @module\n */\n\nimport type Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n ChatModelStreamEvent,\n ContentBlockDelta,\n FinishReason,\n} from \"@langchain/core/language_models/event\";\nimport type { ContentBlock, UsageMetadata } from \"@langchain/core/messages\";\nimport type { AnthropicMessageStreamEvent } from \"../types.js\";\n\n// ─── Public API ─────────────────────────────────────────────────\n\nexport interface ConvertAnthropicStreamOptions {\n streamUsage?: boolean;\n}\n\n/**\n * Convert an async iterable of raw Anthropic stream events into\n * LangChain `ChatModelStreamEvent`s with typed deltas.\n */\nexport async function* convertAnthropicStream(\n source: AsyncIterable<AnthropicMessageStreamEvent>,\n options: ConvertAnthropicStreamOptions = {}\n): AsyncGenerator<ChatModelStreamEvent> {\n const shouldStreamUsage = options.streamUsage ?? true;\n\n // Track accumulated state per content block (for finalization)\n const blockAccumulators = new Map<\n number,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n Record<string, any>\n >();\n let usageSnapshot: UsageMetadata | undefined;\n let responseMetadata: Record<string, unknown> = {};\n let stopReason: string | null = null;\n\n for await (const data of source) {\n switch (data.type) {\n // ── Message lifecycle ──────────────────────────────────\n case \"message_start\": {\n const { usage, id, model } = data.message;\n if (usage && shouldStreamUsage) {\n usageSnapshot = buildUsageSnapshot(usage);\n }\n yield {\n event: \"message-start\" as const,\n id,\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n };\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"message_start\",\n payload: { model, id },\n };\n break;\n }\n\n case \"message_delta\": {\n stopReason = data.delta.stop_reason;\n // Some gateway providers add additional usage values to the final usage object\n if (\n \"usage\" in data &&\n \"cost\" in data.usage &&\n typeof data.usage.cost === \"number\"\n ) {\n responseMetadata = { usage: { cost: data.usage.cost } };\n }\n if (shouldStreamUsage && data.usage) {\n if (!usageSnapshot) {\n usageSnapshot = {\n input_tokens: 0,\n output_tokens: data.usage.output_tokens,\n total_tokens: data.usage.output_tokens,\n };\n } else {\n usageSnapshot = {\n ...usageSnapshot,\n output_tokens:\n usageSnapshot.output_tokens + data.usage.output_tokens,\n total_tokens:\n usageSnapshot.input_tokens +\n usageSnapshot.output_tokens +\n data.usage.output_tokens,\n };\n }\n yield { event: \"usage\" as const, usage: usageSnapshot };\n }\n if (\n \"context_management\" in data.delta &&\n data.delta.context_management\n ) {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"context_management\",\n payload: data.delta.context_management,\n };\n }\n break;\n }\n\n case \"message_stop\": {\n yield {\n event: \"message-finish\" as const,\n reason: mapStopReason(stopReason),\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n metadata: { model_provider: \"anthropic\" },\n responseMetadata,\n };\n break;\n }\n\n // ── Content block lifecycle ───────────────────────────\n case \"content_block_start\": {\n const { index, content_block } = data;\n const mapped = mapBlockToContentBlock(content_block, index);\n blockAccumulators.set(index, { ...mapped });\n yield {\n event: \"content-block-start\" as const,\n index,\n content: mapped,\n };\n break;\n }\n\n case \"content_block_delta\": {\n const { index, delta } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const { contentDelta, accumulated } = applyAnthropicDelta(acc, delta);\n blockAccumulators.set(index, accumulated);\n\n yield {\n event: \"content-block-delta\" as const,\n index,\n delta: contentDelta,\n };\n break;\n }\n\n case \"content_block_stop\": {\n const { index } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const finalized = finalizeBlock(acc);\n yield {\n event: \"content-block-finish\" as const,\n index,\n content: finalized,\n };\n blockAccumulators.delete(index);\n break;\n }\n\n // ── Unhandled → provider passthrough ───────────────────\n default: {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: data.type,\n payload: data,\n };\n break;\n }\n }\n }\n}\n\n// ─── Internal helpers ───────────────────────────────────────────\n\nfunction mapStopReason(stopReason: string | null | undefined): FinishReason {\n switch (stopReason) {\n case \"end_turn\":\n case \"stop_sequence\":\n return \"stop\";\n case \"tool_use\":\n return \"tool_use\";\n case \"max_tokens\":\n return \"length\";\n default:\n return \"stop\";\n }\n}\n\nfunction buildUsageSnapshot(\n usage: Anthropic.Messages.Usage | Record<string, number>\n): UsageMetadata {\n const cacheCreation =\n (usage as Record<string, number>).cache_creation_input_tokens ?? 0;\n const cacheRead =\n (usage as Record<string, number>).cache_read_input_tokens ?? 0;\n const totalInput = usage.input_tokens + cacheCreation + cacheRead;\n return {\n input_tokens: totalInput,\n output_tokens: usage.output_tokens,\n total_tokens: totalInput + usage.output_tokens,\n input_token_details: {\n cache_creation: cacheCreation,\n cache_read: cacheRead,\n },\n };\n}\n\nfunction mapBlockToContentBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n block: any,\n index: number\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n): Record<string, any> {\n switch (block.type) {\n case \"text\":\n return { type: \"text\" as const, text: block.text ?? \"\", index };\n case \"thinking\":\n return {\n type: \"reasoning\" as const,\n reasoning: block.thinking ?? \"\",\n index,\n };\n case \"redacted_thinking\":\n return { type: \"non_standard\" as const, value: { ...block }, index };\n case \"tool_use\":\n return {\n type: \"tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n case \"server_tool_use\":\n return {\n type: \"server_tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n default:\n return { type: \"non_standard\" as const, value: { ...block }, index };\n }\n}\n\n/**\n * Map an Anthropic content_block_delta to a content block delta\n * and update the accumulated state.\n */\nfunction applyAnthropicDelta(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n delta: any\n): {\n contentDelta: ContentBlockDelta;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>;\n} {\n switch (delta.type) {\n case \"text_delta\":\n return {\n contentDelta: { type: \"text-delta\" as const, text: delta.text },\n accumulated: {\n ...accumulated,\n text: (accumulated.text ?? \"\") + delta.text,\n },\n };\n\n case \"thinking_delta\":\n return {\n contentDelta: {\n type: \"reasoning-delta\" as const,\n reasoning: delta.thinking,\n },\n accumulated: {\n ...accumulated,\n reasoning: (accumulated.reasoning ?? \"\") + delta.thinking,\n },\n };\n\n case \"input_json_delta\": {\n const newArgs = (accumulated.args ?? \"\") + delta.partial_json;\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, args: newArgs },\n },\n accumulated: { ...accumulated, args: newArgs },\n };\n }\n\n case \"citations_delta\": {\n const annotations = [...(accumulated.annotations ?? []), delta.citation];\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: accumulated.type,\n annotations,\n },\n },\n accumulated: {\n ...accumulated,\n annotations,\n },\n };\n }\n\n case \"signature_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, signature: delta.signature },\n },\n accumulated: { ...accumulated, signature: delta.signature },\n };\n\n case \"compaction_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: \"non_standard\",\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n },\n accumulated: {\n ...accumulated,\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n };\n\n default:\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, ...delta },\n },\n accumulated,\n };\n }\n}\n\nfunction finalizeBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>\n): ContentBlock {\n if (\n accumulated.type === \"tool_call_chunk\" ||\n accumulated.type === \"server_tool_call_chunk\"\n ) {\n const finalType =\n accumulated.type === \"tool_call_chunk\"\n ? (\"tool_call\" as const)\n : (\"server_tool_call\" as const);\n let parsedArgs: unknown;\n try {\n parsedArgs = JSON.parse(accumulated.args || \"{}\");\n } catch {\n return {\n type: \"invalid_tool_call\" as const,\n id: accumulated.id,\n name: accumulated.name,\n args: accumulated.args,\n error: \"Failed to parse tool call arguments as JSON\",\n } as ContentBlock.Tools.InvalidToolCall;\n }\n return {\n type: finalType,\n id: accumulated.id,\n name: accumulated.name,\n args: parsedArgs,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n }\n\n const { index: _index, ...rest } = accumulated;\n return rest as ContentBlock;\n}\n"],"mappings":";;;;;AAyBA,gBAAuB,uBACrB,QACA,UAAyC,CAAC,GACJ;CACtC,MAAM,oBAAoB,QAAQ,eAAe;CAGjD,MAAM,oCAAoB,IAAI,IAI5B;CACF,IAAI;CACJ,IAAI,mBAA4C,CAAC;CACjD,IAAI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ACL,cAAc;IACZ,MAAM;IACN,QAAQ;KACN,MAAM;KACN,OAAO;MAAE,GAAI,YAAY,SAAS,CAAC;MAAI,YAAY;KAAM;IAC3D;GACF;GACA,aAAa;IACX,GAAG;IACH,OAAO;KAAE,GAAI,YAAY,SAAS,CAAC;KAAI,YAAY;IAAM;GAC3D;EACF;EAEF,SACE,OAAO;GACL,cAAc;IACZ,MAAM;IACN,QAAQ;KAAE,MAAM,YAAY;KAAM,GAAG;IAAM;GAC7C;GACA;EACF;CACJ;AACF;AAEA,SAAS,cAEP,aACc;CACd,IACE,YAAY,SAAS,qBACrB,YAAY,SAAS,0BACrB;EACA,MAAM,YACJ,YAAY,SAAS,oBAChB,cACA;EACP,IAAI;EACJ,IAAI;GACF,aAAa,KAAK,MAAM,YAAY,QAAQ,IAAI;EAClD,QAAQ;GACN,OAAO;IACL,MAAM;IACN,IAAI,YAAY;IAChB,MAAM,YAAY;IAClB,MAAM,YAAY;IAClB,OAAO;GACT;EACF;EACA,OAAO;GACL,MAAM;GACN,IAAI,YAAY;GAChB,MAAM,YAAY;GAClB,MAAM;EAER;CACF;CAEA,MAAM,EAAE,OAAO,QAAQ,GAAG,SAAS;CACnC,OAAO;AACT"}
1
+ {"version":3,"file":"stream_events.cjs","names":[],"sources":["../../src/utils/stream_events.ts"],"sourcesContent":["/**\n * Converts a raw Anthropic SSE event stream into LangChain ChatModelStreamEvents.\n *\n * @module\n */\n\nimport type Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n ChatModelStreamEvent,\n ContentBlockDelta,\n FinishReason,\n} from \"@langchain/core/language_models/event\";\nimport type { ContentBlock, UsageMetadata } from \"@langchain/core/messages\";\nimport type { AnthropicMessageStreamEvent } from \"../types.js\";\n\n// ─── Public API ─────────────────────────────────────────────────\n\nexport interface ConvertAnthropicStreamOptions {\n streamUsage?: boolean;\n}\n\n/**\n * Convert an async iterable of raw Anthropic stream events into\n * LangChain `ChatModelStreamEvent`s with typed deltas.\n */\nexport async function* convertAnthropicStream(\n source: AsyncIterable<AnthropicMessageStreamEvent>,\n options: ConvertAnthropicStreamOptions = {}\n): AsyncGenerator<ChatModelStreamEvent> {\n const shouldStreamUsage = options.streamUsage ?? true;\n\n // Track accumulated state per content block (for finalization)\n const blockAccumulators = new Map<\n number,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n Record<string, any>\n >();\n let usageSnapshot: UsageMetadata | undefined;\n let responseMetadata: Record<string, unknown> = {};\n let stopReason: string | null = null;\n\n for await (const data of source) {\n switch (data.type) {\n // ── Message lifecycle ──────────────────────────────────\n case \"message_start\": {\n const { usage, id, model } = data.message;\n if (usage && shouldStreamUsage) {\n usageSnapshot = buildUsageSnapshot(usage);\n }\n yield {\n event: \"message-start\" as const,\n id,\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n };\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"message_start\",\n payload: { model, id },\n };\n break;\n }\n\n case \"message_delta\": {\n stopReason = data.delta.stop_reason;\n // Some gateway providers add additional usage values to the final usage object\n if (\n \"usage\" in data &&\n \"cost\" in data.usage &&\n typeof data.usage.cost === \"number\"\n ) {\n responseMetadata = { usage: { cost: data.usage.cost } };\n }\n if (shouldStreamUsage && data.usage) {\n if (!usageSnapshot) {\n usageSnapshot = {\n input_tokens: 0,\n output_tokens: data.usage.output_tokens,\n total_tokens: data.usage.output_tokens,\n };\n } else {\n usageSnapshot = {\n ...usageSnapshot,\n output_tokens:\n usageSnapshot.output_tokens + data.usage.output_tokens,\n total_tokens:\n usageSnapshot.input_tokens +\n usageSnapshot.output_tokens +\n data.usage.output_tokens,\n };\n }\n yield { event: \"usage\" as const, usage: usageSnapshot };\n }\n if (\n \"context_management\" in data.delta &&\n data.delta.context_management\n ) {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"context_management\",\n payload: data.delta.context_management,\n };\n }\n break;\n }\n\n case \"message_stop\": {\n yield {\n event: \"message-finish\" as const,\n reason: mapStopReason(stopReason),\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n metadata: { model_provider: \"anthropic\" },\n responseMetadata,\n };\n break;\n }\n\n // ── Content block lifecycle ───────────────────────────\n case \"content_block_start\": {\n const { index, content_block } = data;\n const mapped = mapBlockToContentBlock(content_block, index);\n blockAccumulators.set(index, { ...mapped });\n yield {\n event: \"content-block-start\" as const,\n index,\n content: mapped,\n };\n break;\n }\n\n case \"content_block_delta\": {\n const { index, delta } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const { contentDelta, accumulated } = applyAnthropicDelta(acc, delta);\n blockAccumulators.set(index, accumulated);\n\n if (contentDelta) {\n yield {\n event: \"content-block-delta\" as const,\n index,\n delta: contentDelta,\n };\n }\n break;\n }\n\n case \"content_block_stop\": {\n const { index } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const finalized = finalizeBlock(acc);\n yield {\n event: \"content-block-finish\" as const,\n index,\n content: finalized,\n };\n blockAccumulators.delete(index);\n break;\n }\n\n // ── Unhandled → provider passthrough ───────────────────\n default: {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: data.type,\n payload: data,\n };\n break;\n }\n }\n }\n}\n\n// ─── Internal helpers ───────────────────────────────────────────\n\nfunction mapStopReason(stopReason: string | null | undefined): FinishReason {\n switch (stopReason) {\n case \"end_turn\":\n case \"stop_sequence\":\n return \"stop\";\n case \"tool_use\":\n return \"tool_use\";\n case \"max_tokens\":\n return \"length\";\n default:\n return \"stop\";\n }\n}\n\nfunction buildUsageSnapshot(\n usage: Anthropic.Messages.Usage | Record<string, number>\n): UsageMetadata {\n const cacheCreation =\n (usage as Record<string, number>).cache_creation_input_tokens ?? 0;\n const cacheRead =\n (usage as Record<string, number>).cache_read_input_tokens ?? 0;\n const totalInput = usage.input_tokens + cacheCreation + cacheRead;\n return {\n input_tokens: totalInput,\n output_tokens: usage.output_tokens,\n total_tokens: totalInput + usage.output_tokens,\n input_token_details: {\n cache_creation: cacheCreation,\n cache_read: cacheRead,\n },\n };\n}\n\nfunction mapBlockToContentBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n block: any,\n index: number\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n): Record<string, any> {\n switch (block.type) {\n case \"text\":\n return { type: \"text\" as const, text: block.text ?? \"\", index };\n case \"thinking\":\n return {\n type: \"reasoning\" as const,\n reasoning: block.thinking ?? \"\",\n index,\n };\n case \"redacted_thinking\":\n return { type: \"non_standard\" as const, value: { ...block }, index };\n case \"tool_use\":\n return {\n type: \"tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n case \"server_tool_use\":\n return {\n type: \"server_tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n default:\n return { type: \"non_standard\" as const, value: { ...block }, index };\n }\n}\n\n/**\n * Map an Anthropic content_block_delta to a content block delta\n * and update the accumulated state. `contentDelta` is undefined when\n * there's nothing new to send.\n */\nfunction applyAnthropicDelta(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n delta: any\n): {\n contentDelta: ContentBlockDelta | undefined;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>;\n} {\n switch (delta.type) {\n case \"text_delta\":\n return {\n contentDelta: { type: \"text-delta\" as const, text: delta.text },\n accumulated: {\n ...accumulated,\n text: (accumulated.text ?? \"\") + delta.text,\n },\n };\n\n case \"thinking_delta\":\n return {\n contentDelta: {\n type: \"reasoning-delta\" as const,\n reasoning: delta.thinking,\n },\n accumulated: {\n ...accumulated,\n reasoning: (accumulated.reasoning ?? \"\") + delta.thinking,\n },\n };\n\n case \"input_json_delta\": {\n // `tool-call-delta` is for tool calls only. Other blocks that stream\n // JSON input, such as `mcp_tool_use`, keep sending `block-delta`.\n if (\n accumulated.type !== \"tool_call_chunk\" &&\n accumulated.type !== \"server_tool_call_chunk\"\n ) {\n const newArgs = (accumulated.args ?? \"\") + delta.partial_json;\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, args: newArgs },\n },\n accumulated: { ...accumulated, args: newArgs },\n };\n }\n if (!delta.partial_json) {\n return { contentDelta: undefined, accumulated };\n }\n return {\n contentDelta: {\n type: \"tool-call-delta\" as const,\n args: delta.partial_json,\n },\n accumulated: {\n ...accumulated,\n args: (accumulated.args ?? \"\") + delta.partial_json,\n },\n };\n }\n\n case \"citations_delta\": {\n const annotations = [...(accumulated.annotations ?? []), delta.citation];\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: accumulated.type,\n annotations,\n },\n },\n accumulated: {\n ...accumulated,\n annotations,\n },\n };\n }\n\n case \"signature_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, signature: delta.signature },\n },\n accumulated: { ...accumulated, signature: delta.signature },\n };\n\n case \"compaction_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: \"non_standard\",\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n },\n accumulated: {\n ...accumulated,\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n };\n\n default:\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, ...delta },\n },\n accumulated,\n };\n }\n}\n\nfunction finalizeBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>\n): ContentBlock {\n if (\n accumulated.type === \"tool_call_chunk\" ||\n accumulated.type === \"server_tool_call_chunk\"\n ) {\n const finalType =\n accumulated.type === \"tool_call_chunk\"\n ? (\"tool_call\" as const)\n : (\"server_tool_call\" as const);\n let parsedArgs: unknown;\n try {\n parsedArgs = JSON.parse(accumulated.args || \"{}\");\n } catch {\n return {\n type: \"invalid_tool_call\" as const,\n id: accumulated.id,\n name: accumulated.name,\n args: accumulated.args,\n error: \"Failed to parse tool call arguments as JSON\",\n } as ContentBlock.Tools.InvalidToolCall;\n }\n return {\n type: finalType,\n id: accumulated.id,\n name: accumulated.name,\n args: parsedArgs,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n }\n\n const { index: _index, ...rest } = accumulated;\n return rest as ContentBlock;\n}\n"],"mappings":";;;;;AAyBA,gBAAuB,uBACrB,QACA,UAAyC,CAAC,GACJ;CACtC,MAAM,oBAAoB,QAAQ,eAAe;CAGjD,MAAM,oCAAoB,IAAI,IAI5B;CACF,IAAI;CACJ,IAAI,mBAA4C,CAAC;CACjD,IAAI,aAA4B;CAEhC,WAAW,MAAM,QAAQ,QACvB,QAAQ,KAAK,MAAb;EAEE,KAAK,iBAAiB;GACpB,MAAM,EAAE,OAAO,IAAI,UAAU,KAAK;GAClC,IAAI,SAAS,mBACX,gBAAgB,mBAAmB,KAAK;GAE1C,MAAM;IACJ,OAAO;IACP;IACA,GAAI,gBAAgB,EAAE,OAAO,cAAc,IAAI,CAAC;GAClD;GACA,MAAM;IACJ,OAAO;IACP,UAAU;IACV,MAAM;IACN,SAAS;KAAE;KAAO;IAAG;GACvB;GACA;EACF;EAEA,KAAK;GACH,aAAa,KAAK,MAAM;GAExB,IACE,WAAW,QACX,UAAU,KAAK,SACf,OAAO,KAAK,MAAM,SAAS,UAE3B,mBAAmB,EAAE,OAAO,EAAE,MAAM,KAAK,MAAM,KAAK,EAAE;GAExD,IAAI,qBAAqB,KAAK,OAAO;IACnC,IAAI,CAAC,eACH,gBAAgB;KACd,cAAc;KACd,eAAe,KAAK,MAAM;KAC1B,cAAc,KAAK,MAAM;IAC3B;SAEA,gBAAgB;KACd,GAAG;KACH,eACE,cAAc,gBAAgB,KAAK,MAAM;KAC3C,cACE,cAAc,eACd,cAAc,gBACd,KAAK,MAAM;IACf;IAEF,MAAM;KAAE,OAAO;KAAkB,OAAO;IAAc;GACxD;GACA,IACE,wBAAwB,KAAK,SAC7B,KAAK,MAAM,oBAEX,MAAM;IACJ,OAAO;IACP,UAAU;IACV,MAAM;IACN,S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ChB,MAAM,YAAY;GAClB,MAAM;EAER;CACF;CAEA,MAAM,EAAE,OAAO,QAAQ,GAAG,SAAS;CACnC,OAAO;AACT"}
@@ -81,7 +81,7 @@ async function* convertAnthropicStream(source, options = {}) {
81
81
  if (!acc) break;
82
82
  const { contentDelta, accumulated } = applyAnthropicDelta(acc, delta);
83
83
  blockAccumulators.set(index, accumulated);
84
- yield {
84
+ if (contentDelta) yield {
85
85
  event: "content-block-delta",
86
86
  index,
87
87
  delta: contentDelta
@@ -171,7 +171,8 @@ function mapBlockToContentBlock(block, index) {
171
171
  }
172
172
  /**
173
173
  * Map an Anthropic content_block_delta to a content block delta
174
- * and update the accumulated state.
174
+ * and update the accumulated state. `contentDelta` is undefined when
175
+ * there's nothing new to send.
175
176
  */
176
177
  function applyAnthropicDelta(accumulated, delta) {
177
178
  switch (delta.type) {
@@ -195,22 +196,37 @@ function applyAnthropicDelta(accumulated, delta) {
195
196
  reasoning: (accumulated.reasoning ?? "") + delta.thinking
196
197
  }
197
198
  };
198
- case "input_json_delta": {
199
- const newArgs = (accumulated.args ?? "") + delta.partial_json;
200
- return {
201
- contentDelta: {
202
- type: "block-delta",
203
- fields: {
204
- type: accumulated.type,
199
+ case "input_json_delta":
200
+ if (accumulated.type !== "tool_call_chunk" && accumulated.type !== "server_tool_call_chunk") {
201
+ const newArgs = (accumulated.args ?? "") + delta.partial_json;
202
+ return {
203
+ contentDelta: {
204
+ type: "block-delta",
205
+ fields: {
206
+ type: accumulated.type,
207
+ args: newArgs
208
+ }
209
+ },
210
+ accumulated: {
211
+ ...accumulated,
205
212
  args: newArgs
206
213
  }
214
+ };
215
+ }
216
+ if (!delta.partial_json) return {
217
+ contentDelta: void 0,
218
+ accumulated
219
+ };
220
+ return {
221
+ contentDelta: {
222
+ type: "tool-call-delta",
223
+ args: delta.partial_json
207
224
  },
208
225
  accumulated: {
209
226
  ...accumulated,
210
- args: newArgs
227
+ args: (accumulated.args ?? "") + delta.partial_json
211
228
  }
212
229
  };
213
- }
214
230
  case "citations_delta": {
215
231
  const annotations = [...accumulated.annotations ?? [], delta.citation];
216
232
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"stream_events.js","names":[],"sources":["../../src/utils/stream_events.ts"],"sourcesContent":["/**\n * Converts a raw Anthropic SSE event stream into LangChain ChatModelStreamEvents.\n *\n * @module\n */\n\nimport type Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n ChatModelStreamEvent,\n ContentBlockDelta,\n FinishReason,\n} from \"@langchain/core/language_models/event\";\nimport type { ContentBlock, UsageMetadata } from \"@langchain/core/messages\";\nimport type { AnthropicMessageStreamEvent } from \"../types.js\";\n\n// ─── Public API ─────────────────────────────────────────────────\n\nexport interface ConvertAnthropicStreamOptions {\n streamUsage?: boolean;\n}\n\n/**\n * Convert an async iterable of raw Anthropic stream events into\n * LangChain `ChatModelStreamEvent`s with typed deltas.\n */\nexport async function* convertAnthropicStream(\n source: AsyncIterable<AnthropicMessageStreamEvent>,\n options: ConvertAnthropicStreamOptions = {}\n): AsyncGenerator<ChatModelStreamEvent> {\n const shouldStreamUsage = options.streamUsage ?? true;\n\n // Track accumulated state per content block (for finalization)\n const blockAccumulators = new Map<\n number,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n Record<string, any>\n >();\n let usageSnapshot: UsageMetadata | undefined;\n let responseMetadata: Record<string, unknown> = {};\n let stopReason: string | null = null;\n\n for await (const data of source) {\n switch (data.type) {\n // ── Message lifecycle ──────────────────────────────────\n case \"message_start\": {\n const { usage, id, model } = data.message;\n if (usage && shouldStreamUsage) {\n usageSnapshot = buildUsageSnapshot(usage);\n }\n yield {\n event: \"message-start\" as const,\n id,\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n };\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"message_start\",\n payload: { model, id },\n };\n break;\n }\n\n case \"message_delta\": {\n stopReason = data.delta.stop_reason;\n // Some gateway providers add additional usage values to the final usage object\n if (\n \"usage\" in data &&\n \"cost\" in data.usage &&\n typeof data.usage.cost === \"number\"\n ) {\n responseMetadata = { usage: { cost: data.usage.cost } };\n }\n if (shouldStreamUsage && data.usage) {\n if (!usageSnapshot) {\n usageSnapshot = {\n input_tokens: 0,\n output_tokens: data.usage.output_tokens,\n total_tokens: data.usage.output_tokens,\n };\n } else {\n usageSnapshot = {\n ...usageSnapshot,\n output_tokens:\n usageSnapshot.output_tokens + data.usage.output_tokens,\n total_tokens:\n usageSnapshot.input_tokens +\n usageSnapshot.output_tokens +\n data.usage.output_tokens,\n };\n }\n yield { event: \"usage\" as const, usage: usageSnapshot };\n }\n if (\n \"context_management\" in data.delta &&\n data.delta.context_management\n ) {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"context_management\",\n payload: data.delta.context_management,\n };\n }\n break;\n }\n\n case \"message_stop\": {\n yield {\n event: \"message-finish\" as const,\n reason: mapStopReason(stopReason),\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n metadata: { model_provider: \"anthropic\" },\n responseMetadata,\n };\n break;\n }\n\n // ── Content block lifecycle ───────────────────────────\n case \"content_block_start\": {\n const { index, content_block } = data;\n const mapped = mapBlockToContentBlock(content_block, index);\n blockAccumulators.set(index, { ...mapped });\n yield {\n event: \"content-block-start\" as const,\n index,\n content: mapped,\n };\n break;\n }\n\n case \"content_block_delta\": {\n const { index, delta } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const { contentDelta, accumulated } = applyAnthropicDelta(acc, delta);\n blockAccumulators.set(index, accumulated);\n\n yield {\n event: \"content-block-delta\" as const,\n index,\n delta: contentDelta,\n };\n break;\n }\n\n case \"content_block_stop\": {\n const { index } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const finalized = finalizeBlock(acc);\n yield {\n event: \"content-block-finish\" as const,\n index,\n content: finalized,\n };\n blockAccumulators.delete(index);\n break;\n }\n\n // ── Unhandled → provider passthrough ───────────────────\n default: {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: data.type,\n payload: data,\n };\n break;\n }\n }\n }\n}\n\n// ─── Internal helpers ───────────────────────────────────────────\n\nfunction mapStopReason(stopReason: string | null | undefined): FinishReason {\n switch (stopReason) {\n case \"end_turn\":\n case \"stop_sequence\":\n return \"stop\";\n case \"tool_use\":\n return \"tool_use\";\n case \"max_tokens\":\n return \"length\";\n default:\n return \"stop\";\n }\n}\n\nfunction buildUsageSnapshot(\n usage: Anthropic.Messages.Usage | Record<string, number>\n): UsageMetadata {\n const cacheCreation =\n (usage as Record<string, number>).cache_creation_input_tokens ?? 0;\n const cacheRead =\n (usage as Record<string, number>).cache_read_input_tokens ?? 0;\n const totalInput = usage.input_tokens + cacheCreation + cacheRead;\n return {\n input_tokens: totalInput,\n output_tokens: usage.output_tokens,\n total_tokens: totalInput + usage.output_tokens,\n input_token_details: {\n cache_creation: cacheCreation,\n cache_read: cacheRead,\n },\n };\n}\n\nfunction mapBlockToContentBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n block: any,\n index: number\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n): Record<string, any> {\n switch (block.type) {\n case \"text\":\n return { type: \"text\" as const, text: block.text ?? \"\", index };\n case \"thinking\":\n return {\n type: \"reasoning\" as const,\n reasoning: block.thinking ?? \"\",\n index,\n };\n case \"redacted_thinking\":\n return { type: \"non_standard\" as const, value: { ...block }, index };\n case \"tool_use\":\n return {\n type: \"tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n case \"server_tool_use\":\n return {\n type: \"server_tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n default:\n return { type: \"non_standard\" as const, value: { ...block }, index };\n }\n}\n\n/**\n * Map an Anthropic content_block_delta to a content block delta\n * and update the accumulated state.\n */\nfunction applyAnthropicDelta(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n delta: any\n): {\n contentDelta: ContentBlockDelta;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>;\n} {\n switch (delta.type) {\n case \"text_delta\":\n return {\n contentDelta: { type: \"text-delta\" as const, text: delta.text },\n accumulated: {\n ...accumulated,\n text: (accumulated.text ?? \"\") + delta.text,\n },\n };\n\n case \"thinking_delta\":\n return {\n contentDelta: {\n type: \"reasoning-delta\" as const,\n reasoning: delta.thinking,\n },\n accumulated: {\n ...accumulated,\n reasoning: (accumulated.reasoning ?? \"\") + delta.thinking,\n },\n };\n\n case \"input_json_delta\": {\n const newArgs = (accumulated.args ?? \"\") + delta.partial_json;\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, args: newArgs },\n },\n accumulated: { ...accumulated, args: newArgs },\n };\n }\n\n case \"citations_delta\": {\n const annotations = [...(accumulated.annotations ?? []), delta.citation];\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: accumulated.type,\n annotations,\n },\n },\n accumulated: {\n ...accumulated,\n annotations,\n },\n };\n }\n\n case \"signature_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, signature: delta.signature },\n },\n accumulated: { ...accumulated, signature: delta.signature },\n };\n\n case \"compaction_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: \"non_standard\",\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n },\n accumulated: {\n ...accumulated,\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n };\n\n default:\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, ...delta },\n },\n accumulated,\n };\n }\n}\n\nfunction finalizeBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>\n): ContentBlock {\n if (\n accumulated.type === \"tool_call_chunk\" ||\n accumulated.type === \"server_tool_call_chunk\"\n ) {\n const finalType =\n accumulated.type === \"tool_call_chunk\"\n ? (\"tool_call\" as const)\n : (\"server_tool_call\" as const);\n let parsedArgs: unknown;\n try {\n parsedArgs = JSON.parse(accumulated.args || \"{}\");\n } catch {\n return {\n type: \"invalid_tool_call\" as const,\n id: accumulated.id,\n name: accumulated.name,\n args: accumulated.args,\n error: \"Failed to parse tool call arguments as JSON\",\n } as ContentBlock.Tools.InvalidToolCall;\n }\n return {\n type: finalType,\n id: accumulated.id,\n name: accumulated.name,\n args: parsedArgs,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n }\n\n const { index: _index, ...rest } = accumulated;\n return rest as ContentBlock;\n}\n"],"mappings":";;;;;AAyBA,gBAAuB,uBACrB,QACA,UAAyC,CAAC,GACJ;CACtC,MAAM,oBAAoB,QAAQ,eAAe;CAGjD,MAAM,oCAAoB,IAAI,IAI5B;CACF,IAAI;CACJ,IAAI,mBAA4C,CAAC;CACjD,IAAI,aAA4B;CAEhC,WAAW,MAAM,QAAQ,QACvB,QAAQ,KAAK,MAAb;EAEE,KAAK,iBAAiB;GACpB,MAAM,EAAE,OAAO,IAAI,UAAU,KAAK;GAClC,IAAI,SAAS,mBACX,gBAAgB,mBAAmB,KAAK;GAE1C,MAAM;IACJ,OAAO;IACP;IACA,GAAI,gBAAgB,EAAE,OAAO,cAAc,IAAI,CAAC;GAClD;GACA,MAAM;IACJ,OAAO;IACP,UAAU;IACV,MAAM;IACN,SAAS;KAAE;KAAO;IAAG;GACvB;GACA;EACF;EAEA,KAAK;GACH,aAAa,KAAK,MAAM;GAExB,IACE,WAAW,QACX,UAAU,KAAK,SACf,OAAO,KAAK,MAAM,SAAS,UAE3B,mBAAmB,EAAE,OAAO,EAAE,MAAM,KAAK,MAAM,KAAK,EAAE;GAExD,IAAI,qBAAqB,KAAK,OAAO;IACnC,IAAI,CAAC,eACH,gBAAgB;KACd,cAAc;KACd,eAAe,KAAK,MAAM;KAC1B,cAAc,KAAK,MAAM;IAC3B;SAEA,gBAAgB;KACd,GAAG;KACH,eACE,cAAc,gBAAgB,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ACF;CAEA,MAAM,EAAE,OAAO,QAAQ,GAAG,SAAS;CACnC,OAAO;AACT"}
1
+ {"version":3,"file":"stream_events.js","names":[],"sources":["../../src/utils/stream_events.ts"],"sourcesContent":["/**\n * Converts a raw Anthropic SSE event stream into LangChain ChatModelStreamEvents.\n *\n * @module\n */\n\nimport type Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n ChatModelStreamEvent,\n ContentBlockDelta,\n FinishReason,\n} from \"@langchain/core/language_models/event\";\nimport type { ContentBlock, UsageMetadata } from \"@langchain/core/messages\";\nimport type { AnthropicMessageStreamEvent } from \"../types.js\";\n\n// ─── Public API ─────────────────────────────────────────────────\n\nexport interface ConvertAnthropicStreamOptions {\n streamUsage?: boolean;\n}\n\n/**\n * Convert an async iterable of raw Anthropic stream events into\n * LangChain `ChatModelStreamEvent`s with typed deltas.\n */\nexport async function* convertAnthropicStream(\n source: AsyncIterable<AnthropicMessageStreamEvent>,\n options: ConvertAnthropicStreamOptions = {}\n): AsyncGenerator<ChatModelStreamEvent> {\n const shouldStreamUsage = options.streamUsage ?? true;\n\n // Track accumulated state per content block (for finalization)\n const blockAccumulators = new Map<\n number,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n Record<string, any>\n >();\n let usageSnapshot: UsageMetadata | undefined;\n let responseMetadata: Record<string, unknown> = {};\n let stopReason: string | null = null;\n\n for await (const data of source) {\n switch (data.type) {\n // ── Message lifecycle ──────────────────────────────────\n case \"message_start\": {\n const { usage, id, model } = data.message;\n if (usage && shouldStreamUsage) {\n usageSnapshot = buildUsageSnapshot(usage);\n }\n yield {\n event: \"message-start\" as const,\n id,\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n };\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"message_start\",\n payload: { model, id },\n };\n break;\n }\n\n case \"message_delta\": {\n stopReason = data.delta.stop_reason;\n // Some gateway providers add additional usage values to the final usage object\n if (\n \"usage\" in data &&\n \"cost\" in data.usage &&\n typeof data.usage.cost === \"number\"\n ) {\n responseMetadata = { usage: { cost: data.usage.cost } };\n }\n if (shouldStreamUsage && data.usage) {\n if (!usageSnapshot) {\n usageSnapshot = {\n input_tokens: 0,\n output_tokens: data.usage.output_tokens,\n total_tokens: data.usage.output_tokens,\n };\n } else {\n usageSnapshot = {\n ...usageSnapshot,\n output_tokens:\n usageSnapshot.output_tokens + data.usage.output_tokens,\n total_tokens:\n usageSnapshot.input_tokens +\n usageSnapshot.output_tokens +\n data.usage.output_tokens,\n };\n }\n yield { event: \"usage\" as const, usage: usageSnapshot };\n }\n if (\n \"context_management\" in data.delta &&\n data.delta.context_management\n ) {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"context_management\",\n payload: data.delta.context_management,\n };\n }\n break;\n }\n\n case \"message_stop\": {\n yield {\n event: \"message-finish\" as const,\n reason: mapStopReason(stopReason),\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n metadata: { model_provider: \"anthropic\" },\n responseMetadata,\n };\n break;\n }\n\n // ── Content block lifecycle ───────────────────────────\n case \"content_block_start\": {\n const { index, content_block } = data;\n const mapped = mapBlockToContentBlock(content_block, index);\n blockAccumulators.set(index, { ...mapped });\n yield {\n event: \"content-block-start\" as const,\n index,\n content: mapped,\n };\n break;\n }\n\n case \"content_block_delta\": {\n const { index, delta } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const { contentDelta, accumulated } = applyAnthropicDelta(acc, delta);\n blockAccumulators.set(index, accumulated);\n\n if (contentDelta) {\n yield {\n event: \"content-block-delta\" as const,\n index,\n delta: contentDelta,\n };\n }\n break;\n }\n\n case \"content_block_stop\": {\n const { index } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const finalized = finalizeBlock(acc);\n yield {\n event: \"content-block-finish\" as const,\n index,\n content: finalized,\n };\n blockAccumulators.delete(index);\n break;\n }\n\n // ── Unhandled → provider passthrough ───────────────────\n default: {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: data.type,\n payload: data,\n };\n break;\n }\n }\n }\n}\n\n// ─── Internal helpers ───────────────────────────────────────────\n\nfunction mapStopReason(stopReason: string | null | undefined): FinishReason {\n switch (stopReason) {\n case \"end_turn\":\n case \"stop_sequence\":\n return \"stop\";\n case \"tool_use\":\n return \"tool_use\";\n case \"max_tokens\":\n return \"length\";\n default:\n return \"stop\";\n }\n}\n\nfunction buildUsageSnapshot(\n usage: Anthropic.Messages.Usage | Record<string, number>\n): UsageMetadata {\n const cacheCreation =\n (usage as Record<string, number>).cache_creation_input_tokens ?? 0;\n const cacheRead =\n (usage as Record<string, number>).cache_read_input_tokens ?? 0;\n const totalInput = usage.input_tokens + cacheCreation + cacheRead;\n return {\n input_tokens: totalInput,\n output_tokens: usage.output_tokens,\n total_tokens: totalInput + usage.output_tokens,\n input_token_details: {\n cache_creation: cacheCreation,\n cache_read: cacheRead,\n },\n };\n}\n\nfunction mapBlockToContentBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n block: any,\n index: number\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n): Record<string, any> {\n switch (block.type) {\n case \"text\":\n return { type: \"text\" as const, text: block.text ?? \"\", index };\n case \"thinking\":\n return {\n type: \"reasoning\" as const,\n reasoning: block.thinking ?? \"\",\n index,\n };\n case \"redacted_thinking\":\n return { type: \"non_standard\" as const, value: { ...block }, index };\n case \"tool_use\":\n return {\n type: \"tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n case \"server_tool_use\":\n return {\n type: \"server_tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n default:\n return { type: \"non_standard\" as const, value: { ...block }, index };\n }\n}\n\n/**\n * Map an Anthropic content_block_delta to a content block delta\n * and update the accumulated state. `contentDelta` is undefined when\n * there's nothing new to send.\n */\nfunction applyAnthropicDelta(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n delta: any\n): {\n contentDelta: ContentBlockDelta | undefined;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>;\n} {\n switch (delta.type) {\n case \"text_delta\":\n return {\n contentDelta: { type: \"text-delta\" as const, text: delta.text },\n accumulated: {\n ...accumulated,\n text: (accumulated.text ?? \"\") + delta.text,\n },\n };\n\n case \"thinking_delta\":\n return {\n contentDelta: {\n type: \"reasoning-delta\" as const,\n reasoning: delta.thinking,\n },\n accumulated: {\n ...accumulated,\n reasoning: (accumulated.reasoning ?? \"\") + delta.thinking,\n },\n };\n\n case \"input_json_delta\": {\n // `tool-call-delta` is for tool calls only. Other blocks that stream\n // JSON input, such as `mcp_tool_use`, keep sending `block-delta`.\n if (\n accumulated.type !== \"tool_call_chunk\" &&\n accumulated.type !== \"server_tool_call_chunk\"\n ) {\n const newArgs = (accumulated.args ?? \"\") + delta.partial_json;\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, args: newArgs },\n },\n accumulated: { ...accumulated, args: newArgs },\n };\n }\n if (!delta.partial_json) {\n return { contentDelta: undefined, accumulated };\n }\n return {\n contentDelta: {\n type: \"tool-call-delta\" as const,\n args: delta.partial_json,\n },\n accumulated: {\n ...accumulated,\n args: (accumulated.args ?? \"\") + delta.partial_json,\n },\n };\n }\n\n case \"citations_delta\": {\n const annotations = [...(accumulated.annotations ?? []), delta.citation];\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: accumulated.type,\n annotations,\n },\n },\n accumulated: {\n ...accumulated,\n annotations,\n },\n };\n }\n\n case \"signature_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, signature: delta.signature },\n },\n accumulated: { ...accumulated, signature: delta.signature },\n };\n\n case \"compaction_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: \"non_standard\",\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n },\n accumulated: {\n ...accumulated,\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n };\n\n default:\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, ...delta },\n },\n accumulated,\n };\n }\n}\n\nfunction finalizeBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>\n): ContentBlock {\n if (\n accumulated.type === \"tool_call_chunk\" ||\n accumulated.type === \"server_tool_call_chunk\"\n ) {\n const finalType =\n accumulated.type === \"tool_call_chunk\"\n ? (\"tool_call\" as const)\n : (\"server_tool_call\" as const);\n let parsedArgs: unknown;\n try {\n parsedArgs = JSON.parse(accumulated.args || \"{}\");\n } catch {\n return {\n type: \"invalid_tool_call\" as const,\n id: accumulated.id,\n name: accumulated.name,\n args: accumulated.args,\n error: \"Failed to parse tool call arguments as JSON\",\n } as ContentBlock.Tools.InvalidToolCall;\n }\n return {\n type: finalType,\n id: accumulated.id,\n name: accumulated.name,\n args: parsedArgs,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n }\n\n const { index: _index, ...rest } = accumulated;\n return rest as ContentBlock;\n}\n"],"mappings":";;;;;AAyBA,gBAAuB,uBACrB,QACA,UAAyC,CAAC,GACJ;CACtC,MAAM,oBAAoB,QAAQ,eAAe;CAGjD,MAAM,oCAAoB,IAAI,IAI5B;CACF,IAAI;CACJ,IAAI,mBAA4C,CAAC;CACjD,IAAI,aAA4B;CAEhC,WAAW,MAAM,QAAQ,QACvB,QAAQ,KAAK,MAAb;EAEE,KAAK,iBAAiB;GACpB,MAAM,EAAE,OAAO,IAAI,UAAU,KAAK;GAClC,IAAI,SAAS,mBACX,gBAAgB,mBAAmB,KAAK;GAE1C,MAAM;IACJ,OAAO;IACP;IACA,GAAI,gBAAgB,EAAE,OAAO,cAAc,IAAI,CAAC;GAClD;GACA,MAAM;IACJ,OAAO;IACP,UAAU;IACV,MAAM;IACN,SAAS;KAAE;KAAO;IAAG;GACvB;GACA;EACF;EAEA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