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/config.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 配置 schema 与解析。
|
|
3
|
+
*
|
|
4
|
+
* 本插件拥有一个设置命名空间 `aperture`:它的组合层是 bundle 里
|
|
5
|
+
* `cordis.patch.yml` 的那一行,用户层是 `$DSH_HOME/settings.yaml` 里的
|
|
6
|
+
* `aperture:` 段。而它发布出去的一切都写进**另一个**命名空间 `llm-pi-ai`——
|
|
7
|
+
* 真正提供这些路由的适配器。这个分工就是整个设计:本插件决定有哪些模型,
|
|
8
|
+
* 那个适配器决定怎么跟它们说话。
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-aperture/config
|
|
11
|
+
*/
|
|
12
|
+
import z from '@deepseek-ai/schemastery';
|
|
13
|
+
import { DEFAULT_PLACEHOLDER_CREDENTIAL } from "./profile.js";
|
|
14
|
+
import { normalizeBaseUrl } from "./url.js";
|
|
15
|
+
export { APERTURE_NAMESPACE, PI_AI_NAMESPACE } from "./namespaces.js";
|
|
16
|
+
/** 默认清单地址;参考实现用的是同一份文档。 */
|
|
17
|
+
export const DEFAULT_MODEL_METADATA_URL = 'https://models.dev/models.json';
|
|
18
|
+
/** 当 Aperture 与清单都没给出容量时,为模型假定的上下文容量。 */
|
|
19
|
+
export const DEFAULT_CONTEXT_WINDOW = 128_000;
|
|
20
|
+
/** provider 路由键的文法,与 Models 页面自身的规则一致。 */
|
|
21
|
+
const ROUTE_PATTERN = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
22
|
+
/** 一个配置好的模型:对已发现模型的覆盖,或者一个额外的模型。 */
|
|
23
|
+
const modelConfig = z.object({
|
|
24
|
+
/** 模型 id;与已发现的 id 对应,或者新增一个网关没有列出的模型。 */
|
|
25
|
+
id: z.string().required(),
|
|
26
|
+
/** 选择器里显示的名字。 */
|
|
27
|
+
name: z.string(),
|
|
28
|
+
/** 协议覆盖:`openai-completions` 或 `anthropic-messages`。 */
|
|
29
|
+
api: z.string(),
|
|
30
|
+
/** 上下文容量(token 数)。 */
|
|
31
|
+
contextWindow: z.number().step(1).min(1),
|
|
32
|
+
/** 输出能力(token 数)。 */
|
|
33
|
+
maxTokens: z.number().step(1).min(1),
|
|
34
|
+
/** 请求模态。 */
|
|
35
|
+
input: z.array(z.union([z.const('text'), z.const('image')])),
|
|
36
|
+
/** 强制打开或关闭推理能力。 */
|
|
37
|
+
thinking: z.boolean(),
|
|
38
|
+
/** 提供的推理档位:键 = 档位,值 = 协议里的写法。 */
|
|
39
|
+
reasoningEfforts: z.dict(z.union([z.string(), z.const(null)])),
|
|
40
|
+
});
|
|
41
|
+
/** {@link Config} 的运行时 schema。 */
|
|
42
|
+
export const Config = z.object({
|
|
43
|
+
baseUrl: z.string().default(''),
|
|
44
|
+
route: z.string().default('aperture'),
|
|
45
|
+
anthropicRoute: z.string().default('aperture-anthropic'),
|
|
46
|
+
displayName: z.string().default('Aperture'),
|
|
47
|
+
anthropicDisplayName: z.string().default('Aperture (Anthropic)'),
|
|
48
|
+
apiKeyEnv: z.string().default(''),
|
|
49
|
+
placeholderCredential: z.string().default(DEFAULT_PLACEHOLDER_CREDENTIAL),
|
|
50
|
+
headers: z.dict(z.string()).default({}),
|
|
51
|
+
enabledModelIds: z.array(z.string()).default([]),
|
|
52
|
+
modelAliases: z.dict(z.string()).default({}),
|
|
53
|
+
models: z.array(modelConfig).default([]),
|
|
54
|
+
modelMetadataUrl: z.string().default(DEFAULT_MODEL_METADATA_URL),
|
|
55
|
+
defaultContextWindow: z.number().step(1).min(1).default(DEFAULT_CONTEXT_WINDOW),
|
|
56
|
+
images: z.union([z.const('ignore'), z.const('metadata')]).default('ignore'),
|
|
57
|
+
reasoning: z.union([z.const('auto'), z.const('off')]).default('auto'),
|
|
58
|
+
sync: z.boolean().default(true),
|
|
59
|
+
refreshIntervalMinutes: z.number().min(0).max(24 * 60).default(0),
|
|
60
|
+
timeoutMs: z.number().step(1).min(1).default(20_000),
|
|
61
|
+
});
|
|
62
|
+
/**
|
|
63
|
+
* 解析一段配置,只拒绝那些自身就矛盾的错误。
|
|
64
|
+
*
|
|
65
|
+
* `baseUrl` 缺失或不可用**不**算错误:插件照常挂载但处于休眠,这样 profile 可以在
|
|
66
|
+
* 还没人填地址时就先带着这一行——这也正是该设置命名空间能变得可编辑的原因。
|
|
67
|
+
* 路由键则必须拒绝,因为写错的路由键无法靠后续写入补救:插件会静默地什么都不发布。
|
|
68
|
+
*
|
|
69
|
+
* @param config - 解析好的 `aperture` 段。
|
|
70
|
+
* @returns 校验过的配置。
|
|
71
|
+
* @throws Error 当路由键不合文法、或两条路由相同时抛出,并在消息里点名字段。
|
|
72
|
+
*/
|
|
73
|
+
export function resolveConfig(config) {
|
|
74
|
+
const rawBaseUrl = (config.baseUrl ?? '').trim();
|
|
75
|
+
const instanceRoot = normalizeBaseUrl(rawBaseUrl);
|
|
76
|
+
const route = (config.route ?? '').trim();
|
|
77
|
+
const anthropicRoute = (config.anthropicRoute ?? '').trim();
|
|
78
|
+
for (const [field, value] of [
|
|
79
|
+
['route', route],
|
|
80
|
+
['anthropicRoute', anthropicRoute],
|
|
81
|
+
]) {
|
|
82
|
+
if (!ROUTE_PATTERN.test(value)) {
|
|
83
|
+
throw new Error(`${field} "${value}" 必须是小写连字符形式的 provider 路由名(需匹配 ${String(ROUTE_PATTERN)})`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (route === anthropicRoute) {
|
|
87
|
+
throw new Error(`route 与 anthropicRoute 不能相同,两者都是 "${route}"`);
|
|
88
|
+
}
|
|
89
|
+
const models = config.models ?? [];
|
|
90
|
+
const seen = new Set();
|
|
91
|
+
for (const model of models) {
|
|
92
|
+
const id = model.id.trim();
|
|
93
|
+
if (id.length === 0) {
|
|
94
|
+
throw new Error('models[].id 不能为空');
|
|
95
|
+
}
|
|
96
|
+
if (seen.has(id)) {
|
|
97
|
+
throw new Error(`models 里重复列出了 "${id}"`);
|
|
98
|
+
}
|
|
99
|
+
seen.add(id);
|
|
100
|
+
if (model.api !== undefined && model.api !== 'openai-completions' && model.api !== 'anthropic-messages') {
|
|
101
|
+
throw new Error(`models["${id}"].api "${model.api}" 无法服务;请使用 openai-completions 或 anthropic-messages`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const apiKeyEnv = (config.apiKeyEnv ?? '').trim();
|
|
105
|
+
return {
|
|
106
|
+
instanceRoot,
|
|
107
|
+
rawBaseUrl,
|
|
108
|
+
route,
|
|
109
|
+
anthropicRoute,
|
|
110
|
+
displayName: (config.displayName ?? '').trim() || 'Aperture',
|
|
111
|
+
anthropicDisplayName: (config.anthropicDisplayName ?? '').trim() || 'Aperture (Anthropic)',
|
|
112
|
+
apiKeyEnv: apiKeyEnv.length === 0 ? undefined : apiKeyEnv,
|
|
113
|
+
placeholderCredential: config.placeholderCredential ?? DEFAULT_PLACEHOLDER_CREDENTIAL,
|
|
114
|
+
headers: config.headers ?? {},
|
|
115
|
+
enabledModelIds: config.enabledModelIds ?? [],
|
|
116
|
+
modelAliases: config.modelAliases ?? {},
|
|
117
|
+
models,
|
|
118
|
+
modelMetadataUrl: (config.modelMetadataUrl ?? '').trim(),
|
|
119
|
+
defaultContextWindow: config.defaultContextWindow ?? DEFAULT_CONTEXT_WINDOW,
|
|
120
|
+
images: config.images ?? 'ignore',
|
|
121
|
+
reasoning: config.reasoning ?? 'auto',
|
|
122
|
+
sync: config.sync ?? true,
|
|
123
|
+
refreshIntervalMinutes: config.refreshIntervalMinutes ?? 0,
|
|
124
|
+
timeoutMs: config.timeoutMs ?? 20_000,
|
|
125
|
+
};
|
|
126
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-aperture:发现 Aperture 网关所服务的模型,并把它们作为 `llm-pi-ai` 的
|
|
3
|
+
* provider 路由发布给 DeepSeek Harness。
|
|
4
|
+
*
|
|
5
|
+
* Aperture 是 Tailscale 的集中式 LLM 网关:一个端点挡在团队有权使用的所有上游
|
|
6
|
+
* 前面,靠网络身份而不是密钥来认证。harness 本来就会跟这样的网关说话——
|
|
7
|
+
* `dsh-llm-pi-ai` 适配器讲 OpenAI 兼容的 Chat Completions 与 Anthropic Messages,
|
|
8
|
+
* 而这正是 Aperture 暴露的全部——但没有任何东西让模型清单自己刷新:适配器自带的
|
|
9
|
+
* “获取可用模型”动作只接受用户当时还在编辑的一份草稿,而它写在文档里的局限是
|
|
10
|
+
* “一条路由的模型清单永远不会自己刷新”。
|
|
11
|
+
*
|
|
12
|
+
* 本插件就是缺的那一半。它读取 `GET {baseUrl}/v1/models`,逐个模型判断网关实际用
|
|
13
|
+
* 哪种协议服务它,再用网关自己的字段加上 models.dev 定容量、写描述,最后把结果写进
|
|
14
|
+
* `llm-pi-ai` 的 provider 字典——harness 自己把那份文档称作“决定哪些 provider 运行”
|
|
15
|
+
* 的东西。
|
|
16
|
+
*
|
|
17
|
+
* 它不转换任何协议格式。这正是重点。
|
|
18
|
+
*
|
|
19
|
+
* ```yaml
|
|
20
|
+
* - id: aperture
|
|
21
|
+
* name: 'dsh-aperture'
|
|
22
|
+
* config:
|
|
23
|
+
* baseUrl: https://ai.example.ts.net
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @module dsh-aperture
|
|
27
|
+
*/
|
|
28
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
29
|
+
import { type Config } from './config.ts';
|
|
30
|
+
export { fetchModelsListing } from './aperture.ts';
|
|
31
|
+
export type { ModelsListing } from './aperture.ts';
|
|
32
|
+
export { ModelCatalog } from './catalog.ts';
|
|
33
|
+
export { APERTURE_NAMESPACE, DEFAULT_CONTEXT_WINDOW, DEFAULT_MODEL_METADATA_URL, PI_AI_NAMESPACE, resolveConfig, } from './config.ts';
|
|
34
|
+
export type { Config as ApertureConfig, ResolvedConfig } from './config.ts';
|
|
35
|
+
export { DEFAULT_PLACEHOLDER_CREDENTIAL, buildProfilePlan } from './profile.ts';
|
|
36
|
+
export type { ProfilePlan, ProfileOptions, RoutePlan } from './profile.ts';
|
|
37
|
+
export { buildRegistry, classifyProtocol, isDeepSeekFamily } from './registry.ts';
|
|
38
|
+
export type { RegistryResult } from './registry.ts';
|
|
39
|
+
export { applySync, clearRoutes, planSync } from './sync.ts';
|
|
40
|
+
export type { SyncOutcome } from './sync.ts';
|
|
41
|
+
export type { ConfiguredModel, DiscoveredModel, FactSource, Modality, ModelProvenance } from './types.ts';
|
|
42
|
+
export { buildModelsEndpoint, buildRouteBaseUrl, normalizeBaseUrl } from './url.ts';
|
|
43
|
+
export { Config } from './config.ts';
|
|
44
|
+
/** 出现在加载器诊断里的插件名。 */
|
|
45
|
+
export declare const name = "dsh-aperture";
|
|
46
|
+
/**
|
|
47
|
+
* settings 是必需依赖,而不是可选项:把发现的模型清单发布进 `llm-pi-ai` **就是**
|
|
48
|
+
* 本插件的职责,而同一次注册又让插件自己的 `aperture` 段变得可编辑。在这里声明它,
|
|
49
|
+
* 意味着框架会把插件挂在 PENDING 直到 settings 就绪;provider 一旦被替换就卸载插件,
|
|
50
|
+
* 恢复后再重新加载——而不是留下一个已经加载、却无处发布的实例。
|
|
51
|
+
*
|
|
52
|
+
* `commands` 则刻意**不**声明:`/aperture` 这层界面只是顺手提供的便利,没有它的
|
|
53
|
+
* 部署也应该照样获得发现能力。
|
|
54
|
+
*/
|
|
55
|
+
export declare const inject: string[];
|
|
56
|
+
/**
|
|
57
|
+
* 发布网关的模型清单:现在发布,之后有任何变化也发布。
|
|
58
|
+
*
|
|
59
|
+
* @param ctx - 插件上下文,其中 `settings` 已就绪。
|
|
60
|
+
* @param config - 组合层的 `aperture` 段,已按 {@link ConfigSchema} 校验并带上默认值。
|
|
61
|
+
* @throws Error 当配置自身矛盾(路由键不合文法或两条路由重复),或存储的 `aperture`
|
|
62
|
+
* 段非法时抛出——框架的失败路径正是“坏配置要响亮”的实现方式。
|
|
63
|
+
*/
|
|
64
|
+
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-aperture:发现 Aperture 网关所服务的模型,并把它们作为 `llm-pi-ai` 的
|
|
3
|
+
* provider 路由发布给 DeepSeek Harness。
|
|
4
|
+
*
|
|
5
|
+
* Aperture 是 Tailscale 的集中式 LLM 网关:一个端点挡在团队有权使用的所有上游
|
|
6
|
+
* 前面,靠网络身份而不是密钥来认证。harness 本来就会跟这样的网关说话——
|
|
7
|
+
* `dsh-llm-pi-ai` 适配器讲 OpenAI 兼容的 Chat Completions 与 Anthropic Messages,
|
|
8
|
+
* 而这正是 Aperture 暴露的全部——但没有任何东西让模型清单自己刷新:适配器自带的
|
|
9
|
+
* “获取可用模型”动作只接受用户当时还在编辑的一份草稿,而它写在文档里的局限是
|
|
10
|
+
* “一条路由的模型清单永远不会自己刷新”。
|
|
11
|
+
*
|
|
12
|
+
* 本插件就是缺的那一半。它读取 `GET {baseUrl}/v1/models`,逐个模型判断网关实际用
|
|
13
|
+
* 哪种协议服务它,再用网关自己的字段加上 models.dev 定容量、写描述,最后把结果写进
|
|
14
|
+
* `llm-pi-ai` 的 provider 字典——harness 自己把那份文档称作“决定哪些 provider 运行”
|
|
15
|
+
* 的东西。
|
|
16
|
+
*
|
|
17
|
+
* 它不转换任何协议格式。这正是重点。
|
|
18
|
+
*
|
|
19
|
+
* ```yaml
|
|
20
|
+
* - id: aperture
|
|
21
|
+
* name: 'dsh-aperture'
|
|
22
|
+
* config:
|
|
23
|
+
* baseUrl: https://ai.example.ts.net
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @module dsh-aperture
|
|
27
|
+
*/
|
|
28
|
+
import { fetchModelsListing } from "./aperture.js";
|
|
29
|
+
import { ModelCatalog } from "./catalog.js";
|
|
30
|
+
import { registerApertureCommand } from "./command.js";
|
|
31
|
+
import { APERTURE_NAMESPACE, Config as ConfigSchema, resolveConfig } from "./config.js";
|
|
32
|
+
import { buildProfilePlan } from "./profile.js";
|
|
33
|
+
import { buildRegistry, classifyProtocol } from "./registry.js";
|
|
34
|
+
import { ApertureRuntime } from "./runtime.js";
|
|
35
|
+
export { fetchModelsListing } from "./aperture.js";
|
|
36
|
+
export { ModelCatalog } from "./catalog.js";
|
|
37
|
+
export { APERTURE_NAMESPACE, DEFAULT_CONTEXT_WINDOW, DEFAULT_MODEL_METADATA_URL, PI_AI_NAMESPACE, resolveConfig, } from "./config.js";
|
|
38
|
+
export { DEFAULT_PLACEHOLDER_CREDENTIAL, buildProfilePlan } from "./profile.js";
|
|
39
|
+
export { buildRegistry, classifyProtocol, isDeepSeekFamily } from "./registry.js";
|
|
40
|
+
export { applySync, clearRoutes, planSync } from "./sync.js";
|
|
41
|
+
export { buildModelsEndpoint, buildRouteBaseUrl, normalizeBaseUrl } from "./url.js";
|
|
42
|
+
export { Config } from "./config.js";
|
|
43
|
+
/** 出现在加载器诊断里的插件名。 */
|
|
44
|
+
export const name = 'dsh-aperture';
|
|
45
|
+
/**
|
|
46
|
+
* settings 是必需依赖,而不是可选项:把发现的模型清单发布进 `llm-pi-ai` **就是**
|
|
47
|
+
* 本插件的职责,而同一次注册又让插件自己的 `aperture` 段变得可编辑。在这里声明它,
|
|
48
|
+
* 意味着框架会把插件挂在 PENDING 直到 settings 就绪;provider 一旦被替换就卸载插件,
|
|
49
|
+
* 恢复后再重新加载——而不是留下一个已经加载、却无处发布的实例。
|
|
50
|
+
*
|
|
51
|
+
* `commands` 则刻意**不**声明:`/aperture` 这层界面只是顺手提供的便利,没有它的
|
|
52
|
+
* 部署也应该照样获得发现能力。
|
|
53
|
+
*/
|
|
54
|
+
export const inject = ['settings'];
|
|
55
|
+
/**
|
|
56
|
+
* 发布网关的模型清单:现在发布,之后有任何变化也发布。
|
|
57
|
+
*
|
|
58
|
+
* @param ctx - 插件上下文,其中 `settings` 已就绪。
|
|
59
|
+
* @param config - 组合层的 `aperture` 段,已按 {@link ConfigSchema} 校验并带上默认值。
|
|
60
|
+
* @throws Error 当配置自身矛盾(路由键不合文法或两条路由重复),或存储的 `aperture`
|
|
61
|
+
* 段非法时抛出——框架的失败路径正是“坏配置要响亮”的实现方式。
|
|
62
|
+
*/
|
|
63
|
+
export function apply(ctx, config) {
|
|
64
|
+
const logger = ctx.logger;
|
|
65
|
+
const catalog = new ModelCatalog();
|
|
66
|
+
// `installSection` 交出来的是**活引用**(thunk)而不是快照:解析后的配置段每次被
|
|
67
|
+
// 编辑都是就地替换,所以只有持有这个 thunk,后续的配置变更才能抵达本插件。下面
|
|
68
|
+
// 所有读取都因此走它。
|
|
69
|
+
let source = () => config;
|
|
70
|
+
const readConfig = () => resolveConfig(source());
|
|
71
|
+
const runtime = new ApertureRuntime({
|
|
72
|
+
config: readConfig,
|
|
73
|
+
settings: ctx.settings,
|
|
74
|
+
logger,
|
|
75
|
+
catalog,
|
|
76
|
+
});
|
|
77
|
+
// 刷新间隔在每次配置变更时重新计算,而不是只捕获一次,这样部署可以在设置文档里
|
|
78
|
+
// 开关周期刷新,无需重启。
|
|
79
|
+
let timer;
|
|
80
|
+
const armInterval = () => {
|
|
81
|
+
if (timer !== undefined) {
|
|
82
|
+
clearInterval(timer);
|
|
83
|
+
timer = undefined;
|
|
84
|
+
}
|
|
85
|
+
const { refreshIntervalMinutes } = readConfig();
|
|
86
|
+
if (refreshIntervalMinutes > 0) {
|
|
87
|
+
timer = setInterval(() => void runtime.refresh('定时'), refreshIntervalMinutes * 60_000);
|
|
88
|
+
timer.unref?.();
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
ctx.effect(() => {
|
|
92
|
+
armInterval();
|
|
93
|
+
return () => {
|
|
94
|
+
if (timer !== undefined) {
|
|
95
|
+
clearInterval(timer);
|
|
96
|
+
timer = undefined;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}, 'aperture refresh interval');
|
|
100
|
+
const initial = readConfig();
|
|
101
|
+
logger.info('dsh-aperture: %s', initial.instanceRoot === undefined
|
|
102
|
+
? '还没有可用的 baseUrl,发现功能处于休眠'
|
|
103
|
+
: `正在监视 ${initial.instanceRoot} 的模型`);
|
|
104
|
+
// 注册配置段本身也是启动发现的那个动作:`installSection` 在挂载时会先调用
|
|
105
|
+
// `setSource`、再通知一次 `onChange`,顺序如此,所以第一次刷新看到的就是叠加了
|
|
106
|
+
// 用户层的配置——只跑一遍,而不是先按组合层配置跑一遍、再补一遍去对齐。
|
|
107
|
+
ctx.settings.installSection(ctx, APERTURE_NAMESPACE, ConfigSchema, config, {
|
|
108
|
+
setSource(current) {
|
|
109
|
+
source = current;
|
|
110
|
+
},
|
|
111
|
+
onChange() {
|
|
112
|
+
armInterval();
|
|
113
|
+
void runtime.refresh('配置变更');
|
|
114
|
+
},
|
|
115
|
+
validate(value) {
|
|
116
|
+
resolveConfig(value);
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
ctx.inject(['commands'], (commandCtx) => {
|
|
120
|
+
registerApertureCommand(commandCtx, runtime, readConfig);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 提取 Aperture 模型条目对自身所述的事实。
|
|
3
|
+
*
|
|
4
|
+
* 网关的列表是容量的权威来源:它知道每个上游之前的代理会接受什么,并且在上游
|
|
5
|
+
* 更换模型时依然正确。因此每个字段都会列出网关被观察到使用过的所有写法,第一个
|
|
6
|
+
* 成立的答案胜出。
|
|
7
|
+
*
|
|
8
|
+
* 下面的写法是参考 VS Code 扩展清单的超集,再加上本插件在真实 Aperture 实例上
|
|
9
|
+
* 实际观察到的几个(`context_window_tokens`、`max_output_tokens`、`display_name`),
|
|
10
|
+
* 参考的清单早于它们。
|
|
11
|
+
*
|
|
12
|
+
* @module dsh-aperture/metadata/extract
|
|
13
|
+
*/
|
|
14
|
+
import type { Modality } from '../types.ts';
|
|
15
|
+
/** 一个模型条目所述的容量事实。 */
|
|
16
|
+
export interface ExtractedLimits {
|
|
17
|
+
/** 请求与响应合计的最大上下文窗口,条目给出时才有。 */
|
|
18
|
+
contextWindow?: number;
|
|
19
|
+
/** 最大输出 token 数,条目给出时才有。 */
|
|
20
|
+
maxTokens?: number;
|
|
21
|
+
}
|
|
22
|
+
/** 一个模型条目所述的能力事实。 */
|
|
23
|
+
export interface ExtractedCapabilities {
|
|
24
|
+
/** 条目是否声称支持推理强度控制。 */
|
|
25
|
+
reasoning?: boolean;
|
|
26
|
+
/** 条目是否声称支持工具调用。 */
|
|
27
|
+
toolCalling?: boolean;
|
|
28
|
+
/** 条目声称的请求模态。 */
|
|
29
|
+
input?: Modality[];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 读取一个条目所述的容量,可位于顶层,或嵌套在 `metadata`、`limit`、`limits`
|
|
33
|
+
* 之下。
|
|
34
|
+
*
|
|
35
|
+
* @param value - 一条原始模型条目。
|
|
36
|
+
* @returns 所述的容量;两者都未给出时为 `undefined`。
|
|
37
|
+
*/
|
|
38
|
+
export declare function extractLimits(value: unknown): ExtractedLimits | undefined;
|
|
39
|
+
/** 读取条目所述的显示名(`display_name` 是 Aperture 的写法)。 */
|
|
40
|
+
export declare function extractDisplayName(value: unknown): string | undefined;
|
|
41
|
+
/** 读取条目公布的端点路径。 */
|
|
42
|
+
export declare function extractEndpoints(value: unknown): string[];
|
|
43
|
+
/** 读取条目上报的上游 provider 身份。 */
|
|
44
|
+
export declare function extractProvider(value: unknown): {
|
|
45
|
+
id?: string;
|
|
46
|
+
name?: string;
|
|
47
|
+
} | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* 读取一个条目所述的能力事实。
|
|
50
|
+
*
|
|
51
|
+
* `reasoning: true` 是参考扩展与 models.dev 的信号;`thinking` 标志和嵌套的
|
|
52
|
+
* `capabilities` 块则是网关自己的词汇。视觉能力从 `modalities.input` 列表以及
|
|
53
|
+
* `vision` / `supports_vision` 写法读取。
|
|
54
|
+
*
|
|
55
|
+
* @param value - 一条原始模型条目。
|
|
56
|
+
* @returns 所述的能力;未给出任何能力时为 `undefined`。
|
|
57
|
+
*/
|
|
58
|
+
export declare function extractCapabilities(value: unknown): ExtractedCapabilities | undefined;
|
|
59
|
+
/** 一个条目究竟是不是可用的模型行。 */
|
|
60
|
+
export declare function extractModelId(value: unknown): string | undefined;
|
|
61
|
+
/** 读取参考扩展渲染为一行详情文本的价格摘要。 */
|
|
62
|
+
export declare function extractPricingDetail(value: unknown): string | undefined;
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 提取 Aperture 模型条目对自身所述的事实。
|
|
3
|
+
*
|
|
4
|
+
* 网关的列表是容量的权威来源:它知道每个上游之前的代理会接受什么,并且在上游
|
|
5
|
+
* 更换模型时依然正确。因此每个字段都会列出网关被观察到使用过的所有写法,第一个
|
|
6
|
+
* 成立的答案胜出。
|
|
7
|
+
*
|
|
8
|
+
* 下面的写法是参考 VS Code 扩展清单的超集,再加上本插件在真实 Aperture 实例上
|
|
9
|
+
* 实际观察到的几个(`context_window_tokens`、`max_output_tokens`、`display_name`),
|
|
10
|
+
* 参考的清单早于它们。
|
|
11
|
+
*
|
|
12
|
+
* @module dsh-aperture/metadata/extract
|
|
13
|
+
*/
|
|
14
|
+
import { asRecord, firstBoolean, firstPositiveInteger, firstString, stringValue } from "./utils.js";
|
|
15
|
+
/** 每个 Aperture 条目可能用来藏匿其事实的嵌套容器。 */
|
|
16
|
+
function containers(record) {
|
|
17
|
+
const metadata = asRecord(record.metadata);
|
|
18
|
+
return {
|
|
19
|
+
metadata,
|
|
20
|
+
capabilities: asRecord(record.capabilities),
|
|
21
|
+
metadataCapabilities: asRecord(metadata?.capabilities),
|
|
22
|
+
limit: asRecord(record.limit),
|
|
23
|
+
limits: asRecord(record.limits),
|
|
24
|
+
modalities: asRecord(record.modalities),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 读取一个条目所述的容量,可位于顶层,或嵌套在 `metadata`、`limit`、`limits`
|
|
29
|
+
* 之下。
|
|
30
|
+
*
|
|
31
|
+
* @param value - 一条原始模型条目。
|
|
32
|
+
* @returns 所述的容量;两者都未给出时为 `undefined`。
|
|
33
|
+
*/
|
|
34
|
+
export function extractLimits(value) {
|
|
35
|
+
const record = asRecord(value);
|
|
36
|
+
if (!record) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
const { metadata, limit, limits } = containers(record);
|
|
40
|
+
const metadataLimit = asRecord(metadata?.limit);
|
|
41
|
+
const metadataLimits = asRecord(metadata?.limits);
|
|
42
|
+
const contextWindow = firstPositiveInteger([
|
|
43
|
+
// 在真实 Aperture 列表上观察到的。
|
|
44
|
+
record.context_window_tokens,
|
|
45
|
+
record.max_context_tokens,
|
|
46
|
+
// 参考扩展的写法。
|
|
47
|
+
record.maxInputTokens,
|
|
48
|
+
record.max_input_tokens,
|
|
49
|
+
record.input_token_limit,
|
|
50
|
+
limit?.input,
|
|
51
|
+
limits?.input,
|
|
52
|
+
metadata?.maxInputTokens,
|
|
53
|
+
metadata?.max_input_tokens,
|
|
54
|
+
metadata?.input_token_limit,
|
|
55
|
+
metadataLimit?.input,
|
|
56
|
+
metadataLimits?.input,
|
|
57
|
+
record.context_length,
|
|
58
|
+
record.max_context_length,
|
|
59
|
+
record.context_window,
|
|
60
|
+
limit?.context,
|
|
61
|
+
limits?.context,
|
|
62
|
+
metadata?.context_length,
|
|
63
|
+
metadata?.max_context_length,
|
|
64
|
+
metadata?.context_window,
|
|
65
|
+
metadata?.max_context_tokens,
|
|
66
|
+
metadata?.context_window_tokens,
|
|
67
|
+
metadataLimit?.context,
|
|
68
|
+
metadataLimits?.context,
|
|
69
|
+
]);
|
|
70
|
+
const maxTokens = firstPositiveInteger([
|
|
71
|
+
record.max_output_tokens,
|
|
72
|
+
record.maxOutputTokens,
|
|
73
|
+
record.output_token_limit,
|
|
74
|
+
record.max_completion_tokens,
|
|
75
|
+
limit?.output,
|
|
76
|
+
limits?.output,
|
|
77
|
+
metadata?.max_output_tokens,
|
|
78
|
+
metadata?.maxOutputTokens,
|
|
79
|
+
metadata?.output_token_limit,
|
|
80
|
+
metadata?.max_completion_tokens,
|
|
81
|
+
metadataLimit?.output,
|
|
82
|
+
metadataLimits?.output,
|
|
83
|
+
]);
|
|
84
|
+
if (contextWindow === undefined && maxTokens === undefined) {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
...(contextWindow === undefined ? {} : { contextWindow }),
|
|
89
|
+
...(maxTokens === undefined ? {} : { maxTokens }),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/** 读取条目所述的显示名(`display_name` 是 Aperture 的写法)。 */
|
|
93
|
+
export function extractDisplayName(value) {
|
|
94
|
+
const record = asRecord(value);
|
|
95
|
+
if (!record) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
const metadata = asRecord(record.metadata);
|
|
99
|
+
return firstString([record.display_name, record.displayName, record.name, metadata?.display_name, metadata?.name]);
|
|
100
|
+
}
|
|
101
|
+
/** 读取条目公布的端点路径。 */
|
|
102
|
+
export function extractEndpoints(value) {
|
|
103
|
+
const record = asRecord(value);
|
|
104
|
+
const raw = record?.supported_endpoints;
|
|
105
|
+
if (!Array.isArray(raw)) {
|
|
106
|
+
return [];
|
|
107
|
+
}
|
|
108
|
+
return raw.flatMap((entry) => {
|
|
109
|
+
const path = stringValue(entry);
|
|
110
|
+
return path === undefined ? [] : [path];
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/** 读取条目上报的上游 provider 身份。 */
|
|
114
|
+
export function extractProvider(value) {
|
|
115
|
+
const record = asRecord(value);
|
|
116
|
+
if (!record) {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
const metadata = asRecord(record.metadata);
|
|
120
|
+
const provider = asRecord(metadata?.provider);
|
|
121
|
+
const id = firstString([provider?.id, record.owned_by, metadata?.provider_id]);
|
|
122
|
+
const name = firstString([provider?.name, metadata?.provider_name]);
|
|
123
|
+
if (id === undefined && name === undefined) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
return { ...(id === undefined ? {} : { id }), ...(name === undefined ? {} : { name }) };
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* 读取一个条目所述的能力事实。
|
|
130
|
+
*
|
|
131
|
+
* `reasoning: true` 是参考扩展与 models.dev 的信号;`thinking` 标志和嵌套的
|
|
132
|
+
* `capabilities` 块则是网关自己的词汇。视觉能力从 `modalities.input` 列表以及
|
|
133
|
+
* `vision` / `supports_vision` 写法读取。
|
|
134
|
+
*
|
|
135
|
+
* @param value - 一条原始模型条目。
|
|
136
|
+
* @returns 所述的能力;未给出任何能力时为 `undefined`。
|
|
137
|
+
*/
|
|
138
|
+
export function extractCapabilities(value) {
|
|
139
|
+
const record = asRecord(value);
|
|
140
|
+
if (!record) {
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
const { metadata, capabilities, metadataCapabilities, modalities } = containers(record);
|
|
144
|
+
const reasoning = firstBoolean([
|
|
145
|
+
record.reasoning,
|
|
146
|
+
record.thinking,
|
|
147
|
+
capabilities?.reasoning,
|
|
148
|
+
capabilities?.thinking,
|
|
149
|
+
metadata?.reasoning,
|
|
150
|
+
metadata?.thinking,
|
|
151
|
+
metadataCapabilities?.reasoning,
|
|
152
|
+
metadataCapabilities?.thinking,
|
|
153
|
+
]) ?? undefined;
|
|
154
|
+
const toolCalling = firstBoolean([
|
|
155
|
+
record.tool_call,
|
|
156
|
+
record.toolCalling,
|
|
157
|
+
capabilities?.tool_call,
|
|
158
|
+
capabilities?.toolCalling,
|
|
159
|
+
metadata?.tool_call,
|
|
160
|
+
metadata?.toolCalling,
|
|
161
|
+
metadataCapabilities?.tool_call,
|
|
162
|
+
metadataCapabilities?.toolCalling,
|
|
163
|
+
]) ?? undefined;
|
|
164
|
+
const input = extractInput(record, metadata, capabilities, metadataCapabilities, modalities);
|
|
165
|
+
if (reasoning === undefined && toolCalling === undefined && input === undefined) {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
...(reasoning === undefined ? {} : { reasoning }),
|
|
170
|
+
...(toolCalling === undefined ? {} : { toolCalling }),
|
|
171
|
+
...(input === undefined ? {} : { input }),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/** 从两个清单使用的所有写法中读取声明的请求模态。 */
|
|
175
|
+
function extractInput(record, metadata, capabilities, metadataCapabilities, modalities) {
|
|
176
|
+
const lists = [
|
|
177
|
+
record.input,
|
|
178
|
+
record.input_modalities,
|
|
179
|
+
modalities?.input,
|
|
180
|
+
asRecord(metadata?.modalities)?.input,
|
|
181
|
+
metadata?.input,
|
|
182
|
+
metadata?.input_modalities,
|
|
183
|
+
];
|
|
184
|
+
for (const list of lists) {
|
|
185
|
+
if (!Array.isArray(list)) {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
const declared = [];
|
|
189
|
+
for (const entry of list) {
|
|
190
|
+
const label = stringValue(entry)?.toLowerCase();
|
|
191
|
+
if (label === 'text' || label === 'image' || label === 'vision') {
|
|
192
|
+
declared.push(label === 'text' ? 'text' : 'image');
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (declared.length > 0) {
|
|
196
|
+
return [...new Set(declared)];
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const vision = firstBoolean([
|
|
200
|
+
record.vision,
|
|
201
|
+
record.supports_vision,
|
|
202
|
+
capabilities?.vision,
|
|
203
|
+
capabilities?.image,
|
|
204
|
+
metadata?.vision,
|
|
205
|
+
metadata?.supports_vision,
|
|
206
|
+
metadataCapabilities?.vision,
|
|
207
|
+
metadataCapabilities?.image,
|
|
208
|
+
]);
|
|
209
|
+
if (vision === true) {
|
|
210
|
+
return ['text', 'image'];
|
|
211
|
+
}
|
|
212
|
+
if (vision === false) {
|
|
213
|
+
return ['text'];
|
|
214
|
+
}
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
/** 一个条目究竟是不是可用的模型行。 */
|
|
218
|
+
export function extractModelId(value) {
|
|
219
|
+
const record = asRecord(value);
|
|
220
|
+
return record === undefined ? undefined : stringValue(record.id);
|
|
221
|
+
}
|
|
222
|
+
/** 读取参考扩展渲染为一行详情文本的价格摘要。 */
|
|
223
|
+
export function extractPricingDetail(value) {
|
|
224
|
+
const record = asRecord(value);
|
|
225
|
+
const pricing = asRecord(record?.pricing);
|
|
226
|
+
if (!pricing) {
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
const input = stringValue(pricing.input);
|
|
230
|
+
const output = stringValue(pricing.output);
|
|
231
|
+
if (!input && !output) {
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
return `in ${input ?? '?'} / out ${output ?? '?'}`;
|
|
235
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* models.dev 清单的索引与查找。
|
|
3
|
+
*
|
|
4
|
+
* Aperture 会上报容量,但并不总是上报能力,而且在大多数 provider 上,其列表
|
|
5
|
+
* 根本没有「这个模型是否会推理」的字段。models.dev 为同样的模型携带 `reasoning`、
|
|
6
|
+
* `tool_call` 和输入模态,但 id 只是有时逐字节相同:网关会重命名上游(这里是
|
|
7
|
+
* `x-ai/grok-4.5`,那里是 `xai/grok-4.5`),而其别名完全不带厂商前缀(`k3`)。
|
|
8
|
+
*
|
|
9
|
+
* 因此查找是评分式的,而非精确匹配。模型 id 按完整写法、按最后一个斜杠之后的
|
|
10
|
+
* 分段、以及两者折叠标点后的 slug 来匹配;一个清单条目贡献同样的三个键;而两侧
|
|
11
|
+
* 匹配上的 provider 别名优先于任何键得分,因为跨厂商的重名正是孤立的模型 id
|
|
12
|
+
* 唯一会说谎的地方。
|
|
13
|
+
*
|
|
14
|
+
* @module dsh-aperture/metadata/modelsdev
|
|
15
|
+
*/
|
|
16
|
+
import type { CatalogLookup, CatalogMetadata } from '../types.ts';
|
|
17
|
+
/** 一个已索引的清单条目。 */
|
|
18
|
+
interface CatalogEntry {
|
|
19
|
+
readonly modelId: string;
|
|
20
|
+
readonly modelName?: string;
|
|
21
|
+
readonly providerAliases: readonly string[];
|
|
22
|
+
readonly metadata: CatalogMetadata;
|
|
23
|
+
}
|
|
24
|
+
/** 已索引的 models.dev 清单,按单个模型的 id 与 provider 提示查询。 */
|
|
25
|
+
export declare class ModelCatalogIndex {
|
|
26
|
+
private readonly byKey;
|
|
27
|
+
private readonly entries;
|
|
28
|
+
/**
|
|
29
|
+
* @param entries - 每一个可用的清单条目。
|
|
30
|
+
*/
|
|
31
|
+
constructor(entries: readonly CatalogEntry[]);
|
|
32
|
+
/** 该索引持有的可用条目数量。 */
|
|
33
|
+
get size(): number;
|
|
34
|
+
/**
|
|
35
|
+
* 为一个网关模型解析出最佳清单条目。
|
|
36
|
+
* @param modelId - 网关公布的 id。
|
|
37
|
+
* @param providerHints - 网关上报的上游 provider id 与名称。
|
|
38
|
+
* @returns 胜出条目的元数据;无任何匹配时为 `undefined`。
|
|
39
|
+
*/
|
|
40
|
+
lookup(modelId: string, providerHints?: readonly string[]): CatalogMetadata | undefined;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 在一份已解析的 models.dev 文档之上构建查找。
|
|
44
|
+
*
|
|
45
|
+
* 接受该文档发布过的所有形态:当前扁平的 `{ "<provider>/<model>": { … } }` 映射、
|
|
46
|
+
* 值携带 `models` 的旧式 provider 键映射,以及在 `providers` 或 `models` 之下
|
|
47
|
+
* 暴露上述任一形态的包装对象。
|
|
48
|
+
*
|
|
49
|
+
* @param document - 已解析的 JSON,或任何其他值。
|
|
50
|
+
* @returns 该查找;文档不含可用条目时为 `undefined`。
|
|
51
|
+
*/
|
|
52
|
+
export declare function buildCatalogLookup(document: unknown): CatalogLookup | undefined;
|
|
53
|
+
/** 仅构建索引,并暴露其大小以用于诊断。 */
|
|
54
|
+
export declare function buildCatalogIndex(document: unknown): ModelCatalogIndex | undefined;
|
|
55
|
+
export {};
|