@evandu/super-pi 0.1.224 → 0.1.225

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 (51) hide show
  1. package/dist/client/.super-pi-client-sources.json +7 -6
  2. package/dist/client/assets/{CodeMirrorEditor-CophJ9Eb.js → CodeMirrorEditor-OLbwCzUh.js} +2 -2
  3. package/dist/client/assets/{dist-BGVYvXYo.js → dist-BQImRXFO.js} +1 -1
  4. package/dist/client/assets/{dist-CFMeFXLU.js → dist-BUOU8RAm.js} +1 -1
  5. package/dist/client/assets/{dist-ukIdoKjd.js → dist-BdrKprbZ.js} +1 -1
  6. package/dist/client/assets/{dist-C0s45hG8.js → dist-BffuhHE1.js} +1 -1
  7. package/dist/client/assets/{dist-nSNA_ze3.js → dist-C8-e07vM.js} +1 -1
  8. package/dist/client/assets/{dist-DBdR1Wph.js → dist-C8naDDXu.js} +1 -1
  9. package/dist/client/assets/{dist-Ch4dVwk4.js → dist-C9f0Riun.js} +1 -1
  10. package/dist/client/assets/{dist-CEbbDGoy.js → dist-CB9Qr9C6.js} +1 -1
  11. package/dist/client/assets/{dist-D-s1GEbP.js → dist-CIEDxNn7.js} +1 -1
  12. package/dist/client/assets/{dist-Clk6Knpv.js → dist-CIsUvpC9.js} +1 -1
  13. package/dist/client/assets/{dist-i8fMtdTP.js → dist-CKA2u-s7.js} +1 -1
  14. package/dist/client/assets/{dist-CxeSxC9m.js → dist-COIAnYiN.js} +1 -1
  15. package/dist/client/assets/{dist-TcwefdPn.js → dist-CXpar2cD.js} +1 -1
  16. package/dist/client/assets/{dist-C3amppza.js → dist-CZu3CzDX.js} +1 -1
  17. package/dist/client/assets/{dist-C8FqYuNv.js → dist-CaePDjkH.js} +1 -1
  18. package/dist/client/assets/{dist-D7On6h3N.js → dist-Cro2exEx.js} +1 -1
  19. package/dist/client/assets/{dist-pVo8vQ15.js → dist-CxJkT0iM.js} +1 -1
  20. package/dist/client/assets/{dist-CMPtWQjx.js → dist-D53uN01l.js} +1 -1
  21. package/dist/client/assets/{dist-ZuNGtw_8.js → dist-D9kySnbi.js} +1 -1
  22. package/dist/client/assets/{dist-C3BgT3PU.js → dist-MARoODS1.js} +1 -1
  23. package/dist/client/assets/{dist-BYjXg164.js → dist-ot9lBE7N.js} +1 -1
  24. package/dist/client/assets/{dist-BxcN7lnR.js → dist-sv2AtsDR.js} +1 -1
  25. package/dist/client/assets/{index-CoJT13t0.js → index-B0m5CXCB.js} +39 -39
  26. package/dist/client/index.html +1 -1
  27. package/dist/server/application/extension-ui-service.d.ts +6 -1
  28. package/dist/server/application/extension-ui-service.js +31 -2
  29. package/dist/server/application/run-service.d.ts +4 -1
  30. package/dist/server/application/run-service.js +58 -18
  31. package/dist/server/command-handler.js +11 -2
  32. package/dist/server/exec-service.d.ts +5 -2
  33. package/dist/server/exec-service.js +12 -2
  34. package/dist/server/http-server.js +40 -9
  35. package/dist/server/index.js +14 -9
  36. package/dist/server/persistence/app-state-repository.d.ts +3 -1
  37. package/dist/server/persistence/sqlite-state-repository.js +14 -1
  38. package/dist/server/store.d.ts +1 -0
  39. package/dist/server/store.js +49 -12
  40. package/dist/server/terminal.d.ts +26 -76
  41. package/dist/server/terminal.js +239 -172
  42. package/dist/server/tool-llm.d.ts +7 -0
  43. package/dist/server/tool-llm.js +8 -1
  44. package/dist/server/tool-runner.d.ts +7 -0
  45. package/dist/server/tool-runner.js +8 -1
  46. package/dist/server/types.d.ts +23 -75
  47. package/dist/server/ws-server.d.ts +7 -12
  48. package/dist/server/ws-server.js +23 -18
  49. package/dist/shared/terminal-protocol.d.ts +71 -0
  50. package/dist/shared/terminal-protocol.js +1 -0
  51. package/package.json +1 -1
@@ -1114,6 +1114,10 @@ export class Store {
1114
1114
  hasMessage(conversationId, messageId) {
1115
1115
  return Boolean(this.state.conversations.find((item) => item.id === conversationId)?.messages.some((message) => message.id === messageId));
1116
1116
  }
1117
+ hasAcceptedSend(conversationId, clientId) {
1118
+ const conversation = this.state.conversations.find(item => item.id === conversationId);
1119
+ return Boolean(conversation?.acceptedSendIds?.includes(clientId) || conversation?.messages.some(message => message.id === clientId && message.role === "user"));
1120
+ }
1117
1121
  /**
1118
1122
  * One conversation and its workspace, as DETACHED copies, without cloning the whole state.
1119
1123
  *
@@ -1267,7 +1271,8 @@ export class Store {
1267
1271
  const { projects: _projects, ...publicState } = this.state;
1268
1272
  const closed = new Set(this.state.workspaces.filter((item) => item.closedAt).map((item) => item.id));
1269
1273
  const workspaces = this.state.workspaces.filter((item) => !closed.has(item.id));
1270
- const visible = this.state.conversations.filter((item) => !closed.has(item.workspaceId) && !item.warmSession);
1274
+ const visible = this.state.conversations.filter((item) => !closed.has(item.workspaceId) && !item.warmSession)
1275
+ .map(({ acceptedSendIds: _accepted, ...conversation }) => conversation);
1271
1276
  const warmSessions = workspaces.flatMap((workspace) => this.listWarmSessions(workspace.id));
1272
1277
  const presets = warmSessions.length ? { warmSessions } : {};
1273
1278
  const openEscalations = this.openEscalationAudiences();
@@ -1903,13 +1908,30 @@ export class Store {
1903
1908
  conversation.messages.push(message);
1904
1909
  // Persisted as a transcript change and nothing more: this is the honest record of "the user said this
1905
1910
  // and nothing has picked it up yet", and a refresh has to show it in the same place it was sent from.
1906
- if (message.delivery)
1907
- await this.persistMessages(conversation);
1908
- else
1909
- await this.persistIncomingTurn(conversation, message);
1911
+ const acceptedSendId = options?.clientId === message.id ? message.id : undefined;
1912
+ const previousMetadata = !message.delivery && acceptedSendId
1913
+ ? { title: conversation.title, updatedAt: conversation.updatedAt, status: conversation.status, error: conversation.error, warning: conversation.warning }
1914
+ : undefined;
1915
+ if (acceptedSendId)
1916
+ (conversation.acceptedSendIds ??= []).push(acceptedSendId);
1917
+ try {
1918
+ if (message.delivery)
1919
+ await this.persistMessages(conversation, acceptedSendId);
1920
+ else
1921
+ await this.persistIncomingTurn(conversation, message, acceptedSendId);
1922
+ }
1923
+ catch (error) {
1924
+ if (acceptedSendId) {
1925
+ conversation.messages = conversation.messages.filter(item => item !== message);
1926
+ conversation.acceptedSendIds = conversation.acceptedSendIds?.filter(id => id !== acceptedSendId);
1927
+ if (previousMetadata && conversation.updatedAt === message.createdAt && conversation.status === "waiting")
1928
+ Object.assign(conversation, previousMetadata);
1929
+ }
1930
+ throw error;
1931
+ }
1910
1932
  return structuredClone(message);
1911
1933
  }
1912
- async persistIncomingTurn(conversation, message) {
1934
+ async persistIncomingTurn(conversation, message, acceptedSendId) {
1913
1935
  const firstUser = conversation.messages.find((item) => item.role === "user");
1914
1936
  const autoTitle = firstUser?.content.trim()
1915
1937
  ? firstUser.content.slice(0, 42)
@@ -1943,8 +1965,10 @@ export class Store {
1943
1965
  // The conversation's metadata (status, title, updatedAt) and its messages are exactly what
1944
1966
  // changed, and the repository has an incremental path for each. The base snapshot every other
1945
1967
  // conversation lives in is untouched by this call, so rewriting it buys nothing.
1946
- await this.persistMessages(conversation);
1947
- await this.persistMetadata(conversation.id);
1968
+ const { messages: _messages, acceptedSendIds: _receipts, ...metadata } = conversation;
1969
+ await this.persistMessages(conversation, acceptedSendId, acceptedSendId ? metadata : undefined);
1970
+ if (!acceptedSendId)
1971
+ await this.persistMetadata(conversation.id);
1948
1972
  }
1949
1973
  /**
1950
1974
  * Takes the browser's id for its own optimistic message, when it is safe to.
@@ -2339,8 +2363,21 @@ export class Store {
2339
2363
  if (patch.thinkingBlock !== undefined)
2340
2364
  call.thinkingBlock = patch.thinkingBlock;
2341
2365
  // The question rides on the call rather than beside it, so this is the only write it needs.
2342
- if (patch.question !== undefined)
2366
+ if (patch.question !== undefined && (!call.question || call.question.status === "pending")) {
2367
+ const previous = call.question;
2343
2368
  call.question = patch.question;
2369
+ if (patch.question.status !== "pending") {
2370
+ try {
2371
+ await this.persistToolCallMessage(conversationId, messageId);
2372
+ }
2373
+ catch (error) {
2374
+ if (call.question === patch.question)
2375
+ call.question = previous;
2376
+ throw error;
2377
+ }
2378
+ return structuredClone(calls);
2379
+ }
2380
+ }
2344
2381
  conversation.updatedAt = now();
2345
2382
  await this.persistToolCallMessage(conversationId, messageId);
2346
2383
  return structuredClone(calls);
@@ -2561,8 +2598,8 @@ export class Store {
2561
2598
  throw new Error(`等待中的消息不存在或已被处理:${messageId}`);
2562
2599
  return { conversation, message };
2563
2600
  }
2564
- async persistMessages(conversation) {
2565
- await this.persistTransaction({ kind: "conversation.messages", conversationId: conversation.id, messages: structuredClone(conversation.messages), updatedAt: conversation.updatedAt });
2601
+ async persistMessages(conversation, acceptedSendId, metadata) {
2602
+ await this.persistTransaction({ kind: "conversation.messages", conversationId: conversation.id, messages: structuredClone(conversation.messages), updatedAt: conversation.updatedAt, ...(acceptedSendId ? { acceptedSendId } : {}), ...(metadata ? { metadata: structuredClone(metadata) } : {}) });
2566
2603
  }
2567
2604
  /**
2568
2605
  * Claims one waiting message for delivery to the live Pi process: `waiting` becomes `sending`, and the
@@ -3137,7 +3174,7 @@ export class Store {
3137
3174
  await this.persistTransaction({ kind: "collections.replace", values: { [key]: structuredClone(value) } });
3138
3175
  }
3139
3176
  async persistMetadata(conversationId) {
3140
- const { messages: _messages, ...metadata } = this.requireConversation(conversationId);
3177
+ const { messages: _messages, acceptedSendIds: _receipts, ...metadata } = this.requireConversation(conversationId);
3141
3178
  await this.persistTransaction({ kind: "conversation.metadata", conversationId, metadata: structuredClone(metadata) });
3142
3179
  }
3143
3180
  async persist() { await this.repository.save(this.state); }
@@ -25,12 +25,10 @@
25
25
  * 2. NETWORK SCOPE. Even enabled, a command is refused unless the WebSocket arrived from loopback or
26
26
  * an RFC 1918 / link-local / ULA address (`isLocalOrPrivateAddress`, the same predicate the file
27
27
  * endpoints use). A request that arrives from a routed or proxied address is refused by name.
28
- * 3. THE BROWSER NEVER CHOOSES THE WORKING DIRECTORY. `terminal.open` carries a WORKSPACE ID and
29
- * nothing else; this file resolves the id against the store and spawns in that workspace's path.
30
- * A browser that asked for `C:\` or `/etc` gets its own workspace's directory instead, because
31
- * there is no code path on which a client-supplied path reaches `spawn`.
32
- * 4. BOUNDED SESSIONS. `MAX_SESSIONS` live shells, capped so a loop of reconnects cannot accumulate
33
- * processes.
28
+ * 3. WORKSPACE SCOPE. The server resolves a workspace ID and validates the relative starting directory.
29
+ * 4. BOUNDED SESSIONS. Shells and retained output count toward fixed limits.
30
+ * 5. REATTACHMENT. Browser ownership scopes session discovery; only a subscribed socket may
31
+ * address a slot. Losing a subscription does not end the shell.
34
32
  *
35
33
  * What is NOT here is a per-command allowlist. A terminal that refuses `rm` or `curl` is not a terminal,
36
34
  * and pretending otherwise would be worse than the honest version: with (1) and (2) the operator has
@@ -48,7 +46,8 @@
48
46
  * `node-pty` allocates a real pseudo-terminal (ConPTY on Windows, `forkpty` elsewhere), which is the
49
47
  * only way to get them. The cost is a native module, and it is paid only by operators who set
50
48
  */
51
- import type { ServerEvent, Workspace } from "./types.js";
49
+ import type { Workspace } from "./types.js";
50
+ import { type TerminalCommand, type TerminalEvent } from "../shared/terminal-protocol.js";
52
51
  /**
53
52
  * WHICH SHELL, and why it is not `COMSPEC`.
54
53
  *
@@ -116,62 +115,15 @@ export declare function terminalShellCandidates(env?: NodeJS.ProcessEnv): {
116
115
  * otherwise leave them behind.
117
116
  */
118
117
  export declare const MAX_SESSIONS = 256;
119
- /**
120
- * The terminal's wire protocol, as members of the EXISTING `ClientCommand` / `ServerEvent` unions rather
121
- * than a second transport.
122
- *
123
- * Reusing the command channel is what makes this feature cheap to wire: the WebSocket already carries
124
- * authentication, and `ws-server.ts` already answers exactly one socket. A parallel HTTP endpoint with
125
- * server-sent events would have had to re-implement both, and the terminal's traffic is bidirectional -
126
- * keystrokes have to flow back the same way output does.
127
- */
128
- export type TerminalCommand = {
129
- type: "terminal.open";
130
- sessionId: string;
131
- workspaceId: string;
132
- cwd?: string;
133
- cols?: number;
134
- rows?: number;
135
- } | {
136
- type: "terminal.input";
137
- sessionId: string;
138
- data: string;
139
- } | {
140
- type: "terminal.resize";
141
- sessionId: string;
142
- cols: number;
143
- rows: number;
144
- } | {
145
- type: "terminal.close";
146
- sessionId: string;
147
- };
148
- export type TerminalEvent = {
149
- type: "terminal.output";
150
- sessionId: string;
151
- data: string;
152
- } | {
153
- type: "terminal.exit";
154
- sessionId: string;
155
- exitCode: number;
156
- } | {
157
- type: "terminal.error";
158
- sessionId: string;
159
- message: string;
160
- } | {
161
- type: "terminal.status";
162
- enabled: boolean;
163
- reason?: string;
164
- };
165
- /**
166
- * The terminal is built in; node-pty availability is checked when a session opens.
167
- */
118
+ type TerminalSender = (event: TerminalEvent) => void;
119
+ /** The terminal is built in. Native PTY availability is checked on use. */
168
120
  export declare function isTerminalEnabled(env?: NodeJS.ProcessEnv): boolean;
169
- /** The sentence a browser shows when the terminal is missing. Says WHICH of the three gates closed. */
170
121
  export declare function terminalDisabledReason(_env?: NodeJS.ProcessEnv): string;
171
122
  export declare class TerminalService {
172
- private readonly sessions;
123
+ private readonly groups;
124
+ private readonly subscriptions;
125
+ private closing;
173
126
  private readonly workspaceFor;
174
- /** Whether a command from this peer is allowed to touch a shell at all. */
175
127
  private readonly isTrustedPeer;
176
128
  private readonly configuredShell;
177
129
  private readonly saveConfiguredShell;
@@ -190,24 +142,22 @@ export declare class TerminalService {
190
142
  });
191
143
  readonly enabled: boolean;
192
144
  setShell(command: string | null, remoteAddress: string | undefined): Promise<boolean>;
193
- /**
194
- * WHY THE PEER CHECK LIVES ON EVERY COMMAND AND NOT ONLY ON `open`.
195
- *
196
- * The obvious implementation authorises `terminal.open` and then trusts the session. That leaves a
197
- * live shell reachable by a socket opened BEFORE the address changed - and, on this server, the
198
- * browser is same-origin on whatever host it was opened from, so a user who tunnels in, opens a
199
- * terminal, and then forwards their port to a colleague has silently widened the audience for every
200
- * keystroke that follows.
201
- *
202
- * `isTrustedPeer` is re-evaluated per command. It is a pure function of the socket's remote address,
203
- * so the cost is one predicate per keystroke and the guarantee is that no session outlives its
204
- * socket's authorisation.
205
- */
206
145
  private assertUsable;
207
- handle(command: TerminalCommand, remoteAddress: string | undefined, send: (event: ServerEvent) => void): Promise<void>;
208
- private enqueue;
146
+ handle(command: TerminalCommand, remoteAddress: string | undefined, send: TerminalSender): Promise<void>;
147
+ private sync;
148
+ private open;
149
+ private spawn;
150
+ private slots;
151
+ private sendSessions;
152
+ private publish;
209
153
  private flush;
210
- private kill;
211
- /** Every shell, for shutdown. Exits without reporting anything - the server is going away. */
154
+ private recordExit;
155
+ private stop;
156
+ private errorMessage;
157
+ /** Losing a reader never changes a shell or its output history. */
158
+ detach(send: TerminalSender): void;
159
+ private pruneEmptyGroup;
160
+ removeWorkspace(workspaceId: string): void;
212
161
  closeAll(): void;
213
162
  }
163
+ export {};