@startsimpli/integrations 0.1.3 → 0.1.5

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.
@@ -0,0 +1,321 @@
1
+ /**
2
+ * Ask & Generate over the corpus (bd startsim-768w.38.7).
3
+ *
4
+ * This is the whole pipeline, server-side, in one function:
5
+ *
6
+ * 1. pull CANDIDATES from the tenant data API, already org-scoped by the
7
+ * server and narrowed by the same filters the Library uses;
8
+ * 2. run the SCOPE GUARD over them — a third check, immediately before the
9
+ * prompt is built (see ./retrieval for why);
10
+ * 3. RANK them, keyword + recency;
11
+ * 4. ONE LLM call carrying all N documents with ids the model must echo
12
+ * (feedback_batch_llm_calls — never a loop of N);
13
+ * 5. RESOLVE every citation back to a record retrieval actually supplied,
14
+ * drop the ones that do not resolve, and report that it happened;
15
+ * 6. refuse to present an UNCITED answer as an answer.
16
+ *
17
+ * STEP 5 AND 6 ARE THE POINT. An answer the user cannot trace is an answer they
18
+ * cannot trust with a contract. A model that cites a document id it was never
19
+ * given has invented a source, and the only safe rendering of that is to say so.
20
+ */
21
+ import { moneyEventAsDocument } from './entities';
22
+ import { retrieveForQuestion } from './retrieval';
23
+ import type {
24
+ AnswerRequest,
25
+ AskRequest,
26
+ AskResult,
27
+ Citation,
28
+ CorpusApi,
29
+ CorpusLlm,
30
+ PromptDocument,
31
+ RetrievedDocument,
32
+ SourceDocument,
33
+ SourceFreshness,
34
+ } from './types';
35
+
36
+ export interface AskCorpusOptions {
37
+ /** How many candidate records to pull from the API before ranking. */
38
+ candidateLimit?: number;
39
+ /** How many ranked records the model is allowed to see. */
40
+ contextLimit?: number;
41
+ /** Characters of each document's text included in the prompt. */
42
+ maxCharsPerDocument?: number;
43
+ /** Injectable clock for the recency prior. */
44
+ now?: number;
45
+ }
46
+
47
+ const DEFAULTS: Required<Omit<AskCorpusOptions, 'now'>> = {
48
+ candidateLimit: 200,
49
+ contextLimit: 8,
50
+ maxCharsPerDocument: 6000,
51
+ };
52
+
53
+ /**
54
+ * The system prompt. It is short on purpose: every rule in it is enforced in
55
+ * code below as well, because a prompt is a request and a check is a guarantee.
56
+ */
57
+ function systemPrompt(mode: AskRequest['mode']): string {
58
+ const shared = [
59
+ 'You answer only from the DOCUMENTS supplied in this message.',
60
+ 'Every claim you make must be supported by at least one document, and you must cite it by its exact id.',
61
+ 'Never cite an id that is not in the list. If the documents do not support an answer, set "unanswerable": true and say what is missing.',
62
+ 'Respond with JSON only: {"answer": string, "citations": [{"documentId": string, "quote": string}], "unanswerable": boolean}.',
63
+ 'Each "quote" must be a short verbatim span copied from the cited document.',
64
+ ];
65
+ if (mode === 'generate') {
66
+ return [
67
+ 'You draft business documents for a company using only that company\'s own records.',
68
+ ...shared,
69
+ 'Put the finished draft in "answer", ready to send or sign. Do not add commentary around it.',
70
+ 'Do not invent names, dates, amounts or terms that are not in the documents. Leave an explicit [TO CONFIRM: ...] placeholder instead.',
71
+ ].join('\n');
72
+ }
73
+ return [
74
+ 'You answer questions about a company using only that company\'s own records.',
75
+ ...shared,
76
+ 'Be specific and short. Prefer naming the record and the date over hedging.',
77
+ ].join('\n');
78
+ }
79
+
80
+ /** ONE payload carrying every retrieved document, each tagged with the id to echo. */
81
+ export function buildAnswerRequest(
82
+ request: AskRequest,
83
+ retrieved: RetrievedDocument[],
84
+ maxChars: number,
85
+ ): AnswerRequest {
86
+ const documents: PromptDocument[] = retrieved.map(({ document }) => ({
87
+ id: document.id,
88
+ title: document.title,
89
+ // The source travels INTO the prompt because "we learned this from a
90
+ // meeting" and "we learned this from a contract in Drive" are different
91
+ // claims, and the model should be able to say which it used.
92
+ source: document.sourceLabel || document.source,
93
+ occurredAt: document.occurredAt,
94
+ participants: document.participants.map((party) => party.name),
95
+ text: documentText(document, maxChars),
96
+ }));
97
+ return {
98
+ question: request.question,
99
+ mode: request.mode,
100
+ instructions: request.instructions,
101
+ documents,
102
+ };
103
+ }
104
+
105
+ /**
106
+ * A document's text for the prompt: body first, then each named section, cut at
107
+ * a budget. Sections are labelled with the source's own words, so the model sees
108
+ * "Transcript" or "Extracted text" without this package knowing what either is.
109
+ */
110
+ export function documentText(document: SourceDocument, maxChars: number): string {
111
+ const parts = [document.body, ...document.sections.map((s) => `## ${s.label}\n${s.text}`)];
112
+ const joined = parts.filter(Boolean).join('\n\n');
113
+ return joined.length > maxChars ? `${joined.slice(0, maxChars)}\n…[truncated]` : joined;
114
+ }
115
+
116
+ /** Render the batched request as the single user message. */
117
+ export function renderAnswerPrompt(input: AnswerRequest): string {
118
+ const blocks = input.documents.map((document) =>
119
+ [
120
+ `<document id="${document.id}">`,
121
+ `title: ${document.title}`,
122
+ `source: ${document.source}`,
123
+ `occurred_at: ${document.occurredAt ?? 'unknown'}`,
124
+ document.participants.length ? `participants: ${document.participants.join(', ')}` : '',
125
+ '',
126
+ document.text,
127
+ '</document>',
128
+ ]
129
+ .filter((line) => line !== '')
130
+ .join('\n'),
131
+ );
132
+ const task =
133
+ input.mode === 'generate'
134
+ ? `DRAFT REQUEST:\n${input.question}`
135
+ : `QUESTION:\n${input.question}`;
136
+ return [
137
+ `DOCUMENTS (${input.documents.length}):`,
138
+ blocks.join('\n\n'),
139
+ '',
140
+ task,
141
+ input.instructions ? `\nADDITIONAL INSTRUCTIONS:\n${input.instructions}` : '',
142
+ ]
143
+ .filter(Boolean)
144
+ .join('\n');
145
+ }
146
+
147
+ export { systemPrompt as answerSystemPrompt };
148
+
149
+ /**
150
+ * Resolve the model's citations against what retrieval actually supplied.
151
+ *
152
+ * An id the model invented resolves to nothing and lands in `unresolved`. It is
153
+ * NOT quietly discarded: `AskResult.unresolvedCitations` carries it to the
154
+ * screen, because a model inventing sources is a fact about the answer.
155
+ */
156
+ export function resolveCitations(
157
+ draft: { citations: Array<{ documentId: string; quote?: string }> },
158
+ retrieved: RetrievedDocument[],
159
+ ): { citations: Citation[]; unresolved: string[] } {
160
+ const byId = new Map(retrieved.map((item) => [item.document.id, item.document]));
161
+ const citations: Citation[] = [];
162
+ const unresolved: string[] = [];
163
+ const seen = new Set<string>();
164
+
165
+ for (const raw of draft.citations ?? []) {
166
+ // Both spellings. A model asked for `documentId` returns `document_id` often
167
+ // enough, and the cost of being strict here is EVERY citation silently
168
+ // dropped — which the surface would then correctly, uselessly, report as an
169
+ // uncited answer. The tolerance belongs at the seam, not only in the one
170
+ // adapter that happens to parse the JSON.
171
+ const record = (raw ?? {}) as { documentId?: unknown; document_id?: unknown; quote?: unknown };
172
+ const id = String(record.documentId ?? record.document_id ?? '').trim();
173
+ if (!id) continue;
174
+ const document = byId.get(id);
175
+ if (!document) {
176
+ if (!unresolved.includes(id)) unresolved.push(id);
177
+ continue;
178
+ }
179
+ if (seen.has(id)) continue;
180
+ seen.add(id);
181
+ citations.push({
182
+ documentId: id,
183
+ document,
184
+ quote: String(record.quote ?? '').trim() || snippetOf(document),
185
+ });
186
+ }
187
+ return { citations, unresolved };
188
+ }
189
+
190
+ function snippetOf(document: SourceDocument): string {
191
+ const text = document.body || document.sections[0]?.text || '';
192
+ return text.length > 200 ? `${text.slice(0, 200).trim()}…` : text;
193
+ }
194
+
195
+ /**
196
+ * Pull the candidate set. Two reads, in PARALLEL, not one read per document —
197
+ * the server pages and filters, this only decides how wide a net to cast.
198
+ *
199
+ * Money events are folded in as documents so one ranking pass spans meetings and
200
+ * revenue; "what did we agree with this company" is a question about both.
201
+ */
202
+ async function loadCandidates(
203
+ corpus: CorpusApi,
204
+ request: AskRequest,
205
+ candidateLimit: number,
206
+ ): Promise<SourceDocument[]> {
207
+ const documentQuery = {
208
+ search: request.question,
209
+ ...request.filters,
210
+ page: 1,
211
+ pageSize: candidateLimit,
212
+ };
213
+
214
+ const [documents, money] = await Promise.all([
215
+ corpus.listDocuments(documentQuery),
216
+ corpus.listMoneyEvents
217
+ ? corpus
218
+ .listMoneyEvents({
219
+ search: request.question,
220
+ partyId: request.filters?.partyId,
221
+ page: 1,
222
+ pageSize: Math.ceil(candidateLimit / 4),
223
+ })
224
+ // A tenant with no payment connector, or a money endpoint that is down,
225
+ // must not take the whole answer with it — the meetings still answer.
226
+ .catch(() => ({ count: 0, next: null, previous: null, results: [] }))
227
+ : Promise.resolve({ count: 0, next: null, previous: null, results: [] }),
228
+ ]);
229
+
230
+ return [...documents.results, ...money.results.map(moneyEventAsDocument)];
231
+ }
232
+
233
+ /**
234
+ * Ask (or generate) over the corpus.
235
+ *
236
+ * Runs SERVER-SIDE — it holds a `CorpusLlm`, which holds a provider key. The
237
+ * browser composer talks to a route that calls this, never to a provider.
238
+ */
239
+ export async function askCorpus(
240
+ request: AskRequest,
241
+ deps: { corpus: CorpusApi; llm: CorpusLlm },
242
+ options: AskCorpusOptions = {},
243
+ ): Promise<AskResult> {
244
+ const { candidateLimit, contextLimit, maxCharsPerDocument } = { ...DEFAULTS, ...options };
245
+ const { corpus, llm } = deps;
246
+
247
+ // An empty scope grants nothing (see `isInScope`). Refusing here rather than
248
+ // relying on the guard downstream keeps a missing claim from ever reaching a
249
+ // prompt builder in the first place.
250
+ if (!request.scope?.orgId) {
251
+ return emptyResult(request, llm, 'This session has no tenant scope, so there is nothing to search.');
252
+ }
253
+
254
+ const candidates = await loadCandidates(corpus, request, candidateLimit);
255
+ const { retrieved, droppedOutOfScope } = retrieveForQuestion(
256
+ candidates,
257
+ request.question,
258
+ request.scope,
259
+ { limit: contextLimit, now: options.now },
260
+ );
261
+
262
+ const freshness = await corpus.listSources().catch(() => [] as SourceFreshness[]);
263
+
264
+ if (retrieved.length === 0) {
265
+ return {
266
+ status: 'no_evidence',
267
+ answer:
268
+ 'Nothing in your corpus matches that. Either it has not been pulled in yet, or it belongs to a different workspace.',
269
+ citations: [],
270
+ considered: [],
271
+ unresolvedCitations: [],
272
+ droppedOutOfScope,
273
+ provider: llm.describe(),
274
+ freshness,
275
+ };
276
+ }
277
+
278
+ const draft = await llm.answer(
279
+ buildAnswerRequest(request, retrieved, maxCharsPerDocument),
280
+ );
281
+ const { citations, unresolved } = resolveCitations(draft, retrieved);
282
+
283
+ const answer = String(draft.answer ?? '').trim();
284
+ const status = deriveStatus(draft.unanswerable === true, answer, citations.length);
285
+
286
+ return {
287
+ status,
288
+ answer,
289
+ citations,
290
+ considered: retrieved,
291
+ unresolvedCitations: unresolved,
292
+ droppedOutOfScope,
293
+ provider: llm.describe(),
294
+ freshness,
295
+ };
296
+ }
297
+
298
+ /**
299
+ * `uncited` is a real outcome, not a failure to report. The model produced text
300
+ * and nothing traceable; the surface renders that as a warning over the text
301
+ * rather than as an answer, which is the difference between a demo that impresses
302
+ * on Monday and one that survives Tuesday.
303
+ */
304
+ function deriveStatus(unanswerable: boolean, answer: string, citationCount: number): AskResult['status'] {
305
+ if (unanswerable && citationCount === 0) return 'no_evidence';
306
+ if (!answer) return 'no_evidence';
307
+ return citationCount > 0 ? 'answered' : 'uncited';
308
+ }
309
+
310
+ function emptyResult(request: AskRequest, llm: CorpusLlm, answer: string): AskResult {
311
+ void request;
312
+ return {
313
+ status: 'no_evidence',
314
+ answer,
315
+ citations: [],
316
+ considered: [],
317
+ unresolvedCitations: [],
318
+ droppedOutOfScope: 0,
319
+ provider: llm.describe(),
320
+ };
321
+ }
@@ -0,0 +1,196 @@
1
+ /**
2
+ * A `CorpusApi` over the tenant data plane's generic `/api/v1/*` endpoints.
3
+ *
4
+ * This is the ONLY place that knows the corpus is stored as generic `Entity`
5
+ * rows behind `?type=`. Swap this adapter and every other file in the package —
6
+ * both composers, retrieval, the ask pipeline, all the tests — is unaffected.
7
+ *
8
+ * SERVER-SIDE FILTERING AND PAGING, ALWAYS (CLAUDE.md rule 8). Every filter goes
9
+ * out as a query parameter and the DRF page envelope comes back untouched. There
10
+ * is no client-side `.filter()` on a page of results anywhere in this file, and
11
+ * there must not be: it produces a list whose count disagrees with its rows and
12
+ * that stops working at exactly the corpus size where it starts to matter.
13
+ *
14
+ * WHAT THE BACKEND HONOURS TODAY: `tenant-starter`'s `EntityViewSet` filters on
15
+ * `?type=` and nothing else — no `search`, no `source`, no date window (see
16
+ * `apps/api/views.py`). So `supportedFilters` defaults to `[]` and the composer
17
+ * says so on screen rather than pretending. Pass the real list once the tenant
18
+ * API gains them; the request already sends every parameter.
19
+ */
20
+ import { toMoneyEvent, toParty, toSourceDocument, DEFAULT_TYPE_KEYS } from './entities';
21
+ import type { CorpusTypeKeys, TenantEntity } from './entities';
22
+ import type {
23
+ CorpusApi,
24
+ CorpusPage,
25
+ CorpusScope,
26
+ DocumentQuery,
27
+ MoneyEvent,
28
+ Party,
29
+ SourceDocument,
30
+ SourceFreshness,
31
+ } from './types';
32
+
33
+ /** The `@startsimpli/api` client surface this adapter needs, and no more. */
34
+ export interface CorpusHttp {
35
+ get<T>(path: string, options?: { params?: Record<string, unknown> }): Promise<T>;
36
+ }
37
+
38
+ export interface CreateCorpusApiOptions {
39
+ http: CorpusHttp;
40
+ /** The asking principal's scope, used as the fallback when a row omits its own. */
41
+ scope: CorpusScope;
42
+ /** Override when a tenant modelled its types under different keys. */
43
+ typeKeys?: Partial<CorpusTypeKeys>;
44
+ /** Human names per source key, from the connector catalog. Data, not a mapping table. */
45
+ sourceLabels?: Record<string, string>;
46
+ /**
47
+ * Which `DocumentQuery` keys this deployment's API really applies. Anything
48
+ * not listed is reported to the user as unfiltered rather than assumed.
49
+ */
50
+ supportedFilters?: Array<keyof DocumentQuery>;
51
+ /** Endpoint bases, without leading/trailing slashes (the client convention). */
52
+ paths?: { entities?: string; connections?: string };
53
+ /** How old a source's last sync may be before the Library calls it stale. */
54
+ staleAfterHours?: number;
55
+ }
56
+
57
+ interface DrfPage<T> {
58
+ count: number;
59
+ next: string | null;
60
+ previous: string | null;
61
+ results: T[];
62
+ }
63
+
64
+ const EMPTY_PAGE = { count: 0, next: null, previous: null, results: [] };
65
+
66
+ function params(query: DocumentQuery, type: string) {
67
+ const out: Record<string, unknown> = { type, page: query.page ?? 1 };
68
+ if (query.pageSize) out.page_size = query.pageSize;
69
+ if (query.search) out.search = query.search;
70
+ if (query.source) out.source = query.source;
71
+ if (query.partyId) out.party = query.partyId;
72
+ if (query.occurredAfter) out.occurred_after = query.occurredAfter;
73
+ if (query.occurredBefore) out.occurred_before = query.occurredBefore;
74
+ return out;
75
+ }
76
+
77
+ /** Which of the filters the caller asked for are actually honoured server-side. */
78
+ function appliedFilters(
79
+ query: DocumentQuery,
80
+ supported: Array<keyof DocumentQuery>,
81
+ ): string[] {
82
+ const requested = (['search', 'source', 'partyId', 'occurredAfter', 'occurredBefore'] as const).filter(
83
+ (key) => Boolean(query[key]),
84
+ );
85
+ return requested.filter((key) => supported.includes(key));
86
+ }
87
+
88
+ export function createCorpusApi(options: CreateCorpusApiOptions): CorpusApi {
89
+ const {
90
+ http,
91
+ scope,
92
+ sourceLabels = {},
93
+ supportedFilters = [],
94
+ staleAfterHours = 26,
95
+ } = options;
96
+ const types: CorpusTypeKeys = { ...DEFAULT_TYPE_KEYS, ...options.typeKeys };
97
+ const entities = options.paths?.entities ?? 'api/v1/entities';
98
+ const connections = options.paths?.connections ?? 'api/v1/connections';
99
+
100
+ async function page<T>(
101
+ type: string,
102
+ query: DocumentQuery,
103
+ map: (entity: TenantEntity) => T,
104
+ ): Promise<CorpusPage<T>> {
105
+ const response = await http.get<DrfPage<TenantEntity>>(entities, {
106
+ params: params(query, type),
107
+ });
108
+ return {
109
+ count: response.count ?? response.results?.length ?? 0,
110
+ next: response.next ?? null,
111
+ previous: response.previous ?? null,
112
+ results: (response.results ?? []).map(map),
113
+ appliedFilters: appliedFilters(query, supportedFilters),
114
+ };
115
+ }
116
+
117
+ return {
118
+ listDocuments(query) {
119
+ return page<SourceDocument>(types.document, query, (entity) =>
120
+ toSourceDocument(entity, scope, sourceLabels),
121
+ );
122
+ },
123
+
124
+ async getDocument(id) {
125
+ const entity = await http.get<TenantEntity>(`${entities}/${encodeURIComponent(id)}`);
126
+ return toSourceDocument(entity, scope, sourceLabels);
127
+ },
128
+
129
+ listParties(query = {}) {
130
+ return page<Party>(types.party, query, (entity) => toParty(entity, scope));
131
+ },
132
+
133
+ listMoneyEvents(query = {}) {
134
+ return page<MoneyEvent>(types.moneyEvent, query, (entity) =>
135
+ toMoneyEvent(entity, scope, sourceLabels),
136
+ );
137
+ },
138
+
139
+ /**
140
+ * Freshness per SOURCE, assembled from the connections the tenant has.
141
+ *
142
+ * It reads the connections API rather than the documents, because "Dropbox
143
+ * has not synced since Tuesday" is a true and useful statement even when
144
+ * Dropbox has contributed zero documents — and a count derived from the
145
+ * corpus alone can never say it.
146
+ */
147
+ async listSources(): Promise<SourceFreshness[]> {
148
+ const [connectionPage, documentPage] = await Promise.all([
149
+ http
150
+ .get<DrfPage<Record<string, unknown>>>(connections)
151
+ .catch(() => EMPTY_PAGE as DrfPage<Record<string, unknown>>),
152
+ http
153
+ .get<DrfPage<TenantEntity>>(entities, { params: { type: types.document, page_size: 1 } })
154
+ .catch(() => EMPTY_PAGE as DrfPage<TenantEntity>),
155
+ ]);
156
+
157
+ void documentPage; // total count is per-source below; kept for the empty-corpus case
158
+
159
+ const now = Date.now();
160
+ return (connectionPage.results ?? []).map((connection) => {
161
+ const key = String(connection.connector_key ?? connection.connectorKey ?? 'unknown');
162
+ const lastSyncedAt =
163
+ (connection.last_synced_at as string) ??
164
+ (connection.lastSyncedAt as string) ??
165
+ (connection.last_checked_at as string) ??
166
+ (connection.lastCheckedAt as string) ??
167
+ null;
168
+ const documentCount = Number(connection.document_count ?? connection.documentCount ?? 0);
169
+ const error = String(connection.check_error ?? connection.checkError ?? '');
170
+ const status = connection.status ?? '';
171
+
172
+ let health: SourceFreshness['status'] = 'ok';
173
+ let detail = '';
174
+ if (error || status === 'error' || status === 'needs_reauth') {
175
+ health = 'error';
176
+ detail = error || `Connection status is ${String(status)}.`;
177
+ } else if (!lastSyncedAt) {
178
+ health = 'never';
179
+ detail = 'Connected, but nothing has arrived yet.';
180
+ } else if (now - Date.parse(lastSyncedAt) > staleAfterHours * 3_600_000) {
181
+ health = 'stale';
182
+ detail = `Last sync was more than ${staleAfterHours} hours ago.`;
183
+ }
184
+
185
+ return {
186
+ source: key,
187
+ label: String(connection.connector_name ?? connection.connectorName ?? sourceLabels[key] ?? key),
188
+ documentCount,
189
+ lastSyncedAt,
190
+ status: health,
191
+ ...(detail ? { detail } : {}),
192
+ };
193
+ });
194
+ },
195
+ };
196
+ }