Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ Docs:

- Codex App Server docs: https://developers.openai.com/codex/sdk/#app-server

## ACP Registry

In addition to the four bespoke providers (Codex, Claude, Cursor, OpenCode),
T3 Code bundles a snapshot of the
[Agent Client Protocol registry](https://agentclientprotocol.com/get-started/registry)
and exposes a Zed-style installer at **Settings → ACP Registry**. See
[docs/providers/acp-registry.md](./docs/providers/acp-registry.md) for the
install pipeline, distribution channels, and how to refresh the bundled
snapshot (`bun run sync:acp-registry`).

## Reference Repos

- Open-source Codex repo: https://github.com/openai/codex
Expand Down
227 changes: 227 additions & 0 deletions apps/server/src/acpRegistry/AcpRegistryService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
import {
ACP_REGISTRY,
acpRegistryDriverKindFor,
acpRegistryEntryById,
AcpRegistryError,
type AcpRegistryEntry,
type AcpRegistryEntryWithStatus,
type AcpRegistryInstallState,
type AcpRegistryInstallStatus,
ProviderDriverKind,
type ProviderInstanceConfig,
type ProviderInstanceConfigMap,
ProviderInstanceId,
} from "@t3tools/contracts";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";

import { ServerConfig } from "../config.ts";
import { ServerSettingsService } from "../serverSettings.ts";

import { availableChannels, installAgent, uninstallAgent } from "./installer.ts";
import { resolveCurrentPlatform } from "./platform.ts";

export interface AcpRegistryServiceShape {
readonly list: () => Effect.Effect<ReadonlyArray<AcpRegistryEntryWithStatus>, AcpRegistryError>;
readonly install: (agentId: string) => Effect.Effect<AcpRegistryInstallState, AcpRegistryError>;
readonly uninstall: (agentId: string) => Effect.Effect<void, AcpRegistryError>;
}

export class AcpRegistryService extends Context.Service<
AcpRegistryService,
AcpRegistryServiceShape
>()("t3/acpRegistry/AcpRegistryService") {}

export const layer: Layer.Layer<AcpRegistryService, never, ServerConfig | ServerSettingsService> =
Layer.effect(
AcpRegistryService,
Effect.gen(function* () {
const config = yield* ServerConfig;
const settingsService = yield* ServerSettingsService;
const platform = resolveCurrentPlatform();
const cacheRoot = config.acpRegistryCacheDir;

const wrapSettingsError = (detail: string) => (cause: unknown) =>
new AcpRegistryError({ detail, cause });

const readSettings = settingsService.getSettings.pipe(
Effect.mapError(wrapSettingsError("Failed to read server settings")),
);
const readInstalls = readSettings.pipe(Effect.map((s) => s.acpRegistryInstalls));

const writeInstalls = (next: Readonly<Record<string, AcpRegistryInstallState>>) =>
settingsService
.updateSettings({ acpRegistryInstalls: next })
.pipe(
Effect.asVoid,
Effect.mapError(wrapSettingsError("Failed to persist server settings")),
);

const writeInstallAndInstance = (
nextInstalls: Readonly<Record<string, AcpRegistryInstallState>>,
nextInstances: ProviderInstanceConfigMap,
) =>
settingsService
.updateSettings({
acpRegistryInstalls: nextInstalls,
providerInstances: nextInstances,
})
.pipe(
Effect.asVoid,
Effect.mapError(wrapSettingsError("Failed to persist server settings")),
);

const requireEntry = (agentId: string): Effect.Effect<AcpRegistryEntry, AcpRegistryError> => {
const entry = acpRegistryEntryById(agentId);
return entry
? Effect.succeed(entry)
: Effect.fail(new AcpRegistryError({ agentId, detail: "Unknown ACP registry agent." }));
};

// One-shot backfill: every installed agent should have a default
// provider instance so it appears in the chat picker. Installs
// recorded before this auto-create logic existed are stranded
// otherwise. We only create instances for installs whose driver has
// *no* existing instance — manual instances and prior backfills are
// left alone. If the user deletes the auto-created instance and
// wants it gone permanently, they should uninstall the agent too.
yield* Effect.gen(function* () {
const settings = yield* readSettings;
const installs = settings.acpRegistryInstalls;
const installedAgentIds = Object.keys(installs);
if (installedAgentIds.length === 0) return;
const existingInstances = settings.providerInstances ?? {};
const existingDrivers = new Set(
Object.values(existingInstances).map((instance) => instance.driver),
);
let nextInstances: ProviderInstanceConfigMap = existingInstances;
let changed = false;
for (const agentId of installedAgentIds) {
const entry = acpRegistryEntryById(agentId);
if (!entry) continue;
const driverKind = ProviderDriverKind.make(acpRegistryDriverKindFor(entry.id));
if (existingDrivers.has(driverKind)) continue;
nextInstances = {
...nextInstances,
[ProviderInstanceId.make(entry.id)]: {
driver: driverKind,
displayName: entry.name,
enabled: true,
} satisfies ProviderInstanceConfig,
};
existingDrivers.add(driverKind);
changed = true;
}
if (!changed) return;
yield* settingsService
.updateSettings({ providerInstances: nextInstances })
.pipe(
Effect.asVoid,
Effect.mapError(wrapSettingsError("Failed to backfill provider instances")),
);
}).pipe(
Effect.catch((error: AcpRegistryError) =>
Effect.logWarning("acp.registry backfill skipped", { detail: error.detail }),
),
);

const isAcpRegistryError = Schema.is(AcpRegistryError);
const toAcpRegistryError =
(agentId: string, fallback: string) =>
(cause: unknown): AcpRegistryError => {
if (isAcpRegistryError(cause)) return cause;
return new AcpRegistryError({
agentId,
detail: cause instanceof Error ? cause.message : fallback,
cause,
});
};

return {
list: () =>
Effect.gen(function* () {
const installs = yield* readInstalls;
return ACP_REGISTRY.map((entry) =>
buildEntryStatus(entry, installs[entry.id], availableChannels(entry, platform)),
);
}),

install: (agentId) =>
Effect.gen(function* () {
const entry = yield* requireEntry(agentId);
const result = yield* Effect.tryPromise({
try: () => installAgent(entry, { cacheRoot }),
catch: toAcpRegistryError(agentId, "Install failed"),
});
const settings = yield* readSettings;
const nextInstalls = {
...settings.acpRegistryInstalls,
[agentId]: result.state,
};

// Auto-create a default provider instance for this agent so it
// appears in the chat picker without forcing the user through
// the Add Provider Instance wizard. Users can still create
// additional instances or customize this one via Settings →
// Providers. If they already have any instance for this driver
// (manually-created or auto-created earlier), we leave it alone.
const driverKind = ProviderDriverKind.make(acpRegistryDriverKindFor(entry.id));
const existingInstances = settings.providerInstances ?? {};
const hasInstance = Object.values(existingInstances).some(
(instance) => instance.driver === driverKind,
);
const nextInstances = hasInstance
? existingInstances
: ({
...existingInstances,
[ProviderInstanceId.make(entry.id)]: {
driver: driverKind,
displayName: entry.name,
enabled: true,
} satisfies ProviderInstanceConfig,
} satisfies ProviderInstanceConfigMap);

yield* writeInstallAndInstance(nextInstalls, nextInstances);
return result.state;
}),

uninstall: (agentId) =>
Effect.gen(function* () {
const entry = yield* requireEntry(agentId);
yield* Effect.tryPromise({
try: () => uninstallAgent(entry, cacheRoot),
catch: toAcpRegistryError(agentId, "Uninstall failed"),
});
const installs = yield* readInstalls;
if (!(agentId in installs)) return;
const { [agentId]: _removed, ...rest } = installs;
yield* writeInstalls(rest);
}),
} satisfies AcpRegistryServiceShape;
}),
);

function buildEntryStatus(
entry: AcpRegistryEntry,
installed: AcpRegistryInstallState | undefined,
channels: ReadonlyArray<ReturnType<typeof availableChannels>[number]>,
): AcpRegistryEntryWithStatus {
return {
entry,
availableChannels: channels,
status: rollupStatus(entry, installed, channels),
...(installed ? { installed } : {}),
};
}

function rollupStatus(
entry: AcpRegistryEntry,
installed: AcpRegistryInstallState | undefined,
channels: ReadonlyArray<ReturnType<typeof availableChannels>[number]>,
): AcpRegistryInstallStatus {
if (channels.length === 0) return "unsupported";
if (!installed) return "not_installed";
return installed.version === entry.version ? "installed" : "update_available";
}
Loading
Loading