Open History to see the last commit
1import * as Context from "effect/Context";
2import type * as DateTime from "effect/DateTime";
3import * as Schema from "effect/Schema";
4import * as HttpApi from "effect/http-api/HttpApi";
5import * as HttpApiEndpoint from "effect/http-api/HttpApiEndpoint";
6import * as HttpApiGroup from "effect/http-api/HttpApiGroup";
7import * as HttpApiMiddleware from "effect/http-api/HttpApiMiddleware";
8import * as HttpApiSchema from "effect/http-api/HttpApiSchema";
9import * as HttpServerRespondable from "effect/http/HttpServerRespondable";
10import * as HttpServerResponse from "effect/http/HttpServerResponse";
11
12import {
13 AuthAccessTokenResult,
14 AuthBrowserSessionRequest,
15 AuthBrowserSessionResult,
16 AuthClientSession,
17 AuthCreatePairingCredentialInput,
18 AuthMcpApprovalDecisionRequest,
19 AuthMcpApprovalDetails,
20 AuthMcpApprovalError,
21 AuthMcpApprovalRedirect,
22 AuthMcpAuthorizationRequest,
23 AuthMcpAuthorizationServerMetadata,
24 AuthMcpClientRegistration,
25 AuthMcpProtectedResourceMetadata,
26 AuthMcpRegisteredClient,
27 AuthMcpRegistrationError,
28 AuthMcpTokenError,
29 AuthMcpTokenRequest,
30 AuthMcpTokenResult,
31 AuthPairingCredentialResult,
32 AuthPairingLink,
33 AuthRevokeClientSessionInput,
34 AuthRevokePairingLinkInput,
35 AuthEnvironmentScope,
36 AuthTokenExchangeRequest,
37 AuthSessionState,
38 AuthWebSocketTicketResult,
39 ServerAuthSessionMethod,
40} from "./auth.ts";
41import {
42 ExecutionEnvironmentDescriptor,
43 ORCHESTRATION_PROTOCOL_HEADER,
44 ORCHESTRATION_PROTOCOL_VERSION_TEXT,
45} from "./environment.ts";
46import {
47 DpopFailureReason,
48 AuthSessionId,
49 ThreadId,
50 TrimmedNonEmptyString,
51} from "./baseSchemas.ts";
52import {
53 OrchestrationV2ShellSnapshot,
54 OrchestrationV2ThreadBoundedSnapshot,
55 OrchestrationV2ThreadDetailSnapshot,
56 OrchestrationV2ThreadHistoryPage,
57 OrchestrationV2ThreadTranscript,
58} from "./orchestrationV2.ts";
59import { Project, ProjectMutation, ProjectSnapshot } from "./project.ts";
60import {
61 PullRequestDiffInput,
62 PullRequestDiffResult,
63 PullRequestOperationError,
64 PullRequestUnavailableError,
65} from "./pullRequest.ts";
66import {
67 RelayCloudEnvironmentHealthRequest,
68 RelayCloudMintCredentialRequest,
69 RelayEnvironmentConfigRequest,
70 RelayEnvironmentHealthResponse,
71 RelayEnvironmentLinkProof,
72 RelayEnvironmentMintResponse,
73 RelayLinkProofRequest,
74} from "./relay.ts";
75
76const OptionalBearerHeaders = Schema.Struct({
77 authorization: Schema.optionalKey(Schema.String),
78 dpop: Schema.optionalKey(Schema.String),
79});
80
81const OrchestrationProtocolHeaders = Schema.Struct({
82 authorization: Schema.optionalKey(Schema.String),
83 dpop: Schema.optionalKey(Schema.String),
84 [ORCHESTRATION_PROTOCOL_HEADER]: Schema.Literal(ORCHESTRATION_PROTOCOL_VERSION_TEXT),
85});
86
87const OptionalDpopProofHeaders = Schema.Struct({
88 dpop: Schema.optionalKey(Schema.String),
89});
90
91export const EnvironmentRequestInvalidReason = Schema.Literals([
92 "invalid_scope",
93 "scope_not_granted",
94 "invalid_command",
95 "invalid_history_cursor",
96 "thread_transcript_too_large",
97]);
98export type EnvironmentRequestInvalidReason = typeof EnvironmentRequestInvalidReason.Type;
99
100export const EnvironmentAuthInvalidReason = Schema.Literals([
101 "missing_credential",
102 "invalid_credential",
103]);
104export type EnvironmentAuthInvalidReason = typeof EnvironmentAuthInvalidReason.Type;
105
106export const EnvironmentOperationForbiddenReason = Schema.Literals([
107 "current_session_revoke_not_allowed",
108]);
109export type EnvironmentOperationForbiddenReason = typeof EnvironmentOperationForbiddenReason.Type;
110
111export const EnvironmentInternalErrorReason = Schema.Literals([
112 "bootstrap_validation_failed",
113 "browser_session_issuance_failed",
114 "browser_session_cookie_failed",
115 "access_token_issuance_failed",
116 "websocket_ticket_issuance_failed",
117 "pairing_credential_issuance_failed",
118 "pairing_links_load_failed",
119 "pairing_link_revoke_failed",
120 "client_sessions_load_failed",
121 "client_session_revoke_failed",
122 "project_snapshot_failed",
123 "project_mutation_failed",
124 "orchestration_snapshot_failed",
125 "orchestration_thread_snapshot_failed",
126 "orchestration_thread_bounded_snapshot_failed",
127 "orchestration_thread_history_failed",
128 "internal_error",
129]);
130export type EnvironmentInternalErrorReason = typeof EnvironmentInternalErrorReason.Type;
131
132export class EnvironmentRequestInvalidError extends Schema.TaggedError<EnvironmentRequestInvalidError>()(
133 "EnvironmentRequestInvalidError",
134 {
135 code: Schema.Literal("invalid_request"),
136 reason: EnvironmentRequestInvalidReason,
137 traceId: TrimmedNonEmptyString,
138 },
139 { httpApiStatus: 400 },
140) {
141 [HttpServerRespondable.symbol]() {
142 return HttpServerResponse.schemaJson(EnvironmentRequestInvalidError)(this, { status: 400 });
143 }
144
145 override get message(): string {
146 return `The environment rejected the request (${this.reason}).`;
147 }
148}
149
150export class EnvironmentAuthInvalidError extends Schema.TaggedError<EnvironmentAuthInvalidError>()(
151 "EnvironmentAuthInvalidError",
152 {
153 code: Schema.Literal("auth_invalid"),
154 reason: EnvironmentAuthInvalidReason,
155 dpopFailureReason: Schema.optionalKey(DpopFailureReason),
156 traceId: TrimmedNonEmptyString,
157 },
158 { httpApiStatus: 401 },
159) {
160 [HttpServerRespondable.symbol]() {
161 return HttpServerResponse.schemaJson(EnvironmentAuthInvalidError)(this, { status: 401 });
162 }
163
164 override get message(): string {
165 return `The environment rejected this client's credentials (${this.reason}).`;
166 }
167}
168
169export class EnvironmentScopeRequiredError extends Schema.TaggedError<EnvironmentScopeRequiredError>()(
170 "EnvironmentScopeRequiredError",
171 {
172 code: Schema.Literal("insufficient_scope"),
173 requiredScope: AuthEnvironmentScope,
174 requiredPermission: Schema.optionalKey(Schema.String),
175 traceId: TrimmedNonEmptyString,
176 },
177 { httpApiStatus: 403 },
178) {
179 [HttpServerRespondable.symbol]() {
180 return HttpServerResponse.schemaJson(EnvironmentScopeRequiredError)(this, { status: 403 });
181 }
182
183 override get message(): string {
184 return `This request needs the ${this.requiredPermission ?? this.requiredScope} scope, which this client does not have.`;
185 }
186}
187
188export class EnvironmentOperationForbiddenError extends Schema.TaggedError<EnvironmentOperationForbiddenError>()(
189 "EnvironmentOperationForbiddenError",
190 {
191 code: Schema.Literal("operation_forbidden"),
192 reason: EnvironmentOperationForbiddenReason,
193 traceId: TrimmedNonEmptyString,
194 },
195 { httpApiStatus: 403 },
196) {
197 [HttpServerRespondable.symbol]() {
198 return HttpServerResponse.schemaJson(EnvironmentOperationForbiddenError)(this, { status: 403 });
199 }
200
201 override get message(): string {
202 return `The environment refused this operation (${this.reason}).`;
203 }
204}
205
206export class EnvironmentInternalError extends Schema.TaggedError<EnvironmentInternalError>()(
207 "EnvironmentInternalError",
208 {
209 code: Schema.Literal("internal_error"),
210 reason: EnvironmentInternalErrorReason,
211 traceId: TrimmedNonEmptyString,
212 },
213 { httpApiStatus: 500 },
214) {
215 [HttpServerRespondable.symbol]() {
216 return HttpServerResponse.schemaJson(EnvironmentInternalError)(this, { status: 500 });
217 }
218
219 override get message(): string {
220 return `The environment failed to answer this request (${this.reason}).`;
221 }
222}
223
224export const EnvironmentResourceNotFoundReason = Schema.Literals(["thread_not_found"]);
225export type EnvironmentResourceNotFoundReason = typeof EnvironmentResourceNotFoundReason.Type;
226
227export class EnvironmentResourceNotFoundError extends Schema.TaggedError<EnvironmentResourceNotFoundError>()(
228 "EnvironmentResourceNotFoundError",
229 {
230 code: Schema.Literal("not_found"),
231 reason: EnvironmentResourceNotFoundReason,
232 traceId: TrimmedNonEmptyString,
233 },
234 { httpApiStatus: 404 },
235) {
236 [HttpServerRespondable.symbol]() {
237 return HttpServerResponse.schemaJson(EnvironmentResourceNotFoundError)(this, { status: 404 });
238 }
239
240 override get message(): string {
241 return `The environment could not find what this request named (${this.reason}).`;
242 }
243}
244
245export const EnvironmentHttpCommonError = Schema.Union([
246 EnvironmentRequestInvalidError,
247 EnvironmentAuthInvalidError,
248 EnvironmentScopeRequiredError,
249 EnvironmentOperationForbiddenError,
250 EnvironmentResourceNotFoundError,
251 EnvironmentInternalError,
252]);
253export type EnvironmentHttpCommonError = typeof EnvironmentHttpCommonError.Type;
254
255const EnvironmentAuthenticationErrors = [
256 EnvironmentAuthInvalidError,
257 EnvironmentInternalError,
258] as const;
259
260export class EnvironmentHttpBadRequestError extends Schema.TaggedError<EnvironmentHttpBadRequestError>()(
261 "EnvironmentHttpBadRequestError",
262 {
263 message: Schema.String,
264 },
265 { httpApiStatus: 400 },
266) {
267 [HttpServerRespondable.symbol]() {
268 return HttpServerResponse.schemaJson(EnvironmentHttpBadRequestError)(this, { status: 400 });
269 }
270}
271
272export class EnvironmentHttpUnauthorizedError extends Schema.TaggedError<EnvironmentHttpUnauthorizedError>()(
273 "EnvironmentHttpUnauthorizedError",
274 {
275 message: Schema.String,
276 },
277 { httpApiStatus: 401 },
278) {
279 [HttpServerRespondable.symbol]() {
280 return HttpServerResponse.schemaJson(EnvironmentHttpUnauthorizedError)(this, { status: 401 });
281 }
282}
283
284export class EnvironmentHttpForbiddenError extends Schema.TaggedError<EnvironmentHttpForbiddenError>()(
285 "EnvironmentHttpForbiddenError",
286 {
287 message: Schema.String,
288 },
289 { httpApiStatus: 403 },
290) {
291 [HttpServerRespondable.symbol]() {
292 return HttpServerResponse.schemaJson(EnvironmentHttpForbiddenError)(this, { status: 403 });
293 }
294}
295
296export class EnvironmentHttpInternalServerError extends Schema.TaggedError<EnvironmentHttpInternalServerError>()(
297 "EnvironmentHttpInternalServerError",
298 {
299 message: Schema.String,
300 },
301 { httpApiStatus: 500 },
302) {
303 [HttpServerRespondable.symbol]() {
304 return HttpServerResponse.schemaJson(EnvironmentHttpInternalServerError)(this, { status: 500 });
305 }
306}
307
308export class EnvironmentHttpConflictError extends Schema.TaggedError<EnvironmentHttpConflictError>()(
309 "EnvironmentHttpConflictError",
310 {
311 message: Schema.String,
312 },
313 { httpApiStatus: 409 },
314) {
315 [HttpServerRespondable.symbol]() {
316 return HttpServerResponse.schemaJson(EnvironmentHttpConflictError)(this, { status: 409 });
317 }
318}
319
320export class EnvironmentCloudEndpointUnavailableError extends Schema.TaggedError<EnvironmentCloudEndpointUnavailableError>()(
321 "EnvironmentCloudEndpointUnavailableError",
322 {
323 message: Schema.String,
324 endpointRuntimeStatus: Schema.Unknown,
325 },
326 { httpApiStatus: 503 },
327) {
328 [HttpServerRespondable.symbol]() {
329 return HttpServerResponse.schemaJson(EnvironmentCloudEndpointUnavailableError)(this, {
330 status: 503,
331 });
332 }
333}
334const EnvironmentSessionCreationErrors = [
335 EnvironmentAuthInvalidError,
336 EnvironmentInternalError,
337] as const;
338const EnvironmentTokenExchangeErrors = [
339 EnvironmentRequestInvalidError,
340 EnvironmentAuthInvalidError,
341 EnvironmentInternalError,
342] as const;
343const EnvironmentScopedOperationErrors = [
344 EnvironmentScopeRequiredError,
345 EnvironmentInternalError,
346] as const;
347const EnvironmentPairingCredentialErrors = [
348 EnvironmentRequestInvalidError,
349 ...EnvironmentScopedOperationErrors,
350] as const;
351const EnvironmentSessionRevokeErrors = [
352 EnvironmentScopeRequiredError,
353 EnvironmentOperationForbiddenError,
354 EnvironmentInternalError,
355] as const;
356const EnvironmentProjectSnapshotErrors = [
357 EnvironmentScopeRequiredError,
358 EnvironmentInternalError,
359] as const;
360const EnvironmentOrchestrationSnapshotErrors = [
361 EnvironmentScopeRequiredError,
362 EnvironmentInternalError,
363] as const;
364const EnvironmentOrchestrationThreadSnapshotErrors = [
365 EnvironmentScopeRequiredError,
366 EnvironmentResourceNotFoundError,
367 EnvironmentInternalError,
368] as const;
369const EnvironmentProjectMutationErrors = [
370 EnvironmentRequestInvalidError,
371 EnvironmentScopeRequiredError,
372 EnvironmentInternalError,
373] as const;
374
375export interface EnvironmentSessionPrincipalShape {
376 readonly sessionId: AuthSessionId;
377 readonly subject: string;
378 readonly method: ServerAuthSessionMethod;
379 readonly scopes: ReadonlySet<AuthEnvironmentScope>;
380 readonly proofKeyThumbprint?: string;
381 readonly expiresAt?: DateTime.DateTime;
382}
383
384export class EnvironmentAuthenticatedPrincipal extends Context.Service<
385 EnvironmentAuthenticatedPrincipal,
386 EnvironmentSessionPrincipalShape
387>()("@t3tools/contracts/environmentHttp/EnvironmentAuthenticatedPrincipal") {}
388
389export class EnvironmentAuthenticatedAuth extends HttpApiMiddleware.Service<
390 EnvironmentAuthenticatedAuth,
391 { provides: EnvironmentAuthenticatedPrincipal }
392>()("EnvironmentAuthenticatedAuth", {
393 error: EnvironmentAuthenticationErrors,
394}) {}
395
396const EnvironmentHttpCloudErrors = [
397 EnvironmentHttpBadRequestError,
398 EnvironmentHttpUnauthorizedError,
399 EnvironmentHttpForbiddenError,
400 EnvironmentHttpConflictError,
401 EnvironmentHttpInternalServerError,
402 EnvironmentScopeRequiredError,
403] as const;
404
405export const EnvironmentCloudRelayConfigResult = Schema.Struct({
406 ok: Schema.Boolean,
407 endpointRuntimeStatus: Schema.Unknown,
408});
409export type EnvironmentCloudRelayConfigResult = typeof EnvironmentCloudRelayConfigResult.Type;
410
411export const EnvironmentCloudLinkStateResult = Schema.Struct({
412 linked: Schema.Boolean,
413 cloudUserId: Schema.NullOr(Schema.String),
414 relayUrl: Schema.NullOr(Schema.String),
415 relayIssuer: Schema.NullOr(Schema.String),
416 managedTunnelActive: Schema.optional(Schema.Boolean),
417 publishAgentActivity: Schema.Boolean,
418 holdWebhooksWhileOffline: Schema.optional(Schema.Boolean),
419 linkRevoked: Schema.optional(Schema.Boolean),
420});
421export type EnvironmentCloudLinkStateResult = typeof EnvironmentCloudLinkStateResult.Type;
422
423export const EnvironmentCloudPreferencesRequest = Schema.Struct({
424 publishAgentActivity: Schema.Boolean,
425 holdWebhooksWhileOffline: Schema.optional(Schema.Boolean),
426});
427export type EnvironmentCloudPreferencesRequest = typeof EnvironmentCloudPreferencesRequest.Type;
428
429export const AuthPairingLinkRevokeResult = Schema.Struct({
430 revoked: Schema.Boolean,
431});
432export type AuthPairingLinkRevokeResult = typeof AuthPairingLinkRevokeResult.Type;
433
434export const AuthClientSessionRevokeResult = Schema.Struct({
435 revoked: Schema.Boolean,
436});
437export type AuthClientSessionRevokeResult = typeof AuthClientSessionRevokeResult.Type;
438
439export const AuthOtherClientSessionsRevokeResult = Schema.Struct({
440 revokedCount: Schema.Number,
441});
442export type AuthOtherClientSessionsRevokeResult = typeof AuthOtherClientSessionsRevokeResult.Type;
443
444class EnvironmentMetadataHttpApi extends HttpApiGroup.make("metadata").add(
445 HttpApiEndpoint.get("descriptor", "/.well-known/t3/environment", {
446 success: ExecutionEnvironmentDescriptor,
447 }),
448) {}
449
450class EnvironmentAuthHttpApi extends HttpApiGroup.make("auth")
451 .add(
452 HttpApiEndpoint.get("session", "/api/auth/session", {
453 headers: OptionalBearerHeaders,
454 success: AuthSessionState,
455 error: [EnvironmentInternalError],
456 }),
457 )
458 .add(
459 HttpApiEndpoint.post("browserSession", "/api/auth/browser-session", {
460 payload: AuthBrowserSessionRequest,
461 success: AuthBrowserSessionResult,
462 error: EnvironmentSessionCreationErrors,
463 }),
464 )
465 .add(
466 HttpApiEndpoint.post("token", "/oauth/token", {
467 headers: OptionalDpopProofHeaders,
468 payload: AuthTokenExchangeRequest,
469 success: AuthAccessTokenResult,
470 error: EnvironmentTokenExchangeErrors,
471 }),
472 )
473 .add(
474 HttpApiEndpoint.post("webSocketTicket", "/api/auth/websocket-ticket", {
475 headers: OptionalBearerHeaders,
476 success: AuthWebSocketTicketResult,
477 error: [EnvironmentInternalError],
478 }).middleware(EnvironmentAuthenticatedAuth),
479 )
480 .add(
481 HttpApiEndpoint.post("pairingCredential", "/api/auth/pairing-token", {
482 headers: OptionalBearerHeaders,
483 payload: AuthCreatePairingCredentialInput,
484 success: AuthPairingCredentialResult,
485 error: EnvironmentPairingCredentialErrors,
486 }).middleware(EnvironmentAuthenticatedAuth),
487 )
488 .add(
489 HttpApiEndpoint.get("pairingLinks", "/api/auth/pairing-links", {
490 headers: OptionalBearerHeaders,
491 success: Schema.Array(AuthPairingLink),
492 error: EnvironmentScopedOperationErrors,
493 }).middleware(EnvironmentAuthenticatedAuth),
494 )
495 .add(
496 HttpApiEndpoint.post("revokePairingLink", "/api/auth/pairing-links/revoke", {
497 headers: OptionalBearerHeaders,
498 payload: AuthRevokePairingLinkInput,
499 success: AuthPairingLinkRevokeResult,
500 error: EnvironmentScopedOperationErrors,
501 }).middleware(EnvironmentAuthenticatedAuth),
502 )
503 .add(
504 HttpApiEndpoint.get("clients", "/api/auth/clients", {
505 headers: OptionalBearerHeaders,
506 success: Schema.Array(AuthClientSession),
507 error: EnvironmentScopedOperationErrors,
508 }).middleware(EnvironmentAuthenticatedAuth),
509 )
510 .add(
511 HttpApiEndpoint.post("revokeClient", "/api/auth/clients/revoke", {
512 headers: OptionalBearerHeaders,
513 payload: AuthRevokeClientSessionInput,
514 success: AuthClientSessionRevokeResult,
515 error: EnvironmentSessionRevokeErrors,
516 }).middleware(EnvironmentAuthenticatedAuth),
517 )
518 .add(
519 HttpApiEndpoint.post("revokeOtherClients", "/api/auth/clients/revoke-others", {
520 headers: OptionalBearerHeaders,
521 success: AuthOtherClientSessionsRevokeResult,
522 error: EnvironmentScopedOperationErrors,
523 }).middleware(EnvironmentAuthenticatedAuth),
524 ) {}
525
526class EnvironmentMcpOAuthHttpApi extends HttpApiGroup.make("mcpOAuth")
527 .add(
528 HttpApiEndpoint.get("protectedResource", "/.well-known/oauth-protected-resource", {
529 success: AuthMcpProtectedResourceMetadata,
530 }),
531 )
532 .add(
533 HttpApiEndpoint.get("mcpProtectedResource", "/.well-known/oauth-protected-resource/mcp", {
534 success: AuthMcpProtectedResourceMetadata,
535 }),
536 )
537 .add(
538 HttpApiEndpoint.get("authorizationServer", "/.well-known/oauth-authorization-server", {
539 success: AuthMcpAuthorizationServerMetadata,
540 }),
541 )
542 .add(
543 HttpApiEndpoint.post("register", "/oauth/mcp/register", {
544 payload: AuthMcpClientRegistration,
545 success: AuthMcpRegisteredClient,
546 error: AuthMcpRegistrationError,
547 }),
548 )
549 .add(HttpApiEndpoint.get("authorize", "/oauth/mcp/authorize"))
550 .add(
551 HttpApiEndpoint.post("approval", "/oauth/mcp/approval", {
552 payload: AuthMcpAuthorizationRequest,
553 success: [AuthMcpApprovalDetails, AuthMcpApprovalRedirect],
554 error: AuthMcpApprovalError,
555 }),
556 )
557 .add(
558 HttpApiEndpoint.post("decision", "/oauth/mcp/decision", {
559 payload: AuthMcpApprovalDecisionRequest,
560 success: AuthMcpApprovalRedirect,
561 error: AuthMcpApprovalError,
562 }),
563 )
564 .add(
565 HttpApiEndpoint.post("token", "/oauth/mcp/token", {
566 payload: AuthMcpTokenRequest,
567 success: AuthMcpTokenResult,
568 error: AuthMcpTokenError,
569 }),
570 ) {}
571
572const EnvironmentOrchestrationThreadSnapshotParams = Schema.Struct({
573 threadId: ThreadId,
574});
575
576const EnvironmentOrchestrationThreadBoundedSnapshotQuery = Schema.Struct({
577 compactTurnItems: Schema.optionalKey(Schema.String),
578});
579
580const EnvironmentOrchestrationThreadHistoryQuery = Schema.Struct({
581 cursor: TrimmedNonEmptyString,
582 throughEntryId: Schema.optional(TrimmedNonEmptyString),
583 view: Schema.optional(Schema.Literals(["conversation", "activity"])),
584});
585
586const EnvironmentOrchestrationThreadHistoryErrors = [
587 EnvironmentRequestInvalidError,
588 EnvironmentScopeRequiredError,
589 EnvironmentResourceNotFoundError,
590 EnvironmentInternalError,
591] as const;
592
593class EnvironmentOrchestrationHttpApi extends HttpApiGroup.make("orchestration")
594 .add(
595 HttpApiEndpoint.get("shellSnapshot", "/api/orchestration/shell", {
596 headers: OrchestrationProtocolHeaders,
597 success: OrchestrationV2ShellSnapshot,
598 error: EnvironmentOrchestrationSnapshotErrors,
599 }).middleware(EnvironmentAuthenticatedAuth),
600 )
601 .add(
602 HttpApiEndpoint.get("threadSnapshot", "/api/orchestration/threads/:threadId", {
603 headers: OrchestrationProtocolHeaders,
604 params: EnvironmentOrchestrationThreadSnapshotParams,
605 success: OrchestrationV2ThreadDetailSnapshot,
606 error: EnvironmentOrchestrationThreadSnapshotErrors,
607 }).middleware(EnvironmentAuthenticatedAuth),
608 )
609 .add(
610 HttpApiEndpoint.get("threadTranscript", "/api/orchestration/threads/:threadId/transcript", {
611 headers: OrchestrationProtocolHeaders,
612 params: EnvironmentOrchestrationThreadSnapshotParams,
613 success: OrchestrationV2ThreadTranscript,
614 error: [EnvironmentRequestInvalidError, ...EnvironmentOrchestrationThreadSnapshotErrors],
615 }).middleware(EnvironmentAuthenticatedAuth),
616 )
617 .add(
618 HttpApiEndpoint.get("threadBoundedSnapshot", "/api/orchestration/threads/:threadId/bounded", {
619 headers: OrchestrationProtocolHeaders,
620 params: EnvironmentOrchestrationThreadSnapshotParams,
621 query: EnvironmentOrchestrationThreadBoundedSnapshotQuery,
622 success: OrchestrationV2ThreadBoundedSnapshot,
623 error: EnvironmentOrchestrationThreadSnapshotErrors,
624 }).middleware(EnvironmentAuthenticatedAuth),
625 )
626 .add(
627 HttpApiEndpoint.get("threadHistoryPage", "/api/orchestration/threads/:threadId/history", {
628 headers: OrchestrationProtocolHeaders,
629 params: EnvironmentOrchestrationThreadSnapshotParams,
630 query: EnvironmentOrchestrationThreadHistoryQuery,
631 success: OrchestrationV2ThreadHistoryPage,
632 error: EnvironmentOrchestrationThreadHistoryErrors,
633 }).middleware(EnvironmentAuthenticatedAuth),
634 ) {}
635
636class EnvironmentProjectsHttpApi extends HttpApiGroup.make("projects")
637 .add(
638 HttpApiEndpoint.get("snapshot", "/api/projects", {
639 headers: OptionalBearerHeaders,
640 success: ProjectSnapshot,
641 error: EnvironmentProjectSnapshotErrors,
642 }).middleware(EnvironmentAuthenticatedAuth),
643 )
644 .add(
645 HttpApiEndpoint.post("mutate", "/api/projects/mutate", {
646 headers: OptionalBearerHeaders,
647 payload: ProjectMutation,
648 success: Project,
649 error: EnvironmentProjectMutationErrors,
650 }).middleware(EnvironmentAuthenticatedAuth),
651 ) {}
652
653class EnvironmentPullRequestsHttpApi extends HttpApiGroup.make("pullRequests").add(
654 HttpApiEndpoint.post("diff", "/api/pull-requests/diff", {
655 headers: OptionalBearerHeaders,
656 payload: PullRequestDiffInput,
657 success: PullRequestDiffResult,
658 error: [
659 PullRequestUnavailableError,
660 PullRequestOperationError,
661 EnvironmentAuthInvalidError,
662 EnvironmentScopeRequiredError,
663 EnvironmentInternalError,
664 ],
665 }).middleware(EnvironmentAuthenticatedAuth),
666) {}
667
668class EnvironmentConnectHttpApi extends HttpApiGroup.make("connect")
669 .add(
670 HttpApiEndpoint.post("linkProof", "/api/connect/link-proof", {
671 headers: OptionalBearerHeaders,
672 payload: RelayLinkProofRequest,
673 success: RelayEnvironmentLinkProof,
674 error: EnvironmentHttpCloudErrors,
675 }).middleware(EnvironmentAuthenticatedAuth),
676 )
677 .add(
678 HttpApiEndpoint.post("relayConfig", "/api/connect/relay-config", {
679 headers: OptionalBearerHeaders,
680 payload: RelayEnvironmentConfigRequest,
681 success: EnvironmentCloudRelayConfigResult,
682 error: [...EnvironmentHttpCloudErrors, EnvironmentCloudEndpointUnavailableError],
683 }).middleware(EnvironmentAuthenticatedAuth),
684 )
685 .add(
686 HttpApiEndpoint.get("linkState", "/api/connect/link-state", {
687 headers: OptionalBearerHeaders,
688 success: EnvironmentCloudLinkStateResult,
689 error: EnvironmentHttpCloudErrors,
690 }).middleware(EnvironmentAuthenticatedAuth),
691 )
692 .add(
693 HttpApiEndpoint.post("unlink", "/api/connect/unlink", {
694 headers: OptionalBearerHeaders,
695 success: EnvironmentCloudRelayConfigResult,
696 error: EnvironmentHttpCloudErrors,
697 }).middleware(EnvironmentAuthenticatedAuth),
698 )
699 .add(
700 HttpApiEndpoint.post("preferences", "/api/connect/preferences", {
701 headers: OptionalBearerHeaders,
702 payload: EnvironmentCloudPreferencesRequest,
703 success: EnvironmentCloudLinkStateResult,
704 error: EnvironmentHttpCloudErrors,
705 }).middleware(EnvironmentAuthenticatedAuth),
706 )
707 .add(
708 HttpApiEndpoint.post("health", "/api/t3-connect/health", {
709 payload: RelayCloudEnvironmentHealthRequest,
710 success: RelayEnvironmentHealthResponse,
711 error: EnvironmentHttpCloudErrors,
712 }),
713 )
714 .add(
715 HttpApiEndpoint.post("mintCredential", "/api/connect/mint-credential", {
716 payload: RelayCloudMintCredentialRequest,
717 success: RelayEnvironmentMintResponse,
718 error: EnvironmentHttpCloudErrors,
719 }),
720 )
721 .add(
722 HttpApiEndpoint.post("t3MintCredential", "/api/t3-connect/mint-credential", {
723 payload: RelayCloudMintCredentialRequest,
724 success: RelayEnvironmentMintResponse,
725 error: EnvironmentHttpCloudErrors,
726 }),
727 ) {}
728
729/**
730 * Public entry point for webhook tasks. Unauthenticated by design: the token
731 * in the path, and an optional body signature, are the credential. The handler
732 * reads the raw body itself so a signature is checked over the exact bytes.
733 */
734const WebhookParams = Schema.Struct({
735 hookId: TrimmedNonEmptyString,
736 token: TrimmedNonEmptyString,
737});
738const WebhookAccepted = Schema.Struct({ deliveryId: TrimmedNonEmptyString }).pipe(
739 HttpApiSchema.status(202),
740);
741const webhookEndpoint = {
742 params: WebhookParams,
743 success: WebhookAccepted,
744} as const;
745const WEBHOOK_PATH = "/api/hooks/:hookId/:token";
746
747class EnvironmentWebhooksHttpApi extends HttpApiGroup.make("webhooks")
748 .add(HttpApiEndpoint.post("webhookPost", WEBHOOK_PATH, webhookEndpoint))
749 .add(HttpApiEndpoint.put("webhookPut", WEBHOOK_PATH, webhookEndpoint))
750 .add(HttpApiEndpoint.patch("webhookPatch", WEBHOOK_PATH, webhookEndpoint))
751 .add(HttpApiEndpoint.get("webhookGet", WEBHOOK_PATH, webhookEndpoint)) {}
752
753export class EnvironmentHttpApi extends HttpApi.make("environment")
754 .add(EnvironmentMetadataHttpApi)
755 .add(EnvironmentAuthHttpApi)
756 .add(EnvironmentMcpOAuthHttpApi)
757 .add(EnvironmentOrchestrationHttpApi)
758 .add(EnvironmentPullRequestsHttpApi)
759 .add(EnvironmentProjectsHttpApi)
760 .add(EnvironmentConnectHttpApi)
761 .add(EnvironmentWebhooksHttpApi) {}