|
1 | 1 | /** |
2 | 2 | * OpenCode Plugin Types |
3 | 3 | * |
4 | | - * Minimal type definitions needed for the plugin. |
5 | | - * Based on @opencode-ai/plugin package types. |
| 4 | + * Re-exports the types we need directly from the official @opencode-ai/plugin |
| 5 | + * SDK so that any change to the SDK's type signatures (e.g. ToolContext.ask) |
| 6 | + * is caught at compile time rather than silently breaking at runtime. |
| 7 | + * |
| 8 | + * Only types that are NOT exported by the SDK are defined here. |
6 | 9 | */ |
7 | 10 |
|
8 | | -import { z } from 'zod'; |
9 | | - |
10 | | -// Simplified types from opencode SDK |
11 | | -export type Part = { |
12 | | - type: 'text' | 'image' | 'file' | 'tool_use' | 'tool_result'; |
13 | | - text?: string; |
14 | | - [key: string]: unknown; |
15 | | -}; |
16 | | - |
17 | | -export type UserMessage = { |
18 | | - id: string; |
19 | | - sessionID: string; |
20 | | - role: 'user'; |
21 | | - [key: string]: unknown; |
22 | | -}; |
23 | | - |
24 | | -export type Message = { |
25 | | - id: string; |
26 | | - sessionID: string; |
27 | | - role: 'user' | 'assistant'; |
28 | | - [key: string]: unknown; |
29 | | -}; |
| 11 | +// --------------------------------------------------------------------------- |
| 12 | +// Re-export everything from the official SDK |
| 13 | +// --------------------------------------------------------------------------- |
| 14 | + |
| 15 | +export type { |
| 16 | + PluginInput, |
| 17 | + Plugin, |
| 18 | + PluginModule, |
| 19 | + Hooks, |
| 20 | + ToolDefinition, |
| 21 | + ToolContext, |
| 22 | +} from '@opencode-ai/plugin'; |
| 23 | + |
| 24 | +// --------------------------------------------------------------------------- |
| 25 | +// BusEvent subtypes |
| 26 | +// |
| 27 | +// The SDK exports a single opaque `Event` type from @opencode-ai/sdk, but the |
| 28 | +// plugin needs to narrow on specific event types (session.compacted, |
| 29 | +// session.idle) that are not individually exported. These local types stay |
| 30 | +// here until the SDK exposes them directly. |
| 31 | +// --------------------------------------------------------------------------- |
30 | 32 |
|
31 | | -export type Model = { |
32 | | - providerID: string; |
33 | | - modelID: string; |
34 | | - [key: string]: unknown; |
35 | | -}; |
36 | | - |
37 | | -export type Project = { |
38 | | - id: string; |
39 | | - path: string; |
40 | | - [key: string]: unknown; |
41 | | -}; |
42 | | - |
43 | | -// Plugin input provided by opencode |
44 | | -export type PluginInput = { |
45 | | - client: unknown; // SDK client |
46 | | - project: Project; |
47 | | - directory: string; |
48 | | - worktree: string; |
49 | | - serverUrl: URL; |
50 | | - $: unknown; // BunShell |
51 | | -}; |
52 | | - |
53 | | -// Tool context for custom tools |
54 | | -import type { Effect } from 'effect'; |
55 | | - |
56 | | -export type ToolContext = { |
57 | | - sessionID: string; |
58 | | - messageID: string; |
59 | | - agent: string; |
60 | | - directory: string; |
61 | | - worktree: string; |
62 | | - abort: AbortSignal; |
63 | | - metadata(input: { title?: string; metadata?: Record<string, unknown> }): void; |
64 | | - ask(input: { |
65 | | - permission: string; |
66 | | - patterns: string[]; |
67 | | - always: string[]; |
68 | | - metadata: Record<string, unknown>; |
69 | | - }): Effect.Effect<void>; |
70 | | -}; |
71 | | - |
72 | | -// Tool definition |
73 | | -export type ToolDefinition = { |
74 | | - description: string; |
75 | | - args: z.ZodRawShape; |
76 | | - execute(args: unknown, context: ToolContext): Promise<string>; |
77 | | -}; |
78 | | - |
79 | | -// Minimal Event types from @opencode-ai/sdk needed for the event hook |
80 | 33 | export type SessionCompactedEvent = { |
81 | 34 | type: 'session.compacted'; |
82 | 35 | properties: { sessionID: string }; |
83 | 36 | }; |
| 37 | + |
84 | 38 | export type SessionIdleEvent = { |
85 | 39 | type: 'session.idle'; |
86 | 40 | properties: { sessionID: string }; |
87 | 41 | }; |
| 42 | + |
88 | 43 | export type OtherEvent = { |
89 | 44 | type: string; |
90 | 45 | properties: Record<string, unknown>; |
91 | 46 | }; |
92 | | -export type BusEvent = SessionCompactedEvent | SessionIdleEvent | OtherEvent; |
93 | | - |
94 | | -// All available hooks |
95 | | -export interface Hooks { |
96 | | - event?: (input: { event: BusEvent }) => Promise<void>; |
97 | | - config?: (input: unknown) => Promise<void>; |
98 | | - tool?: { |
99 | | - [key: string]: ToolDefinition; |
100 | | - }; |
101 | | - auth?: unknown; |
102 | | - |
103 | | - /** |
104 | | - * Called when a new message is received |
105 | | - */ |
106 | | - 'chat.message'?: ( |
107 | | - input: { |
108 | | - sessionID: string; |
109 | | - agent?: string; |
110 | | - model?: { providerID: string; modelID: string }; |
111 | | - messageID?: string; |
112 | | - variant?: string; |
113 | | - }, |
114 | | - output: { message: UserMessage; parts: Part[] } |
115 | | - ) => Promise<void>; |
116 | | - |
117 | | - /** |
118 | | - * Modify parameters sent to LLM |
119 | | - */ |
120 | | - 'chat.params'?: ( |
121 | | - input: { |
122 | | - sessionID: string; |
123 | | - agent: string; |
124 | | - model: Model; |
125 | | - provider: unknown; |
126 | | - message: UserMessage; |
127 | | - }, |
128 | | - output: { |
129 | | - temperature: number; |
130 | | - topP: number; |
131 | | - topK: number; |
132 | | - options: Record<string, unknown>; |
133 | | - } |
134 | | - ) => Promise<void>; |
135 | | - |
136 | | - 'chat.headers'?: ( |
137 | | - input: { |
138 | | - sessionID: string; |
139 | | - agent: string; |
140 | | - model: Model; |
141 | | - provider: unknown; |
142 | | - message: UserMessage; |
143 | | - }, |
144 | | - output: { headers: Record<string, string> } |
145 | | - ) => Promise<void>; |
146 | | - |
147 | | - 'permission.ask'?: ( |
148 | | - input: unknown, |
149 | | - output: { status: 'ask' | 'deny' | 'allow' } |
150 | | - ) => Promise<void>; |
151 | | - |
152 | | - 'command.execute.before'?: ( |
153 | | - input: { command: string; sessionID: string; arguments: string }, |
154 | | - output: { parts: Part[] } |
155 | | - ) => Promise<void>; |
156 | | - |
157 | | - 'tool.execute.before'?: ( |
158 | | - input: { tool: string; sessionID: string; callID: string }, |
159 | | - output: { args: Record<string, unknown> } |
160 | | - ) => Promise<void>; |
161 | | - |
162 | | - 'shell.env'?: ( |
163 | | - input: { cwd: string; sessionID?: string; callID?: string }, |
164 | | - output: { env: Record<string, string> } |
165 | | - ) => Promise<void>; |
166 | 47 |
|
167 | | - 'tool.execute.after'?: ( |
168 | | - input: { |
169 | | - tool: string; |
170 | | - sessionID: string; |
171 | | - callID: string; |
172 | | - args: unknown; |
173 | | - }, |
174 | | - output: { |
175 | | - title: string; |
176 | | - output: string; |
177 | | - metadata: unknown; |
178 | | - } |
179 | | - ) => Promise<void>; |
180 | | - |
181 | | - 'experimental.chat.messages.transform'?: ( |
182 | | - input: Record<string, never>, |
183 | | - output: { |
184 | | - messages: { |
185 | | - info: Message; |
186 | | - parts: Part[]; |
187 | | - }[]; |
188 | | - } |
189 | | - ) => Promise<void>; |
190 | | - |
191 | | - 'experimental.chat.system.transform'?: ( |
192 | | - input: { sessionID?: string; model: Model }, |
193 | | - output: { |
194 | | - system: string[]; |
195 | | - } |
196 | | - ) => Promise<void>; |
197 | | - |
198 | | - /** |
199 | | - * Called before session compaction starts. Allows plugins to customize |
200 | | - * the compaction prompt. |
201 | | - */ |
202 | | - 'experimental.session.compacting'?: ( |
203 | | - input: { sessionID: string }, |
204 | | - output: { context: string[]; prompt?: string } |
205 | | - ) => Promise<void>; |
206 | | - |
207 | | - 'experimental.text.complete'?: ( |
208 | | - input: { sessionID: string; messageID: string; partID: string }, |
209 | | - output: { text: string } |
210 | | - ) => Promise<void>; |
211 | | - |
212 | | - /** |
213 | | - * Modify tool definitions (description and parameters) sent to LLM |
214 | | - */ |
215 | | - 'tool.definition'?: ( |
216 | | - input: { toolID: string }, |
217 | | - output: { description: string; parameters: unknown } |
218 | | - ) => Promise<void>; |
219 | | -} |
220 | | - |
221 | | -// Plugin function signature |
222 | | -export type Plugin = (input: PluginInput) => Promise<Hooks>; |
223 | | - |
224 | | -// Plugin module structure expected by opencode |
225 | | -export type PluginModule = { |
226 | | - id?: string; |
227 | | - server: Plugin; |
228 | | - tui?: never; |
229 | | -}; |
| 48 | +export type BusEvent = SessionCompactedEvent | SessionIdleEvent | OtherEvent; |
0 commit comments