dsh-aperture 0.3.2 → 0.4.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/README.en.md +73 -50
- package/README.md +70 -47
- package/lib/client.js +481 -273
- package/lib/client.js.map +1 -1
- package/lib/index.js +1373 -342
- package/lib/types/adapter/context.d.ts +24 -0
- package/lib/types/adapter/index.d.ts +52 -0
- package/lib/types/adapter/replay.d.ts +66 -0
- package/lib/types/adapter/route.d.ts +45 -0
- package/lib/types/adapter/stream.d.ts +24 -0
- package/lib/types/config.d.ts +44 -32
- package/lib/types/index.d.ts +14 -13
- package/lib/types/panel.d.ts +1 -1
- package/lib/types/plan.d.ts +95 -0
- package/lib/types/provider.d.ts +84 -0
- package/lib/types/report.d.ts +11 -11
- package/lib/types/routes.d.ts +35 -0
- package/lib/types/runtime.d.ts +23 -6
- package/lib/types/types.d.ts +15 -5
- package/package.json +39 -23
- package/lib/types/profile.d.ts +0 -55
- package/lib/types/sync.d.ts +0 -47
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { GenerateOptions } from "@deepseek-ai/dsh-llm";
|
|
2
|
+
import { AttachmentStore } from "@deepseek-ai/dsh-attachment";
|
|
3
|
+
import { Context } from "@earendil-works/pi-ai";
|
|
4
|
+
//#region src/adapter/context.d.ts
|
|
5
|
+
/** 转换一张图片所需的东西。 */
|
|
6
|
+
export interface ImageConversion {
|
|
7
|
+
/** 附件服务;没有它的部署里图片无法转换。 */
|
|
8
|
+
readonly attachments: AttachmentStore;
|
|
9
|
+
/** 取消信号,透传给附件的转换。 */
|
|
10
|
+
readonly signal?: AbortSignal;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 把一次请求的历史转成 pi-ai 的 `Context`。
|
|
14
|
+
*
|
|
15
|
+
* @param options - 完整请求。
|
|
16
|
+
* @param images - 图片转换所需的附件服务;缺失时任何图片都按不可用处理。
|
|
17
|
+
* @param onDegrade - 观察到一条历史因为重放元数据不可用而降级时调用。
|
|
18
|
+
* @returns pi-ai 的请求上下文。
|
|
19
|
+
* @throws LlmError 历史里有无法表示的块、缺附件服务、或图片超出请求预算时。
|
|
20
|
+
*/
|
|
21
|
+
export declare function toPiContext(options: GenerateOptions, images?: ImageConversion, onDegrade?: (reason: string) => void): Promise<Context>;
|
|
22
|
+
/** 供流式转换复用的空用量,语义同 {@link ZERO_USAGE}。 */
|
|
23
|
+
export declare const EMPTY_USAGE: import("@earendil-works/pi-ai").Usage;
|
|
24
|
+
//#endregion
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { PlannedRoute, ProviderRoute } from "../plan.js";
|
|
2
|
+
import { RuntimeLogger } from "../runtime.js";
|
|
3
|
+
import { GenerateOptions, LlmAdapter, LlmModelInfo, LlmProviderInfo, LlmResolvedModelInfo, PreparedAdapterCall, StreamChunk } from "@deepseek-ai/dsh-llm";
|
|
4
|
+
import { AttachmentStore } from "@deepseek-ai/dsh-attachment";
|
|
5
|
+
//#region src/adapter/index.d.ts
|
|
6
|
+
/** 适配器需要的外部事实。 */
|
|
7
|
+
export interface AdapterDeps {
|
|
8
|
+
/** 当前这一代要服务的路由;换一份新数组就是新一代。 */
|
|
9
|
+
readonly routes: () => readonly PlannedRoute[];
|
|
10
|
+
/** 解析一条路由的凭据;未配置 `apiKeyEnv` 时返回 `undefined`(该路由用占位凭据)。 */
|
|
11
|
+
readonly resolveApiKey: (provider: string, route: ProviderRoute) => Promise<string | undefined>;
|
|
12
|
+
/** 附件服务;没有它的部署里图片不可转换。 */
|
|
13
|
+
readonly attachments: () => AttachmentStore | undefined;
|
|
14
|
+
/** 诊断。 */
|
|
15
|
+
readonly logger: RuntimeLogger;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 服务本插件声明的那几条路由。
|
|
19
|
+
*
|
|
20
|
+
* 一个实例服务三代路由,`replace()` 只换路由集合、不换实例——这正是宿主那条原子替换契约
|
|
21
|
+
* 想要的形状。
|
|
22
|
+
*/
|
|
23
|
+
export declare class ApertureAdapter extends LlmAdapter {
|
|
24
|
+
/**
|
|
25
|
+
* 外部事实。
|
|
26
|
+
*
|
|
27
|
+
* 写成显式字段而不是构造函数参数属性:本插件的源码会被宿主的 Loader 直接装载,而 Node 的
|
|
28
|
+
* 「只剥类型」模式不支持参数属性——那样加载会在解析期就失败(`ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX`)。
|
|
29
|
+
*/
|
|
30
|
+
private readonly deps;
|
|
31
|
+
private snapshot;
|
|
32
|
+
constructor(deps: AdapterDeps);
|
|
33
|
+
/** {@inheritDoc LlmAdapter.providerInfo} */
|
|
34
|
+
providerInfo(provider: string): LlmProviderInfo;
|
|
35
|
+
/** {@inheritDoc LlmAdapter.listModels} */
|
|
36
|
+
listModels(provider: string): Promise<readonly LlmModelInfo[]>;
|
|
37
|
+
/** {@inheritDoc LlmAdapter.resolveModel} */
|
|
38
|
+
resolveModel(provider: string, model: string): Promise<LlmResolvedModelInfo>;
|
|
39
|
+
/** {@inheritDoc LlmAdapter.prepareCall} */
|
|
40
|
+
prepareCall(provider: string, model: string): Promise<PreparedAdapterCall>;
|
|
41
|
+
/** {@inheritDoc LlmAdapter.stream} */
|
|
42
|
+
stream(options: GenerateOptions): AsyncIterable<StreamChunk>;
|
|
43
|
+
/** 描述一条路由上的模型;未服务的模型只回显身份。 */
|
|
44
|
+
private describe;
|
|
45
|
+
/** 找到当前这一代里的某条路由。 */
|
|
46
|
+
private findRoute;
|
|
47
|
+
/** 取当前这一代;路由数组换了身份才重建。 */
|
|
48
|
+
private current;
|
|
49
|
+
/** 派发一次请求。 */
|
|
50
|
+
private dispatch;
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { ReplayEnvelope } from "@deepseek-ai/dsh-llm";
|
|
2
|
+
import { AssistantMessage } from "@earendil-works/pi-ai";
|
|
3
|
+
//#region src/adapter/replay.d.ts
|
|
4
|
+
/** 本插件信封的类型标记;与任何别的适配器都不共享。 */
|
|
5
|
+
export declare const REPLAY_KIND = "aperture";
|
|
6
|
+
/** 信封的格式版本;字段含义变化时递增。 */
|
|
7
|
+
export declare const REPLAY_VERSION = 1;
|
|
8
|
+
/** 一个块要带回来的原生元数据。 */
|
|
9
|
+
export interface ReplayBlock {
|
|
10
|
+
/** OpenAI Responses 的文本消息 id 与阶段。 */
|
|
11
|
+
readonly textSignature?: string;
|
|
12
|
+
/** Anthropic 的思考签名;Responses 路径上存整条 reasoning item 的 JSON。 */
|
|
13
|
+
readonly thinkingSignature?: string;
|
|
14
|
+
/** 该思考块是否被安全过滤改写(密文仍在 {@link ReplayBlock.thinkingSignature})。 */
|
|
15
|
+
readonly redacted?: boolean;
|
|
16
|
+
/** OpenAI 兼容端点上的工具调用签名。 */
|
|
17
|
+
readonly thoughtSignature?: string;
|
|
18
|
+
/** Responses 路径上动态加载的工具的命名空间。 */
|
|
19
|
+
readonly namespace?: string;
|
|
20
|
+
}
|
|
21
|
+
/** 一次响应的身份与原生元数据。 */
|
|
22
|
+
export interface ReplayResponse {
|
|
23
|
+
readonly kind: typeof REPLAY_KIND;
|
|
24
|
+
readonly version: typeof REPLAY_VERSION;
|
|
25
|
+
/** 产出这条响应的线缆协议。 */
|
|
26
|
+
readonly api: string;
|
|
27
|
+
/** 产出这条响应的 provider 路由。 */
|
|
28
|
+
readonly provider: string;
|
|
29
|
+
/** 请求里的模型 id。 */
|
|
30
|
+
readonly model: string;
|
|
31
|
+
/** 响应级 id(OpenAI 兼容端点的响应 id)。 */
|
|
32
|
+
readonly responseId?: string;
|
|
33
|
+
/** 响应里回报的实际模型名。 */
|
|
34
|
+
readonly responseModel?: string;
|
|
35
|
+
/** 响应实际使用的推理档位。 */
|
|
36
|
+
readonly providerThinkingLevel?: string;
|
|
37
|
+
/** pi-ai 给出的终止原因。 */
|
|
38
|
+
readonly stopReason: string;
|
|
39
|
+
}
|
|
40
|
+
/** 一份可读的重放状态。 */
|
|
41
|
+
export interface ReplayState {
|
|
42
|
+
readonly response: ReplayResponse;
|
|
43
|
+
readonly blocks?: readonly ReplayBlock[];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 把一次成功的响应收成重放信封。
|
|
47
|
+
*
|
|
48
|
+
* `blocks` 由调用方按**实际发出的块顺序**给出:宿主在装配时会按同样顺序对齐,数量不符就整封
|
|
49
|
+
* 丢弃。因此这里不自己去数消息里的块,而是照抄流式转换记下来的那一份。
|
|
50
|
+
*
|
|
51
|
+
* @param message - pi-ai 的终态助手消息。
|
|
52
|
+
* @param provider - provider 路由键。
|
|
53
|
+
* @param model - 请求里的模型 id。
|
|
54
|
+
* @param blocks - 每个已发出块的原生元数据,按首次出现的顺序。
|
|
55
|
+
* @returns 宿主可以存进消息来源的信封。
|
|
56
|
+
*/
|
|
57
|
+
export declare function toReplayState(message: AssistantMessage, provider: string, model: string, blocks: readonly ReplayBlock[]): ReplayEnvelope;
|
|
58
|
+
/**
|
|
59
|
+
* 读回一份重放状态。
|
|
60
|
+
*
|
|
61
|
+
* @param value - 会话日志里携带的原生元数据。
|
|
62
|
+
* @returns 本插件写下的状态;不是本插件的(例如升级前由 `llm-pi-ai` 写下的)时为 `undefined`。
|
|
63
|
+
* @throws LlmError 类型标记与版本都是自己的、字段却不成形时——那是记录损坏,不是别人的信封。
|
|
64
|
+
*/
|
|
65
|
+
export declare function readReplayState(value: unknown): ReplayState | undefined;
|
|
66
|
+
//#endregion
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ApertureProtocol } from "../types.js";
|
|
2
|
+
import { ProviderModel, ProviderRoute } from "../plan.js";
|
|
3
|
+
import { Api, Model, ProviderStreams, ThinkingLevel } from "@earendil-works/pi-ai";
|
|
4
|
+
//#region src/adapter/route.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* 模型没有声明输出上限时,pi-ai 侧使用的上限。
|
|
7
|
+
*
|
|
8
|
+
* 与 `dsh-llm-pi-ai` 的缺省值一致,而 pi-ai 的每一层都要求 `Model.maxTokens` 存在、并**总是**
|
|
9
|
+
* 把它作为请求上限发出去——没有「不带上限」这条路。这里选一个宽松的值,同时保证它不作为
|
|
10
|
+
* 本插件向宿主声明的默认(见 `resolveModel` 的 `defaultMaxTokens`):宿主侧的「没声明过上限」
|
|
11
|
+
* 必须继续是「没声明」。
|
|
12
|
+
*/
|
|
13
|
+
export declare const DEFAULT_MAX_TOKENS = 32768;
|
|
14
|
+
/** 取一种协议的实现。 */
|
|
15
|
+
export declare function protocolApi(protocol: ApertureProtocol): ProviderStreams;
|
|
16
|
+
/**
|
|
17
|
+
* 把一条已发布的模型变成 pi-ai 的 `Model`。
|
|
18
|
+
*
|
|
19
|
+
* @param provider - provider 路由键。
|
|
20
|
+
* @param route - 该路由的协议与地址。
|
|
21
|
+
* @param model - 已发布的模型。
|
|
22
|
+
* @returns pi-ai 的模型描述符。
|
|
23
|
+
*/
|
|
24
|
+
export declare function buildModel(provider: string, route: ProviderRoute, model: ProviderModel): Model<Api>;
|
|
25
|
+
/**
|
|
26
|
+
* 一个模型可选的全部推理档位。
|
|
27
|
+
*
|
|
28
|
+
* 与 pi-ai 的 `getSupportedThinkingLevels` 同义,就地实现是为了不从聚合入口取值:那个入口会
|
|
29
|
+
* 连带载入本插件用不到的目录与 provider 实现。
|
|
30
|
+
*
|
|
31
|
+
* @param model - pi-ai 的模型描述符。
|
|
32
|
+
* @returns 按升序排列的档位。
|
|
33
|
+
*/
|
|
34
|
+
export declare function supportedEfforts(model: Model<Api>): readonly string[];
|
|
35
|
+
/**
|
|
36
|
+
* 宿主选中的推理档位 → pi-ai 的 `reasoning` 选项。
|
|
37
|
+
*
|
|
38
|
+
* 调用方**必须先**用 {@link supportedEfforts} 验证过档位;这里只负责最后的映射:`off` 与
|
|
39
|
+
* 未指定都在协议上等同「不带上思考参数」。
|
|
40
|
+
*
|
|
41
|
+
* @param effort - 已通过验证的档位 id。
|
|
42
|
+
* @returns pi-ai 的档位,或 `undefined`。
|
|
43
|
+
*/
|
|
44
|
+
export declare function effortOption(effort: string | undefined): ThinkingLevel | undefined;
|
|
45
|
+
//#endregion
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { StreamChunk } from "@deepseek-ai/dsh-llm";
|
|
2
|
+
import { AssistantMessageEvent } from "@earendil-works/pi-ai";
|
|
3
|
+
//#region src/adapter/stream.d.ts
|
|
4
|
+
/** 一次流式转换所需的上下文事实。 */
|
|
5
|
+
export interface StreamConversion {
|
|
6
|
+
/** provider 路由键,写进重放信封。 */
|
|
7
|
+
readonly provider: string;
|
|
8
|
+
/** 请求里的模型 id,写进重放信封。 */
|
|
9
|
+
readonly model: string;
|
|
10
|
+
/** 该模型声明的上下文容量,用于识别静默溢出。 */
|
|
11
|
+
readonly contextWindow?: number;
|
|
12
|
+
/** 调用方的取消信号;已中止时把终态错误改写成中止。 */
|
|
13
|
+
readonly signal?: AbortSignal;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 把一条 pi-ai 事件流翻成宿主的块序列。
|
|
17
|
+
*
|
|
18
|
+
* @param events - 一次助手回合的 pi-ai 事件流。
|
|
19
|
+
* @param conversion - 路由、模型与容量事实。
|
|
20
|
+
* @returns 块序列;以 `usage` 与 `finish` 收尾。
|
|
21
|
+
* @throws LlmError 事件流在没有终态事件的情况下结束(`STREAM_CLOSED`)。
|
|
22
|
+
*/
|
|
23
|
+
export declare function toStreamChunks(events: AsyncIterable<AssistantMessageEvent>, conversion: StreamConversion): AsyncIterable<StreamChunk>;
|
|
24
|
+
//#endregion
|
package/lib/types/config.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ResolvedRoute } from "./routes.js";
|
|
2
|
+
import { Dict } from "@deepseek-ai/cosmokit";
|
|
1
3
|
import z from "@deepseek-ai/schemastery";
|
|
2
4
|
//#region src/config.d.ts
|
|
3
5
|
/** 本插件拥有、并可通过它配置的设置命名空间。 */
|
|
@@ -8,30 +10,42 @@ export declare const DEFAULT_MODEL_METADATA_URL = "https://models.dev/models.jso
|
|
|
8
10
|
export declare const DEFAULT_CONTEXT_WINDOW = 128000;
|
|
9
11
|
/** 访问网关与清单的单次请求超时。没有哪个部署需要为此改一次配置。 */
|
|
10
12
|
export declare const DEFAULT_TIMEOUT_MS = 20000;
|
|
11
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* 插件配置。
|
|
15
|
+
*
|
|
16
|
+
* 三个字典字段(`headers`、`modelAliases`、`reasoningEfforts`)的类型写作 cosmokit 的 `Dict`:
|
|
17
|
+
* 那正是 `z.dict()` 的输出类型,而**声明产物必须能写出这个名字**——本模块自己引用它一次,
|
|
18
|
+
* 产物的 `.d.ts` 里才有名字可写。只写 `Record<string, string>` 时两者在类型上等价,但
|
|
19
|
+
* `pnpm run build` 的声明打包会因为推断出的 schema 类型里含 `Dict` 而报 TS2742
|
|
20
|
+
* (「inferred type ... cannot be named」)并中断。
|
|
21
|
+
*/
|
|
12
22
|
export interface Config {
|
|
13
23
|
/** Aperture 实例根地址,例如 `https://ai.example.ts.net`。留空则关闭发现。 */
|
|
14
24
|
baseUrl?: string;
|
|
15
|
-
/**
|
|
16
|
-
|
|
25
|
+
/**
|
|
26
|
+
* 三条路由名的共同前缀:路由键是前缀加协议名的小写连字符写法(`aperture` →
|
|
27
|
+
* `aperture-openai-chat-completions`),显示名是前缀的标题写法加同一串协议名(`Aperture (OpenAI
|
|
28
|
+
* Chat Completions)`)。
|
|
29
|
+
*/
|
|
30
|
+
routePrefix?: string;
|
|
17
31
|
/** 按请求解析的凭据引用;留空则改为发布一个占位请求头。 */
|
|
18
32
|
apiKeyEnv?: string;
|
|
19
33
|
/** 每条路由的请求都会带上的额外请求头;它们优先于占位凭据。 */
|
|
20
|
-
headers?:
|
|
34
|
+
headers?: Dict<string>;
|
|
21
35
|
/** 非空时,只发现这些模型 id。 */
|
|
22
36
|
enabledModelIds?: string[];
|
|
23
37
|
/** 网关模型 id → models.dev 模型 id,用于两边写法不同的 id。 */
|
|
24
|
-
modelAliases?:
|
|
38
|
+
modelAliases?: Dict<string>;
|
|
25
39
|
/** 覆盖与追加,按 id 合并。 */
|
|
26
40
|
models?: Array<{
|
|
27
41
|
id: string;
|
|
28
42
|
name?: string;
|
|
29
|
-
|
|
43
|
+
protocol?: string;
|
|
30
44
|
contextWindow?: number;
|
|
31
45
|
maxTokens?: number;
|
|
32
46
|
input?: Array<'text' | 'image'>;
|
|
33
47
|
thinking?: boolean;
|
|
34
|
-
reasoningEfforts?:
|
|
48
|
+
reasoningEfforts?: Dict<string | null>;
|
|
35
49
|
}>;
|
|
36
50
|
/** models.dev 清单地址;留空则关闭这次补齐。 */
|
|
37
51
|
modelMetadataUrl?: string;
|
|
@@ -39,7 +53,7 @@ export interface Config {
|
|
|
39
53
|
images?: 'ignore' | 'metadata';
|
|
40
54
|
/** `auto` 映射模型的推理能力;`off` 声明所有模型都不推理。 */
|
|
41
55
|
reasoning?: 'auto' | 'off';
|
|
42
|
-
/**
|
|
56
|
+
/** 是否把发现的路由注册给 dsh;关掉即撤下它们(本插件不写任何配置)。 */
|
|
43
57
|
sync?: boolean;
|
|
44
58
|
/** 自动刷新间隔(分钟);`0` 表示只在加载时与配置变更时刷新。 */
|
|
45
59
|
refreshIntervalMinutes?: number;
|
|
@@ -53,27 +67,27 @@ export interface Config {
|
|
|
53
67
|
*/
|
|
54
68
|
export declare const Config: z<NoInfer<Schemastery.ObjectS<NoInfer<{
|
|
55
69
|
baseUrl: z<string, string, "defined">;
|
|
56
|
-
|
|
70
|
+
routePrefix: z<string, string, "defined">;
|
|
57
71
|
apiKeyEnv: z<string, string, "defined">;
|
|
58
|
-
headers: z<
|
|
72
|
+
headers: z<Dict<string, string>, Dict<string, string>, "defined">;
|
|
59
73
|
enabledModelIds: z<string[], string[], "defined">;
|
|
60
|
-
modelAliases: z<
|
|
74
|
+
modelAliases: z<Dict<string, string>, Dict<string, string>, "defined">;
|
|
61
75
|
models: z<({
|
|
62
76
|
id?: string | null | undefined;
|
|
63
77
|
name?: string | null | undefined;
|
|
64
|
-
|
|
78
|
+
protocol?: string | null | undefined;
|
|
65
79
|
contextWindow?: number | null | undefined;
|
|
66
80
|
maxTokens?: number | null | undefined;
|
|
67
81
|
input?: ("text" | "image")[] | null | undefined;
|
|
68
82
|
thinking?: boolean | null | undefined;
|
|
69
|
-
reasoningEfforts?:
|
|
70
|
-
} &
|
|
83
|
+
reasoningEfforts?: Dict<string | null, string> | null | undefined;
|
|
84
|
+
} & Dict)[], Schemastery.ObjectT<NoInfer<{
|
|
71
85
|
/** 模型 id;与已发现的 id 对应,或者新增一个网关没有列出的模型。 */
|
|
72
86
|
id: z<string, string, "defined">;
|
|
73
87
|
/** 选择器里显示的名字。 */
|
|
74
88
|
name: z<string, string, "plain">;
|
|
75
89
|
/** 协议覆盖:OpenAI Chat Completions、OpenAI Responses 或 Anthropic Messages。 */
|
|
76
|
-
|
|
90
|
+
protocol: z<string, string, "plain">;
|
|
77
91
|
/** 上下文容量(token 数)。 */
|
|
78
92
|
contextWindow: z<number, number, "plain">;
|
|
79
93
|
/** 输出能力(token 数)。 */
|
|
@@ -82,8 +96,8 @@ export declare const Config: z<NoInfer<Schemastery.ObjectS<NoInfer<{
|
|
|
82
96
|
input: z<("text" | "image")[], ("text" | "image")[], "plain">;
|
|
83
97
|
/** 强制打开或关闭推理能力。 */
|
|
84
98
|
thinking: z<boolean, boolean, "plain">;
|
|
85
|
-
/** 提供的推理档位:键 = 档位,值 =
|
|
86
|
-
reasoningEfforts: z<
|
|
99
|
+
/** 提供的推理档位:键 = 档位,值 = 协议里的写法;只有 `off` 可以留空。 */
|
|
100
|
+
reasoningEfforts: z<Dict<string | null, string>, Dict<string | null, string>, "plain">;
|
|
87
101
|
}>>[], "defined">;
|
|
88
102
|
modelMetadataUrl: z<string, string, "defined">;
|
|
89
103
|
images: z<"ignore" | "metadata", "ignore" | "metadata", "defined">;
|
|
@@ -92,27 +106,27 @@ export declare const Config: z<NoInfer<Schemastery.ObjectS<NoInfer<{
|
|
|
92
106
|
refreshIntervalMinutes: z<number, number, "defined">;
|
|
93
107
|
}>>>, NoInfer<Schemastery.ObjectT<NoInfer<{
|
|
94
108
|
baseUrl: z<string, string, "defined">;
|
|
95
|
-
|
|
109
|
+
routePrefix: z<string, string, "defined">;
|
|
96
110
|
apiKeyEnv: z<string, string, "defined">;
|
|
97
|
-
headers: z<
|
|
111
|
+
headers: z<Dict<string, string>, Dict<string, string>, "defined">;
|
|
98
112
|
enabledModelIds: z<string[], string[], "defined">;
|
|
99
|
-
modelAliases: z<
|
|
113
|
+
modelAliases: z<Dict<string, string>, Dict<string, string>, "defined">;
|
|
100
114
|
models: z<({
|
|
101
115
|
id?: string | null | undefined;
|
|
102
116
|
name?: string | null | undefined;
|
|
103
|
-
|
|
117
|
+
protocol?: string | null | undefined;
|
|
104
118
|
contextWindow?: number | null | undefined;
|
|
105
119
|
maxTokens?: number | null | undefined;
|
|
106
120
|
input?: ("text" | "image")[] | null | undefined;
|
|
107
121
|
thinking?: boolean | null | undefined;
|
|
108
|
-
reasoningEfforts?:
|
|
109
|
-
} &
|
|
122
|
+
reasoningEfforts?: Dict<string | null, string> | null | undefined;
|
|
123
|
+
} & Dict)[], Schemastery.ObjectT<NoInfer<{
|
|
110
124
|
/** 模型 id;与已发现的 id 对应,或者新增一个网关没有列出的模型。 */
|
|
111
125
|
id: z<string, string, "defined">;
|
|
112
126
|
/** 选择器里显示的名字。 */
|
|
113
127
|
name: z<string, string, "plain">;
|
|
114
128
|
/** 协议覆盖:OpenAI Chat Completions、OpenAI Responses 或 Anthropic Messages。 */
|
|
115
|
-
|
|
129
|
+
protocol: z<string, string, "plain">;
|
|
116
130
|
/** 上下文容量(token 数)。 */
|
|
117
131
|
contextWindow: z<number, number, "plain">;
|
|
118
132
|
/** 输出能力(token 数)。 */
|
|
@@ -121,8 +135,8 @@ export declare const Config: z<NoInfer<Schemastery.ObjectS<NoInfer<{
|
|
|
121
135
|
input: z<("text" | "image")[], ("text" | "image")[], "plain">;
|
|
122
136
|
/** 强制打开或关闭推理能力。 */
|
|
123
137
|
thinking: z<boolean, boolean, "plain">;
|
|
124
|
-
/** 提供的推理档位:键 = 档位,值 =
|
|
125
|
-
reasoningEfforts: z<
|
|
138
|
+
/** 提供的推理档位:键 = 档位,值 = 协议里的写法;只有 `off` 可以留空。 */
|
|
139
|
+
reasoningEfforts: z<Dict<string | null, string>, Dict<string | null, string>, "plain">;
|
|
126
140
|
}>>[], "defined">;
|
|
127
141
|
modelMetadataUrl: z<string, string, "defined">;
|
|
128
142
|
images: z<"ignore" | "metadata", "ignore" | "metadata", "defined">;
|
|
@@ -160,12 +174,10 @@ export interface ResolvedConfig {
|
|
|
160
174
|
readonly instanceRoot: string | undefined;
|
|
161
175
|
/** 原样保留的配置值,供诊断使用。 */
|
|
162
176
|
readonly rawBaseUrl: string;
|
|
163
|
-
|
|
164
|
-
readonly
|
|
165
|
-
|
|
166
|
-
readonly
|
|
167
|
-
readonly responsesDisplayName: string;
|
|
168
|
-
readonly anthropicDisplayName: string;
|
|
177
|
+
/** 三条路由的共同前缀,写错了路由名时报告里指出来的就是它。 */
|
|
178
|
+
readonly routePrefix: string;
|
|
179
|
+
/** 这一代的三条路由(短名、协议、注册键、显示名),顺序固定。 */
|
|
180
|
+
readonly routes: readonly ResolvedRoute[];
|
|
169
181
|
/** 凭据引用;未配置时为 `undefined`。 */
|
|
170
182
|
readonly apiKeyEnv: string | undefined;
|
|
171
183
|
readonly headers: Readonly<Record<string, string>>;
|
package/lib/types/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { ModelsListing, fetchModelsListing } from "./aperture.js";
|
|
2
1
|
import { ConfiguredModel, DiscoveredModel, FactSource, Modality, ModelProvenance } from "./types.js";
|
|
2
|
+
import { ResolvedRoute, resolveRoutes } from "./routes.js";
|
|
3
|
+
import { DEFAULT_PLACEHOLDER_CREDENTIAL, PlannedRoute, ProviderModel, ProviderRoute, RoutePlan, RoutePlanOptions, planRoutes } from "./plan.js";
|
|
3
4
|
import { ModelCatalog } from "./catalog.js";
|
|
4
5
|
import { APERTURE_NAMESPACE, Config, ConfigRef, DEFAULT_CONTEXT_WINDOW, DEFAULT_MODEL_METADATA_URL, ResolvedConfig, configValue, resolveConfig } from "./config.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
6
|
+
import { ApertureProvider, ProviderDeps, PublishOutcome } from "./provider.js";
|
|
7
|
+
import { ApertureRuntime, RefreshOutcome, RuntimeDeps, RuntimeLogger } from "./runtime.js";
|
|
8
|
+
import { ModelsListing, fetchModelsListing } from "./aperture.js";
|
|
7
9
|
import { DeclaredOverrides, PanelModel, PanelRefresh, PanelReport, PanelRoute, buildReport } from "./report.js";
|
|
8
10
|
import { PanelAction, PanelDeps, PanelModelPatch, PanelOps, createPanelOps } from "./panel.js";
|
|
9
11
|
import { RegistryResult, buildRegistry, classifyProtocol, isDeepSeekFamily } from "./registry.js";
|
|
@@ -14,24 +16,23 @@ import { Context } from "@deepseek-ai/cordis";
|
|
|
14
16
|
/** 出现在加载器诊断里的插件名。 */
|
|
15
17
|
export declare const name = "dsh-aperture";
|
|
16
18
|
/**
|
|
17
|
-
* settings
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* 发布的实例。
|
|
19
|
+
* `settings` 是必需依赖:发现报告与配置页都要读它,而插件自己的 `aperture` 段也要由它变成
|
|
20
|
+
* 可编辑的。`llm` 同样必需:本插件的产出就是那几条 provider 路由,没有 LLM 接缝就没有地方
|
|
21
|
+
* 可以注册——走到这里却注册不了,用户只会看到模型莫名其妙地不见了。
|
|
21
22
|
*
|
|
22
23
|
* `typert` 则刻意**不**声明:配置页只是顺手提供的便利,没有它的部署(例如 headless profile)
|
|
23
|
-
*
|
|
24
|
+
* 也该照样获得路由,因此它在下面按需注入,不在就安静跳过。
|
|
24
25
|
*/
|
|
25
26
|
export declare const inject: string[];
|
|
26
27
|
/**
|
|
27
|
-
*
|
|
28
|
+
* 把网关的模型清单注册成自己的 provider 路由:现在注册,之后有任何变化也重新注册。
|
|
28
29
|
*
|
|
29
|
-
* @param ctx - 插件上下文,其中 `settings` 已就绪。
|
|
30
|
+
* @param ctx - 插件上下文,其中 `settings` 与 `llm` 已就绪。
|
|
30
31
|
* @param config - 组合层的 `aperture` 段,已按 `Config` schema 校验并带上默认值;根级 volatile
|
|
31
32
|
* 使它成为活引用,读值走 `config.get()`。
|
|
32
|
-
* @throws Error
|
|
33
|
-
*
|
|
33
|
+
* @throws Error 当配置自身矛盾(路由键不合文法、推理档位非法、模型覆盖重复或无法服务)时抛出
|
|
34
|
+
* ——框架的失败路径正是「坏配置要响亮」的实现方式。
|
|
34
35
|
*/
|
|
35
36
|
export declare function apply(ctx: Context, config: ConfigRef): void;
|
|
36
37
|
//#endregion
|
|
37
|
-
export { APERTURE_NAMESPACE, type Config as ApertureConfig, AperturePanelService, Config, type ConfigRef, type ConfiguredModel, DEFAULT_CONTEXT_WINDOW, DEFAULT_MODEL_METADATA_URL, DEFAULT_PLACEHOLDER_CREDENTIAL, type DeclaredOverrides, type DiscoveredModel, type FactSource, type Modality, ModelCatalog, type ModelProvenance, type ModelsListing, PANEL_CONTRIBUTION, PANEL_INVOCATIONS, PANEL_NAMESPACE, PANEL_PACKAGE,
|
|
38
|
+
export { APERTURE_NAMESPACE, type Config as ApertureConfig, AperturePanelService, ApertureProvider, ApertureRuntime, Config, type ConfigRef, type ConfiguredModel, DEFAULT_CONTEXT_WINDOW, DEFAULT_MODEL_METADATA_URL, DEFAULT_PLACEHOLDER_CREDENTIAL, type DeclaredOverrides, type DiscoveredModel, type FactSource, type Modality, ModelCatalog, type ModelProvenance, type ModelsListing, PANEL_CONTRIBUTION, PANEL_INVOCATIONS, PANEL_NAMESPACE, PANEL_PACKAGE, type PanelAction, type PanelDeps, type PanelModel, type PanelModelPatch, type PanelOps, type PanelRefresh, type PanelReport, type PanelRoute, type PlannedRoute, type ProviderDeps, type ProviderModel, type ProviderRoute, type PublishOutcome, type RefreshOutcome, type RegistryResult, type ResolvedConfig, type ResolvedRoute, type RoutePlan, type RoutePlanOptions, type RuntimeDeps, type RuntimeLogger, buildModelsEndpoint, buildRegistry, buildReport, buildRouteBaseUrl, classifyProtocol, configValue, createPanelOps, fetchModelsListing, isDeepSeekFamily, normalizeBaseUrl, planRoutes, resolveConfig, resolveRoutes };
|
package/lib/types/panel.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ export interface PanelModelPatch {
|
|
|
15
15
|
/** 显示名。 */
|
|
16
16
|
readonly name?: string | null;
|
|
17
17
|
/** 协议覆盖;也是让「未服务」的模型变得可服务的唯一方式。 */
|
|
18
|
-
readonly
|
|
18
|
+
readonly protocol?: string | null;
|
|
19
19
|
/** 上下文容量,以 token 计。 */
|
|
20
20
|
readonly contextWindow?: number | null;
|
|
21
21
|
/** 最大输出,以 token 计。 */
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { ApertureProtocol, ConfiguredModel, DiscoveredModel, Modality, REASONING_LEVELS, ReasoningLevel } from "./types.js";
|
|
2
|
+
import { ResolvedRoute } from "./routes.js";
|
|
3
|
+
//#region src/plan.d.ts
|
|
4
|
+
/** 无凭据路由发送的占位凭据的默认值。 */
|
|
5
|
+
export declare const DEFAULT_PLACEHOLDER_CREDENTIAL = "dsh-aperture";
|
|
6
|
+
/** 一条模型声明的推理档位:键 = 档位,值 = 协议里的写法;只有 `off` 可以留空。 */
|
|
7
|
+
export type ReasoningEfforts = Partial<Record<ReasoningLevel, string | null>>;
|
|
8
|
+
/**
|
|
9
|
+
* 本插件会写下的 pi-ai 兼容开关。
|
|
10
|
+
*
|
|
11
|
+
* 只列真的会用到的两个:pi-ai 从 base URL 与 provider id 推断协议形态,而 Aperture 的
|
|
12
|
+
* URL 对它说明不了什么,因此 DeepSeek 方言必须直接声明。
|
|
13
|
+
*/
|
|
14
|
+
export interface ModelCompat {
|
|
15
|
+
/** 该端点接受 `reasoning_effort`。 */
|
|
16
|
+
readonly supportsReasoningEffort?: boolean;
|
|
17
|
+
/** 思考开关的线缆写法。 */
|
|
18
|
+
readonly thinkingFormat?: 'deepseek';
|
|
19
|
+
}
|
|
20
|
+
/** 一条 pi-ai 路由上的一个模型。 */
|
|
21
|
+
export interface ProviderModel {
|
|
22
|
+
/** 网关接受的模型 id。 */
|
|
23
|
+
readonly id: string;
|
|
24
|
+
/** 选择器里显示的名字。 */
|
|
25
|
+
readonly name: string;
|
|
26
|
+
/** 上下文容量(token 数)。 */
|
|
27
|
+
readonly contextWindow?: number;
|
|
28
|
+
/** 输出上限(token 数)。 */
|
|
29
|
+
readonly maxTokens?: number;
|
|
30
|
+
/** 请求模态。 */
|
|
31
|
+
readonly input: readonly Modality[];
|
|
32
|
+
/** 推理档位;未声明表示不提供任何档位。 */
|
|
33
|
+
readonly reasoningEfforts?: Readonly<ReasoningEfforts>;
|
|
34
|
+
/** 协议兼容开关。 */
|
|
35
|
+
readonly compat?: ModelCompat;
|
|
36
|
+
}
|
|
37
|
+
/** 一条发布出去的路由:pi-ai 侧的 provider 事实。 */
|
|
38
|
+
export interface ProviderRoute {
|
|
39
|
+
/** 选择器里显示的路由名。 */
|
|
40
|
+
readonly displayName: string;
|
|
41
|
+
/** 线缆协议,也是适配器挑哪一种 pi-ai 实现的依据。 */
|
|
42
|
+
readonly protocol: ApertureProtocol;
|
|
43
|
+
/** 该协议下所有模型的基点地址。 */
|
|
44
|
+
readonly baseURL: string;
|
|
45
|
+
/** 每条请求带上的头;没有时为缺失。 */
|
|
46
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
47
|
+
/** 该路由解析凭据用的引用;没有时以占位凭据发出。 */
|
|
48
|
+
readonly apiKeyEnv?: string;
|
|
49
|
+
/** 该路由承载的模型。 */
|
|
50
|
+
readonly models: readonly ProviderModel[];
|
|
51
|
+
}
|
|
52
|
+
/** 一次发布过程所需的全部内容。 */
|
|
53
|
+
export interface RoutePlanOptions {
|
|
54
|
+
/** 归一化后的实例根。 */
|
|
55
|
+
readonly instanceRoot: string;
|
|
56
|
+
/**
|
|
57
|
+
* 这一代的三条路由,顺序即它们在报告与界面上出现的顺序。
|
|
58
|
+
*
|
|
59
|
+
* 命名(短名、路由键、显示名)只在 `resolveRoutes` 里推一次,这一层只按协议归拢模型;名字因此
|
|
60
|
+
* 不可能与发出去的东西对不上。
|
|
61
|
+
*/
|
|
62
|
+
readonly routes: readonly ResolvedRoute[];
|
|
63
|
+
/** 凭据引用;部署配置了才有。 */
|
|
64
|
+
readonly apiKeyEnv?: string;
|
|
65
|
+
/** 额外的路由头;它们优先于占位头。 */
|
|
66
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
67
|
+
/** 已配置的逐模型覆盖项,用于查询显式的推理档位。 */
|
|
68
|
+
readonly configured: readonly ConfiguredModel[];
|
|
69
|
+
}
|
|
70
|
+
/** 一条要注册出去的路由:命名 + pi-ai 侧的事实 + 它承载的模型。 */
|
|
71
|
+
export interface PlannedRoute {
|
|
72
|
+
/** 这条路由的命名(短名、协议、路由键、显示名)。 */
|
|
73
|
+
readonly route: ResolvedRoute;
|
|
74
|
+
/** pi-ai 侧要用的事实,适配器按它派发。 */
|
|
75
|
+
readonly provider: ProviderRoute;
|
|
76
|
+
/** 该路由发布的模型,用于报告与模型信息查询。 */
|
|
77
|
+
readonly models: readonly DiscoveredModel[];
|
|
78
|
+
}
|
|
79
|
+
/** 完整的发布方案。 */
|
|
80
|
+
export interface RoutePlan {
|
|
81
|
+
/** 至少含一个模型的路由,顺序稳定。 */
|
|
82
|
+
readonly routes: readonly PlannedRoute[];
|
|
83
|
+
/** 没有任何路由可以服务的已发现模型。 */
|
|
84
|
+
readonly unserved: readonly DiscoveredModel[];
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 把归一化后的注册表变成 provider 路由。
|
|
88
|
+
*
|
|
89
|
+
* @param models - 所有已发现的模型。
|
|
90
|
+
* @param options - 路由与凭据配置。
|
|
91
|
+
* @returns 要发布的路由,以及未能被服务的模型。
|
|
92
|
+
*/
|
|
93
|
+
export declare function planRoutes(models: readonly DiscoveredModel[], options: RoutePlanOptions): RoutePlan;
|
|
94
|
+
//#endregion
|
|
95
|
+
export { REASONING_LEVELS, type ReasoningLevel };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { RoutePlan } from "./plan.js";
|
|
2
|
+
import { RuntimeLogger } from "./runtime.js";
|
|
3
|
+
import { LlmRuntime } from "@deepseek-ai/dsh-llm";
|
|
4
|
+
import { AttachmentStore } from "@deepseek-ai/dsh-attachment";
|
|
5
|
+
import { CredentialProvider } from "@deepseek-ai/dsh-credentials";
|
|
6
|
+
//#region src/provider.d.ts
|
|
7
|
+
/** 一次发布的结果。 */
|
|
8
|
+
export interface PublishOutcome {
|
|
9
|
+
/** 已注册的路由键;注册未成功时为空。 */
|
|
10
|
+
readonly routes: readonly string[];
|
|
11
|
+
/** 未能注册时,给用户看的原因与要做的事。 */
|
|
12
|
+
readonly reason?: string;
|
|
13
|
+
}
|
|
14
|
+
/** 注册层需要的外部事实。 */
|
|
15
|
+
export interface ProviderDeps {
|
|
16
|
+
/** LLM 服务。 */
|
|
17
|
+
readonly llm: LlmRuntime;
|
|
18
|
+
/** 凭据服务;未挂载时为缺失。 */
|
|
19
|
+
readonly credentials: () => CredentialProvider | undefined;
|
|
20
|
+
/** 附件服务;没有它的部署里图片不可转换。 */
|
|
21
|
+
readonly attachments: () => AttachmentStore | undefined;
|
|
22
|
+
/** 诊断。 */
|
|
23
|
+
readonly logger: RuntimeLogger;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 本插件的 provider 身份。
|
|
27
|
+
*
|
|
28
|
+
* 一个实例跨代复用同一个适配器对象,只替换它的路由集合。
|
|
29
|
+
*/
|
|
30
|
+
export declare class ApertureProvider {
|
|
31
|
+
private readonly adapter;
|
|
32
|
+
/** 外部事实;显式字段而非参数属性,理由见 `ApertureAdapter`。 */
|
|
33
|
+
private readonly deps;
|
|
34
|
+
private registration;
|
|
35
|
+
private directory;
|
|
36
|
+
/** 当前这一代适配器服务的路由。 */
|
|
37
|
+
private routes;
|
|
38
|
+
/** 最近一次想要服务的路由键;冲突重试要用它。 */
|
|
39
|
+
private desired;
|
|
40
|
+
/** 最近一次注册被谁挡住了;没有冲突时为 `undefined`。 */
|
|
41
|
+
private blocked;
|
|
42
|
+
constructor(deps: ProviderDeps);
|
|
43
|
+
/**
|
|
44
|
+
* 发布一代方案。
|
|
45
|
+
*
|
|
46
|
+
* @param plan - 本次要服务的路由与未被服务的模型。
|
|
47
|
+
* @returns 发生了什么。
|
|
48
|
+
*/
|
|
49
|
+
publish(plan: RoutePlan): Promise<PublishOutcome>;
|
|
50
|
+
/**
|
|
51
|
+
* 冲突之后重试注册。
|
|
52
|
+
*
|
|
53
|
+
* 由 `llm/adapters-updated` 触发:挡住本插件的那一方可能刚刚把自己撤下(例如用户照报告删掉了
|
|
54
|
+
* 遗留配置)。没有冲突时是空操作,因此本插件自己的注册所触发的那次事件不会变成自激循环。
|
|
55
|
+
*
|
|
56
|
+
* @returns 这一轮之后是否已注册。
|
|
57
|
+
*/
|
|
58
|
+
retry(): boolean;
|
|
59
|
+
/** 撤下本插件的全部注册。 */
|
|
60
|
+
dispose(): void;
|
|
61
|
+
/** 首次注册或原子替换路由。 */
|
|
62
|
+
private registerRoutes;
|
|
63
|
+
/**
|
|
64
|
+
* 声明本插件的路由可以由哪个配置段激活。
|
|
65
|
+
*
|
|
66
|
+
* 声明让官方「模型」页把本插件的路由列成行,并读出它们的凭据引用。那个页面只为
|
|
67
|
+
* `llm-deepseek` 与 `llm-pi-ai` 准备了编辑布局,因此落到本插件的命名空间时会显示「其余字段在
|
|
68
|
+
* cordis.patch.yml 中」的提示并禁用保存——本插件的配置面是自己的插件页。
|
|
69
|
+
*
|
|
70
|
+
* 只声明**真的有模型**的那些路由:一种协议在这个网关上没有模型时,那一行只会是多出来的空行
|
|
71
|
+
* (该协议下没有任何可选的东西),而它在这里也声明不了任何事实——本插件不写配置。
|
|
72
|
+
*/
|
|
73
|
+
private registerDirectory;
|
|
74
|
+
/** 给用户看的冲突说明:点名被占用的键,并说清要手工做什么。 */
|
|
75
|
+
private conflictReason;
|
|
76
|
+
/**
|
|
77
|
+
* 解析一条路由的凭据。
|
|
78
|
+
*
|
|
79
|
+
* 配置了 `apiKeyEnv` 就**必须**解析出可用的值:那是一个明确的声明,悄悄退回占位凭据会让
|
|
80
|
+
* 一次配错了密钥的部署看起来像一次网关拒绝。
|
|
81
|
+
*/
|
|
82
|
+
private resolveApiKey;
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|