@evandu/super-pi 0.1.281 → 0.1.283

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.
Files changed (64) hide show
  1. package/agents/rsi-discussion.md +21 -0
  2. package/agents/rsi-repair.md +33 -0
  3. package/agents/super-pi-model-policy/index.ts +1 -0
  4. package/desktop/preload.cjs +1 -1
  5. package/dist/.super-pi-check-inputs.json +2 -2
  6. package/dist/client/.super-pi-client-sources.json +21 -14
  7. package/dist/client/assets/{CodeMirrorEditor-xxJP_tVy.js → CodeMirrorEditor-Y4BbFnPe.js} +1 -1
  8. package/dist/client/assets/{GitDiffEditor-X8eCsha9.js → GitDiffEditor-BhUOinn3.js} +1 -1
  9. package/dist/client/assets/index-CMb8f0dS.css +1 -0
  10. package/dist/client/assets/index-DGnOud6x.js +350 -0
  11. package/dist/client/index.html +2 -2
  12. package/dist/server/application/conversation-service.js +4 -0
  13. package/dist/server/application/group-chat-service.d.ts +8 -0
  14. package/dist/server/application/group-chat-service.js +13 -4
  15. package/dist/server/application/group-execution.d.ts +1 -0
  16. package/dist/server/application/group-execution.js +18 -0
  17. package/dist/server/application/group-rsi.d.ts +9 -0
  18. package/dist/server/application/group-rsi.js +19 -6
  19. package/dist/server/application/rsi-repairs.d.ts +27 -0
  20. package/dist/server/application/rsi-repairs.js +201 -0
  21. package/dist/server/application/rsi-service.d.ts +4 -0
  22. package/dist/server/application/rsi-service.js +29 -9
  23. package/dist/server/application/run-service.d.ts +3 -0
  24. package/dist/server/application/run-service.js +79 -5
  25. package/dist/server/bounded-compaction.d.ts +12 -0
  26. package/dist/server/bounded-compaction.js +34 -0
  27. package/dist/server/command-handler.js +10 -0
  28. package/dist/server/conversation-usage.d.ts +6 -1
  29. package/dist/server/conversation-usage.js +2 -2
  30. package/dist/server/hierarchical-compaction.d.ts +1 -0
  31. package/dist/server/hierarchical-compaction.js +115 -0
  32. package/dist/server/index.js +35 -0
  33. package/dist/server/length-continuation.js +1 -1
  34. package/dist/server/model-capabilities.d.ts +6 -0
  35. package/dist/server/model-capabilities.js +57 -0
  36. package/dist/server/model-policy-extension.d.ts +3 -0
  37. package/dist/server/model-policy-extension.js +91 -0
  38. package/dist/server/model-request-policy.d.ts +2 -0
  39. package/dist/server/model-request-policy.js +30 -0
  40. package/dist/server/pi-session-events.d.ts +2 -0
  41. package/dist/server/pi-session-events.js +10 -0
  42. package/dist/server/pi-session.d.ts +14 -0
  43. package/dist/server/pi-session.js +9 -0
  44. package/dist/server/provider-proxy.js +7 -2
  45. package/dist/server/rsi-http.js +6 -3
  46. package/dist/server/runtime.d.ts +11 -0
  47. package/dist/server/runtime.js +200 -13
  48. package/dist/server/store.d.ts +2 -0
  49. package/dist/server/store.js +17 -1
  50. package/dist/server/subagents.js +2 -1
  51. package/dist/server/thinking-level-discovery.d.ts +6 -0
  52. package/dist/server/thinking-level-discovery.js +10 -3
  53. package/dist/server/turn-failure.js +3 -1
  54. package/dist/server/types.d.ts +13 -0
  55. package/dist/server/ws-server.js +1 -1
  56. package/dist/shared/context-compaction.d.ts +15 -0
  57. package/dist/shared/context-compaction.js +14 -0
  58. package/dist/shared/context-meter.d.ts +22 -0
  59. package/dist/shared/context-meter.js +9 -0
  60. package/dist/shared/group-chat.d.ts +6 -0
  61. package/dist/shared/rsi.d.ts +33 -0
  62. package/package.json +7 -1
  63. package/dist/client/assets/index-DaKloC7K.css +0 -1
  64. package/dist/client/assets/index-sWsdT0fC.js +0 -350
@@ -1,5 +1,6 @@
1
1
  import { spawn } from "node:child_process";
2
2
  import { randomUUID } from "node:crypto";
3
+ import { modelPolicyExtensionPath } from "./model-policy-extension.js";
3
4
  import { mkdir, stat } from "node:fs/promises";
4
5
  import { join, resolve } from "node:path";
5
6
  import { agentSpawnArgs, readAgents, toolSelection } from "./agents.js";
@@ -277,7 +278,7 @@ export class SubagentSupervisor {
277
278
  throw new SubagentCancelled(id);
278
279
  }
279
280
  // npm exposes global CLIs as .cmd shims on Windows; Node requires a shell to launch those shims.
280
- const child = spawn(shellQuote(this.pi.command), args, { cwd: childCwd, detached: process.platform !== "win32", stdio: ["pipe", "pipe", "pipe"], shell: process.platform === "win32", env: { ...process.env, ...nativeToolEnv(this.sessionDir, nativePolicy), ...(memoryToken ? { SUPER_PI_MEMORY_ENDPOINT: process.env.SUPER_PI_MEMORY_ENDPOINT, SUPER_PI_MEMORY_TOKEN: memoryToken, SUPER_PI_MEMORY_READ_ONLY: "1" } : {}) } });
281
+ const child = spawn(shellQuote(this.pi.command), [...args, "--extension", shellQuote(modelPolicyExtensionPath())], { cwd: childCwd, detached: process.platform !== "win32", stdio: ["pipe", "pipe", "pipe"], shell: process.platform === "win32", env: { ...process.env, ...nativeToolEnv(this.sessionDir, nativePolicy), ...(memoryToken ? { SUPER_PI_MEMORY_ENDPOINT: process.env.SUPER_PI_MEMORY_ENDPOINT, SUPER_PI_MEMORY_TOKEN: memoryToken, SUPER_PI_MEMORY_READ_ONLY: "1" } : {}) } });
281
282
  if (memoryToken)
282
283
  child.once("close", () => this.memory?.release(memoryToken));
283
284
  this.children.set(id, { conversationId, child });
@@ -29,6 +29,12 @@ export type ThinkingLevelContext = {
29
29
  /** The API that endpoint is spoken with. */
30
30
  api?: string;
31
31
  };
32
+ /** Match the documented API and model, never a same-named model at an unrelated proxy. */
33
+ export declare function isOfficialMinimaxFlash(model: {
34
+ id?: unknown;
35
+ baseUrl?: unknown;
36
+ api?: unknown;
37
+ }): boolean;
32
38
  /**
33
39
  * THE LEVELS A MODEL OFFERS, or `undefined` when nobody said.
34
40
  *
@@ -36,12 +36,14 @@ const EFFORT_TO_LEVEL = new Map([
36
36
  * (https://platform.minimax.io/docs/api-reference/text-openai-api), which publishes the base URL
37
37
  * `https://api.minimax.io/v1` and the `reasoning_effort` values of `MiniMax-M3.1-Flash`.
38
38
  *
39
- * EXACTLY THIS ORIGIN. A proxy that serves a model called `MiniMax-M3.1-Flash` is a different
39
+ * The China reference (https://platform.minimax.cn/docs/api-reference/text-openai-api) publishes
40
+ * api.minimax.cn. api.minimaxi.com is the earlier official China API origin.
41
+ * EXACT OFFICIAL ORIGINS. A proxy that serves a model called `MiniMax-M3.1-Flash` is a different
40
42
  * endpoint with a different answer, and it does not get these levels. The path is part of the
41
43
  * match too, because the same host serves other paths - `/anthropic` is a different API with
42
44
  * different parameters.
43
45
  */
44
- const MINIMAX_ORIGIN = "https://api.minimax.io";
46
+ const MINIMAX_ORIGINS = new Set(["https://api.minimax.io", "https://api.minimax.cn", "https://api.minimaxi.com"]);
45
47
  /**
46
48
  * THE PATHS THAT ARE THE OFFICIAL OPENAI-COMPATIBLE TEXT API.
47
49
  *
@@ -92,7 +94,7 @@ const KIMI_K31_PRO_LEVELS = {
92
94
  function officialMinimaxEndpoint(baseUrl) {
93
95
  try {
94
96
  const parsed = new URL(baseUrl.trim());
95
- if (parsed.origin !== MINIMAX_ORIGIN)
97
+ if (!MINIMAX_ORIGINS.has(parsed.origin))
96
98
  return false;
97
99
  const path = parsed.pathname.replace(/\/+$/, "");
98
100
  return MINIMAX_PATHS.has(path);
@@ -110,6 +112,11 @@ function officialKimiEndpoint(baseUrl) {
110
112
  return false;
111
113
  }
112
114
  }
115
+ /** Match the documented API and model, never a same-named model at an unrelated proxy. */
116
+ export function isOfficialMinimaxFlash(model) {
117
+ return typeof model.id === "string" && model.id.toLowerCase() === MINIMAX_FLASH_MODEL
118
+ && model.api === DEFAULT_API && typeof model.baseUrl === "string" && officialMinimaxEndpoint(model.baseUrl);
119
+ }
113
120
  /**
114
121
  * The levels a provider DECLARED for a model, when it declared them properly.
115
122
  *
@@ -31,6 +31,8 @@ export function formatTurnFailure(error) {
31
31
  if (controllerAborted(error))
32
32
  return "本轮任务已停止。";
33
33
  const raw = error instanceof Error ? error.message : typeof error === "string" ? error : "";
34
+ if (/^上下文(?:自动)?压缩失败/.test(raw))
35
+ return raw;
34
36
  if (/^(本地模型通道|模型通道在回复完成前断开)/.test(raw))
35
37
  return raw;
36
38
  const lower = raw.toLowerCase();
@@ -89,7 +91,7 @@ export function formatTurnFailure(error) {
89
91
  // reader's choices are to start a new conversation or use a model with a bigger window. Saying
90
92
  // "the model refused" would hide both.
91
93
  if (/context[\s_]*(?:length|window|size)[\s_]*(?:exceeded|exceeds|too large|limit)|maximum context|context limit|too many tokens|prompt is too long|reduce the length of the messages|exceeds the maximum|上下文(?:超|过)限|上下文长度/.test(lower)) {
92
- return "这一轮的内容超过了模型的上下文上限,模型没能读下整段对话。可以新开一个会话继续,或换一个上下文窗口更大的模型(Pi 会在超限前自动压缩,这轮没能完成压缩)。";
94
+ return "当前上下文达到模型的上下文上限或挤占了必要的回答空间;这是上下文容量问题,不是正文达到单次输出上限。请查看上下文压缩状态;压缩失败时可换一个上下文窗口更大的模型后继续,或新开一个会话并带上必要资料。";
93
95
  }
94
96
  // IMAGE MISMATCH - explicit by the caller, no need to translate.
95
97
  if (lower.includes("不支持图片输入")) {
@@ -9,6 +9,7 @@ import type { BackgroundTaskDetails } from "../shared/background-task.js";
9
9
  import type { GroupActivityUpdate } from "../shared/group-chat.js";
10
10
  import type { ScheduledTask } from "./scheduled-tasks.js";
11
11
  import type { ThinkingLevelMap } from "../shared/thinking-levels.js";
12
+ import type { ConversationCompaction } from "../shared/context-compaction.js";
12
13
  import type { TerminalCommand, TerminalEvent } from "../shared/terminal-protocol.js";
13
14
  /**
14
15
  * The lifecycle states a `Conversation.status` moves through while a turn is in flight. `waiting` is the
@@ -491,6 +492,7 @@ export type ModelCatalog = {
491
492
  configError?: string;
492
493
  };
493
494
  export type Conversation = {
495
+ compaction?: ConversationCompaction;
494
496
  id: string;
495
497
  title: string;
496
498
  workspaceId: string;
@@ -771,6 +773,11 @@ export type ConversationContextUsage = {
771
773
  tokens: number | null;
772
774
  contextWindow: number;
773
775
  percent: number | null;
776
+ estimated?: boolean;
777
+ reserveTokens?: number;
778
+ keepRecentTokens?: number;
779
+ autoCompact?: boolean;
780
+ outputReserveTokens?: number;
774
781
  };
775
782
  /**
776
783
  * ONE TOOL CALL, as the card shows it, and the boundary that keeps a child's turn out of `state.json`.
@@ -1677,6 +1684,12 @@ export type ClientCommandBody =
1677
1684
  conversationId: string;
1678
1685
  provider: string;
1679
1686
  modelId: string;
1687
+ } | {
1688
+ type: "conversation.compact";
1689
+ conversationId: string;
1690
+ } | {
1691
+ type: "conversation.compact.cancel";
1692
+ conversationId: string;
1680
1693
  } | {
1681
1694
  type: "conversation.thinking";
1682
1695
  conversationId: string;
@@ -586,7 +586,7 @@ export class WsServer {
586
586
  // chain immediately: waiting for the model turn would block later priority/cancel commands and
587
587
  // make a second send miss the durable queue. The service owns per-conversation run claims.
588
588
  const runIndependently = parsed.type === "message.send" || parsed.type === "message.sendQueued"
589
- || parsed.type === "message.interrupt" || parsed.type === "run.stop" || parsed.type === "message.revert";
589
+ || parsed.type === "message.interrupt" || parsed.type === "run.stop" || parsed.type === "message.revert" || parsed.type === "conversation.compact" || parsed.type === "conversation.compact.cancel";
590
590
  // ...WHICH MEANS A SEND CAN OVERTAKE A REVERT SENT JUST BEFORE IT, because the revert below is
591
591
  // queued behind whatever this socket was already doing. The service only learns it is cutting
592
592
  // when the command EXECUTES, so a send handled in between would find the conversation open and
@@ -0,0 +1,15 @@
1
+ export type ContextCompactionEvent = {
2
+ phase: "start" | "end";
3
+ reason: "threshold" | "overflow" | "manual";
4
+ tokensBefore?: number;
5
+ estimatedTokensAfter?: number;
6
+ error?: string;
7
+ aborted?: boolean;
8
+ willRetry?: boolean;
9
+ };
10
+ export type ConversationCompaction = Omit<ContextCompactionEvent, "phase"> & {
11
+ status: "running" | "completed" | "failed" | "cancelled";
12
+ startedAt: string;
13
+ finishedAt?: string;
14
+ };
15
+ export declare function readCompactionEvent(value: unknown): ContextCompactionEvent | undefined;
@@ -0,0 +1,14 @@
1
+ export function readCompactionEvent(value) {
2
+ if (!value || typeof value !== "object")
3
+ return;
4
+ const record = value;
5
+ if (record.type !== "compaction_start" && record.type !== "compaction_end")
6
+ return;
7
+ const count = (number) => typeof number === "number" && Number.isFinite(number) && number >= 0 ? number : undefined;
8
+ return { phase: record.type === "compaction_start" ? "start" : "end",
9
+ reason: record.reason === "threshold" || record.reason === "overflow" ? record.reason : "manual",
10
+ tokensBefore: count(record.result?.tokensBefore), estimatedTokensAfter: count(record.result?.estimatedTokensAfter),
11
+ ...(typeof record.errorMessage === "string" && record.errorMessage.trim() ? { error: record.errorMessage.trim().slice(0, 1600) }
12
+ : record.type === "compaction_end" && !record.aborted && !record.result ? { error: "Pi 未返回上下文压缩结果,请重试或检查服务日志。" } : {}),
13
+ aborted: record.aborted === true, willRetry: record.willRetry === true };
14
+ }
@@ -0,0 +1,22 @@
1
+ export declare function contextMeter(conversation: {
2
+ contextUsage?: {
3
+ tokens: number | null;
4
+ contextWindow: number;
5
+ estimated?: boolean;
6
+ reserveTokens?: number;
7
+ outputReserveTokens?: number;
8
+ autoCompact?: boolean;
9
+ };
10
+ compaction?: {
11
+ status: string;
12
+ estimatedTokensAfter?: number;
13
+ };
14
+ }, fallbackWindow?: number): {
15
+ used: number | undefined;
16
+ window: number;
17
+ percent: number | null;
18
+ fill: number;
19
+ reserve: number;
20
+ risk: "caution" | "danger" | "safe" | "unknown";
21
+ estimated: boolean;
22
+ };
@@ -0,0 +1,9 @@
1
+ export function contextMeter(conversation, fallbackWindow) {
2
+ const usage = conversation.contextUsage, window = usage?.contextWindow ?? fallbackWindow ?? 0;
3
+ const estimate = usage?.tokens == null ? conversation.compaction?.estimatedTokensAfter : undefined, used = usage?.tokens ?? estimate;
4
+ const known = typeof used === "number" && Number.isFinite(used) && used >= 0 && window > 0;
5
+ const reserve = Math.min(window, Math.max(usage?.autoCompact === false ? 0 : usage?.reserveTokens ?? Math.min(16384, window / 4), (usage?.outputReserveTokens ?? Math.min(8192, Math.max(1024, window / 8))) + 4096));
6
+ const percent = known ? used / window * 100 : null, usable = Math.max(1, window - reserve);
7
+ const risk = !known ? "unknown" : used >= usable || percent >= 95 ? "danger" : used >= usable * .85 || percent >= 80 ? "caution" : "safe";
8
+ return { used: known ? used : undefined, window, percent, fill: percent === null ? 0 : Math.min(100, Math.max(0, percent)), reserve, risk, estimated: known && (usage?.estimated === true || usage?.tokens == null) };
9
+ }
@@ -61,6 +61,7 @@ export type GroupRsi = {
61
61
  notice?: string;
62
62
  requestedAt?: string;
63
63
  automaticImprovements?: boolean;
64
+ automaticImprovementsAuthorized?: boolean;
64
65
  };
65
66
  export type GroupModerator = {
66
67
  model?: GroupModel;
@@ -90,6 +91,8 @@ export type GroupCloneRequest = {
90
91
  export type GroupStatus = "queued" | "running" | "completed" | "failed" | "cancelled" | "interrupted";
91
92
  export type GroupExecutionActivity = {
92
93
  kind: "thinking";
94
+ } | {
95
+ kind: "compaction";
93
96
  } | {
94
97
  kind: "tool";
95
98
  name: string;
@@ -103,6 +106,9 @@ type GroupExecutionStepBase = {
103
106
  export type GroupExecutionStep = GroupExecutionStepBase & ({
104
107
  kind: "thinking";
105
108
  text: string;
109
+ } | {
110
+ kind: "compaction";
111
+ text: string;
106
112
  } | {
107
113
  kind: "tool";
108
114
  name: string;
@@ -4,6 +4,38 @@ export type RsiSource = {
4
4
  id: string;
5
5
  workspaceId: string;
6
6
  };
7
+ export type RsiRepair = {
8
+ id: string;
9
+ source: RsiSource;
10
+ reportId: string;
11
+ findingId?: string;
12
+ title: string;
13
+ conversationId: string;
14
+ status: "discussing" | "implementing" | "review" | "completed" | "failed" | "interrupted" | "cancelled";
15
+ createdAt: string;
16
+ updatedAt: string;
17
+ approvedAt?: string;
18
+ approvedRevision?: string;
19
+ approvedPlan?: string;
20
+ error?: string;
21
+ initialMessageId?: string;
22
+ executionMessageId?: string;
23
+ approvals?: {
24
+ at: string;
25
+ revision: string;
26
+ plan: string;
27
+ }[];
28
+ };
29
+ export type RsiRepairView = RsiRepair & {
30
+ busy: boolean;
31
+ revision?: string;
32
+ proposedPlan?: string;
33
+ messages: {
34
+ id: string;
35
+ role: "user" | "assistant";
36
+ content: string;
37
+ }[];
38
+ };
7
39
  export type RsiImprovement = {
8
40
  id: string;
9
41
  key: string;
@@ -25,6 +57,7 @@ export type RsiImprovement = {
25
57
  };
26
58
  export type RsiSnapshot = {
27
59
  state: GroupRsi;
60
+ repairs?: RsiRepairView[];
28
61
  improvements: RsiImprovement[];
29
62
  capabilities: {
30
63
  slug: string;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "superPi": {
4
4
  "managedDirectListener": true
5
5
  },
6
- "version": "0.1.281",
6
+ "version": "0.1.283",
7
7
  "description": "Web control plane and companion for Pi",
8
8
  "license": "MIT",
9
9
  "type": "module",
@@ -54,6 +54,10 @@
54
54
  "pack:server": "npm run build && node scripts/pack-server.mjs"
55
55
  },
56
56
  "check:logic": [
57
+ "scripts/context-compaction.test.mjs",
58
+ "scripts/minimax-output.test.mjs",
59
+ "scripts/rsi-repairs.test.mjs",
60
+ "scripts/rsi-observer-pi.test.mjs",
57
61
  "scripts/model-discovery.test.mjs",
58
62
  "scripts/tab-content-layout.test.mjs",
59
63
  "scripts/rsi-capability-loop.test.mjs",
@@ -331,6 +335,8 @@
331
335
  "scripts/session-reminder-pmo-rights.test.mjs"
332
336
  ],
333
337
  "check:browser": [
338
+ "scripts/context-meter-browser.test.mjs",
339
+ "scripts/rsi-sidebar-browser.test.mjs",
334
340
  "scripts/model-discovery-browser.test.mjs",
335
341
  "scripts/tab-content-browser.test.mjs",
336
342
  "scripts/rsi-session-browser.test.mjs",