Repository navigation
Expand file tree
/
Copy pathAntigravityTextGeneration.ts
More file actions
308 lines (293 loc) · 11.7 KB
/
Copy pathAntigravityTextGeneration.ts
File metadata and controls
308 lines (293 loc) · 11.7 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
import { type ProviderSetupError, TextGenerationError } from "@t3tools/contracts";
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 * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
import * as TextGenerationOperations from "@t3tools/provider-core/server/textGenerationOperations";
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.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;
readonly defaultModel?: Effect.Effect<string | undefined>;
readonly makeRuntime: (
cwd: string,
) => Effect.Effect<AntigravityTextRuntime, AcpError | ProviderSetupError, Scope.Scope>;
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;
});
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: TextGenerationOperations.Request<S>) {
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: "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.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.",
});
}
return yield* TextGenerationOperations.decodeJsonReply(input, "Antigravity", rawResult);
}).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,
),
);
return TextGenerationOperations.fromRunner(
"AntigravityTextGeneration",
TextGenerationOperations.withoutInvestigation("Antigravity", runAntigravityJson),
);
});