dsh-aperture 0.1.0 → 0.2.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/cordis.patch.yml CHANGED
@@ -41,8 +41,9 @@
41
41
  modelMetadataUrl: 'https://models.dev/models.json'
42
42
 
43
43
  # 是否把发现的模型清单写进 llm-pi-ai 设置段。
44
- # false = 只发现不写入(`/aperture` 命令照常汇报)。
44
+ # false = 只发现不写入(标签页照常显示发现结果)。
45
45
  sync: true
46
46
 
47
- # 自动刷新间隔(分钟)。0(默认)表示只在加载时、配置变更时和 `/aperture refresh` 时刷新。
47
+ # 自动刷新间隔(分钟)。0(默认)表示只在加载时、配置变更时和标签页上的
48
+ # 「立即刷新」时刷新。
48
49
  refreshIntervalMinutes: 0
package/lib/index.d.ts CHANGED
@@ -16,6 +16,10 @@
16
16
  *
17
17
  * 它不转换任何协议格式。这正是重点。
18
18
  *
19
+ * 界面在设置里:浏览器半边(`client/aperture.js`)在「插件」下挂一个 Aperture 标签页,
20
+ * 用来改实例地址与同步开关、立刻刷新、以及把已经发布的路由撤下来。除此之外没有别的
21
+ * 界面——发现本身发生在插件加载、配置变更与刷新间隔到点上。
22
+ *
19
23
  * ```yaml
20
24
  * - id: aperture
21
25
  * name: 'dsh-aperture'
@@ -32,10 +36,15 @@ export type { ModelsListing } from './aperture.ts';
32
36
  export { ModelCatalog } from './catalog.ts';
33
37
  export { APERTURE_NAMESPACE, DEFAULT_CONTEXT_WINDOW, DEFAULT_MODEL_METADATA_URL, PI_AI_NAMESPACE, resolveConfig, } from './config.ts';
34
38
  export type { Config as ApertureConfig, ResolvedConfig } from './config.ts';
39
+ export { createPanelOps } from './panel.ts';
40
+ export type { PanelAction, PanelConfiguration, PanelDeps, PanelModelPatch, PanelOps } from './panel.ts';
41
+ export { buildReport } from './report.ts';
42
+ export type { PanelModel, PanelModelOverride, PanelRefresh, PanelReport, PanelRoute } from './report.ts';
35
43
  export { DEFAULT_PLACEHOLDER_CREDENTIAL, buildProfilePlan } from './profile.ts';
36
44
  export type { ProfilePlan, ProfileOptions, RoutePlan } from './profile.ts';
37
45
  export { buildRegistry, classifyProtocol, isDeepSeekFamily } from './registry.ts';
38
46
  export type { RegistryResult } from './registry.ts';
47
+ export { AperturePanelService, PANEL_CONTRIBUTION, PANEL_INVOCATIONS, PANEL_NAMESPACE, PANEL_PACKAGE } from './remote.ts';
39
48
  export { applySync, clearRoutes, planSync } from './sync.ts';
40
49
  export type { SyncOutcome } from './sync.ts';
41
50
  export type { ConfiguredModel, DiscoveredModel, FactSource, Modality, ModelProvenance } from './types.ts';
@@ -49,8 +58,8 @@ export declare const name = "dsh-aperture";
49
58
  * 意味着框架会把插件挂在 PENDING 直到 settings 就绪;provider 一旦被替换就卸载插件,
50
59
  * 恢复后再重新加载——而不是留下一个已经加载、却无处发布的实例。
51
60
  *
52
- * `commands` 则刻意**不**声明:`/aperture` 这层界面只是顺手提供的便利,没有它的
53
- * 部署也应该照样获得发现能力。
61
+ * `typert` 则刻意**不**声明:设置界面上的标签页只是顺手提供的便利,没有它的部署(例如
62
+ * headless profile)也应该照样获得发现能力,因此它在下面按需注入,不在就安静地跳过。
54
63
  */
55
64
  export declare const inject: string[];
56
65
  /**
package/lib/index.js CHANGED
@@ -16,6 +16,10 @@
16
16
  *
17
17
  * 它不转换任何协议格式。这正是重点。
18
18
  *
19
+ * 界面在设置里:浏览器半边(`client/aperture.js`)在「插件」下挂一个 Aperture 标签页,
20
+ * 用来改实例地址与同步开关、立刻刷新、以及把已经发布的路由撤下来。除此之外没有别的
21
+ * 界面——发现本身发生在插件加载、配置变更与刷新间隔到点上。
22
+ *
19
23
  * ```yaml
20
24
  * - id: aperture
21
25
  * name: 'dsh-aperture'
@@ -27,16 +31,20 @@
27
31
  */
28
32
  import { fetchModelsListing } from "./aperture.js";
29
33
  import { ModelCatalog } from "./catalog.js";
30
- import { registerApertureCommand } from "./command.js";
31
34
  import { APERTURE_NAMESPACE, Config as ConfigSchema, resolveConfig } from "./config.js";
35
+ import { createPanelOps } from "./panel.js";
32
36
  import { buildProfilePlan } from "./profile.js";
33
37
  import { buildRegistry, classifyProtocol } from "./registry.js";
38
+ import { AperturePanelService, PANEL_CONTRIBUTION, PANEL_NAMESPACE, PANEL_PACKAGE } from "./remote.js";
34
39
  import { ApertureRuntime } from "./runtime.js";
35
40
  export { fetchModelsListing } from "./aperture.js";
36
41
  export { ModelCatalog } from "./catalog.js";
37
42
  export { APERTURE_NAMESPACE, DEFAULT_CONTEXT_WINDOW, DEFAULT_MODEL_METADATA_URL, PI_AI_NAMESPACE, resolveConfig, } from "./config.js";
43
+ export { createPanelOps } from "./panel.js";
44
+ export { buildReport } from "./report.js";
38
45
  export { DEFAULT_PLACEHOLDER_CREDENTIAL, buildProfilePlan } from "./profile.js";
39
46
  export { buildRegistry, classifyProtocol, isDeepSeekFamily } from "./registry.js";
47
+ export { AperturePanelService, PANEL_CONTRIBUTION, PANEL_INVOCATIONS, PANEL_NAMESPACE, PANEL_PACKAGE } from "./remote.js";
40
48
  export { applySync, clearRoutes, planSync } from "./sync.js";
41
49
  export { buildModelsEndpoint, buildRouteBaseUrl, normalizeBaseUrl } from "./url.js";
42
50
  export { Config } from "./config.js";
@@ -48,8 +56,8 @@ export const name = 'dsh-aperture';
48
56
  * 意味着框架会把插件挂在 PENDING 直到 settings 就绪;provider 一旦被替换就卸载插件,
49
57
  * 恢复后再重新加载——而不是留下一个已经加载、却无处发布的实例。
50
58
  *
51
- * `commands` 则刻意**不**声明:`/aperture` 这层界面只是顺手提供的便利,没有它的
52
- * 部署也应该照样获得发现能力。
59
+ * `typert` 则刻意**不**声明:设置界面上的标签页只是顺手提供的便利,没有它的部署(例如
60
+ * headless profile)也应该照样获得发现能力,因此它在下面按需注入,不在就安静地跳过。
53
61
  */
54
62
  export const inject = ['settings'];
55
63
  /**
@@ -116,7 +124,12 @@ export function apply(ctx, config) {
116
124
  resolveConfig(value);
117
125
  },
118
126
  });
119
- ctx.inject(['commands'], (commandCtx) => {
120
- registerApertureCommand(commandCtx, runtime, readConfig);
127
+ // 设置里那个标签页需要宿主半边的 Remote 面(报告、配置读写、立刻刷新、撤下路由),而
128
+ // Typert 注册表只有 Web 这类装配了网关的 profile 才有。其余 profile 里这一整块被跳过:
129
+ // 发现照常运行,只是没有可点按的界面。
130
+ ctx.inject(['typert'], (panelCtx) => {
131
+ const ops = createPanelOps({ runtime, config: readConfig, settings: ctx.settings });
132
+ panelCtx.effect(() => panelCtx.typert.register(PANEL_CONTRIBUTION), 'aperture panel invocations');
133
+ panelCtx.plugin(AperturePanelService, ops);
121
134
  });
122
135
  }
package/lib/panel.d.ts ADDED
@@ -0,0 +1,113 @@
1
+ /**
2
+ * 设置界面标签页的宿主半边。
3
+ *
4
+ * 标签页本身在浏览器里跑(`client/aperture.js`),它能读到的只有这里暴露的端点——与参考
5
+ * 实现(`@xiaoyuyu6420/dsh-backup` 的 `backupPanel`)同一种做法:界面不直接碰宿主存储,
6
+ * 所有读写都经自己的 Remote 命名空间往返,因此客户端半边不必注入设置传输,也不必知道设置
7
+ * 文档长什么样。
8
+ *
9
+ * 写入仍然是配置,所以它落在 `aperture` 命名空间的用户层:地址与同步开关用路径操作写,
10
+ * 「撤销」是把字段从用户层移除、回落到组合层与默认值。单个模型的参数也走这里,只是它们更
11
+ * 零碎——容量、模态、推理、协议落在 `models` 的对应条目上,清单别名落在 `modelAliases[id]`,
12
+ * 而写入是按字段合并的:界面没提到的字段原样留着(`reasoningEfforts` 界面根本不编辑,也不该
13
+ * 被顺手抹掉)。报告与两个动作则不是配置——把发现的模型塞进设置文档会让「用户写了什么」与
14
+ * 「插件发现了什么」混成同一份账,而后者每轮刷新都会被重写。
15
+ *
16
+ * 端点都不抛异常:失败是界面要显示的结果之一,因此它是返回值里的字段,而不是需要标签页
17
+ * 去分辨的 rejected promise。
18
+ *
19
+ * @module dsh-aperture/panel
20
+ */
21
+ import type { SettingsProvider } from '@deepseek-ai/dsh-settings';
22
+ import type { ResolvedConfig } from './config.ts';
23
+ import { type PanelReport } from './report.ts';
24
+ import { type ApertureRuntime } from './runtime.ts';
25
+ import type { Modality } from './types.ts';
26
+ /**
27
+ * 界面为单个模型写下的参数。
28
+ *
29
+ * 补丁是**稀疏**的:界面只发它改动过的字段,没提到的字段原样留在设置文档里——因此界面
30
+ * 完全不编辑的字段(`reasoningEfforts`)也不会被它顺手抹掉。`null` 是「这条覆盖不要了」,
31
+ * 也就是把字段从 `models` 的那一条里删掉,回落到发现值与清单。
32
+ */
33
+ export interface PanelModelPatch {
34
+ /** 显示名。 */
35
+ readonly name?: string | null;
36
+ /** 协议覆盖;也是让「未服务」的模型变得可服务的唯一方式。 */
37
+ readonly api?: string | null;
38
+ /** 上下文容量,以 token 计。 */
39
+ readonly contextWindow?: number | null;
40
+ /** 最大输出,以 token 计。 */
41
+ readonly maxTokens?: number | null;
42
+ /** 请求模态。 */
43
+ readonly input?: readonly Modality[] | null;
44
+ /** 强制打开(`true`)或关闭(`false`)推理。 */
45
+ readonly thinking?: boolean | null;
46
+ /** 清单别名(`aperture.modelAliases`);空串或 `null` 表示不设。 */
47
+ readonly alias?: string | null;
48
+ }
49
+ /** 一次动作的结果:成败与一句人话。 */
50
+ export interface PanelAction {
51
+ /** 动作是否落地。 */
52
+ readonly ok: boolean;
53
+ /** 面向用户的说明。 */
54
+ readonly summary: string;
55
+ }
56
+ /** 标签页表单要显示的东西。 */
57
+ export interface PanelConfiguration {
58
+ /** 生效的实例地址(schema 默认值 → 组合层 → 用户层)。 */
59
+ readonly baseUrl: string;
60
+ /** 生效的同步开关。 */
61
+ readonly sync: boolean;
62
+ /** `baseUrl` 在用户层里有条目,也就是被覆盖了。 */
63
+ readonly baseUrlOverridden: boolean;
64
+ /** `sync` 在用户层里有条目。 */
65
+ readonly syncOverridden: boolean;
66
+ /** 设置文档是否接受写入;为假时表单只读。 */
67
+ readonly writable: boolean;
68
+ }
69
+ /** 端点背后的东西。 */
70
+ export interface PanelDeps {
71
+ /** 发现运行时,报告与刷新都来自它。 */
72
+ readonly runtime: ApertureRuntime;
73
+ /** 当前生效配置的活引用(thunk)。 */
74
+ readonly config: () => ResolvedConfig;
75
+ /** 设置服务:读 `aperture` 段的用户层,也写它。 */
76
+ readonly settings: SettingsProvider;
77
+ }
78
+ /** 标签页可以调用的端点。 */
79
+ export interface PanelOps {
80
+ /** 最近一次刷新做了什么;不触发任何工作。 */
81
+ status(): PanelReport;
82
+ /** 立刻重新发现并发布。 */
83
+ refresh(): Promise<PanelAction>;
84
+ /** 把本插件拥有的路由从 `llm-pi-ai` 段撤下来。 */
85
+ withdraw(): Promise<PanelAction>;
86
+ /** 表单要显示的配置与「是否被覆盖」。 */
87
+ configuration(): PanelConfiguration;
88
+ /**
89
+ * 写入配置。
90
+ *
91
+ * @param baseUrl - 新地址;`null` 表示撤销覆盖(从用户层移除),`undefined` 表示不碰。
92
+ * @param sync - 新开关;`undefined` 表示不碰。
93
+ */
94
+ save(baseUrl: string | null | undefined, sync: boolean | undefined): Promise<PanelAction>;
95
+ /**
96
+ * 写入一个模型的参数。
97
+ *
98
+ * 一次只动一个模型:界面上一行一个「保存」,写下去的就只有那一行,版本校验也只管这一次
99
+ * 写入。没提到的字段原样留在设置文档里(界面根本不编辑的 `reasoningEfforts` 就不会被顺手
100
+ * 抹掉),空串与 `null` 都表示「这一项不覆盖」。
101
+ *
102
+ * @param id - 模型 id(Aperture 接受的那个)。
103
+ * @param patch - 要改的字段;`null` 表示撤销这个模型的全部覆盖(含别名)。
104
+ */
105
+ edit(id: string, patch: PanelModelPatch | null): Promise<PanelAction>;
106
+ }
107
+ /**
108
+ * 组装端点。
109
+ *
110
+ * @param deps - 运行时、配置活引用与设置服务。
111
+ * @returns 五个端点;`status` 与 `configuration` 是同步的,读一份已经算好的结果不该等待。
112
+ */
113
+ export declare function createPanelOps(deps: PanelDeps): PanelOps;
package/lib/panel.js ADDED
@@ -0,0 +1,271 @@
1
+ /**
2
+ * 设置界面标签页的宿主半边。
3
+ *
4
+ * 标签页本身在浏览器里跑(`client/aperture.js`),它能读到的只有这里暴露的端点——与参考
5
+ * 实现(`@xiaoyuyu6420/dsh-backup` 的 `backupPanel`)同一种做法:界面不直接碰宿主存储,
6
+ * 所有读写都经自己的 Remote 命名空间往返,因此客户端半边不必注入设置传输,也不必知道设置
7
+ * 文档长什么样。
8
+ *
9
+ * 写入仍然是配置,所以它落在 `aperture` 命名空间的用户层:地址与同步开关用路径操作写,
10
+ * 「撤销」是把字段从用户层移除、回落到组合层与默认值。单个模型的参数也走这里,只是它们更
11
+ * 零碎——容量、模态、推理、协议落在 `models` 的对应条目上,清单别名落在 `modelAliases[id]`,
12
+ * 而写入是按字段合并的:界面没提到的字段原样留着(`reasoningEfforts` 界面根本不编辑,也不该
13
+ * 被顺手抹掉)。报告与两个动作则不是配置——把发现的模型塞进设置文档会让「用户写了什么」与
14
+ * 「插件发现了什么」混成同一份账,而后者每轮刷新都会被重写。
15
+ *
16
+ * 端点都不抛异常:失败是界面要显示的结果之一,因此它是返回值里的字段,而不是需要标签页
17
+ * 去分辨的 rejected promise。
18
+ *
19
+ * @module dsh-aperture/panel
20
+ */
21
+ import { APERTURE_NAMESPACE, PI_AI_NAMESPACE } from "./namespaces.js";
22
+ import { buildReport } from "./report.js";
23
+ import { message } from "./runtime.js";
24
+ import { clearRoutes } from "./sync.js";
25
+ /** 把未知值当成一个普通对象;数组与 `null` 都不算。 */
26
+ function asRecord(input) {
27
+ return typeof input === 'object' && input !== null && !Array.isArray(input)
28
+ ? input
29
+ : {};
30
+ }
31
+ /**
32
+ * 读 `aperture` 段。
33
+ *
34
+ * `value` 是叠加了默认值、组合层与用户层之后的生效值,`user` 是用户层的原始片段——
35
+ * 字段在不在 `user` 里,才是「有没有被覆盖」的判据;拿生效值去和组合层比会得出错误答案。
36
+ *
37
+ * @param settings - 设置服务。
38
+ * @returns 解析视图;命名空间尚未注册或描述符读不到时给出空值。
39
+ */
40
+ function readSection(settings) {
41
+ const descriptor = settings.describe({ redactSecrets: true }).find((entry) => entry.ns === APERTURE_NAMESPACE);
42
+ return {
43
+ value: asRecord(descriptor?.value),
44
+ user: asRecord(descriptor?.user),
45
+ revision: descriptor?.revision,
46
+ writable: settings.writable !== false,
47
+ };
48
+ }
49
+ /**
50
+ * 把补丁合并进 `models` 里的一条覆盖。
51
+ *
52
+ * 只做**校验与合并**,不做任何写入:非法值在这里被挡下来,因为写进设置文档的坏值会让下一轮
53
+ * 刷新的 `resolveConfig` 直接抛异常——那时用户已经在别处改坏了配置,而界面只能给他一份再也
54
+ * 刷新不出来的报告。
55
+ *
56
+ * @param base - 现有条目;没有时只带 `id`。
57
+ * @param patch - 界面发来的稀疏补丁。
58
+ * @returns 合并后的条目(已经没有任何字段时为 `undefined`),或一个错误。
59
+ */
60
+ function mergeEntry(base, patch) {
61
+ const entry = { ...base };
62
+ if (patch.name !== undefined) {
63
+ const name = patch.name === null ? '' : patch.name.trim();
64
+ if (name.length === 0)
65
+ delete entry.name;
66
+ else
67
+ entry.name = name;
68
+ }
69
+ if (patch.api !== undefined) {
70
+ const api = patch.api === null ? '' : patch.api.trim();
71
+ if (api.length === 0)
72
+ delete entry.api;
73
+ else if (api !== 'openai-completions' && api !== 'anthropic-messages') {
74
+ return { error: `api "${api}" 无法服务;只能是 openai-completions 或 anthropic-messages` };
75
+ }
76
+ else
77
+ entry.api = api;
78
+ }
79
+ for (const field of ['contextWindow', 'maxTokens']) {
80
+ const value = patch[field];
81
+ if (value === undefined)
82
+ continue;
83
+ if (value === null) {
84
+ delete entry[field];
85
+ continue;
86
+ }
87
+ if (!Number.isInteger(value) || value < 1) {
88
+ return { error: `${field} 必须是不小于 1 的整数` };
89
+ }
90
+ entry[field] = value;
91
+ }
92
+ if (patch.input !== undefined) {
93
+ const input = patch.input === null ? [] : [...new Set(patch.input)];
94
+ if (input.some((item) => item !== 'text' && item !== 'image')) {
95
+ return { error: 'input 只能包含 text 或 image' };
96
+ }
97
+ if (input.length === 0)
98
+ delete entry.input;
99
+ else
100
+ entry.input = input;
101
+ }
102
+ if (patch.thinking !== undefined) {
103
+ if (patch.thinking === null)
104
+ delete entry.thinking;
105
+ else
106
+ entry.thinking = patch.thinking;
107
+ }
108
+ return Object.keys(entry).some((key) => key !== 'id') ? { entry } : {};
109
+ }
110
+ /** 一个字段在用户层里有没有条目。 */
111
+ function overridden(user, field) {
112
+ return Object.prototype.hasOwnProperty.call(user, field);
113
+ }
114
+ /**
115
+ * 组装端点。
116
+ *
117
+ * @param deps - 运行时、配置活引用与设置服务。
118
+ * @returns 五个端点;`status` 与 `configuration` 是同步的,读一份已经算好的结果不该等待。
119
+ */
120
+ export function createPanelOps(deps) {
121
+ // 报告每次都按当前配置现组装:覆盖与别名本身是配置,改完必须立刻能在列表里看到。
122
+ const report = () => buildReport(deps.runtime.last(), deps.config());
123
+ return {
124
+ status: report,
125
+ async refresh() {
126
+ // 刷新自己吞掉异常并把原因放进 outcome,因此这里只搬运它。
127
+ const outcome = await deps.runtime.refresh('设置界面');
128
+ return outcome.ok
129
+ ? { ok: true, summary: '已重新发现并发布。' }
130
+ : { ok: false, summary: `刷新没有成功:${outcome.error ?? '原因未知'}` };
131
+ },
132
+ async withdraw() {
133
+ const owned = [deps.config().route, deps.config().anthropicRoute];
134
+ try {
135
+ const outcome = await clearRoutes(deps.settings, owned);
136
+ // 「已经没有了」与「刚撤下来」都是想要的状态,因此都算成功。
137
+ return outcome.applied
138
+ ? {
139
+ ok: true,
140
+ summary: `已从 "${PI_AI_NAMESPACE}" 撤下 ${outcome.ops} 条路由:${owned.join('、')}。`
141
+ + '下一次刷新会按当前配置重新发布;要让撤下长期生效,请关掉同步开关。',
142
+ }
143
+ : { ok: true, summary: `没有需要撤下的路由:${outcome.reason ?? '原因未知'}` };
144
+ }
145
+ catch (error) {
146
+ return { ok: false, summary: `撤下路由失败:${message(error)}` };
147
+ }
148
+ },
149
+ configuration() {
150
+ const section = readSection(deps.settings);
151
+ const config = deps.config();
152
+ return {
153
+ // 表单显示的是用户写的那个值(`rawBaseUrl`),不是归一化后的 `instanceRoot`:
154
+ // 把归一化结果回填进输入框,会让人以为自己写的地址被悄悄改掉了。
155
+ baseUrl: typeof section.value.baseUrl === 'string' ? section.value.baseUrl : config.rawBaseUrl,
156
+ sync: typeof section.value.sync === 'boolean' ? section.value.sync : config.sync,
157
+ baseUrlOverridden: overridden(section.user, 'baseUrl'),
158
+ syncOverridden: overridden(section.user, 'sync'),
159
+ writable: section.writable,
160
+ };
161
+ },
162
+ async save(baseUrl, sync) {
163
+ const ops = [];
164
+ if (baseUrl === null)
165
+ ops.push({ op: 'unset', path: ['baseUrl'] });
166
+ else if (baseUrl !== undefined)
167
+ ops.push({ op: 'set', path: ['baseUrl'], value: baseUrl.trim() });
168
+ if (sync !== undefined)
169
+ ops.push({ op: 'set', path: ['sync'], value: sync });
170
+ if (ops.length === 0)
171
+ return { ok: true, summary: '没有要保存的改动。' };
172
+ // 撤销覆盖是唯一一种「只移除、不写入」的保存,值得单独说一句。
173
+ const withdrawOnly = ops.every((op) => op.op === 'unset');
174
+ try {
175
+ // 带着刚读到的版本号写入:期间有别人改过就拒绝,而不是覆盖他的改动。
176
+ await deps.settings.mutate(APERTURE_NAMESPACE, ops, readSection(deps.settings).revision);
177
+ return {
178
+ ok: true,
179
+ summary: withdrawOnly
180
+ ? '已撤销覆盖,回落到组合层与默认值。'
181
+ : '已写入设置;插件会按新配置重新发现。',
182
+ };
183
+ }
184
+ catch (error) {
185
+ return { ok: false, summary: `保存失败:${message(error)}` };
186
+ }
187
+ },
188
+ async edit(id, patch) {
189
+ const modelId = typeof id === 'string' ? id.trim() : '';
190
+ if (modelId.length === 0)
191
+ return { ok: false, summary: '缺少模型 id。' };
192
+ // 补丁必须显式给出:`null` 是「撤销这个模型的全部覆盖」,缺失是调用方写错了。
193
+ if (patch === undefined || (patch !== null && typeof patch !== 'object')) {
194
+ return { ok: false, summary: '缺少要写入的参数。' };
195
+ }
196
+ const section = readSection(deps.settings);
197
+ // `models` 是数组,而路径操作只能整段替换它,因此每次都算出完整的新数组再写回去。
198
+ // 基础取自生效值:组合层若也写过 models,它在界面上本来就是看得见的那些条目。
199
+ const before = (Array.isArray(section.value.models) ? section.value.models : []).map(asRecord);
200
+ const aliases = asRecord(section.value.modelAliases);
201
+ // 撤销别名只在**用户层确实有**这个键时才写:界面显示的是生效别名,它可能来自组合层或
202
+ // 清单,而删一个不存在的键要么白写、要么被设置服务当成坏路径拒绝,两种都不该发生。
203
+ const owned = asRecord(section.user.modelAliases);
204
+ const index = before.findIndex((entry) => entry.id === modelId);
205
+ const currentAlias = typeof aliases[modelId] === 'string' ? aliases[modelId] : '';
206
+ let models = before;
207
+ const nextAliases = { ...aliases };
208
+ let revoked = false;
209
+ if (patch === null) {
210
+ // `null` 是「撤销这个模型的全部覆盖」:`models` 里那条与清单别名一起走。
211
+ models = before.filter((_, at) => at !== index);
212
+ if (currentAlias.length > 0)
213
+ delete nextAliases[modelId];
214
+ revoked = true;
215
+ }
216
+ else {
217
+ const merged = mergeEntry(index === -1 ? { id: modelId } : before[index], patch);
218
+ if (merged.error !== undefined)
219
+ return { ok: false, summary: `"${modelId}":${merged.error}` };
220
+ // 就地替换而不是挪到末尾:一条覆盖的位置不该因为改了一个字段就变(界面上也一样,
221
+ // 列表顺序来自路由与发现顺序,不来自用户改过哪一条)。
222
+ if (index === -1) {
223
+ if (merged.entry !== undefined)
224
+ models = [...before, merged.entry];
225
+ }
226
+ else if (merged.entry === undefined) {
227
+ models = before.filter((_, at) => at !== index);
228
+ }
229
+ else {
230
+ models = before.map((entry, at) => (at === index ? merged.entry : entry));
231
+ }
232
+ // 别名不进 `models`:它是「网关 id → 清单 id」的映射,与覆盖是两件事。
233
+ const alias = patch.alias === undefined ? currentAlias : (patch.alias ?? '').trim();
234
+ if (alias.length === 0)
235
+ delete nextAliases[modelId];
236
+ else
237
+ nextAliases[modelId] = alias;
238
+ }
239
+ const ops = [];
240
+ if (JSON.stringify(models) !== JSON.stringify(before)) {
241
+ ops.push({ op: 'set', path: ['models'], value: models });
242
+ }
243
+ // 别名逐键比对:改过的写、撤掉的删,没动过的连碰都不碰。
244
+ for (const key of new Set([...Object.keys(aliases), ...Object.keys(nextAliases)])) {
245
+ if (nextAliases[key] === aliases[key])
246
+ continue;
247
+ if (nextAliases[key] !== undefined) {
248
+ ops.push({ op: 'set', path: ['modelAliases', key], value: nextAliases[key] });
249
+ continue;
250
+ }
251
+ // 生效值里没有它了,但界面上那只是「回落到清单」;只有用户层写过才需要删。
252
+ if (Object.prototype.hasOwnProperty.call(owned, key)) {
253
+ ops.push({ op: 'unset', path: ['modelAliases', key] });
254
+ }
255
+ }
256
+ if (ops.length === 0)
257
+ return { ok: true, summary: '没有要保存的改动。' };
258
+ const summary = revoked
259
+ ? `已撤销 "${modelId}" 的全部覆盖,回落到发现值与清单;插件会按新配置重新发现。`
260
+ : `已保存 "${modelId}" 的参数;插件会按新配置重新发现。`;
261
+ try {
262
+ // 与 `save` 同一条路径:带着刚读到的版本号写入,期间别人改过就拒绝。
263
+ await deps.settings.mutate(APERTURE_NAMESPACE, ops, section.revision);
264
+ return { ok: true, summary };
265
+ }
266
+ catch (error) {
267
+ return { ok: false, summary: `保存失败:${message(error)}` };
268
+ }
269
+ },
270
+ };
271
+ }
package/lib/registry.d.ts CHANGED
@@ -21,7 +21,7 @@
21
21
  *
22
22
  * @module dsh-aperture/registry
23
23
  */
24
- import type { ApertureProtocol, BuildOptions, CatalogLookup, DiscoveredModel, Modality, ModelProvenance } from './types.ts';
24
+ import type { ApertureProtocol, BuildOptions, CatalogLookup, DiscoveredModel } from './types.ts';
25
25
  /** 一次归一化的结果。 */
26
26
  export interface RegistryResult {
27
27
  /** 所有模型,包括没有路由能服务的那些(它们的 `protocol` 缺失)。 */
@@ -49,9 +49,5 @@ export declare function buildRegistry(entries: readonly unknown[], options: Buil
49
49
  * @returns 可服务的协议;没有适配的协议时为 `undefined`。
50
50
  */
51
51
  export declare function classifyProtocol(endpoints: readonly string[]): ApertureProtocol | undefined;
52
- /** 为 `/aperture models` 报告渲染一个模型的来源。 */
53
- export declare function describeProvenance(provenance: ModelProvenance): string;
54
52
  /** 判断一个模型是否属于 DeepSeek 推理方言。 */
55
53
  export declare function isDeepSeekFamily(model: DiscoveredModel): boolean;
56
- /** 为报告中的一行渲染一个模态列表。 */
57
- export declare function formatModalities(input: readonly Modality[]): string;
package/lib/registry.js CHANGED
@@ -226,21 +226,7 @@ function matchesEndpoint(advertised, known) {
226
226
  const path = advertised.split('?')[0]?.replace(/\/+$/u, '') ?? '';
227
227
  return path === known || path.endsWith(known);
228
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
229
  /** 判断一个模型是否属于 DeepSeek 推理方言。 */
240
230
  export function isDeepSeekFamily(model) {
241
231
  return `${model.id} ${model.name} ${model.provider ?? ''}`.toLowerCase().includes('deepseek');
242
232
  }
243
- /** 为报告中的一行渲染一个模态列表。 */
244
- export function formatModalities(input) {
245
- return input.length === 0 ? 'none' : input.join('+');
246
- }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * 标签页与宿主之间的 Typert Remote 面:`aperturePanel` 命名空间。
3
+ *
4
+ * 描述符是手工登记的,因为生成它们的 Typert 生成器并不随 DSH 发布,而这一步的规范本身
5
+ * 很小:宿主半边用 `src-json` 编解码器,浏览器半边带 strict 校验,两端共享同一组端点名。
6
+ * 参数按描述符里的顺序位置传入宿主方法,因此两边的名字与顺序必须一致。
7
+ *
8
+ * 端点 id 的 `#` 之前是包名,之后是 `<命名空间>/<方法>`;这与网关、注册表和客户端三方
9
+ * 共用的文法一致。
10
+ *
11
+ * @module dsh-aperture/remote
12
+ */
13
+ import type { Context } from '@deepseek-ai/cordis';
14
+ import type { InvocationDescriptor } from '@deepseek-ai/dsh-typert-protocol';
15
+ import { TypertRemoteService } from '@deepseek-ai/dsh-typert-protocol';
16
+ import type { TypertContribution } from '@deepseek-ai/dsh-typert-registry';
17
+ import type { PanelAction, PanelConfiguration, PanelModelPatch, PanelOps } from './panel.ts';
18
+ import type { PanelReport } from './report.ts';
19
+ /** 包名,同时是端点 id 的前缀。 */
20
+ export declare const PANEL_PACKAGE = "dsh-aperture";
21
+ /** 线上命名空间,也是浏览器半边注入的服务名(`remote.aperturePanel`)。 */
22
+ export declare const PANEL_NAMESPACE = "aperturePanel";
23
+ /**
24
+ * 本插件暴露给浏览器的全部端点。
25
+ *
26
+ * 端点名有一处不显眼的约束:api-gateway 在客户端为每个命名空间建一个
27
+ * `RemoteNamespaceService`,端点会成为它的属性,因此与它自己的成员重名会被
28
+ * `validateContribution` 拒绝,**整份贡献一起撤回**(浏览器里只剩一行 console.error,
29
+ * 界面安静地什么都不出现)。它预置的名字是 `ctx` / `empty` / `invokeRemote` / `methods`
30
+ * / `name` / `namespace` 与 `has` / `install` / `installDirect` / `installScoped` /
31
+ * `assertMethodAvailable` / `remove` —— 「撤下路由」因此叫 `withdraw` 而不是 `remove`。
32
+ */
33
+ export declare const PANEL_INVOCATIONS: readonly InvocationDescriptor[];
34
+ /**
35
+ * 登记进 Typert 注册表的贡献。
36
+ *
37
+ * `schemas`/`model` 是生成器的产物,这里没有生成器,因此它们如实为空:本插件不导出可
38
+ * 复用的 schema,也没有需要反射的业务类型。端点本身仍然完整——宿主注册 `src-json`、浏览器
39
+ * 带 strict 校验,两者按同一份契约工作。
40
+ */
41
+ export declare const PANEL_CONTRIBUTION: TypertContribution;
42
+ /**
43
+ * `aperturePanel` 宿主服务:方法与描述符一一对应,实现全部委托给 {@link PanelOps}。
44
+ *
45
+ * 方法签名必须与描述符的参数顺序一致——`save` 的两个形参名字与描述符里的两个参数同名,
46
+ * 顺序也相同,网关就是按描述符顺序把 wire 参数位置传入的。
47
+ */
48
+ export declare class AperturePanelService extends TypertRemoteService {
49
+ private readonly ops;
50
+ /**
51
+ * @param ctx - 挂载上下文。
52
+ * @param ops - 端点实现。
53
+ */
54
+ constructor(ctx: Context, ops: PanelOps);
55
+ /** 最近一次刷新的报告。 */
56
+ status(): PanelReport;
57
+ /** 立刻重新发现并发布。 */
58
+ refresh(): Promise<PanelAction>;
59
+ /** 撤下本插件发布的路由。方法名受端点名约束,见 {@link PANEL_INVOCATIONS}。 */
60
+ withdraw(): Promise<PanelAction>;
61
+ /** 标签页表单要显示的配置。 */
62
+ configuration(): PanelConfiguration;
63
+ /**
64
+ * 写入配置。
65
+ *
66
+ * @param baseUrl - 新地址;`null` 撤销覆盖,`undefined` 不碰。
67
+ * @param sync - 新开关;`undefined` 不碰。
68
+ * @returns 成败与一句人话。
69
+ */
70
+ save(baseUrl: string | null | undefined, sync: boolean | undefined): Promise<PanelAction>;
71
+ /**
72
+ * 写入一个模型的参数。
73
+ *
74
+ * @param id - 模型 id。
75
+ * @param patch - 要改的字段;`null` 表示撤销这个模型的全部覆盖。
76
+ * @returns 成败与一句人话。
77
+ */
78
+ edit(id: string, patch: PanelModelPatch | null): Promise<PanelAction>;
79
+ }