diff options
| author | Dax Raad <[email protected]> | 2025-05-18 22:30:41 -0400 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-05-26 12:40:17 -0400 |
| commit | 99af6146d5def31c59993636d60eb75a483a283b (patch) | |
| tree | eb57ed227c15cf9be54bc9f231c83895d812f881 /js/src | |
| parent | 020e0ca039287b73fa33041fbd1bb214e6ccb396 (diff) | |
| download | opencode-99af6146d5def31c59993636d60eb75a483a283b.tar.gz opencode-99af6146d5def31c59993636d60eb75a483a283b.zip | |
openapi
Diffstat (limited to 'js/src')
| -rw-r--r-- | js/src/app/config.ts | 2 | ||||
| -rw-r--r-- | js/src/bus/index.ts | 2 | ||||
| -rw-r--r-- | js/src/id/id.ts | 2 | ||||
| -rw-r--r-- | js/src/index.ts | 35 | ||||
| -rw-r--r-- | js/src/server/server.ts | 60 | ||||
| -rw-r--r-- | js/src/session/session.ts | 22 | ||||
| -rw-r--r-- | js/src/storage/storage.ts | 2 |
7 files changed, 97 insertions, 28 deletions
diff --git a/js/src/app/config.ts b/js/src/app/config.ts index 84960db61..947298c67 100644 --- a/js/src/app/config.ts +++ b/js/src/app/config.ts @@ -1,6 +1,6 @@ import path from "node:path"; import { Log } from "../util/log"; -import { z } from "zod/v4"; +import { z } from "zod"; export namespace Config { const log = Log.create({ service: "config" }); diff --git a/js/src/bus/index.ts b/js/src/bus/index.ts index 5359debd9..4f3b40041 100644 --- a/js/src/bus/index.ts +++ b/js/src/bus/index.ts @@ -1,4 +1,4 @@ -import type { z, ZodSchema } from "zod/v4"; +import type { z, ZodSchema } from "zod"; import { App } from "../app"; import { Log } from "../util/log"; diff --git a/js/src/id/id.ts b/js/src/id/id.ts index 39ee5326f..96ea870a1 100644 --- a/js/src/id/id.ts +++ b/js/src/id/id.ts @@ -1,4 +1,4 @@ -import { z } from "zod/v4"; +import { z } from "zod"; import { randomBytes } from "crypto"; export namespace Identifier { diff --git a/js/src/index.ts b/js/src/index.ts index 8f98310be..10b3fef32 100644 --- a/js/src/index.ts +++ b/js/src/index.ts @@ -1,11 +1,34 @@ import { App } from "./app"; -import process from "node:process"; import { Server } from "./server/server"; +import { Cli, Command, runExit } from "clipanion"; -const app = await App.create({ - directory: process.cwd(), +const cli = new Cli({ + binaryLabel: `opencode`, + binaryName: `opencode`, + binaryVersion: `1.0.0`, }); -App.provide(app, async () => { - const server = Server.listen(); -}); +cli.register( + class Run extends Command { + async execute() { + const app = await App.create({ + directory: process.cwd(), + }); + + await App.provide(app, async () => { + const server = Server.listen(); + }); + } + }, +); +cli.register( + class OpenApi extends Command { + static paths = [["openapi"]]; + async execute() { + const specs = await Server.openapi(); + this.context.stdout.write(JSON.stringify(specs, null, 2)); + } + }, +); +const [_bun, _app, ...args] = process.argv; +cli.runExit(args); diff --git a/js/src/server/server.ts b/js/src/server/server.ts index 7ed67057c..5d9333e45 100644 --- a/js/src/server/server.ts +++ b/js/src/server/server.ts @@ -1,10 +1,10 @@ import { Log } from "../util/log"; import { Bus } from "../bus"; - +import { describeRoute, generateSpecs, openAPISpecs } from "hono-openapi"; import { Hono } from "hono"; import { streamSSE } from "hono/streaming"; import { Session } from "../session/session"; -import { zValidator } from "@hono/zod-validator"; +import { resolver, validator as zValidator } from "hono-openapi/zod"; import { z } from "zod"; export namespace Server { @@ -14,7 +14,21 @@ export namespace Server { export type App = ReturnType<typeof app>; function app() { - return new Hono() + const app = new Hono(); + + const result = app + .get( + "/openapi", + openAPISpecs(app, { + documentation: { + info: { + title: "opencode", + version: "1.0.0", + description: "opencode api", + }, + }, + }), + ) .get("/event", async (c) => { log.info("event connected"); return streamSSE(c, async (stream) => { @@ -32,10 +46,26 @@ export namespace Server { }); }); }) - .post("/session_create", async (c) => { - const session = await Session.create(); - return c.json(session); - }) + .post( + "/session_create", + describeRoute({ + description: "Create a new session", + responses: { + 200: { + description: "Successfully created session", + content: { + "application/json": { + schema: resolver(Session.Info), + }, + }, + }, + }, + }), + async (c) => { + const session = await Session.create(); + return c.json(session); + }, + ) .post( "/session_chat", zValidator( @@ -51,6 +81,22 @@ export namespace Server { return c.json(msg); }, ); + + return result; + } + + export async function openapi() { + const a = app(); + const result = await generateSpecs(a, { + documentation: { + info: { + title: "opencode", + version: "1.0.0", + description: "opencode api", + }, + }, + }); + return result; } export function listen() { diff --git a/js/src/session/session.ts b/js/src/session/session.ts index fb45f0e59..e86048b1c 100644 --- a/js/src/session/session.ts +++ b/js/src/session/session.ts @@ -1,5 +1,4 @@ import path from "path"; -import { z } from "zod/v3"; import { App } from "../app/"; import { Identifier } from "../id/id"; import { LLM } from "../llm/llm"; @@ -8,26 +7,27 @@ import { Log } from "../util/log"; import { convertToModelMessages, streamText, - tool, type TextUIPart, type ToolInvocationUIPart, type UIDataTypes, type UIMessage, type UIMessagePart, } from "ai"; +import { z } from "zod"; export namespace Session { const log = Log.create({ service: "session" }); - export interface Info { - id: string; - title: string; - tokens: { - input: number; - output: number; - reasoning: number; - }; - } + export const Info = z.object({ + id: Identifier.schema("session"), + title: z.string(), + tokens: z.object({ + input: z.number(), + output: z.number(), + reasoning: z.number(), + }), + }); + export type Info = z.output<typeof Info>; export type Message = UIMessage<{ sessionID: string }>; diff --git a/js/src/storage/storage.ts b/js/src/storage/storage.ts index 50364beeb..c24666c9f 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/v4"; +import z from "zod"; export namespace Storage { const log = Log.create({ service: "storage" }); |
