|
| 1 | +/* |
| 2 | + * A third-party tool plugin, vendored as a worked example. It uses only the public plugin, |
| 3 | + * tool and sandbox surfaces, and default-exports one plugin object — what the loader imports. |
| 4 | + * |
| 5 | + * Two entries: one names the module, the next configures it by the package name. |
| 6 | + * |
| 7 | + * { "plugins": ["@acme/codework-tool-proc@1.2.0", |
| 8 | + * { "package": "@acme/codework-tool-proc", "options": { "limit": 40 } }] } |
| 9 | + * |
| 10 | + * In this repository the module entry is a path, because this example ships TypeScript and Node |
| 11 | + * refuses to strip types under `node_modules`. A plugin package published for real ships built |
| 12 | + * JavaScript; see the README. |
| 13 | + */ |
| 14 | +import { Plugin, Tool } from "@codeworksh/harness/effect"; |
| 15 | +import { SandboxIO } from "@codeworksh/harness/sandbox"; |
| 16 | +import { Effect, Schema } from "effect"; |
| 17 | + |
| 18 | +/** Rows to return when the entry carries no `limit`. */ |
| 19 | +const DEFAULT_LIMIT = 20; |
| 20 | +/** `ps` fields, printed headerless (`=`) so there is nothing to skip while parsing. */ |
| 21 | +const COMMAND = "ps -eo pid=,pcpu=,pmem=,args="; |
| 22 | +const ROW = /^\s*(\d+)\s+([\d.]+)\s+([\d.]+)\s+(.+)$/; |
| 23 | + |
| 24 | +/** |
| 25 | + * The plugin's own configuration block. The harness never inspects it, so every field is |
| 26 | + * checked here and a malformed one falls back rather than failing the boot. |
| 27 | + */ |
| 28 | +const readLimit = (options: Plugin.PluginOptions): number => { |
| 29 | + const limit = options["limit"]; |
| 30 | + return typeof limit === "number" && Number.isInteger(limit) && limit > 0 ? limit : DEFAULT_LIMIT; |
| 31 | +}; |
| 32 | + |
| 33 | +const ProcParams = Schema.Struct({ |
| 34 | + filter: Schema.optional( |
| 35 | + Schema.String.annotate({ description: "Only list processes whose command line contains this text." }), |
| 36 | + ), |
| 37 | +}); |
| 38 | + |
| 39 | +const ProcessRow = Schema.Struct({ |
| 40 | + pid: Schema.Finite, |
| 41 | + cpu: Schema.Finite, |
| 42 | + memory: Schema.Finite, |
| 43 | + command: Schema.String, |
| 44 | +}); |
| 45 | + |
| 46 | +const ProcSuccess = Schema.Struct({ |
| 47 | + processes: Schema.Array(ProcessRow), |
| 48 | + /** How many matched in total, which is more than `processes.length` when `limit` cut the list. */ |
| 49 | + matched: Schema.Finite, |
| 50 | +}); |
| 51 | + |
| 52 | +/** A non-zero `ps` is expected and model-visible: the model can retry with a different filter. */ |
| 53 | +class ProcFailed extends Schema.TaggedError<ProcFailed>()("ProcFailed", { |
| 54 | + exitCode: Schema.Finite, |
| 55 | + stderr: Schema.String, |
| 56 | +}) {} |
| 57 | + |
| 58 | +const parse = (stdout: string): ReadonlyArray<typeof ProcessRow.Type> => { |
| 59 | + const rows: Array<typeof ProcessRow.Type> = []; |
| 60 | + for (const line of stdout.split("\n")) { |
| 61 | + const match = ROW.exec(line); |
| 62 | + if (match === null) continue; |
| 63 | + const [, pid, cpu, memory, command] = match; |
| 64 | + rows.push({ |
| 65 | + pid: Number(pid), |
| 66 | + cpu: Number(cpu), |
| 67 | + memory: Number(memory), |
| 68 | + command: (command ?? "").trim(), |
| 69 | + }); |
| 70 | + } |
| 71 | + return rows; |
| 72 | +}; |
| 73 | + |
| 74 | +const table = (rows: ReadonlyArray<typeof ProcessRow.Type>): string => |
| 75 | + [ |
| 76 | + "PID CPU% MEM% COMMAND", |
| 77 | + ...rows.map( |
| 78 | + (row) => |
| 79 | + `${String(row.pid).padEnd(8)} ${row.cpu.toFixed(1).padStart(5)} ${row.memory |
| 80 | + .toFixed(1) |
| 81 | + .padStart(5)} ${row.command}`, |
| 82 | + ), |
| 83 | + ].join("\n"); |
| 84 | + |
| 85 | +export default Plugin.define({ |
| 86 | + id: "acme.tool.proc", |
| 87 | + setup: Effect.fn("ProcPlugin.setup")(function* (ctx, options) { |
| 88 | + // The sandbox's shell, not `node:child_process`: the tool lists processes wherever the |
| 89 | + // session actually runs — this machine, a container, or a remote sandbox. |
| 90 | + const shell = yield* SandboxIO.Shell; |
| 91 | + const limit = readLimit(options); |
| 92 | + ctx.plugin.tools.add( |
| 93 | + Tool.register( |
| 94 | + Tool.make({ |
| 95 | + name: "list_processes", |
| 96 | + label: "processes", |
| 97 | + promptSnippet: "List the processes running in the session's environment.", |
| 98 | + description: |
| 99 | + "List running processes with their PID, CPU and memory share, and command line. " + |
| 100 | + `Returns at most ${limit} rows, busiest first; pass \`filter\` to narrow by command line.`, |
| 101 | + parameters: ProcParams, |
| 102 | + success: ProcSuccess, |
| 103 | + failure: ProcFailed, |
| 104 | + encodeContent: (success) => |
| 105 | + success.processes.length === 0 |
| 106 | + ? [{ type: "text", text: "No matching processes." }] |
| 107 | + : [{ type: "text", text: table(success.processes) }], |
| 108 | + encodeFailureContent: (failure) => [{ type: "text", text: failure.stderr }], |
| 109 | + handler: (params) => |
| 110 | + Effect.gen(function* () { |
| 111 | + const result = yield* shell |
| 112 | + .exec(COMMAND) |
| 113 | + // A shell that cannot run at all is infrastructure, not something the |
| 114 | + // model can act on, so it becomes a defect rather than a tool failure. |
| 115 | + .pipe(Effect.catchTag("ShellError", (cause) => Effect.die(cause))); |
| 116 | + if (result.exitCode !== 0) { |
| 117 | + return yield* new ProcFailed({ exitCode: result.exitCode, stderr: result.stderr }); |
| 118 | + } |
| 119 | + const filter = params.filter?.toLowerCase(); |
| 120 | + const matched = parse(result.stdout).filter( |
| 121 | + (row) => filter === undefined || row.command.toLowerCase().includes(filter), |
| 122 | + ); |
| 123 | + const processes = [...matched].sort((a, b) => b.cpu - a.cpu).slice(0, limit); |
| 124 | + return { processes, matched: matched.length } satisfies typeof ProcSuccess.Type; |
| 125 | + }), |
| 126 | + }), |
| 127 | + ), |
| 128 | + ); |
| 129 | + }), |
| 130 | +}); |
0 commit comments