dsh-aperture 0.1.0 → 0.3.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 -21
- package/README.en.md +148 -227
- package/README.md +136 -213
- package/client/aperture.js +1566 -0
- package/cordis.patch.yml +33 -43
- package/icon.svg +125 -0
- package/lib/aperture.d.ts +6 -8
- package/lib/aperture.js +6 -8
- package/lib/catalog.d.ts +0 -2
- package/lib/catalog.js +0 -4
- package/lib/config.d.ts +106 -89
- package/lib/config.js +79 -37
- package/lib/index.d.ts +31 -25
- package/lib/index.js +55 -48
- package/lib/metadata/extract.d.ts +7 -13
- package/lib/metadata/extract.js +7 -25
- package/lib/metadata/modelsdev.d.ts +9 -11
- package/lib/metadata/modelsdev.js +21 -23
- package/lib/metadata/utils.d.ts +0 -2
- package/lib/metadata/utils.js +0 -14
- package/lib/panel.d.ts +86 -0
- package/lib/panel.js +263 -0
- package/lib/profile.d.ts +5 -13
- package/lib/profile.js +25 -28
- package/lib/registry.d.ts +11 -20
- package/lib/registry.js +23 -35
- package/lib/remote.d.ts +64 -0
- package/lib/remote.js +96 -0
- package/lib/report.d.ts +116 -9
- package/lib/report.js +97 -77
- package/lib/runtime.d.ts +18 -17
- package/lib/runtime.js +41 -50
- package/lib/sync.d.ts +12 -25
- package/lib/sync.js +41 -55
- package/lib/types.d.ts +4 -6
- package/lib/types.js +3 -3
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +34 -15
- package/lib/command.d.ts +0 -21
- package/lib/command.js +0 -58
- package/lib/namespaces.d.ts +0 -12
- package/lib/namespaces.js +0 -12
package/lib/remote.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 配置页与宿主之间的 Typert Remote 面:`aperturePanel` 命名空间。
|
|
3
|
+
*
|
|
4
|
+
* 描述符是手工登记的,因为生成它们的 Typert 生成器并不随 DSH 发布,而这一步的规范本身很小:
|
|
5
|
+
* 宿主半边用 `src-json` 编解码器,浏览器半边带 strict 校验,两端共享同一组端点名;参数按描述符
|
|
6
|
+
* 里的顺序位置传入宿主方法,因此两边的名字与顺序必须一致。
|
|
7
|
+
*
|
|
8
|
+
* 端点 id 的 `#` 之前是包名,之后是 `<命名空间>/<方法>`。
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-aperture/remote
|
|
11
|
+
*/
|
|
12
|
+
import { TypertRemoteService } from '@deepseek-ai/dsh-typert-protocol';
|
|
13
|
+
/** 包名,同时是端点 id 的前缀。 */
|
|
14
|
+
export const PANEL_PACKAGE = 'dsh-aperture';
|
|
15
|
+
/** 线上命名空间,也是浏览器半边注入的服务名(`remote.aperturePanel`)。 */
|
|
16
|
+
export const PANEL_NAMESPACE = 'aperturePanel';
|
|
17
|
+
/**
|
|
18
|
+
* 一个端点的宿主侧调用描述符。
|
|
19
|
+
*
|
|
20
|
+
* @param method - 端点方法名,同时也是宿主服务上的方法名。
|
|
21
|
+
* @param parameters - 参数名,按宿主方法的形参顺序;省略即无参数。
|
|
22
|
+
* @returns 一个结果为 JSON 的直调描述符。
|
|
23
|
+
*/
|
|
24
|
+
function panelDescriptor(method, parameters = []) {
|
|
25
|
+
return {
|
|
26
|
+
id: `${PANEL_PACKAGE}#${PANEL_NAMESPACE}/${method}`,
|
|
27
|
+
service: PANEL_NAMESPACE,
|
|
28
|
+
namespace: PANEL_NAMESPACE,
|
|
29
|
+
method,
|
|
30
|
+
invocation: { kind: 'direct' },
|
|
31
|
+
parameters: parameters.map((name) => ({ name, wire: name, source: 'json', codec: { mode: 'src-json' } })),
|
|
32
|
+
result: { mode: 'src-json' },
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 本插件暴露给浏览器的全部端点。
|
|
37
|
+
*
|
|
38
|
+
* 端点名有一处不显眼的约束:api-gateway 在客户端为每个命名空间建一个 `RemoteNamespaceService`,
|
|
39
|
+
* 端点会成为它的属性,因此与它自己的成员重名会被 `validateContribution` 拒绝,**整份贡献一起
|
|
40
|
+
* 撤回**(浏览器里只剩一行 console.error,界面安静地什么都不出现)。预置名有 `ctx` / `empty` /
|
|
41
|
+
* `invokeRemote` / `methods` / `name` / `namespace` 与 `has` / `install` / `installDirect` /
|
|
42
|
+
* `installScoped` / `assertMethodAvailable` / `remove`——新端点起名时先对一遍这份名单。
|
|
43
|
+
*/
|
|
44
|
+
export const PANEL_INVOCATIONS = [
|
|
45
|
+
panelDescriptor('status'),
|
|
46
|
+
panelDescriptor('refresh'),
|
|
47
|
+
panelDescriptor('edit', ['id', 'patch']),
|
|
48
|
+
];
|
|
49
|
+
/**
|
|
50
|
+
* 登记进 Typert 注册表的贡献。
|
|
51
|
+
*
|
|
52
|
+
* `schemas`/`model` 是生成器的产物,这里没有生成器,因此它们如实为空:本插件不导出可复用的
|
|
53
|
+
* schema,也没有需要反射的业务类型。端点本身仍然完整。
|
|
54
|
+
*/
|
|
55
|
+
export const PANEL_CONTRIBUTION = {
|
|
56
|
+
package: PANEL_PACKAGE,
|
|
57
|
+
face: 'host',
|
|
58
|
+
schemas: [],
|
|
59
|
+
invocations: PANEL_INVOCATIONS,
|
|
60
|
+
model: { services: [], events: [], objects: [] },
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* `aperturePanel` 宿主服务:方法与描述符一一对应,实现全部委托给 {@link PanelOps}。
|
|
64
|
+
*
|
|
65
|
+
* 方法签名必须与描述符的参数顺序一致——`edit` 的两个形参名字与描述符里的两个参数同名,
|
|
66
|
+
* 顺序也相同,网关就是按描述符顺序把 wire 参数位置传入的。
|
|
67
|
+
*/
|
|
68
|
+
export class AperturePanelService extends TypertRemoteService {
|
|
69
|
+
ops;
|
|
70
|
+
/**
|
|
71
|
+
* @param ctx - 挂载上下文。
|
|
72
|
+
* @param ops - 端点实现。
|
|
73
|
+
*/
|
|
74
|
+
constructor(ctx, ops) {
|
|
75
|
+
super(ctx, PANEL_NAMESPACE);
|
|
76
|
+
this.ops = ops;
|
|
77
|
+
}
|
|
78
|
+
/** 最近一次刷新的报告。 */
|
|
79
|
+
status() {
|
|
80
|
+
return this.ops.status();
|
|
81
|
+
}
|
|
82
|
+
/** 立刻重新发现并发布。 */
|
|
83
|
+
refresh() {
|
|
84
|
+
return this.ops.refresh();
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 写入一个模型的参数。
|
|
88
|
+
*
|
|
89
|
+
* @param id - 模型 id。
|
|
90
|
+
* @param patch - 要改的字段;`null` 表示撤销这个模型的全部覆盖。
|
|
91
|
+
* @returns 成败与一句人话。
|
|
92
|
+
*/
|
|
93
|
+
edit(id, patch) {
|
|
94
|
+
return this.ops.edit(id, patch);
|
|
95
|
+
}
|
|
96
|
+
}
|
package/lib/report.d.ts
CHANGED
|
@@ -1,16 +1,123 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 报告:配置页的状态段与模型清单。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* 这里是**结构化数据**,不是拼好的句子:配置页是双语的,把中文句子拼在宿主半边就等于让英文
|
|
5
|
+
* 界面显示中文,而「哪个模型属于哪条路由」「这条事实是谁给的」本来就是数据。宿主只把发现过程
|
|
6
|
+
* 决定了什么摆齐——哪个端点应答了、哪些模型进了哪条路由、哪个模型没有端点能服务、每条事实来自
|
|
7
|
+
* 哪里、哪些字段是用户写下的覆盖——措辞与排版交给界面。
|
|
8
8
|
*
|
|
9
9
|
* @module dsh-aperture/report
|
|
10
10
|
*/
|
|
11
11
|
import type { ResolvedConfig } from './config.ts';
|
|
12
12
|
import type { RefreshOutcome } from './runtime.ts';
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
import type { FactSource, Modality } from './types.ts';
|
|
14
|
+
/** 报告里的一个模型。 */
|
|
15
|
+
export interface PanelModel {
|
|
16
|
+
/** Aperture 接受的模型 id。 */
|
|
17
|
+
readonly id: string;
|
|
18
|
+
/** 显示名(可能来自清单或覆盖)。 */
|
|
19
|
+
readonly name: string;
|
|
20
|
+
/** 承载它的路由键;没有任何路由能服务时缺失。 */
|
|
21
|
+
readonly route?: string;
|
|
22
|
+
/** 网关通告的协议;未通告时缺失。 */
|
|
23
|
+
readonly protocol?: string;
|
|
24
|
+
/** 网关为该模型通告的每一个端点,用于诊断。 */
|
|
25
|
+
readonly endpoints: readonly string[];
|
|
26
|
+
/** 生效的上下文容量,以 token 计。 */
|
|
27
|
+
readonly contextWindow?: number;
|
|
28
|
+
/** 生效的最大输出,以 token 计。 */
|
|
29
|
+
readonly maxTokens?: number;
|
|
30
|
+
/** 生效的请求模态。 */
|
|
31
|
+
readonly input: readonly Modality[];
|
|
32
|
+
/** 生效的推理能力。 */
|
|
33
|
+
readonly reasoning: boolean;
|
|
34
|
+
/** 每项事实的来源;界面按语言渲染它。 */
|
|
35
|
+
readonly provenance: {
|
|
36
|
+
readonly limits: FactSource;
|
|
37
|
+
readonly reasoning: FactSource;
|
|
38
|
+
readonly input: FactSource;
|
|
39
|
+
readonly name: FactSource;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* 用户层里这一行自己写下的键(含界面不编辑的,例如 `reasoningEfforts`;别名写成 `alias`)。
|
|
43
|
+
*
|
|
44
|
+
* 这是「覆盖」的唯一判据,且只能从用户层读——官方也是这么定义的:看字段在不在用户层里,
|
|
45
|
+
* 而不是拿值与默认值比。解析后的配置用不得:schema 会把没写的数组字段补成 `[]`,界面就会
|
|
46
|
+
* 在一行什么都没写过的模型上挂一颗永远撤不掉的「已覆盖」。
|
|
47
|
+
*/
|
|
48
|
+
readonly overrideKeys?: readonly string[];
|
|
49
|
+
/** 用户写下的清单别名(`aperture.modelAliases` 里对应那一项)。 */
|
|
50
|
+
readonly alias?: string;
|
|
51
|
+
}
|
|
52
|
+
/** 报告里的一条已发布路由。 */
|
|
53
|
+
export interface PanelRoute {
|
|
54
|
+
/** provider 路由键。 */
|
|
55
|
+
readonly provider: string;
|
|
56
|
+
/** 服务它的协议;本插件自己拼的方案里一定有,类型上仍是可选(`PiAiProviderProfile`)。 */
|
|
57
|
+
readonly api?: string;
|
|
58
|
+
/** 路由的 baseURL,同上。 */
|
|
59
|
+
readonly baseURL?: string;
|
|
60
|
+
/** 该路由承载的模型数。 */
|
|
61
|
+
readonly models: number;
|
|
62
|
+
}
|
|
63
|
+
/** 最近一次刷新做了什么。 */
|
|
64
|
+
export interface PanelRefresh {
|
|
65
|
+
/** 触发来源。 */
|
|
66
|
+
readonly trigger: string;
|
|
67
|
+
/** ISO 时间戳;界面按本地时区渲染。 */
|
|
68
|
+
readonly at: string;
|
|
69
|
+
/** 耗时。 */
|
|
70
|
+
readonly durationMs: number;
|
|
71
|
+
/** 发现与发布是否都成功。 */
|
|
72
|
+
readonly ok: boolean;
|
|
73
|
+
/** 失败原因。 */
|
|
74
|
+
readonly error?: string;
|
|
75
|
+
/** 清单提供了什么。 */
|
|
76
|
+
readonly catalog: {
|
|
77
|
+
/** 清单是否可用。 */
|
|
78
|
+
readonly available: boolean;
|
|
79
|
+
/** 条目数。 */
|
|
80
|
+
readonly entries: number;
|
|
81
|
+
/** 清单不可用时的原因。 */
|
|
82
|
+
readonly reason?: string;
|
|
83
|
+
};
|
|
84
|
+
/** 应答的端点与它列出的行数。 */
|
|
85
|
+
readonly endpoint?: {
|
|
86
|
+
readonly url: string;
|
|
87
|
+
readonly listed: number;
|
|
88
|
+
};
|
|
89
|
+
/** 设置写入的结果。 */
|
|
90
|
+
readonly sync?: {
|
|
91
|
+
readonly applied: boolean;
|
|
92
|
+
readonly ops: number;
|
|
93
|
+
readonly routes: readonly string[];
|
|
94
|
+
readonly reason?: string;
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/** 配置页要显示的整份报告。 */
|
|
98
|
+
export interface PanelReport {
|
|
99
|
+
/** 实例地址(已归一化);没有配置时为空串,措辞交给界面。 */
|
|
100
|
+
readonly place: string;
|
|
101
|
+
/** 最近一次刷新;还一次都没跑过时为缺失。 */
|
|
102
|
+
readonly refresh?: PanelRefresh;
|
|
103
|
+
/** 已发布的路由。 */
|
|
104
|
+
readonly routes: readonly PanelRoute[];
|
|
105
|
+
/** 逐模型清单:先是各条路由承载的模型,最后是没有任何路由的模型。 */
|
|
106
|
+
readonly models: readonly PanelModel[];
|
|
107
|
+
}
|
|
108
|
+
/** 用户层里写下的东西:报告要照着它说「哪些是这一行自己写的」。 */
|
|
109
|
+
export interface DeclaredOverrides {
|
|
110
|
+
/** 用户层 `aperture.models` 的原样条目。 */
|
|
111
|
+
readonly models?: readonly Record<string, unknown>[];
|
|
112
|
+
/** 用户层 `aperture.modelAliases` 里写过的模型 id。 */
|
|
113
|
+
readonly aliasIds?: readonly string[];
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 组装报告。
|
|
117
|
+
*
|
|
118
|
+
* @param outcome - 最近一次刷新;一次都没完成过时传 `undefined`。
|
|
119
|
+
* @param config - 当前生效配置。
|
|
120
|
+
* @param declared - 用户层里写下的覆盖;缺省即「什么都没写过」。
|
|
121
|
+
* @returns 报告。
|
|
122
|
+
*/
|
|
123
|
+
export declare function buildReport(outcome: RefreshOutcome | undefined, config: ResolvedConfig, declared?: DeclaredOverrides): PanelReport;
|
package/lib/report.js
CHANGED
|
@@ -1,89 +1,109 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 报告:配置页的状态段与模型清单。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* 这里是**结构化数据**,不是拼好的句子:配置页是双语的,把中文句子拼在宿主半边就等于让英文
|
|
5
|
+
* 界面显示中文,而「哪个模型属于哪条路由」「这条事实是谁给的」本来就是数据。宿主只把发现过程
|
|
6
|
+
* 决定了什么摆齐——哪个端点应答了、哪些模型进了哪条路由、哪个模型没有端点能服务、每条事实来自
|
|
7
|
+
* 哪里、哪些字段是用户写下的覆盖——措辞与排版交给界面。
|
|
8
8
|
*
|
|
9
9
|
* @module dsh-aperture/report
|
|
10
10
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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';
|
|
11
|
+
/**
|
|
12
|
+
* 组装报告。
|
|
13
|
+
*
|
|
14
|
+
* @param outcome - 最近一次刷新;一次都没完成过时传 `undefined`。
|
|
15
|
+
* @param config - 当前生效配置。
|
|
16
|
+
* @param declared - 用户层里写下的覆盖;缺省即「什么都没写过」。
|
|
17
|
+
* @returns 报告。
|
|
18
|
+
*/
|
|
19
|
+
export function buildReport(outcome, config, declared = {}) {
|
|
20
|
+
const keys = new Map();
|
|
21
|
+
for (const entry of declared.models ?? []) {
|
|
22
|
+
const id = typeof entry.id === 'string' ? entry.id.trim() : '';
|
|
23
|
+
if (id.length === 0)
|
|
24
|
+
continue;
|
|
25
|
+
keys.set(id, Object.keys(entry).filter((key) => key !== 'id'));
|
|
55
26
|
}
|
|
56
|
-
|
|
57
|
-
|
|
27
|
+
for (const id of declared.aliasIds ?? []) {
|
|
28
|
+
// 别名是另一张表,但对界面来说它是这一行的覆盖之一:撤的时候一起撤。
|
|
29
|
+
keys.set(id, [...(keys.get(id) ?? []), 'alias']);
|
|
58
30
|
}
|
|
59
|
-
const
|
|
31
|
+
const routes = (outcome?.routes ?? []).map((route) => ({
|
|
32
|
+
provider: route.provider,
|
|
33
|
+
...(route.profile.api === undefined ? {} : { api: route.profile.api }),
|
|
34
|
+
...(route.profile.baseURL === undefined ? {} : { baseURL: route.profile.baseURL }),
|
|
35
|
+
models: route.models.length,
|
|
36
|
+
}));
|
|
37
|
+
const models = [];
|
|
60
38
|
const routed = new Set();
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
39
|
+
const describe = (model, route) => {
|
|
40
|
+
routed.add(model.id);
|
|
41
|
+
const declaredKeys = keys.get(model.id) ?? [];
|
|
42
|
+
const alias = config.modelAliases[model.id];
|
|
43
|
+
return {
|
|
44
|
+
id: model.id,
|
|
45
|
+
name: model.name,
|
|
46
|
+
...(route === undefined ? {} : { route }),
|
|
47
|
+
...(model.protocol === undefined ? {} : { protocol: model.protocol }),
|
|
48
|
+
endpoints: model.endpoints,
|
|
49
|
+
...(model.contextWindow === undefined ? {} : { contextWindow: model.contextWindow }),
|
|
50
|
+
...(model.maxTokens === undefined ? {} : { maxTokens: model.maxTokens }),
|
|
51
|
+
input: model.input,
|
|
52
|
+
reasoning: model.reasoning,
|
|
53
|
+
provenance: model.provenance,
|
|
54
|
+
...(declaredKeys.length === 0 ? {} : { overrideKeys: declaredKeys }),
|
|
55
|
+
...(alias === undefined ? {} : { alias }),
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
for (const route of outcome?.routes ?? []) {
|
|
59
|
+
for (const model of route.models)
|
|
60
|
+
models.push(describe(model, route.provider));
|
|
67
61
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const endpoints = model.endpoints.length === 0 ? '未通告任何端点' : model.endpoints.join(', ');
|
|
73
|
-
lines.push(` ${model.id} — ${endpoints}`);
|
|
74
|
-
}
|
|
62
|
+
// 没有任何路由能服务的模型跟在后面:它们没有协议可用,或者被配置挡掉了。
|
|
63
|
+
for (const model of outcome?.models ?? []) {
|
|
64
|
+
if (!routed.has(model.id))
|
|
65
|
+
models.push(describe(model));
|
|
75
66
|
}
|
|
76
|
-
return
|
|
67
|
+
return {
|
|
68
|
+
place: config.instanceRoot ?? config.rawBaseUrl,
|
|
69
|
+
...(outcome === undefined ? {} : { refresh: refresh(outcome) }),
|
|
70
|
+
routes,
|
|
71
|
+
models,
|
|
72
|
+
};
|
|
77
73
|
}
|
|
78
|
-
/**
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
74
|
+
/**
|
|
75
|
+
* 把一次刷新收成报告要显示的字段。
|
|
76
|
+
*
|
|
77
|
+
* @param outcome - 已完成的刷新。
|
|
78
|
+
* @returns 状态段的数据。
|
|
79
|
+
*/
|
|
80
|
+
function refresh(outcome) {
|
|
81
|
+
const catalog = outcome.catalog;
|
|
82
|
+
const sync = outcome.sync;
|
|
83
|
+
return {
|
|
84
|
+
trigger: outcome.trigger,
|
|
85
|
+
at: outcome.at.toISOString(),
|
|
86
|
+
durationMs: outcome.durationMs,
|
|
87
|
+
ok: outcome.ok,
|
|
88
|
+
...(outcome.error === undefined ? {} : { error: outcome.error }),
|
|
89
|
+
catalog: {
|
|
90
|
+
// `lookup` 在清单不可用时缺失;报告只报可用性,不报这个函数本身。
|
|
91
|
+
available: catalog.lookup !== undefined,
|
|
92
|
+
entries: catalog.entries,
|
|
93
|
+
...(catalog.reason === undefined ? {} : { reason: catalog.reason }),
|
|
94
|
+
},
|
|
95
|
+
...(outcome.endpoint === undefined
|
|
96
|
+
? {}
|
|
97
|
+
: { endpoint: { url: outcome.endpoint, listed: outcome.listed } }),
|
|
98
|
+
...(sync === undefined
|
|
99
|
+
? {}
|
|
100
|
+
: {
|
|
101
|
+
sync: {
|
|
102
|
+
applied: sync.applied,
|
|
103
|
+
ops: sync.ops,
|
|
104
|
+
routes: sync.routes,
|
|
105
|
+
...(sync.reason === undefined ? {} : { reason: sync.reason }),
|
|
106
|
+
},
|
|
107
|
+
}),
|
|
108
|
+
};
|
|
89
109
|
}
|
package/lib/runtime.d.ts
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 发现运行时:同一时刻只跑一次刷新,只记住一个结果。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* 刷新有四个来路(插件加载、设置变更、刷新间隔、配置页),其中两条容易重叠——本插件写入的
|
|
5
|
+
* 设置变更会唤醒触发这次写入的同一个 watcher。因此刷新是单飞的:途中到达的请求若正在跑的那
|
|
6
|
+
* 一轮读的就是此刻这份配置就并进它,否则排在它后面(见 {@link ApertureRuntime.refresh});
|
|
7
|
+
* 排队者共享同一轮,所以再多的调用方也只多跑一轮。
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
* 在设置文档里,而真正服务请求的正是这份文档。
|
|
9
|
+
* 记住的结果只是设置文档里那份的影子:配置页报告它,真正服务请求的是文档里已生效的那份。
|
|
11
10
|
*
|
|
12
11
|
* @module dsh-aperture/runtime
|
|
13
12
|
*/
|
|
14
|
-
import type {
|
|
13
|
+
import type { SettingsForms } from '@deepseek-ai/dsh-settings';
|
|
15
14
|
import { ModelCatalog, type CatalogLoad } from './catalog.ts';
|
|
16
15
|
import { type ResolvedConfig } from './config.ts';
|
|
17
16
|
import { type RoutePlan } from './profile.ts';
|
|
@@ -56,7 +55,7 @@ export interface RuntimeDeps {
|
|
|
56
55
|
/** 当前生效的配置,每次刷新都会重新读取。 */
|
|
57
56
|
readonly config: () => ResolvedConfig;
|
|
58
57
|
/** 设置服务;插件把它声明为必需注入。 */
|
|
59
|
-
readonly settings:
|
|
58
|
+
readonly settings: SettingsForms;
|
|
60
59
|
/** 具名 logger。 */
|
|
61
60
|
readonly logger: RuntimeLogger;
|
|
62
61
|
/** models.dev 缓存,在各次刷新之间共享。 */
|
|
@@ -66,7 +65,8 @@ export interface RuntimeDeps {
|
|
|
66
65
|
export declare class ApertureRuntime {
|
|
67
66
|
private readonly deps;
|
|
68
67
|
private running;
|
|
69
|
-
|
|
68
|
+
/** 正在跑的那一轮读的是哪一份配置;用来判断排队者该并进它还是排到它后面。 */
|
|
69
|
+
private runningConfig;
|
|
70
70
|
private latest;
|
|
71
71
|
/**
|
|
72
72
|
* @param deps - 宿主服务与配置活引用(thunk)。
|
|
@@ -75,16 +75,17 @@ export declare class ApertureRuntime {
|
|
|
75
75
|
/** 最近一次已完成的刷新(如果完成过)。 */
|
|
76
76
|
last(): RefreshOutcome | undefined;
|
|
77
77
|
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
78
|
+
* 刷新;已有刷新在运行时,等它收尾之后再决定是并进新的一轮,还是自己起一轮。
|
|
79
|
+
*
|
|
80
|
+
* 承诺是「返回的那一轮读的是**此刻**的配置」:正在跑的那一轮若读的就是此刻这份配置,
|
|
81
|
+
* 调用方并进它;否则等它收尾再重问一次。设置界面刚写完配置就来重读报告时,等的必须是
|
|
82
|
+
* 读过新配置的那一轮——等正在跑的那一轮回来等于拿回过期报告,界面于是「保存了却没变」。
|
|
83
|
+
* 配置身份可以直接比:设置服务每次提交都换一份深冻结的解析结果,没变就还是同一个对象。
|
|
84
|
+
*
|
|
85
|
+
* @param trigger - 触发来源;出现在配置页的状态段里。
|
|
86
|
+
* @returns 本次调用所参与的那一轮刷新的结果。
|
|
81
87
|
*/
|
|
82
88
|
refresh(trigger: string): Promise<RefreshOutcome>;
|
|
83
|
-
/**
|
|
84
|
-
* 从设置文档中撤出本插件的路由。
|
|
85
|
-
* @returns 供命令界面展示的可读结果。
|
|
86
|
-
*/
|
|
87
|
-
remove(): Promise<string>;
|
|
88
89
|
/** 执行一次刷新,绝不抛出异常。 */
|
|
89
90
|
private run;
|
|
90
91
|
/** 一次刷新的主体。 */
|
package/lib/runtime.js
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 发现运行时:同一时刻只跑一次刷新,只记住一个结果。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* 刷新有四个来路(插件加载、设置变更、刷新间隔、配置页),其中两条容易重叠——本插件写入的
|
|
5
|
+
* 设置变更会唤醒触发这次写入的同一个 watcher。因此刷新是单飞的:途中到达的请求若正在跑的那
|
|
6
|
+
* 一轮读的就是此刻这份配置就并进它,否则排在它后面(见 {@link ApertureRuntime.refresh});
|
|
7
|
+
* 排队者共享同一轮,所以再多的调用方也只多跑一轮。
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
* 在设置文档里,而真正服务请求的正是这份文档。
|
|
9
|
+
* 记住的结果只是设置文档里那份的影子:配置页报告它,真正服务请求的是文档里已生效的那份。
|
|
11
10
|
*
|
|
12
11
|
* @module dsh-aperture/runtime
|
|
13
12
|
*/
|
|
14
13
|
import { fetchModelsListing } from "./aperture.js";
|
|
15
14
|
import { ModelCatalog } from "./catalog.js";
|
|
16
|
-
import {
|
|
15
|
+
import { DEFAULT_TIMEOUT_MS } from "./config.js";
|
|
17
16
|
import { buildProfilePlan } from "./profile.js";
|
|
18
17
|
import { buildRegistry } from "./registry.js";
|
|
19
|
-
import { applySync
|
|
18
|
+
import { applySync } from "./sync.js";
|
|
20
19
|
/** 单飞(single-flight)的发现与发布。 */
|
|
21
20
|
export class ApertureRuntime {
|
|
22
21
|
deps;
|
|
23
22
|
running;
|
|
24
|
-
|
|
23
|
+
/** 正在跑的那一轮读的是哪一份配置;用来判断排队者该并进它还是排到它后面。 */
|
|
24
|
+
runningConfig;
|
|
25
25
|
latest;
|
|
26
26
|
/**
|
|
27
27
|
* @param deps - 宿主服务与配置活引用(thunk)。
|
|
@@ -34,51 +34,41 @@ export class ApertureRuntime {
|
|
|
34
34
|
return this.latest;
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
37
|
+
* 刷新;已有刷新在运行时,等它收尾之后再决定是并进新的一轮,还是自己起一轮。
|
|
38
|
+
*
|
|
39
|
+
* 承诺是「返回的那一轮读的是**此刻**的配置」:正在跑的那一轮若读的就是此刻这份配置,
|
|
40
|
+
* 调用方并进它;否则等它收尾再重问一次。设置界面刚写完配置就来重读报告时,等的必须是
|
|
41
|
+
* 读过新配置的那一轮——等正在跑的那一轮回来等于拿回过期报告,界面于是「保存了却没变」。
|
|
42
|
+
* 配置身份可以直接比:设置服务每次提交都换一份深冻结的解析结果,没变就还是同一个对象。
|
|
43
|
+
*
|
|
44
|
+
* @param trigger - 触发来源;出现在配置页的状态段里。
|
|
45
|
+
* @returns 本次调用所参与的那一轮刷新的结果。
|
|
40
46
|
*/
|
|
41
47
|
async refresh(trigger) {
|
|
42
48
|
if (this.running !== undefined) {
|
|
43
|
-
this.
|
|
44
|
-
|
|
49
|
+
if (this.runningConfig === this.deps.config())
|
|
50
|
+
return this.running;
|
|
51
|
+
await this.running;
|
|
52
|
+
return this.refresh(trigger);
|
|
45
53
|
}
|
|
46
|
-
|
|
54
|
+
const config = this.deps.config();
|
|
55
|
+
const run = this.run(trigger, config);
|
|
56
|
+
this.running = run;
|
|
57
|
+
this.runningConfig = config;
|
|
47
58
|
try {
|
|
48
|
-
return await
|
|
59
|
+
return await run;
|
|
49
60
|
}
|
|
50
61
|
finally {
|
|
51
62
|
this.running = undefined;
|
|
52
|
-
|
|
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)}`;
|
|
63
|
+
this.runningConfig = undefined;
|
|
74
64
|
}
|
|
75
65
|
}
|
|
76
66
|
/** 执行一次刷新,绝不抛出异常。 */
|
|
77
|
-
async run(trigger) {
|
|
67
|
+
async run(trigger, config) {
|
|
78
68
|
const started = Date.now();
|
|
79
69
|
const at = new Date();
|
|
80
70
|
try {
|
|
81
|
-
return await this.execute(trigger, started, at);
|
|
71
|
+
return await this.execute(trigger, config, started, at);
|
|
82
72
|
}
|
|
83
73
|
catch (error) {
|
|
84
74
|
// 上面的任何代码都不允许 reject:所有调用方都是即发即忘的 `void`,而在宿主的
|
|
@@ -101,8 +91,7 @@ export class ApertureRuntime {
|
|
|
101
91
|
}
|
|
102
92
|
}
|
|
103
93
|
/** 一次刷新的主体。 */
|
|
104
|
-
async execute(trigger, started, at) {
|
|
105
|
-
const config = this.deps.config();
|
|
94
|
+
async execute(trigger, config, started, at) {
|
|
106
95
|
if (config.instanceRoot === undefined) {
|
|
107
96
|
const outcome = {
|
|
108
97
|
trigger,
|
|
@@ -119,11 +108,11 @@ export class ApertureRuntime {
|
|
|
119
108
|
this.latest = outcome;
|
|
120
109
|
return outcome;
|
|
121
110
|
}
|
|
122
|
-
const catalog = await this.deps.catalog.load(config.modelMetadataUrl,
|
|
111
|
+
const catalog = await this.deps.catalog.load(config.modelMetadataUrl, DEFAULT_TIMEOUT_MS);
|
|
123
112
|
let listed;
|
|
124
113
|
let endpoint;
|
|
125
114
|
try {
|
|
126
|
-
const listing = await fetchModelsListing(config.instanceRoot, { timeoutMs:
|
|
115
|
+
const listing = await fetchModelsListing(config.instanceRoot, { timeoutMs: DEFAULT_TIMEOUT_MS });
|
|
127
116
|
listed = listing.entries;
|
|
128
117
|
endpoint = listing.endpoint;
|
|
129
118
|
}
|
|
@@ -150,7 +139,6 @@ export class ApertureRuntime {
|
|
|
150
139
|
models: config.models,
|
|
151
140
|
enabledModelIds: config.enabledModelIds,
|
|
152
141
|
modelAliases: config.modelAliases,
|
|
153
|
-
defaultContextWindow: config.defaultContextWindow,
|
|
154
142
|
images: config.images,
|
|
155
143
|
reasoning: config.reasoning,
|
|
156
144
|
}, catalog.lookup);
|
|
@@ -162,7 +150,6 @@ export class ApertureRuntime {
|
|
|
162
150
|
anthropicDisplayName: config.anthropicDisplayName,
|
|
163
151
|
...(config.apiKeyEnv === undefined ? {} : { apiKeyEnv: config.apiKeyEnv }),
|
|
164
152
|
headers: config.headers,
|
|
165
|
-
placeholderCredential: config.placeholderCredential,
|
|
166
153
|
configured: config.models,
|
|
167
154
|
});
|
|
168
155
|
const sync = await this.publish(config, plan);
|
|
@@ -185,11 +172,15 @@ export class ApertureRuntime {
|
|
|
185
172
|
}
|
|
186
173
|
/** 写入方案,或说明为什么没有写入任何内容。 */
|
|
187
174
|
async publish(config, plan) {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
175
|
+
// 关掉同步是「撤下本插件的路由」,不是「什么都不做」:留着一份不再由配置决定的清单,
|
|
176
|
+
// 界面看不出还有谁在服务,用户只能自己去翻 `llm-pi-ai` 段。空方案正好表达撤下——
|
|
177
|
+
// `planSync` 会把每个拥有但不再需要的路由键 unset 掉。
|
|
178
|
+
const routes = config.sync ? plan.routes : [];
|
|
191
179
|
try {
|
|
192
|
-
|
|
180
|
+
const outcome = await applySync(this.deps.settings, routes, plan.ownedRoutes);
|
|
181
|
+
return config.sync || outcome.applied
|
|
182
|
+
? outcome
|
|
183
|
+
: { ...outcome, reason: '同步已关闭,本插件没有发布过路由' };
|
|
193
184
|
}
|
|
194
185
|
catch (error) {
|
|
195
186
|
this.deps.logger.warn('发布发现的清单失败:%s', message(error));
|