diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts index f837fd8ca9722..3d0a9af868bae 100644 --- a/apps/server/src/serverSettings.test.ts +++ b/apps/server/src/serverSettings.test.ts @@ -731,6 +731,29 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings" }).pipe(Effect.provide(layerServerSettings())), ); + it.effect.each([ + { agentId: "glm-acp-agent", kept: true }, + { agentId: "kilo", kept: false }, + ])("keeps a registry text generation selection only for $agentId=$kept", ({ agentId, kept }) => + Effect.gen(function* () { + const serverSettings = yield* ServerSettingsModule.ServerSettingsService; + const instanceId = ProviderInstanceId.make("acpRegistry_text"); + const selection = { instanceId, model: "glm-5.3-flash" }; + const next = yield* serverSettings.updateSettings({ + providerInstances: { + [instanceId]: { + driver: ProviderDriverKind.make("acpRegistry"), + enabled: true, + config: { agentId }, + }, + }, + textGenerationModelSelection: selection, + }); + + assert.equal(next.textGenerationModelSelection.instanceId === instanceId, kept); + }).pipe(Effect.provide(layerServerSettings())), + ); + it.effect.each(["codex"])( "falls back to enabled instance %s after disabling the selection", (fallbackId) => diff --git a/apps/server/src/serverSettings.ts b/apps/server/src/serverSettings.ts index d4ddfd8b5c0c1..c91d93da6b024 100644 --- a/apps/server/src/serverSettings.ts +++ b/apps/server/src/serverSettings.ts @@ -54,6 +54,10 @@ import { fallbackTextGenerationModelSelection, isModelSelectionProviderEnabled, } from "@t3tools/shared/serverSettings"; +import { + AcpRegistrySettings, + supportsGlmTextGeneration, +} from "@t3tools/provider-acp-registry/settings"; import * as ServerSecretStore from "./auth/ServerSecretStore.ts"; import * as ManagedPolicy from "./policy/ManagedPolicy.ts"; import { secretProviderDefinitions } from "./secrets/secretProviderDefinitions.ts"; @@ -498,12 +502,17 @@ function migrateLegacyProviderSettings( } const ACP_REGISTRY_DRIVER = ProviderDriverKind.make("acpRegistry"); +const decodeAcpRegistrySettings = Schema.decodeUnknownOption(AcpRegistrySettings); function selectionSupportsTextGeneration( settings: ServerSettings, selection: ModelSelection, ): boolean { - return settings.providerInstances[selection.instanceId]?.driver !== ACP_REGISTRY_DRIVER; + const instance = settings.providerInstances[selection.instanceId]; + return ( + instance?.driver !== ACP_REGISTRY_DRIVER || + Option.exists(decodeAcpRegistrySettings(instance.config ?? {}), supportsGlmTextGeneration) + ); } type DriverAllowed = (driver: ProviderDriverKind) => boolean; diff --git a/docs/user/providers-acp.md b/docs/user/providers-acp.md index 453c777e3c8c0..9b5fdb652940d 100644 --- a/docs/user/providers-acp.md +++ b/docs/user/providers-acp.md @@ -132,8 +132,10 @@ so the next turn after a rollback starts a fresh agent session and T3 Code sends conversation, without the turns you rolled back. Registry instances are not used for T3's app-owned text generation, such as thread titles, commit -messages, branch names, or pull request descriptions. Configure a text-generation-capable provider -for those actions. +messages, branch names, or pull request descriptions, with one exception: Z.ai GLM +(`glm-acp-agent`) can be selected in **Settings → Threads → Text generation model**. T3 runs it with +tools and file access refused, in the default permission mode whatever mode you pick there, and +removes its temporary session afterwards. GLM cannot write review guides. ## Advanced configuration diff --git a/packages/provider-acp-registry/src/server/driver.test.ts b/packages/provider-acp-registry/src/server/driver.test.ts index 684eb21d7456b..a7045352117de 100644 --- a/packages/provider-acp-registry/src/server/driver.test.ts +++ b/packages/provider-acp-registry/src/server/driver.test.ts @@ -528,6 +528,14 @@ describe("acpRegistrySnapshotReadiness", () => { expect(snapshot.auth).toEqual({ status: "unknown", canLogout: false }); expect(snapshot.supportsTextGeneration).toBe(false); + expect( + buildCheckedAcpRegistrySnapshot({ + ...identity, + settings: decodeSettings({ agentId: "glm-acp-agent" }), + checkedAt: "2026-08-13T10:00:00.000Z", + inspection: { status: "unconfigured" }, + }).supportsTextGeneration, + ).toBe(true); expect( snapshot.models.map(({ slug, name, isCustom, isDefault }) => ({ slug, diff --git a/packages/provider-acp-registry/src/server/driver.ts b/packages/provider-acp-registry/src/server/driver.ts index 46d8ed3219aa4..1b9b5a76b37cc 100644 --- a/packages/provider-acp-registry/src/server/driver.ts +++ b/packages/provider-acp-registry/src/server/driver.ts @@ -9,7 +9,7 @@ import { type ServerProvider, type ServerProviderModel, } from "@t3tools/contracts"; -import { AcpRegistrySettings } from "../settings.ts"; +import { AcpRegistrySettings, supportsGlmTextGeneration } from "../settings.ts"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { createModelCapabilities } from "@t3tools/shared/model"; import * as Clock from "effect/Clock"; @@ -63,6 +63,7 @@ import { } from "./probe.ts"; import * as AcpRegistrySupport from "./AcpRegistrySupport.ts"; import * as AcpRegistryRuntimeCoordinator from "./AcpRegistryRuntimeCoordinator.ts"; +import { makeGlmTextGeneration } from "./glmTextGeneration.ts"; import * as AcpRegistryAuth from "./auth.ts"; import * as AcpRegistryAuthenticationState from "./authenticationState.ts"; @@ -303,7 +304,7 @@ function baseSnapshot( ...(input.accentColor ? { accentColor: input.accentColor } : {}), ...(iconUrl ? { iconUrl } : {}), continuation: { groupKey: input.continuationKey }, - supportsTextGeneration: false, + supportsTextGeneration: supportsGlmTextGeneration(input.settings), enabled: input.settings.enabled, installed: input.installed, version: input.version, @@ -1011,7 +1012,12 @@ export const AcpRegistryDriver: ProviderDriver provideAcpManagementServices( diff --git a/packages/provider-acp-registry/src/server/glmTextGeneration.test.ts b/packages/provider-acp-registry/src/server/glmTextGeneration.test.ts new file mode 100644 index 0000000000000..792f6f9567367 --- /dev/null +++ b/packages/provider-acp-registry/src/server/glmTextGeneration.test.ts @@ -0,0 +1,463 @@ +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { it } from "@effect/vitest"; +import { ProviderInstanceId } from "@t3tools/contracts"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; +import * as Fiber from "effect/Fiber"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +import * as Path from "effect/Path"; +import * as Queue from "effect/Queue"; +import * as SecureRandom from "effect/crypto/SecureRandom"; +import * as Schema from "effect/Schema"; +import * as Stream from "effect/Stream"; +import { type AcpError } from "effect-acp/errors"; +import type * as AcpSchema from "effect-acp/compat"; +import { expect } from "vite-plus/test"; + +import type * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime"; +import { AcpRegistrySettings } from "../settings.ts"; +import * as AcpRegistrySupport from "./AcpRegistrySupport.ts"; +import { + type GlmTextRuntime, + type GlmTextRuntimeFactory, + makeGlmTextGeneration, +} from "./glmTextGeneration.ts"; + +const requestContext = { requestId: "test-request", method: "test" }; +const SESSION_ID = "047c62f6-607b-44db-bfbe-f83b67e9e8b1"; +const decodeSettings = Schema.decodeSync(AcpRegistrySettings); +const glmSettings = decodeSettings({ agentId: "glm-acp-agent" }); +const modelSelection = { + instanceId: ProviderInstanceId.make("glm-text-test"), + model: "glm-5.3", +}; +const SAFETY_PREFIX = + "Use only the input below. Do not use tools, read or write files, run commands, or ask questions.\nReturn only the requested JSON object.\n\n"; + +interface PromptContext { + readonly emit: ( + update: AcpSchema.SessionNotification["update"], + sessionId?: string, + ) => Effect.Effect; + permission: Parameters[0]; + question: Parameters[0]; + readFile: Parameters[0]; + writeFile: Parameters[0]; + createTerminal: Parameters[0]; + extension: Parameters[0]; + readonly cwd: string; +} + +const makeFixture = Effect.fn("makeGlmTextGenerationFixture")(function* ( + options: { + readonly outputs?: ReadonlyArray; + readonly prompt?: (context: PromptContext) => Effect.Effect; + } = {}, +) { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const enteredPrompt = yield* Deferred.make(); + const state = { + runtimes: [] as Array<{ + readonly cwd: string; + readonly sessionDirectory: string; + readonly environment: Readonly>; + }>, + prompts: [] as Array[0]>, + selectedModes: [] as Array, + configSelections: [] as Array<{ readonly id: string; readonly value: string | boolean }>, + modesAtPrompt: [] as Array, + startedSessions: 0, + cancellations: 0, + events: [] as Array, + }; + const outputs = [...(options.outputs ?? ['{"title":"Repair login"}'])]; + const incoming: Omit = { + permission: () => Effect.die("No permission handler."), + question: () => Effect.die("No question handler."), + readFile: () => Effect.die("No file handler."), + writeFile: () => Effect.die("No file handler."), + createTerminal: () => Effect.die("No terminal handler."), + extension: () => Effect.die("No extension handler."), + }; + + const makeRuntime: GlmTextRuntimeFactory = (input) => + Effect.gen(function* () { + const events = yield* Queue.unbounded(); + const environment = { ...input.environment }; + const sessionDirectory = environment.ACP_GLM_SESSION_DIR; + if (sessionDirectory === undefined) { + return yield* Effect.die("Missing ACP_GLM_SESSION_DIR."); + } + state.runtimes.push({ cwd: input.cwd, sessionDirectory, environment }); + expect(yield* fs.readDirectory(input.cwd).pipe(Effect.orDie)).toEqual([]); + yield* Effect.addFinalizer(() => + Effect.sync(() => { + state.events.push(`runtime-closed:${input.cwd}`); + }), + ); + let currentMode = "default"; + let currentModel = "glm-5.3"; + let currentThoughtLevel = "medium"; + let sessionUpdate: Parameters[0] = () => Effect.void; + const configOptions = (): Array => [ + { + id: "model", + name: "Model", + category: "model", + type: "select", + currentValue: currentModel, + options: [ + { value: "glm-5.3", name: "GLM 5.3" }, + { value: "glm-5.3-flash", name: "GLM 5.3 Flash" }, + ], + }, + { + id: "thought_level", + name: "Thinking", + category: "thought_level", + type: "select", + currentValue: currentThoughtLevel, + options: ["minimal", "low", "medium", "high", "xhigh", "max"].map((value) => ({ + value, + name: value, + })), + }, + { + id: "mode", + name: "Mode", + category: "mode", + type: "select", + currentValue: currentMode, + options: ["default", "accept_edits", "bypass_permissions"].map((value) => ({ + value, + name: value, + })), + }, + ]; + + return { + start: () => + Effect.gen(function* () { + state.startedSessions += 1; + yield* fs + .writeFileString(path.join(sessionDirectory, `${SESSION_ID}.json`), "{}") + .pipe(Effect.orDie); + return { + sessionId: SESSION_ID, + initializeResult: { protocolVersion: 1 }, + sessionSetupResult: { sessionId: SESSION_ID }, + modelConfigId: "model", + }; + }), + setMode: (modeId) => + Effect.sync(() => { + state.selectedModes.push(modeId); + currentMode = modeId; + return {}; + }), + getConfigOptions: Effect.sync(configOptions), + getEvents: () => Stream.fromQueue(events), + setConfigOption: (configId, value) => + Effect.sync(() => { + state.configSelections.push({ id: configId, value }); + if (configId === "model" && typeof value === "string") currentModel = value; + if (configId === "thought_level" && typeof value === "string") { + currentThoughtLevel = value; + } + return { configOptions: configOptions() }; + }), + prompt: (request) => + Effect.gen(function* () { + state.prompts.push(request); + state.modesAtPrompt.push(currentMode); + yield* Deferred.succeed(enteredPrompt, undefined); + const emit: PromptContext["emit"] = (update, sessionId = SESSION_ID) => + sessionUpdate({ sessionId, update }); + if (options.prompt) { + return yield* options.prompt({ emit, ...incoming, cwd: input.cwd }); + } + const output = outputs.shift() ?? '{"title":"Repair login"}'; + yield* emit({ + sessionUpdate: "agent_thought_chunk", + content: { type: "text", text: "Choose concise text." }, + }); + yield* emit( + { + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "unrelated session" }, + }, + "another-session", + ); + for (const text of [output.slice(0, 9), output.slice(9)]) { + yield* emit({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text }, + }); + } + return { stopReason: "end_turn" }; + }), + cancel: Effect.gen(function* () { + state.cancellations += 1; + const acknowledge = yield* Deferred.make(); + yield* Queue.offer(events, { _tag: "EventStreamBarrier", acknowledge }); + yield* Deferred.await(acknowledge); + }), + closeSession: (sessionId) => + Effect.sync(() => { + state.events.push(`session-closed:${sessionId ?? SESSION_ID}`); + return {}; + }), + handleSessionUpdate: (handler) => + Effect.sync(() => { + sessionUpdate = handler; + }), + handleRequestPermission: (handler) => + Effect.sync(() => { + incoming.permission = handler; + }), + handleElicitation: (handler) => + Effect.sync(() => { + incoming.question = handler; + }), + handleReadTextFile: (handler) => + Effect.sync(() => { + incoming.readFile = handler; + }), + handleWriteTextFile: (handler) => + Effect.sync(() => { + incoming.writeFile = handler; + }), + handleCreateTerminal: (handler) => + Effect.sync(() => { + incoming.createTerminal = handler; + }), + handleTerminalOutput: () => Effect.void, + handleTerminalWaitForExit: () => Effect.void, + handleTerminalKill: () => Effect.void, + handleTerminalRelease: () => Effect.void, + handleUnknownExtRequest: (handler) => + Effect.sync(() => { + incoming.extension = handler; + }), + } satisfies GlmTextRuntime; + }); + + const textGeneration = yield* makeGlmTextGeneration({ + settings: glmSettings, + environment: {}, + makeRuntime, + }); + const titleInput = { cwd: "/workspace/project", message: "Repair Google login", modelSelection }; + const assertCleaned = Effect.gen(function* () { + for (const runtime of state.runtimes) { + expect(yield* fs.exists(runtime.cwd)).toBe(false); + expect(yield* fs.exists(runtime.sessionDirectory)).toBe(false); + } + expect(state.events.filter((event) => event.startsWith("session-closed"))).toHaveLength( + state.startedSessions, + ); + for (const [index, event] of state.events.entries()) { + if (!event.startsWith("runtime-closed")) continue; + expect(state.events[index - 1]).toMatch(/^session-closed:/); + } + }); + return { fs, path, state, incoming, enteredPrompt, textGeneration, titleInput, assertCleaned }; +}); + +const unusedCatalog: AcpRegistrySupport.AcpRegistryCatalog["Service"] = { + search: () => Effect.die("unused search"), + prepare: () => Effect.die("unused prepare"), + inspect: () => Effect.die("unused inspect"), + resolve: () => Effect.die("unused resolve"), + uninstallManagedBinary: () => Effect.die("unused uninstall"), +}; + +it.layer( + Layer.mergeAll( + NodeServices.layer, + SecureRandom.layerWebCrypto, + Layer.succeed(AcpRegistrySupport.AcpRegistryCatalog, unusedCatalog), + ), +)("GlmTextGeneration", (it) => { + it.effect("turns a JSON reply into a title from a jailed temporary session", () => + Effect.gen(function* () { + const fixture = yield* makeFixture({ + outputs: ['{"title":" \\"Repair Google login\\" "}'], + }); + expect(yield* fixture.textGeneration.generateThreadTitle(fixture.titleInput)).toEqual({ + title: "Repair Google login", + }); + const runtime = fixture.state.runtimes[0]!; + expect(runtime.cwd).not.toBe(fixture.titleInput.cwd); + expect(runtime.environment.ACP_GLM_MAX_TURNS).toBe("1"); + expect(runtime.sessionDirectory.startsWith(runtime.cwd)).toBe(false); + expect(fixture.state.selectedModes).toEqual(["default"]); + const prompt = fixture.state.prompts[0]!.prompt[0]!; + expect(prompt.type === "text" && prompt.text.startsWith(SAFETY_PREFIX)).toBe(true); + yield* fixture.assertCleaned; + }).pipe(Effect.scoped), + ); + + it.effect("applies only thought_level selections and never a saved mode", () => + Effect.gen(function* () { + const fixture = yield* makeFixture(); + yield* fixture.textGeneration.generateThreadTitle({ + ...fixture.titleInput, + modelSelection: { + ...modelSelection, + model: "glm-5.3-flash", + options: [ + { id: "mode", value: "bypass_permissions" }, + { id: "_t3/session-mode", value: "bypass_permissions" }, + { id: "thought_level", value: "low" }, + ], + }, + }); + expect(fixture.state.selectedModes).toEqual(["default"]); + expect(fixture.state.configSelections).toEqual([ + { id: "model", value: "glm-5.3-flash" }, + { id: "thought_level", value: "low" }, + ]); + expect(fixture.state.modesAtPrompt).toEqual(["default"]); + yield* fixture.assertCleaned; + }).pipe(Effect.scoped), + ); + + it.effect.each(["tool_call", "tool_call_update"] as const)( + "aborts on %s even without a permission request", + (sessionUpdate) => + Effect.gen(function* () { + const fixture = yield* makeFixture({ + prompt: ({ emit }) => + emit({ sessionUpdate, toolCallId: "tool-1", title: "Read files" }).pipe( + Effect.andThen(Effect.never), + ), + }); + const error = yield* fixture.textGeneration + .generateThreadTitle(fixture.titleInput) + .pipe(Effect.flip); + expect(error.detail).toContain("tool work"); + expect(fixture.state.cancellations).toBe(1); + yield* fixture.assertCleaned; + }).pipe(Effect.scoped), + ); + + it.effect.each([ + { kind: "permission", reply: { outcome: { outcome: "cancelled" } } }, + { kind: "question", reply: { action: "decline" } }, + ] as const)("refuses a $kind request and fails the request", ({ kind, reply }) => + Effect.gen(function* () { + const fixture = yield* makeFixture({ prompt: () => Effect.never }); + const child = yield* fixture.textGeneration + .generateThreadTitle(fixture.titleInput) + .pipe(Effect.forkChild); + yield* Deferred.await(fixture.enteredPrompt); + const answer = + kind === "permission" + ? yield* fixture.incoming.permission( + { + sessionId: SESSION_ID, + toolCall: { toolCallId: "question-1", title: "Choose a title", kind: "other" }, + options: [ + { optionId: "first-native-choice", name: "Use this title", kind: "allow_once" }, + { optionId: "second-native-choice", name: "Use this title", kind: "allow_once" }, + ], + }, + requestContext, + ) + : yield* fixture.incoming.question( + { + sessionId: SESSION_ID, + message: "Which title do you prefer?", + mode: "form", + requestedSchema: { type: "object", properties: {} }, + }, + requestContext, + ); + const error = yield* Fiber.join(child).pipe(Effect.flip); + expect(answer).toEqual(reply); + expect(error.detail).toContain("user input"); + yield* fixture.assertCleaned; + }).pipe(Effect.scoped), + ); + + it.effect.each(["readFile", "writeFile", "createTerminal", "extension"] as const)( + "denies unexpected %s requests and fails the request", + (kind) => + Effect.gen(function* () { + const fixture = yield* makeFixture({ prompt: () => Effect.never }); + const child = yield* fixture.textGeneration + .generateThreadTitle(fixture.titleInput) + .pipe(Effect.forkChild); + yield* Deferred.await(fixture.enteredPrompt); + const request = + kind === "readFile" + ? fixture.incoming.readFile( + { sessionId: SESSION_ID, path: "/not-allowed" }, + requestContext, + ) + : kind === "writeFile" + ? fixture.incoming.writeFile( + { sessionId: SESSION_ID, path: "/not-allowed", content: "no" }, + requestContext, + ) + : kind === "createTerminal" + ? fixture.incoming.createTerminal( + { sessionId: SESSION_ID, command: "not-allowed" }, + requestContext, + ) + : fixture.incoming.extension("_ask_user", {}, requestContext); + const denied = yield* request.pipe(Effect.exit); + expect(Exit.isFailure(denied)).toBe(true); + const error = yield* Fiber.join(child).pipe(Effect.flip); + expect(error.detail).toContain("tool or user input"); + yield* fixture.assertCleaned; + }).pipe(Effect.scoped), + ); + + it.effect("fails when GLM writes files into the temporary workspace", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const fixture = yield* makeFixture({ + prompt: ({ cwd, emit }) => + Effect.gen(function* () { + yield* fs.writeFileString(`${cwd}/unexpected.txt`, "no").pipe(Effect.orDie); + yield* emit({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: '{"title":"Ignored output"}' }, + }); + return { stopReason: "end_turn" }; + }), + }); + const error = yield* fixture.textGeneration + .generateThreadTitle(fixture.titleInput) + .pipe(Effect.flip); + expect(error.detail).toContain("wrote files"); + yield* fixture.assertCleaned; + }).pipe(Effect.scoped), + ); + + it.effect("cannot generate review guides", () => + Effect.gen(function* () { + const fixture = yield* makeFixture(); + const error = yield* fixture.textGeneration + .generateReviewGuide({ + cwd: "/workspace/project", + number: 1, + title: "Repair login", + description: "Login is broken.", + baseBranch: "main", + baseCommit: "0000000", + settledReview: "", + modelSelection, + }) + .pipe(Effect.flip); + expect(error.detail).toContain("cannot investigate"); + expect(fixture.state.runtimes).toEqual([]); + yield* fixture.assertCleaned; + }).pipe(Effect.scoped), + ); +}); diff --git a/packages/provider-acp-registry/src/server/glmTextGeneration.ts b/packages/provider-acp-registry/src/server/glmTextGeneration.ts new file mode 100644 index 0000000000000..827bda1783fba --- /dev/null +++ b/packages/provider-acp-registry/src/server/glmTextGeneration.ts @@ -0,0 +1,343 @@ +import { TextGenerationError } from "@t3tools/contracts"; +import { readCustomModelEntries } from "@t3tools/shared/model"; +import * as SecureRandom from "effect/crypto/SecureRandom"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +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 type * as Scope from "effect/Scope"; +import * as Stream from "effect/Stream"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; +import { type AcpError, AcpRequestError, AcpSpawnError } from "effect-acp/errors"; + +import type { AcpRegistrySettings } from "../settings.ts"; +import * as TextGenerationOperations from "@t3tools/provider-core/server/textGenerationOperations"; +import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime"; +import { findModelConfigOption } from "@t3tools/provider-acp/server/runtimeModel"; +import { flattenSelectChoices } from "@t3tools/provider-acp/server/sessionConfig"; +import * as AcpRegistrySupport from "./AcpRegistrySupport.ts"; + +const GLM_TEXT_TIMEOUT_MS = 180_000; +const MAX_OUTPUT_CHARS = 128_000; +const isTextGenerationError = Schema.is(TextGenerationError); + +export type GlmTextRuntime = Pick< + AcpSessionRuntime.AcpSessionRuntime["Service"], + | "start" + | "setMode" + | "getConfigOptions" + | "getEvents" + | "setConfigOption" + | "prompt" + | "cancel" + | "closeSession" + | "handleSessionUpdate" + | "handleRequestPermission" + | "handleElicitation" + | "handleReadTextFile" + | "handleWriteTextFile" + | "handleCreateTerminal" + | "handleTerminalOutput" + | "handleTerminalWaitForExit" + | "handleTerminalKill" + | "handleTerminalRelease" + | "handleUnknownExtRequest" +>; + +export type GlmTextRuntimeFactory = (input: { + readonly cwd: string; + readonly environment: Readonly>; +}) => Effect.Effect; + +export interface GlmTextGenerationOptions { + readonly settings: AcpRegistrySettings; + readonly environment: NodeJS.ProcessEnv; + readonly makeRuntime?: GlmTextRuntimeFactory; +} + +export const makeGlmTextGeneration = Effect.fn("makeGlmTextGeneration")(function* ( + options: GlmTextGenerationOptions, +) { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { settings, environment } = options; + const catalog = yield* AcpRegistrySupport.AcpRegistryCatalog; + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const random = yield* SecureRandom.SecureRandom; + const customModelSlugs = readCustomModelEntries(settings.customModels).map((entry) => entry.slug); + + const makeRuntime: GlmTextRuntimeFactory = + options.makeRuntime ?? + ((input) => + Effect.gen(function* () { + const resolved = yield* catalog + .resolve(settings, input.cwd, environment) + .pipe( + Effect.mapError((cause) => new AcpSpawnError({ command: settings.agentId, cause })), + ); + const context = yield* Layer.build( + AcpSessionRuntime.layer({ + spawn: { + ...resolved.spawn, + env: { ...resolved.spawn.env, ...input.environment }, + }, + cwd: input.cwd, + clientInfo: { name: "t3-code", version: "0.0.0" }, + clientCapabilities: { + fs: { readTextFile: false, writeTextFile: false }, + terminal: false, + }, + ...(settings.authMethodId ? { authMethodId: settings.authMethodId } : {}), + customModels: customModelSlugs, + mcpServers: [], + }), + ).pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + Effect.provideService(SecureRandom.SecureRandom, random), + ); + return yield* Effect.service(AcpSessionRuntime.AcpSessionRuntime).pipe( + Effect.provide(context), + ); + })); + + const runGlmJson = Effect.fn("GlmTextGeneration.runJson")( + function* (input: TextGenerationOperations.Request) { + const { operation } = input; + const helper = Effect.gen(function* () { + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-glm-text-" }); + const cwd = path.join(root, "workspace"); + const sessionDirectory = path.join(root, "sessions"); + yield* fs.makeDirectory(cwd, { recursive: true }); + yield* fs.makeDirectory(sessionDirectory, { recursive: true }); + + const rawResult = yield* Effect.gen(function* () { + const runtime = yield* makeRuntime({ + cwd, + environment: { + ACP_GLM_SESSION_DIR: sessionDirectory, + // Reads run unprompted in default mode; one turn keeps their output from reaching the model. + ACP_GLM_MAX_TURNS: "1", + }, + }); + 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(); + const reject = (detail: string) => + Deferred.fail(rejected, new TextGenerationError({ operation, detail })).pipe( + Effect.asVoid, + ); + const rejectToolRequest = () => + reject("GLM 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("GLM text generation requested a tool permission or user input.").pipe( + Effect.as({ outcome: { outcome: "cancelled" as const } }), + ), + ); + yield* runtime.handleElicitation(() => + reject("GLM 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); + let sessionId: string | undefined; + yield* runtime.handleSessionUpdate((notification) => + Effect.gen(function* () { + const update = notification.update; + if ( + update.sessionUpdate === "tool_call" || + update.sessionUpdate === "tool_call_update" + ) { + return yield* reject("GLM 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("GLM text generation exceeded the output limit."); + } + }), + ); + + return yield* Effect.gen(function* () { + const started = yield* runtime.start(); + sessionId = started.sessionId; + // Close the ACP session before the runtime scope (the process) closes. + yield* Effect.addFinalizer(() => + runtime.closeSession(sessionId).pipe(Effect.timeoutOption(2_000), Effect.ignore), + ); + yield* runtime.setMode("default"); + + const model = input.modelSelection.model; + if (model.length > 0 && model !== "default") { + const modelOption = findModelConfigOption(yield* runtime.getConfigOptions); + const selectable = + modelOption !== undefined && + (customModelSlugs.includes(model) || + flattenSelectChoices(modelOption.options).some( + (choice) => choice.value === model, + )); + if (!selectable) { + return yield* new TextGenerationError({ + operation, + detail: "Could not select the GLM model for text generation.", + }); + } + yield* runtime + .setConfigOption(modelOption.id, model, { allowUnlistedValue: true }) + .pipe( + Effect.mapError( + (cause) => + new TextGenerationError({ + operation, + detail: "Could not select the GLM model for text generation.", + cause, + }), + ), + ); + } + for (const selection of input.modelSelection.options ?? []) { + const option = (yield* runtime.getConfigOptions).find( + (candidate) => candidate.id === selection.id, + ); + // Only thought_level is safe to replay: saved selections can carry mode bypass. + if (option?.category !== "thought_level" || option.type !== "select") continue; + if (typeof selection.value !== "string") continue; + if ( + !flattenSelectChoices(option.options).some( + (choice) => choice.value === selection.value, + ) + ) { + continue; + } + yield* runtime.setConfigOption(option.id, selection.value).pipe( + Effect.catchTags({ + AcpRequestError: (error) => + Effect.logWarning("GLM session rejected a configuration option value", { + optionId: option.id, + value: selection.value, + code: error.code, + }), + }), + ); + } + + 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: "GLM 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: "GLM wrote files during text generation.", + }); + } + if (!rawResult) { + return yield* new TextGenerationError({ + operation, + detail: "GLM returned empty text generation output.", + }); + } + return yield* TextGenerationOperations.decodeJsonReply(input, "GLM", rawResult); + }).pipe( + Effect.scoped, + Effect.timeoutOption(GLM_TEXT_TIMEOUT_MS), + Effect.flatMap( + Option.match({ + onNone: () => + Effect.fail( + new TextGenerationError({ + operation, + detail: "GLM text generation timed out.", + }), + ), + onSome: Effect.succeed, + }), + ), + ); + + return yield* helper; + }, + (effect, input) => + effect.pipe( + Effect.mapError((cause) => + isTextGenerationError(cause) + ? cause + : new TextGenerationError({ + operation: input.operation, + detail: "GLM text generation failed.", + cause, + }), + ), + ), + ); + + return TextGenerationOperations.fromRunner( + "GlmTextGeneration", + TextGenerationOperations.withoutInvestigation("GLM", runGlmJson), + ); +}); diff --git a/packages/provider-acp-registry/src/settings.ts b/packages/provider-acp-registry/src/settings.ts index aba9f5f9db5ac..33ee63b842200 100644 --- a/packages/provider-acp-registry/src/settings.ts +++ b/packages/provider-acp-registry/src/settings.ts @@ -68,3 +68,10 @@ export const AcpRegistrySettings = makeProviderSettingsSchema( }, ); export type AcpRegistrySettings = typeof AcpRegistrySettings.Type; + +/** Z.ai GLM is the one registry agent qualified for app-owned text generation. */ +export function supportsGlmTextGeneration( + settings: Pick, +): boolean { + return settings.source !== "local" && settings.agentId === "glm-acp-agent"; +}