Repository navigation
feat(acp-registry): support explicit text generation #41289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { TextGenerationError } from "@t3tools/contracts"; | ||
| import * as SecureRandom from "effect/crypto/SecureRandom"; | ||
| import * as FileSystem from "effect/FileSystem"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Ref from "effect/Ref"; | ||
| import * as Schema from "effect/Schema"; | ||
| import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; | ||
|
|
||
| import type { ProviderTextGeneration } from "@t3tools/provider-core/server/textGeneration"; | ||
| import * as TextGenerationOperations from "@t3tools/provider-core/server/textGenerationOperations"; | ||
| import { findModelConfigOption } from "@t3tools/provider-acp/server/runtimeModel"; | ||
|
|
||
| import * as AcpRegistrySupport from "./AcpRegistrySupport.ts"; | ||
| import { | ||
| makeAcpRegistryManagementRuntime, | ||
| type AcpRegistryConfigurationProbeInput, | ||
| } from "./probe.ts"; | ||
|
|
||
| const TIMEOUT = "3 minutes"; | ||
| const isTextGenerationError = Schema.is(TextGenerationError); | ||
|
|
||
| /** | ||
| * Runs each request in a disposable ACP session outside the checkout. | ||
| */ | ||
| export const makeAcpRegistryTextGeneration = ( | ||
| input: Omit<AcpRegistryConfigurationProbeInput, "cwd">, | ||
| services: { | ||
| readonly catalog: AcpRegistrySupport.AcpRegistryCatalog["Service"]; | ||
| readonly spawner: ChildProcessSpawner.ChildProcessSpawner["Service"]; | ||
| readonly random: SecureRandom.SecureRandom["Service"]; | ||
| readonly fs: FileSystem.FileSystem; | ||
| }, | ||
| ): ProviderTextGeneration => { | ||
| const run: TextGenerationOperations.Runner = Effect.fn("AcpRegistryTextGeneration.run")( | ||
| function* (request) { | ||
| const fs = yield* FileSystem.FileSystem; | ||
| const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-acp-text-" }); | ||
| const runtime = yield* makeAcpRegistryManagementRuntime({ ...input, cwd }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apply DeepSeek history isolation to text-generation sessions. The text-generation runtime uses the unmodified spawn from Apply the same temporary persistence root to the management spawn. Either reuse the isolation helper for that path or move it to shared code. Text-generation sessions must stay disposable and must not be written to the user's history.
|
||
| const output = yield* Ref.make(""); | ||
| yield* runtime.handleSessionUpdate(({ update }) => { | ||
| if (update.sessionUpdate === "agent_message" && update.content != null) { | ||
| return Ref.set( | ||
| output, | ||
| update.content.map((block) => (block.type === "text" ? block.text : "")).join(""), | ||
| ); | ||
| } | ||
| if (update.sessionUpdate !== "agent_message_chunk" || update.content.type !== "text") { | ||
| return Effect.void; | ||
| } | ||
| const chunk = update.content.text; | ||
| return Ref.update(output, (text) => text + chunk); | ||
| }); | ||
| // A hidden session must never act on the checkout. | ||
| yield* runtime.handleRequestPermission(() => | ||
| Effect.succeed({ outcome: { outcome: "cancelled" } } as const), | ||
| ); | ||
| const started = yield* runtime.start(); | ||
| yield* Effect.addFinalizer(() => | ||
| runtime | ||
| .deleteSession(started.sessionId) | ||
| .pipe(Effect.interruptible, Effect.timeout("5 seconds"), Effect.ignore), | ||
| ); | ||
| const model = request.modelSelection.model; | ||
| const modelOption = findModelConfigOption(yield* runtime.getConfigOptions); | ||
| if (model.length > 0 && model !== "default" && modelOption?.currentValue !== model) { | ||
| yield* modelOption === undefined | ||
| ? runtime.setSessionModel(model) | ||
| : runtime.setConfigOption(modelOption.id, model, { allowUnlistedValue: true }); | ||
| } | ||
| yield* runtime.prompt({ prompt: [{ type: "text", text: request.prompt }] }); | ||
| const text = (yield* Ref.get(output)).trim(); | ||
| if (text.length === 0) { | ||
| return yield* new TextGenerationError({ | ||
| operation: request.operation, | ||
| detail: "The ACP agent returned no text.", | ||
| }); | ||
| } | ||
| return yield* TextGenerationOperations.decodeJsonReply(request, "ACP agent", text); | ||
| }, | ||
| (effect, request) => | ||
| effect.pipe( | ||
| Effect.scoped, | ||
| Effect.timeoutOrElse({ | ||
| duration: TIMEOUT, | ||
| orElse: () => | ||
| Effect.fail( | ||
| new TextGenerationError({ | ||
| operation: request.operation, | ||
| detail: "The ACP agent did not answer in time.", | ||
| }), | ||
| ), | ||
| }), | ||
| Effect.mapError((cause) => | ||
| isTextGenerationError(cause) | ||
| ? cause | ||
| : new TextGenerationError({ | ||
| operation: request.operation, | ||
| detail: `ACP request failed: ${cause.message}`, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep underlying failure text out of the public error detail. Every wrapped failure copies As per coding guidelines, the message must be “never from
|
||
| cause, | ||
| }), | ||
| ), | ||
| Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, services.catalog), | ||
| Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, services.spawner), | ||
| Effect.provideService(SecureRandom.SecureRandom, services.random), | ||
| Effect.provideService(FileSystem.FileSystem, services.fs), | ||
| ), | ||
| ); | ||
| return TextGenerationOperations.fromRunner( | ||
| "AcpRegistryTextGeneration", | ||
| TextGenerationOperations.withoutInvestigation("ACP Registry", run), | ||
| ); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document the branch-name exception for a configured source-control writer model.
Branch names use the text-generation model only when no source-control writer model is configured. When a writer model is set, branch naming uses that model instead. The paragraph implies that selecting an ACP model covers branch names in every case. Name the exception in the paragraph.
Suggested documentation fix