|
| 1 | +// --------------------------------------------------------------------------- |
| 2 | +// core-tools plugin |
| 3 | +// |
| 4 | +// Built-in plugin that contributes agent-facing static tools for managing |
| 5 | +// executor-level primitives (scopes, secrets). Auto-registered by |
| 6 | +// `createExecutor`, so callers don't need to wire it in. |
| 7 | +// |
| 8 | +// Today's surface: |
| 9 | +// - scopes.list — enumerate visible scopes by name |
| 10 | +// - secrets.list — list visible secrets (collapsed across scopes) |
| 11 | +// - secrets.create — agent supplies scope + name; tool returns a URL |
| 12 | +// that opens the existing /secrets web page with the |
| 13 | +// add-modal pre-filled. User enters the value in |
| 14 | +// that form (writes via the existing secrets HTTP |
| 15 | +// endpoint). Agent confirms by calling secrets.list. |
| 16 | +// |
| 17 | +// No elicitation suspension, no cross-request coordination. Works on |
| 18 | +// Cloudflare Workers because the tool's return value is just a URL. |
| 19 | +// |
| 20 | +// The agent never sees plaintext secret values. The agent never picks a |
| 21 | +// default scope on the user's behalf — every write tool requires an |
| 22 | +// explicit scope name, and `scopes.list` exists so the agent can |
| 23 | +// enumerate options before asking. |
| 24 | +// --------------------------------------------------------------------------- |
| 25 | + |
| 26 | +import { Effect, Schema } from "effect"; |
| 27 | + |
| 28 | +import { definePlugin, tool } from "./plugin"; |
| 29 | + |
| 30 | +// --------------------------------------------------------------------------- |
| 31 | +// Tool input/output schemas |
| 32 | +// --------------------------------------------------------------------------- |
| 33 | + |
| 34 | +const ScopesListOutput = Schema.Struct({ |
| 35 | + scopes: Schema.Array( |
| 36 | + Schema.Struct({ |
| 37 | + name: Schema.String, |
| 38 | + }), |
| 39 | + ), |
| 40 | +}); |
| 41 | + |
| 42 | +const SecretsListOutput = Schema.Struct({ |
| 43 | + secrets: Schema.Array( |
| 44 | + Schema.Struct({ |
| 45 | + id: Schema.String, |
| 46 | + name: Schema.String, |
| 47 | + provider: Schema.String, |
| 48 | + }), |
| 49 | + ), |
| 50 | +}); |
| 51 | + |
| 52 | +const SecretsCreateInput = Schema.Struct({ |
| 53 | + /** Display name shown in the secrets UI and used to reference this |
| 54 | + * secret in subsequent tool calls. */ |
| 55 | + name: Schema.String, |
| 56 | + /** Name of the scope (from `scopes.list`) that should own this |
| 57 | + * secret. Required — there is no default. */ |
| 58 | + scope: Schema.String, |
| 59 | + /** Optional provider override. If omitted, the executor picks the |
| 60 | + * first writable provider in registration order. */ |
| 61 | + provider: Schema.optional(Schema.String), |
| 62 | +}); |
| 63 | + |
| 64 | +const SecretsCreateOutput = Schema.Struct({ |
| 65 | + /** Pre-allocated id the secret will receive when the user submits the |
| 66 | + * form. The agent can pass this to other tools that need a secret |
| 67 | + * reference; it materializes in `secrets.list` once the user saves. */ |
| 68 | + id: Schema.String, |
| 69 | + /** URL to hand to the user. Opens the /secrets page with the add |
| 70 | + * modal pre-filled with name, scope, and the pre-allocated id. */ |
| 71 | + url: Schema.String, |
| 72 | +}); |
| 73 | + |
| 74 | +const ScopesListOutputStd = Schema.toStandardSchemaV1( |
| 75 | + Schema.toStandardJSONSchemaV1(ScopesListOutput), |
| 76 | +); |
| 77 | +const SecretsListOutputStd = Schema.toStandardSchemaV1( |
| 78 | + Schema.toStandardJSONSchemaV1(SecretsListOutput), |
| 79 | +); |
| 80 | +const SecretsCreateInputStd = Schema.toStandardSchemaV1( |
| 81 | + Schema.toStandardJSONSchemaV1(SecretsCreateInput), |
| 82 | +); |
| 83 | +const SecretsCreateOutputStd = Schema.toStandardSchemaV1( |
| 84 | + Schema.toStandardJSONSchemaV1(SecretsCreateOutput), |
| 85 | +); |
| 86 | + |
| 87 | +// --------------------------------------------------------------------------- |
| 88 | +// Options |
| 89 | +// --------------------------------------------------------------------------- |
| 90 | + |
| 91 | +export interface CoreToolsPluginOptions { |
| 92 | + /** Base URL of the executor's web UI. Used to build the URL handed to |
| 93 | + * the user for secret-value entry, e.g. `${webBaseUrl}/secrets?...`. |
| 94 | + * If omitted, secrets.create is registered but will fail at invoke |
| 95 | + * time — the host must supply a URL it can route back to. */ |
| 96 | + readonly webBaseUrl?: string; |
| 97 | +} |
| 98 | + |
| 99 | +// --------------------------------------------------------------------------- |
| 100 | +// Plugin |
| 101 | +// --------------------------------------------------------------------------- |
| 102 | + |
| 103 | +export const coreToolsPlugin = definePlugin((options: CoreToolsPluginOptions = {}) => ({ |
| 104 | + id: "core-tools" as const, |
| 105 | + packageName: "@executor-js/sdk/core-tools", |
| 106 | + storage: () => ({}), |
| 107 | + extension: () => ({}), |
| 108 | + |
| 109 | + staticSources: () => [ |
| 110 | + { |
| 111 | + id: "core-tools", |
| 112 | + kind: "executor", |
| 113 | + name: "Executor", |
| 114 | + tools: [ |
| 115 | + tool({ |
| 116 | + name: "scopes.list", |
| 117 | + description: |
| 118 | + "List the scopes visible to this executor. Use this before any tool that takes a `scope` argument so you can ask the user which scope to use.", |
| 119 | + outputSchema: ScopesListOutputStd, |
| 120 | + execute: (_args, { ctx }) => |
| 121 | + Effect.succeed({ |
| 122 | + scopes: ctx.scopes.map((s) => ({ name: s.name })), |
| 123 | + }), |
| 124 | + }), |
| 125 | + |
| 126 | + tool({ |
| 127 | + name: "secrets.list", |
| 128 | + description: |
| 129 | + "List secrets visible to this executor. Returns id, display name, and provider — never values. Use the returned id when other tools ask for a secret reference.", |
| 130 | + outputSchema: SecretsListOutputStd, |
| 131 | + execute: (_args, { ctx }) => |
| 132 | + Effect.gen(function* () { |
| 133 | + const refs = yield* ctx.secrets.list(); |
| 134 | + return { |
| 135 | + secrets: refs.map((r) => ({ |
| 136 | + id: r.id, |
| 137 | + name: r.name, |
| 138 | + provider: r.provider, |
| 139 | + })), |
| 140 | + }; |
| 141 | + }), |
| 142 | + }), |
| 143 | + |
| 144 | + tool({ |
| 145 | + name: "secrets.create", |
| 146 | + description: |
| 147 | + "Create a new secret. Returns a URL the user should open to enter the value securely; the agent never sees plaintext. The secret materializes once the user submits the form — confirm by calling `secrets.list` and looking for the returned id.", |
| 148 | + inputSchema: SecretsCreateInputStd, |
| 149 | + outputSchema: SecretsCreateOutputStd, |
| 150 | + execute: (input, { ctx }) => |
| 151 | + Effect.gen(function* () { |
| 152 | + const webBaseUrl = options.webBaseUrl; |
| 153 | + if (!webBaseUrl) { |
| 154 | + return yield* Effect.die( |
| 155 | + new Error( |
| 156 | + "core-tools secrets.create requires webBaseUrl. Pass it to coreToolsPlugin({ webBaseUrl }) at executor construction.", |
| 157 | + ), |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + const targetScope = ctx.scopes.find((s) => s.name === input.scope); |
| 162 | + if (!targetScope) { |
| 163 | + return yield* Effect.die( |
| 164 | + new Error( |
| 165 | + `secrets.create: unknown scope "${input.scope}". Call scopes.list to see valid names.`, |
| 166 | + ), |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + const secretId = crypto.randomUUID(); |
| 171 | + |
| 172 | + const url = new URL(`${webBaseUrl.replace(/\/$/, "")}/secrets`); |
| 173 | + // Page reads these and opens the add modal pre-filled. |
| 174 | + // Final value is collected from the user and written via |
| 175 | + // the existing /scopes/:id/secrets POST. The presence of |
| 176 | + // `name` is the open-modal signal (no separate flag). |
| 177 | + url.searchParams.set("scope", String(targetScope.id)); |
| 178 | + url.searchParams.set("name", input.name); |
| 179 | + url.searchParams.set("secretId", secretId); |
| 180 | + if (input.provider) url.searchParams.set("provider", input.provider); |
| 181 | + |
| 182 | + return { id: secretId, url: url.toString() }; |
| 183 | + }), |
| 184 | + }), |
| 185 | + ], |
| 186 | + }, |
| 187 | + ], |
| 188 | + })); |
| 189 | + |
| 190 | +export default coreToolsPlugin; |
0 commit comments