@indexnetwork/protocol 4.1.2-rc.293.1 → 4.2.0-rc.294.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,6 +27,12 @@ export type NegotiationContextDatabase = Pick<NegotiationGraphDatabase, 'getNego
27
27
  */
28
28
  export interface NegotiationContext {
29
29
  status: OpportunityStatus;
30
+ /**
31
+ * Conversation/task id of the A2A negotiation that produced this opportunity.
32
+ * Lets callers deep-link to the negotiation trace (e.g. `/chat/:conversationId`).
33
+ * Present whenever a negotiation task exists (i.e. context is non-null).
34
+ */
35
+ conversationId: string;
30
36
  turnCount: number;
31
37
  /** Max turns allowed for this negotiation (0 = unlimited). */
32
38
  turnCap: number;
@@ -1 +1 @@
1
- {"version":3,"file":"negotiation-context.loader.d.ts","sourceRoot":"/","sources":["opportunity/negotiation-context.loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,MAAM,4CAA4C,CAAC;AAC9G,OAAO,KAAK,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAK/F;;;GAGG;AACH,MAAM,MAAM,0BAA0B,GAAG,IAAI,CAC3C,wBAAwB,EACxB,kCAAkC,GAAG,4BAA4B,GAAG,qBAAqB,CAC1F,CAAC;AAEF;;;;GAIG;AACH,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,iBAAiB,CAAC;IAC1B,SAAS,EAAE,MAAM,CAAC;IAClB,8DAA8D;IAC9D,OAAO,EAAE,MAAM,CAAC;IAChB,qDAAqD;IACrD,OAAO,CAAC,EAAE,kBAAkB,CAAC;IAC7B,qDAAqD;IACrD,KAAK,CAAC,EAAE,eAAe,EAAE,CAAC;CAC3B;AAKD;;;;;;;;;GASG;AACH,wBAAsB,sBAAsB,CAC1C,EAAE,EAAE,0BAA0B,EAC9B,aAAa,EAAE,MAAM,EACrB,iBAAiB,EAAE,iBAAiB,GACnC,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC,CA+BpC"}
1
+ {"version":3,"file":"negotiation-context.loader.d.ts","sourceRoot":"/","sources":["opportunity/negotiation-context.loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,MAAM,4CAA4C,CAAC;AAC9G,OAAO,KAAK,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAK/F;;;GAGG;AACH,MAAM,MAAM,0BAA0B,GAAG,IAAI,CAC3C,wBAAwB,EACxB,kCAAkC,GAAG,4BAA4B,GAAG,qBAAqB,CAC1F,CAAC;AAEF;;;;GAIG;AACH,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,iBAAiB,CAAC;IAC1B;;;;OAIG;IACH,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,8DAA8D;IAC9D,OAAO,EAAE,MAAM,CAAC;IAChB,qDAAqD;IACrD,OAAO,CAAC,EAAE,kBAAkB,CAAC;IAC7B,qDAAqD;IACrD,KAAK,CAAC,EAAE,eAAe,EAAE,CAAC;CAC3B;AAKD;;;;;;;;;GASG;AACH,wBAAsB,sBAAsB,CAC1C,EAAE,EAAE,0BAA0B,EAC9B,aAAa,EAAE,MAAM,EACrB,iBAAiB,EAAE,iBAAiB,GACnC,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC,CAgCpC"}
@@ -41,12 +41,13 @@ export async function loadNegotiationContext(db, opportunityId, opportunityStatu
41
41
  const turns = extractTurns(messages);
42
42
  const turnCount = turns.length;
43
43
  if (opportunityStatus === 'negotiating') {
44
- return { status: opportunityStatus, turnCount, turnCap };
44
+ return { status: opportunityStatus, conversationId: task.conversationId, turnCount, turnCap };
45
45
  }
46
46
  const artifacts = await db.getArtifactsForTask(task.id);
47
47
  const outcome = extractOutcome(artifacts);
48
48
  return {
49
49
  status: opportunityStatus,
50
+ conversationId: task.conversationId,
50
51
  turnCount,
51
52
  turnCap,
52
53
  ...(outcome ? { outcome } : {}),
@@ -1 +1 @@
1
- {"version":3,"file":"negotiation-context.loader.js","sourceRoot":"/","sources":["opportunity/negotiation-context.loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,EAAE,cAAc,EAAE,MAAM,4CAA4C,CAAC;AAE5E,MAAM,MAAM,GAAG,cAAc,CAAC,0BAA0B,CAAC,CAAC;AA2B1D,MAAM,4BAA4B,GAAqC,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC;AACtG,MAAM,iCAAiC,GAAG,qBAAqB,CAAC;AAEhE;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,EAA8B,EAC9B,aAAqB,EACrB,iBAAoC;IAEpC,IAAI,4BAA4B,CAAC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,CAAC;QAC7D,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,EAAE,CAAC,gCAAgC,CAAC,aAAa,CAAC,CAAC;IACtE,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,CAAC,OAAO,CAAC,2CAA2C,EAAE,EAAE,aAAa,EAAE,iBAAiB,EAAE,CAAC,CAAC;QAClG,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,OAAO,GAAG,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE,UAAU,CAAC,IAAI,CAAC,CAAC;IAE3D,MAAM,QAAQ,GAAG,MAAM,EAAE,CAAC,0BAA0B,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;IAC1E,MAAM,KAAK,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;IACrC,MAAM,SAAS,GAAG,KAAK,CAAC,MAAM,CAAC;IAE/B,IAAI,iBAAiB,KAAK,aAAa,EAAE,CAAC;QACxC,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AC5F,IAAI,CAAC,eAAe;QAAE,OAAO,SAAS,CAAC;IAEvC,MAAM,QAAQ,GAAI,eAAe,CAAC,KAAkD,CAAC,IAAI,CACvF,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CACzB,CAAC;IACF,OAAO,QAAQ,EAAE,IAAsC,CAAC;AAC1D,CAAC","sourcesContent":["/**\n * Negotiation context loader: given an opportunity, fetches the attached\n * negotiation task's transcript and outcome so the home-card presenter can\n * explain *why* the opportunity surfaced.\n *\n * For `draft`, `latent`, and `expired` opportunities, no negotiation has\n * happened (or no longer matters) so the loader returns null.\n *\n * For `negotiating` opportunities, only `turnCount` / `turnCap` are returned\n * — the presenter renders a templated chip without invoking the LLM.\n *\n * For `pending`, `stalled`, `accepted`, and `rejected` opportunities, the\n * full transcript and outcome are included so the prompt can ground its\n * explanation in concrete turn content.\n */\n\nimport type { NegotiationGraphDatabase, OpportunityStatus } from '../shared/interfaces/database.interface.js';\nimport type { NegotiationOutcome, NegotiationTurn } from '../negotiation/negotiation.state.js';\nimport { protocolLogger } from '../shared/observability/protocol.logger.js';\n\nconst logger = protocolLogger('NegotiationContextLoader');\n\n/**\n * Narrow slice of {@link NegotiationGraphDatabase} required by the loader. Kept\n * minimal so call sites can opt into a smaller surface.\n */\nexport type NegotiationContextDatabase = Pick<\n NegotiationGraphDatabase,\n 'getNegotiationTaskForOpportunity' | 'getMessagesForConversation' | 'getArtifactsForTask'\n>;\n\n/**\n * Snapshot of a negotiation surfaced to the presenter. `turns` and `outcome`\n * are only populated for post-negotiation statuses (pending/stalled/\n * accepted/rejected); `negotiating` gets only the counters.\n */\nexport interface NegotiationContext {\n status: OpportunityStatus;\n turnCount: number;\n /** Max turns allowed for this negotiation (0 = unlimited). */\n turnCap: number;\n /** Only present when status is not `negotiating`. */\n outcome?: NegotiationOutcome;\n /** Only present when status is not `negotiating`. */\n turns?: NegotiationTurn[];\n}\n\nconst STATUSES_WITH_NO_NEGOTIATION: ReadonlyArray<OpportunityStatus> = ['draft', 'latent', 'expired'];\nconst NEGOTIATION_OUTCOME_ARTIFACT_NAME = 'negotiation-outcome';\n\n/**\n * Loads the negotiation context for an opportunity.\n *\n * @param db - Narrow slice of NegotiationGraphDatabase.\n * @param opportunityId - Opportunity to load negotiation context for.\n * @param opportunityStatus - Current opportunity status. Used to gate loading\n * and to decide which fields to populate.\n * @returns NegotiationContext, or null when no meaningful negotiation exists\n * (draft/latent/expired) or when the task lookup fails.\n */\nexport async function loadNegotiationContext(\n db: NegotiationContextDatabase,\n opportunityId: string,\n opportunityStatus: OpportunityStatus,\n): Promise<NegotiationContext | null> {\n if (STATUSES_WITH_NO_NEGOTIATION.includes(opportunityStatus)) {\n return null;\n }\n\n const task = await db.getNegotiationTaskForOpportunity(opportunityId);\n if (!task) {\n logger.verbose('No negotiation task found for opportunity', { opportunityId, opportunityStatus });\n return null;\n }\n\n const turnCap = readNumber(task.metadata, 'maxTurns') ?? 0;\n\n const messages = await db.getMessagesForConversation(task.conversationId);\n const turns = extractTurns(messages);\n const turnCount = turns.length;\n\n if (opportunityStatus === 'negotiating') {\n return { status: opportunityStatus, turnCount, turnCap };\n }\n\n const artifacts = await db.getArtifactsForTask(task.id);\n const outcome = extractOutcome(artifacts);\n\n return {\n status: opportunityStatus,\n turnCount,\n turnCap,\n ...(outcome ? { outcome } : {}),\n turns,\n };\n}\n\nfunction readNumber(metadata: Record<string, unknown> | null, key: string): number | undefined {\n if (!metadata) return undefined;\n const value = metadata[key];\n return typeof value === 'number' ? value : undefined;\n}\n\nfunction extractTurns(messages: Array<{ parts: unknown[] }>): NegotiationTurn[] {\n const turns: NegotiationTurn[] = [];\n for (const message of messages) {\n const dataPart = (message.parts as Array<{ kind?: string; data?: unknown }>).find((p) => p.kind === 'data');\n if (dataPart?.data) {\n turns.push(dataPart.data as NegotiationTurn);\n }\n }\n return turns;\n}\n\nfunction extractOutcome(\n artifacts: Array<{ name: string | null; parts: unknown[] }>,\n): NegotiationOutcome | undefined {\n const outcomeArtifact = artifacts.find((a) => a.name === NEGOTIATION_OUTCOME_ARTIFACT_NAME);\n if (!outcomeArtifact) return undefined;\n\n const dataPart = (outcomeArtifact.parts as Array<{ kind?: string; data?: unknown }>).find(\n (p) => p.kind === 'data',\n );\n return dataPart?.data as NegotiationOutcome | undefined;\n}\n"]}
1
+ {"version":3,"file":"negotiation-context.loader.js","sourceRoot":"/","sources":["opportunity/negotiation-context.loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,EAAE,cAAc,EAAE,MAAM,4CAA4C,CAAC;AAE5E,MAAM,MAAM,GAAG,cAAc,CAAC,0BAA0B,CAAC,CAAC;AAiC1D,MAAM,4BAA4B,GAAqC,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC;AACtG,MAAM,iCAAiC,GAAG,qBAAqB,CAAC;AAEhE;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,EAA8B,EAC9B,aAAqB,EACrB,iBAAoC;IAEpC,IAAI,4BAA4B,CAAC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,CAAC;QAC7D,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,EAAE,CAAC,gCAAgC,CAAC,aAAa,CAAC,CAAC;IACtE,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,CAAC,OAAO,CAAC,2CAA2C,EAAE,EAAE,aAAa,EAAE,iBAAiB,EAAE,CAAC,CAAC;QAClG,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,OAAO,GAAG,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE,UAAU,CAAC,IAAI,CAAC,CAAC;IAE3D,MAAM,QAAQ,GAAG,MAAM,EAAE,CAAC,0BAA0B,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;IAC1E,MAAM,KAAK,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;IACr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;AACf,CAAC;AAED,SAAS,cAAc,CACrB,SAA2D;IAE3D,MAAM,eAAe,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,iCAAiC,CAAC,CAAC;IAC5F,IAAI,CAAC,eAAe;QAAE,OAAO,SAAS,CAAC;IAEvC,MAAM,QAAQ,GAAI,eAAe,CAAC,KAAkD,CAAC,IAAI,CACvF,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CACzB,CAAC;IACF,OAAO,QAAQ,EAAE,IAAsC,CAAC;AAC1D,CAAC","sourcesContent":["/**\n * Negotiation context loader: given an opportunity, fetches the attached\n * negotiation task's transcript and outcome so the home-card presenter can\n * explain *why* the opportunity surfaced.\n *\n * For `draft`, `latent`, and `expired` opportunities, no negotiation has\n * happened (or no longer matters) so the loader returns null.\n *\n * For `negotiating` opportunities, only `turnCount` / `turnCap` are returned\n * — the presenter renders a templated chip without invoking the LLM.\n *\n * For `pending`, `stalled`, `accepted`, and `rejected` opportunities, the\n * full transcript and outcome are included so the prompt can ground its\n * explanation in concrete turn content.\n */\n\nimport type { NegotiationGraphDatabase, OpportunityStatus } from '../shared/interfaces/database.interface.js';\nimport type { NegotiationOutcome, NegotiationTurn } from '../negotiation/negotiation.state.js';\nimport { protocolLogger } from '../shared/observability/protocol.logger.js';\n\nconst logger = protocolLogger('NegotiationContextLoader');\n\n/**\n * Narrow slice of {@link NegotiationGraphDatabase} required by the loader. Kept\n * minimal so call sites can opt into a smaller surface.\n */\nexport type NegotiationContextDatabase = Pick<\n NegotiationGraphDatabase,\n 'getNegotiationTaskForOpportunity' | 'getMessagesForConversation' | 'getArtifactsForTask'\n>;\n\n/**\n * Snapshot of a negotiation surfaced to the presenter. `turns` and `outcome`\n * are only populated for post-negotiation statuses (pending/stalled/\n * accepted/rejected); `negotiating` gets only the counters.\n */\nexport interface NegotiationContext {\n status: OpportunityStatus;\n /**\n * Conversation/task id of the A2A negotiation that produced this opportunity.\n * Lets callers deep-link to the negotiation trace (e.g. `/chat/:conversationId`).\n * Present whenever a negotiation task exists (i.e. context is non-null).\n */\n conversationId: string;\n turnCount: number;\n /** Max turns allowed for this negotiation (0 = unlimited). */\n turnCap: number;\n /** Only present when status is not `negotiating`. */\n outcome?: NegotiationOutcome;\n /** Only present when status is not `negotiating`. */\n turns?: NegotiationTurn[];\n}\n\nconst STATUSES_WITH_NO_NEGOTIATION: ReadonlyArray<OpportunityStatus> = ['draft', 'latent', 'expired'];\nconst NEGOTIATION_OUTCOME_ARTIFACT_NAME = 'negotiation-outcome';\n\n/**\n * Loads the negotiation context for an opportunity.\n *\n * @param db - Narrow slice of NegotiationGraphDatabase.\n * @param opportunityId - Opportunity to load negotiation context for.\n * @param opportunityStatus - Current opportunity status. Used to gate loading\n * and to decide which fields to populate.\n * @returns NegotiationContext, or null when no meaningful negotiation exists\n * (draft/latent/expired) or when the task lookup fails.\n */\nexport async function loadNegotiationContext(\n db: NegotiationContextDatabase,\n opportunityId: string,\n opportunityStatus: OpportunityStatus,\n): Promise<NegotiationContext | null> {\n if (STATUSES_WITH_NO_NEGOTIATION.includes(opportunityStatus)) {\n return null;\n }\n\n const task = await db.getNegotiationTaskForOpportunity(opportunityId);\n if (!task) {\n logger.verbose('No negotiation task found for opportunity', { opportunityId, opportunityStatus });\n return null;\n }\n\n const turnCap = readNumber(task.metadata, 'maxTurns') ?? 0;\n\n const messages = await db.getMessagesForConversation(task.conversationId);\n const turns = extractTurns(messages);\n const turnCount = turns.length;\n\n if (opportunityStatus === 'negotiating') {\n return { status: opportunityStatus, conversationId: task.conversationId, turnCount, turnCap };\n }\n\n const artifacts = await db.getArtifactsForTask(task.id);\n const outcome = extractOutcome(artifacts);\n\n return {\n status: opportunityStatus,\n conversationId: task.conversationId,\n turnCount,\n turnCap,\n ...(outcome ? { outcome } : {}),\n turns,\n };\n}\n\nfunction readNumber(metadata: Record<string, unknown> | null, key: string): number | undefined {\n if (!metadata) return undefined;\n const value = metadata[key];\n return typeof value === 'number' ? value : undefined;\n}\n\nfunction extractTurns(messages: Array<{ parts: unknown[] }>): NegotiationTurn[] {\n const turns: NegotiationTurn[] = [];\n for (const message of messages) {\n const dataPart = (message.parts as Array<{ kind?: string; data?: unknown }>).find((p) => p.kind === 'data');\n if (dataPart?.data) {\n turns.push(dataPart.data as NegotiationTurn);\n }\n }\n return turns;\n}\n\nfunction extractOutcome(\n artifacts: Array<{ name: string | null; parts: unknown[] }>,\n): NegotiationOutcome | undefined {\n const outcomeArtifact = artifacts.find((a) => a.name === NEGOTIATION_OUTCOME_ARTIFACT_NAME);\n if (!outcomeArtifact) return undefined;\n\n const dataPart = (outcomeArtifact.parts as Array<{ kind?: string; data?: unknown }>).find(\n (p) => p.kind === 'data',\n );\n return dataPart?.data as NegotiationOutcome | undefined;\n}\n"]}
@@ -302,7 +302,7 @@ Produce headline, personalizedSummary (2-3 sentences in "you" language), suggest
302
302
  // *because* the negotiation happened. Trailing the block lets weaker
303
303
  // models lean on surface signals and ignore the transcript entirely.
304
304
  const negotiationDirective = negotiationBlock
305
- ? `\nIMPORTANT: This opportunity surfaced because the agents negotiated and converged. Your personalizedSummary MUST reference at least one specific signal from the NEGOTIATION CONTEXT block below — what concern was raised, what was confirmed, what the agents agreed on. Do not produce a generic skill-complementarity summary; that's what every card looked like before this negotiation happened. Use the transcript to explain *why this specific match* surfaced now.\n`
305
+ ? `\nIMPORTANT: This opportunity surfaced because the agents negotiated and converged. Both your personalizedSummary AND your digestSummary MUST reference at least one specific signal from the NEGOTIATION CONTEXT block below — what concern was raised, what was confirmed, what the agents agreed on. The digestSummary is the one-line morning-brief sentence a user reads before deciding to act, so it must communicate *why this specific match* surfaced now (the negotiation that led to it), not a generic skill-complementarity line. Do not produce the generic summary every card looked like before this negotiation happened.\n`
306
306
  : "";
307
307
  const humanContent = `
308
308
  ${negotiationBlock}${negotiationDirective}
@@ -1 +1 @@
1
- {"version":3,"file":"opportunity.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QAAQ,EAAE,MAAM,CAAC,QAAQ,IAAI,YAAY;QACzC,UAAU,EACR,OAAO,MAAM,CAAC,UAAU,KAAK,QAAQ;YACnC,CAAC,CAAC,MAAM,CAAC,UAAU;YACnB,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;QACrD,cAAc;QACd,SAAS,EAAE,WAAW,EAAE,KAAK,IAAI,cAAc,IAAI,EAAE;QACrD,UAAU,EAAE,OAAO,CAAC,IAAI,IAAI,OAAO;QACnC,cAAc;QACd,cAAc;KACf,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["/**\n * Opportunity Presenter Agent\n *\n * Generates personalized, second-person explanations of why an opportunity\n * matters to the viewing user. Uses full opportunity data (interpretation,\n * actors, profiles, intents, index) to produce headline, personalizedSummary,\n * and suggestedAction for chat tools and user-facing surfaces.\n */\n\nimport type { Runnable } from \"@langchain/core/runnables\";\nimport { HumanMessage, SystemMessage } from \"@langchain/core/messages\";\nimport { z } from \"zod\";\n\nimport { Timed } from \"../shared/observability/performance.js\";\n\nimport { protocolLogger } from \"../shared/observability/protocol.logger.js\";\nimport { createModel } from \"../shared/agent/model.config.js\";\nimport { viewerCentricCardSummary } from \"./opportunity.presentation.js\";\nimport type { Opportunity } from \"../shared/interfaces/database.interface.js\";\nimport type { ChatGraphCompositeDatabase } from \"../shared/interfaces/database.interface.js\";\nimport type { NegotiationContext } from \"./negotiation-context.loader.js\";\nimport { stripUuids, stripIntroducerMentions, truncateAtBoundary } from \"./opportunity.presentation.js\";\n\n/**\n * Minimal database interface required by gatherPresenterContext.\n * Any database adapter that implements these three methods can be passed.\n */\nexport type PresenterDatabase = Pick<\n ChatGraphCompositeDatabase,\n \"getProfile\" | \"getActiveIntents\" | \"getNetwork\" | \"getPremisesForUser\"\n>;\n\nconst logger = protocolLogger(\"OpportunityPresenter\");\nconst LLM_TIMEOUT_MS = 20_000;\n\nconst model = createModel(\"opportunityPresenter\");\n\nconst GREETING_DESCRIPTION =\n \"A 2-4 sentence first-person message the viewer could send to the counterpart, in the viewer's voice, referencing what they have in common. Plain prose only — no markdown, no greeting prefix like 'Hey {Name},'. Example body: 'Saw we're both working on regenerative coordination tooling — your post on consent flows resonated. Would love to compare notes if you have time this week.'\";\n\n// ──────────────────────────────────────────────────────────────\n// SCHEMA & TYPES\n// ──────────────────────────────────────────────────────────────\n\nconst PresentationSchema = z.object({\n headline: z\n .string()\n .describe(\n \"Short, compelling headline for this opportunity (e.g., 'A React expert who needs your design skills')\",\n ),\n personalizedSummary: z\n .string()\n .describe(\n \"2-3 sentence explanation using 'you' language, explaining why this opportunity is specifically valuable for the viewer based on their intents and profile\",\n ),\n suggestedAction: z.string().describe(\"Brief suggested next step\"),\n greeting: z.string().max(500).describe(GREETING_DESCRIPTION),\n});\n\nconst responseFormat = z.object({\n presentation: PresentationSchema,\n});\n\nexport type OpportunityPresentationResult = z.infer<typeof PresentationSchema>;\n\n/** Input for home-card presenter call; extends PresenterInput with optional mutual intent count. */\nexport interface HomeCardPresenterInput extends PresenterInput {\n /** Number of overlapping intents (for generating mutualIntentsLabel). */\n mutualIntentCount?: number;\n /**\n * Snapshot of the opportunity's negotiation, if one exists. When status is\n * `negotiating`, the presenter returns a templated chip without invoking\n * the LLM. For `pending`/`stalled`/`accepted`/`rejected`, the full\n * transcript and outcome ground the LLM's explanation.\n */\n negotiationContext?: NegotiationContext;\n}\n\n/** LLM-generated fields for home-card presentation (buttons are hardcoded by callers, not LLM-generated). */\nexport const HomeCardLLMSchema = z.object({\n headline: z\n .string()\n .describe(\"Short, compelling headline for this opportunity\"),\n personalizedSummary: z\n .string()\n .describe(\n \"2-3 sentence explanation in 'you' language for the main card body\",\n ),\n digestSummary: z\n .string()\n .max(220)\n .describe(\n \"One concise digest-ready sentence for a morning brief. It must be addressed to the viewer and mention the counterpart by name, e.g. 'You might like meeting Paul because ...'. No markdown.\",\n ),\n suggestedAction: z\n .string()\n .describe(\"Brief suggested next step (e.g. CTA line)\"),\n narratorRemark: z\n .string()\n .max(80)\n .describe(\n \"One short sentence for the narrator chip, max ~80 chars (e.g. who is suggesting and why)\",\n ),\n mutualIntentsLabel: z\n .string()\n .max(48)\n .describe(\n \"Short line for the subtitle under the other party name (e.g. '3 mutual intents', 'Shared interests', 'Aligned goals'). NEVER output '0 mutual intents' — use a qualitative phrase like 'Shared interests' when no numeric count is available.\",\n ),\n greeting: z.string().max(500).describe(GREETING_DESCRIPTION),\n});\n\n/** LLM-generated result from presentHomeCard (callers append button labels from opportunity.constants). */\nexport type HomeCardLLMResult = z.infer<typeof HomeCardLLMSchema>;\n\n/** Full home-card display contract including hardcoded button labels (assembled by callers). */\nexport type HomeCardPresentationResult = HomeCardLLMResult & {\n primaryActionLabel: string;\n secondaryActionLabel: string;\n};\n\nconst homeCardResponseFormat = z.object({\n presentation: HomeCardLLMSchema,\n});\n\n/** Input for a single presenter call (all context pre-assembled). */\nexport interface PresenterInput {\n viewerContext: string;\n otherPartyContext: string;\n matchReasoning: string;\n category: string;\n confidence: number;\n signalsSummary: string;\n indexName: string;\n viewerRole: string;\n opportunityStatus?: string;\n /** True when this opportunity was created via an explicit introduction (not automatic discovery). */\n isIntroduction?: boolean;\n /** Name of the person who made the introduction, if applicable. */\n introducerName?: string;\n}\n\n// ──────────────────────────────────────────────────────────────\n// SYSTEM PROMPT\n// ──────────────────────────────────────────────────────────────\n\nconst systemPrompt = `\nYou are an expert at presenting connection opportunities to users in a way that feels personal and compelling.\n\nYour goal: Given raw context about the viewer (their profile, intents), the other person(s), and why the system matched them, produce a short headline, a personalized summary, and a suggested action.\n\nRules:\n1. Address the VIEWER directly using \"you\" and \"your\". This is for them.\n2. Be concise and compelling — not analytical or third-party. No \"The source user\" or \"The candidate\"; use names or \"they\" where needed.\n3. Do not leak private or confidential details. Use only the context provided.\n4. Vary user-facing nouns naturally. Do not repeatedly use the same label in one response.\n5. If possible, avoid repeating \"opportunity\" in both headline and summary. Prefer alternatives like \"connection\", \"thought partner\", \"mutual fit\", \"valuable conversation\", or \"peer\".\n6. Prefer first names in user-facing copy. Do not repeatedly use full names unless needed to disambiguate.\n\n**Introduction-originated opportunities:**\nWhen INTRODUCTION CONTEXT is provided, this opportunity was explicitly created by an introducer (a real person who saw value in this connection). This is NOT an automatic system discovery — someone made a deliberate judgment.\n- For ALL roles: acknowledge the introducer's role naturally. E.g., \"[Introducer name] thinks you should meet [other person]\" or \"[Introducer name] connected you because...\"\n- The introduction itself is a strong signal — treat it with the weight of a personal recommendation.\n- If the parties' intents don't obviously overlap, that's fine — the introducer saw something worth connecting. Focus on what the introducer likely saw.\n\n**Role-Specific Presentation:**\n\n**If viewer is \"introducer\":**\n- The viewer suggested this connection between two (or more) OTHER people. The opportunity is NOT about the viewer's own needs.\n- Headline: describe the connection between the parties (e.g., \"Connecting a React expert with a startup founder\").\n- Personalized summary: explain why the people YOU are introducing should meet. Reference THEIR profiles and intents, not yours. Frame it as \"you're connecting X and Y because...\" rather than \"this matches your intent\".\n- Suggested action: guide sharing (e.g., \"Share this with [name] to get things started\").\n- CRITICAL: Do NOT reference the introducer's own intents, skills, or needs. The introducer is the matchmaker, not a party.\n\n**If viewer is \"patient\" or \"party\":**\n- Reference their specific intents, skills, or interests that align with this opportunity.\n- If this is an introduction: mention who introduced them and frame it as a personal recommendation.\n- Headline: one short line that hooks (e.g., \"[Name] thinks you should meet [Other]\" or \"A React expert who needs your design skills\").\n- Personalized summary: 2-3 sentences. Why is this opportunity for *them*? If introduced, lead with the introduction.\n- Suggested action: encourage action (\"Send a message to start the conversation\" or \"Share this intro\").\n\n**If viewer is \"agent\":**\n- They are seeing this because someone already reached out.\n- If this is an introduction: mention who made the introduction.\n- Reference their skills/expertise that make them a match.\n- Headline: what the other person needs that they can provide.\n- Personalized summary: 2-3 sentences. Why someone reached out to them.\n- Suggested action: \"Someone is interested in connecting — check their message\" or \"Review and respond\".\n\n**If viewer is \"peer\":**\n- Mutual opportunity. Reference shared or complementary interests.\n- If this is an introduction: mention who connected them.\n- Headline: the mutual connection angle.\n- Personalized summary: 2-3 sentences. Why this is mutually valuable.\n- Suggested action: \"Send an intro to connect\" or \"Start a conversation\".\n`;\n\nconst homeCardSystemPrompt = `\nYou are an expert at presenting connection opportunities for a home feed card.\n\nGiven context about the viewer, the other person, and why they were matched, produce:\n1. headline: one short hook line.\n2. personalizedSummary: 2-3 sentences in \"you\" language (main body text).\n3. digestSummary: one polished morning-brief sentence that can be printed directly after the person's linked name. No markdown, no field labels.\n4. suggestedAction: one brief suggested next step.\n5. narratorRemark: one short sentence for the narrator chip (who is suggesting and why; max ~80 chars).\n6. greeting: a 2-4 sentence first-person message the viewer could send to the counterpart. Plain prose, no greeting prefix, no markdown.\n7. mutualIntentsLabel: short subtitle under the other party's name. Examples: \"3 mutual intents\", \"Shared interests\", \"Aligned goals\" — keep it brief. NEVER output \"0 mutual intents\" or any zero-count label; use a qualitative phrase instead.\n\nRules:\n- Address the viewer with \"you\"/\"your\". Be concise and compelling.\n- narratorRemark should feel like a single sentence from the narrator (Index or a person), not meta-commentary.\n- narratorRemark is displayed with the narrator name prepended (e.g. \"Index: …\" or \"Alice: …\"). Do NOT start narratorRemark with the narrator's name or repeat it; write only the remark (e.g. \"Based on your overlapping intents\" or \"introduced you two, sensing a valuable connection\").\n- Vary wording for the match itself. Do not repeat \"opportunity\" across headline, summary, and narratorRemark when alternatives fit.\n- Prefer first names in user-facing copy. Avoid repeated full names unless disambiguation is necessary.\n- digestSummary must be grammatically complete as a standalone sentence. It should usually start with \"You might like meeting {Name} because ...\" for direct connections, or \"You may be able to help {Name} because ...\" for connector/introducer cards.\n- digestSummary must NOT use awkward third-person fragments like \"Name is...\", \"they're ..., and is...\", \"you is...\", or \"the discoverer's query\".\n- digestSummary must be one sentence, MUST fit within 180 characters when possible, and MUST contain no markdown links; the caller will attach links.\n- If you cannot fit every detail, choose one clear reason and stop. Do not rely on downstream truncation.\n\n**Introduction-originated opportunities (ONLY when INTRODUCTION CONTEXT is provided):**\nWhen INTRODUCTION CONTEXT is provided, this opportunity was explicitly created by an introducer. It was NOT automatically discovered.\n- For parties/patients/agents/peers viewing an introduction: keep the introducer signal in narratorRemark (and narrator chip), not in personalizedSummary.\n- For these introduced parties, personalizedSummary must focus ONLY on fit/value between viewer and counterpart. Do NOT mention the introducer there.\n- narratorRemark should carry the introduction signal (e.g., \"saw strong alignment between you two\" or \"thought this connection could be valuable\"), without repeating the narrator name at the start.\n- This is a personal recommendation, not an algorithm match. Frame it accordingly.\n\n**CRITICAL: NEVER include introducer names in personalizedSummary. Examples:**\n❌ WRONG: \"Seref introduced you to Lucy, who is actively seeking a product co-founder...\"\n✅ CORRECT: \"Lucy is actively seeking a product co-founder for a niche APAC marketplace. With your expertise in UX and AI, this could be an ideal collaboration.\"\n\n❌ WRONG: \"Bob thinks you should meet Alice because your React skills align with her needs.\"\n✅ CORRECT: \"Alice is building a React-based platform and needs frontend expertise. Your experience with component architecture makes you a strong fit.\"\n\n❌ WRONG: \"Jane connected you to Mark, who is looking for a designer.\"\n✅ CORRECT: \"Mark is building a consumer app and needs design expertise. Your background in user-centered design aligns well with what he's building.\"\n\nRemember: The introducer's name goes ONLY in narratorRemark, NEVER in personalizedSummary.\n\n**When INTRODUCTION CONTEXT is NOT provided (system-discovered match):**\n- Do NOT use introducer-style wording. Do NOT say \"you suggested\", \"this is an introduction you suggested\", or \"you suggested this connection\". The system found this match; no human introducer was involved.\n- Instead, narratorRemark should describe why the match is relevant (e.g. \"Based on your overlapping intents\", \"Your skills align with what they need\").\n\n**Negotiation-grounded explanations (ONLY when NEGOTIATION CONTEXT is provided):**\nWhen NEGOTIATION CONTEXT is provided, this opportunity passed through an agent-to-agent negotiation. Use the transcript to ground your explanation in the concrete reasoning the agents exchanged.\n- Personalize the summary with *why* the negotiation produced this match — reference the roles the agents agreed on, the specific concerns raised, and how they were resolved.\n- For status \"stalled\" with reason \"turn_cap\": the agents hit the turn limit without reaching agreement. Frame the card as a hedged possibility rather than a confident match; narratorRemark should signal \"agents couldn't fully converge\" without sounding negative.\n- For status \"stalled\" with reason \"timeout\": one side went silent. Suggest the user re-engage if interested.\n- For status \"accepted\": the agents agreed; the card should confidently explain *why* they agreed.\n- For status \"rejected\": the agents declined. The card should explain the reason briefly so the user understands — not dwell on it.\n- Do NOT invent turn content. Only reference what is in the NEGOTIATION CONTEXT block.\n\n- Exception for connector/introducer: if viewer role is \"introducer\" (any status), this is a curation/connector card. Use:\n - suggestedAction: one short line about sharing the intro or confirming the match.\n - mutualIntentsLabel: a short connector label (e.g. \"Connector match\", \"You can bridge this\").\n - headline: describe the connection between the parties (e.g., \"Connecting a PhD researcher with a translator\"). Do NOT reference the introducer's own needs.\n - personalizedSummary: explain why the parties you're introducing should meet, referencing THEIR profiles and intents, not yours.\n\n**CRITICAL for latent introducer cards (opportunity status is \"latent\"):**\nWhen the viewer is the introducer and the opportunity status is \"latent\", the introducer has NOT yet approved this match. They are evaluating whether to make the introduction.\n- narratorRemark MUST use evaluation/curation language (e.g. \"Could be a strong match\", \"Worth introducing?\", \"Interesting overlap here\").\n- Do NOT say \"you suggested\", \"you introduced\", \"you connected\", or any past-tense language implying the introduction was already made.\n- suggestedAction should encourage evaluation (e.g. \"Approve if you see the fit\").\n- Exception for new-connection reveal: if viewer role is \"agent\", status is \"accepted\", and there is an introducer, this is the agent's first time seeing this opportunity. Use:\n - suggestedAction: a short line about joining the conversation.\n`;\n\n// ──────────────────────────────────────────────────────────────\n// CLASS\n// ──────────────────────────────────────────────────────────────\n\nexport class OpportunityPresenter {\n private model: Runnable;\n private homeCardModel: Runnable;\n\n constructor() {\n this.model = model.withStructuredOutput(responseFormat, {\n name: \"opportunity_presenter\",\n });\n this.homeCardModel = model.withStructuredOutput(homeCardResponseFormat, {\n name: \"opportunity_presenter_home_card\",\n });\n }\n\n private async invokeWithTimeout(\n targetModel: Runnable,\n messages: (SystemMessage | HumanMessage)[],\n ): Promise<unknown> {\n const timeoutReason = `LLM invoke timed out after ${LLM_TIMEOUT_MS}ms`;\n const controller = new AbortController();\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n\n const invokePromise = targetModel.invoke(messages, {\n signal: controller.signal,\n });\n\n const timeoutPromise = new Promise<never>((_, reject) => {\n timeoutId = setTimeout(() => {\n controller.abort(timeoutReason);\n reject(new Error(timeoutReason));\n }, LLM_TIMEOUT_MS);\n });\n\n try {\n return await Promise.race([invokePromise, timeoutPromise]);\n } finally {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n }\n }\n\n /**\n * Generate personalized presentation for a single opportunity.\n */\n @Timed()\n public async present(\n input: PresenterInput,\n ): Promise<OpportunityPresentationResult> {\n const introContext = input.isIntroduction\n ? `\\nINTRODUCTION CONTEXT: This opportunity was created by an explicit introduction from ${input.introducerName ?? \"someone in the community\"}. It was NOT discovered automatically — a real person made this connection.\\n`\n : \"\";\n const humanContent = `\nVIEWER (the person seeing this opportunity):\n${input.viewerContext}\n\nOTHER PARTY:\n${input.otherPartyContext}\n\nMATCH CONTEXT:\n- Category: ${input.category}\n- Confidence: ${input.confidence}\n- Why we matched: ${input.matchReasoning}\n- Signals: ${input.signalsSummary}\n${introContext}\nCOMMUNITY: ${input.indexName}\nViewer's role in this opportunity: ${input.viewerRole}\n\nProduce headline, personalizedSummary (2-3 sentences in \"you\" language), suggestedAction, and greeting.\n`;\n\n try {\n const messages = [\n new SystemMessage(systemPrompt),\n new HumanMessage(humanContent),\n ];\n const result = await this.invokeWithTimeout(this.model, messages);\n const parsed = responseFormat.parse(result);\n parsed.presentation.personalizedSummary = stripUuids(parsed.presentation.personalizedSummary);\n return parsed.presentation;\n } catch (e) {\n const message = e instanceof Error ? e.message : String(e);\n const timeoutReason = message.includes(\"timed out\") ? message : undefined;\n logger.warn(\n \"[OpportunityPresenter.present] LLM failed, returning fallback\",\n {\n event: \"presenter_fallback\",\n presenter: \"opportunity\",\n reason: timeoutReason ? \"timeout\" : \"parse_error\",\n message,\n timeoutReason,\n },\n );\n return {\n headline: \"A promising connection\",\n personalizedSummary: truncateAtBoundary(stripUuids(input.matchReasoning), 300),\n suggestedAction: \"Take a look and decide whether to reach out.\",\n greeting: \"\",\n };\n }\n }\n\n /**\n * Generate LLM-powered home-card content (headline, body, narrator remark, mutual-intent label).\n * Callers append button labels from opportunity.constants.\n *\n * When `negotiationContext.status === 'negotiating'`, returns a templated\n * chip synchronously without invoking the LLM — the card just reflects\n * \"negotiation in progress\" at that point.\n */\n @Timed()\n public async presentHomeCard(\n input: HomeCardPresenterInput,\n ): Promise<HomeCardLLMResult> {\n if (input.negotiationContext?.status === 'negotiating') {\n return buildNegotiatingChip(input);\n }\n\n const mutualHint =\n input.mutualIntentCount != null && input.mutualIntentCount > 0\n ? `There are ${input.mutualIntentCount} overlapping intent(s) between viewer and other party.`\n : \"Match is based on profile and intent alignment. Do not cite a numeric intent count.\";\n const introContext = input.isIntroduction\n ? `\\nINTRODUCTION CONTEXT: This opportunity was created by an explicit introduction from ${input.introducerName ?? \"someone in the community\"}. It was NOT discovered automatically — a real person made this connection.\\n`\n : \"\";\n const negotiationBlock = buildNegotiationPromptBlock(input.negotiationContext);\n // When negotiation context exists, lead with it — these cards exist\n // *because* the negotiation happened. Trailing the block lets weaker\n // models lean on surface signals and ignore the transcript entirely.\n const negotiationDirective = negotiationBlock\n ? `\\nIMPORTANT: This opportunity surfaced because the agents negotiated and converged. Your personalizedSummary MUST reference at least one specific signal from the NEGOTIATION CONTEXT block below — what concern was raised, what was confirmed, what the agents agreed on. Do not produce a generic skill-complementarity summary; that's what every card looked like before this negotiation happened. Use the transcript to explain *why this specific match* surfaced now.\\n`\n : \"\";\n const humanContent = `\n${negotiationBlock}${negotiationDirective}\nVIEWER (the person seeing this opportunity):\n${input.viewerContext}\n\nOTHER PARTY:\n${input.otherPartyContext}\n\nMATCH CONTEXT:\n- Category: ${input.category}\n- Confidence: ${input.confidence}\n- Why we matched: ${input.matchReasoning}\n- Signals: ${input.signalsSummary}\n- ${mutualHint}\n${introContext}\nCOMMUNITY: ${input.indexName}\nViewer's role in this opportunity: ${input.viewerRole}\nOpportunity status: ${input.opportunityStatus ?? \"pending\"}\n\nProduce headline, personalizedSummary, digestSummary, suggestedAction, narratorRemark, greeting, and mutualIntentsLabel.\n`;\n\n const isIntroducer = input.viewerRole === \"introducer\";\n\n try {\n const messages = [\n new SystemMessage(homeCardSystemPrompt),\n new HumanMessage(humanContent),\n ];\n const result = await this.invokeWithTimeout(this.homeCardModel, messages);\n const parsed = homeCardResponseFormat.parse(result);\n parsed.presentation.personalizedSummary = stripUuids(parsed.presentation.personalizedSummary);\n parsed.presentation.digestSummary = stripUuids(parsed.presentation.digestSummary);\n parsed.presentation.narratorRemark = stripUuids(parsed.presentation.narratorRemark);\n if (/^0\\s+(mutual|overlapping)\\s+intent/i.test(parsed.presentation.mutualIntentsLabel)) {\n parsed.presentation.mutualIntentsLabel = \"Shared interests\";\n }\n if (input.isIntroduction && input.introducerName) {\n parsed.presentation.personalizedSummary = stripIntroducerMentions(\n parsed.presentation.personalizedSummary,\n input.introducerName,\n );\n parsed.presentation.digestSummary = stripIntroducerMentions(\n parsed.presentation.digestSummary,\n input.introducerName,\n );\n }\n return parsed.presentation;\n } catch (e) {\n const message = e instanceof Error ? e.message : String(e);\n const timeoutReason = message.includes(\"timed out\") ? message : undefined;\n logger.warn(\n \"[OpportunityPresenter.presentHomeCard] LLM failed, returning fallback\",\n {\n event: \"presenter_fallback\",\n presenter: \"home_card\",\n reason: timeoutReason ? \"timeout\" : \"parse_error\",\n message,\n timeoutReason,\n },\n );\n let fallbackSummary = truncateAtBoundary(stripUuids(input.matchReasoning), 300);\n if (input.isIntroduction && input.introducerName) {\n fallbackSummary = stripIntroducerMentions(fallbackSummary, input.introducerName);\n }\n return {\n headline: \"A promising connection\",\n personalizedSummary: fallbackSummary,\n digestSummary: isIntroducer\n ? \"You may be able to help make a useful introduction here.\"\n : \"You might like meeting them based on your current interests.\",\n suggestedAction: isIntroducer\n ? \"Share this introduction to get things started.\"\n : \"Take a look and decide whether to reach out.\",\n narratorRemark: \"Worth a look.\",\n mutualIntentsLabel: isIntroducer\n ? \"Connector match\"\n : input.mutualIntentCount != null && input.mutualIntentCount > 0\n ? `${input.mutualIntentCount} mutual intent${input.mutualIntentCount !== 1 ? \"s\" : \"\"}`\n : \"Shared interests\",\n greeting: \"\",\n };\n }\n }\n\n /**\n * Process multiple opportunities in parallel with bounded concurrency.\n */\n @Timed()\n public async presentBatch(\n inputs: PresenterInput[],\n options?: { concurrency?: number },\n ): Promise<OpportunityPresentationResult[]> {\n const concurrency = options?.concurrency ?? 5;\n const results: OpportunityPresentationResult[] = [];\n for (let i = 0; i < inputs.length; i += concurrency) {\n const chunk = inputs.slice(i, i + concurrency);\n const chunkResults = await Promise.all(\n chunk.map((inp) => this.present(inp)),\n );\n results.push(...chunkResults);\n }\n return results;\n }\n\n /**\n * Process multiple opportunities as home cards in parallel with bounded concurrency.\n * Returns full home-card display contracts (headline, body, narrator remark, action labels, mutual-intent label).\n */\n @Timed()\n public async presentHomeCardBatch(\n inputs: HomeCardPresenterInput[],\n options?: { concurrency?: number },\n ): Promise<HomeCardLLMResult[]> {\n const concurrency = options?.concurrency ?? 5;\n const results: HomeCardLLMResult[] = [];\n for (let i = 0; i < inputs.length; i += concurrency) {\n const chunk = inputs.slice(i, i + concurrency);\n const chunkResults = await Promise.all(\n chunk.map((inp) => this.presentHomeCard(inp)),\n );\n results.push(...chunkResults);\n }\n return results;\n }\n}\n\n// ──────────────────────────────────────────────────────────────\n// NEGOTIATION CONTEXT HELPERS\n// ──────────────────────────────────────────────────────────────\n\n/**\n * Builds a \"NEGOTIATION CONTEXT:\" block for the home-card prompt. Returns an\n * empty string when the opportunity has no meaningful negotiation context\n * (draft/latent) or when the opportunity is still negotiating (handled via\n * the templated chip, not the LLM).\n */\nfunction buildNegotiationPromptBlock(context: NegotiationContext | undefined): string {\n if (!context || context.status === 'negotiating') return \"\";\n\n const turnCapLabel = context.turnCap > 0 ? `${context.turnCap}` : \"unlimited\";\n const reason = context.outcome?.reason;\n const reasonLabel = reason === 'turn_cap'\n ? \"agents hit the turn cap without converging\"\n : reason === 'timeout'\n ? \"counterpart went silent before responding\"\n : undefined;\n\n const turnLines = (context.turns ?? []).map((turn, index) => {\n const action = turn.action;\n const reasoning = turn.assessment?.reasoning ?? \"(no reasoning)\";\n const message = turn.message ? ` — said: \"${turn.message}\"` : \"\";\n return `Turn ${index + 1} (${action}): ${reasoning}${message}`;\n });\n\n const outcomeSummary = context.outcome\n ? `Final outcome: ${context.outcome.hasOpportunity ? \"agreed\" : \"declined\"} — ${context.outcome.reasoning}`\n : \"Final outcome: not recorded.\";\n\n return `\nNEGOTIATION CONTEXT:\n- Negotiation status: ${context.status}${reasonLabel ? ` (${reasonLabel})` : \"\"}\n- Turns exchanged: ${context.turnCount} of ${turnCapLabel}\n- Transcript:\n${turnLines.length > 0 ? turnLines.map((l) => ` ${l}`).join(\"\\n\") : \" (no turns recorded)\"}\n- ${outcomeSummary}\n`;\n}\n\n/**\n * Builds a templated home-card result for an opportunity whose negotiation\n * is still in progress. Bypasses the LLM so users see a stable \"currently\n * negotiating\" chip while turns are still being exchanged.\n */\nfunction buildNegotiatingChip(input: HomeCardPresenterInput): HomeCardLLMResult {\n const ctx = input.negotiationContext;\n const turnCount = ctx?.turnCount ?? 0;\n const turnCap = ctx?.turnCap && ctx.turnCap > 0 ? ctx.turnCap : undefined;\n const narratorRemark = turnCap\n ? `Currently negotiating · turn ${turnCount} of ${turnCap}`\n : `Currently negotiating · turn ${turnCount}`;\n\n return {\n headline: \"Negotiation in progress\",\n personalizedSummary: \"Your agent is still talking with theirs to see if this connection makes sense. We'll surface the full match as soon as they converge.\",\n digestSummary: \"Your agent is still checking whether this connection makes sense.\",\n suggestedAction: \"Check back shortly — no action needed yet.\",\n narratorRemark,\n mutualIntentsLabel: input.mutualIntentCount && input.mutualIntentCount > 0\n ? `${input.mutualIntentCount} mutual intent${input.mutualIntentCount !== 1 ? \"s\" : \"\"}`\n : \"Shared interests\",\n greeting: \"\",\n };\n}\n\n// ──────────────────────────────────────────────────────────────\n// CONTEXT GATHERER (used by tools)\n// ──────────────────────────────────────────────────────────────\n\n/**\n * Gather all context needed for the presenter from the database.\n * Fetches viewer profile, viewer intents, other party profile(s), and index in parallel.\n *\n * @param displayCounterpartUserId - When set (e.g. for home card), only this counterpart is included in otherPartyContext so the presenter writes about the person on the card. Omitted for introducer view (card shows both parties).\n */\nexport async function gatherPresenterContext(\n database: PresenterDatabase,\n opportunity: Opportunity,\n viewerId: string,\n displayCounterpartUserId?: string,\n): Promise<PresenterInput> {\n const myActor = opportunity.actors.find((a) => a.userId === viewerId);\n if (!myActor) {\n throw new Error(\"Viewer is not an actor in this opportunity\");\n }\n\n const isIntroducer = myActor.role === \"introducer\";\n const otherActors = opportunity.actors.filter((a) => a.userId !== viewerId);\n let otherPartyIds = [...new Set(otherActors.map((a) => a.userId))];\n if (\n displayCounterpartUserId &&\n !isIntroducer &&\n otherPartyIds.includes(displayCounterpartUserId)\n ) {\n otherPartyIds = [displayCounterpartUserId];\n }\n\n const contextIndexId = opportunity.context?.networkId;\n\n // For introducers: fetch profiles + intents for both parties; skip introducer's own intents.\n // For other roles: fetch viewer's profile + intents and other party profiles.\n const [viewerProfile, indexRecord, ...otherProfiles] = await Promise.all([\n database.getProfile(viewerId),\n contextIndexId ? database.getNetwork(contextIndexId) : Promise.resolve(null),\n ...otherPartyIds.map((uid) => database.getProfile(uid)),\n ]);\n\n // Fetch intents: for introducer, fetch each party's intents; otherwise fetch viewer's intents.\n let viewerIntents:\n | Awaited<ReturnType<typeof database.getActiveIntents>>\n | undefined;\n let partyIntentsMap:\n | Map<string, Awaited<ReturnType<typeof database.getActiveIntents>>>\n | undefined;\n\n if (isIntroducer) {\n const partyIntentResults = await Promise.all(\n otherPartyIds.map(async (uid) => ({\n uid,\n intents: await database.getActiveIntents(uid),\n })),\n );\n partyIntentsMap = new Map(\n partyIntentResults.map((r) => [r.uid, r.intents]),\n );\n } else {\n viewerIntents = await database.getActiveIntents(viewerId);\n }\n\n // Fetch premises when any actor is premise-grounded\n const premiseGroundedActors = opportunity.actors.filter((a) => a.premise);\n let viewerPremiseContext = '';\n let otherPremiseContext = '';\n\n if (premiseGroundedActors.length > 0) {\n // Only fetch premises for actors that are actually premise-grounded, not all parties\n const groundedOtherIds = premiseGroundedActors\n .filter((a) => a.userId !== viewerId)\n .map((a) => a.userId);\n const viewerIsGrounded = premiseGroundedActors.some((a) => a.userId === viewerId);\n\n const results = await Promise.all([\n ...(viewerIsGrounded ? [database.getPremisesForUser(viewerId, 'ACTIVE')] : []),\n ...groundedOtherIds.map((uid) => database.getPremisesForUser(uid, 'ACTIVE')),\n ]);\n\n let idx = 0;\n if (viewerIsGrounded) {\n const viewerPremises = results[idx++];\n if (viewerPremises?.length) {\n viewerPremiseContext =\n '\\nPremises (self-descriptions):\\n' +\n viewerPremises\n .slice(0, 5)\n .map((p) => `- ${p.assertion.text}`)\n .join('\\n');\n }\n }\n\n const otherPremiseLines: string[] = [];\n for (let i = 0; i < groundedOtherIds.length; i++) {\n const premises = results[idx++];\n if (premises?.length) {\n for (const p of premises.slice(0, 3)) {\n otherPremiseLines.push(`- ${p.assertion.text}`);\n }\n }\n }\n if (otherPremiseLines.length > 0) {\n otherPremiseContext = '\\nPremises (self-descriptions):\\n' + otherPremiseLines.join('\\n');\n }\n }\n\n let viewerContext: string;\n let otherPartyContext: string;\n\n if (isIntroducer) {\n // Introducer view: minimal viewer context (just name + role), rich other-party context with intents\n const introducerApproved = opportunity.actors.find(a => a.role === 'introducer')?.approved === true;\n const hasApproved = introducerApproved || opportunity.status !== 'latent';\n viewerContext = [\n \"Profile:\",\n `Name: ${viewerProfile?.identity?.name ?? \"Unknown\"}`,\n hasApproved\n ? \"Role: You are the introducer who suggested this connection.\"\n : \"Role: You are being asked whether these two people would benefit from meeting. You have NOT yet approved this introduction.\",\n ].join(\"\\n\");\n\n const otherParts = otherPartyIds.map((uid, idx) => {\n const profile = otherProfiles[idx] as Awaited<\n ReturnType<typeof database.getProfile>\n >;\n const name = profile?.identity?.name ?? \"Unknown\";\n const bio = profile?.identity?.bio ?? \"\";\n const location = profile?.identity?.location ?? \"\";\n const context = profile?.context ?? \"\";\n const intents = partyIntentsMap?.get(uid);\n const intentLines = intents?.length\n ? intents\n .slice(0, 5)\n .map((i) => ` - ${i.payload}${i.summary ? ` (${i.summary})` : \"\"}`)\n : [\" (no active intents)\"];\n return [\n `${name}:`,\n ` Bio: ${bio}`,\n location ? ` Location: ${location}` : null,\n context ? ` Context: ${context}` : null,\n ` Active intents:`,\n ...intentLines,\n ]\n .filter(Boolean)\n .join(\"\\n\");\n });\n otherPartyContext =\n otherParts.join(\"\\n\\n\") || \"Parties (details not available).\";\n } else {\n // Non-introducer view: full viewer profile + intents, other party profiles\n const viewerContextLines = [\n \"Profile:\",\n `Name: ${viewerProfile?.identity?.name ?? \"Unknown\"}`,\n `Bio: ${viewerProfile?.identity?.bio ?? \"\"}`,\n `Location: ${viewerProfile?.identity?.location ?? \"\"}`,\n `Context: ${viewerProfile?.context ?? \"\"}`,\n \"Active intents:\",\n ...(viewerIntents?.length\n ? viewerIntents.map(\n (i) => `- ${i.payload}${i.summary ? ` (${i.summary})` : \"\"}`,\n )\n : [\"(none listed)\"]),\n ];\n viewerContext = viewerContextLines.join(\"\\n\");\n\n const otherParts = otherPartyIds.map((uid, idx) => {\n const profile = otherProfiles[idx] as Awaited<\n ReturnType<typeof database.getProfile>\n >;\n const name = profile?.identity?.name ?? \"Unknown\";\n const bio = profile?.identity?.bio ?? \"\";\n return `${name}: ${bio}`;\n });\n otherPartyContext =\n otherParts.join(\"\\n\\n\") || \"Other party (details not available).\";\n }\n\n const interp = opportunity.interpretation;\n const signalsSummary =\n interp.signals?.map((s) => `${s.type}: ${s.detail ?? s.type}`).join(\"; \") ??\n \"Match based on profile and intent alignment.\";\n\n // Detect introduction-originated opportunities: only when there is an explicit introducer actor.\n // Do NOT use detection.source === \"manual\" alone — system-discovered opportunities can have manual source without an introducer.\n const introducerActor = opportunity.actors.find(\n (a) => a.role === \"introducer\",\n );\n const isIntroduction = !!introducerActor;\n let introducerName: string | undefined;\n if (introducerActor) {\n introducerName = opportunity.detection.createdByName;\n if (!introducerName) {\n const introducerProfile = await database.getProfile(\n introducerActor.userId,\n );\n introducerName = introducerProfile?.identity?.name ?? undefined;\n }\n }\n\n const counterpartName =\n otherPartyIds.length === 1 && otherProfiles[0]\n ? (otherProfiles[0] as { identity?: { name?: string } })?.identity?.name?.trim()\n : undefined;\n const viewerNameForFilter = viewerProfile?.identity?.name?.trim();\n const matchReasoning =\n counterpartName && interp.reasoning\n ? viewerCentricCardSummary(\n interp.reasoning,\n counterpartName,\n 400,\n viewerNameForFilter,\n introducerName,\n )\n : stripUuids(interp.reasoning);\n\n if (viewerPremiseContext) {\n viewerContext += viewerPremiseContext;\n }\n if (otherPremiseContext) {\n otherPartyContext += otherPremiseContext;\n }\n\n const result: PresenterInput = {\n viewerContext,\n otherPartyContext,\n matchReasoning,\n category: interp.category ?? \"connection\",\n confidence:\n typeof interp.confidence === \"number\"\n ? interp.confidence\n : parseFloat(String(interp.confidence ?? 0)) || 0,\n signalsSummary,\n indexName: indexRecord?.title ?? contextIndexId ?? \"\",\n viewerRole: myActor.role ?? \"party\",\n isIntroduction,\n introducerName,\n };\n\n return result;\n}\n"]}
1
+ {"version":3,"file":"opportunity.presenter.js","sourceRoot":"/","sources":["opportunity/opportunity.presenter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;;;;;;;;;;AAGH,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,KAAK,EAAE,MAAM,wCAAwC,CAAC;AAE/D,OAAO,EAAE,cAAc,EAAE,MAAM,4CAA4C,CAAC;AAC5E,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AA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nC,cAAc;QACd,cAAc;KACf,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["/**\n * Opportunity Presenter Agent\n *\n * Generates personalized, second-person explanations of why an opportunity\n * matters to the viewing user. Uses full opportunity data (interpretation,\n * actors, profiles, intents, index) to produce headline, personalizedSummary,\n * and suggestedAction for chat tools and user-facing surfaces.\n */\n\nimport type { Runnable } from \"@langchain/core/runnables\";\nimport { HumanMessage, SystemMessage } from \"@langchain/core/messages\";\nimport { z } from \"zod\";\n\nimport { Timed } from \"../shared/observability/performance.js\";\n\nimport { protocolLogger } from \"../shared/observability/protocol.logger.js\";\nimport { createModel } from \"../shared/agent/model.config.js\";\nimport { viewerCentricCardSummary } from \"./opportunity.presentation.js\";\nimport type { Opportunity } from \"../shared/interfaces/database.interface.js\";\nimport type { ChatGraphCompositeDatabase } from \"../shared/interfaces/database.interface.js\";\nimport type { NegotiationContext } from \"./negotiation-context.loader.js\";\nimport { stripUuids, stripIntroducerMentions, truncateAtBoundary } from \"./opportunity.presentation.js\";\n\n/**\n * Minimal database interface required by gatherPresenterContext.\n * Any database adapter that implements these three methods can be passed.\n */\nexport type PresenterDatabase = Pick<\n ChatGraphCompositeDatabase,\n \"getProfile\" | \"getActiveIntents\" | \"getNetwork\" | \"getPremisesForUser\"\n>;\n\nconst logger = protocolLogger(\"OpportunityPresenter\");\nconst LLM_TIMEOUT_MS = 20_000;\n\nconst model = createModel(\"opportunityPresenter\");\n\nconst GREETING_DESCRIPTION =\n \"A 2-4 sentence first-person message the viewer could send to the counterpart, in the viewer's voice, referencing what they have in common. Plain prose only — no markdown, no greeting prefix like 'Hey {Name},'. Example body: 'Saw we're both working on regenerative coordination tooling — your post on consent flows resonated. Would love to compare notes if you have time this week.'\";\n\n// ──────────────────────────────────────────────────────────────\n// SCHEMA & TYPES\n// ──────────────────────────────────────────────────────────────\n\nconst PresentationSchema = z.object({\n headline: z\n .string()\n .describe(\n \"Short, compelling headline for this opportunity (e.g., 'A React expert who needs your design skills')\",\n ),\n personalizedSummary: z\n .string()\n .describe(\n \"2-3 sentence explanation using 'you' language, explaining why this opportunity is specifically valuable for the viewer based on their intents and profile\",\n ),\n suggestedAction: z.string().describe(\"Brief suggested next step\"),\n greeting: z.string().max(500).describe(GREETING_DESCRIPTION),\n});\n\nconst responseFormat = z.object({\n presentation: PresentationSchema,\n});\n\nexport type OpportunityPresentationResult = z.infer<typeof PresentationSchema>;\n\n/** Input for home-card presenter call; extends PresenterInput with optional mutual intent count. */\nexport interface HomeCardPresenterInput extends PresenterInput {\n /** Number of overlapping intents (for generating mutualIntentsLabel). */\n mutualIntentCount?: number;\n /**\n * Snapshot of the opportunity's negotiation, if one exists. When status is\n * `negotiating`, the presenter returns a templated chip without invoking\n * the LLM. For `pending`/`stalled`/`accepted`/`rejected`, the full\n * transcript and outcome ground the LLM's explanation.\n */\n negotiationContext?: NegotiationContext;\n}\n\n/** LLM-generated fields for home-card presentation (buttons are hardcoded by callers, not LLM-generated). */\nexport const HomeCardLLMSchema = z.object({\n headline: z\n .string()\n .describe(\"Short, compelling headline for this opportunity\"),\n personalizedSummary: z\n .string()\n .describe(\n \"2-3 sentence explanation in 'you' language for the main card body\",\n ),\n digestSummary: z\n .string()\n .max(220)\n .describe(\n \"One concise digest-ready sentence for a morning brief. It must be addressed to the viewer and mention the counterpart by name, e.g. 'You might like meeting Paul because ...'. No markdown.\",\n ),\n suggestedAction: z\n .string()\n .describe(\"Brief suggested next step (e.g. CTA line)\"),\n narratorRemark: z\n .string()\n .max(80)\n .describe(\n \"One short sentence for the narrator chip, max ~80 chars (e.g. who is suggesting and why)\",\n ),\n mutualIntentsLabel: z\n .string()\n .max(48)\n .describe(\n \"Short line for the subtitle under the other party name (e.g. '3 mutual intents', 'Shared interests', 'Aligned goals'). NEVER output '0 mutual intents' — use a qualitative phrase like 'Shared interests' when no numeric count is available.\",\n ),\n greeting: z.string().max(500).describe(GREETING_DESCRIPTION),\n});\n\n/** LLM-generated result from presentHomeCard (callers append button labels from opportunity.constants). */\nexport type HomeCardLLMResult = z.infer<typeof HomeCardLLMSchema>;\n\n/** Full home-card display contract including hardcoded button labels (assembled by callers). */\nexport type HomeCardPresentationResult = HomeCardLLMResult & {\n primaryActionLabel: string;\n secondaryActionLabel: string;\n};\n\nconst homeCardResponseFormat = z.object({\n presentation: HomeCardLLMSchema,\n});\n\n/** Input for a single presenter call (all context pre-assembled). */\nexport interface PresenterInput {\n viewerContext: string;\n otherPartyContext: string;\n matchReasoning: string;\n category: string;\n confidence: number;\n signalsSummary: string;\n indexName: string;\n viewerRole: string;\n opportunityStatus?: string;\n /** True when this opportunity was created via an explicit introduction (not automatic discovery). */\n isIntroduction?: boolean;\n /** Name of the person who made the introduction, if applicable. */\n introducerName?: string;\n}\n\n// ──────────────────────────────────────────────────────────────\n// SYSTEM PROMPT\n// ──────────────────────────────────────────────────────────────\n\nconst systemPrompt = `\nYou are an expert at presenting connection opportunities to users in a way that feels personal and compelling.\n\nYour goal: Given raw context about the viewer (their profile, intents), the other person(s), and why the system matched them, produce a short headline, a personalized summary, and a suggested action.\n\nRules:\n1. Address the VIEWER directly using \"you\" and \"your\". This is for them.\n2. Be concise and compelling — not analytical or third-party. No \"The source user\" or \"The candidate\"; use names or \"they\" where needed.\n3. Do not leak private or confidential details. Use only the context provided.\n4. Vary user-facing nouns naturally. Do not repeatedly use the same label in one response.\n5. If possible, avoid repeating \"opportunity\" in both headline and summary. Prefer alternatives like \"connection\", \"thought partner\", \"mutual fit\", \"valuable conversation\", or \"peer\".\n6. Prefer first names in user-facing copy. Do not repeatedly use full names unless needed to disambiguate.\n\n**Introduction-originated opportunities:**\nWhen INTRODUCTION CONTEXT is provided, this opportunity was explicitly created by an introducer (a real person who saw value in this connection). This is NOT an automatic system discovery — someone made a deliberate judgment.\n- For ALL roles: acknowledge the introducer's role naturally. E.g., \"[Introducer name] thinks you should meet [other person]\" or \"[Introducer name] connected you because...\"\n- The introduction itself is a strong signal — treat it with the weight of a personal recommendation.\n- If the parties' intents don't obviously overlap, that's fine — the introducer saw something worth connecting. Focus on what the introducer likely saw.\n\n**Role-Specific Presentation:**\n\n**If viewer is \"introducer\":**\n- The viewer suggested this connection between two (or more) OTHER people. The opportunity is NOT about the viewer's own needs.\n- Headline: describe the connection between the parties (e.g., \"Connecting a React expert with a startup founder\").\n- Personalized summary: explain why the people YOU are introducing should meet. Reference THEIR profiles and intents, not yours. Frame it as \"you're connecting X and Y because...\" rather than \"this matches your intent\".\n- Suggested action: guide sharing (e.g., \"Share this with [name] to get things started\").\n- CRITICAL: Do NOT reference the introducer's own intents, skills, or needs. The introducer is the matchmaker, not a party.\n\n**If viewer is \"patient\" or \"party\":**\n- Reference their specific intents, skills, or interests that align with this opportunity.\n- If this is an introduction: mention who introduced them and frame it as a personal recommendation.\n- Headline: one short line that hooks (e.g., \"[Name] thinks you should meet [Other]\" or \"A React expert who needs your design skills\").\n- Personalized summary: 2-3 sentences. Why is this opportunity for *them*? If introduced, lead with the introduction.\n- Suggested action: encourage action (\"Send a message to start the conversation\" or \"Share this intro\").\n\n**If viewer is \"agent\":**\n- They are seeing this because someone already reached out.\n- If this is an introduction: mention who made the introduction.\n- Reference their skills/expertise that make them a match.\n- Headline: what the other person needs that they can provide.\n- Personalized summary: 2-3 sentences. Why someone reached out to them.\n- Suggested action: \"Someone is interested in connecting — check their message\" or \"Review and respond\".\n\n**If viewer is \"peer\":**\n- Mutual opportunity. Reference shared or complementary interests.\n- If this is an introduction: mention who connected them.\n- Headline: the mutual connection angle.\n- Personalized summary: 2-3 sentences. Why this is mutually valuable.\n- Suggested action: \"Send an intro to connect\" or \"Start a conversation\".\n`;\n\nconst homeCardSystemPrompt = `\nYou are an expert at presenting connection opportunities for a home feed card.\n\nGiven context about the viewer, the other person, and why they were matched, produce:\n1. headline: one short hook line.\n2. personalizedSummary: 2-3 sentences in \"you\" language (main body text).\n3. digestSummary: one polished morning-brief sentence that can be printed directly after the person's linked name. No markdown, no field labels.\n4. suggestedAction: one brief suggested next step.\n5. narratorRemark: one short sentence for the narrator chip (who is suggesting and why; max ~80 chars).\n6. greeting: a 2-4 sentence first-person message the viewer could send to the counterpart. Plain prose, no greeting prefix, no markdown.\n7. mutualIntentsLabel: short subtitle under the other party's name. Examples: \"3 mutual intents\", \"Shared interests\", \"Aligned goals\" — keep it brief. NEVER output \"0 mutual intents\" or any zero-count label; use a qualitative phrase instead.\n\nRules:\n- Address the viewer with \"you\"/\"your\". Be concise and compelling.\n- narratorRemark should feel like a single sentence from the narrator (Index or a person), not meta-commentary.\n- narratorRemark is displayed with the narrator name prepended (e.g. \"Index: …\" or \"Alice: …\"). Do NOT start narratorRemark with the narrator's name or repeat it; write only the remark (e.g. \"Based on your overlapping intents\" or \"introduced you two, sensing a valuable connection\").\n- Vary wording for the match itself. Do not repeat \"opportunity\" across headline, summary, and narratorRemark when alternatives fit.\n- Prefer first names in user-facing copy. Avoid repeated full names unless disambiguation is necessary.\n- digestSummary must be grammatically complete as a standalone sentence. It should usually start with \"You might like meeting {Name} because ...\" for direct connections, or \"You may be able to help {Name} because ...\" for connector/introducer cards.\n- digestSummary must NOT use awkward third-person fragments like \"Name is...\", \"they're ..., and is...\", \"you is...\", or \"the discoverer's query\".\n- digestSummary must be one sentence, MUST fit within 180 characters when possible, and MUST contain no markdown links; the caller will attach links.\n- If you cannot fit every detail, choose one clear reason and stop. Do not rely on downstream truncation.\n\n**Introduction-originated opportunities (ONLY when INTRODUCTION CONTEXT is provided):**\nWhen INTRODUCTION CONTEXT is provided, this opportunity was explicitly created by an introducer. It was NOT automatically discovered.\n- For parties/patients/agents/peers viewing an introduction: keep the introducer signal in narratorRemark (and narrator chip), not in personalizedSummary.\n- For these introduced parties, personalizedSummary must focus ONLY on fit/value between viewer and counterpart. Do NOT mention the introducer there.\n- narratorRemark should carry the introduction signal (e.g., \"saw strong alignment between you two\" or \"thought this connection could be valuable\"), without repeating the narrator name at the start.\n- This is a personal recommendation, not an algorithm match. Frame it accordingly.\n\n**CRITICAL: NEVER include introducer names in personalizedSummary. Examples:**\n❌ WRONG: \"Seref introduced you to Lucy, who is actively seeking a product co-founder...\"\n✅ CORRECT: \"Lucy is actively seeking a product co-founder for a niche APAC marketplace. With your expertise in UX and AI, this could be an ideal collaboration.\"\n\n❌ WRONG: \"Bob thinks you should meet Alice because your React skills align with her needs.\"\n✅ CORRECT: \"Alice is building a React-based platform and needs frontend expertise. Your experience with component architecture makes you a strong fit.\"\n\n❌ WRONG: \"Jane connected you to Mark, who is looking for a designer.\"\n✅ CORRECT: \"Mark is building a consumer app and needs design expertise. Your background in user-centered design aligns well with what he's building.\"\n\nRemember: The introducer's name goes ONLY in narratorRemark, NEVER in personalizedSummary.\n\n**When INTRODUCTION CONTEXT is NOT provided (system-discovered match):**\n- Do NOT use introducer-style wording. Do NOT say \"you suggested\", \"this is an introduction you suggested\", or \"you suggested this connection\". The system found this match; no human introducer was involved.\n- Instead, narratorRemark should describe why the match is relevant (e.g. \"Based on your overlapping intents\", \"Your skills align with what they need\").\n\n**Negotiation-grounded explanations (ONLY when NEGOTIATION CONTEXT is provided):**\nWhen NEGOTIATION CONTEXT is provided, this opportunity passed through an agent-to-agent negotiation. Use the transcript to ground your explanation in the concrete reasoning the agents exchanged.\n- Personalize the summary with *why* the negotiation produced this match — reference the roles the agents agreed on, the specific concerns raised, and how they were resolved.\n- For status \"stalled\" with reason \"turn_cap\": the agents hit the turn limit without reaching agreement. Frame the card as a hedged possibility rather than a confident match; narratorRemark should signal \"agents couldn't fully converge\" without sounding negative.\n- For status \"stalled\" with reason \"timeout\": one side went silent. Suggest the user re-engage if interested.\n- For status \"accepted\": the agents agreed; the card should confidently explain *why* they agreed.\n- For status \"rejected\": the agents declined. The card should explain the reason briefly so the user understands — not dwell on it.\n- Do NOT invent turn content. Only reference what is in the NEGOTIATION CONTEXT block.\n\n- Exception for connector/introducer: if viewer role is \"introducer\" (any status), this is a curation/connector card. Use:\n - suggestedAction: one short line about sharing the intro or confirming the match.\n - mutualIntentsLabel: a short connector label (e.g. \"Connector match\", \"You can bridge this\").\n - headline: describe the connection between the parties (e.g., \"Connecting a PhD researcher with a translator\"). Do NOT reference the introducer's own needs.\n - personalizedSummary: explain why the parties you're introducing should meet, referencing THEIR profiles and intents, not yours.\n\n**CRITICAL for latent introducer cards (opportunity status is \"latent\"):**\nWhen the viewer is the introducer and the opportunity status is \"latent\", the introducer has NOT yet approved this match. They are evaluating whether to make the introduction.\n- narratorRemark MUST use evaluation/curation language (e.g. \"Could be a strong match\", \"Worth introducing?\", \"Interesting overlap here\").\n- Do NOT say \"you suggested\", \"you introduced\", \"you connected\", or any past-tense language implying the introduction was already made.\n- suggestedAction should encourage evaluation (e.g. \"Approve if you see the fit\").\n- Exception for new-connection reveal: if viewer role is \"agent\", status is \"accepted\", and there is an introducer, this is the agent's first time seeing this opportunity. Use:\n - suggestedAction: a short line about joining the conversation.\n`;\n\n// ──────────────────────────────────────────────────────────────\n// CLASS\n// ──────────────────────────────────────────────────────────────\n\nexport class OpportunityPresenter {\n private model: Runnable;\n private homeCardModel: Runnable;\n\n constructor() {\n this.model = model.withStructuredOutput(responseFormat, {\n name: \"opportunity_presenter\",\n });\n this.homeCardModel = model.withStructuredOutput(homeCardResponseFormat, {\n name: \"opportunity_presenter_home_card\",\n });\n }\n\n private async invokeWithTimeout(\n targetModel: Runnable,\n messages: (SystemMessage | HumanMessage)[],\n ): Promise<unknown> {\n const timeoutReason = `LLM invoke timed out after ${LLM_TIMEOUT_MS}ms`;\n const controller = new AbortController();\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n\n const invokePromise = targetModel.invoke(messages, {\n signal: controller.signal,\n });\n\n const timeoutPromise = new Promise<never>((_, reject) => {\n timeoutId = setTimeout(() => {\n controller.abort(timeoutReason);\n reject(new Error(timeoutReason));\n }, LLM_TIMEOUT_MS);\n });\n\n try {\n return await Promise.race([invokePromise, timeoutPromise]);\n } finally {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n }\n }\n\n /**\n * Generate personalized presentation for a single opportunity.\n */\n @Timed()\n public async present(\n input: PresenterInput,\n ): Promise<OpportunityPresentationResult> {\n const introContext = input.isIntroduction\n ? `\\nINTRODUCTION CONTEXT: This opportunity was created by an explicit introduction from ${input.introducerName ?? \"someone in the community\"}. It was NOT discovered automatically — a real person made this connection.\\n`\n : \"\";\n const humanContent = `\nVIEWER (the person seeing this opportunity):\n${input.viewerContext}\n\nOTHER PARTY:\n${input.otherPartyContext}\n\nMATCH CONTEXT:\n- Category: ${input.category}\n- Confidence: ${input.confidence}\n- Why we matched: ${input.matchReasoning}\n- Signals: ${input.signalsSummary}\n${introContext}\nCOMMUNITY: ${input.indexName}\nViewer's role in this opportunity: ${input.viewerRole}\n\nProduce headline, personalizedSummary (2-3 sentences in \"you\" language), suggestedAction, and greeting.\n`;\n\n try {\n const messages = [\n new SystemMessage(systemPrompt),\n new HumanMessage(humanContent),\n ];\n const result = await this.invokeWithTimeout(this.model, messages);\n const parsed = responseFormat.parse(result);\n parsed.presentation.personalizedSummary = stripUuids(parsed.presentation.personalizedSummary);\n return parsed.presentation;\n } catch (e) {\n const message = e instanceof Error ? e.message : String(e);\n const timeoutReason = message.includes(\"timed out\") ? message : undefined;\n logger.warn(\n \"[OpportunityPresenter.present] LLM failed, returning fallback\",\n {\n event: \"presenter_fallback\",\n presenter: \"opportunity\",\n reason: timeoutReason ? \"timeout\" : \"parse_error\",\n message,\n timeoutReason,\n },\n );\n return {\n headline: \"A promising connection\",\n personalizedSummary: truncateAtBoundary(stripUuids(input.matchReasoning), 300),\n suggestedAction: \"Take a look and decide whether to reach out.\",\n greeting: \"\",\n };\n }\n }\n\n /**\n * Generate LLM-powered home-card content (headline, body, narrator remark, mutual-intent label).\n * Callers append button labels from opportunity.constants.\n *\n * When `negotiationContext.status === 'negotiating'`, returns a templated\n * chip synchronously without invoking the LLM — the card just reflects\n * \"negotiation in progress\" at that point.\n */\n @Timed()\n public async presentHomeCard(\n input: HomeCardPresenterInput,\n ): Promise<HomeCardLLMResult> {\n if (input.negotiationContext?.status === 'negotiating') {\n return buildNegotiatingChip(input);\n }\n\n const mutualHint =\n input.mutualIntentCount != null && input.mutualIntentCount > 0\n ? `There are ${input.mutualIntentCount} overlapping intent(s) between viewer and other party.`\n : \"Match is based on profile and intent alignment. Do not cite a numeric intent count.\";\n const introContext = input.isIntroduction\n ? `\\nINTRODUCTION CONTEXT: This opportunity was created by an explicit introduction from ${input.introducerName ?? \"someone in the community\"}. It was NOT discovered automatically — a real person made this connection.\\n`\n : \"\";\n const negotiationBlock = buildNegotiationPromptBlock(input.negotiationContext);\n // When negotiation context exists, lead with it — these cards exist\n // *because* the negotiation happened. Trailing the block lets weaker\n // models lean on surface signals and ignore the transcript entirely.\n const negotiationDirective = negotiationBlock\n ? `\\nIMPORTANT: This opportunity surfaced because the agents negotiated and converged. Both your personalizedSummary AND your digestSummary MUST reference at least one specific signal from the NEGOTIATION CONTEXT block below — what concern was raised, what was confirmed, what the agents agreed on. The digestSummary is the one-line morning-brief sentence a user reads before deciding to act, so it must communicate *why this specific match* surfaced now (the negotiation that led to it), not a generic skill-complementarity line. Do not produce the generic summary every card looked like before this negotiation happened.\\n`\n : \"\";\n const humanContent = `\n${negotiationBlock}${negotiationDirective}\nVIEWER (the person seeing this opportunity):\n${input.viewerContext}\n\nOTHER PARTY:\n${input.otherPartyContext}\n\nMATCH CONTEXT:\n- Category: ${input.category}\n- Confidence: ${input.confidence}\n- Why we matched: ${input.matchReasoning}\n- Signals: ${input.signalsSummary}\n- ${mutualHint}\n${introContext}\nCOMMUNITY: ${input.indexName}\nViewer's role in this opportunity: ${input.viewerRole}\nOpportunity status: ${input.opportunityStatus ?? \"pending\"}\n\nProduce headline, personalizedSummary, digestSummary, suggestedAction, narratorRemark, greeting, and mutualIntentsLabel.\n`;\n\n const isIntroducer = input.viewerRole === \"introducer\";\n\n try {\n const messages = [\n new SystemMessage(homeCardSystemPrompt),\n new HumanMessage(humanContent),\n ];\n const result = await this.invokeWithTimeout(this.homeCardModel, messages);\n const parsed = homeCardResponseFormat.parse(result);\n parsed.presentation.personalizedSummary = stripUuids(parsed.presentation.personalizedSummary);\n parsed.presentation.digestSummary = stripUuids(parsed.presentation.digestSummary);\n parsed.presentation.narratorRemark = stripUuids(parsed.presentation.narratorRemark);\n if (/^0\\s+(mutual|overlapping)\\s+intent/i.test(parsed.presentation.mutualIntentsLabel)) {\n parsed.presentation.mutualIntentsLabel = \"Shared interests\";\n }\n if (input.isIntroduction && input.introducerName) {\n parsed.presentation.personalizedSummary = stripIntroducerMentions(\n parsed.presentation.personalizedSummary,\n input.introducerName,\n );\n parsed.presentation.digestSummary = stripIntroducerMentions(\n parsed.presentation.digestSummary,\n input.introducerName,\n );\n }\n return parsed.presentation;\n } catch (e) {\n const message = e instanceof Error ? e.message : String(e);\n const timeoutReason = message.includes(\"timed out\") ? message : undefined;\n logger.warn(\n \"[OpportunityPresenter.presentHomeCard] LLM failed, returning fallback\",\n {\n event: \"presenter_fallback\",\n presenter: \"home_card\",\n reason: timeoutReason ? \"timeout\" : \"parse_error\",\n message,\n timeoutReason,\n },\n );\n let fallbackSummary = truncateAtBoundary(stripUuids(input.matchReasoning), 300);\n if (input.isIntroduction && input.introducerName) {\n fallbackSummary = stripIntroducerMentions(fallbackSummary, input.introducerName);\n }\n return {\n headline: \"A promising connection\",\n personalizedSummary: fallbackSummary,\n digestSummary: isIntroducer\n ? \"You may be able to help make a useful introduction here.\"\n : \"You might like meeting them based on your current interests.\",\n suggestedAction: isIntroducer\n ? \"Share this introduction to get things started.\"\n : \"Take a look and decide whether to reach out.\",\n narratorRemark: \"Worth a look.\",\n mutualIntentsLabel: isIntroducer\n ? \"Connector match\"\n : input.mutualIntentCount != null && input.mutualIntentCount > 0\n ? `${input.mutualIntentCount} mutual intent${input.mutualIntentCount !== 1 ? \"s\" : \"\"}`\n : \"Shared interests\",\n greeting: \"\",\n };\n }\n }\n\n /**\n * Process multiple opportunities in parallel with bounded concurrency.\n */\n @Timed()\n public async presentBatch(\n inputs: PresenterInput[],\n options?: { concurrency?: number },\n ): Promise<OpportunityPresentationResult[]> {\n const concurrency = options?.concurrency ?? 5;\n const results: OpportunityPresentationResult[] = [];\n for (let i = 0; i < inputs.length; i += concurrency) {\n const chunk = inputs.slice(i, i + concurrency);\n const chunkResults = await Promise.all(\n chunk.map((inp) => this.present(inp)),\n );\n results.push(...chunkResults);\n }\n return results;\n }\n\n /**\n * Process multiple opportunities as home cards in parallel with bounded concurrency.\n * Returns full home-card display contracts (headline, body, narrator remark, action labels, mutual-intent label).\n */\n @Timed()\n public async presentHomeCardBatch(\n inputs: HomeCardPresenterInput[],\n options?: { concurrency?: number },\n ): Promise<HomeCardLLMResult[]> {\n const concurrency = options?.concurrency ?? 5;\n const results: HomeCardLLMResult[] = [];\n for (let i = 0; i < inputs.length; i += concurrency) {\n const chunk = inputs.slice(i, i + concurrency);\n const chunkResults = await Promise.all(\n chunk.map((inp) => this.presentHomeCard(inp)),\n );\n results.push(...chunkResults);\n }\n return results;\n }\n}\n\n// ──────────────────────────────────────────────────────────────\n// NEGOTIATION CONTEXT HELPERS\n// ──────────────────────────────────────────────────────────────\n\n/**\n * Builds a \"NEGOTIATION CONTEXT:\" block for the home-card prompt. Returns an\n * empty string when the opportunity has no meaningful negotiation context\n * (draft/latent) or when the opportunity is still negotiating (handled via\n * the templated chip, not the LLM).\n */\nfunction buildNegotiationPromptBlock(context: NegotiationContext | undefined): string {\n if (!context || context.status === 'negotiating') return \"\";\n\n const turnCapLabel = context.turnCap > 0 ? `${context.turnCap}` : \"unlimited\";\n const reason = context.outcome?.reason;\n const reasonLabel = reason === 'turn_cap'\n ? \"agents hit the turn cap without converging\"\n : reason === 'timeout'\n ? \"counterpart went silent before responding\"\n : undefined;\n\n const turnLines = (context.turns ?? []).map((turn, index) => {\n const action = turn.action;\n const reasoning = turn.assessment?.reasoning ?? \"(no reasoning)\";\n const message = turn.message ? ` — said: \"${turn.message}\"` : \"\";\n return `Turn ${index + 1} (${action}): ${reasoning}${message}`;\n });\n\n const outcomeSummary = context.outcome\n ? `Final outcome: ${context.outcome.hasOpportunity ? \"agreed\" : \"declined\"} — ${context.outcome.reasoning}`\n : \"Final outcome: not recorded.\";\n\n return `\nNEGOTIATION CONTEXT:\n- Negotiation status: ${context.status}${reasonLabel ? ` (${reasonLabel})` : \"\"}\n- Turns exchanged: ${context.turnCount} of ${turnCapLabel}\n- Transcript:\n${turnLines.length > 0 ? turnLines.map((l) => ` ${l}`).join(\"\\n\") : \" (no turns recorded)\"}\n- ${outcomeSummary}\n`;\n}\n\n/**\n * Builds a templated home-card result for an opportunity whose negotiation\n * is still in progress. Bypasses the LLM so users see a stable \"currently\n * negotiating\" chip while turns are still being exchanged.\n */\nfunction buildNegotiatingChip(input: HomeCardPresenterInput): HomeCardLLMResult {\n const ctx = input.negotiationContext;\n const turnCount = ctx?.turnCount ?? 0;\n const turnCap = ctx?.turnCap && ctx.turnCap > 0 ? ctx.turnCap : undefined;\n const narratorRemark = turnCap\n ? `Currently negotiating · turn ${turnCount} of ${turnCap}`\n : `Currently negotiating · turn ${turnCount}`;\n\n return {\n headline: \"Negotiation in progress\",\n personalizedSummary: \"Your agent is still talking with theirs to see if this connection makes sense. We'll surface the full match as soon as they converge.\",\n digestSummary: \"Your agent is still checking whether this connection makes sense.\",\n suggestedAction: \"Check back shortly — no action needed yet.\",\n narratorRemark,\n mutualIntentsLabel: input.mutualIntentCount && input.mutualIntentCount > 0\n ? `${input.mutualIntentCount} mutual intent${input.mutualIntentCount !== 1 ? \"s\" : \"\"}`\n : \"Shared interests\",\n greeting: \"\",\n };\n}\n\n// ──────────────────────────────────────────────────────────────\n// CONTEXT GATHERER (used by tools)\n// ──────────────────────────────────────────────────────────────\n\n/**\n * Gather all context needed for the presenter from the database.\n * Fetches viewer profile, viewer intents, other party profile(s), and index in parallel.\n *\n * @param displayCounterpartUserId - When set (e.g. for home card), only this counterpart is included in otherPartyContext so the presenter writes about the person on the card. Omitted for introducer view (card shows both parties).\n */\nexport async function gatherPresenterContext(\n database: PresenterDatabase,\n opportunity: Opportunity,\n viewerId: string,\n displayCounterpartUserId?: string,\n): Promise<PresenterInput> {\n const myActor = opportunity.actors.find((a) => a.userId === viewerId);\n if (!myActor) {\n throw new Error(\"Viewer is not an actor in this opportunity\");\n }\n\n const isIntroducer = myActor.role === \"introducer\";\n const otherActors = opportunity.actors.filter((a) => a.userId !== viewerId);\n let otherPartyIds = [...new Set(otherActors.map((a) => a.userId))];\n if (\n displayCounterpartUserId &&\n !isIntroducer &&\n otherPartyIds.includes(displayCounterpartUserId)\n ) {\n otherPartyIds = [displayCounterpartUserId];\n }\n\n const contextIndexId = opportunity.context?.networkId;\n\n // For introducers: fetch profiles + intents for both parties; skip introducer's own intents.\n // For other roles: fetch viewer's profile + intents and other party profiles.\n const [viewerProfile, indexRecord, ...otherProfiles] = await Promise.all([\n database.getProfile(viewerId),\n contextIndexId ? database.getNetwork(contextIndexId) : Promise.resolve(null),\n ...otherPartyIds.map((uid) => database.getProfile(uid)),\n ]);\n\n // Fetch intents: for introducer, fetch each party's intents; otherwise fetch viewer's intents.\n let viewerIntents:\n | Awaited<ReturnType<typeof database.getActiveIntents>>\n | undefined;\n let partyIntentsMap:\n | Map<string, Awaited<ReturnType<typeof database.getActiveIntents>>>\n | undefined;\n\n if (isIntroducer) {\n const partyIntentResults = await Promise.all(\n otherPartyIds.map(async (uid) => ({\n uid,\n intents: await database.getActiveIntents(uid),\n })),\n );\n partyIntentsMap = new Map(\n partyIntentResults.map((r) => [r.uid, r.intents]),\n );\n } else {\n viewerIntents = await database.getActiveIntents(viewerId);\n }\n\n // Fetch premises when any actor is premise-grounded\n const premiseGroundedActors = opportunity.actors.filter((a) => a.premise);\n let viewerPremiseContext = '';\n let otherPremiseContext = '';\n\n if (premiseGroundedActors.length > 0) {\n // Only fetch premises for actors that are actually premise-grounded, not all parties\n const groundedOtherIds = premiseGroundedActors\n .filter((a) => a.userId !== viewerId)\n .map((a) => a.userId);\n const viewerIsGrounded = premiseGroundedActors.some((a) => a.userId === viewerId);\n\n const results = await Promise.all([\n ...(viewerIsGrounded ? [database.getPremisesForUser(viewerId, 'ACTIVE')] : []),\n ...groundedOtherIds.map((uid) => database.getPremisesForUser(uid, 'ACTIVE')),\n ]);\n\n let idx = 0;\n if (viewerIsGrounded) {\n const viewerPremises = results[idx++];\n if (viewerPremises?.length) {\n viewerPremiseContext =\n '\\nPremises (self-descriptions):\\n' +\n viewerPremises\n .slice(0, 5)\n .map((p) => `- ${p.assertion.text}`)\n .join('\\n');\n }\n }\n\n const otherPremiseLines: string[] = [];\n for (let i = 0; i < groundedOtherIds.length; i++) {\n const premises = results[idx++];\n if (premises?.length) {\n for (const p of premises.slice(0, 3)) {\n otherPremiseLines.push(`- ${p.assertion.text}`);\n }\n }\n }\n if (otherPremiseLines.length > 0) {\n otherPremiseContext = '\\nPremises (self-descriptions):\\n' + otherPremiseLines.join('\\n');\n }\n }\n\n let viewerContext: string;\n let otherPartyContext: string;\n\n if (isIntroducer) {\n // Introducer view: minimal viewer context (just name + role), rich other-party context with intents\n const introducerApproved = opportunity.actors.find(a => a.role === 'introducer')?.approved === true;\n const hasApproved = introducerApproved || opportunity.status !== 'latent';\n viewerContext = [\n \"Profile:\",\n `Name: ${viewerProfile?.identity?.name ?? \"Unknown\"}`,\n hasApproved\n ? \"Role: You are the introducer who suggested this connection.\"\n : \"Role: You are being asked whether these two people would benefit from meeting. You have NOT yet approved this introduction.\",\n ].join(\"\\n\");\n\n const otherParts = otherPartyIds.map((uid, idx) => {\n const profile = otherProfiles[idx] as Awaited<\n ReturnType<typeof database.getProfile>\n >;\n const name = profile?.identity?.name ?? \"Unknown\";\n const bio = profile?.identity?.bio ?? \"\";\n const location = profile?.identity?.location ?? \"\";\n const context = profile?.context ?? \"\";\n const intents = partyIntentsMap?.get(uid);\n const intentLines = intents?.length\n ? intents\n .slice(0, 5)\n .map((i) => ` - ${i.payload}${i.summary ? ` (${i.summary})` : \"\"}`)\n : [\" (no active intents)\"];\n return [\n `${name}:`,\n ` Bio: ${bio}`,\n location ? ` Location: ${location}` : null,\n context ? ` Context: ${context}` : null,\n ` Active intents:`,\n ...intentLines,\n ]\n .filter(Boolean)\n .join(\"\\n\");\n });\n otherPartyContext =\n otherParts.join(\"\\n\\n\") || \"Parties (details not available).\";\n } else {\n // Non-introducer view: full viewer profile + intents, other party profiles\n const viewerContextLines = [\n \"Profile:\",\n `Name: ${viewerProfile?.identity?.name ?? \"Unknown\"}`,\n `Bio: ${viewerProfile?.identity?.bio ?? \"\"}`,\n `Location: ${viewerProfile?.identity?.location ?? \"\"}`,\n `Context: ${viewerProfile?.context ?? \"\"}`,\n \"Active intents:\",\n ...(viewerIntents?.length\n ? viewerIntents.map(\n (i) => `- ${i.payload}${i.summary ? ` (${i.summary})` : \"\"}`,\n )\n : [\"(none listed)\"]),\n ];\n viewerContext = viewerContextLines.join(\"\\n\");\n\n const otherParts = otherPartyIds.map((uid, idx) => {\n const profile = otherProfiles[idx] as Awaited<\n ReturnType<typeof database.getProfile>\n >;\n const name = profile?.identity?.name ?? \"Unknown\";\n const bio = profile?.identity?.bio ?? \"\";\n return `${name}: ${bio}`;\n });\n otherPartyContext =\n otherParts.join(\"\\n\\n\") || \"Other party (details not available).\";\n }\n\n const interp = opportunity.interpretation;\n const signalsSummary =\n interp.signals?.map((s) => `${s.type}: ${s.detail ?? s.type}`).join(\"; \") ??\n \"Match based on profile and intent alignment.\";\n\n // Detect introduction-originated opportunities: only when there is an explicit introducer actor.\n // Do NOT use detection.source === \"manual\" alone — system-discovered opportunities can have manual source without an introducer.\n const introducerActor = opportunity.actors.find(\n (a) => a.role === \"introducer\",\n );\n const isIntroduction = !!introducerActor;\n let introducerName: string | undefined;\n if (introducerActor) {\n introducerName = opportunity.detection.createdByName;\n if (!introducerName) {\n const introducerProfile = await database.getProfile(\n introducerActor.userId,\n );\n introducerName = introducerProfile?.identity?.name ?? undefined;\n }\n }\n\n const counterpartName =\n otherPartyIds.length === 1 && otherProfiles[0]\n ? (otherProfiles[0] as { identity?: { name?: string } })?.identity?.name?.trim()\n : undefined;\n const viewerNameForFilter = viewerProfile?.identity?.name?.trim();\n const matchReasoning =\n counterpartName && interp.reasoning\n ? viewerCentricCardSummary(\n interp.reasoning,\n counterpartName,\n 400,\n viewerNameForFilter,\n introducerName,\n )\n : stripUuids(interp.reasoning);\n\n if (viewerPremiseContext) {\n viewerContext += viewerPremiseContext;\n }\n if (otherPremiseContext) {\n otherPartyContext += otherPremiseContext;\n }\n\n const result: PresenterInput = {\n viewerContext,\n otherPartyContext,\n matchReasoning,\n category: interp.category ?? \"connection\",\n confidence:\n typeof interp.confidence === \"number\"\n ? interp.confidence\n : parseFloat(String(interp.confidence ?? 0)) || 0,\n signalsSummary,\n indexName: indexRecord?.title ?? contextIndexId ?? \"\",\n viewerRole: myActor.role ?? \"party\",\n isIntroduction,\n introducerName,\n };\n\n return result;\n}\n"]}
@@ -42,6 +42,17 @@ export declare function resolveActionableLinkKind(input: {
42
42
  * Trailing slashes on frontendUrl are stripped before concatenation.
43
43
  */
44
44
  export declare function buildProfileUrl(counterpartUserId: string, frontendUrl: string | undefined): string | undefined;
45
+ /**
46
+ * Build the deep-link to an opportunity's A2A negotiation trace
47
+ * (`/chat/:conversationId`) so users can see *what negotiation led to* the
48
+ * surfaced opportunity (EDG-50/EDG-51). Returns `undefined` when `frontendUrl`
49
+ * is unset or there is no negotiation conversation to link to.
50
+ *
51
+ * The `?link_preview=false` hint mirrors `buildProfileUrl` — chat-gateway
52
+ * runtimes (e.g. Telegram delivery) strip link previews when it is present.
53
+ * Trailing slashes on `frontendUrl` are stripped before concatenation.
54
+ */
55
+ export declare function buildNegotiationUrl(conversationId: string | undefined, frontendUrl: string | undefined): string | undefined;
45
56
  /**
46
57
  * Mint a short-link for `card` if the (status, viewerRole, viewerApproved)
47
58
  * combination is actionable; mutate the card in place with `acceptUrl`,
@@ -117,6 +128,8 @@ type OpportunityCardLike = Record<string, unknown> & {
117
128
  feedCategory?: string | undefined;
118
129
  acceptUrl?: string | undefined;
119
130
  profileUrl?: string | undefined;
131
+ /** Deep-link to the A2A negotiation trace that produced this opportunity. */
132
+ negotiationUrl?: string | undefined;
120
133
  score?: number | undefined;
121
134
  /** Digest-mode cooldown re-show — the user has seen this card before. */
122
135
  redelivery?: boolean | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"opportunity.tools.d.ts","sourceRoot":"/","sources":["opportunity/opportunity.tools.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAC;AA6B5E,OAAO,KAAK,EAAE,WAAW,EAAqB,MAAM,4CAA4C,CAAC;AAEjG,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gDAAgD,CAAC;AAOtF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,yBAAyB,CAAC,KAAK,EAAE;IAC/C,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,GAAG,eAAe,GAAG,IAAI,CAczB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAC7B,iBAAiB,EAAE,MAAM,EACzB,WAAW,EAAE,MAAM,GAAG,SAAS,GAC9B,MAAM,GAAG,SAAS,CAQpB;AAED;;;;;;;GAOG;AACH,wBAAsB,qBAAqB,CACzC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG;IAC9B,aAAa,EAAE,MAAM,CAAC;IACtB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;CAChB,EACD,IAAI,EAAE;IACJ,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,eAAe,EAAE,WAAW,CAAC,QAAQ,CAAC,iBAAiB,CAAC,CAAC,CAAC;IAC1D,WAAW,EAAE,MAA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,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,2CAouD5E"}
@@ -5,6 +5,7 @@ import { MINIMAL_MAIN_TEXT_MAX_CHARS, getPrimaryActionLabel, SECONDARY_ACTION_LA
5
5
  import { viewerCentricCardSummary, narratorRemarkFromReasoning, stripUuids } from "./opportunity.presentation.js";
6
6
  import { runDiscoverFromQuery, continueDiscovery } from "./opportunity.discover.js";
7
7
  import { OpportunityPresenter, gatherPresenterContext } from "./opportunity.presenter.js";
8
+ import { loadNegotiationContext } from "./negotiation-context.loader.js";
8
9
  function stripLeadingNarratorName(remark, narratorName) {
9
10
  let t = remark.trim();
10
11
  if (!t || !narratorName.trim())
@@ -95,6 +96,22 @@ export function buildProfileUrl(counterpartUserId, frontendUrl) {
95
96
  const base = frontendUrl.replace(/\/+$/, "");
96
97
  return `${base}/u/${counterpartUserId}?link_preview=false`;
97
98
  }
99
+ /**
100
+ * Build the deep-link to an opportunity's A2A negotiation trace
101
+ * (`/chat/:conversationId`) so users can see *what negotiation led to* the
102
+ * surfaced opportunity (EDG-50/EDG-51). Returns `undefined` when `frontendUrl`
103
+ * is unset or there is no negotiation conversation to link to.
104
+ *
105
+ * The `?link_preview=false` hint mirrors `buildProfileUrl` — chat-gateway
106
+ * runtimes (e.g. Telegram delivery) strip link previews when it is present.
107
+ * Trailing slashes on `frontendUrl` are stripped before concatenation.
108
+ */
109
+ export function buildNegotiationUrl(conversationId, frontendUrl) {
110
+ if (!frontendUrl || !conversationId)
111
+ return undefined;
112
+ const base = frontendUrl.replace(/\/+$/, "");
113
+ return `${base}/chat/${conversationId}?link_preview=false`;
114
+ }
98
115
  /**
99
116
  * Mint a short-link for `card` if the (status, viewerRole, viewerApproved)
100
117
  * combination is actionable; mutate the card in place with `acceptUrl`,
@@ -289,6 +306,8 @@ export function buildOpportunityPresentation(cards, opts) {
289
306
  lines.push(` profileUrl: ${card.profileUrl}`);
290
307
  if (card.acceptUrl)
291
308
  lines.push(` acceptUrl: ${card.acceptUrl}`);
309
+ if (opts.includeDigestMarkers && card.negotiationUrl)
310
+ lines.push(` negotiationUrl: ${card.negotiationUrl}`);
292
311
  if (card.feedCategory)
293
312
  lines.push(` feedCategory: ${card.feedCategory}`);
294
313
  if (opts.includeDigestMarkers && card.score != null)
@@ -1528,11 +1547,22 @@ export function createOpportunityTools(defineTool, deps) {
1528
1547
  const viewerRole = viewerActor?.role ?? "party";
1529
1548
  const isCounterpartGhost = counterpartUser?.isGhost ?? false;
1530
1549
  try {
1531
- const ctx = await gatherOpportunityPresenterContext(presenterDb, opp, context.userId, counterpartUserId);
1550
+ // Load the negotiation context alongside presenter context so
1551
+ // the digest copy can explain *why* the opportunity surfaced
1552
+ // (EDG-50) — the presenter grounds `digestSummary` in concrete
1553
+ // negotiation turns when this is present. The same context
1554
+ // yields the conversationId for the negotiation-trace link
1555
+ // (EDG-51).
1556
+ const [ctx, negotiationContext] = await Promise.all([
1557
+ gatherOpportunityPresenterContext(presenterDb, opp, context.userId, counterpartUserId),
1558
+ loadNegotiationContext(deps.negotiationDatabase, opp.id, opp.status),
1559
+ ]);
1532
1560
  const presentation = await presenter.presentHomeCard({
1533
1561
  ...ctx,
1534
1562
  opportunityStatus: opp.status,
1563
+ ...(negotiationContext ? { negotiationContext } : {}),
1535
1564
  });
1565
+ const negotiationUrl = buildNegotiationUrl(negotiationContext?.conversationId, deps.frontendUrl);
1536
1566
  // Build narrator chip from presenter output
1537
1567
  let narratorChip;
1538
1568
  const introducerIsCounterpart = introducerActor && counterpartActor && introducerActor.userId === counterpartActor.userId;
@@ -1558,6 +1588,9 @@ export function createOpportunityTools(defineTool, deps) {
1558
1588
  avatar: counterpartUser?.avatar ?? null,
1559
1589
  mainText: stripUuids(presentation.personalizedSummary),
1560
1590
  digestSummary: stripUuids(presentation.digestSummary),
1591
+ // Deep-link to the negotiation trace that produced this card
1592
+ // (EDG-51). Only present when a negotiation conversation exists.
1593
+ ...(negotiationUrl ? { negotiationUrl } : {}),
1561
1594
  cta: presentation.suggestedAction,
1562
1595
  headline: viewerIsIntroducerHere && secondPartyNameForHeadline
1563
1596
  ? `${counterpartName} → ${secondPartyNameForHeadline}`