@claritylabs/cl-sdk 1.3.7 → 1.3.8

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.
@@ -29,6 +29,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
29
29
  deductible: z.ZodOptional<z.ZodString>;
30
30
  deductibleAmount: z.ZodOptional<z.ZodNumber>;
31
31
  deductibleValueType: z.ZodOptional<z.ZodEnum<["numeric", "included", "not_included", "as_stated", "waiting_period", "referential", "other"]>>;
32
+ trigger: z.ZodOptional<z.ZodEnum<["occurrence", "claims_made", "accident"]>>;
33
+ retroactiveDate: z.ZodOptional<z.ZodString>;
32
34
  formNumber: z.ZodOptional<z.ZodString>;
33
35
  pageNumber: z.ZodOptional<z.ZodNumber>;
34
36
  sectionRef: z.ZodOptional<z.ZodString>;
@@ -46,6 +48,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
46
48
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
47
49
  deductibleAmount?: number | undefined;
48
50
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
51
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
52
+ retroactiveDate?: string | undefined;
49
53
  pageNumber?: number | undefined;
50
54
  sectionRef?: string | undefined;
51
55
  originalContent?: string | undefined;
@@ -62,6 +66,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
62
66
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
63
67
  deductibleAmount?: number | undefined;
64
68
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
69
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
70
+ retroactiveDate?: string | undefined;
65
71
  pageNumber?: number | undefined;
66
72
  sectionRef?: string | undefined;
67
73
  originalContent?: string | undefined;
@@ -359,6 +365,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
359
365
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
360
366
  deductibleAmount?: number | undefined;
361
367
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
368
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
369
+ retroactiveDate?: string | undefined;
362
370
  pageNumber?: number | undefined;
363
371
  sectionRef?: string | undefined;
364
372
  originalContent?: string | undefined;
@@ -373,8 +381,6 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
373
381
  coinsurance?: string | undefined;
374
382
  valuation?: "replacement_cost" | "actual_cash_value" | "agreed_value" | "functional_replacement" | undefined;
375
383
  territory?: string | undefined;
376
- trigger?: "occurrence" | "claims_made" | "accident" | undefined;
377
- retroactiveDate?: string | undefined;
378
384
  premium?: string | undefined;
379
385
  }, {
380
386
  name: string;
@@ -387,6 +393,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
387
393
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
388
394
  deductibleAmount?: number | undefined;
389
395
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
396
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
397
+ retroactiveDate?: string | undefined;
390
398
  pageNumber?: number | undefined;
391
399
  sectionRef?: string | undefined;
392
400
  originalContent?: string | undefined;
@@ -401,8 +409,6 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
401
409
  coinsurance?: string | undefined;
402
410
  valuation?: "replacement_cost" | "actual_cash_value" | "agreed_value" | "functional_replacement" | undefined;
403
411
  territory?: string | undefined;
404
- trigger?: "occurrence" | "claims_made" | "accident" | undefined;
405
- retroactiveDate?: string | undefined;
406
412
  premium?: string | undefined;
407
413
  }>, "many">>;
408
414
  endorsements: z.ZodOptional<z.ZodArray<z.ZodObject<{
@@ -3731,6 +3737,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
3731
3737
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
3732
3738
  deductibleAmount?: number | undefined;
3733
3739
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
3740
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
3741
+ retroactiveDate?: string | undefined;
3734
3742
  pageNumber?: number | undefined;
3735
3743
  sectionRef?: string | undefined;
3736
3744
  originalContent?: string | undefined;
@@ -4454,6 +4462,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
4454
4462
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
4455
4463
  deductibleAmount?: number | undefined;
4456
4464
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
4465
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
4466
+ retroactiveDate?: string | undefined;
4457
4467
  pageNumber?: number | undefined;
4458
4468
  sectionRef?: string | undefined;
4459
4469
  originalContent?: string | undefined;
@@ -4468,8 +4478,6 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
4468
4478
  coinsurance?: string | undefined;
4469
4479
  valuation?: "replacement_cost" | "actual_cash_value" | "agreed_value" | "functional_replacement" | undefined;
4470
4480
  territory?: string | undefined;
4471
- trigger?: "occurrence" | "claims_made" | "accident" | undefined;
4472
- retroactiveDate?: string | undefined;
4473
4481
  premium?: string | undefined;
4474
4482
  }[] | undefined;
4475
4483
  endorsements?: {
@@ -4723,6 +4731,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
4723
4731
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
4724
4732
  deductibleAmount?: number | undefined;
4725
4733
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
4734
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
4735
+ retroactiveDate?: string | undefined;
4726
4736
  pageNumber?: number | undefined;
4727
4737
  sectionRef?: string | undefined;
4728
4738
  originalContent?: string | undefined;
@@ -5446,6 +5456,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
5446
5456
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
5447
5457
  deductibleAmount?: number | undefined;
5448
5458
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
5459
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
5460
+ retroactiveDate?: string | undefined;
5449
5461
  pageNumber?: number | undefined;
5450
5462
  sectionRef?: string | undefined;
5451
5463
  originalContent?: string | undefined;
@@ -5460,8 +5472,6 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
5460
5472
  coinsurance?: string | undefined;
5461
5473
  valuation?: "replacement_cost" | "actual_cash_value" | "agreed_value" | "functional_replacement" | undefined;
5462
5474
  territory?: string | undefined;
5463
- trigger?: "occurrence" | "claims_made" | "accident" | undefined;
5464
- retroactiveDate?: string | undefined;
5465
5475
  premium?: string | undefined;
5466
5476
  }[] | undefined;
5467
5477
  endorsements?: {
@@ -5805,6 +5815,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
5805
5815
  deductible: z.ZodOptional<z.ZodString>;
5806
5816
  deductibleAmount: z.ZodOptional<z.ZodNumber>;
5807
5817
  deductibleValueType: z.ZodOptional<z.ZodEnum<["numeric", "included", "not_included", "as_stated", "waiting_period", "referential", "other"]>>;
5818
+ trigger: z.ZodOptional<z.ZodEnum<["occurrence", "claims_made", "accident"]>>;
5819
+ retroactiveDate: z.ZodOptional<z.ZodString>;
5808
5820
  formNumber: z.ZodOptional<z.ZodString>;
5809
5821
  pageNumber: z.ZodOptional<z.ZodNumber>;
5810
5822
  sectionRef: z.ZodOptional<z.ZodString>;
@@ -5822,6 +5834,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
5822
5834
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
5823
5835
  deductibleAmount?: number | undefined;
5824
5836
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
5837
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
5838
+ retroactiveDate?: string | undefined;
5825
5839
  pageNumber?: number | undefined;
5826
5840
  sectionRef?: string | undefined;
5827
5841
  originalContent?: string | undefined;
@@ -5838,6 +5852,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
5838
5852
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
5839
5853
  deductibleAmount?: number | undefined;
5840
5854
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
5855
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
5856
+ retroactiveDate?: string | undefined;
5841
5857
  pageNumber?: number | undefined;
5842
5858
  sectionRef?: string | undefined;
5843
5859
  originalContent?: string | undefined;
@@ -6135,6 +6151,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
6135
6151
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
6136
6152
  deductibleAmount?: number | undefined;
6137
6153
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
6154
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
6155
+ retroactiveDate?: string | undefined;
6138
6156
  pageNumber?: number | undefined;
6139
6157
  sectionRef?: string | undefined;
6140
6158
  originalContent?: string | undefined;
@@ -6149,8 +6167,6 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
6149
6167
  coinsurance?: string | undefined;
6150
6168
  valuation?: "replacement_cost" | "actual_cash_value" | "agreed_value" | "functional_replacement" | undefined;
6151
6169
  territory?: string | undefined;
6152
- trigger?: "occurrence" | "claims_made" | "accident" | undefined;
6153
- retroactiveDate?: string | undefined;
6154
6170
  premium?: string | undefined;
6155
6171
  }, {
6156
6172
  name: string;
@@ -6163,6 +6179,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
6163
6179
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
6164
6180
  deductibleAmount?: number | undefined;
6165
6181
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
6182
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
6183
+ retroactiveDate?: string | undefined;
6166
6184
  pageNumber?: number | undefined;
6167
6185
  sectionRef?: string | undefined;
6168
6186
  originalContent?: string | undefined;
@@ -6177,8 +6195,6 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
6177
6195
  coinsurance?: string | undefined;
6178
6196
  valuation?: "replacement_cost" | "actual_cash_value" | "agreed_value" | "functional_replacement" | undefined;
6179
6197
  territory?: string | undefined;
6180
- trigger?: "occurrence" | "claims_made" | "accident" | undefined;
6181
- retroactiveDate?: string | undefined;
6182
6198
  premium?: string | undefined;
6183
6199
  }>, "many">>;
6184
6200
  endorsements: z.ZodOptional<z.ZodArray<z.ZodObject<{
@@ -9506,6 +9522,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
9506
9522
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
9507
9523
  deductibleAmount?: number | undefined;
9508
9524
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
9525
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
9526
+ retroactiveDate?: string | undefined;
9509
9527
  pageNumber?: number | undefined;
9510
9528
  sectionRef?: string | undefined;
9511
9529
  originalContent?: string | undefined;
@@ -10225,6 +10243,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
10225
10243
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
10226
10244
  deductibleAmount?: number | undefined;
10227
10245
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
10246
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
10247
+ retroactiveDate?: string | undefined;
10228
10248
  pageNumber?: number | undefined;
10229
10249
  sectionRef?: string | undefined;
10230
10250
  originalContent?: string | undefined;
@@ -10239,8 +10259,6 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
10239
10259
  coinsurance?: string | undefined;
10240
10260
  valuation?: "replacement_cost" | "actual_cash_value" | "agreed_value" | "functional_replacement" | undefined;
10241
10261
  territory?: string | undefined;
10242
- trigger?: "occurrence" | "claims_made" | "accident" | undefined;
10243
- retroactiveDate?: string | undefined;
10244
10262
  premium?: string | undefined;
10245
10263
  }[] | undefined;
10246
10264
  endorsements?: {
@@ -10528,6 +10546,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
10528
10546
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
10529
10547
  deductibleAmount?: number | undefined;
10530
10548
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
10549
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
10550
+ retroactiveDate?: string | undefined;
10531
10551
  pageNumber?: number | undefined;
10532
10552
  sectionRef?: string | undefined;
10533
10553
  originalContent?: string | undefined;
@@ -11247,6 +11267,8 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
11247
11267
  limitValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
11248
11268
  deductibleAmount?: number | undefined;
11249
11269
  deductibleValueType?: "other" | "waiting_period" | "numeric" | "included" | "not_included" | "as_stated" | "referential" | undefined;
11270
+ trigger?: "occurrence" | "claims_made" | "accident" | undefined;
11271
+ retroactiveDate?: string | undefined;
11250
11272
  pageNumber?: number | undefined;
11251
11273
  sectionRef?: string | undefined;
11252
11274
  originalContent?: string | undefined;
@@ -11261,8 +11283,6 @@ declare const InsuranceDocumentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObj
11261
11283
  coinsurance?: string | undefined;
11262
11284
  valuation?: "replacement_cost" | "actual_cash_value" | "agreed_value" | "functional_replacement" | undefined;
11263
11285
  territory?: string | undefined;
11264
- trigger?: "occurrence" | "claims_made" | "accident" | undefined;
11265
- retroactiveDate?: string | undefined;
11266
11286
  premium?: string | undefined;
11267
11287
  }[] | undefined;
11268
11288
  endorsements?: {
@@ -11607,6 +11627,33 @@ declare const SourceSpanSchema: z.ZodObject<{
11607
11627
  pageEnd: z.ZodOptional<z.ZodNumber>;
11608
11628
  sectionId: z.ZodOptional<z.ZodString>;
11609
11629
  formNumber: z.ZodOptional<z.ZodString>;
11630
+ sourceUnit: z.ZodOptional<z.ZodEnum<["page", "section", "table", "table_row", "table_cell", "key_value", "text"]>>;
11631
+ parentSpanId: z.ZodOptional<z.ZodString>;
11632
+ table: z.ZodOptional<z.ZodObject<{
11633
+ tableId: z.ZodOptional<z.ZodString>;
11634
+ rowIndex: z.ZodOptional<z.ZodNumber>;
11635
+ columnIndex: z.ZodOptional<z.ZodNumber>;
11636
+ columnName: z.ZodOptional<z.ZodString>;
11637
+ rowSpanId: z.ZodOptional<z.ZodString>;
11638
+ tableSpanId: z.ZodOptional<z.ZodString>;
11639
+ isHeader: z.ZodOptional<z.ZodBoolean>;
11640
+ }, "strip", z.ZodTypeAny, {
11641
+ tableId?: string | undefined;
11642
+ rowIndex?: number | undefined;
11643
+ columnIndex?: number | undefined;
11644
+ columnName?: string | undefined;
11645
+ rowSpanId?: string | undefined;
11646
+ tableSpanId?: string | undefined;
11647
+ isHeader?: boolean | undefined;
11648
+ }, {
11649
+ tableId?: string | undefined;
11650
+ rowIndex?: number | undefined;
11651
+ columnIndex?: number | undefined;
11652
+ columnName?: string | undefined;
11653
+ rowSpanId?: string | undefined;
11654
+ tableSpanId?: string | undefined;
11655
+ isHeader?: boolean | undefined;
11656
+ }>>;
11610
11657
  bbox: z.ZodOptional<z.ZodArray<z.ZodObject<{
11611
11658
  page: z.ZodNumber;
11612
11659
  x: z.ZodNumber;
@@ -11657,9 +11704,9 @@ declare const SourceSpanSchema: z.ZodObject<{
11657
11704
  metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
11658
11705
  }, "strip", z.ZodTypeAny, {
11659
11706
  id: string;
11707
+ text: string;
11660
11708
  documentId: string;
11661
11709
  kind: "pdf_text" | "pdf_image" | "html" | "markdown" | "plain_text" | "structured_field";
11662
- text: string;
11663
11710
  hash: string;
11664
11711
  formNumber?: string | undefined;
11665
11712
  pageStart?: number | undefined;
@@ -11674,10 +11721,21 @@ declare const SourceSpanSchema: z.ZodObject<{
11674
11721
  lineEnd?: number | undefined;
11675
11722
  fieldPath?: string | undefined;
11676
11723
  } | undefined;
11724
+ table?: {
11725
+ tableId?: string | undefined;
11726
+ rowIndex?: number | undefined;
11727
+ columnIndex?: number | undefined;
11728
+ columnName?: string | undefined;
11729
+ rowSpanId?: string | undefined;
11730
+ tableSpanId?: string | undefined;
11731
+ isHeader?: boolean | undefined;
11732
+ } | undefined;
11677
11733
  sourceKind?: "email" | "policy_pdf" | "application_pdf" | "attachment" | "manual_note" | undefined;
11678
11734
  chunkId?: string | undefined;
11679
11735
  textHash?: string | undefined;
11680
11736
  sectionId?: string | undefined;
11737
+ sourceUnit?: "section" | "page" | "table" | "table_row" | "table_cell" | "key_value" | "text" | undefined;
11738
+ parentSpanId?: string | undefined;
11681
11739
  bbox?: {
11682
11740
  page: number;
11683
11741
  x: number;
@@ -11688,9 +11746,9 @@ declare const SourceSpanSchema: z.ZodObject<{
11688
11746
  metadata?: Record<string, string> | undefined;
11689
11747
  }, {
11690
11748
  id: string;
11749
+ text: string;
11691
11750
  documentId: string;
11692
11751
  kind: "pdf_text" | "pdf_image" | "html" | "markdown" | "plain_text" | "structured_field";
11693
- text: string;
11694
11752
  hash: string;
11695
11753
  formNumber?: string | undefined;
11696
11754
  pageStart?: number | undefined;
@@ -11705,10 +11763,21 @@ declare const SourceSpanSchema: z.ZodObject<{
11705
11763
  lineEnd?: number | undefined;
11706
11764
  fieldPath?: string | undefined;
11707
11765
  } | undefined;
11766
+ table?: {
11767
+ tableId?: string | undefined;
11768
+ rowIndex?: number | undefined;
11769
+ columnIndex?: number | undefined;
11770
+ columnName?: string | undefined;
11771
+ rowSpanId?: string | undefined;
11772
+ tableSpanId?: string | undefined;
11773
+ isHeader?: boolean | undefined;
11774
+ } | undefined;
11708
11775
  sourceKind?: "email" | "policy_pdf" | "application_pdf" | "attachment" | "manual_note" | undefined;
11709
11776
  chunkId?: string | undefined;
11710
11777
  textHash?: string | undefined;
11711
11778
  sectionId?: string | undefined;
11779
+ sourceUnit?: "section" | "page" | "table" | "table_row" | "table_cell" | "key_value" | "text" | undefined;
11780
+ parentSpanId?: string | undefined;
11712
11781
  bbox?: {
11713
11782
  page: number;
11714
11783
  x: number;
@@ -11731,8 +11800,8 @@ declare const SourceChunkSchema: z.ZodObject<{
11731
11800
  }, "strip", z.ZodTypeAny, {
11732
11801
  sourceSpanIds: string[];
11733
11802
  id: string;
11734
- documentId: string;
11735
11803
  text: string;
11804
+ documentId: string;
11736
11805
  textHash: string;
11737
11806
  metadata: Record<string, string>;
11738
11807
  pageStart?: number | undefined;
@@ -11740,8 +11809,8 @@ declare const SourceChunkSchema: z.ZodObject<{
11740
11809
  }, {
11741
11810
  sourceSpanIds: string[];
11742
11811
  id: string;
11743
- documentId: string;
11744
11812
  text: string;
11813
+ documentId: string;
11745
11814
  textHash: string;
11746
11815
  pageStart?: number | undefined;
11747
11816
  pageEnd?: number | undefined;
@@ -70,6 +70,9 @@ CREATE TABLE IF NOT EXISTS source_spans (
70
70
  page_end INTEGER,
71
71
  section_id TEXT,
72
72
  form_number TEXT,
73
+ source_unit TEXT,
74
+ parent_span_id TEXT,
75
+ table_location TEXT,
73
76
  location TEXT,
74
77
  bbox TEXT,
75
78
  metadata TEXT NOT NULL DEFAULT '{}',
@@ -290,6 +293,15 @@ var SourceSpanKindSchema = import_zod.z.enum([
290
293
  "plain_text",
291
294
  "structured_field"
292
295
  ]);
296
+ var SourceSpanUnitSchema = import_zod.z.enum([
297
+ "page",
298
+ "section",
299
+ "table",
300
+ "table_row",
301
+ "table_cell",
302
+ "key_value",
303
+ "text"
304
+ ]);
293
305
  var SourceKindSchema = import_zod.z.enum([
294
306
  "policy_pdf",
295
307
  "application_pdf",
@@ -314,6 +326,15 @@ var SourceSpanLocationSchema = import_zod.z.object({
314
326
  lineEnd: import_zod.z.number().int().positive().optional(),
315
327
  fieldPath: import_zod.z.string().optional()
316
328
  });
329
+ var SourceSpanTableLocationSchema = import_zod.z.object({
330
+ tableId: import_zod.z.string().optional(),
331
+ rowIndex: import_zod.z.number().int().nonnegative().optional(),
332
+ columnIndex: import_zod.z.number().int().nonnegative().optional(),
333
+ columnName: import_zod.z.string().optional(),
334
+ rowSpanId: import_zod.z.string().optional(),
335
+ tableSpanId: import_zod.z.string().optional(),
336
+ isHeader: import_zod.z.boolean().optional()
337
+ });
317
338
  var SourceSpanSchema = import_zod.z.object({
318
339
  id: import_zod.z.string().min(1),
319
340
  documentId: import_zod.z.string().min(1),
@@ -327,6 +348,9 @@ var SourceSpanSchema = import_zod.z.object({
327
348
  pageEnd: import_zod.z.number().int().positive().optional(),
328
349
  sectionId: import_zod.z.string().optional(),
329
350
  formNumber: import_zod.z.string().optional(),
351
+ sourceUnit: SourceSpanUnitSchema.optional(),
352
+ parentSpanId: import_zod.z.string().optional(),
353
+ table: SourceSpanTableLocationSchema.optional(),
330
354
  bbox: import_zod.z.array(SourceSpanBBoxSchema).optional(),
331
355
  location: SourceSpanLocationSchema.optional(),
332
356
  metadata: import_zod.z.record(import_zod.z.string(), import_zod.z.string()).optional()
@@ -411,6 +435,9 @@ function rowToSourceSpan(row) {
411
435
  pageEnd: row.page_end ?? void 0,
412
436
  sectionId: row.section_id ?? void 0,
413
437
  formNumber: row.form_number ?? void 0,
438
+ sourceUnit: row.source_unit ?? void 0,
439
+ parentSpanId: row.parent_span_id ?? void 0,
440
+ table: parseJson(row.table_location, void 0),
414
441
  location: parseJson(row.location, void 0),
415
442
  bbox: parseJson(row.bbox, void 0),
416
443
  metadata: Object.keys(metadata).length ? metadata : void 0
@@ -435,8 +462,9 @@ function createSqliteSourceStore(db, embed) {
435
462
  const stmt = db.prepare(`
436
463
  INSERT OR REPLACE INTO source_spans (
437
464
  id, document_id, source_kind, chunk_id, kind, text, hash, text_hash,
438
- page_start, page_end, section_id, form_number, location, bbox, metadata, embedding
439
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
465
+ page_start, page_end, section_id, form_number, source_unit, parent_span_id,
466
+ table_location, location, bbox, metadata, embedding
467
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
440
468
  `);
441
469
  const insertMany = db.transaction((items) => {
442
470
  for (const span of items) {
@@ -453,6 +481,9 @@ function createSqliteSourceStore(db, embed) {
453
481
  span.pageEnd ?? null,
454
482
  span.sectionId ?? null,
455
483
  span.formNumber ?? null,
484
+ span.sourceUnit ?? null,
485
+ span.parentSpanId ?? null,
486
+ span.table ? JSON.stringify(span.table) : null,
456
487
  span.location ? JSON.stringify(span.location) : null,
457
488
  span.bbox ? JSON.stringify(span.bbox) : null,
458
489
  JSON.stringify(span.metadata ?? {}),
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/storage/sqlite/index.ts","../src/storage/sqlite/migrations.ts","../src/storage/sqlite/document-store.ts","../src/core/concurrency.ts","../src/storage/sqlite/memory-store.ts","../src/source/schemas.ts","../src/source/retrieval.ts","../src/storage/sqlite/source-store.ts"],"sourcesContent":["import type { EmbedText } from \"../../core/types\";\nimport type { DocumentStore } from \"../interfaces\";\nimport type { MemoryStore } from \"../interfaces\";\nimport type { SourceStore } from \"../../source\";\nimport { CREATE_TABLES } from \"./migrations\";\nimport { createSqliteDocumentStore } from \"./document-store\";\nimport { createSqliteMemoryStore } from \"./memory-store\";\nimport { createSqliteSourceStore } from \"./source-store\";\n\nexport { createSqliteDocumentStore } from \"./document-store\";\nexport { createSqliteMemoryStore } from \"./memory-store\";\nexport { createSqliteSourceStore } from \"./source-store\";\n\nexport interface SqliteStoreOptions {\n path: string;\n embed: EmbedText;\n}\n\nexport function createSqliteStore(options: SqliteStoreOptions): {\n documents: DocumentStore;\n memory: MemoryStore;\n source: SourceStore;\n close: () => void;\n} {\n // Dynamic import to keep better-sqlite3 optional\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n const BetterSqlite3 = require(\"better-sqlite3\");\n const db = new BetterSqlite3(options.path);\n\n db.pragma(\"journal_mode = WAL\");\n db.pragma(\"foreign_keys = ON\");\n db.exec(CREATE_TABLES);\n\n return {\n documents: createSqliteDocumentStore(db),\n memory: createSqliteMemoryStore(db, options.embed),\n source: createSqliteSourceStore(db, options.embed),\n close: () => db.close(),\n };\n}\n\nexport type { DocumentStore, MemoryStore } from \"../interfaces\";\nexport type { SourceStore } from \"../../source\";\n","export const CREATE_TABLES = `\nCREATE TABLE IF NOT EXISTS documents (\n id TEXT PRIMARY KEY,\n type TEXT NOT NULL,\n data TEXT NOT NULL,\n created_at INTEGER NOT NULL DEFAULT (unixepoch())\n);\n\nCREATE TABLE IF NOT EXISTS chunks (\n id TEXT PRIMARY KEY,\n document_id TEXT NOT NULL,\n type TEXT NOT NULL,\n text TEXT NOT NULL,\n metadata TEXT NOT NULL DEFAULT '{}',\n embedding BLOB,\n FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE\n);\n\nCREATE TABLE IF NOT EXISTS conversation_turns (\n id TEXT PRIMARY KEY,\n conversation_id TEXT NOT NULL,\n role TEXT NOT NULL,\n content TEXT NOT NULL,\n tool_name TEXT,\n tool_result TEXT,\n timestamp INTEGER NOT NULL,\n embedding BLOB\n);\n\nCREATE TABLE IF NOT EXISTS source_spans (\n id TEXT PRIMARY KEY,\n document_id TEXT NOT NULL,\n source_kind TEXT,\n chunk_id TEXT,\n kind TEXT NOT NULL,\n text TEXT NOT NULL,\n hash TEXT NOT NULL,\n text_hash TEXT,\n page_start INTEGER,\n page_end INTEGER,\n section_id TEXT,\n form_number TEXT,\n location TEXT,\n bbox TEXT,\n metadata TEXT NOT NULL DEFAULT '{}',\n embedding BLOB\n);\n\nCREATE TABLE IF NOT EXISTS source_chunks (\n id TEXT PRIMARY KEY,\n document_id TEXT NOT NULL,\n source_span_ids TEXT NOT NULL,\n text TEXT NOT NULL,\n text_hash TEXT NOT NULL,\n page_start INTEGER,\n page_end INTEGER,\n metadata TEXT NOT NULL DEFAULT '{}'\n);\n\nCREATE INDEX IF NOT EXISTS idx_chunks_document_id ON chunks(document_id);\nCREATE INDEX IF NOT EXISTS idx_chunks_type ON chunks(type);\nCREATE INDEX IF NOT EXISTS idx_turns_conversation_id ON conversation_turns(conversation_id);\nCREATE INDEX IF NOT EXISTS idx_turns_timestamp ON conversation_turns(timestamp);\nCREATE INDEX IF NOT EXISTS idx_source_spans_document_id ON source_spans(document_id);\nCREATE INDEX IF NOT EXISTS idx_source_spans_chunk_id ON source_spans(chunk_id);\nCREATE INDEX IF NOT EXISTS idx_source_chunks_document_id ON source_chunks(document_id);\n`;\n","import type Database from \"better-sqlite3\";\nimport type { DocumentStore } from \"../interfaces\";\nimport type { InsuranceDocument } from \"../../schemas/document\";\nimport type { DocumentFilters } from \"../chunk-types\";\n\nexport function createSqliteDocumentStore(db: Database.Database): DocumentStore {\n return {\n async save(doc: InsuranceDocument): Promise<void> {\n db.prepare(\"INSERT OR REPLACE INTO documents (id, type, data) VALUES (?, ?, ?)\").run(\n doc.id, doc.type, JSON.stringify(doc),\n );\n },\n\n async get(id: string): Promise<InsuranceDocument | null> {\n const row = db.prepare(\"SELECT data FROM documents WHERE id = ?\").get(id) as { data: string } | undefined;\n return row ? JSON.parse(row.data) : null;\n },\n\n async query(filters: DocumentFilters): Promise<InsuranceDocument[]> {\n let sql = \"SELECT data FROM documents WHERE 1=1\";\n const params: unknown[] = [];\n\n if (filters.type) {\n sql += \" AND type = ?\";\n params.push(filters.type);\n }\n if (filters.carrier) {\n sql += \" AND json_extract(data, '$.carrier') LIKE ?\";\n params.push(`%${filters.carrier}%`);\n }\n if (filters.insuredName) {\n sql += \" AND json_extract(data, '$.insuredName') LIKE ?\";\n params.push(`%${filters.insuredName}%`);\n }\n if (filters.policyNumber) {\n sql += \" AND json_extract(data, '$.policyNumber') = ?\";\n params.push(filters.policyNumber);\n }\n if (filters.quoteNumber) {\n sql += \" AND json_extract(data, '$.quoteNumber') = ?\";\n params.push(filters.quoteNumber);\n }\n\n const rows = db.prepare(sql).all(...params) as { data: string }[];\n return rows.map((r) => JSON.parse(r.data));\n },\n\n async delete(id: string): Promise<void> {\n db.prepare(\"DELETE FROM documents WHERE id = ?\").run(id);\n },\n };\n}\n","/**\n * Concurrency limiter — returns a function that wraps async tasks\n * so at most `concurrency` run simultaneously.\n */\nexport function pLimit(concurrency: number) {\n const maxConcurrency = Number.isFinite(concurrency) ? Math.max(1, Math.floor(concurrency)) : 1;\n let active = 0;\n const queue: Array<() => void> = [];\n\n function next() {\n if (queue.length > 0 && active < maxConcurrency) {\n active++;\n queue.shift()!();\n }\n }\n\n return <T>(fn: () => Promise<T>): Promise<T> =>\n new Promise<T>((resolve, reject) => {\n const run = () => {\n fn().then(resolve, reject).finally(() => {\n active--;\n next();\n });\n };\n queue.push(run);\n next();\n });\n}\n","import type Database from \"better-sqlite3\";\nimport type { MemoryStore } from \"../interfaces\";\nimport type { DocumentChunk, ConversationTurn, ChunkFilter } from \"../chunk-types\";\nimport type { EmbedText } from \"../../core/types\";\nimport { pLimit } from \"../../core/concurrency\";\n\nfunction cosineSimilarity(a: number[], b: number[]): number {\n let dot = 0, magA = 0, magB = 0;\n for (let i = 0; i < a.length; i++) {\n dot += a[i] * b[i];\n magA += a[i] * a[i];\n magB += b[i] * b[i];\n }\n return dot / (Math.sqrt(magA) * Math.sqrt(magB));\n}\n\nexport function createSqliteMemoryStore(db: Database.Database, embed: EmbedText): MemoryStore {\n const limit = pLimit(4);\n\n return {\n async addChunks(chunks: DocumentChunk[]): Promise<void> {\n const stmt = db.prepare(\"INSERT OR REPLACE INTO chunks (id, document_id, type, text, metadata, embedding) VALUES (?, ?, ?, ?, ?, ?)\");\n const insertMany = db.transaction((items: DocumentChunk[]) => {\n for (const chunk of items) {\n stmt.run(chunk.id, chunk.documentId, chunk.type, chunk.text, JSON.stringify(chunk.metadata), null);\n }\n });\n insertMany(chunks);\n\n await Promise.all(chunks.map((chunk) =>\n limit(async () => {\n try {\n const embedding = await embed(chunk.text);\n const buf = Buffer.from(new Float64Array(embedding).buffer);\n db.prepare(\"UPDATE chunks SET embedding = ? WHERE id = ?\").run(buf, chunk.id);\n } catch {\n // Embedding failure is non-fatal\n }\n }),\n ));\n },\n\n async search(query: string, options?: { limit?: number; filter?: ChunkFilter }): Promise<DocumentChunk[]> {\n const queryEmbedding = await embed(query);\n const resultLimit = options?.limit ?? 10;\n\n let sql = \"SELECT id, document_id, type, text, metadata, embedding FROM chunks WHERE embedding IS NOT NULL\";\n const params: unknown[] = [];\n\n if (options?.filter?.documentId) {\n sql += \" AND document_id = ?\";\n params.push(options.filter.documentId);\n }\n if (options?.filter?.type) {\n sql += \" AND type = ?\";\n params.push(options.filter.type);\n }\n\n const rows = db.prepare(sql).all(...params) as Array<{\n id: string; document_id: string; type: string; text: string; metadata: string; embedding: Buffer;\n }>;\n\n const scored = rows.map((row) => {\n const stored = Array.from(new Float64Array(row.embedding.buffer, row.embedding.byteOffset, row.embedding.byteLength / 8));\n return {\n chunk: {\n id: row.id,\n documentId: row.document_id,\n type: row.type as DocumentChunk[\"type\"],\n text: row.text,\n metadata: JSON.parse(row.metadata),\n },\n score: cosineSimilarity(queryEmbedding, stored),\n };\n });\n\n scored.sort((a, b) => b.score - a.score);\n return scored.slice(0, resultLimit).map((s) => s.chunk);\n },\n\n async addTurn(turn: ConversationTurn): Promise<void> {\n const embedding = await embed(turn.content).catch(() => null);\n const buf = embedding ? Buffer.from(new Float64Array(embedding).buffer) : null;\n db.prepare(\n \"INSERT INTO conversation_turns (id, conversation_id, role, content, tool_name, tool_result, timestamp, embedding) VALUES (?, ?, ?, ?, ?, ?, ?, ?)\"\n ).run(turn.id, turn.conversationId, turn.role, turn.content, turn.toolName ?? null, turn.toolResult ?? null, turn.timestamp, buf);\n },\n\n async getHistory(conversationId: string, options?: { limit?: number }): Promise<ConversationTurn[]> {\n const resultLimit = options?.limit ?? 50;\n const rows = db.prepare(\n \"SELECT id, conversation_id, role, content, tool_name, tool_result, timestamp FROM conversation_turns WHERE conversation_id = ? ORDER BY timestamp DESC LIMIT ?\"\n ).all(conversationId, resultLimit) as Array<Record<string, unknown>>;\n\n return rows.reverse().map((r) => ({\n id: r.id as string,\n conversationId: r.conversation_id as string,\n role: r.role as ConversationTurn[\"role\"],\n content: r.content as string,\n toolName: r.tool_name as string | undefined,\n toolResult: r.tool_result as string | undefined,\n timestamp: r.timestamp as number,\n }));\n },\n\n async searchHistory(query: string, conversationId?: string): Promise<ConversationTurn[]> {\n const queryEmbedding = await embed(query);\n\n let sql = \"SELECT id, conversation_id, role, content, tool_name, tool_result, timestamp, embedding FROM conversation_turns WHERE embedding IS NOT NULL\";\n const params: unknown[] = [];\n if (conversationId) {\n sql += \" AND conversation_id = ?\";\n params.push(conversationId);\n }\n\n const rows = db.prepare(sql).all(...params) as Array<Record<string, unknown>>;\n\n const scored = rows.map((row) => {\n const buf = row.embedding as Buffer;\n const stored = Array.from(new Float64Array(buf.buffer, buf.byteOffset, buf.byteLength / 8));\n return {\n turn: {\n id: row.id as string,\n conversationId: row.conversation_id as string,\n role: row.role as ConversationTurn[\"role\"],\n content: row.content as string,\n toolName: row.tool_name as string | undefined,\n toolResult: row.tool_result as string | undefined,\n timestamp: row.timestamp as number,\n },\n score: cosineSimilarity(queryEmbedding, stored),\n };\n });\n\n scored.sort((a, b) => b.score - a.score);\n return scored.slice(0, 10).map((s) => s.turn);\n },\n };\n}\n","import { z } from \"zod\";\n\nexport const SourceSpanKindSchema = z.enum([\n \"pdf_text\",\n \"pdf_image\",\n \"html\",\n \"markdown\",\n \"plain_text\",\n \"structured_field\",\n]);\nexport type SourceSpanKind = z.infer<typeof SourceSpanKindSchema>;\n\nexport const SourceKindSchema = z.enum([\n \"policy_pdf\",\n \"application_pdf\",\n \"email\",\n \"attachment\",\n \"manual_note\",\n]);\nexport type SourceKind = z.infer<typeof SourceKindSchema>;\n\nexport const SourceSpanBBoxSchema = z.object({\n page: z.number().int().positive(),\n x: z.number(),\n y: z.number(),\n width: z.number(),\n height: z.number(),\n});\nexport type SourceSpanBBox = z.infer<typeof SourceSpanBBoxSchema>;\n\nexport const SourceSpanLocationSchema = z.object({\n page: z.number().int().positive().optional(),\n startPage: z.number().int().positive().optional(),\n endPage: z.number().int().positive().optional(),\n charStart: z.number().int().nonnegative().optional(),\n charEnd: z.number().int().nonnegative().optional(),\n lineStart: z.number().int().positive().optional(),\n lineEnd: z.number().int().positive().optional(),\n fieldPath: z.string().optional(),\n});\nexport type SourceSpanLocation = z.infer<typeof SourceSpanLocationSchema>;\n\nexport const SourceSpanSchema = z.object({\n id: z.string().min(1),\n documentId: z.string().min(1),\n sourceKind: SourceKindSchema.optional(),\n chunkId: z.string().optional(),\n kind: SourceSpanKindSchema,\n text: z.string(),\n hash: z.string().min(1),\n textHash: z.string().optional(),\n pageStart: z.number().int().positive().optional(),\n pageEnd: z.number().int().positive().optional(),\n sectionId: z.string().optional(),\n formNumber: z.string().optional(),\n bbox: z.array(SourceSpanBBoxSchema).optional(),\n location: SourceSpanLocationSchema.optional(),\n metadata: z.record(z.string(), z.string()).optional(),\n});\nexport type SourceSpan = z.infer<typeof SourceSpanSchema>;\n\nexport const SourceSpanRefSchema = z.object({\n sourceSpanId: z.string().min(1),\n documentId: z.string().min(1).optional(),\n chunkId: z.string().optional(),\n quote: z.string().optional(),\n hash: z.string().optional(),\n location: SourceSpanLocationSchema.optional(),\n});\nexport type SourceSpanRef = z.infer<typeof SourceSpanRefSchema>;\n\nexport const SourceChunkSchema = z.object({\n id: z.string().min(1),\n documentId: z.string().min(1),\n sourceSpanIds: z.array(z.string().min(1)),\n text: z.string(),\n textHash: z.string().min(1),\n pageStart: z.number().int().positive().optional(),\n pageEnd: z.number().int().positive().optional(),\n metadata: z.record(z.string(), z.string()).default({}),\n});\nexport type SourceChunk = z.infer<typeof SourceChunkSchema>;\n","import type { SourceSpan } from \"./schemas\";\n\nexport type SourceRetrievalMode = \"graph_only\" | \"source_rag\" | \"long_context\" | \"hybrid\";\n\nexport interface SourceRetrievalQuery {\n question: string;\n documentIds?: string[];\n chunkIds?: string[];\n limit?: number;\n mode?: SourceRetrievalMode;\n filters?: Record<string, string>;\n}\n\nexport interface SourceRetrievalResult {\n span: SourceSpan;\n relevance: number;\n}\n\nexport interface SourceRetriever {\n searchSourceSpans(query: SourceRetrievalQuery): Promise<SourceRetrievalResult[]>;\n}\n\nexport interface OrderableSourceEvidence {\n source?: string;\n sourceSpanId?: string;\n chunkId?: string;\n documentId?: string;\n turnId?: string;\n attachmentId?: string;\n text: string;\n relevance: number;\n}\n\nfunction evidenceTieBreakId(evidence: OrderableSourceEvidence): string {\n return [\n evidence.source ?? \"\",\n evidence.sourceSpanId ?? \"\",\n evidence.chunkId ?? \"\",\n evidence.documentId ?? \"\",\n evidence.turnId ?? \"\",\n evidence.attachmentId ?? \"\",\n evidence.text,\n ].join(\"|\");\n}\n\nexport function compareSourceEvidence(a: OrderableSourceEvidence, b: OrderableSourceEvidence): number {\n const relevanceDelta = b.relevance - a.relevance;\n if (relevanceDelta !== 0) return relevanceDelta;\n return evidenceTieBreakId(a).localeCompare(evidenceTieBreakId(b));\n}\n\nexport function orderSourceEvidence<T extends OrderableSourceEvidence>(evidence: T[]): T[] {\n return [...evidence].sort(compareSourceEvidence);\n}\n","import type Database from \"better-sqlite3\";\nimport type { EmbedText } from \"../../core/types\";\nimport type {\n SourceChunk,\n SourceRetrievalQuery,\n SourceRetrievalResult,\n SourceSpan,\n SourceStore,\n} from \"../../source\";\nimport { orderSourceEvidence } from \"../../source\";\nimport { pLimit } from \"../../core/concurrency\";\n\nfunction cosineSimilarity(a: number[], b: number[]): number {\n let dot = 0, magA = 0, magB = 0;\n for (let i = 0; i < a.length; i++) {\n dot += a[i] * b[i];\n magA += a[i] * a[i];\n magB += b[i] * b[i];\n }\n const denominator = Math.sqrt(magA) * Math.sqrt(magB);\n return denominator === 0 ? 0 : dot / denominator;\n}\n\nfunction embeddingToBuffer(embedding: number[]): Buffer {\n return Buffer.from(new Float64Array(embedding).buffer);\n}\n\nfunction bufferToEmbedding(buffer: Buffer): number[] {\n return Array.from(new Float64Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / 8));\n}\n\nfunction parseJson<T>(value: string | null | undefined, fallback: T): T {\n if (!value) return fallback;\n try {\n return JSON.parse(value) as T;\n } catch {\n return fallback;\n }\n}\n\nfunction rowToSourceSpan(row: Record<string, unknown>): SourceSpan {\n const metadata = parseJson(row.metadata as string | undefined, {});\n return {\n id: row.id as string,\n documentId: row.document_id as string,\n sourceKind: (row.source_kind ?? undefined) as SourceSpan[\"sourceKind\"],\n chunkId: (row.chunk_id ?? undefined) as string | undefined,\n kind: row.kind as SourceSpan[\"kind\"],\n text: row.text as string,\n hash: row.hash as string,\n textHash: (row.text_hash ?? undefined) as string | undefined,\n pageStart: (row.page_start ?? undefined) as number | undefined,\n pageEnd: (row.page_end ?? undefined) as number | undefined,\n sectionId: (row.section_id ?? undefined) as string | undefined,\n formNumber: (row.form_number ?? undefined) as string | undefined,\n location: parseJson(row.location as string | undefined, undefined),\n bbox: parseJson(row.bbox as string | undefined, undefined),\n metadata: Object.keys(metadata).length ? metadata : undefined,\n };\n}\n\nfunction rowToSourceChunk(row: Record<string, unknown>): SourceChunk {\n return {\n id: row.id as string,\n documentId: row.document_id as string,\n sourceSpanIds: parseJson(row.source_span_ids as string | undefined, []),\n text: row.text as string,\n textHash: row.text_hash as string,\n pageStart: row.page_start as number | undefined,\n pageEnd: row.page_end as number | undefined,\n metadata: parseJson(row.metadata as string | undefined, {}),\n };\n}\n\nexport function createSqliteSourceStore(db: Database.Database, embed: EmbedText): SourceStore {\n const limit = pLimit(4);\n\n return {\n async addSourceSpans(spans: SourceSpan[]): Promise<void> {\n const stmt = db.prepare(`\n INSERT OR REPLACE INTO source_spans (\n id, document_id, source_kind, chunk_id, kind, text, hash, text_hash,\n page_start, page_end, section_id, form_number, location, bbox, metadata, embedding\n ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\n `);\n const insertMany = db.transaction((items: SourceSpan[]) => {\n for (const span of items) {\n stmt.run(\n span.id,\n span.documentId,\n span.sourceKind ?? null,\n span.chunkId ?? null,\n span.kind,\n span.text,\n span.hash,\n span.textHash ?? null,\n span.pageStart ?? null,\n span.pageEnd ?? null,\n span.sectionId ?? null,\n span.formNumber ?? null,\n span.location ? JSON.stringify(span.location) : null,\n span.bbox ? JSON.stringify(span.bbox) : null,\n JSON.stringify(span.metadata ?? {}),\n null,\n );\n }\n });\n insertMany(spans);\n\n await Promise.all(spans.map((span) =>\n limit(async () => {\n try {\n const embedding = await embed(span.text);\n db.prepare(\"UPDATE source_spans SET embedding = ? WHERE id = ?\").run(embeddingToBuffer(embedding), span.id);\n } catch {\n // Embedding failure is non-fatal; span text remains persisted.\n }\n }),\n ));\n },\n\n async addSourceChunks(chunks: SourceChunk[]): Promise<void> {\n const stmt = db.prepare(`\n INSERT OR REPLACE INTO source_chunks (\n id, document_id, source_span_ids, text, text_hash, page_start, page_end, metadata\n ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)\n `);\n const insertMany = db.transaction((items: SourceChunk[]) => {\n for (const chunk of items) {\n stmt.run(\n chunk.id,\n chunk.documentId,\n JSON.stringify(chunk.sourceSpanIds),\n chunk.text,\n chunk.textHash,\n chunk.pageStart ?? null,\n chunk.pageEnd ?? null,\n JSON.stringify(chunk.metadata ?? {}),\n );\n }\n });\n insertMany(chunks);\n },\n\n async getSourceSpan(id: string): Promise<SourceSpan | null> {\n const row = db.prepare(\"SELECT * FROM source_spans WHERE id = ?\").get(id) as Record<string, unknown> | undefined;\n return row ? rowToSourceSpan(row) : null;\n },\n\n async getSourceSpansByDocument(documentId: string): Promise<SourceSpan[]> {\n const rows = db.prepare(\"SELECT * FROM source_spans WHERE document_id = ? ORDER BY id ASC\").all(documentId) as Array<Record<string, unknown>>;\n return rows.map(rowToSourceSpan);\n },\n\n async getSourceChunksByDocument(documentId: string): Promise<SourceChunk[]> {\n const rows = db.prepare(\"SELECT * FROM source_chunks WHERE document_id = ? ORDER BY id ASC\").all(documentId) as Array<Record<string, unknown>>;\n return rows.map(rowToSourceChunk);\n },\n\n async deleteDocumentSource(documentId: string): Promise<void> {\n db.prepare(\"DELETE FROM source_chunks WHERE document_id = ?\").run(documentId);\n db.prepare(\"DELETE FROM source_spans WHERE document_id = ?\").run(documentId);\n },\n\n async searchSourceSpans(query: SourceRetrievalQuery): Promise<SourceRetrievalResult[]> {\n const queryEmbedding = await embed(query.question);\n const resultLimit = query.limit ?? 10;\n let sql = \"SELECT * FROM source_spans WHERE embedding IS NOT NULL\";\n const params: unknown[] = [];\n\n if (query.documentIds?.length) {\n sql += ` AND document_id IN (${query.documentIds.map(() => \"?\").join(\",\")})`;\n params.push(...query.documentIds);\n }\n if (query.chunkIds?.length) {\n sql += ` AND chunk_id IN (${query.chunkIds.map(() => \"?\").join(\",\")})`;\n params.push(...query.chunkIds);\n }\n if (query.filters?.sourceKind) {\n sql += \" AND source_kind = ?\";\n params.push(query.filters.sourceKind);\n }\n if (query.filters?.formNumber) {\n sql += \" AND form_number = ?\";\n params.push(query.filters.formNumber);\n }\n if (query.filters?.sectionId) {\n sql += \" AND section_id = ?\";\n params.push(query.filters.sectionId);\n }\n\n const rows = db.prepare(sql).all(...params) as Array<Record<string, unknown>>;\n const scored = rows.map((row) => {\n const span = rowToSourceSpan(row);\n return {\n span,\n relevance: cosineSimilarity(queryEmbedding, bufferToEmbedding(row.embedding as Buffer)),\n };\n });\n\n return orderSourceEvidence(scored.map((result) => ({\n ...result,\n sourceSpanId: result.span.id,\n chunkId: result.span.chunkId,\n documentId: result.span.documentId,\n text: result.span.text,\n }))).map(({ span, relevance }) => ({ span, relevance })).slice(0, resultLimit);\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,gBAAgB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;A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,IACvC,QAAQ,wBAAwB,IAAI,QAAQ,KAAK;AAAA,IACjD,QAAQ,wBAAwB,IAAI,QAAQ,KAAK;AAAA,IACjD,OAAO,MAAM,GAAG,MAAM;AAAA,EACxB;AACF;","names":["cosineSimilarity"]}
1
+ {"version":3,"sources":["../src/storage/sqlite/index.ts","../src/storage/sqlite/migrations.ts","../src/storage/sqlite/document-store.ts","../src/core/concurrency.ts","../src/storage/sqlite/memory-store.ts","../src/source/schemas.ts","../src/source/retrieval.ts","../src/storage/sqlite/source-store.ts"],"sourcesContent":["import type { EmbedText } from \"../../core/types\";\nimport type { DocumentStore } from \"../interfaces\";\nimport type { MemoryStore } from \"../interfaces\";\nimport type { SourceStore } from \"../../source\";\nimport { CREATE_TABLES } from \"./migrations\";\nimport { createSqliteDocumentStore } from \"./document-store\";\nimport { createSqliteMemoryStore } from \"./memory-store\";\nimport { createSqliteSourceStore } from \"./source-store\";\n\nexport { createSqliteDocumentStore } from \"./document-store\";\nexport { createSqliteMemoryStore } from \"./memory-store\";\nexport { createSqliteSourceStore } from \"./source-store\";\n\nexport interface SqliteStoreOptions {\n path: string;\n embed: EmbedText;\n}\n\nexport function createSqliteStore(options: SqliteStoreOptions): {\n documents: DocumentStore;\n memory: MemoryStore;\n source: SourceStore;\n close: () => void;\n} {\n // Dynamic import to keep better-sqlite3 optional\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n const BetterSqlite3 = require(\"better-sqlite3\");\n const db = new BetterSqlite3(options.path);\n\n db.pragma(\"journal_mode = WAL\");\n db.pragma(\"foreign_keys = ON\");\n db.exec(CREATE_TABLES);\n\n return {\n documents: createSqliteDocumentStore(db),\n memory: createSqliteMemoryStore(db, options.embed),\n source: createSqliteSourceStore(db, options.embed),\n close: () => db.close(),\n };\n}\n\nexport type { DocumentStore, MemoryStore } from \"../interfaces\";\nexport type { SourceStore } from \"../../source\";\n","export const CREATE_TABLES = `\nCREATE TABLE IF NOT EXISTS documents (\n id TEXT PRIMARY KEY,\n type TEXT NOT NULL,\n data TEXT NOT NULL,\n created_at INTEGER NOT NULL DEFAULT (unixepoch())\n);\n\nCREATE TABLE IF NOT EXISTS chunks (\n id TEXT PRIMARY KEY,\n document_id TEXT NOT NULL,\n type TEXT NOT NULL,\n text TEXT NOT NULL,\n metadata TEXT NOT NULL DEFAULT '{}',\n embedding BLOB,\n FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE\n);\n\nCREATE TABLE IF NOT EXISTS conversation_turns (\n id TEXT PRIMARY KEY,\n conversation_id TEXT NOT NULL,\n role TEXT NOT NULL,\n content TEXT NOT NULL,\n tool_name TEXT,\n tool_result TEXT,\n timestamp INTEGER NOT NULL,\n embedding BLOB\n);\n\nCREATE TABLE IF NOT EXISTS source_spans (\n id TEXT PRIMARY KEY,\n document_id TEXT NOT NULL,\n source_kind TEXT,\n chunk_id TEXT,\n kind TEXT NOT NULL,\n text TEXT NOT NULL,\n hash TEXT NOT NULL,\n text_hash TEXT,\n page_start INTEGER,\n page_end INTEGER,\n section_id TEXT,\n form_number TEXT,\n source_unit TEXT,\n parent_span_id TEXT,\n table_location TEXT,\n location TEXT,\n bbox TEXT,\n metadata TEXT NOT NULL DEFAULT '{}',\n embedding BLOB\n);\n\nCREATE TABLE IF NOT EXISTS source_chunks (\n id TEXT PRIMARY KEY,\n document_id TEXT NOT NULL,\n source_span_ids TEXT NOT NULL,\n text TEXT NOT NULL,\n text_hash TEXT NOT NULL,\n page_start INTEGER,\n page_end INTEGER,\n metadata TEXT NOT NULL DEFAULT '{}'\n);\n\nCREATE INDEX IF NOT EXISTS idx_chunks_document_id ON chunks(document_id);\nCREATE INDEX IF NOT EXISTS idx_chunks_type ON chunks(type);\nCREATE INDEX IF NOT EXISTS idx_turns_conversation_id ON conversation_turns(conversation_id);\nCREATE INDEX IF NOT EXISTS idx_turns_timestamp ON conversation_turns(timestamp);\nCREATE INDEX IF NOT EXISTS idx_source_spans_document_id ON source_spans(document_id);\nCREATE INDEX IF NOT EXISTS idx_source_spans_chunk_id ON source_spans(chunk_id);\nCREATE INDEX IF NOT EXISTS idx_source_chunks_document_id ON source_chunks(document_id);\n`;\n","import type Database from \"better-sqlite3\";\nimport type { DocumentStore } from \"../interfaces\";\nimport type { InsuranceDocument } from \"../../schemas/document\";\nimport type { DocumentFilters } from \"../chunk-types\";\n\nexport function createSqliteDocumentStore(db: Database.Database): DocumentStore {\n return {\n async save(doc: InsuranceDocument): Promise<void> {\n db.prepare(\"INSERT OR REPLACE INTO documents (id, type, data) VALUES (?, ?, ?)\").run(\n doc.id, doc.type, JSON.stringify(doc),\n );\n },\n\n async get(id: string): Promise<InsuranceDocument | null> {\n const row = db.prepare(\"SELECT data FROM documents WHERE id = ?\").get(id) as { data: string } | undefined;\n return row ? JSON.parse(row.data) : null;\n },\n\n async query(filters: DocumentFilters): Promise<InsuranceDocument[]> {\n let sql = \"SELECT data FROM documents WHERE 1=1\";\n const params: unknown[] = [];\n\n if (filters.type) {\n sql += \" AND type = ?\";\n params.push(filters.type);\n }\n if (filters.carrier) {\n sql += \" AND json_extract(data, '$.carrier') LIKE ?\";\n params.push(`%${filters.carrier}%`);\n }\n if (filters.insuredName) {\n sql += \" AND json_extract(data, '$.insuredName') LIKE ?\";\n params.push(`%${filters.insuredName}%`);\n }\n if (filters.policyNumber) {\n sql += \" AND json_extract(data, '$.policyNumber') = ?\";\n params.push(filters.policyNumber);\n }\n if (filters.quoteNumber) {\n sql += \" AND json_extract(data, '$.quoteNumber') = ?\";\n params.push(filters.quoteNumber);\n }\n\n const rows = db.prepare(sql).all(...params) as { data: string }[];\n return rows.map((r) => JSON.parse(r.data));\n },\n\n async delete(id: string): Promise<void> {\n db.prepare(\"DELETE FROM documents WHERE id = ?\").run(id);\n },\n };\n}\n","/**\n * Concurrency limiter — returns a function that wraps async tasks\n * so at most `concurrency` run simultaneously.\n */\nexport function pLimit(concurrency: number) {\n const maxConcurrency = Number.isFinite(concurrency) ? Math.max(1, Math.floor(concurrency)) : 1;\n let active = 0;\n const queue: Array<() => void> = [];\n\n function next() {\n if (queue.length > 0 && active < maxConcurrency) {\n active++;\n queue.shift()!();\n }\n }\n\n return <T>(fn: () => Promise<T>): Promise<T> =>\n new Promise<T>((resolve, reject) => {\n const run = () => {\n fn().then(resolve, reject).finally(() => {\n active--;\n next();\n });\n };\n queue.push(run);\n next();\n });\n}\n","import type Database from \"better-sqlite3\";\nimport type { MemoryStore } from \"../interfaces\";\nimport type { DocumentChunk, ConversationTurn, ChunkFilter } from \"../chunk-types\";\nimport type { EmbedText } from \"../../core/types\";\nimport { pLimit } from \"../../core/concurrency\";\n\nfunction cosineSimilarity(a: number[], b: number[]): number {\n let dot = 0, magA = 0, magB = 0;\n for (let i = 0; i < a.length; i++) {\n dot += a[i] * b[i];\n magA += a[i] * a[i];\n magB += b[i] * b[i];\n }\n return dot / (Math.sqrt(magA) * Math.sqrt(magB));\n}\n\nexport function createSqliteMemoryStore(db: Database.Database, embed: EmbedText): MemoryStore {\n const limit = pLimit(4);\n\n return {\n async addChunks(chunks: DocumentChunk[]): Promise<void> {\n const stmt = db.prepare(\"INSERT OR REPLACE INTO chunks (id, document_id, type, text, metadata, embedding) VALUES (?, ?, ?, ?, ?, ?)\");\n const insertMany = db.transaction((items: DocumentChunk[]) => {\n for (const chunk of items) {\n stmt.run(chunk.id, chunk.documentId, chunk.type, chunk.text, JSON.stringify(chunk.metadata), null);\n }\n });\n insertMany(chunks);\n\n await Promise.all(chunks.map((chunk) =>\n limit(async () => {\n try {\n const embedding = await embed(chunk.text);\n const buf = Buffer.from(new Float64Array(embedding).buffer);\n db.prepare(\"UPDATE chunks SET embedding = ? WHERE id = ?\").run(buf, chunk.id);\n } catch {\n // Embedding failure is non-fatal\n }\n }),\n ));\n },\n\n async search(query: string, options?: { limit?: number; filter?: ChunkFilter }): Promise<DocumentChunk[]> {\n const queryEmbedding = await embed(query);\n const resultLimit = options?.limit ?? 10;\n\n let sql = \"SELECT id, document_id, type, text, metadata, embedding FROM chunks WHERE embedding IS NOT NULL\";\n const params: unknown[] = [];\n\n if (options?.filter?.documentId) {\n sql += \" AND document_id = ?\";\n params.push(options.filter.documentId);\n }\n if (options?.filter?.type) {\n sql += \" AND type = ?\";\n params.push(options.filter.type);\n }\n\n const rows = db.prepare(sql).all(...params) as Array<{\n id: string; document_id: string; type: string; text: string; metadata: string; embedding: Buffer;\n }>;\n\n const scored = rows.map((row) => {\n const stored = Array.from(new Float64Array(row.embedding.buffer, row.embedding.byteOffset, row.embedding.byteLength / 8));\n return {\n chunk: {\n id: row.id,\n documentId: row.document_id,\n type: row.type as DocumentChunk[\"type\"],\n text: row.text,\n metadata: JSON.parse(row.metadata),\n },\n score: cosineSimilarity(queryEmbedding, stored),\n };\n });\n\n scored.sort((a, b) => b.score - a.score);\n return scored.slice(0, resultLimit).map((s) => s.chunk);\n },\n\n async addTurn(turn: ConversationTurn): Promise<void> {\n const embedding = await embed(turn.content).catch(() => null);\n const buf = embedding ? Buffer.from(new Float64Array(embedding).buffer) : null;\n db.prepare(\n \"INSERT INTO conversation_turns (id, conversation_id, role, content, tool_name, tool_result, timestamp, embedding) VALUES (?, ?, ?, ?, ?, ?, ?, ?)\"\n ).run(turn.id, turn.conversationId, turn.role, turn.content, turn.toolName ?? null, turn.toolResult ?? null, turn.timestamp, buf);\n },\n\n async getHistory(conversationId: string, options?: { limit?: number }): Promise<ConversationTurn[]> {\n const resultLimit = options?.limit ?? 50;\n const rows = db.prepare(\n \"SELECT id, conversation_id, role, content, tool_name, tool_result, timestamp FROM conversation_turns WHERE conversation_id = ? ORDER BY timestamp DESC LIMIT ?\"\n ).all(conversationId, resultLimit) as Array<Record<string, unknown>>;\n\n return rows.reverse().map((r) => ({\n id: r.id as string,\n conversationId: r.conversation_id as string,\n role: r.role as ConversationTurn[\"role\"],\n content: r.content as string,\n toolName: r.tool_name as string | undefined,\n toolResult: r.tool_result as string | undefined,\n timestamp: r.timestamp as number,\n }));\n },\n\n async searchHistory(query: string, conversationId?: string): Promise<ConversationTurn[]> {\n const queryEmbedding = await embed(query);\n\n let sql = \"SELECT id, conversation_id, role, content, tool_name, tool_result, timestamp, embedding FROM conversation_turns WHERE embedding IS NOT NULL\";\n const params: unknown[] = [];\n if (conversationId) {\n sql += \" AND conversation_id = ?\";\n params.push(conversationId);\n }\n\n const rows = db.prepare(sql).all(...params) as Array<Record<string, unknown>>;\n\n const scored = rows.map((row) => {\n const buf = row.embedding as Buffer;\n const stored = Array.from(new Float64Array(buf.buffer, buf.byteOffset, buf.byteLength / 8));\n return {\n turn: {\n id: row.id as string,\n conversationId: row.conversation_id as string,\n role: row.role as ConversationTurn[\"role\"],\n content: row.content as string,\n toolName: row.tool_name as string | undefined,\n toolResult: row.tool_result as string | undefined,\n timestamp: row.timestamp as number,\n },\n score: cosineSimilarity(queryEmbedding, stored),\n };\n });\n\n scored.sort((a, b) => b.score - a.score);\n return scored.slice(0, 10).map((s) => s.turn);\n },\n };\n}\n","import { z } from \"zod\";\n\nexport const SourceSpanKindSchema = z.enum([\n \"pdf_text\",\n \"pdf_image\",\n \"html\",\n \"markdown\",\n \"plain_text\",\n \"structured_field\",\n]);\nexport type SourceSpanKind = z.infer<typeof SourceSpanKindSchema>;\n\nexport const SourceSpanUnitSchema = z.enum([\n \"page\",\n \"section\",\n \"table\",\n \"table_row\",\n \"table_cell\",\n \"key_value\",\n \"text\",\n]);\nexport type SourceSpanUnit = z.infer<typeof SourceSpanUnitSchema>;\n\nexport const SourceKindSchema = z.enum([\n \"policy_pdf\",\n \"application_pdf\",\n \"email\",\n \"attachment\",\n \"manual_note\",\n]);\nexport type SourceKind = z.infer<typeof SourceKindSchema>;\n\nexport const SourceSpanBBoxSchema = z.object({\n page: z.number().int().positive(),\n x: z.number(),\n y: z.number(),\n width: z.number(),\n height: z.number(),\n});\nexport type SourceSpanBBox = z.infer<typeof SourceSpanBBoxSchema>;\n\nexport const SourceSpanLocationSchema = z.object({\n page: z.number().int().positive().optional(),\n startPage: z.number().int().positive().optional(),\n endPage: z.number().int().positive().optional(),\n charStart: z.number().int().nonnegative().optional(),\n charEnd: z.number().int().nonnegative().optional(),\n lineStart: z.number().int().positive().optional(),\n lineEnd: z.number().int().positive().optional(),\n fieldPath: z.string().optional(),\n});\nexport type SourceSpanLocation = z.infer<typeof SourceSpanLocationSchema>;\n\nexport const SourceSpanTableLocationSchema = z.object({\n tableId: z.string().optional(),\n rowIndex: z.number().int().nonnegative().optional(),\n columnIndex: z.number().int().nonnegative().optional(),\n columnName: z.string().optional(),\n rowSpanId: z.string().optional(),\n tableSpanId: z.string().optional(),\n isHeader: z.boolean().optional(),\n});\nexport type SourceSpanTableLocation = z.infer<typeof SourceSpanTableLocationSchema>;\n\nexport const SourceSpanSchema = z.object({\n id: z.string().min(1),\n documentId: z.string().min(1),\n sourceKind: SourceKindSchema.optional(),\n chunkId: z.string().optional(),\n kind: SourceSpanKindSchema,\n text: z.string(),\n hash: z.string().min(1),\n textHash: z.string().optional(),\n pageStart: z.number().int().positive().optional(),\n pageEnd: z.number().int().positive().optional(),\n sectionId: z.string().optional(),\n formNumber: z.string().optional(),\n sourceUnit: SourceSpanUnitSchema.optional(),\n parentSpanId: z.string().optional(),\n table: SourceSpanTableLocationSchema.optional(),\n bbox: z.array(SourceSpanBBoxSchema).optional(),\n location: SourceSpanLocationSchema.optional(),\n metadata: z.record(z.string(), z.string()).optional(),\n});\nexport type SourceSpan = z.infer<typeof SourceSpanSchema>;\n\nexport const SourceSpanRefSchema = z.object({\n sourceSpanId: z.string().min(1),\n documentId: z.string().min(1).optional(),\n chunkId: z.string().optional(),\n quote: z.string().optional(),\n hash: z.string().optional(),\n location: SourceSpanLocationSchema.optional(),\n});\nexport type SourceSpanRef = z.infer<typeof SourceSpanRefSchema>;\n\nexport const SourceChunkSchema = z.object({\n id: z.string().min(1),\n documentId: z.string().min(1),\n sourceSpanIds: z.array(z.string().min(1)),\n text: z.string(),\n textHash: z.string().min(1),\n pageStart: z.number().int().positive().optional(),\n pageEnd: z.number().int().positive().optional(),\n metadata: z.record(z.string(), z.string()).default({}),\n});\nexport type SourceChunk = z.infer<typeof SourceChunkSchema>;\n","import type { SourceSpan } from \"./schemas\";\n\nexport type SourceRetrievalMode = \"graph_only\" | \"source_rag\" | \"long_context\" | \"hybrid\";\n\nexport interface SourceRetrievalQuery {\n question: string;\n documentIds?: string[];\n chunkIds?: string[];\n limit?: number;\n mode?: SourceRetrievalMode;\n filters?: Record<string, string>;\n}\n\nexport interface SourceRetrievalResult {\n span: SourceSpan;\n relevance: number;\n}\n\nexport interface SourceRetriever {\n searchSourceSpans(query: SourceRetrievalQuery): Promise<SourceRetrievalResult[]>;\n}\n\nexport interface OrderableSourceEvidence {\n source?: string;\n sourceSpanId?: string;\n chunkId?: string;\n documentId?: string;\n turnId?: string;\n attachmentId?: string;\n text: string;\n relevance: number;\n}\n\nfunction evidenceTieBreakId(evidence: OrderableSourceEvidence): string {\n return [\n evidence.source ?? \"\",\n evidence.sourceSpanId ?? \"\",\n evidence.chunkId ?? \"\",\n evidence.documentId ?? \"\",\n evidence.turnId ?? \"\",\n evidence.attachmentId ?? \"\",\n evidence.text,\n ].join(\"|\");\n}\n\nexport function compareSourceEvidence(a: OrderableSourceEvidence, b: OrderableSourceEvidence): number {\n const relevanceDelta = b.relevance - a.relevance;\n if (relevanceDelta !== 0) return relevanceDelta;\n return evidenceTieBreakId(a).localeCompare(evidenceTieBreakId(b));\n}\n\nexport function orderSourceEvidence<T extends OrderableSourceEvidence>(evidence: T[]): T[] {\n return [...evidence].sort(compareSourceEvidence);\n}\n","import type Database from \"better-sqlite3\";\nimport type { EmbedText } from \"../../core/types\";\nimport type {\n SourceChunk,\n SourceRetrievalQuery,\n SourceRetrievalResult,\n SourceSpan,\n SourceStore,\n} from \"../../source\";\nimport { orderSourceEvidence } from \"../../source\";\nimport { pLimit } from \"../../core/concurrency\";\n\nfunction cosineSimilarity(a: number[], b: number[]): number {\n let dot = 0, magA = 0, magB = 0;\n for (let i = 0; i < a.length; i++) {\n dot += a[i] * b[i];\n magA += a[i] * a[i];\n magB += b[i] * b[i];\n }\n const denominator = Math.sqrt(magA) * Math.sqrt(magB);\n return denominator === 0 ? 0 : dot / denominator;\n}\n\nfunction embeddingToBuffer(embedding: number[]): Buffer {\n return Buffer.from(new Float64Array(embedding).buffer);\n}\n\nfunction bufferToEmbedding(buffer: Buffer): number[] {\n return Array.from(new Float64Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / 8));\n}\n\nfunction parseJson<T>(value: string | null | undefined, fallback: T): T {\n if (!value) return fallback;\n try {\n return JSON.parse(value) as T;\n } catch {\n return fallback;\n }\n}\n\nfunction rowToSourceSpan(row: Record<string, unknown>): SourceSpan {\n const metadata = parseJson(row.metadata as string | undefined, {});\n return {\n id: row.id as string,\n documentId: row.document_id as string,\n sourceKind: (row.source_kind ?? undefined) as SourceSpan[\"sourceKind\"],\n chunkId: (row.chunk_id ?? undefined) as string | undefined,\n kind: row.kind as SourceSpan[\"kind\"],\n text: row.text as string,\n hash: row.hash as string,\n textHash: (row.text_hash ?? undefined) as string | undefined,\n pageStart: (row.page_start ?? undefined) as number | undefined,\n pageEnd: (row.page_end ?? undefined) as number | undefined,\n sectionId: (row.section_id ?? undefined) as string | undefined,\n formNumber: (row.form_number ?? undefined) as string | undefined,\n sourceUnit: (row.source_unit ?? undefined) as SourceSpan[\"sourceUnit\"],\n parentSpanId: (row.parent_span_id ?? undefined) as string | undefined,\n table: parseJson(row.table_location as string | undefined, undefined),\n location: parseJson(row.location as string | undefined, undefined),\n bbox: parseJson(row.bbox as string | undefined, undefined),\n metadata: Object.keys(metadata).length ? metadata : undefined,\n };\n}\n\nfunction rowToSourceChunk(row: Record<string, unknown>): SourceChunk {\n return {\n id: row.id as string,\n documentId: row.document_id as string,\n sourceSpanIds: parseJson(row.source_span_ids as string | undefined, []),\n text: row.text as string,\n textHash: row.text_hash as string,\n pageStart: row.page_start as number | undefined,\n pageEnd: row.page_end as number | undefined,\n metadata: parseJson(row.metadata as string | undefined, {}),\n };\n}\n\nexport function createSqliteSourceStore(db: Database.Database, embed: EmbedText): SourceStore {\n const limit = pLimit(4);\n\n return {\n async addSourceSpans(spans: SourceSpan[]): Promise<void> {\n const stmt = db.prepare(`\n INSERT OR REPLACE INTO source_spans (\n id, document_id, source_kind, chunk_id, kind, text, hash, text_hash,\n page_start, page_end, section_id, form_number, source_unit, parent_span_id,\n table_location, location, bbox, metadata, embedding\n ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\n `);\n const insertMany = db.transaction((items: SourceSpan[]) => {\n for (const span of items) {\n stmt.run(\n span.id,\n span.documentId,\n span.sourceKind ?? null,\n span.chunkId ?? null,\n span.kind,\n span.text,\n span.hash,\n span.textHash ?? null,\n span.pageStart ?? null,\n span.pageEnd ?? null,\n span.sectionId ?? null,\n span.formNumber ?? null,\n span.sourceUnit ?? null,\n span.parentSpanId ?? null,\n span.table ? JSON.stringify(span.table) : null,\n span.location ? JSON.stringify(span.location) : null,\n span.bbox ? JSON.stringify(span.bbox) : null,\n JSON.stringify(span.metadata ?? {}),\n null,\n );\n }\n });\n insertMany(spans);\n\n await Promise.all(spans.map((span) =>\n limit(async () => {\n try {\n const embedding = await embed(span.text);\n db.prepare(\"UPDATE source_spans SET embedding = ? WHERE id = ?\").run(embeddingToBuffer(embedding), span.id);\n } catch {\n // Embedding failure is non-fatal; span text remains persisted.\n }\n }),\n ));\n },\n\n async addSourceChunks(chunks: SourceChunk[]): Promise<void> {\n const stmt = db.prepare(`\n INSERT OR REPLACE INTO source_chunks (\n id, document_id, source_span_ids, text, text_hash, page_start, page_end, metadata\n ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)\n `);\n const insertMany = db.transaction((items: SourceChunk[]) => {\n for (const chunk of items) {\n stmt.run(\n chunk.id,\n chunk.documentId,\n JSON.stringify(chunk.sourceSpanIds),\n chunk.text,\n chunk.textHash,\n chunk.pageStart ?? null,\n chunk.pageEnd ?? null,\n JSON.stringify(chunk.metadata ?? {}),\n );\n }\n });\n insertMany(chunks);\n },\n\n async getSourceSpan(id: string): Promise<SourceSpan | null> {\n const row = db.prepare(\"SELECT * FROM source_spans WHERE id = ?\").get(id) as Record<string, unknown> | undefined;\n return row ? rowToSourceSpan(row) : null;\n },\n\n async getSourceSpansByDocument(documentId: string): Promise<SourceSpan[]> {\n const rows = db.prepare(\"SELECT * FROM source_spans WHERE document_id = ? ORDER BY id ASC\").all(documentId) as Array<Record<string, unknown>>;\n return rows.map(rowToSourceSpan);\n },\n\n async getSourceChunksByDocument(documentId: string): Promise<SourceChunk[]> {\n const rows = db.prepare(\"SELECT * FROM source_chunks WHERE document_id = ? ORDER BY id ASC\").all(documentId) as Array<Record<string, unknown>>;\n return rows.map(rowToSourceChunk);\n },\n\n async deleteDocumentSource(documentId: string): Promise<void> {\n db.prepare(\"DELETE FROM source_chunks WHERE document_id = ?\").run(documentId);\n db.prepare(\"DELETE FROM source_spans WHERE document_id = ?\").run(documentId);\n },\n\n async searchSourceSpans(query: SourceRetrievalQuery): Promise<SourceRetrievalResult[]> {\n const queryEmbedding = await embed(query.question);\n const resultLimit = query.limit ?? 10;\n let sql = \"SELECT * FROM source_spans WHERE embedding IS NOT NULL\";\n const params: unknown[] = [];\n\n if (query.documentIds?.length) {\n sql += ` AND document_id IN (${query.documentIds.map(() => \"?\").join(\",\")})`;\n params.push(...query.documentIds);\n }\n if (query.chunkIds?.length) {\n sql += ` AND chunk_id IN (${query.chunkIds.map(() => \"?\").join(\",\")})`;\n params.push(...query.chunkIds);\n }\n if (query.filters?.sourceKind) {\n sql += \" AND source_kind = ?\";\n params.push(query.filters.sourceKind);\n }\n if (query.filters?.formNumber) {\n sql += \" AND form_number = ?\";\n params.push(query.filters.formNumber);\n }\n if (query.filters?.sectionId) {\n sql += \" AND section_id = ?\";\n params.push(query.filters.sectionId);\n }\n\n const rows = db.prepare(sql).all(...params) as Array<Record<string, unknown>>;\n const scored = rows.map((row) => {\n const span = rowToSourceSpan(row);\n return {\n span,\n relevance: cosineSimilarity(queryEmbedding, bufferToEmbedding(row.embedding as Buffer)),\n };\n });\n\n return orderSourceEvidence(scored.map((result) => ({\n ...result,\n sourceSpanId: result.span.id,\n chunkId: result.span.chunkId,\n documentId: result.span.documentId,\n text: result.span.text,\n }))).map(({ span, relevance }) => ({ span, relevance })).slice(0, resultLimit);\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,gBAAgB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKtB,SAAS,0BAA0B,IAAsC;AAC9E,SAAO;AAAA,IACL,MAAM,KAAK,KAAuC;AAChD,SAAG,QAAQ,oEAAoE,EAAE;AAAA,QAC/E,IAAI;AAAA,QAAI,IAAI;AAAA,QAAM,KAAK,UAAU,GAAG;AAAA,MACtC;AAAA,IACF;AAAA,IAEA,MAAM,IAAI,IAA+C;AACvD,YAAM,MAAM,GAAG,QAAQ,yCAAyC,EAAE,IAAI,EAAE;AACxE,aAAO,MAAM,KAAK,MAAM,IAAI,IAAI,IAAI;AAAA,IACtC;AAAA,IAEA,MAAM,MAAM,SAAwD;AAClE,UAAI,MAAM;AACV,YAAM,SAAoB,CAAC;AAE3B,UAAI,QAAQ,MAAM;AAChB,eAAO;AACP,eAAO,KAAK,QAAQ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