@firstpick/pi-package-webui 0.5.8 → 0.6.0
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 +12 -0
- package/bin/pi-webui.mjs +857 -12
- package/lib/git-worktrees.mjs +5 -1
- package/package.json +2 -2
- package/public/app.js +1090 -45
- package/public/index.html +20 -4
- package/public/service-worker.js +2 -1
- package/public/styles.css +368 -5
- package/public/voice-conversation.mjs +466 -0
- package/tests/compaction-resume-harness.test.mjs +114 -0
- package/tests/compaction-static.test.mjs +25 -0
- package/tests/fixtures/compacting-pi.mjs +115 -0
- package/tests/fixtures/fake-pi.mjs +220 -2
- package/tests/http-endpoints-harness.test.mjs +194 -2
- package/tests/mobile-static.test.mjs +82 -8
- package/tests/voice-conversation.test.mjs +361 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// JSONL RPC fixture that simulates a Pi session compacting while the Web UI
|
|
3
|
+
// queues a prompt for automatic resume after compaction_end.
|
|
4
|
+
import { createInterface } from "node:readline";
|
|
5
|
+
|
|
6
|
+
const COMPACTION_TRIGGER = "__pi_webui_test_start_compaction__";
|
|
7
|
+
|
|
8
|
+
let isCompacting = false;
|
|
9
|
+
let compactionTimer = null;
|
|
10
|
+
const received = [];
|
|
11
|
+
|
|
12
|
+
function respond(payload) {
|
|
13
|
+
process.stdout.write(`${JSON.stringify(payload)}\n`);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function emit(payload) {
|
|
17
|
+
process.stdout.write(`${JSON.stringify(payload)}\n`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function statePayload() {
|
|
21
|
+
return {
|
|
22
|
+
model: { provider: "fake", id: "fake-model" },
|
|
23
|
+
thinkingLevel: "off",
|
|
24
|
+
isStreaming: false,
|
|
25
|
+
isCompacting,
|
|
26
|
+
steeringMode: "one-at-a-time",
|
|
27
|
+
followUpMode: "one-at-a-time",
|
|
28
|
+
sessionFile: undefined,
|
|
29
|
+
sessionId: "fake-compaction-session",
|
|
30
|
+
sessionName: "fake-compaction",
|
|
31
|
+
autoCompactionEnabled: true,
|
|
32
|
+
messageCount: received.length,
|
|
33
|
+
pendingMessageCount: 0,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function finishCompaction() {
|
|
38
|
+
compactionTimer = null;
|
|
39
|
+
isCompacting = false;
|
|
40
|
+
emit({
|
|
41
|
+
type: "compaction_end",
|
|
42
|
+
reason: "test",
|
|
43
|
+
result: { summary: "fake compaction summary", tokensBefore: 1000, estimatedTokensAfter: 100 },
|
|
44
|
+
aborted: false,
|
|
45
|
+
willRetry: false,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function startCompaction() {
|
|
50
|
+
if (compactionTimer) clearTimeout(compactionTimer);
|
|
51
|
+
isCompacting = true;
|
|
52
|
+
emit({ type: "compaction_start", reason: "test" });
|
|
53
|
+
compactionTimer = setTimeout(finishCompaction, 1500);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const rl = createInterface({ input: process.stdin });
|
|
57
|
+
rl.on("line", (line) => {
|
|
58
|
+
if (!line.trim()) return;
|
|
59
|
+
let command;
|
|
60
|
+
try {
|
|
61
|
+
command = JSON.parse(line);
|
|
62
|
+
} catch {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const { id, type } = command || {};
|
|
67
|
+
if (!id || !type) return;
|
|
68
|
+
const base = { type: "response", id, command: type, success: true };
|
|
69
|
+
|
|
70
|
+
switch (type) {
|
|
71
|
+
case "get_state":
|
|
72
|
+
respond({ ...base, data: statePayload() });
|
|
73
|
+
return;
|
|
74
|
+
case "get_messages":
|
|
75
|
+
respond({
|
|
76
|
+
...base,
|
|
77
|
+
data: {
|
|
78
|
+
messages: received.map((entry, index) => ({
|
|
79
|
+
role: "user",
|
|
80
|
+
content: `${entry.type}:${entry.message}`,
|
|
81
|
+
timestamp: 1000 + index,
|
|
82
|
+
})),
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
return;
|
|
86
|
+
case "get_available_models":
|
|
87
|
+
respond({ ...base, data: { models: [{ provider: "fake", id: "fake-model", name: "Fake Model" }] } });
|
|
88
|
+
return;
|
|
89
|
+
case "get_session_stats":
|
|
90
|
+
respond({ ...base, data: { tokens: 0 } });
|
|
91
|
+
return;
|
|
92
|
+
case "prompt": {
|
|
93
|
+
const message = String(command.message || "");
|
|
94
|
+
if (message === COMPACTION_TRIGGER) {
|
|
95
|
+
startCompaction();
|
|
96
|
+
respond({ ...base, data: { compacting: true } });
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
received.push({ type: "prompt", message, streamingBehavior: command.streamingBehavior || "" });
|
|
100
|
+
respond({ ...base, data: { received: received.length } });
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
case "steer":
|
|
104
|
+
case "follow_up":
|
|
105
|
+
received.push({ type, message: String(command.message || "") });
|
|
106
|
+
respond({ ...base, data: { received: received.length } });
|
|
107
|
+
return;
|
|
108
|
+
default:
|
|
109
|
+
respond({ ...base, data: {} });
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
process.on("exit", () => {
|
|
114
|
+
if (compactionTimer) clearTimeout(compactionTimer);
|
|
115
|
+
});
|
|
@@ -1,18 +1,203 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// Minimal JSONL RPC stub standing in for the pi coding agent so HTTP endpoint
|
|
3
3
|
// tests can boot the real pi-webui server without a model provider.
|
|
4
|
+
//
|
|
5
|
+
// Optional env-gated features (default off; without them the voice scripting
|
|
6
|
+
// and logging additions are inert and command responses are unchanged, except
|
|
7
|
+
// that "steer" now gets an explicit success payload instead of the generic
|
|
8
|
+
// default-case response — no test asserts on either shape):
|
|
9
|
+
// - FAKE_PI_LOG_FILE=<path>: append one JSON line per received RPC command
|
|
10
|
+
// ({direction:"command", type, message}) plus one per scripted event emitted
|
|
11
|
+
// ({direction:"event", type}), so browser-driver tests can assert ordering.
|
|
12
|
+
// - FAKE_PI_VOICE_SCRIPTS=1: prompts containing "voice test say|question|tool|slow"
|
|
13
|
+
// respond success and then asynchronously emit a scripted agent event flow
|
|
14
|
+
// (agent_start/message_*/tool_execution_*/agent_end) over stdout; the scripted
|
|
15
|
+
// assistant turns are appended to a dynamic transcript returned by
|
|
16
|
+
// get_messages AFTER the three baseline messages, and get_state reports
|
|
17
|
+
// isStreaming=true while a scripted flow runs.
|
|
4
18
|
import { createInterface } from "node:readline";
|
|
19
|
+
import { randomUUID } from "node:crypto";
|
|
20
|
+
import { appendFileSync } from "node:fs";
|
|
5
21
|
|
|
6
22
|
const sessionIndex = process.argv.indexOf("--session");
|
|
7
23
|
const sessionFile = sessionIndex !== -1 ? process.argv[sessionIndex + 1] : undefined;
|
|
8
24
|
|
|
9
25
|
let activeBash = 0;
|
|
10
26
|
let peakBash = 0;
|
|
27
|
+
let thinkingLevel = "off";
|
|
28
|
+
let conversationEnabled = false;
|
|
29
|
+
|
|
30
|
+
const voiceScriptsEnabled = process.env.FAKE_PI_VOICE_SCRIPTS === "1";
|
|
31
|
+
const commandLogFile = process.env.FAKE_PI_LOG_FILE || "";
|
|
32
|
+
const dynamicMessages = [];
|
|
33
|
+
let scriptedStreaming = false;
|
|
34
|
+
|
|
35
|
+
function logJsonLine(entry) {
|
|
36
|
+
if (!commandLogFile) return;
|
|
37
|
+
try {
|
|
38
|
+
appendFileSync(commandLogFile, `${JSON.stringify({ at: Date.now(), ...entry })}\n`);
|
|
39
|
+
} catch {
|
|
40
|
+
// Logging must never break the fixture.
|
|
41
|
+
}
|
|
42
|
+
}
|
|
11
43
|
|
|
12
44
|
function respond(payload) {
|
|
13
45
|
process.stdout.write(`${JSON.stringify(payload)}\n`);
|
|
14
46
|
}
|
|
15
47
|
|
|
48
|
+
function emitEvent(payload) {
|
|
49
|
+
process.stdout.write(`${JSON.stringify(payload)}\n`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function emitScriptedEvent(payload) {
|
|
53
|
+
logJsonLine({ direction: "event", type: payload.type, ...(payload.toolName ? { toolName: payload.toolName } : {}) });
|
|
54
|
+
emitEvent(payload);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const scriptedTimers = new Set();
|
|
58
|
+
|
|
59
|
+
function runScriptedSteps(steps) {
|
|
60
|
+
let at = 0;
|
|
61
|
+
for (const step of steps) {
|
|
62
|
+
at += step.afterMs ?? 0;
|
|
63
|
+
const timer = setTimeout(() => {
|
|
64
|
+
scriptedTimers.delete(timer);
|
|
65
|
+
step.run();
|
|
66
|
+
}, at);
|
|
67
|
+
scriptedTimers.add(timer);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function cancelScriptedSteps() {
|
|
72
|
+
for (const timer of scriptedTimers) clearTimeout(timer);
|
|
73
|
+
scriptedTimers.clear();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Emits a full scripted assistant turn: agent_start, optional tool phase,
|
|
77
|
+
// streamed assistant text, message_end, agent_end. The final assistant text is
|
|
78
|
+
// appended to the dynamic transcript so get_messages returns it afterwards.
|
|
79
|
+
function runVoiceScriptFlow({ text, chunks, chunkSpacingMs = 60, toolPhase = false, toolDurationMs = 1500 }) {
|
|
80
|
+
scriptedStreaming = true;
|
|
81
|
+
const steps = [{ afterMs: 30, run: () => emitScriptedEvent({ type: "agent_start" }) }];
|
|
82
|
+
if (toolPhase) {
|
|
83
|
+
const toolCallId = `voice-tool-${randomUUID()}`;
|
|
84
|
+
steps.push({ afterMs: 50, run: () => emitScriptedEvent({ type: "tool_execution_start", toolCallId, toolName: "read", args: { path: "README.md" } }) });
|
|
85
|
+
steps.push({ afterMs: toolDurationMs, run: () => emitScriptedEvent({ type: "tool_execution_end", toolCallId, toolName: "read", isError: false, result: { content: [{ type: "text", text: "fake tool output" }] } }) });
|
|
86
|
+
}
|
|
87
|
+
steps.push({ afterMs: toolPhase ? 120 : 50, run: () => emitScriptedEvent({ type: "message_start", message: { role: "assistant" } }) });
|
|
88
|
+
for (const delta of chunks || [text.slice(0, Math.ceil(text.length / 2)), text.slice(Math.ceil(text.length / 2))]) {
|
|
89
|
+
steps.push({ afterMs: chunkSpacingMs, run: () => emitScriptedEvent({ type: "message_update", assistantMessageEvent: { type: "text_delta", delta } }) });
|
|
90
|
+
}
|
|
91
|
+
steps.push({
|
|
92
|
+
afterMs: chunkSpacingMs,
|
|
93
|
+
run: () => {
|
|
94
|
+
dynamicMessages.push({ role: "assistant", content: [{ type: "text", text }], timestamp: Date.now() });
|
|
95
|
+
emitScriptedEvent({ type: "message_end", message: { role: "assistant", content: [{ type: "text", text }] } });
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
steps.push({
|
|
99
|
+
afterMs: 60,
|
|
100
|
+
run: () => {
|
|
101
|
+
scriptedStreaming = false;
|
|
102
|
+
emitScriptedEvent({ type: "agent_end" });
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
runScriptedSteps(steps);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function handleVoiceScriptPrompt(command, base) {
|
|
109
|
+
if (!voiceScriptsEnabled) return false;
|
|
110
|
+
const message = String(command.message || "");
|
|
111
|
+
const marker = ["voice test say", "voice test question", "voice test tool", "voice test slow"].find((item) => message.includes(item));
|
|
112
|
+
if (!marker) return false;
|
|
113
|
+
dynamicMessages.push({ role: "user", content: message, timestamp: Date.now() });
|
|
114
|
+
respond({ ...base, data: { output: "voice scripted prompt accepted" } });
|
|
115
|
+
if (marker === "voice test say") {
|
|
116
|
+
runVoiceScriptFlow({ text: "Okay, this is the spoken answer." });
|
|
117
|
+
} else if (marker === "voice test question") {
|
|
118
|
+
runVoiceScriptFlow({ text: "Should we proceed with the next step?" });
|
|
119
|
+
} else if (marker === "voice test tool") {
|
|
120
|
+
runVoiceScriptFlow({ text: "The tool has finished.", toolPhase: true });
|
|
121
|
+
} else {
|
|
122
|
+
runVoiceScriptFlow({
|
|
123
|
+
text: "This is a long streaming answer that keeps going.",
|
|
124
|
+
chunks: ["This is a long ", "streaming answer ", "that keeps ", "going", "."],
|
|
125
|
+
chunkSpacingMs: 500,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function conversationStatusText() {
|
|
132
|
+
return conversationEnabled ? "Voice: listening" : "";
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function emitConversationStatus() {
|
|
136
|
+
emitEvent({
|
|
137
|
+
type: "extension_ui_request",
|
|
138
|
+
id: randomUUID(),
|
|
139
|
+
method: "setStatus",
|
|
140
|
+
statusKey: "natural-conversation",
|
|
141
|
+
statusText: conversationStatusText(),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function handleWebuiHelperPrompt(command, base) {
|
|
146
|
+
const message = String(command.message || "").trim();
|
|
147
|
+
const match = message.match(/^\/webui-helper\s+(\{[\s\S]*\})$/);
|
|
148
|
+
if (!match) return false;
|
|
149
|
+
let request = {};
|
|
150
|
+
try {
|
|
151
|
+
request = JSON.parse(match[1]);
|
|
152
|
+
} catch {
|
|
153
|
+
request = {};
|
|
154
|
+
}
|
|
155
|
+
const requestId = String(request.requestId || "");
|
|
156
|
+
const respondHelper = (payload) => emitEvent({
|
|
157
|
+
type: "extension_ui_request",
|
|
158
|
+
id: randomUUID(),
|
|
159
|
+
method: "notify",
|
|
160
|
+
message: `__PI_WEBUI_HELPER_RESPONSE__:${JSON.stringify(payload)}`,
|
|
161
|
+
notifyType: payload.ok === false ? "error" : "info",
|
|
162
|
+
});
|
|
163
|
+
respond({ ...base, data: { output: "webui-helper handled" } });
|
|
164
|
+
switch (request.action) {
|
|
165
|
+
case "tools-state":
|
|
166
|
+
respondHelper({ requestId, ok: true, data: { tools: [], activeTools: [] } });
|
|
167
|
+
return true;
|
|
168
|
+
case "skills-state":
|
|
169
|
+
respondHelper({ requestId, ok: true, data: { skills: [] } });
|
|
170
|
+
return true;
|
|
171
|
+
default:
|
|
172
|
+
respondHelper({ requestId, ok: false, error: `Unknown webui-helper action: ${String(request.action || "")}` });
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function handleTalkPrompt(command, base) {
|
|
178
|
+
const message = String(command.message || "").trim();
|
|
179
|
+
const match = message.match(/^\/(talk|voice|conversation)(?:\s+(\S+))?/i);
|
|
180
|
+
if (!match) return false;
|
|
181
|
+
const subcommand = String(match[2] || "").toLowerCase();
|
|
182
|
+
if (!subcommand) conversationEnabled = !conversationEnabled;
|
|
183
|
+
else if (["on", "enable", "start"].includes(subcommand)) conversationEnabled = true;
|
|
184
|
+
else if (["off", "disable", "end", "stop"].includes(subcommand)) conversationEnabled = false;
|
|
185
|
+
emitConversationStatus();
|
|
186
|
+
respond({
|
|
187
|
+
...base,
|
|
188
|
+
data: {
|
|
189
|
+
output: conversationEnabled ? "Natural Conversation Mode on." : "Natural Conversation Mode off.",
|
|
190
|
+
conversationMode: {
|
|
191
|
+
enabled: conversationEnabled,
|
|
192
|
+
uiState: conversationEnabled ? "listening" : "off",
|
|
193
|
+
statusText: conversationStatusText(),
|
|
194
|
+
allowedTools: ["read", "grep", "find", "ls"],
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
|
|
16
201
|
const rl = createInterface({ input: process.stdin });
|
|
17
202
|
rl.on("line", (line) => {
|
|
18
203
|
if (!line.trim()) return;
|
|
@@ -24,6 +209,7 @@ rl.on("line", (line) => {
|
|
|
24
209
|
}
|
|
25
210
|
const { id, type } = command || {};
|
|
26
211
|
if (!id || !type) return;
|
|
212
|
+
logJsonLine({ direction: "command", type, ...(command.message !== undefined ? { message: String(command.message) } : {}) });
|
|
27
213
|
const base = { type: "response", id, command: type, success: true };
|
|
28
214
|
|
|
29
215
|
switch (type) {
|
|
@@ -32,8 +218,8 @@ rl.on("line", (line) => {
|
|
|
32
218
|
...base,
|
|
33
219
|
data: {
|
|
34
220
|
model: { provider: "fake", id: "fake-model" },
|
|
35
|
-
thinkingLevel
|
|
36
|
-
isStreaming:
|
|
221
|
+
thinkingLevel,
|
|
222
|
+
isStreaming: scriptedStreaming,
|
|
37
223
|
isCompacting: false,
|
|
38
224
|
steeringMode: "one-at-a-time",
|
|
39
225
|
followUpMode: "one-at-a-time",
|
|
@@ -54,10 +240,36 @@ rl.on("line", (line) => {
|
|
|
54
240
|
{ role: "user", content: "fake prompt", timestamp: 1000 },
|
|
55
241
|
{ role: "assistant", content: [{ type: "text", text: "fake answer" }], timestamp: 2000 },
|
|
56
242
|
{ role: "user", content: "fake follow-up", timestamp: 3000 },
|
|
243
|
+
...dynamicMessages,
|
|
57
244
|
],
|
|
58
245
|
},
|
|
59
246
|
});
|
|
60
247
|
return;
|
|
248
|
+
case "get_commands":
|
|
249
|
+
respond({
|
|
250
|
+
...base,
|
|
251
|
+
data: {
|
|
252
|
+
commands: [
|
|
253
|
+
{ name: "talk", source: "extension", description: "Toggle Natural Conversation Mode" },
|
|
254
|
+
{ name: "voice", source: "extension", description: "Natural Conversation Mode alias" },
|
|
255
|
+
{ name: "conversation", source: "extension", description: "Natural Conversation Mode alias" },
|
|
256
|
+
],
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
return;
|
|
260
|
+
case "prompt":
|
|
261
|
+
if (handleWebuiHelperPrompt(command, base)) return;
|
|
262
|
+
if (handleTalkPrompt(command, base)) return;
|
|
263
|
+
if (handleVoiceScriptPrompt(command, base)) return;
|
|
264
|
+
respond({ ...base, data: { output: "fake prompt accepted" } });
|
|
265
|
+
return;
|
|
266
|
+
case "steer":
|
|
267
|
+
respond({ ...base, data: { output: "fake steer accepted" } });
|
|
268
|
+
return;
|
|
269
|
+
case "set_thinking_level":
|
|
270
|
+
thinkingLevel = String(command.level || "off");
|
|
271
|
+
respond({ ...base, data: { level: thinkingLevel } });
|
|
272
|
+
return;
|
|
61
273
|
case "get_available_models":
|
|
62
274
|
respond({ ...base, data: { models: [{ provider: "fake", id: "fake-model", name: "Fake Model" }] } });
|
|
63
275
|
return;
|
|
@@ -80,3 +292,9 @@ rl.on("line", (line) => {
|
|
|
80
292
|
respond({ ...base, data: {} });
|
|
81
293
|
}
|
|
82
294
|
});
|
|
295
|
+
|
|
296
|
+
rl.on("close", () => {
|
|
297
|
+
// The parent (pi-webui or a test driver) closed stdin or died; never let
|
|
298
|
+
// pending scripted voice events outlive it and write into a broken pipe.
|
|
299
|
+
cancelScriptedSteps();
|
|
300
|
+
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import { spawn, spawnSync } from "node:child_process";
|
|
3
|
+
import { createServer } from "node:http";
|
|
3
4
|
import { chmod, mkdir, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
4
5
|
import { networkInterfaces, tmpdir } from "node:os";
|
|
5
6
|
import path from "node:path";
|
|
@@ -37,6 +38,21 @@ async function request(host, pathname, { method = "GET", body, timeoutMs = 5_000
|
|
|
37
38
|
return { status: response.status, body: payload };
|
|
38
39
|
}
|
|
39
40
|
|
|
41
|
+
async function rmWithRetry(target) {
|
|
42
|
+
let lastError;
|
|
43
|
+
for (let attempt = 0; attempt < 8; attempt++) {
|
|
44
|
+
try {
|
|
45
|
+
await rm(target, { recursive: true, force: true });
|
|
46
|
+
return;
|
|
47
|
+
} catch (error) {
|
|
48
|
+
lastError = error;
|
|
49
|
+
if (error?.code !== "EBUSY" && error?.code !== "EPERM" && error?.code !== "ENOTEMPTY") throw error;
|
|
50
|
+
await delay(150 * (attempt + 1));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
throw lastError;
|
|
54
|
+
}
|
|
55
|
+
|
|
40
56
|
function runGitFixture(args, cwd, message) {
|
|
41
57
|
const result = spawnSync("git", args, {
|
|
42
58
|
cwd,
|
|
@@ -57,6 +73,28 @@ const cwd = await mkdtemp(path.join(tmpdir(), "pi-webui-http-harness-"));
|
|
|
57
73
|
const settingsFile = path.join(cwd, "webui-settings.json");
|
|
58
74
|
await chmod(fakePi, 0o755);
|
|
59
75
|
|
|
76
|
+
const voiceProviderRequests = [];
|
|
77
|
+
const voiceProvider = createServer(async (req, res) => {
|
|
78
|
+
const chunks = [];
|
|
79
|
+
for await (const chunk of req) chunks.push(chunk);
|
|
80
|
+
const body = Buffer.concat(chunks);
|
|
81
|
+
voiceProviderRequests.push({ method: req.method, url: req.url, contentType: req.headers["content-type"], bodyLength: body.length });
|
|
82
|
+
if (req.url === "/stt") {
|
|
83
|
+
res.writeHead(200, { "content-type": "application/json" });
|
|
84
|
+
res.end(JSON.stringify({ text: "fake transcript from local stt" }));
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (req.url === "/tts") {
|
|
88
|
+
res.writeHead(200, { "content-type": "audio/mpeg" });
|
|
89
|
+
res.end(Buffer.from("fake mp3 bytes"));
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
res.writeHead(404, { "content-type": "application/json" });
|
|
93
|
+
res.end(JSON.stringify({ error: "not found" }));
|
|
94
|
+
});
|
|
95
|
+
await new Promise((resolve) => voiceProvider.listen(0, "127.0.0.1", resolve));
|
|
96
|
+
const voiceProviderPort = voiceProvider.address().port;
|
|
97
|
+
|
|
60
98
|
const child = spawn(process.execPath, [serverScript, "--cwd", cwd, "--host", "0.0.0.0", "--port", String(port), "--pi", fakePi], {
|
|
61
99
|
stdio: ["ignore", "pipe", "pipe"],
|
|
62
100
|
env: {
|
|
@@ -66,6 +104,8 @@ const child = spawn(process.execPath, [serverScript, "--cwd", cwd, "--host", "0.
|
|
|
66
104
|
GIT_COMMITTER_NAME: "Pi WebUI Test",
|
|
67
105
|
GIT_COMMITTER_EMAIL: "pi-webui-test@example.invalid",
|
|
68
106
|
PI_WEBUI_SETTINGS_FILE: settingsFile,
|
|
107
|
+
PI_VOICE_STT_URL: `http://127.0.0.1:${voiceProviderPort}/stt`,
|
|
108
|
+
PI_VOICE_TTS_URL: `http://127.0.0.1:${voiceProviderPort}/tts`,
|
|
69
109
|
},
|
|
70
110
|
});
|
|
71
111
|
let serverOutput = "";
|
|
@@ -125,6 +165,17 @@ try {
|
|
|
125
165
|
assert.equal(gzipResponse.headers.get("content-encoding"), "gzip", "styles.css should fall back to gzip");
|
|
126
166
|
await gzipResponse.arrayBuffer();
|
|
127
167
|
|
|
168
|
+
// The browser voice loop is dynamically imported by app.js; a missing static
|
|
169
|
+
// allowlist entry would 404 the module and silently disable voice mode.
|
|
170
|
+
const voiceModuleResponse = await fetch(`http://127.0.0.1:${port}/voice-conversation.mjs`, {
|
|
171
|
+
signal: AbortSignal.timeout(5_000),
|
|
172
|
+
});
|
|
173
|
+
assert.equal(voiceModuleResponse.status, 200, "voice-conversation.mjs must be served for the Natural Conversation browser voice loop");
|
|
174
|
+
assert.match(voiceModuleResponse.headers.get("content-type") || "", /text\/javascript/, "voice-conversation.mjs should use a JavaScript MIME type");
|
|
175
|
+
const voiceModuleBody = await voiceModuleResponse.arrayBuffer();
|
|
176
|
+
const rawVoiceModuleSize = (await stat(join(root, "public", "voice-conversation.mjs"))).size;
|
|
177
|
+
assert.equal(voiceModuleBody.byteLength, rawVoiceModuleSize, "served voice-conversation.mjs should match the raw file byte-for-byte in size");
|
|
178
|
+
|
|
128
179
|
const mermaidModuleResponse = await fetch(`http://127.0.0.1:${port}/vendor/mermaid/mermaid.esm.min.mjs`, {
|
|
129
180
|
signal: AbortSignal.timeout(5_000),
|
|
130
181
|
});
|
|
@@ -567,6 +618,62 @@ try {
|
|
|
567
618
|
await request("127.0.0.1", "/api/app-runner/clear", { method: "POST", body: { tab: tabId } });
|
|
568
619
|
}
|
|
569
620
|
|
|
621
|
+
if (process.platform !== "win32") {
|
|
622
|
+
await writeFile(path.join(cwd, "signal-runner.mjs"), [
|
|
623
|
+
"import { writeFileSync } from 'node:fs';",
|
|
624
|
+
"process.on('SIGINT', () => { writeFileSync('signal-result.txt', 'SIGINT\\n'); process.exit(130); });",
|
|
625
|
+
"process.on('SIGTERM', () => { writeFileSync('signal-result.txt', 'SIGTERM\\n'); process.exit(143); });",
|
|
626
|
+
"console.log('signal runner ready');",
|
|
627
|
+
"setInterval(() => {}, 1000);",
|
|
628
|
+
"",
|
|
629
|
+
].join("\n"));
|
|
630
|
+
const savedSignalRunner = await request("127.0.0.1", "/api/app-runner-config", {
|
|
631
|
+
method: "POST",
|
|
632
|
+
body: { tab: tabId, runner: { label: "Signal node", command: process.execPath, path: "signal-runner.mjs" } },
|
|
633
|
+
timeoutMs: 10_000,
|
|
634
|
+
});
|
|
635
|
+
assert.equal(savedSignalRunner.status, 200, `saving a signal custom runner should succeed: ${savedSignalRunner.body?.error || ""}`);
|
|
636
|
+
const signalRunner = savedSignalRunner.body?.data?.runners?.find((runner) => runner.custom === true && runner.label === "Signal node");
|
|
637
|
+
assert.ok(signalRunner?.id, "signal custom runner should appear in detected app runners");
|
|
638
|
+
let signalRunState = await request("127.0.0.1", "/api/app-runner", {
|
|
639
|
+
method: "POST",
|
|
640
|
+
body: { tab: tabId, runnerId: signalRunner.id },
|
|
641
|
+
timeoutMs: 10_000,
|
|
642
|
+
});
|
|
643
|
+
assert.equal(signalRunState.status, 200, `signal runner start should return ok: ${signalRunState.body?.error || ""}`);
|
|
644
|
+
for (let attempt = 0; attempt < 50; attempt++) {
|
|
645
|
+
signalRunState = await request("127.0.0.1", `/api/app-runners?tab=${encodeURIComponent(tabId)}`, { timeoutMs: 5_000 });
|
|
646
|
+
const output = [
|
|
647
|
+
...(signalRunState.body?.data?.activeRun?.lines || []),
|
|
648
|
+
signalRunState.body?.data?.activeRun?.pendingLine || "",
|
|
649
|
+
].join("\n");
|
|
650
|
+
if (/signal runner ready/.test(output)) break;
|
|
651
|
+
await delay(100);
|
|
652
|
+
}
|
|
653
|
+
assert.match([
|
|
654
|
+
...(signalRunState.body?.data?.activeRun?.lines || []),
|
|
655
|
+
signalRunState.body?.data?.activeRun?.pendingLine || "",
|
|
656
|
+
].join("\n"), /signal runner ready/, "signal app runner should start before stop is requested");
|
|
657
|
+
const signalStop = await request("127.0.0.1", "/api/app-runner/stop", { method: "POST", body: { tab: tabId }, timeoutMs: 10_000 });
|
|
658
|
+
assert.equal(signalStop.status, 200, `signal runner stop should return ok: ${signalStop.body?.error || ""}`);
|
|
659
|
+
assert.match((signalStop.body?.data?.activeRun?.lines || []).join("\n"), /sending Ctrl\+C/, "Web UI stop should document Ctrl+C-equivalent interruption");
|
|
660
|
+
let signalResult = "";
|
|
661
|
+
for (let attempt = 0; attempt < 50; attempt++) {
|
|
662
|
+
signalRunState = await request("127.0.0.1", `/api/app-runners?tab=${encodeURIComponent(tabId)}`, { timeoutMs: 5_000 });
|
|
663
|
+
try {
|
|
664
|
+
signalResult = await readFile(path.join(cwd, "signal-result.txt"), "utf8");
|
|
665
|
+
} catch {
|
|
666
|
+
signalResult = "";
|
|
667
|
+
}
|
|
668
|
+
const signalRunStopped = signalRunState.body?.data?.activeRun?.status && signalRunState.body.data.activeRun.status !== "running";
|
|
669
|
+
if (signalResult && signalRunStopped) break;
|
|
670
|
+
await delay(100);
|
|
671
|
+
}
|
|
672
|
+
assert.equal(signalResult.trim(), "SIGINT", "Web UI app-runner stop should deliver SIGINT like terminal Ctrl+C, not SIGTERM");
|
|
673
|
+
assert.notEqual(signalRunState.body?.data?.activeRun?.status, "running", "signal app runner should fully stop before cleanup continues");
|
|
674
|
+
await request("127.0.0.1", "/api/app-runner/clear", { method: "POST", body: { tab: tabId } });
|
|
675
|
+
}
|
|
676
|
+
|
|
570
677
|
await writeFile(path.join(cwd, ".pi-webui-runners.json"), `${JSON.stringify({
|
|
571
678
|
version: 1,
|
|
572
679
|
runners: [{ id: "broken-custom", label: "Broken custom", command: "definitely-missing-pi-webui-runner", path: "custom-runner.mjs" }],
|
|
@@ -587,6 +694,73 @@ try {
|
|
|
587
694
|
assert.equal(copy.body?.data?.status, "succeeded", "native /copy should succeed through the adapter");
|
|
588
695
|
assert.equal(copy.body?.data?.copyText, "fake last text");
|
|
589
696
|
|
|
697
|
+
// Natural Conversation shell: /talk availability drives per-tab status and safety guards.
|
|
698
|
+
const conversationFeature = await request("127.0.0.1", `/api/features/natural-conversation?tab=${encodeURIComponent(tabId)}`);
|
|
699
|
+
assert.equal(conversationFeature.status, 200);
|
|
700
|
+
assert.equal(conversationFeature.body?.data?.available, true, "fake /talk command should make Natural Conversation available");
|
|
701
|
+
assert.ok(conversationFeature.body?.data?.commands?.includes("talk"), "Natural Conversation feature data should expose loaded /talk commands");
|
|
702
|
+
assert.equal(conversationFeature.body?.data?.mode?.enabled, false, "Natural Conversation should start disabled per tab");
|
|
703
|
+
|
|
704
|
+
const conversationVoices = await request("127.0.0.1", `/api/conversation-voices?tab=${encodeURIComponent(tabId)}`);
|
|
705
|
+
assert.equal(conversationVoices.status, 200);
|
|
706
|
+
assert.ok(Array.isArray(conversationVoices.body?.data?.voices), "conversation-voices should return a voices array");
|
|
707
|
+
assert.ok(
|
|
708
|
+
conversationVoices.body.data.voices.some((voice) => voice.id === "de_DE-thorsten-medium"),
|
|
709
|
+
"conversation-voices should include the mirrored Piper catalog",
|
|
710
|
+
);
|
|
711
|
+
const badVoice = await request("127.0.0.1", "/api/conversation-voice", {
|
|
712
|
+
method: "POST",
|
|
713
|
+
body: { voice: "../etc/passwd; rm -rf", tab: tabId },
|
|
714
|
+
});
|
|
715
|
+
assert.equal(badVoice.status, 400, "conversation-voice must reject ids that are not plain voice names");
|
|
716
|
+
|
|
717
|
+
const conversationOn = await request("127.0.0.1", "/api/conversation-mode", {
|
|
718
|
+
method: "POST",
|
|
719
|
+
body: { enabled: true, tab: tabId },
|
|
720
|
+
});
|
|
721
|
+
assert.equal(conversationOn.status, 200, `conversation-mode on should succeed: ${conversationOn.body?.error || ""}`);
|
|
722
|
+
assert.equal(conversationOn.body?.data?.mode?.enabled, true, "conversation-mode on should update tab-local mode state");
|
|
723
|
+
assert.equal(conversationOn.body?.tab?.conversationMode?.enabled, true, "conversation mode should be included in returned tab metadata");
|
|
724
|
+
|
|
725
|
+
const conversationState = await request("127.0.0.1", `/api/state?tab=${encodeURIComponent(tabId)}`);
|
|
726
|
+
assert.equal(conversationState.status, 200);
|
|
727
|
+
assert.equal(conversationState.body?.data?.thinkingLevel, "off", "Natural Conversation shell should keep thinking forced off");
|
|
728
|
+
|
|
729
|
+
const blockedSlash = await request("127.0.0.1", "/api/prompt", { method: "POST", body: { message: "/copy", tab: tabId } });
|
|
730
|
+
assert.equal(blockedSlash.status, 409, "non-/talk slash commands should be blocked while Natural Conversation is active");
|
|
731
|
+
assert.match(String(blockedSlash.body?.error || ""), /Natural Conversation Mode is active; slash commands are blocked/);
|
|
732
|
+
|
|
733
|
+
const blockedSettings = await request("127.0.0.1", "/api/settings", { method: "POST", body: { thinkingLevel: "high", tab: tabId } });
|
|
734
|
+
assert.equal(blockedSettings.status, 409, "settings changes should be blocked while Natural Conversation is active");
|
|
735
|
+
assert.match(String(blockedSettings.body?.error || ""), /settings changes are blocked/);
|
|
736
|
+
|
|
737
|
+
const blockedBash = await request("127.0.0.1", "/api/bash", { method: "POST", body: { command: "echo blocked", tab: tabId } });
|
|
738
|
+
assert.equal(blockedBash.status, 409, "user bash should be blocked while Natural Conversation is active");
|
|
739
|
+
assert.match(String(blockedBash.body?.error || ""), /bash is blocked|Natural Conversation Mode is active/);
|
|
740
|
+
|
|
741
|
+
const allowedConversationPrompt = await request("127.0.0.1", "/api/prompt", { method: "POST", body: { message: "Explain the current repo briefly", tab: tabId } });
|
|
742
|
+
assert.equal(allowedConversationPrompt.status, 200, "ordinary prompts remain allowed while Natural Conversation is active");
|
|
743
|
+
|
|
744
|
+
const localStt = await request("127.0.0.1", "/api/stt/transcribe", { method: "POST", body: { tab: tabId, provider: "local", mimeType: "audio/webm", audioBase64: Buffer.from("fake audio").toString("base64") } });
|
|
745
|
+
assert.equal(localStt.status, 200, `local STT fallback should transcribe through the configured endpoint: ${localStt.body?.error || ""}`);
|
|
746
|
+
assert.equal(localStt.body?.data?.provider, "local");
|
|
747
|
+
assert.equal(localStt.body?.data?.text, "fake transcript from local stt");
|
|
748
|
+
assert.ok(voiceProviderRequests.some((item) => item.url === "/stt" && /multipart\/form-data/.test(String(item.contentType || ""))), "local STT should receive a multipart audio upload");
|
|
749
|
+
|
|
750
|
+
const localTts = await request("127.0.0.1", "/api/tts/speech", { method: "POST", body: { tab: tabId, provider: "local", text: "Say this aloud" } });
|
|
751
|
+
assert.equal(localTts.status, 200, `local TTS fallback should synthesize through the configured endpoint: ${localTts.body?.error || ""}`);
|
|
752
|
+
assert.equal(localTts.body?.data?.provider, "local");
|
|
753
|
+
assert.equal(localTts.body?.data?.contentType, "audio/mpeg");
|
|
754
|
+
assert.equal(Buffer.from(localTts.body?.data?.audioBase64 || "", "base64").toString("utf8"), "fake mp3 bytes");
|
|
755
|
+
assert.ok(voiceProviderRequests.some((item) => item.url === "/tts" && /application\/json/.test(String(item.contentType || ""))), "local TTS should receive a JSON text synthesis request");
|
|
756
|
+
|
|
757
|
+
const conversationOff = await request("127.0.0.1", "/api/conversation-mode", {
|
|
758
|
+
method: "POST",
|
|
759
|
+
body: { enabled: false, tab: tabId },
|
|
760
|
+
});
|
|
761
|
+
assert.equal(conversationOff.status, 200, `conversation-mode off should succeed: ${conversationOff.body?.error || ""}`);
|
|
762
|
+
assert.equal(conversationOff.body?.data?.mode?.enabled, false, "conversation-mode off should restore normal WebUI actions");
|
|
763
|
+
|
|
590
764
|
// Bash FIFO queue: concurrent requests must execute serially on the RPC.
|
|
591
765
|
const [bashA, bashB] = await Promise.all([
|
|
592
766
|
request("127.0.0.1", "/api/bash", { method: "POST", body: { command: "echo a", tab: tabId }, timeoutMs: 10_000 }),
|
|
@@ -622,6 +796,20 @@ try {
|
|
|
622
796
|
const remoteHealthBeforeAuth = await request(lan, "/api/health");
|
|
623
797
|
assert.equal(remoteHealthBeforeAuth.status, 200, "LAN clients should connect without a PIN while auth is off");
|
|
624
798
|
|
|
799
|
+
const remoteSttWithoutConsent = await request(lan, "/api/stt/transcribe", {
|
|
800
|
+
method: "POST",
|
|
801
|
+
body: { tab: tabId, provider: "local", mimeType: "audio/webm", audioBase64: Buffer.from("fake remote audio").toString("base64") },
|
|
802
|
+
});
|
|
803
|
+
assert.equal(remoteSttWithoutConsent.status, 403, "remote STT uploads must require explicit microphone streaming consent");
|
|
804
|
+
assert.match(String(remoteSttWithoutConsent.body?.error || ""), /explicit.*remote microphone streaming consent/i);
|
|
805
|
+
|
|
806
|
+
const remoteSttWithConsent = await request(lan, "/api/stt/transcribe", {
|
|
807
|
+
method: "POST",
|
|
808
|
+
body: { tab: tabId, provider: "local", mimeType: "audio/webm", audioBase64: Buffer.from("fake remote audio").toString("base64"), remoteMicStreamingConsentAccepted: true },
|
|
809
|
+
});
|
|
810
|
+
assert.equal(remoteSttWithConsent.status, 200, "remote STT uploads should proceed after explicit per-request consent");
|
|
811
|
+
assert.equal(remoteSttWithConsent.body?.data?.text, "fake transcript from local stt");
|
|
812
|
+
|
|
625
813
|
const remoteDelete = await request(lan, "/api/session-delete", {
|
|
626
814
|
method: "POST",
|
|
627
815
|
body: { sessionPath: path.join(cwd, "outside.jsonl"), confirmed: true, tab: tabId },
|
|
@@ -709,8 +897,12 @@ try {
|
|
|
709
897
|
}
|
|
710
898
|
assert.notEqual(child.exitCode, null, "server should exit after /api/shutdown");
|
|
711
899
|
} finally {
|
|
712
|
-
if (child.exitCode === null)
|
|
713
|
-
|
|
900
|
+
if (child.exitCode === null) {
|
|
901
|
+
child.kill("SIGKILL");
|
|
902
|
+
await new Promise((resolve) => child.once("exit", resolve));
|
|
903
|
+
}
|
|
904
|
+
await new Promise((resolve) => voiceProvider.close(() => resolve()));
|
|
905
|
+
await rmWithRetry(cwd);
|
|
714
906
|
}
|
|
715
907
|
|
|
716
908
|
console.log("http-endpoints-harness.test.mjs passed");
|