@evandu/super-pi 0.1.271 → 0.1.273

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.
@@ -1,3 +1,12 @@
1
+ const recordedFailures = new WeakSet();
2
+ export function recordedTurnFailure(error) {
3
+ const failure = error instanceof Error ? error : new Error(String(error));
4
+ recordedFailures.add(failure);
5
+ return failure;
6
+ }
7
+ export function isRecordedTurnFailure(error) {
8
+ return error instanceof Error && recordedFailures.has(error);
9
+ }
1
10
  /**
2
11
  * Translates a thrown error from the turn loop into a sentence a person reads.
3
12
  *
@@ -22,6 +31,8 @@ export function formatTurnFailure(error) {
22
31
  if (controllerAborted(error))
23
32
  return "本轮任务已停止。";
24
33
  const raw = error instanceof Error ? error.message : typeof error === "string" ? error : "";
34
+ if (/^(本地模型通道|模型通道在回复完成前断开)/.test(raw))
35
+ return raw;
25
36
  const lower = raw.toLowerCase();
26
37
  // PROVIDER QUOTA / RATE LIMIT - the only reason the user is in this branch is because Pi was
27
38
  // answering them a moment ago and now the model will not let it. The action is "wait or change
@@ -2143,6 +2143,7 @@ export type ServerEvent = {
2143
2143
  } | {
2144
2144
  type: "error";
2145
2145
  message: string;
2146
+ recordedTurnFailure?: boolean;
2146
2147
  } | {
2147
2148
  type: "extension.ui";
2148
2149
  conversationId: string;
@@ -40,6 +40,12 @@ export declare function packageUpdateArgs(rootDir: string, version?: string, pla
40
40
  args: string[];
41
41
  prefix?: string;
42
42
  };
43
+ /** Managed installs are private npm projects underneath the instance directory. */
44
+ export declare function managedUpdatePlan(rootDir: string, config: string, version?: string): {
45
+ launcher: string;
46
+ pidFile: string;
47
+ args: string[];
48
+ };
43
49
  /** Claims the update slot. False when an update is already running. */
44
50
  export declare function claimUpdateSlot(): boolean;
45
51
  /** Releases the update slot, so the next update can start. */
@@ -1,5 +1,5 @@
1
1
  import { readFileSync } from "node:fs";
2
- import { join, resolve } from "node:path";
2
+ import { basename, dirname, join, resolve } from "node:path";
3
3
  import { npmInstallPrefix } from "../../bin/npm-prefix.js";
4
4
  /**
5
5
  * Quote an argument for a shell on Windows.
@@ -82,6 +82,21 @@ export function packageUpdateArgs(rootDir, version, platform = process.platform)
82
82
  }
83
83
  return { args: ["install", "-g", "--fetch-timeout=900000", "--fetch-retries=8", "--fetch-retry-maxtimeout=120000", target] };
84
84
  }
85
+ /** Managed installs are private npm projects underneath the instance directory. */
86
+ export function managedUpdatePlan(rootDir, config, version) {
87
+ const instance = dirname(resolve(config));
88
+ const prefix = resolve(rootDir, "../../..");
89
+ if (basename(config) !== "models.json" || dirname(prefix) !== instance ||
90
+ !/^runtime-[a-f0-9]{16}$/.test(basename(prefix)) ||
91
+ resolve(rootDir) !== join(prefix, "node_modules", "@evandu", "super-pi-server")) {
92
+ throw new Error("当前服务的安装路径与托管实例不匹配,无法确定升级位置。");
93
+ }
94
+ return {
95
+ launcher: join(instance, "start-service.sh"),
96
+ pidFile: join(instance, "service.pid"),
97
+ args: ["install", "--prefix", prefix, "--omit=dev", "--no-audit", "--no-fund", "--fetch-timeout=900000", "--fetch-retries=3", `@evandu/super-pi-server@${version ?? "latest"}`],
98
+ };
99
+ }
85
100
  /**
86
101
  * ONE UPDATE AT A TIME.
87
102
  *
@@ -1,7 +1,9 @@
1
1
  import { isBrowserAuthorized } from "./browser-access.js";
2
2
  import { WebSocket, WebSocketServer } from "ws";
3
+ import { remoteModelRelay } from "./managed-model-relay.js";
3
4
  import { isAuthorized, isLoopbackAddress } from "./http-server.js";
4
5
  import { describeThrown, describeThrownWithStack } from "./failure-text.js";
6
+ import { isRecordedTurnFailure } from "./turn-failure.js";
5
7
  /**
6
8
  * A WebSocket is a live transport, not an unbounded queue. `ws.send()` only
7
9
  * copies bytes into the socket's userland buffer; a browser that is frozen or
@@ -389,6 +391,18 @@ export class WsServer {
389
391
  return;
390
392
  }
391
393
  const url = new URL(request.url ?? "/", `http://${request.headers.host ?? "localhost"}`);
394
+ if (url.pathname === "/api/managed-model/channel") {
395
+ if (!process.env.SUPER_PI_MANAGED_CONFIG || !browserAuthorized) {
396
+ socket.destroy();
397
+ return;
398
+ }
399
+ this.websocketServer.handleUpgrade(request, socket, head, ws => {
400
+ const release = this.browserAccess.onRevoke(() => ws.terminate());
401
+ ws.once("close", release);
402
+ remoteModelRelay.attach(ws);
403
+ });
404
+ return;
405
+ }
392
406
  const managed = /^\/managed\/([a-f0-9-]+)(\/ws)$/.exec(url.pathname);
393
407
  if (managed) {
394
408
  if (!this.managedServers || !isAuthorized(request, this.accessToken)
@@ -607,6 +621,7 @@ export class WsServer {
607
621
  safeSend(socket, JSON.stringify({
608
622
  type: "error",
609
623
  message: reason || "服务端没有说明这次失败的原因。",
624
+ ...(isRecordedTurnFailure(error) ? { recordedTurnFailure: true } : {}),
610
625
  }));
611
626
  }
612
627
  finally {
@@ -636,6 +651,7 @@ export class WsServer {
636
651
  // A pending state window must not outlive the server: the timer holds the event loop
637
652
  // open, and a flush after the sockets are closed would send to a dead peer.
638
653
  this.broadcaster.dispose();
654
+ remoteModelRelay.close();
639
655
  for (const socket of this.broadcaster.getSockets()) {
640
656
  socket.close(1001, "server-shutdown");
641
657
  }
@@ -46,6 +46,8 @@ export interface ManagedServerProfile {
46
46
  ssh: "offline" | "connecting" | "online" | "needsTrust" | "awaitingVerification";
47
47
  service: "unknown" | "offline" | "online";
48
48
  modelChannel: "offline" | "online";
49
+ modelTransport?: "direct";
50
+ modelChannelPaused?: boolean;
49
51
  phase: "idle" | "connecting" | "needsTrust" | "awaitingVerification" | "deploying" | "ready" | "failed";
50
52
  verification?: SshVerification;
51
53
  existingService?: {
@@ -19,6 +19,7 @@ export type UpdateOperation = {
19
19
  };
20
20
  };
21
21
  export type UpdateStatus = UpdateVersions & {
22
+ serviceSelfUpdate?: boolean;
22
23
  operation: UpdateOperation | null;
23
24
  };
24
25
  export type UpdateCheck = UpdateStatus & {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "superPi": {
4
4
  "managedDirectListener": true
5
5
  },
6
- "version": "0.1.271",
6
+ "version": "0.1.273",
7
7
  "description": "Web control plane and companion for Pi",
8
8
  "license": "MIT",
9
9
  "type": "module",