agy-delegate-mcp 0.0.0 → 1.0.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.
@@ -0,0 +1,159 @@
1
+ // Everything describing the turn in progress, in one object because it has to be wiped as one:
2
+ // session/load replays the previous turn as ordinary update frames, so anything left behind
3
+ // leaks into the next result. Fields are declared in exactly one place, freshFields(), which is
4
+ // also what reset() applies — a field added there is reset for free, and one that is missed is a
5
+ // silent stale-data bug on resume.
6
+
7
+ const isTerminalToolStatus = (status) =>
8
+ status === "completed" || status === "failed" || status === "cancelled";
9
+
10
+ // Each stream reports its newest complete sentence, at most one per throttle window.
11
+ // Capital-letter boundary: an agent can stream sentences with no separator between them.
12
+ const SENTENCE_END = /[.!?](?=\s|[A-Z])|\n/;
13
+ const MARKDOWN_LINE = /^(?:[|#>`~*_=+-]|\d+[.)]\s)/;
14
+ /**
15
+ * @param {string} prefix
16
+ * @param {((message: string) => void) | undefined} onProgress
17
+ * @param {number} throttleMs
18
+ */
19
+ function progressStream(prefix, onProgress, throttleMs) {
20
+ let buf = "", pending = null, lastEmit = 0;
21
+ const flush = (force) => {
22
+ if (pending === null || (!force && Date.now() - lastEmit < throttleMs)) return;
23
+ lastEmit = Date.now();
24
+ try { onProgress?.(prefix + pending); } catch {}
25
+ pending = null;
26
+ };
27
+ const take = (s) => {
28
+ const line = s.replace(/\s+/g, " ").trim().slice(0, 200);
29
+ if (line.length > 3 && !MARKDOWN_LINE.test(line)) pending = line;
30
+ flush(false);
31
+ };
32
+ return {
33
+ push(text) {
34
+ buf += text;
35
+ for (let m; (m = SENTENCE_END.exec(buf)); buf = buf.slice(m.index + 1)) {
36
+ take(buf.slice(0, m.index + 1));
37
+ }
38
+ if (buf.length > 300) { take(buf); buf = ""; }
39
+ },
40
+ end() { take(buf); buf = ""; flush(true); },
41
+ reset() { buf = ""; pending = null; },
42
+ };
43
+ }
44
+
45
+ // The single declaration of what a turn accumulates.
46
+ function freshFields() {
47
+ return {
48
+ resultChunks: [],
49
+ // Text superseded by a later tool call is normally a preamble ("Inspecting the
50
+ // implementation."), but the rule cannot tell a preamble from the whole answer: an agent
51
+ // that replies and then runs one more command has its entire reply discarded, leaving the
52
+ // caller "" with stopReason end_turn and no error. Kept so it can be handed back when
53
+ // nothing survived — labelled, never blended with a real final message.
54
+ discardedResult: "",
55
+ sawToolCall: false,
56
+ collectingPostToolResult: false,
57
+ activeToolCalls: new Set(),
58
+ // Ids observed terminal, so a stale non-terminal re-send for one is recognized and dropped
59
+ // instead of restarting collection — which would demote an already-collected final message
60
+ // to discardedResult and mislabel it pre-tool-fallback.
61
+ finishedToolCalls: new Set(),
62
+ // agy writes its plan as an artifact: a write_file call carrying ArtifactMetadata, saved
63
+ // under ~/.gemini/antigravity-cli/brain/<sessionId>/. Keyed by path, so a rewrite of the
64
+ // same artifact replaces the earlier entry.
65
+ artifacts: new Map(),
66
+ touched: new Set(),
67
+ lastToolLabel: null,
68
+ sessionTitle: undefined,
69
+ };
70
+ }
71
+
72
+ /**
73
+ * @param {{ onProgress?: (message: string) => void, progressThrottleMs?: number }} [opts]
74
+ */
75
+ export function makeTurnState({ onProgress, progressThrottleMs = 2000 } = {}) {
76
+ const state = {
77
+ ...freshFields(),
78
+ thoughts: progressStream("thinking: ", onProgress, progressThrottleMs),
79
+ messages: progressStream("agy: ", onProgress, progressThrottleMs),
80
+
81
+ reset() {
82
+ Object.assign(state, freshFields());
83
+ state.thoughts.reset();
84
+ state.messages.reset();
85
+ },
86
+
87
+ text() {
88
+ return state.resultChunks.join("");
89
+ },
90
+
91
+ resetResult() {
92
+ if (state.resultChunks.length > 0) state.discardedResult = state.resultChunks.join("");
93
+ state.resultChunks.length = 0;
94
+ },
95
+
96
+ // The stream is the model's own message, bounded by its output limit; a byte cap
97
+ // here would hand back a partial answer as if it were the whole one.
98
+ appendResult(text) {
99
+ state.resultChunks.push(text);
100
+ },
101
+
102
+ startTool(toolCallId, status) {
103
+ state.sawToolCall = true;
104
+ state.collectingPostToolResult = false;
105
+ state.resetResult();
106
+ if (toolCallId != null && !isTerminalToolStatus(status)) {
107
+ state.activeToolCalls.add(toolCallId);
108
+ // A fresh tool_call frame for a finished id is the agent reusing it, not history.
109
+ state.finishedToolCalls.delete(toolCallId);
110
+ }
111
+ if (toolCallId != null && isTerminalToolStatus(status)) state.finishedToolCalls.add(toolCallId);
112
+ if (isTerminalToolStatus(status) && state.activeToolCalls.size === 0) state.collectingPostToolResult = true;
113
+ },
114
+
115
+ updateToolStatus(toolCallId, status) {
116
+ if (!status) return;
117
+ if (toolCallId != null && !isTerminalToolStatus(status) && state.finishedToolCalls.has(toolCallId)) return;
118
+ if (!state.sawToolCall) {
119
+ state.sawToolCall = true;
120
+ state.resetResult();
121
+ }
122
+ if (isTerminalToolStatus(status)) {
123
+ if (toolCallId != null) {
124
+ state.activeToolCalls.delete(toolCallId);
125
+ state.finishedToolCalls.add(toolCallId);
126
+ }
127
+ if (state.activeToolCalls.size === 0 && !state.collectingPostToolResult) {
128
+ // Discard any message text emitted while tools were still running.
129
+ // A duplicate or late terminal update must not wipe an already-collected final message.
130
+ state.resetResult();
131
+ state.collectingPostToolResult = true;
132
+ }
133
+ } else {
134
+ state.collectingPostToolResult = false;
135
+ state.resetResult();
136
+ if (toolCallId != null) state.activeToolCalls.add(toolCallId);
137
+ }
138
+ },
139
+
140
+ // True while a message chunk still belongs in the result: before any tool ran, or after
141
+ // the last one finished.
142
+ collectingResult() {
143
+ return !state.sawToolCall || (state.collectingPostToolResult && state.activeToolCalls.size === 0);
144
+ },
145
+
146
+ // The artifact's content arrives whole in rawInput.Content; Path is where agy saved it.
147
+ recordArtifact(input) {
148
+ const meta = input?.ArtifactMetadata;
149
+ if (!meta || typeof meta !== "object" || typeof input.Path !== "string" || !input.Path) return false;
150
+ state.artifacts.set(input.Path, {
151
+ path: input.Path,
152
+ ...(typeof meta.Summary === "string" ? { summary: meta.Summary } : {}),
153
+ ...(typeof input.Content === "string" ? { content: input.Content } : {}),
154
+ });
155
+ return true;
156
+ },
157
+ };
158
+ return state;
159
+ }
package/src/version.js ADDED
@@ -0,0 +1,24 @@
1
+ import { readFileSync } from "node:fs";
2
+ import path from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+
5
+ const PKG_PATH = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "package.json");
6
+
7
+ function readVersion() {
8
+ return JSON.parse(readFileSync(PKG_PATH, "utf8")).version;
9
+ }
10
+
11
+ // Captured once, for the ACP/MCP identity handshake that is sent at connect and never re-read.
12
+ export const VERSION = readVersion();
13
+
14
+ // doctor reads fresh rather than using the constant: the MCP child is long-lived and
15
+ // /reload-plugins does not restart it, so a version captured at process start goes stale after
16
+ // an in-place upgrade — exactly when doctor is run to confirm the new version shipped. An
17
+ // un-restarted child running old code stays a gap, but no in-process value can close that.
18
+ export function readPackageVersion() {
19
+ try {
20
+ return readVersion();
21
+ } catch {
22
+ return VERSION;
23
+ }
24
+ }