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,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IMAGE_PROVIDERS = exports.OpenAIImageCompatProvider = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
class OpenAIImageCompatProvider extends types_1.BaseImageProvider {
|
|
6
|
+
constructor(platform, baseUrl, defaultModel) {
|
|
7
|
+
super();
|
|
8
|
+
this.platform = platform;
|
|
9
|
+
this.baseUrl = baseUrl;
|
|
10
|
+
this._defaultModel = defaultModel;
|
|
11
|
+
}
|
|
12
|
+
defaultModel() {
|
|
13
|
+
return this._defaultModel;
|
|
14
|
+
}
|
|
15
|
+
async generateImage(apiKey, req) {
|
|
16
|
+
const headers = {
|
|
17
|
+
'Content-Type': 'application/json',
|
|
18
|
+
};
|
|
19
|
+
if (apiKey)
|
|
20
|
+
headers['Authorization'] = `Bearer ${apiKey}`;
|
|
21
|
+
const model = req.model === 'auto' ? this.defaultModel() : req.model;
|
|
22
|
+
const res = await fetch(`${this.baseUrl}/images/generations`, {
|
|
23
|
+
method: 'POST',
|
|
24
|
+
headers,
|
|
25
|
+
body: JSON.stringify({ ...req, model }),
|
|
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
|
+
return res.json();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.OpenAIImageCompatProvider = OpenAIImageCompatProvider;
|
|
37
|
+
exports.IMAGE_PROVIDERS = {
|
|
38
|
+
openrouter: new OpenAIImageCompatProvider('openrouter', 'https://openrouter.ai/api/v1', 'dall-e-3'),
|
|
39
|
+
github: new OpenAIImageCompatProvider('github', 'https://models.inference.ai.azure.com', 'dall-e-3'),
|
|
40
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BaseTTSProvider, TTSRequest } from './types';
|
|
2
|
+
export declare class OpenAITTSCompatProvider extends BaseTTSProvider {
|
|
3
|
+
readonly platform: string;
|
|
4
|
+
readonly baseUrl: string;
|
|
5
|
+
private _defaultModel;
|
|
6
|
+
constructor(platform: string, baseUrl: string, defaultModel: string);
|
|
7
|
+
protected defaultModel(): string;
|
|
8
|
+
textToSpeech(apiKey: string, req: TTSRequest): Promise<ArrayBuffer>;
|
|
9
|
+
}
|
|
10
|
+
export declare const TTS_PROVIDERS: Record<string, OpenAITTSCompatProvider>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TTS_PROVIDERS = exports.OpenAITTSCompatProvider = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
class OpenAITTSCompatProvider extends types_1.BaseTTSProvider {
|
|
6
|
+
constructor(platform, baseUrl, defaultModel) {
|
|
7
|
+
super();
|
|
8
|
+
this.platform = platform;
|
|
9
|
+
this.baseUrl = baseUrl;
|
|
10
|
+
this._defaultModel = defaultModel;
|
|
11
|
+
}
|
|
12
|
+
defaultModel() {
|
|
13
|
+
return this._defaultModel;
|
|
14
|
+
}
|
|
15
|
+
async textToSpeech(apiKey, req) {
|
|
16
|
+
const headers = {
|
|
17
|
+
'Content-Type': 'application/json',
|
|
18
|
+
};
|
|
19
|
+
if (apiKey)
|
|
20
|
+
headers['Authorization'] = `Bearer ${apiKey}`;
|
|
21
|
+
const model = req.model === 'auto' ? this.defaultModel() : req.model;
|
|
22
|
+
const res = await fetch(`${this.baseUrl}/audio/speech`, {
|
|
23
|
+
method: 'POST',
|
|
24
|
+
headers,
|
|
25
|
+
body: JSON.stringify({ ...req, model }),
|
|
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
|
+
return res.arrayBuffer();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.OpenAITTSCompatProvider = OpenAITTSCompatProvider;
|
|
37
|
+
exports.TTS_PROVIDERS = {
|
|
38
|
+
openrouter: new OpenAITTSCompatProvider('openrouter', 'https://openrouter.ai/api/v1', 'tts-1'),
|
|
39
|
+
github: new OpenAITTSCompatProvider('github', 'https://models.inference.ai.azure.com', 'tts-1'),
|
|
40
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { BaseImageProvider, ImageRequest, ImageResponse } from './types';
|
|
2
|
+
export declare class PollinationsImageProvider extends BaseImageProvider {
|
|
3
|
+
readonly platform = "pollinations";
|
|
4
|
+
readonly baseUrl = "https://image.pollinations.ai";
|
|
5
|
+
protected defaultModel(): string;
|
|
6
|
+
generateImage(_apiKey: string, req: ImageRequest): Promise<ImageResponse>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PollinationsImageProvider = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
class PollinationsImageProvider extends types_1.BaseImageProvider {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(...arguments);
|
|
8
|
+
this.platform = 'pollinations';
|
|
9
|
+
this.baseUrl = 'https://image.pollinations.ai';
|
|
10
|
+
}
|
|
11
|
+
defaultModel() {
|
|
12
|
+
return 'flux';
|
|
13
|
+
}
|
|
14
|
+
async generateImage(_apiKey, req) {
|
|
15
|
+
const model = req.model === 'auto' ? this.defaultModel() : req.model;
|
|
16
|
+
let width = 1024;
|
|
17
|
+
let height = 1024;
|
|
18
|
+
if (req.size) {
|
|
19
|
+
const parts = req.size.split('x');
|
|
20
|
+
width = parseInt(parts[0]) || 1024;
|
|
21
|
+
height = parseInt(parts[1]) || 1024;
|
|
22
|
+
}
|
|
23
|
+
const params = new URLSearchParams();
|
|
24
|
+
params.set('width', String(width));
|
|
25
|
+
params.set('height', String(height));
|
|
26
|
+
if (model)
|
|
27
|
+
params.set('model', model);
|
|
28
|
+
params.set('nologo', 'true');
|
|
29
|
+
const imageUrl = `${this.baseUrl}/prompt/${encodeURIComponent(req.prompt)}?${params}`;
|
|
30
|
+
const now = Math.floor(Date.now() / 1000);
|
|
31
|
+
const data = [];
|
|
32
|
+
if (req.response_format === 'b64_json') {
|
|
33
|
+
const res = await fetch(imageUrl);
|
|
34
|
+
if (!res.ok) {
|
|
35
|
+
const text = await res.text().catch(() => '');
|
|
36
|
+
const err = new Error(`${res.status}: ${text}`);
|
|
37
|
+
err.status = res.status;
|
|
38
|
+
throw err;
|
|
39
|
+
}
|
|
40
|
+
const buf = await res.arrayBuffer();
|
|
41
|
+
const b64 = Buffer.from(buf).toString('base64');
|
|
42
|
+
data.push({ b64_json: b64 });
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
data.push({ url: imageUrl });
|
|
46
|
+
}
|
|
47
|
+
return { created: now, data };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.PollinationsImageProvider = PollinationsImageProvider;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BaseProvider, ChatRequest, ChatResponse, StreamChunk } from './types';
|
|
2
|
+
export declare class PollinationsProvider extends BaseProvider {
|
|
3
|
+
readonly platform = "pollinations";
|
|
4
|
+
readonly baseUrl = "https://text.pollinations.ai";
|
|
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
|
+
healthCheck(_apiKey: string): Promise<boolean>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PollinationsProvider = void 0;
|
|
4
|
+
// Pollinations.ai — no API key required
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
class PollinationsProvider extends types_1.BaseProvider {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(...arguments);
|
|
9
|
+
this.platform = 'pollinations';
|
|
10
|
+
this.baseUrl = 'https://text.pollinations.ai';
|
|
11
|
+
}
|
|
12
|
+
defaultModel() {
|
|
13
|
+
return 'openai-fast';
|
|
14
|
+
}
|
|
15
|
+
async chat(_apiKey, req) {
|
|
16
|
+
const model = req.model === 'auto' ? this.defaultModel() : req.model;
|
|
17
|
+
const res = await fetch(`${this.baseUrl}/openai`, {
|
|
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
|
+
const data = await res.json();
|
|
29
|
+
return data;
|
|
30
|
+
}
|
|
31
|
+
async streamChat(_apiKey, req, onChunk, onDone, onError) {
|
|
32
|
+
// Pollinations doesn't support streaming well; fall back to non-streaming
|
|
33
|
+
try {
|
|
34
|
+
const resp = await this.chat('', req);
|
|
35
|
+
const id = (0, types_1.makeId)();
|
|
36
|
+
onChunk({
|
|
37
|
+
id,
|
|
38
|
+
object: 'chat.completion.chunk',
|
|
39
|
+
created: Math.floor(Date.now() / 1000),
|
|
40
|
+
model: resp.model,
|
|
41
|
+
choices: [
|
|
42
|
+
{
|
|
43
|
+
index: 0,
|
|
44
|
+
delta: { role: 'assistant', content: resp.choices[0]?.message?.content ?? '' },
|
|
45
|
+
finish_reason: null,
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
});
|
|
49
|
+
onChunk({
|
|
50
|
+
id,
|
|
51
|
+
object: 'chat.completion.chunk',
|
|
52
|
+
created: Math.floor(Date.now() / 1000),
|
|
53
|
+
model: resp.model,
|
|
54
|
+
choices: [{ index: 0, delta: {}, finish_reason: 'stop' }],
|
|
55
|
+
});
|
|
56
|
+
onDone();
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
onError(e);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async healthCheck(_apiKey) {
|
|
63
|
+
try {
|
|
64
|
+
await this.chat('', {
|
|
65
|
+
model: 'openai',
|
|
66
|
+
messages: [{ role: 'user', content: 'hi' }],
|
|
67
|
+
max_tokens: 5,
|
|
68
|
+
});
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.PollinationsProvider = PollinationsProvider;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
export type ChatMessage = {
|
|
2
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
3
|
+
content: string | ContentPart[];
|
|
4
|
+
tool_calls?: ToolCall[];
|
|
5
|
+
tool_call_id?: string;
|
|
6
|
+
name?: string;
|
|
7
|
+
};
|
|
8
|
+
export type ContentPart = {
|
|
9
|
+
type: 'text';
|
|
10
|
+
text: string;
|
|
11
|
+
} | {
|
|
12
|
+
type: 'image_url';
|
|
13
|
+
image_url: {
|
|
14
|
+
url: string;
|
|
15
|
+
detail?: string;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export type ToolCall = {
|
|
19
|
+
id: string;
|
|
20
|
+
type: 'function';
|
|
21
|
+
function: {
|
|
22
|
+
name: string;
|
|
23
|
+
arguments: string;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export type Tool = {
|
|
27
|
+
type: 'function';
|
|
28
|
+
function: {
|
|
29
|
+
name: string;
|
|
30
|
+
description?: string;
|
|
31
|
+
parameters?: Record<string, unknown>;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
export type ChatRequest = {
|
|
35
|
+
model: string;
|
|
36
|
+
messages: ChatMessage[];
|
|
37
|
+
stream?: boolean;
|
|
38
|
+
temperature?: number;
|
|
39
|
+
max_tokens?: number;
|
|
40
|
+
top_p?: number;
|
|
41
|
+
tools?: Tool[];
|
|
42
|
+
tool_choice?: 'auto' | 'none' | 'required' | {
|
|
43
|
+
type: 'function';
|
|
44
|
+
function: {
|
|
45
|
+
name: string;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
stop?: string | string[];
|
|
49
|
+
};
|
|
50
|
+
export type ChatResponse = {
|
|
51
|
+
id: string;
|
|
52
|
+
object: 'chat.completion';
|
|
53
|
+
created: number;
|
|
54
|
+
model: string;
|
|
55
|
+
choices: Array<{
|
|
56
|
+
index: number;
|
|
57
|
+
message: {
|
|
58
|
+
role: 'assistant';
|
|
59
|
+
content: string | null;
|
|
60
|
+
tool_calls?: ToolCall[];
|
|
61
|
+
};
|
|
62
|
+
finish_reason: string;
|
|
63
|
+
}>;
|
|
64
|
+
usage?: {
|
|
65
|
+
prompt_tokens: number;
|
|
66
|
+
completion_tokens: number;
|
|
67
|
+
total_tokens: number;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
export type StreamChunk = {
|
|
71
|
+
id: string;
|
|
72
|
+
object: 'chat.completion.chunk';
|
|
73
|
+
created: number;
|
|
74
|
+
model: string;
|
|
75
|
+
choices: Array<{
|
|
76
|
+
index: number;
|
|
77
|
+
delta: {
|
|
78
|
+
role?: 'assistant';
|
|
79
|
+
content?: string;
|
|
80
|
+
tool_calls?: Partial<ToolCall>[];
|
|
81
|
+
};
|
|
82
|
+
finish_reason: string | null;
|
|
83
|
+
}>;
|
|
84
|
+
};
|
|
85
|
+
export declare abstract class BaseProvider {
|
|
86
|
+
abstract readonly platform: string;
|
|
87
|
+
abstract readonly baseUrl: string;
|
|
88
|
+
protected fetchJSON<T>(url: string, options: RequestInit): Promise<T>;
|
|
89
|
+
abstract chat(apiKey: string, req: ChatRequest): Promise<ChatResponse>;
|
|
90
|
+
abstract streamChat(apiKey: string, req: ChatRequest, onChunk: (chunk: StreamChunk) => void, onDone: () => void, onError: (err: Error) => void): Promise<void>;
|
|
91
|
+
/** Default implementation: call the non-streaming endpoint */
|
|
92
|
+
healthCheck(apiKey: string): Promise<boolean>;
|
|
93
|
+
protected abstract defaultModel(): string;
|
|
94
|
+
}
|
|
95
|
+
export type ImageRequest = {
|
|
96
|
+
prompt: string;
|
|
97
|
+
n?: number;
|
|
98
|
+
size?: string;
|
|
99
|
+
model?: string;
|
|
100
|
+
quality?: string;
|
|
101
|
+
response_format?: 'url' | 'b64_json';
|
|
102
|
+
style?: string;
|
|
103
|
+
};
|
|
104
|
+
export type ImageResponse = {
|
|
105
|
+
created: number;
|
|
106
|
+
data: Array<{
|
|
107
|
+
url?: string;
|
|
108
|
+
b64_json?: string;
|
|
109
|
+
}>;
|
|
110
|
+
};
|
|
111
|
+
export declare abstract class BaseImageProvider {
|
|
112
|
+
abstract readonly platform: string;
|
|
113
|
+
abstract readonly baseUrl: string;
|
|
114
|
+
abstract generateImage(apiKey: string, req: ImageRequest): Promise<ImageResponse>;
|
|
115
|
+
protected abstract defaultModel(): string;
|
|
116
|
+
healthCheck(apiKey: string): Promise<boolean>;
|
|
117
|
+
}
|
|
118
|
+
export type TTSRequest = {
|
|
119
|
+
model: string;
|
|
120
|
+
input: string;
|
|
121
|
+
voice?: string;
|
|
122
|
+
response_format?: 'mp3' | 'opus' | 'aac' | 'flac' | 'wav' | 'pcm';
|
|
123
|
+
speed?: number;
|
|
124
|
+
};
|
|
125
|
+
export declare abstract class BaseTTSProvider {
|
|
126
|
+
abstract readonly platform: string;
|
|
127
|
+
abstract readonly baseUrl: string;
|
|
128
|
+
abstract textToSpeech(apiKey: string, req: TTSRequest): Promise<ArrayBuffer>;
|
|
129
|
+
protected abstract defaultModel(): string;
|
|
130
|
+
healthCheck(apiKey: string): Promise<boolean>;
|
|
131
|
+
}
|
|
132
|
+
export declare function makeId(): string;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseTTSProvider = exports.BaseImageProvider = exports.BaseProvider = void 0;
|
|
4
|
+
exports.makeId = makeId;
|
|
5
|
+
class BaseProvider {
|
|
6
|
+
async fetchJSON(url, options) {
|
|
7
|
+
const res = await fetch(url, options);
|
|
8
|
+
if (!res.ok) {
|
|
9
|
+
const text = await res.text().catch(() => '');
|
|
10
|
+
const err = new Error(`${res.status} ${res.statusText}: ${text}`);
|
|
11
|
+
err.status = res.status;
|
|
12
|
+
throw err;
|
|
13
|
+
}
|
|
14
|
+
return res.json();
|
|
15
|
+
}
|
|
16
|
+
/** Default implementation: call the non-streaming endpoint */
|
|
17
|
+
async healthCheck(apiKey) {
|
|
18
|
+
try {
|
|
19
|
+
await this.chat(apiKey, {
|
|
20
|
+
model: this.defaultModel(),
|
|
21
|
+
messages: [{ role: 'user', content: 'hi' }],
|
|
22
|
+
max_tokens: 1,
|
|
23
|
+
});
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.BaseProvider = BaseProvider;
|
|
32
|
+
class BaseImageProvider {
|
|
33
|
+
async healthCheck(apiKey) {
|
|
34
|
+
try {
|
|
35
|
+
await this.generateImage(apiKey, {
|
|
36
|
+
prompt: 'hi',
|
|
37
|
+
n: 1,
|
|
38
|
+
size: '256x256',
|
|
39
|
+
});
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.BaseImageProvider = BaseImageProvider;
|
|
48
|
+
class BaseTTSProvider {
|
|
49
|
+
async healthCheck(apiKey) {
|
|
50
|
+
try {
|
|
51
|
+
await this.textToSpeech(apiKey, {
|
|
52
|
+
model: this.defaultModel(),
|
|
53
|
+
input: 'hi',
|
|
54
|
+
voice: 'alloy',
|
|
55
|
+
});
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.BaseTTSProvider = BaseTTSProvider;
|
|
64
|
+
function makeId() {
|
|
65
|
+
return 'chatcmpl-' + Math.random().toString(36).slice(2, 12);
|
|
66
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const analyticsRouter: import("express-serve-static-core").Router;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.analyticsRouter = void 0;
|
|
4
|
+
const express_1 = require("express");
|
|
5
|
+
const db_1 = require("../../db");
|
|
6
|
+
exports.analyticsRouter = (0, express_1.Router)();
|
|
7
|
+
exports.analyticsRouter.get('/analytics', (req, res) => {
|
|
8
|
+
const hours = Number(req.query.hours) || 24;
|
|
9
|
+
return res.json((0, db_1.getAnalytics)(hours));
|
|
10
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.authRouter = void 0;
|
|
4
|
+
exports.requireAuth = requireAuth;
|
|
5
|
+
const express_1 = require("express");
|
|
6
|
+
const nanoid_1 = require("nanoid");
|
|
7
|
+
const db_1 = require("../../db");
|
|
8
|
+
const crypto_1 = require("../../crypto");
|
|
9
|
+
exports.authRouter = (0, express_1.Router)();
|
|
10
|
+
// Middleware: protect API routes with session token
|
|
11
|
+
function requireAuth(req, res, next) {
|
|
12
|
+
// Skip auth for setup endpoint
|
|
13
|
+
const token = req.headers['x-session-token']
|
|
14
|
+
|| req.cookies?.['freellm-session'];
|
|
15
|
+
if (!token || !(0, db_1.validateSession)(token)) {
|
|
16
|
+
res.status(401).json({ error: 'Unauthorized' });
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
next();
|
|
20
|
+
}
|
|
21
|
+
// Check if first-time setup is needed
|
|
22
|
+
exports.authRouter.get('/auth/status', (_req, res) => {
|
|
23
|
+
const hasPassword = !!(0, db_1.getSetting)('password_hash');
|
|
24
|
+
res.json({ setup: !hasPassword });
|
|
25
|
+
});
|
|
26
|
+
// Setup (first run)
|
|
27
|
+
exports.authRouter.post('/auth/setup', (req, res) => {
|
|
28
|
+
const { password } = req.body;
|
|
29
|
+
if (!password || password.length < 8) {
|
|
30
|
+
return res.status(400).json({ error: 'Password must be at least 8 characters' });
|
|
31
|
+
}
|
|
32
|
+
if ((0, db_1.getSetting)('password_hash')) {
|
|
33
|
+
return res.status(409).json({ error: 'Already set up' });
|
|
34
|
+
}
|
|
35
|
+
(0, db_1.setSetting)('password_hash', (0, crypto_1.hashPassword)(password));
|
|
36
|
+
const token = (0, nanoid_1.nanoid)(48);
|
|
37
|
+
(0, db_1.createSession)(token);
|
|
38
|
+
res.cookie('freellm-session', token, { httpOnly: true, sameSite: 'strict' });
|
|
39
|
+
return res.json({ token });
|
|
40
|
+
});
|
|
41
|
+
// Login
|
|
42
|
+
exports.authRouter.post('/auth/login', (req, res) => {
|
|
43
|
+
const { password } = req.body;
|
|
44
|
+
const hash = (0, db_1.getSetting)('password_hash');
|
|
45
|
+
if (!hash || !password || !(0, crypto_1.verifyPassword)(password, hash)) {
|
|
46
|
+
return res.status(401).json({ error: 'Invalid password' });
|
|
47
|
+
}
|
|
48
|
+
const token = (0, nanoid_1.nanoid)(48);
|
|
49
|
+
(0, db_1.createSession)(token);
|
|
50
|
+
res.cookie('freellm-session', token, { httpOnly: true, sameSite: 'strict' });
|
|
51
|
+
return res.json({ token });
|
|
52
|
+
});
|
|
53
|
+
// Logout
|
|
54
|
+
exports.authRouter.post('/auth/logout', (req, res) => {
|
|
55
|
+
const token = req.headers['x-session-token'] || req.cookies?.['freellm-session'];
|
|
56
|
+
if (token)
|
|
57
|
+
(0, db_1.deleteSession)(token);
|
|
58
|
+
res.clearCookie('freellm-session');
|
|
59
|
+
return res.json({ ok: true });
|
|
60
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const keysRouter: import("express-serve-static-core").Router;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.keysRouter = void 0;
|
|
4
|
+
const express_1 = require("express");
|
|
5
|
+
const db_1 = require("../../db");
|
|
6
|
+
const db_2 = require("../../db");
|
|
7
|
+
const crypto_1 = require("../../crypto");
|
|
8
|
+
exports.keysRouter = (0, express_1.Router)();
|
|
9
|
+
// Get unified API key (or generate one)
|
|
10
|
+
exports.keysRouter.get('/keys/unified', (_req, res) => {
|
|
11
|
+
let key = (0, db_2.getSetting)('unified_key');
|
|
12
|
+
if (!key) {
|
|
13
|
+
key = (0, crypto_1.generateUnifiedKey)();
|
|
14
|
+
(0, db_2.setSetting)('unified_key', key);
|
|
15
|
+
}
|
|
16
|
+
return res.json({ key });
|
|
17
|
+
});
|
|
18
|
+
// Regenerate unified key
|
|
19
|
+
exports.keysRouter.post('/keys/unified/regenerate', (_req, res) => {
|
|
20
|
+
const key = (0, crypto_1.generateUnifiedKey)();
|
|
21
|
+
(0, db_2.setSetting)('unified_key', key);
|
|
22
|
+
return res.json({ key });
|
|
23
|
+
});
|
|
24
|
+
// List provider keys (masked)
|
|
25
|
+
exports.keysRouter.get('/keys', (_req, res) => {
|
|
26
|
+
const keys = (0, db_1.listKeys)().map((k) => {
|
|
27
|
+
let masked = '****';
|
|
28
|
+
try {
|
|
29
|
+
const raw = (0, db_1.decryptKey)(k.key_enc);
|
|
30
|
+
masked = raw.slice(0, 6) + '****' + raw.slice(-4);
|
|
31
|
+
}
|
|
32
|
+
catch { /* ignore */ }
|
|
33
|
+
return {
|
|
34
|
+
id: k.id,
|
|
35
|
+
platform: k.platform,
|
|
36
|
+
label: k.label,
|
|
37
|
+
masked,
|
|
38
|
+
enabled: !!k.enabled,
|
|
39
|
+
status: k.status,
|
|
40
|
+
last_check: k.last_check,
|
|
41
|
+
created_at: k.created_at,
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
return res.json(keys);
|
|
45
|
+
});
|
|
46
|
+
// Add provider key
|
|
47
|
+
exports.keysRouter.post('/keys', (req, res) => {
|
|
48
|
+
const { platform, key, label } = req.body;
|
|
49
|
+
if (!platform || !key) {
|
|
50
|
+
return res.status(400).json({ error: 'platform and key are required' });
|
|
51
|
+
}
|
|
52
|
+
const id = (0, db_1.addKey)(platform, key, label);
|
|
53
|
+
return res.json({ id });
|
|
54
|
+
});
|
|
55
|
+
// Delete provider key
|
|
56
|
+
exports.keysRouter.delete('/keys/:id', (req, res) => {
|
|
57
|
+
(0, db_1.deleteKey)(Number(req.params.id));
|
|
58
|
+
return res.json({ ok: true });
|
|
59
|
+
});
|
|
60
|
+
// Toggle provider key
|
|
61
|
+
exports.keysRouter.patch('/keys/:id/toggle', (req, res) => {
|
|
62
|
+
const { enabled } = req.body;
|
|
63
|
+
(0, db_1.toggleKey)(Number(req.params.id), !!enabled);
|
|
64
|
+
return res.json({ ok: true });
|
|
65
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const modelsApiRouter: import("express-serve-static-core").Router;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.modelsApiRouter = void 0;
|
|
4
|
+
const express_1 = require("express");
|
|
5
|
+
const db_1 = require("../../db");
|
|
6
|
+
exports.modelsApiRouter = (0, express_1.Router)();
|
|
7
|
+
exports.modelsApiRouter.get('/models', (req, res) => {
|
|
8
|
+
const type = req.query.type;
|
|
9
|
+
if (!type || type === 'all') {
|
|
10
|
+
const all = [...(0, db_1.listModels)('chat'), ...(0, db_1.listModels)('image'), ...(0, db_1.listModels)('tts')];
|
|
11
|
+
return res.json(all);
|
|
12
|
+
}
|
|
13
|
+
return res.json((0, db_1.listModels)(type));
|
|
14
|
+
});
|
|
15
|
+
exports.modelsApiRouter.patch('/models/:id/toggle', (req, res) => {
|
|
16
|
+
const { enabled } = req.body;
|
|
17
|
+
(0, db_1.toggleModel)(Number(req.params.id), !!enabled);
|
|
18
|
+
return res.json({ ok: true });
|
|
19
|
+
});
|
|
20
|
+
exports.modelsApiRouter.patch('/models/:id/order', (req, res) => {
|
|
21
|
+
const { order } = req.body;
|
|
22
|
+
if (order === undefined)
|
|
23
|
+
return res.status(400).json({ error: 'order required' });
|
|
24
|
+
(0, db_1.updateModelOrder)(Number(req.params.id), order);
|
|
25
|
+
return res.json({ ok: true });
|
|
26
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const audioRouter: import("express-serve-static-core").Router;
|