llmjs2 0.0.2 → 1.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.
- package/LICENSE +21 -0
- package/README.md +486 -1
- package/dist/agent.d.ts +80 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +189 -0
- package/dist/agent.js.map +1 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +191 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/base.d.ts +58 -0
- package/dist/providers/base.d.ts.map +1 -0
- package/dist/providers/base.js +149 -0
- package/dist/providers/base.js.map +1 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +7 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/ollama.d.ts +42 -0
- package/dist/providers/ollama.d.ts.map +1 -0
- package/dist/providers/ollama.js +260 -0
- package/dist/providers/ollama.js.map +1 -0
- package/dist/providers/openai.d.ts +38 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +289 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/types.d.ts +182 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/package.json +44 -10
- package/src/agent.ts +285 -0
- package/src/index.ts +268 -0
- package/src/providers/base.ts +216 -0
- package/src/providers/index.ts +8 -0
- package/src/providers/ollama.ts +429 -0
- package/src/providers/openai.ts +485 -0
- package/src/types.ts +231 -0
- package/llmjs.js +0 -61
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base provider class with common functionality
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Validation helper for requests
|
|
6
|
+
*/
|
|
7
|
+
export function validateCompletionRequest(request) {
|
|
8
|
+
if (!request.model) {
|
|
9
|
+
throw new Error('Model is required');
|
|
10
|
+
}
|
|
11
|
+
if (!Array.isArray(request.messages) || request.messages.length === 0) {
|
|
12
|
+
throw new Error('Messages array is required and must not be empty');
|
|
13
|
+
}
|
|
14
|
+
// Validate message structure
|
|
15
|
+
for (const msg of request.messages) {
|
|
16
|
+
if (!msg.role || msg.content === undefined || msg.content === null) {
|
|
17
|
+
throw new Error('Each message must have role and content');
|
|
18
|
+
}
|
|
19
|
+
if (!['system', 'user', 'assistant'].includes(msg.role)) {
|
|
20
|
+
throw new Error(`Invalid role: ${msg.role}`);
|
|
21
|
+
}
|
|
22
|
+
if (typeof msg.content !== 'string') {
|
|
23
|
+
throw new Error('Message content must be a string');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
// Validate numeric ranges
|
|
27
|
+
if (request.temperature !== undefined && request.temperature !== null) {
|
|
28
|
+
if (typeof request.temperature !== 'number' || request.temperature < 0 || request.temperature > 2) {
|
|
29
|
+
throw new Error('Temperature must be a number between 0 and 2');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (request.topP !== undefined && request.topP !== null) {
|
|
33
|
+
if (typeof request.topP !== 'number' || request.topP < 0 || request.topP > 1) {
|
|
34
|
+
throw new Error('topP must be a number between 0 and 1');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (request.maxTokens !== undefined && request.maxTokens !== null) {
|
|
38
|
+
if (typeof request.maxTokens !== 'number' || request.maxTokens < 1) {
|
|
39
|
+
throw new Error('maxTokens must be a number greater than 0');
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (request.frequencyPenalty !== undefined && request.frequencyPenalty !== null) {
|
|
43
|
+
if (typeof request.frequencyPenalty !== 'number' || request.frequencyPenalty < -2 || request.frequencyPenalty > 2) {
|
|
44
|
+
throw new Error('frequencyPenalty must be a number between -2 and 2');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (request.presencePenalty !== undefined && request.presencePenalty !== null) {
|
|
48
|
+
if (typeof request.presencePenalty !== 'number' || request.presencePenalty < -2 || request.presencePenalty > 2) {
|
|
49
|
+
throw new Error('presencePenalty must be a number between -2 and 2');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Custom error class for provider errors
|
|
55
|
+
*/
|
|
56
|
+
export class LLMError extends Error {
|
|
57
|
+
constructor(message, code, statusCode, details, retryable) {
|
|
58
|
+
super(message);
|
|
59
|
+
this.name = 'LLMError';
|
|
60
|
+
this.code = code;
|
|
61
|
+
this.statusCode = statusCode;
|
|
62
|
+
this.details = details;
|
|
63
|
+
this.retryable = retryable ?? false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Retry helper with exponential backoff
|
|
68
|
+
*/
|
|
69
|
+
export async function withRetry(fn, options = {}) {
|
|
70
|
+
const maxRetries = options.maxRetries ?? 3;
|
|
71
|
+
const backoffMultiplier = options.backoffMultiplier ?? 2;
|
|
72
|
+
const initialDelayMs = options.initialDelayMs ?? 1000;
|
|
73
|
+
let lastError;
|
|
74
|
+
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
75
|
+
try {
|
|
76
|
+
return await fn();
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
lastError = error;
|
|
80
|
+
// Check if error is retryable
|
|
81
|
+
const isRetryable = error instanceof LLMError
|
|
82
|
+
? error.retryable
|
|
83
|
+
: error instanceof Error &&
|
|
84
|
+
(error.message.includes('timeout') ||
|
|
85
|
+
error.message.includes('ECONNREFUSED') ||
|
|
86
|
+
error.message.includes('ENOTFOUND'));
|
|
87
|
+
if (!isRetryable || attempt === maxRetries - 1) {
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
// Exponential backoff
|
|
91
|
+
const delay = initialDelayMs * Math.pow(backoffMultiplier, attempt);
|
|
92
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
throw lastError || new Error('Retry failed');
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Base provider class
|
|
99
|
+
*/
|
|
100
|
+
export class BaseProvider {
|
|
101
|
+
constructor(config) {
|
|
102
|
+
this.config = config;
|
|
103
|
+
this.debug = false;
|
|
104
|
+
this.logger = (level, message, data) => {
|
|
105
|
+
if (this.debug) {
|
|
106
|
+
console.log(`[${level}] ${message}`, data ?? '');
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
setDebug(debug) {
|
|
111
|
+
this.debug = debug;
|
|
112
|
+
}
|
|
113
|
+
setLogger(logger) {
|
|
114
|
+
this.logger = logger;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Get the timeout for a request
|
|
118
|
+
*/
|
|
119
|
+
getTimeout(request) {
|
|
120
|
+
return (request?.timeout ??
|
|
121
|
+
this.config.timeout ??
|
|
122
|
+
30000 // Default 30 seconds
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Get retry config
|
|
127
|
+
*/
|
|
128
|
+
getRetryConfig(request) {
|
|
129
|
+
return {
|
|
130
|
+
maxRetries: request?.retry?.maxRetries ?? this.config.retry?.maxRetries ?? 3,
|
|
131
|
+
backoffMultiplier: request?.retry?.backoffMultiplier ??
|
|
132
|
+
this.config.retry?.backoffMultiplier ??
|
|
133
|
+
2,
|
|
134
|
+
initialDelayMs: request?.retry?.initialDelayMs ??
|
|
135
|
+
this.config.retry?.initialDelayMs ??
|
|
136
|
+
1000,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Get headers including custom headers
|
|
141
|
+
*/
|
|
142
|
+
getHeaders(request) {
|
|
143
|
+
return {
|
|
144
|
+
...(this.config.headers ?? {}),
|
|
145
|
+
...(request?.headers ?? {}),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=base.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":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U,EAAE,OAAO,EAAE,KAAK,EAAE,UAAU,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,UAAU,IAAI,CAAC;YAC5E,iBAAiB,EACf,OAAO,EAAE,KAAK,EAAE,iBAAiB;gBACjC,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,iBAAiB;gBACpC,CAAC;YACH,cAAc,EACZ,OAAO,EAAE,KAAK,EAAE,cAAc;gBAC9B,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,cAAc;gBACjC,IAAI;SACP,CAAC;IACJ,CAAC;IAED;;OAEG;IACO,UAAU,CAAC,OAA2B;QAC9C,OAAO;YACL,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC;YAC9B,GAAG,CAAC,OAAO,EAAE,OAAO,IAAI,EAAE,CAAC;SAC5B,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,YAAY,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ollama provider implementation
|
|
3
|
+
*/
|
|
4
|
+
import { CompletionRequest, CompletionResponse, CompletionChunk, ProviderConfig } from '../types.js';
|
|
5
|
+
import { BaseProvider } from './base.js';
|
|
6
|
+
/**
|
|
7
|
+
* Ollama Provider implementation
|
|
8
|
+
*/
|
|
9
|
+
export declare class OllamaProvider extends BaseProvider {
|
|
10
|
+
private baseUrl;
|
|
11
|
+
private apiKey?;
|
|
12
|
+
constructor(config: ProviderConfig);
|
|
13
|
+
/**
|
|
14
|
+
* Parse model string (e.g., 'ollama/mistral' -> 'mistral')
|
|
15
|
+
*/
|
|
16
|
+
parseModel(model: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Validate configuration
|
|
19
|
+
*/
|
|
20
|
+
validate(): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Create a completion
|
|
23
|
+
*/
|
|
24
|
+
complete(request: CompletionRequest): Promise<CompletionResponse>;
|
|
25
|
+
/**
|
|
26
|
+
* Stream completion
|
|
27
|
+
*/
|
|
28
|
+
completeStream(request: CompletionRequest): AsyncIterable<CompletionChunk>;
|
|
29
|
+
/**
|
|
30
|
+
* Internal stream completion handler
|
|
31
|
+
*/
|
|
32
|
+
private streamCompletion;
|
|
33
|
+
/**
|
|
34
|
+
* Make HTTP request to Ollama API using fetch
|
|
35
|
+
*/
|
|
36
|
+
private makeRequest;
|
|
37
|
+
/**
|
|
38
|
+
* Stream HTTP request using fetch
|
|
39
|
+
*/
|
|
40
|
+
private makeStreamRequest;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=ollama.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ollama.d.ts","sourceRoot":"","sources":["../../src/providers/ollama.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EACf,cAAc,EACf,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,YAAY,EAIb,MAAM,WAAW,CAAC;AAsCnB;;GAEG;AACH,qBAAa,cAAe,SAAQ,YAAY;IAC9C,OAAO,CAAC,OAAO,CAAgC;IAC/C,OAAO,CAAC,MAAM,CAAC,CAAS;gBAEZ,MAAM,EAAE,cAAc;IAsBlC;;OAEG;IACH,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAOjC;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAwB/B;;OAEG;IACG,QAAQ,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAiDvE;;OAEG;IACI,cAAc,CACnB,OAAO,EAAE,iBAAiB,GACzB,aAAa,CAAC,eAAe,CAAC;IA4BjC;;OAEG;YACY,gBAAgB;IAgB/B;;OAEG;YACW,WAAW;IA6EzB;;OAEG;IACH,OAAO,CAAC,iBAAiB;CAwH1B"}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ollama provider implementation
|
|
3
|
+
*/
|
|
4
|
+
import { URL } from 'url';
|
|
5
|
+
import { BaseProvider, validateCompletionRequest, withRetry, LLMError, } from './base.js';
|
|
6
|
+
/**
|
|
7
|
+
* Ollama Provider implementation
|
|
8
|
+
*/
|
|
9
|
+
export class OllamaProvider extends BaseProvider {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
super(config);
|
|
12
|
+
this.baseUrl = 'https://ollama.com';
|
|
13
|
+
// Get API key from config or env variable
|
|
14
|
+
const apiKey = config.apiKey || process.env.OLLAMA_CLOUD_API_KEY;
|
|
15
|
+
if (apiKey) {
|
|
16
|
+
this.apiKey = apiKey;
|
|
17
|
+
// Default to Ollama Cloud for API key-based requests
|
|
18
|
+
if (!config.baseUrl) {
|
|
19
|
+
this.baseUrl = 'https://ollama.com';
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
this.baseUrl = config.baseUrl;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
// Local Ollama setup
|
|
27
|
+
if (config.baseUrl) {
|
|
28
|
+
this.baseUrl = config.baseUrl;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Parse model string (e.g., 'ollama/mistral' -> 'mistral')
|
|
34
|
+
*/
|
|
35
|
+
parseModel(model) {
|
|
36
|
+
if (model.startsWith('ollama/')) {
|
|
37
|
+
return model.slice(7); // Remove 'ollama/' prefix
|
|
38
|
+
}
|
|
39
|
+
return model;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Validate configuration
|
|
43
|
+
*/
|
|
44
|
+
async validate() {
|
|
45
|
+
try {
|
|
46
|
+
// Make a simple request to verify Ollama is running
|
|
47
|
+
const response = await this.makeRequest('/api/tags', 'GET');
|
|
48
|
+
if (!Array.isArray(response.models)) {
|
|
49
|
+
throw new Error('Invalid Ollama response format');
|
|
50
|
+
}
|
|
51
|
+
this.logger('info', 'Ollama API validation successful');
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
throw new LLMError(`Ollama validation failed: ${error instanceof Error ? error.message : String(error)}`, 'VALIDATION_FAILED', undefined, null, true);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Create a completion
|
|
59
|
+
*/
|
|
60
|
+
async complete(request) {
|
|
61
|
+
validateCompletionRequest(request);
|
|
62
|
+
const model = this.parseModel(request.model);
|
|
63
|
+
return withRetry(async () => {
|
|
64
|
+
const ollamaRequest = {
|
|
65
|
+
model,
|
|
66
|
+
stream: false,
|
|
67
|
+
messages: request.messages.map((msg) => ({
|
|
68
|
+
role: msg.role,
|
|
69
|
+
content: msg.content,
|
|
70
|
+
})),
|
|
71
|
+
};
|
|
72
|
+
this.logger('debug', 'Ollama completion request', {
|
|
73
|
+
model,
|
|
74
|
+
messageCount: request.messages.length,
|
|
75
|
+
});
|
|
76
|
+
// Use non-streaming request for complete()
|
|
77
|
+
const response = await this.makeRequest('/api/chat', 'POST', ollamaRequest, request);
|
|
78
|
+
const fullResponse = response.message?.content || '';
|
|
79
|
+
const result = {
|
|
80
|
+
content: fullResponse,
|
|
81
|
+
model: model,
|
|
82
|
+
stopReason: 'stop_sequence',
|
|
83
|
+
raw: response,
|
|
84
|
+
};
|
|
85
|
+
this.logger('debug', 'Ollama completion response', {
|
|
86
|
+
model,
|
|
87
|
+
contentLength: fullResponse.length,
|
|
88
|
+
});
|
|
89
|
+
return result;
|
|
90
|
+
}, this.getRetryConfig(request));
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Stream completion
|
|
94
|
+
*/
|
|
95
|
+
async *completeStream(request) {
|
|
96
|
+
validateCompletionRequest(request);
|
|
97
|
+
const model = this.parseModel(request.model);
|
|
98
|
+
const ollamaRequest = {
|
|
99
|
+
model,
|
|
100
|
+
stream: true,
|
|
101
|
+
messages: request.messages.map((msg) => ({
|
|
102
|
+
role: msg.role,
|
|
103
|
+
content: msg.content,
|
|
104
|
+
})),
|
|
105
|
+
};
|
|
106
|
+
this.logger('debug', 'Ollama stream request', { model });
|
|
107
|
+
const chunks = await this.streamCompletion(ollamaRequest, request);
|
|
108
|
+
for await (const chunk of chunks) {
|
|
109
|
+
if (chunk.message?.content) {
|
|
110
|
+
yield {
|
|
111
|
+
delta: chunk.message.content,
|
|
112
|
+
stopReason: chunk.done ? 'stop_sequence' : undefined,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Internal stream completion handler
|
|
119
|
+
*/
|
|
120
|
+
async *streamCompletion(body, request) {
|
|
121
|
+
const stream = await this.makeStreamRequest('/api/chat', 'POST', body, request);
|
|
122
|
+
for await (const chunk of stream) {
|
|
123
|
+
yield chunk;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Make HTTP request to Ollama API using fetch
|
|
128
|
+
*/
|
|
129
|
+
async makeRequest(path, method = 'POST', body, request) {
|
|
130
|
+
const url = new URL(path, this.baseUrl).toString();
|
|
131
|
+
const timeout = this.getTimeout(request);
|
|
132
|
+
const headers = {
|
|
133
|
+
'Content-Type': 'application/json',
|
|
134
|
+
...this.getHeaders(request),
|
|
135
|
+
};
|
|
136
|
+
if (this.apiKey) {
|
|
137
|
+
headers['Authorization'] = `Bearer ${this.apiKey}`;
|
|
138
|
+
}
|
|
139
|
+
this.logger('info', '=== Ollama Request ===', {
|
|
140
|
+
url,
|
|
141
|
+
method,
|
|
142
|
+
headers,
|
|
143
|
+
body: JSON.stringify(body),
|
|
144
|
+
});
|
|
145
|
+
try {
|
|
146
|
+
const controller = new AbortController();
|
|
147
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
148
|
+
const response = await fetch(url, {
|
|
149
|
+
method,
|
|
150
|
+
headers,
|
|
151
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
152
|
+
signal: controller.signal,
|
|
153
|
+
});
|
|
154
|
+
clearTimeout(timeoutId);
|
|
155
|
+
if (!response.ok) {
|
|
156
|
+
const errorText = await response.text();
|
|
157
|
+
throw new LLMError(`Ollama API error: ${errorText}`, 'API_ERROR', response.status, null, response.status === 503 || response.status === 502);
|
|
158
|
+
}
|
|
159
|
+
const data = await response.json();
|
|
160
|
+
return data;
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
if (error instanceof LLMError) {
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
if (error instanceof Error &&
|
|
167
|
+
error.name === 'AbortError') {
|
|
168
|
+
throw new LLMError('Ollama request timeout', 'TIMEOUT', undefined, null, true);
|
|
169
|
+
}
|
|
170
|
+
throw new LLMError(`Ollama request failed: ${error instanceof Error ? error.message : String(error)}`, 'REQUEST_FAILED', undefined, { error: error instanceof Error ? error.message : String(error) }, true);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Stream HTTP request using fetch
|
|
175
|
+
*/
|
|
176
|
+
makeStreamRequest(path, method = 'POST', body, request) {
|
|
177
|
+
const self = this;
|
|
178
|
+
return {
|
|
179
|
+
async *[Symbol.asyncIterator]() {
|
|
180
|
+
const url = new URL(path, self.baseUrl).toString();
|
|
181
|
+
const timeout = self.getTimeout(request);
|
|
182
|
+
const headers = {
|
|
183
|
+
'Content-Type': 'application/json',
|
|
184
|
+
...self.getHeaders(request),
|
|
185
|
+
};
|
|
186
|
+
if (self.apiKey) {
|
|
187
|
+
headers['Authorization'] = `Bearer ${self.apiKey}`;
|
|
188
|
+
}
|
|
189
|
+
self.logger('debug', 'Ollama stream request', {
|
|
190
|
+
url,
|
|
191
|
+
method,
|
|
192
|
+
headers,
|
|
193
|
+
body: JSON.stringify(body),
|
|
194
|
+
});
|
|
195
|
+
try {
|
|
196
|
+
const controller = new AbortController();
|
|
197
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
198
|
+
const response = await fetch(url, {
|
|
199
|
+
method,
|
|
200
|
+
headers,
|
|
201
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
202
|
+
signal: controller.signal,
|
|
203
|
+
});
|
|
204
|
+
clearTimeout(timeoutId);
|
|
205
|
+
if (!response.ok) {
|
|
206
|
+
const errorText = await response.text();
|
|
207
|
+
throw new LLMError(`Ollama stream error: ${errorText}`, 'STREAM_ERROR', response.status);
|
|
208
|
+
}
|
|
209
|
+
const reader = response.body?.getReader();
|
|
210
|
+
if (!reader) {
|
|
211
|
+
throw new LLMError('No response body', 'STREAM_ERROR', undefined);
|
|
212
|
+
}
|
|
213
|
+
const decoder = new TextDecoder();
|
|
214
|
+
let buffer = '';
|
|
215
|
+
while (true) {
|
|
216
|
+
const { done, value } = await reader.read();
|
|
217
|
+
if (done)
|
|
218
|
+
break;
|
|
219
|
+
buffer += decoder.decode(value, { stream: true });
|
|
220
|
+
const lines = buffer.split('\n');
|
|
221
|
+
buffer = lines[lines.length - 1];
|
|
222
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
223
|
+
const line = lines[i].trim();
|
|
224
|
+
if (!line)
|
|
225
|
+
continue;
|
|
226
|
+
try {
|
|
227
|
+
const chunk = JSON.parse(line);
|
|
228
|
+
yield chunk;
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
// Ignore parse errors in stream
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
// Process any remaining data in buffer
|
|
236
|
+
if (buffer.trim()) {
|
|
237
|
+
try {
|
|
238
|
+
const chunk = JSON.parse(buffer);
|
|
239
|
+
yield chunk;
|
|
240
|
+
}
|
|
241
|
+
catch (error) {
|
|
242
|
+
// Ignore parse errors
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
if (error instanceof LLMError) {
|
|
248
|
+
throw error;
|
|
249
|
+
}
|
|
250
|
+
if (error instanceof Error &&
|
|
251
|
+
error.name === 'AbortError') {
|
|
252
|
+
throw new LLMError('Ollama stream request timeout', 'TIMEOUT', undefined, null, true);
|
|
253
|
+
}
|
|
254
|
+
throw new LLMError(`Ollama stream request failed: ${error instanceof Error ? error.message : String(error)}`, 'REQUEST_FAILED', undefined, { error: error instanceof Error ? error.message : String(error) }, true);
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
//# sourceMappingURL=ollama.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ollama.js","sourceRoot":"","sources":["../../src/providers/ollama.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,CAAC;AAQ1B,OAAO,EACL,YAAY,EACZ,yBAAyB,EACzB,SAAS,EACT,QAAQ,GACT,MAAM,WAAW,CAAC;AAsCnB;;GAEG;AACH,MAAM,OAAO,cAAe,SAAQ,YAAY;IAI9C,YAAY,MAAsB;QAChC,KAAK,CAAC,MAAM,CAAC,CAAC;QAJR,YAAO,GAAW,oBAAoB,CAAC;QAM7C,0CAA0C;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AC;wBAAC,OAAO,KAAK,EAAE,CAAC;4BACf,sBAAsB;wBACxB,CAAC;oBACH,CAAC;gBACH,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,IAAI,KAAK,YAAY,QAAQ,EAAE,CAAC;wBAC9B,MAAM,KAAK,CAAC;oBACd,CAAC;oBACD,IACE,KAAK,YAAY,KAAK;wBACtB,KAAK,CAAC,IAAI,KAAK,YAAY,EAC3B,CAAC;wBACD,MAAM,IAAI,QAAQ,CAChB,+BAA+B,EAC/B,SAAS,EACT,SAAS,EACT,IAAI,EACJ,IAAI,CACL,CAAC;oBACJ,CAAC;oBACD,MAAM,IAAI,QAAQ,CAChB,iCAAiC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EACzF,gBAAgB,EAChB,SAAS,EACT,EAAE,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EACjE,IAAI,CACL,CAAC;gBACJ,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI provider implementation
|
|
3
|
+
*/
|
|
4
|
+
import { CompletionRequest, CompletionResponse, CompletionChunk, ProviderConfig } from '../types.js';
|
|
5
|
+
import { BaseProvider } from './base.js';
|
|
6
|
+
/**
|
|
7
|
+
* OpenAI Provider implementation
|
|
8
|
+
*/
|
|
9
|
+
export declare class OpenAIProvider extends BaseProvider {
|
|
10
|
+
private apiKey;
|
|
11
|
+
private baseUrl;
|
|
12
|
+
constructor(config: ProviderConfig);
|
|
13
|
+
/**
|
|
14
|
+
* Parse model string (e.g., 'openai/gpt-4' -> 'gpt-4')
|
|
15
|
+
*/
|
|
16
|
+
parseModel(model: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Validate configuration
|
|
19
|
+
*/
|
|
20
|
+
validate(): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Create a completion
|
|
23
|
+
*/
|
|
24
|
+
complete(request: CompletionRequest): Promise<CompletionResponse>;
|
|
25
|
+
/**
|
|
26
|
+
* Stream completion
|
|
27
|
+
*/
|
|
28
|
+
completeStream(request: CompletionRequest): AsyncIterable<CompletionChunk>;
|
|
29
|
+
/**
|
|
30
|
+
* Make HTTP request to OpenAI API
|
|
31
|
+
*/
|
|
32
|
+
private makeRequest;
|
|
33
|
+
/**
|
|
34
|
+
* Stream HTTP request
|
|
35
|
+
*/
|
|
36
|
+
private makeStreamRequest;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=openai.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/providers/openai.ts"],"names":[],"mappings":"AAAA;;GAEG;AAMH,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EACf,cAAc,EACf,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,YAAY,EAIb,MAAM,WAAW,CAAC;AAwEnB;;GAEG;AACH,qBAAa,cAAe,SAAQ,YAAY;IAC9C,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,OAAO,CAAuC;gBAE1C,MAAM,EAAE,cAAc;IAclC;;OAEG;IACH,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAOjC;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAa/B;;OAEG;IACG,QAAQ,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAqFvE;;OAEG;IACI,cAAc,CACnB,OAAO,EAAE,iBAAiB,GACzB,aAAa,CAAC,eAAe,CAAC;IAiDjC;;OAEG;IACH,OAAO,CAAC,WAAW;IAsFnB;;OAEG;IACH,OAAO,CAAC,iBAAiB;CAgH1B"}
|