dsh-open-model 1.0.0 → 1.1.1
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 +68 -16
- package/dist/client.js +2137 -0
- package/dist/index.js +27974 -0
- package/models.json +169 -0
- package/native/credential-key +0 -0
- package/notices/LICENSE +54 -0
- package/notices/THIRD_PARTY_NOTICES.md +424 -0
- package/package.json +41 -9
- package/catalog.js +0 -182
- package/client.js +0 -709
- package/index.js +0 -96
- package/model-availability.js +0 -99
- package/model-management.js +0 -111
- package/stream.js +0 -382
- package/updates.js +0 -141
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-open-model",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "DSH插件:为 DSH 提供免费模型,让 DSH 直接使用你的 ChatGPT 套餐额度。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "GPL-3.0-or-later",
|
|
7
7
|
"repository": {
|
|
@@ -9,18 +9,50 @@
|
|
|
9
9
|
"url": "git+https://github.com/houyanchao/dsh-open-model.git"
|
|
10
10
|
},
|
|
11
11
|
"exports": {
|
|
12
|
-
".": "./index.js",
|
|
13
|
-
"./client": "./client.js",
|
|
12
|
+
".": "./dist/index.js",
|
|
13
|
+
"./client": "./dist/client.js",
|
|
14
14
|
"./package.json": "./package.json"
|
|
15
15
|
},
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
"imports": {
|
|
17
|
+
"#openmodel-models": "./models.json"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"models.json",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"native/credential-key",
|
|
24
|
+
"cordis.patch.yml",
|
|
25
|
+
"notices"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "node scripts/build.mjs && node scripts/build-native.mjs",
|
|
29
|
+
"test": "node --test",
|
|
30
|
+
"prepack": "npm run build"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": "^22.19.0 || >=24.0.0"
|
|
34
|
+
},
|
|
19
35
|
"dsh": {
|
|
20
|
-
"bundle": {
|
|
36
|
+
"bundle": {
|
|
37
|
+
"patch": "./cordis.patch.yml"
|
|
38
|
+
},
|
|
21
39
|
"client": {
|
|
22
|
-
"inject": [
|
|
40
|
+
"inject": [
|
|
41
|
+
"@deepseek-ai/dsh-client-locale",
|
|
42
|
+
"@deepseek-ai/dsh-client-ui-sidebar",
|
|
43
|
+
"@deepseek-ai/dsh-client-ui-conversation",
|
|
44
|
+
"@deepseek-ai/dsh-client-ui-model-selection"
|
|
45
|
+
],
|
|
23
46
|
"platform": "web"
|
|
24
47
|
}
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"esbuild": "0.28.2",
|
|
51
|
+
"jose": "6.2.12",
|
|
52
|
+
"openai": "7.21.0",
|
|
53
|
+
"proper-lockfile": "4.1.2"
|
|
54
|
+
},
|
|
55
|
+
"overrides": {
|
|
56
|
+
"graceful-fs": "4.2.11"
|
|
25
57
|
}
|
|
26
58
|
}
|
package/catalog.js
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
export const KILO_BASE_URL = 'https://ai-gateway.kilo.ai/api/v1';
|
|
2
|
-
export const KILO_PROVIDER = 'dsh-free-model-kilo';
|
|
3
|
-
|
|
4
|
-
const REQUEST_TIMEOUT_MS = 10_000;
|
|
5
|
-
const FRESH_MS = 5 * 60_000;
|
|
6
|
-
const MAX_STALE_MS = 20 * 60_000;
|
|
7
|
-
const RETRY_MS = 30_000;
|
|
8
|
-
|
|
9
|
-
export class KiloCatalogError extends Error {
|
|
10
|
-
constructor(message, options = {}) {
|
|
11
|
-
super(message, { cause: options.cause });
|
|
12
|
-
this.name = 'KiloCatalogError';
|
|
13
|
-
this.code = options.code;
|
|
14
|
-
this.status = options.status;
|
|
15
|
-
this.providerRetryAfterMs = options.providerRetryAfterMs;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function isCatalogConnectionError(error) {
|
|
20
|
-
return error instanceof KiloCatalogError && (error.code === 'TRANSPORT' || error.code === 'TIMEOUT');
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function isZero(price) {
|
|
24
|
-
return price === 0 || (typeof price === 'string' && /^0+(?:\.0+)?(?:e[+-]?\d+)?$/i.test(price));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function isFreeTextModel(row, now) {
|
|
28
|
-
if (!row || typeof row.id !== 'string' || !row.id.trim() || row.isFree !== true) return false;
|
|
29
|
-
|
|
30
|
-
const pricing = row.pricing;
|
|
31
|
-
if (!pricing || !isZero(pricing.prompt) || !isZero(pricing.completion)) return false;
|
|
32
|
-
if (!Object.entries(pricing).every(([key, value]) => key === 'discount' || isZero(value))) return false;
|
|
33
|
-
|
|
34
|
-
const { input_modalities: input = [], output_modalities: output = [] } = row.architecture ?? {};
|
|
35
|
-
if (!Array.isArray(input) || !input.includes('text') || !Array.isArray(output) || !output.includes('text')) {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
return !row.expiration_date || Date.parse(row.expiration_date) > now;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function positiveInteger(value) {
|
|
42
|
-
return Number.isSafeInteger(value) && value > 0 ? value : undefined;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function modelInfo(row) {
|
|
46
|
-
const contextWindow = positiveInteger(row.context_length) ?? positiveInteger(row.top_provider?.context_length);
|
|
47
|
-
const maxCompletionTokens = positiveInteger(row.top_provider?.max_completion_tokens);
|
|
48
|
-
const supportedParameters = Array.isArray(row.supported_parameters)
|
|
49
|
-
? row.supported_parameters.filter(parameter => typeof parameter === 'string')
|
|
50
|
-
: [];
|
|
51
|
-
const description = typeof row.description === 'string' ? row.description.trim() : '';
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
provider: KILO_PROVIDER,
|
|
55
|
-
id: row.id,
|
|
56
|
-
name: (typeof row.name === 'string' ? row.name.trim() : '') || row.id,
|
|
57
|
-
inputModalities: ['text'],
|
|
58
|
-
...(description ? { description } : {}),
|
|
59
|
-
...(contextWindow ? { context: { contextWindow } } : {}),
|
|
60
|
-
...(maxCompletionTokens ? { maxCompletionTokens } : {}),
|
|
61
|
-
supportedParameters,
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function retryAfterMs(header, now) {
|
|
66
|
-
if (!header) return undefined;
|
|
67
|
-
const seconds = /^\d+(?:\.\d+)?$/.test(header) ? Number(header) : NaN;
|
|
68
|
-
const duration = Number.isFinite(seconds) ? seconds * 1000 : Date.parse(header) - now;
|
|
69
|
-
return Number.isSafeInteger(duration) && duration > 0 ? duration : undefined;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async function requestCatalog(fetchImpl, now) {
|
|
73
|
-
const timeout = AbortSignal.timeout(REQUEST_TIMEOUT_MS);
|
|
74
|
-
let response;
|
|
75
|
-
|
|
76
|
-
try {
|
|
77
|
-
response = await fetchImpl(`${KILO_BASE_URL}/models`, {
|
|
78
|
-
headers: { accept: 'application/json' },
|
|
79
|
-
redirect: 'error',
|
|
80
|
-
signal: timeout,
|
|
81
|
-
});
|
|
82
|
-
} catch (cause) {
|
|
83
|
-
throw new KiloCatalogError('无法连接 Kilo 模型目录,请稍后重试。', {
|
|
84
|
-
code: timeout.aborted ? 'TIMEOUT' : 'TRANSPORT',
|
|
85
|
-
cause,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (!response.ok) {
|
|
90
|
-
const status = response.status;
|
|
91
|
-
await response.body?.cancel().catch(() => {});
|
|
92
|
-
throw new KiloCatalogError(
|
|
93
|
-
status === 429 ? 'Kilo 模型目录暂时限流,请稍后重试。' : `Kilo 模型目录请求失败(HTTP ${status})。`,
|
|
94
|
-
{
|
|
95
|
-
code: status === 429 ? 'RATE_LIMIT' : 'KILO_CATALOG_HTTP',
|
|
96
|
-
status,
|
|
97
|
-
providerRetryAfterMs: retryAfterMs(response.headers.get('retry-after'), now()),
|
|
98
|
-
},
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
let payload;
|
|
103
|
-
try {
|
|
104
|
-
payload = await response.json();
|
|
105
|
-
} catch (cause) {
|
|
106
|
-
throw new KiloCatalogError('Kilo 模型目录响应无法读取。', {
|
|
107
|
-
code: timeout.aborted ? 'TIMEOUT' : 'KILO_CATALOG_INVALID',
|
|
108
|
-
cause,
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
if (!Array.isArray(payload?.data)) {
|
|
112
|
-
throw new KiloCatalogError('Kilo 返回的模型目录格式有误。', { code: 'KILO_CATALOG_INVALID' });
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const models = new Map();
|
|
116
|
-
const fetchedAt = now();
|
|
117
|
-
for (const row of payload.data) {
|
|
118
|
-
if (isFreeTextModel(row, fetchedAt) && !models.has(row.id)) {
|
|
119
|
-
models.set(row.id, { model: modelInfo(row), expiresAt: row.expiration_date ? Date.parse(row.expiration_date) : Infinity });
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
return { models, fetchedAt };
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function waitWithSignal(promise, signal) {
|
|
126
|
-
if (!signal) return promise;
|
|
127
|
-
signal.throwIfAborted();
|
|
128
|
-
let abort;
|
|
129
|
-
const aborted = new Promise((_, reject) => {
|
|
130
|
-
abort = () => reject(signal.reason);
|
|
131
|
-
signal.addEventListener('abort', abort, { once: true });
|
|
132
|
-
});
|
|
133
|
-
return Promise.race([promise, aborted]).finally(() => signal.removeEventListener('abort', abort));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/** Refresh DSH's model catalog on demand, sharing one anonymous request between readers. */
|
|
137
|
-
export function createKiloCatalog({ fetch: fetchImpl = globalThis.fetch, now = Date.now } = {}) {
|
|
138
|
-
let snapshot;
|
|
139
|
-
let lastError;
|
|
140
|
-
let nextRefresh = 0;
|
|
141
|
-
let inFlight;
|
|
142
|
-
|
|
143
|
-
function currentSnapshot() {
|
|
144
|
-
if (snapshot && now() - snapshot.fetchedAt < MAX_STALE_MS) return snapshot.models;
|
|
145
|
-
throw lastError;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
async function load(signal, refresh = false) {
|
|
149
|
-
signal?.throwIfAborted();
|
|
150
|
-
const manualRefresh = refresh && (!lastError || isCatalogConnectionError(lastError));
|
|
151
|
-
if (now() >= nextRefresh || manualRefresh || (!snapshot && !lastError)) {
|
|
152
|
-
inFlight ??= requestCatalog(fetchImpl, now).then(
|
|
153
|
-
result => {
|
|
154
|
-
snapshot = result;
|
|
155
|
-
lastError = undefined;
|
|
156
|
-
nextRefresh = result.fetchedAt + FRESH_MS;
|
|
157
|
-
},
|
|
158
|
-
error => {
|
|
159
|
-
lastError = error;
|
|
160
|
-
nextRefresh = now() + Math.max(RETRY_MS, error.providerRetryAfterMs ?? 0);
|
|
161
|
-
},
|
|
162
|
-
).finally(() => { inFlight = undefined; });
|
|
163
|
-
await waitWithSignal(inFlight, signal);
|
|
164
|
-
}
|
|
165
|
-
if (refresh && lastError) throw lastError;
|
|
166
|
-
return currentSnapshot();
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return {
|
|
170
|
-
async listModels(signal, { refresh = false } = {}) {
|
|
171
|
-
const entries = await load(signal, refresh);
|
|
172
|
-
return [...entries.values()]
|
|
173
|
-
.filter(entry => entry.expiresAt > now())
|
|
174
|
-
.map(entry => structuredClone(entry.model));
|
|
175
|
-
},
|
|
176
|
-
|
|
177
|
-
async getModel(id, signal) {
|
|
178
|
-
const entry = (await load(signal)).get(id);
|
|
179
|
-
return entry?.expiresAt > now() ? structuredClone(entry.model) : undefined;
|
|
180
|
-
},
|
|
181
|
-
};
|
|
182
|
-
}
|