Repository navigation
Expand file tree
/
Copy pathbrowser.ts
More file actions
317 lines (309 loc) · 12.5 KB
/
Copy pathbrowser.ts
File metadata and controls
317 lines (309 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Exercise the operator's real Console sign-in and Open Agent UI launch. Keep
// browser credentials and conversation history on this runner.
import assert from "node:assert/strict";
import { randomUUID } from "node:crypto";
// Synchronous reads: a rejected fs/promises read has no caller frame for the location.
import { readFileSync } from "node:fs";
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { setTimeout as delay } from "node:timers/promises";
import { chromium } from "playwright";
import type { Browser, Page } from "playwright";
import { textFromFrame, waitForStockUi } from "../../../tests/helpers/native-ui-chat.ts";
import { launchTrustingBrowser } from "../../../tests/helpers/trusted-chromium.ts";
import { isRecord } from "../records.ts";
// Playwright runs these calls in the stock UI's page context.
declare const openclawControlUi: {
navigate(route: "sessions", location: { pathname: string; search: string }): void;
snapshot(): { basePath: string; routeReady: boolean; sessionKey: string };
};
type BrowserHistory = {
agent: string;
namespace: string;
sessionKey: string;
sessionId: string;
preference: string;
};
let stage = "initialize";
let browser: Browser | undefined;
async function openAgent(consolePage: Page, origin: string, sessionKey?: string) {
const [page] = await Promise.all([
consolePage.waitForEvent("popup", { timeout: 60_000 }),
consolePage.getByRole("link", { name: "Open Agent UI", exact: true }).click(),
]);
await page.waitForURL((url) => url.origin === origin, { timeout: 60_000 });
const messages: { text: string; sessionKey: string }[] = [];
page.on("websocket", (ws) => {
if (new URL(ws.url()).origin !== origin.replace("https:", "wss:")) {
return;
}
ws.on("framereceived", (frame) => {
let event: unknown;
try {
event = JSON.parse(textFromFrame(frame));
} catch {
return;
}
if (!isRecord(event) || event.type !== "event" || !isRecord(event.payload)) {
return;
}
const { message, sessionKey } = event.payload;
if (
typeof sessionKey !== "string" ||
!isRecord(message) ||
message.role !== "assistant" ||
message.isError
) {
return;
}
const terminal =
(event.event === "chat" && event.payload.state === "final") ||
(event.event === "session.message" &&
isRecord(message.__openclaw) &&
message.__openclaw.runTerminal === true);
const { content, stopReason } = message;
if (
!terminal ||
(typeof stopReason === "string" && ["toolUse", "aborted", "error"].includes(stopReason))
) {
return;
}
let text = "";
if (typeof content === "string") {
text = content;
} else if (Array.isArray(content)) {
const parts: readonly unknown[] = content;
for (const part of parts) {
if (isRecord(part) && part.type === "text" && typeof part.text === "string") {
text += part.text;
}
}
}
messages.push({ text, sessionKey });
});
});
// A document request for a chat permalink reads public chat publications.
// Load the signed-in UI first, then open the private chat from its history.
const response = await page.goto(new URL("/new", origin).href);
if (!response?.ok()) {
console.error("Control UI navigation HTTP " + (response?.status() ?? 0));
throw new Error("Control UI navigation failed");
}
await waitForStockUi(page);
if (sessionKey !== undefined) {
stage = "search signed-in session history";
await page.evaluate((sessionKey) => {
const app = openclawControlUi;
app.navigate("sessions", {
pathname: app.snapshot().basePath + "/sessions",
search: "?" + new URLSearchParams({ session: sessionKey, status: "all" }),
});
}, sessionKey);
await page.waitForURL((url) => url.origin === origin && url.pathname === "/sessions", {
timeout: 60_000,
});
// The displayed title may change. The Sessions table labels its selection
// control with the full key and links to that same row's private chat.
stage = "open matching session history row";
const row = page.getByRole("row").filter({
has: page.getByRole("checkbox", { name: "Select session: " + sessionKey, exact: true }),
});
const chatLink = row.locator("a.session-link");
await chatLink.waitFor({ state: "visible", timeout: 60_000 });
await chatLink.click();
stage = "verify original session selected";
await page.waitForFunction(
(sessionKey) => {
const state = openclawControlUi.snapshot();
return state.routeReady === true && state.sessionKey === sessionKey;
},
sessionKey,
{ timeout: 90_000 },
);
assert.equal(new URL(page.url()).origin, origin);
}
async function send(prompt: string, expected: string, newChat = false) {
const editor = page.locator(".agent-chat__composer-combobox > textarea").first();
await editor.fill(prompt);
const start = messages.length;
await page
.getByRole("button", { name: newChat ? "Start session" : "Send message", exact: true })
.click();
const deadline = Date.now() + 360_000;
while (Date.now() < deadline) {
const result = messages
.slice(start)
.find(
(m) =>
m.text.trim() === expected && (sessionKey === undefined || m.sessionKey === sessionKey),
);
if (result) {
return result.sessionKey;
}
await delay(500);
}
throw new Error("Expected a new terminal assistant message");
}
return { page, send };
}
try {
const phase = process.argv[2];
assert.ok(phase === "onboard" || phase === "final");
const { RUN_DIR, CONSOLE_ORIGIN, agent, namespace, revision } = process.env;
assert.ok(RUN_DIR && CONSOLE_ORIGIN && agent && namespace && revision);
const stateDir = join(RUN_DIR, "k3d-state");
const historyFile = join(RUN_DIR, "browser-conversation.json");
const state: unknown = JSON.parse(readFileSync(join(stateDir, "state.json"), "utf8"));
assert.ok(isRecord(state));
const { browserOrigin, cluster, nativeAdminDomain } = state;
assert.ok(typeof cluster === "string" && typeof nativeAdminDomain === "string");
const origin = new URL(CONSOLE_ORIGIN);
assert.equal(origin.origin, browserOrigin);
assert.equal(origin.hostname, `console.${cluster}.oce.localhost`);
assert.equal(origin.protocol, "https:");
const browserDir = join(RUN_DIR, `browser-${phase}`);
await mkdir(browserDir, { mode: 0o700 });
stage = "launch trusted Chromium";
browser = await launchTrustingBrowser(chromium, browserDir, {
caFiles: [join(stateDir, "browser-ca.crt")],
hosts: [
{ host: origin.hostname, address: "127.0.0.1" },
{ host: `*.${nativeAdminDomain}`, address: "127.0.0.1" },
{ host: "*", address: "~NOTFOUND" },
],
});
const context = await browser.newContext({ serviceWorkers: "block" });
const allowed = new Set([origin.origin]);
await context.route("**/*", (route) =>
allowed.has(new URL(route.request().url()).origin) ? route.continue() : route.abort(),
);
stage = "sign in to Console";
const owner = await context.newPage();
const loaded = await owner.goto(new URL("/console/", origin).href);
assert.equal((await loaded?.serverAddr())?.ipAddress, "127.0.0.1");
await owner.getByLabel("Email", { exact: true }).fill("admin@development.openclaw.invalid");
assert.equal(new URL(owner.url()).origin, origin.origin);
await owner
.getByLabel("Password", { exact: true })
.fill(readFileSync(join(stateDir, "initial-admin-password"), "utf8").trim());
const [login] = await Promise.all([
owner.waitForResponse(
(res) =>
res.url() === origin.origin + "/api/auth/sign-in/email" &&
res.request().method() === "POST",
),
owner.getByRole("button", { name: "Sign in", exact: true }).click(),
]);
assert.equal(login.status(), 200);
await owner.getByLabel("Password", { exact: true }).waitFor({ state: "hidden" });
stage = "resolve the running revision";
const detail = new URL(`/console/agents/${agent}`, origin);
detail.search = new URLSearchParams({ namespace, revision, tab: "workspace" }).toString();
await owner.goto(detail.href);
const access = await owner.evaluate(async (path) => {
const response = await fetch(path, { mode: "same-origin", redirect: "error" });
if (!response.ok) {
throw new Error("Native Agent access failed");
}
const envelope: unknown = await response.json();
if (typeof envelope !== "object" || envelope === null || !("data" in envelope)) {
throw new Error("Native Agent access response is missing data");
}
return envelope.data;
}, `/namespaces/${namespace}/agents/${agent}/native-admin`);
assert.ok(isRecord(access));
assert.equal(access.activeRevisionId, revision);
assert.equal(access.status, "available");
assert.ok(typeof access.origin === "string");
const nativeOrigin = new URL(access.origin);
assert.equal(nativeOrigin.protocol, "https:");
assert.ok(nativeOrigin.hostname.endsWith("." + nativeAdminDomain));
assert.equal(nativeOrigin.port, origin.port);
allowed.add(nativeOrigin.origin);
let history: BrowserHistory;
if (phase === "onboard") {
stage = "complete onboarding";
const ui = await openAgent(owner, nativeOrigin.origin);
const id = randomUUID().slice(0, 8);
const name = "Wren-" + id;
const preference = "saffron-" + randomUUID();
const marker = "OCE_ONBOARDED_" + id;
const sessionKey = await ui.send(
`Complete first-run onboarding in this workspace. Your name is ${name}; my name is Alex, and my preferred project color is ${preference}. Use sensible defaults for the rest. Save your name to IDENTITY.md and my preference to USER.md. Follow BOOTSTRAP.md and remove it when finished. Then reply with exactly ${marker}.`,
marker,
true,
);
const sessionId = /^agent:main:dashboard:([a-f0-9-]{36})$/.exec(sessionKey)?.[1];
assert.ok(sessionId);
await ui.page.waitForURL((url) => url.pathname.startsWith("/chat/main/"));
history = {
agent,
namespace,
sessionKey,
sessionId,
preference,
};
stage = "verify Workspace identity and preference";
await owner.reload();
for (const [filename, expected] of [
["IDENTITY.md", name],
["USER.md", preference],
] as const) {
await owner.getByText(filename + " loaded.", { exact: true }).waitFor({ state: "attached" });
assert.ok(
(await owner.getByLabel(filename, { exact: true }).inputValue()).includes(expected),
);
}
stage = "verify ready reply";
assert.equal(await ui.send("Reply with OCE_READY.", "OCE_READY"), sessionKey);
await ui.page.close();
} else {
const saved: unknown = JSON.parse(readFileSync(historyFile, "utf8"));
assert.ok(isRecord(saved));
assert.equal(saved.agent, agent);
assert.equal(saved.namespace, namespace);
const { sessionKey, sessionId, preference } = saved;
assert.ok(
typeof sessionKey === "string" &&
typeof sessionId === "string" &&
typeof preference === "string",
);
history = { agent, namespace, sessionKey, sessionId, preference };
}
stage = "reopen original conversation from chat history";
const reopened = await openAgent(owner, nativeOrigin.origin, history.sessionKey);
stage = "recall saved preference";
const nonce = "OCE_RECALL_" + randomUUID().slice(0, 8);
await reopened.send(
`What is my preferred project color? Reply with exactly ${nonce}, a space, and its saved value.`,
nonce + " " + history.preference,
);
if (phase === "onboard") {
await writeFile(historyFile, JSON.stringify(history), { mode: 0o600, flag: "wx" });
}
await writeFile(
join(RUN_DIR, `receipt-browser-${phase}.json`),
JSON.stringify({
phase,
agentId: agent,
namespaceId: namespace,
revisionId: revision,
sessionId: history.sessionId,
terminalAssistant: true,
preferenceRecalled: true,
...(phase === "onboard" ? { workspaceVerified: true } : {}),
}),
{ mode: 0o600, flag: "wx" },
);
} catch (error) {
// Export only a source line, not a stack frame, URL or assertion operand.
const frame = error instanceof Error && error.stack?.match(/\bbrowser\.ts:(\d{1,5}):\d+/);
if (frame) {
console.error("quickstart-location browser.ts:" + frame[1]);
}
// Playwright errors can contain URLs, form values and returned content.
console.error(`Control UI verification failed during ${stage}.`);
process.exitCode = 1;
} finally {
await browser?.close();
}