freellm 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/README.md +89 -0
- package/bin/cli.js +25 -0
- package/client-dist/assets/index-aUFet1Mp.js +84 -0
- package/client-dist/index.html +16 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +38 -0
- package/dist/crypto.d.ts +6 -0
- package/dist/crypto.js +56 -0
- package/dist/db/index.d.ts +62 -0
- package/dist/db/index.js +368 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +100 -0
- package/dist/providers/cloudflare.d.ts +10 -0
- package/dist/providers/cloudflare.js +140 -0
- package/dist/providers/google.d.ts +8 -0
- package/dist/providers/google.js +181 -0
- package/dist/providers/index.d.ts +14 -0
- package/dist/providers/index.js +60 -0
- package/dist/providers/ollama.d.ts +10 -0
- package/dist/providers/ollama.js +99 -0
- package/dist/providers/openai-compat.d.ts +11 -0
- package/dist/providers/openai-compat.js +115 -0
- package/dist/providers/openai-image-compat.d.ts +10 -0
- package/dist/providers/openai-image-compat.js +40 -0
- package/dist/providers/openai-tts-compat.d.ts +10 -0
- package/dist/providers/openai-tts-compat.js +40 -0
- package/dist/providers/pollinations-image.d.ts +7 -0
- package/dist/providers/pollinations-image.js +50 -0
- package/dist/providers/pollinations.d.ts +9 -0
- package/dist/providers/pollinations.js +76 -0
- package/dist/providers/types.d.ts +132 -0
- package/dist/providers/types.js +66 -0
- package/dist/routes/api/analytics.d.ts +1 -0
- package/dist/routes/api/analytics.js +10 -0
- package/dist/routes/api/auth.d.ts +3 -0
- package/dist/routes/api/auth.js +60 -0
- package/dist/routes/api/keys.d.ts +1 -0
- package/dist/routes/api/keys.js +65 -0
- package/dist/routes/api/models.d.ts +1 -0
- package/dist/routes/api/models.js +26 -0
- package/dist/routes/v1/audio.d.ts +1 -0
- package/dist/routes/v1/audio.js +94 -0
- package/dist/routes/v1/chat.d.ts +1 -0
- package/dist/routes/v1/chat.js +131 -0
- package/dist/routes/v1/images.d.ts +1 -0
- package/dist/routes/v1/images.js +85 -0
- package/dist/routes/v1/models.d.ts +1 -0
- package/dist/routes/v1/models.js +59 -0
- package/dist/services/health.d.ts +2 -0
- package/dist/services/health.js +80 -0
- package/dist/services/ratelimit.d.ts +4 -0
- package/dist/services/ratelimit.js +88 -0
- package/dist/services/router.d.ts +14 -0
- package/dist/services/router.js +72 -0
- package/package.json +57 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CloudflareProvider = void 0;
|
|
4
|
+
// Cloudflare Workers AI provider
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
class CloudflareProvider extends types_1.BaseProvider {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(...arguments);
|
|
9
|
+
this.platform = 'cloudflare';
|
|
10
|
+
this.baseUrl = 'https://api.cloudflare.com/client/v4';
|
|
11
|
+
}
|
|
12
|
+
defaultModel() {
|
|
13
|
+
return '@cf/meta/llama-3.1-8b-instruct';
|
|
14
|
+
}
|
|
15
|
+
// apiKey format: "ACCOUNT_ID:API_TOKEN"
|
|
16
|
+
parseKey(apiKey) {
|
|
17
|
+
const idx = apiKey.indexOf(':');
|
|
18
|
+
if (idx < 0)
|
|
19
|
+
throw new Error('Cloudflare key must be "ACCOUNT_ID:API_TOKEN"');
|
|
20
|
+
return { accountId: apiKey.slice(0, idx), token: apiKey.slice(idx + 1) };
|
|
21
|
+
}
|
|
22
|
+
toMessages(messages) {
|
|
23
|
+
return messages.map((m) => ({
|
|
24
|
+
role: m.role,
|
|
25
|
+
content: typeof m.content === 'string' ? m.content : JSON.stringify(m.content),
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
async chat(apiKey, req) {
|
|
29
|
+
const { accountId, token } = this.parseKey(apiKey);
|
|
30
|
+
const model = req.model === 'auto' ? this.defaultModel() : req.model;
|
|
31
|
+
const res = await fetch(`${this.baseUrl}/accounts/${accountId}/ai/run/${model}`, {
|
|
32
|
+
method: 'POST',
|
|
33
|
+
headers: {
|
|
34
|
+
Authorization: `Bearer ${token}`,
|
|
35
|
+
'Content-Type': 'application/json',
|
|
36
|
+
},
|
|
37
|
+
body: JSON.stringify({
|
|
38
|
+
messages: this.toMessages(req.messages),
|
|
39
|
+
max_tokens: req.max_tokens,
|
|
40
|
+
temperature: req.temperature,
|
|
41
|
+
stream: false,
|
|
42
|
+
}),
|
|
43
|
+
});
|
|
44
|
+
if (!res.ok) {
|
|
45
|
+
const text = await res.text().catch(() => '');
|
|
46
|
+
const err = new Error(`${res.status}: ${text}`);
|
|
47
|
+
err.status = res.status;
|
|
48
|
+
throw err;
|
|
49
|
+
}
|
|
50
|
+
const data = await res.json();
|
|
51
|
+
const id = (0, types_1.makeId)();
|
|
52
|
+
return {
|
|
53
|
+
id,
|
|
54
|
+
object: 'chat.completion',
|
|
55
|
+
created: Math.floor(Date.now() / 1000),
|
|
56
|
+
model,
|
|
57
|
+
choices: [
|
|
58
|
+
{
|
|
59
|
+
index: 0,
|
|
60
|
+
message: { role: 'assistant', content: data.result?.response ?? '' },
|
|
61
|
+
finish_reason: 'stop',
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
async streamChat(apiKey, req, onChunk, onDone, onError) {
|
|
67
|
+
const { accountId, token } = this.parseKey(apiKey);
|
|
68
|
+
const model = req.model === 'auto' ? this.defaultModel() : req.model;
|
|
69
|
+
let res;
|
|
70
|
+
try {
|
|
71
|
+
res = await fetch(`${this.baseUrl}/accounts/${accountId}/ai/run/${model}`, {
|
|
72
|
+
method: 'POST',
|
|
73
|
+
headers: {
|
|
74
|
+
Authorization: `Bearer ${token}`,
|
|
75
|
+
'Content-Type': 'application/json',
|
|
76
|
+
},
|
|
77
|
+
body: JSON.stringify({
|
|
78
|
+
messages: this.toMessages(req.messages),
|
|
79
|
+
stream: true,
|
|
80
|
+
}),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
onError(e);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (!res.ok) {
|
|
88
|
+
const text = await res.text().catch(() => '');
|
|
89
|
+
const err = new Error(`${res.status}: ${text}`);
|
|
90
|
+
err.status = res.status;
|
|
91
|
+
onError(err);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const reader = res.body?.getReader();
|
|
95
|
+
if (!reader) {
|
|
96
|
+
onDone();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const decoder = new TextDecoder();
|
|
100
|
+
let buffer = '';
|
|
101
|
+
const id = (0, types_1.makeId)();
|
|
102
|
+
try {
|
|
103
|
+
while (true) {
|
|
104
|
+
const { done, value } = await reader.read();
|
|
105
|
+
if (done)
|
|
106
|
+
break;
|
|
107
|
+
buffer += decoder.decode(value, { stream: true });
|
|
108
|
+
const lines = buffer.split('\n');
|
|
109
|
+
buffer = lines.pop() ?? '';
|
|
110
|
+
for (const line of lines) {
|
|
111
|
+
const trimmed = line.trim();
|
|
112
|
+
if (!trimmed || trimmed === 'data: [DONE]')
|
|
113
|
+
continue;
|
|
114
|
+
if (!trimmed.startsWith('data: '))
|
|
115
|
+
continue;
|
|
116
|
+
try {
|
|
117
|
+
const data = JSON.parse(trimmed.slice(6));
|
|
118
|
+
onChunk({
|
|
119
|
+
id,
|
|
120
|
+
object: 'chat.completion.chunk',
|
|
121
|
+
created: Math.floor(Date.now() / 1000),
|
|
122
|
+
model,
|
|
123
|
+
choices: [{ index: 0, delta: { content: data.response ?? '' }, finish_reason: null }],
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
catch { /* skip */ }
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch (e) {
|
|
131
|
+
onError(e);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
finally {
|
|
135
|
+
reader.releaseLock();
|
|
136
|
+
}
|
|
137
|
+
onDone();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
exports.CloudflareProvider = CloudflareProvider;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BaseProvider, ChatRequest, ChatResponse, StreamChunk } from './types';
|
|
2
|
+
export declare class GoogleProvider extends BaseProvider {
|
|
3
|
+
readonly platform = "google";
|
|
4
|
+
readonly baseUrl = "https://generativelanguage.googleapis.com/v1beta";
|
|
5
|
+
protected defaultModel(): string;
|
|
6
|
+
chat(apiKey: string, req: ChatRequest): Promise<ChatResponse>;
|
|
7
|
+
streamChat(apiKey: string, req: ChatRequest, onChunk: (chunk: StreamChunk) => void, onDone: () => void, onError: (err: Error) => void): Promise<void>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GoogleProvider = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
function toGeminiMessages(messages) {
|
|
6
|
+
const systemMsgs = messages.filter((m) => m.role === 'system');
|
|
7
|
+
const chatMsgs = messages.filter((m) => m.role !== 'system');
|
|
8
|
+
const systemInstruction = systemMsgs.length > 0
|
|
9
|
+
? { parts: [{ text: systemMsgs.map((m) => (typeof m.content === 'string' ? m.content : '')).join('\n') }] }
|
|
10
|
+
: undefined;
|
|
11
|
+
const contents = chatMsgs.map((m) => {
|
|
12
|
+
const role = m.role === 'assistant' ? 'model' : 'user';
|
|
13
|
+
const parts = [];
|
|
14
|
+
if (typeof m.content === 'string') {
|
|
15
|
+
parts.push({ text: m.content });
|
|
16
|
+
}
|
|
17
|
+
else if (Array.isArray(m.content)) {
|
|
18
|
+
for (const part of m.content) {
|
|
19
|
+
if (part.type === 'text') {
|
|
20
|
+
parts.push({ text: part.text });
|
|
21
|
+
}
|
|
22
|
+
else if (part.type === 'image_url') {
|
|
23
|
+
const url = part.image_url.url;
|
|
24
|
+
if (url.startsWith('data:')) {
|
|
25
|
+
const [header, data] = url.split(',');
|
|
26
|
+
const mimeType = header.split(':')[1].split(';')[0];
|
|
27
|
+
parts.push({ inlineData: { mimeType, data } });
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
parts.push({ text: `[Image: ${url}]` });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return { role, parts };
|
|
36
|
+
});
|
|
37
|
+
return { contents, systemInstruction };
|
|
38
|
+
}
|
|
39
|
+
class GoogleProvider extends types_1.BaseProvider {
|
|
40
|
+
constructor() {
|
|
41
|
+
super(...arguments);
|
|
42
|
+
this.platform = 'google';
|
|
43
|
+
this.baseUrl = 'https://generativelanguage.googleapis.com/v1beta';
|
|
44
|
+
}
|
|
45
|
+
defaultModel() {
|
|
46
|
+
return 'gemini-2.5-flash';
|
|
47
|
+
}
|
|
48
|
+
async chat(apiKey, req) {
|
|
49
|
+
const { contents, systemInstruction } = toGeminiMessages(req.messages);
|
|
50
|
+
const model = req.model === 'auto' ? this.defaultModel() : req.model;
|
|
51
|
+
const body = {
|
|
52
|
+
contents,
|
|
53
|
+
generationConfig: {
|
|
54
|
+
temperature: req.temperature,
|
|
55
|
+
maxOutputTokens: req.max_tokens,
|
|
56
|
+
topP: req.top_p,
|
|
57
|
+
stopSequences: typeof req.stop === 'string' ? [req.stop] : req.stop,
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
if (systemInstruction)
|
|
61
|
+
body.systemInstruction = systemInstruction;
|
|
62
|
+
const res = await fetch(`${this.baseUrl}/models/${model}:generateContent?key=${apiKey}`, {
|
|
63
|
+
method: 'POST',
|
|
64
|
+
headers: { 'Content-Type': 'application/json' },
|
|
65
|
+
body: JSON.stringify(body),
|
|
66
|
+
});
|
|
67
|
+
if (!res.ok) {
|
|
68
|
+
const text = await res.text().catch(() => '');
|
|
69
|
+
const err = new Error(`${res.status}: ${text}`);
|
|
70
|
+
err.status = res.status;
|
|
71
|
+
throw err;
|
|
72
|
+
}
|
|
73
|
+
const data = await res.json();
|
|
74
|
+
const content = data.candidates?.[0]?.content?.parts?.map((p) => p.text).join('') ?? '';
|
|
75
|
+
const id = (0, types_1.makeId)();
|
|
76
|
+
return {
|
|
77
|
+
id,
|
|
78
|
+
object: 'chat.completion',
|
|
79
|
+
created: Math.floor(Date.now() / 1000),
|
|
80
|
+
model,
|
|
81
|
+
choices: [
|
|
82
|
+
{
|
|
83
|
+
index: 0,
|
|
84
|
+
message: { role: 'assistant', content },
|
|
85
|
+
finish_reason: data.candidates?.[0]?.finishReason?.toLowerCase() ?? 'stop',
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
usage: data.usageMetadata
|
|
89
|
+
? {
|
|
90
|
+
prompt_tokens: data.usageMetadata.promptTokenCount,
|
|
91
|
+
completion_tokens: data.usageMetadata.candidatesTokenCount,
|
|
92
|
+
total_tokens: data.usageMetadata.totalTokenCount,
|
|
93
|
+
}
|
|
94
|
+
: undefined,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
async streamChat(apiKey, req, onChunk, onDone, onError) {
|
|
98
|
+
const { contents, systemInstruction } = toGeminiMessages(req.messages);
|
|
99
|
+
const model = req.model === 'auto' ? this.defaultModel() : req.model;
|
|
100
|
+
const body = {
|
|
101
|
+
contents,
|
|
102
|
+
generationConfig: {
|
|
103
|
+
temperature: req.temperature,
|
|
104
|
+
maxOutputTokens: req.max_tokens,
|
|
105
|
+
topP: req.top_p,
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
if (systemInstruction)
|
|
109
|
+
body.systemInstruction = systemInstruction;
|
|
110
|
+
let res;
|
|
111
|
+
try {
|
|
112
|
+
res = await fetch(`${this.baseUrl}/models/${model}:streamGenerateContent?alt=sse&key=${apiKey}`, {
|
|
113
|
+
method: 'POST',
|
|
114
|
+
headers: { 'Content-Type': 'application/json' },
|
|
115
|
+
body: JSON.stringify(body),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
catch (e) {
|
|
119
|
+
onError(e);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (!res.ok) {
|
|
123
|
+
const text = await res.text().catch(() => '');
|
|
124
|
+
const err = new Error(`${res.status}: ${text}`);
|
|
125
|
+
err.status = res.status;
|
|
126
|
+
onError(err);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const reader = res.body?.getReader();
|
|
130
|
+
if (!reader) {
|
|
131
|
+
onDone();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const decoder = new TextDecoder();
|
|
135
|
+
let buffer = '';
|
|
136
|
+
const id = (0, types_1.makeId)();
|
|
137
|
+
try {
|
|
138
|
+
while (true) {
|
|
139
|
+
const { done, value } = await reader.read();
|
|
140
|
+
if (done)
|
|
141
|
+
break;
|
|
142
|
+
buffer += decoder.decode(value, { stream: true });
|
|
143
|
+
const lines = buffer.split('\n');
|
|
144
|
+
buffer = lines.pop() ?? '';
|
|
145
|
+
for (const line of lines) {
|
|
146
|
+
const trimmed = line.trim();
|
|
147
|
+
if (!trimmed || !trimmed.startsWith('data: '))
|
|
148
|
+
continue;
|
|
149
|
+
try {
|
|
150
|
+
const data = JSON.parse(trimmed.slice(6));
|
|
151
|
+
const text = data.candidates?.[0]?.content?.parts?.[0]?.text ?? '';
|
|
152
|
+
const finishReason = data.candidates?.[0]?.finishReason ?? null;
|
|
153
|
+
onChunk({
|
|
154
|
+
id,
|
|
155
|
+
object: 'chat.completion.chunk',
|
|
156
|
+
created: Math.floor(Date.now() / 1000),
|
|
157
|
+
model,
|
|
158
|
+
choices: [
|
|
159
|
+
{
|
|
160
|
+
index: 0,
|
|
161
|
+
delta: { content: text },
|
|
162
|
+
finish_reason: finishReason?.toLowerCase() ?? null,
|
|
163
|
+
},
|
|
164
|
+
],
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
catch { /* skip */ }
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
catch (e) {
|
|
172
|
+
onError(e);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
finally {
|
|
176
|
+
reader.releaseLock();
|
|
177
|
+
}
|
|
178
|
+
onDone();
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
exports.GoogleProvider = GoogleProvider;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BaseProvider, BaseImageProvider, BaseTTSProvider } from './types';
|
|
2
|
+
import { OpenAICompatProvider } from './openai-compat';
|
|
3
|
+
import { OpenAIImageCompatProvider } from './openai-image-compat';
|
|
4
|
+
import { OpenAITTSCompatProvider } from './openai-tts-compat';
|
|
5
|
+
export type { BaseProvider, BaseImageProvider, BaseTTSProvider };
|
|
6
|
+
export { OpenAICompatProvider, OpenAIImageCompatProvider, OpenAITTSCompatProvider };
|
|
7
|
+
export declare function getChatProvider(platform: string): BaseProvider | undefined;
|
|
8
|
+
export declare function getImageProvider(platform: string): BaseImageProvider | undefined;
|
|
9
|
+
export declare function getTTSProvider(platform: string): BaseTTSProvider | undefined;
|
|
10
|
+
export declare function getProvider(platform: string, type: 'chat'): BaseProvider | undefined;
|
|
11
|
+
export declare function getProvider(platform: string, type: 'image'): BaseImageProvider | undefined;
|
|
12
|
+
export declare function getProvider(platform: string, type: 'tts'): BaseTTSProvider | undefined;
|
|
13
|
+
export declare function allProviders(type?: 'chat' | 'image' | 'tts'): string[];
|
|
14
|
+
export declare function registerCustomProvider(platform: string, baseUrl: string, defaultModel: string): void;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpenAITTSCompatProvider = exports.OpenAIImageCompatProvider = exports.OpenAICompatProvider = void 0;
|
|
4
|
+
exports.getChatProvider = getChatProvider;
|
|
5
|
+
exports.getImageProvider = getImageProvider;
|
|
6
|
+
exports.getTTSProvider = getTTSProvider;
|
|
7
|
+
exports.getProvider = getProvider;
|
|
8
|
+
exports.allProviders = allProviders;
|
|
9
|
+
exports.registerCustomProvider = registerCustomProvider;
|
|
10
|
+
const openai_compat_1 = require("./openai-compat");
|
|
11
|
+
Object.defineProperty(exports, "OpenAICompatProvider", { enumerable: true, get: function () { return openai_compat_1.OpenAICompatProvider; } });
|
|
12
|
+
const google_1 = require("./google");
|
|
13
|
+
const cloudflare_1 = require("./cloudflare");
|
|
14
|
+
const pollinations_1 = require("./pollinations");
|
|
15
|
+
const ollama_1 = require("./ollama");
|
|
16
|
+
const openai_image_compat_1 = require("./openai-image-compat");
|
|
17
|
+
Object.defineProperty(exports, "OpenAIImageCompatProvider", { enumerable: true, get: function () { return openai_image_compat_1.OpenAIImageCompatProvider; } });
|
|
18
|
+
const pollinations_image_1 = require("./pollinations-image");
|
|
19
|
+
const openai_tts_compat_1 = require("./openai-tts-compat");
|
|
20
|
+
Object.defineProperty(exports, "OpenAITTSCompatProvider", { enumerable: true, get: function () { return openai_tts_compat_1.OpenAITTSCompatProvider; } });
|
|
21
|
+
const chatRegistry = {
|
|
22
|
+
...openai_compat_1.PROVIDERS,
|
|
23
|
+
google: new google_1.GoogleProvider(),
|
|
24
|
+
cloudflare: new cloudflare_1.CloudflareProvider(),
|
|
25
|
+
pollinations: new pollinations_1.PollinationsProvider(),
|
|
26
|
+
ollama: new ollama_1.OllamaProvider(),
|
|
27
|
+
};
|
|
28
|
+
const imageRegistry = {
|
|
29
|
+
...openai_image_compat_1.IMAGE_PROVIDERS,
|
|
30
|
+
pollinations: new pollinations_image_1.PollinationsImageProvider(),
|
|
31
|
+
};
|
|
32
|
+
const ttsRegistry = {
|
|
33
|
+
...openai_tts_compat_1.TTS_PROVIDERS,
|
|
34
|
+
};
|
|
35
|
+
function getChatProvider(platform) {
|
|
36
|
+
return chatRegistry[platform];
|
|
37
|
+
}
|
|
38
|
+
function getImageProvider(platform) {
|
|
39
|
+
return imageRegistry[platform];
|
|
40
|
+
}
|
|
41
|
+
function getTTSProvider(platform) {
|
|
42
|
+
return ttsRegistry[platform];
|
|
43
|
+
}
|
|
44
|
+
function getProvider(platform, type = 'chat') {
|
|
45
|
+
if (type === 'image')
|
|
46
|
+
return imageRegistry[platform];
|
|
47
|
+
if (type === 'tts')
|
|
48
|
+
return ttsRegistry[platform];
|
|
49
|
+
return chatRegistry[platform];
|
|
50
|
+
}
|
|
51
|
+
function allProviders(type = 'chat') {
|
|
52
|
+
if (type === 'image')
|
|
53
|
+
return Object.keys(imageRegistry);
|
|
54
|
+
if (type === 'tts')
|
|
55
|
+
return Object.keys(ttsRegistry);
|
|
56
|
+
return Object.keys(chatRegistry);
|
|
57
|
+
}
|
|
58
|
+
function registerCustomProvider(platform, baseUrl, defaultModel) {
|
|
59
|
+
chatRegistry[platform] = new openai_compat_1.OpenAICompatProvider(platform, baseUrl, defaultModel);
|
|
60
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BaseProvider, ChatRequest, ChatResponse, StreamChunk } from './types';
|
|
2
|
+
export declare class OllamaProvider extends BaseProvider {
|
|
3
|
+
readonly platform = "ollama";
|
|
4
|
+
readonly baseUrl: string;
|
|
5
|
+
constructor(baseUrl?: string);
|
|
6
|
+
protected defaultModel(): string;
|
|
7
|
+
chat(_apiKey: string, req: ChatRequest): Promise<ChatResponse>;
|
|
8
|
+
streamChat(_apiKey: string, req: ChatRequest, onChunk: (chunk: StreamChunk) => void, onDone: () => void, onError: (err: Error) => void): Promise<void>;
|
|
9
|
+
healthCheck(_apiKey: string): Promise<boolean>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OllamaProvider = void 0;
|
|
4
|
+
// Ollama local provider
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
class OllamaProvider extends types_1.BaseProvider {
|
|
7
|
+
constructor(baseUrl = 'http://localhost:11434') {
|
|
8
|
+
super();
|
|
9
|
+
this.platform = 'ollama';
|
|
10
|
+
this.baseUrl = baseUrl + '/v1';
|
|
11
|
+
}
|
|
12
|
+
defaultModel() {
|
|
13
|
+
return 'llama3.2';
|
|
14
|
+
}
|
|
15
|
+
async chat(_apiKey, req) {
|
|
16
|
+
const model = req.model === 'auto' ? this.defaultModel() : req.model;
|
|
17
|
+
const res = await fetch(`${this.baseUrl}/chat/completions`, {
|
|
18
|
+
method: 'POST',
|
|
19
|
+
headers: { 'Content-Type': 'application/json' },
|
|
20
|
+
body: JSON.stringify({ ...req, model, stream: false }),
|
|
21
|
+
});
|
|
22
|
+
if (!res.ok) {
|
|
23
|
+
const text = await res.text().catch(() => '');
|
|
24
|
+
const err = new Error(`${res.status}: ${text}`);
|
|
25
|
+
err.status = res.status;
|
|
26
|
+
throw err;
|
|
27
|
+
}
|
|
28
|
+
return res.json();
|
|
29
|
+
}
|
|
30
|
+
async streamChat(_apiKey, req, onChunk, onDone, onError) {
|
|
31
|
+
const model = req.model === 'auto' ? this.defaultModel() : req.model;
|
|
32
|
+
let res;
|
|
33
|
+
try {
|
|
34
|
+
res = await fetch(`${this.baseUrl}/chat/completions`, {
|
|
35
|
+
method: 'POST',
|
|
36
|
+
headers: { 'Content-Type': 'application/json' },
|
|
37
|
+
body: JSON.stringify({ ...req, model, stream: true }),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
onError(e);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (!res.ok) {
|
|
45
|
+
const text = await res.text().catch(() => '');
|
|
46
|
+
const err = new Error(`${res.status}: ${text}`);
|
|
47
|
+
err.status = res.status;
|
|
48
|
+
onError(err);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const reader = res.body?.getReader();
|
|
52
|
+
if (!reader) {
|
|
53
|
+
onDone();
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const decoder = new TextDecoder();
|
|
57
|
+
let buffer = '';
|
|
58
|
+
try {
|
|
59
|
+
while (true) {
|
|
60
|
+
const { done, value } = await reader.read();
|
|
61
|
+
if (done)
|
|
62
|
+
break;
|
|
63
|
+
buffer += decoder.decode(value, { stream: true });
|
|
64
|
+
const lines = buffer.split('\n');
|
|
65
|
+
buffer = lines.pop() ?? '';
|
|
66
|
+
for (const line of lines) {
|
|
67
|
+
const trimmed = line.trim();
|
|
68
|
+
if (!trimmed || trimmed === 'data: [DONE]')
|
|
69
|
+
continue;
|
|
70
|
+
if (!trimmed.startsWith('data: '))
|
|
71
|
+
continue;
|
|
72
|
+
try {
|
|
73
|
+
const chunk = JSON.parse(trimmed.slice(6));
|
|
74
|
+
onChunk(chunk);
|
|
75
|
+
}
|
|
76
|
+
catch { /* skip */ }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
onError(e);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
finally {
|
|
85
|
+
reader.releaseLock();
|
|
86
|
+
}
|
|
87
|
+
onDone();
|
|
88
|
+
}
|
|
89
|
+
async healthCheck(_apiKey) {
|
|
90
|
+
try {
|
|
91
|
+
const res = await fetch(`${this.baseUrl.replace('/v1', '')}/api/tags`, { signal: AbortSignal.timeout(3000) });
|
|
92
|
+
return res.ok;
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.OllamaProvider = OllamaProvider;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BaseProvider, ChatRequest, ChatResponse, StreamChunk } from './types';
|
|
2
|
+
export declare class OpenAICompatProvider extends BaseProvider {
|
|
3
|
+
readonly platform: string;
|
|
4
|
+
readonly baseUrl: string;
|
|
5
|
+
private _defaultModel;
|
|
6
|
+
constructor(platform: string, baseUrl: string, defaultModel: string);
|
|
7
|
+
protected defaultModel(): string;
|
|
8
|
+
chat(apiKey: string, req: ChatRequest): Promise<ChatResponse>;
|
|
9
|
+
streamChat(apiKey: string, req: ChatRequest, onChunk: (chunk: StreamChunk) => void, onDone: () => void, onError: (err: Error) => void): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export declare const PROVIDERS: Record<string, OpenAICompatProvider>;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PROVIDERS = exports.OpenAICompatProvider = void 0;
|
|
4
|
+
// Generic OpenAI-compatible provider (Groq, Cerebras, Mistral, GitHub, Cohere, LLM7, OVH, OpenRouter, etc.)
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
class OpenAICompatProvider extends types_1.BaseProvider {
|
|
7
|
+
constructor(platform, baseUrl, defaultModel) {
|
|
8
|
+
super();
|
|
9
|
+
this.platform = platform;
|
|
10
|
+
this.baseUrl = baseUrl;
|
|
11
|
+
this._defaultModel = defaultModel;
|
|
12
|
+
}
|
|
13
|
+
defaultModel() {
|
|
14
|
+
return this._defaultModel;
|
|
15
|
+
}
|
|
16
|
+
async chat(apiKey, req) {
|
|
17
|
+
const headers = {
|
|
18
|
+
'Content-Type': 'application/json',
|
|
19
|
+
};
|
|
20
|
+
if (apiKey)
|
|
21
|
+
headers['Authorization'] = `Bearer ${apiKey}`;
|
|
22
|
+
const res = await fetch(`${this.baseUrl}/chat/completions`, {
|
|
23
|
+
method: 'POST',
|
|
24
|
+
headers,
|
|
25
|
+
body: JSON.stringify({ ...req, stream: false }),
|
|
26
|
+
});
|
|
27
|
+
if (!res.ok) {
|
|
28
|
+
const text = await res.text().catch(() => '');
|
|
29
|
+
const err = new Error(`${res.status}: ${text}`);
|
|
30
|
+
err.status = res.status;
|
|
31
|
+
throw err;
|
|
32
|
+
}
|
|
33
|
+
const data = await res.json();
|
|
34
|
+
return data;
|
|
35
|
+
}
|
|
36
|
+
async streamChat(apiKey, req, onChunk, onDone, onError) {
|
|
37
|
+
const headers = {
|
|
38
|
+
'Content-Type': 'application/json',
|
|
39
|
+
};
|
|
40
|
+
if (apiKey)
|
|
41
|
+
headers['Authorization'] = `Bearer ${apiKey}`;
|
|
42
|
+
let res;
|
|
43
|
+
try {
|
|
44
|
+
res = await fetch(`${this.baseUrl}/chat/completions`, {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
headers,
|
|
47
|
+
body: JSON.stringify({ ...req, stream: true }),
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
catch (e) {
|
|
51
|
+
onError(e);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (!res.ok) {
|
|
55
|
+
const text = await res.text().catch(() => '');
|
|
56
|
+
const err = new Error(`${res.status}: ${text}`);
|
|
57
|
+
err.status = res.status;
|
|
58
|
+
onError(err);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const reader = res.body?.getReader();
|
|
62
|
+
if (!reader) {
|
|
63
|
+
onDone();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const decoder = new TextDecoder();
|
|
67
|
+
let buffer = '';
|
|
68
|
+
try {
|
|
69
|
+
while (true) {
|
|
70
|
+
const { done, value } = await reader.read();
|
|
71
|
+
if (done)
|
|
72
|
+
break;
|
|
73
|
+
buffer += decoder.decode(value, { stream: true });
|
|
74
|
+
const lines = buffer.split('\n');
|
|
75
|
+
buffer = lines.pop() ?? '';
|
|
76
|
+
for (const line of lines) {
|
|
77
|
+
const trimmed = line.trim();
|
|
78
|
+
if (!trimmed || trimmed === 'data: [DONE]')
|
|
79
|
+
continue;
|
|
80
|
+
if (!trimmed.startsWith('data: '))
|
|
81
|
+
continue;
|
|
82
|
+
try {
|
|
83
|
+
const chunk = JSON.parse(trimmed.slice(6));
|
|
84
|
+
onChunk(chunk);
|
|
85
|
+
}
|
|
86
|
+
catch { /* ignore parse errors */ }
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch (e) {
|
|
91
|
+
onError(e);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
reader.releaseLock();
|
|
96
|
+
}
|
|
97
|
+
onDone();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.OpenAICompatProvider = OpenAICompatProvider;
|
|
101
|
+
// Pre-configured providers
|
|
102
|
+
exports.PROVIDERS = {
|
|
103
|
+
groq: new OpenAICompatProvider('groq', 'https://api.groq.com/openai/v1', 'llama-3.1-8b-instant'),
|
|
104
|
+
cerebras: new OpenAICompatProvider('cerebras', 'https://api.cerebras.ai/v1', 'llama3.1-8b'),
|
|
105
|
+
mistral: new OpenAICompatProvider('mistral', 'https://api.mistral.ai/v1', 'mistral-small-latest'),
|
|
106
|
+
openrouter: new OpenAICompatProvider('openrouter', 'https://openrouter.ai/api/v1', 'meta-llama/llama-3.2-3b-instruct:free'),
|
|
107
|
+
github: new OpenAICompatProvider('github', 'https://models.inference.ai.azure.com', 'gpt-4o-mini'),
|
|
108
|
+
cohere: new OpenAICompatProvider('cohere', 'https://api.cohere.com/compatibility/v1', 'command-r'),
|
|
109
|
+
llm7: new OpenAICompatProvider('llm7', 'https://llm7.io/v1', 'gpt-4o-mini'),
|
|
110
|
+
ovh: new OpenAICompatProvider('ovh', 'https://oai.endpoints.kepler.ai.cloud.ovh.net/v1', 'Llama-3.3-70B-Instruct'),
|
|
111
|
+
nvidia: new OpenAICompatProvider('nvidia', 'https://integrate.api.nvidia.com/v1', 'deepseek-ai/deepseek-v4-pro'),
|
|
112
|
+
kilo: new OpenAICompatProvider('kilo', 'https://gateway.kilo.chat/v1', 'poolside/laguna-m.1:free'),
|
|
113
|
+
opencode: new OpenAICompatProvider('opencode', 'https://api.opencode.ai/v1', 'deepseek-v4-flash-free'),
|
|
114
|
+
zhipu: new OpenAICompatProvider('zhipu', 'https://open.bigmodel.cn/api/paas/v4', 'glm-4.7-flash'),
|
|
115
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BaseImageProvider, ImageRequest, ImageResponse } from './types';
|
|
2
|
+
export declare class OpenAIImageCompatProvider extends BaseImageProvider {
|
|
3
|
+
readonly platform: string;
|
|
4
|
+
readonly baseUrl: string;
|
|
5
|
+
private _defaultModel;
|
|
6
|
+
constructor(platform: string, baseUrl: string, defaultModel: string);
|
|
7
|
+
protected defaultModel(): string;
|
|
8
|
+
generateImage(apiKey: string, req: ImageRequest): Promise<ImageResponse>;
|
|
9
|
+
}
|
|
10
|
+
export declare const IMAGE_PROVIDERS: Record<string, OpenAIImageCompatProvider>;
|