774 lines
26 KiB
TypeScript
774 lines
26 KiB
TypeScript
import * as Context from "effect/Context";
|
|
import type * as DateTime from "effect/DateTime";
|
|
import * as Schema from "effect/Schema";
|
|
import * as HttpApi from "effect/http-api/HttpApi";
|
|
import * as HttpApiEndpoint from "effect/http-api/HttpApiEndpoint";
|
|
import * as HttpApiGroup from "effect/http-api/HttpApiGroup";
|
|
import * as HttpApiMiddleware from "effect/http-api/HttpApiMiddleware";
|
|
import * as HttpApiSchema from "effect/http-api/HttpApiSchema";
|
|
import * as HttpServerRespondable from "effect/http/HttpServerRespondable";
|
|
import * as HttpServerResponse from "effect/http/HttpServerResponse";
|
|
|
|
import {
|
|
AuthAccessTokenResult,
|
|
AuthBrowserSessionRequest,
|
|
AuthBrowserSessionResult,
|
|
AuthClientSession,
|
|
AuthCreatePairingCredentialInput,
|
|
AuthMcpApprovalDecisionRequest,
|
|
AuthMcpApprovalDetails,
|
|
AuthMcpApprovalError,
|
|
AuthMcpApprovalRedirect,
|
|
AuthMcpAuthorizationRequest,
|
|
AuthMcpAuthorizationServerMetadata,
|
|
AuthMcpClientRegistration,
|
|
AuthMcpProtectedResourceMetadata,
|
|
AuthMcpRegisteredClient,
|
|
AuthMcpRegistrationError,
|
|
AuthMcpTokenError,
|
|
AuthMcpTokenRequest,
|
|
AuthMcpTokenResult,
|
|
AuthPairingCredentialResult,
|
|
AuthPairingLink,
|
|
AuthRevokeClientSessionInput,
|
|
AuthRevokePairingLinkInput,
|
|
AuthEnvironmentScope,
|
|
AuthTokenExchangeRequest,
|
|
AuthSessionState,
|
|
AuthWebSocketTicketResult,
|
|
ServerAuthSessionMethod,
|
|
} from "./auth.ts";
|
|
import {
|
|
ExecutionEnvironmentDescriptor,
|
|
ORCHESTRATION_PROTOCOL_HEADER,
|
|
ORCHESTRATION_PROTOCOL_VERSION_TEXT,
|
|
} from "./environment.ts";
|
|
import {
|
|
DpopFailureReason,
|
|
AuthSessionId,
|
|
ThreadId,
|
|
TrimmedNonEmptyString,
|
|
} from "./baseSchemas.ts";
|
|
import {
|
|
OrchestrationV2ShellSnapshot,
|
|
OrchestrationV2ThreadBoundedSnapshot,
|
|
OrchestrationV2ThreadDetailSnapshot,
|
|
OrchestrationV2ThreadHistoryPage,
|
|
} from "./orchestrationV2.ts";
|
|
import { Project, ProjectMutation, ProjectSnapshot } from "./project.ts";
|
|
import {
|
|
PullRequestDiffInput,
|
|
PullRequestDiffResult,
|
|
PullRequestOperationError,
|
|
PullRequestUnavailableError,
|
|
} from "./pullRequest.ts";
|
|
import {
|
|
RelayCloudEnvironmentHealthRequest,
|
|
RelayCloudMintCredentialRequest,
|
|
RelayEnvironmentConfigRequest,
|
|
RelayEnvironmentHealthResponse,
|
|
RelayEnvironmentLinkProof,
|
|
RelayEnvironmentMintResponse,
|
|
RelayLinkProofRequest,
|
|
} from "./relay.ts";
|
|
|
|
const OptionalBearerHeaders = Schema.Struct({
|
|
authorization: Schema.optionalKey(Schema.String),
|
|
dpop: Schema.optionalKey(Schema.String),
|
|
});
|
|
|
|
const OrchestrationProtocolHeaders = Schema.Struct({
|
|
authorization: Schema.optionalKey(Schema.String),
|
|
dpop: Schema.optionalKey(Schema.String),
|
|
[ORCHESTRATION_PROTOCOL_HEADER]: Schema.Literal(ORCHESTRATION_PROTOCOL_VERSION_TEXT),
|
|
});
|
|
|
|
const OptionalDpopProofHeaders = Schema.Struct({
|
|
dpop: Schema.optionalKey(Schema.String),
|
|
});
|
|
|
|
export const EnvironmentRequestInvalidReason = Schema.Literals([
|
|
"invalid_scope",
|
|
"scope_not_granted",
|
|
"invalid_command",
|
|
"invalid_history_cursor",
|
|
]);
|
|
export type EnvironmentRequestInvalidReason = typeof EnvironmentRequestInvalidReason.Type;
|
|
|
|
export const EnvironmentAuthInvalidReason = Schema.Literals([
|
|
"missing_credential",
|
|
"invalid_credential",
|
|
]);
|
|
export type EnvironmentAuthInvalidReason = typeof EnvironmentAuthInvalidReason.Type;
|
|
|
|
export const EnvironmentOperationForbiddenReason = Schema.Literals([
|
|
"current_session_revoke_not_allowed",
|
|
]);
|
|
export type EnvironmentOperationForbiddenReason = typeof EnvironmentOperationForbiddenReason.Type;
|
|
|
|
export const EnvironmentInternalErrorReason = Schema.Literals([
|
|
"bootstrap_validation_failed",
|
|
"browser_session_issuance_failed",
|
|
"browser_session_cookie_failed",
|
|
"access_token_issuance_failed",
|
|
"websocket_ticket_issuance_failed",
|
|
"pairing_credential_issuance_failed",
|
|
"pairing_links_load_failed",
|
|
"pairing_link_revoke_failed",
|
|
"client_sessions_load_failed",
|
|
"client_session_revoke_failed",
|
|
"project_snapshot_failed",
|
|
"project_mutation_failed",
|
|
"orchestration_snapshot_failed",
|
|
"orchestration_thread_snapshot_failed",
|
|
"orchestration_thread_bounded_snapshot_failed",
|
|
"orchestration_thread_history_failed",
|
|
"internal_error",
|
|
]);
|
|
export type EnvironmentInternalErrorReason = typeof EnvironmentInternalErrorReason.Type;
|
|
|
|
export class EnvironmentRequestInvalidError extends Schema.TaggedError<EnvironmentRequestInvalidError>()(
|
|
"EnvironmentRequestInvalidError",
|
|
{
|
|
code: Schema.Literal("invalid_request"),
|
|
reason: EnvironmentRequestInvalidReason,
|
|
traceId: TrimmedNonEmptyString,
|
|
},
|
|
{ httpApiStatus: 400 },
|
|
) {
|
|
[HttpServerRespondable.symbol]() {
|
|
return HttpServerResponse.schemaJson(EnvironmentRequestInvalidError)(this, { status: 400 });
|
|
}
|
|
|
|
override get message(): string {
|
|
return `The environment rejected the request (${this.reason}).`;
|
|
}
|
|
}
|
|
|
|
export class EnvironmentAuthInvalidError extends Schema.TaggedError<EnvironmentAuthInvalidError>()(
|
|
"EnvironmentAuthInvalidError",
|
|
{
|
|
code: Schema.Literal("auth_invalid"),
|
|
reason: EnvironmentAuthInvalidReason,
|
|
// Older servers do not send a DPoP failure category.
|
|
dpopFailureReason: Schema.optionalKey(DpopFailureReason),
|
|
traceId: TrimmedNonEmptyString,
|
|
},
|
|
{ httpApiStatus: 401 },
|
|
) {
|
|
[HttpServerRespondable.symbol]() {
|
|
return HttpServerResponse.schemaJson(EnvironmentAuthInvalidError)(this, { status: 401 });
|
|
}
|
|
|
|
override get message(): string {
|
|
return `The environment rejected this client's credentials (${this.reason}).`;
|
|
}
|
|
}
|
|
|
|
export class EnvironmentScopeRequiredError extends Schema.TaggedError<EnvironmentScopeRequiredError>()(
|
|
"EnvironmentScopeRequiredError",
|
|
{
|
|
code: Schema.Literal("insufficient_scope"),
|
|
requiredScope: AuthEnvironmentScope,
|
|
requiredPermission: Schema.optionalKey(Schema.String),
|
|
traceId: TrimmedNonEmptyString,
|
|
},
|
|
{ httpApiStatus: 403 },
|
|
) {
|
|
[HttpServerRespondable.symbol]() {
|
|
return HttpServerResponse.schemaJson(EnvironmentScopeRequiredError)(this, { status: 403 });
|
|
}
|
|
|
|
override get message(): string {
|
|
return `This request needs the ${this.requiredPermission ?? this.requiredScope} scope, which this client does not have.`;
|
|
}
|
|
}
|
|
|
|
export class EnvironmentOperationForbiddenError extends Schema.TaggedError<EnvironmentOperationForbiddenError>()(
|
|
"EnvironmentOperationForbiddenError",
|
|
{
|
|
code: Schema.Literal("operation_forbidden"),
|
|
reason: EnvironmentOperationForbiddenReason,
|
|
traceId: TrimmedNonEmptyString,
|
|
},
|
|
{ httpApiStatus: 403 },
|
|
) {
|
|
[HttpServerRespondable.symbol]() {
|
|
return HttpServerResponse.schemaJson(EnvironmentOperationForbiddenError)(this, { status: 403 });
|
|
}
|
|
|
|
override get message(): string {
|
|
return `The environment refused this operation (${this.reason}).`;
|
|
}
|
|
}
|
|
|
|
export class EnvironmentInternalError extends Schema.TaggedError<EnvironmentInternalError>()(
|
|
"EnvironmentInternalError",
|
|
{
|
|
code: Schema.Literal("internal_error"),
|
|
reason: EnvironmentInternalErrorReason,
|
|
traceId: TrimmedNonEmptyString,
|
|
},
|
|
{ httpApiStatus: 500 },
|
|
) {
|
|
[HttpServerRespondable.symbol]() {
|
|
return HttpServerResponse.schemaJson(EnvironmentInternalError)(this, { status: 500 });
|
|
}
|
|
|
|
override get message(): string {
|
|
return `The environment failed to answer this request (${this.reason}).`;
|
|
}
|
|
}
|
|
|
|
export const EnvironmentResourceNotFoundReason = Schema.Literals(["thread_not_found"]);
|
|
export type EnvironmentResourceNotFoundReason = typeof EnvironmentResourceNotFoundReason.Type;
|
|
|
|
export class EnvironmentResourceNotFoundError extends Schema.TaggedError<EnvironmentResourceNotFoundError>()(
|
|
"EnvironmentResourceNotFoundError",
|
|
{
|
|
code: Schema.Literal("not_found"),
|
|
reason: EnvironmentResourceNotFoundReason,
|
|
traceId: TrimmedNonEmptyString,
|
|
},
|
|
{ httpApiStatus: 404 },
|
|
) {
|
|
[HttpServerRespondable.symbol]() {
|
|
return HttpServerResponse.schemaJson(EnvironmentResourceNotFoundError)(this, { status: 404 });
|
|
}
|
|
|
|
override get message(): string {
|
|
return `The environment could not find what this request named (${this.reason}).`;
|
|
}
|
|
}
|
|
|
|
export const EnvironmentHttpCommonError = Schema.Union([
|
|
EnvironmentRequestInvalidError,
|
|
EnvironmentAuthInvalidError,
|
|
EnvironmentScopeRequiredError,
|
|
EnvironmentOperationForbiddenError,
|
|
EnvironmentResourceNotFoundError,
|
|
EnvironmentInternalError,
|
|
]);
|
|
export type EnvironmentHttpCommonError = typeof EnvironmentHttpCommonError.Type;
|
|
|
|
const EnvironmentAuthenticationErrors = [
|
|
EnvironmentAuthInvalidError,
|
|
EnvironmentInternalError,
|
|
] as const;
|
|
|
|
export class EnvironmentHttpBadRequestError extends Schema.TaggedError<EnvironmentHttpBadRequestError>()(
|
|
"EnvironmentHttpBadRequestError",
|
|
{
|
|
message: Schema.String,
|
|
},
|
|
{ httpApiStatus: 400 },
|
|
) {
|
|
[HttpServerRespondable.symbol]() {
|
|
return HttpServerResponse.schemaJson(EnvironmentHttpBadRequestError)(this, { status: 400 });
|
|
}
|
|
}
|
|
|
|
export class EnvironmentHttpUnauthorizedError extends Schema.TaggedError<EnvironmentHttpUnauthorizedError>()(
|
|
"EnvironmentHttpUnauthorizedError",
|
|
{
|
|
message: Schema.String,
|
|
},
|
|
{ httpApiStatus: 401 },
|
|
) {
|
|
[HttpServerRespondable.symbol]() {
|
|
return HttpServerResponse.schemaJson(EnvironmentHttpUnauthorizedError)(this, { status: 401 });
|
|
}
|
|
}
|
|
|
|
export class EnvironmentHttpForbiddenError extends Schema.TaggedError<EnvironmentHttpForbiddenError>()(
|
|
"EnvironmentHttpForbiddenError",
|
|
{
|
|
message: Schema.String,
|
|
},
|
|
{ httpApiStatus: 403 },
|
|
) {
|
|
[HttpServerRespondable.symbol]() {
|
|
return HttpServerResponse.schemaJson(EnvironmentHttpForbiddenError)(this, { status: 403 });
|
|
}
|
|
}
|
|
|
|
export class EnvironmentHttpInternalServerError extends Schema.TaggedError<EnvironmentHttpInternalServerError>()(
|
|
"EnvironmentHttpInternalServerError",
|
|
{
|
|
message: Schema.String,
|
|
},
|
|
{ httpApiStatus: 500 },
|
|
) {
|
|
[HttpServerRespondable.symbol]() {
|
|
return HttpServerResponse.schemaJson(EnvironmentHttpInternalServerError)(this, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export class EnvironmentHttpConflictError extends Schema.TaggedError<EnvironmentHttpConflictError>()(
|
|
"EnvironmentHttpConflictError",
|
|
{
|
|
message: Schema.String,
|
|
},
|
|
{ httpApiStatus: 409 },
|
|
) {
|
|
[HttpServerRespondable.symbol]() {
|
|
return HttpServerResponse.schemaJson(EnvironmentHttpConflictError)(this, { status: 409 });
|
|
}
|
|
}
|
|
|
|
export class EnvironmentCloudEndpointUnavailableError extends Schema.TaggedError<EnvironmentCloudEndpointUnavailableError>()(
|
|
"EnvironmentCloudEndpointUnavailableError",
|
|
{
|
|
message: Schema.String,
|
|
endpointRuntimeStatus: Schema.Unknown,
|
|
},
|
|
{ httpApiStatus: 503 },
|
|
) {
|
|
[HttpServerRespondable.symbol]() {
|
|
return HttpServerResponse.schemaJson(EnvironmentCloudEndpointUnavailableError)(this, {
|
|
status: 503,
|
|
});
|
|
}
|
|
}
|
|
const EnvironmentSessionCreationErrors = [
|
|
EnvironmentAuthInvalidError,
|
|
EnvironmentInternalError,
|
|
] as const;
|
|
const EnvironmentTokenExchangeErrors = [
|
|
EnvironmentRequestInvalidError,
|
|
EnvironmentAuthInvalidError,
|
|
EnvironmentInternalError,
|
|
] as const;
|
|
const EnvironmentScopedOperationErrors = [
|
|
EnvironmentScopeRequiredError,
|
|
EnvironmentInternalError,
|
|
] as const;
|
|
const EnvironmentPairingCredentialErrors = [
|
|
EnvironmentRequestInvalidError,
|
|
...EnvironmentScopedOperationErrors,
|
|
] as const;
|
|
const EnvironmentSessionRevokeErrors = [
|
|
EnvironmentScopeRequiredError,
|
|
EnvironmentOperationForbiddenError,
|
|
EnvironmentInternalError,
|
|
] as const;
|
|
const EnvironmentProjectSnapshotErrors = [
|
|
EnvironmentScopeRequiredError,
|
|
EnvironmentInternalError,
|
|
] as const;
|
|
const EnvironmentOrchestrationSnapshotErrors = [
|
|
EnvironmentScopeRequiredError,
|
|
EnvironmentInternalError,
|
|
] as const;
|
|
const EnvironmentOrchestrationThreadSnapshotErrors = [
|
|
EnvironmentScopeRequiredError,
|
|
EnvironmentResourceNotFoundError,
|
|
EnvironmentInternalError,
|
|
] as const;
|
|
const EnvironmentProjectMutationErrors = [
|
|
EnvironmentRequestInvalidError,
|
|
EnvironmentScopeRequiredError,
|
|
EnvironmentInternalError,
|
|
] as const;
|
|
|
|
export interface EnvironmentSessionPrincipalShape {
|
|
readonly sessionId: AuthSessionId;
|
|
readonly subject: string;
|
|
readonly method: ServerAuthSessionMethod;
|
|
readonly scopes: ReadonlySet<AuthEnvironmentScope>;
|
|
readonly proofKeyThumbprint?: string;
|
|
readonly expiresAt?: DateTime.DateTime;
|
|
}
|
|
|
|
export class EnvironmentAuthenticatedPrincipal extends Context.Service<
|
|
EnvironmentAuthenticatedPrincipal,
|
|
EnvironmentSessionPrincipalShape
|
|
>()("@t3tools/contracts/environmentHttp/EnvironmentAuthenticatedPrincipal") {}
|
|
|
|
export class EnvironmentAuthenticatedAuth extends HttpApiMiddleware.Service<
|
|
EnvironmentAuthenticatedAuth,
|
|
{ provides: EnvironmentAuthenticatedPrincipal }
|
|
>()("EnvironmentAuthenticatedAuth", {
|
|
error: EnvironmentAuthenticationErrors,
|
|
}) {}
|
|
|
|
const EnvironmentHttpCloudErrors = [
|
|
EnvironmentHttpBadRequestError,
|
|
EnvironmentHttpUnauthorizedError,
|
|
EnvironmentHttpForbiddenError,
|
|
EnvironmentHttpConflictError,
|
|
EnvironmentHttpInternalServerError,
|
|
EnvironmentScopeRequiredError,
|
|
] as const;
|
|
|
|
export const EnvironmentCloudRelayConfigResult = Schema.Struct({
|
|
ok: Schema.Boolean,
|
|
endpointRuntimeStatus: Schema.Unknown,
|
|
});
|
|
export type EnvironmentCloudRelayConfigResult = typeof EnvironmentCloudRelayConfigResult.Type;
|
|
|
|
export const EnvironmentCloudLinkStateResult = Schema.Struct({
|
|
linked: Schema.Boolean,
|
|
cloudUserId: Schema.NullOr(Schema.String),
|
|
relayUrl: Schema.NullOr(Schema.String),
|
|
relayIssuer: Schema.NullOr(Schema.String),
|
|
// A managed Cloudflare tunnel is provisioned for this link. False for a
|
|
// publish-only link (activity publishing without a relay-managed tunnel), so
|
|
// clients can present the two capabilities as independent settings.
|
|
// Optional so newer clients tolerate older environment servers.
|
|
managedTunnelActive: Schema.optional(Schema.Boolean),
|
|
publishAgentActivity: Schema.Boolean,
|
|
// Opt-in: T3 Connect holds webhook requests while this environment is
|
|
// offline. Optional so newer clients tolerate older environment servers.
|
|
holdWebhooksWhileOffline: Schema.optional(Schema.Boolean),
|
|
// The relay rejected this link's credential, usually because the environment
|
|
// was deregistered from T3 Connect. The link stays stored until it is linked
|
|
// again. Optional so newer clients tolerate older environment servers.
|
|
linkRevoked: Schema.optional(Schema.Boolean),
|
|
});
|
|
export type EnvironmentCloudLinkStateResult = typeof EnvironmentCloudLinkStateResult.Type;
|
|
|
|
export const EnvironmentCloudPreferencesRequest = Schema.Struct({
|
|
publishAgentActivity: Schema.Boolean,
|
|
// Omit to leave the current value unchanged.
|
|
holdWebhooksWhileOffline: Schema.optional(Schema.Boolean),
|
|
});
|
|
export type EnvironmentCloudPreferencesRequest = typeof EnvironmentCloudPreferencesRequest.Type;
|
|
|
|
export const AuthPairingLinkRevokeResult = Schema.Struct({
|
|
revoked: Schema.Boolean,
|
|
});
|
|
export type AuthPairingLinkRevokeResult = typeof AuthPairingLinkRevokeResult.Type;
|
|
|
|
export const AuthClientSessionRevokeResult = Schema.Struct({
|
|
revoked: Schema.Boolean,
|
|
});
|
|
export type AuthClientSessionRevokeResult = typeof AuthClientSessionRevokeResult.Type;
|
|
|
|
export const AuthOtherClientSessionsRevokeResult = Schema.Struct({
|
|
revokedCount: Schema.Number,
|
|
});
|
|
export type AuthOtherClientSessionsRevokeResult = typeof AuthOtherClientSessionsRevokeResult.Type;
|
|
|
|
class EnvironmentMetadataHttpApi extends HttpApiGroup.make("metadata").add(
|
|
HttpApiEndpoint.get("descriptor", "/.well-known/t3/environment", {
|
|
success: ExecutionEnvironmentDescriptor,
|
|
}),
|
|
) {}
|
|
|
|
class EnvironmentAuthHttpApi extends HttpApiGroup.make("auth")
|
|
.add(
|
|
HttpApiEndpoint.get("session", "/api/auth/session", {
|
|
headers: OptionalBearerHeaders,
|
|
success: AuthSessionState,
|
|
error: [EnvironmentInternalError],
|
|
}),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("browserSession", "/api/auth/browser-session", {
|
|
payload: AuthBrowserSessionRequest,
|
|
success: AuthBrowserSessionResult,
|
|
error: EnvironmentSessionCreationErrors,
|
|
}),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("token", "/oauth/token", {
|
|
headers: OptionalDpopProofHeaders,
|
|
payload: AuthTokenExchangeRequest,
|
|
success: AuthAccessTokenResult,
|
|
error: EnvironmentTokenExchangeErrors,
|
|
}),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("webSocketTicket", "/api/auth/websocket-ticket", {
|
|
headers: OptionalBearerHeaders,
|
|
success: AuthWebSocketTicketResult,
|
|
error: [EnvironmentInternalError],
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("pairingCredential", "/api/auth/pairing-token", {
|
|
headers: OptionalBearerHeaders,
|
|
payload: AuthCreatePairingCredentialInput,
|
|
success: AuthPairingCredentialResult,
|
|
error: EnvironmentPairingCredentialErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.get("pairingLinks", "/api/auth/pairing-links", {
|
|
headers: OptionalBearerHeaders,
|
|
success: Schema.Array(AuthPairingLink),
|
|
error: EnvironmentScopedOperationErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("revokePairingLink", "/api/auth/pairing-links/revoke", {
|
|
headers: OptionalBearerHeaders,
|
|
payload: AuthRevokePairingLinkInput,
|
|
success: AuthPairingLinkRevokeResult,
|
|
error: EnvironmentScopedOperationErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.get("clients", "/api/auth/clients", {
|
|
headers: OptionalBearerHeaders,
|
|
success: Schema.Array(AuthClientSession),
|
|
error: EnvironmentScopedOperationErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("revokeClient", "/api/auth/clients/revoke", {
|
|
headers: OptionalBearerHeaders,
|
|
payload: AuthRevokeClientSessionInput,
|
|
success: AuthClientSessionRevokeResult,
|
|
error: EnvironmentSessionRevokeErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("revokeOtherClients", "/api/auth/clients/revoke-others", {
|
|
headers: OptionalBearerHeaders,
|
|
success: AuthOtherClientSessionsRevokeResult,
|
|
error: EnvironmentScopedOperationErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
) {}
|
|
|
|
/**
|
|
* The OAuth authorization server outside agents use to sign in to `/mcp`.
|
|
* `authorize` is where the agent sends the browser: it answers a redirect to
|
|
* the web app's approval page, or a plain error page for a request that names
|
|
* an unverified client or redirect, so the response is not a schema.
|
|
*/
|
|
class EnvironmentMcpOAuthHttpApi extends HttpApiGroup.make("mcpOAuth")
|
|
.add(
|
|
HttpApiEndpoint.get("protectedResource", "/.well-known/oauth-protected-resource", {
|
|
success: AuthMcpProtectedResourceMetadata,
|
|
}),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.get("mcpProtectedResource", "/.well-known/oauth-protected-resource/mcp", {
|
|
success: AuthMcpProtectedResourceMetadata,
|
|
}),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.get("authorizationServer", "/.well-known/oauth-authorization-server", {
|
|
success: AuthMcpAuthorizationServerMetadata,
|
|
}),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("register", "/oauth/mcp/register", {
|
|
payload: AuthMcpClientRegistration,
|
|
success: AuthMcpRegisteredClient,
|
|
error: AuthMcpRegistrationError,
|
|
}),
|
|
)
|
|
.add(HttpApiEndpoint.get("authorize", "/oauth/mcp/authorize"))
|
|
.add(
|
|
HttpApiEndpoint.post("approval", "/oauth/mcp/approval", {
|
|
payload: AuthMcpAuthorizationRequest,
|
|
success: [AuthMcpApprovalDetails, AuthMcpApprovalRedirect],
|
|
error: AuthMcpApprovalError,
|
|
}),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("decision", "/oauth/mcp/decision", {
|
|
payload: AuthMcpApprovalDecisionRequest,
|
|
success: AuthMcpApprovalRedirect,
|
|
error: AuthMcpApprovalError,
|
|
}),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("token", "/oauth/mcp/token", {
|
|
payload: AuthMcpTokenRequest,
|
|
success: AuthMcpTokenResult,
|
|
error: AuthMcpTokenError,
|
|
}),
|
|
) {}
|
|
|
|
const EnvironmentOrchestrationThreadSnapshotParams = Schema.Struct({
|
|
threadId: ThreadId,
|
|
});
|
|
|
|
/**
|
|
* `compactTurnItems=1` opts into `turnItemsOmitLocalVisible`. Other values are
|
|
* ignored, and older servers ignore the whole query.
|
|
*/
|
|
const EnvironmentOrchestrationThreadBoundedSnapshotQuery = Schema.Struct({
|
|
compactTurnItems: Schema.optionalKey(Schema.String),
|
|
});
|
|
|
|
const EnvironmentOrchestrationThreadHistoryQuery = Schema.Struct({
|
|
cursor: TrimmedNonEmptyString,
|
|
throughEntryId: Schema.optional(TrimmedNonEmptyString),
|
|
view: Schema.optional(Schema.Literals(["conversation", "activity"])),
|
|
});
|
|
|
|
const EnvironmentOrchestrationThreadHistoryErrors = [
|
|
EnvironmentRequestInvalidError,
|
|
EnvironmentScopeRequiredError,
|
|
EnvironmentResourceNotFoundError,
|
|
EnvironmentInternalError,
|
|
] as const;
|
|
|
|
class EnvironmentOrchestrationHttpApi extends HttpApiGroup.make("orchestration")
|
|
.add(
|
|
HttpApiEndpoint.get("shellSnapshot", "/api/orchestration/shell", {
|
|
headers: OrchestrationProtocolHeaders,
|
|
success: OrchestrationV2ShellSnapshot,
|
|
error: EnvironmentOrchestrationSnapshotErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.get("threadSnapshot", "/api/orchestration/threads/:threadId", {
|
|
headers: OrchestrationProtocolHeaders,
|
|
params: EnvironmentOrchestrationThreadSnapshotParams,
|
|
success: OrchestrationV2ThreadDetailSnapshot,
|
|
error: EnvironmentOrchestrationThreadSnapshotErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.get("threadBoundedSnapshot", "/api/orchestration/threads/:threadId/bounded", {
|
|
headers: OrchestrationProtocolHeaders,
|
|
params: EnvironmentOrchestrationThreadSnapshotParams,
|
|
query: EnvironmentOrchestrationThreadBoundedSnapshotQuery,
|
|
success: OrchestrationV2ThreadBoundedSnapshot,
|
|
error: EnvironmentOrchestrationThreadSnapshotErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.get("threadHistoryPage", "/api/orchestration/threads/:threadId/history", {
|
|
headers: OrchestrationProtocolHeaders,
|
|
params: EnvironmentOrchestrationThreadSnapshotParams,
|
|
query: EnvironmentOrchestrationThreadHistoryQuery,
|
|
success: OrchestrationV2ThreadHistoryPage,
|
|
error: EnvironmentOrchestrationThreadHistoryErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
) {}
|
|
|
|
class EnvironmentProjectsHttpApi extends HttpApiGroup.make("projects")
|
|
.add(
|
|
HttpApiEndpoint.get("snapshot", "/api/projects", {
|
|
headers: OptionalBearerHeaders,
|
|
success: ProjectSnapshot,
|
|
error: EnvironmentProjectSnapshotErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("mutate", "/api/projects/mutate", {
|
|
headers: OptionalBearerHeaders,
|
|
payload: ProjectMutation,
|
|
success: Project,
|
|
error: EnvironmentProjectMutationErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
) {}
|
|
|
|
/** Large, compressible pull-request payloads travel over HTTP rather than the RPC socket. */
|
|
class EnvironmentPullRequestsHttpApi extends HttpApiGroup.make("pullRequests").add(
|
|
HttpApiEndpoint.post("diff", "/api/pull-requests/diff", {
|
|
headers: OptionalBearerHeaders,
|
|
payload: PullRequestDiffInput,
|
|
success: PullRequestDiffResult,
|
|
error: [
|
|
PullRequestUnavailableError,
|
|
PullRequestOperationError,
|
|
EnvironmentAuthInvalidError,
|
|
EnvironmentScopeRequiredError,
|
|
EnvironmentInternalError,
|
|
],
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
) {}
|
|
|
|
class EnvironmentConnectHttpApi extends HttpApiGroup.make("connect")
|
|
.add(
|
|
HttpApiEndpoint.post("linkProof", "/api/connect/link-proof", {
|
|
headers: OptionalBearerHeaders,
|
|
payload: RelayLinkProofRequest,
|
|
success: RelayEnvironmentLinkProof,
|
|
error: EnvironmentHttpCloudErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("relayConfig", "/api/connect/relay-config", {
|
|
headers: OptionalBearerHeaders,
|
|
payload: RelayEnvironmentConfigRequest,
|
|
success: EnvironmentCloudRelayConfigResult,
|
|
error: [...EnvironmentHttpCloudErrors, EnvironmentCloudEndpointUnavailableError],
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.get("linkState", "/api/connect/link-state", {
|
|
headers: OptionalBearerHeaders,
|
|
success: EnvironmentCloudLinkStateResult,
|
|
error: EnvironmentHttpCloudErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("unlink", "/api/connect/unlink", {
|
|
headers: OptionalBearerHeaders,
|
|
success: EnvironmentCloudRelayConfigResult,
|
|
error: EnvironmentHttpCloudErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("preferences", "/api/connect/preferences", {
|
|
headers: OptionalBearerHeaders,
|
|
payload: EnvironmentCloudPreferencesRequest,
|
|
success: EnvironmentCloudLinkStateResult,
|
|
error: EnvironmentHttpCloudErrors,
|
|
}).middleware(EnvironmentAuthenticatedAuth),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("health", "/api/t3-connect/health", {
|
|
payload: RelayCloudEnvironmentHealthRequest,
|
|
success: RelayEnvironmentHealthResponse,
|
|
error: EnvironmentHttpCloudErrors,
|
|
}),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("mintCredential", "/api/connect/mint-credential", {
|
|
payload: RelayCloudMintCredentialRequest,
|
|
success: RelayEnvironmentMintResponse,
|
|
error: EnvironmentHttpCloudErrors,
|
|
}),
|
|
)
|
|
.add(
|
|
HttpApiEndpoint.post("t3MintCredential", "/api/t3-connect/mint-credential", {
|
|
payload: RelayCloudMintCredentialRequest,
|
|
success: RelayEnvironmentMintResponse,
|
|
error: EnvironmentHttpCloudErrors,
|
|
}),
|
|
) {}
|
|
|
|
/**
|
|
* Public entry point for webhook tasks. Unauthenticated by design: the token
|
|
* in the path, and an optional body signature, are the credential. The handler
|
|
* reads the raw body itself so a signature is checked over the exact bytes.
|
|
*/
|
|
const WebhookParams = Schema.Struct({
|
|
hookId: TrimmedNonEmptyString,
|
|
token: TrimmedNonEmptyString,
|
|
});
|
|
const WebhookAccepted = Schema.Struct({ deliveryId: TrimmedNonEmptyString }).pipe(
|
|
HttpApiSchema.status(202),
|
|
);
|
|
const webhookEndpoint = {
|
|
params: WebhookParams,
|
|
success: WebhookAccepted,
|
|
} as const;
|
|
const WEBHOOK_PATH = "/api/hooks/:hookId/:token";
|
|
|
|
class EnvironmentWebhooksHttpApi extends HttpApiGroup.make("webhooks")
|
|
.add(HttpApiEndpoint.post("webhookPost", WEBHOOK_PATH, webhookEndpoint))
|
|
.add(HttpApiEndpoint.put("webhookPut", WEBHOOK_PATH, webhookEndpoint))
|
|
.add(HttpApiEndpoint.patch("webhookPatch", WEBHOOK_PATH, webhookEndpoint))
|
|
.add(HttpApiEndpoint.get("webhookGet", WEBHOOK_PATH, webhookEndpoint)) {}
|
|
|
|
export class EnvironmentHttpApi extends HttpApi.make("environment")
|
|
.add(EnvironmentMetadataHttpApi)
|
|
.add(EnvironmentAuthHttpApi)
|
|
.add(EnvironmentMcpOAuthHttpApi)
|
|
.add(EnvironmentOrchestrationHttpApi)
|
|
.add(EnvironmentPullRequestsHttpApi)
|
|
.add(EnvironmentProjectsHttpApi)
|
|
.add(EnvironmentConnectHttpApi)
|
|
.add(EnvironmentWebhooksHttpApi) {}
|