From 652429377b99085d686d6b907c2f550c304e6b98 Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Mon, 19 May 2025 15:43:14 -0400 Subject: sync --- js/src/bus/index.ts | 22 +++++++++++++++++++--- js/src/index.ts | 19 ++++++++++++++++--- js/src/storage/storage.ts | 2 +- 3 files changed, 36 insertions(+), 7 deletions(-) (limited to 'js/src') diff --git a/js/src/bus/index.ts b/js/src/bus/index.ts index 4f3b40041..f320aeef3 100644 --- a/js/src/bus/index.ts +++ b/js/src/bus/index.ts @@ -1,4 +1,4 @@ -import type { z, ZodSchema } from "zod"; +import { z, type ZodType } from "zod/v4"; import { App } from "../app"; import { Log } from "../util/log"; @@ -16,14 +16,30 @@ export namespace Bus { export type EventDefinition = ReturnType; - export function event( + const registry = new Map(); + + export function event( type: Type, properties: Properties, ) { - return { + const result = { type, properties, }; + registry.set(type, result); + return result; + } + + export function specs() { + const children = {} as any; + for (const [type, def] of registry.entries()) { + children[def.type] = def.properties; + } + const result = z.toJSONSchema(z.object(children)) as any; + result.definitions = result.properties; + delete result.properties; + delete result.required; + return result; } export function publish( diff --git a/js/src/index.ts b/js/src/index.ts index 10b3fef32..b2a7c5f21 100644 --- a/js/src/index.ts +++ b/js/src/index.ts @@ -1,6 +1,9 @@ import { App } from "./app"; import { Server } from "./server/server"; import { Cli, Command, runExit } from "clipanion"; +import fs from "fs/promises"; +import path from "path"; +import { Bus } from "./bus"; const cli = new Cli({ binaryLabel: `opencode`, @@ -22,11 +25,21 @@ cli.register( }, ); cli.register( - class OpenApi extends Command { - static paths = [["openapi"]]; + class Generate extends Command { + static paths = [["generate"]]; async execute() { const specs = await Server.openapi(); - this.context.stdout.write(JSON.stringify(specs, null, 2)); + const dir = "gen"; + await fs.rmdir(dir, { recursive: true }).catch(() => {}); + await fs.mkdir(dir, { recursive: true }); + await Bun.write( + path.join(dir, "openapi.json"), + JSON.stringify(specs, null, 2), + ); + await Bun.write( + path.join(dir, "event.json"), + JSON.stringify(Bus.specs(), null, 2), + ); } }, ); diff --git a/js/src/storage/storage.ts b/js/src/storage/storage.ts index c24666c9f..50364beeb 100644 --- a/js/src/storage/storage.ts +++ b/js/src/storage/storage.ts @@ -5,7 +5,7 @@ import { Log } from "../util/log"; import { App } from "../app"; import { AppPath } from "../app/path"; import { Bus } from "../bus"; -import z from "zod"; +import z from "zod/v4"; export namespace Storage { const log = Log.create({ service: "storage" }); -- cgit v1.2.3