@agentdevjs/llm 0.1.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.
@@ -0,0 +1,224 @@
1
+ import { LLMClient, ThinkingEffort, CustomHeaderEntry, Message, Tool, LLMChatOptions, LLMResponse, AgentConfigFile, ModelConfig } from '@agentdevjs/core';
2
+ export { APIErrorType, ClassifiedAPIError, ConnectionErrorDetails, DEFAULT_MAX_RETRIES, classifyAPIError, classifyAndWrapError, extractConnectionErrorDetails, getRetryDelay, getUserFriendlyMessage, parseRetryAfter, sleep as retrySleep, shouldRetry } from '@agentdevjs/core';
3
+ import OpenAI from 'openai';
4
+
5
+ type AnthropicTextBlock = {
6
+ type: 'text';
7
+ text: string;
8
+ cache_control?: {
9
+ type: 'ephemeral';
10
+ };
11
+ };
12
+ type AnthropicThinkingBlock = {
13
+ type: 'thinking';
14
+ thinking: string;
15
+ signature: string;
16
+ };
17
+ type AnthropicToolResultBlock = {
18
+ type: 'tool_result';
19
+ tool_use_id: string;
20
+ content: string | (AnthropicTextBlock | AnthropicImageBlock)[];
21
+ is_error?: boolean;
22
+ };
23
+ type AnthropicToolUseBlock = {
24
+ type: 'tool_use';
25
+ id: string;
26
+ name: string;
27
+ input?: unknown;
28
+ };
29
+ type AnthropicImageBlock = {
30
+ type: 'image';
31
+ source: {
32
+ type: 'base64';
33
+ media_type: string;
34
+ data: string;
35
+ };
36
+ };
37
+ type AnthropicContentBlock = AnthropicTextBlock | AnthropicThinkingBlock | AnthropicToolResultBlock | AnthropicToolUseBlock | AnthropicImageBlock;
38
+ interface AnthropicRequestMessage {
39
+ role: 'user' | 'assistant';
40
+ content: string | AnthropicContentBlock[];
41
+ }
42
+ interface AnthropicToolDef {
43
+ name: string;
44
+ description: string;
45
+ input_schema: Record<string, unknown>;
46
+ }
47
+ interface CompiledAnthropicRequest {
48
+ system?: AnthropicTextBlock[];
49
+ messages: AnthropicRequestMessage[];
50
+ tools?: AnthropicToolDef[];
51
+ }
52
+ declare class AnthropicLLM implements LLMClient {
53
+ private readonly apiKey;
54
+ private readonly _modelName;
55
+ private readonly baseUrl;
56
+ private readonly maxTokens;
57
+ private readonly thinkingEffort?;
58
+ private readonly _thinkingBudgetTokens?;
59
+ private readonly _thinkingKeepTurns;
60
+ private readonly customHeaders?;
61
+ private readonly visionEnabled;
62
+ private initPromise;
63
+ private maxRetries;
64
+ private deadlineMs;
65
+ /** 返回当前 LLM 实例使用的模型名 */
66
+ get modelName(): string;
67
+ constructor(apiKey: string, _modelName?: string, baseUrl?: string, maxTokens?: number, thinkingEffort?: ThinkingEffort | undefined, _thinkingBudgetTokens?: number | undefined, _thinkingKeepTurns?: number, customHeaders?: CustomHeaderEntry[] | undefined, visionEnabled?: boolean, callPolicy?: {
68
+ maxRetries?: number;
69
+ timeoutMs?: number;
70
+ });
71
+ chat(messages: Message[], tools: Tool[], options?: LLMChatOptions): Promise<LLMResponse>;
72
+ }
73
+ declare function compileContextForAnthropic(messages: Message[], tools: Tool[], visionEnabled?: boolean): CompiledAnthropicRequest;
74
+ declare function createAnthropicLLM(config: AgentConfigFile): AnthropicLLM;
75
+ declare function createAnthropicLLM(modelConfig: ModelConfig): AnthropicLLM;
76
+ declare function createAnthropicLLM(apiKey: string, modelName: string, baseUrl?: string): AnthropicLLM;
77
+
78
+ /**
79
+ * OpenAI LLM 适配器
80
+ * 实现 LLMClient 接口
81
+ */
82
+
83
+ /**
84
+ * 将内部 Message[] 编译为 OpenAI Chat Completions wire 格式。
85
+ * 与 compileContextForAnthropic / compileContextForOpenAIResponses 对称的导出编译函数。
86
+ *
87
+ * system 消息按 source 二分处理(对齐 compileContextForAnthropic):
88
+ * - 无 source(agent 系统提示词及经 context.add 注入的同级文档)→ 合并为
89
+ * 开头恰好一条 system 消息。部分 OpenAI 兼容后端(vLLM Qwen chat template 等)
90
+ * 仅接受一条位于开头的 system,多条返回 400 "System message must be at the beginning."。
91
+ * - 有 source(Feature 注入,如 handoff-seed、partial-compact)→ 包 <reminder>
92
+ * 嵌入下一个 user turn(作为文本前缀);若后续无 user 消息则以独立 user
93
+ * 消息落尾。不插入 assistant/tool 之间,避免破坏 tool_calls 配对。
94
+ */
95
+ declare function compileChatMessages(messages: Message[], visionEnabled?: boolean): OpenAI.Chat.ChatCompletionMessageParam[];
96
+ declare class OpenAILLM implements LLMClient {
97
+ private client;
98
+ private _modelName;
99
+ private maxTokens?;
100
+ private thinkingEffort?;
101
+ private providerOptions?;
102
+ private customHeaders?;
103
+ private visionEnabled;
104
+ private initPromise;
105
+ private maxRetries;
106
+ private deadlineMs;
107
+ /** 返回当前 LLM 实例使用的模型名 */
108
+ get modelName(): string;
109
+ constructor(apiKey: string, modelName?: string, baseUrl?: string, maxTokens?: number, thinkingEffort?: ThinkingEffort, providerOptions?: Record<string, unknown>, customHeaders?: CustomHeaderEntry[], visionEnabled?: boolean, callPolicy?: {
110
+ maxRetries?: number;
111
+ timeoutMs?: number;
112
+ });
113
+ /**
114
+ * 聊天 - 核心方法(内部使用流式处理,带重试)
115
+ */
116
+ chat(messages: Message[], tools: Tool[], options?: {
117
+ signal?: AbortSignal;
118
+ }): Promise<LLMResponse>;
119
+ }
120
+
121
+ /**
122
+ * 从配置创建 OpenAI LLM 实例
123
+ *
124
+ * @example
125
+ * // 方式1:传入配置文件对象(推荐)
126
+ * const llm = createOpenAILLM(config);
127
+ *
128
+ * @example
129
+ * // 方式2:传入模型配置
130
+ * const llm = createOpenAILLM(config.defaultModel);
131
+ *
132
+ * @example
133
+ * // 方式3:单独传参
134
+ * const llm = createOpenAILLM(apiKey, 'gpt-4o', baseUrl);
135
+ *
136
+ * @example
137
+ * // 方式4:自定义配置
138
+ * const llm = createOpenAILLM({ apiKey: 'xxx', model: 'gpt-4o' });
139
+ */
140
+ declare function createOpenAILLM(config: AgentConfigFile): OpenAILLM;
141
+ declare function createOpenAILLM(modelConfig: ModelConfig): OpenAILLM;
142
+ declare function createOpenAILLM(apiKey: string, modelName: string, baseUrl?: string): OpenAILLM;
143
+
144
+ /**
145
+ * OpenAI Responses LLM 适配器
146
+ *
147
+ * 只负责把框架的 Message / Tool 编译为 Responses 输入,
148
+ * 再把 Responses 输出收敛回 LLMResponse。
149
+ */
150
+
151
+ type ResponsesRequest = {
152
+ model: string;
153
+ instructions?: string;
154
+ input: any[];
155
+ tools?: Array<Record<string, unknown>>;
156
+ tool_choice?: 'auto';
157
+ max_output_tokens?: number;
158
+ parallel_tool_calls?: boolean;
159
+ reasoning?: Record<string, unknown>;
160
+ previous_response_id?: string;
161
+ store?: boolean;
162
+ text?: Record<string, unknown>;
163
+ include?: Array<string>;
164
+ [key: string]: unknown;
165
+ };
166
+ type OpenAIResponsesProfile = 'standard' | 'codex';
167
+ declare class OpenAIResponsesLLM implements LLMClient {
168
+ private client;
169
+ private _modelName;
170
+ private maxTokens?;
171
+ private thinkingEffort?;
172
+ private thinkingBudgetTokens?;
173
+ private providerOptions?;
174
+ private customHeaders?;
175
+ private visionEnabled;
176
+ private responsesProfile;
177
+ private initPromise;
178
+ private maxRetries;
179
+ private deadlineMs;
180
+ /** 返回当前 LLM 实例使用的模型名 */
181
+ get modelName(): string;
182
+ constructor(apiKey: string, modelName?: string, baseUrl?: string, maxTokens?: number, thinkingEffort?: ThinkingEffort, thinkingBudgetTokens?: number, providerOptions?: Record<string, unknown>, customHeaders?: CustomHeaderEntry[], visionEnabled?: boolean, responsesProfile?: OpenAIResponsesProfile, callPolicy?: {
183
+ maxRetries?: number;
184
+ timeoutMs?: number;
185
+ });
186
+ chat(messages: Message[], tools: Tool[], options?: {
187
+ signal?: AbortSignal;
188
+ }): Promise<LLMResponse>;
189
+ private createResponsesCompletion;
190
+ }
191
+ interface CompileOpenAIResponsesOptions {
192
+ modelName?: string;
193
+ maxTokens?: number;
194
+ thinkingEffort?: ThinkingEffort;
195
+ thinkingBudgetTokens?: number;
196
+ providerOptions?: Record<string, unknown>;
197
+ visionEnabled?: boolean;
198
+ responsesProfile?: OpenAIResponsesProfile;
199
+ }
200
+ declare function compileContextForOpenAIResponses(messages: Message[], tools: Tool[], options?: CompileOpenAIResponsesOptions): ResponsesRequest;
201
+ declare function createOpenAIResponsesLLM(config: AgentConfigFile): OpenAIResponsesLLM;
202
+ declare function createOpenAIResponsesLLM(modelConfig: ModelConfig): OpenAIResponsesLLM;
203
+ declare function createOpenAIResponsesLLM(apiKey: string, modelName: string, baseUrl?: string): OpenAIResponsesLLM;
204
+
205
+ /**
206
+ * 获取当前的 undici Dispatcher
207
+ */
208
+ declare function getGlobalDispatcher(): any;
209
+ /**
210
+ * 初始化 HTTP 客户端
211
+ *
212
+ * 设置 Undici 全局调度器(同步完成):
213
+ * - 有代理 → EnvHttpProxyAgent(自动处理 NO_PROXY)
214
+ * - 无代理 → Agent(keep-alive + 连接池,内置 DNS 缓存)
215
+ *
216
+ * 此函数幂等,多次调用无副作用。
217
+ */
218
+ declare function initHttpClient(): Promise<void>;
219
+
220
+ declare function createLLM(config: AgentConfigFile): LLMClient;
221
+ declare function createLLM(modelConfig: ModelConfig): LLMClient;
222
+ declare function createLLM(apiKey: string, modelName: string, provider?: string, baseUrl?: string): LLMClient;
223
+
224
+ export { AnthropicLLM, OpenAILLM, OpenAIResponsesLLM, compileChatMessages, compileContextForAnthropic, compileContextForOpenAIResponses, createAnthropicLLM, createLLM, createOpenAILLM, createOpenAIResponsesLLM, getGlobalDispatcher, initHttpClient };