| 6441 | }); |
| 6442 | |
| 6443 | const execute = ( |
| 6444 | address: ToolAddress, |
| 6445 | args: unknown, |
| 6446 | options?: InvokeOptions, |
| 6447 | ): Effect.Effect<unknown, ExecuteError> => { |
| 6448 | const handler = pickHandler(options); |
| 6449 | return Effect.gen(function* () { |
| 6450 | // 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 |
| 6451 | const formatInvocationCauseMessage = (cause: unknown): string => { |
| 6452 | if (cause instanceof Error && cause.message.length > 0) return cause.message; |
| 6453 | // Non-Error / empty-message causes: `String(plainObject)` renders |
| 6454 | // "[object Object]", which is what telemetry then shows as the only |
| 6455 | // label for the failure. Prefer the tag, else stringify structurally. |
| 6456 | if (typeof cause === "object" && cause !== null) { |
| 6457 | const tag = (cause as { readonly _tag?: unknown })._tag; |
| 6458 | if (typeof tag === "string") return tag; |
| 6459 | return Inspectable.toStringUnknown(cause, 0); |
| 6460 | } |
| 6461 | return String(cause); |
| 6462 | }; |
| 6463 | // oxlint-enable executor/no-instanceof-error, executor/no-unknown-error-message, executor/no-manual-tag-check |
| 6464 | const wrapInvocationError = <A, E>( |
| 6465 | effect: Effect.Effect<A, E>, |
| 6466 | ): Effect.Effect<A, ToolInvocationError> => |
| 6467 | effect.pipe( |
| 6468 | Effect.mapError( |
| 6469 | (cause) => |
| 6470 | new ToolInvocationError({ |
| 6471 | address, |
| 6472 | message: formatInvocationCauseMessage(cause), |
| 6473 | cause, |
| 6474 | }), |
| 6475 | ), |
| 6476 | ); |
| 6477 | |
| 6478 | // Static path — O(1) map lookup for plugin-contributed static tools |
| 6479 | // (core-tools, plugin executor namespaces). Addressed by their fqid, |
| 6480 | // not the 5-segment dynamic form. |
| 6481 | const staticEntry = staticTools.get(String(address)); |
| 6482 | if (staticEntry) { |
| 6483 | const policyRules = yield* listActivePolicyRuleSet(); |
| 6484 | const policy = yield* resolvePolicyFromRuleSet( |
| 6485 | String(address), |
| 6486 | policyRules, |
| 6487 | staticEntry.tool.annotations?.requiresApproval, |
| 6488 | ); |
| 6489 | if (policy.action === "block") { |
| 6490 | return yield* new ToolBlockedError({ |
| 6491 | address, |
| 6492 | pattern: policy.pattern ?? "*", |
| 6493 | }); |
| 6494 | } |
| 6495 | yield* enforceApproval(staticEntry.tool.annotations, address, args, policy, handler); |
| 6496 | return yield* wrapInvocationError( |
| 6497 | staticEntry.tool.handler({ |
| 6498 | ctx: staticEntry.ctx, |
| 6499 | args, |
| 6500 | elicit: buildElicit(address, args, handler), |