@indexnetwork/protocol 3.7.0-rc.276.1 → 3.7.1-rc.278.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.
Files changed (95) hide show
  1. package/dist/agent/agent.tools.js +1 -1
  2. package/dist/agent/agent.tools.js.map +1 -1
  3. package/dist/chat/chat.agent.d.ts +6 -6
  4. package/dist/chat/chat.agent.d.ts.map +1 -1
  5. package/dist/chat/chat.agent.js +9 -9
  6. package/dist/chat/chat.agent.js.map +1 -1
  7. package/dist/chat/chat.graph.d.ts.map +1 -1
  8. package/dist/chat/chat.graph.js +3 -12
  9. package/dist/chat/chat.graph.js.map +1 -1
  10. package/dist/chat/chat.interrupt.classifier.d.ts.map +1 -1
  11. package/dist/chat/chat.interrupt.classifier.js +1 -3
  12. package/dist/chat/chat.interrupt.classifier.js.map +1 -1
  13. package/dist/chat/chat.suggester.js.map +1 -1
  14. package/dist/contact/contact.tools.d.ts.map +1 -1
  15. package/dist/contact/contact.tools.js +17 -5
  16. package/dist/contact/contact.tools.js.map +1 -1
  17. package/dist/context/context.generator.d.ts +2 -0
  18. package/dist/context/context.generator.d.ts.map +1 -1
  19. package/dist/context/context.generator.js +8 -6
  20. package/dist/context/context.generator.js.map +1 -1
  21. package/dist/integration/integration.tools.d.ts.map +1 -1
  22. package/dist/integration/integration.tools.js +6 -0
  23. package/dist/integration/integration.tools.js.map +1 -1
  24. package/dist/intent/intent.clarifier.d.ts +2 -0
  25. package/dist/intent/intent.clarifier.d.ts.map +1 -1
  26. package/dist/intent/intent.clarifier.js +9 -23
  27. package/dist/intent/intent.clarifier.js.map +1 -1
  28. package/dist/intent/intent.graph.d.ts.map +1 -1
  29. package/dist/intent/intent.graph.js +29 -26
  30. package/dist/intent/intent.graph.js.map +1 -1
  31. package/dist/intent/intent.tools.d.ts.map +1 -1
  32. package/dist/intent/intent.tools.js +18 -48
  33. package/dist/intent/intent.tools.js.map +1 -1
  34. package/dist/maintenance/maintenance.graph.d.ts.map +1 -1
  35. package/dist/maintenance/maintenance.graph.js +1 -2
  36. package/dist/maintenance/maintenance.graph.js.map +1 -1
  37. package/dist/mcp/mcp.server.d.ts.map +1 -1
  38. package/dist/mcp/mcp.server.js +2 -4
  39. package/dist/mcp/mcp.server.js.map +1 -1
  40. package/dist/negotiation/negotiation.graph.d.ts.map +1 -1
  41. package/dist/negotiation/negotiation.graph.js +13 -20
  42. package/dist/negotiation/negotiation.graph.js.map +1 -1
  43. package/dist/negotiation/negotiation.tools.d.ts.map +1 -1
  44. package/dist/negotiation/negotiation.tools.js +12 -12
  45. package/dist/negotiation/negotiation.tools.js.map +1 -1
  46. package/dist/network/indexer/indexer.graph.d.ts +9 -9
  47. package/dist/network/indexer/indexer.graph.d.ts.map +1 -1
  48. package/dist/network/indexer/indexer.graph.js.map +1 -1
  49. package/dist/network/network.graph.d.ts.map +1 -1
  50. package/dist/network/network.graph.js +19 -25
  51. package/dist/network/network.graph.js.map +1 -1
  52. package/dist/opportunity/feed/feed.categorizer.d.ts.map +1 -1
  53. package/dist/opportunity/feed/feed.categorizer.js +15 -20
  54. package/dist/opportunity/feed/feed.categorizer.js.map +1 -1
  55. package/dist/opportunity/feed/feed.graph.d.ts.map +1 -1
  56. package/dist/opportunity/feed/feed.graph.js +8 -10
  57. package/dist/opportunity/feed/feed.graph.js.map +1 -1
  58. package/dist/opportunity/opportunity.introducer.d.ts.map +1 -1
  59. package/dist/opportunity/opportunity.introducer.js +1 -2
  60. package/dist/opportunity/opportunity.introducer.js.map +1 -1
  61. package/dist/opportunity/opportunity.tools.d.ts.map +1 -1
  62. package/dist/opportunity/opportunity.tools.js +3 -2
  63. package/dist/opportunity/opportunity.tools.js.map +1 -1
  64. package/dist/profile/profile.enricher.d.ts +5 -7
  65. package/dist/profile/profile.enricher.d.ts.map +1 -1
  66. package/dist/profile/profile.enricher.js +8 -10
  67. package/dist/profile/profile.enricher.js.map +1 -1
  68. package/dist/profile/profile.generator.d.ts.map +1 -1
  69. package/dist/profile/profile.generator.js +1 -2
  70. package/dist/profile/profile.generator.js.map +1 -1
  71. package/dist/profile/profile.tools.js +1 -1
  72. package/dist/profile/profile.tools.js.map +1 -1
  73. package/dist/questioner/questioner.presets.d.ts.map +1 -1
  74. package/dist/questioner/questioner.presets.js +24 -38
  75. package/dist/questioner/questioner.presets.js.map +1 -1
  76. package/dist/shared/agent/tool.factory.d.ts.map +1 -1
  77. package/dist/shared/agent/tool.factory.js +2 -2
  78. package/dist/shared/agent/tool.factory.js.map +1 -1
  79. package/dist/shared/agent/tool.helpers.d.ts +14 -0
  80. package/dist/shared/agent/tool.helpers.d.ts.map +1 -1
  81. package/dist/shared/agent/tool.helpers.js.map +1 -1
  82. package/dist/shared/agent/tool.runtime.d.ts.map +1 -1
  83. package/dist/shared/agent/tool.runtime.js +20 -13
  84. package/dist/shared/agent/tool.runtime.js.map +1 -1
  85. package/dist/shared/hyde/hyde.graph.d.ts.map +1 -1
  86. package/dist/shared/hyde/hyde.graph.js +3 -2
  87. package/dist/shared/hyde/hyde.graph.js.map +1 -1
  88. package/dist/shared/hyde/hyde.strategies.d.ts +2 -1
  89. package/dist/shared/hyde/hyde.strategies.d.ts.map +1 -1
  90. package/dist/shared/hyde/hyde.strategies.js.map +1 -1
  91. package/dist/shared/observability/trace.d.ts +3 -3
  92. package/dist/shared/observability/trace.d.ts.map +1 -1
  93. package/dist/shared/observability/trace.js +19 -33
  94. package/dist/shared/observability/trace.js.map +1 -1
  95. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"mcp.server.js","sourceRoot":"/","sources":["mcp/mcp.server.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAMzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAErE,OAAO,EAAE,cAAc,EAAE,MAAM,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AAO,EAAE,IAAI;iBACd,CAAC;YACJ,CAAC;QACH,CAAC,CACF,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,OAAO,CAAC,2BAA2B,QAAQ,CAAC,IAAI,QAAQ,CAAC,CAAC;IACjE,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["/**\n * MCP Server Factory — creates an McpServer instance with all protocol tools\n * registered from the existing tool registry. Each tool invocation resolves\n * auth from the HTTP request, builds a ResolvedToolContext, and delegates\n * to the raw tool handler.\n */\n\nimport { z } from 'zod';\nimport { McpServer, fromJsonSchema } from '@modelcontextprotocol/server';\nimport type { ServerContext, JsonSchemaType } from '@modelcontextprotocol/server';\n\nimport type { McpAuthResolver } from '../shared/interfaces/auth.interface.js';\nimport type { McpAuthInput } from '../shared/schemas/mcp-auth.schema.js';\nimport type { ToolDeps, ResolvedToolContext } from '../shared/agent/tool.helpers.js';\nimport { resolveChatContext } from '../shared/agent/tool.helpers.js';\nimport type { Question } from '../shared/schemas/question.schema.js';\nimport { QuestionSchema } from '../shared/schemas/question.schema.js';\nimport { dispatchElicitations } from './elicitation.dispatcher.js';\nimport { createToolRegistry } from '../shared/agent/tool.registry.js';\nimport { ToolRuntimeError, invokeToolRuntime, toolRuntimeErrorToResult } from '../shared/agent/tool.runtime.js';\nimport type { TraceEmitter } from '../shared/observability/request-context.js';\nimport { protocolLogger } from '../shared/observability/protocol.logger.js';\n\nconst logger = protocolLogger('McpServer');\n\nfunction isExpectedMcpAuthError(message: string): boolean {\n return message.includes('Authentication required') ||\n message.includes('Invalid API key') ||\n message.includes('Invalid or expired access token') ||\n message.includes('JWT payload missing user ID');\n}\n\n/**\n * Runtime/auth failures are converted into structured MCP `isError` tool\n * results for the caller. Reporting them as application exceptions produces\n * Sentry noise for expected client failures and policy-enforced timeouts.\n */\nexport function shouldReportMcpToolError(err: unknown): boolean {\n if (err instanceof ToolRuntimeError) return false;\n const message = err instanceof Error ? err.message : String(err);\n return !isExpectedMcpAuthError(message);\n}\n\n// ═══════════════════════════════════════════════════════════════════════════════\n// ZOD 3 → JSON SCHEMA CONVERSION\n// ═══════════════════════════════════════════════════════════════════════════════\n\n/**\n * Minimal Zod-to-JSON-Schema conversion for MCP tool registration.\n * Converts Zod 3.x schemas to plain JSON Schema objects that can be\n * wrapped with `fromJsonSchema()` for MCP SDK compatibility.\n */\nfunction zodToJsonSchema(schema: z.ZodType): Record<string, unknown> {\n if (schema instanceof z.ZodObject) {\n const shape = schema.shape;\n const properties: Record<string, unknown> = {};\n const required: string[] = [];\n for (const [key, value] of Object.entries(shape)) {\n const zodValue = value as z.ZodType;\n properties[key] = zodToJsonSchema(zodValue);\n if (!(zodValue instanceof z.ZodOptional) && !(zodValue instanceof z.ZodDefault)) {\n required.push(key);\n }\n }\n return { type: 'object', properties, ...(required.length ? { required } : {}) };\n }\n if (schema instanceof z.ZodString) {\n const result: Record<string, unknown> = { type: 'string' };\n // Detect .url(), .email(), .uuid() etc. via Zod's internal checks array\n const checks = (schema as z.ZodString & { _def: { checks: Array<{ kind: string }> } })._def?.checks;\n if (checks) {\n for (const check of checks) {\n if (check.kind === 'url') result.format = 'uri';\n else if (check.kind === 'email') result.format = 'email';\n else if (check.kind === 'uuid') result.format = 'uuid';\n else if (check.kind === 'datetime') result.format = 'date-time';\n }\n }\n return result;\n }\n if (schema instanceof z.ZodNumber) {\n const checks = (schema as z.ZodNumber & { _def: { checks: Array<{ kind: string; value?: number }> } })._def?.checks;\n const result: Record<string, unknown> = { type: 'number' };\n if (checks) {\n for (const check of checks) {\n if (check.kind === 'int') result.type = 'integer';\n else if (check.kind === 'min') result.minimum = check.value;\n else if (check.kind === 'max') result.maximum = check.value;\n }\n }\n return result;\n }\n if (schema instanceof z.ZodBoolean) return { type: 'boolean' };\n if (schema instanceof z.ZodArray) {\n return { type: 'array', items: zodToJsonSchema((schema as z.ZodArray<z.ZodType>).element) };\n }\n if (schema instanceof z.ZodOptional) {\n return zodToJsonSchema((schema as z.ZodOptional<z.ZodType>).unwrap());\n }\n if (schema instanceof z.ZodDefault) {\n return zodToJsonSchema((schema as z.ZodDefault<z.ZodType>).removeDefault());\n }\n if (schema instanceof z.ZodEnum) {\n return { type: 'string', enum: (schema as z.ZodEnum<[string, ...string[]]>).options };\n }\n if (schema instanceof z.ZodNullable) {\n const inner = zodToJsonSchema((schema as z.ZodNullable<z.ZodType>).unwrap());\n return { ...inner, nullable: true };\n }\n if (schema instanceof z.ZodRecord) {\n return { type: 'object', additionalProperties: true };\n }\n return { type: 'object' };\n}\n\n// ═══════════════════════════════════════════════════════════════════════════════\n// RESULT POST-PROCESSING\n// ═══════════════════════════════════════════════════════════════════════════════\n\n/**\n * Strips internal `_`-prefixed keys from `data` and promotes `isError`\n * from the inner `success: false` signal to the MCP envelope level.\n * Fail-open: if JSON parsing throws, returns the original text with isError: false.\n */\nexport function sanitizeMcpResult(text: string): { text: string; isError: boolean } {\n try {\n const parsed = JSON.parse(text);\n if (\n parsed &&\n typeof parsed === 'object' &&\n parsed.data &&\n typeof parsed.data === 'object' &&\n !Array.isArray(parsed.data)\n ) {\n for (const key of Object.keys(parsed.data)) {\n if (key.startsWith('_') || key === 'debugSteps') {\n delete parsed.data[key];\n }\n }\n }\n const isError = parsed?.success === false;\n return { text: JSON.stringify(parsed), isError };\n } catch {\n return { text, isError: false };\n }\n}\n\n/** Spec cap on the number of decision questions surfaced per turn. */\nconst MAX_DECISION_QUESTIONS = 3;\n\n/**\n * Extracts decision questions from a parsed tool-result text, if present.\n * Validates each entry against `QuestionSchema` and drops malformed items;\n * caps the array at `MAX_DECISION_QUESTIONS` (defense-in-depth — Slice 2's\n * generator already caps at 3, but we don't trust the cast here).\n *\n * Returns null when the text isn't JSON, has no `data.questions`, or\n * contains zero valid questions after validation.\n */\nexport function extractDecisionQuestions(text: string): Question[] | null {\n let parsed: unknown;\n try {\n parsed = JSON.parse(text);\n } catch {\n return null;\n }\n\n const rawQs = (parsed as { data?: { questions?: unknown } } | null)?.data?.questions;\n if (!Array.isArray(rawQs) || rawQs.length === 0) return null;\n\n const valid: Question[] = [];\n for (const raw of rawQs) {\n const result = QuestionSchema.safeParse(raw);\n if (result.success) valid.push(result.data);\n if (valid.length === MAX_DECISION_QUESTIONS) break;\n }\n return valid.length > 0 ? valid : null;\n}\n\n/**\n * Renders the JSON-envelope text block appended to the tool result content\n * when decision questions are present. The leading sentinel string lets the\n * LLM client recognize and surface the questions in prose for clients\n * without elicitation support.\n */\nexport function renderQuestionsEnvelope(questions: Question[]): string {\n return `Decision questions (structured): ${JSON.stringify({ questions })}`;\n}\n\n// ═══════════════════════════════════════════════════════════════════════════════\n// MCP SERVER FACTORY\n// ═══════════════════════════════════════════════════════════════════════════════\n\n/**\n * Factory for creating per-request scoped database instances.\n * Injected from the controller/handler layer to keep the protocol layer\n * free of direct adapter imports.\n */\nexport interface ScopedDepsFactory {\n /** Creates scoped userDb and systemDb for the given user and index scope. */\n create(userId: string, indexScope: string[]): Pick<ToolDeps, 'userDb' | 'systemDb'>;\n}\n\n/**\n * Computes the index scope passed to the per-request scoped DB factory. When\n * `networkScopeId` is non-null, the agent is bound to a single network and\n * may only reach that network plus the user's personal index. Otherwise the\n * full set of the user's network memberships is returned.\n */\nexport const computeAgentIndexScope = (\n userNetworks: { networkId: string; isPersonal?: boolean | null }[],\n networkScopeId: string | null | undefined,\n): string[] => {\n if (!networkScopeId) {\n return userNetworks.map((m) => m.networkId);\n }\n return userNetworks\n .filter((m) => m.networkId === networkScopeId || m.isPersonal === true)\n .map((m) => m.networkId);\n};\n\n/**\n * Promotes a network-scoped agent's bound network into the resolved tool\n * context as the implicit chat scope. Every tool that branches on\n * `context.networkId` (read_networks, read_intents, read_user_profiles,\n * opportunity tools, etc.) then enforces scope automatically — without this\n * step the DB-level `indexScope` clamp guards cross-user data but tools that\n * shape their response off `context.networkId` (notably `read_networks`'\n * `publicNetworks` branch) would still leak the global view.\n *\n * No-op when there is no scope, or when an explicit chat scope is already\n * set (a user-driven index-scoped chat must keep precedence over the agent\n * binding — which would be a strict subset anyway, since the API key cannot\n * reach beyond its bound network).\n */\nexport const applyNetworkScopeToContext = (\n context: ResolvedToolContext,\n networkScopeId: string | null | undefined,\n): void => {\n if (!networkScopeId) return;\n if (context.networkId) return;\n\n context.networkId = networkScopeId;\n // Clamp indexScope to [boundNetwork, personalIndex] BEFORE the membership\n // check below. If the bound network is not in userNetworks (defensive case),\n // the filter still produces a safe scope (personal index only) rather than\n // leaving the unclamped scope set by resolveChatContext.\n context.indexScope = context.userNetworks\n .filter((m) => m.networkId === networkScopeId || m.isPersonal === true)\n .map((m) => m.networkId);\n\n const bound = context.userNetworks.find((m) => m.networkId === networkScopeId);\n if (!bound) return;\n\n context.indexName = bound.networkTitle;\n context.scopedIndex = {\n id: bound.networkId,\n title: bound.networkTitle,\n prompt: bound.indexPrompt ?? null,\n };\n const isOwner = bound.permissions?.includes('owner') ?? false;\n context.scopedMembershipRole = isOwner ? 'owner' : 'member';\n context.isOwner = isOwner;\n};\n\n/**\n * Tools allowed during onboarding — everything else is gated until\n * complete_onboarding is called. Includes the agent-gate-exempt tools\n * (register_agent, read_docs, scrape_url) because they are informational /\n * registration primitives needed at every lifecycle stage.\n */\nexport const ONBOARDING_ALLOWED: ReadonlySet<string> = new Set([\n 'register_agent',\n 'read_docs',\n 'scrape_url',\n 'record_onboarding_privacy_consent',\n 'preview_user_profile',\n 'get_profile_run',\n 'cancel_profile_run',\n 'confirm_user_profile',\n 'create_user_profile',\n 'complete_onboarding',\n 'import_gmail_contacts',\n 'read_networks',\n 'create_network_membership',\n 'create_intent',\n 'read_user_profiles',\n]);\n\n/**\n * Builds the onboarding gate message for MCP callers. Condensed from the\n * chat orchestrator's 8-step flow (chat.prompt.ts buildOnboarding) into a\n * 7-step tool-error guide suited for non-interactive MCP clients.\n */\nexport function buildMcpOnboardingMessage(ctx: ResolvedToolContext): string {\n const nameStep = ctx.hasName\n ? `1. Greet the user and confirm their name (\"You're ${ctx.userName}, right?\").`\n : `1. Ask the user for their name and a short self-description.`;\n\n const communityStep = ctx.networkId\n ? `5. (Skipped — user is already in \"${ctx.indexName ?? 'their community'}\".)`\n : `5. Call read_networks() and let the user pick communities to join via create_network_membership(networkId=...).`;\n\n const allowedList = Array.from(ONBOARDING_ALLOWED).join(', ');\n\n return (\n `This user has not completed onboarding. You must guide them through setup before they can use other tools. ` +\n `Only the following tools are available until onboarding is complete: ` +\n `${allowedList}.\\n\\n` +\n `Onboarding flow:\\n` +\n `${nameStep}\\n` +\n `2. Ask whether the user allows use of event/EdgeOS profile data, then call record_onboarding_privacy_consent(edgeosImportGranted=...).\\n` +\n `3. Ask separately whether the user allows public internet/profile lookup, then call record_onboarding_privacy_consent(publicProfileLookupGranted=...).\\n` +\n `4. Call preview_user_profile(...) using only allowed inputs; do not run public lookup unless consent was granted. If it returns profileRunId, poll get_profile_run(profileRunId=...) until status is succeeded, then use its result as the draft.\\n` +\n `5. Present the profile draft and ask \"Does that look right?\" On approval/correction, call confirm_user_profile(...).\\n` +\n `${communityStep}\\n` +\n `6. Ask what the user is looking for and call create_intent(description=\"...\", autoApprove=true) so the first signal is persisted.\\n` +\n `7. Call complete_onboarding() to finish setup. Gmail/contact import and discovery are optional after onboarding, never mandatory.`\n );\n}\n\n/**\n * Creates an MCP server with all protocol tools registered.\n * Tools resolve auth per-request via the HTTP request available in ServerContext.\n *\n * @param deps - Shared tool dependencies (graphs, database, embedder, etc.)\n * @param authResolver - Resolves authenticated identity from the HTTP request\n * @param scopedDepsFactory - Factory for creating per-request scoped databases\n * @returns A configured McpServer ready to be connected to a transport\n */\nfunction createMcpTraceEmitter(toolName: string, ctx: ServerContext): TraceEmitter | undefined {\n const token = ctx.mcpReq._meta?.progressToken;\n if (typeof token !== 'string' && typeof token !== 'number') return undefined;\n\n let progress = 0;\n return (event) => {\n progress += 1;\n const message = (() => {\n if (event.type === 'graph_start') return `${toolName}: ${event.name} started`;\n if (event.type === 'graph_end') return `${toolName}: ${event.name} finished${event.durationMs != null ? ` in ${event.durationMs}ms` : ''}`;\n if (event.type === 'agent_start') return `${toolName}: ${event.name} agent started`;\n if (event.type === 'agent_end') return `${toolName}: ${event.name} agent finished${event.durationMs != null ? ` in ${event.durationMs}ms` : ''}`;\n if (event.type === 'opportunity_draft_ready') return `${toolName}: opportunity draft ready`;\n return `${toolName}: progress`;\n })();\n\n const notification: Parameters<ServerContext['mcpReq']['notify']>[0] = {\n method: 'notifications/progress',\n params: { progressToken: token, progress, message },\n };\n void ctx.mcpReq.notify(notification).catch((err) => {\n logger.debug('Failed to send MCP progress notification', {\n toolName,\n error: err instanceof Error ? err.message : String(err),\n });\n });\n };\n}\n\nexport const MCP_INSTRUCTIONS = `\nIndex Network is a private, intent-driven discovery protocol. You help users find the right people and help the right people find them, via Index Network MCP tools.\n\n# Voice\nCalm, direct, analytical, concise. Preferred vocabulary: opportunity, overlap, signal, pattern, emerging, relevant, adjacency.\n\n# Banned vocabulary\nNEVER use \"search\" in any form. Use \"looking up\" for indexed data, \"find\" / \"look for\" for discovery, \"check\" for verification, \"discover\" for exploration. Banned: leverage, unlock, optimize, scale, disrupt, revolutionary, AI-powered, maximize value, act fast, networking, match.\n\n# Entity model\n- User — has one Profile, many Memberships, many Intents.\n- Profile — identity (bio, skills, interests, location).\n- Index — community with title, prompt (purpose), join policy. Has Members.\n- Membership — User↔Index junction. \\`isPersonal: true\\` marks the user's personal index (contacts).\n- Intent — what a user is looking for (signal). Description, summary, embedding.\n- IntentIndex — Intent↔Index junction (auto-assigned).\n- Opportunity — discovered connection between users. Roles, status, reasoning.\n\n# Output rules\n- NEVER expose internal IDs, UUIDs, field names, or tool names — EXCEPT when an ID is actionable for the user (e.g. a \\`conversationId\\` they need to open a chat). Surface such IDs verbatim when the tool returns them.\n- NEVER use internal vocabulary — say \"signal\" not \"intent\", \"community\" not \"index\".\n- NEVER dump raw JSON. Synthesize in natural language.\n- Surface top 1–3 relevant points unless asked for the full list.\n- Prefer first names; use full names only to disambiguate.\n- Translate statuses: draft/latent → \"draft\", pending → \"sent\", accepted → \"connected\".\n- NEVER fabricate data. If you don't have it, call the appropriate tool.\n\n# Tool guidance\nEach tool's description contains its own usage rules (when to call, when NOT to call, required prerequisites, post-call follow-ups). Read the description of every tool you call — that is where the per-tool workflow patterns live.\n\n# Authentication\nPass your API key in the \\`x-api-key\\` request header (not \\`Authorization: Bearer\\`).\n\n# Opportunity lifecycle\nOpportunities move through: draft → pending → accepted (or rejected).\n\n- **draft** (you created it, not yet sent): offer to send it; confirm before calling update_opportunity with pending.\n- **pending, you sent it**: waiting for the other side — nothing to do.\n- **pending, you received it**: the other person is waiting for your response. Surface it to the user and ask if they want to start a chat. Only call update_opportunity with accepted after explicit user confirmation.\n- **accepted**: both sides are connected — a direct conversation exists. Surface the conversationId to the user if available.\n\nNever accept a received opportunity without explicit user approval in the current conversation.\n\n# Decision questions after discovery\n\nAfter \\`discover_opportunities\\`, the tool result may include a second text block starting with \\`Decision questions (structured): ...\\`. This means the discovery engine ran negotiations but needs human input to sharpen the next turn — e.g. clarify timing, role, stage, or location.\n\n**When this block is present:**\n1. Parse the \\`questions\\` array from the JSON after the sentinel.\n2. Each question has \\`title\\` (decision domain, ≤12 chars), \\`prompt\\` (ends in \\`?\\`), \\`options\\` (2–4 items, each with \\`label\\` and \\`description\\`), and \\`multiSelect\\`. The safest option is labeled \\`... (Recommended)\\`.\n3. Present each question in natural language: ask the \\`prompt\\`, list options as \\`**{label}** — {description}\\`. Never expose the JSON or technical field names.\n4. Wait for the user's answer, then fold it into the next \\`discover_opportunities(searchQuery=...)\\` call.\n\n**Elicitation-capable clients** (those that declared \\`elicitation\\` support in \\`initialize\\`): the server dispatches \\`elicitation/create\\` requests directly — answers are written back to the chat session automatically. You will not see the envelope as a follow-up task in that case.\n`.trim();\n\n/**\n * Extracts a Bearer token from an HTTP Authorization header.\n */\nexport function extractBearerToken(req: Request): string | undefined {\n const authHeader = req.headers.get('Authorization');\n if (!authHeader) return undefined;\n const [scheme, token] = authHeader.trim().split(/\\s+/, 2);\n if (scheme?.toLowerCase() === 'bearer' && token) return token;\n return undefined;\n}\n\n/**\n * Normalizes the x-index-surface header to a typed surface value.\n * Unknown or absent values collapse to 'web'.\n */\nlet hasWarnedInvalidSurface = false;\nexport function parseClientSurface(raw: string | null): 'telegram' | 'web' {\n if (raw === null || raw === '') return 'web';\n const trimmed = raw.trim().toLowerCase();\n if (trimmed === '') return 'web';\n if (trimmed === 'telegram') return 'telegram';\n if (trimmed === 'web') return 'web';\n if (!hasWarnedInvalidSurface) {\n hasWarnedInvalidSurface = true;\n logger.warn('Unknown x-index-surface value (collapsing to web; warning once per process)');\n }\n return 'web';\n}\n\nexport function createMcpServer(\n deps: ToolDeps,\n authResolver: McpAuthResolver,\n scopedDepsFactory: ScopedDepsFactory,\n): McpServer {\n // Tools exempt from the agent-registration gate — available before registration is complete.\n const AGENT_GATE_EXEMPT = new Set(['register_agent', 'read_docs', 'scrape_url']);\n\n const server = new McpServer(\n { name: 'index-network', version: '1.0.0' },\n { instructions: MCP_INSTRUCTIONS },\n );\n\n const registry = createToolRegistry(deps);\n\n for (const [toolName, toolDef] of registry) {\n // Convert Zod 3 schema to JSON Schema, then wrap with fromJsonSchema\n // for MCP SDK's StandardSchemaWithJSON compatibility\n const jsonSchema = zodToJsonSchema(toolDef.schema) as JsonSchemaType;\n const mcpSchema = fromJsonSchema(jsonSchema);\n\n server.registerTool(\n toolName,\n {\n description: toolDef.description,\n inputSchema: mcpSchema,\n },\n async (args: unknown, ctx: ServerContext) => {\n let reportDeps = deps;\n let reportUserId: string | undefined;\n let reportContext: ResolvedToolContext | undefined;\n\n try {\n // Extract the original HTTP request from the MCP server context\n const httpReq = ctx.http?.req;\n if (!httpReq) {\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ error: 'No HTTP request available in MCP context' }) }],\n isError: true,\n };\n }\n\n // Extract transport-neutral auth input DTO from the HTTP request\n const mcpAuthInput: McpAuthInput = {\n bearerToken: extractBearerToken(httpReq),\n apiKey: httpReq.headers.get('x-api-key') ?? undefined,\n clientSurface: parseClientSurface(httpReq.headers.get('x-index-surface')),\n telegramHandle: httpReq.headers.get('x-index-telegram-handle') ?? undefined,\n telegramUsername: httpReq.headers.get('x-index-telegram-username') ?? undefined,\n };\n\n // Resolve authenticated identity from the auth input DTO\n const { userId, agentId, isSessionAuth, networkScopeId, clientSurface } = await authResolver.resolveIdentity(mcpAuthInput);\n reportUserId = userId;\n\n // Per-principal MCP throttle. Runs BEFORE any DB work so a throttled\n // call short-circuits cheaply. The /mcp transport bypasses the\n // controller-level RateLimit guard, so this is the only volume cap on\n // tool calls — it stops an over-eager agent from cascading itself into\n // provider rate limits.\n if (deps.mcpRateLimiter) {\n // Throttling is best-effort: never let a limiter failure (or a host\n // implementation that throws instead of failing open) break tool\n // dispatch. Treat any error as \"allowed\".\n let decision: Awaited<ReturnType<NonNullable<typeof deps.mcpRateLimiter>>> | null = null;\n try {\n decision = await deps.mcpRateLimiter({\n userId,\n ...(agentId ? { agentId } : {}),\n toolName,\n });\n } catch (rlErr) {\n logger.warn(`MCP rate limiter threw for \"${toolName}\" — failing open`, {\n error: rlErr instanceof Error ? rlErr.message : String(rlErr),\n });\n }\n if (decision && !decision.allowed) {\n const retryAfterSec = decision.retryAfterSec ?? 60;\n return {\n content: [{\n type: 'text' as const,\n text: JSON.stringify({\n error: 'Rate limit exceeded',\n message:\n `Too many ${toolName} calls in a short period. Wait ${retryAfterSec}s before retrying.` +\n (toolName === 'discover_opportunities'\n ? ` If a discovery run is in progress, poll get_discovery_run instead of calling discover_opportunities again.`\n : ''),\n retryAfterSec,\n }),\n }],\n isError: true,\n };\n }\n }\n\n // Resolve chat context for the user (mark as MCP — no interactive UI available)\n const context = await resolveChatContext({ database: deps.database, userId });\n reportContext = context;\n context.isMcp = true;\n if (agentId) {\n context.agentId = agentId;\n }\n if (clientSurface) {\n context.clientSurface = clientSurface;\n }\n\n // Network-scoped agents inherit their bound network as the implicit chat\n // scope. Every tool that branches on `context.networkId` then enforces\n // the same boundary the DB-level `indexScope` clamp enforces below —\n // most importantly `read_networks`, which would otherwise return the\n // global `publicNetworks` catalog for unscoped contexts.\n applyNetworkScopeToContext(context, networkScopeId);\n\n // Gate: API-key callers (background agents) must register before using most tools.\n // OAuth/JWT session callers (human MCP clients such as Claude Code) are exempt —\n // their identity is already established via the auth flow and they have no agent entity.\n if (!isSessionAuth && !context.agentId && !AGENT_GATE_EXEMPT.has(toolName)) {\n return {\n content: [{\n type: 'text' as const,\n text: JSON.stringify({\n error: 'Agent not registered',\n message:\n 'You must register as an agent before using Index tools. ' +\n 'Call register_agent with your agent name to establish an identity. ' +\n 'The tools register_agent, read_docs, and scrape_url are available without registration.',\n }),\n }],\n isError: true,\n };\n }\n\n // Gate: non-onboarded users can only use onboarding-related tools.\n // Mirrors the chat orchestrator's ONBOARDING MODE — the MCP client must\n // walk the user through profile creation, Gmail connect, intent capture,\n // and complete_onboarding() before full tool access is granted.\n if (context.isOnboarding && !ONBOARDING_ALLOWED.has(toolName)) {\n const onboardingSteps = buildMcpOnboardingMessage(context);\n return {\n content: [{\n type: 'text' as const,\n text: JSON.stringify({\n error: 'Onboarding required',\n message: onboardingSteps,\n }),\n }],\n isError: true,\n };\n }\n\n // Build per-request scoped databases via injected factory.\n // Network-scoped agents are clamped to their bound network plus the user's\n // personal index — they cannot reach other networks even when the user is\n // a member of them. The personal-index reachability is preserved so the\n // agent can still manage its owner's profile and contacts.\n // context.indexScope is now the single source of truth: set by\n // resolveChatContext (full set) and narrowed by applyNetworkScopeToContext.\n const scopedDbs = scopedDepsFactory.create(userId, context.indexScope);\n\n // Override deps with per-request scoped databases\n const requestDeps: ToolDeps = { ...deps, ...scopedDbs };\n reportDeps = requestDeps;\n\n // Re-create registry with per-request deps for scoped database access\n const requestRegistry = createToolRegistry(requestDeps);\n const requestTool = requestRegistry.get(toolName);\n\n if (!requestTool) {\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ error: `Tool \"${toolName}\" not found` }) }],\n isError: true,\n };\n }\n\n // Validate input against the original Zod schema\n const parseResult = (toolDef.schema as z.ZodType).safeParse(args);\n if (!parseResult.success) {\n const issues = parseResult.error.issues.map((i) => `${i.path.join('.')}: ${i.message}`).join('; ');\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ success: false, error: `Invalid input: ${issues}` }) }],\n isError: true,\n };\n }\n const validatedArgs = parseResult.data;\n\n // Execute the tool handler through the shared runtime so MCP calls have\n // consistent timeout, cancellation, progress, and requestContext plumbing.\n const result = await invokeToolRuntime({\n toolName,\n tool: requestTool,\n context,\n query: validatedArgs,\n signal: ctx.mcpReq.signal,\n traceEmitter: createMcpTraceEmitter(toolName, ctx),\n });\n\n const { text: sanitizedText, isError: toolIsError } = sanitizeMcpResult(result);\n\n // Slice 5: decision questions post-processing for discover_opportunities only.\n if (toolName === \"discover_opportunities\" && !toolIsError) {\n const questions = extractDecisionQuestions(sanitizedText);\n if (questions) {\n const envelopeBlock = {\n type: \"text\" as const,\n text: renderQuestionsEnvelope(questions),\n };\n\n const supportsElicitation =\n !!server.server.getClientCapabilities()?.elicitation;\n\n // Capture into a local const so TS preserves the narrowing\n // inside the callback below. Optional chains don't survive\n // across closure boundaries under strict mode.\n const elicitInput = ctx.mcpReq?.elicitInput;\n\n if (supportsElicitation && elicitInput) {\n // Sequential — never parallel (day-one rule). We await the loop\n // before returning the tool result so test harnesses can observe\n // the dispatched calls deterministically.\n await dispatchElicitations({\n userId,\n questions,\n elicitInput: (params) => elicitInput(params),\n chatMessageWriter: deps.chatMessageWriter,\n });\n }\n\n return {\n content: [\n { type: \"text\" as const, text: sanitizedText },\n envelopeBlock,\n ],\n ...(toolIsError ? { isError: true } : {}),\n };\n }\n }\n\n return {\n content: [{ type: 'text' as const, text: sanitizedText }],\n ...(toolIsError ? { isError: true } : {}),\n };\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err);\n logger.error(`MCP tool \"${toolName}\" failed`, { error: message });\n if (shouldReportMcpToolError(err)) {\n reportDeps.reportToolError?.(err, {\n subsystem: 'mcp',\n operation: 'mcp.tool',\n toolName,\n userId: reportUserId,\n tags: {\n transport: 'mcp',\n toolName,\n },\n context: {\n agentId: reportContext?.agentId,\n networkId: reportContext?.networkId,\n indexScope: reportContext?.indexScope,\n },\n });\n }\n const runtimeResult = toolRuntimeErrorToResult(err);\n return {\n content: [{ type: 'text' as const, text: runtimeResult ?? JSON.stringify({ error: message }) }],\n isError: true,\n };\n }\n },\n );\n }\n\n logger.verbose(`MCP server created with ${registry.size} tools`);\n return server;\n}\n"]}
1
+ {"version":3,"file":"mcp.server.js","sourceRoot":"/","sources":["mcp/mcp.server.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAMzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAErE,OAAO,EAAE,cAAc,EAAE,MAAM,sCAAsC,CAAC;AACtE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AACtE,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAEhH,OAAO,EAAE,cAAc,EAAE,MAAM,4CAA4C,CAAC;AAE5E,MAAM,MAAM,GAAG,cAAc,CAAC,WAAW,CAAC,CAAC;AAE3C,SAAS,sBAAsB,CAAC,OAAe;IAC7C,OAAO,OAAO,CAAC,QAAQ,CAAC,yBAAyB,CAAC;QAChD,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAC;QACnC,OAAO,CAAC,QAAQ,CAAC,iCAAiC,CAAC;QACnD,OAAO,CAAC,QAAQ,CAAC,6BAA6B,CAAC,CAAC;AACpD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CAAC,GAAY;IACnD,IAAI,GAAG,YAAY,gBA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J,SAAS,EAAE,KAAK;4BAChB,QAAQ;yBACT;wBACD,OAAO,EAAE;4BACP,OAAO,EAAE,aAAa,EAAE,OAAO;4BAC/B,SAAS,EAAE,aAAa,EAAE,SAAS;4BACnC,UAAU,EAAE,aAAa,EAAE,UAAU;yBACtC;qBACF,CAAC,CAAC;gBACL,CAAC;gBACD,MAAM,aAAa,GAAG,wBAAwB,CAAC,GAAG,CAAC,CAAC;gBACpD,OAAO;oBACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,aAAa,IAAI,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;oBAC/F,OAAO,EAAE,IAAI;iBACd,CAAC;YACJ,CAAC;QACH,CAAC,CACF,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,OAAO,CAAC,2BAA2B,QAAQ,CAAC,IAAI,QAAQ,CAAC,CAAC;IACjE,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["/**\n * MCP Server Factory — creates an McpServer instance with all protocol tools\n * registered from the existing tool registry. Each tool invocation resolves\n * auth from the HTTP request, builds a ResolvedToolContext, and delegates\n * to the raw tool handler.\n */\n\nimport { z } from 'zod';\nimport { McpServer, fromJsonSchema } from '@modelcontextprotocol/server';\nimport type { ServerContext, JsonSchemaType } from '@modelcontextprotocol/server';\n\nimport type { McpAuthResolver } from '../shared/interfaces/auth.interface.js';\nimport type { McpAuthInput } from '../shared/schemas/mcp-auth.schema.js';\nimport type { ToolDeps, ResolvedToolContext } from '../shared/agent/tool.helpers.js';\nimport { resolveChatContext } from '../shared/agent/tool.helpers.js';\nimport type { Question } from '../shared/schemas/question.schema.js';\nimport { QuestionSchema } from '../shared/schemas/question.schema.js';\nimport { dispatchElicitations } from './elicitation.dispatcher.js';\nimport { createToolRegistry } from '../shared/agent/tool.registry.js';\nimport { ToolRuntimeError, invokeToolRuntime, toolRuntimeErrorToResult } from '../shared/agent/tool.runtime.js';\nimport type { TraceEmitter } from '../shared/observability/request-context.js';\nimport { protocolLogger } from '../shared/observability/protocol.logger.js';\n\nconst logger = protocolLogger('McpServer');\n\nfunction isExpectedMcpAuthError(message: string): boolean {\n return message.includes('Authentication required') ||\n message.includes('Invalid API key') ||\n message.includes('Invalid or expired access token') ||\n message.includes('JWT payload missing user ID');\n}\n\n/**\n * Runtime/auth failures are converted into structured MCP `isError` tool\n * results for the caller. Reporting them as application exceptions produces\n * Sentry noise for expected client failures and policy-enforced timeouts.\n */\nexport function shouldReportMcpToolError(err: unknown): boolean {\n if (err instanceof ToolRuntimeError) return false;\n const message = err instanceof Error ? err.message : String(err);\n return !isExpectedMcpAuthError(message);\n}\n\n// ═══════════════════════════════════════════════════════════════════════════════\n// ZOD 3 → JSON SCHEMA CONVERSION\n// ═══════════════════════════════════════════════════════════════════════════════\n\n/**\n * Minimal Zod-to-JSON-Schema conversion for MCP tool registration.\n * Converts Zod 3.x schemas to plain JSON Schema objects that can be\n * wrapped with `fromJsonSchema()` for MCP SDK compatibility.\n */\nfunction zodToJsonSchema(schema: z.ZodType): Record<string, unknown> {\n if (schema instanceof z.ZodObject) {\n const shape = schema.shape;\n const properties: Record<string, unknown> = {};\n const required: string[] = [];\n for (const [key, value] of Object.entries(shape)) {\n const zodValue = value as z.ZodType;\n properties[key] = zodToJsonSchema(zodValue);\n if (!(zodValue instanceof z.ZodOptional) && !(zodValue instanceof z.ZodDefault)) {\n required.push(key);\n }\n }\n return { type: 'object', properties, ...(required.length ? { required } : {}) };\n }\n if (schema instanceof z.ZodString) {\n const result: Record<string, unknown> = { type: 'string' };\n // Detect .url(), .email(), .uuid() etc. via Zod's internal checks array\n const checks = (schema as z.ZodString & { _def: { checks: Array<{ kind: string }> } })._def?.checks;\n if (checks) {\n for (const check of checks) {\n if (check.kind === 'url') result.format = 'uri';\n else if (check.kind === 'email') result.format = 'email';\n else if (check.kind === 'uuid') result.format = 'uuid';\n else if (check.kind === 'datetime') result.format = 'date-time';\n }\n }\n return result;\n }\n if (schema instanceof z.ZodNumber) {\n const checks = (schema as z.ZodNumber & { _def: { checks: Array<{ kind: string; value?: number }> } })._def?.checks;\n const result: Record<string, unknown> = { type: 'number' };\n if (checks) {\n for (const check of checks) {\n if (check.kind === 'int') result.type = 'integer';\n else if (check.kind === 'min') result.minimum = check.value;\n else if (check.kind === 'max') result.maximum = check.value;\n }\n }\n return result;\n }\n if (schema instanceof z.ZodBoolean) return { type: 'boolean' };\n if (schema instanceof z.ZodArray) {\n return { type: 'array', items: zodToJsonSchema((schema as z.ZodArray<z.ZodType>).element) };\n }\n if (schema instanceof z.ZodOptional) {\n return zodToJsonSchema((schema as z.ZodOptional<z.ZodType>).unwrap());\n }\n if (schema instanceof z.ZodDefault) {\n return zodToJsonSchema((schema as z.ZodDefault<z.ZodType>).removeDefault());\n }\n if (schema instanceof z.ZodEnum) {\n return { type: 'string', enum: (schema as z.ZodEnum<[string, ...string[]]>).options };\n }\n if (schema instanceof z.ZodNullable) {\n const inner = zodToJsonSchema((schema as z.ZodNullable<z.ZodType>).unwrap());\n return { ...inner, nullable: true };\n }\n if (schema instanceof z.ZodRecord) {\n return { type: 'object', additionalProperties: true };\n }\n return { type: 'object' };\n}\n\n// ═══════════════════════════════════════════════════════════════════════════════\n// RESULT POST-PROCESSING\n// ═══════════════════════════════════════════════════════════════════════════════\n\n/**\n * Strips internal `_`-prefixed keys from `data` and promotes `isError`\n * from the inner `success: false` signal to the MCP envelope level.\n * Fail-open: if JSON parsing throws, returns the original text with isError: false.\n */\nexport function sanitizeMcpResult(text: string): { text: string; isError: boolean } {\n try {\n const parsed = JSON.parse(text);\n if (\n parsed &&\n typeof parsed === 'object' &&\n parsed.data &&\n typeof parsed.data === 'object' &&\n !Array.isArray(parsed.data)\n ) {\n for (const key of Object.keys(parsed.data)) {\n if (key.startsWith('_') || key === 'debugSteps') {\n delete parsed.data[key];\n }\n }\n }\n const isError = parsed?.success === false;\n return { text: JSON.stringify(parsed), isError };\n } catch {\n return { text, isError: false };\n }\n}\n\n/** Spec cap on the number of decision questions surfaced per turn. */\nconst MAX_DECISION_QUESTIONS = 3;\n\n/**\n * Extracts decision questions from a parsed tool-result text, if present.\n * Validates each entry against `QuestionSchema` and drops malformed items;\n * caps the array at `MAX_DECISION_QUESTIONS` (defense-in-depth — Slice 2's\n * generator already caps at 3, but we don't trust the cast here).\n *\n * Returns null when the text isn't JSON, has no `data.questions`, or\n * contains zero valid questions after validation.\n */\nexport function extractDecisionQuestions(text: string): Question[] | null {\n let parsed: unknown;\n try {\n parsed = JSON.parse(text);\n } catch {\n return null;\n }\n\n const rawQs = (parsed as { data?: { questions?: unknown } } | null)?.data?.questions;\n if (!Array.isArray(rawQs) || rawQs.length === 0) return null;\n\n const valid: Question[] = [];\n for (const raw of rawQs) {\n const result = QuestionSchema.safeParse(raw);\n if (result.success) valid.push(result.data);\n if (valid.length === MAX_DECISION_QUESTIONS) break;\n }\n return valid.length > 0 ? valid : null;\n}\n\n/**\n * Renders the JSON-envelope text block appended to the tool result content\n * when decision questions are present. The leading sentinel string lets the\n * LLM client recognize and surface the questions in prose for clients\n * without elicitation support.\n */\nexport function renderQuestionsEnvelope(questions: Question[]): string {\n return `Decision questions (structured): ${JSON.stringify({ questions })}`;\n}\n\n// ═══════════════════════════════════════════════════════════════════════════════\n// MCP SERVER FACTORY\n// ═══════════════════════════════════════════════════════════════════════════════\n\n/**\n * Factory for creating per-request scoped database instances.\n * Injected from the controller/handler layer to keep the protocol layer\n * free of direct adapter imports.\n */\nexport interface ScopedDepsFactory {\n /** Creates scoped userDb and systemDb for the given user and index scope. */\n create(userId: string, indexScope: string[]): Pick<ToolDeps, 'userDb' | 'systemDb'>;\n}\n\n/**\n * Computes the index scope passed to the per-request scoped DB factory. When\n * `networkScopeId` is non-null, the agent is bound to a single network and\n * may only reach that network plus the user's personal index. Otherwise the\n * full set of the user's network memberships is returned.\n */\nexport const computeAgentIndexScope = (\n userNetworks: { networkId: string; isPersonal?: boolean | null }[],\n networkScopeId: string | null | undefined,\n): string[] => {\n if (!networkScopeId) {\n return userNetworks.map((m) => m.networkId);\n }\n return userNetworks\n .filter((m) => m.networkId === networkScopeId || m.isPersonal === true)\n .map((m) => m.networkId);\n};\n\n/**\n * Promotes a network-scoped agent's bound network into the resolved tool\n * context as the implicit chat scope. Every tool that branches on\n * `context.networkId` (read_networks, read_intents, read_user_profiles,\n * opportunity tools, etc.) then enforces scope automatically — without this\n * step the DB-level `indexScope` clamp guards cross-user data but tools that\n * shape their response off `context.networkId` (notably `read_networks`'\n * `publicNetworks` branch) would still leak the global view.\n *\n * No-op when there is no scope, or when an explicit chat scope is already\n * set (a user-driven index-scoped chat must keep precedence over the agent\n * binding — which would be a strict subset anyway, since the API key cannot\n * reach beyond its bound network).\n */\nexport const applyNetworkScopeToContext = (\n context: ResolvedToolContext,\n networkScopeId: string | null | undefined,\n): void => {\n if (!networkScopeId) return;\n if (context.networkId) return;\n\n context.networkId = networkScopeId;\n // Clamp indexScope to [boundNetwork, personalIndex] BEFORE the membership\n // check below. If the bound network is not in userNetworks (defensive case),\n // this still produces a safe scope (personal index only) rather than\n // leaving the unclamped scope set by resolveChatContext.\n context.indexScope = computeAgentIndexScope(context.userNetworks, networkScopeId);\n\n const bound = context.userNetworks.find((m) => m.networkId === networkScopeId);\n if (!bound) return;\n\n context.indexName = bound.networkTitle;\n context.scopedIndex = {\n id: bound.networkId,\n title: bound.networkTitle,\n prompt: bound.indexPrompt ?? null,\n };\n const isOwner = bound.permissions?.includes('owner') ?? false;\n context.scopedMembershipRole = isOwner ? 'owner' : 'member';\n context.isOwner = isOwner;\n};\n\n/**\n * Tools allowed during onboarding — everything else is gated until\n * complete_onboarding is called. Includes the agent-gate-exempt tools\n * (register_agent, read_docs, scrape_url) because they are informational /\n * registration primitives needed at every lifecycle stage.\n */\nexport const ONBOARDING_ALLOWED: ReadonlySet<string> = new Set([\n 'register_agent',\n 'read_docs',\n 'scrape_url',\n 'record_onboarding_privacy_consent',\n 'preview_user_profile',\n 'get_profile_run',\n 'cancel_profile_run',\n 'confirm_user_profile',\n 'create_user_profile',\n 'complete_onboarding',\n 'import_gmail_contacts',\n 'read_networks',\n 'create_network_membership',\n 'create_intent',\n 'read_user_profiles',\n]);\n\n/**\n * Builds the onboarding gate message for MCP callers. Condensed from the\n * chat orchestrator's 8-step flow (chat.prompt.ts buildOnboarding) into a\n * 7-step tool-error guide suited for non-interactive MCP clients.\n */\nexport function buildMcpOnboardingMessage(ctx: ResolvedToolContext): string {\n const nameStep = ctx.hasName\n ? `1. Greet the user and confirm their name (\"You're ${ctx.userName}, right?\").`\n : `1. Ask the user for their name and a short self-description.`;\n\n const communityStep = ctx.networkId\n ? `5. (Skipped — user is already in \"${ctx.indexName ?? 'their community'}\".)`\n : `5. Call read_networks() and let the user pick communities to join via create_network_membership(networkId=...).`;\n\n const allowedList = Array.from(ONBOARDING_ALLOWED).join(', ');\n\n return (\n `This user has not completed onboarding. You must guide them through setup before they can use other tools. ` +\n `Only the following tools are available until onboarding is complete: ` +\n `${allowedList}.\\n\\n` +\n `Onboarding flow:\\n` +\n `${nameStep}\\n` +\n `2. Ask whether the user allows use of event/EdgeOS profile data, then call record_onboarding_privacy_consent(edgeosImportGranted=...).\\n` +\n `3. Ask separately whether the user allows public internet/profile lookup, then call record_onboarding_privacy_consent(publicProfileLookupGranted=...).\\n` +\n `4. Call preview_user_profile(...) using only allowed inputs; do not run public lookup unless consent was granted. If it returns profileRunId, poll get_profile_run(profileRunId=...) until status is succeeded, then use its result as the draft.\\n` +\n `5. Present the profile draft and ask \"Does that look right?\" On approval/correction, call confirm_user_profile(...).\\n` +\n `${communityStep}\\n` +\n `6. Ask what the user is looking for and call create_intent(description=\"...\", autoApprove=true) so the first signal is persisted.\\n` +\n `7. Call complete_onboarding() to finish setup. Gmail/contact import and discovery are optional after onboarding, never mandatory.`\n );\n}\n\n/**\n * Creates an MCP server with all protocol tools registered.\n * Tools resolve auth per-request via the HTTP request available in ServerContext.\n *\n * @param deps - Shared tool dependencies (graphs, database, embedder, etc.)\n * @param authResolver - Resolves authenticated identity from the HTTP request\n * @param scopedDepsFactory - Factory for creating per-request scoped databases\n * @returns A configured McpServer ready to be connected to a transport\n */\nfunction createMcpTraceEmitter(toolName: string, ctx: ServerContext): TraceEmitter | undefined {\n const token = ctx.mcpReq._meta?.progressToken;\n if (typeof token !== 'string' && typeof token !== 'number') return undefined;\n\n let progress = 0;\n return (event) => {\n progress += 1;\n const message = (() => {\n if (event.type === 'graph_start') return `${toolName}: ${event.name} started`;\n if (event.type === 'graph_end') return `${toolName}: ${event.name} finished${event.durationMs != null ? ` in ${event.durationMs}ms` : ''}`;\n if (event.type === 'agent_start') return `${toolName}: ${event.name} agent started`;\n if (event.type === 'agent_end') return `${toolName}: ${event.name} agent finished${event.durationMs != null ? ` in ${event.durationMs}ms` : ''}`;\n if (event.type === 'opportunity_draft_ready') return `${toolName}: opportunity draft ready`;\n return `${toolName}: progress`;\n })();\n\n const notification: Parameters<ServerContext['mcpReq']['notify']>[0] = {\n method: 'notifications/progress',\n params: { progressToken: token, progress, message },\n };\n void ctx.mcpReq.notify(notification).catch((err) => {\n logger.debug('Failed to send MCP progress notification', {\n toolName,\n error: err instanceof Error ? err.message : String(err),\n });\n });\n };\n}\n\nexport const MCP_INSTRUCTIONS = `\nIndex Network is a private, intent-driven discovery protocol. You help users find the right people and help the right people find them, via Index Network MCP tools.\n\n# Voice\nCalm, direct, analytical, concise. Preferred vocabulary: opportunity, overlap, signal, pattern, emerging, relevant, adjacency.\n\n# Banned vocabulary\nNEVER use \"search\" in any form. Use \"looking up\" for indexed data, \"find\" / \"look for\" for discovery, \"check\" for verification, \"discover\" for exploration. Banned: leverage, unlock, optimize, scale, disrupt, revolutionary, AI-powered, maximize value, act fast, networking, match.\n\n# Entity model\n- User — has one Profile, many Memberships, many Intents.\n- Profile — identity (bio, skills, interests, location).\n- Index — community with title, prompt (purpose), join policy. Has Members.\n- Membership — User↔Index junction. \\`isPersonal: true\\` marks the user's personal index (contacts).\n- Intent — what a user is looking for (signal). Description, summary, embedding.\n- IntentIndex — Intent↔Index junction (auto-assigned).\n- Opportunity — discovered connection between users. Roles, status, reasoning.\n\n# Output rules\n- NEVER expose internal IDs, UUIDs, field names, or tool names — EXCEPT when an ID is actionable for the user (e.g. a \\`conversationId\\` they need to open a chat). Surface such IDs verbatim when the tool returns them.\n- NEVER use internal vocabulary — say \"signal\" not \"intent\", \"community\" not \"index\".\n- NEVER dump raw JSON. Synthesize in natural language.\n- Surface top 1–3 relevant points unless asked for the full list.\n- Prefer first names; use full names only to disambiguate.\n- Translate statuses: draft/latent → \"draft\", pending → \"sent\", accepted → \"connected\".\n- NEVER fabricate data. If you don't have it, call the appropriate tool.\n\n# Tool guidance\nEach tool's description contains its own usage rules (when to call, when NOT to call, required prerequisites, post-call follow-ups). Read the description of every tool you call — that is where the per-tool workflow patterns live.\n\n# Authentication\nPass your API key in the \\`x-api-key\\` request header (not \\`Authorization: Bearer\\`).\n\n# Opportunity lifecycle\nOpportunities move through: draft → pending → accepted (or rejected).\n\n- **draft** (you created it, not yet sent): offer to send it; confirm before calling update_opportunity with pending.\n- **pending, you sent it**: waiting for the other side — nothing to do.\n- **pending, you received it**: the other person is waiting for your response. Surface it to the user and ask if they want to start a chat. Only call update_opportunity with accepted after explicit user confirmation.\n- **accepted**: both sides are connected — a direct conversation exists. Surface the conversationId to the user if available.\n\nNever accept a received opportunity without explicit user approval in the current conversation.\n\n# Decision questions after discovery\n\nAfter \\`discover_opportunities\\`, the tool result may include a second text block starting with \\`Decision questions (structured): ...\\`. This means the discovery engine ran negotiations but needs human input to sharpen the next turn — e.g. clarify timing, role, stage, or location.\n\n**When this block is present:**\n1. Parse the \\`questions\\` array from the JSON after the sentinel.\n2. Each question has \\`title\\` (decision domain, ≤12 chars), \\`prompt\\` (ends in \\`?\\`), \\`options\\` (2–4 items, each with \\`label\\` and \\`description\\`), and \\`multiSelect\\`. The safest option is labeled \\`... (Recommended)\\`.\n3. Present each question in natural language: ask the \\`prompt\\`, list options as \\`**{label}** — {description}\\`. Never expose the JSON or technical field names.\n4. Wait for the user's answer, then fold it into the next \\`discover_opportunities(searchQuery=...)\\` call.\n\n**Elicitation-capable clients** (those that declared \\`elicitation\\` support in \\`initialize\\`): the server dispatches \\`elicitation/create\\` requests directly — answers are written back to the chat session automatically. You will not see the envelope as a follow-up task in that case.\n`.trim();\n\n/**\n * Extracts a Bearer token from an HTTP Authorization header.\n */\nexport function extractBearerToken(req: Request): string | undefined {\n const authHeader = req.headers.get('Authorization');\n if (!authHeader) return undefined;\n const [scheme, token] = authHeader.trim().split(/\\s+/, 2);\n if (scheme?.toLowerCase() === 'bearer' && token) return token;\n return undefined;\n}\n\n/**\n * Normalizes the x-index-surface header to a typed surface value.\n * Unknown or absent values collapse to 'web'.\n */\nlet hasWarnedInvalidSurface = false;\nexport function parseClientSurface(raw: string | null): 'telegram' | 'web' {\n if (raw === null || raw === '') return 'web';\n const trimmed = raw.trim().toLowerCase();\n if (trimmed === '') return 'web';\n if (trimmed === 'telegram') return 'telegram';\n if (trimmed === 'web') return 'web';\n if (!hasWarnedInvalidSurface) {\n hasWarnedInvalidSurface = true;\n logger.warn('Unknown x-index-surface value (collapsing to web; warning once per process)');\n }\n return 'web';\n}\n\nexport function createMcpServer(\n deps: ToolDeps,\n authResolver: McpAuthResolver,\n scopedDepsFactory: ScopedDepsFactory,\n): McpServer {\n // Tools exempt from the agent-registration gate — available before registration is complete.\n const AGENT_GATE_EXEMPT = new Set(['register_agent', 'read_docs', 'scrape_url']);\n\n const server = new McpServer(\n { name: 'index-network', version: '1.0.0' },\n { instructions: MCP_INSTRUCTIONS },\n );\n\n const registry = createToolRegistry(deps);\n\n for (const [toolName, toolDef] of registry) {\n // Convert Zod 3 schema to JSON Schema, then wrap with fromJsonSchema\n // for MCP SDK's StandardSchemaWithJSON compatibility\n const jsonSchema = zodToJsonSchema(toolDef.schema) as JsonSchemaType;\n const mcpSchema = fromJsonSchema(jsonSchema);\n\n server.registerTool(\n toolName,\n {\n description: toolDef.description,\n inputSchema: mcpSchema,\n },\n async (args: unknown, ctx: ServerContext) => {\n let reportDeps = deps;\n let reportUserId: string | undefined;\n let reportContext: ResolvedToolContext | undefined;\n\n try {\n // Extract the original HTTP request from the MCP server context\n const httpReq = ctx.http?.req;\n if (!httpReq) {\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ error: 'No HTTP request available in MCP context' }) }],\n isError: true,\n };\n }\n\n // Extract transport-neutral auth input DTO from the HTTP request\n const mcpAuthInput: McpAuthInput = {\n bearerToken: extractBearerToken(httpReq),\n apiKey: httpReq.headers.get('x-api-key') ?? undefined,\n clientSurface: parseClientSurface(httpReq.headers.get('x-index-surface')),\n telegramHandle: httpReq.headers.get('x-index-telegram-handle') ?? undefined,\n telegramUsername: httpReq.headers.get('x-index-telegram-username') ?? undefined,\n };\n\n // Resolve authenticated identity from the auth input DTO\n const { userId, agentId, isSessionAuth, networkScopeId, clientSurface } = await authResolver.resolveIdentity(mcpAuthInput);\n reportUserId = userId;\n\n // Per-principal MCP throttle. Runs BEFORE any DB work so a throttled\n // call short-circuits cheaply. The /mcp transport bypasses the\n // controller-level RateLimit guard, so this is the only volume cap on\n // tool calls — it stops an over-eager agent from cascading itself into\n // provider rate limits.\n if (deps.mcpRateLimiter) {\n // Throttling is best-effort: never let a limiter failure (or a host\n // implementation that throws instead of failing open) break tool\n // dispatch. Treat any error as \"allowed\".\n let decision: Awaited<ReturnType<NonNullable<typeof deps.mcpRateLimiter>>> | null = null;\n try {\n decision = await deps.mcpRateLimiter({\n userId,\n ...(agentId ? { agentId } : {}),\n toolName,\n });\n } catch (rlErr) {\n logger.warn(`MCP rate limiter threw for \"${toolName}\" — failing open`, {\n error: rlErr instanceof Error ? rlErr.message : String(rlErr),\n });\n }\n if (decision && !decision.allowed) {\n const retryAfterSec = decision.retryAfterSec ?? 60;\n return {\n content: [{\n type: 'text' as const,\n text: JSON.stringify({\n error: 'Rate limit exceeded',\n message:\n `Too many ${toolName} calls in a short period. Wait ${retryAfterSec}s before retrying.` +\n (toolName === 'discover_opportunities'\n ? ` If a discovery run is in progress, poll get_discovery_run instead of calling discover_opportunities again.`\n : ''),\n retryAfterSec,\n }),\n }],\n isError: true,\n };\n }\n }\n\n // Resolve chat context for the user (mark as MCP — no interactive UI available)\n const context = await resolveChatContext({ database: deps.database, userId });\n reportContext = context;\n context.isMcp = true;\n if (agentId) {\n context.agentId = agentId;\n }\n if (clientSurface) {\n context.clientSurface = clientSurface;\n }\n\n // Network-scoped agents inherit their bound network as the implicit chat\n // scope. Every tool that branches on `context.networkId` then enforces\n // the same boundary the DB-level `indexScope` clamp enforces below —\n // most importantly `read_networks`, which would otherwise return the\n // global `publicNetworks` catalog for unscoped contexts.\n applyNetworkScopeToContext(context, networkScopeId);\n\n // Gate: API-key callers (background agents) must register before using most tools.\n // OAuth/JWT session callers (human MCP clients such as Claude Code) are exempt —\n // their identity is already established via the auth flow and they have no agent entity.\n if (!isSessionAuth && !context.agentId && !AGENT_GATE_EXEMPT.has(toolName)) {\n return {\n content: [{\n type: 'text' as const,\n text: JSON.stringify({\n error: 'Agent not registered',\n message:\n 'You must register as an agent before using Index tools. ' +\n 'Call register_agent with your agent name to establish an identity. ' +\n 'The tools register_agent, read_docs, and scrape_url are available without registration.',\n }),\n }],\n isError: true,\n };\n }\n\n // Gate: non-onboarded users can only use onboarding-related tools.\n // Mirrors the chat orchestrator's ONBOARDING MODE — the MCP client must\n // walk the user through profile creation, Gmail connect, intent capture,\n // and complete_onboarding() before full tool access is granted.\n if (context.isOnboarding && !ONBOARDING_ALLOWED.has(toolName)) {\n const onboardingSteps = buildMcpOnboardingMessage(context);\n return {\n content: [{\n type: 'text' as const,\n text: JSON.stringify({\n error: 'Onboarding required',\n message: onboardingSteps,\n }),\n }],\n isError: true,\n };\n }\n\n // Build per-request scoped databases via injected factory.\n // Network-scoped agents are clamped to their bound network plus the user's\n // personal index — they cannot reach other networks even when the user is\n // a member of them. The personal-index reachability is preserved so the\n // agent can still manage its owner's profile and contacts.\n // context.indexScope is now the single source of truth: set by\n // resolveChatContext (full set) and narrowed by applyNetworkScopeToContext.\n const scopedDbs = scopedDepsFactory.create(userId, context.indexScope);\n\n // Override deps with per-request scoped databases\n const requestDeps: ToolDeps = { ...deps, ...scopedDbs };\n reportDeps = requestDeps;\n\n // Re-create registry with per-request deps for scoped database access\n const requestRegistry = createToolRegistry(requestDeps);\n const requestTool = requestRegistry.get(toolName);\n\n if (!requestTool) {\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ error: `Tool \"${toolName}\" not found` }) }],\n isError: true,\n };\n }\n\n // Validate input against the original Zod schema\n const parseResult = (toolDef.schema as z.ZodType).safeParse(args);\n if (!parseResult.success) {\n const issues = parseResult.error.issues.map((i) => `${i.path.join('.')}: ${i.message}`).join('; ');\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ success: false, error: `Invalid input: ${issues}` }) }],\n isError: true,\n };\n }\n const validatedArgs = parseResult.data;\n\n // Execute the tool handler through the shared runtime so MCP calls have\n // consistent timeout, cancellation, progress, and requestContext plumbing.\n const result = await invokeToolRuntime({\n toolName,\n tool: requestTool,\n context,\n query: validatedArgs,\n signal: ctx.mcpReq.signal,\n traceEmitter: createMcpTraceEmitter(toolName, ctx),\n });\n\n const { text: sanitizedText, isError: toolIsError } = sanitizeMcpResult(result);\n\n // Slice 5: decision questions post-processing for discover_opportunities only.\n if (toolName === \"discover_opportunities\" && !toolIsError) {\n const questions = extractDecisionQuestions(sanitizedText);\n if (questions) {\n const envelopeBlock = {\n type: \"text\" as const,\n text: renderQuestionsEnvelope(questions),\n };\n\n const supportsElicitation =\n !!server.server.getClientCapabilities()?.elicitation;\n\n // Capture into a local const so TS preserves the narrowing\n // inside the callback below. Optional chains don't survive\n // across closure boundaries under strict mode.\n const elicitInput = ctx.mcpReq?.elicitInput;\n\n if (supportsElicitation && elicitInput) {\n // Sequential — never parallel (day-one rule). We await the loop\n // before returning the tool result so test harnesses can observe\n // the dispatched calls deterministically.\n await dispatchElicitations({\n userId,\n questions,\n elicitInput: (params) => elicitInput(params),\n chatMessageWriter: deps.chatMessageWriter,\n });\n }\n\n return {\n content: [\n { type: \"text\" as const, text: sanitizedText },\n envelopeBlock,\n ],\n ...(toolIsError ? { isError: true } : {}),\n };\n }\n }\n\n return {\n content: [{ type: 'text' as const, text: sanitizedText }],\n ...(toolIsError ? { isError: true } : {}),\n };\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err);\n logger.error(`MCP tool \"${toolName}\" failed`, { error: message });\n if (shouldReportMcpToolError(err)) {\n reportDeps.reportToolError?.(err, {\n subsystem: 'mcp',\n operation: 'mcp.tool',\n toolName,\n userId: reportUserId,\n tags: {\n transport: 'mcp',\n toolName,\n },\n context: {\n agentId: reportContext?.agentId,\n networkId: reportContext?.networkId,\n indexScope: reportContext?.indexScope,\n },\n });\n }\n const runtimeResult = toolRuntimeErrorToResult(err);\n return {\n content: [{ type: 'text' as const, text: runtimeResult ?? JSON.stringify({ error: message }) }],\n isError: true,\n };\n }\n },\n );\n }\n\n logger.verbose(`MCP server created with ${registry.size} tools`);\n return server;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"negotiation.graph.d.ts","sourceRoot":"/","sources":["negotiation/negotiation.graph.ts"],"names":[],"mappings":"AAGA,OAAO,EAAkB,KAAK,YAAY,EAAE,MAAM,4CAA4C,CAAC;AAC/F,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,4CAA4C,CAAC;AAC3F,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,sDAAsD,CAAC;AACpG,OAAO,KAAK,EAAE,eAAe,EAA0B,MAAM,oDAAoD,CAAC;AAClH,OAAO,EAAyB,KAAK,eAAe,EAAE,KAAK,kBAAkB,EAAE,KAAK,sBAAsB,EAAE,KAAK,cAAc,EAAE,KAAK,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAG3L,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mC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;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EACnD,IAAI,CAAC,EAAE;IACL,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,qBAAqB,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC5C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,mBAAmB,CAAC,EAAE,qBAAqB,CAAC;IAC5C,OAAO,CAAC,EAAE,cAAc,GAAG,SAAS,CAAC;CACtC,GACA,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAoJ9B;AAED;;GAEG;AACH,wBAAgB,6BAA6B,CAAC,IAAI,EAAE;IAClD,QAAQ,EAAE,wBAAwB,CAAC;IACnC,UAAU,EAAE,eAAe,CAAC;IAC5B,YAAY,CAAC,EAAE,uBAAuB,CAAC;CACxC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;yBAGA"}
@@ -5,6 +5,15 @@ import { NegotiationGraphState } from "./negotiation.state.js";
5
5
  import { IndexNegotiator } from "./negotiation.agent.js";
6
6
  import { protocolLogger } from "../shared/observability/protocol.logger.js";
7
7
  const logger = protocolLogger("NegotiationGraph");
8
+ /** Extracts the ordered NegotiationTurn list from A2A message data parts. */
9
+ function turnsFromMessages(messages) {
10
+ return messages
11
+ .map((m) => {
12
+ const dataPart = m.parts.find((p) => p.kind === "data");
13
+ return dataPart?.data;
14
+ })
15
+ .filter(Boolean);
16
+ }
8
17
  /**
9
18
  * Factory for the bilateral negotiation LangGraph state machine.
10
19
  * @remarks Accepts an AgentDispatcher for per-turn agent resolution.
@@ -46,12 +55,7 @@ export class NegotiationGraphFactory {
46
55
  return { error: 'busy' };
47
56
  }
48
57
  // --- Load prior messages and determine continuation ---
49
- const priorTurns = priorMessages
50
- .map((m) => {
51
- const dataPart = m.parts.find((p) => p.kind === 'data');
52
- return dataPart?.data;
53
- })
54
- .filter(Boolean);
58
+ const priorTurns = turnsFromMessages(priorMessages);
55
59
  const isContinuation = priorTurns.length > 0;
56
60
  // Determine currentSpeaker from last prior message
57
61
  let currentSpeaker = 'source';
@@ -126,10 +130,7 @@ export class NegotiationGraphFactory {
126
130
  const agentStart = Date.now();
127
131
  traceEmitter?.({ type: "agent_start", name: agentName });
128
132
  try {
129
- const history = state.messages.map((m) => {
130
- const dataPart = m.parts.find((p) => p.kind === "data");
131
- return dataPart?.data;
132
- }).filter(Boolean);
133
+ const history = turnsFromMessages(state.messages);
133
134
  const isSource = state.currentSpeaker === "source";
134
135
  const ownUser = isSource ? state.sourceUser : state.candidateUser;
135
136
  const otherUser = isSource ? state.candidateUser : state.sourceUser;
@@ -277,10 +278,7 @@ export class NegotiationGraphFactory {
277
278
  }
278
279
  return {};
279
280
  }
280
- const history = state.messages.map((m) => {
281
- const dataPart = m.parts.find((p) => p.kind === "data");
282
- return dataPart?.data;
283
- }).filter(Boolean);
281
+ const history = turnsFromMessages(state.messages);
284
282
  const lastTurn = state.lastTurn;
285
283
  const hasOpportunity = lastTurn?.action === "accept";
286
284
  const atCap = (state.maxTurns ?? 0) > 0 && state.turnCount >= state.maxTurns && lastTurn?.action !== "accept" && lastTurn?.action !== "reject";
@@ -486,12 +484,7 @@ export async function negotiateCandidates(negotiationGraph, sourceUser, candidat
486
484
  }
487
485
  : null;
488
486
  if (onCandidateResolved) {
489
- const turnHistory = (result.messages ?? [])
490
- .map((m) => {
491
- const dataPart = m.parts?.find((p) => p.kind === "data");
492
- return dataPart?.data;
493
- })
494
- .filter((t) => !!t);
487
+ const turnHistory = turnsFromMessages(result.messages ?? []);
495
488
  const resolvedOutcome = result.outcome ?? {
496
489
  hasOpportunity: false,
497
490
  agreedRoles: [],
@@ -1 +1 @@
1
- {"version":3,"file":"negotiation.graph.js","sourceRoot":"/","sources":["negotiation/negotiation.graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAElD,OAAO,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAC;AACxE,OAAO,EAAE,cAAc,EAAqB,MAAM,4CAA4C,CAAC;AAI/F,OAAO,EAAE,qBAAqB,EAA8H,MAAM,wBAAwB,CAAC;AAC3L,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,4CAA4C,CAAC;AAG5E,MAAM,MAAM,GAAG,cAAc,CAAC,kBAAkB,CAAC,CAAC;AAElD;;;GAGG;AACH,MAAM,OAAO,uBAAuB;IAClC,YACU,QAAkC,EAClC,UAA2B,EAC3B,YAAsC,EACtC,iBAAuC;QAHvC,aAAQ,GAAR,QAAQ,CAA0B;QAClC,eAAU,GAAV,UAAU,CAAiB;QAC3B,iBAAY,GAAZ,YAAY,CAA0B;QACtC,sBAAiB,GAAjB,iBAAiB,CAAsB;IAC9C,CAAC;IAEJ,WAAW;QACT,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,YAAY,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC;QACvE,MAAM,WAAW,GAAG,IAAI,eAAe,EAAE,CAAC;QAE1C,MAAM,QAAQ,GAAG,KAAK,EAAE,KAAyC,EAAE,EAAE;YACnE,IAAI,C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\"../shared/agent/model-signal.js\";\nimport { requestContext, type TraceEmitter } from \"../shared/observability/request-context.js\";\nimport type { NegotiationGraphDatabase } from \"../shared/interfaces/database.interface.js\";\nimport type { NegotiationTimeoutQueue } from \"../shared/interfaces/negotiation-events.interface.js\";\nimport type { AgentDispatcher, NegotiationTurnPayload } from \"../shared/interfaces/agent-dispatcher.interface.js\";\nimport { NegotiationGraphState, type NegotiationTurn, type NegotiationOutcome, type UserNegotiationContext, type SeedAssessment, type NegotiationGraphLike } from \"./negotiation.state.js\";\nimport { IndexNegotiator } from \"./negotiation.agent.js\";\nimport { protocolLogger } from \"../shared/observability/protocol.logger.js\";\nimport type { QuestionerEnqueueFn } from \"../questioner/questioner.types.js\";\n\nconst logger = protocolLogger(\"NegotiationGraph\");\n\n/**\n * Factory for the bilateral negotiation LangGraph state machine.\n * @remarks Accepts an AgentDispatcher for per-turn agent resolution.\n */\nexport class NegotiationGraphFactory {\n constructor(\n private database: NegotiationGraphDatabase,\n private dispatcher: AgentDispatcher,\n private timeoutQueue?: NegotiationTimeoutQueue,\n private questionerEnqueue?: QuestionerEnqueueFn,\n ) {}\n\n createGraph() {\n const { database, dispatcher, timeoutQueue, questionerEnqueue } = this;\n const systemAgent = new IndexNegotiator();\n\n const initNode = async (state: typeof NegotiationGraphState.State) => {\n try {\n // Find-or-create the DM conversation for this agent pair (same as user DMs)\n const agentIdA = `agent:${state.sourceUser.id}`;\n const agentIdB = `agent:${state.candidateUser.id}`;\n const conversation = await database.getOrCreateDM(agentIdA, agentIdB, 'agent');\n\n // --- Lock gate: check for an active task on this conversation ---\n const priorMessages = await database.getMessagesForConversation(conversation.id);\n\n let isLocked = false;\n if (state.opportunityId) {\n const priorTask = await database.getNegotiationTaskForOpportunity(state.opportunityId);\n if (priorTask) {\n const activeStates = ['submitted', 'working', 'input_required', 'waiting_for_agent', 'claimed'];\n const isFresh = (Date.now() - new Date(priorTask.updatedAt).getTime()) < 5 * 60 * 1000;\n if (activeStates.includes(priorTask.state) && isFresh) {\n isLocked = true;\n }\n }\n }\n\n if (isLocked) {\n logger.info('[Graph:Init] Conversation locked by active task, returning busy', {\n conversationId: conversation.id,\n opportunityId: state.opportunityId,\n });\n return { error: 'busy' };\n }\n\n // --- Load prior messages and determine continuation ---\n const priorTurns: NegotiationTurn[] = priorMessages\n .map((m) => {\n const dataPart = (m.parts as Array<{ kind?: string; data?: unknown }>).find((p) => p.kind === 'data');\n return dataPart?.data as NegotiationTurn;\n })\n .filter(Boolean);\n\n const isContinuation = priorTurns.length > 0;\n\n // Determine currentSpeaker from last prior message\n let currentSpeaker: 'source' | 'candidate' = 'source';\n if (isContinuation && priorMessages.length > 0) {\n const lastSender = priorMessages[priorMessages.length - 1].senderId;\n currentSpeaker = lastSender === agentIdA ? 'candidate' : 'source';\n }\n\n // Determine scenario-based maxTurns\n const scope = { action: 'manage:negotiations', scopeType: 'network', scopeId: state.indexContext.networkId };\n const [sourceHasAgent, candidateHasAgent] = await Promise.all([\n dispatcher.hasPersonalAgent(state.sourceUser.id, scope),\n dispatcher.hasPersonalAgent(state.candidateUser.id, scope),\n ]);\n\n const ambientMax = Number(process.env.NEGOTIATION_MAX_TURNS_AMBIENT) || 6;\n let maxTurns = state.maxTurns;\n if (maxTurns == null) {\n maxTurns = (sourceHasAgent && candidateHasAgent) ? 0 : ambientMax;\n }\n\n const task = await database.createTask(conversation.id, {\n type: 'negotiation',\n sourceUserId: state.sourceUser.id,\n candidateUserId: state.candidateUser.id,\n networkId: state.indexContext.networkId,\n ...(state.opportunityId && { opportunityId: state.opportunityId }),\n maxTurns,\n isContinuation,\n priorTurnCount: priorTurns.length,\n });\n\n if (state.opportunityId) {\n await database.updateOpportunityStatus(state.opportunityId, 'negotiating').catch((err) => {\n logger.error('[Graph:Init] Failed to set opportunity status to negotiating', { opportunityId: state.opportunityId, error: err });\n });\n }\n\n // Load user answers collected by the questioner between sessions\n const userAnswers = (isContinuation && state.opportunityId)\n ? await database.getOpportunityUserAnswers(state.opportunityId).catch((err) => {\n logger.error('[Graph:Init] Failed to load user answers', { opportunityId: state.opportunityId, error: err });\n return [];\n })\n : [];\n\n // Seed messages with prior turns (additive reducer appends new turns on top)\n const seedMessages = isContinuation ? priorMessages.map((m) => ({\n id: m.id,\n senderId: m.senderId,\n role: 'agent' as const,\n parts: m.parts,\n createdAt: m.createdAt,\n })) : [];\n\n return {\n conversationId: conversation.id,\n taskId: task.id,\n currentSpeaker,\n turnCount: 0,\n maxTurns,\n isContinuation,\n priorTurnCount: priorTurns.length,\n ...(userAnswers.length > 0 && { userAnswers }),\n ...(seedMessages.length > 0 && { messages: seedMessages }),\n };\n } catch (err) {\n return { error: `Init failed: ${err instanceof Error ? err.message : String(err)}` };\n }\n };\n\n const turnNode = async (state: typeof NegotiationGraphState.State) => {\n const traceEmitter = requestContext.getStore()?.traceEmitter;\n // Local helper to emit events whose shape is wider than the declared\n // `TraceEmitter` union. The chat agent already casts at its relay sink;\n // here we localize the cast at the callsite so the rest of the body stays typed.\n const emitWide = (event: Record<string, unknown>) =>\n (traceEmitter as ((e: Record<string, unknown>) => void) | undefined)?.(event);\n const agentName = \"Index negotiator\";\n const agentStart = Date.now();\n traceEmitter?.({ type: \"agent_start\", name: agentName });\n\n try {\n const history: NegotiationTurn[] = state.messages.map((m) => {\n const dataPart = (m.parts as Array<{ kind?: string; data?: unknown }>).find((p) => p.kind === \"data\");\n return dataPart?.data as NegotiationTurn;\n }).filter(Boolean);\n\n const isSource = state.currentSpeaker === \"source\";\n const ownUser = isSource ? state.sourceUser : state.candidateUser;\n const otherUser = isSource ? state.candidateUser : state.sourceUser;\n\n // Determine if this is the system agent's final allowed turn\n const maxTurns = state.maxTurns ?? 0;\n const isFinalTurn = maxTurns > 0 && (state.turnCount + 1) >= maxTurns;\n\n const payload: NegotiationTurnPayload = {\n negotiationId: state.taskId,\n ownUser,\n otherUser,\n indexContext: state.indexContext,\n seedAssessment: state.seedAssessment,\n history,\n isFinalTurn,\n isDiscoverer: isSource,\n ...(state.discoveryQuery && isSource && { discoveryQuery: state.discoveryQuery }),\n };\n\n const scope = { action: 'manage:negotiations', scopeType: 'network', scopeId: state.indexContext.networkId };\n\n const dispatchResult = await dispatcher.dispatch(ownUser.id, scope, payload, { timeoutMs: state.timeoutMs });\n\n let turn: NegotiationTurn;\n\n if (dispatchResult.handled) {\n // Personal agent responded\n turn = dispatchResult.turn;\n } else if (dispatchResult.reason === 'waiting') {\n // Long timeout — graph suspends. Persist the full turn context so the\n // polling agent (and MCP consumers via get_negotiation) reconstruct\n // the same view the in-process system agent would see. The view is\n // stored in absolute source/candidate terms; perspective is projected\n // at pickup time using the claiming user's id.\n traceEmitter?.({ type: \"agent_end\", name: agentName, durationMs: Date.now() - agentStart, summary: \"waiting_for_agent\" });\n await database.setTaskTurnContext(state.taskId, {\n sourceUser: state.sourceUser,\n candidateUser: state.candidateUser,\n indexContext: state.indexContext,\n seedAssessment: state.seedAssessment,\n // Keep discoveryQuery speaker-scoped: include it only when the\n // parked turn belongs to the discoverer (source). Persisting it on\n // candidate-side turns would make the pickup prompt frame the\n // search as \"your user searched for X\" for the wrong user.\n ...(isSource && state.discoveryQuery && { discoveryQuery: state.discoveryQuery }),\n });\n await database.updateTaskState(state.taskId, \"waiting_for_agent\");\n return { status: 'waiting_for_agent' as const };\n } else {\n // No personal agent or timeout — run system agent\n turn = await systemAgent.invoke({\n ownUser,\n otherUser,\n indexContext: state.indexContext,\n seedAssessment: state.seedAssessment,\n history,\n isFinalTurn,\n isDiscoverer: isSource,\n ...(state.discoveryQuery && isSource && { discoveryQuery: state.discoveryQuery }),\n isContinuation: state.isContinuation,\n ...(state.userAnswers.length > 0 && { userAnswers: state.userAnswers }),\n });\n }\n\n traceEmitter?.({ type: \"agent_end\", name: agentName, durationMs: Date.now() - agentStart, summary: `${turn.action}` });\n\n // First turn must be \"propose\" (unless continuing a prior conversation)\n if (state.turnCount === 0 && !state.isContinuation && turn.action !== \"propose\") {\n logger.warn(\"[Graph:Turn] Agent returned unexpected action on turn 0, forcing to propose\", { action: turn.action });\n turn.action = \"propose\";\n }\n\n const parts = [{ kind: \"data\" as const, data: turn }];\n const message = await database.createMessage({\n conversationId: state.conversationId,\n senderId: `agent:${ownUser.id}`,\n role: \"agent\",\n parts,\n taskId: state.taskId,\n });\n\n await database.updateTaskState(state.taskId, \"working\");\n\n if (state.opportunityId) {\n emitWide({\n type: \"negotiation_turn\",\n opportunityId: state.opportunityId,\n negotiationConversationId: state.conversationId,\n turnIndex: state.turnCount,\n actor: isSource ? \"source\" : \"candidate\",\n action: turn.action,\n ...(turn.assessment?.reasoning && { reasoning: turn.assessment.reasoning }),\n ...(turn.message && { message: turn.message }),\n ...(turn.assessment?.suggestedRoles && { suggestedRoles: turn.assessment.suggestedRoles }),\n durationMs: Date.now() - agentStart,\n });\n }\n\n return {\n messages: [{\n id: message.id,\n senderId: message.senderId,\n role: \"agent\" as const,\n parts: message.parts,\n createdAt: message.createdAt,\n }],\n turnCount: state.turnCount + 1,\n currentSpeaker: (isSource ? \"candidate\" : \"source\") as \"source\" | \"candidate\",\n lastTurn: turn,\n };\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n logger.error(\"[Graph:Turn] Agent invocation failed\", { error: errMsg, stack: err instanceof Error ? err.stack : undefined, turnCount: state.turnCount });\n traceEmitter?.({ type: \"agent_end\", name: agentName, durationMs: Date.now() - agentStart, summary: `error: ${errMsg}` });\n return {\n lastTurn: {\n action: \"reject\" as const,\n assessment: { reasoning: `Agent error: ${errMsg}`, suggestedRoles: { ownUser: \"peer\" as const, otherUser: \"peer\" as const } },\n },\n turnCount: state.turnCount + 1,\n error: `Turn failed: ${errMsg}`,\n };\n }\n };\n\n const evaluateNode = (state: typeof NegotiationGraphState.State): string => {\n if (state.status === 'waiting_for_agent') return \"finalize\";\n if (state.error) return \"finalize\";\n if (!state.lastTurn) return \"finalize\";\n if (state.lastTurn.action === \"accept\") return \"finalize\";\n if (state.lastTurn.action === \"reject\") return \"finalize\";\n // question routes same as counter — next turn\n if ((state.maxTurns ?? 0) > 0 && state.turnCount >= state.maxTurns!) return \"finalize\";\n return \"turn\";\n };\n\n const finalizeNode = async (state: typeof NegotiationGraphState.State) => {\n const traceEmitter = requestContext.getStore()?.traceEmitter;\n const emitWide = (event: Record<string, unknown>) =>\n (traceEmitter as ((e: Record<string, unknown>) => void) | undefined)?.(event);\n\n if (state.status === 'waiting_for_agent') {\n if (state.opportunityId) {\n emitWide({\n type: \"negotiation_outcome\",\n opportunityId: state.opportunityId,\n outcome: \"waiting_for_agent\",\n turnCount: state.turnCount,\n isContinuation: state.isContinuation,\n });\n }\n return {};\n }\n\n const history: NegotiationTurn[] = state.messages.map((m) => {\n const dataPart = (m.parts as Array<{ kind?: string; data?: unknown }>).find((p) => p.kind === \"data\");\n return dataPart?.data as NegotiationTurn;\n }).filter(Boolean);\n\n const lastTurn = state.lastTurn;\n const hasOpportunity = lastTurn?.action === \"accept\";\n const atCap = (state.maxTurns ?? 0) > 0 && state.turnCount >= state.maxTurns! && lastTurn?.action !== \"accept\" && lastTurn?.action !== \"reject\";\n\n let agreedRoles: NegotiationOutcome[\"agreedRoles\"] = [];\n if (hasOpportunity && history.length >= 2) {\n const acceptTurn = history[history.length - 1];\n const precedingTurn = history[history.length - 2];\n const accepterIsSource = state.currentSpeaker === \"candidate\";\n const [sourceRole, candidateRole] = accepterIsSource\n ? [acceptTurn.assessment.suggestedRoles.ownUser, precedingTurn.assessment.suggestedRoles.ownUser]\n : [precedingTurn.assessment.suggestedRoles.ownUser, acceptTurn.assessment.suggestedRoles.ownUser];\n agreedRoles = [\n { userId: state.sourceUser.id, role: sourceRole },\n { userId: state.candidateUser.id, role: candidateRole },\n ];\n }\n\n const outcome: NegotiationOutcome = {\n hasOpportunity,\n agreedRoles,\n reasoning: lastTurn?.assessment.reasoning ?? \"\",\n turnCount: state.turnCount,\n ...(atCap && { reason: \"turn_cap\" as const }),\n };\n\n try {\n await database.updateTaskState(state.taskId, \"completed\");\n await database.createArtifact({\n taskId: state.taskId,\n name: \"negotiation-outcome\",\n parts: [{ kind: \"data\", data: outcome }],\n metadata: { hasOpportunity, turnCount: state.turnCount },\n });\n\n logger.info('[Graph:Finalize] Session complete', {\n conversationId: state.conversationId,\n taskId: state.taskId,\n isContinuation: state.isContinuation,\n turnsAdded: state.turnCount,\n priorTurnCount: state.priorTurnCount,\n outcome: hasOpportunity ? 'accepted' : (atCap ? 'turn_cap' : (lastTurn?.action ?? 'unknown')),\n opportunityId: state.opportunityId || undefined,\n });\n\n if (state.opportunityId) {\n const nextStatus = lastTurn?.action === 'accept'\n ? 'pending'\n : lastTurn?.action === 'reject'\n ? 'rejected'\n : 'stalled';\n await database.updateOpportunityStatus(state.opportunityId, nextStatus).catch((err) => {\n logger.error(\"[Graph:Finalize] Failed to update opportunity status\", { opportunityId: state.opportunityId, nextStatus, error: err });\n });\n }\n } catch (err) {\n logger.error(\"[Graph:Finalize] Failed to persist outcome\", { error: err });\n }\n\n if (state.opportunityId) {\n const emittedOutcome: \"accepted\" | \"rejected_stalled\" | \"turn_cap\" | \"timed_out\" =\n hasOpportunity\n ? \"accepted\"\n : atCap\n ? \"turn_cap\"\n : state.error && /timeout/i.test(state.error)\n ? \"timed_out\"\n : lastTurn?.action === \"reject\"\n ? \"rejected_stalled\"\n : \"rejected_stalled\";\n\n emitWide({\n type: \"negotiation_outcome\",\n opportunityId: state.opportunityId,\n outcome: emittedOutcome,\n turnCount: state.turnCount,\n isContinuation: state.isContinuation,\n turnsAdded: state.turnCount,\n priorTurnCount: state.priorTurnCount,\n ...(outcome.reasoning && { reasoning: outcome.reasoning }),\n ...(hasOpportunity && agreedRoles.length >= 2 && {\n agreedRoles: {\n ownUser: agreedRoles[0]?.role,\n otherUser: agreedRoles[1]?.role,\n },\n }),\n });\n }\n\n // Enqueue question generation for stalled/capped negotiations (not accepted or explicitly rejected).\n // Require turnCount > 0 so early init/turn errors don't enqueue with empty context.\n if (!hasOpportunity && lastTurn?.action !== 'reject' && state.turnCount > 0 && state.opportunityId && questionerEnqueue) {\n const stallReason: 'turn_cap' | 'timeout' | 'stalled' = atCap\n ? 'turn_cap'\n : (state.error && /timeout/i.test(state.error))\n ? 'timeout'\n : 'stalled';\n\n questionerEnqueue({\n mode: 'negotiation',\n userId: state.sourceUser.id,\n sourceType: 'opportunity',\n sourceId: state.opportunityId,\n context: {\n negotiationId: state.taskId,\n counterpartyHint: `${state.candidateUser.profile.name ?? 'Unknown'}${state.candidateUser.profile.bio ? ', ' + state.candidateUser.profile.bio : ''}`,\n indexContext: state.indexContext.prompt,\n outcomeReason: stallReason,\n keyTake: outcome.reasoning,\n userProfile: state.sourceUser.profile,\n },\n }).catch((err) =>\n logger.error('[Graph:Finalize] Failed to enqueue negotiation question generation', {\n opportunityId: state.opportunityId,\n error: err,\n })\n );\n }\n\n return { outcome, status: 'completed' as const };\n };\n\n const workflow = new StateGraph(NegotiationGraphState)\n .addNode(\"init\", initNode)\n .addNode(\"turn\", turnNode)\n .addNode(\"finalize\", finalizeNode)\n .addConditionalEdges(\"turn\", evaluateNode, {\n turn: \"turn\",\n finalize: \"finalize\",\n })\n .addConditionalEdges(\"init\", (state: typeof NegotiationGraphState.State) => {\n return state.error ? \"finalize\" : \"turn\";\n }, { turn: \"turn\", finalize: \"finalize\" })\n .addEdge(\"__start__\", \"init\")\n .addEdge(\"finalize\", \"__end__\");\n\n return workflow.compile();\n }\n}\n\nexport interface NegotiationCandidate {\n userId: string;\n reasoning: string;\n valencyRole: string;\n networkId?: string;\n candidateUser: UserNegotiationContext;\n /** The explicit search query that triggered discovery (if any). */\n discoveryQuery?: string;\n /**\n * ID of the opportunity this negotiation is for. When set, the negotiation\n * graph's finalize node updates the opportunity's status based on the outcome\n * (`accept` → 'pending', `reject` → 'rejected', otherwise → 'stalled').\n */\n opportunityId?: string;\n}\n\nexport interface NegotiationResult {\n userId: string;\n agreedRoles: NegotiationOutcome[\"agreedRoles\"];\n reasoning: string;\n turnCount: number;\n}\n\n/**\n * Per-candidate resolution hook — fires as each negotiation settles, before\n * Promise.all aggregates. Used by the orchestrator branch to progressively\n * stream `opportunity_draft_ready` events as each candidate resolves, rather\n * than emitting all at once after the full fan-out completes. Awaited so the\n * caller can run async work (DB update, event emit) before the next settle.\n *\n * `turns` and `outcome` are passed through from the underlying negotiation\n * graph so consumers can build per-candidate decision-question inputs without\n * re-walking trace events or DB artifacts. Both are present on every\n * resolution (accepted, rejected, stalled, error); error paths receive a\n * synthesized `outcome` with `hasOpportunity: false`.\n */\nexport type OnNegotiationResolved = (entry: {\n candidate: NegotiationCandidate;\n accepted: NegotiationResult | null;\n turns: NegotiationTurn[];\n outcome: NegotiationOutcome;\n}) => Promise<void>;\n\n/**\n * Runs bilateral negotiation for each candidate in parallel.\n * @returns Only candidates that produced an opportunity\n */\nexport async function negotiateCandidates(\n negotiationGraph: NegotiationGraphLike,\n sourceUser: UserNegotiationContext,\n candidates: NegotiationCandidate[],\n indexContext: { networkId: string; prompt: string },\n opts?: {\n maxTurns?: number;\n traceEmitter?: TraceEmitter;\n indexContextOverrides?: Map<string, string>;\n timeoutMs?: number;\n onCandidateResolved?: OnNegotiationResolved;\n trigger?: \"orchestrator\" | \"ambient\";\n },\n): Promise<NegotiationResult[]> {\n const { maxTurns, traceEmitter, indexContextOverrides, timeoutMs, onCandidateResolved, trigger } = opts ?? {};\n\n // Local helper to emit events whose shape is wider than the declared\n // `TraceEmitter` union (mirrors the cast used in chat.agent at the relay sink\n // and inside turn/finalize nodes above).\n const emitWide = (event: Record<string, unknown>) =>\n (traceEmitter as ((e: Record<string, unknown>) => void) | undefined)?.(event);\n\n const results = await Promise.all(\n candidates.map(async (candidate) => {\n const start = Date.now();\n if (candidate.opportunityId) {\n const candidateName = candidate.candidateUser?.profile?.name;\n emitWide({\n type: \"negotiation_session_start\",\n opportunityId: candidate.opportunityId,\n negotiationConversationId: \"\", // filled in on session_end\n sourceUserId: sourceUser.id,\n candidateUserId: candidate.userId,\n ...(candidateName && { candidateName }),\n trigger: trigger ?? \"ambient\",\n startedAt: start,\n });\n }\n traceEmitter?.({ type: \"agent_start\", name: \"Negotiating candidate\" });\n\n try {\n const candidateIndexContext = candidate.networkId\n ? { networkId: candidate.networkId, prompt: indexContextOverrides?.get(candidate.networkId) ?? '' }\n : indexContext;\n\n const result = await invokeWithAbortSignal(negotiationGraph, {\n sourceUser,\n candidateUser: candidate.candidateUser,\n indexContext: candidateIndexContext,\n seedAssessment: {\n reasoning: candidate.reasoning,\n valencyRole: candidate.valencyRole,\n },\n ...(candidate.discoveryQuery && { discoveryQuery: candidate.discoveryQuery }),\n ...(candidate.opportunityId && { opportunityId: candidate.opportunityId }),\n ...(maxTurns !== undefined && { maxTurns }),\n ...(timeoutMs !== undefined && { timeoutMs }),\n });\n\n const durationMs = Date.now() - start;\n const outcome = result.outcome;\n const hasOpportunity = outcome?.hasOpportunity === true;\n const isContinuation = (result as { isContinuation?: boolean }).isContinuation ?? false;\n const priorTurnCount = (result as { priorTurnCount?: number }).priorTurnCount ?? 0;\n\n const turnFlow = (result.messages ?? [])\n .map((m) => {\n const dataPart = (m.parts as Array<{ kind?: string; data?: Record<string, unknown> }>)?.find((p) => p.kind === \"data\");\n if (!dataPart?.data) return null;\n const turn = dataPart.data as { action?: string };\n return turn.action ?? \"unknown\";\n })\n .filter(Boolean)\n .join(\" → \");\n\n const statusTag = hasOpportunity ? \"✓ opportunity\" : \"✗ rejected\";\n traceEmitter?.({ type: \"agent_end\", name: \"Negotiating candidate\", durationMs, summary: `${candidate.userId}: ${turnFlow} ${statusTag}` });\n\n if (candidate.opportunityId) {\n emitWide({\n type: \"negotiation_session_end\",\n opportunityId: candidate.opportunityId,\n negotiationConversationId: (result as { conversationId?: string }).conversationId ?? \"\",\n durationMs: Date.now() - start,\n isContinuation,\n turnsAdded: outcome?.turnCount ?? 0,\n priorTurnCount,\n });\n }\n\n const accepted: NegotiationResult | null = hasOpportunity && outcome\n ? {\n userId: candidate.userId,\n agreedRoles: outcome.agreedRoles,\n reasoning: outcome.reasoning,\n turnCount: outcome.turnCount,\n }\n : null;\n\n if (onCandidateResolved) {\n const turnHistory: NegotiationTurn[] = (result.messages ?? [])\n .map((m) => {\n const dataPart = (m.parts as Array<{ kind?: string; data?: unknown }>)?.find(\n (p) => p.kind === \"data\",\n );\n return dataPart?.data as NegotiationTurn | undefined;\n })\n .filter((t): t is NegotiationTurn => !!t);\n const resolvedOutcome: NegotiationOutcome = result.outcome ?? {\n hasOpportunity: false,\n agreedRoles: [],\n reasoning: \"no outcome returned by negotiation graph\",\n turnCount: turnHistory.length,\n };\n try {\n await onCandidateResolved({\n candidate,\n accepted,\n turns: turnHistory,\n outcome: resolvedOutcome,\n });\n } catch (hookErr) {\n // Hook failures must not sink the candidate result — the aggregate\n // return is still useful, and the orchestrator branch logs its own\n // failures inline.\n logger.error(\"[negotiateCandidates] onCandidateResolved hook threw\", {\n candidateUserId: candidate.userId,\n error: hookErr,\n });\n }\n }\n\n return accepted;\n } catch (err) {\n const durationMs = Date.now() - start;\n traceEmitter?.({ type: \"agent_end\", name: \"Negotiating candidate\", durationMs, summary: `${candidate.userId}: error` });\n if (candidate.opportunityId) {\n emitWide({\n type: \"negotiation_session_end\",\n opportunityId: candidate.opportunityId,\n negotiationConversationId: \"\",\n durationMs: Date.now() - start,\n });\n }\n logger.error(\"[negotiateCandidates] Negotiation failed\", { candidateUserId: candidate.userId, error: err });\n if (onCandidateResolved) {\n try {\n await onCandidateResolved({\n candidate,\n accepted: null,\n turns: [],\n outcome: {\n hasOpportunity: false,\n agreedRoles: [],\n reasoning: err instanceof Error ? err.message : String(err),\n turnCount: 0,\n },\n });\n } catch {\n // ignore hook failure on error path\n }\n }\n return null;\n }\n }),\n );\n\n return results.filter((r): r is NegotiationResult => r !== null);\n}\n\n/**\n * Creates a negotiation graph with the provided dependencies.\n */\nexport function createDefaultNegotiationGraph(deps: {\n database: NegotiationGraphDatabase;\n dispatcher: AgentDispatcher;\n timeoutQueue?: NegotiationTimeoutQueue;\n}) {\n const factory = new NegotiationGraphFactory(deps.database, deps.dispatcher, deps.timeoutQueue);\n return factory.createGraph();\n}\n"]}
1
+ {"version":3,"file":"negotiation.graph.js","sourceRoot":"/","sources":["negotiation/negotiation.graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAElD,OAAO,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAC;AACxE,OAAO,EAAE,cAAc,EAAqB,MAAM,4CAA4C,CAAC;AAI/F,OAAO,EAAE,qBAAqB,EAA8H,MAAM,wBAAwB,CAAC;AAC3L,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,4CAA4C,CAAC;AAG5E,MAAM,MAAM,GAAG,cAAc,CAAC,kBAAkB,CAAC,CAAC;AAElD,6EAA6E;AAC7E,SAAS,iBAAiB,CAAC,QAAqC;IAC9D,OAAO,QAAQ;SACZ,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;QACT,MAAM,QAAQ,GAAI,CAAC,CAAC,KAAkD,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC;QACtG,OAAO,QAAQ,EAAE,IAAuB,CAAC;IAC3C,CAAC,CAAC;SACD,MAAM,CAAC,OAAO,CAAC,CAAC;AACrB,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,uBAAuB;IAClC,YACU,QAAkC,EAClC,UAA2B,EAC3B,YAAsC,EACtC,iBAAuC;QAHvC,aAAQ,GAAR,QAAQ,CAA0B;QAClC,eAAU,GAAV,UAAU,CAAiB;QAC3B,iBAAY,GAAZ,YAAY,CAA0B;QACtC,sBAAiB,GAAjB,iBAAiB,CAAsB;IAC9C,CAAC;IAEJ,WAAW;QACT,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,YAAY,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC;QACvE,MAAM,WAAW,GAAG,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H,MAAM,UAAU,6BAA6B,CAAC,IAI7C;IACC,MAAM,OAAO,GAAG,IAAI,uBAAuB,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC;IAC/F,OAAO,OAAO,CAAC,WAAW,EAAE,CAAC;AAC/B,CAAC","sourcesContent":["import { StateGraph } from \"@langchain/langgraph\";\n\nimport { invokeWithAbortSignal } from \"../shared/agent/model-signal.js\";\nimport { requestContext, type TraceEmitter } from \"../shared/observability/request-context.js\";\nimport type { NegotiationGraphDatabase } from \"../shared/interfaces/database.interface.js\";\nimport type { NegotiationTimeoutQueue } from \"../shared/interfaces/negotiation-events.interface.js\";\nimport type { AgentDispatcher, NegotiationTurnPayload } from \"../shared/interfaces/agent-dispatcher.interface.js\";\nimport { NegotiationGraphState, type NegotiationTurn, type NegotiationOutcome, type UserNegotiationContext, type SeedAssessment, type NegotiationGraphLike } from \"./negotiation.state.js\";\nimport { IndexNegotiator } from \"./negotiation.agent.js\";\nimport { protocolLogger } from \"../shared/observability/protocol.logger.js\";\nimport type { QuestionerEnqueueFn } from \"../questioner/questioner.types.js\";\n\nconst logger = protocolLogger(\"NegotiationGraph\");\n\n/** Extracts the ordered NegotiationTurn list from A2A message data parts. */\nfunction turnsFromMessages(messages: Array<{ parts: unknown[] }>): NegotiationTurn[] {\n return messages\n .map((m) => {\n const dataPart = (m.parts as Array<{ kind?: string; data?: unknown }>).find((p) => p.kind === \"data\");\n return dataPart?.data as NegotiationTurn;\n })\n .filter(Boolean);\n}\n\n/**\n * Factory for the bilateral negotiation LangGraph state machine.\n * @remarks Accepts an AgentDispatcher for per-turn agent resolution.\n */\nexport class NegotiationGraphFactory {\n constructor(\n private database: NegotiationGraphDatabase,\n private dispatcher: AgentDispatcher,\n private timeoutQueue?: NegotiationTimeoutQueue,\n private questionerEnqueue?: QuestionerEnqueueFn,\n ) {}\n\n createGraph() {\n const { database, dispatcher, timeoutQueue, questionerEnqueue } = this;\n const systemAgent = new IndexNegotiator();\n\n const initNode = async (state: typeof NegotiationGraphState.State) => {\n try {\n // Find-or-create the DM conversation for this agent pair (same as user DMs)\n const agentIdA = `agent:${state.sourceUser.id}`;\n const agentIdB = `agent:${state.candidateUser.id}`;\n const conversation = await database.getOrCreateDM(agentIdA, agentIdB, 'agent');\n\n // --- Lock gate: check for an active task on this conversation ---\n const priorMessages = await database.getMessagesForConversation(conversation.id);\n\n let isLocked = false;\n if (state.opportunityId) {\n const priorTask = await database.getNegotiationTaskForOpportunity(state.opportunityId);\n if (priorTask) {\n const activeStates = ['submitted', 'working', 'input_required', 'waiting_for_agent', 'claimed'];\n const isFresh = (Date.now() - new Date(priorTask.updatedAt).getTime()) < 5 * 60 * 1000;\n if (activeStates.includes(priorTask.state) && isFresh) {\n isLocked = true;\n }\n }\n }\n\n if (isLocked) {\n logger.info('[Graph:Init] Conversation locked by active task, returning busy', {\n conversationId: conversation.id,\n opportunityId: state.opportunityId,\n });\n return { error: 'busy' };\n }\n\n // --- Load prior messages and determine continuation ---\n const priorTurns: NegotiationTurn[] = turnsFromMessages(priorMessages);\n\n const isContinuation = priorTurns.length > 0;\n\n // Determine currentSpeaker from last prior message\n let currentSpeaker: 'source' | 'candidate' = 'source';\n if (isContinuation && priorMessages.length > 0) {\n const lastSender = priorMessages[priorMessages.length - 1].senderId;\n currentSpeaker = lastSender === agentIdA ? 'candidate' : 'source';\n }\n\n // Determine scenario-based maxTurns\n const scope = { action: 'manage:negotiations', scopeType: 'network', scopeId: state.indexContext.networkId };\n const [sourceHasAgent, candidateHasAgent] = await Promise.all([\n dispatcher.hasPersonalAgent(state.sourceUser.id, scope),\n dispatcher.hasPersonalAgent(state.candidateUser.id, scope),\n ]);\n\n const ambientMax = Number(process.env.NEGOTIATION_MAX_TURNS_AMBIENT) || 6;\n let maxTurns = state.maxTurns;\n if (maxTurns == null) {\n maxTurns = (sourceHasAgent && candidateHasAgent) ? 0 : ambientMax;\n }\n\n const task = await database.createTask(conversation.id, {\n type: 'negotiation',\n sourceUserId: state.sourceUser.id,\n candidateUserId: state.candidateUser.id,\n networkId: state.indexContext.networkId,\n ...(state.opportunityId && { opportunityId: state.opportunityId }),\n maxTurns,\n isContinuation,\n priorTurnCount: priorTurns.length,\n });\n\n if (state.opportunityId) {\n await database.updateOpportunityStatus(state.opportunityId, 'negotiating').catch((err) => {\n logger.error('[Graph:Init] Failed to set opportunity status to negotiating', { opportunityId: state.opportunityId, error: err });\n });\n }\n\n // Load user answers collected by the questioner between sessions\n const userAnswers = (isContinuation && state.opportunityId)\n ? await database.getOpportunityUserAnswers(state.opportunityId).catch((err) => {\n logger.error('[Graph:Init] Failed to load user answers', { opportunityId: state.opportunityId, error: err });\n return [];\n })\n : [];\n\n // Seed messages with prior turns (additive reducer appends new turns on top)\n const seedMessages = isContinuation ? priorMessages.map((m) => ({\n id: m.id,\n senderId: m.senderId,\n role: 'agent' as const,\n parts: m.parts,\n createdAt: m.createdAt,\n })) : [];\n\n return {\n conversationId: conversation.id,\n taskId: task.id,\n currentSpeaker,\n turnCount: 0,\n maxTurns,\n isContinuation,\n priorTurnCount: priorTurns.length,\n ...(userAnswers.length > 0 && { userAnswers }),\n ...(seedMessages.length > 0 && { messages: seedMessages }),\n };\n } catch (err) {\n return { error: `Init failed: ${err instanceof Error ? err.message : String(err)}` };\n }\n };\n\n const turnNode = async (state: typeof NegotiationGraphState.State) => {\n const traceEmitter = requestContext.getStore()?.traceEmitter;\n // Local helper to emit events whose shape is wider than the declared\n // `TraceEmitter` union. The chat agent already casts at its relay sink;\n // here we localize the cast at the callsite so the rest of the body stays typed.\n const emitWide = (event: Record<string, unknown>) =>\n (traceEmitter as ((e: Record<string, unknown>) => void) | undefined)?.(event);\n const agentName = \"Index negotiator\";\n const agentStart = Date.now();\n traceEmitter?.({ type: \"agent_start\", name: agentName });\n\n try {\n const history: NegotiationTurn[] = turnsFromMessages(state.messages);\n\n const isSource = state.currentSpeaker === \"source\";\n const ownUser = isSource ? state.sourceUser : state.candidateUser;\n const otherUser = isSource ? state.candidateUser : state.sourceUser;\n\n // Determine if this is the system agent's final allowed turn\n const maxTurns = state.maxTurns ?? 0;\n const isFinalTurn = maxTurns > 0 && (state.turnCount + 1) >= maxTurns;\n\n const payload: NegotiationTurnPayload = {\n negotiationId: state.taskId,\n ownUser,\n otherUser,\n indexContext: state.indexContext,\n seedAssessment: state.seedAssessment,\n history,\n isFinalTurn,\n isDiscoverer: isSource,\n ...(state.discoveryQuery && isSource && { discoveryQuery: state.discoveryQuery }),\n };\n\n const scope = { action: 'manage:negotiations', scopeType: 'network', scopeId: state.indexContext.networkId };\n\n const dispatchResult = await dispatcher.dispatch(ownUser.id, scope, payload, { timeoutMs: state.timeoutMs });\n\n let turn: NegotiationTurn;\n\n if (dispatchResult.handled) {\n // Personal agent responded\n turn = dispatchResult.turn;\n } else if (dispatchResult.reason === 'waiting') {\n // Long timeout — graph suspends. Persist the full turn context so the\n // polling agent (and MCP consumers via get_negotiation) reconstruct\n // the same view the in-process system agent would see. The view is\n // stored in absolute source/candidate terms; perspective is projected\n // at pickup time using the claiming user's id.\n traceEmitter?.({ type: \"agent_end\", name: agentName, durationMs: Date.now() - agentStart, summary: \"waiting_for_agent\" });\n await database.setTaskTurnContext(state.taskId, {\n sourceUser: state.sourceUser,\n candidateUser: state.candidateUser,\n indexContext: state.indexContext,\n seedAssessment: state.seedAssessment,\n // Keep discoveryQuery speaker-scoped: include it only when the\n // parked turn belongs to the discoverer (source). Persisting it on\n // candidate-side turns would make the pickup prompt frame the\n // search as \"your user searched for X\" for the wrong user.\n ...(isSource && state.discoveryQuery && { discoveryQuery: state.discoveryQuery }),\n });\n await database.updateTaskState(state.taskId, \"waiting_for_agent\");\n return { status: 'waiting_for_agent' as const };\n } else {\n // No personal agent or timeout — run system agent\n turn = await systemAgent.invoke({\n ownUser,\n otherUser,\n indexContext: state.indexContext,\n seedAssessment: state.seedAssessment,\n history,\n isFinalTurn,\n isDiscoverer: isSource,\n ...(state.discoveryQuery && isSource && { discoveryQuery: state.discoveryQuery }),\n isContinuation: state.isContinuation,\n ...(state.userAnswers.length > 0 && { userAnswers: state.userAnswers }),\n });\n }\n\n traceEmitter?.({ type: \"agent_end\", name: agentName, durationMs: Date.now() - agentStart, summary: `${turn.action}` });\n\n // First turn must be \"propose\" (unless continuing a prior conversation)\n if (state.turnCount === 0 && !state.isContinuation && turn.action !== \"propose\") {\n logger.warn(\"[Graph:Turn] Agent returned unexpected action on turn 0, forcing to propose\", { action: turn.action });\n turn.action = \"propose\";\n }\n\n const parts = [{ kind: \"data\" as const, data: turn }];\n const message = await database.createMessage({\n conversationId: state.conversationId,\n senderId: `agent:${ownUser.id}`,\n role: \"agent\",\n parts,\n taskId: state.taskId,\n });\n\n await database.updateTaskState(state.taskId, \"working\");\n\n if (state.opportunityId) {\n emitWide({\n type: \"negotiation_turn\",\n opportunityId: state.opportunityId,\n negotiationConversationId: state.conversationId,\n turnIndex: state.turnCount,\n actor: isSource ? \"source\" : \"candidate\",\n action: turn.action,\n ...(turn.assessment?.reasoning && { reasoning: turn.assessment.reasoning }),\n ...(turn.message && { message: turn.message }),\n ...(turn.assessment?.suggestedRoles && { suggestedRoles: turn.assessment.suggestedRoles }),\n durationMs: Date.now() - agentStart,\n });\n }\n\n return {\n messages: [{\n id: message.id,\n senderId: message.senderId,\n role: \"agent\" as const,\n parts: message.parts,\n createdAt: message.createdAt,\n }],\n turnCount: state.turnCount + 1,\n currentSpeaker: (isSource ? \"candidate\" : \"source\") as \"source\" | \"candidate\",\n lastTurn: turn,\n };\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n logger.error(\"[Graph:Turn] Agent invocation failed\", { error: errMsg, stack: err instanceof Error ? err.stack : undefined, turnCount: state.turnCount });\n traceEmitter?.({ type: \"agent_end\", name: agentName, durationMs: Date.now() - agentStart, summary: `error: ${errMsg}` });\n return {\n lastTurn: {\n action: \"reject\" as const,\n assessment: { reasoning: `Agent error: ${errMsg}`, suggestedRoles: { ownUser: \"peer\" as const, otherUser: \"peer\" as const } },\n },\n turnCount: state.turnCount + 1,\n error: `Turn failed: ${errMsg}`,\n };\n }\n };\n\n const evaluateNode = (state: typeof NegotiationGraphState.State): string => {\n if (state.status === 'waiting_for_agent') return \"finalize\";\n if (state.error) return \"finalize\";\n if (!state.lastTurn) return \"finalize\";\n if (state.lastTurn.action === \"accept\") return \"finalize\";\n if (state.lastTurn.action === \"reject\") return \"finalize\";\n // question routes same as counter — next turn\n if ((state.maxTurns ?? 0) > 0 && state.turnCount >= state.maxTurns!) return \"finalize\";\n return \"turn\";\n };\n\n const finalizeNode = async (state: typeof NegotiationGraphState.State) => {\n const traceEmitter = requestContext.getStore()?.traceEmitter;\n const emitWide = (event: Record<string, unknown>) =>\n (traceEmitter as ((e: Record<string, unknown>) => void) | undefined)?.(event);\n\n if (state.status === 'waiting_for_agent') {\n if (state.opportunityId) {\n emitWide({\n type: \"negotiation_outcome\",\n opportunityId: state.opportunityId,\n outcome: \"waiting_for_agent\",\n turnCount: state.turnCount,\n isContinuation: state.isContinuation,\n });\n }\n return {};\n }\n\n const history: NegotiationTurn[] = turnsFromMessages(state.messages);\n\n const lastTurn = state.lastTurn;\n const hasOpportunity = lastTurn?.action === \"accept\";\n const atCap = (state.maxTurns ?? 0) > 0 && state.turnCount >= state.maxTurns! && lastTurn?.action !== \"accept\" && lastTurn?.action !== \"reject\";\n\n let agreedRoles: NegotiationOutcome[\"agreedRoles\"] = [];\n if (hasOpportunity && history.length >= 2) {\n const acceptTurn = history[history.length - 1];\n const precedingTurn = history[history.length - 2];\n const accepterIsSource = state.currentSpeaker === \"candidate\";\n const [sourceRole, candidateRole] = accepterIsSource\n ? [acceptTurn.assessment.suggestedRoles.ownUser, precedingTurn.assessment.suggestedRoles.ownUser]\n : [precedingTurn.assessment.suggestedRoles.ownUser, acceptTurn.assessment.suggestedRoles.ownUser];\n agreedRoles = [\n { userId: state.sourceUser.id, role: sourceRole },\n { userId: state.candidateUser.id, role: candidateRole },\n ];\n }\n\n const outcome: NegotiationOutcome = {\n hasOpportunity,\n agreedRoles,\n reasoning: lastTurn?.assessment.reasoning ?? \"\",\n turnCount: state.turnCount,\n ...(atCap && { reason: \"turn_cap\" as const }),\n };\n\n try {\n await database.updateTaskState(state.taskId, \"completed\");\n await database.createArtifact({\n taskId: state.taskId,\n name: \"negotiation-outcome\",\n parts: [{ kind: \"data\", data: outcome }],\n metadata: { hasOpportunity, turnCount: state.turnCount },\n });\n\n logger.info('[Graph:Finalize] Session complete', {\n conversationId: state.conversationId,\n taskId: state.taskId,\n isContinuation: state.isContinuation,\n turnsAdded: state.turnCount,\n priorTurnCount: state.priorTurnCount,\n outcome: hasOpportunity ? 'accepted' : (atCap ? 'turn_cap' : (lastTurn?.action ?? 'unknown')),\n opportunityId: state.opportunityId || undefined,\n });\n\n if (state.opportunityId) {\n const nextStatus = lastTurn?.action === 'accept'\n ? 'pending'\n : lastTurn?.action === 'reject'\n ? 'rejected'\n : 'stalled';\n await database.updateOpportunityStatus(state.opportunityId, nextStatus).catch((err) => {\n logger.error(\"[Graph:Finalize] Failed to update opportunity status\", { opportunityId: state.opportunityId, nextStatus, error: err });\n });\n }\n } catch (err) {\n logger.error(\"[Graph:Finalize] Failed to persist outcome\", { error: err });\n }\n\n if (state.opportunityId) {\n const emittedOutcome: \"accepted\" | \"rejected_stalled\" | \"turn_cap\" | \"timed_out\" =\n hasOpportunity\n ? \"accepted\"\n : atCap\n ? \"turn_cap\"\n : state.error && /timeout/i.test(state.error)\n ? \"timed_out\"\n : lastTurn?.action === \"reject\"\n ? \"rejected_stalled\"\n : \"rejected_stalled\";\n\n emitWide({\n type: \"negotiation_outcome\",\n opportunityId: state.opportunityId,\n outcome: emittedOutcome,\n turnCount: state.turnCount,\n isContinuation: state.isContinuation,\n turnsAdded: state.turnCount,\n priorTurnCount: state.priorTurnCount,\n ...(outcome.reasoning && { reasoning: outcome.reasoning }),\n ...(hasOpportunity && agreedRoles.length >= 2 && {\n agreedRoles: {\n ownUser: agreedRoles[0]?.role,\n otherUser: agreedRoles[1]?.role,\n },\n }),\n });\n }\n\n // Enqueue question generation for stalled/capped negotiations (not accepted or explicitly rejected).\n // Require turnCount > 0 so early init/turn errors don't enqueue with empty context.\n if (!hasOpportunity && lastTurn?.action !== 'reject' && state.turnCount > 0 && state.opportunityId && questionerEnqueue) {\n const stallReason: 'turn_cap' | 'timeout' | 'stalled' = atCap\n ? 'turn_cap'\n : (state.error && /timeout/i.test(state.error))\n ? 'timeout'\n : 'stalled';\n\n questionerEnqueue({\n mode: 'negotiation',\n userId: state.sourceUser.id,\n sourceType: 'opportunity',\n sourceId: state.opportunityId,\n context: {\n negotiationId: state.taskId,\n counterpartyHint: `${state.candidateUser.profile.name ?? 'Unknown'}${state.candidateUser.profile.bio ? ', ' + state.candidateUser.profile.bio : ''}`,\n indexContext: state.indexContext.prompt,\n outcomeReason: stallReason,\n keyTake: outcome.reasoning,\n userProfile: state.sourceUser.profile,\n },\n }).catch((err) =>\n logger.error('[Graph:Finalize] Failed to enqueue negotiation question generation', {\n opportunityId: state.opportunityId,\n error: err,\n })\n );\n }\n\n return { outcome, status: 'completed' as const };\n };\n\n const workflow = new StateGraph(NegotiationGraphState)\n .addNode(\"init\", initNode)\n .addNode(\"turn\", turnNode)\n .addNode(\"finalize\", finalizeNode)\n .addConditionalEdges(\"turn\", evaluateNode, {\n turn: \"turn\",\n finalize: \"finalize\",\n })\n .addConditionalEdges(\"init\", (state: typeof NegotiationGraphState.State) => {\n return state.error ? \"finalize\" : \"turn\";\n }, { turn: \"turn\", finalize: \"finalize\" })\n .addEdge(\"__start__\", \"init\")\n .addEdge(\"finalize\", \"__end__\");\n\n return workflow.compile();\n }\n}\n\nexport interface NegotiationCandidate {\n userId: string;\n reasoning: string;\n valencyRole: string;\n networkId?: string;\n candidateUser: UserNegotiationContext;\n /** The explicit search query that triggered discovery (if any). */\n discoveryQuery?: string;\n /**\n * ID of the opportunity this negotiation is for. When set, the negotiation\n * graph's finalize node updates the opportunity's status based on the outcome\n * (`accept` → 'pending', `reject` → 'rejected', otherwise → 'stalled').\n */\n opportunityId?: string;\n}\n\nexport interface NegotiationResult {\n userId: string;\n agreedRoles: NegotiationOutcome[\"agreedRoles\"];\n reasoning: string;\n turnCount: number;\n}\n\n/**\n * Per-candidate resolution hook — fires as each negotiation settles, before\n * Promise.all aggregates. Used by the orchestrator branch to progressively\n * stream `opportunity_draft_ready` events as each candidate resolves, rather\n * than emitting all at once after the full fan-out completes. Awaited so the\n * caller can run async work (DB update, event emit) before the next settle.\n *\n * `turns` and `outcome` are passed through from the underlying negotiation\n * graph so consumers can build per-candidate decision-question inputs without\n * re-walking trace events or DB artifacts. Both are present on every\n * resolution (accepted, rejected, stalled, error); error paths receive a\n * synthesized `outcome` with `hasOpportunity: false`.\n */\nexport type OnNegotiationResolved = (entry: {\n candidate: NegotiationCandidate;\n accepted: NegotiationResult | null;\n turns: NegotiationTurn[];\n outcome: NegotiationOutcome;\n}) => Promise<void>;\n\n/**\n * Runs bilateral negotiation for each candidate in parallel.\n * @returns Only candidates that produced an opportunity\n */\nexport async function negotiateCandidates(\n negotiationGraph: NegotiationGraphLike,\n sourceUser: UserNegotiationContext,\n candidates: NegotiationCandidate[],\n indexContext: { networkId: string; prompt: string },\n opts?: {\n maxTurns?: number;\n traceEmitter?: TraceEmitter;\n indexContextOverrides?: Map<string, string>;\n timeoutMs?: number;\n onCandidateResolved?: OnNegotiationResolved;\n trigger?: \"orchestrator\" | \"ambient\";\n },\n): Promise<NegotiationResult[]> {\n const { maxTurns, traceEmitter, indexContextOverrides, timeoutMs, onCandidateResolved, trigger } = opts ?? {};\n\n // Local helper to emit events whose shape is wider than the declared\n // `TraceEmitter` union (mirrors the cast used in chat.agent at the relay sink\n // and inside turn/finalize nodes above).\n const emitWide = (event: Record<string, unknown>) =>\n (traceEmitter as ((e: Record<string, unknown>) => void) | undefined)?.(event);\n\n const results = await Promise.all(\n candidates.map(async (candidate) => {\n const start = Date.now();\n if (candidate.opportunityId) {\n const candidateName = candidate.candidateUser?.profile?.name;\n emitWide({\n type: \"negotiation_session_start\",\n opportunityId: candidate.opportunityId,\n negotiationConversationId: \"\", // filled in on session_end\n sourceUserId: sourceUser.id,\n candidateUserId: candidate.userId,\n ...(candidateName && { candidateName }),\n trigger: trigger ?? \"ambient\",\n startedAt: start,\n });\n }\n traceEmitter?.({ type: \"agent_start\", name: \"Negotiating candidate\" });\n\n try {\n const candidateIndexContext = candidate.networkId\n ? { networkId: candidate.networkId, prompt: indexContextOverrides?.get(candidate.networkId) ?? '' }\n : indexContext;\n\n const result = await invokeWithAbortSignal(negotiationGraph, {\n sourceUser,\n candidateUser: candidate.candidateUser,\n indexContext: candidateIndexContext,\n seedAssessment: {\n reasoning: candidate.reasoning,\n valencyRole: candidate.valencyRole,\n },\n ...(candidate.discoveryQuery && { discoveryQuery: candidate.discoveryQuery }),\n ...(candidate.opportunityId && { opportunityId: candidate.opportunityId }),\n ...(maxTurns !== undefined && { maxTurns }),\n ...(timeoutMs !== undefined && { timeoutMs }),\n });\n\n const durationMs = Date.now() - start;\n const outcome = result.outcome;\n const hasOpportunity = outcome?.hasOpportunity === true;\n const isContinuation = (result as { isContinuation?: boolean }).isContinuation ?? false;\n const priorTurnCount = (result as { priorTurnCount?: number }).priorTurnCount ?? 0;\n\n const turnFlow = (result.messages ?? [])\n .map((m) => {\n const dataPart = (m.parts as Array<{ kind?: string; data?: Record<string, unknown> }>)?.find((p) => p.kind === \"data\");\n if (!dataPart?.data) return null;\n const turn = dataPart.data as { action?: string };\n return turn.action ?? \"unknown\";\n })\n .filter(Boolean)\n .join(\" → \");\n\n const statusTag = hasOpportunity ? \"✓ opportunity\" : \"✗ rejected\";\n traceEmitter?.({ type: \"agent_end\", name: \"Negotiating candidate\", durationMs, summary: `${candidate.userId}: ${turnFlow} ${statusTag}` });\n\n if (candidate.opportunityId) {\n emitWide({\n type: \"negotiation_session_end\",\n opportunityId: candidate.opportunityId,\n negotiationConversationId: (result as { conversationId?: string }).conversationId ?? \"\",\n durationMs: Date.now() - start,\n isContinuation,\n turnsAdded: outcome?.turnCount ?? 0,\n priorTurnCount,\n });\n }\n\n const accepted: NegotiationResult | null = hasOpportunity && outcome\n ? {\n userId: candidate.userId,\n agreedRoles: outcome.agreedRoles,\n reasoning: outcome.reasoning,\n turnCount: outcome.turnCount,\n }\n : null;\n\n if (onCandidateResolved) {\n const turnHistory: NegotiationTurn[] = turnsFromMessages(result.messages ?? []);\n const resolvedOutcome: NegotiationOutcome = result.outcome ?? {\n hasOpportunity: false,\n agreedRoles: [],\n reasoning: \"no outcome returned by negotiation graph\",\n turnCount: turnHistory.length,\n };\n try {\n await onCandidateResolved({\n candidate,\n accepted,\n turns: turnHistory,\n outcome: resolvedOutcome,\n });\n } catch (hookErr) {\n // Hook failures must not sink the candidate result — the aggregate\n // return is still useful, and the orchestrator branch logs its own\n // failures inline.\n logger.error(\"[negotiateCandidates] onCandidateResolved hook threw\", {\n candidateUserId: candidate.userId,\n error: hookErr,\n });\n }\n }\n\n return accepted;\n } catch (err) {\n const durationMs = Date.now() - start;\n traceEmitter?.({ type: \"agent_end\", name: \"Negotiating candidate\", durationMs, summary: `${candidate.userId}: error` });\n if (candidate.opportunityId) {\n emitWide({\n type: \"negotiation_session_end\",\n opportunityId: candidate.opportunityId,\n negotiationConversationId: \"\",\n durationMs: Date.now() - start,\n });\n }\n logger.error(\"[negotiateCandidates] Negotiation failed\", { candidateUserId: candidate.userId, error: err });\n if (onCandidateResolved) {\n try {\n await onCandidateResolved({\n candidate,\n accepted: null,\n turns: [],\n outcome: {\n hasOpportunity: false,\n agreedRoles: [],\n reasoning: err instanceof Error ? err.message : String(err),\n turnCount: 0,\n },\n });\n } catch {\n // ignore hook failure on error path\n }\n }\n return null;\n }\n }),\n );\n\n return results.filter((r): r is NegotiationResult => r !== null);\n}\n\n/**\n * Creates a negotiation graph with the provided dependencies.\n */\nexport function createDefaultNegotiationGraph(deps: {\n database: NegotiationGraphDatabase;\n dispatcher: AgentDispatcher;\n timeoutQueue?: NegotiationTimeoutQueue;\n}) {\n const factory = new NegotiationGraphFactory(deps.database, deps.dispatcher, deps.timeoutQueue);\n return factory.createGraph();\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"negotiation.tools.d.ts","sourceRoot":"/","sources":["negotiation/negotiation.tools.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAC;AAS5E;;;;;;;;GAQG;AACH,eAAO,MAAM,sBAAsB,QAAgB,CAAC;AAuBpD;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,4BAiwB5E"}
1
+ {"version":3,"file":"negotiation.tools.d.ts","sourceRoot":"/","sources":["negotiation/negotiation.tools.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAC;AAS5E;;;;;;;;GAQG;AACH,eAAO,MAAM,sBAAsB,QAAgB,CAAC;AAiCpD;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,4BAwvB5E"}