@1outres/dsh-bot-connector-browser 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +53 -2
- package/cordis.patch.yml +6 -0
- package/icon.svg +9 -0
- package/lib/index.js +1762 -0
- package/locale/en.json +6 -0
- package/locale/ja.json +6 -0
- package/package.json +60 -3
- package/scripts/generate-tools.mjs +89 -0
- package/src/generated/playwright-mcp.json +1033 -0
- package/src/host/catalog.ts +104 -0
- package/src/host/connections.ts +21 -0
- package/src/host/connector.ts +48 -0
- package/src/host/mcp/client.ts +190 -0
- package/src/host/mcp/protocol.ts +104 -0
- package/src/host/mcp/server.ts +101 -0
- package/src/host/preparation.ts +120 -0
- package/src/host/result.ts +67 -0
- package/src/host/schema.ts +111 -0
- package/src/host/session.ts +41 -0
- package/src/host/tools.ts +89 -0
- package/src/host/vm/commands.ts +67 -0
- package/src/host/vm/layout.ts +22 -0
- package/src/index.ts +52 -0
- package/src/shared/connector.ts +4 -0
- package/vm/install-mcp.sh +17 -0
- package/vm/mcp/npm-shrinkwrap.json +56 -0
- package/vm/mcp/package.json +8 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Playwright MCP result as the connector returns it: its text blocks as
|
|
3
|
+
* the text, its images checked and admitted for the calling model.
|
|
4
|
+
*/
|
|
5
|
+
import type {
|
|
6
|
+
BotConnectorImage,
|
|
7
|
+
BotConnectorImageInput,
|
|
8
|
+
BotConnectorResult,
|
|
9
|
+
BotConnectorRun,
|
|
10
|
+
} from "@1outres/dsh-bot-core"
|
|
11
|
+
import type { McpCallResult } from "./mcp/protocol"
|
|
12
|
+
|
|
13
|
+
const IMAGE_TYPES = ["image/png", "image/jpeg", "image/webp"] as const
|
|
14
|
+
type ImageType = (typeof IMAGE_TYPES)[number]
|
|
15
|
+
const MAX_IMAGE_BYTES = 16 << 20
|
|
16
|
+
const BASE64 = /^[A-Za-z0-9+/]*={0,2}$/
|
|
17
|
+
|
|
18
|
+
const isImageType = (value: string): value is ImageType =>
|
|
19
|
+
(IMAGE_TYPES as readonly string[]).includes(value)
|
|
20
|
+
|
|
21
|
+
/** An image block's bytes. @throws for another type, bad base64 or one too large. */
|
|
22
|
+
export function imageOf(block: {
|
|
23
|
+
readonly data: string
|
|
24
|
+
readonly mimeType: string
|
|
25
|
+
}): BotConnectorImageInput {
|
|
26
|
+
if (!isImageType(block.mimeType)) {
|
|
27
|
+
throw new Error(`Playwright MCP returned an image of type ${block.mimeType.slice(0, 60)}`)
|
|
28
|
+
}
|
|
29
|
+
if (block.data.length % 4 !== 0 || !BASE64.test(block.data)) {
|
|
30
|
+
throw new Error("Playwright MCP returned an image that is not base64")
|
|
31
|
+
}
|
|
32
|
+
if ((block.data.length / 4) * 3 > MAX_IMAGE_BYTES) {
|
|
33
|
+
throw new Error(`Playwright MCP returned an image larger than ${MAX_IMAGE_BYTES} bytes`)
|
|
34
|
+
}
|
|
35
|
+
return { mediaType: block.mimeType, data: new Uint8Array(Buffer.from(block.data, "base64")) }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @throws with the server's text when it reports an error, and when an image
|
|
40
|
+
* cannot be shown to the calling model (a run of an approved action has no
|
|
41
|
+
* caller: its images are left out and said to be).
|
|
42
|
+
*/
|
|
43
|
+
export async function resultOf(
|
|
44
|
+
result: McpCallResult,
|
|
45
|
+
run: Pick<BotConnectorRun<unknown>, "caller">,
|
|
46
|
+
): Promise<BotConnectorResult> {
|
|
47
|
+
const texts: string[] = []
|
|
48
|
+
const images: BotConnectorImage[] = []
|
|
49
|
+
for (const block of result.content) {
|
|
50
|
+
if (block.type === "text") {
|
|
51
|
+
texts.push(block.text)
|
|
52
|
+
continue
|
|
53
|
+
}
|
|
54
|
+
const image = imageOf(block)
|
|
55
|
+
if (result.isError) continue
|
|
56
|
+
if (run.caller === undefined) {
|
|
57
|
+
texts.push("[An image was left out: an approved action's result carries text only.]")
|
|
58
|
+
continue
|
|
59
|
+
}
|
|
60
|
+
const admitted = await run.caller.images.admit(image)
|
|
61
|
+
if (!admitted.ok) throw new Error(`cannot show the image: ${admitted.reason}`)
|
|
62
|
+
images.push(admitted.image)
|
|
63
|
+
}
|
|
64
|
+
const text = texts.join("\n\n")
|
|
65
|
+
if (result.isError) throw new Error(text === "" ? "Playwright MCP reported an error" : text)
|
|
66
|
+
return { ...(text === "" ? {} : { text }), ...(images.length === 0 ? {} : { images }) }
|
|
67
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An upstream tool's JSON Schema (Playwright MCP's, draft 2020-12 from zod)
|
|
3
|
+
* in the subset DSH's tools take (type, oneOf, properties, required, boolean
|
|
4
|
+
* additionalProperties, items, enum, const, and annotations). The
|
|
5
|
+
* conversion only drops what the subset cannot say, never adds a
|
|
6
|
+
* constraint; the server checks every call against its own schema anyway.
|
|
7
|
+
* What is dropped is said in the description. An unknown keyword throws, so
|
|
8
|
+
* that a new upstream version is looked at before it is offered.
|
|
9
|
+
*/
|
|
10
|
+
export type SchemaNode = { readonly [keyword: string]: unknown }
|
|
11
|
+
|
|
12
|
+
const ANNOTATIONS = new Set(["description", "title", "default", "examples"])
|
|
13
|
+
const KEPT = new Set(["type", "properties", "required", "enum", "const", "items"])
|
|
14
|
+
|
|
15
|
+
const isNode = (value: unknown): value is SchemaNode =>
|
|
16
|
+
typeof value === "object" && value !== null && !Array.isArray(value)
|
|
17
|
+
|
|
18
|
+
const described = (node: SchemaNode, notes: readonly string[]) => {
|
|
19
|
+
if (notes.length === 0) return node
|
|
20
|
+
const note = notes.join("; ")
|
|
21
|
+
return {
|
|
22
|
+
...node,
|
|
23
|
+
description:
|
|
24
|
+
typeof node.description === "string" && node.description !== ""
|
|
25
|
+
? `${node.description} (${note})`
|
|
26
|
+
: `(${note})`,
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function convert(node: unknown, path: string): SchemaNode {
|
|
31
|
+
if (!isNode(node)) throw new Error(`${path} is not a schema object`)
|
|
32
|
+
const out: Record<string, unknown> = {}
|
|
33
|
+
const notes: string[] = []
|
|
34
|
+
for (const [key, value] of Object.entries(node)) {
|
|
35
|
+
if (
|
|
36
|
+
ANNOTATIONS.has(key) ||
|
|
37
|
+
key === "type" ||
|
|
38
|
+
key === "required" ||
|
|
39
|
+
key === "enum" ||
|
|
40
|
+
key === "const"
|
|
41
|
+
) {
|
|
42
|
+
out[key] = value
|
|
43
|
+
} else if (key === "$schema") {
|
|
44
|
+
} else if (key === "properties") {
|
|
45
|
+
if (!isNode(value)) throw new Error(`${path}.properties is not an object`)
|
|
46
|
+
out.properties = Object.fromEntries(
|
|
47
|
+
Object.entries(value).map(([name, child]) => [
|
|
48
|
+
name,
|
|
49
|
+
convert(child, `${path}.properties.${name}`),
|
|
50
|
+
]),
|
|
51
|
+
)
|
|
52
|
+
} else if (key === "items") {
|
|
53
|
+
out.items = convert(value, `${path}.items`)
|
|
54
|
+
} else if (key === "anyOf") {
|
|
55
|
+
out.oneOf = exclusiveBranches(value, path)
|
|
56
|
+
} else if (key === "additionalProperties") {
|
|
57
|
+
if (typeof value === "boolean") out.additionalProperties = value
|
|
58
|
+
else notes.push(`every value: ${valueType(value, path)}`)
|
|
59
|
+
} else if (key === "propertyNames") {
|
|
60
|
+
if (!isNode(value) || value.type !== "string" || Object.keys(value).length !== 1) {
|
|
61
|
+
throw new Error(`${path}.propertyNames is more than "any string"`)
|
|
62
|
+
}
|
|
63
|
+
} else if (key === "minimum") {
|
|
64
|
+
notes.push(`at least ${String(value)}`)
|
|
65
|
+
} else if (key === "maximum") {
|
|
66
|
+
if (value !== Number.MAX_SAFE_INTEGER) notes.push(`at most ${String(value)}`)
|
|
67
|
+
} else {
|
|
68
|
+
throw new Error(`${path}.${key} is a keyword the conversion does not know`)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
for (const key of Object.keys(out)) {
|
|
72
|
+
if (
|
|
73
|
+
!KEPT.has(key) &&
|
|
74
|
+
!ANNOTATIONS.has(key) &&
|
|
75
|
+
key !== "oneOf" &&
|
|
76
|
+
key !== "additionalProperties"
|
|
77
|
+
) {
|
|
78
|
+
throw new Error(`${path}.${key} was kept by mistake`)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return described(out, notes)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** anyOf as oneOf: only when its branches have different types, so exactly one can match. */
|
|
85
|
+
function exclusiveBranches(value: unknown, path: string): readonly SchemaNode[] {
|
|
86
|
+
if (!Array.isArray(value) || value.length < 2) {
|
|
87
|
+
throw new Error(`${path}.anyOf is not a list of at least two schemas`)
|
|
88
|
+
}
|
|
89
|
+
const branches = value.map((branch, index) => convert(branch, `${path}.anyOf[${index}]`))
|
|
90
|
+
const types = branches.map((branch) => branch.type)
|
|
91
|
+
if (types.some((type) => typeof type !== "string") || new Set(types).size !== types.length) {
|
|
92
|
+
throw new Error(`${path}.anyOf has branches that may overlap`)
|
|
93
|
+
}
|
|
94
|
+
return branches
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function valueType(value: unknown, path: string): string {
|
|
98
|
+
if (!isNode(value) || typeof value.type !== "string" || Object.keys(value).length !== 1) {
|
|
99
|
+
throw new Error(`${path}.additionalProperties is more than a type`)
|
|
100
|
+
}
|
|
101
|
+
return value.type
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function toolSchema(
|
|
105
|
+
inputSchema: SchemaNode,
|
|
106
|
+
tool: string,
|
|
107
|
+
): SchemaNode & { readonly type: "object" } {
|
|
108
|
+
const converted = convert(inputSchema, tool)
|
|
109
|
+
if (converted.type !== "object") throw new Error(`${tool}'s arguments are not an object`)
|
|
110
|
+
return converted as SchemaNode & { readonly type: "object" }
|
|
111
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The browser connector's resident resource per bot: Playwright MCP started
|
|
3
|
+
* in the bot's environment, attached to the desktop's Chrome. Chrome itself
|
|
4
|
+
* is the desktop's user service, so its tabs and cookies outlive the server.
|
|
5
|
+
*/
|
|
6
|
+
import type { BotConnectorAttachmentSource } from "@1outres/dsh-bot-core"
|
|
7
|
+
import type { GeneratedServer } from "./catalog"
|
|
8
|
+
import type { BrowserConnections } from "./connections"
|
|
9
|
+
import type { McpClientDeps } from "./mcp/client"
|
|
10
|
+
import { type McpServer, startServer } from "./mcp/server"
|
|
11
|
+
import { readVmUser } from "./vm/commands"
|
|
12
|
+
|
|
13
|
+
export interface BrowserSessionDeps extends McpClientDeps {
|
|
14
|
+
readonly generated: GeneratedServer
|
|
15
|
+
readonly connections: BrowserConnections
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function browserAttachment(
|
|
19
|
+
deps: BrowserSessionDeps,
|
|
20
|
+
): BotConnectorAttachmentSource<McpServer> {
|
|
21
|
+
return {
|
|
22
|
+
async open(context) {
|
|
23
|
+
const user = await readVmUser(context.environment, context.signal)
|
|
24
|
+
const server = await startServer(
|
|
25
|
+
context.environment,
|
|
26
|
+
user,
|
|
27
|
+
deps.generated.arguments,
|
|
28
|
+
context.signal,
|
|
29
|
+
deps,
|
|
30
|
+
)
|
|
31
|
+
deps.connections.connected(context.bot.id)
|
|
32
|
+
return {
|
|
33
|
+
value: server,
|
|
34
|
+
ended: server.ended,
|
|
35
|
+
async dispose() {
|
|
36
|
+
server.close()
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every offered Playwright MCP tool as a connector tool: its upstream JSON
|
|
3
|
+
* Schema and description, its permission, readOnly from the server's
|
|
4
|
+
* readOnlyHint (bot-core runs only those while a person operates the
|
|
5
|
+
* desktop), one call at a time per bot, and long results (page snapshots).
|
|
6
|
+
*/
|
|
7
|
+
import type {
|
|
8
|
+
BotConnectorJsonArgs,
|
|
9
|
+
BotConnectorResult,
|
|
10
|
+
BotConnectorSchemaTool,
|
|
11
|
+
BotConnectorToolDefinition,
|
|
12
|
+
ConnectorPreview,
|
|
13
|
+
} from "@1outres/dsh-bot-core"
|
|
14
|
+
import type { OfferedTool } from "./catalog"
|
|
15
|
+
import type { BrowserConnections } from "./connections"
|
|
16
|
+
import { McpCallError, McpUnavailableError } from "./mcp/client"
|
|
17
|
+
import { McpProtocolError } from "./mcp/protocol"
|
|
18
|
+
import type { McpServer } from "./mcp/server"
|
|
19
|
+
import { resultOf } from "./result"
|
|
20
|
+
import { toolSchema } from "./schema"
|
|
21
|
+
|
|
22
|
+
/** Snapshots of large pages run long; bot-core cuts what is beyond. */
|
|
23
|
+
export const MAX_RESULT_CHARS = 128_000
|
|
24
|
+
/** Playwright's own navigation timeout is 60 s, its actions' 5 s; wait_for may wait longer. */
|
|
25
|
+
export const CALL_TIMEOUT_MS = 180_000
|
|
26
|
+
/** The server does not queue calls; one page, one pointer. */
|
|
27
|
+
export const EXCLUSIVE_PAGE = "page"
|
|
28
|
+
|
|
29
|
+
const RECONNECTED =
|
|
30
|
+
"Note: the connection to the browser was opened again since your last call (the tabs and the page stay as they were). Element refs of earlier snapshots no longer work: take a new browser_snapshot. The previous call was not repeated."
|
|
31
|
+
|
|
32
|
+
export function previewOf(name: string, args: BotConnectorJsonArgs): ConnectorPreview {
|
|
33
|
+
return {
|
|
34
|
+
title: `Run browser_${name} in the bot's browser?`,
|
|
35
|
+
fields: Object.entries(args).map(([label, value]) => {
|
|
36
|
+
const text = typeof value === "string" ? value : JSON.stringify(value)
|
|
37
|
+
return { label, value: text.length > 300 ? `${text.slice(0, 300)}…` : text }
|
|
38
|
+
}),
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** What the model should do about a failure of the server or its connection. */
|
|
43
|
+
export function explain(error: unknown): Error {
|
|
44
|
+
if (error instanceof McpUnavailableError || error instanceof McpProtocolError) {
|
|
45
|
+
return new Error(
|
|
46
|
+
`${error.message}. The next browser call starts it again; take a browser_snapshot before repeating anything.`,
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
if (error instanceof McpCallError) return new Error(`Playwright MCP: ${error.message}`)
|
|
50
|
+
return error instanceof Error ? error : new Error(String(error))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function browserTools(
|
|
54
|
+
offered: readonly OfferedTool[],
|
|
55
|
+
connections: BrowserConnections,
|
|
56
|
+
): readonly BotConnectorToolDefinition<McpServer>[] {
|
|
57
|
+
return offered.map((entry): BotConnectorToolDefinition<McpServer> => {
|
|
58
|
+
const tool: BotConnectorSchemaTool<McpServer> = {
|
|
59
|
+
name: entry.name,
|
|
60
|
+
description: entry.upstream.description,
|
|
61
|
+
inputSchema: toolSchema(entry.upstream.inputSchema, entry.upstream.name),
|
|
62
|
+
permission: entry.permission,
|
|
63
|
+
readOnly: entry.readOnly,
|
|
64
|
+
exclusive: EXCLUSIVE_PAGE,
|
|
65
|
+
maxResultChars: MAX_RESULT_CHARS,
|
|
66
|
+
preview: (args) => previewOf(entry.name, args),
|
|
67
|
+
async execute(args, run) {
|
|
68
|
+
let result: BotConnectorResult
|
|
69
|
+
try {
|
|
70
|
+
result = await resultOf(
|
|
71
|
+
await run.attachment.callTool(entry.upstream.name, args, {
|
|
72
|
+
signal: run.signal,
|
|
73
|
+
timeoutMs: CALL_TIMEOUT_MS,
|
|
74
|
+
}),
|
|
75
|
+
run,
|
|
76
|
+
)
|
|
77
|
+
} catch (error) {
|
|
78
|
+
throw explain(error)
|
|
79
|
+
}
|
|
80
|
+
if (!connections.takeReconnected(run.bot.id)) return result
|
|
81
|
+
return {
|
|
82
|
+
...result,
|
|
83
|
+
text: [RECONNECTED, ...(result.text === undefined ? [] : [result.text])].join("\n\n"),
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
return tool
|
|
88
|
+
})
|
|
89
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Commands in the bot's environment through bot-core's environment access:
|
|
3
|
+
* one that must succeed, and the bot's account.
|
|
4
|
+
*/
|
|
5
|
+
import type { BotConnectorEnvironment, BotEnvironmentCommandResult } from "@1outres/dsh-bot-core"
|
|
6
|
+
import type { VmUser } from "./layout"
|
|
7
|
+
|
|
8
|
+
export interface CommandOptions {
|
|
9
|
+
readonly timeoutMs?: number
|
|
10
|
+
readonly maxOutputBytes?: number
|
|
11
|
+
readonly signal: AbortSignal
|
|
12
|
+
/** Exit codes that are answers rather than failures. Default: 0. */
|
|
13
|
+
readonly accept?: readonly number[]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class CommandError extends Error {
|
|
17
|
+
constructor(argv: readonly string[], result: BotEnvironmentCommandResult) {
|
|
18
|
+
const status = result.timedOut
|
|
19
|
+
? "timed out"
|
|
20
|
+
: result.signal !== null
|
|
21
|
+
? `signal ${result.signal}`
|
|
22
|
+
: `exit ${result.exitCode}`
|
|
23
|
+
const detail = result.stderr.trim().split("\n").slice(-6).join("\n")
|
|
24
|
+
super(`${argv.slice(0, 4).join(" ")} failed (${status})${detail === "" ? "" : `: ${detail}`}`)
|
|
25
|
+
this.name = "CommandError"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function run(
|
|
30
|
+
environment: BotConnectorEnvironment,
|
|
31
|
+
argv: readonly string[],
|
|
32
|
+
options: CommandOptions,
|
|
33
|
+
): Promise<{ readonly exitCode: number; readonly stdout: string }> {
|
|
34
|
+
const result = await environment.exec({
|
|
35
|
+
argv,
|
|
36
|
+
timeoutMs: options.timeoutMs ?? 60_000,
|
|
37
|
+
maxOutputBytes: options.maxOutputBytes ?? 64 * 1024,
|
|
38
|
+
signal: options.signal,
|
|
39
|
+
})
|
|
40
|
+
options.signal.throwIfAborted()
|
|
41
|
+
const accepted = options.accept ?? [0]
|
|
42
|
+
if (result.timedOut || result.exitCode === null || !accepted.includes(result.exitCode)) {
|
|
43
|
+
throw new CommandError(argv, result)
|
|
44
|
+
}
|
|
45
|
+
return { exitCode: result.exitCode, stdout: new TextDecoder().decode(result.stdout) }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const ACCOUNT = 'id -u; id -un; printf "%s\\n" "$HOME"'
|
|
49
|
+
|
|
50
|
+
export async function readVmUser(
|
|
51
|
+
environment: BotConnectorEnvironment,
|
|
52
|
+
signal: AbortSignal,
|
|
53
|
+
): Promise<VmUser> {
|
|
54
|
+
const { stdout } = await run(environment, ["sh", "-c", ACCOUNT], { signal, timeoutMs: 30_000 })
|
|
55
|
+
const [uid, name, home] = stdout.split("\n")
|
|
56
|
+
if (
|
|
57
|
+
uid === undefined ||
|
|
58
|
+
!/^[0-9]{1,9}$/.test(uid) ||
|
|
59
|
+
name === undefined ||
|
|
60
|
+
!/^[a-z_][a-z0-9_-]{0,31}$/.test(name) ||
|
|
61
|
+
home === undefined ||
|
|
62
|
+
!/^\/[^\0\n]*$/.test(home)
|
|
63
|
+
) {
|
|
64
|
+
throw new Error("the bot's account in its environment could not be read")
|
|
65
|
+
}
|
|
66
|
+
return { uid: Number(uid), name, home }
|
|
67
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the browser connector lives in the bot's environment, and what it
|
|
3
|
+
* reaches there: Playwright MCP's installation and the desktop's Chrome.
|
|
4
|
+
*/
|
|
5
|
+
export const MCP_DIR = "/opt/dsh-browser/mcp"
|
|
6
|
+
export const MCP_SHRINKWRAP = `${MCP_DIR}/npm-shrinkwrap.json`
|
|
7
|
+
export const MCP_CLI = `${MCP_DIR}/node_modules/@playwright/mcp/cli.js`
|
|
8
|
+
/** The desktop connector's Chrome answers DevTools here, on loopback only (its vm/bin/chrome-run). */
|
|
9
|
+
export const CDP_ENDPOINT = "http://127.0.0.1:9222"
|
|
10
|
+
/** Most bytes of screenshots and snapshots the server keeps on disk before it drops the oldest. */
|
|
11
|
+
export const OUTPUT_MAX_BYTES = 256 << 20
|
|
12
|
+
|
|
13
|
+
/** The bot's account in its environment. */
|
|
14
|
+
export interface VmUser {
|
|
15
|
+
readonly uid: number
|
|
16
|
+
readonly name: string
|
|
17
|
+
readonly home: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Where the server writes the files its results link to (screenshots, snapshots, PDFs), readable by the bot. */
|
|
21
|
+
export const outputDir = (user: VmUser) => `${user.home}/.cache/dsh-browser/output`
|
|
22
|
+
export const stagingDir = (user: VmUser) => `${user.home}/.cache/dsh-browser/staging`
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@1outres/dsh-bot-connector-browser` (row `bot-connector-browser`):
|
|
3
|
+
* registers the `browser` connector with bot-core. This file is the
|
|
4
|
+
* composition root: it reads the package's vm/ files and gives the Host
|
|
5
|
+
* modules the clock.
|
|
6
|
+
*/
|
|
7
|
+
import { readFile } from "node:fs/promises"
|
|
8
|
+
import type { Bot } from "@1outres/dsh-bot-core"
|
|
9
|
+
import type { Context } from "@deepseek-ai/cordis"
|
|
10
|
+
import { GENERATED_SERVER } from "./host/catalog"
|
|
11
|
+
import { createBrowserConnector } from "./host/connector"
|
|
12
|
+
import type { McpInstallation } from "./host/preparation"
|
|
13
|
+
|
|
14
|
+
export { GENERATED_SERVER } from "./host/catalog"
|
|
15
|
+
export { createBrowserConnector } from "./host/connector"
|
|
16
|
+
export { BROWSER_CONNECTOR_ID } from "./shared/connector"
|
|
17
|
+
|
|
18
|
+
export const name = "bot-connector-browser"
|
|
19
|
+
/**
|
|
20
|
+
* The `bot` service of @1outres/dsh-bot-core. Only types come from that
|
|
21
|
+
* package: a value import would bundle bot-core's runtime into this one.
|
|
22
|
+
*/
|
|
23
|
+
const BOT = "bot"
|
|
24
|
+
export const inject = ["bot"]
|
|
25
|
+
|
|
26
|
+
function botOf(ctx: Context): Bot {
|
|
27
|
+
const bot = ctx.get(BOT) as Bot | undefined
|
|
28
|
+
if (bot === undefined) throw new Error(`the "${BOT}" service is not available; inject it`)
|
|
29
|
+
return bot
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const VM = new URL("../vm/", import.meta.url)
|
|
33
|
+
|
|
34
|
+
/** The vm/ files the preparation installs Playwright MCP from. */
|
|
35
|
+
export async function readMcpInstallation(directory: URL = VM): Promise<McpInstallation> {
|
|
36
|
+
const read = async (path: string) => new Uint8Array(await readFile(new URL(path, directory)))
|
|
37
|
+
return {
|
|
38
|
+
packageJson: await read("mcp/package.json"),
|
|
39
|
+
shrinkwrap: await read("mcp/npm-shrinkwrap.json"),
|
|
40
|
+
script: await read("install-mcp.sh"),
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function apply(ctx: Context): Promise<void> {
|
|
45
|
+
const bot = botOf(ctx)
|
|
46
|
+
const connector = createBrowserConnector({
|
|
47
|
+
generated: GENERATED_SERVER,
|
|
48
|
+
installation: await readMcpInstallation(),
|
|
49
|
+
deadline: (ms) => AbortSignal.timeout(ms),
|
|
50
|
+
})
|
|
51
|
+
ctx.effect(() => bot.registerConnector(connector), "browser: connector")
|
|
52
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# Installs Playwright MCP from the package.json and npm-shrinkwrap.json in $1 into $2 (npm ci, no
|
|
3
|
+
# install scripts, no browser download) and swaps it in. Run as root by the browser connector's
|
|
4
|
+
# preparation and by infra/vm/build-base-image.sh, so both install the same tree.
|
|
5
|
+
set -eu
|
|
6
|
+
source=$1
|
|
7
|
+
target=$2
|
|
8
|
+
rm -rf -- "$target.new" "$target.old"
|
|
9
|
+
install -d -m 0755 -- "$target.new"
|
|
10
|
+
install -m 0644 -- "$source/package.json" "$source/npm-shrinkwrap.json" "$target.new/"
|
|
11
|
+
cd "$target.new"
|
|
12
|
+
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npm ci --omit=dev --ignore-scripts --no-audit --no-fund --loglevel=error
|
|
13
|
+
npm cache clean --force --loglevel=error
|
|
14
|
+
cd /
|
|
15
|
+
if [ -e "$target" ]; then mv -- "$target" "$target.old"; fi
|
|
16
|
+
mv -- "$target.new" "$target"
|
|
17
|
+
rm -rf -- "$target.old"
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dsh-browser-mcp",
|
|
3
|
+
"lockfileVersion": 3,
|
|
4
|
+
"requires": true,
|
|
5
|
+
"packages": {
|
|
6
|
+
"": {
|
|
7
|
+
"name": "dsh-browser-mcp",
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@playwright/mcp": "0.0.86"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"node_modules/@playwright/mcp": {
|
|
13
|
+
"version": "0.0.86",
|
|
14
|
+
"resolved": "https://registry.npmjs.org/@playwright/mcp/-/mcp-0.0.86.tgz",
|
|
15
|
+
"integrity": "sha512-4SL9OSINmik3+EObCYJIxH8BkhcqqGU6CM8XIgztW015iBVR9WR6Q0xF180Ery+xuEVyyK5r6nC2bOg3zjgCoA==",
|
|
16
|
+
"license": "Apache-2.0",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"playwright": "1.66.0-alpha-2026-11-18",
|
|
19
|
+
"playwright-core": "1.66.0-alpha-2026-11-18"
|
|
20
|
+
},
|
|
21
|
+
"bin": {
|
|
22
|
+
"playwright-mcp": "cli.js"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=20"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"node_modules/playwright": {
|
|
29
|
+
"version": "1.66.0-alpha-2026-11-18",
|
|
30
|
+
"resolved": "https://registry.npmjs.org/playwright/-/playwright-1.66.0-alpha-2026-11-18.tgz",
|
|
31
|
+
"integrity": "sha512-otQ+PFujQwUSpcLjoZ1zocwpEEF/5WhsBKaOuZeRhJ3ifjyRJJskKeosaw55WYdJzwlmfugK7OMX/tey1ExhWQ==",
|
|
32
|
+
"license": "Apache-2.0",
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"playwright-core": "1.66.0-alpha-2026-11-18"
|
|
35
|
+
},
|
|
36
|
+
"bin": {
|
|
37
|
+
"playwright": "cli.js"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=20"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"node_modules/playwright-core": {
|
|
44
|
+
"version": "1.66.0-alpha-2026-11-18",
|
|
45
|
+
"resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.66.0-alpha-2026-11-18.tgz",
|
|
46
|
+
"integrity": "sha512-VQ60tKPjqhf/3tHJGKuQ8Qn6e28Ziorhpmh4DGBZThgQp5geZeyKmuoJF2o/OwQBs5K7LNkhxNbOclBoAicqPg==",
|
|
47
|
+
"license": "Apache-2.0",
|
|
48
|
+
"bin": {
|
|
49
|
+
"playwright-core": "cli.js"
|
|
50
|
+
},
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=20"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|