@indexnetwork/protocol 65.0.0 → 66.0.0-rc.609.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/IMPLEMENTATION.md +11 -12
  3. package/STABILITY.md +0 -1
  4. package/dist/index.d.ts +5 -7
  5. package/dist/index.js +3 -6
  6. package/dist/internal/opportunities/opportunity.cards.d.ts +78 -0
  7. package/dist/internal/opportunities/opportunity.cards.js +165 -0
  8. package/dist/internal/opportunities/opportunity.graph.modes.d.ts +0 -1
  9. package/dist/internal/opportunities/opportunity.graph.modes.js +0 -5
  10. package/dist/internal/opportunities/opportunity.labels.d.ts +0 -2
  11. package/dist/internal/opportunities/opportunity.labels.js +0 -2
  12. package/dist/internal/opportunities/opportunity.presentation.d.ts +21 -185
  13. package/dist/internal/opportunities/opportunity.presentation.js +35 -651
  14. package/dist/internal/opportunities/opportunity.utils.d.ts +0 -42
  15. package/dist/internal/opportunities/opportunity.utils.js +0 -102
  16. package/dist/platform/database/capabilities.d.ts +2 -2
  17. package/dist/platform/database/entities.d.ts +1 -1
  18. package/package.json +1 -1
  19. package/dist/internal/opportunities/opportunity.feed-selection.d.ts +0 -24
  20. package/dist/internal/opportunities/opportunity.feed-selection.js +0 -40
  21. package/dist/internal/opportunities/radar/radar.graph.d.ts +0 -230
  22. package/dist/internal/opportunities/radar/radar.graph.js +0 -537
  23. package/dist/internal/opportunities/radar/radar.state.d.ts +0 -88
  24. package/dist/internal/opportunities/radar/radar.state.js +0 -87
  25. package/dist/internal/shared/utils/claim-safety.d.ts +0 -24
  26. package/dist/internal/shared/utils/claim-safety.js +0 -88
@@ -2,186 +2,26 @@
2
2
  * The opportunity presentation cluster.
3
3
  *
4
4
  * One file for the whole path from a persisted opportunity to the copy a user
5
- * reads: the pure text transforms, the cache-key builders, the safe-fallback
6
- * pipeline, and the LLM presenter itself. They were four modules that only ever
7
- * called each other in one direction, and following a card's copy meant hopping
8
- * between them.
5
+ * reads: the cache-key builders and the LLM presenter itself. All user-facing
6
+ * copy comes from the presenter; there is no deterministic fallback.
9
7
  *
10
8
  * Sections, in dependency order:
11
- * 1. Pure presentation transforms
12
- * 2. Presentation cache keys
13
- * 3. Safe-presentation pipeline (fallbacks that never leak raw reasoning)
14
- * 4. OpportunityPresenter (LLM card and chat copy)
9
+ * 1. Presentation cache keys
10
+ * 2. OpportunityPresenter (LLM card and chat copy)
15
11
  */
16
12
  import { z } from "zod";
17
13
  import type { Opportunity } from "../../platform/database.js";
18
14
  import type { CompositeDatabase } from "../../platform/database.js";
19
15
  import type { NegotiationContext } from "./negotiation-context.loader.js";
20
- /**
21
- * Pure presentation layer for opportunities.
22
- * Generates title, description, and CTA based on viewer context — no DB access.
23
- */
24
- export interface OpportunityPresentation {
25
- title: string;
26
- description: string;
27
- callToAction: string;
28
- }
29
16
  export interface UserInfo {
30
17
  id: string;
31
18
  name: string;
32
19
  avatar: string | null;
33
20
  }
34
- /**
35
- * Generate presentation copy for an opportunity based on viewer context.
36
- * Pure function — no side effects, no database access.
37
- */
38
- export declare function presentOpportunity(opp: Opportunity, viewerId: string, otherPartyInfo: UserInfo, format: 'card' | 'email' | 'notification'): OpportunityPresentation;
39
- export declare function stripUuids(text: string): string;
40
- /**
41
- * Truncate user-facing text to at most `maxChars` without cutting mid-word.
42
- *
43
- * Prefers a sentence boundary, then a word boundary, and only falls back to a
44
- * hard slice if no boundary exists within the limit. An ellipsis is appended
45
- * when the text is actually shortened. Used by presenter fallbacks so a degraded
46
- * card never shows a sentence chopped mid-word (e.g. "His focus on 'indiv").
47
- */
48
- export declare function truncateAtBoundary(text: string, maxChars: number): string;
49
- /**
50
- * Returns viewer-centric main text for an opportunity card.
51
- * Prefers the part of the reasoning that describes the counterpart (the person
52
- * on the card), so the viewer sees an introduction to the counterpart rather
53
- * than a description of themselves.
54
- *
55
- * @param reasoning - Raw interpretation.reasoning (may describe both parties).
56
- * @param counterpartName - Display name of the suggested connection (e.g. "Alex Chen").
57
- * @param maxChars - Max length of returned string (default MINIMAL_MAIN_TEXT_MAX_CHARS).
58
- * @param viewerName - Optional display name of the viewer (signed-in user). When provided, sentences or prefixes describing the viewer are skipped so the card introduces the counterpart, not the viewer.
59
- * @returns Viewer-centric snippet mentioning the counterpart when possible; if counterpartName is empty, returns reasoning truncated to maxChars. Never null; may be "A suggested connection." when reasoning is empty.
60
- */
61
- export declare function viewerCentricCardSummary(reasoning: string, counterpartName: string, maxChars?: number, viewerName?: string): string;
62
- /**
63
- * Generates a short narrator remark from opportunity reasoning for the narrator chip.
64
- * Used by the minimal (no-LLM) card path so each card gets a unique remark
65
- * instead of the same static text.
66
- *
67
- * Extracts domain keywords (e.g. "AI", "design", "machine learning") from the
68
- * reasoning and frames them in a short template like "Shared interest in AI and design."
69
- *
70
- * This is a regex-based heuristic — an alternative is OpportunityPresenter.presentCard()
71
- * which generates narratorRemark via LLM with much higher quality.
72
- *
73
- * @param reasoning - Raw interpretation.reasoning text.
74
- * @param counterpartName - Display name of the counterpart (stripped from output).
75
- * @param viewerName - Optional display name of the viewer (stripped from output).
76
- * @returns A short remark (max ~80 chars) suitable for the narrator chip. Never truncated with "...".
77
- */
78
- export declare function narratorRemarkFromReasoning(reasoning: string, counterpartName: string, viewerName?: string): string;
79
21
  /** Cache namespace for opportunity presentation copy. Bump to invalidate copy safety changes. */
80
22
  export declare const OPPORTUNITY_PRESENTATION_CACHE_VERSION = "v2";
81
- export declare function buildRadarCardPresentationCacheKey(opportunityId: string, status: string, viewerId: string, focusedViewerIntentId?: string): string;
23
+ export declare function buildOpportunityCardCacheKey(opportunityId: string, status: string, viewerId: string, focusedViewerIntentId?: string): string;
82
24
  export declare function buildApiChatCardPresentationCacheKey(opportunityId: string, viewerId: string): string;
83
- /**
84
- * Shared safe-presentation primitive for all user-facing opportunity surfaces.
85
- *
86
- * Historically every surface (radar, list/discover cards, minimal chat
87
- * cards, notification emails/Telegram, chat context, delivery cards) invented
88
- * its own fallback chain for the case where genuine LLM presenter output is
89
- * unavailable — some sliced raw `interpretation.reasoning` with no
90
- * sanitization at all. This module is the single standard:
91
- *
92
- * raw reasoning
93
- * → whitespace-normalize
94
- * → viewer-centric rewrite (incl. UUID stripping)
95
- * → boundary-aware truncation
96
- * → per-surface empty-text default
97
- *
98
- * Surfaces choose *policy* (send a sanitized fallback vs skip entirely) via
99
- * `allowFallback`; they no longer choose (or forget) sanitization steps.
100
- *
101
- * See `packages/protocol/s./opportunity/AGENTS.md` for the review checklist this
102
- * module exists to satisfy.
103
- */
104
- /** Default max length for fallback summaries (matches presenter internal fallback). */
105
- export declare const SAFE_FALLBACK_MAX_CHARS = 300;
106
- /** Default copy when no reasoning text is available at all. */
107
- export declare const DEFAULT_EMPTY_FALLBACK_TEXT = "A promising connection.";
108
- /** Default headline for fallback presentations (matches presenter internal fallback). */
109
- export declare const DEFAULT_FALLBACK_HEADLINE = "A promising connection";
110
- /** Default CTA for fallback presentations (matches presenter internal fallback). */
111
- export declare const DEFAULT_FALLBACK_ACTION = "Take a look and decide whether to reach out.";
112
- export interface SafeFallbackOptions {
113
- /** Display name of the counterpart shown on the card (enables viewer-centric rewrite). */
114
- counterpartName?: string;
115
- /** Display name of the viewer; sentences describing the viewer are skipped/rewritten to "you". */
116
- viewerName?: string;
117
- /** Max output length (boundary-aware). Default {@link SAFE_FALLBACK_MAX_CHARS}. */
118
- maxChars?: number;
119
- /** Copy returned when reasoning is empty/blank. Default {@link DEFAULT_EMPTY_FALLBACK_TEXT}. */
120
- emptyText?: string;
121
- }
122
- /**
123
- * Produce safe user-facing fallback copy from raw match reasoning.
124
- *
125
- * This is the ONE sanitization standard: UUID stripping,
126
- * stripping, and viewer-centric rewrite (via {@link viewerCentricCardSummary}),
127
- * followed by whitespace normalization and boundary-aware truncation (via
128
- * {@link truncateAtBoundary}). Never returns raw reasoning verbatim beyond
129
- * these guarantees, and never returns an empty string.
130
- *
131
- * @param rawReasoning - Raw `interpretation.reasoning` / `matchReason` text (may be null/undefined).
132
- * @param opts - Per-surface knobs (names for rewrite, max length, empty-text copy).
133
- */
134
- export declare function safeFallbackSummary(rawReasoning: string | null | undefined, opts?: SafeFallbackOptions): string;
135
- /** Minimal presenter-output shape the primitive inspects (subset of CardPresentationResult). */
136
- export interface SafePresentationCandidate {
137
- headline?: string;
138
- personalizedSummary?: string;
139
- suggestedAction?: string;
140
- /** Set by OpportunityPresenter when its LLM call failed and it returned fallback-shaped copy. */
141
- isFallback?: boolean;
142
- }
143
- /** Opportunity-ish source object accepted by {@link getSafePresentationOrSkip}. */
144
- export interface SafePresentationSource {
145
- /** Presenter output attached to the record, when available. */
146
- homeCardPresentation?: SafePresentationCandidate | null;
147
- /** Pre-truncated raw reasoning carried on discovery/list card data. */
148
- matchReason?: string | null;
149
- /** Full opportunity interpretation, when the caller holds the record. */
150
- interpretation?: {
151
- reasoning?: string | null;
152
- } | null;
153
- }
154
- export interface SafePresentationOptions extends SafeFallbackOptions {
155
- /**
156
- * Policy switch: when false, return null instead of fallback copy so the
157
- * surface can skip rendering entirely (e.g. scheduled digests where sending
158
- * degraded copy is worse than sending nothing). Default true.
159
- */
160
- allowFallback?: boolean;
161
- }
162
- /** Resolved safe presentation for a surface to render. */
163
- export interface SafePresentation {
164
- headline: string;
165
- summary: string;
166
- suggestedAction: string;
167
- /** True when copy was derived from raw reasoning rather than genuine LLM presenter output. */
168
- isFallback: boolean;
169
- }
170
- /**
171
- * Resolve the safe user-facing presentation for an opportunity, or signal skip.
172
- *
173
- * Resolution order:
174
- * 1. Genuine presenter output (`homeCardPresentation` present, non-empty, and
175
- * NOT tagged `isFallback` by the presenter) — claim-validated before return.
176
- * 2. Otherwise, if `allowFallback` (default true): sanitized fallback copy
177
- * built from `matchReason` / `interpretation.reasoning` via
178
- * {@link safeFallbackSummary}.
179
- * 3. Otherwise `null` — the surface must skip this opportunity.
180
- *
181
- * Raw `interpretation.reasoning` / `matchReason` never reaches the caller
182
- * unsanitized through this function.
183
- */
184
- export declare function getSafePresentationOrSkip(source: SafePresentationSource, opts?: SafePresentationOptions): SafePresentation | null;
185
25
  /**
186
26
  * Opportunity Presenter Agent
187
27
  *
@@ -201,22 +41,17 @@ declare const PresentationSchema: z.ZodObject<{
201
41
  suggestedAction: z.ZodString;
202
42
  greeting: z.ZodString;
203
43
  }, "strip", z.ZodTypeAny, {
204
- personalizedSummary: string;
205
44
  headline: string;
45
+ personalizedSummary: string;
206
46
  suggestedAction: string;
207
47
  greeting: string;
208
48
  }, {
209
- personalizedSummary: string;
210
49
  headline: string;
50
+ personalizedSummary: string;
211
51
  suggestedAction: string;
212
52
  greeting: string;
213
53
  }>;
214
- export type OpportunityPresentationResult = z.infer<typeof PresentationSchema> & {
215
- /** True when any output field used resilience fallback copy. */
216
- isFallback?: boolean;
217
- /** Diagnostic category; never changes production fallback policy. */
218
- fallbackReason?: "timeout" | "error" | "sanitization";
219
- };
54
+ export type OpportunityPresentationResult = z.infer<typeof PresentationSchema>;
220
55
  /** Input for card presenter call; extends PresenterInput with optional mutual intent count. */
221
56
  export interface CardPresenterInput extends PresenterInput {
222
57
  /** Number of overlapping intents (for generating mutualIntentsLabel). */
@@ -238,30 +73,22 @@ export declare const CardLLMSchema: z.ZodObject<{
238
73
  mutualIntentsLabel: z.ZodString;
239
74
  greeting: z.ZodString;
240
75
  }, "strip", z.ZodTypeAny, {
241
- personalizedSummary: string;
242
76
  headline: string;
77
+ personalizedSummary: string;
243
78
  suggestedAction: string;
244
79
  greeting: string;
245
80
  narratorRemark: string;
246
81
  mutualIntentsLabel: string;
247
82
  }, {
248
- personalizedSummary: string;
249
83
  headline: string;
84
+ personalizedSummary: string;
250
85
  suggestedAction: string;
251
86
  greeting: string;
252
87
  narratorRemark: string;
253
88
  mutualIntentsLabel: string;
254
89
  }>;
255
90
  /** LLM-generated result from presentCard (callers append button labels from opportunity.constants). */
256
- export type CardLLMResult = z.infer<typeof CardLLMSchema> & {
257
- /**
258
- * True when the LLM call failed and this is fallback-shaped copy built from
259
- * raw match reasoning. Callers with strict quality requirements (digests,
260
- * long-lived caches) should check this before sending/persisting — fallback
261
- * output is otherwise indistinguishable from genuine LLM output.
262
- */
263
- isFallback?: boolean;
264
- };
91
+ export type CardLLMResult = z.infer<typeof CardLLMSchema>;
265
92
  /** Full card display contract including hardcoded button labels (assembled by callers). */
266
93
  export type CardPresentationResult = CardLLMResult & {
267
94
  primaryActionLabel: string;
@@ -286,6 +113,11 @@ export declare class OpportunityPresenter {
286
113
  private invokeWithTimeout;
287
114
  /**
288
115
  * Generate personalized presentation for a single opportunity.
116
+ *
117
+ * @param input - Pre-assembled presenter context.
118
+ * @param options - Optional abort signal.
119
+ * @returns The LLM-generated presentation.
120
+ * @throws When the LLM call fails, times out, or returns invalid output.
289
121
  */
290
122
  present(input: PresenterInput, options?: {
291
123
  signal?: AbortSignal;
@@ -297,6 +129,10 @@ export declare class OpportunityPresenter {
297
129
  * When `negotiationContext.status === 'negotiating'`, returns a templated
298
130
  * chip synchronously without invoking the LLM — the card just reflects
299
131
  * "negotiation in progress" at that point.
132
+ *
133
+ * @param input - Pre-assembled card presenter context.
134
+ * @returns The LLM-generated card copy.
135
+ * @throws When the LLM call fails, times out, or returns invalid output.
300
136
  */
301
137
  presentCard(input: CardPresenterInput): Promise<CardLLMResult>;
302
138
  /**
@@ -323,7 +159,7 @@ export declare function summarizeSignalsForPresenter(signals: Opportunity['inter
323
159
  * Gather all context needed for the presenter from the database.
324
160
  * Fetches viewer profile, viewer intents, other party profile(s), and network in parallel.
325
161
  *
326
- * @param displayCounterpartUserId - When set (e.g. for a radar card), only this counterpart is included in otherPartyContext so the presenter writes about the person on the card.
162
+ * @param displayCounterpartUserId - When set (e.g. for an opportunity card), only this counterpart is included in otherPartyContext so the presenter writes about the person on the card.
327
163
  * @param focusedViewerIntentId - When set, include only that active intent in viewer context.
328
164
  */
329
165
  export declare function gatherPresenterContext(database: PresenterDatabase, opportunity: Opportunity, viewerId: string, displayCounterpartUserId?: string, focusedViewerIntentId?: string): Promise<PresenterInput>;