|
| 1 | +import { Effect, Schema } from "effect" |
| 2 | +import { Monitor as MonitorSchema } from "@opencode-ai/schema/monitor" |
| 3 | +import { Monitor } from "@opencode-ai/core/monitor" |
| 4 | +import { PositiveInt } from "@opencode-ai/core/schema" |
| 5 | +import * as Tool from "./tool" |
| 6 | + |
| 7 | +// V1 bridge for the V2 Monitor tools (packages/core/src/tool/monitor.ts): the live serving |
| 8 | +// path is this legacy registry (see registry.ts's builtin list), which the V2 registration in |
| 9 | +// core/tool/builtins.ts never reaches -- Sean's own live gate walk found this directly (not |
| 10 | +// filed as feedback). Delegates to the |
| 11 | +// same Monitor.Service the V2 tool uses; no duplicated monitor logic, only the tool-shape bridge. |
| 12 | +// |
| 13 | +// Plugin sources and the plugin condition are excluded, mirroring the V2 tool: neither is |
| 14 | +// executable yet (MonitorCondition.evaluate's "plugin" case always reports not-triggered). |
| 15 | +export const CreateParameters = Schema.Struct({ |
| 16 | + title: Schema.String, |
| 17 | + command: Schema.String, |
| 18 | + cwd: Schema.String.pipe(Schema.optional), |
| 19 | + intervalMs: PositiveInt, |
| 20 | + timeoutMs: PositiveInt, |
| 21 | + condition: Schema.Union([MonitorSchema.ExitCodeCondition, MonitorSchema.RegexCondition, MonitorSchema.JsonCondition]), |
| 22 | + maxAttempts: PositiveInt.pipe(Schema.optional), |
| 23 | + outputPolicy: MonitorSchema.OutputPolicy.pipe(Schema.optional), |
| 24 | +}).annotate({ |
| 25 | + description: |
| 26 | + "Declare a command monitor: a shell command re-run on an interval, checked against a condition (exit code, regex, or JSON path) until it triggers or maxAttempts is reached. Runs unattended: if the command's permission is configured to ask for approval rather than allow, checks are refused (not silently skipped) until it is set to allow -- monitors cannot prompt for approval.", |
| 27 | +}) |
| 28 | + |
| 29 | +export const MonitorCreateTool = Tool.define( |
| 30 | + "monitor_create", |
| 31 | + Effect.gen(function* () { |
| 32 | + const monitor = yield* Monitor.Service |
| 33 | + |
| 34 | + return { |
| 35 | + description: |
| 36 | + "Declare a command monitor: a shell command re-run on an interval, checked against a condition (exit code, regex, or JSON path) until it triggers or maxAttempts is reached. Runs unattended: if the command's permission is configured to ask for approval rather than allow, checks are refused (not silently skipped) until it is set to allow -- monitors cannot prompt for approval.", |
| 37 | + parameters: CreateParameters, |
| 38 | + execute: (params: Schema.Schema.Type<typeof CreateParameters>, ctx: Tool.Context) => |
| 39 | + Effect.gen(function* () { |
| 40 | + yield* ctx.ask({ |
| 41 | + permission: "monitor_create", |
| 42 | + patterns: ["*"], |
| 43 | + always: ["*"], |
| 44 | + metadata: { command: params.command }, |
| 45 | + }) |
| 46 | + const info = yield* monitor.create({ |
| 47 | + sessionID: ctx.sessionID, |
| 48 | + title: params.title, |
| 49 | + source: { type: "command", command: params.command, cwd: params.cwd }, |
| 50 | + intervalMs: params.intervalMs, |
| 51 | + timeoutMs: params.timeoutMs, |
| 52 | + condition: params.condition, |
| 53 | + maxAttempts: params.maxAttempts, |
| 54 | + outputPolicy: params.outputPolicy ?? {}, |
| 55 | + }) |
| 56 | + return { |
| 57 | + title: `Monitor declared: ${info.title}`, |
| 58 | + output: JSON.stringify({ monitor: info }, null, 2), |
| 59 | + metadata: { monitorID: info.id }, |
| 60 | + } |
| 61 | + }).pipe(Effect.orDie), |
| 62 | + } |
| 63 | + }), |
| 64 | +) |
| 65 | + |
| 66 | +export const MonitorListParameters = Schema.Struct({}) |
| 67 | + |
| 68 | +export const MonitorListTool = Tool.define( |
| 69 | + "monitor_list", |
| 70 | + Effect.gen(function* () { |
| 71 | + const monitor = yield* Monitor.Service |
| 72 | + |
| 73 | + return { |
| 74 | + description: "List monitors declared in this session, with their current status.", |
| 75 | + parameters: MonitorListParameters, |
| 76 | + execute: (_params: Schema.Schema.Type<typeof MonitorListParameters>, ctx: Tool.Context) => |
| 77 | + Effect.gen(function* () { |
| 78 | + const monitors = yield* monitor.list(ctx.sessionID) |
| 79 | + return { |
| 80 | + title: `${monitors.length} monitor${monitors.length === 1 ? "" : "s"}`, |
| 81 | + output: JSON.stringify({ monitors }, null, 2), |
| 82 | + metadata: { count: monitors.length }, |
| 83 | + } |
| 84 | + }).pipe(Effect.orDie), |
| 85 | + } |
| 86 | + }), |
| 87 | +) |
0 commit comments