@xtrom-ai/sdk 0.0.0-stage → 0.7.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.
Files changed (53) hide show
  1. package/LICENSE +190 -0
  2. package/README.md +185 -2
  3. package/dist/allowances.js +74 -0
  4. package/dist/cli.js +268 -0
  5. package/dist/credentials.js +37 -0
  6. package/dist/directive.js +57 -0
  7. package/dist/enforce.js +972 -0
  8. package/dist/index.js +31 -0
  9. package/dist/inspect.js +135 -0
  10. package/dist/local/agents.js +378 -0
  11. package/dist/local/appliers.js +61 -0
  12. package/dist/local/browser.js +135 -0
  13. package/dist/local/discover.js +154 -0
  14. package/dist/local/hook.js +778 -0
  15. package/dist/local/install.js +238 -0
  16. package/dist/local/inventory.js +219 -0
  17. package/dist/local/reasonText.js +29 -0
  18. package/dist/local/shipper.js +251 -0
  19. package/dist/local/spool.js +178 -0
  20. package/dist/local/updateKeys.js +3 -0
  21. package/dist/local/updater.js +224 -0
  22. package/dist/local/view.js +110 -0
  23. package/dist/memory.js +172 -0
  24. package/dist/reporter.js +336 -0
  25. package/dist/session.js +439 -0
  26. package/dist/version.js +2 -0
  27. package/dist/workspace.js +72 -0
  28. package/package.json +23 -4
  29. package/src/allowances.ts +74 -0
  30. package/src/cli.ts +268 -0
  31. package/src/credentials.ts +37 -0
  32. package/src/directive.ts +57 -0
  33. package/src/enforce.ts +972 -0
  34. package/src/index.ts +31 -0
  35. package/src/inspect.ts +135 -0
  36. package/src/local/agents.ts +378 -0
  37. package/src/local/appliers.ts +61 -0
  38. package/src/local/browser.ts +135 -0
  39. package/src/local/discover.ts +154 -0
  40. package/src/local/hook.ts +778 -0
  41. package/src/local/install.ts +238 -0
  42. package/src/local/inventory.ts +219 -0
  43. package/src/local/reasonText.ts +29 -0
  44. package/src/local/shipper.ts +251 -0
  45. package/src/local/spool.ts +178 -0
  46. package/src/local/updateKeys.ts +3 -0
  47. package/src/local/updater.ts +224 -0
  48. package/src/local/view.ts +110 -0
  49. package/src/memory.ts +172 -0
  50. package/src/reporter.ts +336 -0
  51. package/src/session.ts +439 -0
  52. package/src/version.ts +2 -0
  53. package/src/workspace.ts +72 -0
@@ -0,0 +1,135 @@
1
+ // Browser automation tools: what a browser action does, by risk — mirror of
2
+ // xtrom_sdk/local/browser.py (see its docstring for the model). The differential test runs the same
3
+ // scenario file through both runtimes.
4
+ export type BrowserKind = "observe" | "input" | "interact" | "submit" | "navigate" | "exec" | "upload" | "credentials";
5
+
6
+ export interface BrowserOp {
7
+ kind: BrowserKind;
8
+ url?: string;
9
+ typed: string[];
10
+ files: string[];
11
+ }
12
+
13
+ const SERVER_RE = /(^|[_-])(playwright|puppeteer|browser|browsermcp|chrome|chromium|devtools|stagehand|browserbase|hyperbrowser|steel|selenium|webdriver|browser_use)([_-]|$)/i;
14
+ const PREFIX_RE = /^(browserbase_stagehand|browserbase|browser|puppeteer|playwright|stagehand|chrome)_/i;
15
+
16
+ const OP_TABLE: Array<[string, RegExp]> = [
17
+ ["credentials", /(^|_)(cookies?|local_?storage|session_?storage|storage_state|indexed_?db)(_|$)/],
18
+ ["exec", new RegExp(String.raw`^(evaluate(_script)?|run_code(_unsafe)?|javascript(_tool)?|execute(_\w+)?|run_script|eval`
19
+ + String.raw`|(install|reload|uninstall)_extension|trigger_extension_action|(install|launch|uninstall)_pwa`
20
+ + String.raw`|shortcuts_execute|route|unroute|network_state_set|set_extra_http_headers)$`)],
21
+ ["upload", /^(file_upload|upload(_file|_image)?|drop|set_input_files)$/],
22
+ ["navigate", /^(navigate(_page)?|goto|go_to|open_url|open_page|new_page|new_tab|tabs_create(_mcp)?)$/],
23
+ ["dialog", /^handle_dialog$/],
24
+ ["key", /^(press_key|press|key|keyboard_press)$/],
25
+ ["input", /^(type(_text)?|fill(_form|_in)?|form_input|select_option|select|set_value)$/],
26
+ ["interact", /^(click(_at)?|double_click|drag|hover|tap|act)$/],
27
+ ["observe", new RegExp(String.raw`^(snapshot|take_snapshot|screenshot|take_screenshot|\w*console_messages?|\w*network_requests?`
28
+ + String.raw`|tabs|tabs_context(_mcp)?|list_pages|select_page|close(_page)?|resize(_page|_window)?|wait(_for)?`
29
+ + String.raw`|find|read_page|get_page_text|get_text|extract|observe|navigate_back|navigate_forward|go_back`
30
+ + String.raw`|go_forward|emulate(_media)?|pdf_save|get_config|lighthouse_audit|get_css_styles|performance_\w+`
31
+ + String.raw`|\w*heapsnapshot\w*|screencast_\w+|list_\w+|get_\w+|install|session_create|session_close)$`)],
32
+ ];
33
+
34
+ const COMMIT_RE = new RegExp(String.raw`\b(submit|send|pay|payment|buy|purchase|order|checkout|check out|confirm|delete|remove|transfer|post`
35
+ + String.raw`|publish|sign ?in|sign ?up|log ?in|authori[sz]e|allow|approve|accept|grant|save|subscribe|install`
36
+ + String.raw`|merge|deploy|book|reserve|donate|withdraw|share|invite|reply|comment|upload|continue)\b`, "i");
37
+ const ENTER_RE = /(^|\+)(enter|return|kp_enter|numpadenter)$/i;
38
+
39
+ const COMPUTER_OBSERVE = new Set(["screenshot", "zoom", "cursor_position", "mouse_move", "scroll", "wait", "scroll_to", "find"]);
40
+ const COMPUTER_INTERACT = new Set(["left_click", "right_click", "middle_click", "double_click", "triple_click",
41
+ "left_click_drag", "left_mouse_down", "left_mouse_up", "hold_key"]);
42
+ const TYPED_KEYS = ["text", "value", "values", "promptText", "prompt_text", "function", "code", "script"];
43
+ const LABEL_KEYS = ["element", "startElement", "endElement", "description", "action", "instruction", "label", "name", "selector"];
44
+
45
+ type Obj = Record<string, unknown>;
46
+ const isObj = (v: unknown): v is Obj => typeof v === "object" && v !== null && !Array.isArray(v);
47
+ const words = (name: string) => name.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replaceAll("-", "_").toLowerCase();
48
+ const op = (kind: BrowserKind, extra: Partial<BrowserOp> = {}): BrowserOp => ({ kind, typed: [], files: [], ...extra });
49
+
50
+ export function isBrowserTool(toolName: string): boolean {
51
+ if (toolName === "computer") return true;
52
+ if (!toolName.startsWith("mcp__")) return false;
53
+ const parts = toolName.split("__");
54
+ const [server, tool] = parts.length > 2 ? [parts[1], parts.slice(2).join("__")] : ["", parts[parts.length - 1]];
55
+ return SERVER_RE.test(server) || PREFIX_RE.test(words(tool));
56
+ }
57
+
58
+ function opName(toolName: string): string {
59
+ const tool = toolName.startsWith("mcp__") ? toolName.split("__").pop()! : toolName;
60
+ return words(tool).replace(PREFIX_RE, "");
61
+ }
62
+
63
+ /** String values under `keys`, including inside lists of field objects (fill_form). */
64
+ function strings(value: unknown, keys: string[], depth = 0): string[] {
65
+ const out: string[] = [];
66
+ if (depth > 4) return out;
67
+ if (isObj(value)) {
68
+ for (const [k, v] of Object.entries(value)) {
69
+ if (keys.includes(k)) {
70
+ if (typeof v === "string") out.push(v);
71
+ else if (Array.isArray(v)) out.push(...v.filter((x): x is string => typeof x === "string"));
72
+ }
73
+ if (isObj(v) || Array.isArray(v)) out.push(...strings(v, keys, depth + 1));
74
+ }
75
+ } else if (Array.isArray(value)) {
76
+ for (const v of value) out.push(...strings(v, keys, depth + 1));
77
+ }
78
+ return out;
79
+ }
80
+
81
+ function files(input: Obj): string[] {
82
+ const out: string[] = [];
83
+ for (const key of ["paths", "filePaths", "file_paths", "files", "path", "filePath", "file_path"]) {
84
+ const v = input[key];
85
+ if (typeof v === "string") out.push(v);
86
+ else if (Array.isArray(v)) out.push(...v.filter((x): x is string => typeof x === "string"));
87
+ }
88
+ return out;
89
+ }
90
+
91
+ const str = (v: unknown) => (v === undefined || v === null ? "" : String(v));
92
+
93
+ function computer(input: Obj): BrowserOp {
94
+ const action = str(input.action).toLowerCase();
95
+ const typed = strings(input, ["text"]);
96
+ if (COMPUTER_OBSERVE.has(action)) return op("observe");
97
+ if (COMPUTER_INTERACT.has(action)) return op("interact");
98
+ if (action === "type") return op("input", { typed });
99
+ if (action === "key") return op(ENTER_RE.test(str(input.text).trim()) ? "submit" : "interact");
100
+ // An action we don't know yet could be anything: treat it as a commit (fail closed).
101
+ return op("submit", { typed });
102
+ }
103
+
104
+ /** The browser op for a tool call, or null when it isn't a browser tool or the op is unknown. */
105
+ export function classifyBrowser(toolName: string, input: Obj): BrowserOp | null {
106
+ if (!isBrowserTool(toolName)) return null;
107
+ const name = opName(toolName);
108
+ if (name === "computer") return computer(input);
109
+ const typed = strings(input, TYPED_KEYS);
110
+ for (const [kind, rx] of OP_TABLE) {
111
+ if (!rx.test(name)) continue;
112
+ if (kind === "navigate") {
113
+ const url = input.url || input.uri;
114
+ return url ? op("navigate", { url: String(url) }) : op("observe");
115
+ }
116
+ if (kind === "observe" && name === "tabs") {
117
+ return str(input.action).toLowerCase() === "new" && input.url ? op("navigate", { url: String(input.url) }) : op("observe");
118
+ }
119
+ if (kind === "dialog") {
120
+ const accept = input.accept === true || str(input.action).toLowerCase() === "accept";
121
+ return op(accept ? "submit" : "interact", { typed });
122
+ }
123
+ if (kind === "key") return op(ENTER_RE.test(str(input.key || input.text).trim()) ? "submit" : "interact");
124
+ if (kind === "input") {
125
+ const submitKey = str(input.submitKey || input.submit_key);
126
+ return op(input.submit === true || ENTER_RE.test(submitKey) ? "submit" : "input", { typed });
127
+ }
128
+ if (kind === "interact") {
129
+ return op(strings(input, LABEL_KEYS).some((s) => COMMIT_RE.test(s)) ? "submit" : "interact", { typed });
130
+ }
131
+ if (kind === "upload") return op("upload", { typed, files: files(input) });
132
+ return op(kind as BrowserKind, { typed });
133
+ }
134
+ return null;
135
+ }
@@ -0,0 +1,154 @@
1
+ // Read-only MCP inventory across Claude Code, Cursor, Codex and VS Code configs.
2
+ // Reports server names, transport and env KEY names that look sensitive — never values.
3
+ // URLs are reported without query strings or userinfo (those often carry tokens).
4
+ import { existsSync, readFileSync } from "node:fs";
5
+ import { homedir } from "node:os";
6
+ import { join } from "node:path";
7
+
8
+ const SECRET_KEY_PARTS = ["token", "secret", "password", "passwd", "api_key", "apikey", "key", "credential"];
9
+
10
+ export interface Server { name: string; transport: string; command_or_url: string; source_file: string; risk_hints: string[] }
11
+ type Json = Record<string, any>;
12
+
13
+ function readJson(path: string): unknown {
14
+ try { return JSON.parse(readFileSync(path, "utf8")); } catch { return null; }
15
+ }
16
+
17
+ // Minimal TOML reader for MCP config tables: [a.b] / [a."b c"] headers, key = "str" | 'str' | number |
18
+ // bool | ["arr"] | { k = "v" }. Anything else is skipped — discovery is advisory and never throws.
19
+ export function parseTomlLite(text: string): Json {
20
+ const root: Json = {};
21
+ let cur = root;
22
+ const scalar = (raw: string): unknown => {
23
+ const s = raw.trim();
24
+ if (/^"(?:[^"\\]|\\.)*"$/.test(s)) { try { return JSON.parse(s); } catch { return s.slice(1, -1); } }
25
+ if (/^'[^']*'$/.test(s)) return s.slice(1, -1);
26
+ if (s === "true" || s === "false") return s === "true";
27
+ if (/^[+-]?\d+(\.\d+)?$/.test(s)) return Number(s);
28
+ return undefined;
29
+ };
30
+ const splitTop = (s: string): string[] => {
31
+ const out: string[] = []; let depth = 0, q: string | null = null, buf = "";
32
+ for (const ch of s) {
33
+ if (q) { buf += ch; if (ch === q) q = null; continue; }
34
+ if (ch === '"' || ch === "'") { q = ch; buf += ch; continue; }
35
+ if (ch === "[" || ch === "{") depth++;
36
+ if (ch === "]" || ch === "}") depth--;
37
+ if (ch === "," && depth === 0) { out.push(buf); buf = ""; continue; }
38
+ buf += ch;
39
+ }
40
+ if (buf.trim()) out.push(buf);
41
+ return out;
42
+ };
43
+ const value = (raw: string): unknown => {
44
+ const s = raw.trim();
45
+ if (s.startsWith("[") && s.endsWith("]")) return splitTop(s.slice(1, -1)).map(value).filter((v) => v !== undefined);
46
+ if (s.startsWith("{") && s.endsWith("}")) {
47
+ const o: Json = {};
48
+ for (const part of splitTop(s.slice(1, -1))) {
49
+ const i = part.indexOf("=");
50
+ if (i > 0) o[unquote(part.slice(0, i))] = value(part.slice(i + 1));
51
+ }
52
+ return o;
53
+ }
54
+ return scalar(s);
55
+ };
56
+ const unquote = (k: string) => k.trim().replace(/^"(.*)"$/, "$1").replace(/^'(.*)'$/, "$1");
57
+ const keyPath = (h: string) => (h.match(/"[^"]*"|'[^']*'|[^.]+/g) ?? []).map(unquote);
58
+ for (const rawLine of text.split(/\r?\n/)) {
59
+ const line = rawLine.replace(/^\s+/, "");
60
+ if (!line || line.startsWith("#")) continue;
61
+ const header = /^\[([^\[\]]+)\]\s*(#.*)?$/.exec(line);
62
+ if (header) {
63
+ cur = root;
64
+ for (const k of keyPath(header[1])) cur = (cur[k] && typeof cur[k] === "object") ? cur[k] : (cur[k] = {});
65
+ continue;
66
+ }
67
+ const i = line.indexOf("=");
68
+ if (i <= 0) continue;
69
+ const v = value(line.slice(i + 1).replace(/\s+#[^"']*$/, ""));
70
+ if (v !== undefined) cur[unquote(line.slice(0, i))] = v;
71
+ }
72
+ return root;
73
+ }
74
+
75
+ function readToml(path: string): unknown {
76
+ try { return parseTomlLite(readFileSync(path, "utf8")); } catch { return null; }
77
+ }
78
+
79
+ export function redactUrl(u: string): string {
80
+ return u.replace(/^([A-Za-z][A-Za-z0-9+.-]*:\/\/)[^/@?#]*@/, "$1").replace(/[?#].*$/, "");
81
+ }
82
+
83
+ function riskHints(cfg: Json): string[] {
84
+ const hints: string[] = [];
85
+ const url = cfg.url ?? cfg.endpoint;
86
+ if (typeof url === "string" && /^(https?|wss?):\/\//i.test(url)) hints.push("remote URL");
87
+ if (cfg.env && typeof cfg.env === "object" && !Array.isArray(cfg.env)) {
88
+ const keys = Object.keys(cfg.env).filter((k) => SECRET_KEY_PARTS.some((p) => k.toLowerCase().includes(p)));
89
+ if (keys.length) hints.push("secret env keys: " + keys.sort().join(", "));
90
+ }
91
+ if (cfg.headers && typeof cfg.headers === "object" && Object.keys(cfg.headers).length) hints.push("static auth headers");
92
+ return hints;
93
+ }
94
+
95
+ function serverFrom(name: string, cfg: unknown, source: string): Server | null {
96
+ if (!cfg || typeof cfg !== "object" || Array.isArray(cfg)) return null;
97
+ const c = cfg as Json;
98
+ const url = c.url ?? c.endpoint;
99
+ const command = c.command;
100
+ const transport = String(c.transport ?? c.type ?? (url ? "http" : command ? "stdio" : "unknown"));
101
+ const commandOrUrl = typeof url === "string" ? redactUrl(url) : String(url ?? command ?? "");
102
+ return { name, transport, command_or_url: commandOrUrl, source_file: source, risk_hints: riskHints(c) };
103
+ }
104
+
105
+ export function extractServers(data: unknown, source: string): Server[] {
106
+ const out: Server[] = [];
107
+ const seen = new Set<string>();
108
+ const addFrom = (m: unknown) => {
109
+ if (!m || typeof m !== "object" || Array.isArray(m)) return;
110
+ for (const [name, cfg] of Object.entries(m)) {
111
+ const s = serverFrom(name, cfg, source);
112
+ const key = `${s?.name}\u0000${s?.command_or_url}`;
113
+ if (s && !seen.has(key)) { seen.add(key); out.push(s); }
114
+ }
115
+ };
116
+ const walk = (node: unknown) => {
117
+ if (Array.isArray(node)) { node.forEach(walk); return; }
118
+ if (!node || typeof node !== "object") return;
119
+ const n = node as Json;
120
+ for (const k of ["mcpServers", "servers", "mcp_servers"]) addFrom(n[k]);
121
+ if (n.mcp && typeof n.mcp === "object") addFrom(n.mcp.servers ?? n.mcp.mcpServers);
122
+ for (const v of Object.values(n)) if (v && typeof v === "object") walk(v);
123
+ };
124
+ walk(data);
125
+ return out;
126
+ }
127
+
128
+ export interface DiscoverOpts { home?: string; projectDir?: string; vscodeSettings?: string }
129
+
130
+ export function discover(o: DiscoverOpts = {}): Server[] {
131
+ const home = o.home ?? homedir();
132
+ const project = o.projectDir ?? process.cwd();
133
+ const vscode = o.vscodeSettings ?? (process.platform === "darwin"
134
+ ? join(home, "Library", "Application Support", "Code", "User", "settings.json")
135
+ : process.platform === "win32"
136
+ ? join(process.env.APPDATA ?? join(home, "AppData", "Roaming"), "Code", "User", "settings.json")
137
+ : join(home, ".config", "Code", "User", "settings.json"));
138
+ const candidates: Array<[string, (p: string) => unknown]> = [
139
+ [join(home, ".claude.json"), readJson],
140
+ [join(project, ".mcp.json"), readJson],
141
+ [join(home, ".cursor", "mcp.json"), readJson],
142
+ [join(project, ".cursor", "mcp.json"), readJson],
143
+ [join(home, ".codex", "config.toml"), readToml],
144
+ [vscode, readJson],
145
+ [join(project, ".vscode", "mcp.json"), readJson],
146
+ ];
147
+ const servers: Server[] = [];
148
+ for (const [path, reader] of candidates) {
149
+ if (!existsSync(path)) continue;
150
+ const data = reader(path);
151
+ if (data !== null) servers.push(...extractServers(data, path));
152
+ }
153
+ return servers;
154
+ }