11// Gateway Protocol schema module defines protocol validation shapes.
2+ import type { Static } from "typebox" ;
3+ import { Type } from "typebox" ;
4+ import { closedObject } from "./closed-object.js" ;
25import type { ErrorShape } from "./frames.js" ;
6+ import { NonEmptyString } from "./primitives.js" ;
37
48/** Gateway JSON-RPC style error codes shared by clients and server handlers. */
59export const ErrorCodes = {
@@ -11,6 +15,8 @@ export const ErrorCodes = {
1115 AGENT_TIMEOUT : "AGENT_TIMEOUT" ,
1216 /** Request payload failed protocol validation or method preconditions. */
1317 INVALID_REQUEST : "INVALID_REQUEST" ,
18+ /** Authenticated caller lacks permission for the requested operation. */
19+ FORBIDDEN : "FORBIDDEN" ,
1420 /** Approval resolution referenced a missing or expired approval request. */
1521 APPROVAL_NOT_FOUND : "APPROVAL_NOT_FOUND" ,
1622 /** Gateway service or required backend is temporarily unavailable. */
@@ -20,6 +26,24 @@ export const ErrorCodes = {
2026/** Closed set of canonical gateway error code strings. */
2127export type ErrorCode = ( typeof ErrorCodes ) [ keyof typeof ErrorCodes ] ;
2228
29+ /** Stable discriminants for structured method-level authorization failures. */
30+ export const GatewayErrorDetailCodes = {
31+ MISSING_SCOPE : "MISSING_SCOPE" ,
32+ } as const ;
33+
34+ /** Missing operator-scope details shared by WebSocket and HTTP responses. */
35+ export const MissingScopeErrorDetailsSchema = closedObject ( {
36+ code : Type . Literal ( GatewayErrorDetailCodes . MISSING_SCOPE ) ,
37+ missingScope : NonEmptyString ,
38+ requiredScopes : Type . Array ( NonEmptyString , { minItems : 1 } ) ,
39+ } ) ;
40+
41+ /** Structured details emitted by method-level authorization failures. */
42+ export const GatewayErrorDetailsSchema = MissingScopeErrorDetailsSchema ;
43+
44+ export type MissingScopeErrorDetails = Static < typeof MissingScopeErrorDetailsSchema > ;
45+ export type GatewayErrorDetails = Static < typeof GatewayErrorDetailsSchema > ;
46+
2347/** Builds the canonical gateway error payload while preserving optional retry metadata. */
2448export function errorShape (
2549 code : ErrorCode ,
@@ -32,3 +56,26 @@ export function errorShape(
3256 ...opts ,
3357 } ;
3458}
59+
60+ /** Builds structured details for a missing operator scope. */
61+ export function buildMissingScopeErrorDetails ( params : {
62+ missingScope : string ;
63+ requiredScopes : readonly string [ ] ;
64+ } ) : MissingScopeErrorDetails {
65+ const requiredScopes =
66+ params . requiredScopes . length > 0 ? [ ...params . requiredScopes ] : [ params . missingScope ] ;
67+ return {
68+ code : GatewayErrorDetailCodes . MISSING_SCOPE ,
69+ missingScope : params . missingScope ,
70+ requiredScopes,
71+ } ;
72+ }
73+
74+ /** Builds a forbidden error for a missing operator scope without message parsing. */
75+ export function missingScopeErrorShape ( params : {
76+ missingScope : string ;
77+ requiredScopes : readonly string [ ] ;
78+ } ) : ErrorShape {
79+ const details = buildMissingScopeErrorDetails ( params ) ;
80+ return errorShape ( ErrorCodes . FORBIDDEN , `missing scope: ${ params . missingScope } ` , { details } ) ;
81+ }
0 commit comments