@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/anthropic.ts +491 -0
- package/dist/anthropic.d.ts +40 -0
- package/dist/anthropic.js +379 -0
- package/dist/index.d.ts +56 -42
- package/dist/index.js +14 -403
- package/dist/langchain.d.ts +28 -0
- package/dist/langchain.js +72 -0
- package/index.ts +27 -555
- package/langchain.ts +126 -0
- package/package.json +3 -2
- package/pnpm-workspace.yaml +5 -0
package/dist/index.js
CHANGED
|
@@ -7,180 +7,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
14
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
15
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
16
|
-
};
|
|
17
|
-
import { ChatAnthropic } from "@langchain/anthropic";
|
|
18
|
-
import Anthropic from "@anthropic-ai/sdk";
|
|
19
|
-
import { jsonSchemaOutputFormat } from "@anthropic-ai/sdk/helpers/json-schema";
|
|
20
|
-
function getApiKey(options) {
|
|
21
|
-
return options.anthropicApiKey || options.antropicApiKey;
|
|
22
|
-
}
|
|
23
|
-
function getErrorMessage(error) {
|
|
24
|
-
if (error instanceof Error && error.message)
|
|
25
|
-
return error.message;
|
|
26
|
-
return String(error || "Unknown error");
|
|
27
|
-
}
|
|
28
|
-
function normalizeOutputSchema(outputSchema) {
|
|
29
|
-
if (!outputSchema || typeof outputSchema !== "object")
|
|
30
|
-
return undefined;
|
|
31
|
-
const candidate = outputSchema.schema || outputSchema.json_schema || outputSchema;
|
|
32
|
-
if (!candidate || typeof candidate !== "object") {
|
|
33
|
-
return undefined;
|
|
34
|
-
}
|
|
35
|
-
if (candidate.type !== "object") {
|
|
36
|
-
return undefined;
|
|
37
|
-
}
|
|
38
|
-
return candidate;
|
|
39
|
-
}
|
|
40
|
-
function extractOutputText(data) {
|
|
41
|
-
var _a;
|
|
42
|
-
let text = "";
|
|
43
|
-
for (const block of (_a = data.content) !== null && _a !== void 0 ? _a : []) {
|
|
44
|
-
if ((block === null || block === void 0 ? void 0 : block.type) === "text" && typeof block.text === "string") {
|
|
45
|
-
text += block.text;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return text;
|
|
49
|
-
}
|
|
50
|
-
function extractReasoning(data) {
|
|
51
|
-
var _a;
|
|
52
|
-
let reasoning = "";
|
|
53
|
-
for (const block of (_a = data.content) !== null && _a !== void 0 ? _a : []) {
|
|
54
|
-
if ((block === null || block === void 0 ? void 0 : block.type) === "thinking" && typeof block.thinking === "string") {
|
|
55
|
-
reasoning += block.thinking;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return reasoning || undefined;
|
|
59
|
-
}
|
|
60
|
-
function extractUsedTokens(data) {
|
|
61
|
-
var _a, _b, _c, _d;
|
|
62
|
-
const usage = data.usage;
|
|
63
|
-
if (!usage)
|
|
64
|
-
return undefined;
|
|
65
|
-
const inputTokens = (_a = usage.input_tokens) !== null && _a !== void 0 ? _a : 0;
|
|
66
|
-
const cacheWriteTokens = (_b = usage.cache_creation_input_tokens) !== null && _b !== void 0 ? _b : 0;
|
|
67
|
-
const cacheReadTokens = (_c = usage.cache_read_input_tokens) !== null && _c !== void 0 ? _c : 0;
|
|
68
|
-
return {
|
|
69
|
-
input_uncached: inputTokens + cacheWriteTokens,
|
|
70
|
-
input_cached: cacheReadTokens,
|
|
71
|
-
output: (_d = usage.output_tokens) !== null && _d !== void 0 ? _d : 0,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
function extractToolUse(data) {
|
|
75
|
-
var _a;
|
|
76
|
-
for (const block of (_a = data.content) !== null && _a !== void 0 ? _a : []) {
|
|
77
|
-
if ((block === null || block === void 0 ? void 0 : block.type) === "tool_use") {
|
|
78
|
-
return block;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return undefined;
|
|
82
|
-
}
|
|
83
|
-
function stringifyToolResult(toolResult) {
|
|
84
|
-
if (typeof toolResult === "string")
|
|
85
|
-
return toolResult;
|
|
86
|
-
if (typeof toolResult === "undefined")
|
|
87
|
-
return "";
|
|
88
|
-
return JSON.stringify(toolResult);
|
|
89
|
-
}
|
|
90
|
-
function executeToolCall(toolCall, tools) {
|
|
91
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
-
const tool = tools === null || tools === void 0 ? void 0 : tools.find((candidate) => candidate.name === toolCall.name);
|
|
93
|
-
if (!tool) {
|
|
94
|
-
throw new Error(`Tool "${toolCall.name}" not found`);
|
|
95
|
-
}
|
|
96
|
-
const toolResult = yield tool.handler(toolCall.input || {});
|
|
97
|
-
return stringifyToolResult(toolResult);
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
function mapTools(tools) {
|
|
101
|
-
if (!(tools === null || tools === void 0 ? void 0 : tools.length))
|
|
102
|
-
return undefined;
|
|
103
|
-
return tools.map((tool) => ({
|
|
104
|
-
name: tool.name,
|
|
105
|
-
description: tool.description,
|
|
106
|
-
input_schema: tool.input_schema,
|
|
107
|
-
}));
|
|
108
|
-
}
|
|
109
|
-
function mapReasoningToThinking(reasoningEffort, maxTokens) {
|
|
110
|
-
if (reasoningEffort === "none")
|
|
111
|
-
return undefined;
|
|
112
|
-
const availableBudget = maxTokens - 128;
|
|
113
|
-
if (availableBudget < 1024)
|
|
114
|
-
return undefined;
|
|
115
|
-
const ratioByEffort = {
|
|
116
|
-
minimal: 0.25,
|
|
117
|
-
low: 0.35,
|
|
118
|
-
medium: 0.5,
|
|
119
|
-
high: 0.7,
|
|
120
|
-
xhigh: 0.85,
|
|
121
|
-
};
|
|
122
|
-
const targetBudget = Math.floor(maxTokens * ratioByEffort[reasoningEffort]);
|
|
123
|
-
return {
|
|
124
|
-
type: "enabled",
|
|
125
|
-
budget_tokens: Math.max(1024, Math.min(availableBudget, targetBudget)),
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
function getAgentReasoningEffort(purpose) {
|
|
129
|
-
return purpose === "summary" ? "minimal" : "low";
|
|
130
|
-
}
|
|
131
|
-
function isSystemMessage(message) {
|
|
132
|
-
var _a, _b;
|
|
133
|
-
return (((_a = message._getType) === null || _a === void 0 ? void 0 : _a.call(message)) === "system" ||
|
|
134
|
-
((_b = message.getType) === null || _b === void 0 ? void 0 : _b.call(message)) === "system" ||
|
|
135
|
-
message.type === "system");
|
|
136
|
-
}
|
|
137
|
-
function contentToText(content, text) {
|
|
138
|
-
if (text)
|
|
139
|
-
return text;
|
|
140
|
-
if (typeof content === "string")
|
|
141
|
-
return content;
|
|
142
|
-
if (!Array.isArray(content))
|
|
143
|
-
return "";
|
|
144
|
-
return content
|
|
145
|
-
.map((block) => {
|
|
146
|
-
var _a;
|
|
147
|
-
if (typeof block === "string")
|
|
148
|
-
return block;
|
|
149
|
-
if (block && typeof block === "object" && "text" in block) {
|
|
150
|
-
return String((_a = block.text) !== null && _a !== void 0 ? _a : "");
|
|
151
|
-
}
|
|
152
|
-
return "";
|
|
153
|
-
})
|
|
154
|
-
.join("");
|
|
155
|
-
}
|
|
156
|
-
function normalizeAnthropicSystemMessages(request) {
|
|
157
|
-
const existingSystemText = contentToText(request.systemMessage.content, request.systemMessage.text);
|
|
158
|
-
const extraSystemText = request.messages
|
|
159
|
-
.filter(isSystemMessage)
|
|
160
|
-
.map((message) => contentToText(message.content, message.text))
|
|
161
|
-
.filter((text) => text && text !== existingSystemText)
|
|
162
|
-
.join("\n\n");
|
|
163
|
-
if (!extraSystemText) {
|
|
164
|
-
return Object.assign(Object.assign({}, request), { messages: request.messages.filter((message) => !isSystemMessage(message)) });
|
|
165
|
-
}
|
|
166
|
-
const systemMessage = request.systemMessage.concat(extraSystemText);
|
|
167
|
-
return Object.assign(Object.assign({}, request), { systemMessage, messages: request.messages.filter((message) => !isSystemMessage(message)) });
|
|
168
|
-
}
|
|
169
|
-
function createAnthropicSystemMessageMiddleware() {
|
|
170
|
-
return {
|
|
171
|
-
name: "AnthropicSystemMessageMiddleware",
|
|
172
|
-
wrapModelCall(request, handler) {
|
|
173
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
174
|
-
return handler(normalizeAnthropicSystemMessages(request));
|
|
175
|
-
});
|
|
176
|
-
},
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
export default class CompletionAdapterAnthropicMessages {
|
|
10
|
+
import { AnthropicMessagesService, } from "./anthropic.js";
|
|
11
|
+
import { createLangChainAgentSpec, } from "./langchain.js";
|
|
12
|
+
class CompletionAdapterAnthropicMessages {
|
|
180
13
|
constructor(options) {
|
|
181
14
|
this.complete = (requestOrContent_1, ...args_1) => __awaiter(this, [requestOrContent_1, ...args_1], void 0, function* (requestOrContent, maxTokens = 50, outputSchema, reasoningEffort = "low", toolsOrOnChunk, onChunk) {
|
|
182
|
-
var _a, e_1, _b, _c;
|
|
183
|
-
var _d, _e;
|
|
184
15
|
const request = typeof requestOrContent === "string"
|
|
185
16
|
? {
|
|
186
17
|
content: requestOrContent,
|
|
@@ -193,244 +24,24 @@ export default class CompletionAdapterAnthropicMessages {
|
|
|
193
24
|
: onChunk,
|
|
194
25
|
}
|
|
195
26
|
: requestOrContent;
|
|
196
|
-
|
|
197
|
-
const model = this.options.model || "claude-sonnet-4-5-20250929";
|
|
198
|
-
const isStreaming = typeof streamChunkCallback === "function";
|
|
199
|
-
const normalizedSchema = normalizeOutputSchema(requestOutputSchema);
|
|
200
|
-
const extra = Object.assign({}, (this.options.extraRequestBodyParameters || {}));
|
|
201
|
-
const thinking = extra.thinking || mapReasoningToThinking(requestReasoningEffort, requestMaxTokens);
|
|
202
|
-
const body = Object.assign(Object.assign({ model, max_tokens: requestMaxTokens, messages: [{ role: "user", content }], tools: mapTools(tools) }, (thinking ? { thinking } : {})), extra);
|
|
203
|
-
try {
|
|
204
|
-
if (requestOutputSchema && !normalizedSchema) {
|
|
205
|
-
return {
|
|
206
|
-
error: "Anthropic structured output requires a top-level JSON schema object with type: \"object\"",
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
if (normalizedSchema) {
|
|
210
|
-
const parsedMessage = (yield this.getClient().messages.parse(Object.assign(Object.assign({}, body), { output_config: {
|
|
211
|
-
format: jsonSchemaOutputFormat(normalizedSchema),
|
|
212
|
-
} })));
|
|
213
|
-
const parsedOutput = typeof parsedMessage.parsed_output === "undefined"
|
|
214
|
-
? extractOutputText(parsedMessage)
|
|
215
|
-
: JSON.stringify(parsedMessage.parsed_output);
|
|
216
|
-
const parsedReasoning = extractReasoning(parsedMessage);
|
|
217
|
-
const usedTokens = extractUsedTokens(parsedMessage);
|
|
218
|
-
if (parsedReasoning && isStreaming) {
|
|
219
|
-
yield (streamChunkCallback === null || streamChunkCallback === void 0 ? void 0 : streamChunkCallback(parsedReasoning, {
|
|
220
|
-
type: "reasoning",
|
|
221
|
-
delta: parsedReasoning,
|
|
222
|
-
text: parsedReasoning,
|
|
223
|
-
}));
|
|
224
|
-
}
|
|
225
|
-
if (parsedOutput && isStreaming) {
|
|
226
|
-
yield (streamChunkCallback === null || streamChunkCallback === void 0 ? void 0 : streamChunkCallback(parsedOutput, {
|
|
227
|
-
type: "output",
|
|
228
|
-
delta: parsedOutput,
|
|
229
|
-
text: parsedOutput,
|
|
230
|
-
}));
|
|
231
|
-
}
|
|
232
|
-
const toolUse = extractToolUse(parsedMessage);
|
|
233
|
-
if (toolUse) {
|
|
234
|
-
try {
|
|
235
|
-
const toolResult = yield executeToolCall(toolUse, tools);
|
|
236
|
-
if (toolResult && isStreaming) {
|
|
237
|
-
yield (streamChunkCallback === null || streamChunkCallback === void 0 ? void 0 : streamChunkCallback(toolResult, {
|
|
238
|
-
type: "output",
|
|
239
|
-
delta: toolResult,
|
|
240
|
-
text: toolResult,
|
|
241
|
-
}));
|
|
242
|
-
}
|
|
243
|
-
return {
|
|
244
|
-
content: toolResult,
|
|
245
|
-
finishReason: "tool_call",
|
|
246
|
-
used_tokens: usedTokens,
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
catch (error) {
|
|
250
|
-
return {
|
|
251
|
-
error: getErrorMessage(error),
|
|
252
|
-
finishReason: "tool_call",
|
|
253
|
-
used_tokens: usedTokens,
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
return {
|
|
258
|
-
content: parsedOutput || undefined,
|
|
259
|
-
finishReason: parsedMessage.stop_reason || undefined,
|
|
260
|
-
used_tokens: usedTokens,
|
|
261
|
-
};
|
|
262
|
-
}
|
|
263
|
-
if (!isStreaming) {
|
|
264
|
-
const message = (yield this.getClient().messages.create(body));
|
|
265
|
-
const usedTokens = extractUsedTokens(message);
|
|
266
|
-
const toolUse = extractToolUse(message);
|
|
267
|
-
if (toolUse) {
|
|
268
|
-
try {
|
|
269
|
-
const toolResult = yield executeToolCall(toolUse, tools);
|
|
270
|
-
return {
|
|
271
|
-
content: toolResult,
|
|
272
|
-
finishReason: "tool_call",
|
|
273
|
-
used_tokens: usedTokens,
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
catch (error) {
|
|
277
|
-
return {
|
|
278
|
-
error: getErrorMessage(error),
|
|
279
|
-
finishReason: "tool_call",
|
|
280
|
-
used_tokens: usedTokens,
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
return {
|
|
285
|
-
content: extractOutputText(message) || undefined,
|
|
286
|
-
finishReason: message.stop_reason || undefined,
|
|
287
|
-
used_tokens: usedTokens,
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
|
-
const stream = this.getClient().messages.stream(body);
|
|
291
|
-
let fullContent = "";
|
|
292
|
-
let fullReasoning = "";
|
|
293
|
-
try {
|
|
294
|
-
for (var _f = true, _g = __asyncValues(stream), _h; _h = yield _g.next(), _a = _h.done, !_a; _f = true) {
|
|
295
|
-
_c = _h.value;
|
|
296
|
-
_f = false;
|
|
297
|
-
const event = _c;
|
|
298
|
-
if ((event === null || event === void 0 ? void 0 : event.type) !== "content_block_delta")
|
|
299
|
-
continue;
|
|
300
|
-
if (((_d = event.delta) === null || _d === void 0 ? void 0 : _d.type) === "text_delta" && typeof event.delta.text === "string") {
|
|
301
|
-
fullContent += event.delta.text;
|
|
302
|
-
yield (streamChunkCallback === null || streamChunkCallback === void 0 ? void 0 : streamChunkCallback(event.delta.text, {
|
|
303
|
-
type: "output",
|
|
304
|
-
delta: event.delta.text,
|
|
305
|
-
text: fullContent,
|
|
306
|
-
}));
|
|
307
|
-
continue;
|
|
308
|
-
}
|
|
309
|
-
if (((_e = event.delta) === null || _e === void 0 ? void 0 : _e.type) === "thinking_delta" &&
|
|
310
|
-
typeof event.delta.thinking === "string") {
|
|
311
|
-
fullReasoning += event.delta.thinking;
|
|
312
|
-
yield (streamChunkCallback === null || streamChunkCallback === void 0 ? void 0 : streamChunkCallback(event.delta.thinking, {
|
|
313
|
-
type: "reasoning",
|
|
314
|
-
delta: event.delta.thinking,
|
|
315
|
-
text: fullReasoning,
|
|
316
|
-
}));
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
321
|
-
finally {
|
|
322
|
-
try {
|
|
323
|
-
if (!_f && !_a && (_b = _g.return)) yield _b.call(_g);
|
|
324
|
-
}
|
|
325
|
-
finally { if (e_1) throw e_1.error; }
|
|
326
|
-
}
|
|
327
|
-
const message = (yield stream.finalMessage());
|
|
328
|
-
const finalContent = extractOutputText(message);
|
|
329
|
-
const finalReasoning = extractReasoning(message) || "";
|
|
330
|
-
const usedTokens = extractUsedTokens(message);
|
|
331
|
-
if (finalReasoning && finalReasoning !== fullReasoning) {
|
|
332
|
-
const delta = finalReasoning.startsWith(fullReasoning)
|
|
333
|
-
? finalReasoning.slice(fullReasoning.length)
|
|
334
|
-
: finalReasoning;
|
|
335
|
-
if (delta) {
|
|
336
|
-
fullReasoning = finalReasoning;
|
|
337
|
-
yield (streamChunkCallback === null || streamChunkCallback === void 0 ? void 0 : streamChunkCallback(delta, {
|
|
338
|
-
type: "reasoning",
|
|
339
|
-
delta,
|
|
340
|
-
text: finalReasoning,
|
|
341
|
-
}));
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
if (finalContent && finalContent !== fullContent) {
|
|
345
|
-
const delta = finalContent.startsWith(fullContent)
|
|
346
|
-
? finalContent.slice(fullContent.length)
|
|
347
|
-
: finalContent;
|
|
348
|
-
if (delta) {
|
|
349
|
-
fullContent = finalContent;
|
|
350
|
-
yield (streamChunkCallback === null || streamChunkCallback === void 0 ? void 0 : streamChunkCallback(delta, {
|
|
351
|
-
type: "output",
|
|
352
|
-
delta,
|
|
353
|
-
text: finalContent,
|
|
354
|
-
}));
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
const toolUse = extractToolUse(message);
|
|
358
|
-
if (toolUse) {
|
|
359
|
-
try {
|
|
360
|
-
const toolResult = yield executeToolCall(toolUse, tools);
|
|
361
|
-
if (toolResult) {
|
|
362
|
-
const delta = toolResult.startsWith(fullContent)
|
|
363
|
-
? toolResult.slice(fullContent.length)
|
|
364
|
-
: toolResult;
|
|
365
|
-
if (delta) {
|
|
366
|
-
yield (streamChunkCallback === null || streamChunkCallback === void 0 ? void 0 : streamChunkCallback(delta, {
|
|
367
|
-
type: "output",
|
|
368
|
-
delta,
|
|
369
|
-
text: toolResult,
|
|
370
|
-
}));
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
return {
|
|
374
|
-
content: toolResult,
|
|
375
|
-
finishReason: "tool_call",
|
|
376
|
-
used_tokens: usedTokens,
|
|
377
|
-
};
|
|
378
|
-
}
|
|
379
|
-
catch (error) {
|
|
380
|
-
return {
|
|
381
|
-
error: getErrorMessage(error),
|
|
382
|
-
content: fullContent || undefined,
|
|
383
|
-
finishReason: "tool_call",
|
|
384
|
-
used_tokens: usedTokens,
|
|
385
|
-
};
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
return {
|
|
389
|
-
content: fullContent || undefined,
|
|
390
|
-
finishReason: message.stop_reason || undefined,
|
|
391
|
-
used_tokens: usedTokens,
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
catch (error) {
|
|
395
|
-
return {
|
|
396
|
-
error: getErrorMessage(error),
|
|
397
|
-
};
|
|
398
|
-
}
|
|
27
|
+
return this.anthropic.complete(request);
|
|
399
28
|
});
|
|
400
29
|
this.options = options;
|
|
401
|
-
|
|
402
|
-
getClient() {
|
|
403
|
-
if (!this.client) {
|
|
404
|
-
const apiKey = getApiKey(this.options);
|
|
405
|
-
if (!apiKey) {
|
|
406
|
-
throw new Error("anthropicApiKey is required");
|
|
407
|
-
}
|
|
408
|
-
this.client = new Anthropic({ apiKey });
|
|
409
|
-
}
|
|
410
|
-
return this.client;
|
|
30
|
+
this.anthropic = new AnthropicMessagesService(options);
|
|
411
31
|
}
|
|
412
32
|
validate() {
|
|
413
|
-
|
|
414
|
-
throw new Error("anthropicApiKey is required");
|
|
415
|
-
}
|
|
33
|
+
this.anthropic.validate();
|
|
416
34
|
}
|
|
417
35
|
measureTokensCount(content) {
|
|
418
|
-
return
|
|
419
|
-
const response = yield this.getClient().messages.countTokens({
|
|
420
|
-
model: this.options.model || "claude-sonnet-4-5-20250929",
|
|
421
|
-
messages: [{ role: "user", content }],
|
|
422
|
-
});
|
|
423
|
-
return response.input_tokens;
|
|
424
|
-
});
|
|
36
|
+
return this.anthropic.measureTokensCount(content);
|
|
425
37
|
}
|
|
426
38
|
getLangChainAgentSpec(params) {
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
model: new ChatAnthropic(Object.assign(Object.assign({ model: this.options.model || "claude-sonnet-4-5-20250929", apiKey: getApiKey(this.options), maxTokens: params.maxTokens }, (thinking ? { thinking } : {})), { invocationKwargs: extraRequestBodyParameters })),
|
|
433
|
-
middleware: [createAnthropicSystemMessageMiddleware()],
|
|
434
|
-
};
|
|
39
|
+
return createLangChainAgentSpec({
|
|
40
|
+
options: this.options,
|
|
41
|
+
maxTokens: params.maxTokens,
|
|
42
|
+
purpose: params.purpose,
|
|
43
|
+
});
|
|
435
44
|
}
|
|
436
45
|
}
|
|
46
|
+
export { CompletionAdapterAnthropicMessages };
|
|
47
|
+
export default CompletionAdapterAnthropicMessages;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ChatAnthropic } from "@langchain/anthropic";
|
|
2
|
+
import type { AdapterOptions } from "./types.js";
|
|
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
|
+
type LangChainModelCallRequest = {
|
|
12
|
+
systemMessage: LangChainMessageLike & {
|
|
13
|
+
concat: (content: string) => LangChainMessageLike;
|
|
14
|
+
};
|
|
15
|
+
messages: LangChainMessageLike[];
|
|
16
|
+
};
|
|
17
|
+
export declare function createLangChainAgentSpec(params: {
|
|
18
|
+
options: AdapterOptions;
|
|
19
|
+
maxTokens: number;
|
|
20
|
+
purpose: AgentModelPurpose;
|
|
21
|
+
}): {
|
|
22
|
+
model: ChatAnthropic;
|
|
23
|
+
middleware: {
|
|
24
|
+
name: string;
|
|
25
|
+
wrapModelCall(request: LangChainModelCallRequest, handler: (request: LangChainModelCallRequest) => unknown): Promise<unknown>;
|
|
26
|
+
}[];
|
|
27
|
+
};
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { ChatAnthropic } from "@langchain/anthropic";
|
|
11
|
+
import { getApiKey, mapReasoningToThinking, } from "./anthropic.js";
|
|
12
|
+
function getAgentReasoningEffort(purpose) {
|
|
13
|
+
return purpose === "summary" ? "minimal" : "low";
|
|
14
|
+
}
|
|
15
|
+
function isSystemMessage(message) {
|
|
16
|
+
var _a, _b;
|
|
17
|
+
return (((_a = message._getType) === null || _a === void 0 ? void 0 : _a.call(message)) === "system" ||
|
|
18
|
+
((_b = message.getType) === null || _b === void 0 ? void 0 : _b.call(message)) === "system" ||
|
|
19
|
+
message.type === "system");
|
|
20
|
+
}
|
|
21
|
+
function contentToText(content, text) {
|
|
22
|
+
if (text)
|
|
23
|
+
return text;
|
|
24
|
+
if (typeof content === "string")
|
|
25
|
+
return content;
|
|
26
|
+
if (!Array.isArray(content))
|
|
27
|
+
return "";
|
|
28
|
+
return content
|
|
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("");
|
|
39
|
+
}
|
|
40
|
+
function normalizeAnthropicSystemMessages(request) {
|
|
41
|
+
const existingSystemText = contentToText(request.systemMessage.content, request.systemMessage.text);
|
|
42
|
+
const extraSystemText = request.messages
|
|
43
|
+
.filter(isSystemMessage)
|
|
44
|
+
.map((message) => contentToText(message.content, message.text))
|
|
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)) });
|
|
49
|
+
}
|
|
50
|
+
const systemMessage = request.systemMessage.concat(extraSystemText);
|
|
51
|
+
return Object.assign(Object.assign({}, request), { systemMessage, messages: request.messages.filter((message) => !isSystemMessage(message)) });
|
|
52
|
+
}
|
|
53
|
+
function createAnthropicSystemMessageMiddleware() {
|
|
54
|
+
return {
|
|
55
|
+
name: "AnthropicSystemMessageMiddleware",
|
|
56
|
+
wrapModelCall(request, handler) {
|
|
57
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
+
return handler(normalizeAnthropicSystemMessages(request));
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
export function createLangChainAgentSpec(params) {
|
|
64
|
+
const extraRequestBodyParameters = Object.assign({}, (params.options.extraRequestBodyParameters || {}));
|
|
65
|
+
const thinking = extraRequestBodyParameters.thinking ||
|
|
66
|
+
mapReasoningToThinking(getAgentReasoningEffort(params.purpose), params.maxTokens);
|
|
67
|
+
delete extraRequestBodyParameters.thinking;
|
|
68
|
+
return {
|
|
69
|
+
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: [createAnthropicSystemMessageMiddleware()],
|
|
71
|
+
};
|
|
72
|
+
}
|