@juanbenjumea/pi-agy 0.7.13 → 0.7.15
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/CHANGELOG.md +21 -0
- package/README.md +10 -2
- package/extensions/index.ts +29 -7
- package/extensions/lib/args.ts +51 -24
- package/extensions/lib/cli.ts +2 -0
- package/extensions/lib/execute.ts +7 -3
- package/extensions/lib/inspect.ts +112 -17
- package/extensions/lib/model-sort.ts +11 -3
- package/extensions/lib/provider-driver.ts +115 -15
- package/extensions/lib/provider-mcp.ts +11 -23
- package/extensions/lib/provider-models.ts +7 -9
- package/extensions/lib/provider-prompt.ts +137 -25
- package/extensions/lib/provider-tools.ts +16 -1
- package/extensions/lib/provider-usage.ts +115 -0
- package/extensions/lib/provider.ts +256 -86
- package/extensions/lib/stream.ts +2 -0
- package/extensions/tools.ts +6 -0
- package/package.json +4 -2
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
* Antigravity provider turn runtime (`antigravity/*` models in `/model`).
|
|
3
3
|
*
|
|
4
4
|
* Each Pi turn runs on a persistent agy driver process (see
|
|
5
|
-
* provider-driver.ts) with
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
5
|
+
* provider-driver.ts) with full transcript rendering on cold start and delta
|
|
6
|
+
* prompts on synced resumed conversations. Response text and real model
|
|
7
|
+
* thinking stream into Pi's TUI, while agy step/tool progress routes to Pi's
|
|
8
|
+
* live status bar. By default, a loopback MCP bridge parks agy's Pi-tool
|
|
9
|
+
* calls (micro-batched for parallel calls) for execution by Pi, so its normal
|
|
10
|
+
* permissions, approvals, and diff review apply. The bridge is separate from
|
|
11
|
+
* agy's own tools and can be disabled with `providerBridge: false`.
|
|
11
12
|
*/
|
|
12
13
|
|
|
13
14
|
import { randomBytes } from "node:crypto";
|
|
@@ -22,9 +23,11 @@ import {
|
|
|
22
23
|
collectImageBlocks,
|
|
23
24
|
ensureImageStagingRoot,
|
|
24
25
|
getLiveImageStagingCount,
|
|
26
|
+
renderProviderDeltaPrompt,
|
|
25
27
|
renderProviderPrompt,
|
|
26
28
|
stageImages,
|
|
27
29
|
textOfContent,
|
|
30
|
+
transcriptFingerprint,
|
|
28
31
|
type ProviderTranscriptMessage,
|
|
29
32
|
} from "./provider-prompt.js";
|
|
30
33
|
export {
|
|
@@ -32,23 +35,31 @@ export {
|
|
|
32
35
|
collectImageBlocks,
|
|
33
36
|
ensureImageStagingRoot,
|
|
34
37
|
imageExtension,
|
|
38
|
+
renderProviderDeltaPrompt,
|
|
35
39
|
renderProviderPrompt,
|
|
36
40
|
stageImages,
|
|
41
|
+
transcriptFingerprint,
|
|
37
42
|
MAX_IMAGE_BYTES,
|
|
38
43
|
MAX_STAGED_IMAGES,
|
|
39
44
|
type PromptImageBlock,
|
|
40
45
|
type ProviderTranscriptMessage,
|
|
46
|
+
type RenderProviderPromptOptions,
|
|
41
47
|
type StagedImages,
|
|
42
48
|
} from "./provider-prompt.js";
|
|
43
49
|
|
|
44
50
|
import { withDirLock } from "./lock.js";
|
|
45
|
-
import {
|
|
46
|
-
|
|
47
|
-
type BridgeToolDef,
|
|
48
|
-
type McpBridgeHandle,
|
|
49
|
-
} from "./provider-mcp.js";
|
|
51
|
+
import type { McpBridgeHandle } from "./provider-mcp.js";
|
|
52
|
+
import type { BridgeToolDef } from "./provider-tools.js";
|
|
50
53
|
import { getSession, saveSession } from "./sessions.js";
|
|
51
54
|
import { AgyProviderDriver, PROVIDER_TURN_TIMEOUT_MS } from "./provider-driver.js";
|
|
55
|
+
import type { NormalizedProviderUsage } from "./provider-usage.js";
|
|
56
|
+
export {
|
|
57
|
+
diffAgyUsage,
|
|
58
|
+
validateAgyUsage,
|
|
59
|
+
ZERO_USAGE_BASELINE,
|
|
60
|
+
type NormalizedProviderUsage,
|
|
61
|
+
type ValidatedAgyUsage,
|
|
62
|
+
} from "./provider-usage.js";
|
|
52
63
|
import {
|
|
53
64
|
toProviderEffort,
|
|
54
65
|
validateAgyModelId,
|
|
@@ -67,9 +78,31 @@ export const AGY_PROVIDER_API = "agy-provider";
|
|
|
67
78
|
|
|
68
79
|
export interface ProviderTurnCallbacks {
|
|
69
80
|
onText?: (delta: string) => void;
|
|
81
|
+
onThinking?: (delta: string) => void;
|
|
70
82
|
onActivity?: (label: string) => void;
|
|
71
83
|
}
|
|
72
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Build session-only `agy --settings` JSON that denies agy's built-in shell
|
|
87
|
+
* and file-write tools when Pi already bridges their equivalents via MCP,
|
|
88
|
+
* preventing built-in tools from shadowing Pi's native approvals and diffs.
|
|
89
|
+
* Intentionally emits only `deny` rules (no blanket `allow`) so other MCP
|
|
90
|
+
* servers keep the user's configured permission policy.
|
|
91
|
+
*/
|
|
92
|
+
export function buildBridgeSettings(bridgedTools: readonly BridgeToolDef[]): string | undefined {
|
|
93
|
+
if (bridgedTools.length === 0) return undefined;
|
|
94
|
+
const names = new Set(bridgedTools.map((tool) => tool.piName.toLowerCase()));
|
|
95
|
+
const deny: string[] = [];
|
|
96
|
+
if (names.has("bash") || names.has("shell") || names.has("run_command")) {
|
|
97
|
+
deny.push("command(*)");
|
|
98
|
+
}
|
|
99
|
+
if (names.has("write") || names.has("edit") || names.has("write_file") || names.has("edit_file")) {
|
|
100
|
+
deny.push("write_file(*)");
|
|
101
|
+
}
|
|
102
|
+
if (deny.length === 0) return undefined;
|
|
103
|
+
return JSON.stringify({ permissions: { deny } });
|
|
104
|
+
}
|
|
105
|
+
|
|
73
106
|
export interface ProviderTurnRequest {
|
|
74
107
|
/** Exact agy `--model` value (base or qualified slug). */
|
|
75
108
|
fullModel: string;
|
|
@@ -102,7 +135,7 @@ export interface ProviderTurnOwnership {
|
|
|
102
135
|
|
|
103
136
|
export type ProviderRunEnd =
|
|
104
137
|
| { kind: "done"; result: ProviderTurnResult }
|
|
105
|
-
| { kind: "parked"; park: ProviderToolPark; ownership?: ProviderTurnOwnership };
|
|
138
|
+
| { kind: "parked"; park: ProviderToolPark; parks?: ProviderToolPark[]; ownership?: ProviderTurnOwnership };
|
|
106
139
|
|
|
107
140
|
export interface ProviderToolDelivery {
|
|
108
141
|
callId: string;
|
|
@@ -137,6 +170,8 @@ export interface ProviderTurnResult {
|
|
|
137
170
|
/** Bounded agy tool-activity labels observed during the turn. */
|
|
138
171
|
activity: string[];
|
|
139
172
|
deniedActions?: Array<{ action?: string; display_name?: string }>;
|
|
173
|
+
/** Validated per-turn native usage delta, when available. */
|
|
174
|
+
usage?: NormalizedProviderUsage;
|
|
140
175
|
}
|
|
141
176
|
|
|
142
177
|
/**
|
|
@@ -215,10 +250,12 @@ export function getProviderRunner(dir: string): ProviderTurnRunner {
|
|
|
215
250
|
dir: request.dir, fullModel: request.fullModel, effort: request.effort,
|
|
216
251
|
skipPermissions: request.skipPermissions, bridgeDir: bridge?.configDir,
|
|
217
252
|
bridgeTools: bridge ? fingerprint : undefined, imageRoot,
|
|
253
|
+
settingsJson: bridge ? buildBridgeSettings(request.bridgedTools) : undefined,
|
|
218
254
|
}, {
|
|
219
255
|
prompt: request.prompt, resumeConversationId: request.resumeConversationId,
|
|
220
256
|
signal: request.signal, timeoutMs: remaining,
|
|
221
|
-
onText: request.callbacks?.onText,
|
|
257
|
+
onText: request.callbacks?.onText, onThinking: request.callbacks?.onThinking,
|
|
258
|
+
onActivity: request.callbacks?.onActivity,
|
|
222
259
|
});
|
|
223
260
|
return finishPhase(owned, end);
|
|
224
261
|
} catch (error) {
|
|
@@ -244,7 +281,8 @@ export function getProviderRunner(dir: string): ProviderTurnRunner {
|
|
|
244
281
|
}
|
|
245
282
|
const end = await driver.continueTurn({
|
|
246
283
|
signal: resumeSignal, timeoutMs: resumeTimeoutMs,
|
|
247
|
-
onText: callbacks?.onText,
|
|
284
|
+
onText: callbacks?.onText, onThinking: callbacks?.onThinking,
|
|
285
|
+
onActivity: callbacks?.onActivity,
|
|
248
286
|
});
|
|
249
287
|
return finishPhase(owned, end);
|
|
250
288
|
} catch (error) {
|
|
@@ -264,9 +302,22 @@ export function getProviderRunner(dir: string): ProviderTurnRunner {
|
|
|
264
302
|
return runner;
|
|
265
303
|
}
|
|
266
304
|
|
|
267
|
-
function toRunEnd(
|
|
305
|
+
function toRunEnd(
|
|
306
|
+
end:
|
|
307
|
+
| { kind: "done"; outcome: ProviderTurnResult }
|
|
308
|
+
| {
|
|
309
|
+
kind: "parked";
|
|
310
|
+
park: { callId: string; toolName: string; args: Record<string, unknown> };
|
|
311
|
+
parks?: Array<{ callId: string; toolName: string; args: Record<string, unknown> }>;
|
|
312
|
+
},
|
|
313
|
+
): ProviderRunEnd {
|
|
268
314
|
if (end.kind === "parked") {
|
|
269
|
-
|
|
315
|
+
const parks = (end.parks && end.parks.length > 0 ? end.parks : [end.park]).map((park) => ({
|
|
316
|
+
callId: park.callId,
|
|
317
|
+
toolName: park.toolName,
|
|
318
|
+
args: park.args,
|
|
319
|
+
}));
|
|
320
|
+
return { kind: "parked", park: parks[0], parks };
|
|
270
321
|
}
|
|
271
322
|
return { kind: "done", result: end.outcome };
|
|
272
323
|
}
|
|
@@ -303,6 +354,8 @@ async function ensureProviderBridge(dir: string): Promise<McpBridgeHandle | unde
|
|
|
303
354
|
try {
|
|
304
355
|
const config = await loadAgyConfig();
|
|
305
356
|
if (config.providerBridge !== false) {
|
|
357
|
+
// Keep the bridge and its MCP SDK off the extension startup path.
|
|
358
|
+
const { startMcpBridge } = await import("./provider-mcp.js");
|
|
306
359
|
handle = await startMcpBridge(dir);
|
|
307
360
|
}
|
|
308
361
|
} catch {
|
|
@@ -378,7 +431,9 @@ export function getProviderStatus(dir: string): ProviderStatus {
|
|
|
378
431
|
const suspended: ProviderStatusSuspended[] = [];
|
|
379
432
|
for (const [suspendedDir, entry] of suspendedByDir) {
|
|
380
433
|
if (suspendedDir === key) {
|
|
381
|
-
|
|
434
|
+
for (const call of entry.calls) {
|
|
435
|
+
suspended.push({ toolCallId: call.toolCallId, toolName: call.park.toolName, modelId: entry.modelId });
|
|
436
|
+
}
|
|
382
437
|
}
|
|
383
438
|
}
|
|
384
439
|
try {
|
|
@@ -426,6 +481,8 @@ export interface AgyProviderStreamDeps {
|
|
|
426
481
|
timeoutMs?: number;
|
|
427
482
|
/** Pi tools to bridge into agy turns (projected per turn by the caller). */
|
|
428
483
|
tools?: ProviderToolSource[];
|
|
484
|
+
/** Live status callback for agy step/tool progress (undefined clears status). */
|
|
485
|
+
onActivity?: (label: string | undefined) => void;
|
|
429
486
|
}
|
|
430
487
|
|
|
431
488
|
export interface AgyStreamSimpleModel {
|
|
@@ -522,18 +579,35 @@ function hasUserMessageAfterToolCall(messages: ProviderTranscriptMessage[], tool
|
|
|
522
579
|
return false;
|
|
523
580
|
}
|
|
524
581
|
|
|
582
|
+
interface SuspendedToolCall {
|
|
583
|
+
toolCallId: string;
|
|
584
|
+
park: ProviderToolPark;
|
|
585
|
+
}
|
|
586
|
+
|
|
525
587
|
interface SuspendedTurn {
|
|
526
588
|
/** Original executed selector, retained across reasoning/catalog changes and re-parks. */
|
|
527
589
|
rawModelId: AgyModelId;
|
|
528
590
|
runner: ProviderTurnRunner;
|
|
529
591
|
ownership?: ProviderTurnOwnership;
|
|
530
|
-
|
|
531
|
-
park: ProviderToolPark;
|
|
592
|
+
calls: SuspendedToolCall[];
|
|
532
593
|
modelId: string;
|
|
594
|
+
/** System prompt actually sent on the originating turn, preserved for delta sync. */
|
|
595
|
+
systemPrompt: string;
|
|
596
|
+
/** Bridged tool catalog fingerprint sent on the originating turn. */
|
|
597
|
+
bridgeToolsKey: string;
|
|
533
598
|
/** Image staging cleanups owned by the suspended turn. */
|
|
534
599
|
imageCleanups: Array<() => Promise<void>>;
|
|
535
600
|
}
|
|
536
601
|
|
|
602
|
+
interface ConversationSyncState {
|
|
603
|
+
conversationId: string;
|
|
604
|
+
systemPrompt: string;
|
|
605
|
+
bridgeToolsKey: string;
|
|
606
|
+
syncedInputCount: number;
|
|
607
|
+
syncedInputFingerprint: string;
|
|
608
|
+
syncedAssistantFingerprint: string;
|
|
609
|
+
}
|
|
610
|
+
|
|
537
611
|
/** Run cleanups sequentially, never throwing. */
|
|
538
612
|
async function runImageCleanups(cleanups: Array<() => Promise<void>>): Promise<void> {
|
|
539
613
|
for (const cleanup of cleanups) {
|
|
@@ -577,6 +651,7 @@ export function createAgyStreamSimple(
|
|
|
577
651
|
options?: AgyStreamSimpleOptions,
|
|
578
652
|
) => AgyEventStream {
|
|
579
653
|
const resumeByModel = new Map<string, string>();
|
|
654
|
+
const syncByModel = new Map<string, ConversationSyncState>();
|
|
580
655
|
const getRunner = (dir: string): ProviderTurnRunner => deps.runner ?? getProviderRunner(dir);
|
|
581
656
|
|
|
582
657
|
return (model, messages, options) => {
|
|
@@ -592,6 +667,7 @@ export function createAgyStreamSimple(
|
|
|
592
667
|
output: number;
|
|
593
668
|
cacheRead: number;
|
|
594
669
|
cacheWrite: number;
|
|
670
|
+
reasoning?: number;
|
|
595
671
|
totalTokens: number;
|
|
596
672
|
cost: { input: number; output: number; cacheRead: number; cacheWrite: number; total: number };
|
|
597
673
|
};
|
|
@@ -635,6 +711,13 @@ export function createAgyStreamSimple(
|
|
|
635
711
|
// park transfers ownership into the suspension entry. Declared here
|
|
636
712
|
// so both the try body and the catch below can clean it.
|
|
637
713
|
const pendingImageCleanups: Array<() => Promise<void>> = [];
|
|
714
|
+
const setActivityStatus = (label: string | undefined): void => {
|
|
715
|
+
try {
|
|
716
|
+
deps.onActivity?.(label);
|
|
717
|
+
} catch {
|
|
718
|
+
// Live status surfaces must never disrupt a turn.
|
|
719
|
+
}
|
|
720
|
+
};
|
|
638
721
|
|
|
639
722
|
try {
|
|
640
723
|
const systemPrompt = factory.getSystemPrompt(messages);
|
|
@@ -643,68 +726,87 @@ export function createAgyStreamSimple(
|
|
|
643
726
|
// conversation instead of inheriting stale context.
|
|
644
727
|
const hasPriorAssistantTurn = messages.some((message) => message.role === "assistant");
|
|
645
728
|
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
729
|
+
let textIndex = -1;
|
|
730
|
+
let thinkingIndex = -1;
|
|
731
|
+
const ensureText = (): number => {
|
|
732
|
+
if (textIndex === -1) {
|
|
733
|
+
output.content.push({ type: "text", text: "" });
|
|
734
|
+
textIndex = output.content.length - 1;
|
|
735
|
+
stream.push({ type: "text_start", contentIndex: textIndex, partial: output });
|
|
736
|
+
}
|
|
737
|
+
return textIndex;
|
|
738
|
+
};
|
|
739
|
+
const ensureThinking = (): number => {
|
|
740
|
+
if (thinkingIndex === -1) {
|
|
741
|
+
output.content.push({ type: "thinking", thinking: "" });
|
|
742
|
+
thinkingIndex = output.content.length - 1;
|
|
743
|
+
stream.push({ type: "thinking_start", contentIndex: thinkingIndex, partial: output });
|
|
744
|
+
}
|
|
745
|
+
return thinkingIndex;
|
|
746
|
+
};
|
|
664
747
|
|
|
665
748
|
const callbacks: ProviderTurnCallbacks = {
|
|
666
749
|
onText: (delta) => {
|
|
667
750
|
const index = ensureText();
|
|
668
751
|
const block = output.content[index] as { text: string };
|
|
669
752
|
block.text += delta;
|
|
670
|
-
output.usage.output += delta.length;
|
|
671
753
|
stream.push({ type: "text_delta", contentIndex: index, delta, partial: output });
|
|
672
754
|
},
|
|
673
|
-
|
|
755
|
+
onThinking: (delta) => {
|
|
674
756
|
const index = ensureThinking();
|
|
675
757
|
const block = output.content[index] as { thinking: string };
|
|
676
|
-
block.thinking +=
|
|
677
|
-
stream.push({ type: "thinking_delta", contentIndex: index, delta
|
|
758
|
+
block.thinking += delta;
|
|
759
|
+
stream.push({ type: "thinking_delta", contentIndex: index, delta, partial: output });
|
|
760
|
+
},
|
|
761
|
+
onActivity: (label) => {
|
|
762
|
+
setActivityStatus(label);
|
|
678
763
|
},
|
|
679
764
|
};
|
|
680
765
|
|
|
681
766
|
const runner = getRunner(dir);
|
|
682
767
|
const skey = path.resolve(dir);
|
|
683
768
|
const prior = suspendedByDir.get(skey);
|
|
684
|
-
const
|
|
769
|
+
const deliveredByCall = prior
|
|
770
|
+
? prior.calls.map((call) => ({
|
|
771
|
+
call,
|
|
772
|
+
bodies: collectParkedDeliveries(messages, call.toolCallId),
|
|
773
|
+
}))
|
|
774
|
+
: [];
|
|
775
|
+
const hasCompleteDelivery =
|
|
776
|
+
!!prior &&
|
|
777
|
+
deliveredByCall.length > 0 &&
|
|
778
|
+
deliveredByCall.every((entry) => entry.bodies.length > 0);
|
|
685
779
|
let end: ProviderRunEnd;
|
|
686
780
|
let executedRawModelId: AgyModelId;
|
|
687
|
-
|
|
781
|
+
let activeSystemPrompt = systemPrompt;
|
|
782
|
+
let activeBridgeToolsKey = "";
|
|
783
|
+
if (prior && hasCompleteDelivery) {
|
|
688
784
|
executedRawModelId = prior.rawModelId;
|
|
785
|
+
activeSystemPrompt = prior.systemPrompt;
|
|
786
|
+
activeBridgeToolsKey = prior.bridgeToolsKey;
|
|
689
787
|
prior.ownership?.assertValid();
|
|
690
788
|
suspendedByDir.delete(skey);
|
|
691
|
-
|
|
692
|
-
// the resumed agy turn reads them by path. Owned by this
|
|
693
|
-
// invocation; cleaned on its terminal settle below.
|
|
694
|
-
const deliveryImages = await stageImages(collectImageBlocks(messages, prior.toolCallId));
|
|
695
|
-
const resumeCleanups = [...prior.imageCleanups, deliveryImages.cleanup];
|
|
696
|
-
const imageRefs = [...deliveryImages.paths.values()];
|
|
789
|
+
const resumeCleanups = [...prior.imageCleanups];
|
|
697
790
|
// Deliver by bridge park id (what agy waits on), matched via the
|
|
698
|
-
// shadow toolCall id (what Pi's transcript carries).
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
791
|
+
// shadow toolCall id (what Pi's transcript carries). Images riding
|
|
792
|
+
// on each delivered tool result are staged per tool call.
|
|
793
|
+
const deliveries: ProviderToolDelivery[] = [];
|
|
794
|
+
for (const { call, bodies } of deliveredByCall) {
|
|
795
|
+
const deliveryImages = await stageImages(collectImageBlocks(messages, call.toolCallId));
|
|
796
|
+
resumeCleanups.push(deliveryImages.cleanup);
|
|
797
|
+
const imageRefs = [...deliveryImages.paths.values()];
|
|
798
|
+
for (const body of bodies) {
|
|
799
|
+
deliveries.push({
|
|
800
|
+
callId: call.park.callId,
|
|
801
|
+
text:
|
|
802
|
+
body.text +
|
|
803
|
+
(imageRefs.length > 0
|
|
804
|
+
? `\n[attached images saved at: ${imageRefs.join(", ")} — open them with your file tools to view]`
|
|
805
|
+
: ""),
|
|
806
|
+
isError: body.isError,
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
}
|
|
708
810
|
try {
|
|
709
811
|
end = await runner.resume(
|
|
710
812
|
deliveries,
|
|
@@ -727,7 +829,7 @@ export function createAgyStreamSimple(
|
|
|
727
829
|
// park carries it into the suspension, terminal settle cleans it.
|
|
728
830
|
pendingImageCleanups.push(...resumeCleanups);
|
|
729
831
|
} else {
|
|
730
|
-
if (prior && hasUserMessageAfterToolCall(messages,
|
|
832
|
+
if (prior && prior.calls.some((call) => hasUserMessageAfterToolCall(messages, call.toolCallId))) {
|
|
731
833
|
// Our own transcript moved on without delivering (abort, reject,
|
|
732
834
|
// retry): cancel the stale suspension and start fresh instead
|
|
733
835
|
// of bricking the directory behind the suspended guard.
|
|
@@ -750,13 +852,35 @@ export function createAgyStreamSimple(
|
|
|
750
852
|
// Session-store failures must never block a turn.
|
|
751
853
|
}
|
|
752
854
|
}
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
855
|
+
const bridgedTools = projectBridgeTools(options?.tools ?? deps.tools ?? []);
|
|
856
|
+
activeBridgeToolsKey = bridgeToolsFingerprint(bridgedTools);
|
|
857
|
+
const sync = resume ? syncByModel.get(model.id) : undefined;
|
|
858
|
+
const canDelta =
|
|
859
|
+
!!sync &&
|
|
860
|
+
sync.conversationId === resume &&
|
|
861
|
+
sync.systemPrompt === systemPrompt &&
|
|
862
|
+
sync.bridgeToolsKey === activeBridgeToolsKey &&
|
|
863
|
+
messages.length > sync.syncedInputCount + 1 &&
|
|
864
|
+
messages[sync.syncedInputCount]?.role === "assistant" &&
|
|
865
|
+
transcriptFingerprint(messages.slice(0, sync.syncedInputCount)) === sync.syncedInputFingerprint &&
|
|
866
|
+
transcriptFingerprint([messages[sync.syncedInputCount]]) === sync.syncedAssistantFingerprint;
|
|
867
|
+
let prompt: string;
|
|
868
|
+
if (canDelta) {
|
|
869
|
+
const deltaMessages = messages.slice(sync.syncedInputCount + 1);
|
|
870
|
+
const staged = await stageImages(collectImageBlocks(deltaMessages));
|
|
871
|
+
pendingImageCleanups.push(staged.cleanup);
|
|
872
|
+
prompt = renderProviderDeltaPrompt(deltaMessages, staged.paths);
|
|
873
|
+
} else {
|
|
874
|
+
// Materialize attached images as files agy's tools can open;
|
|
875
|
+
// paths render inline, failures degrade to omitted notes. Held
|
|
876
|
+
// for the driver turn: terminal settle below cleans it, a park
|
|
877
|
+
// carries it into the suspension.
|
|
878
|
+
const staged = await stageImages(collectImageBlocks(messages));
|
|
879
|
+
pendingImageCleanups.push(staged.cleanup);
|
|
880
|
+
prompt = renderProviderPrompt(messages, systemPrompt || undefined, staged.paths, {
|
|
881
|
+
bridgedToolNames: bridgedTools.map((tool) => tool.name),
|
|
882
|
+
});
|
|
883
|
+
}
|
|
760
884
|
// Capture before execution: later invocations can resolve a different
|
|
761
885
|
// profile while this child remains parked on its original target.
|
|
762
886
|
executedRawModelId = validateAgyModelId(
|
|
@@ -771,16 +895,17 @@ export function createAgyStreamSimple(
|
|
|
771
895
|
timeoutMs: deps.timeoutMs ?? PROVIDER_TURN_TIMEOUT_MS,
|
|
772
896
|
resumeConversationId: resume,
|
|
773
897
|
skipPermissions: deps.skipPermissions ?? true,
|
|
774
|
-
bridgedTools
|
|
898
|
+
bridgedTools,
|
|
775
899
|
callbacks,
|
|
776
900
|
});
|
|
777
901
|
}
|
|
778
902
|
|
|
779
903
|
if (end.kind === "parked") {
|
|
780
|
-
|
|
781
|
-
//
|
|
782
|
-
//
|
|
783
|
-
//
|
|
904
|
+
setActivityStatus(undefined);
|
|
905
|
+
// Bridged Pi tool call(s): end any open blocks, then emit REAL
|
|
906
|
+
// shadow toolCall blocks so Pi executes them with normal
|
|
907
|
+
// permissions, approvals, and review. The Pi turn delivering the
|
|
908
|
+
// matching toolResults resumes this driver turn.
|
|
784
909
|
if (thinkingIndex !== -1) {
|
|
785
910
|
const thinking = (output.content[thinkingIndex] as { thinking: string }).thinking;
|
|
786
911
|
stream.push({ type: "thinking_end", contentIndex: thinkingIndex, content: thinking, partial: output });
|
|
@@ -789,23 +914,41 @@ export function createAgyStreamSimple(
|
|
|
789
914
|
const text = (output.content[textIndex] as { text: string }).text;
|
|
790
915
|
stream.push({ type: "text_end", contentIndex: textIndex, content: text, partial: output });
|
|
791
916
|
}
|
|
792
|
-
const
|
|
793
|
-
const
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
917
|
+
const parks = end.parks && end.parks.length > 0 ? end.parks : [end.park];
|
|
918
|
+
const calls: SuspendedToolCall[] = [];
|
|
919
|
+
const toolCalls: Array<{
|
|
920
|
+
type: "toolCall";
|
|
921
|
+
id: string;
|
|
922
|
+
name: string;
|
|
923
|
+
arguments: Record<string, unknown>;
|
|
924
|
+
}> = [];
|
|
925
|
+
for (const park of parks) {
|
|
926
|
+
const toolCallId = `agybridge-${randomBytes(8).toString("hex")}`;
|
|
927
|
+
calls.push({ toolCallId, park });
|
|
928
|
+
toolCalls.push({
|
|
929
|
+
type: "toolCall",
|
|
930
|
+
id: toolCallId,
|
|
931
|
+
name: park.toolName,
|
|
932
|
+
arguments: park.args,
|
|
933
|
+
});
|
|
934
|
+
}
|
|
799
935
|
end.ownership?.assertValid();
|
|
800
936
|
suspendedByDir.set(skey, {
|
|
801
|
-
runner,
|
|
937
|
+
runner,
|
|
938
|
+
ownership: end.ownership,
|
|
939
|
+
calls,
|
|
802
940
|
rawModelId: executedRawModelId,
|
|
803
|
-
modelId: model.id,
|
|
941
|
+
modelId: model.id,
|
|
942
|
+
systemPrompt: activeSystemPrompt,
|
|
943
|
+
bridgeToolsKey: activeBridgeToolsKey,
|
|
944
|
+
imageCleanups: pendingImageCleanups,
|
|
804
945
|
});
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
946
|
+
for (const toolCall of toolCalls) {
|
|
947
|
+
output.content.push(toolCall);
|
|
948
|
+
const toolIndex = output.content.length - 1;
|
|
949
|
+
stream.push({ type: "toolcall_start", contentIndex: toolIndex, partial: output });
|
|
950
|
+
stream.push({ type: "toolcall_end", contentIndex: toolIndex, toolCall, partial: output });
|
|
951
|
+
}
|
|
809
952
|
output.usage.totalTokens =
|
|
810
953
|
output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
811
954
|
output.stopReason = "toolUse";
|
|
@@ -814,6 +957,7 @@ export function createAgyStreamSimple(
|
|
|
814
957
|
return;
|
|
815
958
|
}
|
|
816
959
|
const result = end.result;
|
|
960
|
+
setActivityStatus(undefined);
|
|
817
961
|
|
|
818
962
|
if (thinkingIndex !== -1) {
|
|
819
963
|
const thinking = (output.content[thinkingIndex] as { thinking: string }).thinking;
|
|
@@ -834,7 +978,19 @@ export function createAgyStreamSimple(
|
|
|
834
978
|
block.text = finalText;
|
|
835
979
|
stream.push({ type: "text_end", contentIndex: index, content: finalText, partial: output });
|
|
836
980
|
|
|
837
|
-
|
|
981
|
+
if (result.usage) {
|
|
982
|
+
output.usage.input = result.usage.input;
|
|
983
|
+
output.usage.output = result.usage.output;
|
|
984
|
+
output.usage.cacheRead = result.usage.cacheRead;
|
|
985
|
+
output.usage.cacheWrite = result.usage.cacheWrite;
|
|
986
|
+
if (result.usage.reasoning !== undefined) {
|
|
987
|
+
output.usage.reasoning = result.usage.reasoning;
|
|
988
|
+
}
|
|
989
|
+
output.usage.totalTokens = result.usage.totalTokens;
|
|
990
|
+
} else {
|
|
991
|
+
output.usage.totalTokens =
|
|
992
|
+
output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
993
|
+
}
|
|
838
994
|
// Partial turns must never masquerade as clean stops: surface the
|
|
839
995
|
// real terminal state so Pi retries or reports instead of accepting
|
|
840
996
|
// truncated text as a complete answer.
|
|
@@ -843,6 +999,18 @@ export function createAgyStreamSimple(
|
|
|
843
999
|
else output.stopReason = "stop";
|
|
844
1000
|
if (result.conversationId) {
|
|
845
1001
|
resumeByModel.set(model.id, result.conversationId);
|
|
1002
|
+
if (!result.aborted && !result.timedOut) {
|
|
1003
|
+
syncByModel.set(model.id, {
|
|
1004
|
+
conversationId: result.conversationId,
|
|
1005
|
+
systemPrompt: activeSystemPrompt,
|
|
1006
|
+
bridgeToolsKey: activeBridgeToolsKey,
|
|
1007
|
+
syncedInputCount: messages.length,
|
|
1008
|
+
syncedInputFingerprint: transcriptFingerprint(messages),
|
|
1009
|
+
syncedAssistantFingerprint: transcriptFingerprint([output]),
|
|
1010
|
+
});
|
|
1011
|
+
} else {
|
|
1012
|
+
syncByModel.delete(model.id);
|
|
1013
|
+
}
|
|
846
1014
|
const latestUser = [...messages].reverse().find((message) => message.role === "user");
|
|
847
1015
|
const summary = textOfContent(latestUser?.content).trim().slice(0, 80) || undefined;
|
|
848
1016
|
try {
|
|
@@ -861,6 +1029,8 @@ export function createAgyStreamSimple(
|
|
|
861
1029
|
stream.end();
|
|
862
1030
|
await runImageCleanups(pendingImageCleanups);
|
|
863
1031
|
} catch (error) {
|
|
1032
|
+
syncByModel.delete(model.id);
|
|
1033
|
+
setActivityStatus(undefined);
|
|
864
1034
|
output.stopReason = signal.aborted ? "aborted" : "error";
|
|
865
1035
|
output.errorMessage = error instanceof Error ? error.message : String(error);
|
|
866
1036
|
stream.push({ type: "error", reason: output.stopReason, error: output });
|
package/extensions/lib/stream.ts
CHANGED
package/extensions/tools.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
findAgyQuotaEntries,
|
|
10
10
|
formatAgyUsage,
|
|
11
11
|
inspectAgyAgents,
|
|
12
|
+
inspectAgyModels,
|
|
12
13
|
isAgyQuotaExhausted,
|
|
13
14
|
normalizeAgyAgentName,
|
|
14
15
|
resolveAgyModelAlias,
|
|
@@ -510,6 +511,11 @@ export function registerAgyTools(pi: ExtensionAPI): void {
|
|
|
510
511
|
async execute(_toolCallId, params, signal, _onUpdate, ctx) {
|
|
511
512
|
const cwd = await resolveToolDir(ctx.cwd, params.dir);
|
|
512
513
|
const quota = await checkAgyUsage(cwd, signal);
|
|
514
|
+
if (params.model) {
|
|
515
|
+
// Alias resolution reads the live catalog only. Discovery failure is not
|
|
516
|
+
// fatal here: the report degrades to "unknown" instead of naming a model.
|
|
517
|
+
await inspectAgyModels(cwd, signal).catch(() => undefined);
|
|
518
|
+
}
|
|
513
519
|
const selectedModel = params.model ? resolveAgyModelId(params.model) : undefined;
|
|
514
520
|
const selectedEntries = selectedModel ? findAgyQuotaEntries(quota, selectedModel) : [];
|
|
515
521
|
const quotaStatus = selectedModel
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juanbenjumea/pi-agy",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.15",
|
|
4
4
|
"description": "Antigravity (agy) provider and delegation for Pi — /model picker, background tasks, loopback MCP tool bridge, quota awareness, and repo verification.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -30,8 +30,10 @@
|
|
|
30
30
|
"scripts": {
|
|
31
31
|
"test": "node --import ./tests/resolve-hook.mjs --test tests/*.test.ts",
|
|
32
32
|
"test:live": "node --import ./tests/resolve-hook.mjs tests/live-smoke.ts",
|
|
33
|
+
"test:provider": "node --import ./tests/resolve-hook.mjs tests/live-provider.ts",
|
|
33
34
|
"typecheck": "tsc --noEmit --pretty false",
|
|
34
|
-
"pack:check": "npm pack --dry-run --json"
|
|
35
|
+
"pack:check": "npm pack --dry-run --json",
|
|
36
|
+
"test:bridge": "node --import ./tests/resolve-hook.mjs tests/live-bridge.ts"
|
|
35
37
|
},
|
|
36
38
|
"files": [
|
|
37
39
|
"LICENSE",
|