Doubao Work runs its agent loop in ByteDance's cloud and has no API. This
plugin uses the desktop app the way a person does and gives the reply back
as an OpenAI chat completion:
- cdp.mjs (knows nothing about Doubao): starts a Chromium-based app from a
profile of its own with DevTools on a port it picked and saved, reuses it
while it runs, never kills anything; Page evaluates and presses keys.
- doubao.mjs: per request a new 工作任务, switched to 云电脑 (Doubao's agent
never touches this computer), the model picked (Pro needs a paid plan:
403), the whole request written into tiptap one paragraph a line, Enter,
and the reply read from the message's content_blocks_v2 (markdown, and
thinking as reasoning_content) until is_finish. One request at a time.
- prompt.mjs / openai.mjs: system prompt, tools and turns flattened into one
message; tools asked for as <tool_call>{json}</tool_call> and given back
as tool_calls; SSE that holds text from the first call (or a tail that
may start one), and that throws a failure before the reply begins so it
goes back as an HTTP error.
- Sign-in waits for the account in the separate Doubao window.
Run:
- bun test packages/doubao-work on mote: 24 pass.
- Full suite on mote the way CI runs it (HOME=$(mktemp -d) bun test):
6936 pass, 20 skip, 0 fail. bun scripts/check.mjs: doubao-work ok.
- Broken by hand: sleep without its aborted check; "an abort between two
polls stops the ask too" goes red (runs to the 180s idle timeout), green
with it. Found by the first run: an abort landing between polls was lost.
- Live, the loader against a signed-in Doubao 147 on macOS: auto plain
answer 200 in 3.9s; doubao-2.1-lite streamed Bash tool call (first byte
3.6s, total 4.8s); tool result follow-up answered "412 lines"; Pro gave
403 doubao_upgrade; a 300-word story streamed as 20 content deltas with
its ```python fence kept; a client abort ended the stream and the next
request answered 200.
Not tried: through magpie's plugin host and a real agent (Claude Code
sized requests); Windows and Linux (unsupported, sign-in says so). A
stopped request stops being read but Doubao finishes its task (README).
179 lines
6.8 KiB
JavaScript
179 lines
6.8 KiB
JavaScript
// A small Chrome DevTools Protocol driver for desktop apps built on
|
|
// Chromium. It knows nothing about any one app: an adapter (doubao.mjs)
|
|
// says which binary to start, which page to drive and what to do there.
|
|
//
|
|
// The app runs from a profile of its own (a user-data-dir under the
|
|
// plugin's folder), so it never touches the user's everyday copy of the
|
|
// app, and Chromium allows remote debugging only for such a profile. The
|
|
// debugging port is one this driver picked and saved beside the profile,
|
|
// so a later request finds the same running app instead of starting
|
|
// another.
|
|
import { spawn as nodeSpawn } from "node:child_process"
|
|
import fs from "node:fs"
|
|
import net from "node:net"
|
|
import path from "node:path"
|
|
|
|
// sleep waits ms, or rejects at once when signal is (or becomes) aborted.
|
|
const sleep = (ms, signal) =>
|
|
new Promise((resolve, reject) => {
|
|
if (signal?.aborted) return reject(signal.reason ?? Error("aborted"))
|
|
const stop = () => { clearTimeout(t); reject(signal.reason ?? Error("aborted")) }
|
|
const t = setTimeout(() => { signal?.removeEventListener("abort", stop); resolve() }, ms)
|
|
signal?.addEventListener("abort", stop, { once: true })
|
|
})
|
|
|
|
// freePort asks the system for a port no one holds, then lets it go. The
|
|
// app is started on it a moment later.
|
|
export function freePort() {
|
|
return new Promise((resolve, reject) => {
|
|
const s = net.createServer()
|
|
s.unref()
|
|
s.on("error", reject)
|
|
s.listen(0, "127.0.0.1", () => { const { port } = s.address(); s.close(() => resolve(port)) })
|
|
})
|
|
}
|
|
|
|
async function getJSON(url, ms = 1500, fetchImpl = fetch) {
|
|
const r = await fetchImpl(url, { signal: AbortSignal.timeout(ms) })
|
|
if (!r.ok) throw Error(`${url}: HTTP ${r.status}`)
|
|
return r.json()
|
|
}
|
|
|
|
// targets lists the pages of the app on port, or null when nothing (or
|
|
// something that isn't a DevTools endpoint) answers there.
|
|
export async function targets(port, fetchImpl = fetch) {
|
|
try {
|
|
const list = await getJSON(`http://127.0.0.1:${port}/json/list`, 1500, fetchImpl)
|
|
return Array.isArray(list) ? list : null
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
// App starts and finds one Chromium-based desktop app run from its own
|
|
// profile.
|
|
// binary the executable
|
|
// dataDir the plugin's folder for this app: profile/ and port live here
|
|
// owns (targets) => true when the pages on a port are this app's,
|
|
// so a port another program took since is never driven
|
|
// args more command-line switches
|
|
export class App {
|
|
constructor({ binary, dataDir, owns, args = [], spawn = nodeSpawn, fetchImpl = fetch }) {
|
|
Object.assign(this, { binary, dataDir, owns, args, spawn, fetchImpl })
|
|
this.profile = path.join(dataDir, "profile")
|
|
this.portFile = path.join(dataDir, "port")
|
|
}
|
|
|
|
savedPort() {
|
|
try {
|
|
const n = Number(fs.readFileSync(this.portFile, "utf8").trim())
|
|
return Number.isInteger(n) && n > 0 && n < 65536 ? n : 0
|
|
} catch {
|
|
return 0
|
|
}
|
|
}
|
|
|
|
// running gives the port of this app when it is up, else 0.
|
|
async running() {
|
|
const port = this.savedPort()
|
|
if (!port) return 0
|
|
const list = await targets(port, this.fetchImpl)
|
|
return list && this.owns(list) ? port : 0
|
|
}
|
|
|
|
// start runs the app when it isn't running yet and waits until its pages
|
|
// answer. It never stops or kills anything: a port someone else holds is
|
|
// left to them and the app is started on another.
|
|
async start({ timeout = 45000, signal } = {}) {
|
|
const up = await this.running()
|
|
if (up) return up
|
|
if (!fs.existsSync(this.binary)) throw Object.assign(Error(`${this.binary} is not installed`), { code: "app_missing" })
|
|
fs.mkdirSync(this.profile, { recursive: true })
|
|
const port = await freePort()
|
|
fs.writeFileSync(this.portFile, String(port))
|
|
const child = this.spawn(this.binary, [`--remote-debugging-port=${port}`, `--user-data-dir=${this.profile}`, ...this.args], {
|
|
detached: true,
|
|
stdio: "ignore",
|
|
})
|
|
child.unref?.()
|
|
const until = Date.now() + timeout
|
|
while (Date.now() < until) {
|
|
await sleep(400, signal)
|
|
const list = await targets(port, this.fetchImpl)
|
|
if (list && this.owns(list)) return port
|
|
}
|
|
throw Object.assign(Error(`${path.basename(this.binary)} did not open its debugging port in ${Math.round(timeout / 1000)}s`), { code: "app_start_timeout" })
|
|
}
|
|
}
|
|
|
|
// Page is one CDP session on a page target.
|
|
export class Page {
|
|
static async open(port, pick, { fetchImpl = fetch, WebSocketImpl = globalThis.WebSocket } = {}) {
|
|
const list = (await targets(port, fetchImpl)) ?? []
|
|
const t = list.find((x) => x.type === "page" && pick(x))
|
|
if (!t?.webSocketDebuggerUrl) return null
|
|
const page = new Page(t, WebSocketImpl)
|
|
await page.ready
|
|
return page
|
|
}
|
|
|
|
constructor(target, WebSocketImpl) {
|
|
this.target = target
|
|
this.id = 0
|
|
this.pending = new Map()
|
|
this.closed = false
|
|
this.ws = new WebSocketImpl(target.webSocketDebuggerUrl)
|
|
this.ready = new Promise((resolve, reject) => {
|
|
this.ws.onopen = () => resolve()
|
|
this.ws.onerror = (e) => reject(Error("DevTools connection failed: " + (e?.message ?? "error")))
|
|
})
|
|
this.ws.onmessage = (m) => {
|
|
let d
|
|
try { d = JSON.parse(typeof m.data === "string" ? m.data : String(m.data)) } catch { return }
|
|
const p = this.pending.get(d.id)
|
|
if (!p) return
|
|
this.pending.delete(d.id)
|
|
if (d.error) p.reject(Error(`${p.method}: ${d.error.message}`))
|
|
else p.resolve(d.result)
|
|
}
|
|
this.ws.onclose = () => {
|
|
this.closed = true
|
|
for (const p of this.pending.values()) p.reject(Error("DevTools connection closed"))
|
|
this.pending.clear()
|
|
}
|
|
}
|
|
|
|
call(method, params = {}) {
|
|
if (this.closed) return Promise.reject(Error("DevTools connection closed"))
|
|
return new Promise((resolve, reject) => {
|
|
const id = ++this.id
|
|
this.pending.set(id, { resolve, reject, method })
|
|
this.ws.send(JSON.stringify({ id, method, params }))
|
|
})
|
|
}
|
|
|
|
// eval runs an expression in the page and gives back its value (awaited
|
|
// when it is a promise). A page-side exception becomes an Error here.
|
|
async eval(expression) {
|
|
const r = await this.call("Runtime.evaluate", { expression, returnByValue: true, awaitPromise: true })
|
|
if (r?.exceptionDetails) {
|
|
const d = r.exceptionDetails
|
|
throw Error("page: " + (d.exception?.description ?? d.text ?? "exception").split("\n")[0])
|
|
}
|
|
return r?.result?.value
|
|
}
|
|
|
|
// key presses one key the way a user's keyboard does.
|
|
async key(key, { code = key, keyCode = 0, text, modifiers = 0 } = {}) {
|
|
await this.call("Input.dispatchKeyEvent", { type: "keyDown", key, code, windowsVirtualKeyCode: keyCode, text, modifiers })
|
|
await this.call("Input.dispatchKeyEvent", { type: "keyUp", key, code, windowsVirtualKeyCode: keyCode, modifiers })
|
|
}
|
|
|
|
close() {
|
|
this.closed = true
|
|
try { this.ws.close() } catch {}
|
|
}
|
|
}
|
|
|
|
export { sleep }
|