@wkqco33/pi-remote-control 0.1.1 → 0.1.2
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/package.json +1 -1
- package/src/config.test.ts +22 -2
- package/src/config.ts +38 -0
- package/src/index.ts +36 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wkqco33/pi-remote-control",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Remote control extension for pi coding agent: stream live sessions, steer/followUp prompts, and approve tool calls from a web relay server.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/src/config.test.ts
CHANGED
|
@@ -1,18 +1,38 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { parseRemoteConfig, resolveRemoteConfig } from "./config.js";
|
|
2
|
+
import { expandHomePath, parseRemoteConfig, parseTokenFileContent, resolveRemoteConfig } from "./config.js";
|
|
3
3
|
|
|
4
4
|
describe("remote config", () => {
|
|
5
5
|
it("rejects raw tokens and resolves env overrides", () => {
|
|
6
6
|
const parsed = parseRemoteConfig({
|
|
7
7
|
token: "secret",
|
|
8
8
|
serverUrl: "wss://relay.example.com/ws/agent",
|
|
9
|
+
tokenFile: "~/.pi/agent/remote-control.token.json",
|
|
9
10
|
remoteToolPolicy: "read-only",
|
|
10
11
|
remoteToolApproval: "web",
|
|
11
12
|
});
|
|
12
13
|
expect(parsed.warnings).toHaveLength(1);
|
|
13
|
-
|
|
14
|
+
expect(parsed.config.tokenFile).toBe("~/.pi/agent/remote-control.token.json");
|
|
15
|
+
const resolved = resolveRemoteConfig(parsed.config, {
|
|
16
|
+
PI_REMOTE_SERVER_URL: "ws://override:8787/ws/agent",
|
|
17
|
+
PI_REMOTE_SERVER_TOKEN_FILE: "/tmp/custom.token",
|
|
18
|
+
});
|
|
14
19
|
expect(resolved.serverUrl).toBe("ws://override:8787/ws/agent");
|
|
20
|
+
expect(resolved.tokenFile).toBe("/tmp/custom.token");
|
|
15
21
|
expect(resolved.remoteToolPolicy).toBe("read-only");
|
|
16
22
|
expect(resolved.remoteToolApproval).toBe("web");
|
|
17
23
|
});
|
|
24
|
+
|
|
25
|
+
it("expands home directory paths", () => {
|
|
26
|
+
expect(expandHomePath("~/.pi/agent/remote-control.token.json", "/home/alice")).toBe(
|
|
27
|
+
"/home/alice/.pi/agent/remote-control.token.json",
|
|
28
|
+
);
|
|
29
|
+
expect(expandHomePath("/etc/pi/token", "/home/alice")).toBe("/etc/pi/token");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("parses plain and JSON token files", () => {
|
|
33
|
+
expect(parseTokenFileContent("plain-secret\n")).toBe("plain-secret");
|
|
34
|
+
expect(parseTokenFileContent('{"token":"json-secret"}\n')).toBe("json-secret");
|
|
35
|
+
expect(parseTokenFileContent('{"serverToken":"alt-secret"}\n')).toBe("alt-secret");
|
|
36
|
+
expect(parseTokenFileContent(" \n")).toBe("");
|
|
37
|
+
});
|
|
18
38
|
});
|
package/src/config.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type { RemoteToolApproval, RemoteToolPolicy } from "./core/tool-policy.js
|
|
|
8
8
|
export interface RemoteControlConfig {
|
|
9
9
|
readonly serverUrl: string;
|
|
10
10
|
readonly tokenEnv: string;
|
|
11
|
+
readonly tokenFile?: string;
|
|
11
12
|
readonly requirePairing: boolean;
|
|
12
13
|
readonly pairingTtlMs: number;
|
|
13
14
|
readonly pairingDigits: number;
|
|
@@ -59,6 +60,9 @@ export function parseRemoteConfig(raw: unknown): ParsedConfigResult {
|
|
|
59
60
|
if (typeof obj.tokenEnv === "string" && /^[A-Z0-9_]+$/i.test(obj.tokenEnv.trim())) {
|
|
60
61
|
out.tokenEnv = obj.tokenEnv.trim();
|
|
61
62
|
}
|
|
63
|
+
if (typeof obj.tokenFile === "string" && obj.tokenFile.trim()) {
|
|
64
|
+
out.tokenFile = obj.tokenFile.trim();
|
|
65
|
+
}
|
|
62
66
|
if (typeof obj.requirePairing === "boolean") {
|
|
63
67
|
out.requirePairing = obj.requirePairing;
|
|
64
68
|
}
|
|
@@ -83,9 +87,43 @@ export function resolveRemoteConfig(
|
|
|
83
87
|
env: Record<string, string | undefined> = process.env,
|
|
84
88
|
): RemoteControlConfig {
|
|
85
89
|
const envUrl = env.PI_REMOTE_SERVER_URL?.trim();
|
|
90
|
+
const envTokenFile = env.PI_REMOTE_SERVER_TOKEN_FILE?.trim();
|
|
86
91
|
return {
|
|
87
92
|
...DEFAULT_REMOTE_CONFIG,
|
|
88
93
|
...overrides,
|
|
89
94
|
...(envUrl ? { serverUrl: envUrl } : {}),
|
|
95
|
+
...(envTokenFile ? { tokenFile: envTokenFile } : {}),
|
|
90
96
|
};
|
|
91
97
|
}
|
|
98
|
+
|
|
99
|
+
export function expandHomePath(filePath: string, homeDir: string): string {
|
|
100
|
+
const trimmed = filePath.trim();
|
|
101
|
+
if (trimmed === "~") return homeDir;
|
|
102
|
+
if (trimmed.startsWith("~/")) {
|
|
103
|
+
return `${homeDir.replace(/\/+$/, "")}/${trimmed.slice(2)}`;
|
|
104
|
+
}
|
|
105
|
+
return trimmed;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function parseTokenFileContent(raw: string): string {
|
|
109
|
+
const trimmed = raw.trim();
|
|
110
|
+
if (!trimmed) return "";
|
|
111
|
+
if (trimmed.startsWith("{")) {
|
|
112
|
+
try {
|
|
113
|
+
const parsed = JSON.parse(trimmed) as unknown;
|
|
114
|
+
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
|
|
115
|
+
const record = parsed as Record<string, unknown>;
|
|
116
|
+
if (typeof record.token === "string" && record.token.trim()) {
|
|
117
|
+
return record.token.trim();
|
|
118
|
+
}
|
|
119
|
+
if (typeof record.serverToken === "string" && record.serverToken.trim()) {
|
|
120
|
+
return record.serverToken.trim();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return "";
|
|
124
|
+
} catch {
|
|
125
|
+
return trimmed;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return trimmed;
|
|
129
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -3,13 +3,19 @@
|
|
|
3
3
|
* Thin adapter binding pi ExtensionAPI events to WsClient + RemoteBridge.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { readFile } from "node:fs/promises";
|
|
6
|
+
import { readFile, stat } from "node:fs/promises";
|
|
7
7
|
import { homedir, hostname } from "node:os";
|
|
8
8
|
import { join } from "node:path";
|
|
9
9
|
import { CONFIG_DIR_NAME, type ExtensionAPI, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
10
10
|
import { RemoteBridge, type RemoteHost } from "./client/remote-bridge.js";
|
|
11
11
|
import { WsClient } from "./client/ws-client.js";
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
expandHomePath,
|
|
14
|
+
parseRemoteConfig,
|
|
15
|
+
parseTokenFileContent,
|
|
16
|
+
resolveRemoteConfig,
|
|
17
|
+
type RemoteControlConfig,
|
|
18
|
+
} from "./config.js";
|
|
13
19
|
import { buildWorkDigest, parseGitBranch, parseGitNumstat } from "./core/digest.js";
|
|
14
20
|
import { buildHistorySnapshot, type RawSessionEntry } from "./core/history.js";
|
|
15
21
|
import { formatFooterLabel, formatStatusReport } from "./core/status.js";
|
|
@@ -51,6 +57,33 @@ async function loadExtensionConfig(cwd: string): Promise<{ config: RemoteControl
|
|
|
51
57
|
return { config: resolveRemoteConfig(merged), warnings };
|
|
52
58
|
}
|
|
53
59
|
|
|
60
|
+
async function loadServerToken(config: RemoteControlConfig, warnings: string[]): Promise<string> {
|
|
61
|
+
const envToken = process.env[config.tokenEnv]?.trim();
|
|
62
|
+
if (envToken) {
|
|
63
|
+
return envToken;
|
|
64
|
+
}
|
|
65
|
+
if (!config.tokenFile) {
|
|
66
|
+
return "";
|
|
67
|
+
}
|
|
68
|
+
const resolvedPath = expandHomePath(config.tokenFile, homedir());
|
|
69
|
+
try {
|
|
70
|
+
if (process.platform !== "win32") {
|
|
71
|
+
const info = await stat(resolvedPath);
|
|
72
|
+
if ((info.mode & 0o077) !== 0) {
|
|
73
|
+
warnings.push(`tokenFile (${resolvedPath}) should have 0600 permissions`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const raw = await readFile(resolvedPath, "utf8");
|
|
77
|
+
return parseTokenFileContent(raw);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
80
|
+
if (code !== "ENOENT") {
|
|
81
|
+
warnings.push(`tokenFile error (${resolvedPath}): ${error instanceof Error ? error.message : String(error)}`);
|
|
82
|
+
}
|
|
83
|
+
return "";
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
54
87
|
export default async function remoteControlExtension(pi: ExtensionAPI): Promise<void> {
|
|
55
88
|
let sessionCtx: ExtensionContext | null = null;
|
|
56
89
|
let wsClient: WsClient | null = null;
|
|
@@ -117,7 +150,7 @@ export default async function remoteControlExtension(pi: ExtensionAPI): Promise<
|
|
|
117
150
|
sessionCtx = ctx;
|
|
118
151
|
const { config, warnings } = await loadExtensionConfig(ctx.cwd);
|
|
119
152
|
await refreshBranch(ctx.cwd);
|
|
120
|
-
const serverToken =
|
|
153
|
+
const serverToken = await loadServerToken(config, warnings);
|
|
121
154
|
|
|
122
155
|
const host: RemoteHost = {
|
|
123
156
|
sessionId: getSessionId(ctx),
|