FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

A5+A7: permission gate + stream output to TUI for browser_execute by Alezander9 · Pull Request #7 · browser-use/browsercode · GitHub

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

Filter by extension

Filter by extension .ts  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
15 changes: 12 additions & 3 deletions packages/bcode-browser/src/browser-execute.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export type Parameters = z.infer<typeof parameters>

export interface ExecuteContext {
readonly sessionID: string
// Optional progress callback invoked per output chunk (combined stdout+stderr).
// Level-2 supplies this to drive TUI streaming via opencode's `ctx.metadata`.
// The callback receives the fully accumulated output so far, not just the
// delta — simpler for consumers that just want to set "current output".
readonly onChunk?: (output: string) => Effect.Effect<void>
}

export interface ExecuteResult {
Expand Down Expand Up @@ -78,10 +83,14 @@ export const make = Effect.fn("BrowserExecute.make")(function* () {
)
if (spawned === "uv-missing") return { output: UV_MISSING_HINT, exitCode: 127 } satisfies ExecuteResult

const [output, exitCode] = yield* Effect.all(
[Stream.mkString(Stream.decodeText(spawned.all)), spawned.exitCode],
{ concurrency: 2 },
let output = ""
const drain = Stream.runForEach(Stream.decodeText(spawned.all), (chunk) =>
Effect.gen(function* () {
output += chunk
if (ctx.onChunk) yield* ctx.onChunk(output)
}),
)
const [, exitCode] = yield* Effect.all([drain, spawned.exitCode], { concurrency: 2 })

return { output, exitCode } satisfies ExecuteResult
}).pipe(
Expand Down
27 changes: 24 additions & 3 deletions packages/opencode/src/tool/browser-execute.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// browser_execute — Level-2 hook (decisions.md §1c).
//
// Adapter only. All logic lives in @browser-use/bcode-browser/browser-execute.
// Adapter only. Substantive logic lives in @browser-use/bcode-browser/browser-execute.

import { Effect } from "effect"
import type z from "zod"
import { BrowserExecute } from "@browser-use/bcode-browser/browser-execute"
import * as Tool from "./tool"
import DESCRIPTION from "./browser-execute.txt"

const MAX_METADATA_LENGTH = 30_000
const preview = (text: string) =>
text.length <= MAX_METADATA_LENGTH ? text : "...\n\n" + text.slice(-MAX_METADATA_LENGTH)

export const BrowserExecuteTool = Tool.define(
"browser_execute",
Effect.gen(function* () {
Expand All @@ -17,11 +21,28 @@ export const BrowserExecuteTool = Tool.define(
parameters: impl.parameters,
execute: (args: z.infer<typeof impl.parameters>, ctx: Tool.Context) =>
Effect.gen(function* () {
const result = yield* impl.execute(args, { sessionID: ctx.sessionID })
// Permission gate. Default agent ruleset has `"*": "allow"` so this
// auto-allows; users can opt out via opencode.json — either
// `"tools": { "browser_execute": false }` or a per-permission rule.
yield* ctx.ask({
permission: "browser_execute",
patterns: ["*"],
always: ["*"],
metadata: {},
})

const result = yield* impl.execute(args, {
sessionID: ctx.sessionID,
// Stream chunks to the TUI as they arrive — same pattern as bash.
onChunk: (output) =>
ctx.metadata({
metadata: { output: preview(output) },
}),
})
return {
title: "browser_execute",
output: result.output,
metadata: { exitCode: result.exitCode },
metadata: { exitCode: result.exitCode, output: preview(result.output) },
}
}).pipe(Effect.orDie),
}
Expand Down

Back | FazBrowse Home | New Git URL