@evandu/super-pi 0.1.186 → 0.1.188
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/agents/super-pi-task/core.ts +130 -1
- package/agents/super-pi-task/index.ts +64 -5
- package/dist/client/.super-pi-client-sources.json +1 -1
- package/dist/server/agents.d.ts +1 -1
- package/dist/server/agents.js +1 -1
- package/dist/server/application/project-service.d.ts +26 -1
- package/dist/server/application/project-service.js +50 -4
- package/dist/server/application/session-orchestrator-service.js +8 -0
- package/dist/server/application/session-reminder-service.d.ts +55 -0
- package/dist/server/application/session-reminder-service.js +121 -0
- package/dist/server/conversation-usage.js +2 -2
- package/dist/server/http-server.js +111 -4
- package/dist/server/index.js +3 -0
- package/dist/server/json-comments.d.ts +14 -0
- package/dist/server/json-comments.js +71 -0
- package/dist/server/model-context-window.d.ts +34 -0
- package/dist/server/model-context-window.js +76 -0
- package/dist/server/models-store.d.ts +36 -0
- package/dist/server/models-store.js +132 -0
- package/dist/server/pi-session.js +3 -2
- package/dist/server/providers.js +10 -2
- package/dist/server/runtime.js +6 -2
- package/dist/server/server-context.d.ts +1 -0
- package/dist/server/task-tool.d.ts +3 -0
- package/dist/server/task-tool.js +12 -0
- package/package.json +2 -1
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
export class SessionReminderService {
|
|
3
|
+
runService;
|
|
4
|
+
store;
|
|
5
|
+
reminders = new Map();
|
|
6
|
+
constructor(runService, store) {
|
|
7
|
+
this.runService = runService;
|
|
8
|
+
this.store = store;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Schedules a reminder note to be sent back to the same conversation after delaySeconds.
|
|
12
|
+
*/
|
|
13
|
+
createReminder(params) {
|
|
14
|
+
const { conversationId, note } = params;
|
|
15
|
+
const delaySeconds = Math.max(1, Math.round(params.delaySeconds));
|
|
16
|
+
// A REMINDER THAT FIRES IMMEDIATELY IS NOT A REMINDER. `Math.max(1, ...)` would quietly turn a
|
|
17
|
+
// zero or negative delay into "one second from now", so the bad number is refused in words
|
|
18
|
+
// instead of becoming a timer the caller never asked for.
|
|
19
|
+
if (!Number.isFinite(params.delaySeconds) || params.delaySeconds <= 0) {
|
|
20
|
+
throw new Error("提醒需要正的秒数:请传 delaySeconds(或 seconds)。");
|
|
21
|
+
}
|
|
22
|
+
if (!note || !note.trim()) {
|
|
23
|
+
throw new Error("提醒内容 (note) 不能为空");
|
|
24
|
+
}
|
|
25
|
+
const conversation = this.store.conversation(conversationId);
|
|
26
|
+
if (!conversation) {
|
|
27
|
+
throw new Error(`找不到会话:${conversationId}`);
|
|
28
|
+
}
|
|
29
|
+
const reminderId = `rem_${randomUUID()}`;
|
|
30
|
+
const now = Date.now();
|
|
31
|
+
const fireAt = now + delaySeconds * 1000;
|
|
32
|
+
const item = {
|
|
33
|
+
reminderId,
|
|
34
|
+
conversationId,
|
|
35
|
+
note: note.trim(),
|
|
36
|
+
delaySeconds,
|
|
37
|
+
fireAt,
|
|
38
|
+
createdAt: now,
|
|
39
|
+
};
|
|
40
|
+
const timer = setTimeout(() => {
|
|
41
|
+
void this.triggerReminder(reminderId);
|
|
42
|
+
}, delaySeconds * 1000);
|
|
43
|
+
// Node process unref to avoid blocking process exit if idle
|
|
44
|
+
if (typeof timer.unref === "function") {
|
|
45
|
+
timer.unref();
|
|
46
|
+
}
|
|
47
|
+
this.reminders.set(reminderId, { item, timer });
|
|
48
|
+
return {
|
|
49
|
+
ok: true,
|
|
50
|
+
reminderId,
|
|
51
|
+
delaySeconds,
|
|
52
|
+
note: item.note,
|
|
53
|
+
fireAt: new Date(fireAt).toISOString(),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Cancels a pending reminder by ID.
|
|
58
|
+
*
|
|
59
|
+
* A REMINDER BELONGS TO THE CONVERSATION THAT SET IT. `conversationId` is optional only so the
|
|
60
|
+
* service can be called without one; when it IS given, a reminder owned by another conversation
|
|
61
|
+
* is refused, because one session taking another session's timer away is exactly the kind of
|
|
62
|
+
* cross-session reach the tree rules exist to prevent.
|
|
63
|
+
*/
|
|
64
|
+
cancelReminder(reminderId, conversationId) {
|
|
65
|
+
const existing = this.reminders.get(reminderId);
|
|
66
|
+
if (!existing) {
|
|
67
|
+
return { ok: false, error: `找不到提醒 ${reminderId} 或该提醒已触发` };
|
|
68
|
+
}
|
|
69
|
+
if (conversationId && existing.item.conversationId !== conversationId) {
|
|
70
|
+
return { ok: false, error: `提醒 ${reminderId} 不属于本会话,不能取消。` };
|
|
71
|
+
}
|
|
72
|
+
clearTimeout(existing.timer);
|
|
73
|
+
this.reminders.delete(reminderId);
|
|
74
|
+
return { ok: true, cancelled: true };
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Lists active reminders for a conversation.
|
|
78
|
+
*/
|
|
79
|
+
listReminders(conversationId) {
|
|
80
|
+
const results = [];
|
|
81
|
+
const now = Date.now();
|
|
82
|
+
for (const { item } of this.reminders.values()) {
|
|
83
|
+
if (item.conversationId === conversationId) {
|
|
84
|
+
results.push({ ...item });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return results.sort((a, b) => a.fireAt - b.fireAt);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Cancels all pending reminders for a given conversation.
|
|
91
|
+
*/
|
|
92
|
+
cancelAllForConversation(conversationId) {
|
|
93
|
+
for (const [id, entry] of this.reminders.entries()) {
|
|
94
|
+
if (entry.item.conversationId === conversationId) {
|
|
95
|
+
clearTimeout(entry.timer);
|
|
96
|
+
this.reminders.delete(id);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Timer callback: delivers the note to the originating conversation.
|
|
102
|
+
*/
|
|
103
|
+
async triggerReminder(reminderId) {
|
|
104
|
+
const entry = this.reminders.get(reminderId);
|
|
105
|
+
if (!entry)
|
|
106
|
+
return;
|
|
107
|
+
this.reminders.delete(reminderId);
|
|
108
|
+
const { conversationId, note } = entry.item;
|
|
109
|
+
const conversation = this.store.conversation(conversationId);
|
|
110
|
+
if (!conversation || conversation.archivedAt) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const message = `[定时提醒] ${note}`;
|
|
114
|
+
try {
|
|
115
|
+
await this.runService.enqueueAndRun(conversationId, message);
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
console.warn(`Super Pi: delivering session reminder to ${conversationId} failed:`, error instanceof Error ? error.message : String(error));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { arbitrateContextWindow, readDeclaredWindows, readStoreWindows } from "./model-context-window.js";
|
|
2
2
|
const nonnegative = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0;
|
|
3
3
|
/** Collect only completed assistant messages, never the repeated usage on streaming deltas. */
|
|
4
4
|
export class ConversationUsageCollector {
|
|
@@ -27,7 +27,7 @@ export class ConversationUsageCollector {
|
|
|
27
27
|
let contextUsage;
|
|
28
28
|
if (context) {
|
|
29
29
|
const contextWindow = model?.provider && model.id
|
|
30
|
-
?
|
|
30
|
+
? arbitrateContextWindow(readDeclaredWindows(agentDir), readStoreWindows(agentDir), model.provider, model.id, context.contextWindow) ?? context.contextWindow
|
|
31
31
|
: context.contextWindow;
|
|
32
32
|
if (nonnegative(contextWindow) && contextWindow > 0) {
|
|
33
33
|
// Pi returns null after compaction until another assistant supplies a trustworthy token count.
|
|
@@ -251,6 +251,52 @@ function textBodyReader() {
|
|
|
251
251
|
finish: () => text + decoder.decode(),
|
|
252
252
|
};
|
|
253
253
|
}
|
|
254
|
+
/**
|
|
255
|
+
* THE READ SCOPE FOR A SESSION'S STATUS, WHICH IS DELIBERATELY WIDER THAN THE WRITE SCOPE.
|
|
256
|
+
*
|
|
257
|
+
* SEEING IS NOT ORDERING. A session in the same workspace may read another session's STATUS - is
|
|
258
|
+
* it running, is it stuck, how far has it got - without having any authority over it. The actions
|
|
259
|
+
* that spend somebody else's work - dispatch, stop, resume, report - stay bound to the tree edge,
|
|
260
|
+
* where the caller's DIRECT child is the only reachable session. Keeping the two apart is what
|
|
261
|
+
* lets a supervisor watch a whole project without being able to take it over.
|
|
262
|
+
*
|
|
263
|
+
* WHY IT IS A RULE AND NOT AN ACCIDENT. The rule was tightened once by accident: the read was put
|
|
264
|
+
* behind the same check as the write, so a session could not look at a peer it was supposed to be
|
|
265
|
+
* coordinating with, and the fix for "I cannot see my project" was to grant dispatch authority
|
|
266
|
+
* instead - which is how a supervisor ends up ordering everybody around. Reading and ordering are
|
|
267
|
+
* different powers and they get different boundaries.
|
|
268
|
+
*
|
|
269
|
+
* WHAT IS STILL BOUNDED: another session's FINAL OUTPUT. A status is a fact about the work; the
|
|
270
|
+
* output is the work itself, and reading somebody else's work is not supervision.
|
|
271
|
+
*/
|
|
272
|
+
const verifyStatusReadScope = (options) => {
|
|
273
|
+
const { store, callerConversationId, targetConversationId } = options;
|
|
274
|
+
const caller = callerConversationId?.trim();
|
|
275
|
+
if (!caller)
|
|
276
|
+
return { ok: false, status: 403, error: "调用方会话为空,拒绝读取状态。" };
|
|
277
|
+
const callerConversation = store.conversation(caller);
|
|
278
|
+
if (!callerConversation?.workspaceId) {
|
|
279
|
+
return { ok: false, status: 403, error: `调用方会话 ${caller} 不存在或未关联有效工作区,拒绝读取状态。` };
|
|
280
|
+
}
|
|
281
|
+
const target = store.conversation(targetConversationId);
|
|
282
|
+
if (!target)
|
|
283
|
+
return { ok: false, status: 400, error: `找不到会话:${targetConversationId}` };
|
|
284
|
+
if (target.workspaceId !== callerConversation.workspaceId) {
|
|
285
|
+
return {
|
|
286
|
+
ok: false,
|
|
287
|
+
status: 403,
|
|
288
|
+
error: `调用方会话 ${caller} 所在工作区为 ${callerConversation.workspaceId},目标会话 ${targetConversationId} 所在工作区为 ${target.workspaceId},拒绝跨工作区读取状态。`,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
if (options.includeOutput && target.parentConversationId !== caller) {
|
|
292
|
+
return {
|
|
293
|
+
ok: false,
|
|
294
|
+
status: 403,
|
|
295
|
+
error: `读取会话 ${targetConversationId} 的最终输出只限它的直接上级;同工作区内的状态读取不受此限制。`,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
return { ok: true };
|
|
299
|
+
};
|
|
254
300
|
/**
|
|
255
301
|
* THE REQUEST BODY AS TEXT, DECODED ACROSS CHUNK BOUNDARIES.
|
|
256
302
|
*
|
|
@@ -1856,7 +1902,15 @@ export function createHttpServer(options) {
|
|
|
1856
1902
|
if (action === "status") {
|
|
1857
1903
|
if (!body.conversationId)
|
|
1858
1904
|
throw new Error("status requires conversationId");
|
|
1859
|
-
|
|
1905
|
+
// THE READ IS WIDER THAN THE WRITE, ON PURPOSE. Status is a fact about the work, not
|
|
1906
|
+
// authority over it: a session may look at any session in its own workspace, while only
|
|
1907
|
+
// its DIRECT child may be dispatched to, stopped or resumed. See `verifyStatusReadScope`.
|
|
1908
|
+
const scope = verifyStatusReadScope({
|
|
1909
|
+
store: context.store,
|
|
1910
|
+
callerConversationId,
|
|
1911
|
+
targetConversationId: body.conversationId,
|
|
1912
|
+
includeOutput: body.includeOutput,
|
|
1913
|
+
});
|
|
1860
1914
|
if (!scope.ok) {
|
|
1861
1915
|
json(scope.status, { error: scope.error });
|
|
1862
1916
|
return;
|
|
@@ -2005,6 +2059,58 @@ export function createHttpServer(options) {
|
|
|
2005
2059
|
json(200, { ...result, ok: true });
|
|
2006
2060
|
return;
|
|
2007
2061
|
}
|
|
2062
|
+
if (action === "reminder") {
|
|
2063
|
+
// THE REMINDER LIVES ON THE CALLING CONVERSATION. `callerConversationId` is the id the
|
|
2064
|
+
// capability above was verified against, so this is the session that asked for the timer
|
|
2065
|
+
// and the session the note comes back to - there is no way to aim a reminder at somebody
|
|
2066
|
+
// else's conversation, because the body does not get to choose the target.
|
|
2067
|
+
const reminders = context.sessionReminderService;
|
|
2068
|
+
if (!reminders) {
|
|
2069
|
+
json(503, { error: "提醒服务未就绪" });
|
|
2070
|
+
return;
|
|
2071
|
+
}
|
|
2072
|
+
const subaction = (body.subaction ?? "create").trim();
|
|
2073
|
+
if (subaction === "create") {
|
|
2074
|
+
const delaySeconds = Number(body.delaySeconds ?? body.seconds);
|
|
2075
|
+
if (!Number.isFinite(delaySeconds) || delaySeconds <= 0) {
|
|
2076
|
+
json(400, { error: "提醒需要正的秒数:请传 delaySeconds(或 seconds)。" });
|
|
2077
|
+
return;
|
|
2078
|
+
}
|
|
2079
|
+
const note = typeof body.note === "string" ? body.note.trim() : "";
|
|
2080
|
+
if (!note) {
|
|
2081
|
+
json(400, { error: "提醒内容不能为空:note 要写清楚提醒什么。" });
|
|
2082
|
+
return;
|
|
2083
|
+
}
|
|
2084
|
+
try {
|
|
2085
|
+
const created = reminders.createReminder({ conversationId: callerConversationId, delaySeconds, note });
|
|
2086
|
+
json(200, { ...created, ok: true });
|
|
2087
|
+
}
|
|
2088
|
+
catch (error) {
|
|
2089
|
+
json(400, { error: error instanceof Error ? error.message : "创建提醒失败" });
|
|
2090
|
+
}
|
|
2091
|
+
return;
|
|
2092
|
+
}
|
|
2093
|
+
if (subaction === "cancel") {
|
|
2094
|
+
const reminderId = typeof body.reminderId === "string" ? body.reminderId.trim() : "";
|
|
2095
|
+
if (!reminderId) {
|
|
2096
|
+
json(400, { error: "取消提醒需要 reminderId。" });
|
|
2097
|
+
return;
|
|
2098
|
+
}
|
|
2099
|
+
const result = reminders.cancelReminder(reminderId, callerConversationId);
|
|
2100
|
+
if (!result.ok) {
|
|
2101
|
+
json(404, { error: result.error ?? "找不到这条提醒。" });
|
|
2102
|
+
return;
|
|
2103
|
+
}
|
|
2104
|
+
json(200, { ok: true, cancelled: true, reminderId });
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
if (subaction === "list") {
|
|
2108
|
+
json(200, { ok: true, reminders: reminders.listReminders(callerConversationId) });
|
|
2109
|
+
return;
|
|
2110
|
+
}
|
|
2111
|
+
json(400, { error: `不支持的提醒操作:${subaction}` });
|
|
2112
|
+
return;
|
|
2113
|
+
}
|
|
2008
2114
|
json(400, { error: `不支持的编排操作:${action}` });
|
|
2009
2115
|
}
|
|
2010
2116
|
catch (error) {
|
|
@@ -2131,12 +2237,13 @@ export function createHttpServer(options) {
|
|
|
2131
2237
|
return;
|
|
2132
2238
|
}
|
|
2133
2239
|
const store = context.store;
|
|
2134
|
-
// THE SAME SCOPE
|
|
2135
|
-
|
|
2240
|
+
// THE SAME READ SCOPE AS THE POST ACTION: status is visible across the caller's own
|
|
2241
|
+
// workspace, and only the direct-child rule governs who may be ordered around.
|
|
2242
|
+
const scope = verifyStatusReadScope({
|
|
2136
2243
|
store,
|
|
2137
2244
|
callerConversationId,
|
|
2138
2245
|
targetConversationId: conversationId,
|
|
2139
|
-
|
|
2246
|
+
includeOutput: url.searchParams.get("includeOutput") === "true",
|
|
2140
2247
|
});
|
|
2141
2248
|
if (!scope.ok) {
|
|
2142
2249
|
response.writeHead(scope.status, { "content-type": "application/json; charset=utf-8" }).end(JSON.stringify({ error: scope.error }));
|
package/dist/server/index.js
CHANGED
|
@@ -26,6 +26,7 @@ import { ConversationService } from "./application/conversation-service.js";
|
|
|
26
26
|
import { ScheduledTaskService } from "./application/scheduled-task-service.js";
|
|
27
27
|
import { SessionLedger } from "./session-ledger.js";
|
|
28
28
|
import { SessionOrchestratorService } from "./application/session-orchestrator-service.js";
|
|
29
|
+
import { SessionReminderService } from "./application/session-reminder-service.js";
|
|
29
30
|
import { ProjectService } from "./application/project-service.js";
|
|
30
31
|
import { WorkspaceService } from "./application/workspace-service.js";
|
|
31
32
|
import { CommandHandler } from "./command-handler.js";
|
|
@@ -309,6 +310,7 @@ runService.setExecService(execService);
|
|
|
309
310
|
*/
|
|
310
311
|
const scheduledTaskService = new ScheduledTaskService(store, runService, conversationService, broadcaster);
|
|
311
312
|
const sessionLedger = new SessionLedger();
|
|
313
|
+
const sessionReminderService = new SessionReminderService(runService, store);
|
|
312
314
|
const sessionOrchestratorService = new SessionOrchestratorService(store, conversationService, runService, sessionLedger, broadcaster, undefined, uploadsDir);
|
|
313
315
|
/**
|
|
314
316
|
* THE PROJECT SERVICE IS TOLD HOW TO ASK WHAT AN AGENT IS.
|
|
@@ -345,6 +347,7 @@ const serverContext = {
|
|
|
345
347
|
providerService,
|
|
346
348
|
scheduledTaskService,
|
|
347
349
|
sessionLedger,
|
|
350
|
+
sessionReminderService,
|
|
348
351
|
sessionOrchestratorService,
|
|
349
352
|
projectService,
|
|
350
353
|
execService,
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSONC COMMENTS, STRIPPED THE WAY PI STRIPS THEM.
|
|
3
|
+
*
|
|
4
|
+
* Pi reads `models.json` and `models-store.json` with comments allowed, so a person may well have
|
|
5
|
+
* written comments in them - and Super Pi has to be able to READ those files to merge into them and
|
|
6
|
+
* to learn what a model really costs in context. A naive strip would corrupt a string that contains
|
|
7
|
+
* `//` (a base URL with a path, an api key with a slash), so this walks the text and tracks whether
|
|
8
|
+
* it is inside a string.
|
|
9
|
+
*
|
|
10
|
+
* IT LIVES HERE, NOT IN `providers.ts`, because two unrelated modules need it: the provider registry
|
|
11
|
+
* reads `models.json`, and the model store reads `models-store.json`. Keeping it in one place is
|
|
12
|
+
* what makes "reuse it, do not write a second one" true rather than aspirational.
|
|
13
|
+
*/
|
|
14
|
+
export declare function stripJsonComments(text: string): string;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSONC COMMENTS, STRIPPED THE WAY PI STRIPS THEM.
|
|
3
|
+
*
|
|
4
|
+
* Pi reads `models.json` and `models-store.json` with comments allowed, so a person may well have
|
|
5
|
+
* written comments in them - and Super Pi has to be able to READ those files to merge into them and
|
|
6
|
+
* to learn what a model really costs in context. A naive strip would corrupt a string that contains
|
|
7
|
+
* `//` (a base URL with a path, an api key with a slash), so this walks the text and tracks whether
|
|
8
|
+
* it is inside a string.
|
|
9
|
+
*
|
|
10
|
+
* IT LIVES HERE, NOT IN `providers.ts`, because two unrelated modules need it: the provider registry
|
|
11
|
+
* reads `models.json`, and the model store reads `models-store.json`. Keeping it in one place is
|
|
12
|
+
* what makes "reuse it, do not write a second one" true rather than aspirational.
|
|
13
|
+
*/
|
|
14
|
+
export function stripJsonComments(text) {
|
|
15
|
+
let out = "";
|
|
16
|
+
let inString = false;
|
|
17
|
+
let inLineComment = false;
|
|
18
|
+
let inBlockComment = false;
|
|
19
|
+
for (let index = 0; index < text.length; index++) {
|
|
20
|
+
const char = text[index];
|
|
21
|
+
const next = text[index + 1];
|
|
22
|
+
if (inLineComment) {
|
|
23
|
+
if (char === "\n") {
|
|
24
|
+
inLineComment = false;
|
|
25
|
+
out += char;
|
|
26
|
+
}
|
|
27
|
+
else
|
|
28
|
+
out += " ";
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (inBlockComment) {
|
|
32
|
+
if (char === "*" && next === "/") {
|
|
33
|
+
inBlockComment = false;
|
|
34
|
+
out += " ";
|
|
35
|
+
index++;
|
|
36
|
+
}
|
|
37
|
+
else
|
|
38
|
+
out += char === "\n" ? "\n" : " ";
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (inString) {
|
|
42
|
+
out += char;
|
|
43
|
+
if (char === "\\" && next !== undefined) {
|
|
44
|
+
out += next;
|
|
45
|
+
index++;
|
|
46
|
+
}
|
|
47
|
+
else if (char === "\"")
|
|
48
|
+
inString = false;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (char === "\"") {
|
|
52
|
+
inString = true;
|
|
53
|
+
out += char;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (char === "/" && next === "/") {
|
|
57
|
+
inLineComment = true;
|
|
58
|
+
out += " ";
|
|
59
|
+
index++;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (char === "/" && next === "*") {
|
|
63
|
+
inBlockComment = true;
|
|
64
|
+
out += " ";
|
|
65
|
+
index++;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
out += char;
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
import { type ModelStore } from "./models-store.js";
|
|
2
|
+
/**
|
|
3
|
+
* THE DEFAULTS SUPER PI STAMPS WHEN IT DISCOVERS A MODEL. `providers.ts` writes these into
|
|
4
|
+
* `models.json` for every model the provider did not describe, and they are NOT a person's decision
|
|
5
|
+
* - they are a placeholder. The store's real number must outrank them.
|
|
6
|
+
*/
|
|
7
|
+
export declare const SUPER_PI_DEFAULT_CONTEXT_WINDOW = 128000;
|
|
8
|
+
export declare const SUPER_PI_DEFAULT_MAX_TOKENS = 16384;
|
|
1
9
|
/** One model's declared window, as the person's file states it. */
|
|
2
10
|
export type DeclaredWindow = {
|
|
3
11
|
provider: string;
|
|
@@ -44,3 +52,29 @@ export declare function effectiveContextWindow(declared: Map<string, number>, pr
|
|
|
44
52
|
* simply "Super Pi has no opinion about this model's window" - which is where it started.
|
|
45
53
|
*/
|
|
46
54
|
export declare function readDeclaredWindows(agentDir: string): Map<string, number>;
|
|
55
|
+
/** The windows the catalogue (`models-store.json`) declares, keyed the same way as `declared`. */
|
|
56
|
+
export declare function storeWindows(store: ModelStore): Map<string, number>;
|
|
57
|
+
/**
|
|
58
|
+
* Read the catalogue's declared windows, alongside the person's.
|
|
59
|
+
*
|
|
60
|
+
* The catalogue is the truth the provider self-reports; it is re-read only when the file changes.
|
|
61
|
+
*/
|
|
62
|
+
export declare function readStoreWindows(agentDir: string): ModelStore;
|
|
63
|
+
/**
|
|
64
|
+
* THE WINDOW A MODEL REALLY GETS, arbitrated across the three sources that each claim to know it.
|
|
65
|
+
*
|
|
66
|
+
* PRECEDENCE, HIGHEST FIRST:
|
|
67
|
+
*
|
|
68
|
+
* 1. A NON-DEFAULT value in the person's `models.json` — somebody typed it, so it is a decision.
|
|
69
|
+
* (This is the original rule, unchanged.)
|
|
70
|
+
* 2. The CATALOGUE (`models-store.json`) — the provider's self-report. This is the truth for a
|
|
71
|
+
* model Super Pi only knows from its own default. `MiniMax-M3` is the case in point: the
|
|
72
|
+
* catalogue says 1,000,000 while every `models.json` entry Super Pi wrote says the default
|
|
73
|
+
* 128,000.
|
|
74
|
+
* 3. The `reported` value the model object already carries, then nothing (`undefined`).
|
|
75
|
+
*
|
|
76
|
+
* THE OLD "SAME MODEL TAKE THE SMALLEST" RULE IS GONE. It was written for a catalogue that lies
|
|
77
|
+
* big; on this machine the catalogue is right and the stamped default is what was too small, so
|
|
78
|
+
* taking the smallest is exactly how a million-token model ended up believing it had 128,000.
|
|
79
|
+
*/
|
|
80
|
+
export declare function arbitrateContextWindow(declared: Map<string, number>, store: ModelStore, provider: string, modelId: string, reported?: number): number | undefined;
|
|
@@ -32,6 +32,14 @@
|
|
|
32
32
|
*/
|
|
33
33
|
import { readFileSync } from "node:fs";
|
|
34
34
|
import { join } from "node:path";
|
|
35
|
+
import { readModelStore, storeWindowFor } from "./models-store.js";
|
|
36
|
+
/**
|
|
37
|
+
* THE DEFAULTS SUPER PI STAMPS WHEN IT DISCOVERS A MODEL. `providers.ts` writes these into
|
|
38
|
+
* `models.json` for every model the provider did not describe, and they are NOT a person's decision
|
|
39
|
+
* - they are a placeholder. The store's real number must outrank them.
|
|
40
|
+
*/
|
|
41
|
+
export const SUPER_PI_DEFAULT_CONTEXT_WINDOW = 128000;
|
|
42
|
+
export const SUPER_PI_DEFAULT_MAX_TOKENS = 16384;
|
|
35
43
|
/**
|
|
36
44
|
* The windows the person's `models.json` declares, keyed by `provider/modelId`.
|
|
37
45
|
*
|
|
@@ -119,3 +127,71 @@ export function readDeclaredWindows(agentDir) {
|
|
|
119
127
|
return new Map();
|
|
120
128
|
}
|
|
121
129
|
}
|
|
130
|
+
/** The windows the catalogue (`models-store.json`) declares, keyed the same way as `declared`. */
|
|
131
|
+
export function storeWindows(store) {
|
|
132
|
+
const out = new Map();
|
|
133
|
+
for (const [key, entry] of store.byProviderModel) {
|
|
134
|
+
if (entry.contextWindow !== undefined)
|
|
135
|
+
out.set(key, entry.contextWindow);
|
|
136
|
+
}
|
|
137
|
+
return out;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Read the catalogue's declared windows, alongside the person's.
|
|
141
|
+
*
|
|
142
|
+
* The catalogue is the truth the provider self-reports; it is re-read only when the file changes.
|
|
143
|
+
*/
|
|
144
|
+
export function readStoreWindows(agentDir) {
|
|
145
|
+
return readModelStore(agentDir);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Is a `models.json` window a PERSON's decision, or just Super Pi's own default stamped at import?
|
|
149
|
+
*
|
|
150
|
+
* Only a number somebody actually typed is authoritative. The value Super Pi writes when the
|
|
151
|
+
* provider said nothing (128000 / 16384) is a placeholder, and a placeholder never outranks the
|
|
152
|
+
* catalogue's real figure.
|
|
153
|
+
*/
|
|
154
|
+
function isDeliberateWindow(window) {
|
|
155
|
+
return window !== undefined && window !== SUPER_PI_DEFAULT_CONTEXT_WINDOW;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* THE WINDOW A MODEL REALLY GETS, arbitrated across the three sources that each claim to know it.
|
|
159
|
+
*
|
|
160
|
+
* PRECEDENCE, HIGHEST FIRST:
|
|
161
|
+
*
|
|
162
|
+
* 1. A NON-DEFAULT value in the person's `models.json` — somebody typed it, so it is a decision.
|
|
163
|
+
* (This is the original rule, unchanged.)
|
|
164
|
+
* 2. The CATALOGUE (`models-store.json`) — the provider's self-report. This is the truth for a
|
|
165
|
+
* model Super Pi only knows from its own default. `MiniMax-M3` is the case in point: the
|
|
166
|
+
* catalogue says 1,000,000 while every `models.json` entry Super Pi wrote says the default
|
|
167
|
+
* 128,000.
|
|
168
|
+
* 3. The `reported` value the model object already carries, then nothing (`undefined`).
|
|
169
|
+
*
|
|
170
|
+
* THE OLD "SAME MODEL TAKE THE SMALLEST" RULE IS GONE. It was written for a catalogue that lies
|
|
171
|
+
* big; on this machine the catalogue is right and the stamped default is what was too small, so
|
|
172
|
+
* taking the smallest is exactly how a million-token model ended up believing it had 128,000.
|
|
173
|
+
*/
|
|
174
|
+
export function arbitrateContextWindow(declared, store, provider, modelId, reported) {
|
|
175
|
+
const exact = declared.get(`${provider}/${modelId}`);
|
|
176
|
+
if (isDeliberateWindow(exact))
|
|
177
|
+
return exact;
|
|
178
|
+
// A deliberate cross-provider declaration still wins over the catalogue.
|
|
179
|
+
let deliberate;
|
|
180
|
+
for (const [key, window] of declared) {
|
|
181
|
+
const slash = key.indexOf("/");
|
|
182
|
+
if (slash < 0 || key.slice(slash + 1) !== modelId)
|
|
183
|
+
continue;
|
|
184
|
+
if (!isDeliberateWindow(window))
|
|
185
|
+
continue;
|
|
186
|
+
if (deliberate === undefined || window > deliberate)
|
|
187
|
+
deliberate = window;
|
|
188
|
+
}
|
|
189
|
+
if (deliberate !== undefined)
|
|
190
|
+
return deliberate;
|
|
191
|
+
const storeEntry = storeWindowFor(store, modelId, provider);
|
|
192
|
+
if (storeEntry?.contextWindow !== undefined)
|
|
193
|
+
return storeEntry.contextWindow;
|
|
194
|
+
if (isDeliberateWindow(reported))
|
|
195
|
+
return reported;
|
|
196
|
+
return exact ?? deliberate ?? storeEntry?.contextWindow ?? reported;
|
|
197
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** What the catalogue says about one model. Either number may be absent. */
|
|
2
|
+
export type StoreEntry = {
|
|
3
|
+
contextWindow?: number;
|
|
4
|
+
maxTokens?: number;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* The catalogue, indexed twice.
|
|
8
|
+
*
|
|
9
|
+
* `byProviderModel` is keyed `provider/modelId` and `byModel` is keyed by the model id alone. The
|
|
10
|
+
* second index exists because a model id belongs to the MODEL, not to the endpoint: the catalogue
|
|
11
|
+
* declares `MiniMax-M3` under both `minimax` and `minimax-cn`, and a session may run under either
|
|
12
|
+
* provider id, or under a provider id the user made up.
|
|
13
|
+
*/
|
|
14
|
+
export type ModelStore = {
|
|
15
|
+
byModel: Map<string, StoreEntry>;
|
|
16
|
+
byProviderModel: Map<string, StoreEntry>;
|
|
17
|
+
};
|
|
18
|
+
/** The file Pi writes the catalogue into, next to the file it writes providers into. */
|
|
19
|
+
export declare function modelStorePath(agentDir: string): string;
|
|
20
|
+
export declare function readModelStore(agentDir: string): ModelStore;
|
|
21
|
+
/**
|
|
22
|
+
* The catalogue as data.
|
|
23
|
+
*
|
|
24
|
+
* The shape Pi writes is `{ <provider id>: { models: [{ id, contextWindow, maxTokens }] } }`, and a
|
|
25
|
+
* shape with the providers nested one level deeper is read too - Pi has moved this file around
|
|
26
|
+
* before, and a catalogue in a slightly different shape is still a catalogue.
|
|
27
|
+
*/
|
|
28
|
+
export declare function parseModelStore(raw: unknown): ModelStore;
|
|
29
|
+
/**
|
|
30
|
+
* What the catalogue says about one model.
|
|
31
|
+
*
|
|
32
|
+
* The provider-specific entry is preferred when the catalogue has one, because a provider may serve
|
|
33
|
+
* a model under a different limit than another provider does. The model-id entry is the fallback,
|
|
34
|
+
* and it is what makes the lookup work for a provider id the user invented.
|
|
35
|
+
*/
|
|
36
|
+
export declare function storeWindowFor(store: ModelStore | undefined, modelId: string, providerId?: string): StoreEntry | undefined;
|