opc-agent 1.4.0 → 2.0.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.
Files changed (58) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +91 -32
  3. package/dist/channels/telegram.d.ts +30 -9
  4. package/dist/channels/telegram.js +125 -33
  5. package/dist/cli.js +415 -8
  6. package/dist/core/agent.d.ts +23 -0
  7. package/dist/core/agent.js +120 -3
  8. package/dist/core/runtime.d.ts +1 -0
  9. package/dist/core/runtime.js +44 -0
  10. package/dist/core/scheduler.d.ts +52 -0
  11. package/dist/core/scheduler.js +168 -0
  12. package/dist/core/subagent.d.ts +28 -0
  13. package/dist/core/subagent.js +65 -0
  14. package/dist/daemon.d.ts +3 -0
  15. package/dist/daemon.js +134 -0
  16. package/dist/index.d.ts +7 -0
  17. package/dist/index.js +17 -1
  18. package/dist/providers/index.d.ts +5 -1
  19. package/dist/providers/index.js +16 -9
  20. package/dist/schema/oad.d.ts +179 -4
  21. package/dist/schema/oad.js +12 -1
  22. package/dist/skills/auto-learn.d.ts +28 -0
  23. package/dist/skills/auto-learn.js +257 -0
  24. package/dist/tools/builtin/datetime.d.ts +3 -0
  25. package/dist/tools/builtin/datetime.js +44 -0
  26. package/dist/tools/builtin/file.d.ts +3 -0
  27. package/dist/tools/builtin/file.js +151 -0
  28. package/dist/tools/builtin/index.d.ts +15 -0
  29. package/dist/tools/builtin/index.js +30 -0
  30. package/dist/tools/builtin/shell.d.ts +3 -0
  31. package/dist/tools/builtin/shell.js +43 -0
  32. package/dist/tools/builtin/web.d.ts +3 -0
  33. package/dist/tools/builtin/web.js +37 -0
  34. package/dist/tools/mcp-client.d.ts +24 -0
  35. package/dist/tools/mcp-client.js +119 -0
  36. package/package.json +1 -1
  37. package/src/channels/telegram.ts +212 -90
  38. package/src/cli.ts +418 -8
  39. package/src/core/agent.ts +295 -152
  40. package/src/core/runtime.ts +47 -0
  41. package/src/core/scheduler.ts +187 -0
  42. package/src/core/subagent.ts +98 -0
  43. package/src/daemon.ts +96 -0
  44. package/src/index.ts +11 -0
  45. package/src/providers/index.ts +354 -339
  46. package/src/schema/oad.ts +167 -154
  47. package/src/skills/auto-learn.ts +262 -0
  48. package/src/tools/builtin/datetime.ts +41 -0
  49. package/src/tools/builtin/file.ts +107 -0
  50. package/src/tools/builtin/index.ts +28 -0
  51. package/src/tools/builtin/shell.ts +43 -0
  52. package/src/tools/builtin/web.ts +35 -0
  53. package/src/tools/mcp-client.ts +131 -0
  54. package/tests/auto-learn.test.ts +105 -0
  55. package/tests/builtin-tools.test.ts +83 -0
  56. package/tests/cli.test.ts +46 -0
  57. package/tests/subagent.test.ts +130 -0
  58. package/tests/telegram-discord.test.ts +60 -0
@@ -1,339 +1,354 @@
1
- import type { Message } from '../core/types';
2
- import * as https from 'https';
3
- import * as http from 'http';
4
-
5
- export interface LLMProvider {
6
- name: string;
7
- chat(messages: Message[], systemPrompt?: string): Promise<string>;
8
- chatStream(messages: Message[], systemPrompt?: string): AsyncIterable<string>;
9
- }
10
-
11
- interface OpenAIMessage {
12
- role: 'system' | 'user' | 'assistant';
13
- content: string;
14
- }
15
-
16
- function getApiKey(): string {
17
- return process.env.OPC_LLM_API_KEY || process.env.OPENAI_API_KEY || '';
18
- }
19
-
20
- function getBaseUrl(): string {
21
- return process.env.OPC_LLM_BASE_URL || 'https://api.openai.com/v1';
22
- }
23
-
24
- class OpenAICompatibleProvider implements LLMProvider {
25
- name: string;
26
- private model: string;
27
- private baseUrl: string;
28
- private apiKey: string;
29
-
30
- constructor(name: string, model: string, baseUrl?: string, apiKey?: string) {
31
- this.name = name;
32
- this.model = model;
33
- this.baseUrl = baseUrl || getBaseUrl();
34
- this.apiKey = apiKey || getApiKey();
35
- }
36
-
37
- private formatMessages(messages: Message[], systemPrompt?: string): OpenAIMessage[] {
38
- const formatted: OpenAIMessage[] = [];
39
- if (systemPrompt) {
40
- formatted.push({ role: 'system', content: systemPrompt });
41
- }
42
- for (const m of messages) {
43
- formatted.push({ role: m.role as 'user' | 'assistant', content: m.content });
44
- }
45
- return formatted;
46
- }
47
-
48
- private async request(body: any): Promise<any> {
49
- if (!this.apiKey) {
50
- throw new Error('No API key configured. Set OPC_LLM_API_KEY or OPENAI_API_KEY environment variable.');
51
- }
52
-
53
- const url = new URL(`${this.baseUrl}/chat/completions`);
54
- const isGemini = url.hostname.includes('googleapis.com');
55
- if (isGemini) {
56
- url.searchParams.set('key', this.apiKey);
57
- }
58
- const isHttps = url.protocol === 'https:';
59
- const lib = isHttps ? https : http;
60
-
61
- const postData = JSON.stringify(body);
62
-
63
- const headers: Record<string, string> = {
64
- 'Content-Type': 'application/json',
65
- 'Content-Length': String(Buffer.byteLength(postData)),
66
- };
67
- if (!isGemini) {
68
- headers['Authorization'] = `Bearer ${this.apiKey}`;
69
- }
70
-
71
- return new Promise((resolve, reject) => {
72
- const req = lib.request(
73
- {
74
- hostname: url.hostname,
75
- port: url.port || (isHttps ? 443 : 80),
76
- path: url.pathname + url.search,
77
- method: 'POST',
78
- headers,
79
- },
80
- (res) => {
81
- let data = '';
82
- res.on('data', (chunk: Buffer) => (data += chunk.toString()));
83
- res.on('end', () => {
84
- if (res.statusCode && res.statusCode >= 400) {
85
- reject(new Error(`LLM API error ${res.statusCode}: ${data}`));
86
- return;
87
- }
88
- try {
89
- resolve(JSON.parse(data));
90
- } catch {
91
- reject(new Error(`Invalid JSON response: ${data.slice(0, 200)}`));
92
- }
93
- });
94
- },
95
- );
96
- req.on('error', reject);
97
- req.write(postData);
98
- req.end();
99
- });
100
- }
101
-
102
- async chat(messages: Message[], systemPrompt?: string): Promise<string> {
103
- if (!this.apiKey) {
104
- // Stub mode when no API key
105
- const last = messages[messages.length - 1];
106
- return `[${this.name}/${this.model} - no API key] Echo: ${last?.content ?? ''}`;
107
- }
108
- const formatted = this.formatMessages(messages, systemPrompt);
109
- const result = await this.request({
110
- model: this.model,
111
- messages: formatted,
112
- temperature: 0.7,
113
- max_tokens: 2048,
114
- });
115
- return result.choices?.[0]?.message?.content ?? '';
116
- }
117
-
118
- async *chatStream(messages: Message[], systemPrompt?: string): AsyncIterable<string> {
119
- if (!this.apiKey) {
120
- const last = messages[messages.length - 1];
121
- yield `[${this.name}/${this.model} - no API key] Echo: ${last?.content ?? ''}`;
122
- return;
123
- }
124
-
125
- const formatted = this.formatMessages(messages, systemPrompt);
126
- const url = new URL(`${this.baseUrl}/chat/completions`);
127
- const isGemini = url.hostname.includes('googleapis.com');
128
- if (isGemini) {
129
- url.searchParams.set('key', this.apiKey);
130
- }
131
- const isHttps = url.protocol === 'https:';
132
- const lib = isHttps ? https : http;
133
- const postData = JSON.stringify({
134
- model: this.model,
135
- messages: formatted,
136
- temperature: 0.7,
137
- max_tokens: 2048,
138
- stream: true,
139
- });
140
-
141
- const streamHeaders: Record<string, string> = {
142
- 'Content-Type': 'application/json',
143
- 'Content-Length': String(Buffer.byteLength(postData)),
144
- };
145
- if (!isGemini) {
146
- streamHeaders['Authorization'] = `Bearer ${this.apiKey}`;
147
- }
148
-
149
- const response = await new Promise<http.IncomingMessage>((resolve, reject) => {
150
- const req = lib.request(
151
- {
152
- hostname: url.hostname,
153
- port: url.port || (isHttps ? 443 : 80),
154
- path: url.pathname + url.search,
155
- method: 'POST',
156
- headers: streamHeaders,
157
- },
158
- resolve,
159
- );
160
- req.on('error', reject);
161
- req.write(postData);
162
- req.end();
163
- });
164
-
165
- if (response.statusCode && response.statusCode >= 400) {
166
- let data = '';
167
- for await (const chunk of response) data += chunk.toString();
168
- throw new Error(`LLM API error ${response.statusCode}: ${data}`);
169
- }
170
-
171
- let buffer = '';
172
- for await (const chunk of response) {
173
- buffer += chunk.toString();
174
- const lines = buffer.split('\n');
175
- buffer = lines.pop() ?? '';
176
-
177
- for (const line of lines) {
178
- const trimmed = line.trim();
179
- if (!trimmed || !trimmed.startsWith('data: ')) continue;
180
- const data = trimmed.slice(6);
181
- if (data === '[DONE]') return;
182
- try {
183
- const parsed = JSON.parse(data);
184
- const content = parsed.choices?.[0]?.delta?.content;
185
- if (content) yield content;
186
- } catch {
187
- // skip malformed lines
188
- }
189
- }
190
- }
191
- }
192
- }
193
-
194
- class GeminiNativeProvider implements LLMProvider {
195
- name = 'gemini';
196
- private model: string;
197
- private apiKey: string;
198
-
199
- constructor(model: string, apiKey?: string) {
200
- this.model = model;
201
- this.apiKey = apiKey || getApiKey();
202
- }
203
-
204
- private buildUrl(stream: boolean): string {
205
- const action = stream ? 'streamGenerateContent?alt=sse&' : 'generateContent?';
206
- return `https://generativelanguage.googleapis.com/v1beta/models/${this.model}:${action}key=${this.apiKey}`;
207
- }
208
-
209
- private formatContents(messages: Message[], systemPrompt?: string): { contents: any[]; systemInstruction?: any } {
210
- const contents: any[] = [];
211
- for (const m of messages) {
212
- contents.push({ role: m.role === 'assistant' ? 'model' : 'user', parts: [{ text: m.content }] });
213
- }
214
- const result: any = { contents };
215
- if (systemPrompt) {
216
- result.systemInstruction = { parts: [{ text: systemPrompt }] };
217
- }
218
- return result;
219
- }
220
-
221
- async chat(messages: Message[], systemPrompt?: string): Promise<string> {
222
- if (!this.apiKey) {
223
- const last = messages[messages.length - 1];
224
- return `[gemini/${this.model} - no API key] Echo: ${last?.content ?? ''}`;
225
- }
226
- const body = this.formatContents(messages, systemPrompt);
227
- const url = this.buildUrl(false);
228
- const postData = JSON.stringify(body);
229
-
230
- return new Promise((resolve, reject) => {
231
- const parsedUrl = new URL(url);
232
- const req = https.request({
233
- hostname: parsedUrl.hostname,
234
- path: parsedUrl.pathname + parsedUrl.search,
235
- method: 'POST',
236
- headers: { 'Content-Type': 'application/json', 'Content-Length': String(Buffer.byteLength(postData)) },
237
- }, (res) => {
238
- let data = '';
239
- res.on('data', (chunk: Buffer) => (data += chunk.toString()));
240
- res.on('end', () => {
241
- if (res.statusCode && res.statusCode >= 400) { reject(new Error(`Gemini API error ${res.statusCode}: ${data}`)); return; }
242
- try {
243
- const parsed = JSON.parse(data);
244
- resolve(parsed.candidates?.[0]?.content?.parts?.[0]?.text ?? '');
245
- } catch { reject(new Error(`Invalid Gemini response: ${data.slice(0, 200)}`)); }
246
- });
247
- });
248
- req.on('error', reject);
249
- req.write(postData);
250
- req.end();
251
- });
252
- }
253
-
254
- async *chatStream(messages: Message[], systemPrompt?: string): AsyncIterable<string> {
255
- if (!this.apiKey) {
256
- const last = messages[messages.length - 1];
257
- yield `[gemini/${this.model} - no API key] Echo: ${last?.content ?? ''}`;
258
- return;
259
- }
260
- const body = this.formatContents(messages, systemPrompt);
261
- const url = this.buildUrl(true);
262
- const postData = JSON.stringify(body);
263
- const parsedUrl = new URL(url);
264
-
265
- const response = await new Promise<http.IncomingMessage>((resolve, reject) => {
266
- const req = https.request({
267
- hostname: parsedUrl.hostname,
268
- path: parsedUrl.pathname + parsedUrl.search,
269
- method: 'POST',
270
- headers: { 'Content-Type': 'application/json', 'Content-Length': String(Buffer.byteLength(postData)) },
271
- }, resolve);
272
- req.on('error', reject);
273
- req.write(postData);
274
- req.end();
275
- });
276
-
277
- if (response.statusCode && response.statusCode >= 400) {
278
- let data = '';
279
- for await (const chunk of response) data += chunk.toString();
280
- throw new Error(`Gemini API error ${response.statusCode}: ${data}`);
281
- }
282
-
283
- let buffer = '';
284
- for await (const chunk of response) {
285
- buffer += chunk.toString();
286
- const lines = buffer.split('\n');
287
- buffer = lines.pop() ?? '';
288
- for (const line of lines) {
289
- const trimmed = line.trim();
290
- if (!trimmed.startsWith('data: ')) continue;
291
- const data = trimmed.slice(6);
292
- if (data === '[DONE]') return;
293
- try {
294
- const parsed = JSON.parse(data);
295
- const text = parsed.candidates?.[0]?.content?.parts?.[0]?.text;
296
- if (text) yield text;
297
- } catch {}
298
- }
299
- }
300
- }
301
- }
302
-
303
- function isGeminiNative(): boolean {
304
- const baseUrl = process.env.OPC_LLM_BASE_URL || '';
305
- const key = getApiKey();
306
- // Use native Gemini API when: key starts with AQ. (new format) OR base URL points to googleapis
307
- return key.startsWith('AQ.') || (baseUrl.includes('googleapis.com') && !baseUrl.includes('/openai'));
308
- }
309
-
310
- export function createProvider(name: string = 'openai', model?: string, baseUrl?: string, apiKey?: string): LLMProvider {
311
- const finalModel = model || process.env.OPC_LLM_MODEL || 'gpt-4o-mini';
312
-
313
- // Auto-detect ollama: use localhost:11434/v1 and dummy apiKey
314
- if (name === 'ollama') {
315
- const ollamaBase = baseUrl || process.env.OPC_LLM_BASE_URL || 'http://localhost:11434/v1';
316
- const ollamaKey = apiKey || process.env.OPC_LLM_API_KEY || 'ollama';
317
- return new OpenAICompatibleProvider('ollama', finalModel, ollamaBase, ollamaKey);
318
- }
319
-
320
- const finalKey = apiKey || getApiKey();
321
- const finalBaseUrl = baseUrl || getBaseUrl();
322
-
323
- // Auto-detect Gemini native when key is new format or base URL points to googleapis
324
- if (finalKey.startsWith('AQ.') || isGeminiNative()) {
325
- return new GeminiNativeProvider(finalModel, finalKey);
326
- }
327
-
328
- // Auto-detect provider name from base URL
329
- let resolvedName = name;
330
- if (finalBaseUrl.includes('deepseek.com')) {
331
- resolvedName = 'deepseek';
332
- } else if (finalBaseUrl.includes('dashscope.aliyuncs.com')) {
333
- resolvedName = 'qwen';
334
- }
335
-
336
- return new OpenAICompatibleProvider(resolvedName, finalModel, baseUrl, apiKey);
337
- }
338
-
339
- export const SUPPORTED_PROVIDERS = ['openai', 'ollama', 'deepseek', 'qwen', 'gemini', 'dashscope', 'zhipu', 'moonshot'] as const;
1
+ import type { Message } from '../core/types';
2
+ import type { MCPToolDefinition } from '../tools/mcp';
3
+ import * as https from 'https';
4
+ import * as http from 'http';
5
+
6
+ export interface ChatOptions {
7
+ tools?: MCPToolDefinition[];
8
+ }
9
+
10
+ export interface LLMProvider {
11
+ name: string;
12
+ chat(messages: Message[], systemPrompt?: string, options?: ChatOptions): Promise<string>;
13
+ chatStream(messages: Message[], systemPrompt?: string): AsyncIterable<string>;
14
+ }
15
+
16
+ interface OpenAIMessage {
17
+ role: 'system' | 'user' | 'assistant';
18
+ content: string;
19
+ }
20
+
21
+ function getApiKey(): string {
22
+ return process.env.OPC_LLM_API_KEY || process.env.OPENAI_API_KEY || '';
23
+ }
24
+
25
+ function getBaseUrl(): string {
26
+ return process.env.OPC_LLM_BASE_URL || 'https://api.openai.com/v1';
27
+ }
28
+
29
+ function buildToolPrompt(tools: MCPToolDefinition[]): string {
30
+ const toolsDesc = tools.map(t =>
31
+ `- ${t.name}: ${t.description}\n Input schema: ${JSON.stringify(t.inputSchema)}`
32
+ ).join('\n');
33
+ return `\n\nYou have access to the following tools. To use a tool, respond with ONLY a JSON object in this format:\n<tool_call>{"name": "tool_name", "arguments": {...}}</tool_call>\n\nAvailable tools:\n${toolsDesc}\n\nIf you don't need a tool, respond normally with text.`;
34
+ }
35
+
36
+ class OpenAICompatibleProvider implements LLMProvider {
37
+ name: string;
38
+ private model: string;
39
+ private baseUrl: string;
40
+ private apiKey: string;
41
+
42
+ constructor(name: string, model: string, baseUrl?: string, apiKey?: string) {
43
+ this.name = name;
44
+ this.model = model;
45
+ this.baseUrl = baseUrl || getBaseUrl();
46
+ this.apiKey = apiKey || getApiKey();
47
+ }
48
+
49
+ private formatMessages(messages: Message[], systemPrompt?: string): OpenAIMessage[] {
50
+ const formatted: OpenAIMessage[] = [];
51
+ if (systemPrompt) {
52
+ formatted.push({ role: 'system', content: systemPrompt });
53
+ }
54
+ for (const m of messages) {
55
+ formatted.push({ role: m.role as 'user' | 'assistant', content: m.content });
56
+ }
57
+ return formatted;
58
+ }
59
+
60
+ private async request(body: any): Promise<any> {
61
+ if (!this.apiKey) {
62
+ throw new Error('No API key configured. Set OPC_LLM_API_KEY or OPENAI_API_KEY environment variable.');
63
+ }
64
+
65
+ const url = new URL(`${this.baseUrl}/chat/completions`);
66
+ const isGemini = url.hostname.includes('googleapis.com');
67
+ if (isGemini) {
68
+ url.searchParams.set('key', this.apiKey);
69
+ }
70
+ const isHttps = url.protocol === 'https:';
71
+ const lib = isHttps ? https : http;
72
+
73
+ const postData = JSON.stringify(body);
74
+
75
+ const headers: Record<string, string> = {
76
+ 'Content-Type': 'application/json',
77
+ 'Content-Length': String(Buffer.byteLength(postData)),
78
+ };
79
+ if (!isGemini) {
80
+ headers['Authorization'] = `Bearer ${this.apiKey}`;
81
+ }
82
+
83
+ return new Promise((resolve, reject) => {
84
+ const req = lib.request(
85
+ {
86
+ hostname: url.hostname,
87
+ port: url.port || (isHttps ? 443 : 80),
88
+ path: url.pathname + url.search,
89
+ method: 'POST',
90
+ headers,
91
+ },
92
+ (res) => {
93
+ let data = '';
94
+ res.on('data', (chunk: Buffer) => (data += chunk.toString()));
95
+ res.on('end', () => {
96
+ if (res.statusCode && res.statusCode >= 400) {
97
+ reject(new Error(`LLM API error ${res.statusCode}: ${data}`));
98
+ return;
99
+ }
100
+ try {
101
+ resolve(JSON.parse(data));
102
+ } catch {
103
+ reject(new Error(`Invalid JSON response: ${data.slice(0, 200)}`));
104
+ }
105
+ });
106
+ },
107
+ );
108
+ req.on('error', reject);
109
+ req.write(postData);
110
+ req.end();
111
+ });
112
+ }
113
+
114
+ async chat(messages: Message[], systemPrompt?: string, options?: ChatOptions): Promise<string> {
115
+ if (!this.apiKey) {
116
+ const last = messages[messages.length - 1];
117
+ return `[${this.name}/${this.model} - no API key] Echo: ${last?.content ?? ''}`;
118
+ }
119
+ let effectivePrompt = systemPrompt;
120
+ if (options?.tools && options.tools.length > 0) {
121
+ effectivePrompt = (systemPrompt || '') + buildToolPrompt(options.tools);
122
+ }
123
+ const formatted = this.formatMessages(messages, effectivePrompt);
124
+ const result = await this.request({
125
+ model: this.model,
126
+ messages: formatted,
127
+ temperature: 0.7,
128
+ max_tokens: 2048,
129
+ });
130
+ return result.choices?.[0]?.message?.content ?? '';
131
+ }
132
+
133
+ async *chatStream(messages: Message[], systemPrompt?: string): AsyncIterable<string> {
134
+ if (!this.apiKey) {
135
+ const last = messages[messages.length - 1];
136
+ yield `[${this.name}/${this.model} - no API key] Echo: ${last?.content ?? ''}`;
137
+ return;
138
+ }
139
+
140
+ const formatted = this.formatMessages(messages, systemPrompt);
141
+ const url = new URL(`${this.baseUrl}/chat/completions`);
142
+ const isGemini = url.hostname.includes('googleapis.com');
143
+ if (isGemini) {
144
+ url.searchParams.set('key', this.apiKey);
145
+ }
146
+ const isHttps = url.protocol === 'https:';
147
+ const lib = isHttps ? https : http;
148
+ const postData = JSON.stringify({
149
+ model: this.model,
150
+ messages: formatted,
151
+ temperature: 0.7,
152
+ max_tokens: 2048,
153
+ stream: true,
154
+ });
155
+
156
+ const streamHeaders: Record<string, string> = {
157
+ 'Content-Type': 'application/json',
158
+ 'Content-Length': String(Buffer.byteLength(postData)),
159
+ };
160
+ if (!isGemini) {
161
+ streamHeaders['Authorization'] = `Bearer ${this.apiKey}`;
162
+ }
163
+
164
+ const response = await new Promise<http.IncomingMessage>((resolve, reject) => {
165
+ const req = lib.request(
166
+ {
167
+ hostname: url.hostname,
168
+ port: url.port || (isHttps ? 443 : 80),
169
+ path: url.pathname + url.search,
170
+ method: 'POST',
171
+ headers: streamHeaders,
172
+ },
173
+ resolve,
174
+ );
175
+ req.on('error', reject);
176
+ req.write(postData);
177
+ req.end();
178
+ });
179
+
180
+ if (response.statusCode && response.statusCode >= 400) {
181
+ let data = '';
182
+ for await (const chunk of response) data += chunk.toString();
183
+ throw new Error(`LLM API error ${response.statusCode}: ${data}`);
184
+ }
185
+
186
+ let buffer = '';
187
+ for await (const chunk of response) {
188
+ buffer += chunk.toString();
189
+ const lines = buffer.split('\n');
190
+ buffer = lines.pop() ?? '';
191
+
192
+ for (const line of lines) {
193
+ const trimmed = line.trim();
194
+ if (!trimmed || !trimmed.startsWith('data: ')) continue;
195
+ const data = trimmed.slice(6);
196
+ if (data === '[DONE]') return;
197
+ try {
198
+ const parsed = JSON.parse(data);
199
+ const content = parsed.choices?.[0]?.delta?.content;
200
+ if (content) yield content;
201
+ } catch {
202
+ // skip malformed lines
203
+ }
204
+ }
205
+ }
206
+ }
207
+ }
208
+
209
+ class GeminiNativeProvider implements LLMProvider {
210
+ name = 'gemini';
211
+ private model: string;
212
+ private apiKey: string;
213
+
214
+ constructor(model: string, apiKey?: string) {
215
+ this.model = model;
216
+ this.apiKey = apiKey || getApiKey();
217
+ }
218
+
219
+ private buildUrl(stream: boolean): string {
220
+ const action = stream ? 'streamGenerateContent?alt=sse&' : 'generateContent?';
221
+ return `https://generativelanguage.googleapis.com/v1beta/models/${this.model}:${action}key=${this.apiKey}`;
222
+ }
223
+
224
+ private formatContents(messages: Message[], systemPrompt?: string): { contents: any[]; systemInstruction?: any } {
225
+ const contents: any[] = [];
226
+ for (const m of messages) {
227
+ contents.push({ role: m.role === 'assistant' ? 'model' : 'user', parts: [{ text: m.content }] });
228
+ }
229
+ const result: any = { contents };
230
+ if (systemPrompt) {
231
+ result.systemInstruction = { parts: [{ text: systemPrompt }] };
232
+ }
233
+ return result;
234
+ }
235
+
236
+ async chat(messages: Message[], systemPrompt?: string, options?: ChatOptions): Promise<string> {
237
+ if (!this.apiKey) {
238
+ const last = messages[messages.length - 1];
239
+ return `[gemini/${this.model} - no API key] Echo: ${last?.content ?? ''}`;
240
+ }
241
+ let effectivePrompt = systemPrompt;
242
+ if (options?.tools && options.tools.length > 0) {
243
+ effectivePrompt = (systemPrompt || '') + buildToolPrompt(options.tools);
244
+ }
245
+ const body = this.formatContents(messages, effectivePrompt);
246
+ const url = this.buildUrl(false);
247
+ const postData = JSON.stringify(body);
248
+
249
+ return new Promise((resolve, reject) => {
250
+ const parsedUrl = new URL(url);
251
+ const req = https.request({
252
+ hostname: parsedUrl.hostname,
253
+ path: parsedUrl.pathname + parsedUrl.search,
254
+ method: 'POST',
255
+ headers: { 'Content-Type': 'application/json', 'Content-Length': String(Buffer.byteLength(postData)) },
256
+ }, (res) => {
257
+ let data = '';
258
+ res.on('data', (chunk: Buffer) => (data += chunk.toString()));
259
+ res.on('end', () => {
260
+ if (res.statusCode && res.statusCode >= 400) { reject(new Error(`Gemini API error ${res.statusCode}: ${data}`)); return; }
261
+ try {
262
+ const parsed = JSON.parse(data);
263
+ resolve(parsed.candidates?.[0]?.content?.parts?.[0]?.text ?? '');
264
+ } catch { reject(new Error(`Invalid Gemini response: ${data.slice(0, 200)}`)); }
265
+ });
266
+ });
267
+ req.on('error', reject);
268
+ req.write(postData);
269
+ req.end();
270
+ });
271
+ }
272
+
273
+ async *chatStream(messages: Message[], systemPrompt?: string): AsyncIterable<string> {
274
+ if (!this.apiKey) {
275
+ const last = messages[messages.length - 1];
276
+ yield `[gemini/${this.model} - no API key] Echo: ${last?.content ?? ''}`;
277
+ return;
278
+ }
279
+ const body = this.formatContents(messages, systemPrompt);
280
+ const url = this.buildUrl(true);
281
+ const postData = JSON.stringify(body);
282
+ const parsedUrl = new URL(url);
283
+
284
+ const response = await new Promise<http.IncomingMessage>((resolve, reject) => {
285
+ const req = https.request({
286
+ hostname: parsedUrl.hostname,
287
+ path: parsedUrl.pathname + parsedUrl.search,
288
+ method: 'POST',
289
+ headers: { 'Content-Type': 'application/json', 'Content-Length': String(Buffer.byteLength(postData)) },
290
+ }, resolve);
291
+ req.on('error', reject);
292
+ req.write(postData);
293
+ req.end();
294
+ });
295
+
296
+ if (response.statusCode && response.statusCode >= 400) {
297
+ let data = '';
298
+ for await (const chunk of response) data += chunk.toString();
299
+ throw new Error(`Gemini API error ${response.statusCode}: ${data}`);
300
+ }
301
+
302
+ let buffer = '';
303
+ for await (const chunk of response) {
304
+ buffer += chunk.toString();
305
+ const lines = buffer.split('\n');
306
+ buffer = lines.pop() ?? '';
307
+ for (const line of lines) {
308
+ const trimmed = line.trim();
309
+ if (!trimmed.startsWith('data: ')) continue;
310
+ const data = trimmed.slice(6);
311
+ if (data === '[DONE]') return;
312
+ try {
313
+ const parsed = JSON.parse(data);
314
+ const text = parsed.candidates?.[0]?.content?.parts?.[0]?.text;
315
+ if (text) yield text;
316
+ } catch {}
317
+ }
318
+ }
319
+ }
320
+ }
321
+
322
+ function isGeminiNative(): boolean {
323
+ const baseUrl = process.env.OPC_LLM_BASE_URL || '';
324
+ const key = getApiKey();
325
+ return key.startsWith('AQ.') || (baseUrl.includes('googleapis.com') && !baseUrl.includes('/openai'));
326
+ }
327
+
328
+ export function createProvider(name: string = 'openai', model?: string, baseUrl?: string, apiKey?: string): LLMProvider {
329
+ const finalModel = model || process.env.OPC_LLM_MODEL || 'gpt-4o-mini';
330
+
331
+ if (name === 'ollama') {
332
+ const ollamaBase = baseUrl || process.env.OPC_LLM_BASE_URL || 'http://localhost:11434/v1';
333
+ const ollamaKey = apiKey || process.env.OPC_LLM_API_KEY || 'ollama';
334
+ return new OpenAICompatibleProvider('ollama', finalModel, ollamaBase, ollamaKey);
335
+ }
336
+
337
+ const finalKey = apiKey || getApiKey();
338
+ const finalBaseUrl = baseUrl || getBaseUrl();
339
+
340
+ if (finalKey.startsWith('AQ.') || isGeminiNative()) {
341
+ return new GeminiNativeProvider(finalModel, finalKey);
342
+ }
343
+
344
+ let resolvedName = name;
345
+ if (finalBaseUrl.includes('deepseek.com')) {
346
+ resolvedName = 'deepseek';
347
+ } else if (finalBaseUrl.includes('dashscope.aliyuncs.com')) {
348
+ resolvedName = 'qwen';
349
+ }
350
+
351
+ return new OpenAICompatibleProvider(resolvedName, finalModel, baseUrl, apiKey);
352
+ }
353
+
354
+ export const SUPPORTED_PROVIDERS = ['openai', 'ollama', 'deepseek', 'qwen', 'gemini', 'dashscope', 'zhipu', 'moonshot'] as const;