| 6229 | }); |
| 6230 | |
| 6231 | const execute = ( |
| 6232 | address: ToolAddress, |
| 6233 | args: unknown, |
| 6234 | options?: InvokeOptions, |
| 6235 | ): Effect.Effect<unknown, ExecuteError> => { |
| 6236 | const handler = pickHandler(options); |
| 6237 | return Effect.gen(function* () { |
| 6238 | // oxlint-disable executor/no-instanceof-error, executor/no-unknown-error-message, executor/no-manual-tag-check -- boundary: normalize arbitrary unknown plugin failures into a human-readable message for ToolInvocationError/telemetry |
| 6239 | const formatInvocationCauseMessage = (cause: unknown): string => { |
| 6240 | if (cause instanceof Error && cause.message.length > 0) return cause.message; |
| 6241 | // Non-Error / empty-message causes: `String(plainObject)` renders |
| 6242 | // "[object Object]", which is what telemetry then shows as the only |
| 6243 | // label for the failure. Prefer the tag, else stringify structurally. |
| 6244 | if (typeof cause === "object" && cause !== null) { |
| 6245 | const tag = (cause as { readonly _tag?: unknown })._tag; |
| 6246 | if (typeof tag === "string") return tag; |
| 6247 | return Inspectable.toStringUnknown(cause, 0); |
| 6248 | } |
| 6249 | return String(cause); |
| 6250 | }; |
| 6251 | // oxlint-enable executor/no-instanceof-error, executor/no-unknown-error-message, executor/no-manual-tag-check |
| 6252 | const wrapInvocationError = <A, E>( |
| 6253 | effect: Effect.Effect<A, E>, |
| 6254 | ): Effect.Effect<A, ToolInvocationError> => |
| 6255 | effect.pipe( |
| 6256 | Effect.mapError( |
| 6257 | (cause) => |
| 6258 | new ToolInvocationError({ |
| 6259 | address, |
| 6260 | message: formatInvocationCauseMessage(cause), |
| 6261 | cause, |
| 6262 | }), |
| 6263 | ), |
| 6264 | ); |
| 6265 | |
| 6266 | // Static path — O(1) map lookup for plugin-contributed static tools |
| 6267 | // (core-tools, plugin executor namespaces). Addressed by their fqid, |
| 6268 | // not the 5-segment dynamic form. |
| 6269 | const staticEntry = staticTools.get(String(address)); |
| 6270 | if (staticEntry) { |
| 6271 | const policyRules = yield* listActivePolicyRuleSet(); |
| 6272 | const policy = yield* resolvePolicyFromRuleSet( |
| 6273 | String(address), |
| 6274 | policyRules, |
| 6275 | staticEntry.tool.annotations?.requiresApproval, |
| 6276 | ); |
| 6277 | if (policy.action === "block") { |
| 6278 | return yield* new ToolBlockedError({ |
| 6279 | address, |
| 6280 | pattern: policy.pattern ?? "*", |
| 6281 | }); |
| 6282 | } |
| 6283 | yield* enforceApproval(staticEntry.tool.annotations, address, args, policy, handler); |
| 6284 | return yield* wrapInvocationError( |
| 6285 | staticEntry.tool.handler({ |
| 6286 | ctx: staticEntry.ctx, |
| 6287 | args, |
| 6288 | elicit: buildElicit(address, args, handler), |