@rafads/execution 1.5.38 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -533,7 +533,7 @@ var buildExecuteDescription = (executor) => Effect2.gen(function* () {
|
|
|
533
533
|
"",
|
|
534
534
|
'Before writing code, call `skills({ name: "execute" })` for the workflow on how to use this tool.',
|
|
535
535
|
"",
|
|
536
|
-
'When
|
|
536
|
+
'When a task explicitly calls for an external service capability \u2014 sending email, a database, auth, payments, SMS, DNS, hosting, storage, search \u2014 do not pick a provider from memory or send the user off to sign up somewhere: press the button. `await tools.use({ need: "transactional email" })` returns a wired, working integration in one call (or one remaining human credential click); connected providers resolve through the same press. `skills({ name: "resolve" })` has details and the manual stepwise flow.'
|
|
537
537
|
];
|
|
538
538
|
const inventory = formatIntegrationInventory(connections);
|
|
539
539
|
if (inventory.length > 0) {
|
|
@@ -1223,4 +1223,4 @@ export {
|
|
|
1223
1223
|
formatTtlDuration,
|
|
1224
1224
|
createExecutionEngine
|
|
1225
1225
|
};
|
|
1226
|
-
//# sourceMappingURL=chunk-
|
|
1226
|
+
//# sourceMappingURL=chunk-RBNHDS2R.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts","../src/tool-invoker.ts","../src/description.ts","../src/engine.ts","../src/use-need.ts"],"sourcesContent":["import * as Data from \"effect/Data\";\n\nexport class ExecutionToolError extends Data.TaggedError(\"ExecutionToolError\")<{\n readonly message: string;\n readonly cause?: unknown;\n}> {}\n\n// `CodeExecutionError` lives in `@rafads/codemode-core` — the `CodeExecutor`\n// interface uses it as the default error channel, so the runtime packages\n// can import the same class directly.\nexport { CodeExecutionError } from \"@rafads/codemode-core\";\n","import { Effect, Predicate } from \"effect\";\nimport * as Cause from \"effect/Cause\";\nimport type {\n Executor,\n InvokeOptions,\n Integration,\n ToolError,\n Tool,\n ToolSchemaView,\n} from \"@rafads/sdk/core\";\nimport {\n annotateToolResultOutcome,\n authToolFailure,\n isUserActionableError,\n isToolResult,\n ToolResult,\n ToolAddress,\n parseToolAddress,\n} from \"@rafads/sdk/core\";\nimport type { SandboxToolInvoker } from \"@rafads/codemode-core\";\nimport { ExecutionToolError } from \"./errors\";\n\nconst OPAQUE_DEFECT_MESSAGE = \"Internal tool error\";\nconst TOOL_DESCRIBE_SUGGESTION_LIMIT = 5;\nconst TOOL_ERROR_TYPESCRIPT =\n \"{ code: string; message: string; status?: number; details?: unknown; retryable?: boolean }\";\n// Present on HTTP-backed tools (OpenAPI): transport facts beside the payload\n// so callers can read pagination/rate-limit headers without the payload\n// being wrapped in an envelope.\nconst TOOL_HTTP_META_TYPESCRIPT = \"{ status: number; headers: { [k: string]: string; } }\";\nconst TOOL_FILE_TYPESCRIPT =\n '{ _tag: \"ToolFile\"; name?: string; mimeType: string; encoding: \"base64\"; data: string; byteLength: number; }';\n\nconst wrapOutputTypeScript = (outputTypeScript?: string): string =>\n `{ ok: true; data: ${outputTypeScript ?? \"unknown\"}; http?: ToolHttpMeta } | { ok: false; error: ToolError }`;\n\nconst withToolResultDefinitions = (\n definitions?: Record<string, string>,\n): Record<string, string> => ({\n ...(definitions ?? {}),\n ToolError: TOOL_ERROR_TYPESCRIPT,\n ToolHttpMeta: TOOL_HTTP_META_TYPESCRIPT,\n ToolFile: TOOL_FILE_TYPESCRIPT,\n});\n\nconst ADDRESS_PREFIX = \"tools.\";\n\n/**\n * Map a sandbox tool path to the executor's `execute` address.\n *\n * v2 dynamic tools are addressed `tools.<integration>.<owner>.<connection>.<tool>`.\n * The sandbox proxy strips the leading `tools.` (the proxy root), so a model\n * writing `tools.github.org.main.getRepo(args)` produces the path\n * `github.org.main.getRepo`. Re-prefix it so it parses as a 5-segment address.\n *\n * Plugin-contributed static tools (core-tools under `executor`, plugin executor\n * namespaces) are addressed by their fqid with no prefix; the executor resolves\n * those from its static map directly, so leave them untouched.\n */\nconst pathToAddress = (path: string): ToolAddress => {\n if (path.startsWith(ADDRESS_PREFIX)) return ToolAddress.make(path);\n if (parseToolAddress(`${ADDRESS_PREFIX}${path}`)) {\n return ToolAddress.make(`${ADDRESS_PREFIX}${path}`);\n }\n return ToolAddress.make(path);\n};\n\n/** Strip the proxy-root `tools.` prefix from a full address so it becomes the\n * sandbox-callable path the model writes after `tools.`. */\nconst addressToPath = (address: string): string =>\n address.startsWith(ADDRESS_PREFIX) ? address.slice(ADDRESS_PREFIX.length) : address;\n\ntype DescribedTool = {\n readonly path: string;\n readonly name: string;\n readonly description?: string;\n readonly inputTypeScript?: string;\n readonly outputTypeScript?: string;\n readonly typeScriptDefinitions?: Record<string, string>;\n /** Set when the path resolves to no tool — mirrors invoke's tool_not_found. */\n readonly error?: {\n readonly code: \"tool_not_found\";\n readonly message: string;\n readonly suggestions?: readonly string[];\n };\n};\n\nconst BUILTIN_TOOL_DESCRIPTIONS: ReadonlyMap<string, DescribedTool> = new Map<\n string,\n DescribedTool\n>([\n [\n \"search\",\n {\n path: \"search\",\n name: \"search\",\n description:\n \"Search available Executor tools. An empty query with a namespace enumerates that integration's full catalog, sorted by path.\",\n inputTypeScript: \"{ query: string; namespace?: string; limit?: number; offset?: number; }\",\n outputTypeScript:\n \"{ items: ToolDiscoveryResult[]; total: number; hasMore: boolean; nextOffset: number | null; }\",\n typeScriptDefinitions: {\n ToolDiscoveryResult:\n \"{ path: string; name: string; description?: string; integration: string; score: number; }\",\n },\n },\n ],\n [\n \"executor.integrations.list\",\n {\n path: \"executor.integrations.list\",\n name: \"executor.integrations.list\",\n description: \"List configured Executor integrations.\",\n inputTypeScript: \"{ query?: string; limit?: number; offset?: number; }\",\n outputTypeScript:\n \"{ items: ExecutorIntegrationListItem[]; total: number; hasMore: boolean; nextOffset: number | null; }\",\n typeScriptDefinitions: {\n ExecutorIntegrationListItem:\n \"{ id: string; name: string; description?: string; kind: string; canRemove?: boolean; canRefresh?: boolean; toolCount: number; }\",\n },\n },\n ],\n [\n \"describe.tool\",\n {\n path: \"describe.tool\",\n name: \"describe.tool\",\n description: \"Describe a tool's compact TypeScript input and output shapes.\",\n inputTypeScript: \"{ path: string; }\",\n outputTypeScript: \"DescribedTool\",\n typeScriptDefinitions: {\n DescribedTool:\n '{ path: string; name: string; description?: string; inputTypeScript?: string; outputTypeScript?: string; typeScriptDefinitions?: { [k: string]: string; }; error?: { code: \"tool_not_found\"; message: string; suggestions?: string[]; }; }',\n },\n },\n ],\n [\n \"use\",\n {\n path: \"use\",\n name: \"use\",\n description:\n 'Make an external service capability work in ONE call: state the need (\"transactional email\", \"a database\", or a provider name like \"resend\") and get back a wired, callable integration — resolve, add, and connect handled internally. Returns ready (tools callable now), pending (one human credential click left; re-press to check), compare (full ranked set, on request), or an honest no_match with the catalog taxonomy. The answer is ONE verified provider with a machine-readable `sponsored` label and the count of alternatives; override by pressing again with not: [\"<id>\"].',\n inputTypeScript: \"{ need: string; not?: string[]; compare?: boolean; }\",\n outputTypeScript: \"UseNeedReply\",\n typeScriptDefinitions: {\n UseNeedReply:\n '{ status: \"ready\"; provider: ProviderCard; alternativesCount: number; toolsPath?: string; instructions: string; } | { status: \"pending\"; provider: ProviderCard; alternativesCount: number; url: string; instructions: string; } | { status: \"compare\"; candidates: ProviderCard[]; instructions: string; } | { status: \"no_match\"; categories: string[]; instructions: string; } | { status: \"error\"; step: string; message: string; instructions: string; }',\n ProviderCard:\n \"{ id: string; name: string; domain: string; category: string; sponsored: boolean; offer?: { terms: string; url: string; }; verifiedAt: string; }\",\n },\n },\n ],\n]);\n\nconst newCorrelationId = (): string => {\n // 8-hex-char correlation id; enough entropy to disambiguate within a\n // single deployment without leaking host process info.\n return Math.floor(Math.random() * 0x1_0000_0000)\n .toString(16)\n .padStart(8, \"0\");\n};\n\nconst validationIssues = (value: unknown): readonly unknown[] | null => {\n if (typeof value !== \"object\" || value === null) return null;\n const issues = (value as { readonly issues?: unknown }).issues;\n return Array.isArray(issues) ? issues : null;\n};\n\n// Pre-flight OpenAPI invocation failures (missing/unresolved path params,\n// missing or malformed request body) are caller-argument errors raised before\n// any HTTP request is sent. Their messages are generated locally from the\n// spec (no upstream data, no transport internals), so they are safe to\n// surface, and actionable: the fix is renaming or adding an argument.\n// Discriminator: pre-flight raises carry `statusCode: None` and no `cause`.\n// Transport failures share `statusCode: None` but wrap the raw client error\n// (which can hold internal URLs) as `cause`, and post-response failures carry\n// a status code; both stay on the opaque-defect path. Matched structurally\n// because this package does not depend on the openapi plugin.\nconst openApiPreflightMessage = (value: unknown): string | null => {\n if (!Predicate.isTagged(value, \"OpenApiInvocationError\")) return null;\n const err = value as {\n readonly message?: unknown;\n readonly statusCode?: unknown;\n readonly cause?: unknown;\n };\n if (err.cause !== undefined) return null;\n if (!Predicate.isTagged(err.statusCode, \"None\")) return null;\n // oxlint-disable-next-line executor/no-unknown-error-message -- boundary: structural match on the openapi plugin's tagged error; the pre-flight message is locally generated (no upstream data) and is the payload being surfaced\n return typeof err.message === \"string\" && err.message.length > 0 ? err.message : null;\n};\n\nconst credentialResolutionToolFailure = (input: {\n readonly label: string;\n readonly message: string;\n readonly reauthRequired?: boolean;\n}) =>\n authToolFailure({\n code: input.reauthRequired === true ? \"oauth_reauth_required\" : \"oauth_refresh_failed\",\n message:\n input.reauthRequired === true\n ? `OAuth connection \"${input.label}\" requires reauthorization: ${input.message}`\n : `OAuth connection \"${input.label}\" could not be resolved: ${input.message}`,\n credential: {\n kind: \"oauth\",\n label: input.label,\n },\n });\n\nconst bindingToolFailure = (value: unknown): ToolError | null => {\n if (!Predicate.isTagged(value, \"BindingError\")) return null;\n const maybeBinding = value as {\n readonly message?: unknown;\n readonly role?: unknown;\n readonly integration?: unknown;\n readonly requestedConnection?: unknown;\n };\n const details: Record<string, string> = {};\n if (typeof maybeBinding.role === \"string\") details.role = maybeBinding.role;\n if (typeof maybeBinding.integration === \"string\") details.integration = maybeBinding.integration;\n if (typeof maybeBinding.requestedConnection === \"string\") {\n details.requestedConnection = maybeBinding.requestedConnection;\n }\n return {\n code: \"binding_error\",\n message:\n typeof maybeBinding.message === \"string\" ? maybeBinding.message : \"Tool binding failed.\",\n ...(Object.keys(details).length > 0 ? { details } : {}),\n };\n};\n\nconst expectedToolFailure = (value: unknown): ToolError | null => {\n if (isUserActionableError(value)) {\n return {\n code: value.code,\n message: value.userMessage,\n };\n }\n if (Predicate.isTagged(value, \"ToolNotFoundError\") && \"address\" in value) {\n const suggestions =\n \"suggestions\" in value && Array.isArray(value.suggestions)\n ? value.suggestions.map((suggestion) => addressToPath(String(suggestion)))\n : undefined;\n const address = addressToPath(String(value.address));\n return {\n code: \"tool_not_found\",\n message: `Tool not found: ${address}`,\n details: { path: address, ...(suggestions ? { suggestions } : {}) },\n };\n }\n if (Predicate.isTagged(value, \"ToolBlockedError\") && \"address\" in value) {\n return {\n code: \"tool_blocked\",\n message: `Tool blocked by policy: ${addressToPath(String(value.address))}`,\n details: value,\n };\n }\n if (Predicate.isTagged(value, \"ToolInvocationError\")) {\n const cause = (value as { readonly cause?: unknown }).cause;\n if (isUserActionableError(cause)) {\n return {\n code: cause.code,\n message: cause.userMessage,\n };\n }\n const binding = bindingToolFailure(cause);\n if (binding) return binding;\n const issues = validationIssues(cause);\n if (issues) {\n return {\n code: \"invalid_tool_arguments\",\n message: \"Tool arguments did not match the input schema.\",\n details: { issues },\n };\n }\n const preflight = openApiPreflightMessage(cause);\n if (preflight) {\n return {\n code: \"invalid_tool_arguments\",\n message: preflight,\n };\n }\n }\n return null;\n};\n\n/**\n * Extract the integration namespace from a tool path. v2 addresses look like\n * `<integration>.<owner>.<connection>.<tool>`; static fqids look like\n * `<integration>.<op>`. We take the first segment as a cheap, non-lookup namespace\n * for the span attribute so it's always populated without a catalog read.\n */\nconst extractNamespace = (path: string): string => {\n const normalized = addressToPath(path);\n const idx = normalized.indexOf(\".\");\n return idx === -1 ? normalized : normalized.slice(0, idx);\n};\n\n/**\n * Bridges QuickJS `tools.<integration>.<owner>.<connection>.<tool>(args)` calls\n * into `executor.execute(address, args)`.\n *\n * Wrapped in `Effect.fn(\"mcp.tool.dispatch\")` so every tool call becomes a\n * span in the Effect tracer. Attributes:\n * - `mcp.tool.name` — full tool path (e.g. \"github.org.main.getRepo\")\n * - `mcp.tool.integration` — first segment of the path (namespace)\n *\n * `mcp.tool.kind` (openapi | mcp | graphql | code) is NOT annotated here\n * because it would require an `integrations.list()` lookup on every invocation.\n * Callers that already know the integration kind can annotate at their own span.\n */\nexport const makeExecutorToolInvoker = (\n executor: Executor,\n options: { readonly invokeOptions: InvokeOptions },\n): SandboxToolInvoker => ({\n invoke: Effect.fn(\"mcp.tool.dispatch\")(function* ({ path, args }) {\n yield* Effect.annotateCurrentSpan({\n \"mcp.tool.name\": path,\n \"mcp.tool.integration\": extractNamespace(path),\n });\n\n const address = pathToAddress(path);\n const result = yield* executor.execute(address, args, options.invokeOptions).pipe(\n Effect.catchTag(\"CredentialResolutionError\", (err) =>\n Effect.succeed(\n credentialResolutionToolFailure({\n label: `${err.integration}.${err.owner}.${err.name}`,\n message: err.message,\n reauthRequired: err.reauthRequired,\n }),\n ),\n ),\n Effect.catchCause((cause) => {\n const err = cause.reasons.find(Cause.isFailReason)?.error;\n const expected = expectedToolFailure(err);\n if (expected) {\n return Effect.succeed(ToolResult.fail(expected));\n }\n if (isElicitationDeclinedError(err)) {\n return Effect.fail(\n new ExecutionToolError({\n message: `Tool \"${addressToPath(String(err.address))}\" requires approval but the request was ${err.action === \"cancel\" ? \"cancelled\" : \"declined\"} by the user.`,\n cause: err,\n }),\n );\n }\n // Any other failure here is an infra/plugin defect. Emit an\n // opaque generic with a correlation id so internal context (URLs\n // with tokens, DB connection strings, file paths in stacks)\n // can't leak through Error.message into the sandbox. The full\n // cause is logged with the same correlation id so operators can\n // still trace the failure.\n const correlationId = newCorrelationId();\n return Effect.logError(\"tool dispatch failed\", cause).pipe(\n Effect.annotateLogs({\n \"executor.correlation_id\": correlationId,\n \"mcp.tool.name\": path,\n }),\n Effect.flatMap(() =>\n Effect.fail(\n new ExecutionToolError({\n message: `${OPAQUE_DEFECT_MESSAGE} [${correlationId}]`,\n cause: err ?? cause,\n }),\n ),\n ),\n );\n }),\n );\n\n // Strict: plugins emit ToolResult<T>. Anything else is treated as a\n // raw success value and wrapped — keeps the sandbox-facing contract\n // uniform without forcing every tiny test plugin to import\n // `ToolResult.ok`.\n // Expected failures resolve through the success channel, so without the\n // outcome annotation the dispatch span reads as healthy even when the\n // caller hit an upstream error or auth wall.\n yield* annotateToolResultOutcome(result);\n if (isToolResult(result)) {\n return result;\n }\n return { ok: true, data: result };\n }),\n});\n\nconst isElicitationDeclinedError = (\n value: unknown,\n): value is {\n readonly _tag: \"ElicitationDeclinedError\";\n readonly address: string;\n readonly action: \"cancel\" | \"decline\";\n} =>\n Predicate.isTagged(value, \"ElicitationDeclinedError\") &&\n value !== null &&\n typeof value === \"object\" &&\n \"address\" in value &&\n typeof value.address === \"string\" &&\n \"action\" in value &&\n (value.action === \"cancel\" || value.action === \"decline\");\n\nexport type ToolDiscoveryResult = {\n readonly path: string;\n readonly name: string;\n readonly description?: string;\n readonly integration: string;\n readonly score: number;\n};\n\nexport type ExecutorIntegrationListItem = {\n readonly id: string;\n readonly name: string;\n readonly description?: string;\n readonly kind: string;\n readonly canRemove?: boolean;\n readonly canRefresh?: boolean;\n readonly toolCount: number;\n};\n\nexport type ToolDiscoveryInput = {\n readonly executor: Executor;\n readonly query: string;\n readonly namespace?: string;\n readonly limit: number;\n readonly offset: number;\n};\n\nexport interface ToolDiscoveryProvider {\n readonly searchTools: (\n input: ToolDiscoveryInput,\n ) => Effect.Effect<PagedResult<ToolDiscoveryResult>, ExecutionToolError>;\n}\n\n/**\n * Page of results from a list-style discovery tool. Shared by\n * `tools.search` and `tools.executor.integrations.list` so the model sees one\n * consistent shape:\n *\n * - `items` — the page (slice).\n * - `total` — count after filtering, before pagination. The model\n * can use this to detect truncation.\n * - `hasMore` — convenience flag for `(offset + items.length) < total`.\n * - `nextOffset` — concrete offset for the next page when `hasMore`,\n * `null` otherwise. Pre-computing it removes a class of\n * off-by-one mistakes when the model paginates.\n */\nexport type PagedResult<T> = {\n readonly items: readonly T[];\n readonly total: number;\n readonly hasMore: boolean;\n readonly nextOffset: number | null;\n};\n\nconst paginate = <T>(all: readonly T[], offset: number, limit: number): PagedResult<T> => {\n const total = all.length;\n const start = Math.min(Math.max(offset, 0), total);\n const items = all.slice(start, start + limit);\n const consumed = start + items.length;\n const hasMore = consumed < total;\n return {\n items,\n total,\n hasMore,\n nextOffset: hasMore ? consumed : null,\n };\n};\n\n/** What `searchTools` ranks over — the sandbox-callable path plus the v2\n * identity fields a query can match against. */\ntype SearchableTool = {\n readonly path: string;\n readonly integration: string;\n readonly name: string;\n readonly description?: string;\n};\n\nconst toSearchableTool = (tool: Tool): SearchableTool => ({\n path: addressToPath(String(tool.address)),\n integration: String(tool.integration),\n name: String(tool.name),\n description: tool.description,\n});\n\ntype PreparedField = {\n readonly raw: string;\n readonly tokens: readonly string[];\n};\n\nconst SEARCH_FIELD_WEIGHTS = {\n path: 12,\n integration: 8,\n name: 10,\n description: 5,\n} as const;\n\nconst normalizeSearchText = (value: string): string =>\n value\n .replace(/([a-z0-9])([A-Z])/g, \"$1 $2\")\n .replace(/[_./:-]+/g, \" \")\n .toLowerCase()\n .trim();\n\nconst tokenizeSearchText = (value: string): string[] =>\n normalizeSearchText(value)\n .split(/[^a-z0-9]+/)\n .map((token) => token.trim())\n .filter(Boolean);\n\nconst prepareField = (value?: string): PreparedField => ({\n raw: normalizeSearchText(value ?? \"\"),\n tokens: tokenizeSearchText(value ?? \"\"),\n});\n\nconst scorePreparedField = (\n query: string,\n queryTokens: readonly string[],\n field: PreparedField,\n weight: number,\n): {\n readonly score: number;\n readonly matchedTokens: ReadonlySet<string>;\n readonly exactPhraseMatch: boolean;\n} => {\n if (field.raw.length === 0) {\n return {\n score: 0,\n matchedTokens: new Set<string>(),\n exactPhraseMatch: false,\n };\n }\n\n let score = 0;\n const matchedTokens = new Set<string>();\n const exactPhraseMatch = query.length > 0 && field.raw.includes(query);\n\n if (query.length > 0) {\n if (field.raw === query) {\n score += weight * 14;\n } else if (field.raw.startsWith(query)) {\n score += weight * 9;\n } else if (exactPhraseMatch) {\n score += weight * 6;\n }\n }\n\n for (const token of queryTokens) {\n if (field.tokens.includes(token)) {\n score += weight * 4;\n matchedTokens.add(token);\n continue;\n }\n\n if (\n field.tokens.some((candidate) => candidate.startsWith(token) || token.startsWith(candidate))\n ) {\n score += weight * 2;\n matchedTokens.add(token);\n continue;\n }\n\n if (field.raw.includes(token)) {\n score += weight;\n matchedTokens.add(token);\n }\n }\n\n return {\n score,\n matchedTokens,\n exactPhraseMatch,\n };\n};\n\nconst matchesNamespace = (tool: SearchableTool, namespace?: string): boolean => {\n if (!namespace || normalizeSearchText(namespace).length === 0) {\n return true;\n }\n\n const namespaceTokens = tokenizeSearchText(namespace);\n if (namespaceTokens.length === 0) {\n return true;\n }\n\n const integrationTokens = tokenizeSearchText(tool.integration);\n const pathTokens = tokenizeSearchText(tool.path);\n\n const isPrefixMatch = (tokens: readonly string[]): boolean =>\n namespaceTokens.every((token, index) => tokens[index] === token);\n\n return isPrefixMatch(integrationTokens) || isPrefixMatch(pathTokens);\n};\n\nconst scoreToolMatch = (tool: SearchableTool, query: string): ToolDiscoveryResult | null => {\n const normalizedQuery = normalizeSearchText(query);\n const queryTokens = tokenizeSearchText(query);\n\n if (normalizedQuery.length === 0 || queryTokens.length === 0) {\n return null;\n }\n\n const path = prepareField(tool.path);\n const integration = prepareField(tool.integration);\n const name = prepareField(tool.name);\n const description = prepareField(tool.description);\n\n const fieldScores = [\n scorePreparedField(normalizedQuery, queryTokens, path, SEARCH_FIELD_WEIGHTS.path),\n scorePreparedField(normalizedQuery, queryTokens, integration, SEARCH_FIELD_WEIGHTS.integration),\n scorePreparedField(normalizedQuery, queryTokens, name, SEARCH_FIELD_WEIGHTS.name),\n scorePreparedField(normalizedQuery, queryTokens, description, SEARCH_FIELD_WEIGHTS.description),\n ];\n\n const matchedTokens = new Set<string>();\n let score = 0;\n let exactPhraseMatch = false;\n\n for (const fieldScore of fieldScores) {\n score += fieldScore.score;\n exactPhraseMatch ||= fieldScore.exactPhraseMatch;\n for (const token of fieldScore.matchedTokens) {\n matchedTokens.add(token);\n }\n }\n\n if (matchedTokens.size === 0) {\n return null;\n }\n\n const coverage = matchedTokens.size / queryTokens.length;\n const minimumCoverage = queryTokens.length <= 2 ? 1 : 0.6;\n\n if (coverage < minimumCoverage && !exactPhraseMatch) {\n return null;\n }\n\n if (coverage === 1) {\n score += 25;\n } else {\n score += Math.round(coverage * 10);\n }\n\n if (path.tokens[0] === queryTokens[0] || name.tokens[0] === queryTokens[0]) {\n score += 8;\n }\n\n if (\n normalizeSearchText(tool.path) === normalizedQuery ||\n normalizeSearchText(tool.name) === normalizedQuery\n ) {\n score += 20;\n }\n\n return {\n path: tool.path,\n name: tool.name,\n description: tool.description,\n integration: tool.integration,\n score,\n };\n};\n\n/** What `tools.search()` calls inside the sandbox. */\nexport const searchTools = Effect.fn(\"executor.tools.search\")(function* (\n executor: Executor,\n query: string,\n limit = 12,\n options?: { readonly namespace?: string; readonly offset?: number },\n) {\n const offset = options?.offset ?? 0;\n yield* Effect.annotateCurrentSpan({\n \"executor.search.query_length\": query.length,\n \"executor.search.limit\": limit,\n \"executor.search.offset\": offset,\n ...(options?.namespace ? { \"executor.search.namespace\": options.namespace } : {}),\n });\n\n const emptyQuery = normalizeSearchText(query).length === 0;\n const hasNamespace =\n options?.namespace !== undefined && normalizeSearchText(options.namespace).length > 0;\n\n // An empty query with no namespace stays empty: it carries neither a\n // ranking signal nor a scope, and listing the whole workspace \"by default\"\n // is exactly the arbitrary dump the ranked search refuses to be.\n if (emptyQuery && !hasNamespace) {\n return {\n items: [],\n total: 0,\n hasMore: false,\n nextOffset: null,\n } satisfies PagedResult<ToolDiscoveryResult>;\n }\n\n const all = yield* executor.tools.list({ includeAnnotations: false }).pipe(\n Effect.mapError(\n (cause) =>\n new ExecutionToolError({\n message: \"Failed to list tools for search\",\n cause,\n }),\n ),\n );\n const searchable = all.map(toSearchableTool);\n\n // An empty query WITH a namespace is enumeration, not search: there is no\n // ranking signal, so the namespace's whole catalog comes back sorted by\n // path (score 0) and paged. Enumeration scopes by EXACT integration slug —\n // the token-prefix `matchesNamespace` used for ranked search would also\n // sweep in prefix-sibling integrations (namespace \"google\" matching\n // google_gmail and google_sheets), which would silently break the census\n // guarantee: `total` here must reconcile against\n // `executor.integrations.list`'s per-integration toolCount.\n const ranked: readonly ToolDiscoveryResult[] = emptyQuery\n ? searchable\n .filter((tool) => tool.integration === options?.namespace?.trim())\n .sort((left, right) => left.path.localeCompare(right.path))\n .map((tool) => ({\n path: tool.path,\n name: tool.name,\n integration: tool.integration,\n score: 0,\n ...(tool.description !== undefined ? { description: tool.description } : {}),\n }))\n : searchable\n .filter((tool: SearchableTool) => matchesNamespace(tool, options?.namespace))\n .map((tool: SearchableTool) => scoreToolMatch(tool, query))\n .filter(Predicate.isNotNull)\n .sort((left, right) => right.score - left.score || left.path.localeCompare(right.path));\n\n const page = paginate(ranked, offset, limit);\n\n yield* Effect.annotateCurrentSpan({\n \"executor.search.candidate_count\": all.length,\n \"executor.search.match_count\": ranked.length,\n \"executor.search.result_count\": page.items.length,\n \"executor.search.has_more\": page.hasMore,\n });\n return page;\n});\n\nexport const defaultToolDiscoveryProvider: ToolDiscoveryProvider = {\n searchTools: ({ executor, query, namespace, limit, offset }) =>\n searchTools(executor, query, limit, { namespace, offset }),\n};\n\n/** What `tools.executor.integrations.list()` calls inside the sandbox. v2: the\n * integrations are the integration catalog; tool counts come from the\n * per-connection tool list. */\nexport const listExecutorIntegrations = Effect.fn(\"executor.integrations.list\")(function* (\n executor: Executor,\n options?: {\n readonly query?: string;\n readonly limit?: number;\n readonly offset?: number;\n },\n) {\n const normalizedQuery = normalizeSearchText(options?.query ?? \"\");\n const limit = options?.limit ?? 50;\n const offset = options?.offset ?? 0;\n const integrations = yield* executor.integrations.list().pipe(\n Effect.mapError(\n (cause) =>\n new ExecutionToolError({\n message: \"Failed to list executor integrations\",\n cause,\n }),\n ),\n );\n\n const filtered =\n normalizedQuery.length === 0\n ? integrations\n : integrations.filter((integration: Integration) => {\n const haystack = normalizeSearchText(\n [String(integration.slug), integration.description, integration.kind].join(\" \"),\n );\n return tokenizeSearchText(normalizedQuery).every((token) => haystack.includes(token));\n });\n\n // Single query for all tools, then count per integration in memory.\n const allTools = yield* executor.tools.list({ includeAnnotations: false }).pipe(\n Effect.mapError(\n (cause) =>\n new ExecutionToolError({\n message: \"Failed to list tools for integration counts\",\n cause,\n }),\n ),\n );\n const toolCountByIntegration = new Map<string, number>();\n for (const tool of allTools) {\n const key = String(tool.integration);\n toolCountByIntegration.set(key, (toolCountByIntegration.get(key) ?? 0) + 1);\n }\n\n const sortedWithCounts = filtered\n .map(\n (integration: Integration) =>\n ({\n id: String(integration.slug),\n name: String(integration.slug),\n // The integration's catalog description — user-editable context the\n // agent can use to pick an integration. Omitted when it just repeats the\n // slug or display name (no information beyond identity).\n ...(integration.description &&\n integration.description.toLowerCase() !== String(integration.slug).toLowerCase() &&\n integration.description.toLowerCase() !== integration.name.toLowerCase()\n ? { description: integration.description }\n : {}),\n kind: integration.kind,\n canRemove: integration.canRemove,\n canRefresh: integration.canRefresh,\n toolCount: toolCountByIntegration.get(String(integration.slug)) ?? 0,\n }) satisfies ExecutorIntegrationListItem,\n )\n .sort((left, right) => left.name.localeCompare(right.name) || left.id.localeCompare(right.id));\n\n const page = paginate(sortedWithCounts, offset, limit);\n\n yield* Effect.annotateCurrentSpan({\n \"executor.integrations.candidate_count\": integrations.length,\n \"executor.integrations.match_count\": sortedWithCounts.length,\n \"executor.integrations.result_count\": page.items.length,\n \"executor.integrations.has_more\": page.hasMore,\n });\n return page;\n});\n\n/** What `tools.describe.tool()` calls inside the sandbox. */\nexport const describeTool = Effect.fn(\"executor.tools.describe\")(function* (\n executor: Executor,\n path: string,\n) {\n yield* Effect.annotateCurrentSpan({ \"mcp.tool.name\": path });\n\n const builtin = BUILTIN_TOOL_DESCRIPTIONS.get(path);\n if (builtin) return builtin;\n\n const address = pathToAddress(path);\n\n // Single tools.schema() call — it already fetches the tool row\n // internally. No need to also call tools.list() just for name/description.\n const schema: ToolSchemaView | null = yield* executor.tools.schema(address);\n\n // tools.schema() returns null if the tool doesn't exist. Mirror the\n // invoke path's tool_not_found shape (error + suggestions) instead of a\n // bare stub — a silent `{ path, name }` reads as \"tool exists, schema\n // unavailable\" and sends callers down the wrong debugging path.\n if (schema === null) {\n const lastDot = path.lastIndexOf(\".\");\n const leaf = lastDot === -1 ? path : path.slice(lastDot + 1);\n const scoped = yield* searchTools(executor, leaf, TOOL_DESCRIBE_SUGGESTION_LIMIT, {\n namespace: extractNamespace(path),\n });\n const matches =\n scoped.items.length > 0\n ? scoped.items\n : (yield* searchTools(executor, leaf, TOOL_DESCRIBE_SUGGESTION_LIMIT)).items;\n const suggestions = matches.map((item) => item.path);\n const notFound: DescribedTool = {\n path,\n name: path,\n error: {\n code: \"tool_not_found\",\n message: `Tool not found: ${path}`,\n ...(suggestions.length > 0 ? { suggestions } : {}),\n },\n };\n return notFound;\n }\n\n // The schema's address is the tool address; name/description come from the\n // tool row which tools.schema() already loaded.\n const described: DescribedTool = {\n path,\n name: schema.name ?? path,\n description: schema.description,\n inputTypeScript: schema.inputTypeScript,\n outputTypeScript: wrapOutputTypeScript(schema.outputTypeScript),\n typeScriptDefinitions: withToolResultDefinitions(schema.typeScriptDefinitions),\n };\n return described;\n});\n","import { Effect } from \"effect\";\nimport type { Connection, Executor } from \"@rafads/sdk/core\";\n\n/**\n * Builds the `execute` tool description dynamically.\n *\n * Structure:\n * 1. One-line intro + pointer to the `execute` skill (the full how-to lives\n * behind the `skills` tool, see ./skills.ts, to keep this always-loaded\n * description small)\n * 2. Available integrations (the live, per-session inventory): the top-level\n * integration slugs the user has connected, deduped across connections,\n * names only. The same block is appended to the `execute` skill content.\n */\n\n/** The header that opens the live integration inventory. Exported so the host\n * can locate (and re-use) the inventory block inside the built description. */\nexport const INTEGRATION_INVENTORY_HEADER = \"## Available integrations\";\n\nexport const buildExecuteDescription = (executor: Executor): Effect.Effect<string> =>\n Effect.gen(function* () {\n const connections: readonly Connection[] = yield* executor.connections.list().pipe(\n // oxlint-disable-next-line executor/no-effect-escape-hatch -- boundary: ExecutionEngine.getDescription currently exposes no error channel; engine typed-error widening is covered separately\n Effect.orDie,\n Effect.withSpan(\"executor.connections.list\"),\n );\n\n const description = yield* Effect.sync(() => {\n const lines = [\n \"Execute TypeScript in a sandboxed runtime with access to connected service integrations.\",\n \"\",\n 'Before writing code, call `skills({ name: \"execute\" })` for the workflow on how to use this tool.',\n \"\",\n 'When a task explicitly calls for an external service capability — sending email, a database, auth, payments, SMS, DNS, hosting, storage, search — do not pick a provider from memory or send the user off to sign up somewhere: press the button. `await tools.use({ need: \"transactional email\" })` returns a wired, working integration in one call (or one remaining human credential click); connected providers resolve through the same press. `skills({ name: \"resolve\" })` has details and the manual stepwise flow.',\n ];\n const inventory = formatIntegrationInventory(connections);\n if (inventory.length > 0) {\n lines.push(\"\");\n lines.push(inventory);\n }\n return lines.join(\"\\n\");\n }).pipe(\n Effect.withSpan(\"schema.compile.description\", {\n attributes: { \"executor.connection_count\": connections.length },\n }),\n );\n\n yield* Effect.annotateCurrentSpan({\n \"executor.connection_count\": connections.length,\n \"schema.kind\": \"execute\",\n // Connection inventory so a failing session build (which runs this during\n // init) names the callable prefixes it resolved without listing tools.\n \"executor.connection_addresses\": connections\n .map((connection) => connectionPath(connection))\n .slice(0, 50)\n .join(\",\"),\n \"executor.connection_integrations\": [\n ...new Set(connections.map((connection) => String(connection.integration))),\n ].join(\",\"),\n \"executor.connection_owners\": [\n ...new Set(connections.map((connection) => connection.owner)),\n ].join(\",\"),\n });\n\n return description;\n }).pipe(Effect.withSpan(\"schema.describe.execute\"));\n\nconst connectionPath = (connection: Connection): string => {\n const address = String(connection.address);\n return address.startsWith(\"tools.\") ? address.slice(\"tools.\".length) : address;\n};\n\n// The live inventory block: the top-level integrations the user has connected,\n// one bare line per integration slug (deduped across connections, sorted), no\n// per-connection prefixes and no descriptions. Empty string when nothing is\n// connected.\nconst INVENTORY_LIMIT = 50;\n\nconst formatIntegrationInventory = (connections: readonly Connection[]): string => {\n const slugs = [...new Set(connections.map((connection) => String(connection.integration)))].sort(\n (a, b) => a.localeCompare(b),\n );\n if (slugs.length === 0) return \"\";\n const shown = slugs.slice(0, INVENTORY_LIMIT);\n const lines = [\n INTEGRATION_INVENTORY_HEADER,\n \"\",\n \"Integrations you have connected. Their tools live under `tools.<integration>.…`.\",\n ...shown.map((slug) => `- \\`${slug}\\``),\n ];\n if (slugs.length > shown.length) {\n lines.push(`- ... ${slugs.length - shown.length} more`);\n }\n return lines.join(\"\\n\");\n};\n","import { Deferred, Effect, Fiber, Predicate, Queue } from \"effect\";\nimport type * as Cause from \"effect/Cause\";\nimport * as Exit from \"effect/Exit\";\n\nimport type {\n Executor,\n InvokeOptions,\n ElicitationResponse,\n ElicitationHandler,\n ElicitationContext,\n} from \"@rafads/sdk/core\";\nimport { CodeExecutionError } from \"@rafads/codemode-core\";\nimport type { CodeExecutor, ExecuteResult, SandboxToolInvoker } from \"@rafads/codemode-core\";\n\nimport {\n defaultToolDiscoveryProvider,\n makeExecutorToolInvoker,\n listExecutorIntegrations,\n describeTool,\n type ToolDiscoveryProvider,\n} from \"./tool-invoker\";\nimport { ExecutionToolError } from \"./errors\";\nimport { runUseNeed } from \"./use-need\";\nimport { buildExecuteDescription } from \"./description\";\n\n// ---------------------------------------------------------------------------\n// Types\n// ---------------------------------------------------------------------------\n\nexport type ExecutionEngineConfig<E extends Cause.YieldableError = CodeExecutionError> = {\n readonly executor: Executor;\n readonly codeExecutor: CodeExecutor<E>;\n readonly toolDiscoveryProvider?: ToolDiscoveryProvider;\n /** Fired after every sandbox tool dispatch (builtins included) with the\n * path and outcome — the host's pool-ledger seam (accrual records for the\n * earning lane; see the hq repo's docs/v2/03-plugin-reset.md §5). MUST be\n * fire-and-forget and non-throwing; the engine calls it best-effort and\n * never awaits it. */\n readonly onToolCall?: (info: { readonly path: string; readonly ok: boolean }) => void;\n};\n\nexport type ExecutionResult =\n | { readonly status: \"completed\"; readonly result: ExecuteResult }\n | { readonly status: \"paused\"; readonly execution: PausedExecution };\n\nexport type PausedExecution = {\n readonly id: string;\n readonly elicitationContext: ElicitationContext;\n};\n\nexport type PausedExecutionDeadline = {\n readonly expiresAt: string;\n readonly ttlMs: number;\n};\n\n/** Internal representation with Effect runtime state for pause/resume. */\ntype InternalPausedExecution<E> = PausedExecution & {\n readonly response: Deferred.Deferred<typeof ElicitationResponse.Type>;\n readonly fiber: Fiber.Fiber<ExecuteResult, E>;\n readonly pauseQueue: Queue.Queue<InternalPausedExecution<E>>;\n};\n\nexport type ResumeResponse = {\n readonly action: \"accept\" | \"decline\" | \"cancel\";\n readonly content?: Record<string, unknown>;\n};\n\n// Auto-accept every elicitation. Used by the `autoApprove` path where the\n// caller is itself the human approver (the operator-facing Run/Test panel).\nconst acceptAllHandler: ElicitationHandler = () => Effect.succeed({ action: \"accept\" });\n\n// ---------------------------------------------------------------------------\n// Result formatting\n// ---------------------------------------------------------------------------\n\nconst MAX_PREVIEW_CHARS = 30_000;\n\nconst truncate = (value: string, max: number): string =>\n value.length > max\n ? `${value.slice(0, max)}\\n... [truncated ${value.length - max} chars]`\n : value;\n\nexport const formatExecuteResult = (\n result: ExecuteResult,\n): {\n text: string;\n structured: Record<string, unknown>;\n isError: boolean;\n} => {\n const resultText =\n result.result != null\n ? typeof result.result === \"string\"\n ? result.result\n : JSON.stringify(result.result, null, 2)\n : null;\n\n const logText = result.logs && result.logs.length > 0 ? result.logs.join(\"\\n\") : null;\n\n // `emit()` output is shown to the user, not returned to the model, so a\n // script that only emits comes back with a null result. Acknowledge the\n // emitted items in the envelope so an emit-without-return reads as \"output\n // went to the user\" rather than a silent void.\n const emitted = result.output?.length ?? 0;\n const emittedNote =\n emitted > 0 ? `${emitted} item${emitted === 1 ? \"\" : \"s\"} emitted to the user` : null;\n const emittedField = emitted > 0 ? { emitted } : {};\n\n if (result.error) {\n const parts = [`Error: ${result.error}`, ...(logText ? [`\\nLogs:\\n${logText}`] : [])];\n return {\n text: truncate(parts.join(\"\\n\"), MAX_PREVIEW_CHARS),\n structured: {\n status: \"error\",\n error: result.error,\n ...emittedField,\n logs: result.logs ?? [],\n },\n isError: true,\n };\n }\n\n const resultPart = resultText\n ? truncate(resultText, MAX_PREVIEW_CHARS)\n : emittedNote\n ? `(no return value; ${emittedNote})`\n : \"(no result)\";\n const parts = [resultPart, ...(logText ? [`\\nLogs:\\n${logText}`] : [])];\n return {\n text: parts.join(\"\\n\"),\n structured: {\n status: \"completed\",\n result: result.result ?? null,\n ...emittedField,\n logs: result.logs ?? [],\n },\n isError: false,\n };\n};\n\nexport const formatPausedExecution = (\n paused: PausedExecution,\n options?: { readonly deadline?: PausedExecutionDeadline },\n): {\n text: string;\n structured: Record<string, unknown>;\n} => {\n const req = paused.elicitationContext.request;\n const lines: string[] = [`Execution paused: ${req.message}`];\n const deadline = options?.deadline;\n const isUrlElicitation = Predicate.isTagged(req, \"UrlElicitation\");\n const isFormElicitation = Predicate.isTagged(req, \"FormElicitation\");\n const requestedSchema = isFormElicitation ? req.requestedSchema : undefined;\n const hasRequestedSchema =\n requestedSchema !== undefined && Object.keys(requestedSchema).length > 0;\n const baseInstructions = isUrlElicitation\n ? `The user needs to open this URL in a browser and complete the flow. After the user finishes, call the resume tool with executionId \"${paused.id}\" and action \"accept\".`\n : hasRequestedSchema\n ? `Ask the user for values matching requestedSchema. Then call the resume tool with executionId \"${paused.id}\", action \"accept\", and content matching requestedSchema. If the user declines, call resume with action \"decline\" or \"cancel\".`\n : `This is a model-side confirmation gate; there is no browser form to open. Ask the user whether to approve the paused tool call. If the user approves, call the resume tool with executionId \"${paused.id}\" and action \"accept\". If the user declines, call resume with action \"decline\" or \"cancel\".`;\n const deadlineInstructions = deadline\n ? ` Resume before ${deadline.expiresAt}; this approval window lasts ${formatTtlDuration(deadline.ttlMs)}.`\n : \"\";\n const instructions = `${baseInstructions}${deadlineInstructions}`;\n\n if (isUrlElicitation) {\n lines.push(`\\nOpen this URL in a browser:\\n${req.url}`);\n lines.push('\\nAfter the browser flow, call the resume tool with action \"accept\".');\n } else if (hasRequestedSchema) {\n lines.push(\n \"\\nAsk the user for a response matching the requested schema, then call the resume tool.\",\n );\n lines.push(`\\nRequested schema:\\n${JSON.stringify(requestedSchema, null, 2)}`);\n } else {\n lines.push(\n '\\nThis is a model-side confirmation gate; no browser form is waiting. Ask the user whether to approve, then call the resume tool with action \"accept\", \"decline\", or \"cancel\".',\n );\n }\n\n lines.push(`\\nexecutionId: ${paused.id}`);\n if (deadline) {\n lines.push(\n `\\nresumeDeadline: ${deadline.expiresAt} (${formatTtlDuration(deadline.ttlMs)} approval window)`,\n );\n }\n lines.push(`\\ninstructions: ${instructions}`);\n\n return {\n text: lines.join(\"\\n\"),\n structured: {\n status: \"waiting_for_interaction\",\n executionId: paused.id,\n ...(deadline ? { expiresAt: deadline.expiresAt, ttlMs: deadline.ttlMs } : {}),\n interaction: {\n kind: isUrlElicitation ? \"url\" : \"form\",\n message: req.message,\n instructions,\n address: String(paused.elicitationContext.address),\n args: paused.elicitationContext.args,\n ...(isUrlElicitation ? { url: req.url } : {}),\n ...(isFormElicitation ? { requestedSchema: req.requestedSchema } : {}),\n },\n },\n };\n};\n\nexport const formatTtlDuration = (ttlMs: number): string => {\n const seconds = Math.max(1, Math.round(ttlMs / 1000));\n if (seconds % 60 === 0) {\n const minutes = seconds / 60;\n return `${minutes} minute${minutes === 1 ? \"\" : \"s\"}`;\n }\n return `${seconds} second${seconds === 1 ? \"\" : \"s\"}`;\n};\n\n// ---------------------------------------------------------------------------\n// Full invoker (base + discover + describe)\n// ---------------------------------------------------------------------------\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === \"object\" && value !== null && !Array.isArray(value);\n\nconst readOptionalLimit = (value: unknown, toolName: string): number | ExecutionToolError => {\n if (value === undefined) {\n return 12;\n }\n\n if (typeof value !== \"number\" || !Number.isFinite(value) || value <= 0) {\n return new ExecutionToolError({\n message: `${toolName} limit must be a positive number when provided`,\n });\n }\n\n return Math.floor(value);\n};\n\nconst readOptionalOffset = (value: unknown, toolName: string): number | ExecutionToolError => {\n if (value === undefined) {\n return 0;\n }\n\n if (typeof value !== \"number\" || !Number.isFinite(value) || value < 0) {\n return new ExecutionToolError({\n message: `${toolName} offset must be a non-negative number when provided`,\n });\n }\n\n return Math.floor(value);\n};\n\nconst makeFullInvoker = (\n executor: Executor,\n invokeOptions: InvokeOptions,\n toolDiscoveryProvider: ToolDiscoveryProvider,\n): SandboxToolInvoker => {\n const base = makeExecutorToolInvoker(executor, { invokeOptions });\n return {\n invoke: ({ path, args }) => {\n if (path === \"search\") {\n if (!isRecord(args)) {\n return Effect.fail(\n new ExecutionToolError({\n message:\n \"tools.search expects an object: { query?: string; namespace?: string; limit?: number; offset?: number }\",\n }),\n );\n }\n\n if (args.query !== undefined && typeof args.query !== \"string\") {\n return Effect.fail(\n new ExecutionToolError({\n message: \"tools.search query must be a string when provided\",\n }),\n );\n }\n\n if (args.namespace !== undefined && typeof args.namespace !== \"string\") {\n return Effect.fail(\n new ExecutionToolError({\n message: \"tools.search namespace must be a string when provided\",\n }),\n );\n }\n\n const limit = readOptionalLimit(args.limit, \"tools.search\");\n if (Predicate.isTagged(limit, \"ExecutionToolError\")) {\n return Effect.fail(limit);\n }\n\n const offset = readOptionalOffset(args.offset, \"tools.search\");\n if (Predicate.isTagged(offset, \"ExecutionToolError\")) {\n return Effect.fail(offset);\n }\n\n return toolDiscoveryProvider\n .searchTools({\n executor,\n query: args.query ?? \"\",\n limit,\n namespace: args.namespace,\n offset,\n })\n .pipe(\n Effect.withSpan(\"mcp.tool.dispatch\", {\n attributes: { \"mcp.tool.name\": path, \"executor.tool.builtin\": true },\n }),\n );\n }\n if (path === \"executor.integrations.list\") {\n if (args !== undefined && !isRecord(args)) {\n return Effect.fail(\n new ExecutionToolError({\n message:\n \"tools.executor.integrations.list expects an object: { query?: string; limit?: number; offset?: number }\",\n }),\n );\n }\n\n if (isRecord(args) && args.query !== undefined && typeof args.query !== \"string\") {\n return Effect.fail(\n new ExecutionToolError({\n message: \"tools.executor.integrations.list query must be a string when provided\",\n }),\n );\n }\n\n const limit = readOptionalLimit(\n isRecord(args) ? args.limit : undefined,\n \"tools.executor.integrations.list\",\n );\n if (Predicate.isTagged(limit, \"ExecutionToolError\")) {\n return Effect.fail(limit);\n }\n\n const offset = readOptionalOffset(\n isRecord(args) ? args.offset : undefined,\n \"tools.executor.integrations.list\",\n );\n if (Predicate.isTagged(offset, \"ExecutionToolError\")) {\n return Effect.fail(offset);\n }\n\n return listExecutorIntegrations(executor, {\n query: isRecord(args) && typeof args.query === \"string\" ? args.query : undefined,\n limit,\n offset,\n }).pipe(\n Effect.withSpan(\"mcp.tool.dispatch\", {\n attributes: { \"mcp.tool.name\": path, \"executor.tool.builtin\": true },\n }),\n );\n }\n if (path === \"use\") {\n // The make-it-work button: one call collapsing resolve → add →\n // connect (see ./use-need.ts). Composes base.invoke dispatches so\n // approval gates and elicitation pauses apply unchanged.\n return runUseNeed((input) => base.invoke(input), args).pipe(\n Effect.withSpan(\"mcp.tool.dispatch\", {\n attributes: { \"mcp.tool.name\": path, \"executor.tool.builtin\": true },\n }),\n );\n }\n if (path === \"describe.tool\") {\n if (!isRecord(args)) {\n return Effect.fail(\n new ExecutionToolError({\n message: \"tools.describe.tool expects an object: { path: string }\",\n }),\n );\n }\n\n if (typeof args.path !== \"string\" || args.path.trim().length === 0) {\n return Effect.fail(new ExecutionToolError({ message: \"describe.tool requires a path\" }));\n }\n\n if (\"includeSchemas\" in args) {\n return Effect.fail(\n new ExecutionToolError({\n message: \"tools.describe.tool no longer accepts includeSchemas\",\n }),\n );\n }\n\n return describeTool(executor, args.path).pipe(\n Effect.withSpan(\"mcp.tool.dispatch\", {\n attributes: {\n \"mcp.tool.name\": path,\n \"executor.tool.builtin\": true,\n \"executor.tool.target_path\": args.path,\n },\n }),\n );\n }\n return base.invoke({ path, args });\n },\n };\n};\n\n/** Wrap an invoker so every dispatch (success or failure) reports to the\n * host's tool-call hook. A `{ ok: false }` ToolResult envelope counts as a\n * failed call; the hook itself is best-effort (a throwing hook is a host\n * bug, but it must not break tool dispatch). */\nconst withToolCallHook = (\n invoker: SandboxToolInvoker,\n onToolCall: ((info: { readonly path: string; readonly ok: boolean }) => void) | undefined,\n): SandboxToolInvoker => {\n if (onToolCall === undefined) return invoker;\n const report = (path: string, ok: boolean): void => {\n // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: host hook; a throwing hook must not break tool dispatch\n try {\n onToolCall({ path, ok });\n } catch {\n // best-effort by contract\n }\n };\n return {\n invoke: (input) =>\n invoker.invoke(input).pipe(\n Effect.tap((result) =>\n Effect.sync(() => report(input.path, !(isRecord(result) && result.ok === false))),\n ),\n Effect.tapCause(() => Effect.sync(() => report(input.path, false))),\n ),\n };\n};\n\n// ---------------------------------------------------------------------------\n// Execution Engine\n// ---------------------------------------------------------------------------\n\nexport type ExecutionEngine<E extends Cause.YieldableError = CodeExecutionError> = {\n /**\n * Execute code with elicitation handled inline by the provided handler.\n * Use this when the host supports elicitation (e.g. MCP with elicitation capability).\n *\n * Fails with the code executor's typed error `E` (defaults to\n * `CodeExecutionError`). Runtimes surface their own `Data.TaggedError`\n * subclass, which flows through here unchanged.\n */\n readonly execute: (\n code: string,\n options: { readonly onElicitation: ElicitationHandler },\n ) => Effect.Effect<ExecuteResult, E>;\n\n /**\n * Execute code, intercepting the first elicitation as a pause point.\n * Use this when the host doesn't support inline elicitation.\n * Returns either a completed result or a paused execution that can be resumed.\n *\n * `options.autoApprove` treats the caller as the human in the loop: every\n * elicitation is accepted inline, so an approval-gated tool runs to\n * completion instead of pausing. The operator-facing Run/Test panel sets\n * this because clicking Run IS the approval. `block` policies still fail\n * before any elicitation, so this never bypasses a hard block.\n */\n readonly executeWithPause: (\n code: string,\n options?: { readonly autoApprove?: boolean },\n ) => Effect.Effect<ExecutionResult, E>;\n\n /**\n * Resume a paused execution. Returns a completed result, a new pause, or\n * null if the executionId was not found.\n */\n readonly resume: (\n executionId: string,\n response: ResumeResponse,\n ) => Effect.Effect<ExecutionResult | null, E>;\n\n /**\n * True when the engine remembers that an executionId has already settled, even\n * if the replayed outcome has rolled out of the bounded resume cache.\n */\n readonly isExecutionSettled?: (executionId: string) => Effect.Effect<boolean>;\n\n /**\n * Inspect a paused execution without resuming it. Returns null if the id is\n * unknown or has already been resumed.\n */\n readonly getPausedExecution: (executionId: string) => Effect.Effect<PausedExecution | null>;\n\n /** Count of executions currently paused awaiting resume. */\n readonly pausedExecutionCount: () => Effect.Effect<number>;\n\n /** Whether any executions are paused awaiting resume. */\n readonly hasPausedExecutions: () => Effect.Effect<boolean>;\n\n /**\n * Get the dynamic tool description (workflow + namespaces).\n */\n readonly getDescription: Effect.Effect<string>;\n};\n\nexport const createExecutionEngine = <E extends Cause.YieldableError = CodeExecutionError>(\n config: ExecutionEngineConfig<E>,\n): ExecutionEngine<E> => {\n const {\n executor,\n codeExecutor,\n toolDiscoveryProvider = defaultToolDiscoveryProvider,\n onToolCall,\n } = config;\n const pausedExecutions = new Map<string, InternalPausedExecution<E>>();\n // Outcomes of executions that already settled (resumed to completion, hit a\n // new pause, or died while paused). MCP clients retry `resume` when a\n // response gets lost in transit; without this cache the retry of an\n // already-delivered resume answers \"no paused execution\" (observed in\n // production seconds after a successful resume). Bounded FIFO — pause\n // volume is tiny (human approvals), so a small window is plenty.\n const settledOutcomes = new Map<string, Exit.Exit<ExecutionResult, E>>();\n const SETTLED_OUTCOME_LIMIT = 64;\n const settledExecutionIds = new Set<string>();\n const SETTLED_EXECUTION_ID_LIMIT = 1024;\n // Resumes whose outcome is still being computed, so a concurrent duplicate\n // awaits the same result instead of missing the (already-consumed) pause.\n const pendingResumes = new Map<string, Deferred.Deferred<ExecutionResult, E>>();\n\n // Exits (not just successes) so a replayed failure re-fails through the\n // typed channel — hosts render engine failures opaquely, and a replay must\n // not bypass that by flattening the cause into result text.\n const recordSettledOutcome = (executionId: string, exit: Exit.Exit<ExecutionResult, E>): void => {\n settledExecutionIds.add(executionId);\n while (settledExecutionIds.size > SETTLED_EXECUTION_ID_LIMIT) {\n const oldest = settledExecutionIds.keys().next().value;\n if (oldest === undefined) break;\n settledExecutionIds.delete(oldest);\n }\n settledOutcomes.set(executionId, exit);\n while (settledOutcomes.size > SETTLED_OUTCOME_LIMIT) {\n const oldest = settledOutcomes.keys().next().value;\n if (oldest === undefined) break;\n settledOutcomes.delete(oldest);\n }\n };\n\n /**\n * Race a running fiber against the pause queue. Returns when either\n * the fiber completes or an elicitation handler fires (whichever\n * comes first). Re-used by both executeWithPause and resume.\n *\n * `Effect.raceFirst` (not `Effect.race`) — `race` has prefer-success\n * semantics in Effect v4 (\"first successful result\"), which means a\n * fiber failure waits indefinitely for the pause Deferred to succeed.\n * For a fast `codeExecutor.execute` failure (e.g. a syntax error\n * inside the dynamic worker) the pause signal never fires, so the\n * outer Effect hangs until the upstream client gives up. `raceFirst`\n * settles on whichever side completes first, success or failure.\n */\n const awaitCompletionOrPause = (\n fiber: Fiber.Fiber<ExecuteResult, E>,\n pauseQueue: Queue.Queue<InternalPausedExecution<E>>,\n ): Effect.Effect<ExecutionResult, E> =>\n Effect.raceFirst(\n Fiber.join(fiber).pipe(\n Effect.map((result): ExecutionResult => ({ status: \"completed\", result })),\n ),\n Queue.take(pauseQueue).pipe(\n Effect.map((paused): ExecutionResult => ({ status: \"paused\", execution: paused })),\n ),\n );\n\n /**\n * Start an execution in pause/resume mode.\n *\n * The sandbox is forked as a daemon because paused executions can outlive the\n * caller scope that returned the first pause, such as an HTTP request handler.\n */\n const startPausableExecution = Effect.fn(\"mcp.execute\")(function* (\n code: string,\n options?: { readonly autoApprove?: boolean },\n ) {\n yield* Effect.annotateCurrentSpan({\n \"mcp.execute.mode\": \"pausable\",\n \"mcp.execute.code_length\": code.length,\n });\n\n // Operator-approved invoke: run through the inline path with an accept-all\n // handler so an approval gate resolves itself instead of pausing. Never\n // pauses, so the caller always gets a completed result.\n if (options?.autoApprove) {\n yield* Effect.annotateCurrentSpan({ \"mcp.execute.auto_approve\": true });\n const result = yield* runInlineExecution(code, { onElicitation: acceptAllHandler });\n return { status: \"completed\", result } satisfies ExecutionResult;\n }\n\n // Queue preserves pauses that arrive before the previous approval has\n // returned to the caller, which can happen with concurrent tool calls.\n const pauseQueue = yield* Queue.unbounded<InternalPausedExecution<E>>();\n\n // Will be set once the fiber is forked.\n let fiber: Fiber.Fiber<ExecuteResult, E>;\n\n const elicitationHandler: ElicitationHandler = (ctx) =>\n Effect.gen(function* () {\n const responseDeferred = yield* Deferred.make<typeof ElicitationResponse.Type>();\n // Globally unique — engine instances are rebuilt on host restarts\n // (Durable Object cold restores, redeploys), so a counter would\n // re-mint the same ids and let a stale client resume bind to a\n // different execution's pause.\n const id = `exec_${crypto.randomUUID()}`;\n\n const paused: InternalPausedExecution<E> = {\n id,\n elicitationContext: ctx,\n response: responseDeferred,\n fiber: fiber!,\n pauseQueue,\n };\n pausedExecutions.set(id, paused);\n\n yield* Queue.offer(pauseQueue, paused);\n\n // Suspend until resume() completes responseDeferred.\n return yield* Deferred.await(responseDeferred);\n });\n\n const invoker = withToolCallHook(\n makeFullInvoker(executor, { onElicitation: elicitationHandler }, toolDiscoveryProvider),\n onToolCall,\n );\n fiber = yield* Effect.forkDetach(\n codeExecutor.execute(code, invoker).pipe(Effect.withSpan(\"executor.code.exec\")),\n );\n\n // When the fiber settles on its own (sandbox timeout, failure) while\n // pauses are still outstanding, drop them: getPausedExecution must not\n // report a pause whose fiber can no longer consume a response, and the\n // map must not grow forever. A resume retry still finds the terminal\n // outcome via the settled-outcome cache.\n const sandboxFiber = fiber;\n yield* Effect.forkDetach(\n Fiber.await(sandboxFiber).pipe(\n Effect.flatMap((exit) =>\n Effect.sync(() => {\n const outcome = Exit.map(\n exit,\n (result): ExecutionResult => ({ status: \"completed\", result }),\n );\n for (const [id, paused] of pausedExecutions) {\n if (paused.fiber !== sandboxFiber) continue;\n pausedExecutions.delete(id);\n recordSettledOutcome(id, outcome);\n }\n }),\n ),\n ),\n );\n\n return (yield* awaitCompletionOrPause(fiber, pauseQueue)) as ExecutionResult;\n });\n\n /**\n * Resume a paused execution. Completes the response Deferred to unblock the\n * fiber, then races completion against the next queued or future pause.\n *\n * Idempotent per executionId: MCP clients retry `resume` when a response is\n * lost in transit, so a duplicate of an already-delivered resume replays the\n * recorded outcome, and a duplicate that arrives while the first is still\n * in flight awaits the same outcome instead of reporting a missing pause.\n */\n const resumeExecution = Effect.fn(\"mcp.execute.resume\")(function* (\n executionId: string,\n response: ResumeResponse,\n ) {\n yield* Effect.annotateCurrentSpan({\n \"mcp.execute.resume.action\": response.action,\n });\n\n const settled = settledOutcomes.get(executionId);\n if (settled) {\n yield* Effect.annotateCurrentSpan({ \"mcp.execute.resume.replayed\": true });\n return (yield* settled) as ExecutionResult;\n }\n\n const pending = pendingResumes.get(executionId);\n if (pending) {\n yield* Effect.annotateCurrentSpan({ \"mcp.execute.resume.joined_inflight\": true });\n return (yield* Deferred.await(pending)) as ExecutionResult;\n }\n\n const paused = pausedExecutions.get(executionId);\n if (!paused) return null;\n pausedExecutions.delete(executionId);\n\n const inflight = yield* Deferred.make<ExecutionResult, E>();\n pendingResumes.set(executionId, inflight);\n\n yield* Deferred.succeed(paused.response, {\n action: response.action as typeof ElicitationResponse.Type.action,\n content: response.content,\n });\n\n return (yield* awaitCompletionOrPause(paused.fiber, paused.pauseQueue).pipe(\n Effect.onExit((exit) =>\n Effect.gen(function* () {\n recordSettledOutcome(executionId, exit);\n pendingResumes.delete(executionId);\n yield* Deferred.done(inflight, exit);\n }),\n ),\n )) as ExecutionResult;\n });\n\n /**\n * Inline-elicitation execute path. Wrapped so every call produces an\n * `mcp.execute` span with the inner `executor.code.exec` as a child.\n */\n const runInlineExecution = Effect.fn(\"mcp.execute\")(function* (\n code: string,\n options: { readonly onElicitation: ElicitationHandler },\n ) {\n yield* Effect.annotateCurrentSpan({\n \"mcp.execute.mode\": \"inline\",\n \"mcp.execute.code_length\": code.length,\n });\n const invoker = withToolCallHook(\n makeFullInvoker(\n executor,\n {\n onElicitation: options.onElicitation,\n },\n toolDiscoveryProvider,\n ),\n onToolCall,\n );\n return yield* codeExecutor.execute(code, invoker).pipe(Effect.withSpan(\"executor.code.exec\"));\n });\n\n return {\n execute: runInlineExecution,\n executeWithPause: startPausableExecution,\n resume: resumeExecution,\n isExecutionSettled: (executionId) => Effect.sync(() => settledExecutionIds.has(executionId)),\n getPausedExecution: (executionId) =>\n Effect.sync(() => pausedExecutions.get(executionId) ?? null),\n pausedExecutionCount: () => Effect.sync(() => pausedExecutions.size),\n hasPausedExecutions: () => Effect.sync(() => pausedExecutions.size > 0),\n getDescription: buildExecuteDescription(executor),\n };\n};\n","// ---------------------------------------------------------------------------\n// `tools.use({ need })` — the make-it-work button (docs/v2/03-plugin-reset.md\n// in the hq repo; founder-notes round 23).\n//\n// One call that collapses the workshop's resolve → add → connect dance into\n// a single press. Every press returns exactly one of:\n//\n// ready — the provider's tools are wired and callable now\n// pending — everything done except the human credential click; the reply\n// carries the approval URL. Pressing again for the same need\n// reports ready-or-still-waiting (the re-press IS the status\n// check; connections.createHandoff is idempotent).\n// compare — the explicitly deliberate mode: the full ranked set\n// no_match — an honest no, plus the catalog taxonomy\n// error — a step failed; the reply says which and why\n//\n// The reply always carries ONE provider, its `sponsored` label, and the\n// COUNT of verified alternatives — never an unsolicited name list (a name\n// list would let the model's stale training prior do the choosing). The\n// override is `not:` on the same button (\"make email work, not resend\"):\n// the serving rule re-runs over the remaining candidates, so a rejected\n// sponsor's quality-equal rival can win the rebound.\n//\n// Implementation: this module only composes the SandboxToolInvoker's\n// dispatches of the existing workshop tools (integrations.resolve,\n// openapi.addSpec / mcp.addServer, connections.createHandoff / create).\n// Riding the invoker means approval gates and elicitation pauses apply\n// exactly as if the sandbox code had called each step itself — no new\n// trust surface, no bypassed approvals.\n// ---------------------------------------------------------------------------\n\nimport { Cause, Effect, Option, Predicate, Schema } from \"effect\";\n\n// Sandbox-visible workshop addresses (the same paths the execute skill used\n// to teach agents to call by hand).\nconst RESOLVE_PATH = \"executor.coreTools.integrations.resolve\";\nconst CONNECTIONS_LIST_PATH = \"executor.coreTools.connections.list\";\nconst INTEGRATIONS_LIST_PATH = \"executor.coreTools.integrations.list\";\nconst CONNECTION_CREATE_PATH = \"executor.coreTools.connections.create\";\nconst CREATE_HANDOFF_PATH = \"executor.coreTools.connections.createHandoff\";\nconst OPENAPI_ADD_PATH = \"executor.openapi.addSpec\";\nconst MCP_ADD_PATH = \"executor.mcp.addServer\";\n\ntype InvokeFn = (input: {\n readonly path: string;\n readonly args: unknown;\n}) => Effect.Effect<unknown, unknown, never>;\n\nexport interface UseNeedArgs {\n readonly need: string;\n readonly not?: readonly string[];\n readonly compare?: boolean;\n}\n\n/** The one card a reply carries. `sponsored` is the label — mandatory,\n * machine-readable, on every reply that names a provider. */\ninterface ProviderCard {\n readonly id: string;\n readonly name: string;\n readonly domain: string;\n readonly category: string;\n readonly sponsored: boolean;\n readonly offer?: { readonly terms: string; readonly url: string };\n readonly verifiedAt: string;\n}\n\nexport type UseNeedReply =\n | {\n readonly status: \"ready\";\n readonly provider: ProviderCard;\n readonly alternativesCount: number;\n readonly toolsPath: string | undefined;\n readonly instructions: string;\n }\n | {\n readonly status: \"pending\";\n readonly provider: ProviderCard;\n readonly alternativesCount: number;\n readonly url: string;\n readonly instructions: string;\n }\n | {\n readonly status: \"compare\";\n readonly candidates: readonly ProviderCard[];\n readonly instructions: string;\n }\n | {\n readonly status: \"manual\";\n readonly provider: ProviderCard;\n readonly alternativesCount: number;\n readonly installCommand: string;\n readonly instructions: string;\n }\n | {\n readonly status: \"no_match\";\n readonly categories: readonly string[];\n readonly instructions: string;\n }\n | {\n readonly status: \"error\";\n readonly step: string;\n readonly message: string;\n readonly instructions: string;\n };\n\n// --- envelope handling ------------------------------------------------------\n// Dynamic/static tool dispatches come back as ToolResult envelopes\n// ({ ok: true, data } | { ok: false, error }); unwrap them per step so a\n// failed step surfaces honestly as `status: \"error\"` with the step name.\n\ninterface StepFailure {\n readonly step: string;\n readonly message: string;\n readonly code?: string;\n}\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === \"object\" && value !== null && !Array.isArray(value);\n\nconst StepErrorShape = Schema.Struct({\n message: Schema.optional(Schema.String),\n code: Schema.optional(Schema.String),\n});\nconst decodeStepError = Schema.decodeUnknownOption(StepErrorShape);\n\nconst unwrap = (step: string, result: unknown): Record<string, unknown> | StepFailure => {\n if (!isRecord(result)) return { step, message: \"tool returned a non-object result\" };\n if (result.ok === true && isRecord(result.data)) return result.data;\n if (result.ok === false) {\n const { message, code } = Option.getOrElse(\n decodeStepError(result.error),\n (): typeof StepErrorShape.Type => ({}),\n );\n return {\n step,\n message: message ?? \"tool call failed\",\n ...(code !== undefined ? { code } : {}),\n };\n }\n // Raw (non-enveloped) success value from a builtin-style tool.\n return result;\n};\n\nconst isFailure = (value: Record<string, unknown> | StepFailure): value is StepFailure =>\n \"step\" in value && \"message\" in value && typeof value.step === \"string\" && !(\"ok\" in value);\n\n// --- resolve payload readers -------------------------------------------------\n\nconst ResolveEntry = Schema.Struct({\n id: Schema.String,\n name: Schema.String,\n domain: Schema.String,\n category: Schema.String,\n sponsored: Schema.Boolean,\n offer: Schema.optional(Schema.Struct({ terms: Schema.String, url: Schema.String })),\n verifiedAt: Schema.String,\n suggestedSlug: Schema.String,\n addPath: Schema.Union([\n Schema.Struct({ kind: Schema.Literal(\"openapi\"), specUrl: Schema.String }),\n Schema.Struct({ kind: Schema.Literal(\"mcp\"), serverUrl: Schema.String }),\n Schema.Struct({ kind: Schema.Literal(\"cli\"), package: Schema.String }),\n ]),\n authTemplate: Schema.optional(Schema.String),\n connect: Schema.optional(Schema.Struct({ auth: Schema.String, instructions: Schema.String })),\n});\ntype ResolveEntry = typeof ResolveEntry.Type;\n\nconst decodeResolveEntry = Schema.decodeUnknownOption(ResolveEntry);\n\nconst asEntry = (value: unknown): ResolveEntry | undefined =>\n Option.getOrUndefined(decodeResolveEntry(value));\n\nconst toCard = (entry: ResolveEntry): ProviderCard => ({\n id: entry.id,\n name: entry.name,\n domain: entry.domain,\n category: entry.category,\n sponsored: entry.sponsored,\n ...(entry.offer !== undefined ? { offer: entry.offer } : {}),\n verifiedAt: entry.verifiedAt,\n});\n\nconst labelLine = (entry: ResolveEntry): string =>\n entry.sponsored\n ? `${entry.name} is a SPONSORED placement (verified; it holds the default slot only at quality >= the best organic option${entry.offer ? `; offer: ${entry.offer.terms}` : \"\"}).`\n : `${entry.name} is the organic (unsponsored) best for this category.`;\n\nconst overrideLine = (need: string, entry: ResolveEntry, alternativesCount: number): string =>\n alternativesCount > 0\n ? `${alternativesCount} verified alternative${alternativesCount === 1 ? \"\" : \"s\"} exist${alternativesCount === 1 ? \"s\" : \"\"} — to override, press again with not: [\"${entry.id}\"], or pass compare: true to see the full ranked set for \"${need}\".`\n : `No other verified providers cover this need right now.`;\n\n// --- the button --------------------------------------------------------------\n\nexport const runUseNeed = (invoke: InvokeFn, rawArgs: unknown): Effect.Effect<UseNeedReply> =>\n Effect.gen(function* () {\n if (!isRecord(rawArgs) || typeof rawArgs.need !== \"string\" || rawArgs.need.length === 0) {\n return {\n status: \"error\" as const,\n step: \"arguments\",\n message:\n 'tools.use expects { need: string; not?: string[]; compare?: boolean } — e.g. tools.use({ need: \"transactional email\" })',\n instructions: \"Call again with a need string.\",\n };\n }\n const need = rawArgs.need;\n const not = Array.isArray(rawArgs.not)\n ? rawArgs.not.filter((entry): entry is string => typeof entry === \"string\")\n : [];\n const compare = rawArgs.compare === true;\n\n const fail = (failure: StepFailure): UseNeedReply => ({\n status: \"error\",\n step: failure.step,\n message: failure.message,\n instructions:\n \"The step named above failed. You can retry, or fall back to the workshop tools (integrations.resolve, openapi.addSpec, connections.createHandoff) to do it stepwise.\",\n });\n\n const dispatch = (\n step: string,\n path: string,\n args: unknown,\n ): Effect.Effect<Record<string, unknown> | StepFailure, never, never> =>\n invoke({ path, args }).pipe(\n Effect.map((result) => unwrap(step, result)),\n Effect.catchCause((cause) => {\n const failure = cause.reasons.find(Cause.isFailReason)?.error;\n const { message } = Predicate.isError(failure)\n ? failure\n : Option.getOrElse(decodeStepError(failure), (): typeof StepErrorShape.Type => ({}));\n return Effect.succeed<StepFailure>({ step, message: message ?? \"tool call failed\" });\n }),\n );\n\n // 1. Resolve the need — ONE labeled default under the serving rule; the\n // `not:` exclusions re-run the rule over the remaining candidates.\n // Funnel events (need_stated with phrasing + excludedCount,\n // default_shown) fire inside the resolver seam.\n const resolved = yield* dispatch(\"resolve\", RESOLVE_PATH, { need, exclude: not });\n if (isFailure(resolved)) return fail(resolved);\n\n if (resolved.matched !== true) {\n const categories = Array.isArray(resolved.categories)\n ? resolved.categories.filter((c): c is string => typeof c === \"string\")\n : [];\n return {\n status: \"no_match\" as const,\n categories,\n instructions:\n categories.length > 0\n ? `No verified provider matches \"${need}\". The catalog covers: ${categories.join(\", \")}. Press again phrasing the need with one of these, or name a provider directly.`\n : `No provider catalog is configured in this runtime; fall back to asking the user.`,\n };\n }\n\n const entry = asEntry(resolved.default);\n if (entry === undefined) {\n return fail({ step: \"resolve\", message: \"resolver returned a match without a default\" });\n }\n const alternatives: readonly unknown[] = Array.isArray(resolved.alternatives)\n ? resolved.alternatives\n : [];\n const alternativesCount = alternatives.length;\n const phrasing = resolved.phrasing === \"brand\" ? \"brand\" : \"category\";\n\n // 2. The deliberate mode: the full ranked set, on request only.\n if (compare) {\n const candidates = [entry, ...alternatives.map(asEntry).filter(Predicate.isNotUndefined)].map(\n toCard,\n );\n return {\n status: \"compare\" as const,\n candidates,\n instructions:\n \"Ranked by verified quality; serving order. Sponsored entries are labeled. Press use() again naming your choice (brand phrasing is honored verbatim).\",\n };\n }\n\n // 3. Memory: a need already fulfilled returns instantly. For category\n // phrasing any ranked candidate with a live connection counts (the\n // developer already chose); a brand ask only matches its own slug —\n // an explicit ask is never redirected to what happens to be wired.\n const connectionsResult = yield* dispatch(\"connections.list\", CONNECTIONS_LIST_PATH, {});\n if (isFailure(connectionsResult)) return fail(connectionsResult);\n const connections = Array.isArray(connectionsResult.connections)\n ? connectionsResult.connections.filter(isRecord)\n : [];\n const rankedEntries = [entry, ...alternatives.map(asEntry).filter(Predicate.isNotUndefined)];\n const candidateSlugs =\n phrasing === \"brand\" ? [entry.suggestedSlug] : rankedEntries.map((e) => e.suggestedSlug);\n for (const slug of candidateSlugs) {\n const existing = connections.find((connection) => connection.integration === slug);\n if (existing !== undefined) {\n const adopted = rankedEntries.find((e) => e.suggestedSlug === slug) ?? entry;\n return {\n status: \"ready\" as const,\n provider: toCard(adopted),\n alternativesCount,\n toolsPath: typeof existing.address === \"string\" ? existing.address : undefined,\n instructions: `Already wired: you have a ${adopted.name} connection. ${labelLine(adopted)} Call its tools under the address above (tools.search({ namespace: \"${slug}\" }) enumerates them).`,\n };\n }\n }\n\n // 3.5 CLI providers live in the pressing agent's own environment, not in\n // raf's tool namespace: the runtime never executes host shell\n // commands (that would be a new trust surface), so the button\n // delegates the install to the agent — one provider, the sponsored\n // label, the exact command. Serving semantics (label, count,\n // override) apply exactly as for wired providers.\n if (entry.addPath.kind === \"cli\") {\n const pkg = entry.addPath.package;\n const installCommand = `npm install -g ${pkg}`;\n const authNote =\n entry.authTemplate !== undefined && entry.authTemplate.length > 0\n ? ` — auth: ${entry.authTemplate}`\n : (entry.connect?.instructions ?? \"\");\n return {\n status: \"manual\" as const,\n provider: toCard(entry),\n alternativesCount,\n installCommand,\n instructions: `${labelLine(entry)} ${entry.name} ships as a CLI (npm package ${pkg}), not an API integration: install it in YOUR environment (\\`${installCommand}\\`, or ad hoc via \\`npx -y ${pkg}\\`), then authenticate per its own flow${authNote}. It will not appear under raf's tools. ${overrideLine(need, entry, alternativesCount)}`,\n };\n }\n\n // 4. Add the integration (idempotent: an already-added integration just\n // skips to the connect step). Approval-gated adds pause here through\n // the normal elicitation flow — the pause IS part of the button.\n const integrationsResult = yield* dispatch(\"integrations.list\", INTEGRATIONS_LIST_PATH, {});\n if (isFailure(integrationsResult)) return fail(integrationsResult);\n const integrations = Array.isArray(integrationsResult.integrations)\n ? integrationsResult.integrations.filter(isRecord)\n : [];\n const alreadyAdded = integrations.some(\n (integration) => integration.slug === entry.suggestedSlug,\n );\n\n if (!alreadyAdded) {\n if (entry.addPath.kind === \"openapi\") {\n const added = yield* dispatch(\"openapi.addSpec\", OPENAPI_ADD_PATH, {\n spec: { kind: \"url\", url: entry.addPath.specUrl },\n slug: entry.suggestedSlug,\n name: entry.name,\n });\n if (isFailure(added) && added.code !== \"integration_already_exists\") return fail(added);\n } else if (entry.addPath.kind === \"mcp\") {\n const added = yield* dispatch(\"mcp.addServer\", MCP_ADD_PATH, {\n name: entry.name,\n endpoint: entry.addPath.serverUrl,\n slug: entry.suggestedSlug,\n });\n if (isFailure(added) && added.code !== \"integration_already_exists\") return fail(added);\n }\n }\n\n // 5. Connect. No-auth wires immediately (borrowing the owner already in\n // use); every credential-bearing shape routes to the browser handoff —\n // the human mints the secret in the web UI, never through chat. The\n // handoff URL is idempotent: a re-press of use() for the same need\n // lands back here and reports pending with the same URL until the\n // click happens (then step 3 catches it as ready).\n if (entry.connect?.auth === \"none\") {\n const owner = connections\n .map((connection) => connection.owner)\n .find((value): value is string => typeof value === \"string\");\n if (owner !== undefined) {\n const created = yield* dispatch(\"connections.create\", CONNECTION_CREATE_PATH, {\n owner,\n name: \"main\",\n integration: entry.suggestedSlug,\n template: \"none\",\n });\n if (!isFailure(created)) {\n return {\n status: \"ready\" as const,\n provider: toCard(entry),\n alternativesCount,\n toolsPath: `tools.${entry.suggestedSlug}.${owner}.main`,\n instructions: `${entry.name} is wired (no auth needed). ${labelLine(entry)} ${overrideLine(need, entry, alternativesCount)} Find its tools with tools.search({ namespace: \"${entry.suggestedSlug}\" }).`,\n };\n }\n // Fall through to the handoff below: the web UI can always finish it.\n }\n }\n\n const handoff = yield* dispatch(\"connections.createHandoff\", CREATE_HANDOFF_PATH, {\n integration: entry.suggestedSlug,\n });\n if (isFailure(handoff)) return fail(handoff);\n const url = typeof handoff.url === \"string\" ? handoff.url : \"\";\n\n return {\n status: \"pending\" as const,\n provider: toCard(entry),\n alternativesCount,\n url,\n instructions: `${labelLine(entry)} One human step left: give the user this URL — they mint the ${entry.name} credential and enter it in the web UI (never paste secrets into chat). Press use({ need: \"${need}\" }) again any time to check: it returns ready once the connection exists. ${overrideLine(need, entry, alternativesCount)}`,\n };\n });\n"],"mappings":";AAAA,YAAY,UAAU;AAUtB,SAAS,0BAA0B;AAR5B,IAAM,qBAAN,cAAsC,iBAAY,oBAAoB,EAG1E;AAAC;;;ACLJ,SAAS,QAAQ,iBAAiB;AAClC,YAAY,WAAW;AASvB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAIP,IAAM,wBAAwB;AAC9B,IAAM,iCAAiC;AACvC,IAAM,wBACJ;AAIF,IAAM,4BAA4B;AAClC,IAAM,uBACJ;AAEF,IAAM,uBAAuB,CAAC,qBAC5B,qBAAqB,oBAAoB,SAAS;AAEpD,IAAM,4BAA4B,CAChC,iBAC4B;AAAA,EAC5B,GAAI,eAAe,CAAC;AAAA,EACpB,WAAW;AAAA,EACX,cAAc;AAAA,EACd,UAAU;AACZ;AAEA,IAAM,iBAAiB;AAcvB,IAAM,gBAAgB,CAAC,SAA8B;AACnD,MAAI,KAAK,WAAW,cAAc,EAAG,QAAO,YAAY,KAAK,IAAI;AACjE,MAAI,iBAAiB,GAAG,cAAc,GAAG,IAAI,EAAE,GAAG;AAChD,WAAO,YAAY,KAAK,GAAG,cAAc,GAAG,IAAI,EAAE;AAAA,EACpD;AACA,SAAO,YAAY,KAAK,IAAI;AAC9B;AAIA,IAAM,gBAAgB,CAAC,YACrB,QAAQ,WAAW,cAAc,I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AAQ;AAAA,QACzB;AAAA,QACA;AAAA,MACF;AAAA,MACA;AAAA,IACF;AACA,WAAO,OAAO,aAAa,QAAQ,MAAM,OAAO,EAAE,KAAKA,QAAO,SAAS,oBAAoB,CAAC;AAAA,EAC9F,CAAC;AAED,SAAO;AAAA,IACL,SAAS;AAAA,IACT,kBAAkB;AAAA,IAClB,QAAQ;AAAA,IACR,oBAAoB,CAAC,gBAAgBA,QAAO,KAAK,MAAM,oBAAoB,IAAI,WAAW,CAAC;AAAA,IAC3F,oBAAoB,CAAC,gBACnBA,QAAO,KAAK,MAAM,iBAAiB,IAAI,WAAW,KAAK,IAAI;AAAA,IAC7D,sBAAsB,MAAMA,QAAO,KAAK,MAAM,iBAAiB,IAAI;AAAA,IACnE,qBAAqB,MAAMA,QAAO,KAAK,MAAM,iBAAiB,OAAO,CAAC;AAAA,IACtE,gBAAgB,wBAAwB,QAAQ;AAAA,EAClD;AACF;","names":["Effect","Effect","Predicate","Cause","Effect","Predicate","entry","Effect","parts","Predicate","isRecord"]}
|
package/dist/core.js
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
listExecutorIntegrations,
|
|
12
12
|
makeExecutorToolInvoker,
|
|
13
13
|
searchTools
|
|
14
|
-
} from "./chunk-
|
|
14
|
+
} from "./chunk-RBNHDS2R.js";
|
|
15
15
|
|
|
16
16
|
// src/skills.ts
|
|
17
17
|
var EXECUTE_SKILL_BODY = [
|
|
@@ -87,7 +87,7 @@ var RESOLVE_SKILL_BODY = [
|
|
|
87
87
|
].join("\n");
|
|
88
88
|
var RESOLVE_SKILL = {
|
|
89
89
|
name: "resolve",
|
|
90
|
-
summary: "What to do when
|
|
90
|
+
summary: "What to do when a task explicitly calls for a service capability (email, database, auth, payments, DNS, hosting, ...): press tools.use({ need }) \u2014 one call resolves it to a labeled default provider (connected providers resolve through the same press), adds it, and connects it.",
|
|
91
91
|
body: RESOLVE_SKILL_BODY
|
|
92
92
|
};
|
|
93
93
|
var SKILLS = [EXECUTE_SKILL, RESOLVE_SKILL];
|
package/dist/core.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/skills.ts"],"sourcesContent":["/**\n * On-demand documentation served through the MCP `skills` tool.\n *\n * The long-form how-to for calling integrations used to live in the `execute`\n * tool description, which every session loads into its prompt up front. That\n * is pure context bloat: a model that never calls `execute` still pays for the\n * whole calling-convention essay. Moving it behind a tool lets the `execute`\n * description carry only the live connection inventory plus a pointer here, and\n * the model fetches the full guide the moment it actually needs it.\n *\n * A skill is a static, named markdown document. The registry is intentionally\n * tiny and hand-curated, not a plugin surface: add an entry only when a tool\n * genuinely needs its own how-to behind the same `skills` door.\n */\n\nexport interface Skill {\n /** Stable identifier the model passes to the `skills` tool. */\n readonly name: string;\n /** One-line summary, shown when the `skills` tool lists what is available. */\n readonly summary: string;\n /** The full markdown body returned when the skill is fetched by name. */\n readonly body: string;\n}\n\n// The `execute` how-to. This is the body lifted verbatim out of the old\n// `buildExecuteDescription` (Workflow + Rules); the description now points\n// here instead of inlining it.\nconst EXECUTE_SKILL_BODY = [\n \"# execute\",\n \"\",\n \"Execute TypeScript in a sandboxed runtime with access to configured API tools.\",\n \"\",\n \"## Workflow\",\n \"\",\n '1. `const { items: matches } = await tools.search({ query: \"<intent + key nouns>\", limit: 12 });`',\n '2. `const path = matches[0]?.path;` — when nothing matches because the capability is not connected yet (no email provider, no database, ...), do not give up: fetch `skills({ name: \"resolve\" })` and resolve the need to a provider.',\n \"3. `const details = await tools.describe.tool({ path });`\",\n \"4. Use `details.inputTypeScript` / `details.outputTypeScript` and `details.typeScriptDefinitions` for compact shapes.\",\n \"5. Use `tools.executor.coreTools.connections.list({})` when you need live saved-connection inventory.\",\n \"6. Call the tool: `const result = await tools.<path>(input);`\",\n \"\",\n \"## Rules\",\n \"\",\n \"- `tools.search()` returns paginated, ranked matches: `{ items, total, hasMore, nextOffset }`. Best-first. Use short intent phrases like `github issues`, `repo details`, or `create calendar event`.\",\n '- When you already know the namespace, narrow with `tools.search({ namespace: \"github\", query: \"issues\" })`.',\n \"- `tools.executor.coreTools.connections.list({})` returns saved connections with `{ address, integration, owner, name, ... }`. The `address` field includes the leading `tools.` root.\",\n \"- Tool calls return a value union: `{ ok: true, data }` for success or `{ ok: false, error: { code, message, status?, details?, retryable? } }` for expected tool/domain failures. Branch on `result.ok`.\",\n \"- `data` is the upstream payload itself. HTTP-backed tools (OpenAPI) also set `http: { status, headers }` beside `data` — read `result.http?.headers` for pagination (Link) or rate-limit headers.\",\n \"- Use `emit(value)` to append user-visible output and return `undefined`. Plain values become MCP text content. MCP content blocks are forwarded as-is. `ToolFile` values are rendered by MIME. Emitted output goes to the user, not back to you; the result envelope reports an `emitted` count so you can confirm it landed, but to read a value yourself, `return` it.\",\n '- File-returning tools may return `ToolFile` values: `{ _tag: \"ToolFile\", name?, mimeType, encoding: \"base64\", data, byteLength }`. Emit any attachment with `emit(result.data)`.',\n '- To emit MCP-native content directly, pass an MCP content block to `emit(...)`, such as `{ type: \"image\", data, mimeType }`, `{ type: \"audio\", data, mimeType }`, `{ type: \"text\", text }`, `{ type: \"resource\", resource }`, or `{ type: \"resource_link\", uri, name, ... }`.',\n \"- `emit(ToolFile)` is MIME-based: `image/*` becomes MCP image content, `audio/*` becomes MCP audio content, text-like files become decoded text, and other binary files become embedded MCP resources.\",\n \"- `return` is only for ordinary structured data. Returning a `ToolFile`, a `ToolResult`, an MCP content block, or a bare base64 string does not emit content to the MCP client.\",\n \"- Some providers, including Gmail, return attachment bytes without a public URL. To send that attachment to another API from code, decode `ToolFile.data` from base64 and pass the bytes to that API's upload/file input.\",\n \"- If `tools.search()` returns `hasMore: true` and you didn't find what you need, fetch the next page: `tools.search({ query, offset: nextOffset, limit })`.\",\n \"- Always use the full address when calling tools: `tools.<integration>.<owner>.<connection>.<tool>(args)`. The `path` returned by `tools.search()` / `tools.describe.tool()` is already the exact path under `tools` — call `tools[path]` rather than guessing segments.\",\n \"- The `tools` object is a lazy proxy — enumerating it (`Object.keys(tools)`, spread, `for...in`) throws. Use `tools.search()` or `tools.executor.coreTools.connections.list({})` instead.\",\n '- Pass an object to system tools, e.g. `tools.search({ query: \"...\" })`, `tools.executor.coreTools.connections.list({})`, and `tools.describe.tool({ path })`.',\n '- `tools.describe.tool()` returns compact TypeScript shapes. Use `inputTypeScript`, `outputTypeScript`, and `typeScriptDefinitions`. If the path doesn\\'t resolve, the result carries `error: { code: \"tool_not_found\", suggestions }` — use a suggestion instead of retrying the same path.',\n \"- For tools that return large collections (e.g. `getStates`, `getAll`), filter results in code rather than calling per-item tools.\",\n \"- Do not use `fetch` — all API calls go through `tools.*`.\",\n \"- If execution pauses for interaction, resume it with the returned `resumePayload`.\",\n \"- TypeScript type syntax (`: T`, `as T`, generics, interfaces, type aliases) is stripped before execution — feel free to write idiomatic TypeScript using the shapes from `tools.describe.tool()`. Decorators and `enum` are not supported.\",\n].join(\"\\n\");\n\nexport const EXECUTE_SKILL: Skill = {\n name: \"execute\",\n summary:\n \"How to call integrations from the execute sandbox: search the catalog, read a tool's shape, call it, emit results, and resume paused runs.\",\n body: EXECUTE_SKILL_BODY,\n};\n\n// The choice-layer how-to: what to do when the task needs a service capability\n// that no connected integration covers. The always-loaded `execute`\n// description advertises `tools.use()` directly; this skill carries the\n// details (override, compare, pending) plus the stepwise workshop flow for\n// agents that need manual control.\nconst RESOLVE_SKILL_BODY = [\n \"# resolve\",\n \"\",\n \"Fulfill an external service-capability need — sending email, a database, auth, payments, SMS, DNS, hosting, object storage, search — instead of picking a provider from memory or sending the user off to sign up somewhere.\",\n \"\",\n \"## The button (default path)\",\n \"\",\n 'One call does everything: `const r = await tools.use({ need: \"transactional email\" });` — brand asks work too (`{ need: \"resend\" }`) and are honored verbatim, never rerouted.',\n \"\",\n '- `status: \"ready\"` — the provider\\'s tools are wired and callable now (`toolsPath` / `tools.search({ namespace })`).',\n '- `status: \"pending\"` — one human step left: give the user `r.url` (they mint the credential in the web UI; never paste secrets into chat). Press `use()` again with the same need any time — it returns ready once the connection exists.',\n '- `status: \"manual\"` — the provider ships as a CLI, not an API: run `r.installCommand` in YOUR environment (you have the shell; raf does not execute host commands), then follow the auth flow in `r.instructions`. It will not appear under raf\\'s tools.',\n '- `status: \"no_match\"` — honest no, with `categories` listing what the catalog covers.',\n \"- `r.provider.sponsored` is the label — machine-readable, on every reply. Tell the user which provider was chosen and that it was sponsored when it was.\",\n '- Override: press again with `not: [\"<id>\"]` (\"make email work, not resend\") — the next-best serves under the same quality rule. `compare: true` returns the full ranked set when you genuinely need to weigh options.',\n \"\",\n \"## Stepwise workshop (manual control)\",\n \"\",\n \"The button composes these existing tools; call them yourself when you need to deviate:\",\n \"1. `tools.executor.coreTools.integrations.resolve({ need, exclude? })` → `{ matched, default, alternatives, categories }`.\",\n \"2. Add via the entry's `addPath` with `slug: entry.suggestedSlug` (attribution matches on it): openapi → `tools.executor.openapi.addSpec`; mcp → `tools.executor.mcp.addServer`; cli → install the package and follow its login flow.\",\n '3. Connect per `entry.connect`: `api_key` → `tools.executor.coreTools.connections.createHandoff` (hand the URL to the user); `oauth` → `tools.executor.coreTools.oauth.start`; `none` → `tools.executor.coreTools.connections.create({ template: \"none\", ... })`.',\n \"\",\n \"## Rules\",\n \"\",\n \"- Use the button BEFORE recommending, naming, or signing up for any provider. The catalog's ranking and labels are the source of truth, not model memory.\",\n \"- A need an existing connection already covers returns `ready` instantly — pressing twice is safe and is also how you check on a pending credential.\",\n \"- If `tools.use` is not available in this runtime (no provider catalog configured), fall back to asking the user.\",\n].join(\"\\n\");\n\nexport const RESOLVE_SKILL: Skill = {\n name: \"resolve\",\n summary:\n \"What to do when the task needs a service capability (email, database, auth, payments, DNS, hosting, ...) that nothing connected covers: press tools.use({ need }) — one call resolves it to a labeled default provider, adds it, and connects it.\",\n body: RESOLVE_SKILL_BODY,\n};\n\n/** The full skill catalog. Hand-curated; keep it small. */\nexport const SKILLS: readonly Skill[] = [EXECUTE_SKILL, RESOLVE_SKILL];\n\n/** Look up a skill by its exact name. */\nexport const findSkill = (name: string): Skill | undefined =>\n SKILLS.find((skill) => skill.name === name);\n\n/** The index the `skills` tool returns when called without a name (or with an\n * unknown one): every skill's name and one-line summary, plus how to fetch\n * the body. */\nexport const renderSkillsIndex = (): string =>\n [\n 'Available skills. Fetch one with `skills({ name: \"<name>\" })`.',\n \"\",\n ...SKILLS.map((skill) => `- \\`${skill.name}\\` — ${skill.summary}`),\n ].join(\"\\n\");\n"],"mappings":";;;;;;;;;;;;;;;;AA2BA,IAAM,qBAAqB;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,EAAE,KAAK,IAAI;AAEJ,IAAM,gBAAuB;AAAA,EAClC,MAAM;AAAA,EACN,SACE;AAAA,EACF,MAAM;AACR;AAOA,IAAM,qBAAqB;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,EAAE,KAAK,IAAI;AAEJ,IAAM,gBAAuB;AAAA,EAClC,MAAM;AAAA,EACN,SACE;AAAA,EACF,MAAM;AACR;AAGO,IAAM,SAA2B,CAAC,eAAe,aAAa;AAG9D,IAAM,YAAY,CAAC,SACxB,OAAO,KAAK,CAAC,UAAU,MAAM,SAAS,IAAI;AAKrC,IAAM,oBAAoB,MAC/B;AAAA,EACE;AAAA,EACA;AAAA,EACA,GAAG,OAAO,IAAI,CAAC,UAAU,OAAO,MAAM,IAAI,aAAQ,MAAM,OAAO,EAAE;AACnE,EAAE,KAAK,IAAI;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/skills.ts"],"sourcesContent":["/**\n * On-demand documentation served through the MCP `skills` tool.\n *\n * The long-form how-to for calling integrations used to live in the `execute`\n * tool description, which every session loads into its prompt up front. That\n * is pure context bloat: a model that never calls `execute` still pays for the\n * whole calling-convention essay. Moving it behind a tool lets the `execute`\n * description carry only the live connection inventory plus a pointer here, and\n * the model fetches the full guide the moment it actually needs it.\n *\n * A skill is a static, named markdown document. The registry is intentionally\n * tiny and hand-curated, not a plugin surface: add an entry only when a tool\n * genuinely needs its own how-to behind the same `skills` door.\n */\n\nexport interface Skill {\n /** Stable identifier the model passes to the `skills` tool. */\n readonly name: string;\n /** One-line summary, shown when the `skills` tool lists what is available. */\n readonly summary: string;\n /** The full markdown body returned when the skill is fetched by name. */\n readonly body: string;\n}\n\n// The `execute` how-to. This is the body lifted verbatim out of the old\n// `buildExecuteDescription` (Workflow + Rules); the description now points\n// here instead of inlining it.\nconst EXECUTE_SKILL_BODY = [\n \"# execute\",\n \"\",\n \"Execute TypeScript in a sandboxed runtime with access to configured API tools.\",\n \"\",\n \"## Workflow\",\n \"\",\n '1. `const { items: matches } = await tools.search({ query: \"<intent + key nouns>\", limit: 12 });`',\n '2. `const path = matches[0]?.path;` — when nothing matches because the capability is not connected yet (no email provider, no database, ...), do not give up: fetch `skills({ name: \"resolve\" })` and resolve the need to a provider.',\n \"3. `const details = await tools.describe.tool({ path });`\",\n \"4. Use `details.inputTypeScript` / `details.outputTypeScript` and `details.typeScriptDefinitions` for compact shapes.\",\n \"5. Use `tools.executor.coreTools.connections.list({})` when you need live saved-connection inventory.\",\n \"6. Call the tool: `const result = await tools.<path>(input);`\",\n \"\",\n \"## Rules\",\n \"\",\n \"- `tools.search()` returns paginated, ranked matches: `{ items, total, hasMore, nextOffset }`. Best-first. Use short intent phrases like `github issues`, `repo details`, or `create calendar event`.\",\n '- When you already know the namespace, narrow with `tools.search({ namespace: \"github\", query: \"issues\" })`.',\n \"- `tools.executor.coreTools.connections.list({})` returns saved connections with `{ address, integration, owner, name, ... }`. The `address` field includes the leading `tools.` root.\",\n \"- Tool calls return a value union: `{ ok: true, data }` for success or `{ ok: false, error: { code, message, status?, details?, retryable? } }` for expected tool/domain failures. Branch on `result.ok`.\",\n \"- `data` is the upstream payload itself. HTTP-backed tools (OpenAPI) also set `http: { status, headers }` beside `data` — read `result.http?.headers` for pagination (Link) or rate-limit headers.\",\n \"- Use `emit(value)` to append user-visible output and return `undefined`. Plain values become MCP text content. MCP content blocks are forwarded as-is. `ToolFile` values are rendered by MIME. Emitted output goes to the user, not back to you; the result envelope reports an `emitted` count so you can confirm it landed, but to read a value yourself, `return` it.\",\n '- File-returning tools may return `ToolFile` values: `{ _tag: \"ToolFile\", name?, mimeType, encoding: \"base64\", data, byteLength }`. Emit any attachment with `emit(result.data)`.',\n '- To emit MCP-native content directly, pass an MCP content block to `emit(...)`, such as `{ type: \"image\", data, mimeType }`, `{ type: \"audio\", data, mimeType }`, `{ type: \"text\", text }`, `{ type: \"resource\", resource }`, or `{ type: \"resource_link\", uri, name, ... }`.',\n \"- `emit(ToolFile)` is MIME-based: `image/*` becomes MCP image content, `audio/*` becomes MCP audio content, text-like files become decoded text, and other binary files become embedded MCP resources.\",\n \"- `return` is only for ordinary structured data. Returning a `ToolFile`, a `ToolResult`, an MCP content block, or a bare base64 string does not emit content to the MCP client.\",\n \"- Some providers, including Gmail, return attachment bytes without a public URL. To send that attachment to another API from code, decode `ToolFile.data` from base64 and pass the bytes to that API's upload/file input.\",\n \"- If `tools.search()` returns `hasMore: true` and you didn't find what you need, fetch the next page: `tools.search({ query, offset: nextOffset, limit })`.\",\n \"- Always use the full address when calling tools: `tools.<integration>.<owner>.<connection>.<tool>(args)`. The `path` returned by `tools.search()` / `tools.describe.tool()` is already the exact path under `tools` — call `tools[path]` rather than guessing segments.\",\n \"- The `tools` object is a lazy proxy — enumerating it (`Object.keys(tools)`, spread, `for...in`) throws. Use `tools.search()` or `tools.executor.coreTools.connections.list({})` instead.\",\n '- Pass an object to system tools, e.g. `tools.search({ query: \"...\" })`, `tools.executor.coreTools.connections.list({})`, and `tools.describe.tool({ path })`.',\n '- `tools.describe.tool()` returns compact TypeScript shapes. Use `inputTypeScript`, `outputTypeScript`, and `typeScriptDefinitions`. If the path doesn\\'t resolve, the result carries `error: { code: \"tool_not_found\", suggestions }` — use a suggestion instead of retrying the same path.',\n \"- For tools that return large collections (e.g. `getStates`, `getAll`), filter results in code rather than calling per-item tools.\",\n \"- Do not use `fetch` — all API calls go through `tools.*`.\",\n \"- If execution pauses for interaction, resume it with the returned `resumePayload`.\",\n \"- TypeScript type syntax (`: T`, `as T`, generics, interfaces, type aliases) is stripped before execution — feel free to write idiomatic TypeScript using the shapes from `tools.describe.tool()`. Decorators and `enum` are not supported.\",\n].join(\"\\n\");\n\nexport const EXECUTE_SKILL: Skill = {\n name: \"execute\",\n summary:\n \"How to call integrations from the execute sandbox: search the catalog, read a tool's shape, call it, emit results, and resume paused runs.\",\n body: EXECUTE_SKILL_BODY,\n};\n\n// The choice-layer how-to: what to do when a task explicitly calls for a\n// service capability. The always-loaded `execute` description advertises\n// `tools.use()` directly; this skill carries the details (override, compare,\n// pending) plus the stepwise workshop flow for agents that need manual\n// control.\nconst RESOLVE_SKILL_BODY = [\n \"# resolve\",\n \"\",\n \"Fulfill an external service-capability need — sending email, a database, auth, payments, SMS, DNS, hosting, object storage, search — instead of picking a provider from memory or sending the user off to sign up somewhere.\",\n \"\",\n \"## The button (default path)\",\n \"\",\n 'One call does everything: `const r = await tools.use({ need: \"transactional email\" });` — brand asks work too (`{ need: \"resend\" }`) and are honored verbatim, never rerouted.',\n \"\",\n '- `status: \"ready\"` — the provider\\'s tools are wired and callable now (`toolsPath` / `tools.search({ namespace })`).',\n '- `status: \"pending\"` — one human step left: give the user `r.url` (they mint the credential in the web UI; never paste secrets into chat). Press `use()` again with the same need any time — it returns ready once the connection exists.',\n '- `status: \"manual\"` — the provider ships as a CLI, not an API: run `r.installCommand` in YOUR environment (you have the shell; raf does not execute host commands), then follow the auth flow in `r.instructions`. It will not appear under raf\\'s tools.',\n '- `status: \"no_match\"` — honest no, with `categories` listing what the catalog covers.',\n \"- `r.provider.sponsored` is the label — machine-readable, on every reply. Tell the user which provider was chosen and that it was sponsored when it was.\",\n '- Override: press again with `not: [\"<id>\"]` (\"make email work, not resend\") — the next-best serves under the same quality rule. `compare: true` returns the full ranked set when you genuinely need to weigh options.',\n \"\",\n \"## Stepwise workshop (manual control)\",\n \"\",\n \"The button composes these existing tools; call them yourself when you need to deviate:\",\n \"1. `tools.executor.coreTools.integrations.resolve({ need, exclude? })` → `{ matched, default, alternatives, categories }`.\",\n \"2. Add via the entry's `addPath` with `slug: entry.suggestedSlug` (attribution matches on it): openapi → `tools.executor.openapi.addSpec`; mcp → `tools.executor.mcp.addServer`; cli → install the package and follow its login flow.\",\n '3. Connect per `entry.connect`: `api_key` → `tools.executor.coreTools.connections.createHandoff` (hand the URL to the user); `oauth` → `tools.executor.coreTools.oauth.start`; `none` → `tools.executor.coreTools.connections.create({ template: \"none\", ... })`.',\n \"\",\n \"## Rules\",\n \"\",\n \"- Use the button BEFORE recommending, naming, or signing up for any provider. The catalog's ranking and labels are the source of truth, not model memory.\",\n \"- A need an existing connection already covers returns `ready` instantly — pressing twice is safe and is also how you check on a pending credential.\",\n \"- If `tools.use` is not available in this runtime (no provider catalog configured), fall back to asking the user.\",\n].join(\"\\n\");\n\nexport const RESOLVE_SKILL: Skill = {\n name: \"resolve\",\n summary:\n \"What to do when a task explicitly calls for a service capability (email, database, auth, payments, DNS, hosting, ...): press tools.use({ need }) — one call resolves it to a labeled default provider (connected providers resolve through the same press), adds it, and connects it.\",\n body: RESOLVE_SKILL_BODY,\n};\n\n/** The full skill catalog. Hand-curated; keep it small. */\nexport const SKILLS: readonly Skill[] = [EXECUTE_SKILL, RESOLVE_SKILL];\n\n/** Look up a skill by its exact name. */\nexport const findSkill = (name: string): Skill | undefined =>\n SKILLS.find((skill) => skill.name === name);\n\n/** The index the `skills` tool returns when called without a name (or with an\n * unknown one): every skill's name and one-line summary, plus how to fetch\n * the body. */\nexport const renderSkillsIndex = (): string =>\n [\n 'Available skills. Fetch one with `skills({ name: \"<name>\" })`.',\n \"\",\n ...SKILLS.map((skill) => `- \\`${skill.name}\\` — ${skill.summary}`),\n ].join(\"\\n\");\n"],"mappings":";;;;;;;;;;;;;;;;AA2BA,IAAM,qBAAqB;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,EAAE,KAAK,IAAI;AAEJ,IAAM,gBAAuB;AAAA,EAClC,MAAM;AAAA,EACN,SACE;AAAA,EACF,MAAM;AACR;AAOA,IAAM,qBAAqB;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,EAAE,KAAK,IAAI;AAEJ,IAAM,gBAAuB;AAAA,EAClC,MAAM;AAAA,EACN,SACE;AAAA,EACF,MAAM;AACR;AAGO,IAAM,SAA2B,CAAC,eAAe,aAAa;AAG9D,IAAM,YAAY,CAAC,SACxB,OAAO,KAAK,CAAC,UAAU,MAAM,SAAS,IAAI;AAKrC,IAAM,oBAAoB,MAC/B;AAAA,EACE;AAAA,EACA;AAAA,EACA,GAAG,OAAO,IAAI,CAAC,UAAU,OAAO,MAAM,IAAI,aAAQ,MAAM,OAAO,EAAE;AACnE,EAAE,KAAK,IAAI;","names":[]}
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rafads/execution",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.1",
|
|
4
4
|
"homepage": "https://github.com/UsefulSoftwareCo/executor/tree/main/packages/core/execution",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/UsefulSoftwareCo/executor/issues"
|
|
@@ -39,12 +39,12 @@
|
|
|
39
39
|
"typecheck:slow": "bunx tsc --noEmit -p tsconfig.json"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@rafads/codemode-core": "1.
|
|
43
|
-
"@rafads/sdk": "1.
|
|
42
|
+
"@rafads/codemode-core": "1.6.1",
|
|
43
|
+
"@rafads/sdk": "1.6.1"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@effect/vitest": "4.0.0-beta.59",
|
|
47
|
-
"@rafads/runtime-quickjs": "1.
|
|
47
|
+
"@rafads/runtime-quickjs": "1.6.1",
|
|
48
48
|
"@types/node": "^24.3.1",
|
|
49
49
|
"bun-types": "^1.2.22",
|
|
50
50
|
"effect": "4.0.0-beta.59",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/errors.ts","../src/tool-invoker.ts","../src/description.ts","../src/engine.ts","../src/use-need.ts"],"sourcesContent":["import * as Data from \"effect/Data\";\n\nexport class ExecutionToolError extends Data.TaggedError(\"ExecutionToolError\")<{\n readonly message: string;\n readonly cause?: unknown;\n}> {}\n\n// `CodeExecutionError` lives in `@rafads/codemode-core` — the `CodeExecutor`\n// interface uses it as the default error channel, so the runtime packages\n// can import the same class directly.\nexport { CodeExecutionError } from \"@rafads/codemode-core\";\n","import { Effect, Predicate } from \"effect\";\nimport * as Cause from \"effect/Cause\";\nimport type {\n Executor,\n InvokeOptions,\n Integration,\n ToolError,\n Tool,\n ToolSchemaView,\n} from \"@rafads/sdk/core\";\nimport {\n annotateToolResultOutcome,\n authToolFailure,\n isUserActionableError,\n isToolResult,\n ToolResult,\n ToolAddress,\n parseToolAddress,\n} from \"@rafads/sdk/core\";\nimport type { SandboxToolInvoker } from \"@rafads/codemode-core\";\nimport { ExecutionToolError } from \"./errors\";\n\nconst OPAQUE_DEFECT_MESSAGE = \"Internal tool error\";\nconst TOOL_DESCRIBE_SUGGESTION_LIMIT = 5;\nconst TOOL_ERROR_TYPESCRIPT =\n \"{ code: string; message: string; status?: number; details?: unknown; retryable?: boolean }\";\n// Present on HTTP-backed tools (OpenAPI): transport facts beside the payload\n// so callers can read pagination/rate-limit headers without the payload\n// being wrapped in an envelope.\nconst TOOL_HTTP_META_TYPESCRIPT = \"{ status: number; headers: { [k: string]: string; } }\";\nconst TOOL_FILE_TYPESCRIPT =\n '{ _tag: \"ToolFile\"; name?: string; mimeType: string; encoding: \"base64\"; data: string; byteLength: number; }';\n\nconst wrapOutputTypeScript = (outputTypeScript?: string): string =>\n `{ ok: true; data: ${outputTypeScript ?? \"unknown\"}; http?: ToolHttpMeta } | { ok: false; error: ToolError }`;\n\nconst withToolResultDefinitions = (\n definitions?: Record<string, string>,\n): Record<string, string> => ({\n ...(definitions ?? {}),\n ToolError: TOOL_ERROR_TYPESCRIPT,\n ToolHttpMeta: TOOL_HTTP_META_TYPESCRIPT,\n ToolFile: TOOL_FILE_TYPESCRIPT,\n});\n\nconst ADDRESS_PREFIX = \"tools.\";\n\n/**\n * Map a sandbox tool path to the executor's `execute` address.\n *\n * v2 dynamic tools are addressed `tools.<integration>.<owner>.<connection>.<tool>`.\n * The sandbox proxy strips the leading `tools.` (the proxy root), so a model\n * writing `tools.github.org.main.getRepo(args)` produces the path\n * `github.org.main.getRepo`. Re-prefix it so it parses as a 5-segment address.\n *\n * Plugin-contributed static tools (core-tools under `executor`, plugin executor\n * namespaces) are addressed by their fqid with no prefix; the executor resolves\n * those from its static map directly, so leave them untouched.\n */\nconst pathToAddress = (path: string): ToolAddress => {\n if (path.startsWith(ADDRESS_PREFIX)) return ToolAddress.make(path);\n if (parseToolAddress(`${ADDRESS_PREFIX}${path}`)) {\n return ToolAddress.make(`${ADDRESS_PREFIX}${path}`);\n }\n return ToolAddress.make(path);\n};\n\n/** Strip the proxy-root `tools.` prefix from a full address so it becomes the\n * sandbox-callable path the model writes after `tools.`. */\nconst addressToPath = (address: string): string =>\n address.startsWith(ADDRESS_PREFIX) ? address.slice(ADDRESS_PREFIX.length) : address;\n\ntype DescribedTool = {\n readonly path: string;\n readonly name: string;\n readonly description?: string;\n readonly inputTypeScript?: string;\n readonly outputTypeScript?: string;\n readonly typeScriptDefinitions?: Record<string, string>;\n /** Set when the path resolves to no tool — mirrors invoke's tool_not_found. */\n readonly error?: {\n readonly code: \"tool_not_found\";\n readonly message: string;\n readonly suggestions?: readonly string[];\n };\n};\n\nconst BUILTIN_TOOL_DESCRIPTIONS: ReadonlyMap<string, DescribedTool> = new Map<\n string,\n DescribedTool\n>([\n [\n \"search\",\n {\n path: \"search\",\n name: \"search\",\n description:\n \"Search available Executor tools. An empty query with a namespace enumerates that integration's full catalog, sorted by path.\",\n inputTypeScript: \"{ query: string; namespace?: string; limit?: number; offset?: number; }\",\n outputTypeScript:\n \"{ items: ToolDiscoveryResult[]; total: number; hasMore: boolean; nextOffset: number | null; }\",\n typeScriptDefinitions: {\n ToolDiscoveryResult:\n \"{ path: string; name: string; description?: string; integration: string; score: number; }\",\n },\n },\n ],\n [\n \"executor.integrations.list\",\n {\n path: \"executor.integrations.list\",\n name: \"executor.integrations.list\",\n description: \"List configured Executor integrations.\",\n inputTypeScript: \"{ query?: string; limit?: number; offset?: number; }\",\n outputTypeScript:\n \"{ items: ExecutorIntegrationListItem[]; total: number; hasMore: boolean; nextOffset: number | null; }\",\n typeScriptDefinitions: {\n ExecutorIntegrationListItem:\n \"{ id: string; name: string; description?: string; kind: string; canRemove?: boolean; canRefresh?: boolean; toolCount: number; }\",\n },\n },\n ],\n [\n \"describe.tool\",\n {\n path: \"describe.tool\",\n name: \"describe.tool\",\n description: \"Describe a tool's compact TypeScript input and output shapes.\",\n inputTypeScript: \"{ path: string; }\",\n outputTypeScript: \"DescribedTool\",\n typeScriptDefinitions: {\n DescribedTool:\n '{ path: string; name: string; description?: string; inputTypeScript?: string; outputTypeScript?: string; typeScriptDefinitions?: { [k: string]: string; }; error?: { code: \"tool_not_found\"; message: string; suggestions?: string[]; }; }',\n },\n },\n ],\n [\n \"use\",\n {\n path: \"use\",\n name: \"use\",\n description:\n 'Make an external service capability work in ONE call: state the need (\"transactional email\", \"a database\", or a provider name like \"resend\") and get back a wired, callable integration — resolve, add, and connect handled internally. Returns ready (tools callable now), pending (one human credential click left; re-press to check), compare (full ranked set, on request), or an honest no_match with the catalog taxonomy. The answer is ONE verified provider with a machine-readable `sponsored` label and the count of alternatives; override by pressing again with not: [\"<id>\"].',\n inputTypeScript: \"{ need: string; not?: string[]; compare?: boolean; }\",\n outputTypeScript: \"UseNeedReply\",\n typeScriptDefinitions: {\n UseNeedReply:\n '{ status: \"ready\"; provider: ProviderCard; alternativesCount: number; toolsPath?: string; instructions: string; } | { status: \"pending\"; provider: ProviderCard; alternativesCount: number; url: string; instructions: string; } | { status: \"compare\"; candidates: ProviderCard[]; instructions: string; } | { status: \"no_match\"; categories: string[]; instructions: string; } | { status: \"error\"; step: string; message: string; instructions: string; }',\n ProviderCard:\n \"{ id: string; name: string; domain: string; category: string; sponsored: boolean; offer?: { terms: string; url: string; }; verifiedAt: string; }\",\n },\n },\n ],\n]);\n\nconst newCorrelationId = (): string => {\n // 8-hex-char correlation id; enough entropy to disambiguate within a\n // single deployment without leaking host process info.\n return Math.floor(Math.random() * 0x1_0000_0000)\n .toString(16)\n .padStart(8, \"0\");\n};\n\nconst validationIssues = (value: unknown): readonly unknown[] | null => {\n if (typeof value !== \"object\" || value === null) return null;\n const issues = (value as { readonly issues?: unknown }).issues;\n return Array.isArray(issues) ? issues : null;\n};\n\n// Pre-flight OpenAPI invocation failures (missing/unresolved path params,\n// missing or malformed request body) are caller-argument errors raised before\n// any HTTP request is sent. Their messages are generated locally from the\n// spec (no upstream data, no transport internals), so they are safe to\n// surface, and actionable: the fix is renaming or adding an argument.\n// Discriminator: pre-flight raises carry `statusCode: None` and no `cause`.\n// Transport failures share `statusCode: None` but wrap the raw client error\n// (which can hold internal URLs) as `cause`, and post-response failures carry\n// a status code; both stay on the opaque-defect path. Matched structurally\n// because this package does not depend on the openapi plugin.\nconst openApiPreflightMessage = (value: unknown): string | null => {\n if (!Predicate.isTagged(value, \"OpenApiInvocationError\")) return null;\n const err = value as {\n readonly message?: unknown;\n readonly statusCode?: unknown;\n readonly cause?: unknown;\n };\n if (err.cause !== undefined) return null;\n if (!Predicate.isTagged(err.statusCode, \"None\")) return null;\n // oxlint-disable-next-line executor/no-unknown-error-message -- boundary: structural match on the openapi plugin's tagged error; the pre-flight message is locally generated (no upstream data) and is the payload being surfaced\n return typeof err.message === \"string\" && err.message.length > 0 ? err.message : null;\n};\n\nconst credentialResolutionToolFailure = (input: {\n readonly label: string;\n readonly message: string;\n readonly reauthRequired?: boolean;\n}) =>\n authToolFailure({\n code: input.reauthRequired === true ? \"oauth_reauth_required\" : \"oauth_refresh_failed\",\n message:\n input.reauthRequired === true\n ? `OAuth connection \"${input.label}\" requires reauthorization: ${input.message}`\n : `OAuth connection \"${input.label}\" could not be resolved: ${input.message}`,\n credential: {\n kind: \"oauth\",\n label: input.label,\n },\n });\n\nconst bindingToolFailure = (value: unknown): ToolError | null => {\n if (!Predicate.isTagged(value, \"BindingError\")) return null;\n const maybeBinding = value as {\n readonly message?: unknown;\n readonly role?: unknown;\n readonly integration?: unknown;\n readonly requestedConnection?: unknown;\n };\n const details: Record<string, string> = {};\n if (typeof maybeBinding.role === \"string\") details.role = maybeBinding.role;\n if (typeof maybeBinding.integration === \"string\") details.integration = maybeBinding.integration;\n if (typeof maybeBinding.requestedConnection === \"string\") {\n details.requestedConnection = maybeBinding.requestedConnection;\n }\n return {\n code: \"binding_error\",\n message:\n typeof maybeBinding.message === \"string\" ? maybeBinding.message : \"Tool binding failed.\",\n ...(Object.keys(details).length > 0 ? { details } : {}),\n };\n};\n\nconst expectedToolFailure = (value: unknown): ToolError | null => {\n if (isUserActionableError(value)) {\n return {\n code: value.code,\n message: value.userMessage,\n };\n }\n if (Predicate.isTagged(value, \"ToolNotFoundError\") && \"address\" in value) {\n const suggestions =\n \"suggestions\" in value && Array.isArray(value.suggestions)\n ? value.suggestions.map((suggestion) => addressToPath(String(suggestion)))\n : undefined;\n const address = addressToPath(String(value.address));\n return {\n code: \"tool_not_found\",\n message: `Tool not found: ${address}`,\n details: { path: address, ...(suggestions ? { suggestions } : {}) },\n };\n }\n if (Predicate.isTagged(value, \"ToolBlockedError\") && \"address\" in value) {\n return {\n code: \"tool_blocked\",\n message: `Tool blocked by policy: ${addressToPath(String(value.address))}`,\n details: value,\n };\n }\n if (Predicate.isTagged(value, \"ToolInvocationError\")) {\n const cause = (value as { readonly cause?: unknown }).cause;\n if (isUserActionableError(cause)) {\n return {\n code: cause.code,\n message: cause.userMessage,\n };\n }\n const binding = bindingToolFailure(cause);\n if (binding) return binding;\n const issues = validationIssues(cause);\n if (issues) {\n return {\n code: \"invalid_tool_arguments\",\n message: \"Tool arguments did not match the input schema.\",\n details: { issues },\n };\n }\n const preflight = openApiPreflightMessage(cause);\n if (preflight) {\n return {\n code: \"invalid_tool_arguments\",\n message: preflight,\n };\n }\n }\n return null;\n};\n\n/**\n * Extract the integration namespace from a tool path. v2 addresses look like\n * `<integration>.<owner>.<connection>.<tool>`; static fqids look like\n * `<integration>.<op>`. We take the first segment as a cheap, non-lookup namespace\n * for the span attribute so it's always populated without a catalog read.\n */\nconst extractNamespace = (path: string): string => {\n const normalized = addressToPath(path);\n const idx = normalized.indexOf(\".\");\n return idx === -1 ? normalized : normalized.slice(0, idx);\n};\n\n/**\n * Bridges QuickJS `tools.<integration>.<owner>.<connection>.<tool>(args)` calls\n * into `executor.execute(address, args)`.\n *\n * Wrapped in `Effect.fn(\"mcp.tool.dispatch\")` so every tool call becomes a\n * span in the Effect tracer. Attributes:\n * - `mcp.tool.name` — full tool path (e.g. \"github.org.main.getRepo\")\n * - `mcp.tool.integration` — first segment of the path (namespace)\n *\n * `mcp.tool.kind` (openapi | mcp | graphql | code) is NOT annotated here\n * because it would require an `integrations.list()` lookup on every invocation.\n * Callers that already know the integration kind can annotate at their own span.\n */\nexport const makeExecutorToolInvoker = (\n executor: Executor,\n options: { readonly invokeOptions: InvokeOptions },\n): SandboxToolInvoker => ({\n invoke: Effect.fn(\"mcp.tool.dispatch\")(function* ({ path, args }) {\n yield* Effect.annotateCurrentSpan({\n \"mcp.tool.name\": path,\n \"mcp.tool.integration\": extractNamespace(path),\n });\n\n const address = pathToAddress(path);\n const result = yield* executor.execute(address, args, options.invokeOptions).pipe(\n Effect.catchTag(\"CredentialResolutionError\", (err) =>\n Effect.succeed(\n credentialResolutionToolFailure({\n label: `${err.integration}.${err.owner}.${err.name}`,\n message: err.message,\n reauthRequired: err.reauthRequired,\n }),\n ),\n ),\n Effect.catchCause((cause) => {\n const err = cause.reasons.find(Cause.isFailReason)?.error;\n const expected = expectedToolFailure(err);\n if (expected) {\n return Effect.succeed(ToolResult.fail(expected));\n }\n if (isElicitationDeclinedError(err)) {\n return Effect.fail(\n new ExecutionToolError({\n message: `Tool \"${addressToPath(String(err.address))}\" requires approval but the request was ${err.action === \"cancel\" ? \"cancelled\" : \"declined\"} by the user.`,\n cause: err,\n }),\n );\n }\n // Any other failure here is an infra/plugin defect. Emit an\n // opaque generic with a correlation id so internal context (URLs\n // with tokens, DB connection strings, file paths in stacks)\n // can't leak through Error.message into the sandbox. The full\n // cause is logged with the same correlation id so operators can\n // still trace the failure.\n const correlationId = newCorrelationId();\n return Effect.logError(\"tool dispatch failed\", cause).pipe(\n Effect.annotateLogs({\n \"executor.correlation_id\": correlationId,\n \"mcp.tool.name\": path,\n }),\n Effect.flatMap(() =>\n Effect.fail(\n new ExecutionToolError({\n message: `${OPAQUE_DEFECT_MESSAGE} [${correlationId}]`,\n cause: err ?? cause,\n }),\n ),\n ),\n );\n }),\n );\n\n // Strict: plugins emit ToolResult<T>. Anything else is treated as a\n // raw success value and wrapped — keeps the sandbox-facing contract\n // uniform without forcing every tiny test plugin to import\n // `ToolResult.ok`.\n // Expected failures resolve through the success channel, so without the\n // outcome annotation the dispatch span reads as healthy even when the\n // caller hit an upstream error or auth wall.\n yield* annotateToolResultOutcome(result);\n if (isToolResult(result)) {\n return result;\n }\n return { ok: true, data: result };\n }),\n});\n\nconst isElicitationDeclinedError = (\n value: unknown,\n): value is {\n readonly _tag: \"ElicitationDeclinedError\";\n readonly address: string;\n readonly action: \"cancel\" | \"decline\";\n} =>\n Predicate.isTagged(value, \"ElicitationDeclinedError\") &&\n value !== null &&\n typeof value === \"object\" &&\n \"address\" in value &&\n typeof value.address === \"string\" &&\n \"action\" in value &&\n (value.action === \"cancel\" || value.action === \"decline\");\n\nexport type ToolDiscoveryResult = {\n readonly path: string;\n readonly name: string;\n readonly description?: string;\n readonly integration: string;\n readonly score: number;\n};\n\nexport type ExecutorIntegrationListItem = {\n readonly id: string;\n readonly name: string;\n readonly description?: string;\n readonly kind: string;\n readonly canRemove?: boolean;\n readonly canRefresh?: boolean;\n readonly toolCount: number;\n};\n\nexport type ToolDiscoveryInput = {\n readonly executor: Executor;\n readonly query: string;\n readonly namespace?: string;\n readonly limit: number;\n readonly offset: number;\n};\n\nexport interface ToolDiscoveryProvider {\n readonly searchTools: (\n input: ToolDiscoveryInput,\n ) => Effect.Effect<PagedResult<ToolDiscoveryResult>, ExecutionToolError>;\n}\n\n/**\n * Page of results from a list-style discovery tool. Shared by\n * `tools.search` and `tools.executor.integrations.list` so the model sees one\n * consistent shape:\n *\n * - `items` — the page (slice).\n * - `total` — count after filtering, before pagination. The model\n * can use this to detect truncation.\n * - `hasMore` — convenience flag for `(offset + items.length) < total`.\n * - `nextOffset` — concrete offset for the next page when `hasMore`,\n * `null` otherwise. Pre-computing it removes a class of\n * off-by-one mistakes when the model paginates.\n */\nexport type PagedResult<T> = {\n readonly items: readonly T[];\n readonly total: number;\n readonly hasMore: boolean;\n readonly nextOffset: number | null;\n};\n\nconst paginate = <T>(all: readonly T[], offset: number, limit: number): PagedResult<T> => {\n const total = all.length;\n const start = Math.min(Math.max(offset, 0), total);\n const items = all.slice(start, start + limit);\n const consumed = start + items.length;\n const hasMore = consumed < total;\n return {\n items,\n total,\n hasMore,\n nextOffset: hasMore ? consumed : null,\n };\n};\n\n/** What `searchTools` ranks over — the sandbox-callable path plus the v2\n * identity fields a query can match against. */\ntype SearchableTool = {\n readonly path: string;\n readonly integration: string;\n readonly name: string;\n readonly description?: string;\n};\n\nconst toSearchableTool = (tool: Tool): SearchableTool => ({\n path: addressToPath(String(tool.address)),\n integration: String(tool.integration),\n name: String(tool.name),\n description: tool.description,\n});\n\ntype PreparedField = {\n readonly raw: string;\n readonly tokens: readonly string[];\n};\n\nconst SEARCH_FIELD_WEIGHTS = {\n path: 12,\n integration: 8,\n name: 10,\n description: 5,\n} as const;\n\nconst normalizeSearchText = (value: string): string =>\n value\n .replace(/([a-z0-9])([A-Z])/g, \"$1 $2\")\n .replace(/[_./:-]+/g, \" \")\n .toLowerCase()\n .trim();\n\nconst tokenizeSearchText = (value: string): string[] =>\n normalizeSearchText(value)\n .split(/[^a-z0-9]+/)\n .map((token) => token.trim())\n .filter(Boolean);\n\nconst prepareField = (value?: string): PreparedField => ({\n raw: normalizeSearchText(value ?? \"\"),\n tokens: tokenizeSearchText(value ?? \"\"),\n});\n\nconst scorePreparedField = (\n query: string,\n queryTokens: readonly string[],\n field: PreparedField,\n weight: number,\n): {\n readonly score: number;\n readonly matchedTokens: ReadonlySet<string>;\n readonly exactPhraseMatch: boolean;\n} => {\n if (field.raw.length === 0) {\n return {\n score: 0,\n matchedTokens: new Set<string>(),\n exactPhraseMatch: false,\n };\n }\n\n let score = 0;\n const matchedTokens = new Set<string>();\n const exactPhraseMatch = query.length > 0 && field.raw.includes(query);\n\n if (query.length > 0) {\n if (field.raw === query) {\n score += weight * 14;\n } else if (field.raw.startsWith(query)) {\n score += weight * 9;\n } else if (exactPhraseMatch) {\n score += weight * 6;\n }\n }\n\n for (const token of queryTokens) {\n if (field.tokens.includes(token)) {\n score += weight * 4;\n matchedTokens.add(token);\n continue;\n }\n\n if (\n field.tokens.some((candidate) => candidate.startsWith(token) || token.startsWith(candidate))\n ) {\n score += weight * 2;\n matchedTokens.add(token);\n continue;\n }\n\n if (field.raw.includes(token)) {\n score += weight;\n matchedTokens.add(token);\n }\n }\n\n return {\n score,\n matchedTokens,\n exactPhraseMatch,\n };\n};\n\nconst matchesNamespace = (tool: SearchableTool, namespace?: string): boolean => {\n if (!namespace || normalizeSearchText(namespace).length === 0) {\n return true;\n }\n\n const namespaceTokens = tokenizeSearchText(namespace);\n if (namespaceTokens.length === 0) {\n return true;\n }\n\n const integrationTokens = tokenizeSearchText(tool.integration);\n const pathTokens = tokenizeSearchText(tool.path);\n\n const isPrefixMatch = (tokens: readonly string[]): boolean =>\n namespaceTokens.every((token, index) => tokens[index] === token);\n\n return isPrefixMatch(integrationTokens) || isPrefixMatch(pathTokens);\n};\n\nconst scoreToolMatch = (tool: SearchableTool, query: string): ToolDiscoveryResult | null => {\n const normalizedQuery = normalizeSearchText(query);\n const queryTokens = tokenizeSearchText(query);\n\n if (normalizedQuery.length === 0 || queryTokens.length === 0) {\n return null;\n }\n\n const path = prepareField(tool.path);\n const integration = prepareField(tool.integration);\n const name = prepareField(tool.name);\n const description = prepareField(tool.description);\n\n const fieldScores = [\n scorePreparedField(normalizedQuery, queryTokens, path, SEARCH_FIELD_WEIGHTS.path),\n scorePreparedField(normalizedQuery, queryTokens, integration, SEARCH_FIELD_WEIGHTS.integration),\n scorePreparedField(normalizedQuery, queryTokens, name, SEARCH_FIELD_WEIGHTS.name),\n scorePreparedField(normalizedQuery, queryTokens, description, SEARCH_FIELD_WEIGHTS.description),\n ];\n\n const matchedTokens = new Set<string>();\n let score = 0;\n let exactPhraseMatch = false;\n\n for (const fieldScore of fieldScores) {\n score += fieldScore.score;\n exactPhraseMatch ||= fieldScore.exactPhraseMatch;\n for (const token of fieldScore.matchedTokens) {\n matchedTokens.add(token);\n }\n }\n\n if (matchedTokens.size === 0) {\n return null;\n }\n\n const coverage = matchedTokens.size / queryTokens.length;\n const minimumCoverage = queryTokens.length <= 2 ? 1 : 0.6;\n\n if (coverage < minimumCoverage && !exactPhraseMatch) {\n return null;\n }\n\n if (coverage === 1) {\n score += 25;\n } else {\n score += Math.round(coverage * 10);\n }\n\n if (path.tokens[0] === queryTokens[0] || name.tokens[0] === queryTokens[0]) {\n score += 8;\n }\n\n if (\n normalizeSearchText(tool.path) === normalizedQuery ||\n normalizeSearchText(tool.name) === normalizedQuery\n ) {\n score += 20;\n }\n\n return {\n path: tool.path,\n name: tool.name,\n description: tool.description,\n integration: tool.integration,\n score,\n };\n};\n\n/** What `tools.search()` calls inside the sandbox. */\nexport const searchTools = Effect.fn(\"executor.tools.search\")(function* (\n executor: Executor,\n query: string,\n limit = 12,\n options?: { readonly namespace?: string; readonly offset?: number },\n) {\n const offset = options?.offset ?? 0;\n yield* Effect.annotateCurrentSpan({\n \"executor.search.query_length\": query.length,\n \"executor.search.limit\": limit,\n \"executor.search.offset\": offset,\n ...(options?.namespace ? { \"executor.search.namespace\": options.namespace } : {}),\n });\n\n const emptyQuery = normalizeSearchText(query).length === 0;\n const hasNamespace =\n options?.namespace !== undefined && normalizeSearchText(options.namespace).length > 0;\n\n // An empty query with no namespace stays empty: it carries neither a\n // ranking signal nor a scope, and listing the whole workspace \"by default\"\n // is exactly the arbitrary dump the ranked search refuses to be.\n if (emptyQuery && !hasNamespace) {\n return {\n items: [],\n total: 0,\n hasMore: false,\n nextOffset: null,\n } satisfies PagedResult<ToolDiscoveryResult>;\n }\n\n const all = yield* executor.tools.list({ includeAnnotations: false }).pipe(\n Effect.mapError(\n (cause) =>\n new ExecutionToolError({\n message: \"Failed to list tools for search\",\n cause,\n }),\n ),\n );\n const searchable = all.map(toSearchableTool);\n\n // An empty query WITH a namespace is enumeration, not search: there is no\n // ranking signal, so the namespace's whole catalog comes back sorted by\n // path (score 0) and paged. Enumeration scopes by EXACT integration slug —\n // the token-prefix `matchesNamespace` used for ranked search would also\n // sweep in prefix-sibling integrations (namespace \"google\" matching\n // google_gmail and google_sheets), which would silently break the census\n // guarantee: `total` here must reconcile against\n // `executor.integrations.list`'s per-integration toolCount.\n const ranked: readonly ToolDiscoveryResult[] = emptyQuery\n ? searchable\n .filter((tool) => tool.integration === options?.namespace?.trim())\n .sort((left, right) => left.path.localeCompare(right.path))\n .map((tool) => ({\n path: tool.path,\n name: tool.name,\n integration: tool.integration,\n score: 0,\n ...(tool.description !== undefined ? { description: tool.description } : {}),\n }))\n : searchable\n .filter((tool: SearchableTool) => matchesNamespace(tool, options?.namespace))\n .map((tool: SearchableTool) => scoreToolMatch(tool, query))\n .filter(Predicate.isNotNull)\n .sort((left, right) => right.score - left.score || left.path.localeCompare(right.path));\n\n const page = paginate(ranked, offset, limit);\n\n yield* Effect.annotateCurrentSpan({\n \"executor.search.candidate_count\": all.length,\n \"executor.search.match_count\": ranked.length,\n \"executor.search.result_count\": page.items.length,\n \"executor.search.has_more\": page.hasMore,\n });\n return page;\n});\n\nexport const defaultToolDiscoveryProvider: ToolDiscoveryProvider = {\n searchTools: ({ executor, query, namespace, limit, offset }) =>\n searchTools(executor, query, limit, { namespace, offset }),\n};\n\n/** What `tools.executor.integrations.list()` calls inside the sandbox. v2: the\n * integrations are the integration catalog; tool counts come from the\n * per-connection tool list. */\nexport const listExecutorIntegrations = Effect.fn(\"executor.integrations.list\")(function* (\n executor: Executor,\n options?: {\n readonly query?: string;\n readonly limit?: number;\n readonly offset?: number;\n },\n) {\n const normalizedQuery = normalizeSearchText(options?.query ?? \"\");\n const limit = options?.limit ?? 50;\n const offset = options?.offset ?? 0;\n const integrations = yield* executor.integrations.list().pipe(\n Effect.mapError(\n (cause) =>\n new ExecutionToolError({\n message: \"Failed to list executor integrations\",\n cause,\n }),\n ),\n );\n\n const filtered =\n normalizedQuery.length === 0\n ? integrations\n : integrations.filter((integration: Integration) => {\n const haystack = normalizeSearchText(\n [String(integration.slug), integration.description, integration.kind].join(\" \"),\n );\n return tokenizeSearchText(normalizedQuery).every((token) => haystack.includes(token));\n });\n\n // Single query for all tools, then count per integration in memory.\n const allTools = yield* executor.tools.list({ includeAnnotations: false }).pipe(\n Effect.mapError(\n (cause) =>\n new ExecutionToolError({\n message: \"Failed to list tools for integration counts\",\n cause,\n }),\n ),\n );\n const toolCountByIntegration = new Map<string, number>();\n for (const tool of allTools) {\n const key = String(tool.integration);\n toolCountByIntegration.set(key, (toolCountByIntegration.get(key) ?? 0) + 1);\n }\n\n const sortedWithCounts = filtered\n .map(\n (integration: Integration) =>\n ({\n id: String(integration.slug),\n name: String(integration.slug),\n // The integration's catalog description — user-editable context the\n // agent can use to pick an integration. Omitted when it just repeats the\n // slug or display name (no information beyond identity).\n ...(integration.description &&\n integration.description.toLowerCase() !== String(integration.slug).toLowerCase() &&\n integration.description.toLowerCase() !== integration.name.toLowerCase()\n ? { description: integration.description }\n : {}),\n kind: integration.kind,\n canRemove: integration.canRemove,\n canRefresh: integration.canRefresh,\n toolCount: toolCountByIntegration.get(String(integration.slug)) ?? 0,\n }) satisfies ExecutorIntegrationListItem,\n )\n .sort((left, right) => left.name.localeCompare(right.name) || left.id.localeCompare(right.id));\n\n const page = paginate(sortedWithCounts, offset, limit);\n\n yield* Effect.annotateCurrentSpan({\n \"executor.integrations.candidate_count\": integrations.length,\n \"executor.integrations.match_count\": sortedWithCounts.length,\n \"executor.integrations.result_count\": page.items.length,\n \"executor.integrations.has_more\": page.hasMore,\n });\n return page;\n});\n\n/** What `tools.describe.tool()` calls inside the sandbox. */\nexport const describeTool = Effect.fn(\"executor.tools.describe\")(function* (\n executor: Executor,\n path: string,\n) {\n yield* Effect.annotateCurrentSpan({ \"mcp.tool.name\": path });\n\n const builtin = BUILTIN_TOOL_DESCRIPTIONS.get(path);\n if (builtin) return builtin;\n\n const address = pathToAddress(path);\n\n // Single tools.schema() call — it already fetches the tool row\n // internally. No need to also call tools.list() just for name/description.\n const schema: ToolSchemaView | null = yield* executor.tools.schema(address);\n\n // tools.schema() returns null if the tool doesn't exist. Mirror the\n // invoke path's tool_not_found shape (error + suggestions) instead of a\n // bare stub — a silent `{ path, name }` reads as \"tool exists, schema\n // unavailable\" and sends callers down the wrong debugging path.\n if (schema === null) {\n const lastDot = path.lastIndexOf(\".\");\n const leaf = lastDot === -1 ? path : path.slice(lastDot + 1);\n const scoped = yield* searchTools(executor, leaf, TOOL_DESCRIBE_SUGGESTION_LIMIT, {\n namespace: extractNamespace(path),\n });\n const matches =\n scoped.items.length > 0\n ? scoped.items\n : (yield* searchTools(executor, leaf, TOOL_DESCRIBE_SUGGESTION_LIMIT)).items;\n const suggestions = matches.map((item) => item.path);\n const notFound: DescribedTool = {\n path,\n name: path,\n error: {\n code: \"tool_not_found\",\n message: `Tool not found: ${path}`,\n ...(suggestions.length > 0 ? { suggestions } : {}),\n },\n };\n return notFound;\n }\n\n // The schema's address is the tool address; name/description come from the\n // tool row which tools.schema() already loaded.\n const described: DescribedTool = {\n path,\n name: schema.name ?? path,\n description: schema.description,\n inputTypeScript: schema.inputTypeScript,\n outputTypeScript: wrapOutputTypeScript(schema.outputTypeScript),\n typeScriptDefinitions: withToolResultDefinitions(schema.typeScriptDefinitions),\n };\n return described;\n});\n","import { Effect } from \"effect\";\nimport type { Connection, Executor } from \"@rafads/sdk/core\";\n\n/**\n * Builds the `execute` tool description dynamically.\n *\n * Structure:\n * 1. One-line intro + pointer to the `execute` skill (the full how-to lives\n * behind the `skills` tool, see ./skills.ts, to keep this always-loaded\n * description small)\n * 2. Available integrations (the live, per-session inventory): the top-level\n * integration slugs the user has connected, deduped across connections,\n * names only. The same block is appended to the `execute` skill content.\n */\n\n/** The header that opens the live integration inventory. Exported so the host\n * can locate (and re-use) the inventory block inside the built description. */\nexport const INTEGRATION_INVENTORY_HEADER = \"## Available integrations\";\n\nexport const buildExecuteDescription = (executor: Executor): Effect.Effect<string> =>\n Effect.gen(function* () {\n const connections: readonly Connection[] = yield* executor.connections.list().pipe(\n // oxlint-disable-next-line executor/no-effect-escape-hatch -- boundary: ExecutionEngine.getDescription currently exposes no error channel; engine typed-error widening is covered separately\n Effect.orDie,\n Effect.withSpan(\"executor.connections.list\"),\n );\n\n const description = yield* Effect.sync(() => {\n const lines = [\n \"Execute TypeScript in a sandboxed runtime with access to connected service integrations.\",\n \"\",\n 'Before writing code, call `skills({ name: \"execute\" })` for the workflow on how to use this tool.',\n \"\",\n 'When the task needs an external service capability — sending email, a database, auth, payments, SMS, DNS, hosting, storage, search — and no connected integration covers it, do not pick a provider from memory or send the user off to sign up somewhere: press the button. `await tools.use({ need: \"transactional email\" })` returns a wired, working integration in one call (or one remaining human credential click). `skills({ name: \"resolve\" })` has details and the manual stepwise flow.',\n ];\n const inventory = formatIntegrationInventory(connections);\n if (inventory.length > 0) {\n lines.push(\"\");\n lines.push(inventory);\n }\n return lines.join(\"\\n\");\n }).pipe(\n Effect.withSpan(\"schema.compile.description\", {\n attributes: { \"executor.connection_count\": connections.length },\n }),\n );\n\n yield* Effect.annotateCurrentSpan({\n \"executor.connection_count\": connections.length,\n \"schema.kind\": \"execute\",\n // Connection inventory so a failing session build (which runs this during\n // init) names the callable prefixes it resolved without listing tools.\n \"executor.connection_addresses\": connections\n .map((connection) => connectionPath(connection))\n .slice(0, 50)\n .join(\",\"),\n \"executor.connection_integrations\": [\n ...new Set(connections.map((connection) => String(connection.integration))),\n ].join(\",\"),\n \"executor.connection_owners\": [\n ...new Set(connections.map((connection) => connection.owner)),\n ].join(\",\"),\n });\n\n return description;\n }).pipe(Effect.withSpan(\"schema.describe.execute\"));\n\nconst connectionPath = (connection: Connection): string => {\n const address = String(connection.address);\n return address.startsWith(\"tools.\") ? address.slice(\"tools.\".length) : address;\n};\n\n// The live inventory block: the top-level integrations the user has connected,\n// one bare line per integration slug (deduped across connections, sorted), no\n// per-connection prefixes and no descriptions. Empty string when nothing is\n// connected.\nconst INVENTORY_LIMIT = 50;\n\nconst formatIntegrationInventory = (connections: readonly Connection[]): string => {\n const slugs = [...new Set(connections.map((connection) => String(connection.integration)))].sort(\n (a, b) => a.localeCompare(b),\n );\n if (slugs.length === 0) return \"\";\n const shown = slugs.slice(0, INVENTORY_LIMIT);\n const lines = [\n INTEGRATION_INVENTORY_HEADER,\n \"\",\n \"Integrations you have connected. Their tools live under `tools.<integration>.…`.\",\n ...shown.map((slug) => `- \\`${slug}\\``),\n ];\n if (slugs.length > shown.length) {\n lines.push(`- ... ${slugs.length - shown.length} more`);\n }\n return lines.join(\"\\n\");\n};\n","import { Deferred, Effect, Fiber, Predicate, Queue } from \"effect\";\nimport type * as Cause from \"effect/Cause\";\nimport * as Exit from \"effect/Exit\";\n\nimport type {\n Executor,\n InvokeOptions,\n ElicitationResponse,\n ElicitationHandler,\n ElicitationContext,\n} from \"@rafads/sdk/core\";\nimport { CodeExecutionError } from \"@rafads/codemode-core\";\nimport type { CodeExecutor, ExecuteResult, SandboxToolInvoker } from \"@rafads/codemode-core\";\n\nimport {\n defaultToolDiscoveryProvider,\n makeExecutorToolInvoker,\n listExecutorIntegrations,\n describeTool,\n type ToolDiscoveryProvider,\n} from \"./tool-invoker\";\nimport { ExecutionToolError } from \"./errors\";\nimport { runUseNeed } from \"./use-need\";\nimport { buildExecuteDescription } from \"./description\";\n\n// ---------------------------------------------------------------------------\n// Types\n// ---------------------------------------------------------------------------\n\nexport type ExecutionEngineConfig<E extends Cause.YieldableError = CodeExecutionError> = {\n readonly executor: Executor;\n readonly codeExecutor: CodeExecutor<E>;\n readonly toolDiscoveryProvider?: ToolDiscoveryProvider;\n /** Fired after every sandbox tool dispatch (builtins included) with the\n * path and outcome — the host's pool-ledger seam (accrual records for the\n * earning lane; see the hq repo's docs/v2/03-plugin-reset.md §5). MUST be\n * fire-and-forget and non-throwing; the engine calls it best-effort and\n * never awaits it. */\n readonly onToolCall?: (info: { readonly path: string; readonly ok: boolean }) => void;\n};\n\nexport type ExecutionResult =\n | { readonly status: \"completed\"; readonly result: ExecuteResult }\n | { readonly status: \"paused\"; readonly execution: PausedExecution };\n\nexport type PausedExecution = {\n readonly id: string;\n readonly elicitationContext: ElicitationContext;\n};\n\nexport type PausedExecutionDeadline = {\n readonly expiresAt: string;\n readonly ttlMs: number;\n};\n\n/** Internal representation with Effect runtime state for pause/resume. */\ntype InternalPausedExecution<E> = PausedExecution & {\n readonly response: Deferred.Deferred<typeof ElicitationResponse.Type>;\n readonly fiber: Fiber.Fiber<ExecuteResult, E>;\n readonly pauseQueue: Queue.Queue<InternalPausedExecution<E>>;\n};\n\nexport type ResumeResponse = {\n readonly action: \"accept\" | \"decline\" | \"cancel\";\n readonly content?: Record<string, unknown>;\n};\n\n// Auto-accept every elicitation. Used by the `autoApprove` path where the\n// caller is itself the human approver (the operator-facing Run/Test panel).\nconst acceptAllHandler: ElicitationHandler = () => Effect.succeed({ action: \"accept\" });\n\n// ---------------------------------------------------------------------------\n// Result formatting\n// ---------------------------------------------------------------------------\n\nconst MAX_PREVIEW_CHARS = 30_000;\n\nconst truncate = (value: string, max: number): string =>\n value.length > max\n ? `${value.slice(0, max)}\\n... [truncated ${value.length - max} chars]`\n : value;\n\nexport const formatExecuteResult = (\n result: ExecuteResult,\n): {\n text: string;\n structured: Record<string, unknown>;\n isError: boolean;\n} => {\n const resultText =\n result.result != null\n ? typeof result.result === \"string\"\n ? result.result\n : JSON.stringify(result.result, null, 2)\n : null;\n\n const logText = result.logs && result.logs.length > 0 ? result.logs.join(\"\\n\") : null;\n\n // `emit()` output is shown to the user, not returned to the model, so a\n // script that only emits comes back with a null result. Acknowledge the\n // emitted items in the envelope so an emit-without-return reads as \"output\n // went to the user\" rather than a silent void.\n const emitted = result.output?.length ?? 0;\n const emittedNote =\n emitted > 0 ? `${emitted} item${emitted === 1 ? \"\" : \"s\"} emitted to the user` : null;\n const emittedField = emitted > 0 ? { emitted } : {};\n\n if (result.error) {\n const parts = [`Error: ${result.error}`, ...(logText ? [`\\nLogs:\\n${logText}`] : [])];\n return {\n text: truncate(parts.join(\"\\n\"), MAX_PREVIEW_CHARS),\n structured: {\n status: \"error\",\n error: result.error,\n ...emittedField,\n logs: result.logs ?? [],\n },\n isError: true,\n };\n }\n\n const resultPart = resultText\n ? truncate(resultText, MAX_PREVIEW_CHARS)\n : emittedNote\n ? `(no return value; ${emittedNote})`\n : \"(no result)\";\n const parts = [resultPart, ...(logText ? [`\\nLogs:\\n${logText}`] : [])];\n return {\n text: parts.join(\"\\n\"),\n structured: {\n status: \"completed\",\n result: result.result ?? null,\n ...emittedField,\n logs: result.logs ?? [],\n },\n isError: false,\n };\n};\n\nexport const formatPausedExecution = (\n paused: PausedExecution,\n options?: { readonly deadline?: PausedExecutionDeadline },\n): {\n text: string;\n structured: Record<string, unknown>;\n} => {\n const req = paused.elicitationContext.request;\n const lines: string[] = [`Execution paused: ${req.message}`];\n const deadline = options?.deadline;\n const isUrlElicitation = Predicate.isTagged(req, \"UrlElicitation\");\n const isFormElicitation = Predicate.isTagged(req, \"FormElicitation\");\n const requestedSchema = isFormElicitation ? req.requestedSchema : undefined;\n const hasRequestedSchema =\n requestedSchema !== undefined && Object.keys(requestedSchema).length > 0;\n const baseInstructions = isUrlElicitation\n ? `The user needs to open this URL in a browser and complete the flow. After the user finishes, call the resume tool with executionId \"${paused.id}\" and action \"accept\".`\n : hasRequestedSchema\n ? `Ask the user for values matching requestedSchema. Then call the resume tool with executionId \"${paused.id}\", action \"accept\", and content matching requestedSchema. If the user declines, call resume with action \"decline\" or \"cancel\".`\n : `This is a model-side confirmation gate; there is no browser form to open. Ask the user whether to approve the paused tool call. If the user approves, call the resume tool with executionId \"${paused.id}\" and action \"accept\". If the user declines, call resume with action \"decline\" or \"cancel\".`;\n const deadlineInstructions = deadline\n ? ` Resume before ${deadline.expiresAt}; this approval window lasts ${formatTtlDuration(deadline.ttlMs)}.`\n : \"\";\n const instructions = `${baseInstructions}${deadlineInstructions}`;\n\n if (isUrlElicitation) {\n lines.push(`\\nOpen this URL in a browser:\\n${req.url}`);\n lines.push('\\nAfter the browser flow, call the resume tool with action \"accept\".');\n } else if (hasRequestedSchema) {\n lines.push(\n \"\\nAsk the user for a response matching the requested schema, then call the resume tool.\",\n );\n lines.push(`\\nRequested schema:\\n${JSON.stringify(requestedSchema, null, 2)}`);\n } else {\n lines.push(\n '\\nThis is a model-side confirmation gate; no browser form is waiting. Ask the user whether to approve, then call the resume tool with action \"accept\", \"decline\", or \"cancel\".',\n );\n }\n\n lines.push(`\\nexecutionId: ${paused.id}`);\n if (deadline) {\n lines.push(\n `\\nresumeDeadline: ${deadline.expiresAt} (${formatTtlDuration(deadline.ttlMs)} approval window)`,\n );\n }\n lines.push(`\\ninstructions: ${instructions}`);\n\n return {\n text: lines.join(\"\\n\"),\n structured: {\n status: \"waiting_for_interaction\",\n executionId: paused.id,\n ...(deadline ? { expiresAt: deadline.expiresAt, ttlMs: deadline.ttlMs } : {}),\n interaction: {\n kind: isUrlElicitation ? \"url\" : \"form\",\n message: req.message,\n instructions,\n address: String(paused.elicitationContext.address),\n args: paused.elicitationContext.args,\n ...(isUrlElicitation ? { url: req.url } : {}),\n ...(isFormElicitation ? { requestedSchema: req.requestedSchema } : {}),\n },\n },\n };\n};\n\nexport const formatTtlDuration = (ttlMs: number): string => {\n const seconds = Math.max(1, Math.round(ttlMs / 1000));\n if (seconds % 60 === 0) {\n const minutes = seconds / 60;\n return `${minutes} minute${minutes === 1 ? \"\" : \"s\"}`;\n }\n return `${seconds} second${seconds === 1 ? \"\" : \"s\"}`;\n};\n\n// ---------------------------------------------------------------------------\n// Full invoker (base + discover + describe)\n// ---------------------------------------------------------------------------\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === \"object\" && value !== null && !Array.isArray(value);\n\nconst readOptionalLimit = (value: unknown, toolName: string): number | ExecutionToolError => {\n if (value === undefined) {\n return 12;\n }\n\n if (typeof value !== \"number\" || !Number.isFinite(value) || value <= 0) {\n return new ExecutionToolError({\n message: `${toolName} limit must be a positive number when provided`,\n });\n }\n\n return Math.floor(value);\n};\n\nconst readOptionalOffset = (value: unknown, toolName: string): number | ExecutionToolError => {\n if (value === undefined) {\n return 0;\n }\n\n if (typeof value !== \"number\" || !Number.isFinite(value) || value < 0) {\n return new ExecutionToolError({\n message: `${toolName} offset must be a non-negative number when provided`,\n });\n }\n\n return Math.floor(value);\n};\n\nconst makeFullInvoker = (\n executor: Executor,\n invokeOptions: InvokeOptions,\n toolDiscoveryProvider: ToolDiscoveryProvider,\n): SandboxToolInvoker => {\n const base = makeExecutorToolInvoker(executor, { invokeOptions });\n return {\n invoke: ({ path, args }) => {\n if (path === \"search\") {\n if (!isRecord(args)) {\n return Effect.fail(\n new ExecutionToolError({\n message:\n \"tools.search expects an object: { query?: string; namespace?: string; limit?: number; offset?: number }\",\n }),\n );\n }\n\n if (args.query !== undefined && typeof args.query !== \"string\") {\n return Effect.fail(\n new ExecutionToolError({\n message: \"tools.search query must be a string when provided\",\n }),\n );\n }\n\n if (args.namespace !== undefined && typeof args.namespace !== \"string\") {\n return Effect.fail(\n new ExecutionToolError({\n message: \"tools.search namespace must be a string when provided\",\n }),\n );\n }\n\n const limit = readOptionalLimit(args.limit, \"tools.search\");\n if (Predicate.isTagged(limit, \"ExecutionToolError\")) {\n return Effect.fail(limit);\n }\n\n const offset = readOptionalOffset(args.offset, \"tools.search\");\n if (Predicate.isTagged(offset, \"ExecutionToolError\")) {\n return Effect.fail(offset);\n }\n\n return toolDiscoveryProvider\n .searchTools({\n executor,\n query: args.query ?? \"\",\n limit,\n namespace: args.namespace,\n offset,\n })\n .pipe(\n Effect.withSpan(\"mcp.tool.dispatch\", {\n attributes: { \"mcp.tool.name\": path, \"executor.tool.builtin\": true },\n }),\n );\n }\n if (path === \"executor.integrations.list\") {\n if (args !== undefined && !isRecord(args)) {\n return Effect.fail(\n new ExecutionToolError({\n message:\n \"tools.executor.integrations.list expects an object: { query?: string; limit?: number; offset?: number }\",\n }),\n );\n }\n\n if (isRecord(args) && args.query !== undefined && typeof args.query !== \"string\") {\n return Effect.fail(\n new ExecutionToolError({\n message: \"tools.executor.integrations.list query must be a string when provided\",\n }),\n );\n }\n\n const limit = readOptionalLimit(\n isRecord(args) ? args.limit : undefined,\n \"tools.executor.integrations.list\",\n );\n if (Predicate.isTagged(limit, \"ExecutionToolError\")) {\n return Effect.fail(limit);\n }\n\n const offset = readOptionalOffset(\n isRecord(args) ? args.offset : undefined,\n \"tools.executor.integrations.list\",\n );\n if (Predicate.isTagged(offset, \"ExecutionToolError\")) {\n return Effect.fail(offset);\n }\n\n return listExecutorIntegrations(executor, {\n query: isRecord(args) && typeof args.query === \"string\" ? args.query : undefined,\n limit,\n offset,\n }).pipe(\n Effect.withSpan(\"mcp.tool.dispatch\", {\n attributes: { \"mcp.tool.name\": path, \"executor.tool.builtin\": true },\n }),\n );\n }\n if (path === \"use\") {\n // The make-it-work button: one call collapsing resolve → add →\n // connect (see ./use-need.ts). Composes base.invoke dispatches so\n // approval gates and elicitation pauses apply unchanged.\n return runUseNeed((input) => base.invoke(input), args).pipe(\n Effect.withSpan(\"mcp.tool.dispatch\", {\n attributes: { \"mcp.tool.name\": path, \"executor.tool.builtin\": true },\n }),\n );\n }\n if (path === \"describe.tool\") {\n if (!isRecord(args)) {\n return Effect.fail(\n new ExecutionToolError({\n message: \"tools.describe.tool expects an object: { path: string }\",\n }),\n );\n }\n\n if (typeof args.path !== \"string\" || args.path.trim().length === 0) {\n return Effect.fail(new ExecutionToolError({ message: \"describe.tool requires a path\" }));\n }\n\n if (\"includeSchemas\" in args) {\n return Effect.fail(\n new ExecutionToolError({\n message: \"tools.describe.tool no longer accepts includeSchemas\",\n }),\n );\n }\n\n return describeTool(executor, args.path).pipe(\n Effect.withSpan(\"mcp.tool.dispatch\", {\n attributes: {\n \"mcp.tool.name\": path,\n \"executor.tool.builtin\": true,\n \"executor.tool.target_path\": args.path,\n },\n }),\n );\n }\n return base.invoke({ path, args });\n },\n };\n};\n\n/** Wrap an invoker so every dispatch (success or failure) reports to the\n * host's tool-call hook. A `{ ok: false }` ToolResult envelope counts as a\n * failed call; the hook itself is best-effort (a throwing hook is a host\n * bug, but it must not break tool dispatch). */\nconst withToolCallHook = (\n invoker: SandboxToolInvoker,\n onToolCall: ((info: { readonly path: string; readonly ok: boolean }) => void) | undefined,\n): SandboxToolInvoker => {\n if (onToolCall === undefined) return invoker;\n const report = (path: string, ok: boolean): void => {\n // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: host hook; a throwing hook must not break tool dispatch\n try {\n onToolCall({ path, ok });\n } catch {\n // best-effort by contract\n }\n };\n return {\n invoke: (input) =>\n invoker.invoke(input).pipe(\n Effect.tap((result) =>\n Effect.sync(() => report(input.path, !(isRecord(result) && result.ok === false))),\n ),\n Effect.tapCause(() => Effect.sync(() => report(input.path, false))),\n ),\n };\n};\n\n// ---------------------------------------------------------------------------\n// Execution Engine\n// ---------------------------------------------------------------------------\n\nexport type ExecutionEngine<E extends Cause.YieldableError = CodeExecutionError> = {\n /**\n * Execute code with elicitation handled inline by the provided handler.\n * Use this when the host supports elicitation (e.g. MCP with elicitation capability).\n *\n * Fails with the code executor's typed error `E` (defaults to\n * `CodeExecutionError`). Runtimes surface their own `Data.TaggedError`\n * subclass, which flows through here unchanged.\n */\n readonly execute: (\n code: string,\n options: { readonly onElicitation: ElicitationHandler },\n ) => Effect.Effect<ExecuteResult, E>;\n\n /**\n * Execute code, intercepting the first elicitation as a pause point.\n * Use this when the host doesn't support inline elicitation.\n * Returns either a completed result or a paused execution that can be resumed.\n *\n * `options.autoApprove` treats the caller as the human in the loop: every\n * elicitation is accepted inline, so an approval-gated tool runs to\n * completion instead of pausing. The operator-facing Run/Test panel sets\n * this because clicking Run IS the approval. `block` policies still fail\n * before any elicitation, so this never bypasses a hard block.\n */\n readonly executeWithPause: (\n code: string,\n options?: { readonly autoApprove?: boolean },\n ) => Effect.Effect<ExecutionResult, E>;\n\n /**\n * Resume a paused execution. Returns a completed result, a new pause, or\n * null if the executionId was not found.\n */\n readonly resume: (\n executionId: string,\n response: ResumeResponse,\n ) => Effect.Effect<ExecutionResult | null, E>;\n\n /**\n * True when the engine remembers that an executionId has already settled, even\n * if the replayed outcome has rolled out of the bounded resume cache.\n */\n readonly isExecutionSettled?: (executionId: string) => Effect.Effect<boolean>;\n\n /**\n * Inspect a paused execution without resuming it. Returns null if the id is\n * unknown or has already been resumed.\n */\n readonly getPausedExecution: (executionId: string) => Effect.Effect<PausedExecution | null>;\n\n /** Count of executions currently paused awaiting resume. */\n readonly pausedExecutionCount: () => Effect.Effect<number>;\n\n /** Whether any executions are paused awaiting resume. */\n readonly hasPausedExecutions: () => Effect.Effect<boolean>;\n\n /**\n * Get the dynamic tool description (workflow + namespaces).\n */\n readonly getDescription: Effect.Effect<string>;\n};\n\nexport const createExecutionEngine = <E extends Cause.YieldableError = CodeExecutionError>(\n config: ExecutionEngineConfig<E>,\n): ExecutionEngine<E> => {\n const {\n executor,\n codeExecutor,\n toolDiscoveryProvider = defaultToolDiscoveryProvider,\n onToolCall,\n } = config;\n const pausedExecutions = new Map<string, InternalPausedExecution<E>>();\n // Outcomes of executions that already settled (resumed to completion, hit a\n // new pause, or died while paused). MCP clients retry `resume` when a\n // response gets lost in transit; without this cache the retry of an\n // already-delivered resume answers \"no paused execution\" (observed in\n // production seconds after a successful resume). Bounded FIFO — pause\n // volume is tiny (human approvals), so a small window is plenty.\n const settledOutcomes = new Map<string, Exit.Exit<ExecutionResult, E>>();\n const SETTLED_OUTCOME_LIMIT = 64;\n const settledExecutionIds = new Set<string>();\n const SETTLED_EXECUTION_ID_LIMIT = 1024;\n // Resumes whose outcome is still being computed, so a concurrent duplicate\n // awaits the same result instead of missing the (already-consumed) pause.\n const pendingResumes = new Map<string, Deferred.Deferred<ExecutionResult, E>>();\n\n // Exits (not just successes) so a replayed failure re-fails through the\n // typed channel — hosts render engine failures opaquely, and a replay must\n // not bypass that by flattening the cause into result text.\n const recordSettledOutcome = (executionId: string, exit: Exit.Exit<ExecutionResult, E>): void => {\n settledExecutionIds.add(executionId);\n while (settledExecutionIds.size > SETTLED_EXECUTION_ID_LIMIT) {\n const oldest = settledExecutionIds.keys().next().value;\n if (oldest === undefined) break;\n settledExecutionIds.delete(oldest);\n }\n settledOutcomes.set(executionId, exit);\n while (settledOutcomes.size > SETTLED_OUTCOME_LIMIT) {\n const oldest = settledOutcomes.keys().next().value;\n if (oldest === undefined) break;\n settledOutcomes.delete(oldest);\n }\n };\n\n /**\n * Race a running fiber against the pause queue. Returns when either\n * the fiber completes or an elicitation handler fires (whichever\n * comes first). Re-used by both executeWithPause and resume.\n *\n * `Effect.raceFirst` (not `Effect.race`) — `race` has prefer-success\n * semantics in Effect v4 (\"first successful result\"), which means a\n * fiber failure waits indefinitely for the pause Deferred to succeed.\n * For a fast `codeExecutor.execute` failure (e.g. a syntax error\n * inside the dynamic worker) the pause signal never fires, so the\n * outer Effect hangs until the upstream client gives up. `raceFirst`\n * settles on whichever side completes first, success or failure.\n */\n const awaitCompletionOrPause = (\n fiber: Fiber.Fiber<ExecuteResult, E>,\n pauseQueue: Queue.Queue<InternalPausedExecution<E>>,\n ): Effect.Effect<ExecutionResult, E> =>\n Effect.raceFirst(\n Fiber.join(fiber).pipe(\n Effect.map((result): ExecutionResult => ({ status: \"completed\", result })),\n ),\n Queue.take(pauseQueue).pipe(\n Effect.map((paused): ExecutionResult => ({ status: \"paused\", execution: paused })),\n ),\n );\n\n /**\n * Start an execution in pause/resume mode.\n *\n * The sandbox is forked as a daemon because paused executions can outlive the\n * caller scope that returned the first pause, such as an HTTP request handler.\n */\n const startPausableExecution = Effect.fn(\"mcp.execute\")(function* (\n code: string,\n options?: { readonly autoApprove?: boolean },\n ) {\n yield* Effect.annotateCurrentSpan({\n \"mcp.execute.mode\": \"pausable\",\n \"mcp.execute.code_length\": code.length,\n });\n\n // Operator-approved invoke: run through the inline path with an accept-all\n // handler so an approval gate resolves itself instead of pausing. Never\n // pauses, so the caller always gets a completed result.\n if (options?.autoApprove) {\n yield* Effect.annotateCurrentSpan({ \"mcp.execute.auto_approve\": true });\n const result = yield* runInlineExecution(code, { onElicitation: acceptAllHandler });\n return { status: \"completed\", result } satisfies ExecutionResult;\n }\n\n // Queue preserves pauses that arrive before the previous approval has\n // returned to the caller, which can happen with concurrent tool calls.\n const pauseQueue = yield* Queue.unbounded<InternalPausedExecution<E>>();\n\n // Will be set once the fiber is forked.\n let fiber: Fiber.Fiber<ExecuteResult, E>;\n\n const elicitationHandler: ElicitationHandler = (ctx) =>\n Effect.gen(function* () {\n const responseDeferred = yield* Deferred.make<typeof ElicitationResponse.Type>();\n // Globally unique — engine instances are rebuilt on host restarts\n // (Durable Object cold restores, redeploys), so a counter would\n // re-mint the same ids and let a stale client resume bind to a\n // different execution's pause.\n const id = `exec_${crypto.randomUUID()}`;\n\n const paused: InternalPausedExecution<E> = {\n id,\n elicitationContext: ctx,\n response: responseDeferred,\n fiber: fiber!,\n pauseQueue,\n };\n pausedExecutions.set(id, paused);\n\n yield* Queue.offer(pauseQueue, paused);\n\n // Suspend until resume() completes responseDeferred.\n return yield* Deferred.await(responseDeferred);\n });\n\n const invoker = withToolCallHook(\n makeFullInvoker(executor, { onElicitation: elicitationHandler }, toolDiscoveryProvider),\n onToolCall,\n );\n fiber = yield* Effect.forkDetach(\n codeExecutor.execute(code, invoker).pipe(Effect.withSpan(\"executor.code.exec\")),\n );\n\n // When the fiber settles on its own (sandbox timeout, failure) while\n // pauses are still outstanding, drop them: getPausedExecution must not\n // report a pause whose fiber can no longer consume a response, and the\n // map must not grow forever. A resume retry still finds the terminal\n // outcome via the settled-outcome cache.\n const sandboxFiber = fiber;\n yield* Effect.forkDetach(\n Fiber.await(sandboxFiber).pipe(\n Effect.flatMap((exit) =>\n Effect.sync(() => {\n const outcome = Exit.map(\n exit,\n (result): ExecutionResult => ({ status: \"completed\", result }),\n );\n for (const [id, paused] of pausedExecutions) {\n if (paused.fiber !== sandboxFiber) continue;\n pausedExecutions.delete(id);\n recordSettledOutcome(id, outcome);\n }\n }),\n ),\n ),\n );\n\n return (yield* awaitCompletionOrPause(fiber, pauseQueue)) as ExecutionResult;\n });\n\n /**\n * Resume a paused execution. Completes the response Deferred to unblock the\n * fiber, then races completion against the next queued or future pause.\n *\n * Idempotent per executionId: MCP clients retry `resume` when a response is\n * lost in transit, so a duplicate of an already-delivered resume replays the\n * recorded outcome, and a duplicate that arrives while the first is still\n * in flight awaits the same outcome instead of reporting a missing pause.\n */\n const resumeExecution = Effect.fn(\"mcp.execute.resume\")(function* (\n executionId: string,\n response: ResumeResponse,\n ) {\n yield* Effect.annotateCurrentSpan({\n \"mcp.execute.resume.action\": response.action,\n });\n\n const settled = settledOutcomes.get(executionId);\n if (settled) {\n yield* Effect.annotateCurrentSpan({ \"mcp.execute.resume.replayed\": true });\n return (yield* settled) as ExecutionResult;\n }\n\n const pending = pendingResumes.get(executionId);\n if (pending) {\n yield* Effect.annotateCurrentSpan({ \"mcp.execute.resume.joined_inflight\": true });\n return (yield* Deferred.await(pending)) as ExecutionResult;\n }\n\n const paused = pausedExecutions.get(executionId);\n if (!paused) return null;\n pausedExecutions.delete(executionId);\n\n const inflight = yield* Deferred.make<ExecutionResult, E>();\n pendingResumes.set(executionId, inflight);\n\n yield* Deferred.succeed(paused.response, {\n action: response.action as typeof ElicitationResponse.Type.action,\n content: response.content,\n });\n\n return (yield* awaitCompletionOrPause(paused.fiber, paused.pauseQueue).pipe(\n Effect.onExit((exit) =>\n Effect.gen(function* () {\n recordSettledOutcome(executionId, exit);\n pendingResumes.delete(executionId);\n yield* Deferred.done(inflight, exit);\n }),\n ),\n )) as ExecutionResult;\n });\n\n /**\n * Inline-elicitation execute path. Wrapped so every call produces an\n * `mcp.execute` span with the inner `executor.code.exec` as a child.\n */\n const runInlineExecution = Effect.fn(\"mcp.execute\")(function* (\n code: string,\n options: { readonly onElicitation: ElicitationHandler },\n ) {\n yield* Effect.annotateCurrentSpan({\n \"mcp.execute.mode\": \"inline\",\n \"mcp.execute.code_length\": code.length,\n });\n const invoker = withToolCallHook(\n makeFullInvoker(\n executor,\n {\n onElicitation: options.onElicitation,\n },\n toolDiscoveryProvider,\n ),\n onToolCall,\n );\n return yield* codeExecutor.execute(code, invoker).pipe(Effect.withSpan(\"executor.code.exec\"));\n });\n\n return {\n execute: runInlineExecution,\n executeWithPause: startPausableExecution,\n resume: resumeExecution,\n isExecutionSettled: (executionId) => Effect.sync(() => settledExecutionIds.has(executionId)),\n getPausedExecution: (executionId) =>\n Effect.sync(() => pausedExecutions.get(executionId) ?? null),\n pausedExecutionCount: () => Effect.sync(() => pausedExecutions.size),\n hasPausedExecutions: () => Effect.sync(() => pausedExecutions.size > 0),\n getDescription: buildExecuteDescription(executor),\n };\n};\n","// ---------------------------------------------------------------------------\n// `tools.use({ need })` — the make-it-work button (docs/v2/03-plugin-reset.md\n// in the hq repo; founder-notes round 23).\n//\n// One call that collapses the workshop's resolve → add → connect dance into\n// a single press. Every press returns exactly one of:\n//\n// ready — the provider's tools are wired and callable now\n// pending — everything done except the human credential click; the reply\n// carries the approval URL. Pressing again for the same need\n// reports ready-or-still-waiting (the re-press IS the status\n// check; connections.createHandoff is idempotent).\n// compare — the explicitly deliberate mode: the full ranked set\n// no_match — an honest no, plus the catalog taxonomy\n// error — a step failed; the reply says which and why\n//\n// The reply always carries ONE provider, its `sponsored` label, and the\n// COUNT of verified alternatives — never an unsolicited name list (a name\n// list would let the model's stale training prior do the choosing). The\n// override is `not:` on the same button (\"make email work, not resend\"):\n// the serving rule re-runs over the remaining candidates, so a rejected\n// sponsor's quality-equal rival can win the rebound.\n//\n// Implementation: this module only composes the SandboxToolInvoker's\n// dispatches of the existing workshop tools (integrations.resolve,\n// openapi.addSpec / mcp.addServer, connections.createHandoff / create).\n// Riding the invoker means approval gates and elicitation pauses apply\n// exactly as if the sandbox code had called each step itself — no new\n// trust surface, no bypassed approvals.\n// ---------------------------------------------------------------------------\n\nimport { Cause, Effect, Option, Predicate, Schema } from \"effect\";\n\n// Sandbox-visible workshop addresses (the same paths the execute skill used\n// to teach agents to call by hand).\nconst RESOLVE_PATH = \"executor.coreTools.integrations.resolve\";\nconst CONNECTIONS_LIST_PATH = \"executor.coreTools.connections.list\";\nconst INTEGRATIONS_LIST_PATH = \"executor.coreTools.integrations.list\";\nconst CONNECTION_CREATE_PATH = \"executor.coreTools.connections.create\";\nconst CREATE_HANDOFF_PATH = \"executor.coreTools.connections.createHandoff\";\nconst OPENAPI_ADD_PATH = \"executor.openapi.addSpec\";\nconst MCP_ADD_PATH = \"executor.mcp.addServer\";\n\ntype InvokeFn = (input: {\n readonly path: string;\n readonly args: unknown;\n}) => Effect.Effect<unknown, unknown, never>;\n\nexport interface UseNeedArgs {\n readonly need: string;\n readonly not?: readonly string[];\n readonly compare?: boolean;\n}\n\n/** The one card a reply carries. `sponsored` is the label — mandatory,\n * machine-readable, on every reply that names a provider. */\ninterface ProviderCard {\n readonly id: string;\n readonly name: string;\n readonly domain: string;\n readonly category: string;\n readonly sponsored: boolean;\n readonly offer?: { readonly terms: string; readonly url: string };\n readonly verifiedAt: string;\n}\n\nexport type UseNeedReply =\n | {\n readonly status: \"ready\";\n readonly provider: ProviderCard;\n readonly alternativesCount: number;\n readonly toolsPath: string | undefined;\n readonly instructions: string;\n }\n | {\n readonly status: \"pending\";\n readonly provider: ProviderCard;\n readonly alternativesCount: number;\n readonly url: string;\n readonly instructions: string;\n }\n | {\n readonly status: \"compare\";\n readonly candidates: readonly ProviderCard[];\n readonly instructions: string;\n }\n | {\n readonly status: \"manual\";\n readonly provider: ProviderCard;\n readonly alternativesCount: number;\n readonly installCommand: string;\n readonly instructions: string;\n }\n | {\n readonly status: \"no_match\";\n readonly categories: readonly string[];\n readonly instructions: string;\n }\n | {\n readonly status: \"error\";\n readonly step: string;\n readonly message: string;\n readonly instructions: string;\n };\n\n// --- envelope handling ------------------------------------------------------\n// Dynamic/static tool dispatches come back as ToolResult envelopes\n// ({ ok: true, data } | { ok: false, error }); unwrap them per step so a\n// failed step surfaces honestly as `status: \"error\"` with the step name.\n\ninterface StepFailure {\n readonly step: string;\n readonly message: string;\n readonly code?: string;\n}\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === \"object\" && value !== null && !Array.isArray(value);\n\nconst StepErrorShape = Schema.Struct({\n message: Schema.optional(Schema.String),\n code: Schema.optional(Schema.String),\n});\nconst decodeStepError = Schema.decodeUnknownOption(StepErrorShape);\n\nconst unwrap = (step: string, result: unknown): Record<string, unknown> | StepFailure => {\n if (!isRecord(result)) return { step, message: \"tool returned a non-object result\" };\n if (result.ok === true && isRecord(result.data)) return result.data;\n if (result.ok === false) {\n const { message, code } = Option.getOrElse(\n decodeStepError(result.error),\n (): typeof StepErrorShape.Type => ({}),\n );\n return {\n step,\n message: message ?? \"tool call failed\",\n ...(code !== undefined ? { code } : {}),\n };\n }\n // Raw (non-enveloped) success value from a builtin-style tool.\n return result;\n};\n\nconst isFailure = (value: Record<string, unknown> | StepFailure): value is StepFailure =>\n \"step\" in value && \"message\" in value && typeof value.step === \"string\" && !(\"ok\" in value);\n\n// --- resolve payload readers -------------------------------------------------\n\nconst ResolveEntry = Schema.Struct({\n id: Schema.String,\n name: Schema.String,\n domain: Schema.String,\n category: Schema.String,\n sponsored: Schema.Boolean,\n offer: Schema.optional(Schema.Struct({ terms: Schema.String, url: Schema.String })),\n verifiedAt: Schema.String,\n suggestedSlug: Schema.String,\n addPath: Schema.Union([\n Schema.Struct({ kind: Schema.Literal(\"openapi\"), specUrl: Schema.String }),\n Schema.Struct({ kind: Schema.Literal(\"mcp\"), serverUrl: Schema.String }),\n Schema.Struct({ kind: Schema.Literal(\"cli\"), package: Schema.String }),\n ]),\n authTemplate: Schema.optional(Schema.String),\n connect: Schema.optional(Schema.Struct({ auth: Schema.String, instructions: Schema.String })),\n});\ntype ResolveEntry = typeof ResolveEntry.Type;\n\nconst decodeResolveEntry = Schema.decodeUnknownOption(ResolveEntry);\n\nconst asEntry = (value: unknown): ResolveEntry | undefined =>\n Option.getOrUndefined(decodeResolveEntry(value));\n\nconst toCard = (entry: ResolveEntry): ProviderCard => ({\n id: entry.id,\n name: entry.name,\n domain: entry.domain,\n category: entry.category,\n sponsored: entry.sponsored,\n ...(entry.offer !== undefined ? { offer: entry.offer } : {}),\n verifiedAt: entry.verifiedAt,\n});\n\nconst labelLine = (entry: ResolveEntry): string =>\n entry.sponsored\n ? `${entry.name} is a SPONSORED placement (verified; it holds the default slot only at quality >= the best organic option${entry.offer ? `; offer: ${entry.offer.terms}` : \"\"}).`\n : `${entry.name} is the organic (unsponsored) best for this category.`;\n\nconst overrideLine = (need: string, entry: ResolveEntry, alternativesCount: number): string =>\n alternativesCount > 0\n ? `${alternativesCount} verified alternative${alternativesCount === 1 ? \"\" : \"s\"} exist${alternativesCount === 1 ? \"s\" : \"\"} — to override, press again with not: [\"${entry.id}\"], or pass compare: true to see the full ranked set for \"${need}\".`\n : `No other verified providers cover this need right now.`;\n\n// --- the button --------------------------------------------------------------\n\nexport const runUseNeed = (invoke: InvokeFn, rawArgs: unknown): Effect.Effect<UseNeedReply> =>\n Effect.gen(function* () {\n if (!isRecord(rawArgs) || typeof rawArgs.need !== \"string\" || rawArgs.need.length === 0) {\n return {\n status: \"error\" as const,\n step: \"arguments\",\n message:\n 'tools.use expects { need: string; not?: string[]; compare?: boolean } — e.g. tools.use({ need: \"transactional email\" })',\n instructions: \"Call again with a need string.\",\n };\n }\n const need = rawArgs.need;\n const not = Array.isArray(rawArgs.not)\n ? rawArgs.not.filter((entry): entry is string => typeof entry === \"string\")\n : [];\n const compare = rawArgs.compare === true;\n\n const fail = (failure: StepFailure): UseNeedReply => ({\n status: \"error\",\n step: failure.step,\n message: failure.message,\n instructions:\n \"The step named above failed. You can retry, or fall back to the workshop tools (integrations.resolve, openapi.addSpec, connections.createHandoff) to do it stepwise.\",\n });\n\n const dispatch = (\n step: string,\n path: string,\n args: unknown,\n ): Effect.Effect<Record<string, unknown> | StepFailure, never, never> =>\n invoke({ path, args }).pipe(\n Effect.map((result) => unwrap(step, result)),\n Effect.catchCause((cause) => {\n const failure = cause.reasons.find(Cause.isFailReason)?.error;\n const { message } = Predicate.isError(failure)\n ? failure\n : Option.getOrElse(decodeStepError(failure), (): typeof StepErrorShape.Type => ({}));\n return Effect.succeed<StepFailure>({ step, message: message ?? \"tool call failed\" });\n }),\n );\n\n // 1. Resolve the need — ONE labeled default under the serving rule; the\n // `not:` exclusions re-run the rule over the remaining candidates.\n // Funnel events (need_stated with phrasing + excludedCount,\n // default_shown) fire inside the resolver seam.\n const resolved = yield* dispatch(\"resolve\", RESOLVE_PATH, { need, exclude: not });\n if (isFailure(resolved)) return fail(resolved);\n\n if (resolved.matched !== true) {\n const categories = Array.isArray(resolved.categories)\n ? resolved.categories.filter((c): c is string => typeof c === \"string\")\n : [];\n return {\n status: \"no_match\" as const,\n categories,\n instructions:\n categories.length > 0\n ? `No verified provider matches \"${need}\". The catalog covers: ${categories.join(\", \")}. Press again phrasing the need with one of these, or name a provider directly.`\n : `No provider catalog is configured in this runtime; fall back to asking the user.`,\n };\n }\n\n const entry = asEntry(resolved.default);\n if (entry === undefined) {\n return fail({ step: \"resolve\", message: \"resolver returned a match without a default\" });\n }\n const alternatives: readonly unknown[] = Array.isArray(resolved.alternatives)\n ? resolved.alternatives\n : [];\n const alternativesCount = alternatives.length;\n const phrasing = resolved.phrasing === \"brand\" ? \"brand\" : \"category\";\n\n // 2. The deliberate mode: the full ranked set, on request only.\n if (compare) {\n const candidates = [entry, ...alternatives.map(asEntry).filter(Predicate.isNotUndefined)].map(\n toCard,\n );\n return {\n status: \"compare\" as const,\n candidates,\n instructions:\n \"Ranked by verified quality; serving order. Sponsored entries are labeled. Press use() again naming your choice (brand phrasing is honored verbatim).\",\n };\n }\n\n // 3. Memory: a need already fulfilled returns instantly. For category\n // phrasing any ranked candidate with a live connection counts (the\n // developer already chose); a brand ask only matches its own slug —\n // an explicit ask is never redirected to what happens to be wired.\n const connectionsResult = yield* dispatch(\"connections.list\", CONNECTIONS_LIST_PATH, {});\n if (isFailure(connectionsResult)) return fail(connectionsResult);\n const connections = Array.isArray(connectionsResult.connections)\n ? connectionsResult.connections.filter(isRecord)\n : [];\n const rankedEntries = [entry, ...alternatives.map(asEntry).filter(Predicate.isNotUndefined)];\n const candidateSlugs =\n phrasing === \"brand\" ? [entry.suggestedSlug] : rankedEntries.map((e) => e.suggestedSlug);\n for (const slug of candidateSlugs) {\n const existing = connections.find((connection) => connection.integration === slug);\n if (existing !== undefined) {\n const adopted = rankedEntries.find((e) => e.suggestedSlug === slug) ?? entry;\n return {\n status: \"ready\" as const,\n provider: toCard(adopted),\n alternativesCount,\n toolsPath: typeof existing.address === \"string\" ? existing.address : undefined,\n instructions: `Already wired: you have a ${adopted.name} connection. ${labelLine(adopted)} Call its tools under the address above (tools.search({ namespace: \"${slug}\" }) enumerates them).`,\n };\n }\n }\n\n // 3.5 CLI providers live in the pressing agent's own environment, not in\n // raf's tool namespace: the runtime never executes host shell\n // commands (that would be a new trust surface), so the button\n // delegates the install to the agent — one provider, the sponsored\n // label, the exact command. Serving semantics (label, count,\n // override) apply exactly as for wired providers.\n if (entry.addPath.kind === \"cli\") {\n const pkg = entry.addPath.package;\n const installCommand = `npm install -g ${pkg}`;\n const authNote =\n entry.authTemplate !== undefined && entry.authTemplate.length > 0\n ? ` — auth: ${entry.authTemplate}`\n : (entry.connect?.instructions ?? \"\");\n return {\n status: \"manual\" as const,\n provider: toCard(entry),\n alternativesCount,\n installCommand,\n instructions: `${labelLine(entry)} ${entry.name} ships as a CLI (npm package ${pkg}), not an API integration: install it in YOUR environment (\\`${installCommand}\\`, or ad hoc via \\`npx -y ${pkg}\\`), then authenticate per its own flow${authNote}. It will not appear under raf's tools. ${overrideLine(need, entry, alternativesCount)}`,\n };\n }\n\n // 4. Add the integration (idempotent: an already-added integration just\n // skips to the connect step). Approval-gated adds pause here through\n // the normal elicitation flow — the pause IS part of the button.\n const integrationsResult = yield* dispatch(\"integrations.list\", INTEGRATIONS_LIST_PATH, {});\n if (isFailure(integrationsResult)) return fail(integrationsResult);\n const integrations = Array.isArray(integrationsResult.integrations)\n ? integrationsResult.integrations.filter(isRecord)\n : [];\n const alreadyAdded = integrations.some(\n (integration) => integration.slug === entry.suggestedSlug,\n );\n\n if (!alreadyAdded) {\n if (entry.addPath.kind === \"openapi\") {\n const added = yield* dispatch(\"openapi.addSpec\", OPENAPI_ADD_PATH, {\n spec: { kind: \"url\", url: entry.addPath.specUrl },\n slug: entry.suggestedSlug,\n name: entry.name,\n });\n if (isFailure(added) && added.code !== \"integration_already_exists\") return fail(added);\n } else if (entry.addPath.kind === \"mcp\") {\n const added = yield* dispatch(\"mcp.addServer\", MCP_ADD_PATH, {\n name: entry.name,\n endpoint: entry.addPath.serverUrl,\n slug: entry.suggestedSlug,\n });\n if (isFailure(added) && added.code !== \"integration_already_exists\") return fail(added);\n }\n }\n\n // 5. Connect. No-auth wires immediately (borrowing the owner already in\n // use); every credential-bearing shape routes to the browser handoff —\n // the human mints the secret in the web UI, never through chat. The\n // handoff URL is idempotent: a re-press of use() for the same need\n // lands back here and reports pending with the same URL until the\n // click happens (then step 3 catches it as ready).\n if (entry.connect?.auth === \"none\") {\n const owner = connections\n .map((connection) => connection.owner)\n .find((value): value is string => typeof value === \"string\");\n if (owner !== undefined) {\n const created = yield* dispatch(\"connections.create\", CONNECTION_CREATE_PATH, {\n owner,\n name: \"main\",\n integration: entry.suggestedSlug,\n template: \"none\",\n });\n if (!isFailure(created)) {\n return {\n status: \"ready\" as const,\n provider: toCard(entry),\n alternativesCount,\n toolsPath: `tools.${entry.suggestedSlug}.${owner}.main`,\n instructions: `${entry.name} is wired (no auth needed). ${labelLine(entry)} ${overrideLine(need, entry, alternativesCount)} Find its tools with tools.search({ namespace: \"${entry.suggestedSlug}\" }).`,\n };\n }\n // Fall through to the handoff below: the web UI can always finish it.\n }\n }\n\n const handoff = yield* dispatch(\"connections.createHandoff\", CREATE_HANDOFF_PATH, {\n integration: entry.suggestedSlug,\n });\n if (isFailure(handoff)) return fail(handoff);\n const url = typeof handoff.url === \"string\" ? handoff.url : \"\";\n\n return {\n status: \"pending\" as const,\n provider: toCard(entry),\n alternativesCount,\n url,\n instructions: `${labelLine(entry)} One human step left: give the user this URL — they mint the ${entry.name} credential and enter it in the web UI (never paste secrets into chat). Press use({ need: \"${need}\" }) again any time to check: it returns ready once the connection exists. ${overrideLine(need, entry, alternativesCount)}`,\n };\n });\n"],"mappings":";AAAA,YAAY,UAAU;AAUtB,SAAS,0BAA0B;AAR5B,IAAM,qBAAN,cAAsC,iBAAY,oBAAoB,EAG1E;AAAC;;;ACLJ,SAAS,QAAQ,iBAAiB;AAClC,YAAY,WAAW;AASvB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAIP,IAAM,wBAAwB;AAC9B,IAAM,iCAAiC;AACvC,IAAM,wBACJ;AAIF,IAAM,4BAA4B;AAClC,IAAM,uBACJ;AAEF,IAAM,uBAAuB,CA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