Merge registry-envelope-integration into main (resolve conflicts with call-envelope-integration)

This commit is contained in:
2026-05-11 02:23:52 +00:00
11 changed files with 96 additions and 297 deletions

View File

@@ -1,12 +1,13 @@
import { OperationType } from "./types.js";
import type { OperationContext, OperationEnv, Identity } from "./types.js";
import type { OperationRegistry } from "./registry.js";
import type { ResponseEnvelope } from "./response-envelope.js";
import { getLogger } from "@logtape/logtape";
const logger = getLogger("operations:env");
export interface CallMap {
call(operationId: string, input: unknown, options?: { parentRequestId?: string; deadline?: number; identity?: Identity }): Promise<unknown>;
call(operationId: string, input: unknown, options?: { parentRequestId?: string; deadline?: number; identity?: Identity }): Promise<ResponseEnvelope>;
}
export interface EnvOptions {

View File

@@ -1,7 +1,9 @@
import type { OperationContext, OperationSpec, OperationHandler, SubscriptionHandler } from "./types.js";
import { getLogger } from "@logtape/logtape";
import { Value } from "@alkdev/typebox/value";
import { KindGuard } from "@alkdev/typebox";
import { assertIsSchema, validateOrThrow, collectErrors, formatValueErrors } from "./validation.js";
import { isResponseEnvelope, localEnvelope, type ResponseEnvelope } from "./response-envelope.js";
const logger = getLogger("operations:registry");
@@ -81,7 +83,7 @@ export class OperationRegistry {
operationId: string,
input: TInput,
context: OperationContext,
): Promise<TOutput> {
): Promise<ResponseEnvelope<TOutput>> {
const spec = this.specs.get(operationId);
if (!spec) {
throw new Error(`Operation not found: ${operationId}`);
@@ -94,13 +96,24 @@ export class OperationRegistry {
validateOrThrow(spec.inputSchema, input, `Input validation failed for ${operationId}`);
const result = await handler(input, context) as TOutput;
const result = await handler(input, context);
const errors = collectErrors(spec.outputSchema, result);
let envelope: ResponseEnvelope<TOutput>;
if (isResponseEnvelope(result)) {
envelope = result as ResponseEnvelope<TOutput>;
} else {
envelope = localEnvelope(result as TOutput, operationId);
}
if (!KindGuard.IsUnknown(spec.outputSchema)) {
envelope.data = Value.Cast(spec.outputSchema, envelope.data) as TOutput;
}
const errors = collectErrors(spec.outputSchema, envelope.data);
if (errors.length > 0) {
logger.warn(`Output validation failed for ${operationId}:\n${formatValueErrors(errors)}`);
}
return result;
return envelope;
}
}

View File

@@ -1,4 +1,5 @@
import { Type, type Static, type TSchema } from "@alkdev/typebox";
import type { ResponseEnvelope } from "./response-envelope.js";
export enum OperationType {
QUERY = "query",
@@ -12,7 +13,7 @@ export interface Identity {
resources?: Record<string, string[]>
}
export type OperationEnv = Record<string, Record<string, (input: unknown) => Promise<unknown>>>
export type OperationEnv = Record<string, Record<string, (input: unknown) => Promise<ResponseEnvelope>>>
export const OperationContextSchema = Type.Object({
metadata: Type.Optional(Type.Record(Type.String(), Type.Unknown())),