Repository navigation
Expand file tree
/
Copy pathcommand.ts
More file actions
109 lines (96 loc) · 3.65 KB
/
Copy pathcommand.ts
File metadata and controls
109 lines (96 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
export * as Command from "./command.js"
import { Command } from "@opencode/schema/command"
import type { PromptInput } from "@opencode/schema/prompt-input"
import type { Session } from "@opencode/schema/session"
import type { SessionInbox } from "@opencode/schema/session-inbox"
import { makeLocationNode } from "@opencode/util/effect/app-node"
import { Context, Effect, Layer, Schema } from "effect"
import { Bus } from "./bus.js"
import { State } from "./state.js"
export const Info = Command.Info
export type Info = Command.Info
export { Event } from "@opencode/schema/command"
export interface Invocation {
readonly sessionID: Session.ID
readonly prompt: PromptInput.Prompt
readonly delivery: SessionInbox.Delivery
}
export interface Definition {
readonly name: string
readonly description?: string
readonly execute: (input: Invocation) => Effect.Effect<void, unknown>
}
export type Editor = {
add: (definition: Definition) => void
}
export class NotFoundError extends Schema.TaggedError<NotFoundError>()("Command.NotFoundError", {
command: Schema.String,
message: Schema.String,
}) {}
export class ExecutionError extends Schema.TaggedError<ExecutionError>()("Command.ExecutionError", {
command: Schema.String,
message: Schema.String,
}) {}
export interface Interface extends State.Transformable<Editor> {
readonly get: (name: string) => Effect.Effect<Info | undefined>
readonly list: () => Effect.Effect<Info[]>
readonly execute: (input: {
readonly name: string
readonly invocation: Invocation
}) => Effect.Effect<void, NotFoundError | ExecutionError>
}
export class Service extends Context.Service<Service, Interface>()("@opencode/Command") {}
export const layer = Layer.effect(
Service,
Effect.gen(function* () {
const bus = yield* Bus.Service
const info = (definition: Definition) =>
Info.make({
name: definition.name,
description: definition.description,
})
const state = State.create<Map<string, Definition>, Editor>({
name: "command",
initial: () => new Map(),
editor: (editor) => ({
add: (definition) => editor.set(definition.name, definition),
}),
notify: State.changes(
(commands) => Array.from(commands.values(), info),
() => bus.publish(Command.Event.Updated, {}).pipe(Effect.asVoid),
),
})
return Service.of({
reload: state.reload,
transform: state.transform,
get: Effect.fn("Command.get")((name) =>
Effect.sync(() => {
const definition = state.get().get(name)
return definition ? info(definition) : undefined
}),
),
list: Effect.fn("Command.list")(() => Effect.sync(() => Array.from(state.get().values(), info))),
execute: Effect.fn("Command.execute")(function* (input) {
const definition = state.get().get(input.name)
if (!definition)
return yield* new NotFoundError({ command: input.name, message: `Command not found: ${input.name}` })
return yield* definition.execute(input.invocation).pipe(
Effect.tapError((error) => Effect.logError("command execution failed", { command: input.name, error })),
Effect.mapError((error) => new ExecutionError({ command: input.name, message: errorMessage(error) })),
)
}),
})
}),
)
export const node = makeLocationNode({
service: Service,
layer,
deps: [Bus.node],
})
function errorMessage(error: unknown) {
if (error instanceof Error) return error.message
if (typeof error === "string") return error
if (error && typeof error === "object" && "message" in error && typeof error.message === "string")
return error.message
return "Command execution failed"
}