pi-tsgw 3.4.1 → 3.5.1
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.md +3 -0
- package/extensions/magpie/controller.ts +164 -0
- package/extensions/magpie/data.ts +385 -0
- package/extensions/magpie/service.ts +181 -0
- package/extensions/magpie/summary.ts +43 -0
- package/extensions/magpie/ui.ts +79 -0
- package/extensions/magpie/view.ts +273 -0
- package/extensions/models/gateway-catalog-recovery.ts +13 -9
- package/extensions/models/gateway-catalog-service.ts +65 -62
- package/extensions/models/gateway-catalog-shared.ts +56 -0
- package/extensions/models/gateway-catalog.ts +43 -62
- package/extensions/models/gateway-model-list.ts +20 -0
- package/extensions/provider/catalog-state.ts +7 -1
- package/extensions/provider/index.ts +9 -4
- package/package.json +2 -2
- package/skills/tsgw-mk-docs/SKILL.md +5 -5
- package/skills/tsgw-mk-docs/SOURCE.md +4 -5
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { resolve } from "node:path";
|
|
2
2
|
import { createGatewayCatalogRecovery } from "./gateway-catalog-recovery.ts";
|
|
3
|
+
import { getGatewayCatalogRegistry, type GatewayCatalogState, type CatalogFetchTask as FetchTask } from "./gateway-catalog-shared.ts";
|
|
3
4
|
import {
|
|
4
5
|
DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS,
|
|
5
6
|
gatewayModelCacheScope,
|
|
@@ -21,15 +22,6 @@ export interface GatewayCatalogOutcome {
|
|
|
21
22
|
failed: boolean;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
interface FetchTask {
|
|
25
|
-
controller: AbortController;
|
|
26
|
-
force: boolean;
|
|
27
|
-
networkStarted: boolean;
|
|
28
|
-
cacheSelected: boolean;
|
|
29
|
-
waiters: number;
|
|
30
|
-
promise: Promise<GatewayCatalogOutcome>;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
25
|
export function catalogCancelledError(): Error {
|
|
34
26
|
const error = new Error("TSGW 模型目录获取已取消。");
|
|
35
27
|
error.name = "AbortError";
|
|
@@ -71,58 +63,69 @@ function waitForTask(task: FetchTask, signal: AbortSignal | undefined, valid: ()
|
|
|
71
63
|
});
|
|
72
64
|
}
|
|
73
65
|
|
|
74
|
-
/**
|
|
75
|
-
function createService(root: string,
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (task?.waiters === 0 && !task.cacheSelected) task.controller.abort();
|
|
66
|
+
/** 当前模块的方法包装共享状态,不保存 UI、过滤配置、宿主 context 或常驻明文密钥。 */
|
|
67
|
+
function createService(root: string, cacheFilePath: string, state: GatewayCatalogState) {
|
|
68
|
+
const { scope } = state;
|
|
69
|
+
const changed = () => { for (const listener of state.listeners) listener(); };
|
|
70
|
+
const recovery = createGatewayCatalogRecovery({ state: state.recovery, onIdle: () => {
|
|
71
|
+
if (state.task?.waiters === 0 && !state.task.cacheSelected) state.task.controller.abort();
|
|
81
72
|
} });
|
|
82
73
|
|
|
83
74
|
const check = (key: string): void => {
|
|
84
|
-
if (!valid || gatewayModelCacheScope(root, key) !== scope) throw catalogCancelledError();
|
|
75
|
+
if (!state.valid || gatewayModelCacheScope(root, key) !== scope) throw catalogCancelledError();
|
|
85
76
|
};
|
|
86
77
|
const read = (key: string): GatewayCatalogSnapshot | undefined => {
|
|
87
78
|
check(key);
|
|
88
79
|
const local = getCachedGatewayModelIds(root, key, { cacheFilePath });
|
|
89
|
-
if (local && (!snapshot || local.storedAt > snapshot.storedAt))
|
|
90
|
-
snapshot = { ids: [...local.ids], storedAt: local.storedAt, source: "cache"
|
|
91
|
-
|
|
80
|
+
if (local && (!state.snapshot || local.storedAt > state.snapshot.storedAt)) {
|
|
81
|
+
state.snapshot = { ids: [...local.ids], storedAt: local.storedAt, source: "cache",
|
|
82
|
+
...(local.magpie ? { magpie: local.magpie } : {}) };
|
|
83
|
+
changed();
|
|
84
|
+
}
|
|
85
|
+
return state.snapshot ? copy(state.snapshot) : undefined;
|
|
92
86
|
};
|
|
93
87
|
const currentOutcome = (networkAttempted: boolean, networkSucceeded: boolean,
|
|
94
88
|
failed = false, retryable = true): GatewayCatalogOutcome => ({
|
|
95
|
-
...(snapshot ? { snapshot: copy(snapshot) } : {}),
|
|
89
|
+
...(state.snapshot ? { snapshot: copy(state.snapshot) } : {}),
|
|
96
90
|
networkAttempted, networkSucceeded, failed, retryable,
|
|
97
91
|
});
|
|
98
92
|
|
|
99
93
|
const api = {
|
|
100
94
|
scope,
|
|
101
|
-
|
|
95
|
+
/** 方法包装可更新,额度共享始终关联这一份稳定状态。 */
|
|
96
|
+
identity: state as object,
|
|
97
|
+
subscribe(listener: () => void): () => void {
|
|
98
|
+
state.listeners.add(listener);
|
|
99
|
+
return () => { state.listeners.delete(listener); };
|
|
100
|
+
},
|
|
101
|
+
valid: () => state.valid,
|
|
102
|
+
isMagpie: () => state.valid && state.snapshot?.magpie === true,
|
|
102
103
|
read,
|
|
103
|
-
fetching: () => !!task?.networkStarted && !task.controller.signal.aborted,
|
|
104
|
-
refreshKind: () => task && !task.controller.signal.aborted ? (task.force ? "force" : "normal") : undefined,
|
|
105
|
-
summary(minimumStoredAt = 0, applied = snapshot) {
|
|
106
|
-
if (!valid || !applied) return undefined;
|
|
104
|
+
fetching: () => !!state.task?.networkStarted && !state.task.controller.signal.aborted,
|
|
105
|
+
refreshKind: () => state.task && !state.task.controller.signal.aborted ? (state.task.force ? "force" : "normal") : undefined,
|
|
106
|
+
summary(minimumStoredAt = 0, applied = state.snapshot) {
|
|
107
|
+
if (!state.valid || !applied) return undefined;
|
|
107
108
|
const result = readGatewayModelCacheSummary(cacheFilePath, minimumStoredAt, scope, applied.ids);
|
|
108
109
|
return result?.storedAt === applied.storedAt ? result : undefined;
|
|
109
110
|
},
|
|
110
111
|
invalidate() {
|
|
111
|
-
valid = false;
|
|
112
|
+
state.valid = false;
|
|
112
113
|
recovery.stop();
|
|
113
|
-
task?.controller.abort();
|
|
114
|
-
task = undefined;
|
|
115
|
-
snapshot = undefined;
|
|
114
|
+
state.task?.controller.abort();
|
|
115
|
+
state.task = undefined;
|
|
116
|
+
state.snapshot = undefined;
|
|
117
|
+
changed();
|
|
118
|
+
state.listeners.clear();
|
|
116
119
|
},
|
|
117
120
|
/** 后台通过仍活跃实例的宿主服务发起单次获取,联网许可和凭据仍交宿主。 */
|
|
118
121
|
recoverUsing(run: (signal: AbortSignal) => Promise<GatewayCatalogOutcome>, signal?: AbortSignal): Promise<GatewayCatalogOutcome> {
|
|
119
|
-
if (!valid) return Promise.reject(catalogCancelledError());
|
|
122
|
+
if (!state.valid) return Promise.reject(catalogCancelledError());
|
|
120
123
|
return recovery.join(run, () => currentOutcome(false, false, true), signal).then(outcome => {
|
|
121
|
-
if (!valid || signal?.aborted) throw catalogCancelledError();
|
|
124
|
+
if (!state.valid || signal?.aborted) throw catalogCancelledError();
|
|
122
125
|
return outcome;
|
|
123
126
|
});
|
|
124
127
|
},
|
|
125
|
-
peek: () => snapshot ? copy(snapshot) : undefined,
|
|
128
|
+
peek: () => state.snapshot ? copy(state.snapshot) : undefined,
|
|
126
129
|
recover(key: string, signal?: AbortSignal): Promise<GatewayCatalogOutcome> {
|
|
127
130
|
try { check(key); } catch (error) { return Promise.reject(error); }
|
|
128
131
|
return recovery.join(
|
|
@@ -138,19 +141,19 @@ function createService(root: string, scope: string, cacheFilePath: string) {
|
|
|
138
141
|
read(key);
|
|
139
142
|
return Promise.resolve(currentOutcome(false, false));
|
|
140
143
|
}
|
|
141
|
-
if (task?.controller.signal.aborted) task = undefined;
|
|
142
|
-
if (task?.cacheSelected && options.force) {
|
|
144
|
+
if (state.task?.controller.signal.aborted) state.task = undefined;
|
|
145
|
+
if (state.task?.cacheSelected && options.force) {
|
|
143
146
|
// 缓存决定已经完成;这次 force 不能被旧缓存结果吞掉。
|
|
144
|
-
return task.promise.then(() => api.request(key, options));
|
|
147
|
+
return state.task.promise.then(() => api.request(key, options));
|
|
145
148
|
}
|
|
146
|
-
if (task) {
|
|
147
|
-
task.force ||= options.force === true;
|
|
148
|
-
return waitForTask(task, options.signal, () => valid, recovery.hasParticipants);
|
|
149
|
+
if (state.task) {
|
|
150
|
+
state.task.force ||= options.force === true;
|
|
151
|
+
return waitForTask(state.task, options.signal, () => state.valid, recovery.hasParticipants);
|
|
149
152
|
}
|
|
150
153
|
const controller = new AbortController();
|
|
151
154
|
const active: FetchTask = { controller, force: options.force === true,
|
|
152
155
|
networkStarted: false, cacheSelected: false, waiters: 0, promise: Promise.resolve(currentOutcome(false, false)) };
|
|
153
|
-
task = active;
|
|
156
|
+
state.task = active;
|
|
154
157
|
// 微任务内才决定缓存/网络,使同批 force 能提升尚未决策的 normal。
|
|
155
158
|
const execute = Promise.resolve().then(async () => {
|
|
156
159
|
controller.signal.throwIfAborted();
|
|
@@ -163,14 +166,17 @@ function createService(root: string, scope: string, cacheFilePath: string) {
|
|
|
163
166
|
const result = await getGatewayModelIds(root, key, {
|
|
164
167
|
cacheFilePath, force: true, signal: controller.signal,
|
|
165
168
|
});
|
|
166
|
-
if (!valid || task !== active || controller.signal.aborted) throw catalogCancelledError();
|
|
169
|
+
if (!state.valid || state.task !== active || controller.signal.aborted) throw catalogCancelledError();
|
|
167
170
|
if (!result.ok || result.stale) {
|
|
168
171
|
read(key);
|
|
169
172
|
const code = result.ok ? result.fallbackStatus : result.status;
|
|
170
173
|
return currentOutcome(true, false, true, code !== 401 && code !== 403);
|
|
171
174
|
}
|
|
172
|
-
if (!snapshot || result.storedAt >= snapshot.storedAt)
|
|
173
|
-
snapshot = { ids: [...result.ids], storedAt: result.storedAt, source: result.cached ? "cache" : "network"
|
|
175
|
+
if (!state.snapshot || result.storedAt >= state.snapshot.storedAt) {
|
|
176
|
+
state.snapshot = { ids: [...result.ids], storedAt: result.storedAt, source: result.cached ? "cache" : "network",
|
|
177
|
+
...(result.magpie ? { magpie: result.magpie } : {}) };
|
|
178
|
+
changed();
|
|
179
|
+
}
|
|
174
180
|
return currentOutcome(true, !result.cached);
|
|
175
181
|
});
|
|
176
182
|
// 即使底层忽略取消,取消仍立即释放共享槽;迟到结果不能提交。
|
|
@@ -181,9 +187,9 @@ function createService(root: string, scope: string, cacheFilePath: string) {
|
|
|
181
187
|
});
|
|
182
188
|
active.promise = Promise.race([execute, aborted]).finally(() => {
|
|
183
189
|
if (onAbort) controller.signal.removeEventListener("abort", onAbort);
|
|
184
|
-
if (task === active) task = undefined;
|
|
190
|
+
if (state.task === active) state.task = undefined;
|
|
185
191
|
});
|
|
186
|
-
return waitForTask(active, options.signal, () => valid, recovery.hasParticipants);
|
|
192
|
+
return waitForTask(active, options.signal, () => state.valid, recovery.hasParticipants);
|
|
187
193
|
},
|
|
188
194
|
};
|
|
189
195
|
return api;
|
|
@@ -191,27 +197,24 @@ function createService(root: string, scope: string, cacheFilePath: string) {
|
|
|
191
197
|
|
|
192
198
|
export type GatewayCatalogService = ReturnType<typeof createService>;
|
|
193
199
|
|
|
194
|
-
|
|
195
|
-
version: 4;
|
|
196
|
-
/** 每个 agent-dir 的唯一缓存路径一个当前槽,不按密钥维护多个目录。 */
|
|
197
|
-
slots: Map<string, GatewayCatalogService>;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const REGISTRY = Symbol.for("pi-tsgw:gateway-catalog");
|
|
200
|
+
const services = new WeakMap<GatewayCatalogState, GatewayCatalogService>();
|
|
201
201
|
|
|
202
|
-
/**
|
|
202
|
+
/** 当前模块复用自己的方法包装;跨重导入共用稳定状态与实际 GET。 */
|
|
203
203
|
export function getGatewayCatalogService(root: string, key: string, cacheFilePath: string): GatewayCatalogService {
|
|
204
|
-
const
|
|
205
|
-
let registry = globals[REGISTRY];
|
|
206
|
-
if (registry && registry.version !== 4) throw new Error("TSGW 目录共享状态版本不兼容,请重启宿主。");
|
|
207
|
-
registry ??= globals[REGISTRY] = { version: 4, slots: new Map() };
|
|
204
|
+
const registry = getGatewayCatalogRegistry();
|
|
208
205
|
const path = resolve(cacheFilePath);
|
|
209
206
|
const scope = gatewayModelCacheScope(root, key);
|
|
210
|
-
let
|
|
211
|
-
if (!
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
207
|
+
let state = registry.slots.get(path);
|
|
208
|
+
if (!state || state.scope !== scope || !state.valid) {
|
|
209
|
+
if (state) {
|
|
210
|
+
let previous = services.get(state);
|
|
211
|
+
previous ??= createService(root, path, state);
|
|
212
|
+
previous.invalidate();
|
|
213
|
+
}
|
|
214
|
+
state = { scope, valid: true, listeners: new Set(), recovery: {} };
|
|
215
|
+
registry.slots.set(path, state);
|
|
215
216
|
}
|
|
217
|
+
let service = services.get(state);
|
|
218
|
+
if (!service) services.set(state, service = createService(root, path, state));
|
|
216
219
|
return service;
|
|
217
220
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { FetchGatewayModelIdsResult, ModelCacheEntry } from "./gateway-catalog.ts";
|
|
2
|
+
import type { GatewayCatalogOutcome, GatewayCatalogSnapshot } from "./gateway-catalog-service.ts";
|
|
3
|
+
import type { GatewayCatalogRecoveryState } from "./gateway-catalog-recovery.ts";
|
|
4
|
+
|
|
5
|
+
export interface CatalogFetchTask {
|
|
6
|
+
controller: AbortController;
|
|
7
|
+
force: boolean;
|
|
8
|
+
networkStarted: boolean;
|
|
9
|
+
cacheSelected: boolean;
|
|
10
|
+
waiters: number;
|
|
11
|
+
promise: Promise<GatewayCatalogOutcome>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface GatewayCatalogState {
|
|
15
|
+
scope: string;
|
|
16
|
+
valid: boolean;
|
|
17
|
+
snapshot?: GatewayCatalogSnapshot;
|
|
18
|
+
task?: CatalogFetchTask;
|
|
19
|
+
listeners: Set<() => void>;
|
|
20
|
+
recovery: GatewayCatalogRecoveryState;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface CacheRequest {
|
|
24
|
+
promise: Promise<FetchGatewayModelIdsResult>;
|
|
25
|
+
revision: number;
|
|
26
|
+
storedAt: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface GatewayCatalogRegistry {
|
|
30
|
+
/** 目录与额度共用的核心契约标记;仅不兼容变更时改变,执行方法与可选字段不参与。 */
|
|
31
|
+
schema: 1;
|
|
32
|
+
/** 每条规范化缓存路径只持有一个当前网关身份;会话退出不清空其他实例的数据。 */
|
|
33
|
+
slots: Map<string, GatewayCatalogState>;
|
|
34
|
+
cache: {
|
|
35
|
+
entries: Map<string, ModelCacheEntry>;
|
|
36
|
+
inFlight: Map<string, CacheRequest>;
|
|
37
|
+
latest: Map<string, number>;
|
|
38
|
+
latestDisk: Map<string, number>;
|
|
39
|
+
revision: number;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const REGISTRY = Symbol.for("pi-tsgw:gateway-catalog");
|
|
44
|
+
|
|
45
|
+
/** 跨重导入只保留数据与在途协调记录,服务方法由当前模块创建。 */
|
|
46
|
+
export function getGatewayCatalogRegistry(): GatewayCatalogRegistry {
|
|
47
|
+
const globals = globalThis as typeof globalThis & { [REGISTRY]?: GatewayCatalogRegistry };
|
|
48
|
+
const existing = globals[REGISTRY];
|
|
49
|
+
if (existing && existing.schema !== 1)
|
|
50
|
+
throw new Error("TSGW 共享数据契约不兼容,请重启宿主以统一状态。");
|
|
51
|
+
return globals[REGISTRY] ??= {
|
|
52
|
+
schema: 1,
|
|
53
|
+
slots: new Map(),
|
|
54
|
+
cache: { entries: new Map(), inFlight: new Map(), latest: new Map(), latestDisk: new Map(), revision: 0 },
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -7,6 +7,8 @@ import {
|
|
|
7
7
|
writeFileSync,
|
|
8
8
|
} from "node:fs";
|
|
9
9
|
import { dirname, resolve } from "node:path";
|
|
10
|
+
import { parseGatewayModelList } from "./gateway-model-list.ts";
|
|
11
|
+
import { getGatewayCatalogRegistry } from "./gateway-catalog-shared.ts";
|
|
10
12
|
|
|
11
13
|
export const DEFAULT_GATEWAY_MODEL_TIMEOUT_MS = 5_000;
|
|
12
14
|
export const DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS = 24 * 60 * 60 * 1_000;
|
|
@@ -39,7 +41,7 @@ export interface GetGatewayModelIdsOptions extends FetchGatewayModelIdsOptions {
|
|
|
39
41
|
}
|
|
40
42
|
|
|
41
43
|
export type FetchGatewayModelIdsResult =
|
|
42
|
-
| { ok: true; ids: string[] }
|
|
44
|
+
| { ok: true; ids: string[]; magpie?: true }
|
|
43
45
|
| {
|
|
44
46
|
ok: false;
|
|
45
47
|
reason: GatewayModelFailureReason;
|
|
@@ -47,20 +49,23 @@ export type FetchGatewayModelIdsResult =
|
|
|
47
49
|
detail?: string;
|
|
48
50
|
};
|
|
49
51
|
|
|
52
|
+
/** 网关仅提供可选模型 ID;规格与使用策略由本地目录定义,其余字段只辅助缓存和附加能力。 */
|
|
50
53
|
export interface ModelCacheEntry {
|
|
51
54
|
ids: string[];
|
|
52
55
|
storedAt: number;
|
|
56
|
+
magpie?: true;
|
|
53
57
|
}
|
|
54
58
|
|
|
55
59
|
export interface ModelCache {
|
|
56
60
|
get(): ModelCacheEntry | undefined;
|
|
57
|
-
set(ids: readonly string[], now?: number): void;
|
|
61
|
+
set(ids: readonly string[], now?: number, magpie?: true): void;
|
|
58
62
|
isFresh(now?: number): boolean;
|
|
59
63
|
}
|
|
60
64
|
|
|
61
65
|
export interface LoadedGatewayModelCache {
|
|
62
66
|
ids: string[];
|
|
63
67
|
fresh: boolean;
|
|
68
|
+
magpie?: true;
|
|
64
69
|
}
|
|
65
70
|
|
|
66
71
|
export type CachedGatewayModelIds = ModelCacheEntry & { fresh: boolean };
|
|
@@ -77,6 +82,7 @@ export type GetGatewayModelIdsResult =
|
|
|
77
82
|
cached: boolean;
|
|
78
83
|
stale: boolean;
|
|
79
84
|
storedAt: number;
|
|
85
|
+
magpie?: true;
|
|
80
86
|
fallbackReason?: GatewayModelFailureReason;
|
|
81
87
|
fallbackStatus?: number;
|
|
82
88
|
}
|
|
@@ -96,16 +102,6 @@ function modelsUrl(baseUrl: string): string {
|
|
|
96
102
|
return `${baseUrl.replace(/\/+$/, "")}/v1/models`;
|
|
97
103
|
}
|
|
98
104
|
|
|
99
|
-
function uniqueModelIds(data: unknown[]): string[] {
|
|
100
|
-
const ids = new Set<string>();
|
|
101
|
-
for (const item of data) {
|
|
102
|
-
if (!item || typeof item !== "object") continue;
|
|
103
|
-
const id = (item as { id?: unknown }).id;
|
|
104
|
-
if (typeof id === "string" && id.trim()) ids.add(id.trim());
|
|
105
|
-
}
|
|
106
|
-
return Array.from(ids);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
105
|
/** 加载网关提供的 OpenAI 兼容模型列表。 */
|
|
110
106
|
export async function fetchGatewayModelIds(
|
|
111
107
|
baseUrl: string,
|
|
@@ -159,13 +155,7 @@ export async function fetchGatewayModelIds(
|
|
|
159
155
|
|
|
160
156
|
if (timedOut) return { ok: false, reason: "timeout" };
|
|
161
157
|
if (opts.signal?.aborted) return { ok: false, reason: "aborted" };
|
|
162
|
-
|
|
163
|
-
return { ok: false, reason: "invalid-data" };
|
|
164
|
-
const data = (payload as { data?: unknown }).data;
|
|
165
|
-
if (!Array.isArray(data)) return { ok: false, reason: "invalid-data" };
|
|
166
|
-
const ids = uniqueModelIds(data);
|
|
167
|
-
if (ids.length === 0) return { ok: false, reason: "empty" };
|
|
168
|
-
return { ok: true, ids };
|
|
158
|
+
return parseGatewayModelList(payload);
|
|
169
159
|
} finally {
|
|
170
160
|
clearTimeout(timeout);
|
|
171
161
|
opts.signal?.removeEventListener("abort", abortFromCaller);
|
|
@@ -182,11 +172,11 @@ export function createModelCache(
|
|
|
182
172
|
return {
|
|
183
173
|
get() {
|
|
184
174
|
return entry
|
|
185
|
-
? { ids: [...entry.ids]
|
|
175
|
+
? { ...entry, ids: [...entry.ids] }
|
|
186
176
|
: undefined;
|
|
187
177
|
},
|
|
188
|
-
set(ids, now = Date.now()) {
|
|
189
|
-
entry = { ids: [...ids], storedAt: now };
|
|
178
|
+
set(ids, now = Date.now(), magpie) {
|
|
179
|
+
entry = { ids: [...ids], storedAt: now, ...(magpie ? { magpie } : {}) };
|
|
190
180
|
},
|
|
191
181
|
isFresh(now = Date.now()) {
|
|
192
182
|
if (!entry) return false;
|
|
@@ -208,10 +198,11 @@ function readStoredGatewayModelCache(
|
|
|
208
198
|
try {
|
|
209
199
|
const payload: unknown = JSON.parse(readFileSync(filePath, "utf8"));
|
|
210
200
|
if (!payload || typeof payload !== "object") return undefined;
|
|
211
|
-
const { storedAt, ids, scope: storedScope } = payload as {
|
|
201
|
+
const { storedAt, ids, scope: storedScope, magpie } = payload as {
|
|
212
202
|
scope?: unknown;
|
|
213
203
|
storedAt?: unknown;
|
|
214
204
|
ids?: unknown;
|
|
205
|
+
magpie?: unknown;
|
|
215
206
|
};
|
|
216
207
|
if (
|
|
217
208
|
typeof storedScope !== "string" || !storedScope ||
|
|
@@ -226,6 +217,7 @@ function readStoredGatewayModelCache(
|
|
|
226
217
|
scope: storedScope,
|
|
227
218
|
storedAt,
|
|
228
219
|
ids: Array.from(new Set(ids.map((id) => id.trim()))),
|
|
220
|
+
...(magpie === true ? { magpie } : {}),
|
|
229
221
|
};
|
|
230
222
|
} catch {
|
|
231
223
|
return undefined;
|
|
@@ -238,7 +230,8 @@ function readGatewayModelCacheEntry(
|
|
|
238
230
|
): ModelCacheEntry | undefined {
|
|
239
231
|
const snapshot = readStoredGatewayModelCache(filePath);
|
|
240
232
|
if (!scope || snapshot?.scope !== scope) return undefined;
|
|
241
|
-
|
|
233
|
+
const { scope: _scope, ...entry } = snapshot;
|
|
234
|
+
return entry;
|
|
242
235
|
}
|
|
243
236
|
|
|
244
237
|
/** 刷新后的文件摘要;可同时核对当前配置及已应用的完整模型 ID 快照。 */
|
|
@@ -273,13 +266,14 @@ export function saveGatewayModelCache(
|
|
|
273
266
|
scope: string,
|
|
274
267
|
ids: readonly string[],
|
|
275
268
|
now = Date.now(),
|
|
269
|
+
magpie?: true,
|
|
276
270
|
): void {
|
|
277
271
|
const temporaryPath = `${filePath}.${process.pid}.${randomUUID()}.tmp`;
|
|
278
272
|
try {
|
|
279
273
|
mkdirSync(dirname(filePath), { recursive: true });
|
|
280
274
|
writeFileSync(
|
|
281
275
|
temporaryPath,
|
|
282
|
-
JSON.stringify({ scope, storedAt: now, ids: [...ids] }),
|
|
276
|
+
JSON.stringify({ scope, storedAt: now, ids: [...ids], ...(magpie ? { magpie } : {}) }),
|
|
283
277
|
{ encoding: "utf8", mode: 0o600 },
|
|
284
278
|
);
|
|
285
279
|
renameSync(temporaryPath, filePath);
|
|
@@ -305,32 +299,16 @@ export function loadGatewayModelCache(
|
|
|
305
299
|
return {
|
|
306
300
|
ids: [...entry.ids],
|
|
307
301
|
fresh: isCacheEntryFresh(entry, now, ttl),
|
|
302
|
+
...(entry.magpie ? { magpie: entry.magpie } : {}),
|
|
308
303
|
};
|
|
309
304
|
}
|
|
310
305
|
|
|
311
|
-
const gatewayCaches = new Map<string, ModelCache>();
|
|
312
|
-
interface PendingRequest {
|
|
313
|
-
promise: Promise<FetchGatewayModelIdsResult>;
|
|
314
|
-
revision: number;
|
|
315
|
-
storedAt: number;
|
|
316
|
-
}
|
|
317
|
-
const inFlightRequests = new Map<string, PendingRequest>();
|
|
318
|
-
const latestRequests = new Map<string, number>();
|
|
319
|
-
// 每个规范化路径仅有一个磁盘缓存槽,与内存缓存作用域相互独立。
|
|
320
|
-
const latestDiskRequests = new Map<string, number>();
|
|
321
|
-
let revision = 0;
|
|
322
|
-
|
|
323
306
|
function memoryCacheKey(scope: string, filePath?: string): string {
|
|
324
307
|
return filePath ? `${scope}\0${resolve(filePath)}` : scope;
|
|
325
308
|
}
|
|
326
309
|
|
|
327
|
-
function
|
|
328
|
-
|
|
329
|
-
if (!cache) {
|
|
330
|
-
cache = createModelCache();
|
|
331
|
-
gatewayCaches.set(key, cache);
|
|
332
|
-
}
|
|
333
|
-
return cache;
|
|
310
|
+
function storeMemoryCache(key: string, entry: ModelCacheEntry): void {
|
|
311
|
+
getGatewayCatalogRegistry().cache.entries.set(key, { ...entry, ids: [...entry.ids] });
|
|
334
312
|
}
|
|
335
313
|
|
|
336
314
|
function selectLatestGatewayModelCacheEntry(
|
|
@@ -339,7 +317,7 @@ function selectLatestGatewayModelCacheEntry(
|
|
|
339
317
|
ttlMs: number,
|
|
340
318
|
now: number,
|
|
341
319
|
): { entry: ModelCacheEntry; fresh: boolean } | undefined {
|
|
342
|
-
const memory =
|
|
320
|
+
const memory = getGatewayCatalogRegistry().cache.entries.get(memoryCacheKey(key, cacheFilePath));
|
|
343
321
|
const disk = cacheFilePath
|
|
344
322
|
? readGatewayModelCacheEntry(cacheFilePath, key)
|
|
345
323
|
: undefined;
|
|
@@ -362,7 +340,7 @@ export function getCachedGatewayModelIds(
|
|
|
362
340
|
): CachedGatewayModelIds | undefined {
|
|
363
341
|
const key = gatewayModelCacheScope(baseUrl, apiKey);
|
|
364
342
|
const cacheKey = memoryCacheKey(key, opts.cacheFilePath);
|
|
365
|
-
const memory =
|
|
343
|
+
const memory = getGatewayCatalogRegistry().cache.entries.get(cacheKey);
|
|
366
344
|
const selected = selectLatestGatewayModelCacheEntry(
|
|
367
345
|
key,
|
|
368
346
|
opts.cacheFilePath,
|
|
@@ -373,8 +351,9 @@ export function getCachedGatewayModelIds(
|
|
|
373
351
|
// 选中更新的磁盘快照时同步共享内存,避免磁盘随后损坏或删除时回退旧内存。
|
|
374
352
|
// 仅预热内存,不写磁盘,也不改变原始获取时间。
|
|
375
353
|
if (!memory || selected.entry.storedAt > memory.storedAt)
|
|
376
|
-
|
|
354
|
+
storeMemoryCache(cacheKey, selected.entry);
|
|
377
355
|
return {
|
|
356
|
+
...selected.entry,
|
|
378
357
|
ids: [...selected.entry.ids],
|
|
379
358
|
storedAt: selected.entry.storedAt,
|
|
380
359
|
fresh: selected.fresh,
|
|
@@ -397,13 +376,14 @@ export async function getGatewayModelIds(
|
|
|
397
376
|
const key = gatewayModelCacheScope(baseUrl, apiKey);
|
|
398
377
|
const ttlMs = checkedTtl(opts.ttlMs);
|
|
399
378
|
const cacheKey = memoryCacheKey(key, opts.cacheFilePath);
|
|
400
|
-
const cache =
|
|
379
|
+
const cache = getGatewayCatalogRegistry().cache;
|
|
401
380
|
const local = selectLatestGatewayModelCacheEntry(
|
|
402
381
|
key, opts.cacheFilePath, ttlMs, now,
|
|
403
382
|
);
|
|
404
383
|
if (!opts.force && local?.fresh) {
|
|
405
|
-
|
|
384
|
+
storeMemoryCache(cacheKey, local.entry);
|
|
406
385
|
return {
|
|
386
|
+
...local.entry,
|
|
407
387
|
ok: true,
|
|
408
388
|
ids: [...local.entry.ids],
|
|
409
389
|
cached: true,
|
|
@@ -417,25 +397,25 @@ export async function getGatewayModelIds(
|
|
|
417
397
|
// Pi 可能已替换该次刷新。只有不带信号的调用共享进行中的请求;
|
|
418
398
|
// 可取消的调用独立发起获取请求。
|
|
419
399
|
const shareRequest = opts.signal === undefined;
|
|
420
|
-
let request = shareRequest ?
|
|
400
|
+
let request = shareRequest ? cache.inFlight.get(requestKey) : undefined;
|
|
421
401
|
if (!request) {
|
|
422
|
-
const requestRevision = ++revision;
|
|
402
|
+
const requestRevision = ++cache.revision;
|
|
423
403
|
request = {
|
|
424
404
|
revision: requestRevision,
|
|
425
405
|
storedAt: now,
|
|
426
406
|
promise: fetchGatewayModelIds(baseUrl, apiKey, opts),
|
|
427
407
|
};
|
|
428
|
-
if (shareRequest)
|
|
408
|
+
if (shareRequest) cache.inFlight.set(requestKey, request);
|
|
429
409
|
}
|
|
430
410
|
|
|
431
411
|
// 请求可跨路径复用,但内存写入屏障按各自路径独立登记。
|
|
432
|
-
|
|
412
|
+
cache.latest.set(cacheKey, Math.max(cache.latest.get(cacheKey) ?? 0, request.revision));
|
|
433
413
|
const diskPath = opts.cacheFilePath ? resolve(opts.cacheFilePath) : undefined;
|
|
434
414
|
if (diskPath) {
|
|
435
415
|
// 登记每个订阅者的路径,包括目标路径不同的共享请求。
|
|
436
416
|
// 加入较旧请求时,不得回退较新的版本屏障。
|
|
437
|
-
|
|
438
|
-
|
|
417
|
+
cache.latestDisk.set(diskPath, Math.max(
|
|
418
|
+
cache.latestDisk.get(diskPath) ?? 0, request.revision,
|
|
439
419
|
));
|
|
440
420
|
}
|
|
441
421
|
|
|
@@ -443,8 +423,8 @@ export async function getGatewayModelIds(
|
|
|
443
423
|
try {
|
|
444
424
|
fetched = await request.promise;
|
|
445
425
|
} finally {
|
|
446
|
-
if (shareRequest &&
|
|
447
|
-
|
|
426
|
+
if (shareRequest && cache.inFlight.get(requestKey) === request)
|
|
427
|
+
cache.inFlight.delete(requestKey);
|
|
448
428
|
}
|
|
449
429
|
|
|
450
430
|
// 取消后绝不回退或写入,即使获取器忽略了取消信号。
|
|
@@ -452,13 +432,13 @@ export async function getGatewayModelIds(
|
|
|
452
432
|
if (fetched.ok) {
|
|
453
433
|
// 以后启动的请求为准,即使较新的请求失败或取消。
|
|
454
434
|
// 被取代的成功请求仍向调用者返回结果,但不写入缓存。
|
|
455
|
-
if (
|
|
456
|
-
|
|
457
|
-
if (diskPath &&
|
|
458
|
-
saveGatewayModelCache(diskPath, key, fetched.ids, request.storedAt);
|
|
435
|
+
if (cache.latest.get(cacheKey) === request.revision) {
|
|
436
|
+
storeMemoryCache(cacheKey, { ids: fetched.ids, storedAt: request.storedAt, ...(fetched.magpie ? { magpie: fetched.magpie } : {}) });
|
|
437
|
+
if (diskPath && cache.latestDisk.get(diskPath) === request.revision)
|
|
438
|
+
saveGatewayModelCache(diskPath, key, fetched.ids, request.storedAt, fetched.magpie);
|
|
459
439
|
}
|
|
460
440
|
return {
|
|
461
|
-
|
|
441
|
+
...fetched,
|
|
462
442
|
ids: [...fetched.ids],
|
|
463
443
|
cached: false,
|
|
464
444
|
stale: false,
|
|
@@ -472,6 +452,7 @@ export async function getGatewayModelIds(
|
|
|
472
452
|
);
|
|
473
453
|
if (fallback) {
|
|
474
454
|
return {
|
|
455
|
+
...fallback.entry,
|
|
475
456
|
ok: true,
|
|
476
457
|
ids: [...fallback.entry.ids],
|
|
477
458
|
storedAt: fallback.entry.storedAt,
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { FetchGatewayModelIdsResult } from "./gateway-catalog.ts";
|
|
2
|
+
|
|
3
|
+
/** 只用目录项的 Magpie 特征做简单识别,不承担网关身份认证。 */
|
|
4
|
+
export function parseGatewayModelList(payload: unknown): FetchGatewayModelIdsResult {
|
|
5
|
+
if (!payload || typeof payload !== "object")
|
|
6
|
+
return { ok: false, reason: "invalid-data" };
|
|
7
|
+
const data = (payload as { data?: unknown }).data;
|
|
8
|
+
if (!Array.isArray(data)) return { ok: false, reason: "invalid-data" };
|
|
9
|
+
const ids = new Set<string>();
|
|
10
|
+
let magpie: true | undefined;
|
|
11
|
+
for (const item of data) {
|
|
12
|
+
if (!item || typeof item !== "object") continue;
|
|
13
|
+
const { id, magpie_label: label } = item as { id?: unknown; magpie_label?: unknown };
|
|
14
|
+
if (typeof id !== "string" || !id.trim()) continue;
|
|
15
|
+
ids.add(id.trim());
|
|
16
|
+
if (typeof label === "string") magpie = true;
|
|
17
|
+
}
|
|
18
|
+
if (!ids.size) return { ok: false, reason: "empty" };
|
|
19
|
+
return { ok: true, ids: [...ids], ...(magpie ? { magpie } : {}) };
|
|
20
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ProviderModelConfig } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { effectiveModelsForGateway, type ModelFilter } from "#tsgw/models/catalog";
|
|
3
3
|
import { DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS } from "#tsgw/models/gateway-catalog";
|
|
4
|
+
import { getGatewayCatalogRegistry } from "#tsgw/models/gateway-catalog-shared";
|
|
4
5
|
import {
|
|
5
6
|
catalogCancelledError,
|
|
6
7
|
getGatewayCatalogService,
|
|
@@ -39,6 +40,8 @@ export function createCatalogState(options: {
|
|
|
39
40
|
initialKey?: string;
|
|
40
41
|
onChange(): void;
|
|
41
42
|
}) {
|
|
43
|
+
// 破坏性契约变更在加载入口明确提示,避免无初始凭据时被后续刷新错误吞掉。
|
|
44
|
+
getGatewayCatalogRegistry();
|
|
42
45
|
const { root, filter, cacheFilePath, imagesEnabled, classifiersEnabled } = options;
|
|
43
46
|
let models = effectiveModelsForGateway(root, filter, undefined, imagesEnabled, classifiersEnabled);
|
|
44
47
|
let status: CatalogStatus = { count: models.length, source: "empty", loading: false };
|
|
@@ -77,6 +80,9 @@ export function createCatalogState(options: {
|
|
|
77
80
|
|
|
78
81
|
return {
|
|
79
82
|
models: () => models,
|
|
83
|
+
/** 从进程共享服务读取,其他实例刷新后立即生效。 */
|
|
84
|
+
isMagpie: () => service?.isMagpie() ?? false,
|
|
85
|
+
gateway: () => service,
|
|
80
86
|
status: () => status,
|
|
81
87
|
outcome(failed = false): GatewayCatalogOutcome {
|
|
82
88
|
return { ...(service?.peek() ? { snapshot: service.peek() } : {}),
|
|
@@ -84,7 +90,7 @@ export function createCatalogState(options: {
|
|
|
84
90
|
},
|
|
85
91
|
matches(entry?: GatewayCatalogSnapshot): boolean {
|
|
86
92
|
const current = snapshot;
|
|
87
|
-
return !!entry && !!current && entry.storedAt === current.storedAt &&
|
|
93
|
+
return !!entry && !!current && entry.storedAt === current.storedAt && entry.magpie === current.magpie &&
|
|
88
94
|
entry.ids.length === current.ids.length && entry.ids.every((id, index) => id === current.ids[index]);
|
|
89
95
|
},
|
|
90
96
|
recoverUsing(run: (signal: AbortSignal) => Promise<GatewayCatalogOutcome>, signal?: AbortSignal) {
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
} from "@earendil-works/pi-coding-agent";
|
|
12
12
|
import { join } from "node:path";
|
|
13
13
|
import { registerGatewayTracing } from "./tracing.ts";
|
|
14
|
+
import { registerMagpie } from "#tsgw/magpie/controller";
|
|
14
15
|
import {
|
|
15
16
|
DEFAULT_ROOT,
|
|
16
17
|
PROVIDER_ID,
|
|
@@ -129,6 +130,8 @@ export async function registerTsgwProvider(
|
|
|
129
130
|
});
|
|
130
131
|
latestModels = directory.models();
|
|
131
132
|
catalog = directory.status();
|
|
133
|
+
const magpie = registerMagpie(pi, { root, gateway: directory.gateway,
|
|
134
|
+
onChange: () => publishStatus?.() });
|
|
132
135
|
const coordinator = createCatalogCoordinator({
|
|
133
136
|
begin: () => directory.beginRefresh(),
|
|
134
137
|
inspect: () => ({ networkAttempted: directory.networkAttempted(),
|
|
@@ -171,8 +174,9 @@ export async function registerTsgwProvider(
|
|
|
171
174
|
const ui = ctx.ui;
|
|
172
175
|
notify = message => ui.notify(message, "error");
|
|
173
176
|
publishStatus = () => {
|
|
174
|
-
ui.setStatus("command:/tsgw",
|
|
175
|
-
catalogLabel(catalog, credentialConfigured, rootConfigured))
|
|
177
|
+
ui.setStatus("command:/tsgw", [
|
|
178
|
+
catalogLabel(catalog, credentialConfigured, rootConfigured), magpie.label(),
|
|
179
|
+
].filter(Boolean).join(" · "));
|
|
176
180
|
};
|
|
177
181
|
publishStatus();
|
|
178
182
|
};
|
|
@@ -242,14 +246,15 @@ export async function registerTsgwProvider(
|
|
|
242
246
|
handler: async (_args, ctx) => refreshCatalog(ctx),
|
|
243
247
|
});
|
|
244
248
|
pi.registerCommand("tsgw", {
|
|
245
|
-
description: "TSGW
|
|
249
|
+
description: "TSGW 模型目录、Magpie 状态与额度",
|
|
246
250
|
handler: async (_args, ctx) => {
|
|
247
251
|
if (!ctx.hasUI) return;
|
|
248
252
|
const lifecycle = lifecycleRevision;
|
|
249
253
|
bindStatus(ctx);
|
|
250
|
-
const choice = await ctx.ui.select("TSGW", ["刷新模型目录", "查看状态与诊断"]);
|
|
254
|
+
const choice = await ctx.ui.select("TSGW", ["刷新模型目录", "查看状态与诊断", ...magpie.choices()]);
|
|
251
255
|
if (lifecycle !== lifecycleRevision) return;
|
|
252
256
|
if (choice === "刷新模型目录") await refreshCatalog(ctx);
|
|
257
|
+
if (choice && magpie.choices().includes(choice)) await magpie.handle(choice, ctx);
|
|
253
258
|
if (choice === "查看状态与诊断") {
|
|
254
259
|
bindStatus(ctx);
|
|
255
260
|
const diagnostics = catalogDiagnostics(catalog,
|