forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAntigravityTextGeneration.ts
More file actions
430 lines (410 loc) · 16.2 KB
/
Copy pathAntigravityTextGeneration.ts
File metadata and controls
430 lines (410 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import {
type ModelSelection,
type ProviderSetupError,
TextGenerationError,
} from "@t3tools/contracts";
import { sanitizeBranchFragment, sanitizeFeatureBranchName } from "@t3tools/shared/git";
import { extractJsonObject } from "@t3tools/shared/schemaJson";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Ref from "effect/Ref";
import * as Schema from "effect/Schema";
import * as Scope from "effect/Scope";
import * as Stream from "effect/Stream";
import { type AcpError, AcpRequestError } from "effect-acp/errors";
import { applyAntigravityAcpModelSelection } from "../provider/acp/AntigravityAcpSupport.ts";
import { removeAntigravitySessionFiles } from "../provider/acp/AntigravitySessionFiles.ts";
import type { AcpSessionRuntime } from "../provider/acp/AcpSessionRuntime.ts";
import type * as TextGeneration from "./TextGeneration.ts";
import {
buildBranchNamePrompt,
buildCommitMessagePrompt,
buildPrContentPrompt,
buildPullRequestRankingPrompt,
buildThreadTitlePrompt,
} from "./TextGenerationPrompts.ts";
import {
clampPullRequestRankings,
sanitizeCommitSubject,
sanitizePrTitle,
sanitizeThreadTitle,
} from "./TextGenerationUtils.ts";
const ANTIGRAVITY_TIMEOUT_MS = 180_000;
const MAX_OUTPUT_CHARS = 128_000;
const isTextGenerationError = Schema.is(TextGenerationError);
const isNativeSessionId = Schema.is(Schema.String.check(Schema.isUUID(4)));
const Configuration = Schema.Record(Schema.String, Schema.Unknown);
const decodeConfiguration = Schema.decodeEffect(Schema.fromJsonString(Configuration));
const decodeConfigurationObject = Schema.decodeUnknownEffect(Configuration);
type AntigravityTextRuntime = Pick<
AcpSessionRuntime["Service"],
| "start"
| "setMode"
| "getConfigOptions"
| "getEvents"
| "setModel"
| "prompt"
| "cancel"
| "handleSessionUpdate"
| "handleRequestPermission"
| "handleElicitation"
| "handleReadTextFile"
| "handleWriteTextFile"
| "handleCreateTerminal"
| "handleTerminalOutput"
| "handleTerminalWaitForExit"
| "handleTerminalKill"
| "handleTerminalRelease"
| "handleUnknownExtRequest"
>;
export interface AntigravityTextGenerationOptions {
readonly profileDirectory: string;
/** Model the provider default alias selects, when the account offers it. */
readonly defaultModel?: Effect.Effect<string | undefined>;
/** Uses the instance's personal Google login, with no injected MCP servers or client tools. */
readonly makeRuntime: (
cwd: string,
) => Effect.Effect<AntigravityTextRuntime, AcpError | ProviderSetupError, Scope.Scope>;
/** Registers the whole helper so sign-out can stop it before clearing credentials. */
readonly withProcess: <A, E, R>(
stop: Effect.Effect<void>,
task: Effect.Effect<A, E, R>,
) => Effect.Effect<A, E | ProviderSetupError, R | Scope.Scope>;
}
/** Global hooks and MCP servers can run before a helper can deny a tool request. */
export const isAntigravityTextGenerationAvailable = Effect.fn(
"isAntigravityTextGenerationAvailable",
)(function* (profileDirectory: string) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
for (const name of ["hooks.json", "mcp_config.json"]) {
const configurationPath = path.join(profileDirectory, "config", name);
if (!(yield* fs.exists(configurationPath))) {
continue;
}
const info = yield* fs.stat(configurationPath);
if (info.type !== "File" || info.size > 64_000n) {
return false;
}
const empty = yield* fs.readFileString(configurationPath).pipe(
Effect.flatMap(decodeConfiguration),
Effect.flatMap((configuration) =>
decodeConfigurationObject(
configuration[name === "hooks.json" ? "hooks" : "mcpServers"] ?? configuration,
),
),
Effect.map((configuration) => Object.keys(configuration).length === 0),
Effect.orElseSucceed(() => false),
);
if (!empty) return false;
}
return true;
});
/** Runs short-lived subscription helpers without giving them the user's workspace. */
export const makeAntigravityTextGeneration = Effect.fn("makeAntigravityTextGeneration")(function* (
options: AntigravityTextGenerationOptions,
) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const available = isAntigravityTextGenerationAvailable(options.profileDirectory).pipe(
Effect.provideService(FileSystem.FileSystem, fs),
Effect.provideService(Path.Path, path),
);
const runAntigravityJson = Effect.fn("AntigravityTextGeneration.runJson")(
function* <S extends Schema.Top>(input: {
readonly operation: keyof TextGeneration.TextGeneration["Service"];
readonly prompt: string;
readonly outputSchema: S;
readonly modelSelection: ModelSelection;
}) {
const { operation } = input;
const scope = yield* Scope.make();
yield* Effect.addFinalizer((exit) => Scope.close(scope, exit));
const helper = Effect.gen(function* () {
if (!(yield* available)) {
return yield* new TextGenerationError({
operation,
detail:
"Antigravity text generation is unavailable for profiles with global hooks or MCP configuration. Select another system model.",
});
}
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-antigravity-text-" });
let sessionId: string | undefined;
yield* Effect.addFinalizer(() =>
removeAntigravitySessionFiles({
profileDirectory: options.profileDirectory,
sessionId,
cwd,
}).pipe(
Effect.provideService(FileSystem.FileSystem, fs),
Effect.provideService(Path.Path, path),
),
);
const rawResult = yield* Effect.gen(function* () {
const runtime = yield* options.makeRuntime(cwd);
yield* runtime.getEvents().pipe(
Stream.runForEach((event) =>
event._tag === "EventStreamBarrier"
? Deferred.succeed(event.acknowledge, undefined).pipe(Effect.asVoid)
: Effect.void,
),
Effect.forkScoped,
);
const output = yield* Ref.make("");
const rejected = yield* Deferred.make<never, TextGenerationError>();
const reject = (detail: string) =>
Deferred.fail(rejected, new TextGenerationError({ operation, detail })).pipe(
Effect.asVoid,
);
const rejectToolRequest = () =>
reject("Antigravity text generation requested a tool or user input.").pipe(
Effect.andThen(
Effect.fail(
new AcpRequestError({
code: -32601,
errorMessage: "Tools and user input are disabled for text generation.",
}),
),
),
);
yield* runtime.handleRequestPermission(() =>
reject("Antigravity text generation requested a tool permission or user input.").pipe(
Effect.as({ outcome: { outcome: "cancelled" as const } }),
),
);
yield* runtime.handleElicitation(() =>
reject("Antigravity text generation requested user input.").pipe(
Effect.as({ action: { action: "decline" as const } }),
),
);
yield* runtime.handleReadTextFile(rejectToolRequest);
yield* runtime.handleWriteTextFile(rejectToolRequest);
yield* runtime.handleCreateTerminal(rejectToolRequest);
yield* runtime.handleTerminalOutput(rejectToolRequest);
yield* runtime.handleTerminalWaitForExit(rejectToolRequest);
yield* runtime.handleTerminalKill(rejectToolRequest);
yield* runtime.handleTerminalRelease(rejectToolRequest);
yield* runtime.handleUnknownExtRequest(rejectToolRequest);
yield* runtime.handleSessionUpdate((notification) =>
Effect.gen(function* () {
const update = notification.update;
if (
update.sessionUpdate === "tool_call" ||
update.sessionUpdate === "tool_call_update"
) {
return yield* reject("Antigravity attempted tool work during text generation.");
}
if (
notification.sessionId !== sessionId ||
update.sessionUpdate !== "agent_message_chunk" ||
update.content.type !== "text"
) {
return;
}
const text = update.content.text;
const exceeded = yield* Ref.modify(output, (current) =>
current.length + text.length > MAX_OUTPUT_CHARS
? [true, current]
: [false, current + text],
);
if (exceeded) {
return yield* reject("Antigravity text generation exceeded the output limit.");
}
}),
);
return yield* Effect.gen(function* () {
const started = yield* runtime.start();
sessionId = started.sessionId;
if (!isNativeSessionId(sessionId)) {
return yield* new TextGenerationError({
operation,
detail: "Antigravity returned an invalid text helper session ID.",
});
}
yield* runtime.setMode("default");
yield* applyAntigravityAcpModelSelection({
runtime,
model: input.modelSelection.model,
defaultModel: yield* options.defaultModel ?? Effect.succeed(undefined),
mapError: (cause) =>
new TextGenerationError({
operation,
detail: "Could not select the Antigravity model for text generation.",
cause,
}),
});
const result = yield* runtime.prompt({
prompt: [
{
type: "text",
text: [
"Use only the input below. Do not use tools, read or write files, run commands, or ask questions.",
"Return only the requested JSON object.",
"",
input.prompt,
].join("\n"),
},
],
});
if (yield* Deferred.isDone(rejected)) {
return yield* Deferred.await(rejected);
}
if (result.stopReason === "cancelled") {
return yield* new TextGenerationError({
operation,
detail: "Antigravity text generation was cancelled.",
});
}
return (yield* Ref.get(output)).trim();
}).pipe(
Effect.onInterrupt(() =>
runtime.cancel.pipe(Effect.timeoutOption(2_000), Effect.ignore),
),
Effect.raceFirst(Deferred.await(rejected)),
);
}).pipe(Effect.scoped);
if ((yield* fs.readDirectory(cwd)).length > 0) {
return yield* new TextGenerationError({
operation,
detail: "Antigravity wrote files during text generation.",
});
}
if (!rawResult) {
return yield* new TextGenerationError({
operation,
detail: "Antigravity returned empty text generation output.",
});
}
const decodeOutput = Schema.decodeEffect(Schema.fromJsonString(input.outputSchema));
return yield* decodeOutput(extractJsonObject(rawResult)).pipe(
Effect.mapError(
(cause) =>
new TextGenerationError({
operation,
detail: "Antigravity returned invalid structured output.",
cause,
}),
),
);
}).pipe(
Effect.scoped,
Effect.timeoutOption(ANTIGRAVITY_TIMEOUT_MS),
Effect.flatMap(
Option.match({
onNone: () =>
Effect.fail(
new TextGenerationError({
operation,
detail: "Antigravity text generation timed out.",
}),
),
onSome: Effect.succeed,
}),
),
);
return yield* options
.withProcess(Scope.close(scope, Exit.void), helper)
.pipe(Effect.provideService(Scope.Scope, scope));
},
(effect, input) =>
effect.pipe(
Effect.mapError((cause) =>
isTextGenerationError(cause)
? cause
: new TextGenerationError({
operation: input.operation,
detail: "Antigravity text generation failed.",
cause,
}),
),
Effect.scoped,
),
);
const generateCommitMessage: TextGeneration.TextGeneration["Service"]["generateCommitMessage"] =
Effect.fn("AntigravityTextGeneration.generateCommitMessage")(function* (input) {
const generated = yield* runAntigravityJson({
operation: "generateCommitMessage",
...buildCommitMessagePrompt({
branch: input.branch,
stagedSummary: input.stagedSummary,
stagedPatch: input.stagedPatch,
includeBranch: input.includeBranch === true,
policy: input.policy,
}),
modelSelection: input.modelSelection,
});
return {
subject: sanitizeCommitSubject(generated.subject),
body: generated.body.trim(),
...("branch" in generated && typeof generated.branch === "string"
? { branch: sanitizeFeatureBranchName(generated.branch) }
: {}),
};
});
const generatePrContent: TextGeneration.TextGeneration["Service"]["generatePrContent"] =
Effect.fn("AntigravityTextGeneration.generatePrContent")(function* (input) {
const generated = yield* runAntigravityJson({
operation: "generatePrContent",
...buildPrContentPrompt({
baseBranch: input.baseBranch,
headBranch: input.headBranch,
commitSummary: input.commitSummary,
diffSummary: input.diffSummary,
diffPatch: input.diffPatch,
policy: input.policy,
changeRequestTemplate: input.changeRequestTemplate,
}),
modelSelection: input.modelSelection,
});
return { title: sanitizePrTitle(generated.title), body: generated.body.trim() };
});
const generateBranchName: TextGeneration.TextGeneration["Service"]["generateBranchName"] =
Effect.fn("AntigravityTextGeneration.generateBranchName")(function* (input) {
const generated = yield* runAntigravityJson({
operation: "generateBranchName",
...buildBranchNamePrompt({ message: input.message, attachments: input.attachments }),
modelSelection: input.modelSelection,
});
return { branch: sanitizeBranchFragment(generated.branch) };
});
const generateThreadTitle: TextGeneration.TextGeneration["Service"]["generateThreadTitle"] =
Effect.fn("AntigravityTextGeneration.generateThreadTitle")(function* (input) {
const generated = yield* runAntigravityJson({
operation: "generateThreadTitle",
...buildThreadTitlePrompt({
message: input.message,
previousTitle: input.previousTitle,
attachments: input.attachments,
}),
modelSelection: input.modelSelection,
});
return { title: sanitizeThreadTitle(generated.title) };
});
const rankPullRequests: TextGeneration.TextGeneration["Service"]["rankPullRequests"] = Effect.fn(
"AntigravityTextGeneration.rankPullRequests",
)(function* (input) {
const generated = yield* runAntigravityJson({
operation: "rankPullRequests",
...buildPullRequestRankingPrompt({
repository: input.repository,
intoRepository: input.intoRepository,
candidates: input.candidates,
}),
modelSelection: input.modelSelection,
});
return {
rankings: clampPullRequestRankings(generated.rankings),
} satisfies TextGeneration.PullRequestRankingResult;
});
return {
generateCommitMessage,
generatePrContent,
generateBranchName,
generateThreadTitle,
rankPullRequests,
} satisfies TextGeneration.TextGeneration["Service"];
});