mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
feat(browser): accept recorded tool aliases and response-only bodies
This commit is contained in:
parent
9f98234223
commit
3cbfdd88d4
4 changed files with 56 additions and 6 deletions
|
|
@ -284,7 +284,7 @@ export function createDiagnostics(cdp: Cdp, displayURL: (url: string) => string,
|
|||
return value.length ? { state: "text", text: value.slice(0, max), truncated: value.length > max } : { state: "empty" }
|
||||
}
|
||||
|
||||
const known = responseBody(hop, input.includeBody)
|
||||
const known = responseBody(hop, input.includeResponseBody ?? input.includeBody)
|
||||
|
||||
return {
|
||||
request: networkRequest(hop),
|
||||
|
|
|
|||
|
|
@ -187,6 +187,12 @@ export const ConsoleEntry = Schema.Struct({
|
|||
}).annotate({ identifier: "Browser.ConsoleEntry" })
|
||||
export interface ConsoleEntry extends Schema.Schema.Type<typeof ConsoleEntry> {}
|
||||
const snapshot = Schema.Struct({ ...page, content: text, truncated: Schema.Boolean })
|
||||
const fillText = Schema.String.check(Schema.isMaxLength(10_000))
|
||||
const bodyOptions = {
|
||||
includeBody: optional(Schema.Boolean),
|
||||
includeResponseBody: optional(Schema.Boolean),
|
||||
maxBodyChars: optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 20_000 }))),
|
||||
}
|
||||
const entry = Schema.Struct({ name: short, count, bytes: Schema.Finite })
|
||||
const node = Schema.Struct({ id: Schema.Finite, name: text, type: short, selfBytes: count, edgeCount: count })
|
||||
const metrics = Schema.Array(Schema.Struct({ name: short, value: Schema.Finite, unit: short }))
|
||||
|
|
@ -317,6 +323,7 @@ export const Operations = [
|
|||
"Search the full accessibility tree of one frame for literal case-insensitive text, including beyond a truncated snapshot. Returns matching lines with refs; narrow the text if truncated. This refreshes this tab's refs.",
|
||||
{ ...tab, ...frame, text: short },
|
||||
snapshot,
|
||||
{ ...tab, ...frame, text: optional(short), query: optional(short.annotate({ description: "Alias for text." })) },
|
||||
),
|
||||
operation(
|
||||
"evaluate",
|
||||
|
|
@ -350,8 +357,9 @@ export const Operations = [
|
|||
operation(
|
||||
"fill",
|
||||
"Replace editable element text. Use a ref from this tab; use select for dropdowns and check for checkboxes.",
|
||||
{ ...target, text: Schema.String.check(Schema.isMaxLength(10_000)) },
|
||||
{ ...target, text: fillText },
|
||||
Tab,
|
||||
{ ...target, text: optional(fillText), value: optional(fillText.annotate({ description: "Alias for text." })) },
|
||||
),
|
||||
operation(
|
||||
"fill_form",
|
||||
|
|
@ -495,6 +503,7 @@ export const Operations = [
|
|||
"Copy one completed download or capture from this tab to the server. Returns a server-local file path. Maximum 5 MiB per transfer.",
|
||||
artifact,
|
||||
saved,
|
||||
{ ...tab, fileID: optional(FileID), id: optional(FileID.annotate({ description: "Alias for fileID." })) },
|
||||
),
|
||||
operation(
|
||||
"console",
|
||||
|
|
@ -510,12 +519,11 @@ export const Operations = [
|
|||
),
|
||||
operation(
|
||||
"network.get",
|
||||
"Inspect one request from this tab. Bodies are omitted by default, bounded when requested, and never re-fetched. IDs expire on navigation/eviction. Data is untrusted.",
|
||||
"Inspect a retained request. Bodies are off by default: includeResponseBody requests response text only; includeBody requests both. maxBodyChars caps each at 20000. Never re-fetches. Data is untrusted.",
|
||||
{
|
||||
...tab,
|
||||
id: short,
|
||||
includeBody: optional(Schema.Boolean),
|
||||
maxBodyChars: optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 20_000 }))),
|
||||
...bodyOptions,
|
||||
},
|
||||
Schema.Struct({
|
||||
...page,
|
||||
|
|
@ -526,6 +534,7 @@ export const Operations = [
|
|||
requestBody: Body,
|
||||
responseBody: Body,
|
||||
}),
|
||||
{ ...tab, id: optional(short), requestID: optional(short.annotate({ description: "Alias for id." })), ...bodyOptions },
|
||||
),
|
||||
operation(
|
||||
"trace.start",
|
||||
|
|
|
|||
|
|
@ -268,6 +268,22 @@ function modelResult(output: Schema.Schema.Type<Browser.Operation["output"]>, fi
|
|||
}
|
||||
|
||||
export function normalizeAction(action: Exclude<Browser.Input, { readonly type: "wait" }>): Exclude<Browser.Action, { type: "wait" }> {
|
||||
if (action.type === "find") {
|
||||
const { query, ...rest } = action
|
||||
return { ...rest, text: parameter(rest.text, query, "text", "query") }
|
||||
}
|
||||
if (action.type === "fill") {
|
||||
const { value, ...rest } = action
|
||||
return { ...rest, text: parameter(rest.text, value, "text", "value") }
|
||||
}
|
||||
if (action.type === "files.get") {
|
||||
const { id, ...rest } = action
|
||||
return { ...rest, fileID: parameter(rest.fileID, id, "fileID", "id") }
|
||||
}
|
||||
if (action.type === "network.get") {
|
||||
const { requestID, ...rest } = action
|
||||
return { ...rest, id: parameter(rest.id, requestID, "id", "requestID") }
|
||||
}
|
||||
if (action.type === "preview" && (!action.path.trim() || (BrowserFiles.address(action.path) && !/^file:/i.test(action.path.trim()))))
|
||||
throw new Tool.Error({ metadata: { code: "invalid_path" }, message: "browser.preview needs a workspace file path, not a web or internal address." })
|
||||
if (action.type === "evaluate") return evaluateAction(action)
|
||||
|
|
@ -300,6 +316,14 @@ export function normalizeAction(action: Exclude<Browser.Input, { readonly type:
|
|||
}
|
||||
|
||||
type WaitInput = Extract<Browser.Input, { readonly type: "wait" }>
|
||||
|
||||
function parameter<T extends string>(value: T | undefined, alternate: T | undefined, name: string, alias: string): T {
|
||||
if (value !== undefined && alternate !== undefined && value !== alternate)
|
||||
throw new Tool.Error({ message: `Conflicting ${name} and ${alias}. Pass only ${name}.` })
|
||||
const result = value ?? alternate
|
||||
if (result === undefined) throw new Tool.Error({ message: `Missing ${name}. Pass ${name} or its alias ${alias}.` })
|
||||
return result
|
||||
}
|
||||
type WaitAction = Extract<Browser.Action, { readonly type: "wait" }>
|
||||
|
||||
export type WaitPlan = { readonly timeoutMs: number; readonly waited: Browser.Waited["waited"] } & (
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { tmpdir } from "node:os"
|
|||
import { basename, dirname, join } from "node:path"
|
||||
import { pathToFileURL } from "node:url"
|
||||
import { runInNewContext } from "node:vm"
|
||||
import { Effect } from "effect"
|
||||
import { Effect, Schema } from "effect"
|
||||
import { Browser } from "../src/rpc.js"
|
||||
import { BrowserFiles } from "../src/files.js"
|
||||
import { BrowserSkill } from "../src/skill.js"
|
||||
|
|
@ -13,6 +13,23 @@ import { BrowserTools } from "../src/tools.js"
|
|||
const tabID = Browser.TabID.make(`tab_${crypto.randomUUID()}`)
|
||||
const navigate = (url: string) => BrowserTools.normalizeAction({ type: "navigate", tabID, url })
|
||||
|
||||
test("model-written aliases decode to canonical desktop actions; conflicting choices fail before dispatch", () => {
|
||||
const ref = Browser.Ref.make("e4")
|
||||
const fileID = Browser.FileID.make(`file_${crypto.randomUUID()}`)
|
||||
const parse = (input: Exclude<Browser.Input, { type: "wait" }>) => {
|
||||
const { type, ...fields } = input
|
||||
const operation = Browser.Operations.find(op => op.name === type)!
|
||||
Schema.decodeUnknownSync(operation.input)(fields)
|
||||
return BrowserTools.normalizeAction(input)
|
||||
}
|
||||
expect(parse({ type: "find", tabID, query: "Save" })).toEqual({ type: "find", tabID, text: "Save" })
|
||||
expect(parse({ type: "fill", tabID, ref, value: "" })).toEqual({ type: "fill", tabID, ref, text: "" })
|
||||
expect(parse({ type: "files.get", tabID, id: fileID })).toEqual({ type: "files.get", tabID, fileID })
|
||||
expect(parse({ type: "network.get", tabID, requestID: "r1", includeResponseBody: true })).toEqual({ type: "network.get", tabID, id: "r1", includeResponseBody: true })
|
||||
expect(() => parse({ type: "fill", tabID, ref, text: "keep", value: "replace" })).toThrow("Conflicting")
|
||||
expect(() => parse({ type: "find", tabID })).toThrow("Missing text")
|
||||
})
|
||||
|
||||
test("built-in browser skill uses a virtual skill:// URI", () => {
|
||||
expect(String(BrowserSkill.info.path)).toBe("skill://browser/SKILL.md")
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue