GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — opencode-workflows/src/plugin/setup.ts at main · rphang/opencode-workflows Expand file tree/ Copy pathsetup.tsMore file actions 281 lines (260 loc) · 12.2 KB / Copy pathsetup.tsFile metadata and controls281 lines (260 loc) · 12.2 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 // Plugin setup: registers the tools and commands, the live-tree RPC and activity tap (X11–X13), and // returns the cleanup that stops every run. import { existsSync, watch, type FSWatcher } from "node:fs" import { join } from "node:path" import type { Plugin } from "@opencode/plugin" import { listWorkflows, personalWorkflowsDir, projectWorkflowDirs, type RegistryOptions } from "../registry.ts" import { createOpencodeRunner } from "../opencode/runner.ts" import { createFreeformToolFilter } from "../opencode/freeform.ts" import { createPermissionAsserter, createUnattendedAskGuard } from "../opencode/permission-assert.ts" import { createJobTracker, type JobTracker } from "../opencode/background.ts" import { ChildLiveRegistry, idleHookEnabled, registerIdleHook } from "../opencode/idle.ts" import { createChildAskGuard, effectiveSessionRules } from "../opencode/permissions.ts" import { createSubmitRegistry, createSubmitTool, type SubmitRegistry } from "../opencode/submit.ts" import { RunStore } from "../store.ts" import type { AgentRunner } from "../types.ts" import { addCommands } from "./commands.ts" import { largeWorkflowThreshold, resolveSizeGuideline, sizeGuidelineAdvice } from "./guideline.ts" import { WorkflowHost } from "./host.ts" import { LiveRuntime } from "./rpc.ts" import { CONTROL_TOOL_NAME, createControlTool, createScriptTool, createWorkflowTool, SCRIPT_TOOL_NAME, WORKFLOW_TOOL_NAME, workflowToolDescription, } from "./tools.ts" export const PLUGIN_ID = "dynamic-workflows" export const DISABLE_ENV = "OPENCODE_DISABLE_WORKFLOWS" /** `0`/`false`/`no`/`off` makes the live tree's RPC read-only (X17). */ export const RPC_CONTROL_ENV = "OPENCODE_WORKFLOW_RPC_CONTROL" /** `0`/`false`/`no`/`off` registers no freeform `workflow_script` tool (X24). */ export const FREEFORM_ENV = "OPENCODE_WORKFLOW_FREEFORM" /** What the plugin hands each run's runner: the submit registry and live children are per instance. */ export interface RunnerInput { ctx: Plugin.Context parentSessionID: string parentAgent?: string runId: string registry: SubmitRegistry /** Read by this instance's experimental.idle hook (X22, X23). */ live: ChildLiveRegistry /** Background jobs the idle hook reports and the runner waits for (X31). */ jobs: JobTracker } /** Injection seams (tests) and host overrides. Everything is optional. */ export interface PluginDeps { /** P76: how long a scriptPath check inside a running script may wait (default UNATTENDED_ASSERT_TIMEOUT_MS). */ unattendedAssertTimeoutMs?: number env?: Record<string, string | undefined> /** Run data root (default: plugin option `dataDir`, then defaultDataRoot()). */ dataDir?: string store?: RunStore registryOptions?: RegistryOptions /** Replaces the opencode child-session runner (tests use FakeRunner). */ createRunner?: (input: RunnerInput) => AgentRunner /** Watch the saved-workflow directories and reload commands on change. Default true. */ watch?: boolean now?: () => number summaryThrottleMs?: number /** Parent-location keep-alive period while a run is active (default KEEP_ALIVE_MS; 0 disables). */ keepAliveMs?: number /** Live-tree delta interval (default 250 ms, X13). */ liveIntervalMs?: number } const TRUTHY = /^(1|true|yes|on)$/i const FALSY = /^(0|false|no|off)$/i /** X17: the RPC's `control` is on unless env OPENCODE_WORKFLOW_RPC_CONTROL is falsy or option `rpcControl` is false. */ export function rpcControlEnabled(env: Record<string, string | undefined>, options: Record<string, unknown> | undefined): boolean { const raw = env[RPC_CONTROL_ENV] if (raw !== undefined && FALSY.test(raw.trim())) return false const opt = options?.rpcControl return !(opt === false || (typeof opt === "string" && FALSY.test(opt.trim()))) } /** X24: `workflow_script` is registered unless env OPENCODE_WORKFLOW_FREEFORM is falsy or option `freeformTool` is false. */ export function freeformToolEnabled(env: Record<string, string | undefined>, options: Record<string, unknown> | undefined): boolean { const raw = env[FREEFORM_ENV] if (raw !== undefined && FALSY.test(raw.trim())) return false const opt = options?.freeformTool return !(opt === false || (typeof opt === "string" && FALSY.test(opt.trim()))) } /** P57: env OPENCODE_DISABLE_WORKFLOWS=1 (true/yes/on) or plugin option `disabled: true`. */ export function isDisabled(env: Record<string, string | undefined>, options: Record<string, unknown> | undefined): boolean { const raw = env[DISABLE_ENV] if (raw !== undefined && TRUTHY.test(raw.trim())) return true const opt = options?.disabled return opt === true || (typeof opt === "string" && TRUTHY.test(opt.trim())) } export async function setupPlugin(ctx: Plugin.Context, deps: PluginDeps = {}): Promise<Plugin.Cleanup | void> { const env = deps.env ?? process.env const options = (ctx.options ?? {}) as Record<string, unknown> if (isDisabled(env, options)) return const cwd = String(ctx.location.directory) const registryOptions: RegistryOptions = { env, ...deps.registryOptions } const dataDir = deps.dataDir ?? (typeof options.dataDir === "string" && options.dataDir ? options.dataDir : undefined) const store = deps.store ?? new RunStore(dataDir ? { root: dataDir } : {}) const submitRegistry = createSubmitRegistry(ctx.storage) const liveChildren = new ChildLiveRegistry() const jobTracker = createJobTracker(ctx.storage) const createRunner = deps.createRunner ?? ((input: RunnerInput) => createOpencodeRunner(input.ctx, { parentSessionID: input.parentSessionID, parentAgent: input.parentAgent, runId: input.runId, registry: input.registry, live: input.live, jobs: input.jobs, // P60: workflow agents can neither launch nor manage workflows. deniedTools: [WORKFLOW_TOOL_NAME, CONTROL_TOOL_NAME], })) const freeform = freeformToolEnabled(env, options) const sizeGuideline = resolveSizeGuideline(env, options) const sizeAdvice = sizeGuidelineAdvice(sizeGuideline) // Saved workflows appear both as /<name> commands and in the `workflow` tool description (P59), // so a change reloads both. const reloadCommands = async () => { await ctx.command.reload() await ctx.tool.reload?.() } // Live progress tree (X11–X13): activity from the event stream, views and events over ctx.rpc. const events = (ctx as any).event as { subscribe?: (options?: { signal?: AbortSignal }) => AsyncIterable<unknown> } | undefined const live = new LiveRuntime({ directory: cwd, subscribe: typeof events?.subscribe === "function" ? (signal) => events.subscribe!({ signal }) : undefined, intervalMs: deps.liveIntervalMs, control: rpcControlEnabled(env, options), }) // P76 (opencode 2.3.0): scriptPath approvals through opencode's own permission check and prompt. const assertPermission = createPermissionAsserter(ctx, { unattendedTimeoutMs: deps.unattendedAssertTimeoutMs }) const host = new WorkflowHost({ cwd, store, registryOptions, createRunner: ({ parentSessionID, parentAgent, runId }) => createRunner({ ctx, parentSessionID, parentAgent, runId, registry: submitRegistry, live: liveChildren, jobs: jobTracker }), notify: (input) => ctx.session.synthetic(input as any), onSaved: reloadCommands, now: deps.now, summaryThrottleMs: deps.summaryThrottleMs, largeWorkflowThreshold: largeWorkflowThreshold(sizeGuideline), // opencode evicts a location (disposing this plugin and its runs) after 60 min without session // events there. Renaming the parent to its current title is a no-op durable event that keeps it. keepAlive: async (sessionID) => { const info = (await ctx.session.get({ sessionID } as any)) as any if (typeof info?.title === "string") await ctx.session.update({ sessionID, title: info.title } as any) }, keepAliveMs: deps.keepAliveMs, // P76: scriptPath reads honour the caller's read/external_directory rules, like opencode's read tool. projectRoots: projectRoot(ctx), permissionRules: (sessionID, agent) => effectiveSessionRules(ctx, sessionID, agent), assertPermission, onEvent: live.onEvent, activity: live.activityFor, }) live.attach(host) const registrations: { dispose: () => Promise<void> }[] = [] const disposeAll = async () => { await host.dispose() await live.dispose() for (const r of registrations.splice(0)) { try { await r.dispose() } catch {} } } try { live.start() await live.register((ctx as any).rpc) // P63: an "ask" in a workflow child would wait forever (nobody sees that session): deny it instead. // Registered by every instance, so a worktree instance guards the children in its own location. const permissionHook = (ctx as any).permission?.hook as Plugin.Context["permission"]["hook"] | undefined if (typeof permissionHook === "function") { registrations.push(await permissionHook("evaluate", createChildAskGuard(ctx))) } // X22/X23: opencode 2.3+ runs `experimental.idle` before a session goes idle (2.0.15 never does). const idleHook = idleHookEnabled(env) ? await registerIdleHook(ctx, { registry: submitRegistry, live: liveChildren, jobs: jobTracker }) : undefined if (idleHook) registrations.push(idleHook) registrations.push( await ctx.tool.transform((editor) => { let saved: ReturnType<typeof listWorkflows> = [] try { saved = listWorkflows(cwd, registryOptions) } catch {} editor.add(createWorkflowTool(host, workflowToolDescription({ saved, sizeAdvice }))) if (freeform) editor.add(createScriptTool(host)) editor.add(createControlTool(host)) editor.add(createSubmitTool(submitRegistry)) }), ) // P76: scriptPath checks made outside a tool call (workflow() in a running script) never prompt. if (assertPermission) registrations.push(await ctx.permission.hook("evaluate", createUnattendedAskGuard())) // X24: models that cannot send freeform text would only see a JSON duplicate of `workflow {script}`. if (freeform && typeof (ctx.session as any).hook === "function") { registrations.push(await ctx.session.hook("context", createFreeformToolFilter(ctx, SCRIPT_TOOL_NAME))) } registrations.push( await ctx.command.transform((editor) => { addCommands(editor, { host, cwd, registryOptions, reload: reloadCommands, session: { synthetic: (input) => ctx.session.synthetic(input as any), prompt: (input) => ctx.session.prompt(input), }, }) }), ) } catch (e) { // Setup failed half-way: opencode gets no cleanup, so nothing registered so far may outlive it // (the RPC would answer for a host without tools; the activity tap would keep reading events). await disposeAll() throw e } const watchers = deps.watch === false ? [] : watchWorkflowDirs(cwd, registryOptions, reloadCommands) return async () => { for (const w of watchers) { try { w.close() } catch {} } await disposeAll() } } /** Reloads commands (debounced) when a saved-workflow directory changes (P53). Best effort. */ function watchWorkflowDirs(cwd: string, registryOptions: RegistryOptions, reload: () => Promise<void>): FSWatcher[] { const dirs = [...projectWorkflowDirs(cwd)] try { dirs.push( registryOptions.configHome ? join(registryOptions.configHome, "opencode", "workflows") : personalWorkflowsDir(registryOptions.env ?? process.env, registryOptions.home), ) } catch {} let timer: ReturnType<typeof setTimeout> | undefined const trigger = () => { if (timer) clearTimeout(timer) timer = setTimeout(() => void reload().catch(() => {}), 250) ;(timer as any).unref?.() } const out: FSWatcher[] = [] for (const d of dirs) { if (!existsSync(d)) continue try { const w = watch(d, { persistent: false }, trigger) w.on("error", () => {}) out.push(w) } catch {} } return out } /** The project (worktree) root, which opencode's file access also treats as internal. */ function projectRoot(ctx: Plugin.Context): string[] { const dir = (ctx.location as any)?.project?.directory return typeof dir === "string" && dir ? [dir] : [] } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |