@pmndre/pine 0.0.0-stage → 0.22.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 +226 -2
- package/bin/pine.js +27 -0
- package/docs/SKILL.md +234 -0
- package/docs/references/agents.md +454 -0
- package/docs/references/coding.md +311 -0
- package/docs/references/computer.md +212 -0
- package/docs/references/configuration.md +310 -0
- package/docs/references/events.md +267 -0
- package/docs/references/mcp.md +420 -0
- package/docs/references/operations.md +394 -0
- package/docs/references/overview.md +195 -0
- package/docs/references/security.md +896 -0
- package/docs/references/setup.md +385 -0
- package/docs/references/sources.md +772 -0
- package/docs/references/stack.md +121 -0
- package/docs/references/tui.md +575 -0
- package/examples/home/.env.example +7 -0
- package/examples/home/USER.md +4 -0
- package/examples/home/agents/main/agent.json +4 -0
- package/examples/home/agents/main/workspace/AGENTS.md +14 -0
- package/examples/home/agents/main/workspace/MEMORY.md +4 -0
- package/examples/home/agents/main/workspace/SOUL.md +4 -0
- package/examples/home/models.json +62 -0
- package/examples/home/pine.json +34 -0
- package/examples/home/skills/README.txt +7 -0
- package/examples/launchd/com.pine.daemon.plist +32 -0
- package/examples/systemd/pine.service +22 -0
- package/mcp/browser.d.ts +99 -0
- package/mcp/browser.js +496 -0
- package/mcp/commands.d.ts +20 -0
- package/mcp/commands.js +88 -0
- package/mcp/computer.d.ts +139 -0
- package/mcp/computer.js +698 -0
- package/mcp/computer.swift +989 -0
- package/mcp/lib.d.ts +23 -0
- package/mcp/lib.js +52 -0
- package/mcp/web.d.ts +6 -0
- package/mcp/web.js +140 -0
- package/package.json +32 -4
- package/src/access.d.ts +112 -0
- package/src/access.js +438 -0
- package/src/actions.d.ts +75 -0
- package/src/actions.js +167 -0
- package/src/admin.d.ts +167 -0
- package/src/admin.js +625 -0
- package/src/agent-admin.d.ts +24 -0
- package/src/agent-admin.js +55 -0
- package/src/agents.d.ts +540 -0
- package/src/agents.js +2776 -0
- package/src/background.d.ts +29 -0
- package/src/background.js +100 -0
- package/src/cli.d.ts +2 -0
- package/src/cli.js +777 -0
- package/src/computer.d.ts +30 -0
- package/src/computer.js +32 -0
- package/src/config.d.ts +396 -0
- package/src/config.js +549 -0
- package/src/dev/claude-model.d.ts +37 -0
- package/src/dev/claude-model.js +204 -0
- package/src/env-file.d.ts +4 -0
- package/src/env-file.js +40 -0
- package/src/goals.d.ts +133 -0
- package/src/goals.js +316 -0
- package/src/http.d.ts +46 -0
- package/src/http.js +509 -0
- package/src/limiter.d.ts +32 -0
- package/src/limiter.js +84 -0
- package/src/login.d.ts +23 -0
- package/src/login.js +130 -0
- package/src/manual.d.ts +61 -0
- package/src/manual.js +443 -0
- package/src/mcp-config.d.ts +49 -0
- package/src/mcp-config.js +182 -0
- package/src/models.d.ts +90 -0
- package/src/models.js +274 -0
- package/src/net.d.ts +50 -0
- package/src/net.js +150 -0
- package/src/pine.d.ts +32 -0
- package/src/pine.js +134 -0
- package/src/programs.d.ts +123 -0
- package/src/programs.js +412 -0
- package/src/router.d.ts +56 -0
- package/src/router.js +164 -0
- package/src/sandbox-fs.d.ts +34 -0
- package/src/sandbox-fs.js +186 -0
- package/src/sandbox-linux.d.ts +96 -0
- package/src/sandbox-linux.js +297 -0
- package/src/sandbox-setup.d.ts +70 -0
- package/src/sandbox-setup.js +216 -0
- package/src/security.d.ts +238 -0
- package/src/security.js +660 -0
- package/src/skills.d.ts +67 -0
- package/src/skills.js +163 -0
- package/src/snapshots.d.ts +68 -0
- package/src/snapshots.js +306 -0
- package/src/source-types.d.ts +61 -0
- package/src/source-types.js +1 -0
- package/src/sources/command.d.ts +4 -0
- package/src/sources/command.js +77 -0
- package/src/sources/folder.d.ts +6 -0
- package/src/sources/folder.js +116 -0
- package/src/sources/ftp.d.ts +4 -0
- package/src/sources/ftp.js +307 -0
- package/src/sources/http.d.ts +4 -0
- package/src/sources/http.js +201 -0
- package/src/sources/mqtt.d.ts +18 -0
- package/src/sources/mqtt.js +97 -0
- package/src/sources/telegram.d.ts +22 -0
- package/src/sources/telegram.js +283 -0
- package/src/sources/webhook.d.ts +4 -0
- package/src/sources/webhook.js +52 -0
- package/src/sources.d.ts +344 -0
- package/src/sources.js +966 -0
- package/src/store.d.ts +99 -0
- package/src/store.js +277 -0
- package/src/timers.d.ts +83 -0
- package/src/timers.js +318 -0
- package/src/tools.d.ts +11 -0
- package/src/tools.js +1297 -0
- package/src/tui/app.d.ts +305 -0
- package/src/tui/app.js +3195 -0
- package/src/tui/client.d.ts +236 -0
- package/src/tui/client.js +131 -0
- package/src/tui/code-pane.d.ts +68 -0
- package/src/tui/code-pane.js +535 -0
- package/src/tui/highlight.d.ts +8 -0
- package/src/tui/highlight.js +86 -0
- package/src/tui/picture.d.ts +206 -0
- package/src/tui/picture.js +801 -0
- package/src/tui/run.d.ts +8 -0
- package/src/tui/run.js +97 -0
- package/src/tui/theme.d.ts +32 -0
- package/src/tui/theme.js +128 -0
- package/src/tui/undo.d.ts +5 -0
- package/src/tui/undo.js +50 -0
- package/src/tui/voice.d.ts +49 -0
- package/src/tui/voice.js +155 -0
- package/src/tui/voice.swift +223 -0
- package/src/types.d.ts +139 -0
- package/src/types.js +1 -0
- package/src/undo.d.ts +224 -0
- package/src/undo.js +465 -0
- package/src/update.d.ts +35 -0
- package/src/update.js +130 -0
- package/src/util.d.ts +65 -0
- package/src/util.js +171 -0
- package/src/watching.d.ts +20 -0
- package/src/watching.js +68 -0
- package/src/workspace-view.d.ts +72 -0
- package/src/workspace-view.js +221 -0
package/mcp/browser.js
ADDED
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
// Bundled MCP server "browser": a real browser (Playwright) for pages that need JavaScript,
|
|
2
|
+
// clicks, forms or a login.
|
|
3
|
+
//
|
|
4
|
+
// This file is a guard around Microsoft's Playwright MCP server, which it starts as a child:
|
|
5
|
+
//
|
|
6
|
+
// - Every request the browser makes goes through a small proxy in this process. The proxy
|
|
7
|
+
// refuses private, loopback and link-local addresses (checked on the address it actually
|
|
8
|
+
// connects to), so a page cannot reach your router, your NAS or pine's control port.
|
|
9
|
+
// The one exception is an address the owner allowed for the conversation: the daemon asks
|
|
10
|
+
// them when the agent opens a local address, and writes the yes into `localFile`.
|
|
11
|
+
// - Tools that would run code on this machine or read local files are removed from the tool
|
|
12
|
+
// list and refused if called anyway.
|
|
13
|
+
// - The browser's file access is confined to a scratch folder.
|
|
14
|
+
//
|
|
15
|
+
// - The browser has no window unless `headed` is set. In the owner's own conversations the agent
|
|
16
|
+
// can put it on the owner's screen and take it off again (browser_show, browser_hide): the
|
|
17
|
+
// browser is started anew, on the same page, with the same logins.
|
|
18
|
+
//
|
|
19
|
+
// Everything a browser returns is outside content; pine marks sessions that use it as tainted.
|
|
20
|
+
//
|
|
21
|
+
// options: {
|
|
22
|
+
// headed?, profile?, tools?: "core" | "all" | string[], block?: string[], images?,
|
|
23
|
+
// allowHosts?, denyHosts?, allowPrivate?, browser?, executablePath?, caps?, viewport?, extraArgs?
|
|
24
|
+
// }
|
|
25
|
+
import { spawn } from "node:child_process";
|
|
26
|
+
import dns from "node:dns";
|
|
27
|
+
import fs from "node:fs";
|
|
28
|
+
import http from "node:http";
|
|
29
|
+
import { createRequire } from "node:module";
|
|
30
|
+
import net from "node:net";
|
|
31
|
+
import os from "node:os";
|
|
32
|
+
import path from "node:path";
|
|
33
|
+
import readline from "node:readline";
|
|
34
|
+
import { options } from "./lib.js";
|
|
35
|
+
import { hostPort, isLocalName, isPrivateAddress } from "../src/net.js";
|
|
36
|
+
/** Never offered: they execute code in this process or hand local files to a page. */
|
|
37
|
+
export const ALWAYS_BLOCKED = ["browser_run_code_unsafe", "browser_file_upload", "browser_drop", "browser_install", "browser_storage_state", "browser_set_storage_state"];
|
|
38
|
+
/**
|
|
39
|
+
* This server asks Playwright for its "storage" tools to carry a login from a hidden browser to a
|
|
40
|
+
* visible one. To an agent they are offered only where the owner asked for them (`caps`).
|
|
41
|
+
*/
|
|
42
|
+
const STORAGE_TOOLS = /^browser_(cookie|localstorage|sessionstorage)_/;
|
|
43
|
+
/** pine's own: the browser as a window on the owner's screen. */
|
|
44
|
+
export const WINDOW_TOOLS = [
|
|
45
|
+
{
|
|
46
|
+
name: "browser_show",
|
|
47
|
+
description: "Open the browser as a window on your owner's screen, on the page you are on, with the same logins. Use it when your owner wants to watch, or has to do something themselves: log in, solve a captcha, confirm a payment. You keep working in the same window. The page is loaded again, so text typed into a form is gone.",
|
|
48
|
+
inputSchema: { type: "object", properties: {} },
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
name: "browser_hide",
|
|
52
|
+
description: "Close the browser's window on your owner's screen and carry on without one, on the same page, with the same logins.",
|
|
53
|
+
inputSchema: { type: "object", properties: {} },
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
const isWindowTool = (name) => WINDOW_TOOLS.some((t) => t.name === name);
|
|
57
|
+
/** Enough to read and operate a page; keeps the tool list short for small models. */
|
|
58
|
+
export const CORE_TOOLS = [
|
|
59
|
+
"browser_navigate", "browser_navigate_back", "browser_snapshot", "browser_find", "browser_click", "browser_type", "browser_fill_form",
|
|
60
|
+
"browser_press_key", "browser_select_option", "browser_hover", "browser_wait_for", "browser_tabs", "browser_handle_dialog", "browser_close",
|
|
61
|
+
// To look at a page the way a person does, and to see why it does not work: an agent that builds a page needs both.
|
|
62
|
+
"browser_take_screenshot", "browser_console_messages",
|
|
63
|
+
];
|
|
64
|
+
export function toolAllowed(name, o) {
|
|
65
|
+
if (ALWAYS_BLOCKED.includes(name) || (o.block ?? []).includes(name))
|
|
66
|
+
return false;
|
|
67
|
+
if (isWindowTool(name))
|
|
68
|
+
return o.window === true;
|
|
69
|
+
if (STORAGE_TOOLS.test(name) && !(o.caps ?? "").split(",").some((c) => c.trim() === "storage"))
|
|
70
|
+
return false;
|
|
71
|
+
const tools = o.tools ?? "core";
|
|
72
|
+
return tools === "all" ? true : (tools === "core" ? CORE_TOOLS : tools).includes(name);
|
|
73
|
+
}
|
|
74
|
+
const hostMatches = (list, host) => (list ?? []).some((h) => host === h || host.endsWith(`.${h}`));
|
|
75
|
+
/** What the agent is told when a local address is refused in a conversation where the owner can be asked. */
|
|
76
|
+
export const ASK_HINT = "A local address needs your owner's yes. Open it with browser_navigate (for example http://localhost:3000) and your owner is asked";
|
|
77
|
+
/** The local addresses the owner allowed for this conversation. Read each time: a yes applies at once. */
|
|
78
|
+
function allowedLocal(o) {
|
|
79
|
+
if (!o.localFile)
|
|
80
|
+
return [];
|
|
81
|
+
try {
|
|
82
|
+
const list = JSON.parse(fs.readFileSync(o.localFile, "utf8"));
|
|
83
|
+
return Array.isArray(list) ? list.filter((x) => typeof x === "string") : [];
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Resolve a host and return what the browser may connect to, or throw. For a public name that is
|
|
91
|
+
* the address it resolved to; for a local address the owner allowed (host and port), the host as given.
|
|
92
|
+
*/
|
|
93
|
+
export async function vet(host, o, port) {
|
|
94
|
+
const name = host.replace(/^\[|\]$/g, "").toLowerCase();
|
|
95
|
+
if (hostMatches(o.denyHosts, name))
|
|
96
|
+
throw new Error(`${name} is on the deny list`);
|
|
97
|
+
if (o.allowHosts?.length && !hostMatches(o.allowHosts, name))
|
|
98
|
+
throw new Error(`${name} is not on the allow list`);
|
|
99
|
+
if (!o.allowPrivate && (isLocalName(name) || (net.isIP(name) && isPrivateAddress(name)))) {
|
|
100
|
+
// Only a host that is local by its own name can be allowed: a name that has to be looked up
|
|
101
|
+
// could point somewhere else tomorrow.
|
|
102
|
+
if (port !== undefined && allowedLocal(o).includes(hostPort(name, port)))
|
|
103
|
+
return name;
|
|
104
|
+
throw new Error(`${name} is a ${isLocalName(name) ? "local name" : "private address"}`);
|
|
105
|
+
}
|
|
106
|
+
const addrs = net.isIP(name) ? [{ address: name }] : await dns.promises.lookup(name, { all: true });
|
|
107
|
+
if (!addrs.length)
|
|
108
|
+
throw new Error(`${name} does not resolve`);
|
|
109
|
+
if (!o.allowPrivate && addrs.some((a) => isPrivateAddress(a.address)))
|
|
110
|
+
throw new Error(`${name} resolves to a private address`);
|
|
111
|
+
return addrs[0].address;
|
|
112
|
+
}
|
|
113
|
+
/** A forward proxy that only lets vetted destinations through. */
|
|
114
|
+
export async function startGuardProxy(o, log = () => { }) {
|
|
115
|
+
const refused = [];
|
|
116
|
+
/** Records a refusal and returns the sentence for the agent. */
|
|
117
|
+
const deny = (what, why) => {
|
|
118
|
+
const reason = why instanceof Error ? why.message : String(why);
|
|
119
|
+
// Where the owner can be asked, say how. Elsewhere there is nothing the agent can do.
|
|
120
|
+
const line = `${what}: ${reason}${o.localFile && / is a (local name|private address)$/.test(reason) ? `. ${ASK_HINT}` : ""}`;
|
|
121
|
+
refused.push(line);
|
|
122
|
+
if (refused.length > 200)
|
|
123
|
+
refused.shift();
|
|
124
|
+
log(`refused ${line}`);
|
|
125
|
+
return line;
|
|
126
|
+
};
|
|
127
|
+
const server = http.createServer(async (req, res) => {
|
|
128
|
+
// Plain http through the proxy arrives as an absolute URL.
|
|
129
|
+
let url;
|
|
130
|
+
try {
|
|
131
|
+
url = new URL(req.url ?? "");
|
|
132
|
+
if (url.protocol !== "http:")
|
|
133
|
+
throw new Error("only http and https are proxied");
|
|
134
|
+
const address = await vet(url.hostname, o, Number(url.port || 80));
|
|
135
|
+
const headers = { ...req.headers };
|
|
136
|
+
delete headers["proxy-connection"];
|
|
137
|
+
delete headers["proxy-authorization"];
|
|
138
|
+
const upstream = http.request({ host: address, port: Number(url.port || 80), method: req.method, path: url.pathname + url.search, headers: { ...headers, host: url.host } }, (up) => {
|
|
139
|
+
res.writeHead(up.statusCode ?? 502, up.headers);
|
|
140
|
+
up.pipe(res);
|
|
141
|
+
});
|
|
142
|
+
upstream.on("error", () => res.writeHead(502).end());
|
|
143
|
+
req.pipe(upstream);
|
|
144
|
+
}
|
|
145
|
+
catch (e) {
|
|
146
|
+
res.writeHead(403, { "content-type": "text/plain" }).end(`pine: this address is not reachable from the browser (${deny(req.url ?? "?", e)})`);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
server.on("connect", async (req, client, head) => {
|
|
150
|
+
const m = (req.url ?? "").match(/^(.+):(\d+)$/);
|
|
151
|
+
try {
|
|
152
|
+
if (!m)
|
|
153
|
+
throw new Error("bad CONNECT target");
|
|
154
|
+
const address = await vet(m[1], o, Number(m[2]));
|
|
155
|
+
const upstream = net.connect(Number(m[2]), address, () => {
|
|
156
|
+
client.write("HTTP/1.1 200 Connection Established\r\n\r\n");
|
|
157
|
+
if (head.length)
|
|
158
|
+
upstream.write(head);
|
|
159
|
+
upstream.pipe(client);
|
|
160
|
+
client.pipe(upstream);
|
|
161
|
+
});
|
|
162
|
+
const close = () => (upstream.destroy(), client.destroy());
|
|
163
|
+
upstream.on("error", close);
|
|
164
|
+
client.on("error", close);
|
|
165
|
+
}
|
|
166
|
+
catch (e) {
|
|
167
|
+
deny(req.url ?? "?", e);
|
|
168
|
+
client.end("HTTP/1.1 403 Forbidden\r\n\r\n");
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
await new Promise((r) => server.listen(0, "127.0.0.1", r));
|
|
172
|
+
return { port: server.address().port, refused, close: () => new Promise((r) => (server.closeAllConnections(), server.close(() => r()))) };
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Playwright writes page snapshots and logs to files and returns links to them. An agent cannot
|
|
176
|
+
* follow those links, so put the content of files from the scratch folder into the result itself.
|
|
177
|
+
*/
|
|
178
|
+
export function inlineFiles(text, scratch, limit = 24_000) {
|
|
179
|
+
const root = fs.realpathSync(scratch);
|
|
180
|
+
return text.replace(/\[([^\]\n]+)\]\((\/[^)\s#]+)(#[^)\s]*)?\)/g, (whole, label, file) => {
|
|
181
|
+
try {
|
|
182
|
+
const real = fs.realpathSync(file);
|
|
183
|
+
if (!real.startsWith(root + path.sep))
|
|
184
|
+
return whole;
|
|
185
|
+
const bytes = fs.readFileSync(real);
|
|
186
|
+
// A screenshot or a PDF: the picture itself comes with the result, its bytes are not text.
|
|
187
|
+
if (bytes.subarray(0, 4096).includes(0))
|
|
188
|
+
return `${label}: saved as ${real}`;
|
|
189
|
+
const content = bytes.toString("utf8");
|
|
190
|
+
const max = /snapshot/i.test(label) ? limit : 2000;
|
|
191
|
+
const body = content.length > max ? `${content.slice(0, max)}\n[cut: ${content.length - max} more characters; use browser_find to locate things on a long page]` : content;
|
|
192
|
+
return `${label}:\n${body.trimEnd()}`;
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
return whole;
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/** Playwright's list of tabs ("- 0: (current) [Title](address)"), read. */
|
|
200
|
+
export function readTabs(text) {
|
|
201
|
+
const tabs = [];
|
|
202
|
+
let current = 0;
|
|
203
|
+
for (const m of text.matchAll(/^- (\d+):( \(current\))? \[(.*)\]\((\S*)\)\s*$/gm)) {
|
|
204
|
+
if (m[2])
|
|
205
|
+
current = tabs.length;
|
|
206
|
+
tabs.push({ title: m[3], url: m[4] });
|
|
207
|
+
}
|
|
208
|
+
return { tabs, current };
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Which tab somebody other than the agent changed, if any: a tab that was not there (the last
|
|
212
|
+
* such one), else the agent's own tab if its address is another now, else another tab whose
|
|
213
|
+
* address changed. Undefined when everything is as the agent last saw it.
|
|
214
|
+
*/
|
|
215
|
+
export function changedTab(before, now) {
|
|
216
|
+
if (!now.tabs.length)
|
|
217
|
+
return undefined;
|
|
218
|
+
if (now.tabs.length > before.tabs.length)
|
|
219
|
+
return now.tabs.length - 1;
|
|
220
|
+
// A page that only moves within itself (the part after #) is the same page.
|
|
221
|
+
const bare = (url) => url?.replace(/#.*$/, "");
|
|
222
|
+
const moved = now.tabs.map((t, i) => (bare(t.url) !== bare(before.tabs[i]?.url) ? i : -1)).filter((i) => i >= 0);
|
|
223
|
+
if (!moved.length)
|
|
224
|
+
return now.tabs.length < before.tabs.length || now.current !== before.current ? now.current : undefined;
|
|
225
|
+
return moved.includes(now.current) ? now.current : moved.at(-1);
|
|
226
|
+
}
|
|
227
|
+
/** Calls that only look at a page. Everything else acts on it or leaves it. */
|
|
228
|
+
const LOOKS = ["browser_snapshot", "browser_take_screenshot", "browser_console_messages", "browser_find", "browser_network_requests", "browser_network_request"];
|
|
229
|
+
const looksOnly = (params) => LOOKS.includes(String(params?.name)) || (params?.name === "browser_tabs" && params?.arguments?.action === "list");
|
|
230
|
+
/**
|
|
231
|
+
* The conversation between pine and Playwright's server: the tool list is filtered, blocked calls
|
|
232
|
+
* are refused, calls run one after another, and the browser is restarted when its window is to
|
|
233
|
+
* appear or to go. Returns what takes the client's lines.
|
|
234
|
+
*/
|
|
235
|
+
export function relay(o, io) {
|
|
236
|
+
let headed = !!o.headed;
|
|
237
|
+
/** The address of the page that is open, to open it again after a restart. */
|
|
238
|
+
let page;
|
|
239
|
+
/** The client's first message, said again to a browser that is started later. */
|
|
240
|
+
let hello;
|
|
241
|
+
const retired = new Set();
|
|
242
|
+
const own = new Map();
|
|
243
|
+
let ownIds = 0;
|
|
244
|
+
/** The tabs as the agent last saw them, while the browser has a window. */
|
|
245
|
+
let seen;
|
|
246
|
+
const listIds = new Set();
|
|
247
|
+
// A browser is one page at a time: calls that a model issues in parallel are run one after another,
|
|
248
|
+
// otherwise the second navigation would abort the first.
|
|
249
|
+
const waiting = [];
|
|
250
|
+
let inFlight;
|
|
251
|
+
const start = () => {
|
|
252
|
+
const c = io.launch(headed);
|
|
253
|
+
c.on("exit", () => {
|
|
254
|
+
if (retired.delete(c))
|
|
255
|
+
return;
|
|
256
|
+
for (const answer of own.values())
|
|
257
|
+
answer({ error: { message: "the browser ended" } });
|
|
258
|
+
io.ended();
|
|
259
|
+
});
|
|
260
|
+
readline.createInterface({ input: c.stdout }).on("line", (line) => fromBrowser(c, line));
|
|
261
|
+
return c;
|
|
262
|
+
};
|
|
263
|
+
let child = start();
|
|
264
|
+
const write = (msg) => child.stdin.write(`${JSON.stringify(msg)}\n`);
|
|
265
|
+
/** A request of this server's own; the answer does not go to the client. */
|
|
266
|
+
const ask = (method, params, ms = 30_000) => new Promise((resolve) => {
|
|
267
|
+
const id = `pine-${++ownIds}`;
|
|
268
|
+
const timer = setTimeout(() => own.get(id)?.({ error: { message: "the browser did not answer" } }), ms);
|
|
269
|
+
own.set(id, (msg) => (clearTimeout(timer), own.delete(id), resolve(msg)));
|
|
270
|
+
write({ jsonrpc: "2.0", id, method, params });
|
|
271
|
+
});
|
|
272
|
+
const textOf = (msg) => (msg.result?.content ?? []).filter((p) => p.type === "text").map((p) => String(p.text)).join("\n") || String(msg.error?.message ?? "");
|
|
273
|
+
const call = async (name, args) => {
|
|
274
|
+
const msg = await ask("tools/call", { name, arguments: args });
|
|
275
|
+
return { ok: !msg.error && !msg.result?.isError, text: inlineFiles(textOf(msg), io.scratch) };
|
|
276
|
+
};
|
|
277
|
+
/** The tabs as they are: their addresses in order, and which one the agent works in. */
|
|
278
|
+
const tabsNow = async () => {
|
|
279
|
+
const r = await call("browser_tabs", { action: "list" });
|
|
280
|
+
return r.ok ? readTabs(r.text) : undefined;
|
|
281
|
+
};
|
|
282
|
+
/**
|
|
283
|
+
* With a window, the owner uses the same browser. Before a call of the agent's, look whether
|
|
284
|
+
* the tabs are still as the agent last saw them. If not, the agent is moved to the tab that
|
|
285
|
+
* changed, and a call that would act is not carried out: the agent gets the page as it is now
|
|
286
|
+
* instead, so it never leaves or changes a page it has not seen.
|
|
287
|
+
*/
|
|
288
|
+
const guarded = async (msg) => {
|
|
289
|
+
const now = seen ? await tabsNow() : undefined;
|
|
290
|
+
const front = now && seen ? changedTab(seen, now) : undefined;
|
|
291
|
+
if (!now || front === undefined)
|
|
292
|
+
return write(msg);
|
|
293
|
+
if (front !== now.current)
|
|
294
|
+
await call("browser_tabs", { action: "select", index: front });
|
|
295
|
+
const tab = now.tabs[front];
|
|
296
|
+
const where = `${tab.title || "a page"} (${tab.url})`;
|
|
297
|
+
if (looksOnly(msg.params)) {
|
|
298
|
+
// The note is this server's own and stays here: Playwright's server answers only a message
|
|
299
|
+
// that is exactly a request, and one with a field more it drops without a word.
|
|
300
|
+
msg.notice = `The browser has changed since you last looked (your owner uses this window too). You are now on: ${where}.`;
|
|
301
|
+
return write({ jsonrpc: msg.jsonrpc, id: msg.id, method: msg.method, params: msg.params });
|
|
302
|
+
}
|
|
303
|
+
const page = await call("browser_snapshot", {});
|
|
304
|
+
seen = await tabsNow();
|
|
305
|
+
answered({
|
|
306
|
+
jsonrpc: "2.0",
|
|
307
|
+
id: msg.id,
|
|
308
|
+
result: {
|
|
309
|
+
content: [
|
|
310
|
+
{
|
|
311
|
+
type: "text",
|
|
312
|
+
text: `Not done: the browser is not where you left it. Your owner uses the same window and has probably opened this page: ${where}.\nIf what your owner asked for can be done on this page or this site, do it here. For anything else open a new tab (browser_tabs with action "new") instead of leaving their page. This is the page now:\n\n${page.text}`,
|
|
313
|
+
},
|
|
314
|
+
],
|
|
315
|
+
},
|
|
316
|
+
});
|
|
317
|
+
};
|
|
318
|
+
const next = () => {
|
|
319
|
+
if (inFlight !== undefined || !waiting.length)
|
|
320
|
+
return;
|
|
321
|
+
inFlight = waiting.shift();
|
|
322
|
+
if (isWindowTool(String(inFlight.params?.name)))
|
|
323
|
+
return void turn(inFlight);
|
|
324
|
+
if (headed)
|
|
325
|
+
return void guarded(inFlight);
|
|
326
|
+
write(inFlight);
|
|
327
|
+
};
|
|
328
|
+
const answered = (msg) => {
|
|
329
|
+
io.send(msg);
|
|
330
|
+
inFlight = undefined;
|
|
331
|
+
next();
|
|
332
|
+
};
|
|
333
|
+
/** Ends the browser and starts it again, with a window or without. */
|
|
334
|
+
const restart = async (window) => {
|
|
335
|
+
const old = child;
|
|
336
|
+
const gone = new Promise((r) => old.once("exit", r));
|
|
337
|
+
retired.add(old);
|
|
338
|
+
await ask("tools/call", { name: "browser_close", arguments: {} }, 5000);
|
|
339
|
+
old.kill();
|
|
340
|
+
await gone;
|
|
341
|
+
headed = window;
|
|
342
|
+
child = start();
|
|
343
|
+
if (hello) {
|
|
344
|
+
await ask("initialize", hello.params);
|
|
345
|
+
write({ jsonrpc: "2.0", method: "notifications/initialized" });
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
/** browser_show and browser_hide. The login goes along in a file that exists only for the restart. */
|
|
349
|
+
const turn = async (msg) => {
|
|
350
|
+
const show = msg.params.name === "browser_show";
|
|
351
|
+
const say = (text, isError = false) => answered({ jsonrpc: "2.0", id: msg.id, result: { content: [{ type: "text", text }], ...(isError ? { isError: true } : {}) } });
|
|
352
|
+
if (headed === show)
|
|
353
|
+
return say(show ? "The browser is already a window on your owner's screen." : "The browser has no window.");
|
|
354
|
+
const at = page;
|
|
355
|
+
const state = path.join(io.scratch, `state-${process.pid}-${Date.now()}.json`);
|
|
356
|
+
try {
|
|
357
|
+
const kept = at !== undefined && (await call("browser_storage_state", { filename: state })).ok;
|
|
358
|
+
const open = async (window) => {
|
|
359
|
+
await restart(window);
|
|
360
|
+
if (kept)
|
|
361
|
+
await call("browser_set_storage_state", { filename: state });
|
|
362
|
+
// A browser that was not told to open anything has no window yet.
|
|
363
|
+
return call("browser_navigate", { url: at ?? "about:blank" });
|
|
364
|
+
};
|
|
365
|
+
let now = await open(show);
|
|
366
|
+
if (show && !now.ok) {
|
|
367
|
+
const why = now.text.split("\n").find((l) => /error/i.test(l) && l.trim().length > 12) ?? now.text.split("\n")[0] ?? "";
|
|
368
|
+
now = await open(false);
|
|
369
|
+
return say(`A window could not be opened on this computer (${why.trim().slice(0, 200)}). The browser carries on without one. If pine runs on a machine without a screen, there is no window to show.\n\n${now.text}`, true);
|
|
370
|
+
}
|
|
371
|
+
seen = show ? await tabsNow() : undefined;
|
|
372
|
+
say(`${show ? "The browser is now a window on your owner's screen, and they use it too: they open pages in it, log in, solve a captcha, confirm something. Tell them it is open. From now on, what they ask you to find or do on the web, do in this window, on the page they are on; look at it first (browser_snapshot), because they may have changed it. When they have to do something themselves, wait until they say they are done. browser_hide closes the window and keeps the logins." : "The window is closed. The browser carries on without one, with the same logins."}\n\n${now.text}`);
|
|
373
|
+
}
|
|
374
|
+
catch (e) {
|
|
375
|
+
say(`The browser could not be ${show ? "shown" : "hidden"}: ${e instanceof Error ? e.message : String(e)}`, true);
|
|
376
|
+
}
|
|
377
|
+
finally {
|
|
378
|
+
fs.rmSync(state, { force: true });
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
const fromBrowser = (from, line) => {
|
|
382
|
+
if (!line.trim())
|
|
383
|
+
return;
|
|
384
|
+
let msg;
|
|
385
|
+
try {
|
|
386
|
+
msg = JSON.parse(line);
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
const mine = typeof msg.id === "string" ? own.get(msg.id) : undefined;
|
|
392
|
+
if (from !== child)
|
|
393
|
+
return; // the last words of a browser that was replaced
|
|
394
|
+
for (const part of msg.result?.content ?? []) {
|
|
395
|
+
if (part.type !== "text" || typeof part.text !== "string")
|
|
396
|
+
continue;
|
|
397
|
+
const url = part.text.match(/^- Page URL: (\S+)/m)?.[1];
|
|
398
|
+
if (url)
|
|
399
|
+
page = url === "about:blank" ? undefined : url;
|
|
400
|
+
}
|
|
401
|
+
if (mine)
|
|
402
|
+
return mine(msg);
|
|
403
|
+
if (listIds.delete(msg.id) && Array.isArray(msg.result?.tools))
|
|
404
|
+
msg.result.tools = [...msg.result.tools.filter((t) => toolAllowed(t.name, o)), ...WINDOW_TOOLS.filter((t) => toolAllowed(t.name, o))];
|
|
405
|
+
const current = inFlight !== undefined && msg.id !== undefined && msg.id === inFlight.id ? inFlight : undefined;
|
|
406
|
+
// A page that is still loading its fonts makes a screenshot give up after five seconds. Once more does it.
|
|
407
|
+
if (current?.params?.name === "browser_take_screenshot" && msg.result?.isError && !current.again && /Timeout/.test(textOf(msg))) {
|
|
408
|
+
current.again = true;
|
|
409
|
+
return void write({ jsonrpc: current.jsonrpc, id: current.id, method: current.method, params: current.params });
|
|
410
|
+
}
|
|
411
|
+
const closed = current?.params?.name === "browser_close" && !msg.result?.isError;
|
|
412
|
+
if (closed)
|
|
413
|
+
page = undefined;
|
|
414
|
+
for (const part of msg.result?.content ?? [])
|
|
415
|
+
if (part.type === "text" && typeof part.text === "string")
|
|
416
|
+
part.text = inlineFiles(part.text, io.scratch);
|
|
417
|
+
if (msg.result?.content && io.refused.length && msg.result.isError)
|
|
418
|
+
msg.result.content.push({ type: "text", text: `pine: the browser was refused ${io.refused.at(-1)}` });
|
|
419
|
+
if (current?.notice && Array.isArray(msg.result?.content))
|
|
420
|
+
msg.result.content.unshift({ type: "text", text: current.notice });
|
|
421
|
+
if (!current)
|
|
422
|
+
return io.send(msg);
|
|
423
|
+
// What the tabs are after the agent's own call is what it has seen. (A closed browser is not asked: asking would open it again.)
|
|
424
|
+
if (!headed || closed)
|
|
425
|
+
return (seen = undefined), answered(msg);
|
|
426
|
+
void tabsNow().then((tabs) => ((seen = tabs), answered(msg)));
|
|
427
|
+
};
|
|
428
|
+
return {
|
|
429
|
+
child: () => child,
|
|
430
|
+
close: () => child.stdin.end(),
|
|
431
|
+
line(line) {
|
|
432
|
+
if (!line.trim())
|
|
433
|
+
return;
|
|
434
|
+
let msg;
|
|
435
|
+
try {
|
|
436
|
+
msg = JSON.parse(line);
|
|
437
|
+
}
|
|
438
|
+
catch {
|
|
439
|
+
return void child.stdin.write(`${line}\n`);
|
|
440
|
+
}
|
|
441
|
+
if (msg.method === "initialize")
|
|
442
|
+
hello = msg;
|
|
443
|
+
if (msg.method === "tools/list")
|
|
444
|
+
listIds.add(msg.id);
|
|
445
|
+
if (msg.method === "tools/call" && !toolAllowed(String(msg.params?.name), o)) {
|
|
446
|
+
return io.send({ jsonrpc: "2.0", id: msg.id, result: { content: [{ type: "text", text: `The tool ${msg.params?.name} is not available.` }], isError: true } });
|
|
447
|
+
}
|
|
448
|
+
if (msg.method === "tools/call" && msg.id !== undefined) {
|
|
449
|
+
waiting.push(msg);
|
|
450
|
+
return next();
|
|
451
|
+
}
|
|
452
|
+
write(msg);
|
|
453
|
+
},
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
if (import.meta.main) {
|
|
457
|
+
const o = options();
|
|
458
|
+
const cli = path.join(path.dirname(createRequire(import.meta.url).resolve("@playwright/mcp")), "cli.js");
|
|
459
|
+
const scratch = fs.mkdtempSync(path.join(os.tmpdir(), "pine-browser-"));
|
|
460
|
+
const proxy = await startGuardProxy(o, (line) => process.stderr.write(`[pine browser] ${line}\n`));
|
|
461
|
+
const caps = [...new Set(["storage", ...(o.caps ?? "").split(",").map((c) => c.trim()).filter(Boolean)])].join(",");
|
|
462
|
+
const args = (headed) => [
|
|
463
|
+
cli,
|
|
464
|
+
"--proxy-server", `http://127.0.0.1:${proxy.port}`,
|
|
465
|
+
"--output-dir", scratch,
|
|
466
|
+
"--ignore-roots",
|
|
467
|
+
"--file-paths", "absolute",
|
|
468
|
+
"--block-service-workers",
|
|
469
|
+
// Tools that a page declares are outside content with a name; none of them is offered.
|
|
470
|
+
"--no-webmcp",
|
|
471
|
+
"--image-responses", o.images === false ? "omit" : "allow",
|
|
472
|
+
...(headed ? [] : ["--headless"]),
|
|
473
|
+
...(o.profile ? ["--user-data-dir", o.profile] : ["--isolated"]),
|
|
474
|
+
// Playwright's own Chromium build by default; "chrome" or "msedge" use an installed browser.
|
|
475
|
+
"--browser", o.browser ?? "chromium",
|
|
476
|
+
...(o.executablePath ? ["--executable-path", o.executablePath] : []),
|
|
477
|
+
"--caps", caps,
|
|
478
|
+
...(o.viewport ? ["--viewport-size", o.viewport] : []),
|
|
479
|
+
...(o.extraArgs ?? []),
|
|
480
|
+
];
|
|
481
|
+
const stop = () => {
|
|
482
|
+
server.child().kill();
|
|
483
|
+
fs.rmSync(scratch, { recursive: true, force: true });
|
|
484
|
+
process.exit(0);
|
|
485
|
+
};
|
|
486
|
+
const server = relay(o, {
|
|
487
|
+
launch: (headed) => spawn(process.execPath, args(headed), { cwd: scratch, stdio: ["pipe", "pipe", "inherit"] }),
|
|
488
|
+
send: (msg) => process.stdout.write(`${JSON.stringify(msg)}\n`),
|
|
489
|
+
scratch,
|
|
490
|
+
refused: proxy.refused,
|
|
491
|
+
ended: () => stop(),
|
|
492
|
+
});
|
|
493
|
+
process.on("SIGTERM", stop);
|
|
494
|
+
process.on("SIGINT", stop);
|
|
495
|
+
readline.createInterface({ input: process.stdin }).on("line", (line) => server.line(line)).on("close", () => server.close());
|
|
496
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface Param {
|
|
2
|
+
type?: "string" | "number" | "boolean";
|
|
3
|
+
description?: string;
|
|
4
|
+
enum?: unknown[];
|
|
5
|
+
pattern?: string;
|
|
6
|
+
required?: boolean;
|
|
7
|
+
default?: unknown;
|
|
8
|
+
}
|
|
9
|
+
interface Command {
|
|
10
|
+
description: string;
|
|
11
|
+
run: string[];
|
|
12
|
+
params?: Record<string, Param>;
|
|
13
|
+
timeoutSec?: number;
|
|
14
|
+
cwd?: string;
|
|
15
|
+
env?: Record<string, string>;
|
|
16
|
+
sandbox?: boolean;
|
|
17
|
+
maxOutput?: number;
|
|
18
|
+
}
|
|
19
|
+
export declare function buildArgv(cmd: Command, args: Record<string, unknown>): string[];
|
|
20
|
+
export {};
|
package/mcp/commands.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Bundled MCP server "commands": turns fixed shell commands into typed tools.
|
|
2
|
+
//
|
|
3
|
+
// An agent that should be able to "check the printer" does not need a shell for it. Declare the
|
|
4
|
+
// command once; the model only fills in the parameters you defined. Arguments are passed as an
|
|
5
|
+
// argument vector, never through a shell, so a parameter cannot smuggle in a second command.
|
|
6
|
+
//
|
|
7
|
+
// options: {
|
|
8
|
+
// commands: {
|
|
9
|
+
// <tool name>: {
|
|
10
|
+
// description,
|
|
11
|
+
// run: ["program", "arg", "{{param}}", ...],
|
|
12
|
+
// params?: { <name>: { type?: "string"|"number"|"boolean", description?, enum?, pattern?, required?, default? } },
|
|
13
|
+
// timeoutSec?, cwd?, env?, sandbox?, maxOutput?
|
|
14
|
+
// }
|
|
15
|
+
// },
|
|
16
|
+
// cwd, sandbox, env // filled in by pine: the agent's workspace, what puts a command into its sandbox, its shell environment
|
|
17
|
+
// }
|
|
18
|
+
import { spawn } from "node:child_process";
|
|
19
|
+
import { options, serve } from "./lib.js";
|
|
20
|
+
const o = options();
|
|
21
|
+
export function buildArgv(cmd, args) {
|
|
22
|
+
const values = {};
|
|
23
|
+
for (const [name, p] of Object.entries(cmd.params ?? {})) {
|
|
24
|
+
const v = args[name] ?? p.default;
|
|
25
|
+
if (v === undefined || v === null || v === "") {
|
|
26
|
+
if (p.required !== false && p.default === undefined)
|
|
27
|
+
throw new Error(`parameter "${name}" is required`);
|
|
28
|
+
values[name] = "";
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const s = String(v);
|
|
32
|
+
if (p.type === "number" && !Number.isFinite(Number(v)))
|
|
33
|
+
throw new Error(`parameter "${name}" must be a number`);
|
|
34
|
+
if (p.type === "boolean" && !["true", "false"].includes(s))
|
|
35
|
+
throw new Error(`parameter "${name}" must be true or false`);
|
|
36
|
+
if (p.enum && !p.enum.map(String).includes(s))
|
|
37
|
+
throw new Error(`parameter "${name}" must be one of: ${p.enum.join(", ")}`);
|
|
38
|
+
if (p.pattern && !new RegExp(`^(?:${p.pattern})$`).test(s))
|
|
39
|
+
throw new Error(`parameter "${name}" has an invalid format`);
|
|
40
|
+
// A value that starts with "-" would be read as an option by most programs.
|
|
41
|
+
if (s.startsWith("-") && p.type !== "number" && !p.pattern && !p.enum)
|
|
42
|
+
throw new Error(`parameter "${name}" must not start with "-"`);
|
|
43
|
+
if (s.includes("\0"))
|
|
44
|
+
throw new Error(`parameter "${name}" contains an invalid character`);
|
|
45
|
+
values[name] = s;
|
|
46
|
+
}
|
|
47
|
+
return cmd.run.map((part) => part.replace(/\{\{\s*(\w+)\s*\}\}/g, (_m, name) => (name in values ? values[name] : "")));
|
|
48
|
+
}
|
|
49
|
+
function run(name, cmd, args) {
|
|
50
|
+
let argv = buildArgv(cmd, args);
|
|
51
|
+
if (cmd.sandbox && o.sandbox?.length)
|
|
52
|
+
argv = [...o.sandbox, ...argv];
|
|
53
|
+
const max = cmd.maxOutput ?? 30_000;
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
const child = spawn(argv[0], argv.slice(1), { cwd: cmd.cwd ?? o.cwd, env: { ...(o.env ?? { PATH: process.env.PATH ?? "" }), ...(cmd.env ?? {}) }, stdio: ["ignore", "pipe", "pipe"] });
|
|
56
|
+
let out = "";
|
|
57
|
+
const add = (d) => {
|
|
58
|
+
if (out.length < max)
|
|
59
|
+
out += d.toString("utf8");
|
|
60
|
+
};
|
|
61
|
+
child.stdout.on("data", add);
|
|
62
|
+
child.stderr.on("data", add);
|
|
63
|
+
const timer = setTimeout(() => child.kill("SIGKILL"), (cmd.timeoutSec ?? 30) * 1000);
|
|
64
|
+
child.on("error", (e) => (clearTimeout(timer), reject(new Error(`${name}: ${e.message}`))));
|
|
65
|
+
child.on("close", (code, signal) => {
|
|
66
|
+
clearTimeout(timer);
|
|
67
|
+
const text = out.length > max ? `${out.slice(0, max)}\n[output cut at ${max} characters]` : out;
|
|
68
|
+
if (signal)
|
|
69
|
+
return reject(new Error(`${name} was stopped after ${cmd.timeoutSec ?? 30} s\n${text}`));
|
|
70
|
+
if (code !== 0)
|
|
71
|
+
return reject(new Error(`${name} exited with code ${code}\n${text}`));
|
|
72
|
+
resolve(text.trim() || "(no output)");
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if (import.meta.main) {
|
|
77
|
+
const tools = Object.entries(o.commands ?? {}).map(([name, cmd]) => ({
|
|
78
|
+
name,
|
|
79
|
+
description: cmd.description,
|
|
80
|
+
inputSchema: {
|
|
81
|
+
type: "object",
|
|
82
|
+
properties: Object.fromEntries(Object.entries(cmd.params ?? {}).map(([p, d]) => [p, { type: d.type ?? "string", ...(d.description ? { description: d.description } : {}), ...(d.enum ? { enum: d.enum } : {}) }])),
|
|
83
|
+
required: Object.entries(cmd.params ?? {}).filter(([, d]) => d.required !== false && d.default === undefined).map(([p]) => p),
|
|
84
|
+
},
|
|
85
|
+
handler: (args) => run(name, cmd, args),
|
|
86
|
+
}));
|
|
87
|
+
serve({ name: "pine-commands", version: "0.1.0" }, tools);
|
|
88
|
+
}
|