@evandu/super-pi 0.1.279 → 0.1.281
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/dist/.super-pi-check-inputs.json +2 -2
- package/dist/client/.super-pi-client-sources.json +9 -7
- package/dist/client/assets/{CodeMirrorEditor-D-UuYizz.js → CodeMirrorEditor-xxJP_tVy.js} +1 -1
- package/dist/client/assets/{GitDiffEditor-Bt1n2la-.js → GitDiffEditor-X8eCsha9.js} +1 -1
- package/dist/client/assets/index-DaKloC7K.css +1 -0
- package/dist/client/assets/{index-rWE1xu-t.js → index-sWsdT0fC.js} +24 -24
- package/dist/client/index.html +2 -2
- package/dist/server/application/provider-service.d.ts +27 -11
- package/dist/server/application/provider-service.js +78 -10
- package/dist/server/application/rsi-service.d.ts +2 -0
- package/dist/server/application/rsi-service.js +79 -11
- package/dist/server/http-server.js +23 -0
- package/dist/server/index.js +4 -0
- package/dist/server/model-discovery.d.ts +44 -0
- package/dist/server/model-discovery.js +139 -0
- package/dist/server/pi-session.d.ts +25 -2
- package/dist/server/pi-session.js +30 -1
- package/dist/server/provider-proxy.d.ts +2 -0
- package/dist/server/provider-proxy.js +1 -1
- package/dist/server/providers.d.ts +6 -0
- package/dist/server/providers.js +76 -10
- package/dist/server/runtime.d.ts +2 -0
- package/dist/server/runtime.js +1 -0
- package/dist/shared/group-chat.d.ts +2 -0
- package/dist/shared/model-discovery.d.ts +15 -0
- package/dist/shared/model-discovery.js +1 -0
- package/dist/shared/tab-layout.d.ts +12 -0
- package/dist/shared/tab-layout.js +28 -0
- package/package.json +5 -1
- package/dist/client/assets/index-B5TLGyEw.css +0 -1
package/dist/client/index.html
CHANGED
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
<link rel="alternate icon" href="/favicon.ico" />
|
|
9
9
|
<link rel="apple-touch-icon" href="/favicon.svg" />
|
|
10
10
|
<title>Super Pi</title>
|
|
11
|
-
<script type="module" crossorigin src="/assets/index-
|
|
11
|
+
<script type="module" crossorigin src="/assets/index-sWsdT0fC.js"></script>
|
|
12
12
|
<link rel="modulepreload" crossorigin href="/assets/rolldown-runtime-XKyaHtl9.js">
|
|
13
|
-
<link rel="stylesheet" crossorigin href="/assets/index-
|
|
13
|
+
<link rel="stylesheet" crossorigin href="/assets/index-DaKloC7K.css">
|
|
14
14
|
</head>
|
|
15
15
|
<body>
|
|
16
16
|
<div id="root"></div>
|
|
@@ -4,6 +4,8 @@ import type { Store } from "../store.js";
|
|
|
4
4
|
import type { DiscoveredModel, PublicProvider } from "../types.js";
|
|
5
5
|
import type { EventBroadcaster } from "../server-context.js";
|
|
6
6
|
import type { CatalogService } from "./catalog-service.js";
|
|
7
|
+
import { ModelDiscovery } from "../model-discovery.js";
|
|
8
|
+
import { discoverPiProviderModels } from "../pi-session.js";
|
|
7
9
|
/**
|
|
8
10
|
* THE PROVIDERS A PERSON ADDED, as a service.
|
|
9
11
|
*
|
|
@@ -32,8 +34,25 @@ export declare class ProviderService {
|
|
|
32
34
|
private readonly broadcaster;
|
|
33
35
|
private readonly catalogService;
|
|
34
36
|
private readonly store;
|
|
37
|
+
private changes;
|
|
38
|
+
private revision;
|
|
39
|
+
readonly discovery: ModelDiscovery;
|
|
40
|
+
private scope;
|
|
41
|
+
private mutate;
|
|
42
|
+
private discoveryTargets;
|
|
43
|
+
private applyDiscovered;
|
|
44
|
+
disable(providerId: string): Promise<PublicProvider[]>;
|
|
45
|
+
enable(providerId: string): Promise<PublicProvider[]>;
|
|
46
|
+
repair(): Promise<{
|
|
47
|
+
fixed: string[];
|
|
48
|
+
providers: PublicProvider[];
|
|
49
|
+
}>;
|
|
50
|
+
setNetwork(input: unknown): Promise<import("../../shared/provider-network.js").SharedProviderProxySettings>;
|
|
51
|
+
set(input: unknown): Promise<PublicProvider[]>;
|
|
52
|
+
remove(providerId: string): Promise<PublicProvider[]>;
|
|
53
|
+
removeModel(providerId: string, modelId: string): Promise<PublicProvider[]>;
|
|
35
54
|
private readonly oauthJobs;
|
|
36
|
-
constructor(registry: ProviderRegistry, runtime: AgentRuntime, broadcaster: EventBroadcaster, catalogService: CatalogService, store: Store);
|
|
55
|
+
constructor(registry: ProviderRegistry, runtime: AgentRuntime, broadcaster: EventBroadcaster, catalogService: CatalogService, store: Store, discoverModels?: typeof discoverPiProviderModels);
|
|
37
56
|
/**
|
|
38
57
|
* Every provider Pi knows about, as the browser may see it: no keys, only the fact of one.
|
|
39
58
|
*
|
|
@@ -77,8 +96,8 @@ export declare class ProviderService {
|
|
|
77
96
|
* picker names one model while another one answers. Measured before this change: removing the
|
|
78
97
|
* model in use left the picker empty (`source=unavailable, models=0`).
|
|
79
98
|
*/
|
|
80
|
-
|
|
81
|
-
|
|
99
|
+
private disableNow;
|
|
100
|
+
private enableNow;
|
|
82
101
|
/**
|
|
83
102
|
* Repairs Pi's model configuration: the empty fields Pi's schema refuses are dropped, and nothing
|
|
84
103
|
* else in the file is touched.
|
|
@@ -89,12 +108,9 @@ export declare class ProviderService {
|
|
|
89
108
|
* restores the file Pi will accept, which is the difference between "my provider is gone" and
|
|
90
109
|
* "your file had an empty field".
|
|
91
110
|
*/
|
|
92
|
-
|
|
93
|
-
fixed: string[];
|
|
94
|
-
providers: PublicProvider[];
|
|
95
|
-
}>;
|
|
111
|
+
private repairNow;
|
|
96
112
|
network(): Promise<import("../../shared/provider-network.js").SharedProviderProxySettings>;
|
|
97
|
-
|
|
113
|
+
private setNetworkNow;
|
|
98
114
|
/**
|
|
99
115
|
* Adds a provider, or adds a model to one, or updates one.
|
|
100
116
|
*
|
|
@@ -108,7 +124,7 @@ export declare class ProviderService {
|
|
|
108
124
|
* is the normal state of a form for a provider that already has one. `clearKey` is how a person
|
|
109
125
|
* asks for it to go.
|
|
110
126
|
*/
|
|
111
|
-
|
|
127
|
+
private setNow;
|
|
112
128
|
/**
|
|
113
129
|
* Removes a provider for good.
|
|
114
130
|
*
|
|
@@ -120,7 +136,7 @@ export declare class ProviderService {
|
|
|
120
136
|
* - one that is already switched off: the remembered copy goes, which is what makes "off"
|
|
121
137
|
* permanent instead of something that would come back months later.
|
|
122
138
|
*/
|
|
123
|
-
|
|
139
|
+
private removeNow;
|
|
124
140
|
/**
|
|
125
141
|
* Removes ONE model from a provider, leaving the provider and its other models alone.
|
|
126
142
|
*
|
|
@@ -134,7 +150,7 @@ export declare class ProviderService {
|
|
|
134
150
|
* credential, because a credential for a provider that no longer exists is a secret left lying in
|
|
135
151
|
* a file for nothing.
|
|
136
152
|
*/
|
|
137
|
-
|
|
153
|
+
private removeModelNow;
|
|
138
154
|
/**
|
|
139
155
|
* What has to happen after the files are written, for the change to be REAL rather than pending.
|
|
140
156
|
*
|
|
@@ -4,6 +4,8 @@ import { piAgentDir } from "../providers.js";
|
|
|
4
4
|
import { hasStoredCredential } from "../provider-network.js";
|
|
5
5
|
import { thinkingLevelsEndpoint } from "../../shared/thinking-levels.js";
|
|
6
6
|
import { randomUUID } from "node:crypto";
|
|
7
|
+
import { ModelDiscovery } from "../model-discovery.js";
|
|
8
|
+
import { discoverPiProviderModels } from "../pi-session.js";
|
|
7
9
|
/**
|
|
8
10
|
* THE PROVIDERS A PERSON ADDED, as a service.
|
|
9
11
|
*
|
|
@@ -32,13 +34,73 @@ export class ProviderService {
|
|
|
32
34
|
broadcaster;
|
|
33
35
|
catalogService;
|
|
34
36
|
store;
|
|
37
|
+
changes = Promise.resolve();
|
|
38
|
+
revision = 0;
|
|
39
|
+
discovery;
|
|
40
|
+
scope(provider) { return JSON.stringify([provider.id, provider.baseUrl, provider.api]); }
|
|
41
|
+
mutate(operation) {
|
|
42
|
+
const task = this.changes.catch(() => undefined).then(operation);
|
|
43
|
+
this.changes = task;
|
|
44
|
+
return task;
|
|
45
|
+
}
|
|
46
|
+
async discoveryTargets() {
|
|
47
|
+
const configured = await this.registry.load();
|
|
48
|
+
const builtin = await this.registry.loadCredentialOnly();
|
|
49
|
+
const catalog = builtin.length ? await this.runtime.catalog().catch(() => undefined) : undefined;
|
|
50
|
+
return [...configured, ...builtin].filter(provider => !provider.disabled && !provider.invalidFields?.length && (provider.builtin || provider.hasAuthKey || provider.keyInModelsJson || /ollama|localhost|127\.0\.0\.1/.test(provider.baseUrl))).map(provider => ({
|
|
51
|
+
id: provider.id, name: provider.name ?? provider.id, baseUrl: provider.baseUrl, api: provider.api,
|
|
52
|
+
scope: this.scope(provider), revision: this.revision, builtin: provider.builtin === true || !provider.baseUrl,
|
|
53
|
+
knownIds: provider.builtin ? catalog?.models.filter(model => model.provider === provider.id).map(model => model.modelId) : provider.models.map(model => model.id),
|
|
54
|
+
}));
|
|
55
|
+
}
|
|
56
|
+
async applyDiscovered(target, models) {
|
|
57
|
+
return this.mutate(async () => {
|
|
58
|
+
if (target.revision !== this.revision || !(await this.discoveryTargets()).some(current => current.scope === target.scope))
|
|
59
|
+
return undefined;
|
|
60
|
+
if (target.builtin)
|
|
61
|
+
return models.filter(model => !target.knownIds?.includes(model.id)).length;
|
|
62
|
+
const previous = await this.registry.load();
|
|
63
|
+
const provider = previous.find(item => item.id === target.id);
|
|
64
|
+
if (!provider)
|
|
65
|
+
return undefined;
|
|
66
|
+
const known = new Set(provider.models.map(model => model.id));
|
|
67
|
+
const added = models.filter(model => !known.has(model.id));
|
|
68
|
+
if (!added.length)
|
|
69
|
+
return 0;
|
|
70
|
+
const updated = { ...provider, models: [...provider.models, ...added] };
|
|
71
|
+
const next = previous.map(item => item.id === provider.id ? updated : item);
|
|
72
|
+
// Only the target is rewritten. Preserve raw headers, model overrides and credentials.
|
|
73
|
+
const rollback = await this.registry.appendDiscovered(provider, added);
|
|
74
|
+
try {
|
|
75
|
+
await this.registry.save(next);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
await rollback();
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
return added.length;
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
async disable(providerId) { return this.mutate(() => this.disableNow(providerId)); }
|
|
85
|
+
async enable(providerId) { return this.mutate(() => this.enableNow(providerId)); }
|
|
86
|
+
async repair() { return this.mutate(() => this.repairNow()); }
|
|
87
|
+
async setNetwork(input) { return this.mutate(() => this.setNetworkNow(input)); }
|
|
88
|
+
async set(input) { return this.mutate(() => this.setNow(input)); }
|
|
89
|
+
async remove(providerId) { return this.mutate(() => this.removeNow(providerId)); }
|
|
90
|
+
async removeModel(providerId, modelId) { return this.mutate(() => this.removeModelNow(providerId, modelId)); }
|
|
35
91
|
oauthJobs = new Map();
|
|
36
|
-
constructor(registry, runtime, broadcaster, catalogService, store) {
|
|
92
|
+
constructor(registry, runtime, broadcaster, catalogService, store, discoverModels = discoverPiProviderModels) {
|
|
37
93
|
this.registry = registry;
|
|
38
94
|
this.runtime = runtime;
|
|
39
95
|
this.broadcaster = broadcaster;
|
|
40
96
|
this.catalogService = catalogService;
|
|
41
97
|
this.store = store;
|
|
98
|
+
this.discovery = new ModelDiscovery(registry.discoveryPath, {
|
|
99
|
+
targets: () => this.discoveryTargets(),
|
|
100
|
+
discover: (target, signal, force) => discoverModels({ ...target, agentDir: registry.agentDir, signal, force }),
|
|
101
|
+
apply: (target, models) => this.applyDiscovered(target, models),
|
|
102
|
+
changed: () => this.afterChange([], false),
|
|
103
|
+
});
|
|
42
104
|
}
|
|
43
105
|
/**
|
|
44
106
|
* Every provider Pi knows about, as the browser may see it: no keys, only the fact of one.
|
|
@@ -211,7 +273,7 @@ export class ProviderService {
|
|
|
211
273
|
* picker names one model while another one answers. Measured before this change: removing the
|
|
212
274
|
* model in use left the picker empty (`source=unavailable, models=0`).
|
|
213
275
|
*/
|
|
214
|
-
async
|
|
276
|
+
async disableNow(providerId) {
|
|
215
277
|
const active = await this.registry.load();
|
|
216
278
|
const known = active.find((provider) => provider.id === providerId);
|
|
217
279
|
let models = known?.models ?? [];
|
|
@@ -241,7 +303,7 @@ export class ProviderService {
|
|
|
241
303
|
await this.afterChange([{ provider: providerId }]);
|
|
242
304
|
return this.list();
|
|
243
305
|
}
|
|
244
|
-
async
|
|
306
|
+
async enableNow(providerId) {
|
|
245
307
|
await this.registry.enable(providerId);
|
|
246
308
|
await this.afterChange();
|
|
247
309
|
return this.list();
|
|
@@ -256,14 +318,14 @@ export class ProviderService {
|
|
|
256
318
|
* restores the file Pi will accept, which is the difference between "my provider is gone" and
|
|
257
319
|
* "your file had an empty field".
|
|
258
320
|
*/
|
|
259
|
-
async
|
|
321
|
+
async repairNow() {
|
|
260
322
|
const result = await this.registry.repairModelsConfig();
|
|
261
323
|
if (result.changed)
|
|
262
324
|
await this.afterChange();
|
|
263
325
|
return { fixed: result.fixed, providers: await this.list() };
|
|
264
326
|
}
|
|
265
327
|
async network() { return this.registry.sharedNetwork(await this.list()); }
|
|
266
|
-
async
|
|
328
|
+
async setNetworkNow(input) {
|
|
267
329
|
const settings = await this.registry.setSharedNetwork(input, await this.list());
|
|
268
330
|
await this.afterChange();
|
|
269
331
|
return settings;
|
|
@@ -281,7 +343,7 @@ export class ProviderService {
|
|
|
281
343
|
* is the normal state of a form for a provider that already has one. `clearKey` is how a person
|
|
282
344
|
* asks for it to go.
|
|
283
345
|
*/
|
|
284
|
-
async
|
|
346
|
+
async setNow(input) {
|
|
285
347
|
const requested = validateProvider(input);
|
|
286
348
|
const raw = (input ?? {});
|
|
287
349
|
const previous = await this.registry.load();
|
|
@@ -370,6 +432,7 @@ export class ProviderService {
|
|
|
370
432
|
await this.registry.applyToPi(next, restore).catch(() => undefined);
|
|
371
433
|
throw error;
|
|
372
434
|
}
|
|
435
|
+
await this.discovery.include(this.scope(merged), requested.models.map(model => model.id));
|
|
373
436
|
await this.afterChange();
|
|
374
437
|
return this.list();
|
|
375
438
|
}
|
|
@@ -384,7 +447,7 @@ export class ProviderService {
|
|
|
384
447
|
* - one that is already switched off: the remembered copy goes, which is what makes "off"
|
|
385
448
|
* permanent instead of something that would come back months later.
|
|
386
449
|
*/
|
|
387
|
-
async
|
|
450
|
+
async removeNow(providerId) {
|
|
388
451
|
const previous = await this.registry.load();
|
|
389
452
|
const existing = previous.find((provider) => provider.id === providerId);
|
|
390
453
|
if (!existing) {
|
|
@@ -424,7 +487,7 @@ export class ProviderService {
|
|
|
424
487
|
* credential, because a credential for a provider that no longer exists is a secret left lying in
|
|
425
488
|
* a file for nothing.
|
|
426
489
|
*/
|
|
427
|
-
async
|
|
490
|
+
async removeModelNow(providerId, modelId) {
|
|
428
491
|
const previous = await this.registry.load();
|
|
429
492
|
const existing = previous.find((provider) => provider.id === providerId);
|
|
430
493
|
if (!existing)
|
|
@@ -432,8 +495,9 @@ export class ProviderService {
|
|
|
432
495
|
const remaining = existing.models.filter((model) => model.id !== modelId);
|
|
433
496
|
if (remaining.length === existing.models.length)
|
|
434
497
|
throw new Error(`供应商「${providerId}」里没有模型「${modelId}」`);
|
|
498
|
+
await this.discovery.exclude(this.scope(existing), modelId);
|
|
435
499
|
if (!remaining.length)
|
|
436
|
-
return this.
|
|
500
|
+
return this.removeNow(providerId);
|
|
437
501
|
const next = previous.map((provider) => (provider.id === providerId ? { ...provider, models: remaining } : provider));
|
|
438
502
|
await this.registry.applyToPi(previous, next);
|
|
439
503
|
await this.registry.save(next);
|
|
@@ -448,7 +512,11 @@ export class ProviderService {
|
|
|
448
512
|
* UNFILTERED one, and handing that to a conversation that has model rules would offer it models
|
|
449
513
|
* those rules forbid.
|
|
450
514
|
*/
|
|
451
|
-
async afterChange(removed = []) {
|
|
515
|
+
async afterChange(removed = [], discover = true) {
|
|
516
|
+
if (discover)
|
|
517
|
+
this.revision++;
|
|
518
|
+
if (discover)
|
|
519
|
+
this.discovery.request();
|
|
452
520
|
this.runtime.invalidateCatalog?.();
|
|
453
521
|
this.runtime.bumpModelEpoch?.();
|
|
454
522
|
await this.repairConversationModels(removed);
|
|
@@ -28,6 +28,8 @@ export declare class RsiService {
|
|
|
28
28
|
static open(directory: string, store: Store, runtime: AgentRuntime, tools: UserTools, hooks: Hooks): Promise<RsiService>;
|
|
29
29
|
private save;
|
|
30
30
|
private source;
|
|
31
|
+
private unavailable;
|
|
32
|
+
private active;
|
|
31
33
|
state(source: RsiSource): GroupRsi;
|
|
32
34
|
snapshot(source: RsiSource, groupState?: GroupRsi): RsiSnapshot;
|
|
33
35
|
workerJob(id: string): RsiImprovement | undefined;
|
|
@@ -39,11 +39,16 @@ export class RsiService {
|
|
|
39
39
|
if (error.code !== "ENOENT")
|
|
40
40
|
throw error;
|
|
41
41
|
}
|
|
42
|
-
for (const state of Object.values(service.data.sessions))
|
|
42
|
+
for (const state of Object.values(service.data.sessions)) {
|
|
43
43
|
if (state.status === "running") {
|
|
44
44
|
state.status = "interrupted";
|
|
45
45
|
state.error = "服务重启,观察已中断;可重新观察。";
|
|
46
46
|
}
|
|
47
|
+
if (state.pending)
|
|
48
|
+
state.notice = "服务重启,上次等待中的观察未执行;可重新观察。";
|
|
49
|
+
state.pending = false;
|
|
50
|
+
delete state.requestedAt;
|
|
51
|
+
}
|
|
47
52
|
for (const job of service.data.improvements)
|
|
48
53
|
if (["running", "queued"].includes(job.status)) {
|
|
49
54
|
job.status = "interrupted";
|
|
@@ -75,7 +80,30 @@ export class RsiService {
|
|
|
75
80
|
throw new Error("RSI 来源无效。");
|
|
76
81
|
return rsiKey(source);
|
|
77
82
|
}
|
|
78
|
-
|
|
83
|
+
unavailable(source) {
|
|
84
|
+
if (this.closed)
|
|
85
|
+
return "服务正在关闭,请重新连接后再观察。";
|
|
86
|
+
if (this.store.workspace(source.workspaceId)?.closedAt)
|
|
87
|
+
return "工作区已关闭,不能启动观察。";
|
|
88
|
+
if (source.kind !== "session")
|
|
89
|
+
return;
|
|
90
|
+
const conversation = this.store.conversation(source.id);
|
|
91
|
+
if (conversation.archivedAt)
|
|
92
|
+
return "会话已归档,请恢复后再观察。";
|
|
93
|
+
if (conversation.agentId === "rsi-builder")
|
|
94
|
+
return "RSI 不观察自身的能力开发会话,避免递归启动。";
|
|
95
|
+
if (conversation.parentConversationId)
|
|
96
|
+
return "当前是子会话,请在所属顶层会话中观察完整任务过程。";
|
|
97
|
+
if (!this.active(source) && !conversation.messages.some(message => message.role === "assistant" && (message.content?.trim() || message.toolCalls?.length || conversation.error)))
|
|
98
|
+
return "还没有 Agent 回复或工具执行过程;完成一次任务后再更新观察。";
|
|
99
|
+
}
|
|
100
|
+
active(source) { return ["running", "waiting", "awaiting_background", "queued"].includes(this.store.conversation(source.id)?.status ?? ""); }
|
|
101
|
+
state(source) {
|
|
102
|
+
const key = this.source(source), state = structuredClone(this.data.sessions[key] ?? defaultGroupRsi());
|
|
103
|
+
if (!state.reports.length && !state.pending && state.status === "idle")
|
|
104
|
+
state.notice ??= this.unavailable(source) ?? "还没有观察报告。点击更新观察,RSI 会检查已有任务过程。";
|
|
105
|
+
return state;
|
|
106
|
+
}
|
|
79
107
|
snapshot(source, groupState) {
|
|
80
108
|
const key = this.source(source);
|
|
81
109
|
const improvements = this.data.improvements.filter(job => rsiKey(job.source) === key);
|
|
@@ -102,14 +130,21 @@ export class RsiService {
|
|
|
102
130
|
timings[key] = [...(timings[key] ?? []).slice(-11), evidence];
|
|
103
131
|
void this.save().catch(error => console.error("RSI timing persistence:", error));
|
|
104
132
|
}
|
|
105
|
-
|
|
133
|
+
const source = { kind: "session", id, workspaceId: conversation.workspaceId };
|
|
134
|
+
this.schedule(source, this.data.sessions[rsiKey(source)]?.pending === true);
|
|
106
135
|
}
|
|
107
136
|
schedule(source, force = false) {
|
|
108
137
|
if (this.store.workspace(source.workspaceId)?.closedAt)
|
|
109
138
|
return;
|
|
110
139
|
const key = this.source(source), state = this.data.sessions[key] ?? defaultGroupRsi();
|
|
111
|
-
if (this.closed || state.enabled === false || this.observing.has(key)
|
|
140
|
+
if (this.closed || state.enabled === false || this.observing.has(key))
|
|
112
141
|
return;
|
|
142
|
+
if (this.timers.has(key)) {
|
|
143
|
+
if (!force)
|
|
144
|
+
return;
|
|
145
|
+
clearTimeout(this.timers.get(key));
|
|
146
|
+
this.timers.delete(key);
|
|
147
|
+
}
|
|
113
148
|
const timer = setTimeout(() => {
|
|
114
149
|
this.timers.delete(key);
|
|
115
150
|
const work = this.observe(source).catch(error => console.error("RSI observation:", error));
|
|
@@ -138,12 +173,24 @@ export class RsiService {
|
|
|
138
173
|
return { id: source.id, workspaceId: source.workspaceId, title: conversation.title, updatedAt: conversation.updatedAt, revision: 0, members: [], moderator: { prompt: "" }, messages, executions };
|
|
139
174
|
}
|
|
140
175
|
async observe(source) {
|
|
141
|
-
if (this.closed
|
|
176
|
+
if (this.closed)
|
|
142
177
|
return;
|
|
143
178
|
const key = this.source(source), room = this.sessionRoom(source), conversation = this.store.conversation(source.id);
|
|
144
|
-
if (!["completed", "failed"].includes(conversation.status) || conversation.archivedAt || conversation.agentId === "rsi-builder" || conversation.parentConversationId)
|
|
145
|
-
return;
|
|
146
179
|
const state = this.data.sessions[key] ??= defaultGroupRsi();
|
|
180
|
+
const unavailable = this.unavailable(source);
|
|
181
|
+
if (unavailable) {
|
|
182
|
+
state.pending = false;
|
|
183
|
+
state.notice = unavailable;
|
|
184
|
+
delete state.requestedAt;
|
|
185
|
+
await this.save();
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (this.active(source)) {
|
|
189
|
+
state.pending = true;
|
|
190
|
+
state.notice = "会话仍在执行,结束后将自动更新观察。";
|
|
191
|
+
await this.save();
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
147
194
|
const sourceKey = `${rsiSourceKey(room)}:${conversation.cwd ?? ""}`;
|
|
148
195
|
if (state.enabled === false || state.sourceKey === sourceKey || this.observing.has(key) || !room.messages.some(post => post.role === "assistant"))
|
|
149
196
|
return;
|
|
@@ -151,10 +198,13 @@ export class RsiService {
|
|
|
151
198
|
this.observing.set(key, controller);
|
|
152
199
|
this.lastRun.set(key, Date.now());
|
|
153
200
|
state.status = "running";
|
|
201
|
+
state.pending = false;
|
|
154
202
|
state.startedAt = new Date().toISOString();
|
|
203
|
+
delete state.notice;
|
|
204
|
+
delete state.requestedAt;
|
|
155
205
|
delete state.error;
|
|
156
|
-
await this.save();
|
|
157
206
|
try {
|
|
207
|
+
await this.save();
|
|
158
208
|
const agent = await this.runtime.agentFor?.({ id: `rsi:${source.id}`, agentId: "group-rsi" });
|
|
159
209
|
if (!agent || !this.runtime.askOnce)
|
|
160
210
|
throw new Error("RSI 观察员不可用。");
|
|
@@ -204,7 +254,7 @@ export class RsiService {
|
|
|
204
254
|
const finding = report.findings.find(item => item.ownership === "user" && item.ownershipReason && item.capability && (item.capability.repeatToolIds?.length ?? 0) >= 2 && item.priority !== "low" && item.disposition !== "dismissed");
|
|
205
255
|
if (finding) {
|
|
206
256
|
const recent = this.data.improvements.filter(job => job.key === `${finding.capability.kind}:${finding.capability.slug}` && Date.now() - Date.parse(job.createdAt) < 86400000);
|
|
207
|
-
if (recent.some(job => job.status === 'cancelled') || recent.filter(job => job.
|
|
257
|
+
if (recent.some(job => job.status === 'cancelled') || recent.filter(job => job.status === 'failed' || job.status === 'interrupted' && Boolean(job.conversationId)).length >= 2)
|
|
208
258
|
return;
|
|
209
259
|
await this.improve(source, report, finding.id, context);
|
|
210
260
|
}
|
|
@@ -321,8 +371,21 @@ export class RsiService {
|
|
|
321
371
|
async update(source, body) {
|
|
322
372
|
const key = this.source(source), state = this.data.sessions[key] ??= defaultGroupRsi();
|
|
323
373
|
if (body.action === "rsi.refresh") {
|
|
324
|
-
state.
|
|
325
|
-
|
|
374
|
+
if (state.enabled === false)
|
|
375
|
+
throw new Error("RSI 已暂停,请先在观察设置中启用并保存。");
|
|
376
|
+
const unavailable = this.unavailable(source);
|
|
377
|
+
if (unavailable)
|
|
378
|
+
throw new Error(unavailable);
|
|
379
|
+
if (!this.observing.has(key)) {
|
|
380
|
+
state.sourceKey = undefined;
|
|
381
|
+
state.pending = true;
|
|
382
|
+
state.requestedAt = new Date().toISOString();
|
|
383
|
+
state.status = "idle";
|
|
384
|
+
state.notice = this.active(source) ? "会话仍在执行,结束后将自动更新观察。" : "已排队,正在准备观察已有任务过程。";
|
|
385
|
+
delete state.error;
|
|
386
|
+
await this.save();
|
|
387
|
+
this.schedule(source, true);
|
|
388
|
+
}
|
|
326
389
|
}
|
|
327
390
|
else if (body.action === "rsi.configure") {
|
|
328
391
|
if (body.enabled !== undefined && typeof body.enabled !== "boolean" || body.automaticImprovements !== undefined && typeof body.automaticImprovements !== "boolean")
|
|
@@ -343,7 +406,12 @@ export class RsiService {
|
|
|
343
406
|
this.observing.get(key)?.abort();
|
|
344
407
|
clearTimeout(this.timers.get(key));
|
|
345
408
|
this.timers.delete(key);
|
|
409
|
+
state.pending = false;
|
|
410
|
+
delete state.requestedAt;
|
|
411
|
+
state.notice = "RSI 已暂停,启用后可更新观察。";
|
|
346
412
|
}
|
|
413
|
+
else
|
|
414
|
+
delete state.notice;
|
|
347
415
|
}
|
|
348
416
|
else if (body.action === "rsi.feedback") {
|
|
349
417
|
const finding = state.reports.find(report => report.id === body.reportId)?.findings.find(item => item.id === body.findingId);
|
|
@@ -1274,6 +1274,29 @@ export function createHttpServer(options) {
|
|
|
1274
1274
|
}
|
|
1275
1275
|
return;
|
|
1276
1276
|
}
|
|
1277
|
+
if (url.pathname === "/api/providers/discovery") {
|
|
1278
|
+
if (!isBrowserAuthorized(request) && request.headers.origin && request.headers.origin !== url.origin) {
|
|
1279
|
+
json(403, { error: "Origin not allowed" });
|
|
1280
|
+
return;
|
|
1281
|
+
}
|
|
1282
|
+
try {
|
|
1283
|
+
if (request.method === "GET") {
|
|
1284
|
+
json(200, await context.providerService.discovery.snapshot());
|
|
1285
|
+
return;
|
|
1286
|
+
}
|
|
1287
|
+
if (request.method !== "POST") {
|
|
1288
|
+
response.writeHead(405).end();
|
|
1289
|
+
return;
|
|
1290
|
+
}
|
|
1291
|
+
const snapshot = await context.providerService.discovery.snapshot();
|
|
1292
|
+
void context.providerService.discovery.refresh(true).catch(() => undefined);
|
|
1293
|
+
json(202, { ...snapshot, running: true });
|
|
1294
|
+
}
|
|
1295
|
+
catch {
|
|
1296
|
+
json(503, { error: "模型发现状态暂时不可用,已有模型配置保持不变。" });
|
|
1297
|
+
}
|
|
1298
|
+
return;
|
|
1299
|
+
}
|
|
1277
1300
|
if (url.pathname === "/api/providers/fetch-models") {
|
|
1278
1301
|
if (request.method !== "POST") {
|
|
1279
1302
|
response.writeHead(405).end();
|
package/dist/server/index.js
CHANGED
|
@@ -440,6 +440,7 @@ async function shutdown(reason) {
|
|
|
440
440
|
runService.beginShutdown();
|
|
441
441
|
const rsiClosed = rsiService.close();
|
|
442
442
|
userTools.close();
|
|
443
|
+
await providerService.discovery.close();
|
|
443
444
|
// Stop accepting new connections first. Existing clients stay connected until
|
|
444
445
|
// their cancelled work has recorded its terminal state.
|
|
445
446
|
const httpClosed = closeHttpServer();
|
|
@@ -519,6 +520,9 @@ httpServer.listen(port, host, () => {
|
|
|
519
520
|
// once by the first sweep - the daily report the user asked for is still wanted when they come back,
|
|
520
521
|
// but seven of them are not.
|
|
521
522
|
scheduledTaskService.start();
|
|
523
|
+
// Entry-owned catalogs are relayed to managed servers; they do not probe providers independently.
|
|
524
|
+
if (!process.env.SUPER_PI_MANAGED_CONFIG && process.env.PI_OFFLINE === undefined && runtime.supportsModelDiscovery)
|
|
525
|
+
providerService.discovery.start();
|
|
522
526
|
if (host === "0.0.0.0" || host === "::") {
|
|
523
527
|
const lanIps = getLanIps();
|
|
524
528
|
for (const ip of lanIps) {
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { DiscoveredModel } from "./types.js";
|
|
2
|
+
import type { ModelDiscoverySnapshot } from "../shared/model-discovery.js";
|
|
3
|
+
export type DiscoveryTarget = {
|
|
4
|
+
id: string;
|
|
5
|
+
name: string;
|
|
6
|
+
scope: string;
|
|
7
|
+
revision: number;
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
api: string;
|
|
10
|
+
builtin: boolean;
|
|
11
|
+
knownIds?: string[];
|
|
12
|
+
};
|
|
13
|
+
export type ModelDiscoveryHooks = {
|
|
14
|
+
targets(): Promise<DiscoveryTarget[]>;
|
|
15
|
+
discover(target: DiscoveryTarget, signal: AbortSignal, force: boolean): Promise<DiscoveredModel[]>;
|
|
16
|
+
apply(target: DiscoveryTarget, models: DiscoveredModel[]): Promise<number | undefined>;
|
|
17
|
+
changed(): Promise<void>;
|
|
18
|
+
};
|
|
19
|
+
/** Server-owned discovery; network work never runs in a picker read or on a model turn. */
|
|
20
|
+
export declare class ModelDiscovery {
|
|
21
|
+
private readonly path;
|
|
22
|
+
private readonly hooks;
|
|
23
|
+
private readonly intervalMs;
|
|
24
|
+
private saved;
|
|
25
|
+
private loaded?;
|
|
26
|
+
private writes;
|
|
27
|
+
private pending?;
|
|
28
|
+
private timer?;
|
|
29
|
+
private wake?;
|
|
30
|
+
private controller?;
|
|
31
|
+
private closed;
|
|
32
|
+
private started;
|
|
33
|
+
constructor(path: string, hooks: ModelDiscoveryHooks, intervalMs?: number);
|
|
34
|
+
private load;
|
|
35
|
+
private save;
|
|
36
|
+
snapshot(): Promise<ModelDiscoverySnapshot>;
|
|
37
|
+
exclude(scope: string, modelId: string): Promise<void>;
|
|
38
|
+
include(scope: string, modelIds: string[]): Promise<void>;
|
|
39
|
+
start(): void;
|
|
40
|
+
request(force?: boolean): void;
|
|
41
|
+
refresh(force?: boolean): Promise<void>;
|
|
42
|
+
private run;
|
|
43
|
+
close(): Promise<void>;
|
|
44
|
+
}
|