dsh-jethub-mobile 0.1.5 → 0.1.7
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/lib/client/jet-hub.js +294 -73
- package/lib/eac-auth.d.ts +79 -0
- package/lib/eac-auth.js +172 -0
- package/lib/eac-credits.d.ts +21 -0
- package/lib/eac-credits.js +27 -0
- package/lib/eac-llm.d.ts +65 -0
- package/lib/eac-llm.js +260 -0
- package/lib/eac-product.d.ts +73 -0
- package/lib/eac-product.js +103 -0
- package/lib/eac.d.ts +77 -0
- package/lib/eac.js +130 -0
- package/lib/index.js +0 -1
- package/lib/jet-hub-store.js +4 -0
- package/lib/provider-registry.js +21 -0
- package/package.json +1 -1
package/lib/eac-auth.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EAC 认证服务:凭据保管 + 有效期对账 + device-code 登录形状。
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ **运行时未验证**:EAC 需要 bearer 凭据 + 设备码登录 + 第三方中继,
|
|
5
|
+
* 当前没有真实上游凭据。本服务照搬 qiniu-auth 的静态凭据骨架与
|
|
6
|
+
* dsh-our-free-model 的 device-code 形状,**结构完整但无法真机验证**。
|
|
7
|
+
*
|
|
8
|
+
* 与 qiniu 同格:没有 OAuth refresh 端点(seal 每请求现解封),
|
|
9
|
+
* `refresh*` 只做有效性对账,不假装续期成功;刻意不进
|
|
10
|
+
* `SELF_HEAL_REFRESHABLE_PROVIDERS`(见 credits-balance-self-heal 的豁免表)。
|
|
11
|
+
*
|
|
12
|
+
* @module src/eac-auth.ts
|
|
13
|
+
*/
|
|
14
|
+
import { Service } from '@deepseek-ai/cordis';
|
|
15
|
+
import { credentialRef } from '@deepseek-ai/dsh-credentials';
|
|
16
|
+
import { isEacExpired, eacCredentialExpiresAtMs, eacIdentityOf, parseEacCredential, parseEacDeviceCode, parseEacPoll, eacDevicePollUrl, eacDeviceStartUrl, } from './eac.js';
|
|
17
|
+
import { EAC } from './eac-product.js';
|
|
18
|
+
import { syncAccountExpiry } from './expiry-sync.js';
|
|
19
|
+
const EAC_EXPIRY_ACCESSORS = {
|
|
20
|
+
expiresAtOf: eacCredentialExpiresAtMs,
|
|
21
|
+
identityOf: eacIdentityOf,
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* 发起 device-code 登录(结构占位,运行时未验证)。
|
|
25
|
+
*
|
|
26
|
+
* 立即返回 userCode + verificationUri,由前端展示;token 由 {@link pollDeviceCode} 收集。
|
|
27
|
+
*/
|
|
28
|
+
export async function startEacDeviceCode(product = EAC, fetchImpl = fetch) {
|
|
29
|
+
const response = await fetchImpl(eacDeviceStartUrl(product), { method: 'POST' });
|
|
30
|
+
if (!response.ok)
|
|
31
|
+
throw new Error(`EAC device-code 起点返回 HTTP ${response.status}`);
|
|
32
|
+
const code = parseEacDeviceCode(await response.json().catch(() => undefined));
|
|
33
|
+
if (code === undefined)
|
|
34
|
+
throw new Error('EAC device-code 起点返回了无法识别的响应');
|
|
35
|
+
return code;
|
|
36
|
+
}
|
|
37
|
+
/** 轮询 device-code 授权(结构占位,运行时未验证)。 */
|
|
38
|
+
export async function pollEacDeviceCode(session, product = EAC, fetchImpl = fetch) {
|
|
39
|
+
if (typeof session !== 'string' || session === '' || session.length > 200) {
|
|
40
|
+
throw new Error('EAC 登录会话无效');
|
|
41
|
+
}
|
|
42
|
+
const response = await fetchImpl(eacDevicePollUrl(product), {
|
|
43
|
+
method: 'POST',
|
|
44
|
+
headers: { 'content-type': 'application/json' },
|
|
45
|
+
body: JSON.stringify({ session }),
|
|
46
|
+
});
|
|
47
|
+
if (!response.ok)
|
|
48
|
+
throw new Error(`EAC device-code 轮询返回 HTTP ${response.status}`);
|
|
49
|
+
const result = parseEacPoll(await response.json().catch(() => undefined));
|
|
50
|
+
if (result === undefined)
|
|
51
|
+
throw new Error('EAC device-code 轮询返回了无法识别的状态');
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
/** EAC 认证服务。 */
|
|
55
|
+
export class EacAuth extends Service {
|
|
56
|
+
product;
|
|
57
|
+
credentialRefName;
|
|
58
|
+
fetchImpl;
|
|
59
|
+
constructor(ctx, options = {}) {
|
|
60
|
+
const product = options.product ?? EAC;
|
|
61
|
+
super(ctx, options.serviceName ?? `${product.id}Auth`);
|
|
62
|
+
this.product = product;
|
|
63
|
+
this.credentialRefName = product.defaultCredentialRef;
|
|
64
|
+
this.fetchImpl = options.fetchImpl ?? fetch;
|
|
65
|
+
}
|
|
66
|
+
async status(pool) {
|
|
67
|
+
if (pool === undefined) {
|
|
68
|
+
const resolved = await this.ctx.credentials
|
|
69
|
+
.resolve(credentialRef(this.credentialRefName)).catch(() => undefined);
|
|
70
|
+
if (resolved === undefined)
|
|
71
|
+
return { configured: false, refreshable: false };
|
|
72
|
+
const credential = parseEacCredential(resolved.value);
|
|
73
|
+
if (credential === undefined)
|
|
74
|
+
return { configured: false, refreshable: false };
|
|
75
|
+
const expiresAt = eacCredentialExpiresAtMs(credential);
|
|
76
|
+
return {
|
|
77
|
+
configured: true,
|
|
78
|
+
source: 'device-code',
|
|
79
|
+
refreshable: false,
|
|
80
|
+
...expiresAt === undefined ? {} : { expiresAt },
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
for (const entry of pool.listAccountsByProvider(this.product.id)) {
|
|
84
|
+
const resolved = await this.ctx.credentials
|
|
85
|
+
.resolve(credentialRef(entry.credentialRef)).catch(() => undefined);
|
|
86
|
+
if (resolved === undefined)
|
|
87
|
+
continue;
|
|
88
|
+
const credential = parseEacCredential(resolved.value);
|
|
89
|
+
if (credential === undefined)
|
|
90
|
+
continue;
|
|
91
|
+
const expiresAt = eacCredentialExpiresAtMs(credential);
|
|
92
|
+
return {
|
|
93
|
+
configured: true,
|
|
94
|
+
source: 'device-code',
|
|
95
|
+
refreshable: false,
|
|
96
|
+
...expiresAt === undefined ? {} : { expiresAt },
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return { configured: false, refreshable: false };
|
|
100
|
+
}
|
|
101
|
+
/** 无 refresh 端点:只做有效性对账,不假装续期成功。 */
|
|
102
|
+
async refresh() {
|
|
103
|
+
const resolved = await this.ctx.credentials
|
|
104
|
+
.resolve(credentialRef(this.credentialRefName)).catch(() => undefined);
|
|
105
|
+
if (resolved === undefined || parseEacCredential(resolved.value) === undefined) {
|
|
106
|
+
throw new Error(`EAC:凭据 ${this.credentialRefName} 不可用。`
|
|
107
|
+
+ 'EAC 没有自动续期端点,请在 Jet Hub 的 EAC 面板重新走设备码登录。');
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/** 按 ref 对账指定账号(RPC account.refresh 的分派目标)。 */
|
|
111
|
+
async refreshAccountCredential(refName, pool, accountId) {
|
|
112
|
+
const resolved = await this.ctx.credentials.resolve(credentialRef(refName)).catch(() => undefined);
|
|
113
|
+
if (resolved === undefined || parseEacCredential(resolved.value) === undefined) {
|
|
114
|
+
throw new Error(`EAC:凭据 ${refName} 不可用或不是合法凭据。`);
|
|
115
|
+
}
|
|
116
|
+
const credential = parseEacCredential(resolved.value);
|
|
117
|
+
await syncAccountExpiry({
|
|
118
|
+
pool,
|
|
119
|
+
provider: this.product.id,
|
|
120
|
+
credential,
|
|
121
|
+
accessors: EAC_EXPIRY_ACCESSORS,
|
|
122
|
+
...accountId === undefined ? {} : { accountId },
|
|
123
|
+
tag: '[eac]',
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/** 批量对账:不按 enabled、不按 refreshable 过滤(AGENTS.md 铁律)。 */
|
|
127
|
+
async refreshAll(pool) {
|
|
128
|
+
if (pool === undefined)
|
|
129
|
+
return;
|
|
130
|
+
for (const entry of pool.listAccountsByProvider(this.product.id)) {
|
|
131
|
+
const resolved = await this.ctx.credentials
|
|
132
|
+
.resolve(credentialRef(entry.credentialRef)).catch(() => undefined);
|
|
133
|
+
const credential = resolved === undefined ? undefined : parseEacCredential(resolved.value);
|
|
134
|
+
if (credential === undefined) {
|
|
135
|
+
this.ctx.logger?.warn?.(`[eac] 账号 ${entry.id} 的凭据 ${entry.credentialRef} 不可用,跳过对账`);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
await syncAccountExpiry({
|
|
139
|
+
pool,
|
|
140
|
+
provider: this.product.id,
|
|
141
|
+
credential,
|
|
142
|
+
accessors: EAC_EXPIRY_ACCESSORS,
|
|
143
|
+
accountId: entry.id,
|
|
144
|
+
current: {
|
|
145
|
+
...entry.expiresAt === undefined ? {} : { expiresAt: entry.expiresAt },
|
|
146
|
+
refreshable: entry.refreshable,
|
|
147
|
+
},
|
|
148
|
+
tag: '[eac]',
|
|
149
|
+
warn: (message) => this.ctx.logger?.warn?.(message),
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
async checkExpired(pool) {
|
|
154
|
+
const status = await this.status(pool);
|
|
155
|
+
if (!status.configured)
|
|
156
|
+
return true;
|
|
157
|
+
if (status.expiresAt === undefined)
|
|
158
|
+
return false;
|
|
159
|
+
return isEacExpired({ access_token: '', expires_at: status.expiresAt });
|
|
160
|
+
}
|
|
161
|
+
/** 发起 device-code 登录(运行时未验证)。 */
|
|
162
|
+
async deviceStart() {
|
|
163
|
+
return startEacDeviceCode(this.product, this.fetchImpl);
|
|
164
|
+
}
|
|
165
|
+
/** 轮询 device-code 授权(运行时未验证)。 */
|
|
166
|
+
async devicePoll(session) {
|
|
167
|
+
return pollEacDeviceCode(session, this.product, this.fetchImpl);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/** 默认凭据 ref(等价 `EAC.defaultCredentialRef`)。 */
|
|
171
|
+
export const EAC_DEFAULT_CREDENTIAL_REF = EAC.defaultCredentialRef;
|
|
172
|
+
//# sourceMappingURL=eac-auth.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EAC 积分能力骨架。
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ **运行时未验证**:EAC 协付线没有余额 / 签到端点(用量由中继侧结算,
|
|
5
|
+
* 插件侧读不到)⇒ registry 里 `credits: { balance: false, dailyCheckin: false }`,
|
|
6
|
+
* `provider-credits.ts` 刻意**不接** EAC 分支(客户端能力表从 registry 派生,
|
|
7
|
+
* irty 为 false 时不会发必然失败的请求)。
|
|
8
|
+
*
|
|
9
|
+
* 本文件只放「登录凭证 → 设备码」相关的纯数据归一化,供 device-code
|
|
10
|
+
* 面板/单测复用;真机接入若拿到余额端点,再按其它渠道的格式补 fetcher。
|
|
11
|
+
*
|
|
12
|
+
* @module src/eac-credits.ts
|
|
13
|
+
*/
|
|
14
|
+
import type { EacCredential } from './eac.js';
|
|
15
|
+
/** EAC 无余额端点:余额查询恒返回「查不到」,由 RPC 短路,不发网络。 */
|
|
16
|
+
export declare function eacBalanceUnavailable(): null;
|
|
17
|
+
/** EAC 无签到端点:签到恒返回该可读原因。 */
|
|
18
|
+
export declare const EAC_CHECKIN_UNAVAILABLE = "EAC \u6CA1\u6709\u53EF\u8C03\u7528\u7684\u7B7E\u5230\u7AEF\u70B9\uFF08\u7528\u91CF\u7531\u4E2D\u7EE7\u4FA7\u7ED3\u7B97\uFF09";
|
|
19
|
+
/** 面板展示用的打码 token(绝不回显明文)。 */
|
|
20
|
+
export declare function maskEacToken(credential: EacCredential | undefined): string;
|
|
21
|
+
//# sourceMappingURL=eac-credits.d.ts.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EAC 积分能力骨架。
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ **运行时未验证**:EAC 协付线没有余额 / 签到端点(用量由中继侧结算,
|
|
5
|
+
* 插件侧读不到)⇒ registry 里 `credits: { balance: false, dailyCheckin: false }`,
|
|
6
|
+
* `provider-credits.ts` 刻意**不接** EAC 分支(客户端能力表从 registry 派生,
|
|
7
|
+
* irty 为 false 时不会发必然失败的请求)。
|
|
8
|
+
*
|
|
9
|
+
* 本文件只放「登录凭证 → 设备码」相关的纯数据归一化,供 device-code
|
|
10
|
+
* 面板/单测复用;真机接入若拿到余额端点,再按其它渠道的格式补 fetcher。
|
|
11
|
+
*
|
|
12
|
+
* @module src/eac-credits.ts
|
|
13
|
+
*/
|
|
14
|
+
/** EAC 无余额端点:余额查询恒返回「查不到」,由 RPC 短路,不发网络。 */
|
|
15
|
+
export function eacBalanceUnavailable() {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
/** EAC 无签到端点:签到恒返回该可读原因。 */
|
|
19
|
+
export const EAC_CHECKIN_UNAVAILABLE = 'EAC 没有可调用的签到端点(用量由中继侧结算)';
|
|
20
|
+
/** 面板展示用的打码 token(绝不回显明文)。 */
|
|
21
|
+
export function maskEacToken(credential) {
|
|
22
|
+
const token = credential?.access_token;
|
|
23
|
+
if (typeof token !== 'string' || token.length < 8)
|
|
24
|
+
return '';
|
|
25
|
+
return `${token.slice(0, 4)}…${token.slice(-4)}`;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=eac-credits.js.map
|
package/lib/eac-llm.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EAC LLM 适配器(OpenAI 兼容中继,SSE 流式)。
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ **运行时未验证**:没有真实上游凭据/中继,本适配器照搬 qiniu 的
|
|
5
|
+
* OpenAI 兼容骨架(共享 `serializeMessages` / `consumeOpenAiSse` /
|
|
6
|
+
* `registerAdapterIdempotent`),结构与类型完整但无法真机验证。
|
|
7
|
+
*
|
|
8
|
+
* 轮换语义:认证失败换号(不写限流标记);限流/额度换号并写标记;
|
|
9
|
+
* 形状错误不换号;已产出内容的流不重放。
|
|
10
|
+
*
|
|
11
|
+
* @module src/eac-llm.ts
|
|
12
|
+
*/
|
|
13
|
+
import { LlmAdapter } from '@deepseek-ai/dsh-llm';
|
|
14
|
+
import type { GenerateOptions, LlmModelInfo, LlmProviderInfo, LlmResolvedModelInfo, StreamChunk } from '@deepseek-ai/dsh-llm';
|
|
15
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
16
|
+
import { type EacProduct } from './eac-product.js';
|
|
17
|
+
import { type EacCredential } from './eac.js';
|
|
18
|
+
export interface EacSlot {
|
|
19
|
+
id: string;
|
|
20
|
+
credential: EacCredential;
|
|
21
|
+
}
|
|
22
|
+
export interface EacAdapterOptions {
|
|
23
|
+
credentialRef?: unknown;
|
|
24
|
+
slots: (modelId?: string) => Promise<readonly EacSlot[]>;
|
|
25
|
+
accountPool?: unknown;
|
|
26
|
+
product?: EacProduct;
|
|
27
|
+
warn?: (message: string) => void;
|
|
28
|
+
markLimited?: (slotId: string, modelId: string, resetAtMs: number) => Promise<void>;
|
|
29
|
+
fetchImpl?: typeof fetch;
|
|
30
|
+
}
|
|
31
|
+
export interface EacCatalogEntry {
|
|
32
|
+
id: string;
|
|
33
|
+
name: string;
|
|
34
|
+
contextWindow: number;
|
|
35
|
+
supportsImage: boolean;
|
|
36
|
+
}
|
|
37
|
+
/** 清空目录缓存(单测隔离用)。 */
|
|
38
|
+
export declare function clearEacCatalogCache(): void;
|
|
39
|
+
/** 解析远端 `GET /models`(尽力而为,拿不到就回落兜底;运行时未验证)。 */
|
|
40
|
+
export declare function parseEacCatalog(body: unknown): readonly EacCatalogEntry[];
|
|
41
|
+
export declare class EacAdapter extends LlmAdapter {
|
|
42
|
+
private readonly options;
|
|
43
|
+
private readonly product;
|
|
44
|
+
private readonly skeletonCatalog;
|
|
45
|
+
private readonly fetchImpl;
|
|
46
|
+
constructor(options: EacAdapterOptions);
|
|
47
|
+
private loadCatalogEntries;
|
|
48
|
+
private fallbackEntries;
|
|
49
|
+
private toCatalogEntry;
|
|
50
|
+
private catalog;
|
|
51
|
+
providerInfo(provider: string): LlmProviderInfo;
|
|
52
|
+
listModels(provider: string): Promise<readonly LlmModelInfo[]>;
|
|
53
|
+
resolveModel(provider: string, model: string, _signal?: AbortSignal): Promise<LlmResolvedModelInfo>;
|
|
54
|
+
prepareCall(provider: string, model: string, signal?: AbortSignal): Promise<{
|
|
55
|
+
model: LlmResolvedModelInfo;
|
|
56
|
+
stream: (options: GenerateOptions) => AsyncIterable<StreamChunk>;
|
|
57
|
+
}>;
|
|
58
|
+
private resolveImageUrls;
|
|
59
|
+
stream(options: GenerateOptions): AsyncIterable<StreamChunk>;
|
|
60
|
+
private classify;
|
|
61
|
+
private streamVia;
|
|
62
|
+
}
|
|
63
|
+
/** 在 `ctx.llm` 上注册 EAC 路由。 */
|
|
64
|
+
export declare function registerEacLlm(ctx: Context, options: EacAdapterOptions): EacAdapter;
|
|
65
|
+
//# sourceMappingURL=eac-llm.d.ts.map
|
package/lib/eac-llm.js
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EAC LLM 适配器(OpenAI 兼容中继,SSE 流式)。
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ **运行时未验证**:没有真实上游凭据/中继,本适配器照搬 qiniu 的
|
|
5
|
+
* OpenAI 兼容骨架(共享 `serializeMessages` / `consumeOpenAiSse` /
|
|
6
|
+
* `registerAdapterIdempotent`),结构与类型完整但无法真机验证。
|
|
7
|
+
*
|
|
8
|
+
* 轮换语义:认证失败换号(不写限流标记);限流/额度换号并写标记;
|
|
9
|
+
* 形状错误不换号;已产出内容的流不重放。
|
|
10
|
+
*
|
|
11
|
+
* @module src/eac-llm.ts
|
|
12
|
+
*/
|
|
13
|
+
import { CONTEXT_WINDOW_EXCEEDED_CODE, LlmAdapter, LlmError } from '@deepseek-ai/dsh-llm';
|
|
14
|
+
import { createModelCatalog } from './model-catalog.js';
|
|
15
|
+
import { EAC, EAC_FALLBACK_MODELS, classifyEacError, } from './eac-product.js';
|
|
16
|
+
import { eacChatUrl, eacHeaders, normalizeEacModelId } from './eac.js';
|
|
17
|
+
import { collectImages, consumeOpenAiSse, httpErrorCode, serializeMessages } from './openai-compat.js';
|
|
18
|
+
import { isAbortLike } from './aggregate-adapter.js';
|
|
19
|
+
import { registerAdapterIdempotent } from './llm-register-compat.js';
|
|
20
|
+
import { retryAfterMsFromResponse } from './retry-after.js';
|
|
21
|
+
const DEFAULT_CATALOG_TTL_MS = 300_000;
|
|
22
|
+
const catalogCache = new Map();
|
|
23
|
+
/** 清空目录缓存(单测隔离用)。 */
|
|
24
|
+
export function clearEacCatalogCache() {
|
|
25
|
+
catalogCache.clear();
|
|
26
|
+
}
|
|
27
|
+
/** 解析远端 `GET /models`(尽力而为,拿不到就回落兜底;运行时未验证)。 */
|
|
28
|
+
export function parseEacCatalog(body) {
|
|
29
|
+
if (typeof body !== 'object' || body === null)
|
|
30
|
+
return [];
|
|
31
|
+
const data = body.data;
|
|
32
|
+
if (!Array.isArray(data))
|
|
33
|
+
return [];
|
|
34
|
+
const out = [];
|
|
35
|
+
for (const raw of data) {
|
|
36
|
+
if (typeof raw !== 'object' || raw === null)
|
|
37
|
+
continue;
|
|
38
|
+
const m = raw;
|
|
39
|
+
if (typeof m.id !== 'string' || m.id === '')
|
|
40
|
+
continue;
|
|
41
|
+
const fallback = EAC_FALLBACK_MODELS.find((f) => f.id === m.id);
|
|
42
|
+
const contextWindow = typeof m.context_length === 'number' && m.context_length > 0
|
|
43
|
+
? m.context_length
|
|
44
|
+
: typeof m.context_window === 'number' && m.context_window > 0
|
|
45
|
+
? m.context_window
|
|
46
|
+
: fallback?.contextWindow ?? 0;
|
|
47
|
+
out.push({
|
|
48
|
+
id: m.id,
|
|
49
|
+
name: typeof m.name === 'string' && m.name !== '' ? m.name : m.id,
|
|
50
|
+
contextWindow,
|
|
51
|
+
supportsImage: fallback?.supportsImage ?? false,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
export class EacAdapter extends LlmAdapter {
|
|
57
|
+
options;
|
|
58
|
+
product;
|
|
59
|
+
skeletonCatalog;
|
|
60
|
+
fetchImpl;
|
|
61
|
+
constructor(options) {
|
|
62
|
+
super();
|
|
63
|
+
this.options = options;
|
|
64
|
+
this.product = options.product ?? EAC;
|
|
65
|
+
this.fetchImpl = options.fetchImpl ?? fetch;
|
|
66
|
+
this.skeletonCatalog = createModelCatalog(options.accountPool === undefined ? {} : { accountPool: options.accountPool }, {
|
|
67
|
+
product: this.product,
|
|
68
|
+
loadCatalog: () => this.loadCatalogEntries(),
|
|
69
|
+
syncCatalog: () => this.fallbackEntries(),
|
|
70
|
+
unknownModelInputModalities: () => ['text'],
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
async loadCatalogEntries() {
|
|
74
|
+
const entries = await this.catalog();
|
|
75
|
+
return entries.map((m) => this.toCatalogEntry(m));
|
|
76
|
+
}
|
|
77
|
+
fallbackEntries() {
|
|
78
|
+
return EAC_FALLBACK_MODELS.map((m) => this.toCatalogEntry(m));
|
|
79
|
+
}
|
|
80
|
+
toCatalogEntry(m) {
|
|
81
|
+
const supportsImage = m.supportsImage === true;
|
|
82
|
+
return {
|
|
83
|
+
id: m.id,
|
|
84
|
+
name: m.name,
|
|
85
|
+
inputModalities: supportsImage ? ['text', 'image'] : ['text'],
|
|
86
|
+
...m.contextWindow > 0 ? { contextWindow: m.contextWindow } : {},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
async catalog() {
|
|
90
|
+
const cached = catalogCache.get('default');
|
|
91
|
+
if (cached !== undefined && Date.now() - cached.at < DEFAULT_CATALOG_TTL_MS)
|
|
92
|
+
return cached.entries;
|
|
93
|
+
const slots = await this.options.slots();
|
|
94
|
+
if (slots.length === 0)
|
|
95
|
+
return EAC_FALLBACK_MODELS;
|
|
96
|
+
try {
|
|
97
|
+
const response = await this.fetchImpl(`${this.product.baseUrl}${this.product.modelsPath}`, {
|
|
98
|
+
headers: eacHeaders(slots[0].credential, 'application/json'),
|
|
99
|
+
signal: AbortSignal.timeout(this.product.catalogTimeoutMs),
|
|
100
|
+
});
|
|
101
|
+
if (!response.ok)
|
|
102
|
+
return EAC_FALLBACK_MODELS;
|
|
103
|
+
const entries = parseEacCatalog(await response.json().catch(() => undefined));
|
|
104
|
+
if (entries.length === 0)
|
|
105
|
+
return EAC_FALLBACK_MODELS;
|
|
106
|
+
catalogCache.set('default', { at: Date.now(), entries });
|
|
107
|
+
return entries;
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return EAC_FALLBACK_MODELS;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
providerInfo(provider) {
|
|
114
|
+
const id = typeof provider === 'string' && provider.length > 0 ? provider : this.product.id;
|
|
115
|
+
return { id, name: this.product.displayName };
|
|
116
|
+
}
|
|
117
|
+
async listModels(provider) {
|
|
118
|
+
return await this.skeletonCatalog.listModels(provider);
|
|
119
|
+
}
|
|
120
|
+
async resolveModel(provider, model, _signal) {
|
|
121
|
+
const id = normalizeEacModelId(model);
|
|
122
|
+
const entry = (await this.catalog()).find((m) => m.id === id);
|
|
123
|
+
const resolved = {
|
|
124
|
+
provider,
|
|
125
|
+
id,
|
|
126
|
+
name: entry?.name ?? id,
|
|
127
|
+
inputModalities: entry?.supportsImage === true ? ['text', 'image'] : ['text'],
|
|
128
|
+
};
|
|
129
|
+
if ((entry?.contextWindow ?? 0) > 0)
|
|
130
|
+
resolved.context = { contextWindow: entry.contextWindow };
|
|
131
|
+
return resolved;
|
|
132
|
+
}
|
|
133
|
+
async prepareCall(provider, model, signal) {
|
|
134
|
+
return { model: await this.resolveModel(provider, model, signal), stream: (o) => this.stream(o) };
|
|
135
|
+
}
|
|
136
|
+
async resolveImageUrls(messages) {
|
|
137
|
+
const refs = new Map();
|
|
138
|
+
for (const message of messages) {
|
|
139
|
+
if (Array.isArray(message.content))
|
|
140
|
+
collectImages(message.content, refs);
|
|
141
|
+
}
|
|
142
|
+
if (refs.size === 0)
|
|
143
|
+
return undefined;
|
|
144
|
+
const out = new Map();
|
|
145
|
+
for (const [id, ref] of refs) {
|
|
146
|
+
const dataUrl = typeof ref === 'object' && ref !== null && typeof ref.dataUrl === 'string'
|
|
147
|
+
? ref.dataUrl
|
|
148
|
+
: undefined;
|
|
149
|
+
if (dataUrl !== undefined)
|
|
150
|
+
out.set(id, dataUrl);
|
|
151
|
+
}
|
|
152
|
+
return out.size > 0 ? out : undefined;
|
|
153
|
+
}
|
|
154
|
+
async *stream(options) {
|
|
155
|
+
const slots = await this.options.slots(options.model);
|
|
156
|
+
if (slots.length === 0) {
|
|
157
|
+
throw new LlmError('eac: 没有可用账号。请在 Jet Hub 的 EAC 面板走设备码登录。', 'MISSING_CREDENTIAL');
|
|
158
|
+
}
|
|
159
|
+
const tried = new Set();
|
|
160
|
+
for (;;) {
|
|
161
|
+
const slot = slots.find((s) => !tried.has(s.id));
|
|
162
|
+
if (slot === undefined)
|
|
163
|
+
throw new LlmError('eac: 所有账号均不可用或受限', 'QUOTA_EXCEEDED');
|
|
164
|
+
tried.add(slot.id);
|
|
165
|
+
let delivered = false;
|
|
166
|
+
try {
|
|
167
|
+
for await (const chunk of this.streamVia(slot, options)) {
|
|
168
|
+
delivered = true;
|
|
169
|
+
yield chunk;
|
|
170
|
+
}
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
if (delivered)
|
|
175
|
+
throw error;
|
|
176
|
+
if (error instanceof LlmError && error.code === 'QUOTA_EXCEEDED')
|
|
177
|
+
throw error;
|
|
178
|
+
if (isAbortLike(error, options.signal))
|
|
179
|
+
throw error;
|
|
180
|
+
const info = this.classify(error);
|
|
181
|
+
if (info.kind === 'invalid_request' || info.kind === 'context_window')
|
|
182
|
+
throw error;
|
|
183
|
+
if (info.kind === 'auth') {
|
|
184
|
+
if (tried.size >= slots.length)
|
|
185
|
+
throw error;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (info.kind === 'rate_limit' || info.kind === 'quota') {
|
|
189
|
+
const resetAtMs = Date.now() + 60_000;
|
|
190
|
+
await this.options.markLimited?.(slot.id, options.model, resetAtMs);
|
|
191
|
+
}
|
|
192
|
+
if (tried.size >= slots.length)
|
|
193
|
+
throw error;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
classify(error) {
|
|
198
|
+
const failure = (typeof error === 'object' && error !== null ? error : {});
|
|
199
|
+
const status = typeof failure.failure?.status === 'number' ? failure.failure.status : 0;
|
|
200
|
+
return classifyEacError(status, error instanceof Error ? error.message : String(error));
|
|
201
|
+
}
|
|
202
|
+
async *streamVia(slot, options) {
|
|
203
|
+
const imageUrls = await this.resolveImageUrls(options.messages);
|
|
204
|
+
const messages = serializeMessages(options.messages, imageUrls);
|
|
205
|
+
const payload = {
|
|
206
|
+
model: normalizeEacModelId(options.model),
|
|
207
|
+
messages,
|
|
208
|
+
stream: true,
|
|
209
|
+
};
|
|
210
|
+
if (options.system !== undefined && options.system.length > 0)
|
|
211
|
+
payload.system = options.system;
|
|
212
|
+
if (options.tools !== undefined)
|
|
213
|
+
payload.tools = options.tools;
|
|
214
|
+
if (options.temperature !== undefined)
|
|
215
|
+
payload.temperature = options.temperature;
|
|
216
|
+
if (options.maxTokens !== undefined)
|
|
217
|
+
payload.max_tokens = options.maxTokens;
|
|
218
|
+
if (options.reasoningEffort !== undefined)
|
|
219
|
+
payload.reasoning_effort = String(options.reasoningEffort);
|
|
220
|
+
const response = await this.fetchImpl(eacChatUrl(this.product), {
|
|
221
|
+
method: 'POST',
|
|
222
|
+
headers: eacHeaders(slot.credential, 'text/event-stream'),
|
|
223
|
+
body: JSON.stringify(payload),
|
|
224
|
+
...options.signal === undefined ? {} : { signal: options.signal },
|
|
225
|
+
});
|
|
226
|
+
if (!response.ok) {
|
|
227
|
+
const text = await response.text().catch(() => '');
|
|
228
|
+
const info = classifyEacError(response.status, text);
|
|
229
|
+
const code = info.kind === 'quota'
|
|
230
|
+
? 'QUOTA_EXCEEDED'
|
|
231
|
+
: info.kind === 'rate_limit'
|
|
232
|
+
? 'RATE_LIMIT'
|
|
233
|
+
: info.kind === 'context_window'
|
|
234
|
+
? CONTEXT_WINDOW_EXCEEDED_CODE
|
|
235
|
+
: info.kind === 'auth'
|
|
236
|
+
? 'AUTH'
|
|
237
|
+
: httpErrorCode(response.status);
|
|
238
|
+
throw new LlmError(`eac: HTTP ${response.status} — ${text.slice(0, 300)}`, code, {
|
|
239
|
+
status: response.status,
|
|
240
|
+
...(() => {
|
|
241
|
+
const r = retryAfterMsFromResponse(response.headers);
|
|
242
|
+
return r === undefined ? {} : { providerRetryAfterMs: r };
|
|
243
|
+
})(),
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
yield* consumeOpenAiSse(response, options.signal !== undefined ? { signal: options.signal } : {}, {
|
|
247
|
+
label: 'eac',
|
|
248
|
+
firstTokenTimeoutMs: this.product.chatHeaderTimeoutMs,
|
|
249
|
+
chunkTimeoutMs: this.product.chunkTimeoutMs,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/** 在 `ctx.llm` 上注册 EAC 路由。 */
|
|
254
|
+
export function registerEacLlm(ctx, options) {
|
|
255
|
+
const product = options.product ?? EAC;
|
|
256
|
+
const adapter = new EacAdapter(options);
|
|
257
|
+
registerAdapterIdempotent(ctx.llm, [product.id], adapter, options.warn);
|
|
258
|
+
return adapter;
|
|
259
|
+
}
|
|
260
|
+
//# sourceMappingURL=eac-llm.js.map
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EAC(协付渠道)产品配置与协议常量。
|
|
3
|
+
*
|
|
4
|
+
* ## 协议面(照搬 dsh-our-free-model 的 EAC 协付线,运行时未验证)
|
|
5
|
+
*
|
|
6
|
+
* EAC 是由第三方中继承载的「协付」渠道:上游网关在 baseUrl(seal 里解出来的
|
|
7
|
+
* worker/direct 端点)后面,对 OpenAI 兼容的 `/models` 与 `/chat/completions`
|
|
8
|
+
* 提供 SSE 推理。本骨架只搬结构与形状,**没有真实上游凭据、无法真机验证**。
|
|
9
|
+
*
|
|
10
|
+
* 参考实现里的认证细节(本骨架以占位形式登记,运行时未验证):
|
|
11
|
+
* - 凭据形态:`direct` = 一枚 bearer apiKey;`worker` = 签名网关 + HMAC 共享密钥
|
|
12
|
+
* (`signSealedRequest` 用 `timestamp \n METHOD \n path \n sha256(body)`);
|
|
13
|
+
* - 登录:GitHub device-code 流程(`start` 拿 userCode + verificationUri,
|
|
14
|
+
* `poll` 拿 token),token 作为 `x-ofm-user` 头随请求上传;
|
|
15
|
+
* - 刷新:参考实现没有 OAuth refresh 端点(seal 在每次请求前现解封),
|
|
16
|
+
* 故本渠道**不登记** `SELF_HEAL_REFRESHABLE_PROVIDERS`,走显式豁免;
|
|
17
|
+
* - 模型目录:`GET {base}/models`;
|
|
18
|
+
* - 积分:协付线**没有余额/签到端点**(用量由中继侧结算,插件侧读不到),
|
|
19
|
+
* 故 `credits: { balance: false, dailyCheckin: false }`。
|
|
20
|
+
*
|
|
21
|
+
* ⚠️ 字段值(网关基址、凭据形状、端点路径)是结构占位,**运行时未验证**;
|
|
22
|
+
* 真机接入前必须用真实 seal + 中继实测逐条核对(见 README「运行时未验证」)。
|
|
23
|
+
*
|
|
24
|
+
* @module src/eac-product.ts
|
|
25
|
+
*/
|
|
26
|
+
/** EAC 产品配置(字段围绕 OpenAI 兼容中继)。 */
|
|
27
|
+
export interface EacProduct {
|
|
28
|
+
readonly id: 'eac';
|
|
29
|
+
readonly displayName: string;
|
|
30
|
+
/** OpenAI 兼容中继基址(结构占位,运行时未验证)。 */
|
|
31
|
+
readonly baseUrl: string;
|
|
32
|
+
readonly chatPath: string;
|
|
33
|
+
readonly modelsPath: string;
|
|
34
|
+
readonly chatHeaderTimeoutMs: number;
|
|
35
|
+
readonly chunkTimeoutMs: number;
|
|
36
|
+
readonly catalogTimeoutMs: number;
|
|
37
|
+
/** 单凭据回退 ref。 */
|
|
38
|
+
readonly defaultCredentialRef: string;
|
|
39
|
+
/** device-code 登录起点(结构占位,运行时未验证)。 */
|
|
40
|
+
readonly deviceStartPath: string;
|
|
41
|
+
/** device-code 轮询端点(结构占位,运行时未验证)。 */
|
|
42
|
+
readonly devicePollPath: string;
|
|
43
|
+
}
|
|
44
|
+
export declare const EAC: EacProduct;
|
|
45
|
+
/** 兜底模型目录条目(离线占位;远端优先,运行时未验证)。 */
|
|
46
|
+
export interface EacFallbackModel {
|
|
47
|
+
id: string;
|
|
48
|
+
name: string;
|
|
49
|
+
contextWindow: number;
|
|
50
|
+
supportsImage: boolean;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 兜底目录(**结构占位,运行时未验证**)。
|
|
54
|
+
*
|
|
55
|
+
* 不放空表的理由与 qiniu 一致:冷缓存下空表会让 Jet Hub 把整个 provider
|
|
56
|
+
* 误显示为「已关闭且无模型」,并让「显示列表」开关无从点选。
|
|
57
|
+
*/
|
|
58
|
+
export declare const EAC_FALLBACK_MODELS: readonly EacFallbackModel[];
|
|
59
|
+
/** 该模型是否在本地兜底表里。 */
|
|
60
|
+
export declare function isKnownEacModel(id: string): boolean;
|
|
61
|
+
export type EacErrorKind = 'rate_limit' | 'quota' | 'auth' | 'server' | 'transport' | 'invalid_request' | 'context_window';
|
|
62
|
+
export interface EacErrorInfo {
|
|
63
|
+
kind: EacErrorKind;
|
|
64
|
+
detail: string;
|
|
65
|
+
}
|
|
66
|
+
/** 从错误体抽人可读片段(禁止整坨 JSON 上屏)。 */
|
|
67
|
+
export declare function eacReadableDetail(body: string): string;
|
|
68
|
+
/**
|
|
69
|
+
* 把一次失败的 HTTP 响应归类(纯函数,可单测)。
|
|
70
|
+
* 顺序即优先级:上下文超限 → 401/403 认证 → 限流/额度 → 传输 → 形状 → 服务端。
|
|
71
|
+
*/
|
|
72
|
+
export declare function classifyEacError(status: number, body: string): EacErrorInfo;
|
|
73
|
+
//# sourceMappingURL=eac-product.d.ts.map
|