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

acpRegistry.ts

337 lines12.6 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 AcpRegistrySearchInput = Schema.Struct({
51 query: TrimmedString.check(Schema.isMaxLength(120)),
52});
53export type AcpRegistrySearchInput = typeof AcpRegistrySearchInput.Type;
54​
55const AcpRegistryUrl = Schema.String.check(Schema.isMaxLength(2_048));
56​
57export const AcpRegistrySearchAgent = Schema.Struct({
58 id: AcpRegistryAgentId,
59 name: TrimmedNonEmptyString.check(Schema.isMaxLength(160)),
60 version: TrimmedNonEmptyString.check(Schema.isMaxLength(128)),
61 description: Schema.String.check(Schema.isMaxLength(1_024)),
62 authors: Schema.Array(Schema.String.check(Schema.isMaxLength(256))).check(Schema.isMaxLength(16)),
63 license: Schema.NullOr(Schema.String.check(Schema.isMaxLength(128))),
64 website: Schema.NullOr(AcpRegistryUrl),
65 repository: Schema.NullOr(AcpRegistryUrl),
66 icon: Schema.NullOr(AcpRegistryUrl),
67 distribution: AcpRegistryDistribution,
68 integrity: AcpRegistryIntegrity,
69});
70export type AcpRegistrySearchAgent = typeof AcpRegistrySearchAgent.Type;
71​
72export const AcpRegistrySearchResult = Schema.Struct({
73 agents: Schema.Array(AcpRegistrySearchAgent).check(Schema.isMaxLength(20)),
74});
75export type AcpRegistrySearchResult = typeof AcpRegistrySearchResult.Type;
76​
77export const AcpRegistryPrepareInput = Schema.Struct({
78 agentId: AcpRegistryAgentId,
79});
80export type AcpRegistryPrepareInput = typeof AcpRegistryPrepareInput.Type;
81​
82export const AcpRegistryPrepareResult = Schema.Struct({
83 agentId: AcpRegistryAgentId,
84 version: TrimmedNonEmptyString.check(Schema.isMaxLength(128)),
85 distribution: AcpRegistryDistribution,
86 prepared: Schema.Literal(true),
87});
88export type AcpRegistryPrepareResult = typeof AcpRegistryPrepareResult.Type;
89​
90export const AcpRegistryManagedBinaryUninstallInput = Schema.Struct({
91 agentId: AcpRegistryAgentId,
92});
93export type AcpRegistryManagedBinaryUninstallInput =
94 typeof AcpRegistryManagedBinaryUninstallInput.Type;
95​
96export const AcpRegistryManagedBinaryUninstallResult = Schema.Struct({
97 agentId: AcpRegistryAgentId,
98 removed: Schema.Boolean,
99});
100export type AcpRegistryManagedBinaryUninstallResult =
101 typeof AcpRegistryManagedBinaryUninstallResult.Type;
102​
103const AcpRegistryProbeText = TrimmedNonEmptyString.check(Schema.isMaxLength(256));
104const AcpRegistryProbeDescription = Schema.NullOr(Schema.String.check(Schema.isMaxLength(1_024)));
105​
106export const AcpRegistryProbeAuthMethod = Schema.Struct({
107 id: AcpRegistryProbeText,
108 name: AcpRegistryProbeText,
109 description: AcpRegistryProbeDescription,
110 type: Schema.Literals(["agent", "env_var", "terminal"]),
111 command: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(2_048))),
112 envVarNames: Schema.optionalKey(Schema.Array(AcpRegistryProbeText).check(Schema.isMaxLength(16))),
113 link: Schema.optionalKey(AcpRegistryUrl),
114});
115export type AcpRegistryProbeAuthMethod = typeof AcpRegistryProbeAuthMethod.Type;
116​
117export const AcpRegistryUrlAuthAction = Schema.Struct({
118 elicitationId: AcpRegistryProbeText,
119 url: AcpRegistryUrl,
120 message: Schema.String.check(Schema.isMaxLength(1_024)),
121 createdAt: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(128))),
122 expiresAt: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(128))),
123});
124export type AcpRegistryUrlAuthAction = typeof AcpRegistryUrlAuthAction.Type;
125​
126export const AcpRegistryAcceptUrlAuthInput = Schema.Struct({
127 instanceId: ProviderInstanceId,
128 elicitationId: AcpRegistryProbeText,
129});
130export type AcpRegistryAcceptUrlAuthInput = typeof AcpRegistryAcceptUrlAuthInput.Type;
131​
132export const AcpRegistryAcceptUrlAuthResult = Schema.Struct({
133 accepted: Schema.Boolean,
134});
135export type AcpRegistryAcceptUrlAuthResult = typeof AcpRegistryAcceptUrlAuthResult.Type;
136​
137const AcpRegistrySessionId = TrimmedNonEmptyString.check(Schema.isMaxLength(1_024));
138const AcpRegistrySessionPath = TrimmedNonEmptyString.check(Schema.isMaxLength(4_096));
139​
140export const AcpRegistrySession = Schema.Struct({
141 sessionId: AcpRegistrySessionId,
142 cwd: AcpRegistrySessionPath,
143 additionalDirectories: Schema.Array(AcpRegistrySessionPath).check(Schema.isMaxLength(32)),
144 title: Schema.NullOr(Schema.String.check(Schema.isMaxLength(1_024))),
145 updatedAt: Schema.NullOr(Schema.String.check(Schema.isMaxLength(128))),
146 importedThreadId: Schema.NullOr(ThreadId),
147});
148export type AcpRegistrySession = typeof AcpRegistrySession.Type;
149​
150export const AcpRegistryListSessionsInput = Schema.Struct({
151 instanceId: ProviderInstanceId,
152 projectId: ProjectId,
153 cursor: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(2_048))),
154});
155export type AcpRegistryListSessionsInput = typeof AcpRegistryListSessionsInput.Type;
156​
157export const AcpRegistryListSessionsResult = Schema.Struct({
158 sessions: Schema.Array(AcpRegistrySession).check(Schema.isMaxLength(256)),
159 nextCursor: Schema.NullOr(Schema.String.check(Schema.isMaxLength(2_048))),
160 canLoad: Schema.Boolean,
161 canResume: Schema.Boolean,
162 canDelete: Schema.Boolean,
163});
164export type AcpRegistryListSessionsResult = typeof AcpRegistryListSessionsResult.Type;
165​
166export const AcpRegistryImportSessionInput = Schema.Struct({
167 instanceId: ProviderInstanceId,
168 projectId: ProjectId,
169 sessionId: AcpRegistrySessionId,
170 title: Schema.optionalKey(Schema.NullOr(TrimmedNonEmptyString.check(Schema.isMaxLength(1_024)))),
171 updatedAt: Schema.optionalKey(
172 Schema.NullOr(TrimmedNonEmptyString.check(Schema.isMaxLength(128))),
173 ),
174});
175export type AcpRegistryImportSessionInput = typeof AcpRegistryImportSessionInput.Type;
176​
177export const AcpRegistryImportSessionResult = Schema.Struct({
178 threadId: ThreadId,
179 imported: Schema.Boolean,
180});
181export type AcpRegistryImportSessionResult = typeof AcpRegistryImportSessionResult.Type;
182​
183export const AcpRegistryDeleteSessionInput = Schema.Struct({
184 instanceId: ProviderInstanceId,
185 projectId: ProjectId,
186 sessionId: AcpRegistrySessionId,
187});
188export type AcpRegistryDeleteSessionInput = typeof AcpRegistryDeleteSessionInput.Type;
189​
190export const AcpRegistryDeleteSessionResult = Schema.Struct({ deleted: Schema.Literal(true) });
191export type AcpRegistryDeleteSessionResult = typeof AcpRegistryDeleteSessionResult.Type;
192​
193const AcpRegistryProviderId = TrimmedNonEmptyString.check(Schema.isMaxLength(256));
194const AcpRegistryProviderProtocol = TrimmedNonEmptyString.check(Schema.isMaxLength(64));
195​
196export const AcpRegistryConfigurableProvider = Schema.Struct({
197 providerId: AcpRegistryProviderId,
198 supported: Schema.Array(AcpRegistryProviderProtocol).check(Schema.isMaxLength(16)),
199 required: Schema.Boolean,
200 current: Schema.NullOr(
201 Schema.Struct({
202 apiType: AcpRegistryProviderProtocol,
203 baseUrl: AcpRegistryUrl,
204 }),
205 ),
206});
207export type AcpRegistryConfigurableProvider = typeof AcpRegistryConfigurableProvider.Type;
208​
209export const AcpRegistryListProvidersInput = Schema.Struct({
210 instanceId: ProviderInstanceId,
211 projectId: ProjectId,
212});
213export type AcpRegistryListProvidersInput = typeof AcpRegistryListProvidersInput.Type;
214​
215export const AcpRegistryListProvidersResult = Schema.Struct({
216 providers: Schema.Array(AcpRegistryConfigurableProvider).check(Schema.isMaxLength(64)),
217});
218export type AcpRegistryListProvidersResult = typeof AcpRegistryListProvidersResult.Type;
219​
220export const AcpRegistrySetProviderInput = Schema.Struct({
221 instanceId: ProviderInstanceId,
222 projectId: ProjectId,
223 providerId: AcpRegistryProviderId,
224 apiType: AcpRegistryProviderProtocol,
225 baseUrl: AcpRegistryUrl,
226 // Write-only secrets. Provider list responses intentionally cannot carry headers.
227 headers: Schema.optionalKey(
228 Schema.Record(
229 TrimmedNonEmptyString.check(Schema.isMaxLength(128)),
230 Schema.String.check(Schema.isMaxLength(8_192)),
231 ),
232 ),
233});
234export type AcpRegistrySetProviderInput = typeof AcpRegistrySetProviderInput.Type;
235​
236export const AcpRegistrySetProviderResult = Schema.Struct({ configured: Schema.Literal(true) });
237export type AcpRegistrySetProviderResult = typeof AcpRegistrySetProviderResult.Type;
238​
239export const AcpRegistryDisableProviderInput = Schema.Struct({
240 instanceId: ProviderInstanceId,
241 projectId: ProjectId,
242 providerId: AcpRegistryProviderId,
243});
244export type AcpRegistryDisableProviderInput = typeof AcpRegistryDisableProviderInput.Type;
245​
246export const AcpRegistryDisableProviderResult = Schema.Struct({ disabled: Schema.Literal(true) });
247export type AcpRegistryDisableProviderResult = typeof AcpRegistryDisableProviderResult.Type;
248​
249export const AcpRegistryLogoutInput = Schema.Struct({
250 instanceId: ProviderInstanceId,
251});
252export type AcpRegistryLogoutInput = typeof AcpRegistryLogoutInput.Type;
253​
254export const AcpRegistryLogoutResult = Schema.Struct({
255 loggedOut: Schema.Literal(true),
256});
257export type AcpRegistryLogoutResult = typeof AcpRegistryLogoutResult.Type;
258​
259export const AcpRegistryProbeModel = Schema.Struct({
260 id: AcpRegistryProbeText,
261 name: AcpRegistryProbeText,
262 description: AcpRegistryProbeDescription,
263});
264export type AcpRegistryProbeModel = typeof AcpRegistryProbeModel.Type;
265​
266export const AcpRegistryProbeResult = Schema.Struct({
267 instanceId: ProviderInstanceId,
268 // `ready` is only returned after a disposable ACP session/new probe completes.
269 // It does not imply that authentication was passively detected.
270 ready: Schema.Literal(true),
271 icon: Schema.NullOr(AcpRegistryUrl),
272 authMethods: Schema.Array(AcpRegistryProbeAuthMethod).check(Schema.isMaxLength(32)),
273 models: Schema.Array(AcpRegistryProbeModel).check(Schema.isMaxLength(256)),
274 currentModelId: Schema.NullOr(AcpRegistryProbeText),
275 configOptions: Schema.Array(ProviderOptionDescriptor).check(Schema.isMaxLength(16)),
276 sessionManagement: Schema.Struct({
277 canList: Schema.Boolean,
278 canLoad: Schema.Boolean,
279 canResume: Schema.Boolean,
280 canLogout: Schema.Boolean,
281 canDelete: Schema.Boolean,
282 canConfigureProviders: Schema.Boolean,
283 }).pipe(
284 Schema.withDecodingDefault(
285 Effect.succeed({
286 canList: false,
287 canLoad: false,
288 canResume: false,
289 canLogout: false,
290 canDelete: false,
291 canConfigureProviders: false,
292 }),
293 ),
294 ),
295});
296export type AcpRegistryProbeResult = typeof AcpRegistryProbeResult.Type;
297​
298export const AcpRegistryOperationErrorReason = Schema.Literals([
299 "agent_not_configured",
300 "agent_not_found",
301 "archive_invalid",
302 "authentication_failed",
303 "checksum_mismatch",
304 "download_failed",
305 "install_failed",
306 "instance_not_found",
307 "logout_unsupported",
308 "logout_failed",
309 "probe_failed",
310 "project_not_found",
311 "registry_unavailable",
312 "runner_unavailable",
313 "session_import_failed",
314 "session_delete_unsupported",
315 "session_delete_failed",
316 "session_list_unsupported",
317 "session_resume_unsupported",
318 "providers_unsupported",
319 "providers_list_failed",
320 "provider_configuration_failed",
321 "unsupported_distribution",
322 "unsupported_platform",
323]);
324export type AcpRegistryOperationErrorReason = typeof AcpRegistryOperationErrorReason.Type;
325​
326export class AcpRegistryOperationError extends Schema.TaggedError<AcpRegistryOperationError>()(
327 "AcpRegistryOperationError",
328 {
329 reason: AcpRegistryOperationErrorReason,
330 message: Schema.String,
331 authMethods: Schema.optionalKey(
332 Schema.Array(AcpRegistryProbeAuthMethod).check(Schema.isMaxLength(32)),
333 ),
334 authAction: Schema.optionalKey(AcpRegistryUrlAuthAction),
335 cause: Schema.optionalKey(Schema.Defect()),
336 },
337) {}