Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

acpRegistry.ts

351 lines13.4 KB

1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3​
4import { ProjectId, ThreadId, TrimmedNonEmptyString, TrimmedString } from "./baseSchemas.ts";
5import { ProviderOptionDescriptor } from "./model.ts";
6import { ProviderInstanceId } from "./providerInstance.ts";
7​
8const AcpRegistryAgentId = TrimmedNonEmptyString.check(
9 Schema.isMaxLength(128),
10 Schema.isPattern(/^[a-z0-9][a-z0-9._-]*$/),
11);
12​
13const ACP_REGISTRY_CDN_HOSTNAME = "cdn.agentclientprotocol.com";
14const ACP_REGISTRY_AGENT_ID_PATTERN = /^[a-z0-9][a-z0-9._-]*$/;
15​
16export function officialAcpRegistryIconUrlForAgentId(
17 agentId: string | null | undefined,
18): string | null {
19 if (agentId === null || agentId === undefined || !ACP_REGISTRY_AGENT_ID_PATTERN.test(agentId)) {
20 return null;
21 }
22 return `https://${ACP_REGISTRY_CDN_HOSTNAME}/registry/v1/latest/${agentId}.svg`;
24​
25export function resolveOfficialAcpRegistryIconUrl(icon: string | null | undefined): string | null {
26 if (!icon) return null;
27 try {
28 const url = new URL(icon);
29 if (
30 url.protocol !== "https:" ||
31 url.hostname !== ACP_REGISTRY_CDN_HOSTNAME ||
32 url.port !== "" ||
33 url.username !== "" ||
34 url.password !== ""
35 ) {
36 return null;
37 }
38 return url.href;
39 } catch {
40 return null;
41 }
43​
44export const AcpRegistryDistribution = Schema.Literals(["binary", "npx", "uvx"]);
45export type AcpRegistryDistribution = typeof AcpRegistryDistribution.Type;
46​
47export const AcpRegistryIntegrity = Schema.Literals(["sha256", "registry"]);
48export type AcpRegistryIntegrity = typeof AcpRegistryIntegrity.Type;
49​
50export const ACP_REGISTRY_MAX_AGENTS = 512;
51export const ACP_REGISTRY_SEARCH_QUERY_MAX_LENGTH = 120;
52​
53export const AcpRegistrySearchInput = Schema.Struct({
54 query: TrimmedString.check(Schema.isMaxLength(ACP_REGISTRY_SEARCH_QUERY_MAX_LENGTH)),
55 limit: Schema.optional(
56 Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: ACP_REGISTRY_MAX_AGENTS })),
57 ),
58});
59export type AcpRegistrySearchInput = typeof AcpRegistrySearchInput.Type;
60​
61const AcpRegistryUrl = Schema.String.check(Schema.isMaxLength(2_048));
62​
63export const AcpRegistrySearchAgent = Schema.Struct({
64 id: AcpRegistryAgentId,
65 name: TrimmedNonEmptyString.check(Schema.isMaxLength(160)),
66 version: TrimmedNonEmptyString.check(Schema.isMaxLength(128)),
67 description: Schema.String.check(Schema.isMaxLength(1_024)),
68 authors: Schema.Array(Schema.String.check(Schema.isMaxLength(256))).check(Schema.isMaxLength(16)),
69 license: Schema.NullOr(Schema.String.check(Schema.isMaxLength(128))),
70 website: Schema.NullOr(AcpRegistryUrl),
71 repository: Schema.NullOr(AcpRegistryUrl),
72 icon: Schema.NullOr(AcpRegistryUrl),
73 distribution: AcpRegistryDistribution,
74 integrity: AcpRegistryIntegrity,
75});
76export type AcpRegistrySearchAgent = typeof AcpRegistrySearchAgent.Type;
77​
78export const AcpRegistrySearchResult = Schema.Struct({
79 agents: Schema.Array(AcpRegistrySearchAgent).check(Schema.isMaxLength(ACP_REGISTRY_MAX_AGENTS)),
80});
81export type AcpRegistrySearchResult = typeof AcpRegistrySearchResult.Type;
82​
83export const AcpRegistryPrepareInput = Schema.Struct({
84 agentId: AcpRegistryAgentId,
85});
86export type AcpRegistryPrepareInput = typeof AcpRegistryPrepareInput.Type;
87​
88export const AcpRegistryPrepareResult = Schema.Struct({
89 agentId: AcpRegistryAgentId,
90 version: TrimmedNonEmptyString.check(Schema.isMaxLength(128)),
91 distribution: AcpRegistryDistribution,
92 prepared: Schema.Literal(true),
93});
94export type AcpRegistryPrepareResult = typeof AcpRegistryPrepareResult.Type;
95​
96export const AcpRegistryManagedBinaryUninstallInput = Schema.Struct({
97 agentId: AcpRegistryAgentId,
98});
99export type AcpRegistryManagedBinaryUninstallInput =
100 typeof AcpRegistryManagedBinaryUninstallInput.Type;
101​
102export const AcpRegistryManagedBinaryUninstallResult = Schema.Struct({
103 agentId: AcpRegistryAgentId,
104 removed: Schema.Boolean,
105});
106export type AcpRegistryManagedBinaryUninstallResult =
107 typeof AcpRegistryManagedBinaryUninstallResult.Type;
108​
109const AcpRegistryProbeText = TrimmedNonEmptyString.check(Schema.isMaxLength(256));
110const AcpRegistryProbeDescription = Schema.NullOr(Schema.String.check(Schema.isMaxLength(1_024)));
111​
112export const AcpRegistryProbeAuthMethod = Schema.Struct({
113 id: AcpRegistryProbeText,
114 name: AcpRegistryProbeText,
115 description: AcpRegistryProbeDescription,
116 type: Schema.Literals(["agent", "env_var", "terminal"]),
117 command: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(2_048))),
118 envVarNames: Schema.optionalKey(Schema.Array(AcpRegistryProbeText).check(Schema.isMaxLength(16))),
119 link: Schema.optionalKey(AcpRegistryUrl),
120});
121export type AcpRegistryProbeAuthMethod = typeof AcpRegistryProbeAuthMethod.Type;
122​
123export const AcpRegistryUrlAuthAction = Schema.Struct({
124 elicitationId: AcpRegistryProbeText,
125 url: AcpRegistryUrl,
126 message: Schema.String.check(Schema.isMaxLength(1_024)),
127 createdAt: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(128))),
128 expiresAt: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(128))),
129});
130export type AcpRegistryUrlAuthAction = typeof AcpRegistryUrlAuthAction.Type;
131​
132export const AcpRegistryAcceptUrlAuthInput = Schema.Struct({
133 instanceId: ProviderInstanceId,
134 elicitationId: AcpRegistryProbeText,
135});
136export type AcpRegistryAcceptUrlAuthInput = typeof AcpRegistryAcceptUrlAuthInput.Type;
137​
138export const AcpRegistryAcceptUrlAuthResult = Schema.Struct({
139 accepted: Schema.Boolean,
140});
141export type AcpRegistryAcceptUrlAuthResult = typeof AcpRegistryAcceptUrlAuthResult.Type;
142​
143const AcpRegistrySessionId = TrimmedNonEmptyString.check(Schema.isMaxLength(1_024));
144const AcpRegistrySessionPath = TrimmedNonEmptyString.check(Schema.isMaxLength(4_096));
145​
146export const AcpRegistrySession = Schema.Struct({
147 sessionId: AcpRegistrySessionId,
148 cwd: AcpRegistrySessionPath,
149 additionalDirectories: Schema.Array(AcpRegistrySessionPath).check(Schema.isMaxLength(32)),
150 title: Schema.NullOr(Schema.String.check(Schema.isMaxLength(1_024))),
151 updatedAt: Schema.NullOr(Schema.String.check(Schema.isMaxLength(128))),
152 importedThreadId: Schema.NullOr(ThreadId),
153 /** The imported thread still lacks the session's history, so importing again copies it. */
154 nativeHistoryPending: Schema.optionalKey(Schema.Boolean),
155});
156export type AcpRegistrySession = typeof AcpRegistrySession.Type;
157​
158export const AcpRegistryListSessionsInput = Schema.Struct({
159 instanceId: ProviderInstanceId,
160 projectId: ProjectId,
161 cursor: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(2_048))),
162});
163export type AcpRegistryListSessionsInput = typeof AcpRegistryListSessionsInput.Type;
164​
165export const AcpRegistryListSessionsResult = Schema.Struct({
166 sessions: Schema.Array(AcpRegistrySession).check(Schema.isMaxLength(256)),
167 nextCursor: Schema.NullOr(Schema.String.check(Schema.isMaxLength(2_048))),
168 canLoad: Schema.Boolean,
169 canResume: Schema.Boolean,
170 canDelete: Schema.Boolean,
171});
172export type AcpRegistryListSessionsResult = typeof AcpRegistryListSessionsResult.Type;
173​
174export const AcpRegistryImportSessionInput = Schema.Struct({
175 instanceId: ProviderInstanceId,
176 projectId: ProjectId,
177 sessionId: AcpRegistrySessionId,
178 title: Schema.optionalKey(Schema.NullOr(TrimmedNonEmptyString.check(Schema.isMaxLength(1_024)))),
179 updatedAt: Schema.optionalKey(
180 Schema.NullOr(TrimmedNonEmptyString.check(Schema.isMaxLength(128))),
181 ),
182});
183export type AcpRegistryImportSessionInput = typeof AcpRegistryImportSessionInput.Type;
184​
185export const AcpRegistryImportSessionResult = Schema.Struct({
186 threadId: ThreadId,
187 imported: Schema.Boolean,
188});
189export type AcpRegistryImportSessionResult = typeof AcpRegistryImportSessionResult.Type;
190​
191export const AcpRegistryDeleteSessionInput = Schema.Struct({
192 instanceId: ProviderInstanceId,
193 projectId: ProjectId,
194 sessionId: AcpRegistrySessionId,
195});
196export type AcpRegistryDeleteSessionInput = typeof AcpRegistryDeleteSessionInput.Type;
197​
198export const AcpRegistryDeleteSessionResult = Schema.Struct({ deleted: Schema.Literal(true) });
199export type AcpRegistryDeleteSessionResult = typeof AcpRegistryDeleteSessionResult.Type;
200​
201const AcpRegistryProviderId = TrimmedNonEmptyString.check(Schema.isMaxLength(256));
202const AcpRegistryProviderProtocol = TrimmedNonEmptyString.check(Schema.isMaxLength(64));
203​
204export const AcpRegistryConfigurableProvider = Schema.Struct({
205 providerId: AcpRegistryProviderId,
206 supported: Schema.Array(AcpRegistryProviderProtocol).check(Schema.isMaxLength(16)),
207 required: Schema.Boolean,
208 current: Schema.NullOr(
209 Schema.Struct({
210 apiType: AcpRegistryProviderProtocol,
211 baseUrl: AcpRegistryUrl,
212 }),
213 ),
214});
215export type AcpRegistryConfigurableProvider = typeof AcpRegistryConfigurableProvider.Type;
216​
217export const AcpRegistryListProvidersInput = Schema.Struct({
218 instanceId: ProviderInstanceId,
219 projectId: ProjectId,
220});
221export type AcpRegistryListProvidersInput = typeof AcpRegistryListProvidersInput.Type;
222​
223export const AcpRegistryListProvidersResult = Schema.Struct({
224 providers: Schema.Array(AcpRegistryConfigurableProvider).check(Schema.isMaxLength(64)),
225});
226export type AcpRegistryListProvidersResult = typeof AcpRegistryListProvidersResult.Type;
227​
228export const AcpRegistrySetProviderInput = Schema.Struct({
229 instanceId: ProviderInstanceId,
230 projectId: ProjectId,
231 providerId: AcpRegistryProviderId,
232 apiType: AcpRegistryProviderProtocol,
233 baseUrl: AcpRegistryUrl,
234 // Write-only secrets. Provider list responses intentionally cannot carry headers.
235 headers: Schema.optionalKey(
236 Schema.Record(
237 TrimmedNonEmptyString.check(Schema.isMaxLength(128)),
238 Schema.String.check(Schema.isMaxLength(8_192)),
239 ),
240 ),
241});
242export type AcpRegistrySetProviderInput = typeof AcpRegistrySetProviderInput.Type;
243​
244export const AcpRegistrySetProviderResult = Schema.Struct({ configured: Schema.Literal(true) });
245export type AcpRegistrySetProviderResult = typeof AcpRegistrySetProviderResult.Type;
246​
247export const AcpRegistryDisableProviderInput = Schema.Struct({
248 instanceId: ProviderInstanceId,
249 projectId: ProjectId,
250 providerId: AcpRegistryProviderId,
251});
252export type AcpRegistryDisableProviderInput = typeof AcpRegistryDisableProviderInput.Type;
253​
254export const AcpRegistryDisableProviderResult = Schema.Struct({ disabled: Schema.Literal(true) });
255export type AcpRegistryDisableProviderResult = typeof AcpRegistryDisableProviderResult.Type;
256​
257export const AcpRegistryLogoutInput = Schema.Struct({
258 instanceId: ProviderInstanceId,
259});
260export type AcpRegistryLogoutInput = typeof AcpRegistryLogoutInput.Type;
261​
262export const AcpRegistryLogoutResult = Schema.Struct({
263 loggedOut: Schema.Literal(true),
264});
265export type AcpRegistryLogoutResult = typeof AcpRegistryLogoutResult.Type;
266​
267export const AcpRegistryProbeModel = Schema.Struct({
268 id: AcpRegistryProbeText,
269 name: AcpRegistryProbeText,
270 description: AcpRegistryProbeDescription,
271 // The reasoning control the agent advertises for this model; null when it has none.
272 reasoningEffort: Schema.optionalKey(Schema.NullOr(ProviderOptionDescriptor)),
273});
274export type AcpRegistryProbeModel = typeof AcpRegistryProbeModel.Type;
275​
276export const AcpRegistryProbeResult = Schema.Struct({
277 instanceId: ProviderInstanceId,
278 // `ready` requires a disposable ACP session/new probe, not passive auth detection.
279 ready: Schema.Literal(true),
280 icon: Schema.NullOr(AcpRegistryUrl),
281 agentVersion: Schema.optionalKey(AcpRegistryProbeText),
282 authMethods: Schema.Array(AcpRegistryProbeAuthMethod).check(Schema.isMaxLength(32)),
283 models: Schema.Array(AcpRegistryProbeModel).check(Schema.isMaxLength(256)),
284 currentModelId: Schema.NullOr(AcpRegistryProbeText),
285 configOptions: Schema.Array(ProviderOptionDescriptor).check(Schema.isMaxLength(16)),
286 // configOptions ids that describe only the current model (ACP thought_level / model_config).
287 modelOptionIds: Schema.optionalKey(
288 Schema.Array(AcpRegistryProbeText).check(Schema.isMaxLength(16)),
289 ),
290 sessionManagement: Schema.Struct({
291 canList: Schema.Boolean,
292 canLoad: Schema.Boolean,
293 canResume: Schema.Boolean,
294 canLogout: Schema.Boolean,
295 canDelete: Schema.Boolean,
296 canConfigureProviders: Schema.Boolean,
297 }).pipe(
298 Schema.withDecodingDefault(
299 Effect.succeed({
300 canList: false,
301 canLoad: false,
302 canResume: false,
303 canLogout: false,
304 canDelete: false,
305 canConfigureProviders: false,
306 }),
307 ),
308 ),
309});
310export type AcpRegistryProbeResult = typeof AcpRegistryProbeResult.Type;
311​
312export const AcpRegistryOperationErrorReason = Schema.Literals([
313 "agent_not_configured",
314 "agent_not_found",
315 "archive_invalid",
316 "authentication_failed",
317 "checksum_mismatch",
318 "download_failed",
319 "install_failed",
320 "instance_not_found",
321 "logout_unsupported",
322 "logout_failed",
323 "probe_failed",
324 "project_not_found",
325 "registry_unavailable",
326 "runner_unavailable",
327 "session_import_failed",
328 "session_delete_unsupported",
329 "session_delete_failed",
330 "session_list_unsupported",
331 "session_resume_unsupported",
332 "providers_unsupported",
333 "providers_list_failed",
334 "provider_configuration_failed",
335 "unsupported_distribution",
336 "unsupported_platform",
337]);
338export type AcpRegistryOperationErrorReason = typeof AcpRegistryOperationErrorReason.Type;
339​
340export class AcpRegistryOperationError extends Schema.TaggedError<AcpRegistryOperationError>()(
341 "AcpRegistryOperationError",
342 {
343 reason: AcpRegistryOperationErrorReason,
344 message: Schema.String,
345 authMethods: Schema.optionalKey(
346 Schema.Array(AcpRegistryProbeAuthMethod).check(Schema.isMaxLength(32)),
347 ),
348 authAction: Schema.optionalKey(AcpRegistryUrlAuthAction),
349 cause: Schema.optionalKey(Schema.Defect()),
350 },
351) {}