summaryrefslogtreecommitdiffhomepage
path: root/js
diff options
context:
space:
mode:
authorDax Raad <[email protected]>2025-05-19 15:43:14 -0400
committerDax Raad <[email protected]>2025-05-26 12:40:17 -0400
commit652429377b99085d686d6b907c2f550c304e6b98 (patch)
treee4fa0de6b0503885c76bb77e1e0d0e7d56e5117e /js
parent99af6146d5def31c59993636d60eb75a483a283b (diff)
downloadopencode-652429377b99085d686d6b907c2f550c304e6b98.tar.gz
opencode-652429377b99085d686d6b907c2f550c304e6b98.zip
sync
Diffstat (limited to 'js')
-rw-r--r--js/.gitignore1
-rw-r--r--js/bun.lock4
-rw-r--r--js/openapi.json90
-rw-r--r--js/opencode.jsonc3
-rw-r--r--js/package.json2
-rw-r--r--js/schema.json20
-rw-r--r--js/src/bus/index.ts22
-rw-r--r--js/src/index.ts19
-rw-r--r--js/src/storage/storage.ts2
9 files changed, 60 insertions, 103 deletions
diff --git a/js/.gitignore b/js/.gitignore
index f06235c46..5f9a3e7af 100644
--- a/js/.gitignore
+++ b/js/.gitignore
@@ -1,2 +1,3 @@
node_modules
dist
+gen
diff --git a/js/bun.lock b/js/bun.lock
index e6d8f9074..df1b2a17f 100644
--- a/js/bun.lock
+++ b/js/bun.lock
@@ -12,7 +12,7 @@
"clipanion": "^4.0.0-rc.4",
"hono": "^4.7.10",
"hono-openapi": "^0.4.8",
- "zod": "^3.24.4",
+ "zod": "^3.25.3",
"zod-openapi": "^4.2.4",
},
"devDependencies": {
@@ -199,7 +199,7 @@
"yoga-layout": ["[email protected]", "", {}, "sha512-0LPOt3AxKqMdFBZA3HBAt/t/8vIKq7VaQYbuA8WxCgung+p9TVyKRYdpvCb80HcdTN2NkbIKbhNwKUfm3tQywQ=="],
- "zod": ["[email protected]", "", {}, "sha512-OdqJE9UDRPwWsrHjLN2F8bPxvwJBK22EHLWtanu0LSYr5YqzsaaW3RMgmjwr8Rypg5k+meEJdSPXJZXE/yqOMg=="],
+ "zod": ["[email protected]", "", {}, "sha512-VGZqnyYNrl8JpEJRZaFPqeVNIuqgXNu4cXZ5cOb6zEUO1OxKbRnWB4UdDIXMmiERWncs0yDQukssHov8JUxykQ=="],
"zod-openapi": ["[email protected]", "", { "peerDependencies": { "zod": "^3.21.4" } }, "sha512-tsrQpbpqFCXqVXUzi3TPwFhuMtLN3oNZobOtYnK6/5VkXsNdnIgyNr4r8no4wmYluaxzN3F7iS+8xCW8BmMQ8g=="],
diff --git a/js/openapi.json b/js/openapi.json
deleted file mode 100644
index 8dbe78550..000000000
--- a/js/openapi.json
+++ /dev/null
@@ -1,90 +0,0 @@
-{
- "openapi": "3.1.0",
- "info": {
- "title": "opencode",
- "description": "opencode api",
- "version": "1.0.0"
- },
- "paths": {
- "/session_create": {
- "post": {
- "responses": {
- "200": {
- "description": "Successfully created session",
- "content": {
- "application/json": {
- "schema": {
- "type": "object",
- "properties": {
- "id": {
- "type": "string",
- "pattern": "^ses"
- },
- "title": {
- "type": "string"
- },
- "tokens": {
- "type": "object",
- "properties": {
- "input": {
- "type": "number"
- },
- "output": {
- "type": "number"
- },
- "reasoning": {
- "type": "number"
- }
- },
- "required": [
- "input",
- "output",
- "reasoning"
- ]
- }
- },
- "required": [
- "id",
- "title",
- "tokens"
- ]
- }
- }
- }
- }
- },
- "operationId": "postSession_create",
- "parameters": [],
- "description": "Create a new session"
- }
- },
- "/session_chat": {
- "post": {
- "responses": {},
- "operationId": "postSession_chat",
- "parameters": [],
- "requestBody": {
- "content": {
- "application/json": {
- "schema": {
- "type": "object",
- "properties": {
- "sessionID": {
- "type": "string"
- },
- "parts": {}
- },
- "required": [
- "sessionID"
- ]
- }
- }
- }
- }
- }
- }
- },
- "components": {
- "schemas": {}
- }
-} \ No newline at end of file
diff --git a/js/opencode.jsonc b/js/opencode.jsonc
deleted file mode 100644
index 87b07bc4e..000000000
--- a/js/opencode.jsonc
+++ /dev/null
@@ -1,3 +0,0 @@
-{
- "providers": {}
-}
diff --git a/js/package.json b/js/package.json
index 63429b846..3703c10b0 100644
--- a/js/package.json
+++ b/js/package.json
@@ -22,7 +22,7 @@
"clipanion": "^4.0.0-rc.4",
"hono": "^4.7.10",
"hono-openapi": "^0.4.8",
- "zod": "^3.24.4",
+ "zod": "^3.25.3",
"zod-openapi": "^4.2.4"
}
}
diff --git a/js/schema.json b/js/schema.json
new file mode 100644
index 000000000..bc08652e0
--- /dev/null
+++ b/js/schema.json
@@ -0,0 +1,20 @@
+{
+ "type": "object",
+ "properties": {
+ "type": {
+ "const": "storage.write"
+ },
+ "properties": {
+ "type": "object",
+ "properties": {
+ "key": {
+ "type": "string"
+ },
+ "body": {}
+ },
+ "required": ["key", "body"]
+ }
+ },
+ "required": ["type", "properties"],
+ "$schema": "https://json-schema.org/draft-2020-12/schema"
+}
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<typeof event>;
- export function event<Type extends string, Properties extends ZodSchema>(
+ const registry = new Map<string, EventDefinition>();
+
+ export function event<Type extends string, Properties extends ZodType>(
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<Definition extends EventDefinition>(
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" });