@node-llm/core 0.4.1 → 0.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.
- package/README.md +89 -27
- package/dist/chat/Chat.d.ts +6 -1
- package/dist/chat/Chat.d.ts.map +1 -1
- package/dist/chat/Chat.js +27 -4
- package/dist/chat/ChatOptions.d.ts +3 -0
- package/dist/chat/ChatOptions.d.ts.map +1 -1
- package/dist/chat/ChatResponse.d.ts +3 -0
- package/dist/chat/ChatResponse.d.ts.map +1 -1
- package/dist/chat/ChatResponse.js +3 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/llm.d.ts +5 -1
- package/dist/llm.d.ts.map +1 -1
- package/dist/llm.js +22 -6
- package/dist/models/ModelRegistry.d.ts +39 -12
- package/dist/models/ModelRegistry.d.ts.map +1 -1
- package/dist/models/ModelRegistry.js +50 -40
- package/dist/models/models.d.ts +972 -0
- package/dist/models/models.d.ts.map +1 -0
- package/dist/models/models.js +7026 -0
- package/dist/models/types.d.ts +50 -0
- package/dist/models/types.d.ts.map +1 -0
- package/dist/models/types.js +1 -0
- package/dist/providers/Provider.d.ts +5 -0
- package/dist/providers/Provider.d.ts.map +1 -1
- package/dist/providers/anthropic/AnthropicProvider.d.ts +32 -0
- package/dist/providers/anthropic/AnthropicProvider.d.ts.map +1 -0
- package/dist/providers/anthropic/AnthropicProvider.js +49 -0
- package/dist/providers/anthropic/Capabilities.d.ts +11 -0
- package/dist/providers/anthropic/Capabilities.d.ts.map +1 -0
- package/dist/providers/anthropic/Capabilities.js +82 -0
- package/dist/providers/anthropic/Chat.d.ts +8 -0
- package/dist/providers/anthropic/Chat.d.ts.map +1 -0
- package/dist/providers/anthropic/Chat.js +97 -0
- package/dist/providers/anthropic/Errors.d.ts +2 -0
- package/dist/providers/anthropic/Errors.d.ts.map +1 -0
- package/dist/providers/anthropic/Errors.js +33 -0
- package/dist/providers/anthropic/Models.d.ts +9 -0
- package/dist/providers/anthropic/Models.d.ts.map +1 -0
- package/dist/providers/anthropic/Models.js +58 -0
- package/dist/providers/anthropic/Streaming.d.ts +8 -0
- package/dist/providers/anthropic/Streaming.d.ts.map +1 -0
- package/dist/providers/anthropic/Streaming.js +113 -0
- package/dist/providers/anthropic/Utils.d.ts +5 -0
- package/dist/providers/anthropic/Utils.d.ts.map +1 -0
- package/dist/providers/anthropic/Utils.js +125 -0
- package/dist/providers/anthropic/index.d.ts +2 -0
- package/dist/providers/anthropic/index.d.ts.map +1 -0
- package/dist/providers/anthropic/index.js +11 -0
- package/dist/providers/anthropic/types.d.ts +57 -0
- package/dist/providers/anthropic/types.d.ts.map +1 -0
- package/dist/providers/anthropic/types.js +1 -0
- package/dist/providers/gemini/Capabilities.d.ts +28 -7
- package/dist/providers/gemini/Capabilities.d.ts.map +1 -1
- package/dist/providers/gemini/Capabilities.js +37 -22
- package/dist/providers/gemini/Chat.d.ts +1 -0
- package/dist/providers/gemini/Chat.d.ts.map +1 -1
- package/dist/providers/gemini/Chat.js +40 -3
- package/dist/providers/gemini/GeminiProvider.d.ts +2 -1
- package/dist/providers/gemini/GeminiProvider.d.ts.map +1 -1
- package/dist/providers/gemini/GeminiProvider.js +3 -0
- package/dist/providers/gemini/Models.d.ts +1 -0
- package/dist/providers/gemini/Models.d.ts.map +1 -1
- package/dist/providers/gemini/Models.js +46 -26
- package/dist/providers/gemini/Streaming.d.ts +1 -0
- package/dist/providers/gemini/Streaming.d.ts.map +1 -1
- package/dist/providers/gemini/Streaming.js +34 -4
- package/dist/providers/openai/Capabilities.d.ts +3 -11
- package/dist/providers/openai/Capabilities.d.ts.map +1 -1
- package/dist/providers/openai/Capabilities.js +119 -122
- package/dist/providers/openai/Chat.d.ts.map +1 -1
- package/dist/providers/openai/Chat.js +19 -17
- package/dist/providers/openai/Embedding.d.ts.map +1 -1
- package/dist/providers/openai/Embedding.js +2 -1
- package/dist/providers/openai/Image.d.ts.map +1 -1
- package/dist/providers/openai/Image.js +2 -1
- package/dist/providers/openai/ModelDefinitions.d.ts +1 -24
- package/dist/providers/openai/ModelDefinitions.d.ts.map +1 -1
- package/dist/providers/openai/ModelDefinitions.js +1 -211
- package/dist/providers/openai/Models.d.ts +1 -0
- package/dist/providers/openai/Models.d.ts.map +1 -1
- package/dist/providers/openai/Models.js +46 -22
- package/dist/providers/openai/Moderation.d.ts.map +1 -1
- package/dist/providers/openai/Moderation.js +2 -1
- package/dist/providers/openai/Streaming.d.ts.map +1 -1
- package/dist/providers/openai/Streaming.js +5 -1
- package/dist/providers/openai/Transcription.d.ts.map +1 -1
- package/dist/providers/openai/Transcription.js +3 -2
- package/dist/providers/openai/index.d.ts.map +1 -1
- package/dist/providers/openai/index.js +2 -1
- package/dist/providers/openai/utils.d.ts +20 -0
- package/dist/providers/openai/utils.d.ts.map +1 -0
- package/dist/providers/openai/utils.js +25 -0
- package/dist/providers/registry.js +1 -1
- package/dist/utils/FileLoader.d.ts.map +1 -1
- package/dist/utils/FileLoader.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export interface Model {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
provider: 'openai' | 'anthropic' | 'gemini';
|
|
5
|
+
family?: string | null;
|
|
6
|
+
created_at?: string | null;
|
|
7
|
+
context_window?: number | null;
|
|
8
|
+
max_output_tokens?: number | null;
|
|
9
|
+
knowledge_cutoff?: string | null;
|
|
10
|
+
modalities: {
|
|
11
|
+
input: string[];
|
|
12
|
+
output: string[];
|
|
13
|
+
};
|
|
14
|
+
capabilities: string[];
|
|
15
|
+
pricing?: {
|
|
16
|
+
text_tokens?: {
|
|
17
|
+
standard?: {
|
|
18
|
+
input_per_million?: number;
|
|
19
|
+
cached_input_per_million?: number;
|
|
20
|
+
output_per_million?: number;
|
|
21
|
+
reasoning_output_per_million?: number;
|
|
22
|
+
};
|
|
23
|
+
batch?: {
|
|
24
|
+
input_per_million?: number;
|
|
25
|
+
output_per_million?: number;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
images?: {
|
|
29
|
+
standard?: {
|
|
30
|
+
input?: number;
|
|
31
|
+
output?: number;
|
|
32
|
+
};
|
|
33
|
+
batch?: {
|
|
34
|
+
input?: number;
|
|
35
|
+
output?: number;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
audio_tokens?: {
|
|
39
|
+
standard?: {
|
|
40
|
+
input_per_million?: number;
|
|
41
|
+
output_per_million?: number;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
metadata?: Record<string, any>;
|
|
46
|
+
}
|
|
47
|
+
export type ProviderName = 'openai' | 'anthropic' | 'gemini';
|
|
48
|
+
export type ModelCapability = 'streaming' | 'function_calling' | 'structured_output' | 'predicted_outputs' | 'distillation' | 'fine_tuning' | 'batch' | 'realtime' | 'image_generation' | 'speech_generation' | 'transcription' | 'translation' | 'citations' | 'reasoning' | 'caching' | 'moderation' | 'json_mode' | 'vision';
|
|
49
|
+
export type ModelModality = 'text' | 'image' | 'audio' | 'pdf' | 'video' | 'file' | 'embeddings' | 'moderation';
|
|
50
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/models/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,KAAK;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,QAAQ,GAAG,WAAW,GAAG,QAAQ,CAAC;IAC5C,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,gBAAgB,CAAC,EAAE,MAAM,GAAG,IAAI,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AAa,GACb,WAAW,GACX,WAAW,GACX,SAAS,GACT,YAAY,GACZ,WAAW,GACX,QAAQ,CAAC;AAEb,MAAM,MAAM,aAAa,GACrB,MAAM,GACN,OAAO,GACP,OAAO,GACP,KAAK,GACL,OAAO,GACP,MAAM,GACN,YAAY,GACZ,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -9,6 +9,7 @@ export interface ChatRequest {
|
|
|
9
9
|
max_tokens?: number;
|
|
10
10
|
response_format?: any;
|
|
11
11
|
headers?: Record<string, string>;
|
|
12
|
+
[key: string]: any;
|
|
12
13
|
}
|
|
13
14
|
export interface ChatChunk {
|
|
14
15
|
content: string;
|
|
@@ -19,6 +20,10 @@ export interface Usage {
|
|
|
19
20
|
output_tokens: number;
|
|
20
21
|
total_tokens: number;
|
|
21
22
|
cached_tokens?: number;
|
|
23
|
+
cache_creation_tokens?: number;
|
|
24
|
+
cost?: number;
|
|
25
|
+
input_cost?: number;
|
|
26
|
+
output_cost?: number;
|
|
22
27
|
}
|
|
23
28
|
export interface ChatResponse {
|
|
24
29
|
content: string | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Provider.d.ts","sourceRoot":"","sources":["../../src/providers/Provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAErE,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,KAAK,CAAC,EAAE,IAAI,EAAE,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,GAAG,CAAC;IACtB,O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AC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IAClD,MAAM,CAAC,CAAC,OAAO,EAAE,WAAW,GAAG,aAAa,CAAC,SAAS,CAAC,CAAC;IACxD,UAAU,CAAC,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC;IACpC,KAAK,CAAC,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACtD,UAAU,CAAC,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAC;IAC3E,QAAQ,CAAC,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;IACnE,KAAK,CAAC,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;IAC9D,YAAY,CAAC,EAAE,oBAAoB,CAAC;CACrC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Provider, ChatRequest, ChatResponse, ModelInfo, ChatChunk, ImageRequest, ImageResponse, ModerationRequest, ModerationResponse, TranscriptionRequest, TranscriptionResponse } from "../Provider.js";
|
|
2
|
+
import { EmbeddingRequest, EmbeddingResponse } from "../Embedding.js";
|
|
3
|
+
export interface AnthropicProviderOptions {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class AnthropicProvider implements Provider {
|
|
8
|
+
private readonly options;
|
|
9
|
+
private readonly baseUrl;
|
|
10
|
+
private readonly chatHandler;
|
|
11
|
+
private readonly streamHandler;
|
|
12
|
+
private readonly modelsHandler;
|
|
13
|
+
capabilities: {
|
|
14
|
+
supportsVision: (model: string) => boolean;
|
|
15
|
+
supportsTools: (model: string) => boolean;
|
|
16
|
+
supportsStructuredOutput: (model: string) => boolean;
|
|
17
|
+
supportsEmbeddings: (_model: string) => boolean;
|
|
18
|
+
supportsImageGeneration: (_model: string) => boolean;
|
|
19
|
+
supportsTranscription: (_model: string) => boolean;
|
|
20
|
+
supportsModeration: (_model: string) => boolean;
|
|
21
|
+
getContextWindow: (model: string) => number | null;
|
|
22
|
+
};
|
|
23
|
+
constructor(options: AnthropicProviderOptions);
|
|
24
|
+
chat(request: ChatRequest): Promise<ChatResponse>;
|
|
25
|
+
stream(request: ChatRequest): AsyncGenerator<ChatChunk>;
|
|
26
|
+
listModels(): Promise<ModelInfo[]>;
|
|
27
|
+
paint(_request: ImageRequest): Promise<ImageResponse>;
|
|
28
|
+
transcribe(_request: TranscriptionRequest): Promise<TranscriptionResponse>;
|
|
29
|
+
moderate(_request: ModerationRequest): Promise<ModerationResponse>;
|
|
30
|
+
embed(_request: EmbeddingRequest): Promise<EmbeddingResponse>;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=AnthropicProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AnthropicProvider.d.ts","sourceRoot":"","sources":["../../../src/providers/anthropic/AnthropicProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAC5M,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAMtE,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,iBAAkB,YAAW,QAAQ;IAiBpC,OAAO,CAAC,QAAQ,CAAC,OAAO;IAhBpC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAgB;IAC5C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAqB;IACnD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAkB;IAEzC,YAAY;gCACO,MAAM;+BACP,MAAM;0CACK,MAAM;qCACX,MAAM;0CACD,MAAM;wCACR,MAAM;qCACT,MAAM;kCACT,MAAM;MAChC;gBAE2B,OAAO,EAAE,wBAAwB;IAOxD,IAAI,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;IAIhD,MAAM,CAAC,OAAO,EAAE,WAAW,GAAG,cAAc,CAAC,SAAS,CAAC;IAIxD,UAAU,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;IAIlC,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;IAIrD,UAAU,CAAC,QAAQ,EAAE,oBAAoB,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAI1E,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAIlE,KAAK,CAAC,QAAQ,EAAE,gBAAgB,GAAG,OAAO,CAAC,iBAAiB,CAAC;CAGpE"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Capabilities } from "./Capabilities.js";
|
|
2
|
+
import { AnthropicChat } from "./Chat.js";
|
|
3
|
+
import { AnthropicStreaming } from "./Streaming.js";
|
|
4
|
+
import { AnthropicModels } from "./Models.js";
|
|
5
|
+
export class AnthropicProvider {
|
|
6
|
+
options;
|
|
7
|
+
baseUrl;
|
|
8
|
+
chatHandler;
|
|
9
|
+
streamHandler;
|
|
10
|
+
modelsHandler;
|
|
11
|
+
capabilities = {
|
|
12
|
+
supportsVision: (model) => Capabilities.supportsVision(model),
|
|
13
|
+
supportsTools: (model) => Capabilities.supportsTools(model),
|
|
14
|
+
supportsStructuredOutput: (model) => Capabilities.supportsJsonMode(model),
|
|
15
|
+
supportsEmbeddings: (_model) => false,
|
|
16
|
+
supportsImageGeneration: (_model) => false,
|
|
17
|
+
supportsTranscription: (_model) => false,
|
|
18
|
+
supportsModeration: (_model) => false,
|
|
19
|
+
getContextWindow: (model) => Capabilities.getContextWindow(model),
|
|
20
|
+
};
|
|
21
|
+
constructor(options) {
|
|
22
|
+
this.options = options;
|
|
23
|
+
this.baseUrl = options.baseUrl ?? "https://api.anthropic.com/v1";
|
|
24
|
+
this.chatHandler = new AnthropicChat(this.baseUrl, options.apiKey);
|
|
25
|
+
this.streamHandler = new AnthropicStreaming(this.baseUrl, options.apiKey);
|
|
26
|
+
this.modelsHandler = new AnthropicModels(this.baseUrl, options.apiKey);
|
|
27
|
+
}
|
|
28
|
+
async chat(request) {
|
|
29
|
+
return this.chatHandler.execute(request);
|
|
30
|
+
}
|
|
31
|
+
async *stream(request) {
|
|
32
|
+
yield* this.streamHandler.execute(request);
|
|
33
|
+
}
|
|
34
|
+
async listModels() {
|
|
35
|
+
return this.modelsHandler.execute();
|
|
36
|
+
}
|
|
37
|
+
async paint(_request) {
|
|
38
|
+
throw new Error("Anthropic doesn't support image generation");
|
|
39
|
+
}
|
|
40
|
+
async transcribe(_request) {
|
|
41
|
+
throw new Error("Anthropic doesn't support transcription");
|
|
42
|
+
}
|
|
43
|
+
async moderate(_request) {
|
|
44
|
+
throw new Error("Anthropic doesn't support moderation");
|
|
45
|
+
}
|
|
46
|
+
async embed(_request) {
|
|
47
|
+
throw new Error("Anthropic doesn't support embeddings");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class Capabilities {
|
|
2
|
+
static getContextWindow(modelId: string): number | null;
|
|
3
|
+
static getMaxOutputTokens(modelId: string): number | null;
|
|
4
|
+
static supportsVision(modelId: string): boolean;
|
|
5
|
+
static supportsTools(modelId: string): boolean;
|
|
6
|
+
static supportsJsonMode(modelId: string): boolean;
|
|
7
|
+
static supportsExtendedThinking(modelId: string): boolean;
|
|
8
|
+
static getCapabilities(modelId: string): string[];
|
|
9
|
+
static getPricing(modelId: string): any;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=Capabilities.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Capabilities.d.ts","sourceRoot":"","sources":["../../../src/providers/anthropic/Capabilities.ts"],"names":[],"mappings":"AAEA,qBAAa,YAAY;IACvB,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAKvD,MAAM,CAAC,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAKzD,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAO/C,MAAM,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAO9C,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAOjD,MAAM,CAAC,wBAAwB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAOzD,MAAM,CAAC,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE;IAsBjD,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG;CAsBxC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { ModelRegistry } from "../../models/ModelRegistry.js";
|
|
2
|
+
export class Capabilities {
|
|
3
|
+
static getContextWindow(modelId) {
|
|
4
|
+
const val = ModelRegistry.getContextWindow(modelId, "anthropic");
|
|
5
|
+
return val ?? 200_000;
|
|
6
|
+
}
|
|
7
|
+
static getMaxOutputTokens(modelId) {
|
|
8
|
+
const val = ModelRegistry.getMaxOutputTokens(modelId, "anthropic");
|
|
9
|
+
return val ?? 4_096;
|
|
10
|
+
}
|
|
11
|
+
static supportsVision(modelId) {
|
|
12
|
+
const model = ModelRegistry.find(modelId, "anthropic");
|
|
13
|
+
if (model?.modalities?.input?.includes("image"))
|
|
14
|
+
return true;
|
|
15
|
+
return /claude-3/.test(modelId);
|
|
16
|
+
}
|
|
17
|
+
static supportsTools(modelId) {
|
|
18
|
+
const model = ModelRegistry.find(modelId, "anthropic");
|
|
19
|
+
if (model?.capabilities?.includes("function_calling"))
|
|
20
|
+
return true;
|
|
21
|
+
return /claude-3/.test(modelId);
|
|
22
|
+
}
|
|
23
|
+
static supportsJsonMode(modelId) {
|
|
24
|
+
const model = ModelRegistry.find(modelId, "anthropic");
|
|
25
|
+
if (model?.capabilities.includes("json_mode"))
|
|
26
|
+
return true;
|
|
27
|
+
return this.supportsTools(modelId);
|
|
28
|
+
}
|
|
29
|
+
static supportsExtendedThinking(modelId) {
|
|
30
|
+
const model = ModelRegistry.find(modelId, "anthropic");
|
|
31
|
+
if (model?.capabilities.includes("reasoning"))
|
|
32
|
+
return true;
|
|
33
|
+
return /claude-3-7/.test(modelId) || /thinking/.test(modelId);
|
|
34
|
+
}
|
|
35
|
+
static getCapabilities(modelId) {
|
|
36
|
+
const caps = ["streaming"];
|
|
37
|
+
const model = ModelRegistry.find(modelId, "anthropic");
|
|
38
|
+
if (model) {
|
|
39
|
+
if (model.capabilities.includes("function_calling"))
|
|
40
|
+
caps.push("function_calling");
|
|
41
|
+
if (model.capabilities.includes("reasoning"))
|
|
42
|
+
caps.push("reasoning");
|
|
43
|
+
if (model.capabilities.includes("json_mode"))
|
|
44
|
+
caps.push("json_mode");
|
|
45
|
+
model.capabilities.forEach(c => {
|
|
46
|
+
if (!caps.includes(c))
|
|
47
|
+
caps.push(c);
|
|
48
|
+
});
|
|
49
|
+
return caps;
|
|
50
|
+
}
|
|
51
|
+
if (this.supportsTools(modelId))
|
|
52
|
+
caps.push("function_calling");
|
|
53
|
+
if (this.supportsExtendedThinking(modelId))
|
|
54
|
+
caps.push("reasoning");
|
|
55
|
+
if (this.supportsJsonMode(modelId))
|
|
56
|
+
caps.push("json_mode");
|
|
57
|
+
if (/claude-3/.test(modelId))
|
|
58
|
+
caps.push("batch");
|
|
59
|
+
return caps;
|
|
60
|
+
}
|
|
61
|
+
static getPricing(modelId) {
|
|
62
|
+
const model = ModelRegistry.find(modelId, "anthropic");
|
|
63
|
+
if (model?.pricing)
|
|
64
|
+
return model.pricing;
|
|
65
|
+
const inputCpm = 3.0;
|
|
66
|
+
const outputCpm = 15.0;
|
|
67
|
+
return {
|
|
68
|
+
text_tokens: {
|
|
69
|
+
standard: {
|
|
70
|
+
input_per_million: inputCpm,
|
|
71
|
+
output_per_million: outputCpm,
|
|
72
|
+
...(this.supportsExtendedThinking(modelId) ? { reasoning_output_per_million: outputCpm * 2.5 } : {})
|
|
73
|
+
},
|
|
74
|
+
batch: {
|
|
75
|
+
input_per_million: inputCpm * 0.5,
|
|
76
|
+
output_per_million: outputCpm * 0.5,
|
|
77
|
+
...(this.supportsExtendedThinking(modelId) ? { reasoning_output_per_million: outputCpm * 1.25 } : {})
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ChatRequest, ChatResponse } from "../Provider.js";
|
|
2
|
+
export declare class AnthropicChat {
|
|
3
|
+
private readonly baseUrl;
|
|
4
|
+
private readonly apiKey;
|
|
5
|
+
constructor(baseUrl: string, apiKey: string);
|
|
6
|
+
execute(request: ChatRequest): Promise<ChatResponse>;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=Chat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Chat.d.ts","sourceRoot":"","sources":["../../../src/providers/anthropic/Chat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAS,MAAM,gBAAgB,CAAC;AAUlE,qBAAa,aAAa;IACZ,OAAO,CAAC,QAAQ,CAAC,OAAO;IAAU,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAxC,OAAO,EAAE,MAAM,EAAmB,MAAM,EAAE,MAAM;IAEvE,OAAO,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;CAwG3D"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Capabilities } from "./Capabilities.js";
|
|
2
|
+
import { handleAnthropicError } from "./Errors.js";
|
|
3
|
+
import { ModelRegistry } from "../../models/ModelRegistry.js";
|
|
4
|
+
import { formatSystemPrompt, formatMessages } from "./Utils.js";
|
|
5
|
+
export class AnthropicChat {
|
|
6
|
+
baseUrl;
|
|
7
|
+
apiKey;
|
|
8
|
+
constructor(baseUrl, apiKey) {
|
|
9
|
+
this.baseUrl = baseUrl;
|
|
10
|
+
this.apiKey = apiKey;
|
|
11
|
+
}
|
|
12
|
+
async execute(request) {
|
|
13
|
+
const model = request.model;
|
|
14
|
+
const maxTokens = request.max_tokens || Capabilities.getMaxOutputTokens(model) || 4096;
|
|
15
|
+
const systemPrompt = formatSystemPrompt(request.messages);
|
|
16
|
+
const messages = formatMessages(request.messages);
|
|
17
|
+
let system = systemPrompt;
|
|
18
|
+
if (request.response_format) {
|
|
19
|
+
let schemaText = "";
|
|
20
|
+
if (request.response_format.type === "json_schema" && request.response_format.json_schema?.schema) {
|
|
21
|
+
schemaText = "\nSchema:\n" + JSON.stringify(request.response_format.json_schema.schema, null, 2);
|
|
22
|
+
}
|
|
23
|
+
const instruction = `CRITICAL: Respond ONLY with a valid JSON object matching the requested schema.${schemaText}\n\nDo not include any other text or explanation.`;
|
|
24
|
+
system = system ? `${system}\n\n${instruction}` : instruction;
|
|
25
|
+
}
|
|
26
|
+
const { model: _model, messages: _messages, tools: _tools, temperature: _temp, max_tokens: _max, response_format: _format, headers: _headers, ...rest } = request;
|
|
27
|
+
const body = {
|
|
28
|
+
model: model,
|
|
29
|
+
messages: messages,
|
|
30
|
+
max_tokens: maxTokens,
|
|
31
|
+
system: system,
|
|
32
|
+
stream: false,
|
|
33
|
+
...rest,
|
|
34
|
+
};
|
|
35
|
+
if (request.temperature !== undefined) {
|
|
36
|
+
body.temperature = request.temperature;
|
|
37
|
+
}
|
|
38
|
+
// Map tools
|
|
39
|
+
if (request.tools && request.tools.length > 0) {
|
|
40
|
+
body.tools = request.tools.map(tool => ({
|
|
41
|
+
name: tool.function.name,
|
|
42
|
+
description: tool.function.description,
|
|
43
|
+
input_schema: tool.function.parameters
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
// Check if any message contains PDF content to add beta header
|
|
47
|
+
const hasPdf = messages.some(msg => Array.isArray(msg.content) && msg.content.some(block => block.type === "document"));
|
|
48
|
+
const headers = {
|
|
49
|
+
"x-api-key": this.apiKey,
|
|
50
|
+
"anthropic-version": "2023-06-01",
|
|
51
|
+
"content-type": "application/json",
|
|
52
|
+
...request.headers,
|
|
53
|
+
};
|
|
54
|
+
if (hasPdf) {
|
|
55
|
+
headers["anthropic-beta"] = "pdfs-2024-09-25";
|
|
56
|
+
}
|
|
57
|
+
const response = await fetch(`${this.baseUrl}/messages`, {
|
|
58
|
+
method: "POST",
|
|
59
|
+
headers: headers,
|
|
60
|
+
body: JSON.stringify(body),
|
|
61
|
+
});
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
await handleAnthropicError(response, model);
|
|
64
|
+
}
|
|
65
|
+
const json = (await response.json());
|
|
66
|
+
const contentBlocks = json.content;
|
|
67
|
+
// Extract text content and tool calls
|
|
68
|
+
let content = null;
|
|
69
|
+
const toolCalls = []; // Using any for ToolCall matching Provider.ts interface
|
|
70
|
+
for (const block of contentBlocks) {
|
|
71
|
+
if (block.type === "text") {
|
|
72
|
+
if (content === null)
|
|
73
|
+
content = "";
|
|
74
|
+
content += block.text;
|
|
75
|
+
}
|
|
76
|
+
else if (block.type === "tool_use") {
|
|
77
|
+
toolCalls.push({
|
|
78
|
+
id: block.id,
|
|
79
|
+
type: "function",
|
|
80
|
+
function: {
|
|
81
|
+
name: block.name,
|
|
82
|
+
arguments: JSON.stringify(block.input)
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const usage = json.usage ? {
|
|
88
|
+
input_tokens: json.usage.input_tokens,
|
|
89
|
+
output_tokens: json.usage.output_tokens,
|
|
90
|
+
total_tokens: json.usage.input_tokens + json.usage.output_tokens,
|
|
91
|
+
cached_tokens: json.usage.cache_read_input_tokens,
|
|
92
|
+
cache_creation_tokens: json.usage.cache_creation_input_tokens,
|
|
93
|
+
} : undefined;
|
|
94
|
+
const calculatedUsage = usage ? ModelRegistry.calculateCost(usage, model, "anthropic") : undefined;
|
|
95
|
+
return { content, usage: calculatedUsage, tool_calls: toolCalls.length > 0 ? toolCalls : undefined };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Errors.d.ts","sourceRoot":"","sources":["../../../src/providers/anthropic/Errors.ts"],"names":[],"mappings":"AASA,wBAAsB,oBAAoB,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,CAsC9F"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { BadRequestError, AuthenticationError, RateLimitError, ServerError, ServiceUnavailableError, APIError } from "../../errors/index.js";
|
|
2
|
+
export async function handleAnthropicError(response, modelId) {
|
|
3
|
+
const status = response.status;
|
|
4
|
+
let body;
|
|
5
|
+
let message = `Anthropic error (${status})`;
|
|
6
|
+
try {
|
|
7
|
+
body = await response.json();
|
|
8
|
+
if (body?.error?.message) {
|
|
9
|
+
message = body.error.message;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
body = await response.text().catch(() => "Unknown error");
|
|
14
|
+
message = `Anthropic error (${status}): ${body}`;
|
|
15
|
+
}
|
|
16
|
+
const provider = "anthropic";
|
|
17
|
+
if (status === 400) {
|
|
18
|
+
throw new BadRequestError(message, body, provider, modelId);
|
|
19
|
+
}
|
|
20
|
+
if (status === 401 || status === 403) {
|
|
21
|
+
throw new AuthenticationError(message, status, body, provider);
|
|
22
|
+
}
|
|
23
|
+
if (status === 429) {
|
|
24
|
+
throw new RateLimitError(message, body, provider, modelId);
|
|
25
|
+
}
|
|
26
|
+
if (status === 502 || status === 503 || status === 529) {
|
|
27
|
+
throw new ServiceUnavailableError(message, status, body, provider, modelId);
|
|
28
|
+
}
|
|
29
|
+
if (status >= 500) {
|
|
30
|
+
throw new ServerError(message, status, body, provider, modelId);
|
|
31
|
+
}
|
|
32
|
+
throw new APIError(message, status, body, provider, modelId);
|
|
33
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ModelInfo } from "../Provider.js";
|
|
2
|
+
export declare class AnthropicModels {
|
|
3
|
+
private readonly baseUrl;
|
|
4
|
+
private readonly apiKey;
|
|
5
|
+
constructor(baseUrl: string, apiKey: string);
|
|
6
|
+
execute(): Promise<ModelInfo[]>;
|
|
7
|
+
find(modelId: string): import("../../models/types.js").Model | undefined;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=Models.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Models.d.ts","sourceRoot":"","sources":["../../../src/providers/anthropic/Models.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAK3C,qBAAa,eAAe;IACd,OAAO,CAAC,QAAQ,CAAC,OAAO;IAAU,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAxC,OAAO,EAAE,MAAM,EAAmB,MAAM,EAAE,MAAM;IAEvE,OAAO,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;IAkDrC,IAAI,CAAC,OAAO,EAAE,MAAM;CAGrB"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Capabilities } from "./Capabilities.js";
|
|
2
|
+
import { ModelRegistry } from "../../models/ModelRegistry.js";
|
|
3
|
+
export class AnthropicModels {
|
|
4
|
+
baseUrl;
|
|
5
|
+
apiKey;
|
|
6
|
+
constructor(baseUrl, apiKey) {
|
|
7
|
+
this.baseUrl = baseUrl;
|
|
8
|
+
this.apiKey = apiKey;
|
|
9
|
+
}
|
|
10
|
+
async execute() {
|
|
11
|
+
try {
|
|
12
|
+
const response = await fetch(`${this.baseUrl}/models`, {
|
|
13
|
+
method: "GET",
|
|
14
|
+
headers: {
|
|
15
|
+
"x-api-key": this.apiKey,
|
|
16
|
+
"anthropic-version": "2023-06-01",
|
|
17
|
+
"content-type": "application/json",
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
if (response.ok) {
|
|
21
|
+
const { data } = await response.json();
|
|
22
|
+
return data.map(m => {
|
|
23
|
+
const modelId = m.id;
|
|
24
|
+
const registryModel = ModelRegistry.find(modelId, "anthropic");
|
|
25
|
+
const info = {
|
|
26
|
+
id: modelId,
|
|
27
|
+
name: registryModel?.name || m.display_name || modelId,
|
|
28
|
+
provider: "anthropic",
|
|
29
|
+
family: registryModel?.family || modelId,
|
|
30
|
+
context_window: registryModel?.context_window || Capabilities.getContextWindow(modelId),
|
|
31
|
+
max_output_tokens: registryModel?.max_output_tokens || Capabilities.getMaxOutputTokens(modelId),
|
|
32
|
+
modalities: registryModel?.modalities || { input: ["text"], output: ["text"] },
|
|
33
|
+
capabilities: Capabilities.getCapabilities(modelId),
|
|
34
|
+
pricing: registryModel?.pricing || Capabilities.getPricing(modelId),
|
|
35
|
+
metadata: {
|
|
36
|
+
...(registryModel?.metadata || {}),
|
|
37
|
+
display_name: m.display_name,
|
|
38
|
+
created_at: m.created_at
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
return info;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (_error) {
|
|
46
|
+
// Fallback
|
|
47
|
+
}
|
|
48
|
+
return ModelRegistry.all()
|
|
49
|
+
.filter(m => m.provider === "anthropic")
|
|
50
|
+
.map(m => ({
|
|
51
|
+
...m,
|
|
52
|
+
capabilities: Capabilities.getCapabilities(m.id)
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
find(modelId) {
|
|
56
|
+
return ModelRegistry.find(modelId, "anthropic");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ChatRequest, ChatChunk } from "../Provider.js";
|
|
2
|
+
export declare class AnthropicStreaming {
|
|
3
|
+
private readonly baseUrl;
|
|
4
|
+
private readonly apiKey;
|
|
5
|
+
constructor(baseUrl: string, apiKey: string);
|
|
6
|
+
execute(request: ChatRequest): AsyncGenerator<ChatChunk>;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=Streaming.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Streaming.d.ts","sourceRoot":"","sources":["../../../src/providers/anthropic/Streaming.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAMxD,qBAAa,kBAAkB;IACjB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAAU,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAxC,OAAO,EAAE,MAAM,EAAmB,MAAM,EAAE,MAAM;IAEtE,OAAO,CAAC,OAAO,EAAE,WAAW,GAAG,cAAc,CAAC,SAAS,CAAC;CA0HhE"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Capabilities } from "./Capabilities.js";
|
|
2
|
+
import { handleAnthropicError } from "./Errors.js";
|
|
3
|
+
import { formatSystemPrompt, formatMessages } from "./Utils.js";
|
|
4
|
+
export class AnthropicStreaming {
|
|
5
|
+
baseUrl;
|
|
6
|
+
apiKey;
|
|
7
|
+
constructor(baseUrl, apiKey) {
|
|
8
|
+
this.baseUrl = baseUrl;
|
|
9
|
+
this.apiKey = apiKey;
|
|
10
|
+
}
|
|
11
|
+
async *execute(request) {
|
|
12
|
+
const model = request.model;
|
|
13
|
+
const maxTokens = request.max_tokens || Capabilities.getMaxOutputTokens(model) || 4096;
|
|
14
|
+
const systemPrompt = formatSystemPrompt(request.messages);
|
|
15
|
+
const messages = formatMessages(request.messages);
|
|
16
|
+
let system = systemPrompt;
|
|
17
|
+
if (request.response_format) {
|
|
18
|
+
let schemaText = "";
|
|
19
|
+
if (request.response_format.type === "json_schema" && request.response_format.json_schema?.schema) {
|
|
20
|
+
schemaText = "\nSchema:\n" + JSON.stringify(request.response_format.json_schema.schema, null, 2);
|
|
21
|
+
}
|
|
22
|
+
const instruction = `CRITICAL: Respond ONLY with a valid JSON object matching the requested schema.${schemaText}\n\nDo not include any other text or explanation.`;
|
|
23
|
+
system = system ? `${system}\n\n${instruction}` : instruction;
|
|
24
|
+
}
|
|
25
|
+
const body = {
|
|
26
|
+
model: model,
|
|
27
|
+
messages: messages,
|
|
28
|
+
max_tokens: maxTokens,
|
|
29
|
+
system: system,
|
|
30
|
+
stream: true,
|
|
31
|
+
};
|
|
32
|
+
if (request.temperature !== undefined) {
|
|
33
|
+
body.temperature = request.temperature;
|
|
34
|
+
}
|
|
35
|
+
if (request.tools && request.tools.length > 0) {
|
|
36
|
+
body.tools = request.tools.map(tool => ({
|
|
37
|
+
name: tool.function.name,
|
|
38
|
+
description: tool.function.description,
|
|
39
|
+
input_schema: tool.function.parameters
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
// Check if any message contains PDF content to add beta header
|
|
43
|
+
const hasPdf = messages.some(msg => Array.isArray(msg.content) && msg.content.some(block => block.type === "document"));
|
|
44
|
+
const headers = {
|
|
45
|
+
"x-api-key": this.apiKey,
|
|
46
|
+
"anthropic-version": "2023-06-01",
|
|
47
|
+
"content-type": "application/json",
|
|
48
|
+
...request.headers,
|
|
49
|
+
};
|
|
50
|
+
if (hasPdf) {
|
|
51
|
+
headers["anthropic-beta"] = "pdfs-2024-09-25";
|
|
52
|
+
}
|
|
53
|
+
const response = await fetch(`${this.baseUrl}/messages`, {
|
|
54
|
+
method: "POST",
|
|
55
|
+
headers: headers,
|
|
56
|
+
body: JSON.stringify(body),
|
|
57
|
+
});
|
|
58
|
+
if (!response.ok) {
|
|
59
|
+
await handleAnthropicError(response, model);
|
|
60
|
+
}
|
|
61
|
+
if (!response.body) {
|
|
62
|
+
throw new Error("No response body for streaming");
|
|
63
|
+
}
|
|
64
|
+
const reader = response.body.getReader();
|
|
65
|
+
const decoder = new TextDecoder();
|
|
66
|
+
let buffer = "";
|
|
67
|
+
while (true) {
|
|
68
|
+
const { value, done } = await reader.read();
|
|
69
|
+
if (done)
|
|
70
|
+
break;
|
|
71
|
+
const chunk = decoder.decode(value, { stream: true });
|
|
72
|
+
buffer += chunk;
|
|
73
|
+
const lines = buffer.split("\n\n");
|
|
74
|
+
buffer = lines.pop() || "";
|
|
75
|
+
for (const line of lines) {
|
|
76
|
+
const trimmed = line.trim();
|
|
77
|
+
if (!trimmed.startsWith("event: "))
|
|
78
|
+
continue;
|
|
79
|
+
// Format is:
|
|
80
|
+
// event: type
|
|
81
|
+
// data: json
|
|
82
|
+
const parts = trimmed.split("\n");
|
|
83
|
+
const eventLine = parts[0];
|
|
84
|
+
const dataLine = parts.find(p => p.startsWith("data: "));
|
|
85
|
+
if (!dataLine || !eventLine)
|
|
86
|
+
continue;
|
|
87
|
+
const eventType = eventLine.replace("event: ", "").trim();
|
|
88
|
+
const dataStr = dataLine.replace("data: ", "").trim();
|
|
89
|
+
try {
|
|
90
|
+
const data = JSON.parse(dataStr);
|
|
91
|
+
// Handle different event types from Anthropic
|
|
92
|
+
if (eventType === "content_block_delta") {
|
|
93
|
+
if (data.delta && data.delta.type === "text_delta") {
|
|
94
|
+
yield { content: data.delta.text };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else if (eventType === "message_start") {
|
|
98
|
+
// Could extract initial usage here
|
|
99
|
+
}
|
|
100
|
+
else if (eventType === "message_delta") {
|
|
101
|
+
// Update usage or stop reason
|
|
102
|
+
}
|
|
103
|
+
else if (eventType === "error") {
|
|
104
|
+
throw new Error(`Stream error: ${data.error?.message}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
// console.warn("Parse error", e);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Message } from "../../chat/Message.js";
|
|
2
|
+
import { AnthropicMessage } from "./types.js";
|
|
3
|
+
export declare function formatSystemPrompt(messages: Message[]): string | undefined;
|
|
4
|
+
export declare function formatMessages(requestMessages: Message[]): AnthropicMessage[];
|
|
5
|
+
//# sourceMappingURL=Utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Utils.d.ts","sourceRoot":"","sources":["../../../src/providers/anthropic/Utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAEhD,OAAO,EAAE,gBAAgB,EAAyB,MAAM,YAAY,CAAC;AAErE,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,MAAM,GAAG,SAAS,CAiB1E;AAED,wBAAgB,cAAc,CAAC,eAAe,EAAE,OAAO,EAAE,GAAG,gBAAgB,EAAE,CAyB7E"}
|