Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 104 additions & 25 deletions packages/redrob/src/browser/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,25 @@ export type Ask = (request: Channels.DeskBrowserRequest, signal?: AbortSignal) =

export type Result =
| { readonly kind: "extension"; readonly browser: string; readonly url: string; readonly text: string }
| { readonly kind: "fallback"; readonly reason: string }
| {
readonly kind: "fallback"
readonly reason: string
/** No Chrome with the Redrob extension in this account, said once per session: tell the person how to connect. */
readonly connect?: boolean
}

/** A pairing request waiting for Allow in the person's Chrome while a tool call waits on it; undefined when none. */
export type Pairing = { readonly code: string; readonly name: string } | undefined

export type Context = {
readonly sessionID: string
readonly signal?: AbortSignal
readonly ask?: Ask
/** Where the daemon's link stands; polled while a call waits, for `onPairing`. */
readonly observe?: () => Promise<Channels.DeskBrowserState | undefined>
/** Called with the request while one waits for Allow, and with undefined once it is over. */
readonly onPairing?: (pairing: Pairing) => void
}

const POLL_MS = 1_000
/** A task waits at most this long, as long as a hand-over to the person may last. */
Expand Down Expand Up @@ -61,6 +79,8 @@ type Session = {
elements: Map<string, string>
/** The browser this session last used; a session stays in it until the next `open`. */
used?: "extension" | "chromium"
/** The how-to-connect line was given in this session. */
hinted?: boolean
}

const sessions = new Map<string, Session>()
Expand Down Expand Up @@ -138,19 +158,43 @@ const sleep = (ms: number, signal?: AbortSignal) =>
* Runs one action in the person's Chrome. A `fallback` result means it never reached the extension
* (the tool uses its own Chromium and says why); anything that went wrong after that throws.
*/
export async function act(
input: Input,
context: { readonly sessionID: string; readonly signal?: AbortSignal; readonly ask?: Ask },
): Promise<Result> {
export async function act(input: Input, context: Context): Promise<Result> {
const result = await attempt(input, context)
if (result.kind === "fallback") session(context.sessionID).used = "chromium"
return result
}

async function attempt(
input: Input,
context: { readonly sessionID: string; readonly signal?: AbortSignal; readonly ask?: Ask },
): Promise<Result> {
/**
* Polls the link while `run` waits and reports a pairing request waiting for Allow through
* `onPairing`; it is cleared when `run` settles (allowed, denied, expired or fallen back).
*/
async function watching<A>(context: Context, run: () => Promise<A>): Promise<A> {
const onPairing = context.onPairing
const observe = onPairing ? (context.observe ?? (await observer())) : undefined
if (!observe || !onPairing) return run()
let shown: string | undefined
let done = false
const tick = async () => {
const state = await observe().catch(() => undefined)
if (done) return
const code = state?.state === "pairing" && state.code ? state.code : undefined
if (code === shown) return
shown = code
onPairing(code ? { code, name: state?.name ?? "Chrome" } : undefined)
}
const timer = setInterval(() => void tick(), 1_000)
timer.unref?.()
void tick()
try {
return await run()
} finally {
done = true
clearInterval(timer)
if (shown) onPairing(undefined)
}
}

async function attempt(input: Input, context: Context): Promise<Result> {
const state = session(context.sessionID)
if (input.action !== "open" && state.used === "chromium") return { kind: "fallback", reason: "" }
const ask = context.ask ?? (await asker())
Expand All @@ -166,24 +210,36 @@ async function attempt(

// The same Code session goes on in the same conversation and tab, also after an engine restart.
const chat = chatId(context.sessionID)
if (!state.task) {
const found = await ask({ method: "GET", path: `/browser/task?chatId=${chat}` }, context.signal).catch(() => undefined)
const id = (found?.body as { id?: unknown } | undefined)?.id
if (found?.status === 200 && typeof id === "string") state.task = id
const fallback = (response: Channels.DeskBrowserResponse): Result => {
const connect = response.browser.state === "none" && !state.hinted
if (connect) state.hinted = true
return {
kind: "fallback",
reason: response.error ?? `the extension is not ready (${response.browser.state})`,
...(connect ? { connect } : {}),
}
}
const created = await ask(
{
method: "POST",
path: "/browser/task",
body: { task: text, origin: "code", chatId: chat, ...(state.task ? { followUp: state.task } : {}) },
},
context.signal,
)
// The first request may wait for Allow in Chrome; the person sees the code while it does.
const created = await watching(context, async () => {
if (!state.task) {
const found = await ask({ method: "GET", path: `/browser/task?chatId=${chat}` }, context.signal).catch(() => undefined)
// Not reaching the extension: no second wait for the same answer.
if (found?.status === 0) return found
const id = (found?.body as { id?: unknown } | undefined)?.id
if (found?.status === 200 && typeof id === "string") state.task = id
}
return ask(
{
method: "POST",
path: "/browser/task",
body: { task: text, origin: "code", chatId: chat, ...(state.task ? { followUp: state.task } : {}) },
},
context.signal,
)
})
const id = (created.body as { id?: unknown } | undefined)?.id
if (created.status !== 200 || typeof id !== "string") {
if (created.status === 0 || created.status === 503) {
return { kind: "fallback", reason: created.error ?? `the extension is not ready (${created.browser.state})` }
}
if (created.status === 0 || created.status === 503) return fallback(created)
throw new Error(`The Redrob extension refused the task (HTTP ${created.status}).`)
}
state.used = "extension"
Expand Down Expand Up @@ -253,17 +309,40 @@ export async function asker(env: NodeJS.ProcessEnv = process.env): Promise<Ask |
}
}

/** The daemon link's state: in this process when it is the daemon, else from its `GET /desk/status`. */
export async function observer(
env: NodeJS.ProcessEnv = process.env,
): Promise<(() => Promise<Channels.DeskBrowserState | undefined>) | undefined> {
const local = Channels.current()?.desk
if (local) return async () => (await local.status()).browser
const record = await Registry.read(env).catch(() => undefined)
if (!record || !Registry.alive(record.pid)) return undefined
return async () => {
const response = await fetch(`${record.url}/desk/status`, {
headers: Registry.headers(record.secret),
signal: AbortSignal.timeout(5_000),
})
if (!response.ok) return undefined
return ((await response.json()) as Channels.DeskStatus).browser
}
}

/** Whether the person's browser may be reachable at all, so the tool is offered without a Chromium. */
export async function reachable(env: NodeJS.ProcessEnv = process.env): Promise<boolean> {
if (Channels.current()?.desk?.browser) return true
const record = await Registry.read(env).catch(() => undefined)
return !!record && Registry.alive(record.pid)
}

/** Said once per session when no Chrome with the extension is connected, for the model to pass on. */
export const CONNECT =
"Tell the person once, in their language: to let Redrob Code use their own Chrome with their sign-ins, they install the Redrob extension in Chrome and connect it with the same Redrob account (extension Settings → Redrob chat → Connect). The next browser step then asks them to press Allow in Chrome."

/** The first line of every result: which browser the action ran in, and why not the person's own. */
export function line(result: Result): string {
if (result.kind === "extension") return `Browser: the person's own Chrome (${result.browser}), through the Redrob extension.`
return `Browser: Redrob Code's own Chromium on this machine (headless).${result.reason ? ` The person's Chrome is not used: ${result.reason}.` : ""}`
const base = `Browser: Redrob Code's own Chromium on this machine (headless).${result.reason ? ` The person's Chrome is not used: ${result.reason}.` : ""}`
return result.connect ? `${base}\n${CONNECT}` : base
}

/** Forget a session's state (tests). */
Expand Down
29 changes: 26 additions & 3 deletions packages/redrob/src/desk/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const decoder = new TextDecoder()
export const ANNOUNCE_WAIT_MS = 4_000
/** How long a request waits for the person to press Allow in Chrome the first time. */
export const PAIR_WAIT_MS = 2 * 60_000
/** How long the extension keeps a pairing request (its `PENDING_MS`). */
export const PAIR_TTL_MS = 10 * 60_000
/** A later call while the same request still waits: long enough to show the code and catch an Allow. */
export const REPEAT_WAIT_MS = 20_000
const HANDSHAKE_WAIT_MS = 15_000
const REQUEST_TIMEOUT_MS = 30_000

Expand Down Expand Up @@ -93,6 +97,8 @@ type Browser = {
reason?: string
/** A request already waited out the Allow for the current pairing request. */
waited?: boolean
/** When the extension said the current pairing request is pending. */
askedAt?: number
/** The key this engine said hello with. */
device?: DeskCrypto.KeyPair
/** It left in the middle of a handshake: say hello again when it is back. */
Expand Down Expand Up @@ -132,6 +138,8 @@ export class ExtensionLink {
readonly name?: string
readonly socket?: (url: string) => Socket
readonly announceWaitMs?: number
readonly pairTtlMs?: number
readonly repeatWaitMs?: number
readonly log?: (message: string) => void
} = {},
) {}
Expand Down Expand Up @@ -222,9 +230,20 @@ export class ExtensionLink {
let browser = this.pick()
if (!browser?.peerId) throw new BrowserUnavailable(this.status())
if (browser.link === "denied") throw new BrowserUnavailable(this.status())
// The person is asked to Allow once per request in Chrome: a request after one that already
// waited out the Allow does not wait again (the Allow still links this engine when it comes).
const wait = browser.waited ? 0 : pairMs
// A request the extension has forgotten by now (its pairing requests last ten minutes), even
// when its `desk.denied expired` never reached us: start a fresh one with a new key and code.
const ttl = this.options.pairTtlMs ?? PAIR_TTL_MS
if (browser.link === "pending" && browser.askedAt !== undefined && Date.now() - browser.askedAt >= ttl) {
this.log(`the request to ${browser.name} (code ${browser.code}) expired; asking again`)
if (browser.device === this.candidate) this.candidate = undefined
browser.link = "idle"
browser.waited = false
browser.code = undefined
browser.askedAt = undefined
}
// The person is asked to Allow once per request in Chrome: a call after one that already
// waited the full time waits only briefly (the Allow still links this engine when it comes).
const wait = browser.waited ? Math.min(pairMs, this.options.repeatWaitMs ?? REPEAT_WAIT_MS) : pairMs
const deadline = Date.now() + wait
if (browser.link === "idle") await this.hello(browser)
await this.until(
Expand Down Expand Up @@ -510,6 +529,8 @@ export class ExtensionLink {
return this.hello(browser)
}
browser.link = "pending"
// A hello again with the same key keeps the extension's first deadline, so keep ours too.
browser.askedAt ??= Date.now()
this.log(`waiting for Allow in ${browser.name}: "${this.ownName}" asks, code ${browser.code}`)
this.notify()
}
Expand Down Expand Up @@ -541,6 +562,7 @@ export class ExtensionLink {
browser.link = "ready"
browser.code = undefined
browser.waited = false
browser.askedAt = undefined
this.log(`${browser.name} allowed "${this.ownName}"; linked`)
// Keep the key only now that the extension knows it: the next start says hello without a code.
if (this.candidate === device) {
Expand All @@ -566,6 +588,7 @@ export class ExtensionLink {
browser.link = reason === "expired" ? "idle" : "denied"
browser.reason = reason
browser.waited = false
browser.askedAt = undefined
browser.code = undefined
this.log(`${browser.name} answered ${reason}`)
// That pairing request is over: the next one gets a fresh key and code.
Expand Down
30 changes: 26 additions & 4 deletions packages/redrob/src/tool/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ export const Parameters = Schema.Struct({

type Params = Schema.Schema.Type<typeof Parameters>

type Metadata = { action: Params["action"]; url?: string; handedBack?: string; browser?: "extension" | "chromium" }
type Metadata = {
action: Params["action"]
url?: string
handedBack?: string
browser?: "chrome" | "own"
/** While the call waits for Allow in the person's Chrome. */
pairing?: { code: string; name: string }
/** No Chrome with the extension was found; the TUI says how to connect (once per session). */
connect?: boolean
}

const need = <A>(value: A | undefined, message: string): A => {
if (value === undefined || value === "") throw new Error(message)
Expand Down Expand Up @@ -68,19 +77,32 @@ export const BrowserTool = Tool.define<typeof Parameters, Metadata, never>(
if (params.action === "open" && !/^https?:\/\//i.test(params.url ?? ""))
throw new Error("open takes an http:// or https:// URL.")
// The person's own Chrome through the Redrob extension when it is linked; else our Chromium.
// While the first call waits for Allow in Chrome, the row shows the code (metadata.pairing).
const user = yield* Effect.promise(() =>
UserBrowser.act(params, { sessionID: ctx.sessionID, signal: ctx.abort }),
UserBrowser.act(params, {
sessionID: ctx.sessionID,
signal: ctx.abort,
onPairing: (pairing) =>
void Effect.runPromise(
ctx.metadata({ metadata: { action: params.action, ...(pairing ? { pairing } : {}) } }),
).catch(() => undefined),
}),
)
if (user.kind === "extension")
return {
title: `${params.action} ${user.url} (your Chrome)`,
output: `${UserBrowser.line(user)}\n${user.text}`,
metadata: { action: params.action, url: user.url, browser: "extension" as const },
metadata: { action: params.action, url: user.url, browser: "chrome" as const },
}
const browserLine = UserBrowser.line(user)
const handedBack = yield* Effect.promise(() => RunnerBrowser.turn(ctx.abort))
const lead = `${browserLine}\n${handedBack ? `${handedBack}\n\n` : ""}`
const meta = { action: params.action, browser: "chromium" as const, ...(handedBack ? { handedBack } : {}) }
const meta = {
action: params.action,
browser: "own" as const,
...(handedBack ? { handedBack } : {}),
...(user.connect ? { connect: true } : {}),
}

if (params.action === "screenshot") {
const shot = yield* Effect.promise(() => RunnerBrowser.screenshot())
Expand Down
59 changes: 58 additions & 1 deletion packages/redrob/test/browser/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe("UserBrowser.act", () => {

test("not reaching the extension falls back with the reason, and the session stays on Chromium", async () => {
const { ask, calls } = fake({
create: { status: 0, browser: { state: "none" }, error: "no Chrome with the Redrob extension is connected" },
create: { status: 0, browser: { state: "found", name: "Chrome" }, error: "no Chrome with the Redrob extension is connected" },
})
const result = await UserBrowser.act({ action: "open", url: "https://example.com" }, { sessionID: "ses_2", ask })
expect(result).toEqual({ kind: "fallback", reason: "no Chrome with the Redrob extension is connected" })
Expand Down Expand Up @@ -138,3 +138,60 @@ test("chat ids keep the extension's id shape", () => {
expect(UserBrowser.chatId("ses_abc.1")).toBe("code-ses_abc_1")
expect(/^[A-Za-z0-9_-]{1,128}$/.test(UserBrowser.chatId("x".repeat(300)))).toBe(true)
})

describe("the person's side while a call waits", () => {
test("shows the pairing code while the request waits for Allow, and clears it once it is over", async () => {
const seen: Array<string | undefined> = []
let state: { state: "pairing" | "ready"; name: string; code?: string } = {
state: "pairing",
name: "Chrome on Windows",
code: "197159",
}
const { ask: base } = fake()
// The first request blocks like the daemon's while it waits for Allow, then Allow comes.
const ask = async (request: DeskBrowserRequest) => {
if (request.path.startsWith("/browser/task?chatId=")) {
await Bun.sleep(1_300)
state = { state: "ready", name: "Chrome on Windows" }
}
return base(request)
}
const result = await UserBrowser.act(
{ action: "open", url: "https://example.com" },
{ sessionID: "ses_p1", ask, observe: async () => state, onPairing: (p) => seen.push(p?.code) },
)
expect(result.kind).toBe("extension")
expect(seen).toEqual(["197159", undefined])
})

test("clears the notice when the call falls back (expired, denied or not reached)", async () => {
const seen: Array<string | undefined> = []
const ask = async (): Promise<DeskBrowserResponse> => {
await Bun.sleep(200)
return { status: 0, browser: { state: "pairing", name: "Chrome", code: "111111" }, error: "waiting" }
}
const result = await UserBrowser.act(
{ action: "open", url: "https://example.com" },
{
sessionID: "ses_p2",
ask,
observe: async () => ({ state: "pairing", name: "Chrome", code: "111111" }),
onPairing: (p) => seen.push(p?.code),
},
)
expect(result.kind).toBe("fallback")
expect(seen).toEqual(["111111", undefined])
})

test("says how to connect once per session when no Chrome with the extension is found", async () => {
const none = { status: 0, browser: { state: "none" as const }, error: "no Chrome with the Redrob extension is connected" }
const ask = async (): Promise<DeskBrowserResponse> => none
const first = await UserBrowser.act({ action: "open", url: "https://example.com" }, { sessionID: "ses_c", ask })
expect(first).toMatchObject({ kind: "fallback", connect: true })
expect(UserBrowser.line(first)).toContain("install the Redrob extension in Chrome")
const again = await UserBrowser.act({ action: "open", url: "https://example.org" }, { sessionID: "ses_c", ask })
expect(again.kind).toBe("fallback")
expect("connect" in again && again.connect).toBeFalsy()
expect(UserBrowser.line(again)).not.toContain("install the Redrob extension")
})
})
Loading
Loading