@open-mercato/enterprise 0.10.1-develop.7721.1.b5c4a00a68 → 0.10.1-develop.7722.1.901c72b9e9

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.
@@ -5,6 +5,7 @@ import {
5
5
  resolveAdapterModules
6
6
  } from "@open-mercato/web-research";
7
7
  import { createModelFactory } from "@open-mercato/ai-assistant/modules/ai_assistant/lib/model-factory";
8
+ import { hostnameOf, isHostAllowed } from "./policy.js";
8
9
  const MODEL_ADAPTER_ID = "model-native";
9
10
  const MISSING_REGISTRY_REASON = "the generated web-research adapter registry is not registered on this container, so no adapter package could be loaded - run `yarn generate`, and if this is an MCP or CLI process, confirm bootstrap loaded web-research-adapters.generated.ts";
10
11
  function resolveAdapterEntries(container) {
@@ -94,12 +95,19 @@ function buildWebSearchEngine(options) {
94
95
  allowPrivateHosts: settings.guardrails.allowPrivateHosts
95
96
  });
96
97
  const cache = resolveCache(container, tenantId);
98
+ const restrictContentUrls = settings.guardrails.allowDomains.length > 0 || settings.guardrails.denyDomains.length > 0;
97
99
  const policy = settings.policy.lastResort !== null && spent.has(settings.policy.lastResort) ? { ...settings.policy, lastResort: null } : settings.policy;
98
100
  return {
99
101
  engine: createSearchEngine({
100
102
  policy,
101
103
  adapters,
102
104
  http,
105
+ ...restrictContentUrls ? {
106
+ isContentUrlAllowed: (url) => {
107
+ const host = hostnameOf(url);
108
+ return host !== null && isHostAllowed(host, settings.guardrails);
109
+ }
110
+ } : {},
103
111
  ...cache ? { cache } : {},
104
112
  ...options.onStep ? { onStep: options.onStep } : {}
105
113
  }),
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../src/modules/agent_orchestrator/lib/webSearch/registry.ts"],
4
- "sourcesContent": ["import type { AwilixContainer } from 'awilix'\nimport {\n createHttpClient,\n createSearchEngine,\n instantiateAdapter,\n resolveAdapterModules,\n type AdapterRegistryEntry,\n type EngineAdapterEntry,\n type ResultCache,\n type SearchEngine,\n type SearchEngineResult,\n type StepSink,\n} from '@open-mercato/web-research'\nimport { createModelFactory } from '@open-mercato/ai-assistant/modules/ai_assistant/lib/model-factory'\nimport type { WebSearchSettings } from './policy'\n\n/** Adapters that need a host capability the stored config cannot express. */\nconst MODEL_ADAPTER_ID = 'model-native'\n\ntype CacheLike = {\n get(key: string): Promise<unknown>\n set(key: string, value: unknown, options?: { ttl?: number; tags?: string[] }): Promise<void>\n}\n\nconst MISSING_REGISTRY_REASON =\n 'the generated web-research adapter registry is not registered on this container, so no adapter package could be loaded - run `yarn generate`, and if this is an MCP or CLI process, confirm bootstrap loaded web-research-adapters.generated.ts'\n\nfunction resolveAdapterEntries(container: AwilixContainer): {\n entries: readonly AdapterRegistryEntry[]\n missing: boolean\n} {\n try {\n const entries = container.resolve('webResearchAdapterEntries') as AdapterRegistryEntry[] | undefined\n return { entries: entries ?? [], missing: false }\n } catch {\n // An absent registry and an empty one look identical downstream: both give\n // zero adapters and a fast, empty, `degraded` answer that reads like \"the\n // web had nothing\" rather than \"this process is misconfigured\". The agent\n // then narrates that guess to the user, so the two cases are kept distinct.\n return { entries: [], missing: true }\n }\n}\n\n/**\n * The model adapter needs a live LLM handle, which is not serializable config.\n * Binding it here is the only place OM concepts meet adapter code.\n */\nexport function hostCapabilitiesFor(adapterId: string, container: AwilixContainer): Record<string, unknown> {\n if (adapterId !== MODEL_ADAPTER_ID) return {}\n return {\n resolveModel: () => createModelFactory(container).resolveModel({ moduleId: 'agent_orchestrator' }),\n }\n}\n\nfunction resolveCache(container: AwilixContainer, tenantId: string | null): ResultCache<SearchEngineResult> | null {\n let cache: CacheLike\n try {\n cache = container.resolve('cache') as CacheLike\n } catch {\n return null\n }\n const tag = `agent_orchestrator:web_search:${tenantId ?? 'global'}`\n return {\n async get(key) {\n try {\n const value = await cache.get(`webresearch:v1:${tenantId ?? 'global'}:${key}`)\n return (value as SearchEngineResult | undefined) ?? null\n } catch {\n return null\n }\n },\n async set(key, value, ttlMs) {\n try {\n await cache.set(`webresearch:v1:${tenantId ?? 'global'}:${key}`, value, {\n ttl: ttlMs,\n tags: [tag],\n })\n } catch {\n // A cache outage must never fail a search.\n }\n },\n }\n}\n\nexport type BuildEngineOptions = {\n readonly container: AwilixContainer\n readonly settings: WebSearchSettings\n readonly tenantId: string | null\n readonly onStep?: StepSink\n /** Adapters whose hourly call ceiling is spent; they sit this run out. */\n readonly spentBudgets?: ReadonlySet<string>\n}\n\nexport type BuiltEngine = {\n readonly engine: SearchEngine\n /** Adapters that could not be built, so the health surface can explain why. */\n readonly problems: ReadonlyArray<{ id: string | null; packageName: string; reason: string }>\n}\n\n/**\n * Builds a per-request engine: generated adapter registry filtered by policy,\n * each adapter constructed with its tenant options plus any host capability it\n * declared, over one hardened HTTP client.\n */\nexport function buildWebSearchEngine(options: BuildEngineOptions): BuiltEngine {\n const { container, settings, tenantId } = options\n const { entries, missing } = resolveAdapterEntries(container)\n const registry = resolveAdapterModules(entries)\n const problems: Array<{ id: string | null; packageName: string; reason: string }> = registry.rejected.map(\n (entry) => ({ id: entry.id, packageName: entry.packageName, reason: entry.reason }),\n )\n if (missing) {\n problems.push({ id: null, packageName: '(host)', reason: MISSING_REGISTRY_REASON })\n }\n\n const spent = options.spentBudgets ?? new Set<string>()\n const policyById = new Map(settings.policy.adapters.map((entry) => [entry.id, entry]))\n const adapters: EngineAdapterEntry[] = []\n\n for (const loaded of registry.loaded) {\n const id = loaded.module.id\n const configured = policyById.get(id)\n const built = instantiateAdapter(loaded, {\n ...((settings.adapterOptions[id] as Record<string, unknown> | undefined) ?? {}),\n ...hostCapabilitiesFor(id, container),\n })\n if (built.error) {\n problems.push({ id, packageName: loaded.packageName, reason: built.error })\n }\n if (spent.has(id)) {\n // Reported rather than silent: a search that quietly got worse because a\n // ceiling was reached looks exactly like the web having less to say.\n problems.push({\n id,\n packageName: loaded.packageName,\n reason: `hourly call ceiling of ${configured?.maxCallsPerHour} reached for this tenant; the adapter sat this search out`,\n })\n }\n adapters.push({\n adapter: built.adapter,\n enabled: (configured?.enabled ?? false) && !spent.has(id),\n order: configured?.order ?? Number.MAX_SAFE_INTEGER,\n weight: configured?.weight ?? 1,\n ...(configured?.timeoutMs === undefined ? {} : { timeoutMs: configured.timeoutMs }),\n })\n }\n\n // Deliberately not derived from `adapterTimeoutMs`: that is a whole adapter's\n // budget, while this is one HTTP attempt, and an adapter may make several.\n // Adapters that legitimately need longer pass `timeoutMs` per request.\n const http = createHttpClient({\n maxBytes: settings.guardrails.maxFetchBytes,\n // The only way an operator-hosted service on the container network becomes\n // reachable. Empty unless someone named a host.\n allowPrivateHosts: settings.guardrails.allowPrivateHosts,\n })\n\n const cache = resolveCache(container, tenantId)\n\n // `lastResort` deliberately bypasses `enabled`, which would let a spent ceiling\n // be charged again on every short run \u2014 the exact case the ceiling exists for.\n const policy =\n settings.policy.lastResort !== null && spent.has(settings.policy.lastResort)\n ? { ...settings.policy, lastResort: null }\n : settings.policy\n\n return {\n engine: createSearchEngine({\n policy,\n adapters,\n http,\n ...(cache ? { cache } : {}),\n ...(options.onStep ? { onStep: options.onStep } : {}),\n }),\n problems,\n }\n}\n"],
5
- "mappings": "AACA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAOK;AACP,SAAS,0BAA0B;AAInC,MAAM,mBAAmB;AAOzB,MAAM,0BACJ;AAEF,SAAS,sBAAsB,WAG7B;AACA,MAAI;AACF,UAAM,UAAU,UAAU,QAAQ,2BAA2B;AAC7D,WAAO,EAAE,SAAS,WAAW,CAAC,GAAG,SAAS,MAAM;AAAA,EAClD,QAAQ;AAKN,WAAO,EAAE,SAAS,CAAC,GAAG,SAAS,KAAK;AAAA,EACtC;AACF;AAMO,SAAS,oBAAoB,WAAmB,WAAqD;AAC1G,MAAI,cAAc,iBAAkB,QAAO,CAAC;AAC5C,SAAO;AAAA,IACL,cAAc,MAAM,mBAAmB,SAAS,EAAE,aAAa,EAAE,UAAU,qBAAqB,CAAC;AAA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,IACD;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import type { AwilixContainer } from 'awilix'\nimport {\n createHttpClient,\n createSearchEngine,\n instantiateAdapter,\n resolveAdapterModules,\n type AdapterRegistryEntry,\n type EngineAdapterEntry,\n type ResultCache,\n type SearchEngine,\n type SearchEngineResult,\n type StepSink,\n} from '@open-mercato/web-research'\nimport { createModelFactory } from '@open-mercato/ai-assistant/modules/ai_assistant/lib/model-factory'\nimport { hostnameOf, isHostAllowed, type WebSearchSettings } from './policy'\n\n/** Adapters that need a host capability the stored config cannot express. */\nconst MODEL_ADAPTER_ID = 'model-native'\n\ntype CacheLike = {\n get(key: string): Promise<unknown>\n set(key: string, value: unknown, options?: { ttl?: number; tags?: string[] }): Promise<void>\n}\n\nconst MISSING_REGISTRY_REASON =\n 'the generated web-research adapter registry is not registered on this container, so no adapter package could be loaded - run `yarn generate`, and if this is an MCP or CLI process, confirm bootstrap loaded web-research-adapters.generated.ts'\n\nfunction resolveAdapterEntries(container: AwilixContainer): {\n entries: readonly AdapterRegistryEntry[]\n missing: boolean\n} {\n try {\n const entries = container.resolve('webResearchAdapterEntries') as AdapterRegistryEntry[] | undefined\n return { entries: entries ?? [], missing: false }\n } catch {\n // An absent registry and an empty one look identical downstream: both give\n // zero adapters and a fast, empty, `degraded` answer that reads like \"the\n // web had nothing\" rather than \"this process is misconfigured\". The agent\n // then narrates that guess to the user, so the two cases are kept distinct.\n return { entries: [], missing: true }\n }\n}\n\n/**\n * The model adapter needs a live LLM handle, which is not serializable config.\n * Binding it here is the only place OM concepts meet adapter code.\n */\nexport function hostCapabilitiesFor(adapterId: string, container: AwilixContainer): Record<string, unknown> {\n if (adapterId !== MODEL_ADAPTER_ID) return {}\n return {\n resolveModel: () => createModelFactory(container).resolveModel({ moduleId: 'agent_orchestrator' }),\n }\n}\n\nfunction resolveCache(container: AwilixContainer, tenantId: string | null): ResultCache<SearchEngineResult> | null {\n let cache: CacheLike\n try {\n cache = container.resolve('cache') as CacheLike\n } catch {\n return null\n }\n const tag = `agent_orchestrator:web_search:${tenantId ?? 'global'}`\n return {\n async get(key) {\n try {\n const value = await cache.get(`webresearch:v1:${tenantId ?? 'global'}:${key}`)\n return (value as SearchEngineResult | undefined) ?? null\n } catch {\n return null\n }\n },\n async set(key, value, ttlMs) {\n try {\n await cache.set(`webresearch:v1:${tenantId ?? 'global'}:${key}`, value, {\n ttl: ttlMs,\n tags: [tag],\n })\n } catch {\n // A cache outage must never fail a search.\n }\n },\n }\n}\n\nexport type BuildEngineOptions = {\n readonly container: AwilixContainer\n readonly settings: WebSearchSettings\n readonly tenantId: string | null\n readonly onStep?: StepSink\n /** Adapters whose hourly call ceiling is spent; they sit this run out. */\n readonly spentBudgets?: ReadonlySet<string>\n}\n\nexport type BuiltEngine = {\n readonly engine: SearchEngine\n /** Adapters that could not be built, so the health surface can explain why. */\n readonly problems: ReadonlyArray<{ id: string | null; packageName: string; reason: string }>\n}\n\n/**\n * Builds a per-request engine: generated adapter registry filtered by policy,\n * each adapter constructed with its tenant options plus any host capability it\n * declared, over one hardened HTTP client.\n */\nexport function buildWebSearchEngine(options: BuildEngineOptions): BuiltEngine {\n const { container, settings, tenantId } = options\n const { entries, missing } = resolveAdapterEntries(container)\n const registry = resolveAdapterModules(entries)\n const problems: Array<{ id: string | null; packageName: string; reason: string }> = registry.rejected.map(\n (entry) => ({ id: entry.id, packageName: entry.packageName, reason: entry.reason }),\n )\n if (missing) {\n problems.push({ id: null, packageName: '(host)', reason: MISSING_REGISTRY_REASON })\n }\n\n const spent = options.spentBudgets ?? new Set<string>()\n const policyById = new Map(settings.policy.adapters.map((entry) => [entry.id, entry]))\n const adapters: EngineAdapterEntry[] = []\n\n for (const loaded of registry.loaded) {\n const id = loaded.module.id\n const configured = policyById.get(id)\n const built = instantiateAdapter(loaded, {\n ...((settings.adapterOptions[id] as Record<string, unknown> | undefined) ?? {}),\n ...hostCapabilitiesFor(id, container),\n })\n if (built.error) {\n problems.push({ id, packageName: loaded.packageName, reason: built.error })\n }\n if (spent.has(id)) {\n // Reported rather than silent: a search that quietly got worse because a\n // ceiling was reached looks exactly like the web having less to say.\n problems.push({\n id,\n packageName: loaded.packageName,\n reason: `hourly call ceiling of ${configured?.maxCallsPerHour} reached for this tenant; the adapter sat this search out`,\n })\n }\n adapters.push({\n adapter: built.adapter,\n enabled: (configured?.enabled ?? false) && !spent.has(id),\n order: configured?.order ?? Number.MAX_SAFE_INTEGER,\n weight: configured?.weight ?? 1,\n ...(configured?.timeoutMs === undefined ? {} : { timeoutMs: configured.timeoutMs }),\n })\n }\n\n // Deliberately not derived from `adapterTimeoutMs`: that is a whole adapter's\n // budget, while this is one HTTP attempt, and an adapter may make several.\n // Adapters that legitimately need longer pass `timeoutMs` per request.\n const http = createHttpClient({\n maxBytes: settings.guardrails.maxFetchBytes,\n // The only way an operator-hosted service on the container network becomes\n // reachable. Empty unless someone named a host.\n allowPrivateHosts: settings.guardrails.allowPrivateHosts,\n })\n\n const cache = resolveCache(container, tenantId)\n const restrictContentUrls =\n settings.guardrails.allowDomains.length > 0 || settings.guardrails.denyDomains.length > 0\n\n // `lastResort` deliberately bypasses `enabled`, which would let a spent ceiling\n // be charged again on every short run \u2014 the exact case the ceiling exists for.\n const policy =\n settings.policy.lastResort !== null && spent.has(settings.policy.lastResort)\n ? { ...settings.policy, lastResort: null }\n : settings.policy\n\n return {\n engine: createSearchEngine({\n policy,\n adapters,\n http,\n ...(restrictContentUrls\n ? {\n isContentUrlAllowed: (url: string) => {\n const host = hostnameOf(url)\n return host !== null && isHostAllowed(host, settings.guardrails)\n },\n }\n : {}),\n ...(cache ? { cache } : {}),\n ...(options.onStep ? { onStep: options.onStep } : {}),\n }),\n problems,\n }\n}\n"],
5
+ "mappings": "AACA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAOK;AACP,SAAS,0BAA0B;AACnC,SAAS,YAAY,qBAA6C;AAGlE,MAAM,mBAAmB;AAOzB,MAAM,0BACJ;AAEF,SAA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AAM,QAAQ,aAAa,WAAW,QAAQ;AAC9C,QAAM,sBACJ,SAAS,WAAW,aAAa,SAAS,KAAK,SAAS,WAAW,YAAY,SAAS;AAI1F,QAAM,SACJ,SAAS,OAAO,eAAe,QAAQ,MAAM,IAAI,SAAS,OAAO,UAAU,IACvE,EAAE,GAAG,SAAS,QAAQ,YAAY,KAAK,IACvC,SAAS;AAEf,SAAO;AAAA,IACL,QAAQ,mBAAmB;AAAA,MACzB;AAAA,MACA;AAAA,MACA;AAAA,MACA,GAAI,sBACA;AAAA,QACE,qBAAqB,CAAC,QAAgB;AACpC,gBAAM,OAAO,WAAW,GAAG;AAC3B,iBAAO,SAAS,QAAQ,cAAc,MAAM,SAAS,UAAU;AAAA,QACjE;AAAA,MACF,IACA,CAAC;AAAA,MACL,GAAI,QAAQ,EAAE,MAAM,IAAI,CAAC;AAAA,MACzB,GAAI,QAAQ,SAAS,EAAE,QAAQ,QAAQ,OAAO,IAAI,CAAC;AAAA,IACrD,CAAC;AAAA,IACD;AAAA,EACF;AACF;",
6
6
  "names": []
7
7
  }
@@ -151,7 +151,8 @@ const webFetchTool = {
151
151
  ...input.render === void 0 ? {} : { render: input.render }
152
152
  });
153
153
  if (outcome.status !== "ok") {
154
- return { ok: false, code: outcome.status, error: outcome.reason ?? outcome.status };
154
+ const code = outcome.status === "blocked" ? outcome.code ?? outcome.status : outcome.status;
155
+ return { ok: false, code, error: outcome.reason ?? outcome.status };
155
156
  }
156
157
  const finalHost = hostnameOf(outcome.page.url);
157
158
  if (!finalHost || !isHostAllowed(finalHost, settings.guardrails)) {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../src/modules/agent_orchestrator/lib/webSearch/tools.ts"],
4
- "sourcesContent": ["import { z } from 'zod'\nimport type { AiToolDefinition, McpToolContext } from '@open-mercato/ai-assistant/modules/ai_assistant/lib/types'\nimport {\n chargeAdapterCalls,\n chargeWebFetchBudget,\n enforceWebSearchRateLimit,\n resolveRunId,\n resolveSpentAdapterBudgets,\n} from './guardrails'\nimport { hostnameOf, isHostAllowed, resolveWebSearchSettings } from './policy'\nimport { buildWebSearchEngine } from './registry'\nimport { createStepEmitter } from './steps'\n\nexport const WEB_SEARCH_TOOL_ID = 'agent_orchestrator.web_search'\nexport const WEB_FETCH_TOOL_ID = 'agent_orchestrator.web_fetch'\n\n/**\n * Split so a tenant can be granted discovery without arbitrary URL retrieval.\n * `web_fetch` keeps the original feature id as a second requirement, so every\n * existing grant keeps working while the new one can be withheld.\n */\nconst WEB_SEARCH_FEATURE = 'agent_orchestrator.web_search'\nconst WEB_FETCH_FEATURE = 'agent_orchestrator.web_fetch'\n\nconst webSearchInput = z.object({\n query: z\n .string()\n .min(1)\n .max(500)\n .describe('Search query. Use for discovery - finding pages and sources on the public web.'),\n limit: z\n .number()\n .int()\n .min(1)\n .max(20)\n .optional()\n .describe('Max results to return; the server caps this to its configured maximum.'),\n site: z.string().max(253).optional().describe('Restrict results to a single domain, e.g. \"example.com\".'),\n freshness: z\n .enum(['day', 'week', 'month', 'year'])\n .optional()\n .describe('Prefer sources published within this window.'),\n includeContent: z\n .boolean()\n .optional()\n .describe(\n 'Also read the top results and return their page text inline. Prefer this over calling web_fetch per result.',\n ),\n})\n\nconst webFetchInput = z.object({\n url: z.url().max(2048).describe('Absolute http(s) URL to retrieve as readable text.'),\n render: z\n .enum(['auto', 'never', 'always'])\n .optional()\n .describe('auto (default) renders in a browser only when the plain read looks like a JavaScript shell.'),\n})\n\n/**\n * Resolves the run this call belongs to and wires the live step feed to it.\n * Steps are broadcast, never returned to the model: `diagnostics` is the compact\n * summary it can act on, and a full narration would just spend context.\n */\nasync function loadContext(ctx: McpToolContext, query: string) {\n const settings = await resolveWebSearchSettings(ctx.container, ctx.tenantId ?? null)\n const runId = await resolveRunId(ctx.container, ctx.sessionId)\n const agentId = await resolveAgentId(ctx)\n // Checked before the engine is built, because the only way to not pay for an\n // adapter is to not enable it \u2014 the engine has no notion of cost.\n const spentBudgets = await resolveSpentAdapterBudgets(\n ctx.container,\n ctx.tenantId ?? null,\n settings.policy.adapters,\n )\n const built = buildWebSearchEngine({\n container: ctx.container,\n settings,\n tenantId: ctx.tenantId ?? null,\n spentBudgets,\n onStep: createStepEmitter({\n runId,\n agentId,\n tenantId: ctx.tenantId ?? null,\n organizationId: ctx.organizationId ?? null,\n query,\n }),\n })\n return { settings, runId, ...built }\n}\n\n/**\n * The engine is built per call, and adapters may own OS resources \u2014 the browser\n * tier holds a sidecar process and a Chromium. Without this every call that\n * touched that tier would leak one of each for the life of the server.\n */\nasync function disposeQuietly(engine: { dispose(): Promise<void> }): Promise<void> {\n try {\n await engine.dispose()\n } catch {\n // Teardown is best-effort; a disposal fault must not mask the tool's result.\n }\n}\n\nasync function resolveAgentId(ctx: McpToolContext): Promise<string | null> {\n if (!ctx.sessionId) return null\n try {\n const store = ctx.container.resolve('agentRunSessionStore') as {\n resolveActiveAgentId(token: string): Promise<string | null>\n }\n return await store.resolveActiveAgentId(ctx.sessionId)\n } catch {\n return null\n }\n}\n\n/**\n * Read-only web search over the adapter engine. Errors are returned as data so a\n * failed lookup can never crash the agent loop, and `diagnostics` explains what\n * each adapter did - a thin answer should be legible to the model, not silent.\n */\nexport const webSearchTool: AiToolDefinition = {\n name: WEB_SEARCH_TOOL_ID,\n displayName: 'Web search',\n description:\n 'Search the public web and return ranked results (title, url, snippet, confidence). Set includeContent to also read the top pages in one call. Read-only.',\n inputSchema: webSearchInput,\n requiredFeatures: [WEB_SEARCH_FEATURE],\n isMutation: false,\n tags: ['read', 'agent_orchestrator', 'web'],\n async handler(rawInput, ctx) {\n const input = webSearchInput.parse(rawInput)\n const { settings, engine, problems, runId } = await loadContext(ctx, input.query)\n\n try {\n const gate = await enforceWebSearchRateLimit(\n ctx.container,\n { runId, tenantId: ctx.tenantId ?? null, kind: 'search' },\n settings.guardrails,\n )\n if (!gate.ok) return { ok: false as const, code: 'rate_limited' as const, error: gate.error }\n\n const result = await engine.search({\n query: input.query,\n ...(input.limit === undefined ? {} : { limit: input.limit }),\n ...(input.site === undefined ? {} : { site: input.site }),\n ...(input.freshness === undefined ? {} : { freshness: input.freshness }),\n ...(input.includeContent === undefined ? {} : { includeContent: input.includeContent }),\n })\n\n // includeContent reads pages under a single search point, so the reads are\n // charged here or the fetch budget never engages on the path the tool\n // description recommends.\n await chargeWebFetchBudget(ctx.container, runId, settings.guardrails, result.diagnostics.pagesRead)\n\n // Charged from what actually ran, not from what was planned: quorum often\n // settles before the expensive adapter is ever reached, and billing for a\n // call that never happened would exhaust a ceiling that cost nothing.\n await chargeAdapterCalls(\n ctx.container,\n ctx.tenantId ?? null,\n settings.policy.adapters,\n result.diagnostics.adapters\n .filter((entry) => entry.status !== 'skipped' && entry.status !== 'cancelled')\n .map((entry) => entry.id),\n )\n\n const allowed = result.results.filter((hit) => {\n const host = hostnameOf(hit.url)\n return host ? isHostAllowed(host, settings.guardrails) : false\n })\n\n return {\n ok: true as const,\n results: allowed,\n answer: result.answer,\n diagnostics: {\n adapters: result.diagnostics.adapters,\n degraded: result.diagnostics.degraded || allowed.length !== result.results.length,\n cached: result.diagnostics.cached,\n escalated: result.diagnostics.escalated,\n elapsedMs: result.diagnostics.elapsedMs,\n ...(problems.length > 0 ? { problems } : {}),\n },\n }\n } finally {\n await disposeQuietly(engine)\n }\n },\n}\n\n/** Read-only single-URL retrieval, with automatic browser rendering for JS shells. */\nexport const webFetchTool: AiToolDefinition = {\n name: WEB_FETCH_TOOL_ID,\n displayName: 'Web fetch',\n description:\n 'Retrieve a single public http(s) URL and return its readable text (size-capped). Read-only; prefer web_search with includeContent when you have not chosen a page yet.',\n inputSchema: webFetchInput,\n requiredFeatures: [WEB_SEARCH_FEATURE, WEB_FETCH_FEATURE],\n isMutation: false,\n tags: ['read', 'agent_orchestrator', 'web'],\n async handler(rawInput, ctx) {\n const input = webFetchInput.parse(rawInput)\n const { settings, engine, runId } = await loadContext(ctx, input.url)\n\n try {\n const host = hostnameOf(input.url)\n if (!host || !isHostAllowed(host, settings.guardrails)) {\n return {\n ok: false as const,\n code: 'domain_blocked' as const,\n error: `domain not allowed: ${host ?? input.url}`,\n }\n }\n\n const gate = await enforceWebSearchRateLimit(\n ctx.container,\n { runId, tenantId: ctx.tenantId ?? null, kind: 'fetch' },\n settings.guardrails,\n )\n if (!gate.ok) return { ok: false as const, code: 'rate_limited' as const, error: gate.error }\n\n const outcome = await engine.fetch({\n url: input.url,\n maxBytes: settings.guardrails.maxFetchBytes,\n ...(input.render === undefined ? {} : { render: input.render }),\n })\n\n if (outcome.status !== 'ok') {\n return { ok: false as const, code: outcome.status, error: outcome.reason ?? outcome.status }\n }\n // The engine follows redirects, so the host that actually answered may not\n // be the one the guardrail cleared above.\n const finalHost = hostnameOf(outcome.page.url)\n if (!finalHost || !isHostAllowed(finalHost, settings.guardrails)) {\n return {\n ok: false as const,\n code: 'domain_blocked' as const,\n error: `domain not allowed after redirect: ${finalHost ?? outcome.page.url}`,\n }\n }\n return { ok: true as const, ...outcome.page }\n } finally {\n await disposeQuietly(engine)\n }\n },\n}\n"],
5
- "mappings": "AAAA,SAAS,SAAS;AAElB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,YAAY,eAAe,gCAAgC;AACpE,SAAS,4BAA4B;AACrC,SAAS,yBAAyB;AAE3B,MAAM,qBAAqB;AAC3B,MAAM,oBAAoB;AAOjC,MAAM,qBAAqB;AAC3B,MAAM,oBAAoB;AAE1B,MAAM,iBAAiB,EAAE,OAAO;AAAA,EAC9B,OAAO,EACJ,OAAO,EACP,IAAI,CAAC,EACL,IAAI,GAAG,EACP,SAAS,gFAAgF;AAAA,EAC5F,OAAO,EACJ,OAAO,EACP,IAAI,EACJ,IAAI,CAAC,EACL,IAAI,EAAE,EACN,SAAS,EACT,SAAS,wEAAwE;AAAA,EACpF,MAAM,EAAE,OAAO,EAAE,IAAI,GAAG,EAAE,SAAS,EAAE,SAAS,0DAA0D;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OAAO,MAAM;AAAA,QACjB,IAAI;AAAA,QACJ,EAAE,OAAO,UAAU,IAAI,YAAY,MAAM,MAAM,QAAQ;AAAA,QACvD,SAAS;AAAA,MACX;AACA,UAAI,CAAC,KAAK,GAAI,QAAO,EAAE,IAAI,OAAgB,MAAM,gBAAyB,OAAO,KAAK,MAAM;AAE5F,YAAM,UAAU,MAAM,OAAO,MAAM;AAAA,QACjC,KAAK,MAAM;AAAA,QACX,UAAU,SAAS,WAAW;AAAA,QAC9B,GAAI,MAAM,WAAW,SAAY,CAAC,IAAI,EAAE,QAAQ,MAAM,OAAO;AAAA,MAC/D,CAAC;AAED,UAAI,QAAQ,WAAW,MAAM;AAC3B,eAAO,EAAE,IAAI,OAAgB,MAAM,QAAQ,QAAQ,OAAO,QAAQ,UAAU,QAAQ,OAAO;AAAA,MAC7F;AAGA,YAAM,YAAY,WAAW,QAAQ,KAAK,GAAG;AAC7C,UAAI,CAAC,aAAa,CAAC,cAAc,WAAW,SAAS,UAAU,GAAG;AAChE,eAAO;AAAA,UACL,IAAI;AAAA,UACJ,MAAM;AAAA,UACN,OAAO,sCAAsC,aAAa,QAAQ,KAAK,GAAG;AAAA,QAC5E;AAAA,MACF;AACA,aAAO,EAAE,IAAI,MAAe,GAAG,QAAQ,KAAK;AAAA,IAC9C,UAAE;AACA,YAAM,eAAe,MAAM;AAAA,IAC7B;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import { z } from 'zod'\nimport type { AiToolDefinition, McpToolContext } from '@open-mercato/ai-assistant/modules/ai_assistant/lib/types'\nimport {\n chargeAdapterCalls,\n chargeWebFetchBudget,\n enforceWebSearchRateLimit,\n resolveRunId,\n resolveSpentAdapterBudgets,\n} from './guardrails'\nimport { hostnameOf, isHostAllowed, resolveWebSearchSettings } from './policy'\nimport { buildWebSearchEngine } from './registry'\nimport { createStepEmitter } from './steps'\n\nexport const WEB_SEARCH_TOOL_ID = 'agent_orchestrator.web_search'\nexport const WEB_FETCH_TOOL_ID = 'agent_orchestrator.web_fetch'\n\n/**\n * Split so a tenant can be granted discovery without arbitrary URL retrieval.\n * `web_fetch` keeps the original feature id as a second requirement, so every\n * existing grant keeps working while the new one can be withheld.\n */\nconst WEB_SEARCH_FEATURE = 'agent_orchestrator.web_search'\nconst WEB_FETCH_FEATURE = 'agent_orchestrator.web_fetch'\n\nconst webSearchInput = z.object({\n query: z\n .string()\n .min(1)\n .max(500)\n .describe('Search query. Use for discovery - finding pages and sources on the public web.'),\n limit: z\n .number()\n .int()\n .min(1)\n .max(20)\n .optional()\n .describe('Max results to return; the server caps this to its configured maximum.'),\n site: z.string().max(253).optional().describe('Restrict results to a single domain, e.g. \"example.com\".'),\n freshness: z\n .enum(['day', 'week', 'month', 'year'])\n .optional()\n .describe('Prefer sources published within this window.'),\n includeContent: z\n .boolean()\n .optional()\n .describe(\n 'Also read the top results and return their page text inline. Prefer this over calling web_fetch per result.',\n ),\n})\n\nconst webFetchInput = z.object({\n url: z.url().max(2048).describe('Absolute http(s) URL to retrieve as readable text.'),\n render: z\n .enum(['auto', 'never', 'always'])\n .optional()\n .describe('auto (default) renders in a browser only when the plain read looks like a JavaScript shell.'),\n})\n\n/**\n * Resolves the run this call belongs to and wires the live step feed to it.\n * Steps are broadcast, never returned to the model: `diagnostics` is the compact\n * summary it can act on, and a full narration would just spend context.\n */\nasync function loadContext(ctx: McpToolContext, query: string) {\n const settings = await resolveWebSearchSettings(ctx.container, ctx.tenantId ?? null)\n const runId = await resolveRunId(ctx.container, ctx.sessionId)\n const agentId = await resolveAgentId(ctx)\n // Checked before the engine is built, because the only way to not pay for an\n // adapter is to not enable it \u2014 the engine has no notion of cost.\n const spentBudgets = await resolveSpentAdapterBudgets(\n ctx.container,\n ctx.tenantId ?? null,\n settings.policy.adapters,\n )\n const built = buildWebSearchEngine({\n container: ctx.container,\n settings,\n tenantId: ctx.tenantId ?? null,\n spentBudgets,\n onStep: createStepEmitter({\n runId,\n agentId,\n tenantId: ctx.tenantId ?? null,\n organizationId: ctx.organizationId ?? null,\n query,\n }),\n })\n return { settings, runId, ...built }\n}\n\n/**\n * The engine is built per call, and adapters may own OS resources \u2014 the browser\n * tier holds a sidecar process and a Chromium. Without this every call that\n * touched that tier would leak one of each for the life of the server.\n */\nasync function disposeQuietly(engine: { dispose(): Promise<void> }): Promise<void> {\n try {\n await engine.dispose()\n } catch {\n // Teardown is best-effort; a disposal fault must not mask the tool's result.\n }\n}\n\nasync function resolveAgentId(ctx: McpToolContext): Promise<string | null> {\n if (!ctx.sessionId) return null\n try {\n const store = ctx.container.resolve('agentRunSessionStore') as {\n resolveActiveAgentId(token: string): Promise<string | null>\n }\n return await store.resolveActiveAgentId(ctx.sessionId)\n } catch {\n return null\n }\n}\n\n/**\n * Read-only web search over the adapter engine. Errors are returned as data so a\n * failed lookup can never crash the agent loop, and `diagnostics` explains what\n * each adapter did - a thin answer should be legible to the model, not silent.\n */\nexport const webSearchTool: AiToolDefinition = {\n name: WEB_SEARCH_TOOL_ID,\n displayName: 'Web search',\n description:\n 'Search the public web and return ranked results (title, url, snippet, confidence). Set includeContent to also read the top pages in one call. Read-only.',\n inputSchema: webSearchInput,\n requiredFeatures: [WEB_SEARCH_FEATURE],\n isMutation: false,\n tags: ['read', 'agent_orchestrator', 'web'],\n async handler(rawInput, ctx) {\n const input = webSearchInput.parse(rawInput)\n const { settings, engine, problems, runId } = await loadContext(ctx, input.query)\n\n try {\n const gate = await enforceWebSearchRateLimit(\n ctx.container,\n { runId, tenantId: ctx.tenantId ?? null, kind: 'search' },\n settings.guardrails,\n )\n if (!gate.ok) return { ok: false as const, code: 'rate_limited' as const, error: gate.error }\n\n const result = await engine.search({\n query: input.query,\n ...(input.limit === undefined ? {} : { limit: input.limit }),\n ...(input.site === undefined ? {} : { site: input.site }),\n ...(input.freshness === undefined ? {} : { freshness: input.freshness }),\n ...(input.includeContent === undefined ? {} : { includeContent: input.includeContent }),\n })\n\n // includeContent reads pages under a single search point, so the reads are\n // charged here or the fetch budget never engages on the path the tool\n // description recommends.\n await chargeWebFetchBudget(ctx.container, runId, settings.guardrails, result.diagnostics.pagesRead)\n\n // Charged from what actually ran, not from what was planned: quorum often\n // settles before the expensive adapter is ever reached, and billing for a\n // call that never happened would exhaust a ceiling that cost nothing.\n await chargeAdapterCalls(\n ctx.container,\n ctx.tenantId ?? null,\n settings.policy.adapters,\n result.diagnostics.adapters\n .filter((entry) => entry.status !== 'skipped' && entry.status !== 'cancelled')\n .map((entry) => entry.id),\n )\n\n const allowed = result.results.filter((hit) => {\n const host = hostnameOf(hit.url)\n return host ? isHostAllowed(host, settings.guardrails) : false\n })\n\n return {\n ok: true as const,\n results: allowed,\n answer: result.answer,\n diagnostics: {\n adapters: result.diagnostics.adapters,\n degraded: result.diagnostics.degraded || allowed.length !== result.results.length,\n cached: result.diagnostics.cached,\n escalated: result.diagnostics.escalated,\n elapsedMs: result.diagnostics.elapsedMs,\n ...(problems.length > 0 ? { problems } : {}),\n },\n }\n } finally {\n await disposeQuietly(engine)\n }\n },\n}\n\n/** Read-only single-URL retrieval, with automatic browser rendering for JS shells. */\nexport const webFetchTool: AiToolDefinition = {\n name: WEB_FETCH_TOOL_ID,\n displayName: 'Web fetch',\n description:\n 'Retrieve a single public http(s) URL and return its readable text (size-capped). Read-only; prefer web_search with includeContent when you have not chosen a page yet.',\n inputSchema: webFetchInput,\n requiredFeatures: [WEB_SEARCH_FEATURE, WEB_FETCH_FEATURE],\n isMutation: false,\n tags: ['read', 'agent_orchestrator', 'web'],\n async handler(rawInput, ctx) {\n const input = webFetchInput.parse(rawInput)\n const { settings, engine, runId } = await loadContext(ctx, input.url)\n\n try {\n const host = hostnameOf(input.url)\n if (!host || !isHostAllowed(host, settings.guardrails)) {\n return {\n ok: false as const,\n code: 'domain_blocked' as const,\n error: `domain not allowed: ${host ?? input.url}`,\n }\n }\n\n const gate = await enforceWebSearchRateLimit(\n ctx.container,\n { runId, tenantId: ctx.tenantId ?? null, kind: 'fetch' },\n settings.guardrails,\n )\n if (!gate.ok) return { ok: false as const, code: 'rate_limited' as const, error: gate.error }\n\n const outcome = await engine.fetch({\n url: input.url,\n maxBytes: settings.guardrails.maxFetchBytes,\n ...(input.render === undefined ? {} : { render: input.render }),\n })\n\n if (outcome.status !== 'ok') {\n const code = outcome.status === 'blocked' ? outcome.code ?? outcome.status : outcome.status\n return { ok: false as const, code, error: outcome.reason ?? outcome.status }\n }\n // The engine follows redirects, so the host that actually answered may not\n // be the one the guardrail cleared above.\n const finalHost = hostnameOf(outcome.page.url)\n if (!finalHost || !isHostAllowed(finalHost, settings.guardrails)) {\n return {\n ok: false as const,\n code: 'domain_blocked' as const,\n error: `domain not allowed after redirect: ${finalHost ?? outcome.page.url}`,\n }\n }\n return { ok: true as const, ...outcome.page }\n } finally {\n await disposeQuietly(engine)\n }\n },\n}\n"],
5
+ "mappings": "AAAA,SAAS,SAAS;AAElB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,YAAY,eAAe,gCAAgC;AACpE,SAAS,4BAA4B;AACrC,SAAS,yBAAyB;AAE3B,MAAM,qBAAqB;AAC3B,MAAM,oBAAoB;AAOjC,MAAM,qBAAqB;AAC3B,MAAM,oBAAoB;AAE1B,MAAM,iBAAiB,EAAE,OAAO;AAAA,EAC9B,OAAO,EACJ,OAAO,EACP,IAAI,CAAC,EACL,IAAI,GAAG,EACP,SAAS,gFAAgF;AAAA,EAC5F,OAAO,EACJ,OAAO,EACP,IAAI,EACJ,IAAI,CAAC,EACL,IAAI,EAAE,EACN,SAAS,EACT,SAAS,wEAAwE;AAAA,EACpF,MAAM,EAAE,OAAO,EAAE,IAAI,GAAG,EAAE,SAAS,EAAE,SAAS,0DAA0D;AAAA,EACxG,WAAW,EACR,KAAK,CAAC,OAAO,QAAQ,SAAS,MAAM,CAAC,EACrC,SAAS,EACT,SAAS,8CAA8C;AAAA,EAC1D,gBAAgB,EACb,QAAQ,EACR,SAAS,EACT;AAAA,IACC;AAAA,EACF;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AAgB,MAAM,gBAAyB,OAAO,KAAK,MAAM;AAE5F,YAAM,UAAU,MAAM,OAAO,MAAM;AAAA,QACjC,KAAK,MAAM;AAAA,QACX,UAAU,SAAS,WAAW;AAAA,QAC9B,GAAI,MAAM,WAAW,SAAY,CAAC,IAAI,EAAE,QAAQ,MAAM,OAAO;AAAA,MAC/D,CAAC;AAED,UAAI,QAAQ,WAAW,MAAM;AAC3B,cAAM,OAAO,QAAQ,WAAW,YAAY,QAAQ,QAAQ,QAAQ,SAAS,QAAQ;AACrF,eAAO,EAAE,IAAI,OAAgB,MAAM,OAAO,QAAQ,UAAU,QAAQ,OAAO;AAAA,MAC7E;AAGA,YAAM,YAAY,WAAW,QAAQ,KAAK,GAAG;AAC7C,UAAI,CAAC,aAAa,CAAC,cAAc,WAAW,SAAS,UAAU,GAAG;AAChE,eAAO;AAAA,UACL,IAAI;AAAA,UACJ,MAAM;AAAA,UACN,OAAO,sCAAsC,aAAa,QAAQ,KAAK,GAAG;AAAA,QAC5E;AAAA,MACF;AACA,aAAO,EAAE,IAAI,MAAe,GAAG,QAAQ,KAAK;AAAA,IAC9C,UAAE;AACA,YAAM,eAAe,MAAM;AAAA,IAC7B;AAAA,EACF;AACF;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-mercato/enterprise",
3
- "version": "0.10.1-develop.7721.1.b5c4a00a68",
3
+ "version": "0.10.1-develop.7722.1.901c72b9e9",
4
4
  "license": "SEE LICENSE IN LICENSE.md",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -70,12 +70,12 @@
70
70
  "@dnd-kit/core": "^6.3.1",
71
71
  "@dnd-kit/sortable": "^10.0.0",
72
72
  "@dnd-kit/utilities": "^3.2.2",
73
- "@open-mercato/ai-assistant": "0.10.1-develop.7721.1.b5c4a00a68",
74
- "@open-mercato/core": "0.10.1-develop.7721.1.b5c4a00a68",
75
- "@open-mercato/queue": "0.10.1-develop.7721.1.b5c4a00a68",
76
- "@open-mercato/scheduler": "0.10.1-develop.7721.1.b5c4a00a68",
77
- "@open-mercato/ui": "0.10.1-develop.7721.1.b5c4a00a68",
78
- "@open-mercato/web-research": "0.10.1-develop.7721.1.b5c4a00a68",
73
+ "@open-mercato/ai-assistant": "0.10.1-develop.7722.1.901c72b9e9",
74
+ "@open-mercato/core": "0.10.1-develop.7722.1.901c72b9e9",
75
+ "@open-mercato/queue": "0.10.1-develop.7722.1.901c72b9e9",
76
+ "@open-mercato/scheduler": "0.10.1-develop.7722.1.901c72b9e9",
77
+ "@open-mercato/ui": "0.10.1-develop.7722.1.901c72b9e9",
78
+ "@open-mercato/web-research": "0.10.1-develop.7722.1.901c72b9e9",
79
79
  "@simplewebauthn/browser": "^13.3.0",
80
80
  "@simplewebauthn/server": "^13.3.2",
81
81
  "@simplewebauthn/types": "^12.0.0",
@@ -87,13 +87,13 @@
87
87
  },
88
88
  "peerDependencies": {
89
89
  "@mikro-orm/core": "^7.0.14",
90
- "@open-mercato/shared": "0.10.1-develop.7721.1.b5c4a00a68",
90
+ "@open-mercato/shared": "0.10.1-develop.7722.1.901c72b9e9",
91
91
  "react": "^19.0.0",
92
92
  "react-dom": "^19.0.0",
93
93
  "zod": ">=3.23.0"
94
94
  },
95
95
  "devDependencies": {
96
- "@open-mercato/shared": "0.10.1-develop.7721.1.b5c4a00a68",
96
+ "@open-mercato/shared": "0.10.1-develop.7722.1.901c72b9e9",
97
97
  "@types/jest": "^30.0.0",
98
98
  "@types/react": "^19.2.18",
99
99
  "@types/react-dom": "^19.2.5",
@@ -12,7 +12,7 @@ import {
12
12
  type StepSink,
13
13
  } from '@open-mercato/web-research'
14
14
  import { createModelFactory } from '@open-mercato/ai-assistant/modules/ai_assistant/lib/model-factory'
15
- import type { WebSearchSettings } from './policy'
15
+ import { hostnameOf, isHostAllowed, type WebSearchSettings } from './policy'
16
16
 
17
17
  /** Adapters that need a host capability the stored config cannot express. */
18
18
  const MODEL_ADAPTER_ID = 'model-native'
@@ -156,6 +156,8 @@ export function buildWebSearchEngine(options: BuildEngineOptions): BuiltEngine {
156
156
  })
157
157
 
158
158
  const cache = resolveCache(container, tenantId)
159
+ const restrictContentUrls =
160
+ settings.guardrails.allowDomains.length > 0 || settings.guardrails.denyDomains.length > 0
159
161
 
160
162
  // `lastResort` deliberately bypasses `enabled`, which would let a spent ceiling
161
163
  // be charged again on every short run — the exact case the ceiling exists for.
@@ -169,6 +171,14 @@ export function buildWebSearchEngine(options: BuildEngineOptions): BuiltEngine {
169
171
  policy,
170
172
  adapters,
171
173
  http,
174
+ ...(restrictContentUrls
175
+ ? {
176
+ isContentUrlAllowed: (url: string) => {
177
+ const host = hostnameOf(url)
178
+ return host !== null && isHostAllowed(host, settings.guardrails)
179
+ },
180
+ }
181
+ : {}),
172
182
  ...(cache ? { cache } : {}),
173
183
  ...(options.onStep ? { onStep: options.onStep } : {}),
174
184
  }),
@@ -226,7 +226,8 @@ export const webFetchTool: AiToolDefinition = {
226
226
  })
227
227
 
228
228
  if (outcome.status !== 'ok') {
229
- return { ok: false as const, code: outcome.status, error: outcome.reason ?? outcome.status }
229
+ const code = outcome.status === 'blocked' ? outcome.code ?? outcome.status : outcome.status
230
+ return { ok: false as const, code, error: outcome.reason ?? outcome.status }
230
231
  }
231
232
  // The engine follows redirects, so the host that actually answered may not
232
233
  // be the one the guardrail cleared above.