| 5728 | }); |
| 5729 | |
| 5730 | const execute = ( |
| 5731 | address: ToolAddress, |
| 5732 | args: unknown, |
| 5733 | options?: InvokeOptions, |
| 5734 | ): Effect.Effect<unknown, ExecuteError> => { |
| 5735 | const handler = pickHandler(options); |
| 5736 | return Effect.gen(function* () { |
| 5737 | // 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 |
| 5738 | const formatInvocationCauseMessage = (cause: unknown): string => { |
| 5739 | if (cause instanceof Error && cause.message.length > 0) return cause.message; |
| 5740 | // Non-Error / empty-message causes: `String(plainObject)` renders |
| 5741 | // "[object Object]", which is what telemetry then shows as the only |
| 5742 | // label for the failure. Prefer the tag, else stringify structurally. |
| 5743 | if (typeof cause === "object" && cause !== null) { |
| 5744 | const tag = (cause as { readonly _tag?: unknown })._tag; |
| 5745 | if (typeof tag === "string") return tag; |
| 5746 | return Inspectable.toStringUnknown(cause, 0); |
| 5747 | } |
| 5748 | return String(cause); |
| 5749 | }; |
| 5750 | // oxlint-enable executor/no-instanceof-error, executor/no-unknown-error-message, executor/no-manual-tag-check |
| 5751 | const wrapInvocationError = <A, E>( |
| 5752 | effect: Effect.Effect<A, E>, |
| 5753 | ): Effect.Effect<A, ToolInvocationError> => |
| 5754 | effect.pipe( |
| 5755 | Effect.mapError( |
| 5756 | (cause) => |
| 5757 | new ToolInvocationError({ |
| 5758 | address, |
| 5759 | message: formatInvocationCauseMessage(cause), |
| 5760 | cause, |
| 5761 | }), |
| 5762 | ), |
| 5763 | ); |
| 5764 | |
| 5765 | // Static path — O(1) map lookup for plugin-contributed static tools |
| 5766 | // (core-tools, plugin executor namespaces). Addressed by their fqid, |
| 5767 | // not the 5-segment dynamic form. |
| 5768 | const staticEntry = staticTools.get(String(address)); |
| 5769 | if (staticEntry) { |
| 5770 | const policyRules = yield* listActivePolicyRuleSet(); |
| 5771 | const policy = yield* resolvePolicyFromRuleSet( |
| 5772 | String(address), |
| 5773 | policyRules, |
| 5774 | staticEntry.tool.annotations?.requiresApproval, |
| 5775 | ); |
| 5776 | if (policy.action === "block") { |
| 5777 | return yield* new ToolBlockedError({ |
| 5778 | address, |
| 5779 | pattern: policy.pattern ?? "*", |
| 5780 | }); |
| 5781 | } |
| 5782 | yield* enforceApproval(staticEntry.tool.annotations, address, args, policy, handler); |
| 5783 | return yield* wrapInvocationError( |
| 5784 | staticEntry.tool.handler({ |
| 5785 | ctx: staticEntry.ctx, |
| 5786 | args, |
| 5787 | elicit: buildElicit(address, args, handler), |