| 4336 | }); |
| 4337 | |
| 4338 | const execute = ( |
| 4339 | address: ToolAddress, |
| 4340 | args: unknown, |
| 4341 | options?: InvokeOptions, |
| 4342 | ): Effect.Effect<unknown, ExecuteError> => { |
| 4343 | const handler = pickHandler(options); |
| 4344 | return Effect.gen(function* () { |
| 4345 | // 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 |
| 4346 | const formatInvocationCauseMessage = (cause: unknown): string => { |
| 4347 | if (cause instanceof Error && cause.message.length > 0) return cause.message; |
| 4348 | // Non-Error / empty-message causes: `String(plainObject)` renders |
| 4349 | // "[object Object]", which is what telemetry then shows as the only |
| 4350 | // label for the failure. Prefer the tag, else stringify structurally. |
| 4351 | if (typeof cause === "object" && cause !== null) { |
| 4352 | const tag = (cause as { readonly _tag?: unknown })._tag; |
| 4353 | if (typeof tag === "string") return tag; |
| 4354 | return Inspectable.toStringUnknown(cause, 0); |
| 4355 | } |
| 4356 | return String(cause); |
| 4357 | }; |
| 4358 | // oxlint-enable executor/no-instanceof-error, executor/no-unknown-error-message, executor/no-manual-tag-check |
| 4359 | const wrapInvocationError = <A, E>( |
| 4360 | effect: Effect.Effect<A, E>, |
| 4361 | ): Effect.Effect<A, ToolInvocationError> => |
| 4362 | effect.pipe( |
| 4363 | Effect.mapError( |
| 4364 | (cause) => |
| 4365 | new ToolInvocationError({ |
| 4366 | address, |
| 4367 | message: formatInvocationCauseMessage(cause), |
| 4368 | cause, |
| 4369 | }), |
| 4370 | ), |
| 4371 | ); |
| 4372 | |
| 4373 | // Static path — O(1) map lookup for plugin-contributed static tools |
| 4374 | // (core-tools, plugin executor namespaces). Addressed by their fqid, |
| 4375 | // not the 5-segment dynamic form. |
| 4376 | const staticEntry = staticTools.get(String(address)); |
| 4377 | if (staticEntry) { |
| 4378 | const policyRules = yield* listActivePolicyRuleSet(); |
| 4379 | const policy = yield* resolvePolicyFromRuleSet( |
| 4380 | String(address), |
| 4381 | policyRules, |
| 4382 | staticEntry.tool.annotations?.requiresApproval, |
| 4383 | ); |
| 4384 | if (policy.action === "block") { |
| 4385 | return yield* new ToolBlockedError({ |
| 4386 | address, |
| 4387 | pattern: policy.pattern ?? "*", |
| 4388 | }); |
| 4389 | } |
| 4390 | yield* enforceApproval(staticEntry.tool.annotations, address, args, policy, handler); |
| 4391 | return yield* wrapInvocationError( |
| 4392 | staticEntry.tool.handler({ |
| 4393 | ctx: staticEntry.ctx, |
| 4394 | args, |
| 4395 | elicit: buildElicit(address, args, handler), |