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/profile.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 发布阶段:把发现的模型变成 `llm-pi-ai` provider profile。
|
|
3
|
+
*
|
|
4
|
+
* 这里不做任何负载转换。本插件发布的两种协议——`openai-completions` 与
|
|
5
|
+
* `anthropic-messages`——都由已安装的 `dsh-llm-pi-ai` 适配器实现,因此剩下的工作只是
|
|
6
|
+
* 逐模型陈述该适配器无法通过一个无法识别的网关 URL 推断出的事实:模型有多大、接受
|
|
7
|
+
* 什么,以及它的推理控制如何在协议格式上传输。
|
|
8
|
+
*
|
|
9
|
+
* 该适配器的两个怪癖塑造了本模块。
|
|
10
|
+
*
|
|
11
|
+
* 未在其清单中随附的路由必须声明 `api`、`baseURL` 以及**非空**的 `models` 列表,因此
|
|
12
|
+
* 空无一物的协议完全不产生路由,而不是产生一条空路由。
|
|
13
|
+
*
|
|
14
|
+
* 它的 OpenAI 兼容路径在既无凭据、又无非空 `authorization` 头时拒绝派发。经 Tailscale
|
|
15
|
+
* 认证的网关两者都不需要,因此没有配置 `apiKeyEnv` 的路由会携带一个占位头——有文档
|
|
16
|
+
* 记载、非机密,并且可以通过改为配置一个凭据引用来移除。
|
|
17
|
+
*
|
|
18
|
+
* @module dsh-aperture/profile
|
|
19
|
+
*/
|
|
20
|
+
import { isDeepSeekFamily } from "./registry.js";
|
|
21
|
+
import { buildRouteBaseUrl } from "./url.js";
|
|
22
|
+
/** 无凭据路由发送的占位凭据的默认值。 */
|
|
23
|
+
export const DEFAULT_PLACEHOLDER_CREDENTIAL = 'dsh-aperture';
|
|
24
|
+
/** 为 DeepSeek 方言模型提供的推理档位。 */
|
|
25
|
+
const DEEPSEEK_EFFORTS = { off: 'disabled', high: 'high', max: 'max' };
|
|
26
|
+
/**
|
|
27
|
+
* 为其他所有推理模型提供的推理档位。
|
|
28
|
+
*
|
|
29
|
+
* `off` 无值——适配器把它映射为「什么都不发」,对 OpenAI 兼容端点而言这就是把思考
|
|
30
|
+
* 交给模型自行决定的方式——而它之上的那一档,是 Aperture 在其所代理的全部模型上都能
|
|
31
|
+
* 接受的最宽写法。`minimal`、`xhigh` 与 `max` 都至少被某一个上游以 HTTP 400 拒绝过,
|
|
32
|
+
* 因此发现的模型绝不会提供它们;更了解的部署可以在 `models` 中逐模型声明。
|
|
33
|
+
*/
|
|
34
|
+
const GENERIC_EFFORTS = { off: null, high: 'high' };
|
|
35
|
+
/**
|
|
36
|
+
* 把归一化后的注册表变成 provider profile。
|
|
37
|
+
*
|
|
38
|
+
* @param models - 所有已发现的模型。
|
|
39
|
+
* @param options - 路由与凭据配置。
|
|
40
|
+
* @returns 要发布的路由,以及未能被服务的模型。
|
|
41
|
+
*/
|
|
42
|
+
export function buildProfilePlan(models, options) {
|
|
43
|
+
const openai = models.filter((model) => model.protocol === 'openai-completions');
|
|
44
|
+
const anthropic = models.filter((model) => model.protocol === 'anthropic-messages');
|
|
45
|
+
const routes = [];
|
|
46
|
+
if (openai.length > 0) {
|
|
47
|
+
routes.push({
|
|
48
|
+
provider: options.route,
|
|
49
|
+
profile: buildProfile('openai-completions', openai, options),
|
|
50
|
+
models: openai,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (anthropic.length > 0) {
|
|
54
|
+
routes.push({
|
|
55
|
+
provider: options.anthropicRoute,
|
|
56
|
+
profile: buildProfile('anthropic-messages', anthropic, options),
|
|
57
|
+
models: anthropic,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
routes,
|
|
62
|
+
unserved: models.filter((model) => model.protocol === undefined),
|
|
63
|
+
ownedRoutes: [options.route, options.anthropicRoute],
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** 构建一种协议的路由 profile。 */
|
|
67
|
+
function buildProfile(protocol, models, options) {
|
|
68
|
+
const headers = routeHeaders(protocol, options);
|
|
69
|
+
return {
|
|
70
|
+
displayName: protocol === 'openai-completions' ? options.displayName : options.anthropicDisplayName,
|
|
71
|
+
api: protocol,
|
|
72
|
+
baseURL: buildRouteBaseUrl(options.instanceRoot, protocol),
|
|
73
|
+
...(options.apiKeyEnv === undefined ? {} : { apiKeyEnv: options.apiKeyEnv }),
|
|
74
|
+
...(headers === undefined ? {} : { headers }),
|
|
75
|
+
models: models.map((model) => buildModelEntry(model, protocol, options)),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 一条路由发送的头。
|
|
80
|
+
*
|
|
81
|
+
* 已配置的 `apiKeyEnv` 会完全取代占位头——此后由凭据接缝提供该头。插件自身配置中的
|
|
82
|
+
* 部署头在两种情况下都优先于占位头,需要真实静态令牌的网关正是借此拿到它。
|
|
83
|
+
*
|
|
84
|
+
* @param protocol - 该路由的协议。
|
|
85
|
+
* @param options - 配置。
|
|
86
|
+
* @returns 头字典;该路由不需要任何头时为 `undefined`。
|
|
87
|
+
*/
|
|
88
|
+
function routeHeaders(protocol, options) {
|
|
89
|
+
const headers = {};
|
|
90
|
+
const hasCredential = options.apiKeyEnv !== undefined ||
|
|
91
|
+
Object.keys(options.headers).some((name) => {
|
|
92
|
+
const lower = name.toLowerCase();
|
|
93
|
+
return lower === 'authorization' || lower === 'x-api-key' || lower === 'cf-aig-authorization';
|
|
94
|
+
});
|
|
95
|
+
if (!hasCredential && options.placeholderCredential.length > 0) {
|
|
96
|
+
if (protocol === 'openai-completions') {
|
|
97
|
+
headers.authorization = `Bearer ${options.placeholderCredential}`;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
headers['x-api-key'] = options.placeholderCredential;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
Object.assign(headers, options.headers);
|
|
104
|
+
return Object.keys(headers).length === 0 ? undefined : headers;
|
|
105
|
+
}
|
|
106
|
+
/** 构建一条已配置的模型条目。 */
|
|
107
|
+
function buildModelEntry(model, protocol, options) {
|
|
108
|
+
const configured = options.configured.find((candidate) => candidate.id.trim() === model.id);
|
|
109
|
+
const reasoning = resolveReasoning(model, protocol, configured);
|
|
110
|
+
return {
|
|
111
|
+
id: model.id,
|
|
112
|
+
name: model.name,
|
|
113
|
+
...(model.contextWindow === undefined ? {} : { contextWindow: model.contextWindow }),
|
|
114
|
+
...(model.maxTokens === undefined ? {} : { maxTokens: model.maxTokens }),
|
|
115
|
+
input: [...model.input],
|
|
116
|
+
...reasoning,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/** 解析一条模型条目携带的推理字段。 */
|
|
120
|
+
function resolveReasoning(model, protocol, configured) {
|
|
121
|
+
if (configured?.reasoningEfforts !== undefined) {
|
|
122
|
+
return { reasoningEfforts: { ...configured.reasoningEfforts }, ...compatFor(model, protocol) };
|
|
123
|
+
}
|
|
124
|
+
if (!model.reasoning) {
|
|
125
|
+
return {};
|
|
126
|
+
}
|
|
127
|
+
// Anthropic 传输通过 token 预算而非推理档位来驱动思考,而网关的列表完全没有说明
|
|
128
|
+
// 上游接受哪种预算。因此除非部署另有声明,发现的 Anthropic 模型一律按不具备推理
|
|
129
|
+
// 能力处理。
|
|
130
|
+
if (protocol === 'anthropic-messages' && configured?.thinking !== true) {
|
|
131
|
+
return {};
|
|
132
|
+
}
|
|
133
|
+
if (isDeepSeekFamily(model)) {
|
|
134
|
+
// 对 DeepSeek 方言而言 `off` 必须非 null:只有当该档位映射到某个值时,适配器才会
|
|
135
|
+
// 发送 `thinking: { type: "disabled" }`。
|
|
136
|
+
return { reasoningEfforts: { ...DEEPSEEK_EFFORTS }, ...compatFor(model, protocol) };
|
|
137
|
+
}
|
|
138
|
+
return { reasoningEfforts: { ...GENERIC_EFFORTS }, ...compatFor(model, protocol) };
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* 推理模型所需的 compat 块。
|
|
142
|
+
*
|
|
143
|
+
* pi-ai 从 provider id 与 base URL 推断协议格式兼容性,而 Aperture 的 URL 对它说明不了
|
|
144
|
+
* 什么,因此必须直接声明 DeepSeek 方言。`supportsReasoningEffort` 也要声明:URL 无法被
|
|
145
|
+
* pi-ai 归位的网关目前会被默认为 true,但真正让 `reasoning_effort` 传输出去的是这个
|
|
146
|
+
* 开关,而一条路由不应依赖默认值。
|
|
147
|
+
*
|
|
148
|
+
* @param model - 被描述的模型。
|
|
149
|
+
* @param protocol - 该路由的协议。
|
|
150
|
+
* @returns compat 块,或空对象。
|
|
151
|
+
*/
|
|
152
|
+
function compatFor(model, protocol) {
|
|
153
|
+
if (protocol !== 'openai-completions') {
|
|
154
|
+
return {};
|
|
155
|
+
}
|
|
156
|
+
const compat = { supportsReasoningEffort: true };
|
|
157
|
+
if (isDeepSeekFamily(model)) {
|
|
158
|
+
compat.thinkingFormat = 'deepseek';
|
|
159
|
+
}
|
|
160
|
+
return { compat };
|
|
161
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 归一化阶段:一条原始网关条目,加上清单元数据,再加上配置,变成一个
|
|
3
|
+
* `DiscoveredModel`。
|
|
4
|
+
*
|
|
5
|
+
* 三个决策只在这里做出,别处没有。
|
|
6
|
+
*
|
|
7
|
+
* **模型从哪个端点应答。** Aperture 会报告每个模型可通过哪些路径访问,并用一个
|
|
8
|
+
* 404 拒绝错误的路径、同时给出正确的路径。只通过原生 `generateContent`
|
|
9
|
+
* 传输服务的 Gemini 模型,`dsh-llm-pi-ai` 根本够不到;把它发布到
|
|
10
|
+
* OpenAI 兼容路由上,只会得到一个每个请求都失败的模型。因此由通告的端点决定
|
|
11
|
+
* 路由,而没有路由可供本插件服务的模型会保留在报告里,但不发布到任何地方。
|
|
12
|
+
*
|
|
13
|
+
* **谁来决定模型的容量。** Aperture 优先,因为网关知道每个上游前面的代理接受
|
|
14
|
+
* 什么;models.dev 只回答 Aperture 没有说明的部分;配置的兜底值最后回答。兜底
|
|
15
|
+
* 值只设定 `contextWindow` —— 没人声明过的输出上限保持缺失,这样适配器会把路由
|
|
16
|
+
* 兜底当作一种能力,而不是把每个请求都限制在一个凭空捏造的数字上。
|
|
17
|
+
*
|
|
18
|
+
* **谁来决定模型是否会推理。** 依次是 Aperture、models.dev、插件的 `reasoning`
|
|
19
|
+
* 开关。错误的「是」会让用户选择推理档位时付出一个 400 的代价,因此不从模型名
|
|
20
|
+
* 猜测任何东西。
|
|
21
|
+
*
|
|
22
|
+
* @module dsh-aperture/registry
|
|
23
|
+
*/
|
|
24
|
+
import type { ApertureProtocol, BuildOptions, CatalogLookup, DiscoveredModel, Modality, ModelProvenance } from './types.ts';
|
|
25
|
+
/** 一次归一化的结果。 */
|
|
26
|
+
export interface RegistryResult {
|
|
27
|
+
/** 所有模型,包括没有路由能服务的那些(它们的 `protocol` 缺失)。 */
|
|
28
|
+
readonly models: readonly DiscoveredModel[];
|
|
29
|
+
/** 没有任何已配置路由能服务的子集。 */
|
|
30
|
+
readonly unserved: readonly DiscoveredModel[];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 归一化、补全并合并一份网关清单。
|
|
34
|
+
*
|
|
35
|
+
* @param entries - 按端点顺序排列的原始模型行。
|
|
36
|
+
* @param options - 配置开关。
|
|
37
|
+
* @param lookup - 清单元数据查找器,在已加载时提供。
|
|
38
|
+
* @returns 每个模型及其来源,以及没有路由能服务的那些模型。
|
|
39
|
+
*/
|
|
40
|
+
export declare function buildRegistry(entries: readonly unknown[], options: BuildOptions, lookup?: CatalogLookup): RegistryResult;
|
|
41
|
+
/**
|
|
42
|
+
* 决定一个模型通告的端点适配哪条路由。
|
|
43
|
+
*
|
|
44
|
+
* 完全没有通告任何端点的清单按 OpenAI 兼容处理,因为不上报自身传输方式的网关,
|
|
45
|
+
* 绝大多数情况下就是一个普通的 Chat Completions 代理,而且没有路由的模型无论
|
|
46
|
+
* 如何都不可用。
|
|
47
|
+
*
|
|
48
|
+
* @param endpoints - 通告的端点路径。
|
|
49
|
+
* @returns 可服务的协议;没有适配的协议时为 `undefined`。
|
|
50
|
+
*/
|
|
51
|
+
export declare function classifyProtocol(endpoints: readonly string[]): ApertureProtocol | undefined;
|
|
52
|
+
/** 为 `/aperture models` 报告渲染一个模型的来源。 */
|
|
53
|
+
export declare function describeProvenance(provenance: ModelProvenance): string;
|
|
54
|
+
/** 判断一个模型是否属于 DeepSeek 推理方言。 */
|
|
55
|
+
export declare function isDeepSeekFamily(model: DiscoveredModel): boolean;
|
|
56
|
+
/** 为报告中的一行渲染一个模态列表。 */
|
|
57
|
+
export declare function formatModalities(input: readonly Modality[]): string;
|
package/lib/registry.js
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 归一化阶段:一条原始网关条目,加上清单元数据,再加上配置,变成一个
|
|
3
|
+
* `DiscoveredModel`。
|
|
4
|
+
*
|
|
5
|
+
* 三个决策只在这里做出,别处没有。
|
|
6
|
+
*
|
|
7
|
+
* **模型从哪个端点应答。** Aperture 会报告每个模型可通过哪些路径访问,并用一个
|
|
8
|
+
* 404 拒绝错误的路径、同时给出正确的路径。只通过原生 `generateContent`
|
|
9
|
+
* 传输服务的 Gemini 模型,`dsh-llm-pi-ai` 根本够不到;把它发布到
|
|
10
|
+
* OpenAI 兼容路由上,只会得到一个每个请求都失败的模型。因此由通告的端点决定
|
|
11
|
+
* 路由,而没有路由可供本插件服务的模型会保留在报告里,但不发布到任何地方。
|
|
12
|
+
*
|
|
13
|
+
* **谁来决定模型的容量。** Aperture 优先,因为网关知道每个上游前面的代理接受
|
|
14
|
+
* 什么;models.dev 只回答 Aperture 没有说明的部分;配置的兜底值最后回答。兜底
|
|
15
|
+
* 值只设定 `contextWindow` —— 没人声明过的输出上限保持缺失,这样适配器会把路由
|
|
16
|
+
* 兜底当作一种能力,而不是把每个请求都限制在一个凭空捏造的数字上。
|
|
17
|
+
*
|
|
18
|
+
* **谁来决定模型是否会推理。** 依次是 Aperture、models.dev、插件的 `reasoning`
|
|
19
|
+
* 开关。错误的「是」会让用户选择推理档位时付出一个 400 的代价,因此不从模型名
|
|
20
|
+
* 猜测任何东西。
|
|
21
|
+
*
|
|
22
|
+
* @module dsh-aperture/registry
|
|
23
|
+
*/
|
|
24
|
+
import { extractCapabilities, extractDisplayName, extractEndpoints, extractLimits, extractModelId, extractProvider, } from "./metadata/extract.js";
|
|
25
|
+
/** 每个 OpenAI 兼容模型都必须通告的端点。 */
|
|
26
|
+
const OPENAI_ENDPOINT = '/v1/chat/completions';
|
|
27
|
+
/** 每个 Anthropic Messages 模型都必须通告的端点。 */
|
|
28
|
+
const ANTHROPIC_ENDPOINT = '/v1/messages';
|
|
29
|
+
/**
|
|
30
|
+
* 归一化、补全并合并一份网关清单。
|
|
31
|
+
*
|
|
32
|
+
* @param entries - 按端点顺序排列的原始模型行。
|
|
33
|
+
* @param options - 配置开关。
|
|
34
|
+
* @param lookup - 清单元数据查找器,在已加载时提供。
|
|
35
|
+
* @returns 每个模型及其来源,以及没有路由能服务的那些模型。
|
|
36
|
+
*/
|
|
37
|
+
export function buildRegistry(entries, options, lookup) {
|
|
38
|
+
const enabled = new Set(options.enabledModelIds.map((id) => id.trim()).filter((id) => id.length > 0));
|
|
39
|
+
const configuredById = new Map();
|
|
40
|
+
for (const configured of options.models) {
|
|
41
|
+
const id = configured.id.trim();
|
|
42
|
+
if (id.length > 0 && !configuredById.has(id)) {
|
|
43
|
+
configuredById.set(id, configured);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const seen = new Set();
|
|
47
|
+
const models = [];
|
|
48
|
+
for (const entry of entries) {
|
|
49
|
+
const id = extractModelId(entry);
|
|
50
|
+
if (id === undefined || seen.has(id)) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (enabled.size > 0 && !enabled.has(id)) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
seen.add(id);
|
|
57
|
+
models.push(fromEndpoint(entry, id, options, lookup));
|
|
58
|
+
}
|
|
59
|
+
// 配置里声明、但网关没有通告的模型仍会进入选择器;
|
|
60
|
+
// 网关少报自己的清单时,正是靠这一点保持可用。
|
|
61
|
+
for (const [id] of configuredById) {
|
|
62
|
+
if (seen.has(id)) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
seen.add(id);
|
|
66
|
+
models.push(fromConfiguration(id, options, lookup));
|
|
67
|
+
}
|
|
68
|
+
const merged = models.map((model) => {
|
|
69
|
+
const configured = configuredById.get(model.id);
|
|
70
|
+
return configured === undefined ? model : applyConfigured(model, configured, options);
|
|
71
|
+
});
|
|
72
|
+
return {
|
|
73
|
+
models: merged,
|
|
74
|
+
unserved: merged.filter((model) => model.protocol === undefined),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** 归一化网关通告的一行。 */
|
|
78
|
+
function fromEndpoint(entry, id, options, lookup) {
|
|
79
|
+
const apertureLimits = extractLimits(entry);
|
|
80
|
+
const capabilities = extractCapabilities(entry);
|
|
81
|
+
const provider = extractProvider(entry);
|
|
82
|
+
const endpoints = extractEndpoints(entry);
|
|
83
|
+
const apertureName = extractDisplayName(entry);
|
|
84
|
+
const catalog = lookupModel(lookup, id, provider, options.modelAliases);
|
|
85
|
+
const protocol = classifyProtocol(endpoints);
|
|
86
|
+
return {
|
|
87
|
+
id,
|
|
88
|
+
name: apertureName ?? catalog?.name ?? id,
|
|
89
|
+
...(protocol === undefined ? {} : { protocol }),
|
|
90
|
+
endpoints,
|
|
91
|
+
contextWindow: apertureLimits?.contextWindow ?? catalog?.contextWindow ?? options.defaultContextWindow,
|
|
92
|
+
...resolveOutputCap(apertureLimits?.maxTokens ?? catalog?.maxTokens),
|
|
93
|
+
input: options.images === 'metadata' ? (capabilities?.input ?? catalog?.input ?? ['text']) : ['text'],
|
|
94
|
+
reasoning: options.reasoning === 'auto' && (capabilities?.reasoning ?? catalog?.reasoning ?? false),
|
|
95
|
+
...(provider?.id === undefined ? {} : { provider: provider.id }),
|
|
96
|
+
provenance: {
|
|
97
|
+
limits: limitsSource(apertureLimits, catalog),
|
|
98
|
+
reasoning: factsSource(capabilities?.reasoning, catalog?.reasoning),
|
|
99
|
+
input: factsSource(capabilities?.input, catalog?.input),
|
|
100
|
+
name: apertureName !== undefined ? 'aperture' : catalog?.name !== undefined ? 'models.dev' : 'default',
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/** 归一化一个只因配置点名而存在的模型。 */
|
|
105
|
+
function fromConfiguration(id, options, lookup) {
|
|
106
|
+
const catalog = lookupModel(lookup, id, undefined, options.modelAliases);
|
|
107
|
+
return {
|
|
108
|
+
id,
|
|
109
|
+
name: catalog?.name ?? id,
|
|
110
|
+
endpoints: [],
|
|
111
|
+
contextWindow: catalog?.contextWindow ?? options.defaultContextWindow,
|
|
112
|
+
...resolveOutputCap(catalog?.maxTokens),
|
|
113
|
+
input: options.images === 'metadata' ? (catalog?.input ?? ['text']) : ['text'],
|
|
114
|
+
reasoning: options.reasoning === 'auto' && (catalog?.reasoning ?? false),
|
|
115
|
+
provenance: {
|
|
116
|
+
limits: limitsSource(undefined, catalog),
|
|
117
|
+
reasoning: factsSource(undefined, catalog?.reasoning),
|
|
118
|
+
input: factsSource(undefined, catalog?.input),
|
|
119
|
+
name: catalog?.name !== undefined ? 'models.dev' : 'default',
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/** 把一条配置条目声明的字段应用到一个已发现的模型上。 */
|
|
124
|
+
function applyConfigured(model, configured, options) {
|
|
125
|
+
const protocol = protocolFromConfigured(configured) ?? model.protocol;
|
|
126
|
+
const contextWindow = configured.contextWindow ?? model.contextWindow;
|
|
127
|
+
const maxTokens = configured.maxTokens ?? model.maxTokens;
|
|
128
|
+
const name = configured.name?.trim() || model.name;
|
|
129
|
+
const input = configured.input ?? model.input;
|
|
130
|
+
return {
|
|
131
|
+
id: model.id,
|
|
132
|
+
name,
|
|
133
|
+
...(protocol === undefined ? {} : { protocol }),
|
|
134
|
+
endpoints: model.endpoints,
|
|
135
|
+
contextWindow: contextWindow ?? options.defaultContextWindow,
|
|
136
|
+
...resolveOutputCap(maxTokens),
|
|
137
|
+
input: configured.input !== undefined || options.images === 'metadata' ? input : ['text'],
|
|
138
|
+
reasoning: configured.thinking ?? model.reasoning,
|
|
139
|
+
...(model.provider === undefined ? {} : { provider: model.provider }),
|
|
140
|
+
provenance: {
|
|
141
|
+
limits: configured.contextWindow !== undefined || configured.maxTokens !== undefined
|
|
142
|
+
? 'config'
|
|
143
|
+
: model.provenance.limits,
|
|
144
|
+
reasoning: configured.thinking !== undefined ? 'config' : model.provenance.reasoning,
|
|
145
|
+
input: configured.input !== undefined ? 'config' : model.provenance.input,
|
|
146
|
+
name: configured.name?.trim() ? 'config' : model.provenance.name,
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/** 仅当确实有来源声明了输出上限时才带上它。 */
|
|
151
|
+
function resolveOutputCap(maxTokens) {
|
|
152
|
+
return maxTokens === undefined ? {} : { maxTokens };
|
|
153
|
+
}
|
|
154
|
+
/** 防御式地查询清单查找器:查找器绝不能导致刷新失败。 */
|
|
155
|
+
function lookupModel(lookup, id, provider, aliases) {
|
|
156
|
+
if (lookup === undefined) {
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
const candidates = [aliases[id], id].filter((candidate) => typeof candidate === 'string' && candidate.trim().length > 0);
|
|
160
|
+
for (const candidate of candidates) {
|
|
161
|
+
try {
|
|
162
|
+
const found = lookup({
|
|
163
|
+
id: candidate,
|
|
164
|
+
...(provider?.id === undefined ? {} : { provider: provider.id }),
|
|
165
|
+
...(provider?.name === undefined ? {} : { providerName: provider.name }),
|
|
166
|
+
});
|
|
167
|
+
if (found !== undefined) {
|
|
168
|
+
return found;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
// 清单查找只是参考性的;坏掉的查找器不能导致刷新失败。
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
/** 容量类事实的来源。 */
|
|
178
|
+
function limitsSource(apertureLimits, catalog) {
|
|
179
|
+
if (apertureLimits !== undefined) {
|
|
180
|
+
return 'aperture';
|
|
181
|
+
}
|
|
182
|
+
return catalog?.contextWindow !== undefined || catalog?.maxTokens !== undefined ? 'models.dev' : 'default';
|
|
183
|
+
}
|
|
184
|
+
/** 一项 Aperture 与清单都会回答的能力事实的来源。 */
|
|
185
|
+
function factsSource(apertureFact, catalogFact) {
|
|
186
|
+
if (apertureFact !== undefined) {
|
|
187
|
+
return 'aperture';
|
|
188
|
+
}
|
|
189
|
+
return catalogFact !== undefined ? 'models.dev' : 'default';
|
|
190
|
+
}
|
|
191
|
+
/** 把配置里的一种 `api` 写法映射到可服务的协议。 */
|
|
192
|
+
function protocolFromConfigured(configured) {
|
|
193
|
+
switch (configured.api?.trim()) {
|
|
194
|
+
case 'openai-completions':
|
|
195
|
+
return 'openai-completions';
|
|
196
|
+
case 'anthropic-messages':
|
|
197
|
+
return 'anthropic-messages';
|
|
198
|
+
default:
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* 决定一个模型通告的端点适配哪条路由。
|
|
204
|
+
*
|
|
205
|
+
* 完全没有通告任何端点的清单按 OpenAI 兼容处理,因为不上报自身传输方式的网关,
|
|
206
|
+
* 绝大多数情况下就是一个普通的 Chat Completions 代理,而且没有路由的模型无论
|
|
207
|
+
* 如何都不可用。
|
|
208
|
+
*
|
|
209
|
+
* @param endpoints - 通告的端点路径。
|
|
210
|
+
* @returns 可服务的协议;没有适配的协议时为 `undefined`。
|
|
211
|
+
*/
|
|
212
|
+
export function classifyProtocol(endpoints) {
|
|
213
|
+
if (endpoints.length === 0) {
|
|
214
|
+
return 'openai-completions';
|
|
215
|
+
}
|
|
216
|
+
if (endpoints.some((endpoint) => matchesEndpoint(endpoint, OPENAI_ENDPOINT))) {
|
|
217
|
+
return 'openai-completions';
|
|
218
|
+
}
|
|
219
|
+
if (endpoints.some((endpoint) => matchesEndpoint(endpoint, ANTHROPIC_ENDPOINT))) {
|
|
220
|
+
return 'anthropic-messages';
|
|
221
|
+
}
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
/** 判断一个通告的端点是否就是(或以之结尾)某个已知路径。 */
|
|
225
|
+
function matchesEndpoint(advertised, known) {
|
|
226
|
+
const path = advertised.split('?')[0]?.replace(/\/+$/u, '') ?? '';
|
|
227
|
+
return path === known || path.endsWith(known);
|
|
228
|
+
}
|
|
229
|
+
/** 为 `/aperture models` 报告渲染一个模型的来源。 */
|
|
230
|
+
export function describeProvenance(provenance) {
|
|
231
|
+
const parts = [
|
|
232
|
+
provenance.limits === 'default' ? undefined : `limits:${provenance.limits}`,
|
|
233
|
+
provenance.name === 'default' ? undefined : `name:${provenance.name}`,
|
|
234
|
+
provenance.reasoning === 'default' ? undefined : `reasoning:${provenance.reasoning}`,
|
|
235
|
+
provenance.input === 'default' ? undefined : `input:${provenance.input}`,
|
|
236
|
+
].filter((part) => part !== undefined);
|
|
237
|
+
return parts.length === 0 ? 'defaults' : parts.join(' ');
|
|
238
|
+
}
|
|
239
|
+
/** 判断一个模型是否属于 DeepSeek 推理方言。 */
|
|
240
|
+
export function isDeepSeekFamily(model) {
|
|
241
|
+
return `${model.id} ${model.name} ${model.provider ?? ''}`.toLowerCase().includes('deepseek');
|
|
242
|
+
}
|
|
243
|
+
/** 为报告中的一行渲染一个模态列表。 */
|
|
244
|
+
export function formatModalities(input) {
|
|
245
|
+
return input.length === 0 ? 'none' : input.join('+');
|
|
246
|
+
}
|
package/lib/report.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/aperture` 命令的人类可读渲染。
|
|
3
|
+
*
|
|
4
|
+
* 在不读 `settings.yaml` 的前提下,命令输出是部署方唯一能看到发现过程决定了什么的
|
|
5
|
+
* 地方:哪个端点应答了、哪些模型进了哪条路由、哪个模型没有任何路由能服务,以及每条
|
|
6
|
+
* 事实来自哪里。因此报告会把这些全部讲出来,并使用与失败信息相同的形态——每条事实
|
|
7
|
+
* 一行,不用表格对齐。
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-aperture/report
|
|
10
|
+
*/
|
|
11
|
+
import type { ResolvedConfig } from './config.ts';
|
|
12
|
+
import type { RefreshOutcome } from './runtime.ts';
|
|
13
|
+
/** 最近一次刷新的单行状态,以及它发布的路由。 */
|
|
14
|
+
export declare function formatStatus(outcome: RefreshOutcome | undefined, config: ResolvedConfig): string;
|
|
15
|
+
/** 按路由分组的逐模型清单,带来源信息。 */
|
|
16
|
+
export declare function formatModels(outcome: RefreshOutcome | undefined): string;
|
package/lib/report.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/aperture` 命令的人类可读渲染。
|
|
3
|
+
*
|
|
4
|
+
* 在不读 `settings.yaml` 的前提下,命令输出是部署方唯一能看到发现过程决定了什么的
|
|
5
|
+
* 地方:哪个端点应答了、哪些模型进了哪条路由、哪个模型没有任何路由能服务,以及每条
|
|
6
|
+
* 事实来自哪里。因此报告会把这些全部讲出来,并使用与失败信息相同的形态——每条事实
|
|
7
|
+
* 一行,不用表格对齐。
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-aperture/report
|
|
10
|
+
*/
|
|
11
|
+
import { formatModalities, describeProvenance } from "./registry.js";
|
|
12
|
+
/** token 计数的千位分隔符。 */
|
|
13
|
+
const COUNT = new Intl.NumberFormat('en-US');
|
|
14
|
+
/** 最近一次刷新的单行状态,以及它发布的路由。 */
|
|
15
|
+
export function formatStatus(outcome, config) {
|
|
16
|
+
const where = config.instanceRoot ?? (config.rawBaseUrl.length === 0 ? '(未配置 baseUrl)' : config.rawBaseUrl);
|
|
17
|
+
const lines = [`Aperture:${where}`];
|
|
18
|
+
if (outcome === undefined) {
|
|
19
|
+
lines.push(' 尚未完成任何刷新');
|
|
20
|
+
return lines.join('\n');
|
|
21
|
+
}
|
|
22
|
+
lines.push(` 最近一次刷新:${outcome.trigger} · ${outcome.at.toISOString()} · ${outcome.durationMs}ms · ${outcome.ok ? '成功' : '失败'}`);
|
|
23
|
+
if (outcome.error !== undefined) {
|
|
24
|
+
lines.push(` 错误:${outcome.error}`);
|
|
25
|
+
}
|
|
26
|
+
lines.push(outcome.catalog.lookup === undefined
|
|
27
|
+
? ` 清单:不可用(${outcome.catalog.reason ?? '原因未知'})`
|
|
28
|
+
: ` 清单:${outcome.catalog.entries} 个条目`);
|
|
29
|
+
if (outcome.endpoint !== undefined) {
|
|
30
|
+
lines.push(` 端点:${outcome.endpoint} 列出了 ${outcome.listed} 行`);
|
|
31
|
+
}
|
|
32
|
+
if (outcome.routes.length === 0) {
|
|
33
|
+
lines.push(' 路由:无');
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
for (const route of outcome.routes) {
|
|
37
|
+
lines.push(` 路由 ${route.provider}:${route.models.length} 个模型,经由 ${route.profile.api} → ${route.profile.baseURL}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (outcome.unserved.length > 0) {
|
|
41
|
+
lines.push(` 未服务:${outcome.unserved.length} 个模型(${outcome.unserved.map((model) => model.id).join(', ')})`);
|
|
42
|
+
}
|
|
43
|
+
const sync = outcome.sync;
|
|
44
|
+
if (sync !== undefined) {
|
|
45
|
+
lines.push(sync.applied
|
|
46
|
+
? ` 设置:向 llm-pi-ai 写入 ${sync.ops} 个操作(${sync.routes.join(', ') || '仅移除'})`
|
|
47
|
+
: ` 设置:未写入(${sync.reason ?? '原因未知'})`);
|
|
48
|
+
}
|
|
49
|
+
return lines.join('\n');
|
|
50
|
+
}
|
|
51
|
+
/** 按路由分组的逐模型清单,带来源信息。 */
|
|
52
|
+
export function formatModels(outcome) {
|
|
53
|
+
if (outcome === undefined) {
|
|
54
|
+
return '尚未完成任何刷新;请运行 /aperture refresh';
|
|
55
|
+
}
|
|
56
|
+
if (outcome.models.length === 0) {
|
|
57
|
+
return outcome.error === undefined ? '未发现任何模型' : `未发现任何内容:${outcome.error}`;
|
|
58
|
+
}
|
|
59
|
+
const lines = [];
|
|
60
|
+
const routed = new Set();
|
|
61
|
+
for (const route of outcome.routes) {
|
|
62
|
+
lines.push(`${route.provider} (${route.profile.api})`);
|
|
63
|
+
for (const model of route.models) {
|
|
64
|
+
routed.add(model.id);
|
|
65
|
+
lines.push(` ${formatModel(model)}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const unserved = outcome.models.filter((model) => !routed.has(model.id));
|
|
69
|
+
if (unserved.length > 0) {
|
|
70
|
+
lines.push('未服务(没有本插件可发布的端点)');
|
|
71
|
+
for (const model of unserved) {
|
|
72
|
+
const endpoints = model.endpoints.length === 0 ? '未通告任何端点' : model.endpoints.join(', ');
|
|
73
|
+
lines.push(` ${model.id} — ${endpoints}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return lines.join('\n');
|
|
77
|
+
}
|
|
78
|
+
/** 单行模型信息:名称、尺寸、模态、推理、来源。 */
|
|
79
|
+
function formatModel(model) {
|
|
80
|
+
const facts = [
|
|
81
|
+
`${COUNT.format(model.contextWindow ?? 0)} 上下文窗口`,
|
|
82
|
+
model.maxTokens === undefined ? undefined : `${COUNT.format(model.maxTokens)} 输出`,
|
|
83
|
+
formatModalities(model.input),
|
|
84
|
+
model.reasoning ? '推理' : '无推理',
|
|
85
|
+
describeProvenance(model.provenance),
|
|
86
|
+
].filter((fact) => fact !== undefined);
|
|
87
|
+
const label = model.name === model.id ? '' : ` (${model.name})`;
|
|
88
|
+
return `${model.id}${label} — ${facts.join(' · ')}`;
|
|
89
|
+
}
|
package/lib/runtime.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
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 type { SettingsProvider } from '@deepseek-ai/dsh-settings';
|
|
15
|
+
import { ModelCatalog, type CatalogLoad } from './catalog.ts';
|
|
16
|
+
import { type ResolvedConfig } from './config.ts';
|
|
17
|
+
import { type RoutePlan } from './profile.ts';
|
|
18
|
+
import { type SyncOutcome } from './sync.ts';
|
|
19
|
+
import type { DiscoveredModel } from './types.ts';
|
|
20
|
+
/** 运行时用来输出诊断信息的最小日志接口。 */
|
|
21
|
+
export interface RuntimeLogger {
|
|
22
|
+
error(message: string, ...rest: unknown[]): void;
|
|
23
|
+
info(message: string, ...rest: unknown[]): void;
|
|
24
|
+
warn(message: string, ...rest: unknown[]): void;
|
|
25
|
+
debug(message: string, ...rest: unknown[]): void;
|
|
26
|
+
}
|
|
27
|
+
/** 一次已完成的刷新。 */
|
|
28
|
+
export interface RefreshOutcome {
|
|
29
|
+
/** 触发这次刷新的来源。 */
|
|
30
|
+
readonly trigger: string;
|
|
31
|
+
/** 发现与发布是否都成功。 */
|
|
32
|
+
readonly ok: boolean;
|
|
33
|
+
/** 完成时间。 */
|
|
34
|
+
readonly at: Date;
|
|
35
|
+
/** 耗时。 */
|
|
36
|
+
readonly durationMs: number;
|
|
37
|
+
/** 应答的 URL(如果有)。 */
|
|
38
|
+
readonly endpoint?: string;
|
|
39
|
+
/** 网关列出的模型行数。 */
|
|
40
|
+
readonly listed: number;
|
|
41
|
+
/** 全部归一化后的模型。 */
|
|
42
|
+
readonly models: readonly DiscoveredModel[];
|
|
43
|
+
/** 该方案发布的路由。 */
|
|
44
|
+
readonly routes: readonly RoutePlan[];
|
|
45
|
+
/** 没有任何路由能服务的模型。 */
|
|
46
|
+
readonly unserved: readonly DiscoveredModel[];
|
|
47
|
+
/** 清单提供的内容。 */
|
|
48
|
+
readonly catalog: CatalogLoad;
|
|
49
|
+
/** 设置写入的结果(如果尝试过写入)。 */
|
|
50
|
+
readonly sync?: SyncOutcome;
|
|
51
|
+
/** 刷新失败时的错误。 */
|
|
52
|
+
readonly error?: string;
|
|
53
|
+
}
|
|
54
|
+
/** 运行时从宿主读取的全部内容。 */
|
|
55
|
+
export interface RuntimeDeps {
|
|
56
|
+
/** 当前生效的配置,每次刷新都会重新读取。 */
|
|
57
|
+
readonly config: () => ResolvedConfig;
|
|
58
|
+
/** 设置服务;插件把它声明为必需注入。 */
|
|
59
|
+
readonly settings: SettingsProvider;
|
|
60
|
+
/** 具名 logger。 */
|
|
61
|
+
readonly logger: RuntimeLogger;
|
|
62
|
+
/** models.dev 缓存,在各次刷新之间共享。 */
|
|
63
|
+
readonly catalog: ModelCatalog;
|
|
64
|
+
}
|
|
65
|
+
/** 单飞(single-flight)的发现与发布。 */
|
|
66
|
+
export declare class ApertureRuntime {
|
|
67
|
+
private readonly deps;
|
|
68
|
+
private running;
|
|
69
|
+
private queued;
|
|
70
|
+
private latest;
|
|
71
|
+
/**
|
|
72
|
+
* @param deps - 宿主服务与配置活引用(thunk)。
|
|
73
|
+
*/
|
|
74
|
+
constructor(deps: RuntimeDeps);
|
|
75
|
+
/** 最近一次已完成的刷新(如果完成过)。 */
|
|
76
|
+
last(): RefreshOutcome | undefined;
|
|
77
|
+
/**
|
|
78
|
+
* 刷新;若已有刷新在运行,则把本次刷新排在其后。
|
|
79
|
+
* @param trigger - 触发来源;由 `/aperture` 报告。
|
|
80
|
+
* @returns 本次调用所参与的那次刷新的结果。
|
|
81
|
+
*/
|
|
82
|
+
refresh(trigger: string): Promise<RefreshOutcome>;
|
|
83
|
+
/**
|
|
84
|
+
* 从设置文档中撤出本插件的路由。
|
|
85
|
+
* @returns 供命令界面展示的可读结果。
|
|
86
|
+
*/
|
|
87
|
+
remove(): Promise<string>;
|
|
88
|
+
/** 执行一次刷新,绝不抛出异常。 */
|
|
89
|
+
private run;
|
|
90
|
+
/** 一次刷新的主体。 */
|
|
91
|
+
private execute;
|
|
92
|
+
/** 写入方案,或说明为什么没有写入任何内容。 */
|
|
93
|
+
private publish;
|
|
94
|
+
}
|
|
95
|
+
/** 把未知的可抛出对象渲染为单行原因。 */
|
|
96
|
+
export declare function message(error: unknown): string;
|