@deepcitation/deepcitation-js 1.0.6 → 1.0.7
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.
- package/lib/client/DeepCitation.d.ts +0 -20
- package/lib/client/DeepCitation.js +19 -59
- package/package.json +1 -1
|
@@ -28,13 +28,6 @@ import type { CitationInput, ConvertFileInput, ConvertFileResponse, DeepCitation
|
|
|
28
28
|
export declare class DeepCitation {
|
|
29
29
|
private readonly apiKey;
|
|
30
30
|
private readonly apiUrl;
|
|
31
|
-
/**
|
|
32
|
-
* Stores mapping of user-provided fileId to internal attachmentId
|
|
33
|
-
* This allows users to reference files by their own IDs
|
|
34
|
-
*/
|
|
35
|
-
private fileIdMap;
|
|
36
|
-
/** Store file mapping and return public response */
|
|
37
|
-
private storeAndReturnResponse;
|
|
38
31
|
/**
|
|
39
32
|
* Create a new DeepCitation client instance.
|
|
40
33
|
*
|
|
@@ -190,17 +183,4 @@ export declare class DeepCitation {
|
|
|
190
183
|
verifyCitationsFromLlmOutput(input: VerifyCitationsFromLlmOutput, citations?: {
|
|
191
184
|
[key: string]: Citation;
|
|
192
185
|
}): Promise<VerifyCitationsResponse>;
|
|
193
|
-
/**
|
|
194
|
-
* Register a file that was uploaded separately (e.g., via direct API call).
|
|
195
|
-
* This allows you to use verifyCitations with files not uploaded via uploadFile().
|
|
196
|
-
*
|
|
197
|
-
* @param fileId - Your file ID
|
|
198
|
-
* @param attachmentId - The internal attachment ID
|
|
199
|
-
*/
|
|
200
|
-
registerFile(fileId: string, attachmentId: string): void;
|
|
201
|
-
/**
|
|
202
|
-
* Clear the internal file ID mapping.
|
|
203
|
-
* Useful for cleanup or when working with many files.
|
|
204
|
-
*/
|
|
205
|
-
clearFileMap(): void;
|
|
206
186
|
}
|
|
@@ -18,7 +18,8 @@ function toBlob(file, filename) {
|
|
|
18
18
|
/** Extract error message from API response */
|
|
19
19
|
async function extractErrorMessage(response, fallbackAction) {
|
|
20
20
|
const error = await response.json().catch(() => ({}));
|
|
21
|
-
return error?.error?.message ||
|
|
21
|
+
return (error?.error?.message ||
|
|
22
|
+
`${fallbackAction} failed with status ${response.status}`);
|
|
22
23
|
}
|
|
23
24
|
/**
|
|
24
25
|
* DeepCitation client for file upload and citation verification.
|
|
@@ -48,17 +49,6 @@ async function extractErrorMessage(response, fallbackAction) {
|
|
|
48
49
|
export class DeepCitation {
|
|
49
50
|
apiKey;
|
|
50
51
|
apiUrl;
|
|
51
|
-
/**
|
|
52
|
-
* Stores mapping of user-provided fileId to internal attachmentId
|
|
53
|
-
* This allows users to reference files by their own IDs
|
|
54
|
-
*/
|
|
55
|
-
fileIdMap = new Map();
|
|
56
|
-
/** Store file mapping and return public response */
|
|
57
|
-
storeAndReturnResponse(apiResponse) {
|
|
58
|
-
this.fileIdMap.set(apiResponse.fileId, { attachmentId: apiResponse.attachmentId });
|
|
59
|
-
const { attachmentId: _, ...publicResponse } = apiResponse;
|
|
60
|
-
return publicResponse;
|
|
61
|
-
}
|
|
62
52
|
/**
|
|
63
53
|
* Create a new DeepCitation client instance.
|
|
64
54
|
*
|
|
@@ -111,7 +101,7 @@ export class DeepCitation {
|
|
|
111
101
|
if (!response.ok) {
|
|
112
102
|
throw new Error(await extractErrorMessage(response, "Upload"));
|
|
113
103
|
}
|
|
114
|
-
return
|
|
104
|
+
return (await response.json());
|
|
115
105
|
}
|
|
116
106
|
/**
|
|
117
107
|
* Convert a URL or Office file to PDF for citation verification.
|
|
@@ -179,10 +169,7 @@ export class DeepCitation {
|
|
|
179
169
|
if (!response.ok) {
|
|
180
170
|
throw new Error(await extractErrorMessage(response, "Conversion"));
|
|
181
171
|
}
|
|
182
|
-
|
|
183
|
-
this.fileIdMap.set(apiResponse.fileId, { attachmentId: apiResponse.attachmentId });
|
|
184
|
-
const { attachmentId: _, ...publicResponse } = apiResponse;
|
|
185
|
-
return publicResponse;
|
|
172
|
+
return (await response.json());
|
|
186
173
|
}
|
|
187
174
|
/**
|
|
188
175
|
* Prepare a previously converted file for citation verification.
|
|
@@ -205,10 +192,6 @@ export class DeepCitation {
|
|
|
205
192
|
* ```
|
|
206
193
|
*/
|
|
207
194
|
async prepareConvertedFile(options) {
|
|
208
|
-
const fileInfo = this.fileIdMap.get(options.fileId);
|
|
209
|
-
if (!fileInfo) {
|
|
210
|
-
throw new Error(`File ID "${options.fileId}" not found. Make sure to call convertToPdf() first.`);
|
|
211
|
-
}
|
|
212
195
|
const response = await fetch(`${this.apiUrl}/prepareFile`, {
|
|
213
196
|
method: "POST",
|
|
214
197
|
headers: {
|
|
@@ -216,14 +199,13 @@ export class DeepCitation {
|
|
|
216
199
|
"Content-Type": "application/json",
|
|
217
200
|
},
|
|
218
201
|
body: JSON.stringify({
|
|
219
|
-
attachmentId: fileInfo.attachmentId,
|
|
220
202
|
fileId: options.fileId,
|
|
221
203
|
}),
|
|
222
204
|
});
|
|
223
205
|
if (!response.ok) {
|
|
224
206
|
throw new Error(await extractErrorMessage(response, "Prepare"));
|
|
225
207
|
}
|
|
226
|
-
return
|
|
208
|
+
return (await response.json());
|
|
227
209
|
}
|
|
228
210
|
/**
|
|
229
211
|
* Upload multiple files for citation verification and get structured content.
|
|
@@ -286,11 +268,6 @@ export class DeepCitation {
|
|
|
286
268
|
* ```
|
|
287
269
|
*/
|
|
288
270
|
async verifyCitations(fileId, citations, options) {
|
|
289
|
-
// Look up the internal IDs from our map
|
|
290
|
-
const fileInfo = this.fileIdMap.get(fileId);
|
|
291
|
-
if (!fileInfo) {
|
|
292
|
-
throw new Error(`File ID "${fileId}" not found. Make sure to upload the file first with uploadFile().`);
|
|
293
|
-
}
|
|
294
271
|
// Normalize citations to a map with citation keys
|
|
295
272
|
const citationMap = {};
|
|
296
273
|
if (Array.isArray(citations)) {
|
|
@@ -315,24 +292,27 @@ export class DeepCitation {
|
|
|
315
292
|
else {
|
|
316
293
|
throw new Error("Invalid citations format");
|
|
317
294
|
}
|
|
318
|
-
const
|
|
295
|
+
const requestUrl = `${this.apiUrl}/verifyCitations`;
|
|
296
|
+
const requestBody = {
|
|
297
|
+
data: {
|
|
298
|
+
fileId,
|
|
299
|
+
citations: citationMap,
|
|
300
|
+
outputImageFormat: options?.outputImageFormat || "avif",
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
const response = await fetch(requestUrl, {
|
|
319
304
|
method: "POST",
|
|
320
305
|
headers: {
|
|
321
306
|
Authorization: `Bearer ${this.apiKey}`,
|
|
322
307
|
"Content-Type": "application/json",
|
|
323
308
|
},
|
|
324
|
-
body: JSON.stringify(
|
|
325
|
-
data: {
|
|
326
|
-
attachmentId: fileInfo.attachmentId,
|
|
327
|
-
citations: citationMap,
|
|
328
|
-
outputImageFormat: options?.outputImageFormat || "avif",
|
|
329
|
-
},
|
|
330
|
-
}),
|
|
309
|
+
body: JSON.stringify(requestBody),
|
|
331
310
|
});
|
|
332
311
|
if (!response.ok) {
|
|
333
312
|
throw new Error(await extractErrorMessage(response, "Verification"));
|
|
334
313
|
}
|
|
335
|
-
|
|
314
|
+
const result = (await response.json());
|
|
315
|
+
return result;
|
|
336
316
|
}
|
|
337
317
|
/**
|
|
338
318
|
* Verify citations from LLM output with automatic parsing.
|
|
@@ -362,9 +342,6 @@ export class DeepCitation {
|
|
|
362
342
|
if (Object.keys(citations).length === 0) {
|
|
363
343
|
return { foundHighlights: {} };
|
|
364
344
|
}
|
|
365
|
-
// Note: fileDataParts is now only used to identify which files to verify
|
|
366
|
-
// The mapping from fileId to attachmentId must be registered via uploadFile() or prepareFiles()
|
|
367
|
-
// in the same session. For Zero Data Retention scenarios, use verifyCitations() directly.
|
|
368
345
|
// Group citations by fileId
|
|
369
346
|
const citationsByFile = new Map();
|
|
370
347
|
for (const [key, citation] of Object.entries(citations)) {
|
|
@@ -374,10 +351,10 @@ export class DeepCitation {
|
|
|
374
351
|
}
|
|
375
352
|
citationsByFile.get(fileId)[key] = citation;
|
|
376
353
|
}
|
|
377
|
-
//
|
|
354
|
+
// Verify all files in parallel
|
|
378
355
|
const verificationPromises = [];
|
|
379
356
|
for (const [fileId, fileCitations] of citationsByFile) {
|
|
380
|
-
if (
|
|
357
|
+
if (fileId) {
|
|
381
358
|
verificationPromises.push(this.verifyCitations(fileId, fileCitations, { outputImageFormat }));
|
|
382
359
|
}
|
|
383
360
|
}
|
|
@@ -388,21 +365,4 @@ export class DeepCitation {
|
|
|
388
365
|
}
|
|
389
366
|
return { foundHighlights: allHighlights };
|
|
390
367
|
}
|
|
391
|
-
/**
|
|
392
|
-
* Register a file that was uploaded separately (e.g., via direct API call).
|
|
393
|
-
* This allows you to use verifyCitations with files not uploaded via uploadFile().
|
|
394
|
-
*
|
|
395
|
-
* @param fileId - Your file ID
|
|
396
|
-
* @param attachmentId - The internal attachment ID
|
|
397
|
-
*/
|
|
398
|
-
registerFile(fileId, attachmentId) {
|
|
399
|
-
this.fileIdMap.set(fileId, { attachmentId });
|
|
400
|
-
}
|
|
401
|
-
/**
|
|
402
|
-
* Clear the internal file ID mapping.
|
|
403
|
-
* Useful for cleanup or when working with many files.
|
|
404
|
-
*/
|
|
405
|
-
clearFileMap() {
|
|
406
|
-
this.fileIdMap.clear();
|
|
407
|
-
}
|
|
408
368
|
}
|