@adminforth/completion-adapter-anthropic-messages 1.0.0 → 1.2.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.
- package/anthropic.ts +491 -0
- package/dist/anthropic.d.ts +40 -0
- package/dist/anthropic.js +379 -0
- package/dist/index.d.ts +56 -36
- package/dist/index.js +14 -377
- package/dist/langchain.d.ts +28 -0
- package/dist/langchain.js +72 -0
- package/index.ts +27 -513
- package/langchain.ts +126 -0
- package/package.json +1 -1
package/anthropic.ts
ADDED
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CompletionStreamEvent,
|
|
3
|
+
CompletionTool,
|
|
4
|
+
} from "adminforth";
|
|
5
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
6
|
+
import { jsonSchemaOutputFormat } from "@anthropic-ai/sdk/helpers/json-schema";
|
|
7
|
+
import type { AdapterOptions } from "./types.js";
|
|
8
|
+
|
|
9
|
+
export type StreamChunkCallback = (
|
|
10
|
+
chunk: string,
|
|
11
|
+
event?: CompletionStreamEvent,
|
|
12
|
+
) => void | Promise<void>;
|
|
13
|
+
|
|
14
|
+
export type ReasoningEffort =
|
|
15
|
+
| "none"
|
|
16
|
+
| "minimal"
|
|
17
|
+
| "low"
|
|
18
|
+
| "medium"
|
|
19
|
+
| "high"
|
|
20
|
+
| "xhigh";
|
|
21
|
+
|
|
22
|
+
export type CompletionRequestInput = {
|
|
23
|
+
content: string;
|
|
24
|
+
maxTokens?: number;
|
|
25
|
+
outputSchema?: any;
|
|
26
|
+
reasoningEffort?: ReasoningEffort;
|
|
27
|
+
tools?: CompletionTool[];
|
|
28
|
+
onChunk?: StreamChunkCallback;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
type JsonSchemaObject = {
|
|
32
|
+
type: "object";
|
|
33
|
+
[key: string]: unknown;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
type AnthropicMessageLike = {
|
|
37
|
+
content?: Array<any>;
|
|
38
|
+
stop_reason?: string | null;
|
|
39
|
+
parsed_output?: unknown;
|
|
40
|
+
usage?: AnthropicUsage;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
type AnthropicToolUseBlock = {
|
|
44
|
+
type: "tool_use";
|
|
45
|
+
id: string;
|
|
46
|
+
name: string;
|
|
47
|
+
input: unknown;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
type AnthropicUsage = {
|
|
51
|
+
input_tokens?: number;
|
|
52
|
+
cache_creation_input_tokens?: number | null;
|
|
53
|
+
cache_read_input_tokens?: number | null;
|
|
54
|
+
output_tokens?: number;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type UsedTokens = {
|
|
58
|
+
input_uncached: number;
|
|
59
|
+
input_cached: number;
|
|
60
|
+
output: number;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export type CompletionResult = {
|
|
64
|
+
content?: string;
|
|
65
|
+
finishReason?: string;
|
|
66
|
+
error?: string;
|
|
67
|
+
used_tokens?: UsedTokens;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export function getApiKey(options: AdapterOptions): string | undefined {
|
|
71
|
+
return options.anthropicApiKey || options.antropicApiKey;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function getErrorMessage(error: unknown): string {
|
|
75
|
+
if (error instanceof Error && error.message) return error.message;
|
|
76
|
+
return String(error || "Unknown error");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function normalizeOutputSchema(outputSchema: any): JsonSchemaObject | undefined {
|
|
80
|
+
if (!outputSchema || typeof outputSchema !== "object") return undefined;
|
|
81
|
+
const candidate = outputSchema.schema || outputSchema.json_schema || outputSchema;
|
|
82
|
+
if (!candidate || typeof candidate !== "object") {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
if (candidate.type !== "object") {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
return candidate as JsonSchemaObject;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function extractOutputText(data: AnthropicMessageLike): string {
|
|
92
|
+
let text = "";
|
|
93
|
+
|
|
94
|
+
for (const block of data.content ?? []) {
|
|
95
|
+
if (block?.type === "text" && typeof block.text === "string") {
|
|
96
|
+
text += block.text;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return text;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function extractReasoning(data: AnthropicMessageLike): string | undefined {
|
|
104
|
+
let reasoning = "";
|
|
105
|
+
|
|
106
|
+
for (const block of data.content ?? []) {
|
|
107
|
+
if (block?.type === "thinking" && typeof block.thinking === "string") {
|
|
108
|
+
reasoning += block.thinking;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return reasoning || undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function extractUsedTokens(data: AnthropicMessageLike): UsedTokens | undefined {
|
|
116
|
+
const usage = data.usage;
|
|
117
|
+
if (!usage) return undefined;
|
|
118
|
+
|
|
119
|
+
const inputTokens = usage.input_tokens ?? 0;
|
|
120
|
+
const cacheWriteTokens = usage.cache_creation_input_tokens ?? 0;
|
|
121
|
+
const cacheReadTokens = usage.cache_read_input_tokens ?? 0;
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
input_uncached: inputTokens + cacheWriteTokens,
|
|
125
|
+
input_cached: cacheReadTokens,
|
|
126
|
+
output: usage.output_tokens ?? 0,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function extractToolUse(data: AnthropicMessageLike): AnthropicToolUseBlock | undefined {
|
|
131
|
+
for (const block of data.content ?? []) {
|
|
132
|
+
if (block?.type === "tool_use") {
|
|
133
|
+
return block as AnthropicToolUseBlock;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function stringifyToolResult(toolResult: unknown): string {
|
|
141
|
+
if (typeof toolResult === "string") return toolResult;
|
|
142
|
+
if (typeof toolResult === "undefined") return "";
|
|
143
|
+
return JSON.stringify(toolResult);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function executeToolCall(
|
|
147
|
+
toolCall: AnthropicToolUseBlock,
|
|
148
|
+
tools?: CompletionTool[],
|
|
149
|
+
): Promise<string> {
|
|
150
|
+
const tool = tools?.find((candidate) => candidate.name === toolCall.name);
|
|
151
|
+
if (!tool) {
|
|
152
|
+
throw new Error(`Tool "${toolCall.name}" not found`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const toolResult = await tool.handler(toolCall.input || {});
|
|
156
|
+
return stringifyToolResult(toolResult);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function mapTools(tools?: CompletionTool[]) {
|
|
160
|
+
if (!tools?.length) return undefined;
|
|
161
|
+
return tools.map((tool) => ({
|
|
162
|
+
name: tool.name,
|
|
163
|
+
description: tool.description,
|
|
164
|
+
input_schema: tool.input_schema,
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function mapReasoningToThinking(
|
|
169
|
+
reasoningEffort: ReasoningEffort,
|
|
170
|
+
maxTokens: number,
|
|
171
|
+
): { type: "enabled"; budget_tokens: number } | undefined {
|
|
172
|
+
if (reasoningEffort === "none") return undefined;
|
|
173
|
+
|
|
174
|
+
const availableBudget = maxTokens - 128;
|
|
175
|
+
if (availableBudget < 1024) return undefined;
|
|
176
|
+
|
|
177
|
+
const ratioByEffort: Record<Exclude<ReasoningEffort, "none">, number> = {
|
|
178
|
+
minimal: 0.25,
|
|
179
|
+
low: 0.35,
|
|
180
|
+
medium: 0.5,
|
|
181
|
+
high: 0.7,
|
|
182
|
+
xhigh: 0.85,
|
|
183
|
+
};
|
|
184
|
+
const targetBudget = Math.floor(maxTokens * ratioByEffort[reasoningEffort]);
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
type: "enabled",
|
|
188
|
+
budget_tokens: Math.max(1024, Math.min(availableBudget, targetBudget)),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export class AnthropicMessagesService {
|
|
193
|
+
private client?: Anthropic;
|
|
194
|
+
|
|
195
|
+
constructor(private options: AdapterOptions) {}
|
|
196
|
+
|
|
197
|
+
validate() {
|
|
198
|
+
if (!getApiKey(this.options)) {
|
|
199
|
+
throw new Error("anthropicApiKey is required");
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async measureTokensCount(content: string): Promise<number> {
|
|
204
|
+
const response = await this.getClient().messages.countTokens({
|
|
205
|
+
model: this.options.model || "claude-sonnet-4-5-20250929",
|
|
206
|
+
messages: [{ role: "user", content }],
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
return response.input_tokens;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async complete(request: CompletionRequestInput): Promise<CompletionResult> {
|
|
213
|
+
const {
|
|
214
|
+
content,
|
|
215
|
+
maxTokens: requestMaxTokens = 50,
|
|
216
|
+
outputSchema: requestOutputSchema,
|
|
217
|
+
reasoningEffort: requestReasoningEffort = "low",
|
|
218
|
+
tools,
|
|
219
|
+
onChunk: streamChunkCallback,
|
|
220
|
+
} = request;
|
|
221
|
+
const model = this.options.model || "claude-sonnet-4-5-20250929";
|
|
222
|
+
const isStreaming = typeof streamChunkCallback === "function";
|
|
223
|
+
const normalizedSchema = normalizeOutputSchema(requestOutputSchema);
|
|
224
|
+
const extra = {
|
|
225
|
+
...(this.options.extraRequestBodyParameters || {}),
|
|
226
|
+
} as Record<string, unknown>;
|
|
227
|
+
const thinking = extra.thinking || mapReasoningToThinking(
|
|
228
|
+
requestReasoningEffort,
|
|
229
|
+
requestMaxTokens,
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
const body = {
|
|
233
|
+
model,
|
|
234
|
+
max_tokens: requestMaxTokens,
|
|
235
|
+
messages: [{ role: "user", content }],
|
|
236
|
+
tools: mapTools(tools),
|
|
237
|
+
...(thinking ? { thinking } : {}),
|
|
238
|
+
...extra,
|
|
239
|
+
} as Record<string, unknown>;
|
|
240
|
+
|
|
241
|
+
try {
|
|
242
|
+
if (requestOutputSchema && !normalizedSchema) {
|
|
243
|
+
return {
|
|
244
|
+
error:
|
|
245
|
+
"Anthropic structured output requires a top-level JSON schema object with type: \"object\"",
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (normalizedSchema) {
|
|
250
|
+
return this.completeWithStructuredOutput(
|
|
251
|
+
body,
|
|
252
|
+
normalizedSchema,
|
|
253
|
+
tools,
|
|
254
|
+
isStreaming,
|
|
255
|
+
streamChunkCallback,
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (!isStreaming) {
|
|
260
|
+
return this.completeNonStreaming(body, tools);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return this.completeStreaming(body, tools, streamChunkCallback);
|
|
264
|
+
} catch (error) {
|
|
265
|
+
return {
|
|
266
|
+
error: getErrorMessage(error),
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private getClient(): Anthropic {
|
|
272
|
+
if (!this.client) {
|
|
273
|
+
const apiKey = getApiKey(this.options);
|
|
274
|
+
if (!apiKey) {
|
|
275
|
+
throw new Error("anthropicApiKey is required");
|
|
276
|
+
}
|
|
277
|
+
this.client = new Anthropic({ apiKey });
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return this.client;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private async completeWithStructuredOutput(
|
|
284
|
+
body: Record<string, unknown>,
|
|
285
|
+
normalizedSchema: JsonSchemaObject,
|
|
286
|
+
tools: CompletionTool[] | undefined,
|
|
287
|
+
isStreaming: boolean,
|
|
288
|
+
streamChunkCallback: StreamChunkCallback | undefined,
|
|
289
|
+
): Promise<CompletionResult> {
|
|
290
|
+
const parsedMessage = (await this.getClient().messages.parse({
|
|
291
|
+
...(body as any),
|
|
292
|
+
output_config: {
|
|
293
|
+
format: jsonSchemaOutputFormat(normalizedSchema),
|
|
294
|
+
},
|
|
295
|
+
} as any)) as AnthropicMessageLike;
|
|
296
|
+
const parsedOutput =
|
|
297
|
+
typeof parsedMessage.parsed_output === "undefined"
|
|
298
|
+
? extractOutputText(parsedMessage)
|
|
299
|
+
: JSON.stringify(parsedMessage.parsed_output);
|
|
300
|
+
const parsedReasoning = extractReasoning(parsedMessage);
|
|
301
|
+
const usedTokens = extractUsedTokens(parsedMessage);
|
|
302
|
+
|
|
303
|
+
if (parsedReasoning && isStreaming) {
|
|
304
|
+
await streamChunkCallback?.(parsedReasoning, {
|
|
305
|
+
type: "reasoning",
|
|
306
|
+
delta: parsedReasoning,
|
|
307
|
+
text: parsedReasoning,
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
if (parsedOutput && isStreaming) {
|
|
311
|
+
await streamChunkCallback?.(parsedOutput, {
|
|
312
|
+
type: "output",
|
|
313
|
+
delta: parsedOutput,
|
|
314
|
+
text: parsedOutput,
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const toolUse = extractToolUse(parsedMessage);
|
|
319
|
+
if (toolUse) {
|
|
320
|
+
try {
|
|
321
|
+
const toolResult = await executeToolCall(toolUse, tools);
|
|
322
|
+
if (toolResult && isStreaming) {
|
|
323
|
+
await streamChunkCallback?.(toolResult, {
|
|
324
|
+
type: "output",
|
|
325
|
+
delta: toolResult,
|
|
326
|
+
text: toolResult,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return {
|
|
331
|
+
content: toolResult,
|
|
332
|
+
finishReason: "tool_call",
|
|
333
|
+
used_tokens: usedTokens,
|
|
334
|
+
};
|
|
335
|
+
} catch (error) {
|
|
336
|
+
return {
|
|
337
|
+
error: getErrorMessage(error),
|
|
338
|
+
finishReason: "tool_call",
|
|
339
|
+
used_tokens: usedTokens,
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return {
|
|
345
|
+
content: parsedOutput || undefined,
|
|
346
|
+
finishReason: parsedMessage.stop_reason || undefined,
|
|
347
|
+
used_tokens: usedTokens,
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
private async completeNonStreaming(
|
|
352
|
+
body: Record<string, unknown>,
|
|
353
|
+
tools?: CompletionTool[],
|
|
354
|
+
): Promise<CompletionResult> {
|
|
355
|
+
const message = (await this.getClient().messages.create(
|
|
356
|
+
body as any,
|
|
357
|
+
)) as AnthropicMessageLike;
|
|
358
|
+
const usedTokens = extractUsedTokens(message);
|
|
359
|
+
|
|
360
|
+
const toolUse = extractToolUse(message);
|
|
361
|
+
if (toolUse) {
|
|
362
|
+
try {
|
|
363
|
+
const toolResult = await executeToolCall(toolUse, tools);
|
|
364
|
+
return {
|
|
365
|
+
content: toolResult,
|
|
366
|
+
finishReason: "tool_call",
|
|
367
|
+
used_tokens: usedTokens,
|
|
368
|
+
};
|
|
369
|
+
} catch (error) {
|
|
370
|
+
return {
|
|
371
|
+
error: getErrorMessage(error),
|
|
372
|
+
finishReason: "tool_call",
|
|
373
|
+
used_tokens: usedTokens,
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return {
|
|
379
|
+
content: extractOutputText(message) || undefined,
|
|
380
|
+
finishReason: message.stop_reason || undefined,
|
|
381
|
+
used_tokens: usedTokens,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
private async completeStreaming(
|
|
386
|
+
body: Record<string, unknown>,
|
|
387
|
+
tools: CompletionTool[] | undefined,
|
|
388
|
+
streamChunkCallback: StreamChunkCallback | undefined,
|
|
389
|
+
): Promise<CompletionResult> {
|
|
390
|
+
const stream = this.getClient().messages.stream(body as any);
|
|
391
|
+
let fullContent = "";
|
|
392
|
+
let fullReasoning = "";
|
|
393
|
+
|
|
394
|
+
for await (const event of stream as any) {
|
|
395
|
+
if (event?.type !== "content_block_delta") continue;
|
|
396
|
+
|
|
397
|
+
if (event.delta?.type === "text_delta" && typeof event.delta.text === "string") {
|
|
398
|
+
fullContent += event.delta.text;
|
|
399
|
+
await streamChunkCallback?.(event.delta.text, {
|
|
400
|
+
type: "output",
|
|
401
|
+
delta: event.delta.text,
|
|
402
|
+
text: fullContent,
|
|
403
|
+
});
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if (
|
|
408
|
+
event.delta?.type === "thinking_delta" &&
|
|
409
|
+
typeof event.delta.thinking === "string"
|
|
410
|
+
) {
|
|
411
|
+
fullReasoning += event.delta.thinking;
|
|
412
|
+
await streamChunkCallback?.(event.delta.thinking, {
|
|
413
|
+
type: "reasoning",
|
|
414
|
+
delta: event.delta.thinking,
|
|
415
|
+
text: fullReasoning,
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const message = (await stream.finalMessage()) as AnthropicMessageLike;
|
|
421
|
+
const finalContent = extractOutputText(message);
|
|
422
|
+
const finalReasoning = extractReasoning(message) || "";
|
|
423
|
+
const usedTokens = extractUsedTokens(message);
|
|
424
|
+
|
|
425
|
+
if (finalReasoning && finalReasoning !== fullReasoning) {
|
|
426
|
+
const delta = finalReasoning.startsWith(fullReasoning)
|
|
427
|
+
? finalReasoning.slice(fullReasoning.length)
|
|
428
|
+
: finalReasoning;
|
|
429
|
+
if (delta) {
|
|
430
|
+
fullReasoning = finalReasoning;
|
|
431
|
+
await streamChunkCallback?.(delta, {
|
|
432
|
+
type: "reasoning",
|
|
433
|
+
delta,
|
|
434
|
+
text: finalReasoning,
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if (finalContent && finalContent !== fullContent) {
|
|
440
|
+
const delta = finalContent.startsWith(fullContent)
|
|
441
|
+
? finalContent.slice(fullContent.length)
|
|
442
|
+
: finalContent;
|
|
443
|
+
if (delta) {
|
|
444
|
+
fullContent = finalContent;
|
|
445
|
+
await streamChunkCallback?.(delta, {
|
|
446
|
+
type: "output",
|
|
447
|
+
delta,
|
|
448
|
+
text: finalContent,
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const toolUse = extractToolUse(message);
|
|
454
|
+
if (toolUse) {
|
|
455
|
+
try {
|
|
456
|
+
const toolResult = await executeToolCall(toolUse, tools);
|
|
457
|
+
if (toolResult) {
|
|
458
|
+
const delta = toolResult.startsWith(fullContent)
|
|
459
|
+
? toolResult.slice(fullContent.length)
|
|
460
|
+
: toolResult;
|
|
461
|
+
if (delta) {
|
|
462
|
+
await streamChunkCallback?.(delta, {
|
|
463
|
+
type: "output",
|
|
464
|
+
delta,
|
|
465
|
+
text: toolResult,
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return {
|
|
471
|
+
content: toolResult,
|
|
472
|
+
finishReason: "tool_call",
|
|
473
|
+
used_tokens: usedTokens,
|
|
474
|
+
};
|
|
475
|
+
} catch (error) {
|
|
476
|
+
return {
|
|
477
|
+
error: getErrorMessage(error),
|
|
478
|
+
content: fullContent || undefined,
|
|
479
|
+
finishReason: "tool_call",
|
|
480
|
+
used_tokens: usedTokens,
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
return {
|
|
486
|
+
content: fullContent || undefined,
|
|
487
|
+
finishReason: message.stop_reason || undefined,
|
|
488
|
+
used_tokens: usedTokens,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { CompletionStreamEvent, CompletionTool } from "adminforth";
|
|
2
|
+
import type { AdapterOptions } from "./types.js";
|
|
3
|
+
export type StreamChunkCallback = (chunk: string, event?: CompletionStreamEvent) => void | Promise<void>;
|
|
4
|
+
export type ReasoningEffort = "none" | "minimal" | "low" | "medium" | "high" | "xhigh";
|
|
5
|
+
export type CompletionRequestInput = {
|
|
6
|
+
content: string;
|
|
7
|
+
maxTokens?: number;
|
|
8
|
+
outputSchema?: any;
|
|
9
|
+
reasoningEffort?: ReasoningEffort;
|
|
10
|
+
tools?: CompletionTool[];
|
|
11
|
+
onChunk?: StreamChunkCallback;
|
|
12
|
+
};
|
|
13
|
+
export type UsedTokens = {
|
|
14
|
+
input_uncached: number;
|
|
15
|
+
input_cached: number;
|
|
16
|
+
output: number;
|
|
17
|
+
};
|
|
18
|
+
export type CompletionResult = {
|
|
19
|
+
content?: string;
|
|
20
|
+
finishReason?: string;
|
|
21
|
+
error?: string;
|
|
22
|
+
used_tokens?: UsedTokens;
|
|
23
|
+
};
|
|
24
|
+
export declare function getApiKey(options: AdapterOptions): string | undefined;
|
|
25
|
+
export declare function mapReasoningToThinking(reasoningEffort: ReasoningEffort, maxTokens: number): {
|
|
26
|
+
type: "enabled";
|
|
27
|
+
budget_tokens: number;
|
|
28
|
+
} | undefined;
|
|
29
|
+
export declare class AnthropicMessagesService {
|
|
30
|
+
private options;
|
|
31
|
+
private client?;
|
|
32
|
+
constructor(options: AdapterOptions);
|
|
33
|
+
validate(): void;
|
|
34
|
+
measureTokensCount(content: string): Promise<number>;
|
|
35
|
+
complete(request: CompletionRequestInput): Promise<CompletionResult>;
|
|
36
|
+
private getClient;
|
|
37
|
+
private completeWithStructuredOutput;
|
|
38
|
+
private completeNonStreaming;
|
|
39
|
+
private completeStreaming;
|
|
40
|
+
}
|