GeezerVison is showing github.com — rename bus · kasbah/opencode@1e3bdcc
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +43Lines changed: 43 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import z from "zod" | | 2 | +import type { ZodType } from "zod" | | 3 | +import { Log } from "../util/log" | | 4 | + | | 5 | +export namespace BusEvent { | | 6 | + const log = Log.create({ service: "event" }) | | 7 | + | | 8 | + export type Definition = ReturnType<typeof define> | | 9 | + | | 10 | + const registry = new Map<string, Definition>() | | 11 | + | | 12 | + export function define<Type extends string, Properties extends ZodType>(type: Type, properties: Properties) { | | 13 | + const result = { | | 14 | + type, | | 15 | + properties, | | 16 | + } | | 17 | + registry.set(type, result) | | 18 | + return result | | 19 | + } | | 20 | + | | 21 | + export function payloads() { | | 22 | + return z | | 23 | + .discriminatedUnion( | | 24 | + "type", | | 25 | + registry | | 26 | + .entries() | | 27 | + .map(([type, def]) => { | | 28 | + return z | | 29 | + .object({ | | 30 | + type: z.literal(type), | | 31 | + properties: def.properties, | | 32 | + }) | | 33 | + .meta({ | | 34 | + ref: "Event" + "." + def.type, | | 35 | + }) | | 36 | + }) | | 37 | + .toArray() as any, | | 38 | + ) | | 39 | + .meta({ | | 40 | + ref: "Event", | | 41 | + }) | | 42 | + } | | 43 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/bus/index.ts +5-41Lines changed: 5 additions & 41 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import z from "zod" | 2 | | -import type { ZodType } from "zod" | 3 | 2 | import { Log } from "../util/log" | 4 | 3 | import { Instance } from "../project/instance" | | 4 | +import { BusEvent } from "./bus-event" | 5 | 5 | import { GlobalBus } from "./global" | 6 | 6 |
| 7 | 7 | export namespace Bus { | 8 | 8 | const log = Log.create({ service: "bus" }) | 9 | 9 | type Subscription = (event: any) => void | 10 | 10 | const disposedEventType = "server.instance.disposed" | 11 | 11 |
| 12 | | - export type EventDefinition = ReturnType<typeof event> | 13 | | - | 14 | | - const registry = new Map<string, EventDefinition>() | 15 | | - | 16 | 12 | const state = Instance.state( | 17 | 13 | () => { | 18 | 14 | const subscriptions = new Map<any, Subscription[]>() | @@ -36,46 +32,14 @@ export namespace Bus { | 36 | 32 | }, | 37 | 33 | ) | 38 | 34 |
| 39 | | - export function event<Type extends string, Properties extends ZodType>(type: Type, properties: Properties) { | 40 | | - const result = { | 41 | | - type, | 42 | | - properties, | 43 | | - } | 44 | | - registry.set(type, result) | 45 | | - return result | 46 | | - } | 47 | | - | 48 | | - export const InstanceDisposed = event( | | 35 | + export const InstanceDisposed = BusEvent.define( | 49 | 36 | disposedEventType, | 50 | 37 | z.object({ | 51 | 38 | directory: z.string(), | 52 | 39 | }), | 53 | 40 | ) | 54 | 41 |
| 55 | | - export function payloads() { | 56 | | - return z | 57 | | - .discriminatedUnion( | 58 | | - "type", | 59 | | - registry | 60 | | - .entries() | 61 | | - .map(([type, def]) => { | 62 | | - return z | 63 | | - .object({ | 64 | | - type: z.literal(type), | 65 | | - properties: def.properties, | 66 | | - }) | 67 | | - .meta({ | 68 | | - ref: "Event" + "." + def.type, | 69 | | - }) | 70 | | - }) | 71 | | - .toArray() as any, | 72 | | - ) | 73 | | - .meta({ | 74 | | - ref: "Event", | 75 | | - }) | 76 | | - } | 77 | | - | 78 | | - export async function publish<Definition extends EventDefinition>( | | 42 | + export async function publish<Definition extends BusEvent.Definition>( | 79 | 43 | def: Definition, | 80 | 44 | properties: z.output<Definition["properties"]>, | 81 | 45 | ) { | @@ -100,14 +64,14 @@ export namespace Bus { | 100 | 64 | return Promise.all(pending) | 101 | 65 | } | 102 | 66 |
| 103 | | - export function subscribe<Definition extends EventDefinition>( | | 67 | + export function subscribe<Definition extends BusEvent.Definition>( | 104 | 68 | def: Definition, | 105 | 69 | callback: (event: { type: Definition["type"]; properties: z.infer<Definition["properties"]> }) => void, | 106 | 70 | ) { | 107 | 71 | return raw(def.type, callback) | 108 | 72 | } | 109 | 73 |
| 110 | | - export function once<Definition extends EventDefinition>( | | 74 | + export function once<Definition extends BusEvent.Definition>( | 111 | 75 | def: Definition, | 112 | 76 | callback: (event: { | 113 | 77 | type: Definition["type"] | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/tui/event.ts +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { BusEvent } from "@/bus/bus-event" | 1 | 2 | import { Bus } from "@/bus" | 2 | 3 | import z from "zod" | 3 | 4 |
| 4 | 5 | export const TuiEvent = { | 5 | | - PromptAppend: Bus.event("tui.prompt.append", z.object({ text: z.string() })), | 6 | | - CommandExecute: Bus.event( | | 6 | + PromptAppend: BusEvent.define("tui.prompt.append", z.object({ text: z.string() })), | | 7 | + CommandExecute: BusEvent.define( | 7 | 8 | "tui.command.execute", | 8 | 9 | z.object({ | 9 | 10 | command: z.union([ | @@ -27,7 +28,7 @@ export const TuiEvent = { | 27 | 28 | ]), | 28 | 29 | }), | 29 | 30 | ), | 30 | | - ToastShow: Bus.event( | | 31 | + ToastShow: BusEvent.define( | 31 | 32 | "tui.toast.show", | 32 | 33 | z.object({ | 33 | 34 | title: z.string().optional(), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/command/index.ts +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { Bus } from "@/bus" | | 2 | +import { BusEvent } from "@/bus/bus-event" | 1 | 3 | import z from "zod" | 2 | 4 | import { Config } from "../config/config" | 3 | 5 | import { Instance } from "../project/instance" | 4 | | -import { Bus } from "../bus" | 5 | 6 | import { Identifier } from "../id/id" | 6 | 7 | import PROMPT_INITIALIZE from "./template/initialize.txt" | 7 | 8 | import PROMPT_REVIEW from "./template/review.txt" | 8 | 9 |
| 9 | 10 | export namespace Command { | 10 | 11 | export const Event = { | 11 | | - Executed: Bus.event( | | 12 | + Executed: BusEvent.define( | 12 | 13 | "command.executed", | 13 | 14 | z.object({ | 14 | 15 | name: z.string(), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/file/index.ts +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { BusEvent } from "@/bus/bus-event" | | 2 | +import { Bus } from "@/bus" | 1 | 3 | import z from "zod" | 2 | | -import { Bus } from "../bus" | 3 | 4 | import { $ } from "bun" | 4 | 5 | import type { BunFile } from "bun" | 5 | 6 | import { formatPatch, structuredPatch } from "diff" | @@ -111,7 +112,7 @@ export namespace File { | 111 | 112 | } | 112 | 113 |
| 113 | 114 | export const Event = { | 114 | | - Edited: Bus.event( | | 115 | + Edited: BusEvent.define( | 115 | 116 | "file.edited", | 116 | 117 | z.object({ | 117 | 118 | file: z.string(), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/file/watcher.ts +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { BusEvent } from "@/bus/bus-event" | | 2 | +import { Bus } from "@/bus" | 1 | 3 | import z from "zod" | 2 | | -import { Bus } from "../bus" | 3 | 4 | import { Instance } from "../project/instance" | 4 | 5 | import { Log } from "../util/log" | 5 | 6 | import { FileIgnore } from "./ignore" | @@ -16,7 +17,7 @@ export namespace FileWatcher { | 16 | 17 | const log = Log.create({ service: "file.watcher" }) | 17 | 18 |
| 18 | 19 | export const Event = { | 19 | | - Updated: Bus.event( | | 20 | + Updated: BusEvent.define( | 20 | 21 | "file.watcher.updated", | 21 | 22 | z.object({ | 22 | 23 | file: z.string(), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/ide/index.ts +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { BusEvent } from "@/bus/bus-event" | | 2 | +import { Bus } from "@/bus" | 1 | 3 | import { spawn } from "bun" | 2 | 4 | import z from "zod" | 3 | 5 | import { NamedError } from "@opencode-ai/util/error" | 4 | 6 | import { Log } from "../util/log" | 5 | | -import { Bus } from "../bus" | 6 | 7 |
| 7 | 8 | const SUPPORTED_IDES = [ | 8 | 9 | { name: "Windsurf" as const, cmd: "windsurf" }, | @@ -16,7 +17,7 @@ export namespace Ide { | 16 | 17 | const log = Log.create({ service: "ide" }) | 17 | 18 |
| 18 | 19 | export const Event = { | 19 | | - Installed: Bus.event( | | 20 | + Installed: BusEvent.define( | 20 | 21 | "ide.installed", | 21 | 22 | z.object({ | 22 | 23 | ide: z.string(), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/installation/index.ts +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { BusEvent } from "@/bus/bus-event" | | 2 | +import { Bus } from "@/bus" | 1 | 3 | import path from "path" | 2 | 4 | import { $ } from "bun" | 3 | 5 | import z from "zod" | 4 | 6 | import { NamedError } from "@opencode-ai/util/error" | 5 | | -import { Bus } from "../bus" | 6 | 7 | import { Log } from "../util/log" | 7 | 8 | import { iife } from "@/util/iife" | 8 | 9 |
| @@ -17,13 +18,13 @@ export namespace Installation { | 17 | 18 | export type Method = Awaited<ReturnType<typeof method>> | 18 | 19 |
| 19 | 20 | export const Event = { | 20 | | - Updated: Bus.event( | | 21 | + Updated: BusEvent.define( | 21 | 22 | "installation.updated", | 22 | 23 | z.object({ | 23 | 24 | version: z.string(), | 24 | 25 | }), | 25 | 26 | ), | 26 | | - UpdateAvailable: Bus.event( | | 27 | + UpdateAvailable: BusEvent.define( | 27 | 28 | "installation.update-available", | 28 | 29 | z.object({ | 29 | 30 | version: z.string(), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/lsp/client.ts +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { BusEvent } from "@/bus/bus-event" | | 2 | +import { Bus } from "@/bus" | 1 | 3 | import path from "path" | 2 | 4 | import { createMessageConnection, StreamMessageReader, StreamMessageWriter } from "vscode-jsonrpc/node" | 3 | 5 | import type { Diagnostic as VSCodeDiagnostic } from "vscode-languageserver-types" | 4 | 6 | import { Log } from "../util/log" | 5 | 7 | import { LANGUAGE_EXTENSIONS } from "./language" | 6 | | -import { Bus } from "../bus" | 7 | 8 | import z from "zod" | 8 | 9 | import type { LSPServer } from "./server" | 9 | 10 | import { NamedError } from "@opencode-ai/util/error" | @@ -25,7 +26,7 @@ export namespace LSPClient { | 25 | 26 | ) | 26 | 27 |
| 27 | 28 | export const Event = { | 28 | | - Diagnostics: Bus.event( | | 29 | + Diagnostics: BusEvent.define( | 29 | 30 | "lsp.client.diagnostics", | 30 | 31 | z.object({ | 31 | 32 | serverID: z.string(), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/lsp/index.ts +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { BusEvent } from "@/bus/bus-event" | | 2 | +import { Bus } from "@/bus" | 1 | 3 | import { Log } from "../util/log" | 2 | 4 | import { LSPClient } from "./client" | 3 | 5 | import path from "path" | @@ -6,13 +8,12 @@ import z from "zod" | 6 | 8 | import { Config } from "../config/config" | 7 | 9 | import { spawn } from "child_process" | 8 | 10 | import { Instance } from "../project/instance" | 9 | | -import { Bus } from "../bus" | 10 | 11 |
| 11 | 12 | export namespace LSP { | 12 | 13 | const log = Log.create({ service: "lsp" }) | 13 | 14 |
| 14 | 15 | export const Event = { | 15 | | - Updated: Bus.event("lsp.updated", z.object({})), | | 16 | + Updated: BusEvent.define("lsp.updated", z.object({})), | 16 | 17 | } | 17 | 18 |
| 18 | 19 | export const Range = z | |
|