The best way to control Claude Code, Codex and more from any computer or phone

main
ts 351 lines 13.4 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as Schema from "effect/Schema";
3
4 import { ProjectId, ThreadId, TrimmedNonEmptyString, TrimmedString } from "./baseSchemas.ts";
5 import { ProviderOptionDescriptor } from "./model.ts";
6 import { ProviderInstanceId } from "./providerInstance.ts";
7
8 const AcpRegistryAgentId = TrimmedNonEmptyString.check(
9 Schema.isMaxLength(128),
10 Schema.isPattern(/^[a-z0-9][a-z0-9._-]*$/),
11 );
12
13 const ACP_REGISTRY_CDN_HOSTNAME = "cdn.agentclientprotocol.com";
14 const ACP_REGISTRY_AGENT_ID_PATTERN = /^[a-z0-9][a-z0-9._-]*$/;
15
16 export 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`;
23 }
24
25 export 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 }
42 }
43
44 export const AcpRegistryDistribution = Schema.Literals(["binary", "npx", "uvx"]);
45 export type AcpRegistryDistribution = typeof AcpRegistryDistribution.Type;
46
47 export const AcpRegistryIntegrity = Schema.Literals(["sha256", "registry"]);
48 export type AcpRegistryIntegrity = typeof AcpRegistryIntegrity.Type;
49
50 export const ACP_REGISTRY_MAX_AGENTS = 512;
51 export const ACP_REGISTRY_SEARCH_QUERY_MAX_LENGTH = 120;
52
53 export 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 });
59 export type AcpRegistrySearchInput = typeof AcpRegistrySearchInput.Type;
60
61 const AcpRegistryUrl = Schema.String.check(Schema.isMaxLength(2_048));
62
63 export 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 });
76 export type AcpRegistrySearchAgent = typeof AcpRegistrySearchAgent.Type;
77
78 export const AcpRegistrySearchResult = Schema.Struct({
79 agents: Schema.Array(AcpRegistrySearchAgent).check(Schema.isMaxLength(ACP_REGISTRY_MAX_AGENTS)),
80 });
81 export type AcpRegistrySearchResult = typeof AcpRegistrySearchResult.Type;
82
83 export const AcpRegistryPrepareInput = Schema.Struct({
84 agentId: AcpRegistryAgentId,
85 });
86 export type AcpRegistryPrepareInput = typeof AcpRegistryPrepareInput.Type;
87
88 export const AcpRegistryPrepareResult = Schema.Struct({
89 agentId: AcpRegistryAgentId,
90 version: TrimmedNonEmptyString.check(Schema.isMaxLength(128)),
91 distribution: AcpRegistryDistribution,
92 prepared: Schema.Literal(true),
93 });
94 export type AcpRegistryPrepareResult = typeof AcpRegistryPrepareResult.Type;
95
96 export const AcpRegistryManagedBinaryUninstallInput = Schema.Struct({
97 agentId: AcpRegistryAgentId,
98 });
99 export type AcpRegistryManagedBinaryUninstallInput =
100 typeof AcpRegistryManagedBinaryUninstallInput.Type;
101
102 export const AcpRegistryManagedBinaryUninstallResult = Schema.Struct({
103 agentId: AcpRegistryAgentId,
104 removed: Schema.Boolean,
105 });
106 export type AcpRegistryManagedBinaryUninstallResult =
107 typeof AcpRegistryManagedBinaryUninstallResult.Type;
108
109 const AcpRegistryProbeText = TrimmedNonEmptyString.check(Schema.isMaxLength(256));
110 const AcpRegistryProbeDescription = Schema.NullOr(Schema.String.check(Schema.isMaxLength(1_024)));
111
112 export 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 });
121 export type AcpRegistryProbeAuthMethod = typeof AcpRegistryProbeAuthMethod.Type;
122
123 export 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 });
130 export type AcpRegistryUrlAuthAction = typeof AcpRegistryUrlAuthAction.Type;
131
132 export const AcpRegistryAcceptUrlAuthInput = Schema.Struct({
133 instanceId: ProviderInstanceId,
134 elicitationId: AcpRegistryProbeText,
135 });
136 export type AcpRegistryAcceptUrlAuthInput = typeof AcpRegistryAcceptUrlAuthInput.Type;
137
138 export const AcpRegistryAcceptUrlAuthResult = Schema.Struct({
139 accepted: Schema.Boolean,
140 });
141 export type AcpRegistryAcceptUrlAuthResult = typeof AcpRegistryAcceptUrlAuthResult.Type;
142
143 const AcpRegistrySessionId = TrimmedNonEmptyString.check(Schema.isMaxLength(1_024));
144 const AcpRegistrySessionPath = TrimmedNonEmptyString.check(Schema.isMaxLength(4_096));
145
146 export 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 });
156 export type AcpRegistrySession = typeof AcpRegistrySession.Type;
157
158 export const AcpRegistryListSessionsInput = Schema.Struct({
159 instanceId: ProviderInstanceId,
160 projectId: ProjectId,
161 cursor: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(2_048))),
162 });
163 export type AcpRegistryListSessionsInput = typeof AcpRegistryListSessionsInput.Type;
164
165 export 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 });
172 export type AcpRegistryListSessionsResult = typeof AcpRegistryListSessionsResult.Type;
173
174 export 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 });
183 export type AcpRegistryImportSessionInput = typeof AcpRegistryImportSessionInput.Type;
184
185 export const AcpRegistryImportSessionResult = Schema.Struct({
186 threadId: ThreadId,
187 imported: Schema.Boolean,
188 });
189 export type AcpRegistryImportSessionResult = typeof AcpRegistryImportSessionResult.Type;
190
191 export const AcpRegistryDeleteSessionInput = Schema.Struct({
192 instanceId: ProviderInstanceId,
193 projectId: ProjectId,
194 sessionId: AcpRegistrySessionId,
195 });
196 export type AcpRegistryDeleteSessionInput = typeof AcpRegistryDeleteSessionInput.Type;
197
198 export const AcpRegistryDeleteSessionResult = Schema.Struct({ deleted: Schema.Literal(true) });
199 export type AcpRegistryDeleteSessionResult = typeof AcpRegistryDeleteSessionResult.Type;
200
201 const AcpRegistryProviderId = TrimmedNonEmptyString.check(Schema.isMaxLength(256));
202 const AcpRegistryProviderProtocol = TrimmedNonEmptyString.check(Schema.isMaxLength(64));
203
204 export 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 });
215 export type AcpRegistryConfigurableProvider = typeof AcpRegistryConfigurableProvider.Type;
216
217 export const AcpRegistryListProvidersInput = Schema.Struct({
218 instanceId: ProviderInstanceId,
219 projectId: ProjectId,
220 });
221 export type AcpRegistryListProvidersInput = typeof AcpRegistryListProvidersInput.Type;
222
223 export const AcpRegistryListProvidersResult = Schema.Struct({
224 providers: Schema.Array(AcpRegistryConfigurableProvider).check(Schema.isMaxLength(64)),
225 });
226 export type AcpRegistryListProvidersResult = typeof AcpRegistryListProvidersResult.Type;
227
228 export 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 });
242 export type AcpRegistrySetProviderInput = typeof AcpRegistrySetProviderInput.Type;
243
244 export const AcpRegistrySetProviderResult = Schema.Struct({ configured: Schema.Literal(true) });
245 export type AcpRegistrySetProviderResult = typeof AcpRegistrySetProviderResult.Type;
246
247 export const AcpRegistryDisableProviderInput = Schema.Struct({
248 instanceId: ProviderInstanceId,
249 projectId: ProjectId,
250 providerId: AcpRegistryProviderId,
251 });
252 export type AcpRegistryDisableProviderInput = typeof AcpRegistryDisableProviderInput.Type;
253
254 export const AcpRegistryDisableProviderResult = Schema.Struct({ disabled: Schema.Literal(true) });
255 export type AcpRegistryDisableProviderResult = typeof AcpRegistryDisableProviderResult.Type;
256
257 export const AcpRegistryLogoutInput = Schema.Struct({
258 instanceId: ProviderInstanceId,
259 });
260 export type AcpRegistryLogoutInput = typeof AcpRegistryLogoutInput.Type;
261
262 export const AcpRegistryLogoutResult = Schema.Struct({
263 loggedOut: Schema.Literal(true),
264 });
265 export type AcpRegistryLogoutResult = typeof AcpRegistryLogoutResult.Type;
266
267 export 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 });
274 export type AcpRegistryProbeModel = typeof AcpRegistryProbeModel.Type;
275
276 export 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 });
310 export type AcpRegistryProbeResult = typeof AcpRegistryProbeResult.Type;
311
312 export 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 ]);
338 export type AcpRegistryOperationErrorReason = typeof AcpRegistryOperationErrorReason.Type;
339
340 export 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 ) {}