@firstpick/pi-package-webui 0.7.7 → 0.7.9
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/README.md +23 -0
- package/bin/pi-webui-rpc-supervisor.mjs +115 -21
- package/bin/pi-webui.mjs +447 -88
- package/lib/queue-mutation.mjs +109 -0
- package/lib/rpc-supervisor-client.mjs +18 -2
- package/lib/rpc-supervisor-protocol.mjs +11 -2
- package/lib/scoped-models.mjs +19 -0
- package/lib/webui-workspaces.mjs +265 -0
- package/package.json +4 -4
- package/public/app.js +2227 -195
- package/public/index.html +143 -2
- package/public/issue-bot-client.mjs +344 -0
- package/public/issue-wizard-state.mjs +443 -0
- package/public/service-worker.js +3 -1
- package/public/styles.css +787 -56
- package/public/voice-conversation.mjs +1 -1
- package/tests/boot-failure-diagnostics.test.mjs +3 -3
- package/tests/compaction-mutation-contract.test.mjs +166 -0
- package/tests/compaction-resume-harness.test.mjs +113 -2
- package/tests/compaction-static.test.mjs +5 -0
- package/tests/completion-signal-contract.test.mjs +156 -0
- package/tests/cwd-footer-reliability-static.test.mjs +112 -0
- package/tests/durable-rpc-supervisor-harness.test.mjs +25 -2
- package/tests/fast-mode-client-static.test.mjs +1 -1
- package/tests/file-selection-overlay-static.test.mjs +21 -0
- package/tests/file-viewer-search-static.test.mjs +65 -0
- package/tests/fixtures/compacting-pi.mjs +0 -0
- package/tests/fixtures/fake-pi.mjs +186 -49
- package/tests/footer-context-contrast-static.test.mjs +10 -0
- package/tests/git-panel-file-preview-static.test.mjs +20 -5
- package/tests/git-panel-render-stability-static.test.mjs +11 -1
- package/tests/http-endpoints-harness.test.mjs +160 -1
- package/tests/issue-bot-client.test.mjs +135 -0
- package/tests/issue-wizard-state.test.mjs +126 -0
- package/tests/mobile-static.test.mjs +61 -42
- package/tests/native-parity.test.mjs +3 -3
- package/tests/open-issue-wizard-static.test.mjs +75 -0
- package/tests/prompt-routing-tab-status.test.mjs +5 -0
- package/tests/queue-edit-reorder-static.test.mjs +101 -0
- package/tests/queue-mutation-contract.test.mjs +143 -0
- package/tests/rpc-supervisor-host.test.mjs +135 -2
- package/tests/rpc-supervisor-protocol.test.mjs +26 -0
- package/tests/scoped-models.test.mjs +42 -0
- package/tests/streaming-ui-coupling.test.mjs +3 -2
- package/tests/subagents-helper.test.mjs +216 -3
- package/tests/terminal-tab-workspace-static.test.mjs +101 -0
- package/tests/webui-workspaces-harness.test.mjs +223 -0
- package/tests/webui-workspaces.test.mjs +106 -0
- package/tests/workspace-save-load-static.test.mjs +69 -0
- package/webui-rpc-helper.mjs +300 -41
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
export const PI_RUNTIME_QUEUE_SOURCE = "pi-runtime";
|
|
2
|
+
|
|
3
|
+
function queueSnapshot(steering, followUp) {
|
|
4
|
+
return {
|
|
5
|
+
source: PI_RUNTIME_QUEUE_SOURCE,
|
|
6
|
+
steering: [...steering],
|
|
7
|
+
followUp: [...followUp],
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function arraysEqual(left, right) {
|
|
12
|
+
return left.length === right.length && left.every((value, index) => value === right[index]);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function exactMessageText(message) {
|
|
16
|
+
if (!message || typeof message !== "object" || !Array.isArray(message.content)) return undefined;
|
|
17
|
+
const textParts = message.content.filter((part) => part?.type === "text");
|
|
18
|
+
if (textParts.length !== 1 || typeof textParts[0].text !== "string") return undefined;
|
|
19
|
+
return textParts[0].text;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function matchingQueueMessages(tracked, messages) {
|
|
23
|
+
return Array.isArray(messages)
|
|
24
|
+
&& tracked.length === messages.length
|
|
25
|
+
&& messages.every((message, index) => exactMessageText(message) === tracked[index]);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function mutationFailure(reason, steering, followUp) {
|
|
29
|
+
return { mutated: false, reason, queue: queueSnapshot(steering, followUp) };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function isStringArray(value) {
|
|
33
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function validExpectedSnapshot(expected) {
|
|
37
|
+
return expected
|
|
38
|
+
&& typeof expected === "object"
|
|
39
|
+
&& isStringArray(expected.steering)
|
|
40
|
+
&& isStringArray(expected.followUp);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function validIndex(value, length) {
|
|
44
|
+
return Number.isInteger(value) && value >= 0 && value < length;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Mutate the paired private AgentSession follow-up structures synchronously.
|
|
49
|
+
* The installed Pi runtime exposes no public item mutation API, so unknown
|
|
50
|
+
* queue/message layouts deliberately fail closed before either array changes.
|
|
51
|
+
*/
|
|
52
|
+
export function mutatePiRuntimeFollowUpQueue(session, payload = {}) {
|
|
53
|
+
const steering = session?._steeringMessages;
|
|
54
|
+
const followUp = session?._followUpMessages;
|
|
55
|
+
const steeringMessages = session?.agent?.steeringQueue?.messages;
|
|
56
|
+
const followUpMessages = session?.agent?.followUpQueue?.messages;
|
|
57
|
+
if (!Array.isArray(steering) || !Array.isArray(followUp)
|
|
58
|
+
|| !Array.isArray(steeringMessages) || !Array.isArray(followUpMessages)
|
|
59
|
+
|| typeof session?._emitQueueUpdate !== "function") {
|
|
60
|
+
return { mutated: false, reason: "queue-unsupported", queue: { source: PI_RUNTIME_QUEUE_SOURCE, steering: [], followUp: [] } };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const current = queueSnapshot(steering, followUp);
|
|
64
|
+
if (!matchingQueueMessages(steering, steeringMessages) || !matchingQueueMessages(followUp, followUpMessages)) {
|
|
65
|
+
return mutationFailure("queue-desynchronized", steering, followUp);
|
|
66
|
+
}
|
|
67
|
+
if (payload?.source !== PI_RUNTIME_QUEUE_SOURCE || payload?.kind !== "followUp" || !validExpectedSnapshot(payload.expected)) {
|
|
68
|
+
return mutationFailure("invalid-request", steering, followUp);
|
|
69
|
+
}
|
|
70
|
+
if (!arraysEqual(payload.expected.steering, steering) || !arraysEqual(payload.expected.followUp, followUp)) {
|
|
71
|
+
return mutationFailure("queue-changed", steering, followUp);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const operation = payload.operation;
|
|
75
|
+
if (!operation || typeof operation !== "object" || typeof operation.expectedText !== "string") {
|
|
76
|
+
return mutationFailure("invalid-request", steering, followUp);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const nextTracked = [...followUp];
|
|
80
|
+
const nextMessages = [...followUpMessages];
|
|
81
|
+
if (operation.type === "edit") {
|
|
82
|
+
if (!validIndex(operation.index, followUp.length) || followUp[operation.index] !== operation.expectedText
|
|
83
|
+
|| typeof operation.text !== "string" || !operation.text.trim()) {
|
|
84
|
+
return mutationFailure("invalid-request", steering, followUp);
|
|
85
|
+
}
|
|
86
|
+
const original = followUpMessages[operation.index];
|
|
87
|
+
const textPartIndex = original.content.findIndex((part) => part?.type === "text");
|
|
88
|
+
const nextContent = original.content.map((part, index) => index === textPartIndex ? { ...part, text: operation.text } : part);
|
|
89
|
+
nextTracked[operation.index] = operation.text;
|
|
90
|
+
nextMessages[operation.index] = { ...original, content: nextContent };
|
|
91
|
+
} else if (operation.type === "move") {
|
|
92
|
+
if (!validIndex(operation.from, followUp.length) || !validIndex(operation.to, followUp.length)
|
|
93
|
+
|| operation.from === operation.to || followUp[operation.from] !== operation.expectedText) {
|
|
94
|
+
return mutationFailure("invalid-request", steering, followUp);
|
|
95
|
+
}
|
|
96
|
+
const [tracked] = nextTracked.splice(operation.from, 1);
|
|
97
|
+
const [message] = nextMessages.splice(operation.from, 1);
|
|
98
|
+
// `to` is the final zero-based index after the item has been removed.
|
|
99
|
+
nextTracked.splice(operation.to, 0, tracked);
|
|
100
|
+
nextMessages.splice(operation.to, 0, message);
|
|
101
|
+
} else {
|
|
102
|
+
return mutationFailure("invalid-request", steering, followUp);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
session._followUpMessages = nextTracked;
|
|
106
|
+
session.agent.followUpQueue.messages = nextMessages;
|
|
107
|
+
session._emitQueueUpdate();
|
|
108
|
+
return { mutated: true, source: PI_RUNTIME_QUEUE_SOURCE, queue: queueSnapshot(steering, nextTracked) };
|
|
109
|
+
}
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
encodeFrame,
|
|
10
10
|
frameReader,
|
|
11
11
|
newRequestId,
|
|
12
|
+
protocolCurrent,
|
|
12
13
|
} from "./rpc-supervisor-protocol.mjs";
|
|
13
14
|
import {
|
|
14
15
|
acquireStartupLock,
|
|
@@ -47,6 +48,14 @@ function liveStateError(state) {
|
|
|
47
48
|
});
|
|
48
49
|
}
|
|
49
50
|
|
|
51
|
+
function supervisorRestartRequiredError(state, operation) {
|
|
52
|
+
const version = state?.version;
|
|
53
|
+
const remote = Number.isInteger(version?.major) && Number.isInteger(version?.minor) ? `${version.major}.${version.minor}` : "unknown";
|
|
54
|
+
return Object.assign(new Error(`The detached RPC supervisor protocol ${remote} is older than this WebUI build and cannot ${operation}. Fully shut down Pi WebUI (not Restart), then start it again.`), {
|
|
55
|
+
code: "RPC_SUPERVISOR_RESTART_REQUIRED",
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
50
59
|
function openSocket(socketPath, timeoutMs = DEFAULT_CONNECT_TIMEOUT_MS) {
|
|
51
60
|
return new Promise((resolve, reject) => {
|
|
52
61
|
const socket = createConnection(socketPath);
|
|
@@ -184,6 +193,7 @@ export class RpcSupervisorClient {
|
|
|
184
193
|
}
|
|
185
194
|
|
|
186
195
|
isConnected() { return this.connected && this.attached; }
|
|
196
|
+
isCurrentVersion() { return protocolCurrent(this.state.version); }
|
|
187
197
|
|
|
188
198
|
request(type, payload = {}, { requestId = newRequestId() } = {}) {
|
|
189
199
|
if (!this.attached) return Promise.reject(new Error("RPC supervisor client is not attached"));
|
|
@@ -199,9 +209,15 @@ export class RpcSupervisorClient {
|
|
|
199
209
|
return result.promise;
|
|
200
210
|
}
|
|
201
211
|
|
|
202
|
-
createTab({ tabId, metadata, child }, options) {
|
|
212
|
+
createTab({ tabId, metadata, child }, options) {
|
|
213
|
+
if (!this.isCurrentVersion()) return Promise.reject(supervisorRestartRequiredError(this.state, "create a Pi tab"));
|
|
214
|
+
return this.request("create", { tabId, metadata, child }, options);
|
|
215
|
+
}
|
|
203
216
|
updateTab(tabId, metadata, options) { return this.request("update", { tabId, metadata }, options); }
|
|
204
|
-
replaceTab({ tabId, metadata, child }, options) {
|
|
217
|
+
replaceTab({ tabId, metadata, child }, options) {
|
|
218
|
+
if (!this.isCurrentVersion()) return Promise.reject(supervisorRestartRequiredError(this.state, "replace a Pi tab"));
|
|
219
|
+
return this.request("replace", { tabId, metadata, child }, options);
|
|
220
|
+
}
|
|
205
221
|
closeTab(tabId, options) { return this.request("close", { tabId }, options); }
|
|
206
222
|
command(tabId, command, { requestId = newRequestId(), timeoutMs } = {}) {
|
|
207
223
|
return this.request("command", { tabId, command, ...(timeoutMs ? { timeoutMs } : {}) }, { requestId });
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { randomUUID, timingSafeEqual } from "node:crypto";
|
|
2
2
|
import { StringDecoder } from "node:string_decoder";
|
|
3
3
|
|
|
4
|
-
export const RPC_SUPERVISOR_PROTOCOL = Object.freeze({ major: 1, minor:
|
|
4
|
+
export const RPC_SUPERVISOR_PROTOCOL = Object.freeze({ major: 1, minor: 2 });
|
|
5
5
|
// Pi permits one bounded JSONL record this large. IPC envelopes need a small
|
|
6
6
|
// additional allowance so a single valid Pi record can always be forwarded.
|
|
7
7
|
export const PI_RPC_JSONL_LINE_MAX_BYTES = 32 * 1024 * 1024;
|
|
@@ -13,6 +13,10 @@ export const RPC_SUPERVISOR_EVENT_RING_LIMIT = 512;
|
|
|
13
13
|
export const RPC_SUPERVISOR_EVENT_RING_MAX_BYTES = 4 * 1024 * 1024;
|
|
14
14
|
export const RPC_SUPERVISOR_REQUEST_DEDUPE_LIMIT = 2048;
|
|
15
15
|
export const RPC_SUPERVISOR_TAB_LIMIT = 64;
|
|
16
|
+
// Each configured extension, skill, prompt, and theme contributes a flag/value
|
|
17
|
+
// pair. Real WebUI profiles can exceed 256 arguments before a --session pair
|
|
18
|
+
// is appended during reload, CWD changes, or forks.
|
|
19
|
+
export const RPC_SUPERVISOR_CHILD_ARG_LIMIT = 1024;
|
|
16
20
|
export const RPC_SUPERVISOR_METADATA_MAX_BYTES = 64 * 1024;
|
|
17
21
|
export const RPC_SUPERVISOR_ID_MAX_LENGTH = 128;
|
|
18
22
|
// This applies only to persisted metadata, state, and journals. Live Pi RPC
|
|
@@ -81,6 +85,11 @@ export function protocolCompatible(remote) {
|
|
|
81
85
|
return parsed.major === RPC_SUPERVISOR_PROTOCOL.major;
|
|
82
86
|
}
|
|
83
87
|
|
|
88
|
+
export function protocolCurrent(remote) {
|
|
89
|
+
const parsed = version(remote);
|
|
90
|
+
return parsed.major === RPC_SUPERVISOR_PROTOCOL.major && parsed.minor === RPC_SUPERVISOR_PROTOCOL.minor;
|
|
91
|
+
}
|
|
92
|
+
|
|
84
93
|
export function assertProtocolCompatible(remote) {
|
|
85
94
|
if (!protocolCompatible(remote)) {
|
|
86
95
|
const parsed = version(remote);
|
|
@@ -156,7 +165,7 @@ function validateChild(value, label = "child") {
|
|
|
156
165
|
object(value, label);
|
|
157
166
|
onlyKeys(value, new Set(["command", "args", "cwd"]), label);
|
|
158
167
|
const command = string(value.command, `${label}.command`, { max: 4096 });
|
|
159
|
-
if (!Array.isArray(value.args) || value.args.length >
|
|
168
|
+
if (!Array.isArray(value.args) || value.args.length > RPC_SUPERVISOR_CHILD_ARG_LIMIT) fail(`${label}.args must be an array with at most ${RPC_SUPERVISOR_CHILD_ARG_LIMIT} entries`);
|
|
160
169
|
const args = value.args.map((arg, index) => string(arg, `${label}.args[${index}]`, { min: 0, max: 64 * 1024 }));
|
|
161
170
|
const cwd = string(value.cwd, `${label}.cwd`, { max: 16 * 1024 });
|
|
162
171
|
return { command, args, cwd };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { resolveModelScopeWithDiagnostics } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Resolve WebUI scoped-model patterns with Pi's canonical matching semantics.
|
|
5
|
+
* The RPC model list is already Pi's authenticated/available model catalog.
|
|
6
|
+
*/
|
|
7
|
+
export async function resolveScopedModelsFromPatterns(patterns, models) {
|
|
8
|
+
const safePatterns = Array.isArray(patterns)
|
|
9
|
+
? patterns.filter((pattern) => typeof pattern === "string" && pattern.trim())
|
|
10
|
+
: [];
|
|
11
|
+
const availableModels = Array.isArray(models)
|
|
12
|
+
? models.filter((model) => model?.provider && model?.id)
|
|
13
|
+
: [];
|
|
14
|
+
if (!safePatterns.length || !availableModels.length) return [];
|
|
15
|
+
|
|
16
|
+
const modelRuntime = { getAvailable: async () => availableModels };
|
|
17
|
+
const { scopedModels } = await resolveModelScopeWithDiagnostics(safePatterns, modelRuntime);
|
|
18
|
+
return scopedModels.map(({ model }) => model);
|
|
19
|
+
}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
export const WEBUI_WORKSPACES_VERSION = 1;
|
|
7
|
+
export const WEBUI_WORKSPACE_LIMIT = 20;
|
|
8
|
+
export const WEBUI_WORKSPACE_TAB_LIMIT = 30;
|
|
9
|
+
|
|
10
|
+
const WEBUI_WORKSPACES_FILE_ENV = "PI_WEBUI_WORKSPACES_FILE";
|
|
11
|
+
const workspaceUpdateQueues = new Map();
|
|
12
|
+
|
|
13
|
+
function boundedString(value, maxLength) {
|
|
14
|
+
const text = typeof value === "string" ? value.trim() : "";
|
|
15
|
+
return text ? text.slice(0, maxLength) : undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function normalizedId(value, seenIds = new Set()) {
|
|
19
|
+
const id = boundedString(value, 128);
|
|
20
|
+
if (!id || !/^[A-Za-z0-9._:-]+$/.test(id) || seenIds.has(id)) return undefined;
|
|
21
|
+
seenIds.add(id);
|
|
22
|
+
return id;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function normalizedSavedAt(value) {
|
|
26
|
+
const savedAt = boundedString(value, 128);
|
|
27
|
+
return savedAt && Number.isFinite(Date.parse(savedAt)) ? savedAt : undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function workspaceDocument(value) {
|
|
31
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function webuiWorkspacesFile(env = process.env) {
|
|
35
|
+
if (env[WEBUI_WORKSPACES_FILE_ENV]) return path.resolve(String(env[WEBUI_WORKSPACES_FILE_ENV]).replace(/^~(?=$|[\\/])/, homedir()));
|
|
36
|
+
const configRoot = env.XDG_CONFIG_HOME || path.join(homedir(), ".config");
|
|
37
|
+
return path.join(configRoot, "pi-webui", "workspaces.json");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function normalizeWorkspaceTabDescriptor(value, seenIds = new Set()) {
|
|
41
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
42
|
+
const id = normalizedId(value.id, seenIds);
|
|
43
|
+
const cwd = boundedString(value.cwd || value.workspace?.cwd, 4096);
|
|
44
|
+
if (!id || !cwd) return null;
|
|
45
|
+
const descriptor = {
|
|
46
|
+
id,
|
|
47
|
+
title: boundedString(value.title, 160),
|
|
48
|
+
titleSource: ["explicit", "auto", "default"].includes(value.titleSource) ? value.titleSource : undefined,
|
|
49
|
+
cwd,
|
|
50
|
+
conversationStarted: value.conversationStarted === true,
|
|
51
|
+
sessionFile: boundedString(value.sessionFile || value.state?.sessionFile, 4096),
|
|
52
|
+
};
|
|
53
|
+
if (Number.isInteger(value.index) && value.index > 0) descriptor.index = value.index;
|
|
54
|
+
return descriptor;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function normalizeWorkspaceGroups(value, tabIds = new Set()) {
|
|
58
|
+
if (!Array.isArray(value)) return [];
|
|
59
|
+
const groups = [];
|
|
60
|
+
const seen = new Set();
|
|
61
|
+
for (const item of value.slice(0, WEBUI_WORKSPACE_TAB_LIMIT)) {
|
|
62
|
+
const title = boundedString(item?.title, 160);
|
|
63
|
+
if (!title) continue;
|
|
64
|
+
const tabIdsForGroup = [];
|
|
65
|
+
for (const tabId of Array.isArray(item?.tabIds) ? item.tabIds.slice(0, WEBUI_WORKSPACE_TAB_LIMIT) : []) {
|
|
66
|
+
const normalized = boundedString(tabId, 128);
|
|
67
|
+
if (!normalized || !tabIds.has(normalized) || tabIdsForGroup.includes(normalized)) continue;
|
|
68
|
+
tabIdsForGroup.push(normalized);
|
|
69
|
+
}
|
|
70
|
+
if (!tabIdsForGroup.length) continue;
|
|
71
|
+
const key = `${title}\u0000${tabIdsForGroup.join("\u0000")}`;
|
|
72
|
+
if (seen.has(key)) continue;
|
|
73
|
+
seen.add(key);
|
|
74
|
+
groups.push({ title, tabIds: tabIdsForGroup });
|
|
75
|
+
}
|
|
76
|
+
return groups;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function normalizeWebuiWorkspace(value, seenWorkspaceIds = new Set()) {
|
|
80
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
81
|
+
const id = normalizedId(value.id, seenWorkspaceIds);
|
|
82
|
+
const name = boundedString(value.name, 160);
|
|
83
|
+
const savedAt = normalizedSavedAt(value.savedAt);
|
|
84
|
+
const seenTabIds = new Set();
|
|
85
|
+
const tabs = (Array.isArray(value.tabs) ? value.tabs : [])
|
|
86
|
+
.slice(0, WEBUI_WORKSPACE_TAB_LIMIT)
|
|
87
|
+
.map((item) => normalizeWorkspaceTabDescriptor(item, seenTabIds))
|
|
88
|
+
.filter(Boolean);
|
|
89
|
+
if (!id || !name || !savedAt || !tabs.length) return null;
|
|
90
|
+
const tabIds = new Set(tabs.map((tab) => tab.id));
|
|
91
|
+
const activeTabId = boundedString(value.activeTabId, 128);
|
|
92
|
+
return {
|
|
93
|
+
id,
|
|
94
|
+
name,
|
|
95
|
+
savedAt,
|
|
96
|
+
activeTabId: activeTabId && tabIds.has(activeTabId) ? activeTabId : null,
|
|
97
|
+
tabs,
|
|
98
|
+
groups: normalizeWorkspaceGroups(value.groups, tabIds),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function normalizeWebuiWorkspaces(value) {
|
|
103
|
+
const source = workspaceDocument(value);
|
|
104
|
+
const seenWorkspaceIds = new Set();
|
|
105
|
+
const workspaces = (Array.isArray(source.workspaces) ? source.workspaces : [])
|
|
106
|
+
.map((item) => normalizeWebuiWorkspace(item, seenWorkspaceIds))
|
|
107
|
+
.filter(Boolean)
|
|
108
|
+
.sort((left, right) => Date.parse(left.savedAt) - Date.parse(right.savedAt))
|
|
109
|
+
.slice(-WEBUI_WORKSPACE_LIMIT);
|
|
110
|
+
return { version: WEBUI_WORKSPACES_VERSION, workspaces };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function workspaceMetadata(workspace) {
|
|
114
|
+
const normalized = normalizeWebuiWorkspace(workspace);
|
|
115
|
+
if (!normalized) return null;
|
|
116
|
+
return {
|
|
117
|
+
id: normalized.id,
|
|
118
|
+
name: normalized.name,
|
|
119
|
+
savedAt: normalized.savedAt,
|
|
120
|
+
tabCount: normalized.tabs.length,
|
|
121
|
+
groupCount: normalized.groups.length,
|
|
122
|
+
cwds: [...new Set(normalized.tabs.map((tab) => tab.cwd))],
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function warningMessage(storageFile, error) {
|
|
127
|
+
return `failed to read Pi Web UI workspaces at ${storageFile}; treating it as empty: ${error instanceof Error ? error.message : String(error)}`;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function readWorkspaceDocument(storageFile, { onWarning = console.warn } = {}) {
|
|
131
|
+
try {
|
|
132
|
+
const parsed = JSON.parse(await readFile(storageFile, "utf8"));
|
|
133
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error("workspace document must be an object");
|
|
134
|
+
if (parsed.version !== undefined && parsed.version !== WEBUI_WORKSPACES_VERSION) {
|
|
135
|
+
throw new Error(`unsupported workspace document version ${JSON.stringify(parsed.version)}`);
|
|
136
|
+
}
|
|
137
|
+
if (parsed.workspaces !== undefined && !Array.isArray(parsed.workspaces)) throw new Error("workspaces must be an array");
|
|
138
|
+
return normalizeWebuiWorkspaces(parsed);
|
|
139
|
+
} catch (error) {
|
|
140
|
+
if (error?.code === "ENOENT") return normalizeWebuiWorkspaces({});
|
|
141
|
+
onWarning?.(warningMessage(storageFile, error));
|
|
142
|
+
return normalizeWebuiWorkspaces({});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export async function readWebuiWorkspaces(storageFile = webuiWorkspacesFile(), options = {}) {
|
|
147
|
+
return readWorkspaceDocument(storageFile, options);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export async function listWebuiWorkspaces(storageFile = webuiWorkspacesFile(), options = {}) {
|
|
151
|
+
const document = await readWorkspaceDocument(storageFile, options);
|
|
152
|
+
return document.workspaces.slice().reverse().map(workspaceMetadata).filter(Boolean);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async function writeWorkspaceDocument(document, storageFile) {
|
|
156
|
+
const normalized = normalizeWebuiWorkspaces(document);
|
|
157
|
+
await mkdir(path.dirname(storageFile), { recursive: true });
|
|
158
|
+
const temporaryFile = `${storageFile}.${process.pid}.${Date.now()}.${randomUUID()}.tmp`;
|
|
159
|
+
try {
|
|
160
|
+
await writeFile(temporaryFile, `${JSON.stringify(normalized, null, 2)}\n`, { mode: 0o600 });
|
|
161
|
+
await rename(temporaryFile, storageFile);
|
|
162
|
+
} catch (error) {
|
|
163
|
+
await rm(temporaryFile, { force: true }).catch(() => {});
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
return normalized;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function enqueueWorkspaceUpdate(storageFile, update) {
|
|
170
|
+
const queueKey = path.resolve(storageFile);
|
|
171
|
+
const previous = workspaceUpdateQueues.get(queueKey) || Promise.resolve();
|
|
172
|
+
const current = previous.catch(() => {}).then(update);
|
|
173
|
+
workspaceUpdateQueues.set(queueKey, current);
|
|
174
|
+
return current.finally(() => {
|
|
175
|
+
if (workspaceUpdateQueues.get(queueKey) === current) workspaceUpdateQueues.delete(queueKey);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function mutateWorkspaces(updater, storageFile) {
|
|
180
|
+
return enqueueWorkspaceUpdate(storageFile, async () => {
|
|
181
|
+
const document = await readWorkspaceDocument(storageFile);
|
|
182
|
+
const result = await updater(document);
|
|
183
|
+
if (result?.write !== false) await writeWorkspaceDocument(document, storageFile);
|
|
184
|
+
return result?.data;
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function automaticWorkspaceName(tabs, savedAt) {
|
|
189
|
+
const cwd = tabs[0]?.cwd || "";
|
|
190
|
+
const base = path.basename(cwd) || "workspace";
|
|
191
|
+
return `${base} ${savedAt.slice(0, 10)}`.slice(0, 160);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function normalizedNewWorkspace({ name, tabs, groups, activeTabId, savedAt = new Date().toISOString(), id = `ws-${randomUUID()}` }) {
|
|
195
|
+
const seenTabIds = new Set();
|
|
196
|
+
const normalizedTabs = (Array.isArray(tabs) ? tabs : [])
|
|
197
|
+
.slice(0, WEBUI_WORKSPACE_TAB_LIMIT)
|
|
198
|
+
.map((item) => normalizeWorkspaceTabDescriptor(item, seenTabIds))
|
|
199
|
+
.filter(Boolean);
|
|
200
|
+
if (!normalizedTabs.length) throw new Error("A workspace requires at least one tab");
|
|
201
|
+
const tabIds = new Set(normalizedTabs.map((tab) => tab.id));
|
|
202
|
+
const normalizedName = boundedString(name, 160) || automaticWorkspaceName(normalizedTabs, savedAt);
|
|
203
|
+
return {
|
|
204
|
+
id,
|
|
205
|
+
name: normalizedName,
|
|
206
|
+
savedAt,
|
|
207
|
+
activeTabId: tabIds.has(boundedString(activeTabId, 128)) ? boundedString(activeTabId, 128) : null,
|
|
208
|
+
tabs: normalizedTabs,
|
|
209
|
+
groups: normalizeWorkspaceGroups(groups, tabIds),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export async function saveWebuiWorkspace({ name, tabs, groups, activeTabId, overwrite = false } = {}, storageFile = webuiWorkspacesFile()) {
|
|
214
|
+
return mutateWorkspaces((document) => {
|
|
215
|
+
const savedAt = new Date().toISOString();
|
|
216
|
+
const candidate = normalizedNewWorkspace({ name, tabs, groups, activeTabId, savedAt });
|
|
217
|
+
const existingIndex = document.workspaces.findIndex((workspace) => workspace.name === candidate.name);
|
|
218
|
+
if (existingIndex !== -1 && overwrite !== true) {
|
|
219
|
+
const error = new Error("A workspace with that name already exists");
|
|
220
|
+
error.code = "WORKSPACE_NAME_CONFLICT";
|
|
221
|
+
throw error;
|
|
222
|
+
}
|
|
223
|
+
const existing = existingIndex === -1 ? null : document.workspaces[existingIndex];
|
|
224
|
+
const workspace = existing ? { ...candidate, id: existing.id } : candidate;
|
|
225
|
+
const evicted = [];
|
|
226
|
+
if (existingIndex !== -1) document.workspaces.splice(existingIndex, 1);
|
|
227
|
+
document.workspaces.push(workspace);
|
|
228
|
+
document.workspaces.sort((left, right) => Date.parse(left.savedAt) - Date.parse(right.savedAt));
|
|
229
|
+
while (document.workspaces.length > WEBUI_WORKSPACE_LIMIT) {
|
|
230
|
+
const removed = document.workspaces.shift();
|
|
231
|
+
const metadata = workspaceMetadata(removed);
|
|
232
|
+
if (metadata) evicted.push(metadata);
|
|
233
|
+
}
|
|
234
|
+
return {
|
|
235
|
+
data: {
|
|
236
|
+
workspace: workspaceMetadata(workspace),
|
|
237
|
+
workspaces: document.workspaces.slice().reverse().map(workspaceMetadata).filter(Boolean),
|
|
238
|
+
evicted,
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
}, storageFile);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export async function getWebuiWorkspace(id, storageFile = webuiWorkspacesFile()) {
|
|
245
|
+
const requestedId = boundedString(id, 128);
|
|
246
|
+
if (!requestedId) return null;
|
|
247
|
+
const document = await readWorkspaceDocument(storageFile);
|
|
248
|
+
return document.workspaces.find((workspace) => workspace.id === requestedId) || null;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export async function deleteWebuiWorkspace(id, storageFile = webuiWorkspacesFile()) {
|
|
252
|
+
const requestedId = boundedString(id, 128);
|
|
253
|
+
if (!requestedId) return null;
|
|
254
|
+
return mutateWorkspaces((document) => {
|
|
255
|
+
const index = document.workspaces.findIndex((workspace) => workspace.id === requestedId);
|
|
256
|
+
if (index === -1) return { write: false, data: null };
|
|
257
|
+
document.workspaces.splice(index, 1);
|
|
258
|
+
return {
|
|
259
|
+
data: {
|
|
260
|
+
deletedId: requestedId,
|
|
261
|
+
workspaces: document.workspaces.slice().reverse().map(workspaceMetadata).filter(Boolean),
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
}, storageFile);
|
|
265
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@firstpick/pi-package-webui",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.9",
|
|
4
4
|
"description": "Pi Web UI companion package with a local browser UI CLI plus /webui-start and /webui-status commands.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://github.com/Firstp1ck/pi-coding-agent-forge/tree/main/pi-package-webui#readme",
|
|
@@ -51,11 +51,11 @@
|
|
|
51
51
|
"pi-webui": "./bin/pi-webui.mjs"
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
|
-
"check": "node --check public/app.js && node --check public/workflow-status-stack.mjs && node --check public/fast-output-live.mjs && node --check public/subagent-launch-slot-state.mjs && node --check public/subagent-gate-visibility.mjs && node --check public/voice-conversation.mjs && node --check public/aur-review-payload.mjs && node --check public/guided-git-command-state.mjs && node --check public/guided-git-review-state.mjs && node --check bin/pi-webui.mjs && node --check bin/pi-webui-rpc-supervisor.mjs && node --check lib/rpc-supervisor-protocol.mjs && node --check lib/rpc-supervisor-state.mjs && node --check lib/rpc-supervisor-client.mjs && node --check webui-rpc-helper.mjs && node --check lib/subagent-launch-slots.mjs && node --check lib/git-live-watcher.mjs && node --check lib/git-message-artifacts.mjs && node --check lib/trust-boundaries.mjs && node --check lib/native-command-adapter.mjs && node --check lib/session-actions.mjs && node --check lib/auth-actions.mjs && node --check lib/temp-artifacts.mjs && node tests/run-all.mjs",
|
|
54
|
+
"check": "node --check public/app.js && node --check public/issue-wizard-state.mjs && node --check public/issue-bot-client.mjs && node --check public/workflow-status-stack.mjs && node --check public/fast-output-live.mjs && node --check public/subagent-launch-slot-state.mjs && node --check public/subagent-gate-visibility.mjs && node --check public/voice-conversation.mjs && node --check public/aur-review-payload.mjs && node --check public/guided-git-command-state.mjs && node --check public/guided-git-review-state.mjs && node --check bin/pi-webui.mjs && node --check bin/pi-webui-rpc-supervisor.mjs && node --check lib/rpc-supervisor-protocol.mjs && node --check lib/rpc-supervisor-state.mjs && node --check lib/rpc-supervisor-client.mjs && node --check webui-rpc-helper.mjs && node --check lib/subagent-launch-slots.mjs && node --check lib/git-live-watcher.mjs && node --check lib/git-message-artifacts.mjs && node --check lib/trust-boundaries.mjs && node --check lib/native-command-adapter.mjs && node --check lib/session-actions.mjs && node --check lib/auth-actions.mjs && node --check lib/temp-artifacts.mjs && node tests/run-all.mjs",
|
|
55
55
|
"test": "node tests/run-all.mjs"
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@earendil-works/pi-coding-agent": "^0.82.
|
|
58
|
+
"@earendil-works/pi-coding-agent": "^0.82.1",
|
|
59
59
|
"@firstpick/pi-utils": "^0.2.5",
|
|
60
60
|
"mermaid": "^11.15.0",
|
|
61
61
|
"typebox": "^1.3.6"
|
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
"@firstpick/pi-extension-stats": "^0.2.9",
|
|
73
73
|
"@firstpick/pi-extension-todo-progress": "^0.2.9",
|
|
74
74
|
"@firstpick/pi-extension-tools": "^0.1.7",
|
|
75
|
-
"@firstpick/pi-extension-workflows": "^0.1.
|
|
75
|
+
"@firstpick/pi-extension-workflows": "^0.1.7",
|
|
76
76
|
"@firstpick/pi-package-remote-webui": "^0.1.8",
|
|
77
77
|
"@firstpick/pi-prompts-git-pr": "^0.1.5",
|
|
78
78
|
"@firstpick/pi-themes-bundle": "^0.1.5",
|