| 6349 | }); |
| 6350 | |
| 6351 | const execute = ( |
| 6352 | address: ToolAddress, |
| 6353 | args: unknown, |
| 6354 | options?: InvokeOptions, |
| 6355 | ): Effect.Effect<unknown, ExecuteError> => { |
| 6356 | const handler = pickHandler(options); |
| 6357 | return Effect.gen(function* () { |
| 6358 | // 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 |
| 6359 | const formatInvocationCauseMessage = (cause: unknown): string => { |
| 6360 | if (cause instanceof Error && cause.message.length > 0) return cause.message; |
| 6361 | // Non-Error / empty-message causes: `String(plainObject)` renders |
| 6362 | // "[object Object]", which is what telemetry then shows as the only |
| 6363 | // label for the failure. Prefer the tag, else stringify structurally. |
| 6364 | if (typeof cause === "object" && cause !== null) { |
| 6365 | const tag = (cause as { readonly _tag?: unknown })._tag; |
| 6366 | if (typeof tag === "string") return tag; |
| 6367 | return Inspectable.toStringUnknown(cause, 0); |
| 6368 | } |
| 6369 | return String(cause); |
| 6370 | }; |
| 6371 | // oxlint-enable executor/no-instanceof-error, executor/no-unknown-error-message, executor/no-manual-tag-check |
| 6372 | const wrapInvocationError = <A, E>( |
| 6373 | effect: Effect.Effect<A, E>, |
| 6374 | ): Effect.Effect<A, ToolInvocationError> => |
| 6375 | effect.pipe( |
| 6376 | Effect.mapError( |
| 6377 | (cause) => |
| 6378 | new ToolInvocationError({ |
| 6379 | address, |
| 6380 | message: formatInvocationCauseMessage(cause), |
| 6381 | cause, |
| 6382 | }), |
| 6383 | ), |
| 6384 | ); |
| 6385 | |
| 6386 | // Static path — O(1) map lookup for plugin-contributed static tools |
| 6387 | // (core-tools, plugin executor namespaces). Addressed by their fqid, |
| 6388 | // not the 5-segment dynamic form. |
| 6389 | const staticEntry = staticTools.get(String(address)); |
| 6390 | if (staticEntry) { |
| 6391 | const policyRules = yield* listActivePolicyRuleSet(); |
| 6392 | const policy = yield* resolvePolicyFromRuleSet( |
| 6393 | String(address), |
| 6394 | policyRules, |
| 6395 | staticEntry.tool.annotations?.requiresApproval, |
| 6396 | ); |
| 6397 | if (policy.action === "block") { |
| 6398 | return yield* new ToolBlockedError({ |
| 6399 | address, |
| 6400 | pattern: policy.pattern ?? "*", |
| 6401 | }); |
| 6402 | } |
| 6403 | yield* enforceApproval(staticEntry.tool.annotations, address, args, policy, handler); |
| 6404 | return yield* wrapInvocationError( |
| 6405 | staticEntry.tool.handler({ |
| 6406 | ctx: staticEntry.ctx, |
| 6407 | args, |
| 6408 | elicit: buildElicit(address, args, handler), |