@indexnetwork/protocol 3.6.3 → 3.6.4

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 (84) hide show
  1. package/README.md +90 -26
  2. package/dist/agent/tests/fakes.d.ts.map +1 -1
  3. package/dist/agent/tests/fakes.js.map +1 -1
  4. package/dist/chat/chat.agent.d.ts.map +1 -1
  5. package/dist/chat/chat.agent.js +3 -3
  6. package/dist/chat/chat.agent.js.map +1 -1
  7. package/dist/chat/chat.streamer.d.ts.map +1 -1
  8. package/dist/chat/chat.streamer.js +1 -1
  9. package/dist/chat/chat.streamer.js.map +1 -1
  10. package/dist/chat/chat.summarizer.d.ts.map +1 -1
  11. package/dist/chat/chat.summarizer.js +1 -1
  12. package/dist/chat/chat.summarizer.js.map +1 -1
  13. package/dist/chat/tests/chat.graph.mocks.d.ts.map +1 -1
  14. package/dist/chat/tests/chat.graph.mocks.js +0 -1
  15. package/dist/chat/tests/chat.graph.mocks.js.map +1 -1
  16. package/dist/index.d.ts +21 -21
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +10 -10
  19. package/dist/index.js.map +1 -1
  20. package/dist/intent/intent.graph.js +1 -1
  21. package/dist/intent/intent.graph.js.map +1 -1
  22. package/dist/maintenance/maintenance.graph.d.ts.map +1 -1
  23. package/dist/maintenance/maintenance.graph.js +1 -1
  24. package/dist/maintenance/maintenance.graph.js.map +1 -1
  25. package/dist/negotiation/negotiation.agent.d.ts.map +1 -1
  26. package/dist/negotiation/negotiation.agent.js +1 -1
  27. package/dist/negotiation/negotiation.agent.js.map +1 -1
  28. package/dist/negotiation/negotiation.summarizer.d.ts.map +1 -1
  29. package/dist/negotiation/negotiation.summarizer.js +1 -1
  30. package/dist/negotiation/negotiation.summarizer.js.map +1 -1
  31. package/dist/network/indexer/indexer.graph.d.ts.map +1 -1
  32. package/dist/network/indexer/indexer.graph.js +2 -2
  33. package/dist/network/indexer/indexer.graph.js.map +1 -1
  34. package/dist/opportunity/discovery-question.helper.d.ts.map +1 -1
  35. package/dist/opportunity/discovery-question.helper.js.map +1 -1
  36. package/dist/opportunity/feed/feed.graph.d.ts.map +1 -1
  37. package/dist/opportunity/feed/feed.graph.js +1 -1
  38. package/dist/opportunity/feed/feed.graph.js.map +1 -1
  39. package/dist/opportunity/negotiation-summary.builder.d.ts.map +1 -1
  40. package/dist/opportunity/negotiation-summary.builder.js.map +1 -1
  41. package/dist/opportunity/opportunity.discover.d.ts.map +1 -1
  42. package/dist/opportunity/opportunity.discover.js +1 -1
  43. package/dist/opportunity/opportunity.discover.js.map +1 -1
  44. package/dist/opportunity/opportunity.enricher.d.ts.map +1 -1
  45. package/dist/opportunity/opportunity.enricher.js.map +1 -1
  46. package/dist/opportunity/opportunity.graph.d.ts.map +1 -1
  47. package/dist/opportunity/opportunity.graph.js +5 -5
  48. package/dist/opportunity/opportunity.graph.js.map +1 -1
  49. package/dist/opportunity/opportunity.persist.d.ts.map +1 -1
  50. package/dist/opportunity/opportunity.persist.js.map +1 -1
  51. package/dist/opportunity/opportunity.presentation.d.ts +9 -0
  52. package/dist/opportunity/opportunity.presentation.d.ts.map +1 -1
  53. package/dist/opportunity/opportunity.presentation.js +23 -0
  54. package/dist/opportunity/opportunity.presentation.js.map +1 -1
  55. package/dist/opportunity/opportunity.presenter.d.ts.map +1 -1
  56. package/dist/opportunity/opportunity.presenter.js +9 -3
  57. package/dist/opportunity/opportunity.presenter.js.map +1 -1
  58. package/dist/opportunity/question.generator.d.ts.map +1 -1
  59. package/dist/opportunity/question.generator.js +2 -2
  60. package/dist/opportunity/question.generator.js.map +1 -1
  61. package/dist/premise/premise.graph.d.ts.map +1 -1
  62. package/dist/premise/premise.graph.js +1 -1
  63. package/dist/premise/premise.graph.js.map +1 -1
  64. package/dist/profile/profile.graph.js +1 -1
  65. package/dist/profile/profile.graph.js.map +1 -1
  66. package/dist/questioner/questioner.agent.d.ts.map +1 -1
  67. package/dist/questioner/questioner.agent.js +1 -1
  68. package/dist/questioner/questioner.agent.js.map +1 -1
  69. package/dist/questioner/questioner.presets.d.ts.map +1 -1
  70. package/dist/questioner/questioner.presets.js +1 -1
  71. package/dist/questioner/questioner.presets.js.map +1 -1
  72. package/dist/shared/agent/response.streamer.d.ts.map +1 -1
  73. package/dist/shared/agent/response.streamer.js +1 -1
  74. package/dist/shared/agent/response.streamer.js.map +1 -1
  75. package/dist/shared/agent/tool.factory.d.ts.map +1 -1
  76. package/dist/shared/agent/tool.factory.js +1 -1
  77. package/dist/shared/agent/tool.factory.js.map +1 -1
  78. package/dist/shared/agent/tool.helpers.d.ts.map +1 -1
  79. package/dist/shared/agent/tool.helpers.js.map +1 -1
  80. package/dist/shared/assignment/network-assignment.policy.d.ts.map +1 -1
  81. package/dist/shared/assignment/network-assignment.policy.js.map +1 -1
  82. package/dist/shared/interfaces/questioner.interface.d.ts.map +1 -1
  83. package/dist/shared/interfaces/questioner.interface.js.map +1 -1
  84. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"negotiation-summary.builder.js","sourceRoot":"/","sources":["opportunity/negotiation-summary.builder.ts"],"names":[],"mappings":"AAkCA;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CAAC,CAAwB;IAC7D,MAAM,KAAK,GAAoB,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QACjD,MAAM,EAAE,CAAC,CAAC,MAAM;QAChB,SAAS,EAAE,CAAC,CAAC,UAAU,CAAC,SAAS;QACjC,cAAc,EAAE;YACd,OAAO,EAAE,CAAC,CAAC,UAAU,CAAC,cAAc,CAAC,OAA0B;YAC/D,SAAS,EAAE,CAAC,CAAC,UAAU,CAAC,cAAc,CAAC,SAA4B;SACpE;KACF,CAAC,CAAC,CAAC;IACJ,MAAM,OAAO,GAAqB;QAChC,cAAc,EAAE,CAAC,CAAC,OAAO,CAAC,cAAc;QACxC,SAAS,EAAE,CAAC,CAAC,OAAO,CAAC,SAAS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O,CAAC,WAAW,EAAE,CAAC;gBACzC,MAAM,GAAG,GAAG,IAAI,CAAC,IAAuB,CAAC;gBACzC,gBAAgB,CAAC,GAAG,CAAC,GAAG,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;YAC3D,CAAC;QACH,CAAC;aAAM,CAAC;YACN,kBAAkB,IAAI,CAAC,CAAC;YACxB,IAAI,CAAC,CAAC,OAAO,CAAC,MAAM,KAAK,UAAU,IAAI,CAAC,CAAC,OAAO,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;gBACtE,YAAY,IAAI,CAAC,CAAC;YACpB,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO;QACL,eAAe,EAAE,WAAW,CAAC,MAAM;QACnC,kBAAkB;QAClB,kBAAkB;QAClB,YAAY;QACZ,gBAAgB;KACjB,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Pure mappers from raw per-candidate negotiation data to the protocol's\n * `DiscoveryNegotiation` / `DiscoverySummary` shapes consumed by the question\n * generator. No DB access, no LLM — safe to import from anywhere.\n */\nimport type {\n NegotiationTurn,\n NegotiationOutcome,\n} from \"../negotiation/negotiation.state.js\";\nimport type {\n DiscoveryNegotiation,\n DiscoveryOutcome,\n DiscoverySummary,\n DiscoveryTurn,\n NegotiationRole,\n} from \"./question.prompt.js\";\n\n/**\n * The input shape collected by the opportunity graph's negotiate node for\n * each candidate that completed a negotiation attempt (accepted, rejected,\n * stalled, or errored).\n */\nexport interface NegotiationResolution {\n candidateUserId: string;\n /** Abstract profile slice for the LLM (e.g. \"AI infra founder, Berlin\"). */\n counterpartyHint: string;\n /** Network/community prompt for the negotiation. */\n indexContext: string;\n turns: NegotiationTurn[];\n outcome: NegotiationOutcome;\n /** Optional pre-negotiation evaluator score (0..1). */\n seedAssessmentScore?: number;\n}\n\n/**\n * Convert one negotiation resolution to `DiscoveryNegotiation`.\n *\n * @param r - The raw resolution from the negotiate node.\n * @returns A `DiscoveryNegotiation` ready for the question generator.\n */\nexport function toDiscoveryNegotiation(r: NegotiationResolution): DiscoveryNegotiation {\n const turns: DiscoveryTurn[] = r.turns.map((t) => ({\n action: t.action,\n reasoning: t.assessment.reasoning,\n suggestedRoles: {\n ownUser: t.assessment.suggestedRoles.ownUser as NegotiationRole,\n otherUser: t.assessment.suggestedRoles.otherUser as NegotiationRole,\n },\n }));\n const outcome: DiscoveryOutcome = {\n hasOpportunity: r.outcome.hasOpportunity,\n reasoning: r.outcome.reasoning,\n ...(r.outcome.hasOpportunity && r.outcome.agreedRoles.length > 0\n ? { agreedRoles: r.outcome.agreedRoles.map((a) => ({ userId: a.userId, role: a.role as NegotiationRole })) }\n : {}),\n ...(r.outcome.reason ? { reason: r.outcome.reason } : {}),\n };\n return {\n counterpartyId: r.candidateUserId,\n counterpartyHint: r.counterpartyHint,\n indexContext: r.indexContext,\n turns,\n outcome,\n ...(r.seedAssessmentScore !== undefined ? { seedAssessmentScore: r.seedAssessmentScore } : {}),\n };\n}\n\n/**\n * Aggregate counters across all negotiations in a single discovery turn.\n *\n * @param resolutions - All resolved negotiations from the negotiate node.\n * @returns A `DiscoverySummary` with totals and role distribution.\n */\nexport function buildDiscoverySummary(resolutions: NegotiationResolution[]): DiscoverySummary {\n const roleDistribution: Partial<Record<NegotiationRole, number>> = {};\n let opportunitiesFound = 0;\n let noOpportunityCount = 0;\n let timeoutCount = 0;\n\n for (const r of resolutions) {\n if (r.outcome.hasOpportunity) {\n opportunitiesFound += 1;\n for (const role of r.outcome.agreedRoles) {\n const key = role.role as NegotiationRole;\n roleDistribution[key] = (roleDistribution[key] ?? 0) + 1;\n }\n } else {\n noOpportunityCount += 1;\n if (r.outcome.reason === \"turn_cap\" || r.outcome.reason === \"timeout\") {\n timeoutCount += 1;\n }\n }\n }\n\n return {\n totalCandidates: resolutions.length,\n opportunitiesFound,\n noOpportunityCount,\n timeoutCount,\n roleDistribution,\n };\n}\n"]}
1
+ {"version":3,"file":"negotiation-summary.builder.js","sour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B,KAAK;QACL,OAAO;QACP,GAAG,CAAC,CAAC,CAAC,mBAAmB,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,mBAAmB,EAAE,CAAC,CAAC,mBAAmB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC/F,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CAAC,WAAoC;IACxE,MAAM,gBAAgB,GAA6C,EAAE,CAAC;IACtE,IAAI,kBAAkB,GAAG,CAAC,CAAC;IAC3B,IAAI,kBAAkB,GAAG,CAAC,CAAC;IAC3B,IAAI,YAAY,GAAG,CAAC,CAAC;IAErB,KAAK,MAAM,CAAC,IAAI,WAAW,EAAE,CAAC;QAC5B,IAAI,CAAC,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC;YAC7B,kBAAkB,IAAI,CAAC,CAAC;YACxB,KAAK,MAAM,IAAI,IAAI,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;gBACzC,MAAM,GAAG,GAAG,IAAI,CAAC,IAAuB,CAAC;gBACzC,gBAAgB,CAAC,GAAG,CAAC,GAAG,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;YAC3D,CAAC;QACH,CAAC;aAAM,CAAC;YACN,kBAAkB,IAAI,CAAC,CAAC;YACxB,IAAI,CAAC,CAAC,OAAO,CAAC,MAAM,KAAK,UAAU,IAAI,CAAC,CAAC,OAAO,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;gBACtE,YAAY,IAAI,CAAC,CAAC;YACpB,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO;QACL,eAAe,EAAE,WAAW,CAAC,MAAM;QACnC,kBAAkB;QAClB,kBAAkB;QAClB,YAAY;QACZ,gBAAgB;KACjB,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Pure mappers from raw per-candidate negotiation data to the protocol's\n * `DiscoveryNegotiation` / `DiscoverySummary` shapes consumed by the question\n * generator. No DB access, no LLM — safe to import from anywhere.\n */\nimport type { NegotiationTurn, NegotiationOutcome } from \"../negotiation/negotiation.state.js\";\nimport type { DiscoveryNegotiation, DiscoveryOutcome, DiscoverySummary, DiscoveryTurn, NegotiationRole } from \"./question.prompt.js\";\n\n/**\n * The input shape collected by the opportunity graph's negotiate node for\n * each candidate that completed a negotiation attempt (accepted, rejected,\n * stalled, or errored).\n */\nexport interface NegotiationResolution {\n candidateUserId: string;\n /** Abstract profile slice for the LLM (e.g. \"AI infra founder, Berlin\"). */\n counterpartyHint: string;\n /** Network/community prompt for the negotiation. */\n indexContext: string;\n turns: NegotiationTurn[];\n outcome: NegotiationOutcome;\n /** Optional pre-negotiation evaluator score (0..1). */\n seedAssessmentScore?: number;\n}\n\n/**\n * Convert one negotiation resolution to `DiscoveryNegotiation`.\n *\n * @param r - The raw resolution from the negotiate node.\n * @returns A `DiscoveryNegotiation` ready for the question generator.\n */\nexport function toDiscoveryNegotiation(r: NegotiationResolution): DiscoveryNegotiation {\n const turns: DiscoveryTurn[] = r.turns.map((t) => ({\n action: t.action,\n reasoning: t.assessment.reasoning,\n suggestedRoles: {\n ownUser: t.assessment.suggestedRoles.ownUser as NegotiationRole,\n otherUser: t.assessment.suggestedRoles.otherUser as NegotiationRole,\n },\n }));\n const outcome: DiscoveryOutcome = {\n hasOpportunity: r.outcome.hasOpportunity,\n reasoning: r.outcome.reasoning,\n ...(r.outcome.hasOpportunity && r.outcome.agreedRoles.length > 0\n ? { agreedRoles: r.outcome.agreedRoles.map((a) => ({ userId: a.userId, role: a.role as NegotiationRole })) }\n : {}),\n ...(r.outcome.reason ? { reason: r.outcome.reason } : {}),\n };\n return {\n counterpartyId: r.candidateUserId,\n counterpartyHint: r.counterpartyHint,\n indexContext: r.indexContext,\n turns,\n outcome,\n ...(r.seedAssessmentScore !== undefined ? { seedAssessmentScore: r.seedAssessmentScore } : {}),\n };\n}\n\n/**\n * Aggregate counters across all negotiations in a single discovery turn.\n *\n * @param resolutions - All resolved negotiations from the negotiate node.\n * @returns A `DiscoverySummary` with totals and role distribution.\n */\nexport function buildDiscoverySummary(resolutions: NegotiationResolution[]): DiscoverySummary {\n const roleDistribution: Partial<Record<NegotiationRole, number>> = {};\n let opportunitiesFound = 0;\n let noOpportunityCount = 0;\n let timeoutCount = 0;\n\n for (const r of resolutions) {\n if (r.outcome.hasOpportunity) {\n opportunitiesFound += 1;\n for (const role of r.outcome.agreedRoles) {\n const key = role.role as NegotiationRole;\n roleDistribution[key] = (roleDistribution[key] ?? 0) + 1;\n }\n } else {\n noOpportunityCount += 1;\n if (r.outcome.reason === \"turn_cap\" || r.outcome.reason === \"timeout\") {\n timeoutCount += 1;\n }\n }\n }\n\n return {\n totalCandidates: resolutions.length,\n opportunitiesFound,\n noOpportunityCount,\n timeoutCount,\n roleDistribution,\n };\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"opportunity.discover.d.ts","sourceRoot":"/","sources":["opportunity/opportunity.discover.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAe,0BAA0B,EAAE,UAAU,EAAE,MAAM,4CAA4C,CAAC;AACtH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,yCAAyC,CAAC;AAGrE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mCAAmC,CAAC;AAC7E,OAAO,EACL,oBAAoB,EAEpB,KAAK,6BAA6B,EAClC,KAAK,0BAA0B,EAGhC,MAAM,4BAA4B,CAAC;AAIpC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gDAAgD,CAAC;AAExF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,sDAAsD,CAAC;AACpG,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,uDAAuD,CAAC;AAGtG,OAAO,KAAK,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,sCAAsC,CAAC;AAqDvF,+EAA+E;AAC/E,MAAM,MAAM,wBAAwB,GAAG,UAAU,C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;IAClF,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,SAAS,CAAC,EAAE,oBAAoB,CAAC;IACjC,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B,GAAG,OAAO,CAAC,cAAc,CAAC,CAgI1B"}
@@ -8,7 +8,7 @@
8
8
  *
9
9
  * Used by the discover_opportunities chat tool.
10
10
  */
11
- import { gatherPresenterContext, } from "./opportunity.presenter.js";
11
+ import { gatherPresenterContext } from "./opportunity.presenter.js";
12
12
  import { MINIMAL_MAIN_TEXT_MAX_CHARS, getPrimaryActionLabel, SECONDARY_ACTION_LABEL } from "./opportunity.labels.js";
13
13
  import { viewerCentricCardSummary, narratorRemarkFromReasoning } from "./opportunity.presentation.js";
14
14
  import { protocolLogger, withCallLogging } from "../shared/observability/protocol.logger.js";
@@ -1 +1 @@
1
- {"version":3,"file":"opportunity.discover.js","sourceRoot":"/","sources":["opportunity/opportunity.discover.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAOH,OAAO,EAEL,sBAAsB,GAKvB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,2BAA2B,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AACrH,OAAO,EAAE,wBAAwB,EAAE,2BAA2B,EAAE,MAAM,+BAA+B,CAAC;AACtG,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,4CAA4C,CAAC;AAM7F,OAAO,EAAE,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AAE/E,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,MAAM,4CAA4C,CAAC;AAC5E,OAAO,EAAE,2BAA2B,EAAE,MAAM,gCAAgC,CAAC;AAC7E,OAAO,EAAE,qBAA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L,CAAC;IAED,oCAAoC;IACpC,MAAM,aAAa,GAAkB,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,CAAC;IAErG,IAAI,aAAa,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC/B,OAAO;YACL,KAAK,EAAE,KAAK;YACZ,KAAK,EAAE,CAAC;YACR,OAAO,EAAE,mEAAmE;YAC5E,UAAU;YACV,UAAU;SACX,CAAC;IACJ,CAAC;IAED,MAAM,QAAQ,GAAG,MAAM,mBAAmB,CAAC;QACzC,aAAa;QACb,QAAQ;QACR,MAAM;QACN,aAAa;QACb,cAAc,EAAE,KAAK,CAAC,cAAc;QACpC,SAAS,EAAE,KAAK,CAAC,SAAS;QAC1B,iBAAiB,EAAE,KAAK,CAAC,iBAAiB;QAC1C,UAAU;KACX,CAAC,CAAC;IAEH,OAAO;QACL,KAAK,EAAE,IAAI;QACX,KAAK,EAAE,QAAQ,CAAC,MAAM;QACtB,aAAa,EAAE,QAAQ;QACvB,UAAU;QACV,UAAU;KACX,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Run discovery from an ad-hoc query (e.g. chat \"find me a mentor\", \"who needs a React developer\").\n *\n * Invokes the opportunity graph with the query as sourceText. The HyDE graph's\n * LensInferrer automatically infers search lenses from the query, replacing the\n * old hardcoded strategy selection. Returns formatted candidates (enriched with\n * profile name/bio) for chat display.\n *\n * Used by the discover_opportunities chat tool.\n */\n\nimport type { Opportunity, ChatGraphCompositeDatabase, UserRecord } from \"../shared/interfaces/database.interface.js\";\nimport type { Cache } from \"../shared/interfaces/cache.interface.js\";\nimport type { OpportunityGraphOptions, CandidateMatch, SourceProfileData } from \"./opportunity.state.js\";\nimport type { DiscoveryNegotiation, DiscoverySummary } from \"./question.prompt.js\";\nimport type { QuestionerEnqueueFn } from \"../questioner/questioner.types.js\";\nimport {\n OpportunityPresenter,\n gatherPresenterContext,\n type OpportunityPresentationResult,\n type HomeCardPresentationResult,\n type HomeCardLLMResult,\n type HomeCardPresenterInput,\n} from \"./opportunity.presenter.js\";\nimport { MINIMAL_MAIN_TEXT_MAX_CHARS, getPrimaryActionLabel, SECONDARY_ACTION_LABEL } from \"./opportunity.labels.js\";\nimport { viewerCentricCardSummary, narratorRemarkFromReasoning } from \"./opportunity.presentation.js\";\nimport { protocolLogger, withCallLogging } from \"../shared/observability/protocol.logger.js\";\nimport type { ChatSummaryReader } from \"../shared/interfaces/chat-summary.interface.js\";\nimport type { ChatContextDigest } from \"../shared/schemas/chat-context.schema.js\";\nimport type { QuestionGeneratorReader } from \"../shared/interfaces/question-generator.interface.js\";\nimport type { NegotiationSummaryReader } from \"../shared/interfaces/negotiation-summary.interface.js\";\nimport type { DiscoveryNegotiationDigest } from \"../shared/schemas/negotiation-digest.schema.js\";\nimport { buildFallbackDigest } from \"../negotiation/negotiation.summarizer.js\";\nimport type { Question, QuestionStrategy } from \"../shared/schemas/question.schema.js\";\nimport { traceAgent, tracePhase } from \"../shared/observability/trace.js\";\nimport { requestContext } from \"../shared/observability/request-context.js\";\nimport { buildDiscoveryQuestionInput } from \"./discovery-question.helper.js\";\nimport { invokeWithAbortSignal } from \"../shared/agent/model-signal.js\";\n\nconst logger = protocolLogger(\"OpportunityDiscover\");\n\n/**\n * Per-negotiation summarizer budget. The summarizer fires one LLM call per\n * partial-or-full negotiation (concurrently via Promise.all). Without a cap\n * one slow OpenRouter route dominates the post-discovery tail and pushes the\n * whole MCP response past Railway's ~60 s no-upstream-bytes timeout. Falls\n * back to a deterministic digest when the deadline fires, so question\n * generation still has structured input.\n */\nconst NEGOTIATION_SUMMARY_TIMEOUT_MS_DEFAULT = 5_000;\n/**\n * Question-generator budget. Sized against Railway's ~60 s edge timeout:\n * the discovery + evaluation + negotiate phases consume ~50 s on the slow\n * path, leaving ~10 s of headroom for the tail. 12 s is the larger end of\n * \"fits\"; the question step usually completes in 4-8 s, so most legitimate\n * calls finish well inside. Aborted calls return `null` (no questions);\n * the rest of the discovery payload still ships.\n *\n * Documented at opportunity.tools.ts:912-921 as historically uncapped —\n * this is the cap.\n */\nconst DISCOVERY_QUESTIONS_TIMEOUT_MS_DEFAULT = 12_000;\n\n/**\n * Parse a positive integer env var, clamped to the safe-integer range so a\n * malformed env value cannot crash `AbortSignal.timeout` (which throws on\n * values outside `[0, MAX_SAFE_INTEGER]`). Mirrors the precedent in\n * `negotiation.agent.ts` (`isValidTimeoutMs`).\n */\nfunction parsePositiveIntEnv(name: string, fallback: number): number {\n const raw = process.env[name];\n if (!raw) return fallback;\n const n = Number.parseInt(raw, 10);\n if (!Number.isFinite(n) || n <= 0 || n > Number.MAX_SAFE_INTEGER) return fallback;\n return n;\n}\n\nfunction combineWithDeadline(\n callerSignal: AbortSignal | undefined,\n deadlineMs: number,\n): AbortSignal {\n const deadline = AbortSignal.timeout(deadlineMs);\n if (!callerSignal) return deadline;\n return AbortSignal.any([callerSignal, deadline]);\n}\n\n/** Compiled opportunity graph (from OpportunityGraphFactory.createGraph()). */\nexport type CompiledOpportunityGraph = ReturnType<\n import(\"./opportunity.graph.js\").OpportunityGraphFactory[\"createGraph\"]\n>;\n\nexport interface DiscoverInput {\n /** Compiled opportunity graph (already has DB, embedder, cache, HyDE graph). */\n opportunityGraph: CompiledOpportunityGraph;\n /** Database for enriching candidates with profile (getProfile). */\n database: ChatGraphCompositeDatabase;\n userId: string;\n query: string;\n indexScope: string[];\n limit?: number;\n /** Optional intent to use as discovery source and for triggeredBy (e.g. from opportunity queue). */\n triggerIntentId?: string;\n /** When set, filter discovery candidates to this specific user only (direct connection). */\n targetUserId?: string;\n /** When set, discover on behalf of this user (introducer flow). The caller (userId) becomes the introducer. */\n onBehalfOfUserId?: string;\n /** When provided, each opportunity is enriched with personalized presentation (headline, personalizedSummary, suggestedAction). */\n presenter?: OpportunityPresenter;\n /**\n * When true, use the full home card presentation format (with narratorRemark, action labels, mutualIntentsLabel).\n * This enables rendering the same rich opportunity cards in chat as on the home page.\n */\n useHomeCardFormat?: boolean;\n /**\n * When true, skip the LLM presenter and return minimal card data only (faster for chat).\n * Sets homeCardPresentation and narratorChip from static labels and match reason.\n */\n minimalForChat?: boolean;\n /** When set (e.g. from chat), create opportunities as draft with context.conversationId = chatSessionId. */\n chatSessionId?: string;\n /** Redis cache for discovery pagination. When provided, remaining candidates are cached for continuation. */\n cache?: Cache;\n /**\n * Which flow is invoking discovery. Drives the graph's trigger-aware branches\n * in persist (initial status) and negotiate (park window + streaming). When\n * omitted, the graph defaults to 'ambient'. Pass 'orchestrator' from the\n * chat `discover_opportunities` tool so users see drafts stream in and the\n * accepted-pair lookup surfaces existing connections.\n */\n trigger?: 'ambient' | 'orchestrator';\n /**\n * MCP-only. When set, the opportunity graph's negotiate phase is capped at\n * this many milliseconds; on timeout the caller gets whichever candidates\n * finished, the rest stay in `negotiating` and finalize in the background.\n * Chat, ambient queue, and all other callers omit this — existing behavior.\n */\n negotiateTimeoutMs?: number;\n /** Optional read-through chat-session digest reader. Required for chatContext enrichment. */\n chatSummary?: ChatSummaryReader;\n /**\n * Optional negotiation summarizer. When provided, each post-negotiation digest\n * replaces the raw negotiation in the decision-question generator's input,\n * keeping that prompt small and predictable regardless of candidate count.\n * When omitted, a deterministic fallback digest is built per negotiation.\n */\n negotiationSummary?: NegotiationSummaryReader;\n /** Optional decision-question generator. When omitted, no questions are produced. */\n questionGenerator?: QuestionGeneratorReader;\n /**\n * Master switch for decision-question generation. When false, this code path\n * is skipped entirely regardless of trigger. The composition root passes\n * `process.env.ENABLE_DISCOVERY_QUESTIONS === \"true\"`.\n */\n enableQuestions?: boolean;\n /**\n * Optional async question enqueue callback. When provided, question generation\n * is dispatched asynchronously to the QuestionerQueue instead of running inline\n * via the `questionGenerator`. The callback receives an enqueue payload and\n * returns a promise that resolves when the job is enqueued (not when generation\n * completes).\n */\n questionerEnqueue?: QuestionerEnqueueFn;\n}\n\n/** Context used by the minimal (no-LLM) path; only introducerName is needed for narrator chip. */\ntype MinimalPresenterContext = { introducerName?: string };\n\n/** Max chars for bio and matchReason in chat tool results to keep context manageable. */\nconst MAX_FIELD_CHARS = 100;\n\nfunction truncateForChat(\n s: string | undefined,\n max = MAX_FIELD_CHARS,\n): string | undefined {\n if (s == null || s === \"\") return undefined;\n const trimmed = s.trim();\n if (trimmed.length <= max) return trimmed;\n return trimmed.slice(0, max) + \"...\";\n}\n\n/** One formatted opportunity for chat (candidate-facing). */\nexport interface FormattedDiscoveryCandidate {\n opportunityId: string;\n userId: string;\n name?: string;\n avatar?: string | null;\n bio?: string;\n matchReason: string;\n score: number;\n status?: string;\n /** Present when DiscoverInput.presenter was provided (basic presentation). */\n presentation?: OpportunityPresentationResult;\n /** Present when DiscoverInput.useHomeCardFormat is true (full home card contract). */\n homeCardPresentation?: HomeCardPresentationResult;\n /** Viewer's role in this opportunity. */\n viewerRole?: string;\n /** Whether the viewer (as introducer) has approved the introduction. */\n viewerApproved?: boolean;\n /** Full user record for the candidate (needed for socials / Telegram fallback). */\n candidateUser?: UserRecord | null;\n /** Whether the counterpart is a ghost (not yet onboarded) user. */\n isGhost?: boolean;\n /** Narrator chip for home card display (name + remark, with optional avatar/userId for introducer). */\n narratorChip?: {\n name: string;\n text: string;\n avatar?: string | null;\n userId?: string;\n };\n /** Second party in introducer arrow layout (candidate -> secondParty). Present when viewer is introducer. */\n secondParty?: {\n name: string;\n avatar?: string | null;\n userId?: string;\n };\n}\n\n/** One step for debug visibility (subgraph/subtask). */\nexport interface DiscoverDebugStep {\n step: string;\n detail?: string;\n /** Structured data for rich display (e.g., candidate counts, scores). */\n data?: Record<string, unknown>;\n}\n\n/** One existing connection (no new opportunity created; user already has one with this person). */\nexport interface ExistingConnection {\n userId: string;\n name: string;\n status?: string;\n opportunityId?: string;\n}\n\n/** Statuses for which an existing connection may be shown as a card; others (accepted, rejected, expired) are only mentioned in text. */\nconst EXISTING_CONNECTION_CARD_STATUSES = ['draft', 'latent', 'pending'] as const;\n\nexport interface DiscoverResult {\n found: boolean;\n count: number;\n message?: string;\n opportunities?: FormattedDiscoveryCandidate[];\n /** Existing connections eligible for card display (draft, latent, or pending). Others are mention-only. */\n existingConnections?: ExistingConnection[];\n /** All existing connections for mention text (e.g. \"You already have a connection with: X (pending), Y (draft).\"). */\n existingConnectionsForMention?: ExistingConnection[];\n /**\n * Orchestrator-only: accepted opportunities the persist step found between the\n * discoverer and a candidate counterparty (status='accepted'). Populated from\n * OpportunityGraphState.dedupAlreadyAccepted. Used by the discover_opportunities\n * tool to tell the LLM \"this pair is already connected — open the existing\n * chat rather than creating a new draft\". Empty for the ambient trigger.\n */\n alreadyAcceptedPairs?: Array<{ opportunityId: string; counterpartyUserId: string }>;\n /** When true, the chat agent should call create_intent(suggestedIntentDescription) and retry discovery. */\n createIntentSuggested?: boolean;\n /** Description to pass to create_intent when createIntentSuggested is true. */\n suggestedIntentDescription?: string;\n /** Internal steps for copy-debug (select_strategies, opportunity_graph, enrich, etc.). */\n debugSteps?: DiscoverDebugStep[];\n /** Pagination metadata -- present when there are more unevaluated candidates. */\n pagination?: {\n discoveryId: string;\n evaluated: number;\n remaining: number;\n };\n /** 0–3 decision questions produced by the orchestrator path. Omitted when none. */\n questions?: Question[];\n /** Debug metadata for `debugMeta.discoveryQuestions` plumbing. */\n discoveryQuestionsDebug?: {\n inputMode: \"transcripts\" | \"insights\";\n finalCount: number;\n strategies: QuestionStrategy[];\n durationMs: number;\n };\n}\n\n/** Input for the shared enrichment helper. */\ninterface EnrichOpportunitiesInput {\n opportunities: Opportunity[];\n database: ChatGraphCompositeDatabase;\n userId: string;\n chatSessionId?: string;\n minimalForChat?: boolean;\n presenter?: OpportunityPresenter;\n useHomeCardFormat?: boolean;\n debugSteps: DiscoverDebugStep[];\n /** IDs of pre-existing opportunities merged into the list; these preserve their real status. */\n existingOpportunityIds?: Set<string>;\n /** When set, bypass the embedding filter for this specific user (direct connection mode). */\n targetUserId?: string;\n}\n\n/**\n * Enrich raw opportunities with profile data, presentation (LLM or minimal),\n * and narrator chips. Shared by both `runDiscoverFromQuery` and `continueDiscovery`\n * to avoid duplicating the profile-lookup / presenter / card-formatting logic.\n *\n * @param input - Enrichment context (opportunities, database, viewer, presentation options).\n * @returns Formatted discovery candidates ready for chat or home card display.\n */\nasync function enrichOpportunities(\n input: EnrichOpportunitiesInput,\n): Promise<FormattedDiscoveryCandidate[]> {\n const {\n opportunities,\n database,\n userId,\n chatSessionId,\n minimalForChat,\n presenter,\n useHomeCardFormat,\n debugSteps,\n existingOpportunityIds,\n targetUserId,\n } = input;\n\n const baseEnrichedRaw = await Promise.all(\n opportunities.map(async (opp) => {\n const viewerIsIntroducer = opp.actors.some((a) => a.role === 'introducer' && a.userId === userId);\n // When the viewer is the introducer, the \"candidate\" for the card is the agent\n // (the discovered person), not the patient (the intro target / onBehalfOfUserId).\n // For non-introducer views, pick the first non-viewer, non-introducer actor.\n const nonViewerNonIntroducerActors = opp.actors.filter((a) => a.userId !== userId && a.role !== 'introducer');\n const candidateActor = viewerIsIntroducer\n ? (nonViewerNonIntroducerActors.find((a) => a.role === 'agent') ?? nonViewerNonIntroducerActors[0])\n : nonViewerNonIntroducerActors[0];\n const candidateUserId = candidateActor?.userId ?? \"\";\n const viewerActor = opp.actors.find((a) => a.userId === userId);\n const [profile, candidateUser] = candidateUserId\n ? await Promise.all([database.getProfile(candidateUserId), database.getUser(candidateUserId)])\n : [null, null];\n // Skip soft-deleted users (deletedAt is set)\n if (candidateUser && 'deletedAt' in candidateUser && candidateUser.deletedAt) return null;\n const isDirectTarget = targetUserId && candidateUserId === targetUserId;\n if (!isDirectTarget && !candidateUser?.isGhost && !profile) return null;\n const confidence =\n typeof opp.interpretation?.confidence === \"number\"\n ? opp.interpretation.confidence\n : parseFloat(String(opp.interpretation?.confidence ?? 0)) || 0;\n return {\n opportunity: opp,\n candidateUserId,\n viewerRole: viewerActor?.role ?? \"party\",\n viewerApproved: viewerActor?.approved === true,\n candidateUser,\n profile,\n confidence,\n };\n }),\n );\n const baseEnriched = baseEnrichedRaw.filter((item): item is NonNullable<typeof item> => item !== null);\n debugSteps.push({\n step: \"enrich_profiles\",\n detail: `${baseEnriched.length} profile(s)`,\n });\n\n // Batch-fetch user records (candidates, introducers, and other party actors) for name/avatar fallback.\n const allActorUserIds = new Set<string>();\n for (const item of baseEnriched) {\n for (const actor of item.opportunity.actors) {\n if (actor.userId && actor.userId !== userId) {\n allActorUserIds.add(actor.userId);\n }\n }\n }\n const candidateUserIds = [...allActorUserIds];\n const [viewerUser, ...userResults] = await Promise.all([\n database.getUser(userId),\n ...candidateUserIds.map((id) => database.getUser(id)),\n ]);\n const avatarByUserId = new Map<string, string | null>();\n const nameByUserId = new Map<string, string | null>();\n const isGhostByUserId = new Map<string, boolean>();\n candidateUserIds.forEach((id, i) => {\n const user = userResults[i] ?? null;\n avatarByUserId.set(id, user?.avatar ?? null);\n nameByUserId.set(id, user?.name ?? null);\n isGhostByUserId.set(id, user?.isGhost ?? false);\n });\n const viewerName = viewerUser?.name ?? undefined;\n\n // Retry name resolution for candidates whose name is still missing.\n // The profile or user record may not have been ready on the first fetch\n // (e.g. profile generation still in flight). One retry covers transient gaps.\n const missingNameIds = baseEnriched\n .map((item) => item.candidateUserId)\n .filter((id) => id && !nameByUserId.get(id) && !baseEnriched.find((b) => b.candidateUserId === id && b.profile?.identity?.name));\n if (missingNameIds.length > 0) {\n const retried = await Promise.all(\n missingNameIds.map(async (id) => {\n const [profile, user] = await Promise.all([\n database.getProfile(id),\n database.getUser(id),\n ]);\n return { id, profile, user };\n }),\n );\n for (const r of retried) {\n const name = r.profile?.identity?.name ?? r.user?.name ?? null;\n if (name) nameByUserId.set(r.id, name);\n // Also update the baseEnriched profile so counterpartName picks it up\n if (r.profile) {\n const item = baseEnriched.find((b) => b.candidateUserId === r.id);\n if (item && !item.profile) item.profile = r.profile;\n }\n if (r.user?.avatar && !avatarByUserId.get(r.id)) {\n avatarByUserId.set(r.id, r.user.avatar);\n }\n }\n logger.verbose(\"[enrichOpportunities] Retried name lookup for candidates with missing names\", {\n attempted: missingNameIds.length,\n resolved: retried.filter((r) => r.profile?.identity?.name ?? r.user?.name).length,\n });\n }\n\n let presentations: OpportunityPresentationResult[] | undefined;\n let homeCardPresentations: HomeCardPresentationResult[] | undefined;\n let presenterContexts:\n | (Awaited<ReturnType<typeof gatherPresenterContext>> | MinimalPresenterContext)[]\n | undefined;\n\n if (minimalForChat && baseEnriched.length > 0) {\n // Minimal path: no LLM, viewer-centric card text (introduce counterpart to viewer)\n const counterpartName = (n: {\n profile?: { identity?: { name?: string } } | null;\n candidateUserId: string;\n }) => n.profile?.identity?.name ?? nameByUserId.get(n.candidateUserId) ?? \"\";\n homeCardPresentations = baseEnriched.map((item) => {\n const name = counterpartName(item)?.trim();\n const reasoning = item.opportunity.interpretation?.reasoning ?? \"\";\n const introducerName = item.opportunity.detection?.createdByName ?? undefined;\n const viewerIsIntroducer = item.opportunity.actors.some(\n (a) => a.role === \"introducer\" && a.userId === userId,\n );\n\n // For introducer view, find the second party (target user) name\n let secondPartyName: string | undefined;\n if (viewerIsIntroducer) {\n const otherPartyActors = item.opportunity.actors.filter(\n (a) => a.role !== \"introducer\" && a.userId !== item.candidateUserId,\n );\n if (otherPartyActors.length > 0) {\n const otherUserId = otherPartyActors[0].userId;\n secondPartyName = nameByUserId.get(otherUserId) ?? undefined;\n }\n }\n\n const isCounterpartGhost = isGhostByUserId.get(item.candidateUserId) ?? false;\n const personalizedSummary = viewerCentricCardSummary(\n reasoning,\n name,\n MINIMAL_MAIN_TEXT_MAX_CHARS,\n viewerName,\n introducerName,\n );\n return {\n headline: viewerIsIntroducer && secondPartyName\n ? `${name} → ${secondPartyName}`\n : (name ? `Connection with ${name}` : \"Suggested connection\"),\n personalizedSummary,\n digestSummary: name\n ? `You might like meeting ${name} based on your current interests.`\n : \"This connection may be relevant to your current interests.\",\n suggestedAction: \"Start a conversation to connect.\",\n narratorRemark: narratorRemarkFromReasoning(reasoning, name, viewerName),\n primaryActionLabel: getPrimaryActionLabel(viewerIsIntroducer ? \"introducer\" : \"party\"),\n secondaryActionLabel: SECONDARY_ACTION_LABEL,\n mutualIntentsLabel: \"Suggested connection\",\n greeting: \"\",\n };\n });\n presenterContexts = baseEnriched.map((item) => ({\n introducerName: item.opportunity.detection.createdByName ?? undefined,\n })) as MinimalPresenterContext[];\n } else if (presenter && baseEnriched.length > 0) {\n try {\n presenterContexts = await Promise.all(\n baseEnriched.map(({ opportunity }) =>\n gatherPresenterContext(database, opportunity, userId),\n ),\n );\n\n if (useHomeCardFormat) {\n // Use full home card format with action labels, narrator remark, etc.\n const fullContexts = presenterContexts as Awaited<\n ReturnType<typeof gatherPresenterContext>\n >[];\n const homeCardInputs: HomeCardPresenterInput[] = fullContexts.map(\n (ctx, idx) => ({\n ...ctx,\n mutualIntentCount: undefined,\n opportunityStatus: baseEnriched[idx].opportunity.status,\n }),\n );\n const llmResults = await presenter.presentHomeCardBatch(\n homeCardInputs,\n { concurrency: 5 },\n );\n // Append hardcoded button labels to LLM results\n homeCardPresentations = llmResults.map((llm, idx) => ({\n ...llm,\n primaryActionLabel: getPrimaryActionLabel(baseEnriched[idx].viewerRole),\n secondaryActionLabel: SECONDARY_ACTION_LABEL,\n }));\n } else {\n // Use basic presentation format\n presentations = await presenter.presentBatch(\n presenterContexts as Awaited<\n ReturnType<typeof gatherPresenterContext>\n >[],\n {\n concurrency: 5,\n },\n );\n }\n } catch (error) {\n logger.warn(\n \"Presenter enrichment failed during opportunity discovery; returning base results without presentations\",\n {\n userId,\n opportunitiesCount: baseEnriched.length,\n useHomeCardFormat,\n error: error instanceof Error ? error.message : String(error),\n },\n );\n presentations = undefined;\n homeCardPresentations = undefined;\n }\n }\n\n const enriched: FormattedDiscoveryCandidate[] = baseEnriched.map(\n (item, idx) => {\n const homeCard = homeCardPresentations?.[idx];\n const ctx = presenterContexts?.[idx];\n\n // Build narrator chip for home card format\n let narratorChip: FormattedDiscoveryCandidate[\"narratorChip\"];\n if (homeCard) {\n const viewerIsIntroducer = item.opportunity.actors.some(\n (a) => a.role === \"introducer\" && a.userId === userId,\n );\n if (viewerIsIntroducer) {\n narratorChip = {\n name: \"You\",\n text: homeCard.narratorRemark,\n userId: userId,\n };\n } else {\n const introducerActor = item.opportunity.actors.find(\n (a) => a.role === \"introducer\" && a.userId !== userId,\n );\n if (introducerActor) {\n const introducerName =\n ctx?.introducerName ??\n nameByUserId.get(introducerActor.userId) ??\n \"Someone\";\n narratorChip = {\n name: introducerName,\n text: homeCard.narratorRemark,\n userId: introducerActor.userId,\n avatar: avatarByUserId.get(introducerActor.userId) ?? null,\n };\n } else {\n narratorChip = {\n name: \"Index\",\n text: homeCard.narratorRemark,\n };\n }\n }\n }\n\n const isGhost = isGhostByUserId.get(item.candidateUserId) ?? false;\n\n // Build secondParty for introducer view (the other non-introducer party)\n let secondParty: FormattedDiscoveryCandidate[\"secondParty\"];\n const viewerIsIntroducerForCard = item.opportunity.actors.some(\n (a) => a.role === \"introducer\" && a.userId === userId,\n );\n if (viewerIsIntroducerForCard) {\n const otherPartyActor = item.opportunity.actors.find(\n (a) => a.role !== \"introducer\" && a.userId !== item.candidateUserId,\n );\n if (otherPartyActor) {\n const otherName = nameByUserId.get(otherPartyActor.userId) ?? undefined;\n if (otherName) {\n secondParty = {\n name: otherName,\n avatar: avatarByUserId.get(otherPartyActor.userId) ?? null,\n userId: otherPartyActor.userId,\n };\n }\n }\n }\n\n return {\n opportunityId: item.opportunity.id,\n userId: item.candidateUserId,\n name: item.profile?.identity?.name ?? nameByUserId.get(item.candidateUserId) ?? undefined,\n avatar: avatarByUserId.get(item.candidateUserId) ?? null,\n bio: truncateForChat(item.profile?.identity?.bio),\n matchReason:\n truncateForChat(\n item.opportunity.interpretation?.reasoning ?? \"\",\n ) ?? \"\",\n score: item.confidence,\n status: chatSessionId && !existingOpportunityIds?.has(item.opportunity.id) ? \"draft\" : item.opportunity.status,\n viewerRole: item.viewerRole,\n viewerApproved: item.viewerApproved,\n candidateUser: item.candidateUser,\n isGhost,\n ...(presentations?.[idx] && { presentation: presentations[idx] }),\n ...(homeCard && {\n homeCardPresentation: homeCard,\n }),\n ...(narratorChip && { narratorChip }),\n ...(secondParty && { secondParty }),\n };\n },\n );\n debugSteps.push({\n step: \"format_cards\",\n detail: `${enriched.length} card(s)`,\n });\n\n return enriched;\n}\n\n/** Cached discovery session data stored in Redis. */\ninterface CachedDiscoverySession {\n candidates: CandidateMatch[];\n userId: string;\n onBehalfOfUserId?: string;\n query: string;\n indexScope: string[];\n options: OpportunityGraphOptions;\n /**\n * Carried across pagination so page 2+ stays on the same flow as page 1.\n * Without this, orchestrator runs would fall back to the 'ambient' default\n * mid-search and lose the shorter park window + accepted-pair dedup.\n */\n trigger?: 'ambient' | 'orchestrator';\n}\n\n/**\n * Run discovery from an ad-hoc query (e.g. \"find me a mentor\", \"who needs a React developer\").\n * The HyDE graph's LensInferrer automatically infers search lenses from the query.\n * Invokes the opportunity graph and returns formatted candidates suitable for chat display.\n */\nexport async function runDiscoverFromQuery(\n input: DiscoverInput,\n): Promise<DiscoverResult> {\n const {\n opportunityGraph,\n database,\n userId,\n query,\n indexScope,\n limit = 5,\n triggerIntentId,\n targetUserId,\n onBehalfOfUserId,\n chatSessionId,\n trigger,\n negotiateTimeoutMs,\n } = input;\n\n if (indexScope.length === 0) {\n return {\n found: false,\n count: 0,\n message:\n \"You need to join at least one network (community) to discover opportunities. Use read_networks to see available networks, or create one.\",\n };\n }\n\n const debugSteps: DiscoverDebugStep[] = [];\n\n // When query is empty, the opportunity graph uses the user's intents in scope (indexedIntents[0].payload)\n // Lens inference is handled automatically by the HyDE graph's LensInferrer\n const queryOrEmpty = query?.trim() ?? \"\";\n // Orchestrator discovery defers the initial status to the graph's\n // trigger-aware `resolveInitialStatus`, which opens at 'negotiating' so\n // the accepted-draft streaming flow can run. Ambient chat discovery still\n // wants the legacy 'draft' status so the chat-only lifecycle holds; other\n // ambient callers keep 'latent'.\n const isOrchestrator = trigger === 'orchestrator';\n const options: OpportunityGraphOptions = {\n limit,\n ...(!isOrchestrator && { initialStatus: chatSessionId ? \"draft\" : \"latent\" }),\n ...(chatSessionId ? { conversationId: chatSessionId } : {}),\n ...(negotiateTimeoutMs !== undefined && { negotiateTimeoutMs }),\n };\n\n return withCallLogging(\n logger,\n \"runDiscoverFromQuery\",\n {\n userId,\n queryPreview: queryOrEmpty\n ? queryOrEmpty.substring(0, 50)\n : \"(using user intents in scope)\",\n indexScopeCount: indexScope.length,\n limit,\n },\n async () => {\n const result = await invokeWithAbortSignal(opportunityGraph, {\n userId,\n searchQuery: queryOrEmpty || undefined,\n // A single index resolves to the strict networkId override (membership-\n // validated in the scope node). Multiple indexes (e.g. a network-scoped\n // agent's bound network + personal index) pass through as indexScope so\n // the graph stays bounded instead of falling back to all networks.\n networkId: indexScope.length === 1 ? indexScope[0] : undefined,\n ...(indexScope.length > 1 ? { indexScope } : {}),\n triggerIntentId,\n targetUserId,\n onBehalfOfUserId,\n options,\n ...(trigger && { trigger }),\n });\n\n // Extract trace from graph and append to debugSteps\n const graphTrace = Array.isArray(result.trace) ? result.trace : [];\n for (const t of graphTrace) {\n debugSteps.push({\n step: t.node,\n detail: t.detail,\n ...(t.data ? { data: t.data } : {}),\n });\n }\n\n // Bail early if the graph returned an error\n if (result.error) {\n logger.warn(\"runDiscoverFromQuery graph returned error\", { error: result.error });\n return {\n found: false,\n count: 0,\n message: \"Failed to find opportunities. Please try again.\",\n debugSteps,\n };\n }\n\n // Cache remaining candidates for pagination\n let pagination: DiscoverResult['pagination'] | undefined;\n const remainingCandidates: CandidateMatch[] = result.remainingCandidates || [];\n if (remainingCandidates.length > 0 && input.cache) {\n try {\n const discoveryId = crypto.randomUUID();\n const cacheKey = `discovery:${userId}:${discoveryId}`;\n await input.cache.set(cacheKey, {\n candidates: remainingCandidates,\n userId,\n onBehalfOfUserId,\n query: queryOrEmpty,\n indexScope,\n options,\n ...(trigger && { trigger }),\n } satisfies CachedDiscoverySession, { ttl: 1800 }); // 30 minutes\n pagination = {\n discoveryId,\n evaluated: (result.candidates?.length ?? 0) - remainingCandidates.length,\n remaining: remainingCandidates.length,\n };\n } catch (cacheErr) {\n logger.warn(\"Failed to cache discovery pagination\", {\n userId,\n error: cacheErr instanceof Error ? cacheErr.message : String(cacheErr),\n });\n }\n }\n\n // Refine phase: a sibling of the opportunity graph in the trace tree.\n // Holds the three post-discovery summarization steps. Each step is its\n // own traced agent so it appears as a leaf in the trace UI.\n //\n // Negotiation summary: compress each raw negotiation into a fixed-size\n // structured digest so the question generator's prompt stays small\n // (a 10-candidate turn used to balloon past 60 KB and stall upstream).\n // Decision questions: generate up to 3 clarifying questions from the\n // digests + chat context.\n const { questionPayload } = await tracePhase(\"Refine\", async () => {\n const negotiationDigests = await summarizeNegotiations({\n negotiations: result.discoveryNegotiations ?? [],\n summarizer: input.negotiationSummary,\n enableQuestions: input.enableQuestions ?? false,\n trigger,\n });\n const questionPayload = await maybeBuildQuestions({\n trigger,\n enableQuestions: input.enableQuestions ?? false,\n chatSummary: input.chatSummary,\n questionGenerator: input.questionGenerator,\n chatSessionId,\n graphResult: result,\n negotiationDigests,\n query: queryOrEmpty,\n questionerEnqueue: input.questionerEnqueue,\n userId: input.userId,\n });\n return { negotiationDigests, questionPayload };\n });\n\n if (result.createIntentSuggested && result.suggestedIntentDescription) {\n if (chatSessionId) {\n return {\n found: false,\n count: 0,\n message: \"No matching opportunities found. Try a different query.\",\n pagination,\n ...(questionPayload.questions !== undefined ? { questions: questionPayload.questions } : {}),\n ...(questionPayload.debug !== undefined ? { discoveryQuestionsDebug: questionPayload.debug } : {}),\n };\n }\n return {\n found: false,\n count: 0,\n createIntentSuggested: true,\n suggestedIntentDescription: result.suggestedIntentDescription,\n message:\n \"No matching opportunities; add an intent with the suggested description to improve discovery.\",\n debugSteps,\n pagination,\n ...(questionPayload.questions !== undefined ? { questions: questionPayload.questions } : {}),\n ...(questionPayload.debug !== undefined ? { discoveryQuestionsDebug: questionPayload.debug } : {}),\n };\n }\n\n let opportunities: Opportunity[] = Array.isArray(result.opportunities)\n ? result.opportunities\n : [];\n let existingOpportunityIds: Set<string> | undefined;\n const rawExistingBetweenActors = Array.isArray(result.existingBetweenActors)\n ? result.existingBetweenActors\n : [];\n // Orchestrator trigger populates this; ambient returns []. Kept as a\n // loosely-typed pass-through because DiscoverResult is consumed by\n // callers (chat tool, tests) that already model the narrower shape.\n const alreadyAcceptedPairs = Array.isArray(\n (result as { dedupAlreadyAccepted?: Array<{ opportunityId: string; counterpartyUserId: string }> })\n .dedupAlreadyAccepted,\n )\n ? (result as { dedupAlreadyAccepted: Array<{ opportunityId: string; counterpartyUserId: string }> })\n .dedupAlreadyAccepted\n : [];\n // Enrich existing-between-actors with names so the tool can say \"You already have a connection with X (pending).\"\n const existingConnections: ExistingConnection[] = await Promise.all(\n rawExistingBetweenActors.map(async (item) => {\n const user = await database.getUser(item.candidateUserId);\n return {\n userId: item.candidateUserId,\n name: user?.name ?? \"Someone\",\n ...(item.existingStatus ? { status: item.existingStatus } : {}),\n ...(item.existingOpportunityId ? { opportunityId: item.existingOpportunityId } : {}),\n };\n }),\n );\n if (existingConnections.length > 0) {\n logger.verbose(\"[runDiscoverFromQuery] Skipped duplicates; existing connections\", {\n count: existingConnections.length,\n userIds: existingConnections.map((c) => c.userId),\n });\n }\n // Only expose existing connections as cards when status is in EXISTING_CONNECTION_CARD_STATUSES (draft, latent, pending); others are mention-only.\n const existingConnectionsForCards = existingConnections.filter((c) =>\n c.status != null && EXISTING_CONNECTION_CARD_STATUSES.includes(c.status as typeof EXISTING_CONNECTION_CARD_STATUSES[number])\n );\n\n // Fetch full opportunity data for existing connections that should be shown as cards\n // and merge them with the newly created opportunities\n if (existingConnectionsForCards.length > 0) {\n const existingOpps = await Promise.all(\n existingConnectionsForCards\n .filter((c) => c.opportunityId)\n .map((c) => database.getOpportunity(c.opportunityId!))\n );\n const validExistingOpps = existingOpps.filter((o): o is Opportunity => o != null);\n if (validExistingOpps.length > 0) {\n logger.verbose(\"[runDiscoverFromQuery] Including existing opportunities as cards\", {\n count: validExistingOpps.length,\n ids: validExistingOpps.map((o) => o.id),\n });\n existingOpportunityIds = new Set(validExistingOpps.map((o) => o.id));\n opportunities = [...opportunities, ...validExistingOpps];\n }\n }\n\n // Chat discovery: when we have chatSessionId we just invoked the graph; all result.opportunities\n // were created in this call and belong to this session. Do not filter by status: the enricher\n // may set status to pending/latent when merging with related opportunities, so filtering to\n // \"draft\" would incorrectly drop them.\n if (chatSessionId && (result.opportunities?.length ?? 0) > 0) {\n logger.verbose(\"[runDiscoverFromQuery] Chat session opportunities from graph\", {\n count: opportunities.length,\n statuses: opportunities.map((o) => o.status),\n });\n }\n debugSteps.push({\n step: \"opportunity_graph\",\n detail: `${opportunities.length} opportunity(ies)${existingConnections.length > 0 ? `, ${existingConnections.length} existing` : \"\"}`,\n });\n\n if (opportunities.length === 0) {\n if (existingConnections.length > 0) {\n return {\n found: true,\n count: 0,\n message:\n \"No new opportunities created; you already have a connection with: \" +\n existingConnections.map((c) => `${c.name}${c.status ? ` (${c.status})` : \"\"}`).join(\", \") +\n \". View on your home page.\",\n existingConnections: existingConnectionsForCards,\n existingConnectionsForMention: existingConnections,\n ...(alreadyAcceptedPairs.length > 0 && { alreadyAcceptedPairs }),\n debugSteps,\n pagination,\n ...(questionPayload.questions !== undefined ? { questions: questionPayload.questions } : {}),\n ...(questionPayload.debug !== undefined ? { discoveryQuestionsDebug: questionPayload.debug } : {}),\n };\n }\n return {\n found: false,\n count: 0,\n message:\n \"No matching opportunities found. Try a different query or create intents to improve matching.\",\n ...(alreadyAcceptedPairs.length > 0 && { alreadyAcceptedPairs }),\n debugSteps,\n pagination,\n ...(questionPayload.questions !== undefined ? { questions: questionPayload.questions } : {}),\n ...(questionPayload.debug !== undefined ? { discoveryQuestionsDebug: questionPayload.debug } : {}),\n };\n }\n\n const enriched = await enrichOpportunities({\n opportunities,\n database,\n userId,\n chatSessionId,\n minimalForChat: input.minimalForChat,\n presenter: input.presenter,\n useHomeCardFormat: input.useHomeCardFormat,\n debugSteps,\n existingOpportunityIds,\n targetUserId,\n });\n\n return {\n found: true,\n count: enriched.length,\n opportunities: enriched,\n ...(existingConnectionsForCards.length > 0 ? { existingConnections: existingConnectionsForCards } : {}),\n ...(existingConnections.length > 0 ? { existingConnectionsForMention: existingConnections } : {}),\n ...(alreadyAcceptedPairs.length > 0 ? { alreadyAcceptedPairs } : {}),\n debugSteps,\n pagination,\n ...(questionPayload.questions !== undefined ? { questions: questionPayload.questions } : {}),\n ...(questionPayload.debug !== undefined ? { discoveryQuestionsDebug: questionPayload.debug } : {}),\n };\n },\n { context: { userId }, logOutput: false },\n ).catch((err) => {\n return {\n found: false,\n count: 0,\n message: \"Failed to find opportunities. Please try again.\",\n };\n });\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// DECISION-QUESTION HELPER\n// ─────────────────────────────────────────────────────────────────────────────\n\ntype GraphResultLike = {\n sourceProfile?: SourceProfileData | null;\n discoveryNegotiations?: DiscoveryNegotiation[];\n discoverySummary?: DiscoverySummary | null;\n};\n\ninterface MaybeBuildQuestionsInput {\n trigger: 'ambient' | 'orchestrator' | undefined;\n enableQuestions: boolean;\n chatSummary: ChatSummaryReader | undefined;\n questionGenerator: QuestionGeneratorReader | undefined;\n chatSessionId: string | undefined;\n graphResult: GraphResultLike;\n /** Pre-built per-negotiation digests. Pass [] when summarization is unavailable or disabled. */\n negotiationDigests: DiscoveryNegotiationDigest[];\n query: string;\n /** Optional async enqueue callback for background question generation. */\n questionerEnqueue?: DiscoverInput['questionerEnqueue'];\n /** User ID needed for the enqueue payload. */\n userId?: string;\n}\n\n/**\n * Run the negotiation summarizer over every negotiation in this discovery turn.\n * Each summarization is independent — run them concurrently via Promise.all.\n * When the summarizer is missing (no LLM available) or fails for an individual\n * negotiation, fall back to a deterministic digest so the downstream generator\n * still has structured input.\n */\nasync function summarizeNegotiations(args: {\n negotiations: DiscoveryNegotiation[];\n summarizer: NegotiationSummaryReader | undefined;\n enableQuestions: boolean;\n trigger: 'ambient' | 'orchestrator' | undefined;\n}): Promise<DiscoveryNegotiationDigest[]> {\n // Skip the LLM round-trip entirely when questions won't be built.\n if (!args.enableQuestions || args.trigger !== 'orchestrator') return [];\n if (args.negotiations.length === 0) return [];\n\n const perNegTimeoutMs = parsePositiveIntEnv(\n \"NEGOTIATION_SUMMARY_TIMEOUT_MS\",\n NEGOTIATION_SUMMARY_TIMEOUT_MS_DEFAULT,\n );\n const callerSignal = requestContext.getStore()?.abortSignal;\n\n return traceAgent(\n `Negotiation summary (${args.negotiations.length})`,\n () =>\n Promise.all(\n args.negotiations.map(async (n) => {\n if (!args.summarizer) return buildFallbackDigest(n);\n // Per-negotiation deadline: one slow OpenRouter route used to\n // dominate the post-discovery tail. With a cap, an aborted\n // summarizer falls back to a deterministic digest so the\n // question generator still has structured input.\n const signal = combineWithDeadline(callerSignal, perNegTimeoutMs);\n try {\n const d = await args.summarizer.summarize(n, { signal });\n return d ?? buildFallbackDigest(n);\n } catch (err) {\n // Attribute cause from err.name (AbortError), not from\n // signal.aborted — the latter is read post-catch and can race a\n // deadline-trip-after-unrelated-error, producing a misleading log.\n const aborted = err instanceof Error && err.name === \"AbortError\";\n logger.warn(\"negotiationSummary.summarize threw — using fallback digest\", {\n counterpartyHint: n.counterpartyHint,\n aborted,\n error: err instanceof Error ? err.message : String(err),\n });\n return buildFallbackDigest(n);\n }\n }),\n ),\n (digests) => `${digests.length} digest${digests.length === 1 ? \"\" : \"s\"}`,\n );\n}\n\nasync function maybeBuildQuestions(args: MaybeBuildQuestionsInput): Promise<{\n questions?: Question[];\n debug?: DiscoverResult[\"discoveryQuestionsDebug\"];\n}> {\n if (!args.enableQuestions) return {};\n if (args.trigger !== 'orchestrator') return {};\n\n // Hardcoded — `insights` mode is planned for a later slice. Warn if the env\n // var is set so operators aren't surprised when reporting still says\n // \"transcripts\".\n if (process.env.DISCOVERY_QUESTIONS_INPUT_MODE === \"insights\") {\n logger.warn(\"DISCOVERY_QUESTIONS_INPUT_MODE=insights is not yet implemented; falling back to transcripts\");\n }\n const inputMode: \"transcripts\" | \"insights\" = \"transcripts\";\n\n let chatContext: ChatContextDigest | undefined;\n if (args.chatSummary && args.chatSessionId) {\n const sessionId = args.chatSessionId;\n const summary = args.chatSummary;\n chatContext = await traceAgent(\n \"Chat summary\",\n async () => {\n try {\n return (await summary.getDigest(sessionId)) ?? undefined;\n } catch (err) {\n logger.warn(\"chatSummary.getDigest threw — proceeding without digest\", {\n sessionId,\n error: err instanceof Error ? err.message : String(err),\n });\n return undefined;\n }\n },\n (digest) => (digest ? \"loaded\" : \"empty\"),\n );\n }\n\n // ── Async enqueue path ──────────────────────────────────────────────────\n // When questionerEnqueue is provided, dispatch question generation\n // asynchronously to the background QuestionerQueue. This replaces the\n // inline generator path. Questions will be persisted to DB by the queue\n // worker and served via GET /api/questions.\n if (args.questionerEnqueue && args.userId) {\n const summary = args.graphResult.discoverySummary ?? {\n totalCandidates: 0,\n opportunitiesFound: 0,\n noOpportunityCount: 0,\n timeoutCount: 0,\n roleDistribution: {},\n };\n\n const enqueueInput = buildDiscoveryQuestionInput({\n query: args.query,\n sourceProfile: args.graphResult.sourceProfile ?? null,\n negotiationDigests: args.negotiationDigests,\n summary,\n chatContext,\n now: new Date().toISOString(),\n });\n\n try {\n await args.questionerEnqueue({\n mode: 'discovery',\n userId: args.userId,\n sourceType: 'discovery',\n sourceId: args.chatSessionId ?? crypto.randomUUID(),\n context: enqueueInput,\n conversationId: args.chatSessionId,\n });\n logger.info(\"Question generation enqueued to QuestionerQueue\", {\n userId: args.userId,\n trigger: args.trigger,\n });\n } catch (err) {\n logger.warn(\"Failed to enqueue question generation\", {\n error: err instanceof Error ? err.message : String(err),\n });\n }\n return {};\n }\n\n // ── Inline generator path (backward compat) ────────────────────────────\n if (!args.questionGenerator) return {};\n\n const negotiationDigests = args.negotiationDigests;\n const summary = args.graphResult.discoverySummary ?? {\n totalCandidates: 0,\n opportunitiesFound: 0,\n noOpportunityCount: 0,\n timeoutCount: 0,\n roleDistribution: {},\n };\n\n const input = buildDiscoveryQuestionInput({\n query: args.query,\n sourceProfile: args.graphResult.sourceProfile ?? null,\n negotiationDigests,\n summary,\n chatContext,\n now: new Date().toISOString(),\n });\n\n const questionGenerator = args.questionGenerator;\n const generatorStart = Date.now();\n const questionsTimeoutMs = parsePositiveIntEnv(\n \"DISCOVERY_QUESTIONS_TIMEOUT_MS\",\n DISCOVERY_QUESTIONS_TIMEOUT_MS_DEFAULT,\n );\n const questionsSignal = combineWithDeadline(\n requestContext.getStore()?.abortSignal,\n questionsTimeoutMs,\n );\n const genResult = await traceAgent(\n \"Decision questions\",\n async () => {\n try {\n return await questionGenerator.generate(input, { signal: questionsSignal });\n } catch (err) {\n logger.warn(\"questionGenerator.generate threw — suppressing questions\", {\n error: err instanceof Error ? err.message : String(err),\n });\n return null;\n }\n },\n (r) => {\n const count = r?.questions?.length ?? 0;\n return `${count} question${count === 1 ? \"\" : \"s\"}`;\n },\n );\n const durationMs = Date.now() - generatorStart;\n\n const finalCount = genResult?.questions?.length ?? 0;\n const strategies: QuestionStrategy[] = genResult?.strategies ?? [];\n\n return {\n ...(genResult && genResult.questions.length > 0 ? { questions: genResult.questions } : {}),\n debug: {\n inputMode,\n finalCount,\n strategies,\n durationMs,\n },\n };\n}\n\n/**\n * Continue a paginated discovery by evaluating the next batch of cached candidates.\n * Loads candidates from Redis, invokes the opportunity graph in continue_discovery mode,\n * then enriches and returns the results with updated pagination metadata.\n *\n * @param input - Continuation context (graph, database, cache, discoveryId, etc.).\n * @returns Discovery result with enriched opportunities and pagination state.\n */\nexport async function continueDiscovery(input: {\n opportunityGraph: CompiledOpportunityGraph;\n database: ChatGraphCompositeDatabase;\n cache: Cache;\n userId: string;\n discoveryId: string;\n /** If provided, validates the cached session's indexScope contains this index. */\n expectedIndexId?: string;\n limit?: number;\n chatSessionId?: string;\n minimalForChat?: boolean;\n presenter?: OpportunityPresenter;\n useHomeCardFormat?: boolean;\n}): Promise<DiscoverResult> {\n const {\n opportunityGraph,\n database,\n cache,\n userId,\n discoveryId,\n expectedIndexId,\n limit = 20,\n chatSessionId,\n } = input;\n const cacheKey = `discovery:${userId}:${discoveryId}`;\n\n const cached = await cache.get<CachedDiscoverySession>(cacheKey);\n\n if (!cached) {\n return {\n found: false,\n count: 0,\n message: \"Discovery session expired or not found. Please start a new search.\",\n };\n }\n\n // Validate that the cached session's scope matches the current chat context\n if (expectedIndexId && !cached.indexScope.includes(expectedIndexId)) {\n return {\n found: false,\n count: 0,\n message: \"Discovery session was created in a different context. Please start a new search.\",\n };\n }\n\n const debugSteps: DiscoverDebugStep[] = [];\n\n const result = await invokeWithAbortSignal(opportunityGraph, {\n userId,\n searchQuery: cached.query || undefined,\n candidates: cached.candidates,\n operationMode: 'continue_discovery' as const,\n onBehalfOfUserId: cached.onBehalfOfUserId,\n // Carry the original trigger so page 2+ stays on the same flow as page\n // 1 (orchestrator negotiations with 60s park window + accepted-pair\n // dedup, or ambient with 5-min park window).\n ...(cached.trigger && { trigger: cached.trigger }),\n options: {\n ...cached.options,\n limit,\n ...(chatSessionId ? { conversationId: chatSessionId } : {}),\n },\n });\n\n // Extract trace from graph and append to debugSteps\n const graphTrace = result.trace || [];\n for (const t of graphTrace) {\n debugSteps.push({\n step: t.node,\n detail: t.detail,\n ...(t.data ? { data: t.data } : {}),\n });\n }\n\n // Bail early if the graph returned an error\n if (result.error) {\n logger.warn(\"continueDiscovery graph returned error\", { error: result.error });\n return {\n found: false,\n count: 0,\n message: \"Discovery continuation failed. Please start a new search.\",\n debugSteps,\n };\n }\n\n // Update cache with remaining candidates or delete if exhausted\n const remaining: CandidateMatch[] = result.remainingCandidates || [];\n let pagination: DiscoverResult['pagination'] | undefined;\n try {\n if (remaining.length > 0) {\n await cache.set(cacheKey, {\n ...cached,\n candidates: remaining,\n } satisfies CachedDiscoverySession, { ttl: 1800 });\n pagination = {\n discoveryId,\n evaluated: cached.candidates.length - remaining.length,\n remaining: remaining.length,\n };\n } else {\n await cache.delete(cacheKey);\n }\n } catch (cacheErr) {\n logger.warn(\"Failed to update discovery pagination cache\", {\n userId,\n discoveryId,\n error: cacheErr instanceof Error ? cacheErr.message : String(cacheErr),\n });\n }\n\n // Check for opportunities in result\n const opportunities: Opportunity[] = Array.isArray(result.opportunities) ? result.opportunities : [];\n\n if (opportunities.length === 0) {\n return {\n found: false,\n count: 0,\n message: \"No more matching opportunities found in the remaining candidates.\",\n debugSteps,\n pagination,\n };\n }\n\n const enriched = await enrichOpportunities({\n opportunities,\n database,\n userId,\n chatSessionId,\n minimalForChat: input.minimalForChat,\n presenter: input.presenter,\n useHomeCardFormat: input.useHomeCardFormat,\n debugSteps,\n });\n\n return {\n found: true,\n count: enriched.length,\n opportunities: enriched,\n debugSteps,\n pagination,\n };\n}\n"]}
1
+ {"version":3,"file":"opportunity.discover.js","sourceRoot":"/","sources":["opportunity/opportunity.discover.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAOH,OAAO,EAAwB,sBAAsB,EAA4H,MAAM,4BAA4B,CAAC;AACpN,OAAO,EAAE,2BAA2B,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AACrH,OAAO,EAAE,wBAAwB,EAAE,2BAA2B,EAAE,MAAM,+BAA+B,CAAC;AACtG,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,4CAA4C,CAAC;AAM7F,OAAO,EAAE,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AAE/E,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,MAAM,4CAA4C,CAAC;AAC5E,OAAO,EAAE,2BAA2B,EAAE,MAAM,gCAAgC,CAAC;AAC7E,OAAO,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAC;AAExE,MAAM,MAAM,GAAG,cAAc,CAAC,qBAAqB,CAAC,CAAC;AAErD;;;;;;;GAOG;AACH,MAAM,sCAAsC,G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AAa,CAAC,CAAC,CAAC,EAAE,CAAC;IAErG,IAAI,aAAa,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC/B,OAAO;YACL,KAAK,EAAE,KAAK;YACZ,KAAK,EAAE,CAAC;YACR,OAAO,EAAE,mEAAmE;YAC5E,UAAU;YACV,UAAU;SACX,CAAC;IACJ,CAAC;IAED,MAAM,QAAQ,GAAG,MAAM,mBAAmB,CAAC;QACzC,aAAa;QACb,QAAQ;QACR,MAAM;QACN,aAAa;QACb,cAAc,EAAE,KAAK,CAAC,cAAc;QACpC,SAAS,EAAE,KAAK,CAAC,SAAS;QAC1B,iBAAiB,EAAE,KAAK,CAAC,iBAAiB;QAC1C,UAAU;KACX,CAAC,CAAC;IAEH,OAAO;QACL,KAAK,EAAE,IAAI;QACX,KAAK,EAAE,QAAQ,CAAC,MAAM;QACtB,aAAa,EAAE,QAAQ;QACvB,UAAU;QACV,UAAU;KACX,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Run discovery from an ad-hoc query (e.g. chat \"find me a mentor\", \"who needs a React developer\").\n *\n * Invokes the opportunity graph with the query as sourceText. The HyDE graph's\n * LensInferrer automatically infers search lenses from the query, replacing the\n * old hardcoded strategy selection. Returns formatted candidates (enriched with\n * profile name/bio) for chat display.\n *\n * Used by the discover_opportunities chat tool.\n */\n\nimport type { Opportunity, ChatGraphCompositeDatabase, UserRecord } from \"../shared/interfaces/database.interface.js\";\nimport type { Cache } from \"../shared/interfaces/cache.interface.js\";\nimport type { OpportunityGraphOptions, CandidateMatch, SourceProfileData } from \"./opportunity.state.js\";\nimport type { DiscoveryNegotiation, DiscoverySummary } from \"./question.prompt.js\";\nimport type { QuestionerEnqueueFn } from \"../questioner/questioner.types.js\";\nimport { OpportunityPresenter, gatherPresenterContext, type OpportunityPresentationResult, type HomeCardPresentationResult, type HomeCardLLMResult, type HomeCardPresenterInput } from \"./opportunity.presenter.js\";\nimport { MINIMAL_MAIN_TEXT_MAX_CHARS, getPrimaryActionLabel, SECONDARY_ACTION_LABEL } from \"./opportunity.labels.js\";\nimport { viewerCentricCardSummary, narratorRemarkFromReasoning } from \"./opportunity.presentation.js\";\nimport { protocolLogger, withCallLogging } from \"../shared/observability/protocol.logger.js\";\nimport type { ChatSummaryReader } from \"../shared/interfaces/chat-summary.interface.js\";\nimport type { ChatContextDigest } from \"../shared/schemas/chat-context.schema.js\";\nimport type { QuestionGeneratorReader } from \"../shared/interfaces/question-generator.interface.js\";\nimport type { NegotiationSummaryReader } from \"../shared/interfaces/negotiation-summary.interface.js\";\nimport type { DiscoveryNegotiationDigest } from \"../shared/schemas/negotiation-digest.schema.js\";\nimport { buildFallbackDigest } from \"../negotiation/negotiation.summarizer.js\";\nimport type { Question, QuestionStrategy } from \"../shared/schemas/question.schema.js\";\nimport { traceAgent, tracePhase } from \"../shared/observability/trace.js\";\nimport { requestContext } from \"../shared/observability/request-context.js\";\nimport { buildDiscoveryQuestionInput } from \"./discovery-question.helper.js\";\nimport { invokeWithAbortSignal } from \"../shared/agent/model-signal.js\";\n\nconst logger = protocolLogger(\"OpportunityDiscover\");\n\n/**\n * Per-negotiation summarizer budget. The summarizer fires one LLM call per\n * partial-or-full negotiation (concurrently via Promise.all). Without a cap\n * one slow OpenRouter route dominates the post-discovery tail and pushes the\n * whole MCP response past Railway's ~60 s no-upstream-bytes timeout. Falls\n * back to a deterministic digest when the deadline fires, so question\n * generation still has structured input.\n */\nconst NEGOTIATION_SUMMARY_TIMEOUT_MS_DEFAULT = 5_000;\n/**\n * Question-generator budget. Sized against Railway's ~60 s edge timeout:\n * the discovery + evaluation + negotiate phases consume ~50 s on the slow\n * path, leaving ~10 s of headroom for the tail. 12 s is the larger end of\n * \"fits\"; the question step usually completes in 4-8 s, so most legitimate\n * calls finish well inside. Aborted calls return `null` (no questions);\n * the rest of the discovery payload still ships.\n *\n * Documented at opportunity.tools.ts:912-921 as historically uncapped —\n * this is the cap.\n */\nconst DISCOVERY_QUESTIONS_TIMEOUT_MS_DEFAULT = 12_000;\n\n/**\n * Parse a positive integer env var, clamped to the safe-integer range so a\n * malformed env value cannot crash `AbortSignal.timeout` (which throws on\n * values outside `[0, MAX_SAFE_INTEGER]`). Mirrors the precedent in\n * `negotiation.agent.ts` (`isValidTimeoutMs`).\n */\nfunction parsePositiveIntEnv(name: string, fallback: number): number {\n const raw = process.env[name];\n if (!raw) return fallback;\n const n = Number.parseInt(raw, 10);\n if (!Number.isFinite(n) || n <= 0 || n > Number.MAX_SAFE_INTEGER) return fallback;\n return n;\n}\n\nfunction combineWithDeadline(\n callerSignal: AbortSignal | undefined,\n deadlineMs: number,\n): AbortSignal {\n const deadline = AbortSignal.timeout(deadlineMs);\n if (!callerSignal) return deadline;\n return AbortSignal.any([callerSignal, deadline]);\n}\n\n/** Compiled opportunity graph (from OpportunityGraphFactory.createGraph()). */\nexport type CompiledOpportunityGraph = ReturnType<\n import(\"./opportunity.graph.js\").OpportunityGraphFactory[\"createGraph\"]\n>;\n\nexport interface DiscoverInput {\n /** Compiled opportunity graph (already has DB, embedder, cache, HyDE graph). */\n opportunityGraph: CompiledOpportunityGraph;\n /** Database for enriching candidates with profile (getProfile). */\n database: ChatGraphCompositeDatabase;\n userId: string;\n query: string;\n indexScope: string[];\n limit?: number;\n /** Optional intent to use as discovery source and for triggeredBy (e.g. from opportunity queue). */\n triggerIntentId?: string;\n /** When set, filter discovery candidates to this specific user only (direct connection). */\n targetUserId?: string;\n /** When set, discover on behalf of this user (introducer flow). The caller (userId) becomes the introducer. */\n onBehalfOfUserId?: string;\n /** When provided, each opportunity is enriched with personalized presentation (headline, personalizedSummary, suggestedAction). */\n presenter?: OpportunityPresenter;\n /**\n * When true, use the full home card presentation format (with narratorRemark, action labels, mutualIntentsLabel).\n * This enables rendering the same rich opportunity cards in chat as on the home page.\n */\n useHomeCardFormat?: boolean;\n /**\n * When true, skip the LLM presenter and return minimal card data only (faster for chat).\n * Sets homeCardPresentation and narratorChip from static labels and match reason.\n */\n minimalForChat?: boolean;\n /** When set (e.g. from chat), create opportunities as draft with context.conversationId = chatSessionId. */\n chatSessionId?: string;\n /** Redis cache for discovery pagination. When provided, remaining candidates are cached for continuation. */\n cache?: Cache;\n /**\n * Which flow is invoking discovery. Drives the graph's trigger-aware branches\n * in persist (initial status) and negotiate (park window + streaming). When\n * omitted, the graph defaults to 'ambient'. Pass 'orchestrator' from the\n * chat `discover_opportunities` tool so users see drafts stream in and the\n * accepted-pair lookup surfaces existing connections.\n */\n trigger?: 'ambient' | 'orchestrator';\n /**\n * MCP-only. When set, the opportunity graph's negotiate phase is capped at\n * this many milliseconds; on timeout the caller gets whichever candidates\n * finished, the rest stay in `negotiating` and finalize in the background.\n * Chat, ambient queue, and all other callers omit this — existing behavior.\n */\n negotiateTimeoutMs?: number;\n /** Optional read-through chat-session digest reader. Required for chatContext enrichment. */\n chatSummary?: ChatSummaryReader;\n /**\n * Optional negotiation summarizer. When provided, each post-negotiation digest\n * replaces the raw negotiation in the decision-question generator's input,\n * keeping that prompt small and predictable regardless of candidate count.\n * When omitted, a deterministic fallback digest is built per negotiation.\n */\n negotiationSummary?: NegotiationSummaryReader;\n /** Optional decision-question generator. When omitted, no questions are produced. */\n questionGenerator?: QuestionGeneratorReader;\n /**\n * Master switch for decision-question generation. When false, this code path\n * is skipped entirely regardless of trigger. The composition root passes\n * `process.env.ENABLE_DISCOVERY_QUESTIONS === \"true\"`.\n */\n enableQuestions?: boolean;\n /**\n * Optional async question enqueue callback. When provided, question generation\n * is dispatched asynchronously to the QuestionerQueue instead of running inline\n * via the `questionGenerator`. The callback receives an enqueue payload and\n * returns a promise that resolves when the job is enqueued (not when generation\n * completes).\n */\n questionerEnqueue?: QuestionerEnqueueFn;\n}\n\n/** Context used by the minimal (no-LLM) path; only introducerName is needed for narrator chip. */\ntype MinimalPresenterContext = { introducerName?: string };\n\n/** Max chars for bio and matchReason in chat tool results to keep context manageable. */\nconst MAX_FIELD_CHARS = 100;\n\nfunction truncateForChat(\n s: string | undefined,\n max = MAX_FIELD_CHARS,\n): string | undefined {\n if (s == null || s === \"\") return undefined;\n const trimmed = s.trim();\n if (trimmed.length <= max) return trimmed;\n return trimmed.slice(0, max) + \"...\";\n}\n\n/** One formatted opportunity for chat (candidate-facing). */\nexport interface FormattedDiscoveryCandidate {\n opportunityId: string;\n userId: string;\n name?: string;\n avatar?: string | null;\n bio?: string;\n matchReason: string;\n score: number;\n status?: string;\n /** Present when DiscoverInput.presenter was provided (basic presentation). */\n presentation?: OpportunityPresentationResult;\n /** Present when DiscoverInput.useHomeCardFormat is true (full home card contract). */\n homeCardPresentation?: HomeCardPresentationResult;\n /** Viewer's role in this opportunity. */\n viewerRole?: string;\n /** Whether the viewer (as introducer) has approved the introduction. */\n viewerApproved?: boolean;\n /** Full user record for the candidate (needed for socials / Telegram fallback). */\n candidateUser?: UserRecord | null;\n /** Whether the counterpart is a ghost (not yet onboarded) user. */\n isGhost?: boolean;\n /** Narrator chip for home card display (name + remark, with optional avatar/userId for introducer). */\n narratorChip?: {\n name: string;\n text: string;\n avatar?: string | null;\n userId?: string;\n };\n /** Second party in introducer arrow layout (candidate -> secondParty). Present when viewer is introducer. */\n secondParty?: {\n name: string;\n avatar?: string | null;\n userId?: string;\n };\n}\n\n/** One step for debug visibility (subgraph/subtask). */\nexport interface DiscoverDebugStep {\n step: string;\n detail?: string;\n /** Structured data for rich display (e.g., candidate counts, scores). */\n data?: Record<string, unknown>;\n}\n\n/** One existing connection (no new opportunity created; user already has one with this person). */\nexport interface ExistingConnection {\n userId: string;\n name: string;\n status?: string;\n opportunityId?: string;\n}\n\n/** Statuses for which an existing connection may be shown as a card; others (accepted, rejected, expired) are only mentioned in text. */\nconst EXISTING_CONNECTION_CARD_STATUSES = ['draft', 'latent', 'pending'] as const;\n\nexport interface DiscoverResult {\n found: boolean;\n count: number;\n message?: string;\n opportunities?: FormattedDiscoveryCandidate[];\n /** Existing connections eligible for card display (draft, latent, or pending). Others are mention-only. */\n existingConnections?: ExistingConnection[];\n /** All existing connections for mention text (e.g. \"You already have a connection with: X (pending), Y (draft).\"). */\n existingConnectionsForMention?: ExistingConnection[];\n /**\n * Orchestrator-only: accepted opportunities the persist step found between the\n * discoverer and a candidate counterparty (status='accepted'). Populated from\n * OpportunityGraphState.dedupAlreadyAccepted. Used by the discover_opportunities\n * tool to tell the LLM \"this pair is already connected — open the existing\n * chat rather than creating a new draft\". Empty for the ambient trigger.\n */\n alreadyAcceptedPairs?: Array<{ opportunityId: string; counterpartyUserId: string }>;\n /** When true, the chat agent should call create_intent(suggestedIntentDescription) and retry discovery. */\n createIntentSuggested?: boolean;\n /** Description to pass to create_intent when createIntentSuggested is true. */\n suggestedIntentDescription?: string;\n /** Internal steps for copy-debug (select_strategies, opportunity_graph, enrich, etc.). */\n debugSteps?: DiscoverDebugStep[];\n /** Pagination metadata -- present when there are more unevaluated candidates. */\n pagination?: {\n discoveryId: string;\n evaluated: number;\n remaining: number;\n };\n /** 0–3 decision questions produced by the orchestrator path. Omitted when none. */\n questions?: Question[];\n /** Debug metadata for `debugMeta.discoveryQuestions` plumbing. */\n discoveryQuestionsDebug?: {\n inputMode: \"transcripts\" | \"insights\";\n finalCount: number;\n strategies: QuestionStrategy[];\n durationMs: number;\n };\n}\n\n/** Input for the shared enrichment helper. */\ninterface EnrichOpportunitiesInput {\n opportunities: Opportunity[];\n database: ChatGraphCompositeDatabase;\n userId: string;\n chatSessionId?: string;\n minimalForChat?: boolean;\n presenter?: OpportunityPresenter;\n useHomeCardFormat?: boolean;\n debugSteps: DiscoverDebugStep[];\n /** IDs of pre-existing opportunities merged into the list; these preserve their real status. */\n existingOpportunityIds?: Set<string>;\n /** When set, bypass the embedding filter for this specific user (direct connection mode). */\n targetUserId?: string;\n}\n\n/**\n * Enrich raw opportunities with profile data, presentation (LLM or minimal),\n * and narrator chips. Shared by both `runDiscoverFromQuery` and `continueDiscovery`\n * to avoid duplicating the profile-lookup / presenter / card-formatting logic.\n *\n * @param input - Enrichment context (opportunities, database, viewer, presentation options).\n * @returns Formatted discovery candidates ready for chat or home card display.\n */\nasync function enrichOpportunities(\n input: EnrichOpportunitiesInput,\n): Promise<FormattedDiscoveryCandidate[]> {\n const {\n opportunities,\n database,\n userId,\n chatSessionId,\n minimalForChat,\n presenter,\n useHomeCardFormat,\n debugSteps,\n existingOpportunityIds,\n targetUserId,\n } = input;\n\n const baseEnrichedRaw = await Promise.all(\n opportunities.map(async (opp) => {\n const viewerIsIntroducer = opp.actors.some((a) => a.role === 'introducer' && a.userId === userId);\n // When the viewer is the introducer, the \"candidate\" for the card is the agent\n // (the discovered person), not the patient (the intro target / onBehalfOfUserId).\n // For non-introducer views, pick the first non-viewer, non-introducer actor.\n const nonViewerNonIntroducerActors = opp.actors.filter((a) => a.userId !== userId && a.role !== 'introducer');\n const candidateActor = viewerIsIntroducer\n ? (nonViewerNonIntroducerActors.find((a) => a.role === 'agent') ?? nonViewerNonIntroducerActors[0])\n : nonViewerNonIntroducerActors[0];\n const candidateUserId = candidateActor?.userId ?? \"\";\n const viewerActor = opp.actors.find((a) => a.userId === userId);\n const [profile, candidateUser] = candidateUserId\n ? await Promise.all([database.getProfile(candidateUserId), database.getUser(candidateUserId)])\n : [null, null];\n // Skip soft-deleted users (deletedAt is set)\n if (candidateUser && 'deletedAt' in candidateUser && candidateUser.deletedAt) return null;\n const isDirectTarget = targetUserId && candidateUserId === targetUserId;\n if (!isDirectTarget && !candidateUser?.isGhost && !profile) return null;\n const confidence =\n typeof opp.interpretation?.confidence === \"number\"\n ? opp.interpretation.confidence\n : parseFloat(String(opp.interpretation?.confidence ?? 0)) || 0;\n return {\n opportunity: opp,\n candidateUserId,\n viewerRole: viewerActor?.role ?? \"party\",\n viewerApproved: viewerActor?.approved === true,\n candidateUser,\n profile,\n confidence,\n };\n }),\n );\n const baseEnriched = baseEnrichedRaw.filter((item): item is NonNullable<typeof item> => item !== null);\n debugSteps.push({\n step: \"enrich_profiles\",\n detail: `${baseEnriched.length} profile(s)`,\n });\n\n // Batch-fetch user records (candidates, introducers, and other party actors) for name/avatar fallback.\n const allActorUserIds = new Set<string>();\n for (const item of baseEnriched) {\n for (const actor of item.opportunity.actors) {\n if (actor.userId && actor.userId !== userId) {\n allActorUserIds.add(actor.userId);\n }\n }\n }\n const candidateUserIds = [...allActorUserIds];\n const [viewerUser, ...userResults] = await Promise.all([\n database.getUser(userId),\n ...candidateUserIds.map((id) => database.getUser(id)),\n ]);\n const avatarByUserId = new Map<string, string | null>();\n const nameByUserId = new Map<string, string | null>();\n const isGhostByUserId = new Map<string, boolean>();\n candidateUserIds.forEach((id, i) => {\n const user = userResults[i] ?? null;\n avatarByUserId.set(id, user?.avatar ?? null);\n nameByUserId.set(id, user?.name ?? null);\n isGhostByUserId.set(id, user?.isGhost ?? false);\n });\n const viewerName = viewerUser?.name ?? undefined;\n\n // Retry name resolution for candidates whose name is still missing.\n // The profile or user record may not have been ready on the first fetch\n // (e.g. profile generation still in flight). One retry covers transient gaps.\n const missingNameIds = baseEnriched\n .map((item) => item.candidateUserId)\n .filter((id) => id && !nameByUserId.get(id) && !baseEnriched.find((b) => b.candidateUserId === id && b.profile?.identity?.name));\n if (missingNameIds.length > 0) {\n const retried = await Promise.all(\n missingNameIds.map(async (id) => {\n const [profile, user] = await Promise.all([\n database.getProfile(id),\n database.getUser(id),\n ]);\n return { id, profile, user };\n }),\n );\n for (const r of retried) {\n const name = r.profile?.identity?.name ?? r.user?.name ?? null;\n if (name) nameByUserId.set(r.id, name);\n // Also update the baseEnriched profile so counterpartName picks it up\n if (r.profile) {\n const item = baseEnriched.find((b) => b.candidateUserId === r.id);\n if (item && !item.profile) item.profile = r.profile;\n }\n if (r.user?.avatar && !avatarByUserId.get(r.id)) {\n avatarByUserId.set(r.id, r.user.avatar);\n }\n }\n logger.verbose(\"[enrichOpportunities] Retried name lookup for candidates with missing names\", {\n attempted: missingNameIds.length,\n resolved: retried.filter((r) => r.profile?.identity?.name ?? r.user?.name).length,\n });\n }\n\n let presentations: OpportunityPresentationResult[] | undefined;\n let homeCardPresentations: HomeCardPresentationResult[] | undefined;\n let presenterContexts:\n | (Awaited<ReturnType<typeof gatherPresenterContext>> | MinimalPresenterContext)[]\n | undefined;\n\n if (minimalForChat && baseEnriched.length > 0) {\n // Minimal path: no LLM, viewer-centric card text (introduce counterpart to viewer)\n const counterpartName = (n: {\n profile?: { identity?: { name?: string } } | null;\n candidateUserId: string;\n }) => n.profile?.identity?.name ?? nameByUserId.get(n.candidateUserId) ?? \"\";\n homeCardPresentations = baseEnriched.map((item) => {\n const name = counterpartName(item)?.trim();\n const reasoning = item.opportunity.interpretation?.reasoning ?? \"\";\n const introducerName = item.opportunity.detection?.createdByName ?? undefined;\n const viewerIsIntroducer = item.opportunity.actors.some(\n (a) => a.role === \"introducer\" && a.userId === userId,\n );\n\n // For introducer view, find the second party (target user) name\n let secondPartyName: string | undefined;\n if (viewerIsIntroducer) {\n const otherPartyActors = item.opportunity.actors.filter(\n (a) => a.role !== \"introducer\" && a.userId !== item.candidateUserId,\n );\n if (otherPartyActors.length > 0) {\n const otherUserId = otherPartyActors[0].userId;\n secondPartyName = nameByUserId.get(otherUserId) ?? undefined;\n }\n }\n\n const isCounterpartGhost = isGhostByUserId.get(item.candidateUserId) ?? false;\n const personalizedSummary = viewerCentricCardSummary(\n reasoning,\n name,\n MINIMAL_MAIN_TEXT_MAX_CHARS,\n viewerName,\n introducerName,\n );\n return {\n headline: viewerIsIntroducer && secondPartyName\n ? `${name} → ${secondPartyName}`\n : (name ? `Connection with ${name}` : \"Suggested connection\"),\n personalizedSummary,\n digestSummary: name\n ? `You might like meeting ${name} based on your current interests.`\n : \"This connection may be relevant to your current interests.\",\n suggestedAction: \"Start a conversation to connect.\",\n narratorRemark: narratorRemarkFromReasoning(reasoning, name, viewerName),\n primaryActionLabel: getPrimaryActionLabel(viewerIsIntroducer ? \"introducer\" : \"party\"),\n secondaryActionLabel: SECONDARY_ACTION_LABEL,\n mutualIntentsLabel: \"Suggested connection\",\n greeting: \"\",\n };\n });\n presenterContexts = baseEnriched.map((item) => ({\n introducerName: item.opportunity.detection.createdByName ?? undefined,\n })) as MinimalPresenterContext[];\n } else if (presenter && baseEnriched.length > 0) {\n try {\n presenterContexts = await Promise.all(\n baseEnriched.map(({ opportunity }) =>\n gatherPresenterContext(database, opportunity, userId),\n ),\n );\n\n if (useHomeCardFormat) {\n // Use full home card format with action labels, narrator remark, etc.\n const fullContexts = presenterContexts as Awaited<\n ReturnType<typeof gatherPresenterContext>\n >[];\n const homeCardInputs: HomeCardPresenterInput[] = fullContexts.map(\n (ctx, idx) => ({\n ...ctx,\n mutualIntentCount: undefined,\n opportunityStatus: baseEnriched[idx].opportunity.status,\n }),\n );\n const llmResults = await presenter.presentHomeCardBatch(\n homeCardInputs,\n { concurrency: 5 },\n );\n // Append hardcoded button labels to LLM results\n homeCardPresentations = llmResults.map((llm, idx) => ({\n ...llm,\n primaryActionLabel: getPrimaryActionLabel(baseEnriched[idx].viewerRole),\n secondaryActionLabel: SECONDARY_ACTION_LABEL,\n }));\n } else {\n // Use basic presentation format\n presentations = await presenter.presentBatch(\n presenterContexts as Awaited<\n ReturnType<typeof gatherPresenterContext>\n >[],\n {\n concurrency: 5,\n },\n );\n }\n } catch (error) {\n logger.warn(\n \"Presenter enrichment failed during opportunity discovery; returning base results without presentations\",\n {\n userId,\n opportunitiesCount: baseEnriched.length,\n useHomeCardFormat,\n error: error instanceof Error ? error.message : String(error),\n },\n );\n presentations = undefined;\n homeCardPresentations = undefined;\n }\n }\n\n const enriched: FormattedDiscoveryCandidate[] = baseEnriched.map(\n (item, idx) => {\n const homeCard = homeCardPresentations?.[idx];\n const ctx = presenterContexts?.[idx];\n\n // Build narrator chip for home card format\n let narratorChip: FormattedDiscoveryCandidate[\"narratorChip\"];\n if (homeCard) {\n const viewerIsIntroducer = item.opportunity.actors.some(\n (a) => a.role === \"introducer\" && a.userId === userId,\n );\n if (viewerIsIntroducer) {\n narratorChip = {\n name: \"You\",\n text: homeCard.narratorRemark,\n userId: userId,\n };\n } else {\n const introducerActor = item.opportunity.actors.find(\n (a) => a.role === \"introducer\" && a.userId !== userId,\n );\n if (introducerActor) {\n const introducerName =\n ctx?.introducerName ??\n nameByUserId.get(introducerActor.userId) ??\n \"Someone\";\n narratorChip = {\n name: introducerName,\n text: homeCard.narratorRemark,\n userId: introducerActor.userId,\n avatar: avatarByUserId.get(introducerActor.userId) ?? null,\n };\n } else {\n narratorChip = {\n name: \"Index\",\n text: homeCard.narratorRemark,\n };\n }\n }\n }\n\n const isGhost = isGhostByUserId.get(item.candidateUserId) ?? false;\n\n // Build secondParty for introducer view (the other non-introducer party)\n let secondParty: FormattedDiscoveryCandidate[\"secondParty\"];\n const viewerIsIntroducerForCard = item.opportunity.actors.some(\n (a) => a.role === \"introducer\" && a.userId === userId,\n );\n if (viewerIsIntroducerForCard) {\n const otherPartyActor = item.opportunity.actors.find(\n (a) => a.role !== \"introducer\" && a.userId !== item.candidateUserId,\n );\n if (otherPartyActor) {\n const otherName = nameByUserId.get(otherPartyActor.userId) ?? undefined;\n if (otherName) {\n secondParty = {\n name: otherName,\n avatar: avatarByUserId.get(otherPartyActor.userId) ?? null,\n userId: otherPartyActor.userId,\n };\n }\n }\n }\n\n return {\n opportunityId: item.opportunity.id,\n userId: item.candidateUserId,\n name: item.profile?.identity?.name ?? nameByUserId.get(item.candidateUserId) ?? undefined,\n avatar: avatarByUserId.get(item.candidateUserId) ?? null,\n bio: truncateForChat(item.profile?.identity?.bio),\n matchReason:\n truncateForChat(\n item.opportunity.interpretation?.reasoning ?? \"\",\n ) ?? \"\",\n score: item.confidence,\n status: chatSessionId && !existingOpportunityIds?.has(item.opportunity.id) ? \"draft\" : item.opportunity.status,\n viewerRole: item.viewerRole,\n viewerApproved: item.viewerApproved,\n candidateUser: item.candidateUser,\n isGhost,\n ...(presentations?.[idx] && { presentation: presentations[idx] }),\n ...(homeCard && {\n homeCardPresentation: homeCard,\n }),\n ...(narratorChip && { narratorChip }),\n ...(secondParty && { secondParty }),\n };\n },\n );\n debugSteps.push({\n step: \"format_cards\",\n detail: `${enriched.length} card(s)`,\n });\n\n return enriched;\n}\n\n/** Cached discovery session data stored in Redis. */\ninterface CachedDiscoverySession {\n candidates: CandidateMatch[];\n userId: string;\n onBehalfOfUserId?: string;\n query: string;\n indexScope: string[];\n options: OpportunityGraphOptions;\n /**\n * Carried across pagination so page 2+ stays on the same flow as page 1.\n * Without this, orchestrator runs would fall back to the 'ambient' default\n * mid-search and lose the shorter park window + accepted-pair dedup.\n */\n trigger?: 'ambient' | 'orchestrator';\n}\n\n/**\n * Run discovery from an ad-hoc query (e.g. \"find me a mentor\", \"who needs a React developer\").\n * The HyDE graph's LensInferrer automatically infers search lenses from the query.\n * Invokes the opportunity graph and returns formatted candidates suitable for chat display.\n */\nexport async function runDiscoverFromQuery(\n input: DiscoverInput,\n): Promise<DiscoverResult> {\n const {\n opportunityGraph,\n database,\n userId,\n query,\n indexScope,\n limit = 5,\n triggerIntentId,\n targetUserId,\n onBehalfOfUserId,\n chatSessionId,\n trigger,\n negotiateTimeoutMs,\n } = input;\n\n if (indexScope.length === 0) {\n return {\n found: false,\n count: 0,\n message:\n \"You need to join at least one network (community) to discover opportunities. Use read_networks to see available networks, or create one.\",\n };\n }\n\n const debugSteps: DiscoverDebugStep[] = [];\n\n // When query is empty, the opportunity graph uses the user's intents in scope (indexedIntents[0].payload)\n // Lens inference is handled automatically by the HyDE graph's LensInferrer\n const queryOrEmpty = query?.trim() ?? \"\";\n // Orchestrator discovery defers the initial status to the graph's\n // trigger-aware `resolveInitialStatus`, which opens at 'negotiating' so\n // the accepted-draft streaming flow can run. Ambient chat discovery still\n // wants the legacy 'draft' status so the chat-only lifecycle holds; other\n // ambient callers keep 'latent'.\n const isOrchestrator = trigger === 'orchestrator';\n const options: OpportunityGraphOptions = {\n limit,\n ...(!isOrchestrator && { initialStatus: chatSessionId ? \"draft\" : \"latent\" }),\n ...(chatSessionId ? { conversationId: chatSessionId } : {}),\n ...(negotiateTimeoutMs !== undefined && { negotiateTimeoutMs }),\n };\n\n return withCallLogging(\n logger,\n \"runDiscoverFromQuery\",\n {\n userId,\n queryPreview: queryOrEmpty\n ? queryOrEmpty.substring(0, 50)\n : \"(using user intents in scope)\",\n indexScopeCount: indexScope.length,\n limit,\n },\n async () => {\n const result = await invokeWithAbortSignal(opportunityGraph, {\n userId,\n searchQuery: queryOrEmpty || undefined,\n // A single index resolves to the strict networkId override (membership-\n // validated in the scope node). Multiple indexes (e.g. a network-scoped\n // agent's bound network + personal index) pass through as indexScope so\n // the graph stays bounded instead of falling back to all networks.\n networkId: indexScope.length === 1 ? indexScope[0] : undefined,\n ...(indexScope.length > 1 ? { indexScope } : {}),\n triggerIntentId,\n targetUserId,\n onBehalfOfUserId,\n options,\n ...(trigger && { trigger }),\n });\n\n // Extract trace from graph and append to debugSteps\n const graphTrace = Array.isArray(result.trace) ? result.trace : [];\n for (const t of graphTrace) {\n debugSteps.push({\n step: t.node,\n detail: t.detail,\n ...(t.data ? { data: t.data } : {}),\n });\n }\n\n // Bail early if the graph returned an error\n if (result.error) {\n logger.warn(\"runDiscoverFromQuery graph returned error\", { error: result.error });\n return {\n found: false,\n count: 0,\n message: \"Failed to find opportunities. Please try again.\",\n debugSteps,\n };\n }\n\n // Cache remaining candidates for pagination\n let pagination: DiscoverResult['pagination'] | undefined;\n const remainingCandidates: CandidateMatch[] = result.remainingCandidates || [];\n if (remainingCandidates.length > 0 && input.cache) {\n try {\n const discoveryId = crypto.randomUUID();\n const cacheKey = `discovery:${userId}:${discoveryId}`;\n await input.cache.set(cacheKey, {\n candidates: remainingCandidates,\n userId,\n onBehalfOfUserId,\n query: queryOrEmpty,\n indexScope,\n options,\n ...(trigger && { trigger }),\n } satisfies CachedDiscoverySession, { ttl: 1800 }); // 30 minutes\n pagination = {\n discoveryId,\n evaluated: (result.candidates?.length ?? 0) - remainingCandidates.length,\n remaining: remainingCandidates.length,\n };\n } catch (cacheErr) {\n logger.warn(\"Failed to cache discovery pagination\", {\n userId,\n error: cacheErr instanceof Error ? cacheErr.message : String(cacheErr),\n });\n }\n }\n\n // Refine phase: a sibling of the opportunity graph in the trace tree.\n // Holds the three post-discovery summarization steps. Each step is its\n // own traced agent so it appears as a leaf in the trace UI.\n //\n // Negotiation summary: compress each raw negotiation into a fixed-size\n // structured digest so the question generator's prompt stays small\n // (a 10-candidate turn used to balloon past 60 KB and stall upstream).\n // Decision questions: generate up to 3 clarifying questions from the\n // digests + chat context.\n const { questionPayload } = await tracePhase(\"Refine\", async () => {\n const negotiationDigests = await summarizeNegotiations({\n negotiations: result.discoveryNegotiations ?? [],\n summarizer: input.negotiationSummary,\n enableQuestions: input.enableQuestions ?? false,\n trigger,\n });\n const questionPayload = await maybeBuildQuestions({\n trigger,\n enableQuestions: input.enableQuestions ?? false,\n chatSummary: input.chatSummary,\n questionGenerator: input.questionGenerator,\n chatSessionId,\n graphResult: result,\n negotiationDigests,\n query: queryOrEmpty,\n questionerEnqueue: input.questionerEnqueue,\n userId: input.userId,\n });\n return { negotiationDigests, questionPayload };\n });\n\n if (result.createIntentSuggested && result.suggestedIntentDescription) {\n if (chatSessionId) {\n return {\n found: false,\n count: 0,\n message: \"No matching opportunities found. Try a different query.\",\n pagination,\n ...(questionPayload.questions !== undefined ? { questions: questionPayload.questions } : {}),\n ...(questionPayload.debug !== undefined ? { discoveryQuestionsDebug: questionPayload.debug } : {}),\n };\n }\n return {\n found: false,\n count: 0,\n createIntentSuggested: true,\n suggestedIntentDescription: result.suggestedIntentDescription,\n message:\n \"No matching opportunities; add an intent with the suggested description to improve discovery.\",\n debugSteps,\n pagination,\n ...(questionPayload.questions !== undefined ? { questions: questionPayload.questions } : {}),\n ...(questionPayload.debug !== undefined ? { discoveryQuestionsDebug: questionPayload.debug } : {}),\n };\n }\n\n let opportunities: Opportunity[] = Array.isArray(result.opportunities)\n ? result.opportunities\n : [];\n let existingOpportunityIds: Set<string> | undefined;\n const rawExistingBetweenActors = Array.isArray(result.existingBetweenActors)\n ? result.existingBetweenActors\n : [];\n // Orchestrator trigger populates this; ambient returns []. Kept as a\n // loosely-typed pass-through because DiscoverResult is consumed by\n // callers (chat tool, tests) that already model the narrower shape.\n const alreadyAcceptedPairs = Array.isArray(\n (result as { dedupAlreadyAccepted?: Array<{ opportunityId: string; counterpartyUserId: string }> })\n .dedupAlreadyAccepted,\n )\n ? (result as { dedupAlreadyAccepted: Array<{ opportunityId: string; counterpartyUserId: string }> })\n .dedupAlreadyAccepted\n : [];\n // Enrich existing-between-actors with names so the tool can say \"You already have a connection with X (pending).\"\n const existingConnections: ExistingConnection[] = await Promise.all(\n rawExistingBetweenActors.map(async (item) => {\n const user = await database.getUser(item.candidateUserId);\n return {\n userId: item.candidateUserId,\n name: user?.name ?? \"Someone\",\n ...(item.existingStatus ? { status: item.existingStatus } : {}),\n ...(item.existingOpportunityId ? { opportunityId: item.existingOpportunityId } : {}),\n };\n }),\n );\n if (existingConnections.length > 0) {\n logger.verbose(\"[runDiscoverFromQuery] Skipped duplicates; existing connections\", {\n count: existingConnections.length,\n userIds: existingConnections.map((c) => c.userId),\n });\n }\n // Only expose existing connections as cards when status is in EXISTING_CONNECTION_CARD_STATUSES (draft, latent, pending); others are mention-only.\n const existingConnectionsForCards = existingConnections.filter((c) =>\n c.status != null && EXISTING_CONNECTION_CARD_STATUSES.includes(c.status as typeof EXISTING_CONNECTION_CARD_STATUSES[number])\n );\n\n // Fetch full opportunity data for existing connections that should be shown as cards\n // and merge them with the newly created opportunities\n if (existingConnectionsForCards.length > 0) {\n const existingOpps = await Promise.all(\n existingConnectionsForCards\n .filter((c) => c.opportunityId)\n .map((c) => database.getOpportunity(c.opportunityId!))\n );\n const validExistingOpps = existingOpps.filter((o): o is Opportunity => o != null);\n if (validExistingOpps.length > 0) {\n logger.verbose(\"[runDiscoverFromQuery] Including existing opportunities as cards\", {\n count: validExistingOpps.length,\n ids: validExistingOpps.map((o) => o.id),\n });\n existingOpportunityIds = new Set(validExistingOpps.map((o) => o.id));\n opportunities = [...opportunities, ...validExistingOpps];\n }\n }\n\n // Chat discovery: when we have chatSessionId we just invoked the graph; all result.opportunities\n // were created in this call and belong to this session. Do not filter by status: the enricher\n // may set status to pending/latent when merging with related opportunities, so filtering to\n // \"draft\" would incorrectly drop them.\n if (chatSessionId && (result.opportunities?.length ?? 0) > 0) {\n logger.verbose(\"[runDiscoverFromQuery] Chat session opportunities from graph\", {\n count: opportunities.length,\n statuses: opportunities.map((o) => o.status),\n });\n }\n debugSteps.push({\n step: \"opportunity_graph\",\n detail: `${opportunities.length} opportunity(ies)${existingConnections.length > 0 ? `, ${existingConnections.length} existing` : \"\"}`,\n });\n\n if (opportunities.length === 0) {\n if (existingConnections.length > 0) {\n return {\n found: true,\n count: 0,\n message:\n \"No new opportunities created; you already have a connection with: \" +\n existingConnections.map((c) => `${c.name}${c.status ? ` (${c.status})` : \"\"}`).join(\", \") +\n \". View on your home page.\",\n existingConnections: existingConnectionsForCards,\n existingConnectionsForMention: existingConnections,\n ...(alreadyAcceptedPairs.length > 0 && { alreadyAcceptedPairs }),\n debugSteps,\n pagination,\n ...(questionPayload.questions !== undefined ? { questions: questionPayload.questions } : {}),\n ...(questionPayload.debug !== undefined ? { discoveryQuestionsDebug: questionPayload.debug } : {}),\n };\n }\n return {\n found: false,\n count: 0,\n message:\n \"No matching opportunities found. Try a different query or create intents to improve matching.\",\n ...(alreadyAcceptedPairs.length > 0 && { alreadyAcceptedPairs }),\n debugSteps,\n pagination,\n ...(questionPayload.questions !== undefined ? { questions: questionPayload.questions } : {}),\n ...(questionPayload.debug !== undefined ? { discoveryQuestionsDebug: questionPayload.debug } : {}),\n };\n }\n\n const enriched = await enrichOpportunities({\n opportunities,\n database,\n userId,\n chatSessionId,\n minimalForChat: input.minimalForChat,\n presenter: input.presenter,\n useHomeCardFormat: input.useHomeCardFormat,\n debugSteps,\n existingOpportunityIds,\n targetUserId,\n });\n\n return {\n found: true,\n count: enriched.length,\n opportunities: enriched,\n ...(existingConnectionsForCards.length > 0 ? { existingConnections: existingConnectionsForCards } : {}),\n ...(existingConnections.length > 0 ? { existingConnectionsForMention: existingConnections } : {}),\n ...(alreadyAcceptedPairs.length > 0 ? { alreadyAcceptedPairs } : {}),\n debugSteps,\n pagination,\n ...(questionPayload.questions !== undefined ? { questions: questionPayload.questions } : {}),\n ...(questionPayload.debug !== undefined ? { discoveryQuestionsDebug: questionPayload.debug } : {}),\n };\n },\n { context: { userId }, logOutput: false },\n ).catch((err) => {\n return {\n found: false,\n count: 0,\n message: \"Failed to find opportunities. Please try again.\",\n };\n });\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// DECISION-QUESTION HELPER\n// ─────────────────────────────────────────────────────────────────────────────\n\ntype GraphResultLike = {\n sourceProfile?: SourceProfileData | null;\n discoveryNegotiations?: DiscoveryNegotiation[];\n discoverySummary?: DiscoverySummary | null;\n};\n\ninterface MaybeBuildQuestionsInput {\n trigger: 'ambient' | 'orchestrator' | undefined;\n enableQuestions: boolean;\n chatSummary: ChatSummaryReader | undefined;\n questionGenerator: QuestionGeneratorReader | undefined;\n chatSessionId: string | undefined;\n graphResult: GraphResultLike;\n /** Pre-built per-negotiation digests. Pass [] when summarization is unavailable or disabled. */\n negotiationDigests: DiscoveryNegotiationDigest[];\n query: string;\n /** Optional async enqueue callback for background question generation. */\n questionerEnqueue?: DiscoverInput['questionerEnqueue'];\n /** User ID needed for the enqueue payload. */\n userId?: string;\n}\n\n/**\n * Run the negotiation summarizer over every negotiation in this discovery turn.\n * Each summarization is independent — run them concurrently via Promise.all.\n * When the summarizer is missing (no LLM available) or fails for an individual\n * negotiation, fall back to a deterministic digest so the downstream generator\n * still has structured input.\n */\nasync function summarizeNegotiations(args: {\n negotiations: DiscoveryNegotiation[];\n summarizer: NegotiationSummaryReader | undefined;\n enableQuestions: boolean;\n trigger: 'ambient' | 'orchestrator' | undefined;\n}): Promise<DiscoveryNegotiationDigest[]> {\n // Skip the LLM round-trip entirely when questions won't be built.\n if (!args.enableQuestions || args.trigger !== 'orchestrator') return [];\n if (args.negotiations.length === 0) return [];\n\n const perNegTimeoutMs = parsePositiveIntEnv(\n \"NEGOTIATION_SUMMARY_TIMEOUT_MS\",\n NEGOTIATION_SUMMARY_TIMEOUT_MS_DEFAULT,\n );\n const callerSignal = requestContext.getStore()?.abortSignal;\n\n return traceAgent(\n `Negotiation summary (${args.negotiations.length})`,\n () =>\n Promise.all(\n args.negotiations.map(async (n) => {\n if (!args.summarizer) return buildFallbackDigest(n);\n // Per-negotiation deadline: one slow OpenRouter route used to\n // dominate the post-discovery tail. With a cap, an aborted\n // summarizer falls back to a deterministic digest so the\n // question generator still has structured input.\n const signal = combineWithDeadline(callerSignal, perNegTimeoutMs);\n try {\n const d = await args.summarizer.summarize(n, { signal });\n return d ?? buildFallbackDigest(n);\n } catch (err) {\n // Attribute cause from err.name (AbortError), not from\n // signal.aborted — the latter is read post-catch and can race a\n // deadline-trip-after-unrelated-error, producing a misleading log.\n const aborted = err instanceof Error && err.name === \"AbortError\";\n logger.warn(\"negotiationSummary.summarize threw — using fallback digest\", {\n counterpartyHint: n.counterpartyHint,\n aborted,\n error: err instanceof Error ? err.message : String(err),\n });\n return buildFallbackDigest(n);\n }\n }),\n ),\n (digests) => `${digests.length} digest${digests.length === 1 ? \"\" : \"s\"}`,\n );\n}\n\nasync function maybeBuildQuestions(args: MaybeBuildQuestionsInput): Promise<{\n questions?: Question[];\n debug?: DiscoverResult[\"discoveryQuestionsDebug\"];\n}> {\n if (!args.enableQuestions) return {};\n if (args.trigger !== 'orchestrator') return {};\n\n // Hardcoded — `insights` mode is planned for a later slice. Warn if the env\n // var is set so operators aren't surprised when reporting still says\n // \"transcripts\".\n if (process.env.DISCOVERY_QUESTIONS_INPUT_MODE === \"insights\") {\n logger.warn(\"DISCOVERY_QUESTIONS_INPUT_MODE=insights is not yet implemented; falling back to transcripts\");\n }\n const inputMode: \"transcripts\" | \"insights\" = \"transcripts\";\n\n let chatContext: ChatContextDigest | undefined;\n if (args.chatSummary && args.chatSessionId) {\n const sessionId = args.chatSessionId;\n const summary = args.chatSummary;\n chatContext = await traceAgent(\n \"Chat summary\",\n async () => {\n try {\n return (await summary.getDigest(sessionId)) ?? undefined;\n } catch (err) {\n logger.warn(\"chatSummary.getDigest threw — proceeding without digest\", {\n sessionId,\n error: err instanceof Error ? err.message : String(err),\n });\n return undefined;\n }\n },\n (digest) => (digest ? \"loaded\" : \"empty\"),\n );\n }\n\n // ── Async enqueue path ──────────────────────────────────────────────────\n // When questionerEnqueue is provided, dispatch question generation\n // asynchronously to the background QuestionerQueue. This replaces the\n // inline generator path. Questions will be persisted to DB by the queue\n // worker and served via GET /api/questions.\n if (args.questionerEnqueue && args.userId) {\n const summary = args.graphResult.discoverySummary ?? {\n totalCandidates: 0,\n opportunitiesFound: 0,\n noOpportunityCount: 0,\n timeoutCount: 0,\n roleDistribution: {},\n };\n\n const enqueueInput = buildDiscoveryQuestionInput({\n query: args.query,\n sourceProfile: args.graphResult.sourceProfile ?? null,\n negotiationDigests: args.negotiationDigests,\n summary,\n chatContext,\n now: new Date().toISOString(),\n });\n\n try {\n await args.questionerEnqueue({\n mode: 'discovery',\n userId: args.userId,\n sourceType: 'discovery',\n sourceId: args.chatSessionId ?? crypto.randomUUID(),\n context: enqueueInput,\n conversationId: args.chatSessionId,\n });\n logger.info(\"Question generation enqueued to QuestionerQueue\", {\n userId: args.userId,\n trigger: args.trigger,\n });\n } catch (err) {\n logger.warn(\"Failed to enqueue question generation\", {\n error: err instanceof Error ? err.message : String(err),\n });\n }\n return {};\n }\n\n // ── Inline generator path (backward compat) ────────────────────────────\n if (!args.questionGenerator) return {};\n\n const negotiationDigests = args.negotiationDigests;\n const summary = args.graphResult.discoverySummary ?? {\n totalCandidates: 0,\n opportunitiesFound: 0,\n noOpportunityCount: 0,\n timeoutCount: 0,\n roleDistribution: {},\n };\n\n const input = buildDiscoveryQuestionInput({\n query: args.query,\n sourceProfile: args.graphResult.sourceProfile ?? null,\n negotiationDigests,\n summary,\n chatContext,\n now: new Date().toISOString(),\n });\n\n const questionGenerator = args.questionGenerator;\n const generatorStart = Date.now();\n const questionsTimeoutMs = parsePositiveIntEnv(\n \"DISCOVERY_QUESTIONS_TIMEOUT_MS\",\n DISCOVERY_QUESTIONS_TIMEOUT_MS_DEFAULT,\n );\n const questionsSignal = combineWithDeadline(\n requestContext.getStore()?.abortSignal,\n questionsTimeoutMs,\n );\n const genResult = await traceAgent(\n \"Decision questions\",\n async () => {\n try {\n return await questionGenerator.generate(input, { signal: questionsSignal });\n } catch (err) {\n logger.warn(\"questionGenerator.generate threw — suppressing questions\", {\n error: err instanceof Error ? err.message : String(err),\n });\n return null;\n }\n },\n (r) => {\n const count = r?.questions?.length ?? 0;\n return `${count} question${count === 1 ? \"\" : \"s\"}`;\n },\n );\n const durationMs = Date.now() - generatorStart;\n\n const finalCount = genResult?.questions?.length ?? 0;\n const strategies: QuestionStrategy[] = genResult?.strategies ?? [];\n\n return {\n ...(genResult && genResult.questions.length > 0 ? { questions: genResult.questions } : {}),\n debug: {\n inputMode,\n finalCount,\n strategies,\n durationMs,\n },\n };\n}\n\n/**\n * Continue a paginated discovery by evaluating the next batch of cached candidates.\n * Loads candidates from Redis, invokes the opportunity graph in continue_discovery mode,\n * then enriches and returns the results with updated pagination metadata.\n *\n * @param input - Continuation context (graph, database, cache, discoveryId, etc.).\n * @returns Discovery result with enriched opportunities and pagination state.\n */\nexport async function continueDiscovery(input: {\n opportunityGraph: CompiledOpportunityGraph;\n database: ChatGraphCompositeDatabase;\n cache: Cache;\n userId: string;\n discoveryId: string;\n /** If provided, validates the cached session's indexScope contains this index. */\n expectedIndexId?: string;\n limit?: number;\n chatSessionId?: string;\n minimalForChat?: boolean;\n presenter?: OpportunityPresenter;\n useHomeCardFormat?: boolean;\n}): Promise<DiscoverResult> {\n const {\n opportunityGraph,\n database,\n cache,\n userId,\n discoveryId,\n expectedIndexId,\n limit = 20,\n chatSessionId,\n } = input;\n const cacheKey = `discovery:${userId}:${discoveryId}`;\n\n const cached = await cache.get<CachedDiscoverySession>(cacheKey);\n\n if (!cached) {\n return {\n found: false,\n count: 0,\n message: \"Discovery session expired or not found. Please start a new search.\",\n };\n }\n\n // Validate that the cached session's scope matches the current chat context\n if (expectedIndexId && !cached.indexScope.includes(expectedIndexId)) {\n return {\n found: false,\n count: 0,\n message: \"Discovery session was created in a different context. Please start a new search.\",\n };\n }\n\n const debugSteps: DiscoverDebugStep[] = [];\n\n const result = await invokeWithAbortSignal(opportunityGraph, {\n userId,\n searchQuery: cached.query || undefined,\n candidates: cached.candidates,\n operationMode: 'continue_discovery' as const,\n onBehalfOfUserId: cached.onBehalfOfUserId,\n // Carry the original trigger so page 2+ stays on the same flow as page\n // 1 (orchestrator negotiations with 60s park window + accepted-pair\n // dedup, or ambient with 5-min park window).\n ...(cached.trigger && { trigger: cached.trigger }),\n options: {\n ...cached.options,\n limit,\n ...(chatSessionId ? { conversationId: chatSessionId } : {}),\n },\n });\n\n // Extract trace from graph and append to debugSteps\n const graphTrace = result.trace || [];\n for (const t of graphTrace) {\n debugSteps.push({\n step: t.node,\n detail: t.detail,\n ...(t.data ? { data: t.data } : {}),\n });\n }\n\n // Bail early if the graph returned an error\n if (result.error) {\n logger.warn(\"continueDiscovery graph returned error\", { error: result.error });\n return {\n found: false,\n count: 0,\n message: \"Discovery continuation failed. Please start a new search.\",\n debugSteps,\n };\n }\n\n // Update cache with remaining candidates or delete if exhausted\n const remaining: CandidateMatch[] = result.remainingCandidates || [];\n let pagination: DiscoverResult['pagination'] | undefined;\n try {\n if (remaining.length > 0) {\n await cache.set(cacheKey, {\n ...cached,\n candidates: remaining,\n } satisfies CachedDiscoverySession, { ttl: 1800 });\n pagination = {\n discoveryId,\n evaluated: cached.candidates.length - remaining.length,\n remaining: remaining.length,\n };\n } else {\n await cache.delete(cacheKey);\n }\n } catch (cacheErr) {\n logger.warn(\"Failed to update discovery pagination cache\", {\n userId,\n discoveryId,\n error: cacheErr instanceof Error ? cacheErr.message : String(cacheErr),\n });\n }\n\n // Check for opportunities in result\n const opportunities: Opportunity[] = Array.isArray(result.opportunities) ? result.opportunities : [];\n\n if (opportunities.length === 0) {\n return {\n found: false,\n count: 0,\n message: \"No more matching opportunities found in the remaining candidates.\",\n debugSteps,\n pagination,\n };\n }\n\n const enriched = await enrichOpportunities({\n opportunities,\n database,\n userId,\n chatSessionId,\n minimalForChat: input.minimalForChat,\n presenter: input.presenter,\n useHomeCardFormat: input.useHomeCardFormat,\n debugSteps,\n });\n\n return {\n found: true,\n count: enriched.length,\n opportunities: enriched,\n debugSteps,\n pagination,\n };\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"opportunity.enricher.d.ts","sourceRoot":"/","sources":["opportunity/opportunity.enricher.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EACV,qBAAqB,EACrB,WAAW,EAIX,iBAAiB,EAClB,MAAM,4CAA4C,CAAC;AAEpD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,4CAA4C,CAAC;AAS3E;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,iCAAiC,EAAE,iBAAiB,EAA2C,CAAC;AAE7G,MAAM,MAAM,gBAAgB,GAAG;IAC7B,yBAAyB,CACvB,QAAQ,EAAE,MAAM,EAAE,EAClB,OAAO,CAAC,EAAE;QAAE,kBAAkB,CAAC,EAAE,OAAO,CAAC;QAAC,QAAQ,CAAC,EAAE,iBAAiB,EAAE,CAAC;QAAC,eAAe,CAAC,EAAE,iBAAiB,EAAE,CAAA;KAAE,GAChH,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,gBAAgB,GACxB;IAAE,QAAQ,EAAE,KAAK,CAAC;IAAC,IAAI,EAAE,qBAAqB,CAAA;CAAE,GAChD;IAAE,QAAQ,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,qBAAqB,CAAC;IAAC,UAAU,EAAE,MAAM,EAAE,CAAC;IAAC,cAAc,EAAE,iBAAiB,CAAA;CAAE,CAAC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AY,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAC9C,MAAM,gBAAgB,GACpB,OAAO,oBAAoB,CAAC,UAAU,KAAK,QAAQ;QACjD,CAAC,CAAC,MAAM,CAAC,oBAAoB,CAAC,UAAU,CAAC;QACzC,CAAC,CAAC,oBAAoB,CAAC,UAAU,CAAC;IAEtC,MAAM,YAAY,GAA0B;QAC1C,GAAG,OAAO;QACV,SAAS,EAAE;YACT,GAAG,OAAO,CAAC,SAAS;YACpB,MAAM,EAAE,YAAY;YACpB,YAAY;SACb;QACD,MAAM,EAAE,YAAY;QACpB,cAAc,EAAE,oBAAoB;QACpC,UAAU,EAAE,gBAAgB;KAC7B,CAAC;IAEF,MAAM,cAAc,GAAG,qBAAqB,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IAE3F,MAAM,CAAC,OAAO,CAAC,iCAAiC,EAAE;QAChD,YAAY;QACZ,UAAU,EAAE,YAAY,CAAC,MAAM;KAChC,CAAC,CAAC;IAEH,OAAO;QACL,QAAQ,EAAE,IAAI;QACd,IAAI,EAAE,YAAY;QAClB,UAAU,EAAE,YAAY;QACxB,cAAc;KACf,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Opportunity enricher: when creating an opportunity, find overlapping existing\n * opportunities (by non-introducer actor userId), check semantic relatedness, and\n * optionally merge into a single enriched opportunity and expire the old one(s).\n */\n\nimport type {\n CreateOpportunityData,\n Opportunity,\n OpportunityActor,\n OpportunityInterpretation,\n OpportunitySignal,\n OpportunityStatus,\n} from '../shared/interfaces/database.interface.js';\nimport { getAbortSignalConfig } from '../shared/agent/model-signal.js';\nimport type { Embedder } from '../shared/interfaces/embedder.interface.js';\nimport type { Id } from '../shared/interfaces/database.interface.js';\nimport { protocolLogger } from '../shared/observability/protocol.logger.js';\n\nconst logger = protocolLogger('OpportunityEnricher');\n\nconst DEFAULT_SIMILARITY_THRESHOLD = 0.7;\nconst MIN_REASONING_LENGTH_FOR_EMBEDDING = 10;\n\n/**\n * Statuses excluded from the merge-candidate pool by default.\n *\n * - 'accepted': the pair already connected — do NOT fold a new discovery into\n * the historical opp. IND-237 surfaces the existing conversation separately.\n * - 'negotiating': a negotiation is in-flight for this pair; rolling a new\n * candidate into it would blur the outcome of the active turn. Wait for the\n * negotiation to finalize (→ draft/pending/rejected/stalled) first, then\n * enrichment can pick it up on the next pass.\n * - 'expired': already superseded by a later enriched opportunity. Without this\n * exclusion, each enrichment cycle re-merges previously-expired enriched\n * opportunities, compounding their actor arrays into 100+ entries.\n *\n * Exported for callers that want to extend rather than replace the default.\n */\nexport const DEFAULT_ENRICHER_EXCLUDE_STATUSES: OpportunityStatus[] = ['accepted', 'negotiating', 'expired'];\n\nexport type EnricherDatabase = {\n findOpportunitiesByActors(\n actorIds: string[],\n options?: { includeIntroducers?: boolean; statuses?: OpportunityStatus[]; excludeStatuses?: OpportunityStatus[] }\n ): Promise<Opportunity[]>;\n};\n\nexport type EnrichmentResult =\n | { enriched: false; data: CreateOpportunityData }\n | { enriched: true; data: CreateOpportunityData; expiredIds: string[]; resolvedStatus: OpportunityStatus };\n\nexport type EnrichOrCreateOptions = {\n similarityThreshold?: number;\n /**\n * Statuses to exclude from the merge-candidate pool. Defaults to\n * {@link DEFAULT_ENRICHER_EXCLUDE_STATUSES} (`['accepted', 'negotiating']`).\n * Pass an empty array `[]` to consider all statuses.\n */\n excludeStatuses?: OpportunityStatus[];\n};\n\n/**\n * Cosine similarity between two vectors (0–1).\n */\nfunction cosineSimilarity(a: number[], b: number[]): number {\n if (a.length !== b.length || a.length === 0) return 0;\n let dot = 0;\n let normA = 0;\n let normB = 0;\n for (let i = 0; i < a.length; i++) {\n dot += a[i] * b[i];\n normA += a[i] * a[i];\n normB += b[i] * b[i];\n }\n const denom = Math.sqrt(normA) * Math.sqrt(normB);\n if (denom === 0) return 0;\n const sim = dot / denom;\n return Math.max(0, Math.min(1, sim));\n}\n\n/**\n * Resolve enriched opportunity status from related opportunities' statuses and the incoming status.\n * Priority: accepted > pending > rejected > stalled > draft (only when incoming is draft) > latent.\n * The incoming status is included so we do not wrongly downgrade when the new opportunity has a higher-priority status.\n * When incoming is 'draft' (e.g. from in-chat discovery), we preserve draft so the opportunity stays chat-only and\n * does not appear on the home view (home excludes draft).\n * When incoming is NOT draft (e.g. 'latent' from the background broker), existing draft status does NOT contaminate\n * the result — the broker-created opportunity retains its own status and can appear on the home view.\n */\nfunction resolveEnrichedStatus(relatedStatuses: string[], incomingStatus?: string): OpportunityStatus {\n const statuses = incomingStatus ? [...relatedStatuses, incomingStatus] : relatedStatuses;\n if (statuses.includes('accepted')) return 'accepted';\n if (statuses.includes('pending')) return 'pending';\n if (statuses.includes('rejected')) return 'rejected';\n if (statuses.includes('stalled')) return 'stalled';\n if (incomingStatus === 'draft') return 'draft';\n return 'latent';\n}\n\n/**\n * Extract non-introducer actor userIds from create data.\n */\nfunction getNonIntroducerUserIds(data: CreateOpportunityData): Id<'users'>[] {\n const ids = data.actors\n .filter((a) => a.role !== 'introducer')\n .map((a) => a.userId);\n return [...new Set(ids)];\n}\n\n/**\n * Extract intent IDs from actors.\n */\nfunction getIntentIdsFromActors(actors: OpportunityActor[]): Set<string> {\n const ids = new Set<string>();\n for (const a of actors) {\n if (a.intent) ids.add(a.intent);\n }\n return ids;\n}\n\n/**\n * Check if two opportunities share at least one intent ID.\n */\nfunction shareIntentIds(\n newData: CreateOpportunityData,\n existing: Opportunity\n): boolean {\n const newIntents = getIntentIdsFromActors(newData.actors);\n const existingIntents = getIntentIdsFromActors(existing.actors);\n for (const id of newIntents) {\n if (existingIntents.has(id)) return true;\n }\n return false;\n}\n\n/**\n * Merge actors from new data and existing opportunities: union by (networkId, userId, intent),\n * preserving all unique introducers and preferring newer role on conflict.\n */\nfunction mergeActors(\n newData: CreateOpportunityData,\n existingList: Opportunity[]\n): OpportunityActor[] {\n const key = (a: OpportunityActor) =>\n `${a.networkId}:${a.userId}:${a.intent ?? ''}`;\n const map = new Map<string, OpportunityActor>();\n\n // Add all from existing first (older)\n for (const opp of existingList) {\n for (const a of opp.actors) {\n map.set(key(a), a);\n }\n }\n // Add/overwrite with new (newer wins for same key, e.g. role)\n for (const a of newData.actors) {\n map.set(key(a), a);\n }\n\n return [...map.values()];\n}\n\n/**\n * Merge interpretation: single reasoning (new data only), max confidence, merged signals.\n * We use only the new opportunity's reasoning to avoid repetitive concatenation when\n * multiple overlapping opportunities share the same or similar text (e.g. same pair\n * across indexes), which previously produced long duplicated paragraphs in chat cards.\n */\nfunction mergeInterpretation(\n newData: CreateOpportunityData,\n existingList: Opportunity[]\n): OpportunityInterpretation {\n const reasoning = newData.interpretation.reasoning;\n\n let maxConf =\n typeof newData.interpretation.confidence === 'number'\n ? newData.interpretation.confidence\n : parseFloat(String(newData.interpretation.confidence ?? 0));\n for (const o of existingList) {\n const c = o.interpretation?.confidence;\n const cNum = typeof c === 'number' ? c : parseFloat(String(c ?? 0));\n if (cNum > maxConf) maxConf = cNum;\n }\n const confidence = maxConf;\n\n const signals: OpportunitySignal[] = [\n ...(newData.interpretation.signals ?? []),\n ...existingList.flatMap((o) => o.interpretation?.signals ?? []),\n ];\n const seen = new Set<string>();\n const dedupedSignals = signals.filter((s) => {\n const k = `${s.type}:${s.detail ?? ''}`;\n if (seen.has(k)) return false;\n seen.add(k);\n return true;\n });\n\n return {\n category: newData.interpretation.category,\n reasoning,\n confidence: typeof confidence === 'string' ? parseFloat(confidence) : confidence,\n signals: dedupedSignals.length > 0 ? dedupedSignals : newData.interpretation.signals,\n };\n}\n\n/**\n * Enrich or create: find overlapping opportunities, filter by semantic relatedness\n * using a two-phase approach, merge actors and interpretation into a single\n * CreateOpportunityData, and return the data plus IDs to expire. If no related\n * overlap, return original data unchanged.\n *\n * Phase 1 — Intent check (free, no API call):\n * Shared intent IDs mean the same declared user goal drove both opportunities.\n * This is rare because the IntentReconciler already deduplicates intents per-user\n * upstream, but when it fires it is definitive.\n *\n * Phase 2 — Batched embedding similarity (one API call for all remaining):\n * For opportunities without shared intents, embed all reasoning texts in a single\n * batch call and compare cosine similarity. Cross-user intent comparison (e.g.\n * Alice's \"find ML co-founder\" vs Bob's \"join ML startup\") is implicitly handled\n * here since reasoning text synthesizes both users' intents.\n */\nexport async function enrichOrCreate(\n database: EnricherDatabase,\n embedder: Embedder,\n newData: CreateOpportunityData,\n options?: EnrichOrCreateOptions\n): Promise<EnrichmentResult> {\n const actorUserIds = getNonIntroducerUserIds(newData);\n if (actorUserIds.length === 0) {\n return { enriched: false, data: newData };\n }\n\n const excludeStatuses = options?.excludeStatuses ?? DEFAULT_ENRICHER_EXCLUDE_STATUSES;\n const overlapping = await database.findOpportunitiesByActors(actorUserIds, { excludeStatuses });\n if (overlapping.length === 0) {\n return { enriched: false, data: newData };\n }\n\n const threshold = options?.similarityThreshold ?? DEFAULT_SIMILARITY_THRESHOLD;\n\n // Phase 1: Intent-based relatedness (free, strongest signal)\n const related: Opportunity[] = [];\n const remaining: Opportunity[] = [];\n for (const opp of overlapping) {\n if (shareIntentIds(newData, opp)) {\n related.push(opp);\n } else {\n remaining.push(opp);\n }\n }\n\n // Phase 2: Batched embedding similarity (one API call for all remaining)\n if (remaining.length > 0) {\n const newReasoning = (newData.interpretation?.reasoning ?? '').trim();\n\n // Only embed opps where both reasonings are long enough\n const embeddable: Opportunity[] = [];\n for (const opp of remaining) {\n const existingReasoning = (opp.interpretation?.reasoning ?? '').trim();\n if (\n newReasoning.length >= MIN_REASONING_LENGTH_FOR_EMBEDDING &&\n existingReasoning.length >= MIN_REASONING_LENGTH_FOR_EMBEDDING\n ) {\n embeddable.push(opp);\n }\n // Short reasoning + no shared intents (Phase 1 missed) → not related\n }\n\n if (embeddable.length > 0) {\n try {\n const textsToEmbed = [\n newReasoning,\n ...embeddable.map((o) => (o.interpretation?.reasoning ?? '').trim()),\n ];\n const vectors = (await embedder.generate(textsToEmbed, undefined, getAbortSignalConfig())) as number[][];\n const newVec = vectors[0];\n if (newVec?.length) {\n for (let i = 0; i < embeddable.length; i++) {\n const existingVec = vectors[i + 1];\n if (existingVec?.length && cosineSimilarity(newVec, existingVec) >= threshold) {\n related.push(embeddable[i]);\n }\n }\n }\n } catch (e) {\n logger.warn('[Enricher] Embedding check failed; intent-matched opportunities already captured', { error: e });\n // Phase 1 matches are preserved; remaining opps without shared intents are not related\n }\n }\n }\n\n if (related.length === 0) {\n return { enriched: false, data: newData };\n }\n\n const mergedActors = mergeActors(newData, related);\n const mergedInterpretation = mergeInterpretation(newData, related);\n const enrichedFrom = related.map((o) => o.id);\n const mergedConfidence =\n typeof mergedInterpretation.confidence === 'number'\n ? String(mergedInterpretation.confidence)\n : mergedInterpretation.confidence;\n\n const enrichedData: CreateOpportunityData = {\n ...newData,\n detection: {\n ...newData.detection,\n source: 'enrichment',\n enrichedFrom,\n },\n actors: mergedActors,\n interpretation: mergedInterpretation,\n confidence: mergedConfidence,\n };\n\n const resolvedStatus = resolveEnrichedStatus(related.map((o) => o.status), newData.status);\n\n logger.verbose('[Enricher] Enriched opportunity', {\n enrichedFrom,\n actorCount: mergedActors.length,\n });\n\n return {\n enriched: true,\n data: enrichedData,\n expiredIds: enrichedFrom,\n resolvedStatus,\n };\n}\n"]}
1
+ {"version":3,"file":"opportunity.enricher.js","sourceRoot":"/","sources":["opportunity/opportunity.enricher.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAGvE,OAAO,EAAE,cAAc,EAAE,MAAM,4CAA4C,CAAC;AAE5E,MAAM,MAAM,GAAG,cAAc,CAAC,qBAAqB,CAAC,CAAC;AAErD,MAAM,4BAA4B,GAAG,GAAG,CAAC;AACzC,MAAM,kCAAkC,GAAG,EAAE,CAAC;AAE9C;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM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AO;QACL,QAAQ,EAAE,IAAI;QACd,IAAI,EAAE,YAAY;QAClB,UAAU,EAAE,YAAY;QACxB,cAAc;KACf,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Opportunity enricher: when creating an opportunity, find overlapping existing\n * opportunities (by non-introducer actor userId), check semantic relatedness, and\n * optionally merge into a single enriched opportunity and expire the old one(s).\n */\n\nimport type { CreateOpportunityData, Opportunity, OpportunityActor, OpportunityInterpretation, OpportunitySignal, OpportunityStatus } from '../shared/interfaces/database.interface.js';\nimport { getAbortSignalConfig } from '../shared/agent/model-signal.js';\nimport type { Embedder } from '../shared/interfaces/embedder.interface.js';\nimport type { Id } from '../shared/interfaces/database.interface.js';\nimport { protocolLogger } from '../shared/observability/protocol.logger.js';\n\nconst logger = protocolLogger('OpportunityEnricher');\n\nconst DEFAULT_SIMILARITY_THRESHOLD = 0.7;\nconst MIN_REASONING_LENGTH_FOR_EMBEDDING = 10;\n\n/**\n * Statuses excluded from the merge-candidate pool by default.\n *\n * - 'accepted': the pair already connected — do NOT fold a new discovery into\n * the historical opp. IND-237 surfaces the existing conversation separately.\n * - 'negotiating': a negotiation is in-flight for this pair; rolling a new\n * candidate into it would blur the outcome of the active turn. Wait for the\n * negotiation to finalize (→ draft/pending/rejected/stalled) first, then\n * enrichment can pick it up on the next pass.\n * - 'expired': already superseded by a later enriched opportunity. Without this\n * exclusion, each enrichment cycle re-merges previously-expired enriched\n * opportunities, compounding their actor arrays into 100+ entries.\n *\n * Exported for callers that want to extend rather than replace the default.\n */\nexport const DEFAULT_ENRICHER_EXCLUDE_STATUSES: OpportunityStatus[] = ['accepted', 'negotiating', 'expired'];\n\nexport type EnricherDatabase = {\n findOpportunitiesByActors(\n actorIds: string[],\n options?: { includeIntroducers?: boolean; statuses?: OpportunityStatus[]; excludeStatuses?: OpportunityStatus[] }\n ): Promise<Opportunity[]>;\n};\n\nexport type EnrichmentResult =\n | { enriched: false; data: CreateOpportunityData }\n | { enriched: true; data: CreateOpportunityData; expiredIds: string[]; resolvedStatus: OpportunityStatus };\n\nexport type EnrichOrCreateOptions = {\n similarityThreshold?: number;\n /**\n * Statuses to exclude from the merge-candidate pool. Defaults to\n * {@link DEFAULT_ENRICHER_EXCLUDE_STATUSES} (`['accepted', 'negotiating']`).\n * Pass an empty array `[]` to consider all statuses.\n */\n excludeStatuses?: OpportunityStatus[];\n};\n\n/**\n * Cosine similarity between two vectors (0–1).\n */\nfunction cosineSimilarity(a: number[], b: number[]): number {\n if (a.length !== b.length || a.length === 0) return 0;\n let dot = 0;\n let normA = 0;\n let normB = 0;\n for (let i = 0; i < a.length; i++) {\n dot += a[i] * b[i];\n normA += a[i] * a[i];\n normB += b[i] * b[i];\n }\n const denom = Math.sqrt(normA) * Math.sqrt(normB);\n if (denom === 0) return 0;\n const sim = dot / denom;\n return Math.max(0, Math.min(1, sim));\n}\n\n/**\n * Resolve enriched opportunity status from related opportunities' statuses and the incoming status.\n * Priority: accepted > pending > rejected > stalled > draft (only when incoming is draft) > latent.\n * The incoming status is included so we do not wrongly downgrade when the new opportunity has a higher-priority status.\n * When incoming is 'draft' (e.g. from in-chat discovery), we preserve draft so the opportunity stays chat-only and\n * does not appear on the home view (home excludes draft).\n * When incoming is NOT draft (e.g. 'latent' from the background broker), existing draft status does NOT contaminate\n * the result — the broker-created opportunity retains its own status and can appear on the home view.\n */\nfunction resolveEnrichedStatus(relatedStatuses: string[], incomingStatus?: string): OpportunityStatus {\n const statuses = incomingStatus ? [...relatedStatuses, incomingStatus] : relatedStatuses;\n if (statuses.includes('accepted')) return 'accepted';\n if (statuses.includes('pending')) return 'pending';\n if (statuses.includes('rejected')) return 'rejected';\n if (statuses.includes('stalled')) return 'stalled';\n if (incomingStatus === 'draft') return 'draft';\n return 'latent';\n}\n\n/**\n * Extract non-introducer actor userIds from create data.\n */\nfunction getNonIntroducerUserIds(data: CreateOpportunityData): Id<'users'>[] {\n const ids = data.actors\n .filter((a) => a.role !== 'introducer')\n .map((a) => a.userId);\n return [...new Set(ids)];\n}\n\n/**\n * Extract intent IDs from actors.\n */\nfunction getIntentIdsFromActors(actors: OpportunityActor[]): Set<string> {\n const ids = new Set<string>();\n for (const a of actors) {\n if (a.intent) ids.add(a.intent);\n }\n return ids;\n}\n\n/**\n * Check if two opportunities share at least one intent ID.\n */\nfunction shareIntentIds(\n newData: CreateOpportunityData,\n existing: Opportunity\n): boolean {\n const newIntents = getIntentIdsFromActors(newData.actors);\n const existingIntents = getIntentIdsFromActors(existing.actors);\n for (const id of newIntents) {\n if (existingIntents.has(id)) return true;\n }\n return false;\n}\n\n/**\n * Merge actors from new data and existing opportunities: union by (networkId, userId, intent),\n * preserving all unique introducers and preferring newer role on conflict.\n */\nfunction mergeActors(\n newData: CreateOpportunityData,\n existingList: Opportunity[]\n): OpportunityActor[] {\n const key = (a: OpportunityActor) =>\n `${a.networkId}:${a.userId}:${a.intent ?? ''}`;\n const map = new Map<string, OpportunityActor>();\n\n // Add all from existing first (older)\n for (const opp of existingList) {\n for (const a of opp.actors) {\n map.set(key(a), a);\n }\n }\n // Add/overwrite with new (newer wins for same key, e.g. role)\n for (const a of newData.actors) {\n map.set(key(a), a);\n }\n\n return [...map.values()];\n}\n\n/**\n * Merge interpretation: single reasoning (new data only), max confidence, merged signals.\n * We use only the new opportunity's reasoning to avoid repetitive concatenation when\n * multiple overlapping opportunities share the same or similar text (e.g. same pair\n * across indexes), which previously produced long duplicated paragraphs in chat cards.\n */\nfunction mergeInterpretation(\n newData: CreateOpportunityData,\n existingList: Opportunity[]\n): OpportunityInterpretation {\n const reasoning = newData.interpretation.reasoning;\n\n let maxConf =\n typeof newData.interpretation.confidence === 'number'\n ? newData.interpretation.confidence\n : parseFloat(String(newData.interpretation.confidence ?? 0));\n for (const o of existingList) {\n const c = o.interpretation?.confidence;\n const cNum = typeof c === 'number' ? c : parseFloat(String(c ?? 0));\n if (cNum > maxConf) maxConf = cNum;\n }\n const confidence = maxConf;\n\n const signals: OpportunitySignal[] = [\n ...(newData.interpretation.signals ?? []),\n ...existingList.flatMap((o) => o.interpretation?.signals ?? []),\n ];\n const seen = new Set<string>();\n const dedupedSignals = signals.filter((s) => {\n const k = `${s.type}:${s.detail ?? ''}`;\n if (seen.has(k)) return false;\n seen.add(k);\n return true;\n });\n\n return {\n category: newData.interpretation.category,\n reasoning,\n confidence: typeof confidence === 'string' ? parseFloat(confidence) : confidence,\n signals: dedupedSignals.length > 0 ? dedupedSignals : newData.interpretation.signals,\n };\n}\n\n/**\n * Enrich or create: find overlapping opportunities, filter by semantic relatedness\n * using a two-phase approach, merge actors and interpretation into a single\n * CreateOpportunityData, and return the data plus IDs to expire. If no related\n * overlap, return original data unchanged.\n *\n * Phase 1 — Intent check (free, no API call):\n * Shared intent IDs mean the same declared user goal drove both opportunities.\n * This is rare because the IntentReconciler already deduplicates intents per-user\n * upstream, but when it fires it is definitive.\n *\n * Phase 2 — Batched embedding similarity (one API call for all remaining):\n * For opportunities without shared intents, embed all reasoning texts in a single\n * batch call and compare cosine similarity. Cross-user intent comparison (e.g.\n * Alice's \"find ML co-founder\" vs Bob's \"join ML startup\") is implicitly handled\n * here since reasoning text synthesizes both users' intents.\n */\nexport async function enrichOrCreate(\n database: EnricherDatabase,\n embedder: Embedder,\n newData: CreateOpportunityData,\n options?: EnrichOrCreateOptions\n): Promise<EnrichmentResult> {\n const actorUserIds = getNonIntroducerUserIds(newData);\n if (actorUserIds.length === 0) {\n return { enriched: false, data: newData };\n }\n\n const excludeStatuses = options?.excludeStatuses ?? DEFAULT_ENRICHER_EXCLUDE_STATUSES;\n const overlapping = await database.findOpportunitiesByActors(actorUserIds, { excludeStatuses });\n if (overlapping.length === 0) {\n return { enriched: false, data: newData };\n }\n\n const threshold = options?.similarityThreshold ?? DEFAULT_SIMILARITY_THRESHOLD;\n\n // Phase 1: Intent-based relatedness (free, strongest signal)\n const related: Opportunity[] = [];\n const remaining: Opportunity[] = [];\n for (const opp of overlapping) {\n if (shareIntentIds(newData, opp)) {\n related.push(opp);\n } else {\n remaining.push(opp);\n }\n }\n\n // Phase 2: Batched embedding similarity (one API call for all remaining)\n if (remaining.length > 0) {\n const newReasoning = (newData.interpretation?.reasoning ?? '').trim();\n\n // Only embed opps where both reasonings are long enough\n const embeddable: Opportunity[] = [];\n for (const opp of remaining) {\n const existingReasoning = (opp.interpretation?.reasoning ?? '').trim();\n if (\n newReasoning.length >= MIN_REASONING_LENGTH_FOR_EMBEDDING &&\n existingReasoning.length >= MIN_REASONING_LENGTH_FOR_EMBEDDING\n ) {\n embeddable.push(opp);\n }\n // Short reasoning + no shared intents (Phase 1 missed) → not related\n }\n\n if (embeddable.length > 0) {\n try {\n const textsToEmbed = [\n newReasoning,\n ...embeddable.map((o) => (o.interpretation?.reasoning ?? '').trim()),\n ];\n const vectors = (await embedder.generate(textsToEmbed, undefined, getAbortSignalConfig())) as number[][];\n const newVec = vectors[0];\n if (newVec?.length) {\n for (let i = 0; i < embeddable.length; i++) {\n const existingVec = vectors[i + 1];\n if (existingVec?.length && cosineSimilarity(newVec, existingVec) >= threshold) {\n related.push(embeddable[i]);\n }\n }\n }\n } catch (e) {\n logger.warn('[Enricher] Embedding check failed; intent-matched opportunities already captured', { error: e });\n // Phase 1 matches are preserved; remaining opps without shared intents are not related\n }\n }\n }\n\n if (related.length === 0) {\n return { enriched: false, data: newData };\n }\n\n const mergedActors = mergeActors(newData, related);\n const mergedInterpretation = mergeInterpretation(newData, related);\n const enrichedFrom = related.map((o) => o.id);\n const mergedConfidence =\n typeof mergedInterpretation.confidence === 'number'\n ? String(mergedInterpretation.confidence)\n : mergedInterpretation.confidence;\n\n const enrichedData: CreateOpportunityData = {\n ...newData,\n detection: {\n ...newData.detection,\n source: 'enrichment',\n enrichedFrom,\n },\n actors: mergedActors,\n interpretation: mergedInterpretation,\n confidence: mergedConfidence,\n };\n\n const resolvedStatus = resolveEnrichedStatus(related.map((o) => o.status), newData.status);\n\n logger.verbose('[Enricher] Enriched opportunity', {\n enrichedFrom,\n actorCount: mergedActors.length,\n });\n\n return {\n enriched: true,\n data: enrichedData,\n expiredIds: enrichedFrom,\n resolvedStatus,\n };\n}\n"]}