portal-agent-cli 3.0.1 → 3.0.2
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 +17 -1
- package/README.md +13 -48
- package/lib/args.mjs +5 -31
- package/lib/browser.mjs +6 -21
- package/lib/config.mjs +20 -26
- package/lib/deepthink.mjs +11 -21
- package/lib/exec.mjs +45 -1
- package/lib/instructions.mjs +44 -1
- package/lib/log.mjs +0 -4
- package/lib/parse.mjs +42 -11
- package/lib/paste.mjs +4 -10
- package/lib/providers.mjs +7 -7
- package/lib/repl.mjs +234 -1
- package/lib/safety.mjs +60 -1
- package/lib/send.mjs +5 -14
- package/lib/sessions.mjs +64 -1
- package/lib/system.mjs +41 -1
- package/lib/theme.mjs +1 -4
- package/lib/tools/delete.mjs +24 -1
- package/lib/tools/edit.mjs +46 -1
- package/lib/tools/find.mjs +39 -1
- package/lib/tools/git.mjs +48 -1
- package/lib/tools/grep.mjs +40 -1
- package/lib/tools/index.mjs +52 -1
- package/lib/tools/list.mjs +35 -1
- package/lib/tools/path.mjs +22 -1
- package/lib/tools/read.mjs +28 -1
- package/lib/tools/shell.mjs +59 -1
- package/lib/tools/walk.mjs +30 -1
- package/lib/tools/write.mjs +31 -1
- package/lib/turn.mjs +98 -1
- package/lib/wait.mjs +8 -15
- package/package.json +3 -6
- package/portal.mjs +3 -15
package/LICENSE
CHANGED
|
@@ -2,4 +2,20 @@ MIT License
|
|
|
2
2
|
|
|
3
3
|
Copyright (c) 2026
|
|
4
4
|
|
|
5
|
-
Permission is hereby granted, free of charge, to any
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,63 +1,28 @@
|
|
|
1
1
|
# portal-agent
|
|
2
2
|
|
|
3
|
-
Drive web AI products from the terminal with local tools.
|
|
4
|
-
Chromium browser against the provider's own website. No
|
|
5
|
-
```
|
|
3
|
+
Drive web AI products from the terminal with local tools.
|
|
6
4
|
|
|
7
|
-
|
|
5
|
+
Supports DeepSeek, ChatGPT, Gemini, and Grok.
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
## Install
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
npm install -g portal-agent-cli
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
## Run
|
|
14
12
|
|
|
15
13
|
portal-agent --workspace F:/projects/my-app
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
Or without installing:
|
|
20
|
-
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
npx portal-agent-cli --workspace F:/projects/my-app
|
|
24
|
-
|
|
25
|
-
```
|
|
15
|
+
## Commands
|
|
26
16
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
portal-agent --workspace F:/proj exec "list the files here"
|
|
31
|
-
|
|
32
|
-
```
|
|
17
|
+
/help, /cd, /pwd, /provider, /providers, /deepthink,
|
|
18
|
+
/sessions, /queue, /status, /clear, /quit
|
|
33
19
|
|
|
34
20
|
## Tools
|
|
35
21
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
| `write_file(path, content)` | write a whole file (backs up existing) |
|
|
40
|
-
| `edit_file(path, old_string, new_string, replace_all?)`
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
- approval required
|
|
44
|
-
tool: shell
|
|
45
|
-
{
|
|
46
|
-
"command": "npm test"
|
|
47
|
-
}
|
|
48
|
-
allow? yes / always / no:
|
|
49
|
-
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
Answering `a` approves that tool for the rest of the session.
|
|
53
|
-
|
|
54
|
-
## Safety
|
|
55
|
-
|
|
56
|
-
-
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
portal-tool: {"tool":"read_file","args":{"path":"notes.txt"}}
|
|
22
|
+
read_file, write_file, edit_file, append_file, undo_file,
|
|
23
|
+
list_files, find_files, grep_search, delete_file, move_file,
|
|
24
|
+
shell, git_status, git_diff, git_log
|
|
60
25
|
|
|
61
|
-
|
|
26
|
+
## License
|
|
62
27
|
|
|
63
|
-
|
|
28
|
+
MIT
|
package/lib/args.mjs
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
|
-
import { theme } from "./theme.mjs";
|
|
4
3
|
|
|
5
4
|
export function parseArgs() {
|
|
6
5
|
const a = process.argv.slice(2);
|
|
7
6
|
const o = {
|
|
8
7
|
ws: null,
|
|
9
8
|
provider: null,
|
|
10
|
-
model: null,
|
|
11
9
|
maxSteps: null,
|
|
12
10
|
timeout: null,
|
|
13
11
|
loginTimeout: null,
|
|
@@ -15,8 +13,7 @@ export function parseArgs() {
|
|
|
15
13
|
verbose: false,
|
|
16
14
|
yolo: false,
|
|
17
15
|
session: null,
|
|
18
|
-
resume: null
|
|
19
|
-
theme: "auto"
|
|
16
|
+
resume: null
|
|
20
17
|
};
|
|
21
18
|
|
|
22
19
|
let i = 0;
|
|
@@ -24,18 +21,16 @@ export function parseArgs() {
|
|
|
24
21
|
const t = a[i];
|
|
25
22
|
if (t === "-w" || t === "--workspace") o.ws = a[++i];
|
|
26
23
|
else if (t === "-p" || t === "--provider") o.provider = a[++i];
|
|
27
|
-
else if (t === "--model") o.model = a[++i];
|
|
28
24
|
else if (t === "--timeout") o.timeout = Number(a[++i]);
|
|
29
25
|
else if (t === "--login-timeout") o.loginTimeout = Number(a[++i]);
|
|
30
26
|
else if (t === "--max-steps") o.maxSteps = Number(a[++i]);
|
|
31
27
|
else if (t === "--session") o.session = a[++i];
|
|
32
28
|
else if (t === "--resume") o.resume = a[++i];
|
|
33
|
-
else if (t === "--theme") o.theme = a[++i];
|
|
34
29
|
else if (t === "--verbose" || t === "-V") o.verbose = true;
|
|
35
30
|
else if (t === "--yolo") o.yolo = true;
|
|
36
31
|
else if (t === "exec") { o.exec = a.slice(i + 1).join(" "); break; }
|
|
37
32
|
else if (t === "--help" || t === "-h") { help(); process.exit(0); }
|
|
38
|
-
else if (t === "--version" || t === "-v") { console.log("3.0.
|
|
33
|
+
else if (t === "--version" || t === "-v") { console.log("3.0.2"); process.exit(0); }
|
|
39
34
|
i++;
|
|
40
35
|
}
|
|
41
36
|
|
|
@@ -46,46 +41,25 @@ export function parseArgs() {
|
|
|
46
41
|
process.exit(2);
|
|
47
42
|
}
|
|
48
43
|
}
|
|
49
|
-
|
|
50
44
|
return o;
|
|
51
45
|
}
|
|
52
46
|
|
|
53
47
|
function help() {
|
|
54
|
-
const t = theme;
|
|
55
48
|
console.log("");
|
|
56
|
-
console.log(
|
|
49
|
+
console.log("portal-agent -- drive web AI from the terminal");
|
|
57
50
|
console.log("");
|
|
58
|
-
console.log(t.bold("USAGE"));
|
|
59
51
|
console.log(" portal-agent [options]");
|
|
60
52
|
console.log(" portal-agent [options] exec <task>");
|
|
61
53
|
console.log("");
|
|
62
|
-
console.log(
|
|
54
|
+
console.log("Options:");
|
|
63
55
|
console.log(" -w, --workspace <dir> folder the agent works in");
|
|
64
56
|
console.log(" -p, --provider <name> deepseek | chatgpt | gemini | grok");
|
|
65
|
-
console.log(" --model <name> provider-specific model hint");
|
|
66
57
|
console.log(" --timeout <ms> response timeout (default 180000)");
|
|
67
58
|
console.log(" --login-timeout <ms> login wait (default 600000)");
|
|
68
|
-
console.log(" --max-steps <n> max
|
|
69
|
-
console.log(" --session <name> start or reuse a named session");
|
|
59
|
+
console.log(" --max-steps <n> max steps per turn (default 12)");
|
|
70
60
|
console.log(" --resume <name> resume a saved session");
|
|
71
|
-
console.log(" --theme <name> auto | dark | light");
|
|
72
|
-
console.log(" -V, --verbose print config on startup");
|
|
73
61
|
console.log(" --yolo auto-approve every tool call");
|
|
74
62
|
console.log(" -h, --help show this help");
|
|
75
63
|
console.log(" -v, --version show version");
|
|
76
64
|
console.log("");
|
|
77
|
-
console.log(t.bold("ENVIRONMENT"));
|
|
78
|
-
console.log(" PORTAL_WORKSPACE same as --workspace");
|
|
79
|
-
console.log(" PORTAL_PROVIDER default provider");
|
|
80
|
-
console.log(" PORTAL_BROWSER_PATH explicit browser executable");
|
|
81
|
-
console.log(" PORTAL_DEEPTHINK set to off to skip DeepThink toggle");
|
|
82
|
-
console.log(" PORTAL_DEBUG print stack traces on error");
|
|
83
|
-
console.log(" PORTAL_YOLO same as --yolo");
|
|
84
|
-
console.log(" NO_COLOR disable color output");
|
|
85
|
-
console.log("");
|
|
86
|
-
console.log(t.bold("EXAMPLES"));
|
|
87
|
-
console.log(" portal-agent --workspace F:/projects/app");
|
|
88
|
-
console.log(" portal-agent -w . -p chatgpt");
|
|
89
|
-
console.log(" portal-agent exec \"list the files here\"");
|
|
90
|
-
console.log("");
|
|
91
65
|
}
|
package/lib/browser.mjs
CHANGED
|
@@ -11,16 +11,10 @@ const CANDIDATES = [
|
|
|
11
11
|
"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe",
|
|
12
12
|
"C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe",
|
|
13
13
|
"C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe",
|
|
14
|
-
"C:/Program Files (x86)/BraveSoftware/Brave-Browser/Application/brave.exe",
|
|
15
14
|
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
|
|
16
15
|
"/Applications/Chromium.app/Contents/MacOS/Chromium",
|
|
17
|
-
"/Applications/Brave Browser.app/Contents/MacOS/Brave Browser",
|
|
18
|
-
"/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
|
|
19
16
|
"/usr/bin/google-chrome",
|
|
20
|
-
"/usr/bin/
|
|
21
|
-
"/usr/bin/chromium",
|
|
22
|
-
"/usr/bin/chromium-browser",
|
|
23
|
-
"/usr/bin/microsoft-edge"
|
|
17
|
+
"/usr/bin/chromium"
|
|
24
18
|
];
|
|
25
19
|
|
|
26
20
|
export function findBrowser() {
|
|
@@ -30,20 +24,12 @@ export function findBrowser() {
|
|
|
30
24
|
return null;
|
|
31
25
|
}
|
|
32
26
|
|
|
33
|
-
export function listBrowsers() {
|
|
34
|
-
const found = [];
|
|
35
|
-
for (const p of CANDIDATES) {
|
|
36
|
-
if (p && existsSync(p)) found.push(p);
|
|
37
|
-
}
|
|
38
|
-
return found;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
27
|
export async function launch() {
|
|
42
28
|
const exe = findBrowser();
|
|
43
29
|
if (!exe) {
|
|
44
30
|
throw new Error(
|
|
45
|
-
"No Chrome, Chromium, Edge, or Brave found
|
|
46
|
-
"
|
|
31
|
+
"No Chrome, Chromium, Edge, or Brave found. " +
|
|
32
|
+
"Set PORTAL_BROWSER_PATH to your browser executable."
|
|
47
33
|
);
|
|
48
34
|
}
|
|
49
35
|
|
|
@@ -54,8 +40,7 @@ export async function launch() {
|
|
|
54
40
|
const ctx = await chromium.launchPersistentContext(profileDir, {
|
|
55
41
|
executablePath: exe,
|
|
56
42
|
headless: false,
|
|
57
|
-
viewport: { width: 1280, height: 900 }
|
|
58
|
-
args: ["--disable-blink-features=AutomationControlled"]
|
|
43
|
+
viewport: { width: 1280, height: 900 }
|
|
59
44
|
});
|
|
60
45
|
|
|
61
46
|
ctx.on("page", function (page) {
|
|
@@ -67,9 +52,9 @@ export async function launch() {
|
|
|
67
52
|
return ctx;
|
|
68
53
|
} catch (e) {
|
|
69
54
|
const m = e.message || String(e);
|
|
70
|
-
if (
|
|
55
|
+
if (m.indexOf("ProcessSingleton") >= 0 || m.indexOf("already running") >= 0) {
|
|
71
56
|
throw new Error(
|
|
72
|
-
"Another Chrome window is using the Portal profile
|
|
57
|
+
"Another Chrome window is using the Portal profile. " +
|
|
73
58
|
"Close every Chrome window and try again."
|
|
74
59
|
);
|
|
75
60
|
}
|
package/lib/config.mjs
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
-
import { resolve, join
|
|
2
|
+
import { resolve, join } from "node:path";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
|
-
import { mkdir, writeFile } from "node:fs/promises";
|
|
5
4
|
|
|
6
5
|
const DEFAULTS = {
|
|
7
6
|
provider: "deepseek",
|
|
8
7
|
maxSteps: 12,
|
|
9
8
|
timeout: 180000,
|
|
10
9
|
loginTimeout: 600000,
|
|
11
|
-
theme: "auto",
|
|
12
10
|
yolo: false,
|
|
13
11
|
verbose: false,
|
|
14
|
-
instructions: ["PORTAL.md", "AGENTS.md", ".portal.md"],
|
|
15
12
|
ignored: [
|
|
16
13
|
"node_modules", ".git", ".hg", ".svn", "dist", "build", "out",
|
|
17
14
|
".next", ".nuxt", ".turbo", ".cache", "target", "__pycache__",
|
|
@@ -19,11 +16,11 @@ const DEFAULTS = {
|
|
|
19
16
|
]
|
|
20
17
|
};
|
|
21
18
|
|
|
22
|
-
|
|
19
|
+
function configPath() {
|
|
23
20
|
return join(homedir(), ".portal-agent", "config.json");
|
|
24
21
|
}
|
|
25
22
|
|
|
26
|
-
|
|
23
|
+
function readGlobal() {
|
|
27
24
|
const p = configPath();
|
|
28
25
|
if (!existsSync(p)) return {};
|
|
29
26
|
try {
|
|
@@ -33,31 +30,30 @@ export function readGlobalConfig() {
|
|
|
33
30
|
}
|
|
34
31
|
}
|
|
35
32
|
|
|
36
|
-
export async function writeGlobalConfig(obj) {
|
|
37
|
-
const p = configPath();
|
|
38
|
-
await mkdir(dirname(p), { recursive: true });
|
|
39
|
-
await writeFile(p, JSON.stringify(obj, null, 2), "utf8");
|
|
40
|
-
}
|
|
41
|
-
|
|
42
33
|
export async function loadConfig(args) {
|
|
43
|
-
const
|
|
34
|
+
const g = readGlobal();
|
|
44
35
|
|
|
45
36
|
const config = {
|
|
46
|
-
|
|
47
|
-
|
|
37
|
+
provider: g.provider || DEFAULTS.provider,
|
|
38
|
+
maxSteps: g.maxSteps || DEFAULTS.maxSteps,
|
|
39
|
+
timeout: g.timeout || DEFAULTS.timeout,
|
|
40
|
+
loginTimeout: g.loginTimeout || DEFAULTS.loginTimeout,
|
|
41
|
+
yolo: g.yolo || false,
|
|
42
|
+
verbose: g.verbose || false,
|
|
43
|
+
ignored: g.ignored || DEFAULTS.ignored,
|
|
48
44
|
ws: args.ws || process.env.PORTAL_WORKSPACE || process.cwd(),
|
|
49
|
-
provider: args.provider || process.env.PORTAL_PROVIDER || global.provider || DEFAULTS.provider,
|
|
50
|
-
maxSteps: args.maxSteps || global.maxSteps || DEFAULTS.maxSteps,
|
|
51
|
-
timeout: args.timeout || global.timeout || DEFAULTS.timeout,
|
|
52
|
-
loginTimeout: args.loginTimeout || global.loginTimeout || DEFAULTS.loginTimeout,
|
|
53
|
-
theme: args.theme || global.theme || DEFAULTS.theme,
|
|
54
|
-
yolo: args.yolo || process.env.PORTAL_YOLO === "true" || global.yolo || DEFAULTS.yolo,
|
|
55
|
-
verbose: args.verbose || global.verbose || DEFAULTS.verbose,
|
|
56
45
|
exec: args.exec,
|
|
57
|
-
session: args.session || null,
|
|
58
46
|
resume: args.resume || null
|
|
59
47
|
};
|
|
60
48
|
|
|
49
|
+
if (args.provider) config.provider = args.provider;
|
|
50
|
+
if (process.env.PORTAL_PROVIDER) config.provider = process.env.PORTAL_PROVIDER;
|
|
51
|
+
if (args.maxSteps) config.maxSteps = args.maxSteps;
|
|
52
|
+
if (args.timeout) config.timeout = args.timeout;
|
|
53
|
+
if (args.loginTimeout) config.loginTimeout = args.loginTimeout;
|
|
54
|
+
if (args.yolo || process.env.PORTAL_YOLO === "true") config.yolo = true;
|
|
55
|
+
if (args.verbose) config.verbose = true;
|
|
56
|
+
|
|
61
57
|
config.ws = resolve(config.ws);
|
|
62
58
|
if (!existsSync(config.ws)) {
|
|
63
59
|
throw new Error("workspace does not exist: " + config.ws);
|
|
@@ -68,9 +64,7 @@ export async function loadConfig(args) {
|
|
|
68
64
|
try {
|
|
69
65
|
const local = JSON.parse(readFileSync(localPath, "utf8"));
|
|
70
66
|
Object.assign(config, local);
|
|
71
|
-
} catch (e) {
|
|
72
|
-
// ignore malformed local config
|
|
73
|
-
}
|
|
67
|
+
} catch (e) {}
|
|
74
68
|
}
|
|
75
69
|
|
|
76
70
|
return config;
|
package/lib/deepthink.mjs
CHANGED
|
@@ -1,50 +1,40 @@
|
|
|
1
|
-
// Toggle DeepSeek's DeepThink reasoning mode. Without this, DeepSeek answers
|
|
2
|
-
// with the fast model and the output is shorter and less thorough.
|
|
3
|
-
//
|
|
4
|
-
// The button sits in the composer row with a label like "DeepThink" or
|
|
5
|
-
// "Deep Think". After clicking, it gains an active class or aria-pressed=true.
|
|
6
|
-
|
|
7
1
|
export async function enableDeepThink(page) {
|
|
8
2
|
if (process.env.PORTAL_DEEPTHINK === "off") return false;
|
|
9
3
|
|
|
10
|
-
const
|
|
4
|
+
const btns = page.locator("button, [role=button], div[role=button]");
|
|
11
5
|
let total = 0;
|
|
12
6
|
try {
|
|
13
|
-
total = await
|
|
7
|
+
total = await btns.count();
|
|
14
8
|
} catch (e) {
|
|
15
9
|
return false;
|
|
16
10
|
}
|
|
17
11
|
if (total === 0 || total > 400) return false;
|
|
18
12
|
|
|
19
13
|
for (let i = 0; i < total; i++) {
|
|
20
|
-
const
|
|
14
|
+
const b = btns.nth(i);
|
|
21
15
|
let text = "";
|
|
22
16
|
try {
|
|
23
|
-
text = ((await
|
|
17
|
+
text = ((await b.textContent()) || "").trim();
|
|
24
18
|
} catch (e) {
|
|
25
19
|
continue;
|
|
26
20
|
}
|
|
27
21
|
if (!text) continue;
|
|
28
22
|
|
|
29
|
-
const
|
|
23
|
+
const hit = text === "DeepThink" ||
|
|
30
24
|
text === "Deep Think" ||
|
|
31
|
-
text.indexOf("DeepThink") >= 0
|
|
32
|
-
text.indexOf("Deep Think") >= 0;
|
|
25
|
+
text.indexOf("DeepThink") >= 0;
|
|
33
26
|
|
|
34
|
-
if (!
|
|
27
|
+
if (!hit) continue;
|
|
35
28
|
|
|
36
29
|
let pressed = "";
|
|
37
30
|
let cls = "";
|
|
38
|
-
try { pressed = (await
|
|
39
|
-
try { cls = (await
|
|
40
|
-
|
|
41
|
-
const alreadyOn = pressed === "true" ||
|
|
42
|
-
/active|selected|on\b|enabled/i.test(cls);
|
|
31
|
+
try { pressed = (await b.getAttribute("aria-pressed")) || ""; } catch (e) {}
|
|
32
|
+
try { cls = (await b.getAttribute("class")) || ""; } catch (e) {}
|
|
43
33
|
|
|
44
|
-
if (
|
|
34
|
+
if (pressed === "true" || /active|selected|on\b/i.test(cls)) return true;
|
|
45
35
|
|
|
46
36
|
try {
|
|
47
|
-
await
|
|
37
|
+
await b.click({ timeout: 2000 });
|
|
48
38
|
await new Promise(function (r) { setTimeout(r, 400); });
|
|
49
39
|
return true;
|
|
50
40
|
} catch (e) {
|
package/lib/exec.mjs
CHANGED
|
@@ -1,3 +1,47 @@
|
|
|
1
1
|
import { launch } from "./browser.mjs";
|
|
2
2
|
import { getProvider } from "./providers.mjs";
|
|
3
|
-
import { waitForComposer } from "./wait
|
|
3
|
+
import { waitForComposer } from "./wait.mjs";
|
|
4
|
+
import { enableDeepThink } from "./deepthink.mjs";
|
|
5
|
+
import { runTurn } from "./turn.mjs";
|
|
6
|
+
import { setWorkspace } from "./tools/path.mjs";
|
|
7
|
+
import { setIgnore } from "./tools/walk.mjs";
|
|
8
|
+
import { loadInstructions } from "./instructions.mjs";
|
|
9
|
+
import { info, error } from "./log.mjs";
|
|
10
|
+
|
|
11
|
+
export async function runExec(config) {
|
|
12
|
+
const provider = getProvider(config.provider);
|
|
13
|
+
setWorkspace(config.ws);
|
|
14
|
+
setIgnore(config.ignored);
|
|
15
|
+
|
|
16
|
+
config.instructionsText = await loadInstructions(config.ws);
|
|
17
|
+
|
|
18
|
+
const ctx = await launch();
|
|
19
|
+
const page = await ctx.newPage();
|
|
20
|
+
page.setDefaultTimeout(30000);
|
|
21
|
+
|
|
22
|
+
info("opening " + provider.label + "...");
|
|
23
|
+
await page.goto(provider.url);
|
|
24
|
+
await waitForComposer(page, provider, config.loginTimeout);
|
|
25
|
+
|
|
26
|
+
if (provider.deepthink) await enableDeepThink(page);
|
|
27
|
+
|
|
28
|
+
const session = {
|
|
29
|
+
id: "exec-" + Date.now(),
|
|
30
|
+
ws: config.ws,
|
|
31
|
+
provider: config.provider,
|
|
32
|
+
turns: [],
|
|
33
|
+
approvedAlways: [],
|
|
34
|
+
startedAt: Date.now(),
|
|
35
|
+
updatedAt: Date.now()
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
await runTurn(page, provider, config.exec, config, session);
|
|
40
|
+
process.exitCode = 0;
|
|
41
|
+
} catch (e) {
|
|
42
|
+
error(e.message || String(e));
|
|
43
|
+
process.exitCode = 1;
|
|
44
|
+
} finally {
|
|
45
|
+
await ctx.close().catch(function () {});
|
|
46
|
+
}
|
|
47
|
+
}
|
package/lib/instructions.mjs
CHANGED
|
@@ -1 +1,44 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readFile, stat } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
const FILENAMES = ["PORTAL.md", "AGENTS.md", ".portal.md", "CLAUDE.md"];
|
|
5
|
+
|
|
6
|
+
let cache = null;
|
|
7
|
+
let cacheKey = "";
|
|
8
|
+
|
|
9
|
+
export async function loadInstructions(ws) {
|
|
10
|
+
for (const name of FILENAMES) {
|
|
11
|
+
const path = join(ws, name);
|
|
12
|
+
let info;
|
|
13
|
+
try {
|
|
14
|
+
info = await stat(path);
|
|
15
|
+
} catch (e) {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const key = path + ":" + info.mtimeMs;
|
|
20
|
+
if (cache && cacheKey === key) return cache;
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
const content = await readFile(path, "utf8");
|
|
24
|
+
const trimmed = content.trim();
|
|
25
|
+
if (!trimmed) return null;
|
|
26
|
+
cache = [
|
|
27
|
+
"# Project instructions",
|
|
28
|
+
"",
|
|
29
|
+
"The user placed these instructions in " + name + ". Follow them.",
|
|
30
|
+
"",
|
|
31
|
+
trimmed
|
|
32
|
+
].join("\n");
|
|
33
|
+
cacheKey = key;
|
|
34
|
+
return cache;
|
|
35
|
+
} catch (e) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function activeInstructionFile() {
|
|
43
|
+
return cacheKey ? cacheKey.split(":")[0] : null;
|
|
44
|
+
}
|
package/lib/log.mjs
CHANGED
package/lib/parse.mjs
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
// Tool call protocol:
|
|
2
|
-
//
|
|
3
|
-
// portal-tool: {"tool":"name","args":{"key":"value"}}
|
|
4
|
-
//
|
|
5
|
-
// Written as plain text on its own line. The parser scans for the literal
|
|
6
|
-
// string "portal-tool", finds the balanced JSON object that follows, and
|
|
7
|
-
// tries to parse it. If parse fails, common Windows backslash escapes are
|
|
8
|
-
// repaired first (models copy "L:\this\file" and JSON sees \t, \h, \w as
|
|
9
|
-
// escapes that blow up the parse).
|
|
10
|
-
|
|
11
1
|
function findJsonEnd(s, i) {
|
|
12
2
|
let depth = 0;
|
|
13
3
|
let inStr = false;
|
|
@@ -17,4 +7,45 @@ function findJsonEnd(s, i) {
|
|
|
17
7
|
if (esc) { esc = false; continue; }
|
|
18
8
|
if (c === "\\") { esc = true; continue; }
|
|
19
9
|
if (c === String.fromCharCode(34)) { inStr = !inStr; continue; }
|
|
20
|
-
if (inStr
|
|
10
|
+
if (inStr) continue;
|
|
11
|
+
if (c === "{") depth++;
|
|
12
|
+
else if (c === "}") {
|
|
13
|
+
depth--;
|
|
14
|
+
if (!depth) return j + 1;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return -1;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function repair(raw) {
|
|
21
|
+
let s = raw;
|
|
22
|
+
const bad = ["\\t", "\\h", "\\w", "\\d", "\\p"];
|
|
23
|
+
for (const b of bad) s = s.split(b).join("/");
|
|
24
|
+
return s;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function parseCalls(text) {
|
|
28
|
+
const out = [];
|
|
29
|
+
const seen = new Set();
|
|
30
|
+
const re = /portal-tool[:\s]*/g;
|
|
31
|
+
let m;
|
|
32
|
+
while ((m = re.exec(text))) {
|
|
33
|
+
const b = text.indexOf("{", m.index + m[0].length);
|
|
34
|
+
if (b < 0) continue;
|
|
35
|
+
const e = findJsonEnd(text, b);
|
|
36
|
+
if (e < 0) continue;
|
|
37
|
+
const raw = text.slice(b, e);
|
|
38
|
+
if (seen.has(raw)) continue;
|
|
39
|
+
seen.add(raw);
|
|
40
|
+
try {
|
|
41
|
+
out.push(JSON.parse(raw));
|
|
42
|
+
} catch (x) {
|
|
43
|
+
try { out.push(JSON.parse(repair(raw))); } catch (y) {}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function stripCalls(text) {
|
|
50
|
+
return text.replace(/portal-tool[\s\S]*?\n\}/g, "").trim();
|
|
51
|
+
}
|
package/lib/paste.mjs
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import { sleep } from "./wait.mjs";
|
|
2
2
|
|
|
3
|
-
// DeepSeek shows a Cancel/Send confirmation bar when a large paste is
|
|
4
|
-
// detected. Find the Cancel button, locate its Send sibling, click that.
|
|
5
3
|
export async function dismissPaste(page) {
|
|
6
4
|
const btns = page.locator("button, [role=button]");
|
|
7
5
|
let total = 0;
|
|
8
6
|
try {
|
|
9
7
|
total = await btns.count();
|
|
10
8
|
} catch (e) {
|
|
11
|
-
return;
|
|
9
|
+
return false;
|
|
12
10
|
}
|
|
13
|
-
if (total === 0 || total > 300) return;
|
|
11
|
+
if (total === 0 || total > 300) return false;
|
|
14
12
|
|
|
15
13
|
for (let i = 0; i < total; i++) {
|
|
16
14
|
const b = btns.nth(i);
|
|
@@ -36,18 +34,14 @@ export async function dismissPaste(page) {
|
|
|
36
34
|
await sib.click({ timeout: 1500 });
|
|
37
35
|
await sleep(200);
|
|
38
36
|
return true;
|
|
39
|
-
} catch (e) {
|
|
40
|
-
// fall through
|
|
41
|
-
}
|
|
37
|
+
} catch (e) {}
|
|
42
38
|
}
|
|
43
39
|
}
|
|
44
40
|
|
|
45
41
|
try {
|
|
46
42
|
await page.keyboard.press("Enter");
|
|
47
43
|
await sleep(150);
|
|
48
|
-
} catch (e) {
|
|
49
|
-
// ignore
|
|
50
|
-
}
|
|
44
|
+
} catch (e) {}
|
|
51
45
|
return true;
|
|
52
46
|
}
|
|
53
47
|
return false;
|
package/lib/providers.mjs
CHANGED
|
@@ -15,7 +15,7 @@ export const PROVIDERS = {
|
|
|
15
15
|
url: "https://chatgpt.com/",
|
|
16
16
|
input: ["#prompt-textarea", "div[contenteditable=true][id=prompt-textarea]"],
|
|
17
17
|
submit: ["button[data-testid=send-button]", "button[aria-label*=Send]"],
|
|
18
|
-
response: ["[data-message-author-role=assistant]"
|
|
18
|
+
response: ["[data-message-author-role=assistant]"],
|
|
19
19
|
paste: false,
|
|
20
20
|
deepthink: false
|
|
21
21
|
},
|
|
@@ -23,9 +23,9 @@ export const PROVIDERS = {
|
|
|
23
23
|
id: "gemini",
|
|
24
24
|
label: "Gemini",
|
|
25
25
|
url: "https://gemini.google.com/app",
|
|
26
|
-
input: ["div[contenteditable=true][role=textbox]"
|
|
27
|
-
submit: ["button[aria-label*=Send]"
|
|
28
|
-
response: ["message-content"
|
|
26
|
+
input: ["div[contenteditable=true][role=textbox]"],
|
|
27
|
+
submit: ["button[aria-label*=Send]"],
|
|
28
|
+
response: ["message-content"],
|
|
29
29
|
paste: false,
|
|
30
30
|
deepthink: false
|
|
31
31
|
},
|
|
@@ -34,8 +34,8 @@ export const PROVIDERS = {
|
|
|
34
34
|
label: "Grok",
|
|
35
35
|
url: "https://grok.com/",
|
|
36
36
|
input: ["textarea", "div[contenteditable=true]"],
|
|
37
|
-
submit: ["button[type=submit]"
|
|
38
|
-
response: ["div[class*=message]"
|
|
37
|
+
submit: ["button[type=submit]"],
|
|
38
|
+
response: ["div[class*=message]"],
|
|
39
39
|
paste: false,
|
|
40
40
|
deepthink: false
|
|
41
41
|
}
|
|
@@ -45,7 +45,7 @@ export function getProvider(id) {
|
|
|
45
45
|
const p = PROVIDERS[id];
|
|
46
46
|
if (!p) {
|
|
47
47
|
const names = Object.keys(PROVIDERS).join(", ");
|
|
48
|
-
throw new Error("unknown provider: " + id + "
|
|
48
|
+
throw new Error("unknown provider: " + id + " | available: " + names);
|
|
49
49
|
}
|
|
50
50
|
return p;
|
|
51
51
|
}
|