@aibridge/driver-agy 0.9.3 → 0.10.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/README.md +1 -1
- package/dist/index.mjs +73 -103
- package/package.json +1 -1
- package/src/run.test.ts +13 -82
- package/src/run.ts +100 -144
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @aibridge/driver-agy
|
|
2
2
|
|
|
3
3
|
Driver for the Antigravity (`agy`) CLI. It builds print-mode commands, checks
|
|
4
|
-
availability, runs delegated tasks, captures
|
|
4
|
+
availability, runs delegated tasks, captures their answers, and reads quota
|
|
5
5
|
windows. Delegation `run()` uses `--output-format stream-json` when agy is ≥ 1.2.3; image generation does not.
|
|
6
6
|
|
|
7
7
|
```ts
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AuthExpiredError, isNotFound, probeVersion, runCaptured, semverGte, stripAnsi } from "@aibridge/proc";
|
|
2
|
-
import { existsSync,
|
|
3
|
-
import { homedir
|
|
2
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
//#region src/agy.ts
|
|
6
6
|
/**
|
|
@@ -560,118 +560,88 @@ function logForwarder(onStdout, onActivity) {
|
|
|
560
560
|
};
|
|
561
561
|
}
|
|
562
562
|
async function run(task, exec = runCaptured) {
|
|
563
|
-
|
|
564
|
-
let
|
|
565
|
-
let
|
|
566
|
-
|
|
567
|
-
if (task.tools) {
|
|
568
|
-
tempDir = mkdtempSync(join(tmpdir(), "aibridge-agy-"));
|
|
569
|
-
answerPath = join(tempDir, "answer.md");
|
|
570
|
-
taskPrompt = `${task.prompt}\n\nYou are working in the repository rooted at ${task.cwd}; make ALL file edits there (any relative paths in the task are relative to that root). When the task is complete, write ONLY your final answer (the exact text you would otherwise print as your response, with no narration of your steps) to the file ${answerPath} — nothing else in that file. This is how your answer is captured; do not mention the file in the answer.`;
|
|
571
|
-
addDirs.push(task.cwd, tempDir);
|
|
572
|
-
}
|
|
563
|
+
const addDirs = task.tools ? [task.cwd] : void 0;
|
|
564
|
+
let result;
|
|
565
|
+
let useStreamJson = false;
|
|
566
|
+
let forwarder;
|
|
573
567
|
try {
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
}), ...useStreamJson ? MESSAGE_STREAM_ARGS : []];
|
|
587
|
-
if (useStreamJson) {
|
|
588
|
-
forwarder = logForwarder(task.onStdout, task.onActivity);
|
|
589
|
-
result = await exec("agy", args, {
|
|
590
|
-
cwd: task.cwd,
|
|
591
|
-
timeoutMs: (task.timeoutSec + 20) * 1e3,
|
|
592
|
-
captureStdout: false,
|
|
593
|
-
onStdout: forwarder.onChunk,
|
|
594
|
-
onStderr: task.onStderr,
|
|
595
|
-
onSpawn: task.onSpawn
|
|
596
|
-
});
|
|
597
|
-
} else result = await exec("agy", args, {
|
|
568
|
+
const version = await probeVersion("agy", exec);
|
|
569
|
+
useStreamJson = agySupportsStreamJson(version);
|
|
570
|
+
if (!useStreamJson) task.onStderr?.(`aibridge: agy version ${version ?? "unavailable"} does not support stream-json (requires >= 1.2.3); using text compatibility mode.\n`);
|
|
571
|
+
const args = [...buildAgyPrintArgs(task.prompt, {
|
|
572
|
+
model: task.backendModel,
|
|
573
|
+
printTimeoutSec: task.timeoutSec,
|
|
574
|
+
skipPermissions: task.tools,
|
|
575
|
+
addDirs
|
|
576
|
+
}), ...useStreamJson ? MESSAGE_STREAM_ARGS : []];
|
|
577
|
+
if (useStreamJson) {
|
|
578
|
+
forwarder = logForwarder(task.onStdout, task.onActivity);
|
|
579
|
+
result = await exec("agy", args, {
|
|
598
580
|
cwd: task.cwd,
|
|
599
581
|
timeoutMs: (task.timeoutSec + 20) * 1e3,
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
task.onStdout?.(chunk);
|
|
603
|
-
},
|
|
582
|
+
captureStdout: false,
|
|
583
|
+
onStdout: forwarder.onChunk,
|
|
604
584
|
onStderr: task.onStderr,
|
|
605
585
|
onSpawn: task.onSpawn
|
|
606
586
|
});
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
};
|
|
620
|
-
}
|
|
621
|
-
forwarder?.flush();
|
|
622
|
-
if (result.timedOut) return {
|
|
623
|
-
ok: false,
|
|
624
|
-
kind: "timeout",
|
|
625
|
-
message: `aibridge: agy timed out after ~${task.timeoutSec + 20}s; raise --timeout.`,
|
|
626
|
-
exitCode: result.code
|
|
627
|
-
};
|
|
628
|
-
let fileAnswer = "";
|
|
629
|
-
if (answerPath && existsSync(answerPath)) {
|
|
630
|
-
fileAnswer = clean(readFileSync(answerPath, "utf8"));
|
|
631
|
-
if (fileAnswer.length > 0) task.onStdout?.(`\n--- final answer ---\n${fileAnswer}\n`);
|
|
632
|
-
}
|
|
633
|
-
const snap = useStreamJson && forwarder ? forwarder.snapshot() : void 0;
|
|
634
|
-
let resolvedAnswer = "";
|
|
635
|
-
const sawProtocol = snap?.sawProtocol ?? false;
|
|
636
|
-
const rawTruncated = snap?.rawTruncated ?? false;
|
|
637
|
-
const resultError = snap?.resultError ?? null;
|
|
638
|
-
const resultStatus = snap?.resultStatus ?? null;
|
|
639
|
-
if (fileAnswer.length > 0) resolvedAnswer = fileAnswer;
|
|
640
|
-
else {
|
|
641
|
-
const stderrLines = stripAnsi(result.stderr).split("\n").map((l) => l.trim()).filter((l) => l.length > 0);
|
|
642
|
-
const lastStderrLine = stderrLines[stderrLines.length - 1] ?? "";
|
|
643
|
-
if (result.code === 0 && AGY_PRINT_TIMEOUT_RE.test(lastStderrLine)) return {
|
|
644
|
-
ok: false,
|
|
645
|
-
kind: "timeout",
|
|
646
|
-
message: `aibridge: agy timed out after ~${task.timeoutSec}s; raise --timeout.`,
|
|
647
|
-
exitCode: result.code
|
|
648
|
-
};
|
|
649
|
-
if (snap) resolvedAnswer = clean(snap.text ?? (snap.sawProtocol || snap.rawTruncated ? "" : snap.rawFallback));
|
|
650
|
-
else resolvedAnswer = clean(result.stdout);
|
|
651
|
-
}
|
|
652
|
-
if (useStreamJson && !sawProtocol && rawTruncated && resolvedAnswer.length === 0) return {
|
|
653
|
-
ok: false,
|
|
654
|
-
kind: "no-answer",
|
|
655
|
-
message: `aibridge: agy returned more than ${RAW_FALLBACK_MAX_CHARS} characters without a recognized stream protocol; see the run log.`,
|
|
656
|
-
exitCode: result.code
|
|
657
|
-
};
|
|
658
|
-
if (resultStatus === "ERROR" || result.code !== 0 || resolvedAnswer.length === 0) return {
|
|
587
|
+
} else result = await exec("agy", args, {
|
|
588
|
+
cwd: task.cwd,
|
|
589
|
+
timeoutMs: (task.timeoutSec + 20) * 1e3,
|
|
590
|
+
onStdout: (chunk) => {
|
|
591
|
+
task.onActivity?.();
|
|
592
|
+
task.onStdout?.(chunk);
|
|
593
|
+
},
|
|
594
|
+
onStderr: task.onStderr,
|
|
595
|
+
onSpawn: task.onSpawn
|
|
596
|
+
});
|
|
597
|
+
} catch (err) {
|
|
598
|
+
if (isNotFound(err)) return {
|
|
659
599
|
ok: false,
|
|
660
|
-
kind: "
|
|
661
|
-
message: `aibridge: agy
|
|
662
|
-
exitCode:
|
|
600
|
+
kind: "not-found",
|
|
601
|
+
message: `aibridge: "agy" not found on PATH. ${INSTALL_HINT}`,
|
|
602
|
+
exitCode: null
|
|
663
603
|
};
|
|
664
604
|
return {
|
|
665
|
-
ok:
|
|
666
|
-
|
|
667
|
-
|
|
605
|
+
ok: false,
|
|
606
|
+
kind: "spawn",
|
|
607
|
+
message: `aibridge: failed to run agy: ${err.message}`,
|
|
608
|
+
exitCode: null
|
|
668
609
|
};
|
|
669
|
-
} finally {
|
|
670
|
-
if (tempDir) rmSync(tempDir, {
|
|
671
|
-
recursive: true,
|
|
672
|
-
force: true
|
|
673
|
-
});
|
|
674
610
|
}
|
|
611
|
+
forwarder?.flush();
|
|
612
|
+
if (result.timedOut) return {
|
|
613
|
+
ok: false,
|
|
614
|
+
kind: "timeout",
|
|
615
|
+
message: `aibridge: agy timed out after ~${task.timeoutSec + 20}s; raise --timeout.`,
|
|
616
|
+
exitCode: result.code
|
|
617
|
+
};
|
|
618
|
+
const stderrLines = stripAnsi(result.stderr).split("\n").map((l) => l.trim()).filter((l) => l.length > 0);
|
|
619
|
+
const lastStderrLine = stderrLines[stderrLines.length - 1] ?? "";
|
|
620
|
+
if (result.code === 0 && AGY_PRINT_TIMEOUT_RE.test(lastStderrLine)) return {
|
|
621
|
+
ok: false,
|
|
622
|
+
kind: "timeout",
|
|
623
|
+
message: `aibridge: agy timed out after ~${task.timeoutSec}s; raise --timeout.`,
|
|
624
|
+
exitCode: result.code
|
|
625
|
+
};
|
|
626
|
+
const snap = useStreamJson && forwarder ? forwarder.snapshot() : void 0;
|
|
627
|
+
const resolvedAnswer = snap ? clean(snap.text ?? (snap.sawProtocol || snap.rawTruncated ? "" : snap.rawFallback)) : clean(result.stdout);
|
|
628
|
+
if (snap && !snap.sawProtocol && snap.rawTruncated && resolvedAnswer.length === 0) return {
|
|
629
|
+
ok: false,
|
|
630
|
+
kind: "no-answer",
|
|
631
|
+
message: `aibridge: agy returned more than ${RAW_FALLBACK_MAX_CHARS} characters without a recognized stream protocol; see the run log.`,
|
|
632
|
+
exitCode: result.code
|
|
633
|
+
};
|
|
634
|
+
if (snap?.resultStatus === "ERROR" || result.code !== 0 || resolvedAnswer.length === 0) return {
|
|
635
|
+
ok: false,
|
|
636
|
+
kind: "no-answer",
|
|
637
|
+
message: `aibridge: agy returned no usable answer (${clean(snap?.resultError || result.stderr) || `exit code ${result.code}`}).`,
|
|
638
|
+
exitCode: result.code
|
|
639
|
+
};
|
|
640
|
+
return {
|
|
641
|
+
ok: true,
|
|
642
|
+
response: resolvedAnswer,
|
|
643
|
+
exitCode: result.code ?? 0
|
|
644
|
+
};
|
|
675
645
|
}
|
|
676
646
|
//#endregion
|
|
677
647
|
export { AGY_CANONICAL_TO_NATIVE, MIN_AGY_STREAM_JSON, agySupportsStreamJson, agyTokenPath, buildAgyPrintArgs, fetchAgyQuota, findModelQuota, generateImage, parseModels, parseQuotaGroups, probe, run };
|
package/package.json
CHANGED
package/src/run.test.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { writeFileSync } from 'node:fs';
|
|
2
1
|
import type { RunOptions, RunResult } from '@aibridge/proc';
|
|
3
2
|
import { describe, expect, it } from 'vitest';
|
|
4
3
|
import { run } from './run.ts';
|
|
@@ -11,7 +10,6 @@ interface FakeAgyConfig {
|
|
|
11
10
|
readonly stderr?: string;
|
|
12
11
|
readonly code?: number | null;
|
|
13
12
|
readonly timedOut?: boolean;
|
|
14
|
-
readonly writeAnswerFile?: string;
|
|
15
13
|
readonly streamChunks?: readonly string[];
|
|
16
14
|
}
|
|
17
15
|
|
|
@@ -48,14 +46,6 @@ function fakeAgy(config: FakeAgyConfig = {}): {
|
|
|
48
46
|
};
|
|
49
47
|
}
|
|
50
48
|
|
|
51
|
-
if (config.writeAnswerFile !== undefined) {
|
|
52
|
-
const prompt = args[1] ?? '';
|
|
53
|
-
const match = prompt.match(/file (\/.*answer\.md)/);
|
|
54
|
-
if (match?.[1]) {
|
|
55
|
-
writeFileSync(match[1], config.writeAnswerFile);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
49
|
if (config.streamChunks) {
|
|
60
50
|
for (const chunk of config.streamChunks) {
|
|
61
51
|
opts.onStdout?.(chunk);
|
|
@@ -166,10 +156,10 @@ describe('agy driver argv / version handling', () => {
|
|
|
166
156
|
expect(res).toEqual({ ok: true, response: 'Text fallback', exitCode: 0 });
|
|
167
157
|
});
|
|
168
158
|
|
|
169
|
-
it('passes --dangerously-skip-permissions, add-
|
|
159
|
+
it('passes the prompt verbatim with --dangerously-skip-permissions, cwd as the only add-dir, and stream-json in tools mode', async () => {
|
|
170
160
|
const fake = fakeAgy({
|
|
171
161
|
version: 'agy 1.2.3',
|
|
172
|
-
|
|
162
|
+
stdout: `${JSON.stringify({ event: 'result', result: { status: 'SUCCESS', response: 'from stream' } })}\n`,
|
|
173
163
|
});
|
|
174
164
|
|
|
175
165
|
const res = await run(
|
|
@@ -186,17 +176,16 @@ describe('agy driver argv / version handling', () => {
|
|
|
186
176
|
const runCall = fake.calls[1];
|
|
187
177
|
expect(runCall).toBeDefined();
|
|
188
178
|
if (runCall) {
|
|
189
|
-
|
|
190
|
-
const secondAddDir = runCall.args.indexOf('--add-dir', firstAddDir + 1);
|
|
179
|
+
expect(runCall.args.slice(0, 2)).toEqual(['-p', 'do tool task']);
|
|
191
180
|
expect(runCall.args).toContain('--dangerously-skip-permissions');
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
expect(
|
|
181
|
+
const addDirs = runCall.args.flatMap((a, i) =>
|
|
182
|
+
a === '--add-dir' ? [runCall.args[i + 1]] : [],
|
|
183
|
+
);
|
|
184
|
+
expect(addDirs).toEqual(['/repo/root']);
|
|
196
185
|
expect(runCall.args.slice(-2)).toEqual(['--output-format', 'stream-json']);
|
|
197
186
|
expect(runCall.opts.captureStdout).toBe(false);
|
|
198
187
|
}
|
|
199
|
-
expect(res).toEqual({ ok: true, response: '
|
|
188
|
+
expect(res).toEqual({ ok: true, response: 'from stream', exitCode: 0 });
|
|
200
189
|
});
|
|
201
190
|
});
|
|
202
191
|
|
|
@@ -424,42 +413,6 @@ describe('agy tools stream-json parsing', () => {
|
|
|
424
413
|
backendModel: 'gemini-3.7-flash-high',
|
|
425
414
|
};
|
|
426
415
|
|
|
427
|
-
it('authoritative answer file wins over result.response and emits banner', async () => {
|
|
428
|
-
const log: string[] = [];
|
|
429
|
-
const stream = [
|
|
430
|
-
JSON.stringify({ event: 'init' }),
|
|
431
|
-
JSON.stringify({
|
|
432
|
-
event: 'result',
|
|
433
|
-
result: { status: 'SUCCESS', response: 'something else' },
|
|
434
|
-
}),
|
|
435
|
-
'',
|
|
436
|
-
].join('\n');
|
|
437
|
-
|
|
438
|
-
const fake = fakeAgy({
|
|
439
|
-
version: 'agy 1.2.3',
|
|
440
|
-
stdout: stream,
|
|
441
|
-
writeAnswerFile: 'Written answer from file',
|
|
442
|
-
});
|
|
443
|
-
|
|
444
|
-
const res = await run({ ...task, onStdout: c => log.push(c) }, fake.exec);
|
|
445
|
-
|
|
446
|
-
expect(res).toEqual({ ok: true, response: 'Written answer from file', exitCode: 0 });
|
|
447
|
-
expect(log.join('')).toContain('--- final answer ---\nWritten answer from file\n');
|
|
448
|
-
});
|
|
449
|
-
|
|
450
|
-
it('returns result.response when no answer file is created in tools mode', async () => {
|
|
451
|
-
const stream = [
|
|
452
|
-
JSON.stringify({ event: 'init' }),
|
|
453
|
-
JSON.stringify({ event: 'result', result: { status: 'SUCCESS', response: 'from stream' } }),
|
|
454
|
-
'',
|
|
455
|
-
].join('\n');
|
|
456
|
-
|
|
457
|
-
const fake = fakeAgy({ version: 'agy 1.2.3', stdout: stream });
|
|
458
|
-
const res = await run(task, fake.exec);
|
|
459
|
-
|
|
460
|
-
expect(res).toEqual({ ok: true, response: 'from stream', exitCode: 0 });
|
|
461
|
-
});
|
|
462
|
-
|
|
463
416
|
it('logs tool ACTIVE and DONE concisely without dumping heavy payloads', async () => {
|
|
464
417
|
const log: string[] = [];
|
|
465
418
|
const stream = [
|
|
@@ -547,21 +500,17 @@ describe('agy timeout and error handling', () => {
|
|
|
547
500
|
}
|
|
548
501
|
});
|
|
549
502
|
|
|
550
|
-
it('
|
|
503
|
+
it('maps soft print-timeout stderr to timeout in tools mode too', async () => {
|
|
551
504
|
const stderr = '[agy] print timeout after 1s with turn in progress; returning partial output\n';
|
|
552
|
-
const fake = fakeAgy({
|
|
553
|
-
version: 'agy 1.2.3',
|
|
554
|
-
stderr,
|
|
555
|
-
code: 0,
|
|
556
|
-
writeAnswerFile: 'Full finished answer',
|
|
557
|
-
});
|
|
505
|
+
const fake = fakeAgy({ version: 'agy 1.2.3', stderr, code: 0 });
|
|
558
506
|
|
|
559
507
|
const res = await run({ ...task, tools: true }, fake.exec);
|
|
560
508
|
|
|
561
|
-
expect(res).
|
|
509
|
+
expect(res.ok).toBe(false);
|
|
510
|
+
if (!res.ok) expect(res.kind).toBe('timeout');
|
|
562
511
|
});
|
|
563
512
|
|
|
564
|
-
it('ERROR result frame
|
|
513
|
+
it('ERROR result frame is no-answer even with exit 0', async () => {
|
|
565
514
|
const stream = [
|
|
566
515
|
JSON.stringify({ event: 'init' }),
|
|
567
516
|
JSON.stringify({
|
|
@@ -574,7 +523,6 @@ describe('agy timeout and error handling', () => {
|
|
|
574
523
|
version: 'agy 1.2.3',
|
|
575
524
|
stdout: stream,
|
|
576
525
|
code: 0,
|
|
577
|
-
writeAnswerFile: 'Apparently finished answer',
|
|
578
526
|
});
|
|
579
527
|
|
|
580
528
|
const res = await run({ ...task, tools: true }, fake.exec);
|
|
@@ -622,23 +570,6 @@ describe('agy timeout and error handling', () => {
|
|
|
622
570
|
}
|
|
623
571
|
});
|
|
624
572
|
|
|
625
|
-
it('hard process timeout wins over a non-empty answer file', async () => {
|
|
626
|
-
const fake = fakeAgy({
|
|
627
|
-
version: 'agy 1.2.3',
|
|
628
|
-
timedOut: true,
|
|
629
|
-
code: null,
|
|
630
|
-
writeAnswerFile: 'Possibly torn answer',
|
|
631
|
-
});
|
|
632
|
-
|
|
633
|
-
const res = await run({ ...task, tools: true }, fake.exec);
|
|
634
|
-
|
|
635
|
-
expect(res.ok).toBe(false);
|
|
636
|
-
if (!res.ok) {
|
|
637
|
-
expect(res.kind).toBe('timeout');
|
|
638
|
-
expect(res.message).toBe('aibridge: agy timed out after ~21s; raise --timeout.');
|
|
639
|
-
}
|
|
640
|
-
});
|
|
641
|
-
|
|
642
573
|
it('maps exit 1, error stderr, and ERROR frame to no-answer with detail', async () => {
|
|
643
574
|
const stderr = 'error: unknown model foo\n';
|
|
644
575
|
const stream = [
|
package/src/run.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
2
|
-
import { tmpdir } from 'node:os';
|
|
3
|
-
import { join } from 'node:path';
|
|
4
1
|
import { isNotFound, probeVersion, type RunResult, runCaptured, stripAnsi } from '@aibridge/proc';
|
|
5
2
|
import { agySupportsStreamJson, buildAgyPrintArgs } from './agy.ts';
|
|
6
3
|
import { INSTALL_HINT } from './probe.ts';
|
|
@@ -300,160 +297,119 @@ export async function run(
|
|
|
300
297
|
task: DelegationTask,
|
|
301
298
|
exec: typeof runCaptured = runCaptured,
|
|
302
299
|
): Promise<DelegationResult> {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
let taskPrompt = task.prompt;
|
|
307
|
-
const addDirs: string[] = [];
|
|
308
|
-
|
|
309
|
-
if (task.tools) {
|
|
310
|
-
tempDir = mkdtempSync(join(tmpdir(), 'aibridge-agy-'));
|
|
311
|
-
answerPath = join(tempDir, 'answer.md');
|
|
312
|
-
taskPrompt =
|
|
313
|
-
`${task.prompt}\n\nYou are working in the repository rooted at ${task.cwd}; make ALL file ` +
|
|
314
|
-
`edits there (any relative paths in the task are relative to that root). When the task ` +
|
|
315
|
-
`is complete, write ONLY your final answer (the exact text you would otherwise print as ` +
|
|
316
|
-
`your response, with no narration of your steps) to the file ${answerPath} — nothing else ` +
|
|
317
|
-
`in that file. This is how your answer is captured; do not mention the file in the answer.`;
|
|
318
|
-
addDirs.push(task.cwd, tempDir);
|
|
319
|
-
}
|
|
300
|
+
// agy ignores its spawn cwd and treats the first --add-dir as the workspace.
|
|
301
|
+
const addDirs = task.tools ? [task.cwd] : undefined;
|
|
320
302
|
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
let forwarder: LogForwarder | undefined;
|
|
325
|
-
|
|
326
|
-
try {
|
|
327
|
-
const version = await probeVersion('agy', exec);
|
|
328
|
-
useStreamJson = agySupportsStreamJson(version);
|
|
329
|
-
if (!useStreamJson) {
|
|
330
|
-
task.onStderr?.(
|
|
331
|
-
`aibridge: agy version ${version ?? 'unavailable'} does not support stream-json (requires >= 1.2.3); using text compatibility mode.\n`,
|
|
332
|
-
);
|
|
333
|
-
}
|
|
303
|
+
let result: RunResult;
|
|
304
|
+
let useStreamJson = false;
|
|
305
|
+
let forwarder: LogForwarder | undefined;
|
|
334
306
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
...(useStreamJson ? MESSAGE_STREAM_ARGS : []),
|
|
343
|
-
];
|
|
344
|
-
|
|
345
|
-
if (useStreamJson) {
|
|
346
|
-
forwarder = logForwarder(task.onStdout, task.onActivity);
|
|
347
|
-
result = await exec('agy', args, {
|
|
348
|
-
cwd: task.cwd,
|
|
349
|
-
timeoutMs: (task.timeoutSec + 20) * 1000,
|
|
350
|
-
captureStdout: false,
|
|
351
|
-
onStdout: forwarder.onChunk,
|
|
352
|
-
onStderr: task.onStderr,
|
|
353
|
-
onSpawn: task.onSpawn,
|
|
354
|
-
});
|
|
355
|
-
} else {
|
|
356
|
-
result = await exec('agy', args, {
|
|
357
|
-
cwd: task.cwd,
|
|
358
|
-
timeoutMs: (task.timeoutSec + 20) * 1000,
|
|
359
|
-
onStdout: chunk => {
|
|
360
|
-
task.onActivity?.();
|
|
361
|
-
task.onStdout?.(chunk);
|
|
362
|
-
},
|
|
363
|
-
onStderr: task.onStderr,
|
|
364
|
-
onSpawn: task.onSpawn,
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
} catch (err) {
|
|
368
|
-
if (isNotFound(err)) {
|
|
369
|
-
return {
|
|
370
|
-
ok: false,
|
|
371
|
-
kind: 'not-found',
|
|
372
|
-
message: `aibridge: "agy" not found on PATH. ${INSTALL_HINT}`,
|
|
373
|
-
exitCode: null,
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
return {
|
|
377
|
-
ok: false,
|
|
378
|
-
kind: 'spawn',
|
|
379
|
-
message: `aibridge: failed to run agy: ${(err as Error).message}`,
|
|
380
|
-
exitCode: null,
|
|
381
|
-
};
|
|
307
|
+
try {
|
|
308
|
+
const version = await probeVersion('agy', exec);
|
|
309
|
+
useStreamJson = agySupportsStreamJson(version);
|
|
310
|
+
if (!useStreamJson) {
|
|
311
|
+
task.onStderr?.(
|
|
312
|
+
`aibridge: agy version ${version ?? 'unavailable'} does not support stream-json (requires >= 1.2.3); using text compatibility mode.\n`,
|
|
313
|
+
);
|
|
382
314
|
}
|
|
383
315
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
316
|
+
const args = [
|
|
317
|
+
...buildAgyPrintArgs(task.prompt, {
|
|
318
|
+
model: task.backendModel,
|
|
319
|
+
printTimeoutSec: task.timeoutSec,
|
|
320
|
+
skipPermissions: task.tools,
|
|
321
|
+
addDirs,
|
|
322
|
+
}),
|
|
323
|
+
...(useStreamJson ? MESSAGE_STREAM_ARGS : []),
|
|
324
|
+
];
|
|
325
|
+
|
|
326
|
+
if (useStreamJson) {
|
|
327
|
+
forwarder = logForwarder(task.onStdout, task.onActivity);
|
|
328
|
+
result = await exec('agy', args, {
|
|
329
|
+
cwd: task.cwd,
|
|
330
|
+
timeoutMs: (task.timeoutSec + 20) * 1000,
|
|
331
|
+
captureStdout: false,
|
|
332
|
+
onStdout: forwarder.onChunk,
|
|
333
|
+
onStderr: task.onStderr,
|
|
334
|
+
onSpawn: task.onSpawn,
|
|
335
|
+
});
|
|
336
|
+
} else {
|
|
337
|
+
result = await exec('agy', args, {
|
|
338
|
+
cwd: task.cwd,
|
|
339
|
+
timeoutMs: (task.timeoutSec + 20) * 1000,
|
|
340
|
+
onStdout: chunk => {
|
|
341
|
+
task.onActivity?.();
|
|
342
|
+
task.onStdout?.(chunk);
|
|
343
|
+
},
|
|
344
|
+
onStderr: task.onStderr,
|
|
345
|
+
onSpawn: task.onSpawn,
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
} catch (err) {
|
|
349
|
+
if (isNotFound(err)) {
|
|
387
350
|
return {
|
|
388
351
|
ok: false,
|
|
389
|
-
kind: '
|
|
390
|
-
message: `aibridge: agy
|
|
391
|
-
exitCode:
|
|
352
|
+
kind: 'not-found',
|
|
353
|
+
message: `aibridge: "agy" not found on PATH. ${INSTALL_HINT}`,
|
|
354
|
+
exitCode: null,
|
|
392
355
|
};
|
|
393
356
|
}
|
|
357
|
+
return {
|
|
358
|
+
ok: false,
|
|
359
|
+
kind: 'spawn',
|
|
360
|
+
message: `aibridge: failed to run agy: ${(err as Error).message}`,
|
|
361
|
+
exitCode: null,
|
|
362
|
+
};
|
|
363
|
+
}
|
|
394
364
|
|
|
395
|
-
|
|
396
|
-
if (answerPath && existsSync(answerPath)) {
|
|
397
|
-
fileAnswer = clean(readFileSync(answerPath, 'utf8'));
|
|
398
|
-
if (fileAnswer.length > 0) {
|
|
399
|
-
task.onStdout?.(`\n--- final answer ---\n${fileAnswer}\n`);
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
const snap = useStreamJson && forwarder ? forwarder.snapshot() : undefined;
|
|
404
|
-
let resolvedAnswer = '';
|
|
405
|
-
const sawProtocol = snap?.sawProtocol ?? false;
|
|
406
|
-
const rawTruncated = snap?.rawTruncated ?? false;
|
|
407
|
-
const resultError = snap?.resultError ?? null;
|
|
408
|
-
const resultStatus = snap?.resultStatus ?? null;
|
|
365
|
+
forwarder?.flush();
|
|
409
366
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
if (result.code === 0 && AGY_PRINT_TIMEOUT_RE.test(lastStderrLine)) {
|
|
419
|
-
return {
|
|
420
|
-
ok: false,
|
|
421
|
-
kind: 'timeout',
|
|
422
|
-
message: `aibridge: agy timed out after ~${task.timeoutSec}s; raise --timeout.`,
|
|
423
|
-
exitCode: result.code,
|
|
424
|
-
};
|
|
425
|
-
}
|
|
367
|
+
if (result.timedOut) {
|
|
368
|
+
return {
|
|
369
|
+
ok: false,
|
|
370
|
+
kind: 'timeout',
|
|
371
|
+
message: `aibridge: agy timed out after ~${task.timeoutSec + 20}s; raise --timeout.`,
|
|
372
|
+
exitCode: result.code,
|
|
373
|
+
};
|
|
374
|
+
}
|
|
426
375
|
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
376
|
+
const stderrLines = stripAnsi(result.stderr)
|
|
377
|
+
.split('\n')
|
|
378
|
+
.map(l => l.trim())
|
|
379
|
+
.filter(l => l.length > 0);
|
|
380
|
+
const lastStderrLine = stderrLines[stderrLines.length - 1] ?? '';
|
|
381
|
+
if (result.code === 0 && AGY_PRINT_TIMEOUT_RE.test(lastStderrLine)) {
|
|
382
|
+
return {
|
|
383
|
+
ok: false,
|
|
384
|
+
kind: 'timeout',
|
|
385
|
+
message: `aibridge: agy timed out after ~${task.timeoutSec}s; raise --timeout.`,
|
|
386
|
+
exitCode: result.code,
|
|
387
|
+
};
|
|
388
|
+
}
|
|
435
389
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
message: `aibridge: agy returned more than ${RAW_FALLBACK_MAX_CHARS} characters without a recognized stream protocol; see the run log.`,
|
|
441
|
-
exitCode: result.code,
|
|
442
|
-
};
|
|
443
|
-
}
|
|
390
|
+
const snap = useStreamJson && forwarder ? forwarder.snapshot() : undefined;
|
|
391
|
+
const resolvedAnswer = snap
|
|
392
|
+
? clean(snap.text ?? (snap.sawProtocol || snap.rawTruncated ? '' : snap.rawFallback))
|
|
393
|
+
: clean(result.stdout);
|
|
444
394
|
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
}
|
|
395
|
+
if (snap && !snap.sawProtocol && snap.rawTruncated && resolvedAnswer.length === 0) {
|
|
396
|
+
return {
|
|
397
|
+
ok: false,
|
|
398
|
+
kind: 'no-answer',
|
|
399
|
+
message: `aibridge: agy returned more than ${RAW_FALLBACK_MAX_CHARS} characters without a recognized stream protocol; see the run log.`,
|
|
400
|
+
exitCode: result.code,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
454
403
|
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
404
|
+
if (snap?.resultStatus === 'ERROR' || result.code !== 0 || resolvedAnswer.length === 0) {
|
|
405
|
+
const detail = clean(snap?.resultError || result.stderr) || `exit code ${result.code}`;
|
|
406
|
+
return {
|
|
407
|
+
ok: false,
|
|
408
|
+
kind: 'no-answer',
|
|
409
|
+
message: `aibridge: agy returned no usable answer (${detail}).`,
|
|
410
|
+
exitCode: result.code,
|
|
411
|
+
};
|
|
458
412
|
}
|
|
413
|
+
|
|
414
|
+
return { ok: true, response: resolvedAnswer, exitCode: result.code ?? 0 };
|
|
459
415
|
}
|