dsh-aperture 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.en.md +316 -0
- package/README.md +303 -0
- package/cordis.patch.yml +48 -0
- package/lib/aperture.d.ts +48 -0
- package/lib/aperture.js +109 -0
- package/lib/catalog.d.ts +37 -0
- package/lib/catalog.js +78 -0
- package/lib/command.d.ts +21 -0
- package/lib/command.js +58 -0
- package/lib/config.d.ts +197 -0
- package/lib/config.js +126 -0
- package/lib/index.d.ts +64 -0
- package/lib/index.js +122 -0
- package/lib/metadata/extract.d.ts +62 -0
- package/lib/metadata/extract.js +235 -0
- package/lib/metadata/modelsdev.d.ts +55 -0
- package/lib/metadata/modelsdev.js +304 -0
- package/lib/metadata/utils.d.ts +29 -0
- package/lib/metadata/utils.js +100 -0
- package/lib/namespaces.d.ts +12 -0
- package/lib/namespaces.js +12 -0
- package/lib/profile.d.ts +70 -0
- package/lib/profile.js +161 -0
- package/lib/registry.d.ts +57 -0
- package/lib/registry.js +246 -0
- package/lib/report.d.ts +16 -0
- package/lib/report.js +89 -0
- package/lib/runtime.d.ts +96 -0
- package/lib/runtime.js +203 -0
- package/lib/sync.d.ts +71 -0
- package/lib/sync.js +190 -0
- package/lib/types.d.ts +118 -0
- package/lib/types.js +10 -0
- package/lib/url.d.ts +37 -0
- package/lib/url.js +67 -0
- package/package.json +71 -0
package/lib/runtime.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 发现运行时:同一时刻只跑一次刷新,只记住一个结果。
|
|
3
|
+
*
|
|
4
|
+
* 刷新来自四个方向——插件加载、设置变更、刷新间隔,以及 `/aperture` 命令——其中两个
|
|
5
|
+
* 很容易重叠,因为本插件自己写入并提交的设置变更会唤醒触发这次写入的同一个 watcher。
|
|
6
|
+
* 因此刷新是单飞(single-flight)的:在刷新过程中到达的请求会把自己记为下一次运行,
|
|
7
|
+
* 而不是启动第二次,循环会用最新配置再跑一遍。
|
|
8
|
+
*
|
|
9
|
+
* 记住的结果就是 `/aperture` 所报告的内容。它刻意不是清单(catalog)的缓存:清单存放
|
|
10
|
+
* 在设置文档里,而真正服务请求的正是这份文档。
|
|
11
|
+
*
|
|
12
|
+
* @module dsh-aperture/runtime
|
|
13
|
+
*/
|
|
14
|
+
import { fetchModelsListing } from "./aperture.js";
|
|
15
|
+
import { ModelCatalog } from "./catalog.js";
|
|
16
|
+
import { PI_AI_NAMESPACE } from "./config.js";
|
|
17
|
+
import { buildProfilePlan } from "./profile.js";
|
|
18
|
+
import { buildRegistry } from "./registry.js";
|
|
19
|
+
import { applySync, clearRoutes } from "./sync.js";
|
|
20
|
+
/** 单飞(single-flight)的发现与发布。 */
|
|
21
|
+
export class ApertureRuntime {
|
|
22
|
+
deps;
|
|
23
|
+
running;
|
|
24
|
+
queued;
|
|
25
|
+
latest;
|
|
26
|
+
/**
|
|
27
|
+
* @param deps - 宿主服务与配置活引用(thunk)。
|
|
28
|
+
*/
|
|
29
|
+
constructor(deps) {
|
|
30
|
+
this.deps = deps;
|
|
31
|
+
}
|
|
32
|
+
/** 最近一次已完成的刷新(如果完成过)。 */
|
|
33
|
+
last() {
|
|
34
|
+
return this.latest;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 刷新;若已有刷新在运行,则把本次刷新排在其后。
|
|
38
|
+
* @param trigger - 触发来源;由 `/aperture` 报告。
|
|
39
|
+
* @returns 本次调用所参与的那次刷新的结果。
|
|
40
|
+
*/
|
|
41
|
+
async refresh(trigger) {
|
|
42
|
+
if (this.running !== undefined) {
|
|
43
|
+
this.queued = trigger;
|
|
44
|
+
return this.running;
|
|
45
|
+
}
|
|
46
|
+
this.running = this.run(trigger);
|
|
47
|
+
try {
|
|
48
|
+
return await this.running;
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
this.running = undefined;
|
|
52
|
+
const queued = this.queued;
|
|
53
|
+
this.queued = undefined;
|
|
54
|
+
if (queued !== undefined) {
|
|
55
|
+
void this.refresh(queued);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 从设置文档中撤出本插件的路由。
|
|
61
|
+
* @returns 供命令界面展示的可读结果。
|
|
62
|
+
*/
|
|
63
|
+
async remove() {
|
|
64
|
+
const config = this.deps.config();
|
|
65
|
+
const owned = [config.route, config.anthropicRoute];
|
|
66
|
+
try {
|
|
67
|
+
const outcome = await clearRoutes(this.deps.settings, owned);
|
|
68
|
+
return outcome.applied
|
|
69
|
+
? `已从 "${PI_AI_NAMESPACE}" 配置段移除 ${outcome.ops} 条路由:${owned.join(', ')}`
|
|
70
|
+
: `未移除任何内容:${outcome.reason ?? '原因未知'}`;
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
return `移除路由失败:${message(error)}`;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** 执行一次刷新,绝不抛出异常。 */
|
|
77
|
+
async run(trigger) {
|
|
78
|
+
const started = Date.now();
|
|
79
|
+
const at = new Date();
|
|
80
|
+
try {
|
|
81
|
+
return await this.execute(trigger, started, at);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
// 上面的任何代码都不允许 reject:所有调用方都是即发即忘的 `void`,而在宿主的
|
|
85
|
+
// loader 里出现未处理的 rejection 并不是报告清单损坏的可接受方式。
|
|
86
|
+
const outcome = {
|
|
87
|
+
trigger,
|
|
88
|
+
ok: false,
|
|
89
|
+
at,
|
|
90
|
+
durationMs: Date.now() - started,
|
|
91
|
+
listed: 0,
|
|
92
|
+
models: [],
|
|
93
|
+
routes: [],
|
|
94
|
+
unserved: [],
|
|
95
|
+
catalog: { entries: 0, reason: '未执行' },
|
|
96
|
+
error: message(error),
|
|
97
|
+
};
|
|
98
|
+
this.latest = outcome;
|
|
99
|
+
this.deps.logger.warn('刷新失败:%s', outcome.error ?? '');
|
|
100
|
+
return outcome;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** 一次刷新的主体。 */
|
|
104
|
+
async execute(trigger, started, at) {
|
|
105
|
+
const config = this.deps.config();
|
|
106
|
+
if (config.instanceRoot === undefined) {
|
|
107
|
+
const outcome = {
|
|
108
|
+
trigger,
|
|
109
|
+
ok: false,
|
|
110
|
+
at,
|
|
111
|
+
durationMs: Date.now() - started,
|
|
112
|
+
listed: 0,
|
|
113
|
+
models: [],
|
|
114
|
+
routes: [],
|
|
115
|
+
unserved: [],
|
|
116
|
+
catalog: { entries: 0, reason: '未尝试' },
|
|
117
|
+
error: config.rawBaseUrl.length === 0 ? '未配置 baseUrl' : `baseUrl "${config.rawBaseUrl}" 不是可用的 URL`,
|
|
118
|
+
};
|
|
119
|
+
this.latest = outcome;
|
|
120
|
+
return outcome;
|
|
121
|
+
}
|
|
122
|
+
const catalog = await this.deps.catalog.load(config.modelMetadataUrl, config.timeoutMs);
|
|
123
|
+
let listed;
|
|
124
|
+
let endpoint;
|
|
125
|
+
try {
|
|
126
|
+
const listing = await fetchModelsListing(config.instanceRoot, { timeoutMs: config.timeoutMs });
|
|
127
|
+
listed = listing.entries;
|
|
128
|
+
endpoint = listing.endpoint;
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
const outcome = {
|
|
132
|
+
trigger,
|
|
133
|
+
ok: false,
|
|
134
|
+
at,
|
|
135
|
+
durationMs: Date.now() - started,
|
|
136
|
+
listed: 0,
|
|
137
|
+
models: [],
|
|
138
|
+
routes: [],
|
|
139
|
+
unserved: [],
|
|
140
|
+
catalog,
|
|
141
|
+
error: message(error),
|
|
142
|
+
};
|
|
143
|
+
this.latest = outcome;
|
|
144
|
+
// 失败的刷新不发布任何内容:设置文档中已有的清单继续提供服务,一次瞬时网络错误
|
|
145
|
+
// 不得把它撤销。
|
|
146
|
+
this.deps.logger.warn('发现失败;保留已发布的清单:%s', outcome.error ?? '');
|
|
147
|
+
return outcome;
|
|
148
|
+
}
|
|
149
|
+
const registry = buildRegistry(listed, {
|
|
150
|
+
models: config.models,
|
|
151
|
+
enabledModelIds: config.enabledModelIds,
|
|
152
|
+
modelAliases: config.modelAliases,
|
|
153
|
+
defaultContextWindow: config.defaultContextWindow,
|
|
154
|
+
images: config.images,
|
|
155
|
+
reasoning: config.reasoning,
|
|
156
|
+
}, catalog.lookup);
|
|
157
|
+
const plan = buildProfilePlan(registry.models, {
|
|
158
|
+
instanceRoot: config.instanceRoot,
|
|
159
|
+
route: config.route,
|
|
160
|
+
anthropicRoute: config.anthropicRoute,
|
|
161
|
+
displayName: config.displayName,
|
|
162
|
+
anthropicDisplayName: config.anthropicDisplayName,
|
|
163
|
+
...(config.apiKeyEnv === undefined ? {} : { apiKeyEnv: config.apiKeyEnv }),
|
|
164
|
+
headers: config.headers,
|
|
165
|
+
placeholderCredential: config.placeholderCredential,
|
|
166
|
+
configured: config.models,
|
|
167
|
+
});
|
|
168
|
+
const sync = await this.publish(config, plan);
|
|
169
|
+
const outcome = {
|
|
170
|
+
trigger,
|
|
171
|
+
ok: true,
|
|
172
|
+
at,
|
|
173
|
+
durationMs: Date.now() - started,
|
|
174
|
+
endpoint,
|
|
175
|
+
listed: listed.length,
|
|
176
|
+
models: registry.models,
|
|
177
|
+
routes: plan.routes,
|
|
178
|
+
unserved: plan.unserved,
|
|
179
|
+
catalog,
|
|
180
|
+
sync,
|
|
181
|
+
};
|
|
182
|
+
this.latest = outcome;
|
|
183
|
+
this.deps.logger.info('发现 %d 个模型(来自 %s);已发布 %d 条路由%s', registry.models.length, endpoint, plan.routes.length, sync.applied ? '' : ` (${sync.reason ?? '未写入'})`);
|
|
184
|
+
return outcome;
|
|
185
|
+
}
|
|
186
|
+
/** 写入方案,或说明为什么没有写入任何内容。 */
|
|
187
|
+
async publish(config, plan) {
|
|
188
|
+
if (!config.sync) {
|
|
189
|
+
return { applied: false, ops: 0, routes: [], reason: '同步已禁用' };
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
return await applySync(this.deps.settings, plan.routes, plan.ownedRoutes);
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
this.deps.logger.warn('发布发现的清单失败:%s', message(error));
|
|
196
|
+
return { applied: false, ops: 0, routes: [], reason: message(error) };
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/** 把未知的可抛出对象渲染为单行原因。 */
|
|
201
|
+
export function message(error) {
|
|
202
|
+
return error instanceof Error ? error.message : String(error);
|
|
203
|
+
}
|
package/lib/sync.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 写入阶段:把方案发布到 `llm-pi-ai` 配置段。
|
|
3
|
+
*
|
|
4
|
+
* 该配置段属于另一个插件——`@deepseek-ai/dsh-llm-pi-ai`——而设置接缝不做归属检查,
|
|
5
|
+
* 因此写入它是被允许的。它同时也是被认可的机制,而不是变通做法:宿主把 pi-ai 适配器
|
|
6
|
+
* 组合为休眠状态,并声明「哪些 provider 运行由用户的设置文档决定」。发现阶段若产出了
|
|
7
|
+
* 别的东西,就必须重新实现一套协议格式,而本插件之所以存在,正是因为不必如此。
|
|
8
|
+
*
|
|
9
|
+
* 三条性质使这一写入可以安全重复。
|
|
10
|
+
*
|
|
11
|
+
* 没有任何变化时它是空操作,且比较对象是**已解析的**配置段而非原始配置段,因此部署
|
|
12
|
+
* 自身的组合层不会被视作漂移。
|
|
13
|
+
*
|
|
14
|
+
* 它只写入本插件拥有的路由键,且使用路径寻址的 op,因此配置段中其他每个 provider 都
|
|
15
|
+
* 原样保留。
|
|
16
|
+
*
|
|
17
|
+
* 它绝不会在发现失败后运行:不可达的网关会保留已经在服务的清单,因为在瞬时网络错误
|
|
18
|
+
* 下删除可用模型的刷新,比什么都不做的刷新更糟。
|
|
19
|
+
*
|
|
20
|
+
* @module dsh-aperture/sync
|
|
21
|
+
*/
|
|
22
|
+
import type { SettingsPathOp, SettingsProvider } from '@deepseek-ai/dsh-settings';
|
|
23
|
+
import type { RoutePlan } from './profile.ts';
|
|
24
|
+
/** 一次发布尝试的结果。 */
|
|
25
|
+
export interface SyncOutcome {
|
|
26
|
+
/** 是否发出了写入。 */
|
|
27
|
+
readonly applied: boolean;
|
|
28
|
+
/** 该写入携带的路径 op 数量。 */
|
|
29
|
+
readonly ops: number;
|
|
30
|
+
/** 方案发布的路由键。 */
|
|
31
|
+
readonly routes: readonly string[];
|
|
32
|
+
/** 未写入任何内容时,说明原因。 */
|
|
33
|
+
readonly reason?: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 计算使某个配置段与方案一致的路径 op。
|
|
37
|
+
*
|
|
38
|
+
* @param current - 已解析的 `llm-pi-ai` 值;命名空间未注册时为 `undefined`。
|
|
39
|
+
* @param routes - 方案希望存在的路由。
|
|
40
|
+
* @param ownedRoutes - 本插件拥有的每个路由键,因此不再有模型的路由会被移除,而不是
|
|
41
|
+
* 继续服务一份陈旧的清单。
|
|
42
|
+
* @returns 要应用的 op;配置段已经匹配时为空数组。
|
|
43
|
+
*/
|
|
44
|
+
export declare function planSync(current: unknown, routes: readonly RoutePlan[], ownedRoutes: readonly string[]): SettingsPathOp[];
|
|
45
|
+
/**
|
|
46
|
+
* 把一个方案发布到 `llm-pi-ai` 配置段。
|
|
47
|
+
*
|
|
48
|
+
* @param settings - 设置服务。
|
|
49
|
+
* @param routes - 要发布的路由。
|
|
50
|
+
* @param ownedRoutes - 本插件拥有的每个路由键。
|
|
51
|
+
* @returns 发生了什么;未写入任何内容时包含原因。
|
|
52
|
+
*/
|
|
53
|
+
export declare function applySync(settings: SettingsProvider, routes: readonly RoutePlan[], ownedRoutes: readonly string[]): Promise<SyncOutcome>;
|
|
54
|
+
/**
|
|
55
|
+
* 从配置段中撤出本插件拥有的每条路由。
|
|
56
|
+
* @param settings - 设置服务。
|
|
57
|
+
* @param ownedRoutes - 要移除的路由键。
|
|
58
|
+
* @returns 发生了什么。
|
|
59
|
+
*/
|
|
60
|
+
export declare function clearRoutes(settings: SettingsProvider, ownedRoutes: readonly string[]): Promise<SyncOutcome>;
|
|
61
|
+
/**
|
|
62
|
+
* 结构化 JSON 相等性。
|
|
63
|
+
*
|
|
64
|
+
* 之所以写出实现而不是导入,是因为这里比较的值恰好都是 JSON:本插件生成的 profile,
|
|
65
|
+
* 以及设置接缝从文档中解析回来的 profile。键顺序不属于这种同一性,因此被归一化掉。
|
|
66
|
+
*
|
|
67
|
+
* @param left - 一个 JSON 值。
|
|
68
|
+
* @param right - 另一个。
|
|
69
|
+
* @returns 两者是否结构相等。
|
|
70
|
+
*/
|
|
71
|
+
export declare function deepEqualJson(left: unknown, right: unknown): boolean;
|
package/lib/sync.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 写入阶段:把方案发布到 `llm-pi-ai` 配置段。
|
|
3
|
+
*
|
|
4
|
+
* 该配置段属于另一个插件——`@deepseek-ai/dsh-llm-pi-ai`——而设置接缝不做归属检查,
|
|
5
|
+
* 因此写入它是被允许的。它同时也是被认可的机制,而不是变通做法:宿主把 pi-ai 适配器
|
|
6
|
+
* 组合为休眠状态,并声明「哪些 provider 运行由用户的设置文档决定」。发现阶段若产出了
|
|
7
|
+
* 别的东西,就必须重新实现一套协议格式,而本插件之所以存在,正是因为不必如此。
|
|
8
|
+
*
|
|
9
|
+
* 三条性质使这一写入可以安全重复。
|
|
10
|
+
*
|
|
11
|
+
* 没有任何变化时它是空操作,且比较对象是**已解析的**配置段而非原始配置段,因此部署
|
|
12
|
+
* 自身的组合层不会被视作漂移。
|
|
13
|
+
*
|
|
14
|
+
* 它只写入本插件拥有的路由键,且使用路径寻址的 op,因此配置段中其他每个 provider 都
|
|
15
|
+
* 原样保留。
|
|
16
|
+
*
|
|
17
|
+
* 它绝不会在发现失败后运行:不可达的网关会保留已经在服务的清单,因为在瞬时网络错误
|
|
18
|
+
* 下删除可用模型的刷新,比什么都不做的刷新更糟。
|
|
19
|
+
*
|
|
20
|
+
* @module dsh-aperture/sync
|
|
21
|
+
*/
|
|
22
|
+
import { PI_AI_NAMESPACE } from "./namespaces.js";
|
|
23
|
+
/**
|
|
24
|
+
* 计算使某个配置段与方案一致的路径 op。
|
|
25
|
+
*
|
|
26
|
+
* @param current - 已解析的 `llm-pi-ai` 值;命名空间未注册时为 `undefined`。
|
|
27
|
+
* @param routes - 方案希望存在的路由。
|
|
28
|
+
* @param ownedRoutes - 本插件拥有的每个路由键,因此不再有模型的路由会被移除,而不是
|
|
29
|
+
* 继续服务一份陈旧的清单。
|
|
30
|
+
* @returns 要应用的 op;配置段已经匹配时为空数组。
|
|
31
|
+
*/
|
|
32
|
+
export function planSync(current, routes, ownedRoutes) {
|
|
33
|
+
const providers = readProviders(current);
|
|
34
|
+
// 无法容纳 provider 字典的配置段不是本插件可以据以规划的对象:写入其中只会造出
|
|
35
|
+
// 适配器无论如何都会拒绝的形状。
|
|
36
|
+
if (providers === undefined) {
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
const ops = [];
|
|
40
|
+
const desired = new Map(routes.map((route) => [route.provider, route.profile]));
|
|
41
|
+
for (const provider of ownedRoutes) {
|
|
42
|
+
const profile = desired.get(provider);
|
|
43
|
+
if (profile === undefined) {
|
|
44
|
+
if (provider in providers) {
|
|
45
|
+
ops.push({ op: 'unset', path: ['providers', provider] });
|
|
46
|
+
}
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (deepEqualJson(providers[provider], profile)) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
ops.push({ op: 'set', path: ['providers', provider], value: profile });
|
|
53
|
+
}
|
|
54
|
+
return ops;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 把一个方案发布到 `llm-pi-ai` 配置段。
|
|
58
|
+
*
|
|
59
|
+
* @param settings - 设置服务。
|
|
60
|
+
* @param routes - 要发布的路由。
|
|
61
|
+
* @param ownedRoutes - 本插件拥有的每个路由键。
|
|
62
|
+
* @returns 发生了什么;未写入任何内容时包含原因。
|
|
63
|
+
*/
|
|
64
|
+
export async function applySync(settings, routes, ownedRoutes) {
|
|
65
|
+
const current = settings.get(PI_AI_NAMESPACE);
|
|
66
|
+
if (current === undefined) {
|
|
67
|
+
return {
|
|
68
|
+
applied: false,
|
|
69
|
+
ops: 0,
|
|
70
|
+
routes: [],
|
|
71
|
+
reason: `设置命名空间 "${PI_AI_NAMESPACE}" 未注册;@deepseek-ai/dsh-llm-pi-ai 是否已挂载?`,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const providers = readProviders(current);
|
|
75
|
+
if (providers === undefined) {
|
|
76
|
+
return { applied: false, ops: 0, routes: [], reason: `"${PI_AI_NAMESPACE}" 配置段不是 provider 字典` };
|
|
77
|
+
}
|
|
78
|
+
const ops = planSync(current, routes, ownedRoutes);
|
|
79
|
+
if (ops.length === 0) {
|
|
80
|
+
return { applied: false, ops: 0, routes: routes.map((route) => route.provider), reason: '已处于同步状态' };
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
await settings.mutate(PI_AI_NAMESPACE, ops, currentRevision(settings));
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
// 并发写入者(Models 页面、另一个进程)在读取与写入之间改动了该配置段。用新的
|
|
87
|
+
// 版本号重试一次就足够了:op 是路径寻址的,因此重新规划不会丢失它们的编辑。
|
|
88
|
+
if (!isConflict(error)) {
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
const retryOps = planSync(settings.get(PI_AI_NAMESPACE), routes, ownedRoutes);
|
|
92
|
+
if (retryOps.length === 0) {
|
|
93
|
+
return { applied: false, ops: 0, routes: routes.map((route) => route.provider), reason: '已处于同步状态' };
|
|
94
|
+
}
|
|
95
|
+
await settings.mutate(PI_AI_NAMESPACE, retryOps, currentRevision(settings));
|
|
96
|
+
}
|
|
97
|
+
return { applied: true, ops: ops.length, routes: routes.map((route) => route.provider) };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 从配置段中撤出本插件拥有的每条路由。
|
|
101
|
+
* @param settings - 设置服务。
|
|
102
|
+
* @param ownedRoutes - 要移除的路由键。
|
|
103
|
+
* @returns 发生了什么。
|
|
104
|
+
*/
|
|
105
|
+
export async function clearRoutes(settings, ownedRoutes) {
|
|
106
|
+
const current = settings.get(PI_AI_NAMESPACE);
|
|
107
|
+
if (current === undefined) {
|
|
108
|
+
return {
|
|
109
|
+
applied: false,
|
|
110
|
+
ops: 0,
|
|
111
|
+
routes: [],
|
|
112
|
+
reason: `设置命名空间 "${PI_AI_NAMESPACE}" 未注册`,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const providers = readProviders(current);
|
|
116
|
+
if (providers === undefined) {
|
|
117
|
+
return { applied: false, ops: 0, routes: [], reason: `"${PI_AI_NAMESPACE}" 配置段不是 provider 字典` };
|
|
118
|
+
}
|
|
119
|
+
const ops = [];
|
|
120
|
+
for (const provider of ownedRoutes) {
|
|
121
|
+
if (provider in providers) {
|
|
122
|
+
ops.push({ op: 'unset', path: ['providers', provider] });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (ops.length === 0) {
|
|
126
|
+
return { applied: false, ops: 0, routes: [], reason: '没有需要移除的路由' };
|
|
127
|
+
}
|
|
128
|
+
await settings.mutate(PI_AI_NAMESPACE, ops, currentRevision(settings));
|
|
129
|
+
return { applied: true, ops: ops.length, routes: [] };
|
|
130
|
+
}
|
|
131
|
+
/** 从已解析的配置段值中读出 provider 字典。 */
|
|
132
|
+
function readProviders(current) {
|
|
133
|
+
if (current === null || typeof current !== 'object') {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
const providers = current.providers;
|
|
137
|
+
// 该键缺失即为空字典:适配器的 schema 会为其补上默认值,因此从未承载过 provider 的
|
|
138
|
+
// 配置段正是预期状态。
|
|
139
|
+
if (providers === undefined) {
|
|
140
|
+
return {};
|
|
141
|
+
}
|
|
142
|
+
return providers !== null && typeof providers === 'object' && !Array.isArray(providers)
|
|
143
|
+
? providers
|
|
144
|
+
: undefined;
|
|
145
|
+
}
|
|
146
|
+
/** pi-ai 配置段的当前版本号;provider 未暴露时为空。 */
|
|
147
|
+
function currentRevision(settings) {
|
|
148
|
+
try {
|
|
149
|
+
return settings.describe().find((descriptor) => descriptor.ns === PI_AI_NAMESPACE)?.revision;
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/** 判断某个可抛出对象是否为设置接缝的陈旧版本号冲突。 */
|
|
156
|
+
function isConflict(error) {
|
|
157
|
+
return error instanceof Error && error.code === 'SETTINGS_CONFLICT';
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* 结构化 JSON 相等性。
|
|
161
|
+
*
|
|
162
|
+
* 之所以写出实现而不是导入,是因为这里比较的值恰好都是 JSON:本插件生成的 profile,
|
|
163
|
+
* 以及设置接缝从文档中解析回来的 profile。键顺序不属于这种同一性,因此被归一化掉。
|
|
164
|
+
*
|
|
165
|
+
* @param left - 一个 JSON 值。
|
|
166
|
+
* @param right - 另一个。
|
|
167
|
+
* @returns 两者是否结构相等。
|
|
168
|
+
*/
|
|
169
|
+
export function deepEqualJson(left, right) {
|
|
170
|
+
if (left === right) {
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
if (Array.isArray(left) || Array.isArray(right)) {
|
|
174
|
+
if (!Array.isArray(left) || !Array.isArray(right) || left.length !== right.length) {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
return left.every((value, index) => deepEqualJson(value, right[index]));
|
|
178
|
+
}
|
|
179
|
+
if (left === null || right === null || typeof left !== 'object' || typeof right !== 'object') {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
const leftRecord = left;
|
|
183
|
+
const rightRecord = right;
|
|
184
|
+
const leftKeys = Object.keys(leftRecord).filter((key) => leftRecord[key] !== undefined);
|
|
185
|
+
const rightKeys = Object.keys(rightRecord).filter((key) => rightRecord[key] !== undefined);
|
|
186
|
+
if (leftKeys.length !== rightKeys.length) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
return leftKeys.every((key) => key in rightRecord && deepEqualJson(leftRecord[key], rightRecord[key]));
|
|
190
|
+
}
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Aperture 发现流水线的共享词汇表。
|
|
3
|
+
*
|
|
4
|
+
* 该流水线是一条纯函数链 —— 端点响应进,provider profile 出 —— 因此每个阶段都
|
|
5
|
+
* 在这里命名自己的类型,而不去碰 Cordis 或设置的接缝:只有 `src/index.ts` 和
|
|
6
|
+
* `src/sync.ts` 会接触服务。
|
|
7
|
+
*
|
|
8
|
+
* @module dsh-aperture/types
|
|
9
|
+
*/
|
|
10
|
+
/** 一个已发现模型可以声明的一种请求模态。 */
|
|
11
|
+
export type Modality = 'text' | 'image';
|
|
12
|
+
/**
|
|
13
|
+
* 本插件发布的协议格式。两者都由已安装的 `dsh-llm-pi-ai` 适配器服务,这就是发现
|
|
14
|
+
* 过程从不转换载荷的原因:它只决定每个模型在网关的哪些端点上应答。
|
|
15
|
+
*/
|
|
16
|
+
export type ApertureProtocol = 'openai-completions' | 'anthropic-messages';
|
|
17
|
+
/** 一项已归一化的事实的来源,用于 `/aperture` 报告中的诊断。 */
|
|
18
|
+
export type FactSource = 'aperture' | 'models.dev' | 'config' | 'default';
|
|
19
|
+
/** 已发现模型携带的每个值的来源。 */
|
|
20
|
+
export interface ModelProvenance {
|
|
21
|
+
/** `contextWindow` 与 `maxTokens` 的来源。 */
|
|
22
|
+
readonly limits: FactSource;
|
|
23
|
+
/** `reasoning` 的来源。 */
|
|
24
|
+
readonly reasoning: FactSource;
|
|
25
|
+
/** `input` 的来源。 */
|
|
26
|
+
readonly input: FactSource;
|
|
27
|
+
/** `name` 的来源。 */
|
|
28
|
+
readonly name: FactSource;
|
|
29
|
+
}
|
|
30
|
+
/** 经过归一化、补全与配置合并后的一个模型。 */
|
|
31
|
+
export interface DiscoveredModel {
|
|
32
|
+
/** Aperture 接受的模型 id。 */
|
|
33
|
+
readonly id: string;
|
|
34
|
+
/** 供选择器使用的显示名。 */
|
|
35
|
+
readonly name: string;
|
|
36
|
+
/**
|
|
37
|
+
* 模型可访问所用的协议,由网关的 `supported_endpoints` 推导得出。网关没有提供
|
|
38
|
+
* 本插件可服务的端点时缺失。
|
|
39
|
+
*/
|
|
40
|
+
readonly protocol?: ApertureProtocol;
|
|
41
|
+
/** 网关为该模型通告的每一个端点,用于诊断。 */
|
|
42
|
+
readonly endpoints: readonly string[];
|
|
43
|
+
/** 请求与响应合计的最大上下文长度,以 token 计。 */
|
|
44
|
+
readonly contextWindow?: number;
|
|
45
|
+
/** 最大输出 token 数。 */
|
|
46
|
+
readonly maxTokens?: number;
|
|
47
|
+
/** 为该模型声明的请求模态。 */
|
|
48
|
+
readonly input: readonly Modality[];
|
|
49
|
+
/** 该模型是否接受推理档位控制。 */
|
|
50
|
+
readonly reasoning: boolean;
|
|
51
|
+
/** 网关上报的上游 provider id,在上报时提供。 */
|
|
52
|
+
readonly provider?: string;
|
|
53
|
+
/** 每项事实的来源。 */
|
|
54
|
+
readonly provenance: ModelProvenance;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 插件 `models` 配置列表中的一条条目。
|
|
58
|
+
*
|
|
59
|
+
* id 也被端点通告的条目只覆盖它声明的字段;命名未知 id 的条目会作为额外模型加入,
|
|
60
|
+
* 网关少报时正是靠这一点保持可用。
|
|
61
|
+
*/
|
|
62
|
+
export interface ConfiguredModel {
|
|
63
|
+
/** 模型 id;与端点通告的 id 匹配,或新增一个。 */
|
|
64
|
+
readonly id: string;
|
|
65
|
+
/** 显示名。 */
|
|
66
|
+
readonly name?: string;
|
|
67
|
+
/** 协议覆盖;也是服务 Aperture 只在未服务端点上通告的模型的唯一方式。 */
|
|
68
|
+
readonly api?: string;
|
|
69
|
+
/** 上下文容量,以 token 计。 */
|
|
70
|
+
readonly contextWindow?: number;
|
|
71
|
+
/** 输出容量,以 token 计。 */
|
|
72
|
+
readonly maxTokens?: number;
|
|
73
|
+
/** 请求模态。 */
|
|
74
|
+
readonly input?: readonly Modality[];
|
|
75
|
+
/** 强制打开(`true`)或关闭(`false`)推理能力。 */
|
|
76
|
+
readonly thinking?: boolean;
|
|
77
|
+
/** 显式可选的推理档位:key = 档位,value = 协议格式中的写法。 */
|
|
78
|
+
readonly reasoningEfforts?: Readonly<Record<string, string | null>>;
|
|
79
|
+
}
|
|
80
|
+
/** 流水线把一个网关变成 provider profile 所需的全部内容。 */
|
|
81
|
+
export interface BuildOptions {
|
|
82
|
+
/** 配置的覆盖项与额外项。 */
|
|
83
|
+
readonly models: readonly ConfiguredModel[];
|
|
84
|
+
/** 非空时把清单限制为这些 id。 */
|
|
85
|
+
readonly enabledModelIds: readonly string[];
|
|
86
|
+
/**
|
|
87
|
+
* 网关模型 id → 清单模型 id,在两者不一致时使用。
|
|
88
|
+
*
|
|
89
|
+
* 网关会改名:这个网关把 DeepSeek 的 flash 模型服务为 `deepseek-flash`,把 Kimi
|
|
90
|
+
* 的服务为 `k3`,任何评分规则都无法在不猜测的情况下弥合这一差异。
|
|
91
|
+
*/
|
|
92
|
+
readonly modelAliases: Readonly<Record<string, string>>;
|
|
93
|
+
/** 没有任何来源设定容量的模型的兜底上下文容量。 */
|
|
94
|
+
readonly defaultContextWindow: number;
|
|
95
|
+
/** 是否采纳 models.dev 的输入模态(图像)。 */
|
|
96
|
+
readonly images: 'ignore' | 'metadata';
|
|
97
|
+
/** `auto` 映射推理能力;`off` 把每个模型都声明为非推理。 */
|
|
98
|
+
readonly reasoning: 'auto' | 'off';
|
|
99
|
+
}
|
|
100
|
+
/** 一次清单查找可以为模型贡献的元数据。 */
|
|
101
|
+
export interface CatalogMetadata {
|
|
102
|
+
/** 上下文容量,以 token 计。 */
|
|
103
|
+
readonly contextWindow?: number;
|
|
104
|
+
/** 输出容量,以 token 计。 */
|
|
105
|
+
readonly maxTokens?: number;
|
|
106
|
+
/** 该模型是否接受推理档位控制。 */
|
|
107
|
+
readonly reasoning?: boolean;
|
|
108
|
+
/** 请求模态。 */
|
|
109
|
+
readonly input?: readonly Modality[];
|
|
110
|
+
/** 人类可读的名称。 */
|
|
111
|
+
readonly name?: string;
|
|
112
|
+
}
|
|
113
|
+
/** 为一个端点通告的模型查找清单元数据。 */
|
|
114
|
+
export type CatalogLookup = (model: {
|
|
115
|
+
readonly id: string;
|
|
116
|
+
readonly provider?: string;
|
|
117
|
+
readonly providerName?: string;
|
|
118
|
+
}) => CatalogMetadata | undefined;
|
package/lib/types.js
ADDED
package/lib/url.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 单个 Aperture 实例的 URL 归一化。
|
|
3
|
+
*
|
|
4
|
+
* 配置里给的是实例根地址——`https://ai.example.ts.net`——本插件与 pi-ai 适配器
|
|
5
|
+
* 访问的每个端点都由它推导出来。结尾的 `/v1` 会被容忍并剥掉,而不是报错:VS Code
|
|
6
|
+
* 那个参考实现用的是相反的习惯,带着这种习惯过来的用户不该撞上 `/v1/v1`。
|
|
7
|
+
*
|
|
8
|
+
* @module dsh-aperture/url
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* 归一化配置里的实例根地址;若没有一个可用,则报告这一点。
|
|
12
|
+
*
|
|
13
|
+
* 只给主机名时按 HTTPS 假定,查询串与片段被丢弃,结尾的 `/v1`(连同任意多个
|
|
14
|
+
* 结尾斜杠)被去掉,这样调用方永远从实例根地址开始拼路径。
|
|
15
|
+
*
|
|
16
|
+
* @param raw - 原样保留的配置值。
|
|
17
|
+
* @returns 去掉结尾斜杠的归一化根地址;当值为空、或不是绝对的 HTTP(S) URL 时
|
|
18
|
+
* 返回 `undefined`。
|
|
19
|
+
*/
|
|
20
|
+
export declare function normalizeBaseUrl(raw: string | undefined): string | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* 列出某个实例所服务模型的端点。
|
|
23
|
+
* @param instanceRoot - 归一化后的实例根地址。
|
|
24
|
+
* @returns 绝对地址的模型清单 URL。
|
|
25
|
+
*/
|
|
26
|
+
export declare function buildModelsEndpoint(instanceRoot: string): string;
|
|
27
|
+
/**
|
|
28
|
+
* 某种协议下,一条 pi-ai 路由必须携带的 `baseURL`。
|
|
29
|
+
*
|
|
30
|
+
* `openai-completions` 会在路由的 `baseURL` 后面追加 `/chat/completions`,所以它
|
|
31
|
+
* 拿到的是带 `/v1` 的根地址;`anthropic-messages` 走 provider SDK,SDK 自己会追加
|
|
32
|
+
* `/v1/messages`,所以它拿到的是裸的实例根地址。
|
|
33
|
+
* @param instanceRoot - 归一化后的实例根地址。
|
|
34
|
+
* @param protocol - 该路由的协议格式。
|
|
35
|
+
* @returns 该路由的 `baseURL`。
|
|
36
|
+
*/
|
|
37
|
+
export declare function buildRouteBaseUrl(instanceRoot: string, protocol: 'openai-completions' | 'anthropic-messages'): string;
|