@adminforth/completion-adapter-anthropic-messages 1.2.2 → 1.2.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.ts +6 -40
- package/dist/langchain.d.ts +4 -10
- package/dist/langchain.js +26 -39
- package/langchain.ts +42 -55
- package/package.json +3 -1
package/dist/index.d.ts
CHANGED
|
@@ -17,51 +17,17 @@ declare class CompletionAdapterAnthropicMessages implements CompletionAdapter {
|
|
|
17
17
|
middleware: {
|
|
18
18
|
name: string;
|
|
19
19
|
wrapModelCall(request: {
|
|
20
|
-
systemMessage
|
|
20
|
+
systemMessage?: {
|
|
21
21
|
content?: unknown;
|
|
22
|
-
text?: string;
|
|
23
|
-
type?: string;
|
|
24
|
-
getType?: () => string;
|
|
25
|
-
_getType?: () => string;
|
|
26
|
-
} & {
|
|
27
|
-
concat: (content: string) => {
|
|
28
|
-
content?: unknown;
|
|
29
|
-
text?: string;
|
|
30
|
-
type?: string;
|
|
31
|
-
getType?: () => string;
|
|
32
|
-
_getType?: () => string;
|
|
33
|
-
};
|
|
34
22
|
};
|
|
35
|
-
messages
|
|
36
|
-
|
|
37
|
-
text?: string;
|
|
38
|
-
type?: string;
|
|
39
|
-
getType?: () => string;
|
|
40
|
-
_getType?: () => string;
|
|
41
|
-
}[];
|
|
23
|
+
messages?: unknown[];
|
|
24
|
+
modelSettings?: Record<string, unknown>;
|
|
42
25
|
}, handler: (request: {
|
|
43
|
-
systemMessage
|
|
26
|
+
systemMessage?: {
|
|
44
27
|
content?: unknown;
|
|
45
|
-
text?: string;
|
|
46
|
-
type?: string;
|
|
47
|
-
getType?: () => string;
|
|
48
|
-
_getType?: () => string;
|
|
49
|
-
} & {
|
|
50
|
-
concat: (content: string) => {
|
|
51
|
-
content?: unknown;
|
|
52
|
-
text?: string;
|
|
53
|
-
type?: string;
|
|
54
|
-
getType?: () => string;
|
|
55
|
-
_getType?: () => string;
|
|
56
|
-
};
|
|
57
28
|
};
|
|
58
|
-
messages
|
|
59
|
-
|
|
60
|
-
text?: string;
|
|
61
|
-
type?: string;
|
|
62
|
-
getType?: () => string;
|
|
63
|
-
_getType?: () => string;
|
|
64
|
-
}[];
|
|
29
|
+
messages?: unknown[];
|
|
30
|
+
modelSettings?: Record<string, unknown>;
|
|
65
31
|
}) => unknown): Promise<unknown>;
|
|
66
32
|
}[];
|
|
67
33
|
};
|
package/dist/langchain.d.ts
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
import { ChatAnthropic } from "@langchain/anthropic";
|
|
2
2
|
import type { AdapterOptions } from "./types.js";
|
|
3
3
|
export type AgentModelPurpose = "primary" | "summary";
|
|
4
|
-
type LangChainMessageLike = {
|
|
5
|
-
content?: unknown;
|
|
6
|
-
text?: string;
|
|
7
|
-
type?: string;
|
|
8
|
-
getType?: () => string;
|
|
9
|
-
_getType?: () => string;
|
|
10
|
-
};
|
|
11
4
|
type LangChainModelCallRequest = {
|
|
12
|
-
systemMessage
|
|
13
|
-
|
|
5
|
+
systemMessage?: {
|
|
6
|
+
content?: unknown;
|
|
14
7
|
};
|
|
15
|
-
messages
|
|
8
|
+
messages?: unknown[];
|
|
9
|
+
modelSettings?: Record<string, unknown>;
|
|
16
10
|
};
|
|
17
11
|
export declare function createLangChainAgentSpec(params: {
|
|
18
12
|
options: AdapterOptions;
|
package/dist/langchain.js
CHANGED
|
@@ -12,50 +12,37 @@ import { getApiKey, mapReasoningToThinking, } from "./anthropic.js";
|
|
|
12
12
|
function getAgentReasoningEffort(purpose) {
|
|
13
13
|
return purpose === "summary" ? "minimal" : "low";
|
|
14
14
|
}
|
|
15
|
-
function
|
|
16
|
-
var _a
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (text)
|
|
23
|
-
return
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
.map((block) => {
|
|
30
|
-
var _a;
|
|
31
|
-
if (typeof block === "string")
|
|
32
|
-
return block;
|
|
33
|
-
if (block && typeof block === "object" && "text" in block) {
|
|
34
|
-
return String((_a = block.text) !== null && _a !== void 0 ? _a : "");
|
|
35
|
-
}
|
|
36
|
-
return "";
|
|
37
|
-
})
|
|
38
|
-
.join("");
|
|
15
|
+
function markSystemPromptCacheBreakpoint(request) {
|
|
16
|
+
var _a;
|
|
17
|
+
const content = (_a = request.systemMessage) === null || _a === void 0 ? void 0 : _a.content;
|
|
18
|
+
if (!Array.isArray(content) || content.length === 0) {
|
|
19
|
+
return request;
|
|
20
|
+
}
|
|
21
|
+
const blocks = content;
|
|
22
|
+
if (blocks[0].type !== "text" || blocks[0].cache_control) {
|
|
23
|
+
return request;
|
|
24
|
+
}
|
|
25
|
+
return Object.assign(Object.assign({}, request), { systemMessage: Object.assign(Object.assign({}, request.systemMessage), { content: [
|
|
26
|
+
Object.assign(Object.assign({}, blocks[0]), { cache_control: { type: "ephemeral" } }),
|
|
27
|
+
...blocks.slice(1),
|
|
28
|
+
] }) });
|
|
39
29
|
}
|
|
40
|
-
function
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
.filter((text) => text && text !== existingSystemText)
|
|
46
|
-
.join("\n\n");
|
|
47
|
-
if (!extraSystemText) {
|
|
48
|
-
return Object.assign(Object.assign({}, request), { messages: request.messages.filter((message) => !isSystemMessage(message)) });
|
|
30
|
+
function markHistoryCacheBreakpoint(request) {
|
|
31
|
+
var _a, _b;
|
|
32
|
+
// Nothing to anchor a breakpoint to, and never override an explicit setting.
|
|
33
|
+
if (!((_a = request.messages) === null || _a === void 0 ? void 0 : _a.length) || ((_b = request.modelSettings) === null || _b === void 0 ? void 0 : _b.cache_control)) {
|
|
34
|
+
return request;
|
|
49
35
|
}
|
|
50
|
-
|
|
51
|
-
|
|
36
|
+
return Object.assign(Object.assign({}, request), { modelSettings: Object.assign(Object.assign({}, request.modelSettings), {
|
|
37
|
+
// No `ttl` — Anthropic defaults to 5 minutes, refreshed on every cache hit.
|
|
38
|
+
cache_control: { type: "ephemeral" } }) });
|
|
52
39
|
}
|
|
53
|
-
function
|
|
40
|
+
function createAnthropicPromptCachingMiddleware() {
|
|
54
41
|
return {
|
|
55
|
-
name: "
|
|
42
|
+
name: "AnthropicPromptCachingMiddleware",
|
|
56
43
|
wrapModelCall(request, handler) {
|
|
57
44
|
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
-
return handler(
|
|
45
|
+
return handler(markHistoryCacheBreakpoint(markSystemPromptCacheBreakpoint(request)));
|
|
59
46
|
});
|
|
60
47
|
},
|
|
61
48
|
};
|
|
@@ -67,6 +54,6 @@ export function createLangChainAgentSpec(params) {
|
|
|
67
54
|
delete extraRequestBodyParameters.thinking;
|
|
68
55
|
return {
|
|
69
56
|
model: new ChatAnthropic(Object.assign(Object.assign({ model: params.options.model || "claude-sonnet-4-5-20250929", apiKey: getApiKey(params.options), maxTokens: params.maxTokens }, (thinking ? { thinking } : {})), { invocationKwargs: extraRequestBodyParameters })),
|
|
70
|
-
middleware: [
|
|
57
|
+
middleware: [createAnthropicPromptCachingMiddleware()],
|
|
71
58
|
};
|
|
72
59
|
}
|
package/langchain.ts
CHANGED
|
@@ -8,19 +8,12 @@ import {
|
|
|
8
8
|
|
|
9
9
|
export type AgentModelPurpose = "primary" | "summary";
|
|
10
10
|
|
|
11
|
-
type
|
|
12
|
-
content?: unknown;
|
|
13
|
-
text?: string;
|
|
14
|
-
type?: string;
|
|
15
|
-
getType?: () => string;
|
|
16
|
-
_getType?: () => string;
|
|
17
|
-
};
|
|
11
|
+
type LangChainContentBlock = Record<string, unknown> & { type?: string };
|
|
18
12
|
|
|
19
13
|
type LangChainModelCallRequest = {
|
|
20
|
-
systemMessage
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
messages: LangChainMessageLike[];
|
|
14
|
+
systemMessage?: { content?: unknown };
|
|
15
|
+
messages?: unknown[];
|
|
16
|
+
modelSettings?: Record<string, unknown>;
|
|
24
17
|
};
|
|
25
18
|
|
|
26
19
|
function getAgentReasoningEffort(
|
|
@@ -29,67 +22,61 @@ function getAgentReasoningEffort(
|
|
|
29
22
|
return purpose === "summary" ? "minimal" : "low";
|
|
30
23
|
}
|
|
31
24
|
|
|
32
|
-
function
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
)
|
|
38
|
-
|
|
25
|
+
function markSystemPromptCacheBreakpoint<T extends LangChainModelCallRequest>(
|
|
26
|
+
request: T,
|
|
27
|
+
): T {
|
|
28
|
+
const content = request.systemMessage?.content;
|
|
29
|
+
|
|
30
|
+
if (!Array.isArray(content) || content.length === 0) {
|
|
31
|
+
return request;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const blocks = content as LangChainContentBlock[];
|
|
39
35
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (!Array.isArray(content)) return "";
|
|
36
|
+
if (blocks[0].type !== "text" || blocks[0].cache_control) {
|
|
37
|
+
return request;
|
|
38
|
+
}
|
|
44
39
|
|
|
45
|
-
return
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
40
|
+
return {
|
|
41
|
+
...request,
|
|
42
|
+
systemMessage: {
|
|
43
|
+
...request.systemMessage,
|
|
44
|
+
content: [
|
|
45
|
+
{ ...blocks[0], cache_control: { type: "ephemeral" } },
|
|
46
|
+
...blocks.slice(1),
|
|
47
|
+
],
|
|
48
|
+
},
|
|
49
|
+
};
|
|
54
50
|
}
|
|
55
51
|
|
|
56
|
-
function
|
|
52
|
+
function markHistoryCacheBreakpoint<T extends LangChainModelCallRequest>(
|
|
57
53
|
request: T,
|
|
58
54
|
): T {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
request
|
|
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
|
-
};
|
|
55
|
+
// Nothing to anchor a breakpoint to, and never override an explicit setting.
|
|
56
|
+
if (!request.messages?.length || request.modelSettings?.cache_control) {
|
|
57
|
+
return request;
|
|
74
58
|
}
|
|
75
59
|
|
|
76
|
-
const systemMessage = request.systemMessage.concat(extraSystemText);
|
|
77
|
-
|
|
78
60
|
return {
|
|
79
61
|
...request,
|
|
80
|
-
|
|
81
|
-
|
|
62
|
+
modelSettings: {
|
|
63
|
+
...request.modelSettings,
|
|
64
|
+
// No `ttl` — Anthropic defaults to 5 minutes, refreshed on every cache hit.
|
|
65
|
+
cache_control: { type: "ephemeral" },
|
|
66
|
+
},
|
|
82
67
|
};
|
|
83
68
|
}
|
|
84
69
|
|
|
85
|
-
function
|
|
70
|
+
function createAnthropicPromptCachingMiddleware() {
|
|
86
71
|
return {
|
|
87
|
-
name: "
|
|
72
|
+
name: "AnthropicPromptCachingMiddleware",
|
|
88
73
|
async wrapModelCall(
|
|
89
74
|
request: LangChainModelCallRequest,
|
|
90
75
|
handler: (request: LangChainModelCallRequest) => unknown,
|
|
91
76
|
) {
|
|
92
|
-
return handler(
|
|
77
|
+
return handler(
|
|
78
|
+
markHistoryCacheBreakpoint(markSystemPromptCacheBreakpoint(request)),
|
|
79
|
+
);
|
|
93
80
|
},
|
|
94
81
|
};
|
|
95
82
|
}
|
|
@@ -121,6 +108,6 @@ export function createLangChainAgentSpec(params: {
|
|
|
121
108
|
...(thinking ? { thinking } : {}),
|
|
122
109
|
invocationKwargs: extraRequestBodyParameters,
|
|
123
110
|
} as any),
|
|
124
|
-
middleware: [
|
|
111
|
+
middleware: [createAnthropicPromptCachingMiddleware()],
|
|
125
112
|
};
|
|
126
113
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adminforth/completion-adapter-anthropic-messages",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -30,9 +30,11 @@
|
|
|
30
30
|
"@langchain/anthropic": "1.3.26"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
+
"@langchain/core": "^1.2.5",
|
|
33
34
|
"adminforth": "^3.0.1"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
37
|
+
"@langchain/core": "1.2.5",
|
|
36
38
|
"adminforth": "^3.0.1",
|
|
37
39
|
"semantic-release": "^24.2.1",
|
|
38
40
|
"semantic-release-slack-bot": "^4.0.2",
|