@firefunc-agent/runner 0.9.14 → 0.9.35
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/README.md +184 -10
- package/dist/alive.d.ts +40 -0
- package/dist/alive.js +138 -0
- package/dist/api-fallback.d.ts +4 -2
- package/dist/api-fallback.js +26 -1
- package/dist/api.d.ts +66 -3
- package/dist/api.js +164 -3
- package/dist/brief-file.d.ts +15 -0
- package/dist/brief-file.js +36 -0
- package/dist/brief.d.ts +3 -0
- package/dist/brief.js +14 -4
- package/dist/build-caches.d.ts +7 -0
- package/dist/build-caches.js +63 -0
- package/dist/build-info.json +2 -2
- package/dist/checkpoint.d.ts +40 -0
- package/dist/checkpoint.js +98 -0
- package/dist/cli.js +184 -18
- package/dist/cloud-boot.d.ts +41 -0
- package/dist/cloud-boot.js +239 -0
- package/dist/command-activity.d.ts +23 -0
- package/dist/command-activity.js +89 -0
- package/dist/command-step.d.ts +58 -0
- package/dist/command-step.js +248 -0
- package/dist/config.d.ts +39 -0
- package/dist/config.js +23 -0
- package/dist/cut.d.ts +1 -1
- package/dist/cut.js +13 -3
- package/dist/daemon.js +364 -15
- package/dist/device-lease.d.ts +15 -0
- package/dist/device-lease.js +608 -0
- package/dist/editor/fs-server.d.ts +31 -0
- package/dist/editor/fs-server.js +159 -0
- package/dist/editor/relay-crypto.d.ts +68 -0
- package/dist/editor/relay-crypto.js +191 -0
- package/dist/editor/sessions.d.ts +50 -0
- package/dist/editor/sessions.js +279 -0
- package/dist/editor/worktree-of.d.ts +1 -0
- package/dist/editor/worktree-of.js +23 -0
- package/dist/editor/ws-client.d.ts +26 -0
- package/dist/editor/ws-client.js +227 -0
- package/dist/engine-account.d.ts +24 -0
- package/dist/engine-account.js +118 -0
- package/dist/engine-login.d.ts +12 -0
- package/dist/engine-login.js +88 -0
- package/dist/engine-models.d.ts +13 -0
- package/dist/engine-models.js +71 -0
- package/dist/engine-watch.d.ts +1 -0
- package/dist/engine-watch.js +22 -6
- package/dist/engines/antigravity.d.ts +2 -0
- package/dist/engines/antigravity.js +74 -4
- package/dist/engines/claude.js +22 -9
- package/dist/engines/codex.js +16 -2
- package/dist/engines/copilot.js +12 -0
- package/dist/engines/crush.d.ts +2 -0
- package/dist/engines/crush.js +58 -0
- package/dist/engines/cursor.js +12 -0
- package/dist/engines/gemini.js +14 -9
- package/dist/engines/index.d.ts +8 -0
- package/dist/engines/index.js +73 -1
- package/dist/engines/kilo.d.ts +2 -0
- package/dist/engines/kilo.js +63 -0
- package/dist/engines/opencode.d.ts +9 -0
- package/dist/engines/opencode.js +234 -0
- package/dist/engines/openhands.d.ts +10 -0
- package/dist/engines/openhands.js +271 -0
- package/dist/engines/pi.d.ts +10 -0
- package/dist/engines/pi.js +248 -0
- package/dist/engines/qwen.d.ts +8 -0
- package/dist/engines/qwen.js +153 -0
- package/dist/engines/sessions.d.ts +6 -0
- package/dist/engines/sessions.js +68 -0
- package/dist/engines/types.d.ts +3 -3
- package/dist/exec.d.ts +1 -0
- package/dist/exec.js +10 -4
- package/dist/firefunc/agents.d.ts +8 -1
- package/dist/firefunc/agents.js +53 -3
- package/dist/firefunc/auth.js +72 -19
- package/dist/firefunc/automations.d.ts +2 -0
- package/dist/firefunc/automations.js +290 -0
- package/dist/firefunc/cli.js +25 -0
- package/dist/firefunc/client.d.ts +5 -1
- package/dist/firefunc/client.js +12 -2
- package/dist/firefunc/config.d.ts +11 -1
- package/dist/firefunc/config.js +6 -4
- package/dist/firefunc/io.d.ts +1 -0
- package/dist/firefunc/main.js +34 -2
- package/dist/firefunc/oauth-login.d.ts +23 -0
- package/dist/firefunc/oauth-login.js +304 -0
- package/dist/firefunc/tasks.d.ts +4 -0
- package/dist/firefunc/tasks.js +89 -2
- package/dist/fleet-words.d.ts +2 -0
- package/dist/fleet-words.js +7 -0
- package/dist/git-auth.d.ts +15 -0
- package/dist/git-auth.js +39 -1
- package/dist/held.d.ts +3 -0
- package/dist/job.d.ts +26 -3
- package/dist/job.js +763 -152
- package/dist/keep-awake.d.ts +17 -0
- package/dist/keep-awake.js +90 -0
- package/dist/keep-local.d.ts +1 -1
- package/dist/keep-local.js +5 -3
- package/dist/limit.d.ts +9 -1
- package/dist/limit.js +260 -3
- package/dist/local-checks.d.ts +19 -0
- package/dist/local-checks.js +79 -0
- package/dist/locks.d.ts +18 -1
- package/dist/locks.js +249 -69
- package/dist/machine-commands.d.ts +21 -0
- package/dist/machine-commands.js +107 -0
- package/dist/machine-profile.d.ts +18 -0
- package/dist/machine-profile.js +69 -0
- package/dist/mcp-init.d.ts +2 -0
- package/dist/mcp-init.js +28 -0
- package/dist/mcp.d.ts +13 -1
- package/dist/mcp.js +48 -4
- package/dist/network.js +3 -2
- package/dist/openrouter.d.ts +15 -0
- package/dist/openrouter.js +39 -0
- package/dist/orphans.d.ts +4 -0
- package/dist/orphans.js +96 -4
- package/dist/pool.d.ts +1 -0
- package/dist/pool.js +25 -2
- package/dist/pr-title.d.ts +10 -0
- package/dist/pr-title.js +15 -0
- package/dist/recover.d.ts +3 -1
- package/dist/recover.js +27 -7
- package/dist/release-draft.d.ts +79 -0
- package/dist/release-draft.js +301 -0
- package/dist/resources.d.ts +86 -0
- package/dist/resources.js +217 -0
- package/dist/restart-idle.d.ts +23 -0
- package/dist/restart-idle.js +67 -0
- package/dist/run-attachments.d.ts +5 -0
- package/dist/run-attachments.js +14 -9
- package/dist/run-stops.d.ts +15 -0
- package/dist/run-stops.js +90 -0
- package/dist/run-temp.d.ts +12 -0
- package/dist/run-temp.js +57 -0
- package/dist/secret-scan.d.ts +5 -0
- package/dist/secret-scan.js +30 -0
- package/dist/self-upgrade.d.ts +36 -0
- package/dist/self-upgrade.js +78 -0
- package/dist/service.d.ts +7 -1
- package/dist/service.js +50 -7
- package/dist/sign-in.d.ts +42 -0
- package/dist/sign-in.js +243 -0
- package/dist/tool-call.d.ts +59 -0
- package/dist/tool-call.js +375 -0
- package/dist/transport.d.ts +26 -0
- package/dist/transport.js +118 -0
- package/dist/work-dir.d.ts +11 -0
- package/dist/work-dir.js +69 -0
- package/dist/worktree-report.d.ts +10 -0
- package/dist/worktree-report.js +17 -0
- package/dist/worktrees.d.ts +16 -1
- package/dist/worktrees.js +62 -17
- package/package.json +4 -4
- package/skill/firefunc/SKILL.md +22 -3
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { readWordedLimit } from '../limit.js';
|
|
5
|
+
import { ENGINE_SESSION_ID } from './sessions.js';
|
|
6
|
+
export const OPENHANDS_LLM_VARS = ['LLM_API_KEY', 'LLM_BASE_URL', 'LLM_MODEL'];
|
|
7
|
+
const isObj = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
8
|
+
const str = (v) => (typeof v === 'string' ? v : '');
|
|
9
|
+
const ANSI = /\u001b\[[0-9;]*m/g;
|
|
10
|
+
function* events(stdout) {
|
|
11
|
+
for (const line of stdout.split('\n')) {
|
|
12
|
+
const t = line.replace(ANSI, '').trim();
|
|
13
|
+
if (!t.startsWith('{'))
|
|
14
|
+
continue;
|
|
15
|
+
try {
|
|
16
|
+
const ev = JSON.parse(t);
|
|
17
|
+
if (isObj(ev) && typeof ev.kind === 'string')
|
|
18
|
+
yield ev;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function textOf(content) {
|
|
25
|
+
if (typeof content === 'string')
|
|
26
|
+
return content.trim();
|
|
27
|
+
if (!Array.isArray(content))
|
|
28
|
+
return '';
|
|
29
|
+
return content
|
|
30
|
+
.map((c) => (isObj(c) && typeof c.text === 'string' ? c.text : ''))
|
|
31
|
+
.join('\n')
|
|
32
|
+
.trim();
|
|
33
|
+
}
|
|
34
|
+
function agentMessage(ev) {
|
|
35
|
+
if (ev.kind !== 'MessageEvent' || ev.source !== 'agent')
|
|
36
|
+
return '';
|
|
37
|
+
const msg = isObj(ev.llm_message) ? ev.llm_message : {};
|
|
38
|
+
return textOf(msg.content);
|
|
39
|
+
}
|
|
40
|
+
function finishMessage(ev) {
|
|
41
|
+
if (ev.kind !== 'ActionEvent')
|
|
42
|
+
return null;
|
|
43
|
+
const action = isObj(ev.action) ? ev.action : {};
|
|
44
|
+
if (ev.tool_name !== 'finish' && action.kind !== 'FinishAction')
|
|
45
|
+
return null;
|
|
46
|
+
return str(action.message).trim();
|
|
47
|
+
}
|
|
48
|
+
function errorWords(ev) {
|
|
49
|
+
if (ev.kind === 'ConversationErrorEvent') {
|
|
50
|
+
const code = str(ev.code);
|
|
51
|
+
const detail = str(ev.detail).trim();
|
|
52
|
+
return (detail ? `${code ? `${code}: ` : ''}${detail}` : code || 'OpenHands error').trim();
|
|
53
|
+
}
|
|
54
|
+
if (ev.kind === 'ConversationStateUpdateEvent' && ev.key === 'execution_status') {
|
|
55
|
+
if (ev.value === 'error')
|
|
56
|
+
return 'OpenHands stopped on an error';
|
|
57
|
+
if (ev.value === 'stuck')
|
|
58
|
+
return 'OpenHands stopped: the agent was stuck in a loop';
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
export function openhandsEnding(stdout) {
|
|
63
|
+
try {
|
|
64
|
+
let ending;
|
|
65
|
+
for (const ev of events(stdout)) {
|
|
66
|
+
const err = errorWords(ev);
|
|
67
|
+
if (err)
|
|
68
|
+
ending = { error: err, refusal: false };
|
|
69
|
+
else if (finishMessage(ev) !== null || agentMessage(ev)) {
|
|
70
|
+
ending = { error: null, refusal: false };
|
|
71
|
+
}
|
|
72
|
+
else if (ev.kind === 'ConversationStateUpdateEvent' &&
|
|
73
|
+
ev.key === 'execution_status' &&
|
|
74
|
+
ev.value === 'finished') {
|
|
75
|
+
ending = { error: null, refusal: false };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return ending;
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export function openhandsSummary(stdout) {
|
|
85
|
+
let answer = '';
|
|
86
|
+
let error = '';
|
|
87
|
+
let any = false;
|
|
88
|
+
for (const ev of events(stdout)) {
|
|
89
|
+
any = true;
|
|
90
|
+
const err = errorWords(ev);
|
|
91
|
+
const fin = finishMessage(ev);
|
|
92
|
+
const said = agentMessage(ev);
|
|
93
|
+
if (err)
|
|
94
|
+
error = err;
|
|
95
|
+
else if (fin) {
|
|
96
|
+
answer = fin;
|
|
97
|
+
error = '';
|
|
98
|
+
}
|
|
99
|
+
else if (said) {
|
|
100
|
+
answer = said;
|
|
101
|
+
error = '';
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (error)
|
|
105
|
+
return error;
|
|
106
|
+
if (answer)
|
|
107
|
+
return answer;
|
|
108
|
+
const tail = stdout.replace(ANSI, '').trim().split('\n').slice(-8).join('\n').trim();
|
|
109
|
+
return any ? tail.slice(0, 800) : tail.slice(0, 2000);
|
|
110
|
+
}
|
|
111
|
+
export function openhandsSessionId(stdout) {
|
|
112
|
+
try {
|
|
113
|
+
let any = false;
|
|
114
|
+
for (const _ of events(stdout)) {
|
|
115
|
+
any = true;
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
if (!any)
|
|
119
|
+
return null;
|
|
120
|
+
const m = /Conversation ID:\s*([0-9a-fA-F-]{32,36})\b/.exec(stdout.replace(ANSI, ''));
|
|
121
|
+
const id = m?.[1] ?? null;
|
|
122
|
+
return id && ENGINE_SESSION_ID.test(id) ? id : null;
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
export function openhandsEnv(runEnv, model, host = process.env) {
|
|
129
|
+
const out = { ...runEnv };
|
|
130
|
+
for (const v of OPENHANDS_LLM_VARS) {
|
|
131
|
+
const has = v in runEnv ? runEnv[v] !== undefined : host[v] !== undefined;
|
|
132
|
+
if (!has)
|
|
133
|
+
out[v] = '';
|
|
134
|
+
}
|
|
135
|
+
if (model)
|
|
136
|
+
out.LLM_MODEL = model;
|
|
137
|
+
out.OPENHANDS_SUPPRESS_BANNER = '1';
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
export function openhandsMissingVars(text) {
|
|
141
|
+
const m = /missing required environment variable\(s\):\s*([A-Z0-9_, ]+)/i.exec(text);
|
|
142
|
+
if (!m)
|
|
143
|
+
return null;
|
|
144
|
+
const vars = m[1]
|
|
145
|
+
.split(',')
|
|
146
|
+
.map((v) => v.trim())
|
|
147
|
+
.filter((v) => /^LLM_[A-Z_]+$/.test(v));
|
|
148
|
+
return vars.length ? vars : null;
|
|
149
|
+
}
|
|
150
|
+
function settingsFile(env) {
|
|
151
|
+
const home = env.HOME || env.USERPROFILE || homedir();
|
|
152
|
+
const dir = env.OPENHANDS_PERSISTENCE_DIR || join(home, '.openhands');
|
|
153
|
+
return join(dir, 'agent_settings.json');
|
|
154
|
+
}
|
|
155
|
+
export function openhandsUnattendedBlock(ctx, exists = existsSync) {
|
|
156
|
+
try {
|
|
157
|
+
if (ctx.env.LLM_API_KEY)
|
|
158
|
+
return null;
|
|
159
|
+
if (exists(settingsFile(ctx.env)))
|
|
160
|
+
return null;
|
|
161
|
+
const reason = 'OpenHands has no model or key for this user: there is no ~/.openhands/agent_settings.json and LLM_API_KEY is not set, and headless OpenHands stops before any work without one.';
|
|
162
|
+
const fix = 'On the machine, run `openhands` once as the user the runner runs as and save a model and key, or set LLM_API_KEY (and LLM_MODEL) in the runner’s environment.';
|
|
163
|
+
return {
|
|
164
|
+
code: 'not_signed_in',
|
|
165
|
+
reason,
|
|
166
|
+
fix,
|
|
167
|
+
detail: 'OpenHands has no model or key here — run `openhands` once, or set LLM_API_KEY.',
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
export const openhandsAdapter = {
|
|
175
|
+
id: 'openhands',
|
|
176
|
+
displayName: 'OpenHands',
|
|
177
|
+
branchPrefix: 'openhands',
|
|
178
|
+
bin(cfg) {
|
|
179
|
+
return cfg.openhandsBin ?? 'openhands';
|
|
180
|
+
},
|
|
181
|
+
buildInvocation(ctx) {
|
|
182
|
+
const cfg = ctx.cfg;
|
|
183
|
+
const args = ['--headless', '--json', '--override-with-envs'];
|
|
184
|
+
if (ctx.resumeSessionId && ENGINE_SESSION_ID.test(ctx.resumeSessionId)) {
|
|
185
|
+
args.push('--resume', ctx.resumeSessionId);
|
|
186
|
+
}
|
|
187
|
+
args.push(...(cfg.openhandsArgs ?? []));
|
|
188
|
+
args.push('-t', ctx.prompt);
|
|
189
|
+
const provider = ctx.model?.includes('/') ? ctx.model.split('/')[0] : null;
|
|
190
|
+
return {
|
|
191
|
+
bin: this.bin(cfg),
|
|
192
|
+
args,
|
|
193
|
+
env: openhandsEnv(ctx.runEnv, ctx.model),
|
|
194
|
+
shell: ctx.isWin,
|
|
195
|
+
authLogLine: provider
|
|
196
|
+
? `OpenHands with ${provider} — its key from the machine (LLM_API_KEY or openhands settings)`
|
|
197
|
+
: 'OpenHands with its own saved model — key from the machine (openhands settings or LLM_API_KEY)',
|
|
198
|
+
};
|
|
199
|
+
},
|
|
200
|
+
makeStreamPrinter() {
|
|
201
|
+
const short = (s, n = 160) => {
|
|
202
|
+
const one = s.replace(/\s+/g, ' ').trim();
|
|
203
|
+
return one.length > n ? `${one.slice(0, n)}…` : one;
|
|
204
|
+
};
|
|
205
|
+
let buf = '';
|
|
206
|
+
return (chunk) => {
|
|
207
|
+
buf += chunk;
|
|
208
|
+
const lines = buf.split('\n');
|
|
209
|
+
buf = lines.pop() ?? '';
|
|
210
|
+
for (const ev of events(lines.join('\n'))) {
|
|
211
|
+
const err = errorWords(ev);
|
|
212
|
+
const said = agentMessage(ev);
|
|
213
|
+
if (err)
|
|
214
|
+
process.stdout.write(` ✗ ${short(err, 240)}\n`);
|
|
215
|
+
else if (said)
|
|
216
|
+
process.stdout.write(` │ ${short(said)}\n`);
|
|
217
|
+
else if (ev.kind === 'ActionEvent' && typeof ev.tool_name === 'string') {
|
|
218
|
+
const fin = finishMessage(ev);
|
|
219
|
+
process.stdout.write(fin !== null ? ` ✓ ${short(fin)}\n` : ` ⏺ ${ev.tool_name}\n`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
},
|
|
224
|
+
summarize(stdout) {
|
|
225
|
+
return openhandsSummary(stdout);
|
|
226
|
+
},
|
|
227
|
+
authFailureHint(text) {
|
|
228
|
+
const missing = openhandsMissingVars(text);
|
|
229
|
+
if (missing) {
|
|
230
|
+
return `OpenHands stopped before any work: it needs LLM_API_KEY and LLM_MODEL, or the settings saved by running \`openhands\` once, and on this machine ${missing.join(' and ')} ${missing.length === 1 ? 'is' : 'are'} not set. Fix: on the machine, set LLM_API_KEY (your model provider's key) in the runner's environment and pick the model on the agent or the project (FireFunc passes it as LLM_MODEL, a LiteLLM id such as anthropic/claude-sonnet-4-5-20250929), or run \`openhands\` once as the user the runner runs as and save a model and key; then retry. FireFunc never holds the key.`;
|
|
231
|
+
}
|
|
232
|
+
const t = text.toLowerCase();
|
|
233
|
+
if (t.includes('headless mode requires existing settings') ||
|
|
234
|
+
t.includes('authenticationerror') ||
|
|
235
|
+
t.includes('invalid x-api-key') ||
|
|
236
|
+
t.includes('invalid api key') ||
|
|
237
|
+
t.includes('incorrect api key') ||
|
|
238
|
+
/\b401\b/.test(t) ||
|
|
239
|
+
/\bunauthorized\b/.test(t)) {
|
|
240
|
+
return 'OpenHands could not reach its model with a key. On the machine, run `openhands` once and save a model and key, or set LLM_API_KEY (and LLM_MODEL, a LiteLLM id such as anthropic/claude-sonnet-4-5-20250929) in the runner’s environment, then retry. FireFunc never holds the key.';
|
|
241
|
+
}
|
|
242
|
+
if (t.includes('notfounderror') || t.includes('llm provider not provided')) {
|
|
243
|
+
return 'OpenHands does not know this model. Send it as a LiteLLM id, provider/model (anthropic/claude-sonnet-4-5-20250929, openai/gpt-5.5), on the agent or the project.';
|
|
244
|
+
}
|
|
245
|
+
return null;
|
|
246
|
+
},
|
|
247
|
+
rateLimitHint(text) {
|
|
248
|
+
const t = text.toLowerCase();
|
|
249
|
+
if (/ratelimiterror|rate limit|too many requests|\b429\b|quota exceeded|insufficient credits|\b402\b/.test(t)) {
|
|
250
|
+
return 'OpenHands’ model provider hit a rate, quota or credit limit. This run was throttled, not failed: retry shortly, top up the provider’s credit, or pick another model.';
|
|
251
|
+
}
|
|
252
|
+
return null;
|
|
253
|
+
},
|
|
254
|
+
unattendedBlock(ctx) {
|
|
255
|
+
return openhandsUnattendedBlock(ctx);
|
|
256
|
+
},
|
|
257
|
+
ending(stdout) {
|
|
258
|
+
return openhandsEnding(stdout);
|
|
259
|
+
},
|
|
260
|
+
planLimit(stdout, now, stderr) {
|
|
261
|
+
const ending = openhandsEnding(stdout);
|
|
262
|
+
if (ending && ending.error === null)
|
|
263
|
+
return null;
|
|
264
|
+
return readWordedLimit(ending?.error ?? null, stderr ?? '', now);
|
|
265
|
+
},
|
|
266
|
+
resumesSessions: true,
|
|
267
|
+
sessionId(stdout) {
|
|
268
|
+
return openhandsSessionId(stdout);
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
//# sourceMappingURL=openhands.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AgentAdapter } from './types.js';
|
|
2
|
+
import type { EngineEnding } from '../declined.js';
|
|
3
|
+
import type { RunUsage } from '../usage.js';
|
|
4
|
+
export declare const PI_TOOLS = "read,bash,edit,write,grep,find,ls";
|
|
5
|
+
export declare function piEnding(stdout: string): EngineEnding | undefined;
|
|
6
|
+
export declare function piSummary(stdout: string): string;
|
|
7
|
+
export declare function piUsage(stdout: string): RunUsage | null;
|
|
8
|
+
export declare function piSessionId(stdout: string): string | null;
|
|
9
|
+
export declare function piThinking(effort: string | null, ultracode: boolean): string | null;
|
|
10
|
+
export declare const piAdapter: AgentAdapter;
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { readWordedLimit } from '../limit.js';
|
|
2
|
+
import { ENGINE_SESSION_ID } from './sessions.js';
|
|
3
|
+
const isObj = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
4
|
+
export const PI_TOOLS = 'read,bash,edit,write,grep,find,ls';
|
|
5
|
+
function* events(stdout) {
|
|
6
|
+
for (const line of stdout.split('\n')) {
|
|
7
|
+
const t = line.trim();
|
|
8
|
+
if (!t.startsWith('{'))
|
|
9
|
+
continue;
|
|
10
|
+
try {
|
|
11
|
+
const ev = JSON.parse(t);
|
|
12
|
+
if (isObj(ev) && typeof ev.type === 'string')
|
|
13
|
+
yield ev;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function assistantMessages(stdout) {
|
|
20
|
+
const out = [];
|
|
21
|
+
for (const ev of events(stdout)) {
|
|
22
|
+
if (ev.type !== 'message_end' || !isObj(ev.message))
|
|
23
|
+
continue;
|
|
24
|
+
if (ev.message.role === 'assistant')
|
|
25
|
+
out.push(ev.message);
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
function textOf(message) {
|
|
30
|
+
const content = Array.isArray(message.content) ? message.content : [];
|
|
31
|
+
return content
|
|
32
|
+
.filter((c) => isObj(c) && c.type === 'text' && typeof c.text === 'string')
|
|
33
|
+
.map((c) => c.text)
|
|
34
|
+
.join('')
|
|
35
|
+
.trim();
|
|
36
|
+
}
|
|
37
|
+
export function piEnding(stdout) {
|
|
38
|
+
try {
|
|
39
|
+
const msgs = assistantMessages(stdout);
|
|
40
|
+
const last = msgs[msgs.length - 1];
|
|
41
|
+
if (!last)
|
|
42
|
+
return undefined;
|
|
43
|
+
if (last.stopReason === 'error' || last.stopReason === 'aborted') {
|
|
44
|
+
const words = typeof last.errorMessage === 'string' && last.errorMessage.trim()
|
|
45
|
+
? last.errorMessage.trim()
|
|
46
|
+
: `Pi ended its session: ${String(last.stopReason)}`;
|
|
47
|
+
return { error: words, refusal: false };
|
|
48
|
+
}
|
|
49
|
+
if (last.stopReason === 'stop' || last.stopReason === 'length') {
|
|
50
|
+
return { error: null, refusal: false };
|
|
51
|
+
}
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export function piSummary(stdout) {
|
|
59
|
+
const ending = piEnding(stdout);
|
|
60
|
+
if (ending?.error)
|
|
61
|
+
return ending.error;
|
|
62
|
+
const msgs = assistantMessages(stdout);
|
|
63
|
+
for (let i = msgs.length - 1; i >= 0; i--) {
|
|
64
|
+
const t = textOf(msgs[i]);
|
|
65
|
+
if (t)
|
|
66
|
+
return t;
|
|
67
|
+
}
|
|
68
|
+
return stdout.trim().split('\n').slice(-8).join('\n').trim().slice(0, 800);
|
|
69
|
+
}
|
|
70
|
+
const count = (v) => typeof v === 'number' && Number.isFinite(v) && v >= 0 ? v : 0;
|
|
71
|
+
export function piUsage(stdout) {
|
|
72
|
+
try {
|
|
73
|
+
const msgs = assistantMessages(stdout);
|
|
74
|
+
if (!msgs.length)
|
|
75
|
+
return null;
|
|
76
|
+
const models = {};
|
|
77
|
+
let input = 0;
|
|
78
|
+
let output = 0;
|
|
79
|
+
let cacheRead = 0;
|
|
80
|
+
let cacheWrite = 0;
|
|
81
|
+
let cost = 0;
|
|
82
|
+
for (const m of msgs) {
|
|
83
|
+
const u = isObj(m.usage) ? m.usage : {};
|
|
84
|
+
const c = isObj(u.cost) ? u.cost : {};
|
|
85
|
+
const line = {
|
|
86
|
+
input: count(u.input),
|
|
87
|
+
output: count(u.output),
|
|
88
|
+
cacheRead: count(u.cacheRead),
|
|
89
|
+
cacheWrite: count(u.cacheWrite),
|
|
90
|
+
cost: count(c.total),
|
|
91
|
+
};
|
|
92
|
+
input += line.input;
|
|
93
|
+
output += line.output;
|
|
94
|
+
cacheRead += line.cacheRead;
|
|
95
|
+
cacheWrite += line.cacheWrite;
|
|
96
|
+
cost += line.cost;
|
|
97
|
+
const key = typeof m.provider === 'string' && typeof m.model === 'string'
|
|
98
|
+
? `${m.provider}/${m.model}`
|
|
99
|
+
: null;
|
|
100
|
+
if (!key)
|
|
101
|
+
continue;
|
|
102
|
+
const prev = models[key] ?? { input_tokens: 0, output_tokens: 0 };
|
|
103
|
+
models[key] = {
|
|
104
|
+
input_tokens: prev.input_tokens + line.input,
|
|
105
|
+
output_tokens: prev.output_tokens + line.output,
|
|
106
|
+
...(line.cacheRead || prev.cache_read_input_tokens
|
|
107
|
+
? { cache_read_input_tokens: (prev.cache_read_input_tokens ?? 0) + line.cacheRead }
|
|
108
|
+
: {}),
|
|
109
|
+
...(line.cacheWrite || prev.cache_creation_input_tokens
|
|
110
|
+
? {
|
|
111
|
+
cache_creation_input_tokens: (prev.cache_creation_input_tokens ?? 0) + line.cacheWrite,
|
|
112
|
+
}
|
|
113
|
+
: {}),
|
|
114
|
+
...(line.cost || prev.cost_usd ? { cost_usd: (prev.cost_usd ?? 0) + line.cost } : {}),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
input_tokens: input,
|
|
119
|
+
output_tokens: output,
|
|
120
|
+
...(cacheRead ? { cache_read_input_tokens: cacheRead } : {}),
|
|
121
|
+
...(cacheWrite ? { cache_creation_input_tokens: cacheWrite } : {}),
|
|
122
|
+
total_cost_usd: Math.round(cost * 1e6) / 1e6,
|
|
123
|
+
num_turns: msgs.length,
|
|
124
|
+
...(Object.keys(models).length ? { models } : {}),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
export function piSessionId(stdout) {
|
|
132
|
+
try {
|
|
133
|
+
for (const ev of events(stdout)) {
|
|
134
|
+
if (ev.type === 'session' && typeof ev.id === 'string' && ENGINE_SESSION_ID.test(ev.id)) {
|
|
135
|
+
return ev.id;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
export function piThinking(effort, ultracode) {
|
|
144
|
+
if (ultracode || effort === 'ultracode' || effort === 'max' || effort === 'xhigh')
|
|
145
|
+
return 'xhigh';
|
|
146
|
+
return effort === 'low' || effort === 'medium' || effort === 'high' ? effort : null;
|
|
147
|
+
}
|
|
148
|
+
export const piAdapter = {
|
|
149
|
+
id: 'pi',
|
|
150
|
+
displayName: 'Pi',
|
|
151
|
+
branchPrefix: 'pi',
|
|
152
|
+
bin(cfg) {
|
|
153
|
+
return cfg.piBin ?? 'pi';
|
|
154
|
+
},
|
|
155
|
+
buildInvocation(ctx) {
|
|
156
|
+
const { cfg } = ctx;
|
|
157
|
+
const args = ['-p', '--mode', 'json', '--tools', PI_TOOLS];
|
|
158
|
+
if (ctx.model)
|
|
159
|
+
args.push('--model', ctx.model);
|
|
160
|
+
const thinking = piThinking(ctx.effort, ctx.ultracode);
|
|
161
|
+
if (thinking)
|
|
162
|
+
args.push('--thinking', thinking);
|
|
163
|
+
if (ctx.resumeSessionId && ENGINE_SESSION_ID.test(ctx.resumeSessionId)) {
|
|
164
|
+
args.push('--session', ctx.resumeSessionId);
|
|
165
|
+
}
|
|
166
|
+
args.push(...(cfg.piArgs ?? []));
|
|
167
|
+
const provider = ctx.model?.includes('/') ? ctx.model.split('/')[0] : null;
|
|
168
|
+
return {
|
|
169
|
+
bin: this.bin(cfg),
|
|
170
|
+
args,
|
|
171
|
+
env: ctx.runEnv,
|
|
172
|
+
stdinInput: ctx.prompt,
|
|
173
|
+
shell: ctx.isWin,
|
|
174
|
+
authLogLine: provider
|
|
175
|
+
? `Pi with ${provider} — its key from the machine (env var or Pi's /login)`
|
|
176
|
+
: "Pi with its own default model — keys from the machine (env vars or Pi's /login)",
|
|
177
|
+
};
|
|
178
|
+
},
|
|
179
|
+
makeStreamPrinter() {
|
|
180
|
+
const short = (s, n = 160) => {
|
|
181
|
+
const one = s.replace(/\s+/g, ' ').trim();
|
|
182
|
+
return one.length > n ? `${one.slice(0, n)}…` : one;
|
|
183
|
+
};
|
|
184
|
+
let buf = '';
|
|
185
|
+
return (chunk) => {
|
|
186
|
+
buf += chunk;
|
|
187
|
+
const lines = buf.split('\n');
|
|
188
|
+
buf = lines.pop() ?? '';
|
|
189
|
+
for (const ev of events(lines.join('\n'))) {
|
|
190
|
+
if (ev.type === 'tool_execution_start' && typeof ev.toolName === 'string') {
|
|
191
|
+
process.stdout.write(` ⏺ ${ev.toolName}\n`);
|
|
192
|
+
}
|
|
193
|
+
else if (ev.type === 'message_end' &&
|
|
194
|
+
isObj(ev.message) &&
|
|
195
|
+
ev.message.role === 'assistant') {
|
|
196
|
+
const t = textOf(ev.message);
|
|
197
|
+
if (t)
|
|
198
|
+
process.stdout.write(` │ ${short(t)}\n`);
|
|
199
|
+
if (typeof ev.message.errorMessage === 'string') {
|
|
200
|
+
process.stdout.write(` ✗ ${short(ev.message.errorMessage, 240)}\n`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
},
|
|
206
|
+
summarize(stdout) {
|
|
207
|
+
return piSummary(stdout);
|
|
208
|
+
},
|
|
209
|
+
authFailureHint(text) {
|
|
210
|
+
const t = text.toLowerCase();
|
|
211
|
+
if (t.includes('no api key found') ||
|
|
212
|
+
t.includes('user not found') ||
|
|
213
|
+
t.includes('invalid api key') ||
|
|
214
|
+
t.includes('invalid x-api-key') ||
|
|
215
|
+
/\b401\b/.test(t) ||
|
|
216
|
+
/\bunauthorized\b/.test(t)) {
|
|
217
|
+
return 'Pi could not authenticate with the model’s provider. On the machine, set that provider’s key in the runner’s environment (ANTHROPIC_API_KEY, OPENROUTER_API_KEY, …) or sign in with `pi` then /login, then retry. `pi --list-models` lists the models it can use. FireFunc never holds the key.';
|
|
218
|
+
}
|
|
219
|
+
if (t.includes('no models available') || t.includes('model not found')) {
|
|
220
|
+
return 'Pi has no model it can use for this run. Run `pi --list-models` on the machine, set a provider key, and set a provider/model on the agent or the project.';
|
|
221
|
+
}
|
|
222
|
+
return null;
|
|
223
|
+
},
|
|
224
|
+
rateLimitHint(text) {
|
|
225
|
+
const t = text.toLowerCase();
|
|
226
|
+
if (/rate limit|too many requests|\b429\b|quota exceeded|insufficient credits|\b402\b|overloaded/.test(t)) {
|
|
227
|
+
return 'Pi’s provider hit a rate, quota or credit limit. This run was throttled, not failed: retry shortly, top up the provider’s credit, or pick another model.';
|
|
228
|
+
}
|
|
229
|
+
return null;
|
|
230
|
+
},
|
|
231
|
+
ending(stdout) {
|
|
232
|
+
return piEnding(stdout);
|
|
233
|
+
},
|
|
234
|
+
meter(stdout) {
|
|
235
|
+
return piUsage(stdout);
|
|
236
|
+
},
|
|
237
|
+
planLimit(stdout, now, stderr) {
|
|
238
|
+
const ending = piEnding(stdout);
|
|
239
|
+
if (ending && ending.error === null)
|
|
240
|
+
return null;
|
|
241
|
+
return readWordedLimit(ending?.error ?? null, stderr ?? '', now);
|
|
242
|
+
},
|
|
243
|
+
resumesSessions: true,
|
|
244
|
+
sessionId(stdout) {
|
|
245
|
+
return piSessionId(stdout);
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
//# sourceMappingURL=pi.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AgentAdapter } from './types.js';
|
|
2
|
+
import type { EngineEnding } from '../declined.js';
|
|
3
|
+
import type { RunUsage } from '../usage.js';
|
|
4
|
+
export declare function qwenEnding(stdout: string): EngineEnding | undefined;
|
|
5
|
+
export declare function qwenSummary(stdout: string): string;
|
|
6
|
+
export declare function qwenUsage(stdout: string): RunUsage | null;
|
|
7
|
+
export declare function qwenSessionId(stdout: string): string | null;
|
|
8
|
+
export declare const qwenAdapter: AgentAdapter;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { readWordedLimit } from '../limit.js';
|
|
2
|
+
import { ENGINE_SESSION_ID } from './sessions.js';
|
|
3
|
+
const isObj = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
4
|
+
function* events(stdout) {
|
|
5
|
+
for (const line of stdout.split('\n')) {
|
|
6
|
+
const t = line.trim();
|
|
7
|
+
if (!t.startsWith('{') && !t.startsWith('['))
|
|
8
|
+
continue;
|
|
9
|
+
try {
|
|
10
|
+
const ev = JSON.parse(t);
|
|
11
|
+
for (const e of Array.isArray(ev) ? ev : [ev])
|
|
12
|
+
if (isObj(e))
|
|
13
|
+
yield e;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function lastResult(stdout) {
|
|
20
|
+
let found = null;
|
|
21
|
+
for (const ev of events(stdout))
|
|
22
|
+
if (ev.type === 'result')
|
|
23
|
+
found = ev;
|
|
24
|
+
return found;
|
|
25
|
+
}
|
|
26
|
+
export function qwenEnding(stdout) {
|
|
27
|
+
try {
|
|
28
|
+
const r = lastResult(stdout);
|
|
29
|
+
if (!r)
|
|
30
|
+
return undefined;
|
|
31
|
+
if (r.is_error === true || (typeof r.subtype === 'string' && r.subtype !== 'success')) {
|
|
32
|
+
const err = isObj(r.error) && typeof r.error.message === 'string' ? r.error.message : '';
|
|
33
|
+
const words = err || (typeof r.result === 'string' ? r.result : '') || String(r.subtype);
|
|
34
|
+
return { error: words.trim() || 'Qwen Code ended on an error', refusal: false };
|
|
35
|
+
}
|
|
36
|
+
return { error: null, refusal: false };
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export function qwenSummary(stdout) {
|
|
43
|
+
const r = lastResult(stdout);
|
|
44
|
+
if (r) {
|
|
45
|
+
const ending = qwenEnding(stdout);
|
|
46
|
+
if (ending?.error)
|
|
47
|
+
return ending.error;
|
|
48
|
+
if (typeof r.result === 'string' && r.result.trim())
|
|
49
|
+
return r.result.trim();
|
|
50
|
+
}
|
|
51
|
+
for (const ev of [...events(stdout)].reverse()) {
|
|
52
|
+
if (ev.type !== 'assistant' || !isObj(ev.message))
|
|
53
|
+
continue;
|
|
54
|
+
const content = Array.isArray(ev.message.content) ? ev.message.content : [];
|
|
55
|
+
const t = content
|
|
56
|
+
.filter((c) => isObj(c) && c.type === 'text' && typeof c.text === 'string')
|
|
57
|
+
.map((c) => c.text)
|
|
58
|
+
.join('')
|
|
59
|
+
.trim();
|
|
60
|
+
if (t)
|
|
61
|
+
return t;
|
|
62
|
+
}
|
|
63
|
+
return stdout.trim().split('\n').slice(-8).join('\n').trim().slice(0, 800);
|
|
64
|
+
}
|
|
65
|
+
const count = (v) => typeof v === 'number' && Number.isFinite(v) && v >= 0 ? v : 0;
|
|
66
|
+
export function qwenUsage(stdout) {
|
|
67
|
+
try {
|
|
68
|
+
const r = lastResult(stdout);
|
|
69
|
+
if (!r || !isObj(r.usage))
|
|
70
|
+
return null;
|
|
71
|
+
const u = r.usage;
|
|
72
|
+
return {
|
|
73
|
+
input_tokens: count(u.input_tokens),
|
|
74
|
+
output_tokens: count(u.output_tokens),
|
|
75
|
+
...(count(u.cache_read_input_tokens)
|
|
76
|
+
? { cache_read_input_tokens: count(u.cache_read_input_tokens) }
|
|
77
|
+
: {}),
|
|
78
|
+
...(typeof r.num_turns === 'number' ? { num_turns: count(r.num_turns) } : {}),
|
|
79
|
+
...(typeof r.duration_ms === 'number' ? { duration_ms: count(r.duration_ms) } : {}),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export function qwenSessionId(stdout) {
|
|
87
|
+
try {
|
|
88
|
+
for (const ev of events(stdout)) {
|
|
89
|
+
if (typeof ev.session_id === 'string' && ENGINE_SESSION_ID.test(ev.session_id)) {
|
|
90
|
+
return ev.session_id;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
export const qwenAdapter = {
|
|
99
|
+
id: 'qwen_code',
|
|
100
|
+
displayName: 'Qwen Code',
|
|
101
|
+
branchPrefix: 'qwen',
|
|
102
|
+
bin(cfg) {
|
|
103
|
+
return cfg.qwenBin ?? 'qwen';
|
|
104
|
+
},
|
|
105
|
+
buildInvocation(ctx) {
|
|
106
|
+
const { cfg } = ctx;
|
|
107
|
+
const args = ['-o', 'stream-json', '-y'];
|
|
108
|
+
if (ctx.model)
|
|
109
|
+
args.push('-m', ctx.model);
|
|
110
|
+
if (ctx.resumeSessionId && ENGINE_SESSION_ID.test(ctx.resumeSessionId)) {
|
|
111
|
+
args.push('-r', ctx.resumeSessionId);
|
|
112
|
+
}
|
|
113
|
+
args.push(...(cfg.qwenArgs ?? []));
|
|
114
|
+
return {
|
|
115
|
+
bin: this.bin(cfg),
|
|
116
|
+
args,
|
|
117
|
+
env: ctx.runEnv,
|
|
118
|
+
stdinInput: ctx.prompt,
|
|
119
|
+
shell: ctx.isWin,
|
|
120
|
+
authLogLine: 'Qwen Code with the auth type in its own settings (~/.qwen/settings.json) — keys from the machine',
|
|
121
|
+
};
|
|
122
|
+
},
|
|
123
|
+
makeStreamPrinter: () => undefined,
|
|
124
|
+
summarize: qwenSummary,
|
|
125
|
+
authFailureHint(text) {
|
|
126
|
+
const t = text.toLowerCase();
|
|
127
|
+
if (t.includes('no auth type is selected') ||
|
|
128
|
+
t.includes('invalid api key') ||
|
|
129
|
+
/\b401\b/.test(t) ||
|
|
130
|
+
/\bunauthorized\b/.test(t)) {
|
|
131
|
+
return 'Qwen Code could not authenticate. On the machine, choose an auth type in ~/.qwen/settings.json (or run `qwen` once and sign in), or set OPENAI_API_KEY with OPENAI_BASE_URL for an OpenAI-compatible provider such as OpenRouter, then retry. FireFunc never holds the key.';
|
|
132
|
+
}
|
|
133
|
+
return null;
|
|
134
|
+
},
|
|
135
|
+
rateLimitHint(text) {
|
|
136
|
+
const t = text.toLowerCase();
|
|
137
|
+
if (/rate limit|too many requests|\b429\b|quota exceeded|insufficient credits/.test(t)) {
|
|
138
|
+
return 'Qwen Code’s provider hit a rate, quota or credit limit. This run was throttled, not failed: retry shortly or pick another model.';
|
|
139
|
+
}
|
|
140
|
+
return null;
|
|
141
|
+
},
|
|
142
|
+
ending: qwenEnding,
|
|
143
|
+
meter: qwenUsage,
|
|
144
|
+
planLimit(stdout, now, stderr) {
|
|
145
|
+
const ending = qwenEnding(stdout);
|
|
146
|
+
if (ending && ending.error === null)
|
|
147
|
+
return null;
|
|
148
|
+
return readWordedLimit(ending?.error ?? null, stderr ?? '', now);
|
|
149
|
+
},
|
|
150
|
+
resumesSessions: true,
|
|
151
|
+
sessionId: qwenSessionId,
|
|
152
|
+
};
|
|
153
|
+
//# sourceMappingURL=qwen.js.map
|