@adminforth/completion-adapter-anthropic-messages 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/langchain.ts ADDED
@@ -0,0 +1,126 @@
1
+ import { ChatAnthropic } from "@langchain/anthropic";
2
+ import type { AdapterOptions } from "./types.js";
3
+ import {
4
+ getApiKey,
5
+ mapReasoningToThinking,
6
+ type ReasoningEffort,
7
+ } from "./anthropic.js";
8
+
9
+ export type AgentModelPurpose = "primary" | "summary";
10
+
11
+ type LangChainMessageLike = {
12
+ content?: unknown;
13
+ text?: string;
14
+ type?: string;
15
+ getType?: () => string;
16
+ _getType?: () => string;
17
+ };
18
+
19
+ type LangChainModelCallRequest = {
20
+ systemMessage: LangChainMessageLike & {
21
+ concat: (content: string) => LangChainMessageLike;
22
+ };
23
+ messages: LangChainMessageLike[];
24
+ };
25
+
26
+ function getAgentReasoningEffort(
27
+ purpose: AgentModelPurpose,
28
+ ): ReasoningEffort {
29
+ return purpose === "summary" ? "minimal" : "low";
30
+ }
31
+
32
+ function isSystemMessage(message: LangChainMessageLike): boolean {
33
+ return (
34
+ message._getType?.() === "system" ||
35
+ message.getType?.() === "system" ||
36
+ message.type === "system"
37
+ );
38
+ }
39
+
40
+ function contentToText(content: unknown, text?: string): string {
41
+ if (text) return text;
42
+ if (typeof content === "string") return content;
43
+ if (!Array.isArray(content)) return "";
44
+
45
+ return content
46
+ .map((block) => {
47
+ if (typeof block === "string") return block;
48
+ if (block && typeof block === "object" && "text" in block) {
49
+ return String(block.text ?? "");
50
+ }
51
+ return "";
52
+ })
53
+ .join("");
54
+ }
55
+
56
+ function normalizeAnthropicSystemMessages<T extends LangChainModelCallRequest>(
57
+ request: T,
58
+ ): T {
59
+ const existingSystemText = contentToText(
60
+ request.systemMessage.content,
61
+ request.systemMessage.text,
62
+ );
63
+ const extraSystemText = request.messages
64
+ .filter(isSystemMessage)
65
+ .map((message) => contentToText(message.content, message.text))
66
+ .filter((text) => text && text !== existingSystemText)
67
+ .join("\n\n");
68
+
69
+ if (!extraSystemText) {
70
+ return {
71
+ ...request,
72
+ messages: request.messages.filter((message) => !isSystemMessage(message)),
73
+ };
74
+ }
75
+
76
+ const systemMessage = request.systemMessage.concat(extraSystemText);
77
+
78
+ return {
79
+ ...request,
80
+ systemMessage,
81
+ messages: request.messages.filter((message) => !isSystemMessage(message)),
82
+ };
83
+ }
84
+
85
+ function createAnthropicSystemMessageMiddleware() {
86
+ return {
87
+ name: "AnthropicSystemMessageMiddleware",
88
+ async wrapModelCall(
89
+ request: LangChainModelCallRequest,
90
+ handler: (request: LangChainModelCallRequest) => unknown,
91
+ ) {
92
+ return handler(normalizeAnthropicSystemMessages(request));
93
+ },
94
+ };
95
+ }
96
+
97
+ export function createLangChainAgentSpec(params: {
98
+ options: AdapterOptions;
99
+ maxTokens: number;
100
+ purpose: AgentModelPurpose;
101
+ }) {
102
+ const extraRequestBodyParameters = {
103
+ ...(params.options.extraRequestBodyParameters || {}),
104
+ } as Record<string, unknown> & {
105
+ thinking?: { type: "enabled"; budget_tokens: number };
106
+ };
107
+ const thinking =
108
+ extraRequestBodyParameters.thinking ||
109
+ mapReasoningToThinking(
110
+ getAgentReasoningEffort(params.purpose),
111
+ params.maxTokens,
112
+ );
113
+
114
+ delete extraRequestBodyParameters.thinking;
115
+
116
+ return {
117
+ model: new ChatAnthropic({
118
+ model: params.options.model || "claude-sonnet-4-5-20250929",
119
+ apiKey: getApiKey(params.options),
120
+ maxTokens: params.maxTokens,
121
+ ...(thinking ? { thinking } : {}),
122
+ invocationKwargs: extraRequestBodyParameters,
123
+ } as any),
124
+ middleware: [createAnthropicSystemMessageMiddleware()],
125
+ };
126
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/completion-adapter-anthropic-messages",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -31,9 +31,10 @@
31
31
  "@langchain/anthropic": "1.3.26"
32
32
  },
33
33
  "peerDependencies": {
34
- "adminforth": "^2.24.0"
34
+ "adminforth": "^3.0.1"
35
35
  },
36
36
  "devDependencies": {
37
+ "adminforth": "^3.0.1",
37
38
  "semantic-release": "^24.2.1",
38
39
  "semantic-release-slack-bot": "^4.0.2",
39
40
  "typescript": "^5.9.3"
@@ -0,0 +1,5 @@
1
+ allowBuilds:
2
+ adminforth: true
3
+ better-sqlite3: true
4
+ minimumReleaseAgeExclude:
5
+ - adminforth@3.0.1