@claritylabs/cl-sdk 0.3.1 → 0.5.0

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/dist/index.js CHANGED
@@ -24,9 +24,16 @@ __export(index_exports, {
24
24
  AGENT_TOOLS: () => AGENT_TOOLS,
25
25
  APPLICATION_CLASSIFY_PROMPT: () => APPLICATION_CLASSIFY_PROMPT,
26
26
  AUDIT_TYPES: () => AUDIT_TYPES,
27
+ AcroFormMappingSchema: () => AcroFormMappingSchema,
27
28
  AddressSchema: () => AddressSchema,
28
29
  AdmittedStatusSchema: () => AdmittedStatusSchema,
30
+ AnswerParsingResultSchema: () => AnswerParsingResultSchema,
31
+ ApplicationClassifyResultSchema: () => ApplicationClassifyResultSchema,
32
+ ApplicationFieldSchema: () => ApplicationFieldSchema,
33
+ ApplicationStateSchema: () => ApplicationStateSchema,
29
34
  AuditTypeSchema: () => AuditTypeSchema,
35
+ AutoFillMatchSchema: () => AutoFillMatchSchema,
36
+ AutoFillResultSchema: () => AutoFillResultSchema,
30
37
  BOAT_TYPES: () => BOAT_TYPES,
31
38
  BindingAuthoritySchema: () => BindingAuthoritySchema,
32
39
  BoatTypeSchema: () => BoatTypeSchema,
@@ -40,6 +47,7 @@ __export(index_exports, {
40
47
  COVERAGE_FORMS: () => COVERAGE_FORMS,
41
48
  COVERAGE_TRIGGERS: () => COVERAGE_TRIGGERS,
42
49
  ChunkTypeSchema: () => ChunkTypeSchema,
50
+ CitationSchema: () => CitationSchema,
43
51
  ClaimRecordSchema: () => ClaimRecordSchema,
44
52
  ClaimStatusSchema: () => ClaimStatusSchema,
45
53
  ClassificationCodeSchema: () => ClassificationCodeSchema,
@@ -82,12 +90,16 @@ __export(index_exports, {
82
90
  EnrichedSubjectivitySchema: () => EnrichedSubjectivitySchema,
83
91
  EnrichedUnderwritingConditionSchema: () => EnrichedUnderwritingConditionSchema,
84
92
  EntityTypeSchema: () => EntityTypeSchema,
93
+ EvidenceItemSchema: () => EvidenceItemSchema,
85
94
  ExclusionSchema: () => ExclusionSchema,
86
95
  ExperienceModSchema: () => ExperienceModSchema,
87
96
  ExtendedReportingPeriodSchema: () => ExtendedReportingPeriodSchema,
88
97
  FLOOD_ZONES: () => FLOOD_ZONES,
89
98
  FOUNDATION_TYPES: () => FOUNDATION_TYPES,
90
99
  FarmRanchDeclarationsSchema: () => FarmRanchDeclarationsSchema,
100
+ FieldExtractionResultSchema: () => FieldExtractionResultSchema,
101
+ FieldTypeSchema: () => FieldTypeSchema,
102
+ FlatPdfPlacementSchema: () => FlatPdfPlacementSchema,
91
103
  FloodDeclarationsSchema: () => FloodDeclarationsSchema,
92
104
  FloodZoneSchema: () => FloodZoneSchema,
93
105
  FormReferenceSchema: () => FormReferenceSchema,
@@ -106,6 +118,9 @@ __export(index_exports, {
106
118
  LimitScheduleSchema: () => LimitScheduleSchema,
107
119
  LimitTypeSchema: () => LimitTypeSchema,
108
120
  LocationPremiumSchema: () => LocationPremiumSchema,
121
+ LookupFillResultSchema: () => LookupFillResultSchema,
122
+ LookupFillSchema: () => LookupFillSchema,
123
+ LookupRequestSchema: () => LookupRequestSchema,
109
124
  LossSettlementSchema: () => LossSettlementSchema,
110
125
  LossSummarySchema: () => LossSummarySchema,
111
126
  NamedInsuredSchema: () => NamedInsuredSchema,
@@ -115,6 +130,7 @@ __export(index_exports, {
115
130
  POLICY_SECTION_TYPES: () => POLICY_SECTION_TYPES,
116
131
  POLICY_TERM_TYPES: () => POLICY_TERM_TYPES,
117
132
  POLICY_TYPES: () => POLICY_TYPES,
133
+ ParsedAnswerSchema: () => ParsedAnswerSchema,
118
134
  PaymentInstallmentSchema: () => PaymentInstallmentSchema,
119
135
  PaymentPlanSchema: () => PaymentPlanSchema,
120
136
  PersonalArticlesDeclarationsSchema: () => PersonalArticlesDeclarationsSchema,
@@ -134,6 +150,10 @@ __export(index_exports, {
134
150
  ProducerInfoSchema: () => ProducerInfoSchema,
135
151
  ProfessionalLiabilityDeclarationsSchema: () => ProfessionalLiabilityDeclarationsSchema,
136
152
  QUOTE_SECTION_TYPES: () => QUOTE_SECTION_TYPES,
153
+ QueryClassifyResultSchema: () => QueryClassifyResultSchema,
154
+ QueryIntentSchema: () => QueryIntentSchema,
155
+ QueryResultSchema: () => QueryResultSchema,
156
+ QuestionBatchResultSchema: () => QuestionBatchResultSchema,
137
157
  QuoteDocumentSchema: () => QuoteDocumentSchema,
138
158
  QuoteSectionTypeSchema: () => QuoteSectionTypeSchema,
139
159
  RATING_BASIS_TYPES: () => RATING_BASIS_TYPES,
@@ -143,12 +163,16 @@ __export(index_exports, {
143
163
  RatingBasisSchema: () => RatingBasisSchema,
144
164
  RatingBasisTypeSchema: () => RatingBasisTypeSchema,
145
165
  RecreationalVehicleDeclarationsSchema: () => RecreationalVehicleDeclarationsSchema,
166
+ ReplyIntentSchema: () => ReplyIntentSchema,
167
+ RetrievalResultSchema: () => RetrievalResultSchema,
146
168
  RoofTypeSchema: () => RoofTypeSchema,
147
169
  SCHEDULED_ITEM_CATEGORIES: () => SCHEDULED_ITEM_CATEGORIES,
148
170
  SUBJECTIVITY_CATEGORIES: () => SUBJECTIVITY_CATEGORIES,
149
171
  ScheduledItemCategorySchema: () => ScheduledItemCategorySchema,
150
172
  SectionSchema: () => SectionSchema,
151
173
  SharedLimitSchema: () => SharedLimitSchema,
174
+ SubAnswerSchema: () => SubAnswerSchema,
175
+ SubQuestionSchema: () => SubQuestionSchema,
152
176
  SubjectivityCategorySchema: () => SubjectivityCategorySchema,
153
177
  SubjectivitySchema: () => SubjectivitySchema,
154
178
  SublimitSchema: () => SublimitSchema,
@@ -165,6 +189,7 @@ __export(index_exports, {
165
189
  ValuationMethodSchema: () => ValuationMethodSchema,
166
190
  VehicleCoverageSchema: () => VehicleCoverageSchema,
167
191
  VehicleCoverageTypeSchema: () => VehicleCoverageTypeSchema,
192
+ VerifyResultSchema: () => VerifyResultSchema,
168
193
  WatercraftDeclarationsSchema: () => WatercraftDeclarationsSchema,
169
194
  WorkersCompDeclarationsSchema: () => WorkersCompDeclarationsSchema,
170
195
  buildAcroFormMappingPrompt: () => buildAcroFormMappingPrompt,
@@ -184,12 +209,18 @@ __export(index_exports, {
184
209
  buildIdentityPrompt: () => buildIdentityPrompt,
185
210
  buildIntentPrompt: () => buildIntentPrompt,
186
211
  buildLookupFillPrompt: () => buildLookupFillPrompt,
212
+ buildQueryClassifyPrompt: () => buildQueryClassifyPrompt,
187
213
  buildQuestionBatchPrompt: () => buildQuestionBatchPrompt,
188
214
  buildQuotesPoliciesPrompt: () => buildQuotesPoliciesPrompt,
215
+ buildReasonPrompt: () => buildReasonPrompt,
189
216
  buildReplyIntentClassificationPrompt: () => buildReplyIntentClassificationPrompt,
217
+ buildRespondPrompt: () => buildRespondPrompt,
190
218
  buildSafetyPrompt: () => buildSafetyPrompt,
219
+ buildVerifyPrompt: () => buildVerifyPrompt,
191
220
  chunkDocument: () => chunkDocument,
221
+ createApplicationPipeline: () => createApplicationPipeline,
192
222
  createExtractor: () => createExtractor,
223
+ createQueryAgent: () => createQueryAgent,
193
224
  extractPageRange: () => extractPageRange,
194
225
  fillAcroForm: () => fillAcroForm,
195
226
  getAcroFormFields: () => getAcroFormFields,
@@ -3383,6 +3414,129 @@ Respond with JSON only:
3383
3414
  "applicationType": string | null // e.g. "General Liability", "Professional Liability", "Commercial Property", "Workers Compensation", "ACORD 125", etc.
3384
3415
  }`;
3385
3416
 
3417
+ // src/schemas/application.ts
3418
+ var import_zod31 = require("zod");
3419
+ var FieldTypeSchema = import_zod31.z.enum([
3420
+ "text",
3421
+ "numeric",
3422
+ "currency",
3423
+ "date",
3424
+ "yes_no",
3425
+ "table",
3426
+ "declaration"
3427
+ ]);
3428
+ var ApplicationFieldSchema = import_zod31.z.object({
3429
+ id: import_zod31.z.string(),
3430
+ label: import_zod31.z.string(),
3431
+ section: import_zod31.z.string(),
3432
+ fieldType: FieldTypeSchema,
3433
+ required: import_zod31.z.boolean(),
3434
+ options: import_zod31.z.array(import_zod31.z.string()).optional(),
3435
+ columns: import_zod31.z.array(import_zod31.z.string()).optional(),
3436
+ requiresExplanationIfYes: import_zod31.z.boolean().optional(),
3437
+ condition: import_zod31.z.object({
3438
+ dependsOn: import_zod31.z.string(),
3439
+ whenValue: import_zod31.z.string()
3440
+ }).optional(),
3441
+ value: import_zod31.z.string().optional(),
3442
+ source: import_zod31.z.string().optional().describe("Where the value came from: auto-fill, user, lookup"),
3443
+ confidence: import_zod31.z.enum(["confirmed", "high", "medium", "low"]).optional()
3444
+ });
3445
+ var ApplicationClassifyResultSchema = import_zod31.z.object({
3446
+ isApplication: import_zod31.z.boolean(),
3447
+ confidence: import_zod31.z.number().min(0).max(1),
3448
+ applicationType: import_zod31.z.string().nullable()
3449
+ });
3450
+ var FieldExtractionResultSchema = import_zod31.z.object({
3451
+ fields: import_zod31.z.array(ApplicationFieldSchema)
3452
+ });
3453
+ var AutoFillMatchSchema = import_zod31.z.object({
3454
+ fieldId: import_zod31.z.string(),
3455
+ value: import_zod31.z.string(),
3456
+ confidence: import_zod31.z.enum(["confirmed"]),
3457
+ contextKey: import_zod31.z.string()
3458
+ });
3459
+ var AutoFillResultSchema = import_zod31.z.object({
3460
+ matches: import_zod31.z.array(AutoFillMatchSchema)
3461
+ });
3462
+ var QuestionBatchResultSchema = import_zod31.z.object({
3463
+ batches: import_zod31.z.array(import_zod31.z.array(import_zod31.z.string()).describe("Array of field IDs in this batch"))
3464
+ });
3465
+ var LookupRequestSchema = import_zod31.z.object({
3466
+ type: import_zod31.z.string().describe("Type of lookup: 'records', 'website', 'policy'"),
3467
+ description: import_zod31.z.string(),
3468
+ url: import_zod31.z.string().optional(),
3469
+ targetFieldIds: import_zod31.z.array(import_zod31.z.string())
3470
+ });
3471
+ var ReplyIntentSchema = import_zod31.z.object({
3472
+ primaryIntent: import_zod31.z.enum(["answers_only", "question", "lookup_request", "mixed"]),
3473
+ hasAnswers: import_zod31.z.boolean(),
3474
+ questionText: import_zod31.z.string().optional(),
3475
+ questionFieldIds: import_zod31.z.array(import_zod31.z.string()).optional(),
3476
+ lookupRequests: import_zod31.z.array(LookupRequestSchema).optional()
3477
+ });
3478
+ var ParsedAnswerSchema = import_zod31.z.object({
3479
+ fieldId: import_zod31.z.string(),
3480
+ value: import_zod31.z.string(),
3481
+ explanation: import_zod31.z.string().optional()
3482
+ });
3483
+ var AnswerParsingResultSchema = import_zod31.z.object({
3484
+ answers: import_zod31.z.array(ParsedAnswerSchema),
3485
+ unanswered: import_zod31.z.array(import_zod31.z.string()).describe("Field IDs that were not answered")
3486
+ });
3487
+ var LookupFillSchema = import_zod31.z.object({
3488
+ fieldId: import_zod31.z.string(),
3489
+ value: import_zod31.z.string(),
3490
+ source: import_zod31.z.string().describe("Specific citable reference, e.g. 'GL Policy #POL-12345 (Hartford)'")
3491
+ });
3492
+ var LookupFillResultSchema = import_zod31.z.object({
3493
+ fills: import_zod31.z.array(LookupFillSchema),
3494
+ unfillable: import_zod31.z.array(import_zod31.z.string()),
3495
+ explanation: import_zod31.z.string().optional()
3496
+ });
3497
+ var FlatPdfPlacementSchema = import_zod31.z.object({
3498
+ fieldId: import_zod31.z.string(),
3499
+ page: import_zod31.z.number(),
3500
+ x: import_zod31.z.number().describe("Percentage from left edge (0-100)"),
3501
+ y: import_zod31.z.number().describe("Percentage from top edge (0-100)"),
3502
+ text: import_zod31.z.string(),
3503
+ fontSize: import_zod31.z.number().optional(),
3504
+ isCheckmark: import_zod31.z.boolean().optional()
3505
+ });
3506
+ var AcroFormMappingSchema = import_zod31.z.object({
3507
+ fieldId: import_zod31.z.string(),
3508
+ acroFormName: import_zod31.z.string(),
3509
+ value: import_zod31.z.string()
3510
+ });
3511
+ var ApplicationStateSchema = import_zod31.z.object({
3512
+ id: import_zod31.z.string(),
3513
+ pdfBase64: import_zod31.z.string().optional().describe("Original PDF, omitted after extraction"),
3514
+ title: import_zod31.z.string().optional(),
3515
+ applicationType: import_zod31.z.string().nullable().optional(),
3516
+ fields: import_zod31.z.array(ApplicationFieldSchema),
3517
+ batches: import_zod31.z.array(import_zod31.z.array(import_zod31.z.string())).optional(),
3518
+ currentBatchIndex: import_zod31.z.number().default(0),
3519
+ status: import_zod31.z.enum(["classifying", "extracting", "auto_filling", "batching", "collecting", "confirming", "mapping", "complete"]),
3520
+ createdAt: import_zod31.z.number(),
3521
+ updatedAt: import_zod31.z.number()
3522
+ });
3523
+
3524
+ // src/application/agents/classifier.ts
3525
+ async function classifyApplication(pdfContent, generateObject, providerOptions) {
3526
+ const { object, usage } = await withRetry(
3527
+ () => generateObject({
3528
+ prompt: `${APPLICATION_CLASSIFY_PROMPT}
3529
+
3530
+ Analyze the following document content:
3531
+ ${pdfContent}`,
3532
+ schema: ApplicationClassifyResultSchema,
3533
+ maxTokens: 512,
3534
+ providerOptions
3535
+ })
3536
+ );
3537
+ return { result: object, usage };
3538
+ }
3539
+
3386
3540
  // src/prompts/application/field-extraction.ts
3387
3541
  function buildFieldExtractionPrompt() {
3388
3542
  return `Extract all fillable fields from this insurance application PDF as a JSON array. Be concise \u2014 use short IDs and minimal keys.
@@ -3415,6 +3569,24 @@ Example:
3415
3569
  Extract ALL fields. Respond with ONLY the JSON array, no other text.`;
3416
3570
  }
3417
3571
 
3572
+ // src/application/agents/field-extractor.ts
3573
+ async function extractFields(pdfContent, generateObject, providerOptions) {
3574
+ const prompt = `${buildFieldExtractionPrompt()}
3575
+
3576
+ Extract fields from this application:
3577
+ ${pdfContent}`;
3578
+ const { object, usage } = await withRetry(
3579
+ () => generateObject({
3580
+ prompt,
3581
+ schema: FieldExtractionResultSchema,
3582
+ maxTokens: 8192,
3583
+ providerOptions
3584
+ })
3585
+ );
3586
+ const result = object;
3587
+ return { fields: result.fields, usage };
3588
+ }
3589
+
3418
3590
  // src/prompts/application/auto-fill.ts
3419
3591
  function buildAutoFillPrompt(fields, orgContext) {
3420
3592
  const fieldList = fields.map((f) => `- ${f.id}: "${f.label}" (${f.fieldType}, section: ${f.section})`).join("\n");
@@ -3444,6 +3616,39 @@ Respond with JSON only:
3444
3616
  Only include fields you can confidently fill. Do not guess or fabricate values.`;
3445
3617
  }
3446
3618
 
3619
+ // src/application/agents/auto-filler.ts
3620
+ async function autoFillFromContext(fields, orgContext, generateObject, providerOptions) {
3621
+ const fieldSummaries = fields.map((f) => ({
3622
+ id: f.id,
3623
+ label: f.label,
3624
+ fieldType: f.fieldType,
3625
+ section: f.section
3626
+ }));
3627
+ const prompt = buildAutoFillPrompt(fieldSummaries, orgContext);
3628
+ const { object, usage } = await withRetry(
3629
+ () => generateObject({
3630
+ prompt,
3631
+ schema: AutoFillResultSchema,
3632
+ maxTokens: 4096,
3633
+ providerOptions
3634
+ })
3635
+ );
3636
+ return { result: object, usage };
3637
+ }
3638
+ async function backfillFromPriorAnswers(fields, backfillProvider) {
3639
+ const unfilled = fields.filter((f) => !f.value);
3640
+ if (unfilled.length === 0) return [];
3641
+ return backfillProvider.searchPriorAnswers(
3642
+ unfilled.map((f) => ({
3643
+ id: f.id,
3644
+ label: f.label,
3645
+ section: f.section,
3646
+ fieldType: f.fieldType
3647
+ })),
3648
+ { limit: unfilled.length * 2 }
3649
+ );
3650
+ }
3651
+
3447
3652
  // src/prompts/application/question-batch.ts
3448
3653
  function buildQuestionBatchPrompt(unfilledFields) {
3449
3654
  const fieldList = unfilledFields.map(
@@ -3478,120 +3683,27 @@ Respond with JSON only:
3478
3683
  }`;
3479
3684
  }
3480
3685
 
3481
- // src/prompts/application/answer-parsing.ts
3482
- function buildAnswerParsingPrompt(questions, emailBody) {
3483
- const questionList = questions.map(
3484
- (q, i) => `${i + 1}. ${q.id}: "${q.label ?? q.text}" (type: ${q.fieldType})`
3485
- ).join("\n");
3486
- return `You are parsing a user's email reply to extract answers for specific insurance application questions.
3487
-
3488
- QUESTIONS ASKED:
3489
- ${questionList}
3490
-
3491
- USER'S EMAIL REPLY:
3492
- ${emailBody}
3493
-
3494
- Extract answers for each question. Handle:
3495
- - Direct numbered answers (1. answer, 2. answer)
3496
- - Inline answers referencing the question
3497
- - Table data provided as lists or comma-separated values
3498
- - Yes/no answers with optional explanations
3499
- - Partial responses (some questions answered, others skipped)
3500
-
3501
- Respond with JSON only:
3502
- {
3503
- "answers": [
3504
- {
3505
- "fieldId": "company_name",
3506
- "value": "Acme Corp"
3507
- },
3508
- {
3509
- "fieldId": "prior_claims_decl",
3510
- "value": "yes",
3511
- "explanation": "One claim in 2024 for water damage, $15,000 paid"
3512
- }
3513
- ],
3514
- "unanswered": ["field_id_that_was_not_answered"]
3515
- }
3516
-
3517
- Only include answers you are confident about. If a response is ambiguous, include the field in "unanswered".`;
3518
- }
3519
-
3520
- // src/prompts/application/confirmation.ts
3521
- function buildConfirmationSummaryPrompt(fields, applicationTitle) {
3522
- const fieldList = fields.map((f) => {
3523
- const label = f.label ?? f.text ?? f.id;
3524
- const value = f.value ?? "(not provided)";
3525
- return `[${f.section}] ${label}: ${value}`;
3526
- }).join("\n");
3527
- return `Format the following insurance application answers into a clean, readable summary grouped by section. This will be sent as an email for the user to review and confirm.
3528
-
3529
- APPLICATION: ${applicationTitle}
3530
-
3531
- FIELD VALUES:
3532
- ${fieldList}
3533
-
3534
- Format as a readable summary:
3535
- - Group by section with section headers
3536
- - Show each field as "Label: Value"
3537
- - For declarations, show the question and the yes/no answer plus any explanation
3538
- - Skip fields with no value unless they are required
3539
- - End with a note asking the user to reply "Looks good" to confirm, or describe any changes needed
3540
-
3541
- Respond with the formatted summary text only (no JSON wrapper). Use markdown formatting (bold headers, bullet points).`;
3542
- }
3543
-
3544
- // src/prompts/application/batch-email.ts
3545
- function buildBatchEmailGenerationPrompt(batchFields, batchIndex, totalBatches, appTitle, totalFieldCount, filledFieldCount, previousBatchSummary, companyName) {
3546
- const nonConditionalFields = batchFields.filter((f) => !f.condition);
3547
- const conditionalFields = batchFields.filter((f) => f.condition);
3548
- const fieldList = nonConditionalFields.map((f, i) => {
3549
- let line = `${i + 1}. id="${f.id}" label="${f.label}" type=${f.fieldType}`;
3550
- if (f.options) line += ` options=[${f.options.join(", ")}]`;
3551
- return line;
3552
- }).join("\n");
3553
- const conditionalNote = conditionalFields.length > 0 ? `
3554
-
3555
- CONDITIONAL FIELDS (DO NOT include in this email \u2014 they will be asked as follow-ups in a separate email after the parent is answered):
3556
- ${conditionalFields.map((f) => `- id="${f.id}" label="${f.label}" depends on ${f.condition.dependsOn} = "${f.condition.whenValue}"`).join("\n")}` : "";
3557
- const company = companyName ?? "the company";
3558
- const remainingFields = totalFieldCount - filledFieldCount;
3559
- const estMinutes = Math.max(1, Math.round(remainingFields * 0.5));
3560
- return `You are an internal risk management assistant helping your colleague fill out an insurance application for ${company}. You work FOR ${company} \u2014 you are NOT the insurer, broker, or any external party.
3561
-
3562
- APPLICATION: ${appTitle ?? "Insurance Application"}
3563
- COMPANY: ${company}
3564
- PROGRESS: ${filledFieldCount} of ${totalFieldCount} fields done, ~${remainingFields} remaining (~${estMinutes} min of questions left)
3565
- ${previousBatchSummary ? `
3566
- PREVIOUS ANSWERS RECEIVED:
3567
- ${previousBatchSummary}
3568
- ` : ""}
3569
- FIELDS TO ASK ABOUT:
3570
- ${fieldList}${conditionalNote}
3571
-
3572
- Rules:
3573
- - ${previousBatchSummary ? 'Start by acknowledging previous answers or auto-filled data. If fields were auto-filled, list each field with its value AND cite the specific source (e.g. "from your GL Policy #ABC123", "from vercel.com", "from your business context"). If a web lookup was done, name the URL that was checked. Ask them to reply with corrections if anything is wrong.' : "Start with a one-line intro."}
3574
- - Mention progress once using estimated time remaining. Don't mention section/batch numbers or field counts.
3575
- - Use "${company}" by name when referring to the company. Also fine: "we" or "our". Never "our company" or "the company".
3576
- - Ask questions plainly. No em-dashes for dramatic effect, no filler phrases like "need to nail down" or "let's dive into". Just ask.
3577
- - For yes/no questions, ask naturally in one sentence. Don't list "Yes / No" as options. Mention what you'll need if the answer triggers a follow-up (e.g. "If not, I'll need a brief explanation.").
3578
- - For fields with 2-3 options, mention them inline. 4+ options can be a short list.
3579
- - Group related fields (address, coverage limits) into single compound questions.
3580
- - Do NOT include conditional/follow-up fields. They will be sent separately.
3581
- - Number each question.
3582
- - Note expected format where relevant: dollar amounts for currency, MM/DD/YYYY for dates, column descriptions for tables.
3583
- - End with a short closing.
3584
- - Tone: professional, brief, matter-of-fact. Write like a busy coworker, not a chatbot. No flourishes, no em-dashes between clauses, no editorializing about the questions.
3585
-
3586
- NEVER:
3587
- - Sound like a salesperson or customer service agent
3588
- - Use em-dashes for emphasis or dramatic pacing
3589
- - Editorialize ("these two should wrap up this section", "just a couple more")
3590
- - List "Yes / No / N/A" as bullet options
3591
- - Include conditional follow-up questions
3592
- - Mention section numbers, batch numbers, or field counts
3593
-
3594
- Output the email body text ONLY. No subject line, no JSON. Use markdown for numbered lists.`;
3686
+ // src/application/agents/batcher.ts
3687
+ async function batchQuestions(unfilledFields, generateObject, providerOptions) {
3688
+ const fieldSummaries = unfilledFields.map((f) => ({
3689
+ id: f.id,
3690
+ label: f.label,
3691
+ text: f.label,
3692
+ fieldType: f.fieldType,
3693
+ section: f.section,
3694
+ required: f.required,
3695
+ condition: f.condition
3696
+ }));
3697
+ const prompt = buildQuestionBatchPrompt(fieldSummaries);
3698
+ const { object, usage } = await withRetry(
3699
+ () => generateObject({
3700
+ prompt,
3701
+ schema: QuestionBatchResultSchema,
3702
+ maxTokens: 2048,
3703
+ providerOptions
3704
+ })
3705
+ );
3706
+ return { result: object, usage };
3595
3707
  }
3596
3708
 
3597
3709
  // src/prompts/application/reply-intent.ts
@@ -3630,23 +3742,78 @@ Respond with JSON only:
3630
3742
  }`;
3631
3743
  }
3632
3744
 
3633
- // src/prompts/application/field-explanation.ts
3634
- function buildFieldExplanationPrompt(field, question, policyContext) {
3635
- return `You are an internal risk management assistant helping a colleague fill out an insurance application for your company. They asked a question about a field on the form.
3745
+ // src/application/agents/reply-router.ts
3746
+ async function classifyReplyIntent(fields, replyText, generateObject, providerOptions) {
3747
+ const fieldSummaries = fields.map((f) => ({ id: f.id, label: f.label }));
3748
+ const prompt = buildReplyIntentClassificationPrompt(fieldSummaries, replyText);
3749
+ const { object, usage } = await withRetry(
3750
+ () => generateObject({
3751
+ prompt,
3752
+ schema: ReplyIntentSchema,
3753
+ maxTokens: 1024,
3754
+ providerOptions
3755
+ })
3756
+ );
3757
+ return { intent: object, usage };
3758
+ }
3636
3759
 
3637
- FIELD: "${field.label}" (type: ${field.fieldType}${field.options ? `, options: ${field.options.join(", ")}` : ""})
3760
+ // src/prompts/application/answer-parsing.ts
3761
+ function buildAnswerParsingPrompt(questions, emailBody) {
3762
+ const questionList = questions.map(
3763
+ (q, i) => `${i + 1}. ${q.id}: "${q.label ?? q.text}" (type: ${q.fieldType})`
3764
+ ).join("\n");
3765
+ return `You are parsing a user's email reply to extract answers for specific insurance application questions.
3638
3766
 
3639
- THEIR QUESTION: "${question}"
3767
+ QUESTIONS ASKED:
3768
+ ${questionList}
3640
3769
 
3641
- ${policyContext ? `RELEVANT POLICY/CONTEXT INFO:
3642
- ${policyContext}
3643
- ` : ""}
3770
+ USER'S EMAIL REPLY:
3771
+ ${emailBody}
3644
3772
 
3645
- Provide a short, helpful explanation (2-3 sentences) as a coworker would. If the field has options, briefly explain what each means if relevant. If there's policy context that helps, cite the specific source (e.g. "According to our GL Policy #ABC123 with Hartford, our current aggregate limit is $2M").
3773
+ Extract answers for each question. Handle:
3774
+ - Direct numbered answers (1. answer, 2. answer)
3775
+ - Inline answers referencing the question
3776
+ - Table data provided as lists or comma-separated values
3777
+ - Yes/no answers with optional explanations
3778
+ - Partial responses (some questions answered, others skipped)
3646
3779
 
3647
- End with: "Just reply with the answer when you're ready and I'll fill it in."
3780
+ Respond with JSON only:
3781
+ {
3782
+ "answers": [
3783
+ {
3784
+ "fieldId": "company_name",
3785
+ "value": "Acme Corp"
3786
+ },
3787
+ {
3788
+ "fieldId": "prior_claims_decl",
3789
+ "value": "yes",
3790
+ "explanation": "One claim in 2024 for water damage, $15,000 paid"
3791
+ }
3792
+ ],
3793
+ "unanswered": ["field_id_that_was_not_answered"]
3794
+ }
3648
3795
 
3649
- Respond with the explanation text only \u2014 no JSON, no field ID, no extra formatting.`;
3796
+ Only include answers you are confident about. If a response is ambiguous, include the field in "unanswered".`;
3797
+ }
3798
+
3799
+ // src/application/agents/answer-parser.ts
3800
+ async function parseAnswers(fields, replyText, generateObject, providerOptions) {
3801
+ const questions = fields.map((f) => ({
3802
+ id: f.id,
3803
+ label: f.label,
3804
+ text: f.label,
3805
+ fieldType: f.fieldType
3806
+ }));
3807
+ const prompt = buildAnswerParsingPrompt(questions, replyText);
3808
+ const { object, usage } = await withRetry(
3809
+ () => generateObject({
3810
+ prompt,
3811
+ schema: AnswerParsingResultSchema,
3812
+ maxTokens: 4096,
3813
+ providerOptions
3814
+ })
3815
+ );
3816
+ return { result: object, usage };
3650
3817
  }
3651
3818
 
3652
3819
  // src/prompts/application/pdf-mapping.ts
@@ -3753,6 +3920,1122 @@ Respond with JSON only:
3753
3920
  }`;
3754
3921
  }
3755
3922
 
3923
+ // src/application/agents/lookup-filler.ts
3924
+ async function fillFromLookup(requests, targetFields, availableData, generateObject, providerOptions) {
3925
+ const requestSummaries = requests.map((r) => ({
3926
+ type: r.type,
3927
+ description: r.description,
3928
+ targetFieldIds: r.targetFieldIds
3929
+ }));
3930
+ const fieldSummaries = targetFields.map((f) => ({
3931
+ id: f.id,
3932
+ label: f.label,
3933
+ fieldType: f.fieldType
3934
+ }));
3935
+ const prompt = buildLookupFillPrompt(requestSummaries, fieldSummaries, availableData);
3936
+ const { object, usage } = await withRetry(
3937
+ () => generateObject({
3938
+ prompt,
3939
+ schema: LookupFillResultSchema,
3940
+ maxTokens: 4096,
3941
+ providerOptions
3942
+ })
3943
+ );
3944
+ return { result: object, usage };
3945
+ }
3946
+
3947
+ // src/prompts/application/batch-email.ts
3948
+ function buildBatchEmailGenerationPrompt(batchFields, batchIndex, totalBatches, appTitle, totalFieldCount, filledFieldCount, previousBatchSummary, companyName) {
3949
+ const nonConditionalFields = batchFields.filter((f) => !f.condition);
3950
+ const conditionalFields = batchFields.filter((f) => f.condition);
3951
+ const fieldList = nonConditionalFields.map((f, i) => {
3952
+ let line = `${i + 1}. id="${f.id}" label="${f.label}" type=${f.fieldType}`;
3953
+ if (f.options) line += ` options=[${f.options.join(", ")}]`;
3954
+ return line;
3955
+ }).join("\n");
3956
+ const conditionalNote = conditionalFields.length > 0 ? `
3957
+
3958
+ CONDITIONAL FIELDS (DO NOT include in this email \u2014 they will be asked as follow-ups in a separate email after the parent is answered):
3959
+ ${conditionalFields.map((f) => `- id="${f.id}" label="${f.label}" depends on ${f.condition.dependsOn} = "${f.condition.whenValue}"`).join("\n")}` : "";
3960
+ const company = companyName ?? "the company";
3961
+ const remainingFields = totalFieldCount - filledFieldCount;
3962
+ const estMinutes = Math.max(1, Math.round(remainingFields * 0.5));
3963
+ return `You are an internal risk management assistant helping your colleague fill out an insurance application for ${company}. You work FOR ${company} \u2014 you are NOT the insurer, broker, or any external party.
3964
+
3965
+ APPLICATION: ${appTitle ?? "Insurance Application"}
3966
+ COMPANY: ${company}
3967
+ PROGRESS: ${filledFieldCount} of ${totalFieldCount} fields done, ~${remainingFields} remaining (~${estMinutes} min of questions left)
3968
+ ${previousBatchSummary ? `
3969
+ PREVIOUS ANSWERS RECEIVED:
3970
+ ${previousBatchSummary}
3971
+ ` : ""}
3972
+ FIELDS TO ASK ABOUT:
3973
+ ${fieldList}${conditionalNote}
3974
+
3975
+ Rules:
3976
+ - ${previousBatchSummary ? 'Start by acknowledging previous answers or auto-filled data. If fields were auto-filled, list each field with its value AND cite the specific source (e.g. "from your GL Policy #ABC123", "from vercel.com", "from your business context"). If a web lookup was done, name the URL that was checked. Ask them to reply with corrections if anything is wrong.' : "Start with a one-line intro."}
3977
+ - Mention progress once using estimated time remaining. Don't mention section/batch numbers or field counts.
3978
+ - Use "${company}" by name when referring to the company. Also fine: "we" or "our". Never "our company" or "the company".
3979
+ - Ask questions plainly. No em-dashes for dramatic effect, no filler phrases like "need to nail down" or "let's dive into". Just ask.
3980
+ - For yes/no questions, ask naturally in one sentence. Don't list "Yes / No" as options. Mention what you'll need if the answer triggers a follow-up (e.g. "If not, I'll need a brief explanation.").
3981
+ - For fields with 2-3 options, mention them inline. 4+ options can be a short list.
3982
+ - Group related fields (address, coverage limits) into single compound questions.
3983
+ - Do NOT include conditional/follow-up fields. They will be sent separately.
3984
+ - Number each question.
3985
+ - Note expected format where relevant: dollar amounts for currency, MM/DD/YYYY for dates, column descriptions for tables.
3986
+ - End with a short closing.
3987
+ - Tone: professional, brief, matter-of-fact. Write like a busy coworker, not a chatbot. No flourishes, no em-dashes between clauses, no editorializing about the questions.
3988
+
3989
+ NEVER:
3990
+ - Sound like a salesperson or customer service agent
3991
+ - Use em-dashes for emphasis or dramatic pacing
3992
+ - Editorialize ("these two should wrap up this section", "just a couple more")
3993
+ - List "Yes / No / N/A" as bullet options
3994
+ - Include conditional follow-up questions
3995
+ - Mention section numbers, batch numbers, or field counts
3996
+
3997
+ Output the email body text ONLY. No subject line, no JSON. Use markdown for numbered lists.`;
3998
+ }
3999
+
4000
+ // src/application/agents/email-generator.ts
4001
+ async function generateBatchEmail(batchFields, batchIndex, totalBatches, opts, generateText, providerOptions) {
4002
+ const fieldSummaries = batchFields.map((f) => ({
4003
+ id: f.id,
4004
+ label: f.label,
4005
+ fieldType: f.fieldType,
4006
+ options: f.options,
4007
+ condition: f.condition
4008
+ }));
4009
+ const prompt = buildBatchEmailGenerationPrompt(
4010
+ fieldSummaries,
4011
+ batchIndex,
4012
+ totalBatches,
4013
+ opts.appTitle,
4014
+ opts.totalFieldCount,
4015
+ opts.filledFieldCount,
4016
+ opts.previousBatchSummary,
4017
+ opts.companyName
4018
+ );
4019
+ const { text, usage } = await withRetry(
4020
+ () => generateText({
4021
+ prompt,
4022
+ maxTokens: 2048,
4023
+ providerOptions
4024
+ })
4025
+ );
4026
+ return { text, usage };
4027
+ }
4028
+
4029
+ // src/application/coordinator.ts
4030
+ function createApplicationPipeline(config) {
4031
+ const {
4032
+ generateText,
4033
+ generateObject,
4034
+ applicationStore,
4035
+ documentStore,
4036
+ memoryStore,
4037
+ backfillProvider,
4038
+ orgContext = [],
4039
+ concurrency = 4,
4040
+ onTokenUsage,
4041
+ onProgress,
4042
+ log,
4043
+ providerOptions
4044
+ } = config;
4045
+ const limit = pLimit(concurrency);
4046
+ let totalUsage = { inputTokens: 0, outputTokens: 0 };
4047
+ function trackUsage(usage) {
4048
+ if (usage) {
4049
+ totalUsage.inputTokens += usage.inputTokens;
4050
+ totalUsage.outputTokens += usage.outputTokens;
4051
+ onTokenUsage?.(usage);
4052
+ }
4053
+ }
4054
+ async function processApplication(input) {
4055
+ totalUsage = { inputTokens: 0, outputTokens: 0 };
4056
+ const { pdfBase64, context } = input;
4057
+ const id = input.applicationId ?? `app-${Date.now()}`;
4058
+ const now = Date.now();
4059
+ let state = {
4060
+ id,
4061
+ pdfBase64: void 0,
4062
+ // Don't persist the full PDF in state
4063
+ title: void 0,
4064
+ applicationType: null,
4065
+ fields: [],
4066
+ batches: void 0,
4067
+ currentBatchIndex: 0,
4068
+ status: "classifying",
4069
+ createdAt: now,
4070
+ updatedAt: now
4071
+ };
4072
+ onProgress?.("Classifying document...");
4073
+ const { result: classifyResult, usage: classifyUsage } = await classifyApplication(
4074
+ pdfBase64.slice(0, 2e3),
4075
+ // Send truncated content for classification
4076
+ generateObject,
4077
+ providerOptions
4078
+ );
4079
+ trackUsage(classifyUsage);
4080
+ if (!classifyResult.isApplication) {
4081
+ state.status = "complete";
4082
+ state.updatedAt = Date.now();
4083
+ await applicationStore?.save(state);
4084
+ return { state, tokenUsage: totalUsage };
4085
+ }
4086
+ state.applicationType = classifyResult.applicationType;
4087
+ state.status = "extracting";
4088
+ state.updatedAt = Date.now();
4089
+ onProgress?.("Extracting form fields...");
4090
+ const { fields, usage: extractUsage } = await extractFields(
4091
+ pdfBase64,
4092
+ generateObject,
4093
+ providerOptions
4094
+ );
4095
+ trackUsage(extractUsage);
4096
+ state.fields = fields;
4097
+ state.title = classifyResult.applicationType ?? void 0;
4098
+ state.status = "auto_filling";
4099
+ state.updatedAt = Date.now();
4100
+ await applicationStore?.save(state);
4101
+ onProgress?.(`Auto-filling ${fields.length} fields...`);
4102
+ const fillTasks = [];
4103
+ if (backfillProvider) {
4104
+ fillTasks.push(
4105
+ (async () => {
4106
+ try {
4107
+ const priorAnswers = await backfillFromPriorAnswers(fields, backfillProvider);
4108
+ for (const pa of priorAnswers) {
4109
+ const field = state.fields.find((f) => f.id === pa.fieldId);
4110
+ if (field && !field.value && pa.relevance > 0.8) {
4111
+ field.value = pa.value;
4112
+ field.source = `backfill: ${pa.source}`;
4113
+ field.confidence = "high";
4114
+ }
4115
+ }
4116
+ } catch (e) {
4117
+ await log?.(`Backfill failed: ${e}`);
4118
+ }
4119
+ })()
4120
+ );
4121
+ }
4122
+ if (orgContext.length > 0) {
4123
+ fillTasks.push(
4124
+ limit(async () => {
4125
+ const unfilledFields2 = state.fields.filter((f) => !f.value);
4126
+ if (unfilledFields2.length === 0) return;
4127
+ const { result: autoFillResult, usage: afUsage } = await autoFillFromContext(
4128
+ unfilledFields2,
4129
+ orgContext,
4130
+ generateObject,
4131
+ providerOptions
4132
+ );
4133
+ trackUsage(afUsage);
4134
+ for (const match of autoFillResult.matches) {
4135
+ const field = state.fields.find((f) => f.id === match.fieldId);
4136
+ if (field && !field.value) {
4137
+ field.value = match.value;
4138
+ field.source = `auto-fill: ${match.contextKey}`;
4139
+ field.confidence = match.confidence;
4140
+ }
4141
+ }
4142
+ })
4143
+ );
4144
+ }
4145
+ if (documentStore && memoryStore) {
4146
+ fillTasks.push(
4147
+ (async () => {
4148
+ try {
4149
+ const unfilledFields2 = state.fields.filter((f) => !f.value);
4150
+ const searchPromises = unfilledFields2.slice(0, 10).map(
4151
+ (f) => limit(async () => {
4152
+ const chunks = await memoryStore.search(f.label, { limit: 3 });
4153
+ for (const chunk of chunks) {
4154
+ if (!state.fields.find((sf) => sf.id === f.id)?.value) {
4155
+ }
4156
+ }
4157
+ })
4158
+ );
4159
+ await Promise.all(searchPromises);
4160
+ } catch (e) {
4161
+ await log?.(`Document backfill search failed: ${e}`);
4162
+ }
4163
+ })()
4164
+ );
4165
+ }
4166
+ await Promise.all(fillTasks);
4167
+ state.updatedAt = Date.now();
4168
+ await applicationStore?.save(state);
4169
+ const unfilledFields = state.fields.filter((f) => !f.value);
4170
+ if (unfilledFields.length > 0) {
4171
+ onProgress?.(`Batching ${unfilledFields.length} remaining questions...`);
4172
+ state.status = "batching";
4173
+ const { result: batchResult, usage: batchUsage } = await batchQuestions(
4174
+ unfilledFields,
4175
+ generateObject,
4176
+ providerOptions
4177
+ );
4178
+ trackUsage(batchUsage);
4179
+ state.batches = batchResult.batches;
4180
+ state.currentBatchIndex = 0;
4181
+ state.status = "collecting";
4182
+ } else {
4183
+ state.status = "confirming";
4184
+ }
4185
+ state.updatedAt = Date.now();
4186
+ await applicationStore?.save(state);
4187
+ const filledCount = state.fields.filter((f) => f.value).length;
4188
+ onProgress?.(`Application processed: ${filledCount}/${state.fields.length} fields filled, ${state.batches?.length ?? 0} batches to collect.`);
4189
+ return { state, tokenUsage: totalUsage };
4190
+ }
4191
+ async function processReply(input) {
4192
+ totalUsage = { inputTokens: 0, outputTokens: 0 };
4193
+ const { applicationId, replyText, context } = input;
4194
+ let state = null;
4195
+ if (applicationStore) {
4196
+ state = await applicationStore.get(applicationId);
4197
+ }
4198
+ if (!state) {
4199
+ throw new Error(`Application ${applicationId} not found`);
4200
+ }
4201
+ const currentBatchFieldIds = state.batches?.[state.currentBatchIndex] ?? [];
4202
+ const currentBatchFields = state.fields.filter(
4203
+ (f) => currentBatchFieldIds.includes(f.id)
4204
+ );
4205
+ onProgress?.("Classifying reply...");
4206
+ const { intent, usage: intentUsage } = await classifyReplyIntent(
4207
+ currentBatchFields,
4208
+ replyText,
4209
+ generateObject,
4210
+ providerOptions
4211
+ );
4212
+ trackUsage(intentUsage);
4213
+ let fieldsFilled = 0;
4214
+ let responseText;
4215
+ if (intent.hasAnswers) {
4216
+ onProgress?.("Parsing answers...");
4217
+ const { result: parseResult, usage: parseUsage } = await parseAnswers(
4218
+ currentBatchFields,
4219
+ replyText,
4220
+ generateObject,
4221
+ providerOptions
4222
+ );
4223
+ trackUsage(parseUsage);
4224
+ for (const answer of parseResult.answers) {
4225
+ const field = state.fields.find((f) => f.id === answer.fieldId);
4226
+ if (field) {
4227
+ field.value = answer.value;
4228
+ field.source = "user";
4229
+ field.confidence = "confirmed";
4230
+ fieldsFilled++;
4231
+ }
4232
+ }
4233
+ }
4234
+ if (intent.lookupRequests?.length) {
4235
+ onProgress?.("Processing lookup requests...");
4236
+ let availableData = "";
4237
+ if (documentStore) {
4238
+ try {
4239
+ const docs = await documentStore.query({});
4240
+ availableData = docs.map((d) => {
4241
+ const doc = d;
4242
+ return `Document ${doc.id}: ${doc.type} - ${doc.carrier ?? "unknown carrier"} - ${doc.insuredName ?? ""}`;
4243
+ }).join("\n");
4244
+ } catch (e) {
4245
+ await log?.(`Document query for lookup failed: ${e}`);
4246
+ }
4247
+ }
4248
+ if (availableData) {
4249
+ const targetFields = state.fields.filter(
4250
+ (f) => intent.lookupRequests.some((lr) => lr.targetFieldIds.includes(f.id))
4251
+ );
4252
+ const { result: lookupResult, usage: lookupUsage } = await fillFromLookup(
4253
+ intent.lookupRequests,
4254
+ targetFields,
4255
+ availableData,
4256
+ generateObject,
4257
+ providerOptions
4258
+ );
4259
+ trackUsage(lookupUsage);
4260
+ for (const fill of lookupResult.fills) {
4261
+ const field = state.fields.find((f) => f.id === fill.fieldId);
4262
+ if (field) {
4263
+ field.value = fill.value;
4264
+ field.source = `lookup: ${fill.source}`;
4265
+ field.confidence = "high";
4266
+ fieldsFilled++;
4267
+ }
4268
+ }
4269
+ }
4270
+ }
4271
+ if (intent.primaryIntent === "question" || intent.primaryIntent === "mixed") {
4272
+ if (intent.questionText) {
4273
+ const { text, usage } = await generateText({
4274
+ prompt: `The user is filling out an insurance application and asked: "${intent.questionText}"
4275
+
4276
+ Provide a brief, helpful explanation (2-3 sentences). End with "Just reply with the answer when you're ready and I'll fill it in."`,
4277
+ maxTokens: 512,
4278
+ providerOptions
4279
+ });
4280
+ trackUsage(usage);
4281
+ responseText = text;
4282
+ }
4283
+ }
4284
+ const currentBatchComplete = currentBatchFieldIds.every(
4285
+ (fid) => state.fields.find((f) => f.id === fid)?.value
4286
+ );
4287
+ if (currentBatchComplete && state.batches) {
4288
+ if (state.currentBatchIndex < state.batches.length - 1) {
4289
+ state.currentBatchIndex++;
4290
+ const nextBatchFieldIds = state.batches[state.currentBatchIndex];
4291
+ const nextBatchFields = state.fields.filter(
4292
+ (f) => nextBatchFieldIds.includes(f.id)
4293
+ );
4294
+ const filledCount = state.fields.filter((f) => f.value).length;
4295
+ const { text: emailText, usage: emailUsage } = await generateBatchEmail(
4296
+ nextBatchFields,
4297
+ state.currentBatchIndex,
4298
+ state.batches.length,
4299
+ {
4300
+ appTitle: state.title,
4301
+ totalFieldCount: state.fields.length,
4302
+ filledFieldCount: filledCount,
4303
+ companyName: context?.companyName
4304
+ },
4305
+ generateText,
4306
+ providerOptions
4307
+ );
4308
+ trackUsage(emailUsage);
4309
+ if (!responseText) {
4310
+ responseText = emailText;
4311
+ } else {
4312
+ responseText += `
4313
+
4314
+ ${emailText}`;
4315
+ }
4316
+ } else {
4317
+ state.status = "confirming";
4318
+ }
4319
+ }
4320
+ state.updatedAt = Date.now();
4321
+ await applicationStore?.save(state);
4322
+ return {
4323
+ state,
4324
+ intent: intent.primaryIntent,
4325
+ fieldsFilled,
4326
+ responseText,
4327
+ tokenUsage: totalUsage
4328
+ };
4329
+ }
4330
+ async function generateCurrentBatchEmail(applicationId, opts) {
4331
+ totalUsage = { inputTokens: 0, outputTokens: 0 };
4332
+ const state = await applicationStore?.get(applicationId);
4333
+ if (!state) throw new Error(`Application ${applicationId} not found`);
4334
+ if (!state.batches?.length) throw new Error("No batches available");
4335
+ const batchFieldIds = state.batches[state.currentBatchIndex];
4336
+ const batchFields = state.fields.filter((f) => batchFieldIds.includes(f.id));
4337
+ const filledCount = state.fields.filter((f) => f.value).length;
4338
+ const { text, usage } = await generateBatchEmail(
4339
+ batchFields,
4340
+ state.currentBatchIndex,
4341
+ state.batches.length,
4342
+ {
4343
+ appTitle: state.title,
4344
+ totalFieldCount: state.fields.length,
4345
+ filledFieldCount: filledCount,
4346
+ companyName: opts?.companyName,
4347
+ previousBatchSummary: opts?.previousBatchSummary
4348
+ },
4349
+ generateText,
4350
+ providerOptions
4351
+ );
4352
+ trackUsage(usage);
4353
+ return { text, tokenUsage: totalUsage };
4354
+ }
4355
+ async function getConfirmationSummary(applicationId) {
4356
+ totalUsage = { inputTokens: 0, outputTokens: 0 };
4357
+ const state = await applicationStore?.get(applicationId);
4358
+ if (!state) throw new Error(`Application ${applicationId} not found`);
4359
+ const filledFields = state.fields.filter((f) => f.value);
4360
+ const fieldSummary = filledFields.map((f) => `${f.section} > ${f.label}: ${f.value} (source: ${f.source ?? "unknown"})`).join("\n");
4361
+ const { text, usage } = await generateText({
4362
+ prompt: `Format these filled insurance application fields as a clean confirmation summary for the user to review. Group by section, show each field as "Label: Value". End with a note asking them to confirm or request changes.
4363
+
4364
+ Application: ${state.title ?? "Insurance Application"}
4365
+
4366
+ Fields:
4367
+ ${fieldSummary}`,
4368
+ maxTokens: 4096,
4369
+ providerOptions
4370
+ });
4371
+ trackUsage(usage);
4372
+ return { text, tokenUsage: totalUsage };
4373
+ }
4374
+ return {
4375
+ processApplication,
4376
+ processReply,
4377
+ generateCurrentBatchEmail,
4378
+ getConfirmationSummary
4379
+ };
4380
+ }
4381
+
4382
+ // src/prompts/application/confirmation.ts
4383
+ function buildConfirmationSummaryPrompt(fields, applicationTitle) {
4384
+ const fieldList = fields.map((f) => {
4385
+ const label = f.label ?? f.text ?? f.id;
4386
+ const value = f.value ?? "(not provided)";
4387
+ return `[${f.section}] ${label}: ${value}`;
4388
+ }).join("\n");
4389
+ return `Format the following insurance application answers into a clean, readable summary grouped by section. This will be sent as an email for the user to review and confirm.
4390
+
4391
+ APPLICATION: ${applicationTitle}
4392
+
4393
+ FIELD VALUES:
4394
+ ${fieldList}
4395
+
4396
+ Format as a readable summary:
4397
+ - Group by section with section headers
4398
+ - Show each field as "Label: Value"
4399
+ - For declarations, show the question and the yes/no answer plus any explanation
4400
+ - Skip fields with no value unless they are required
4401
+ - End with a note asking the user to reply "Looks good" to confirm, or describe any changes needed
4402
+
4403
+ Respond with the formatted summary text only (no JSON wrapper). Use markdown formatting (bold headers, bullet points).`;
4404
+ }
4405
+
4406
+ // src/prompts/application/field-explanation.ts
4407
+ function buildFieldExplanationPrompt(field, question, policyContext) {
4408
+ return `You are an internal risk management assistant helping a colleague fill out an insurance application for your company. They asked a question about a field on the form.
4409
+
4410
+ FIELD: "${field.label}" (type: ${field.fieldType}${field.options ? `, options: ${field.options.join(", ")}` : ""})
4411
+
4412
+ THEIR QUESTION: "${question}"
4413
+
4414
+ ${policyContext ? `RELEVANT POLICY/CONTEXT INFO:
4415
+ ${policyContext}
4416
+ ` : ""}
4417
+
4418
+ Provide a short, helpful explanation (2-3 sentences) as a coworker would. If the field has options, briefly explain what each means if relevant. If there's policy context that helps, cite the specific source (e.g. "According to our GL Policy #ABC123 with Hartford, our current aggregate limit is $2M").
4419
+
4420
+ End with: "Just reply with the answer when you're ready and I'll fill it in."
4421
+
4422
+ Respond with the explanation text only \u2014 no JSON, no field ID, no extra formatting.`;
4423
+ }
4424
+
4425
+ // src/prompts/query/classify.ts
4426
+ function buildQueryClassifyPrompt(question, conversationContext) {
4427
+ return `You are a query classifier for an insurance document intelligence system.
4428
+
4429
+ Analyze the user's question and produce a structured classification.
4430
+
4431
+ USER QUESTION:
4432
+ ${question}
4433
+ ${conversationContext ? `
4434
+ CONVERSATION CONTEXT:
4435
+ ${conversationContext}` : ""}
4436
+
4437
+ INSTRUCTIONS:
4438
+
4439
+ 1. Determine the primary intent:
4440
+ - "policy_question": questions about specific coverage, limits, deductibles, endorsements, conditions
4441
+ - "coverage_comparison": comparing coverages across multiple documents or policies
4442
+ - "document_search": looking for a specific document by carrier, policy number, insured name
4443
+ - "claims_inquiry": questions about claims history, loss runs, experience modification
4444
+ - "general_knowledge": insurance concepts not tied to a specific document
4445
+
4446
+ 2. Decompose into atomic sub-questions:
4447
+ - Each sub-question should be answerable from a single retrieval pass
4448
+ - Simple questions produce exactly one sub-question (the question itself)
4449
+ - Complex questions (comparisons, multi-policy, multi-field) decompose into 2-5 sub-questions
4450
+ - Each sub-question should specify which chunk types are most relevant
4451
+
4452
+ 3. Determine which storage backends are needed:
4453
+ - requiresDocumentLookup: true if a specific document needs to be fetched by ID/number/carrier
4454
+ - requiresChunkSearch: true if semantic search over document chunks is needed
4455
+ - requiresConversationHistory: true if the question references prior conversation
4456
+
4457
+ CHUNK TYPES (for chunkTypes filter):
4458
+ carrier_info, named_insured, coverage, endorsement, exclusion, condition, section, declaration, loss_history, premium, supplementary
4459
+
4460
+ Respond with the structured classification.`;
4461
+ }
4462
+
4463
+ // src/prompts/query/respond.ts
4464
+ function buildRespondPrompt(originalQuestion, subAnswersJson, platform) {
4465
+ const formatGuidance = platform === "email" ? "Format as a professional email response. Use plain text, no markdown." : platform === "sms" ? "Keep the response concise and conversational. No markdown." : "Format as clear, well-structured text. Use markdown for lists and emphasis where helpful.";
4466
+ return `You are composing a final answer to an insurance question. You have verified sub-answers with citations that you need to merge into a single, natural response.
4467
+
4468
+ ORIGINAL QUESTION:
4469
+ ${originalQuestion}
4470
+
4471
+ VERIFIED SUB-ANSWERS:
4472
+ ${subAnswersJson}
4473
+
4474
+ FORMATTING:
4475
+ ${formatGuidance}
4476
+
4477
+ INSTRUCTIONS:
4478
+ 1. Write a natural, direct answer to the original question.
4479
+ 2. Embed inline citation numbers [1], [2], etc. after each factual claim. These reference the citation objects from the sub-answers \u2014 preserve the original citation index numbers.
4480
+ 3. If any sub-answer had low confidence or noted missing context, mention what information was unavailable rather than omitting silently.
4481
+ 4. If the answer naturally leads to a follow-up question the user might want to ask, suggest it in the followUp field.
4482
+ 5. Merge overlapping citations \u2014 if two sub-answers cite the same chunk, use one citation number.
4483
+ 6. Keep the tone helpful and professional.
4484
+
4485
+ Respond with the final answer, deduplicated citations array, overall confidence (weighted average of sub-answer confidences), and an optional follow-up suggestion.`;
4486
+ }
4487
+
4488
+ // src/schemas/query.ts
4489
+ var import_zod32 = require("zod");
4490
+ var QueryIntentSchema = import_zod32.z.enum([
4491
+ "policy_question",
4492
+ "coverage_comparison",
4493
+ "document_search",
4494
+ "claims_inquiry",
4495
+ "general_knowledge"
4496
+ ]);
4497
+ var SubQuestionSchema = import_zod32.z.object({
4498
+ question: import_zod32.z.string().describe("Atomic sub-question to retrieve and answer independently"),
4499
+ intent: QueryIntentSchema,
4500
+ chunkTypes: import_zod32.z.array(import_zod32.z.string()).optional().describe("Chunk types to filter retrieval (e.g. coverage, endorsement, declaration)"),
4501
+ documentFilters: import_zod32.z.object({
4502
+ type: import_zod32.z.enum(["policy", "quote"]).optional(),
4503
+ carrier: import_zod32.z.string().optional(),
4504
+ insuredName: import_zod32.z.string().optional(),
4505
+ policyNumber: import_zod32.z.string().optional(),
4506
+ quoteNumber: import_zod32.z.string().optional()
4507
+ }).optional().describe("Structured filters to narrow document lookup")
4508
+ });
4509
+ var QueryClassifyResultSchema = import_zod32.z.object({
4510
+ intent: QueryIntentSchema,
4511
+ subQuestions: import_zod32.z.array(SubQuestionSchema).min(1).describe("Decomposed atomic sub-questions"),
4512
+ requiresDocumentLookup: import_zod32.z.boolean().describe("Whether structured document lookup is needed"),
4513
+ requiresChunkSearch: import_zod32.z.boolean().describe("Whether semantic chunk search is needed"),
4514
+ requiresConversationHistory: import_zod32.z.boolean().describe("Whether conversation history is relevant")
4515
+ });
4516
+ var EvidenceItemSchema = import_zod32.z.object({
4517
+ source: import_zod32.z.enum(["chunk", "document", "conversation"]),
4518
+ chunkId: import_zod32.z.string().optional(),
4519
+ documentId: import_zod32.z.string().optional(),
4520
+ turnId: import_zod32.z.string().optional(),
4521
+ text: import_zod32.z.string().describe("Text excerpt from the source"),
4522
+ relevance: import_zod32.z.number().min(0).max(1),
4523
+ metadata: import_zod32.z.record(import_zod32.z.string(), import_zod32.z.string()).optional()
4524
+ });
4525
+ var RetrievalResultSchema = import_zod32.z.object({
4526
+ subQuestion: import_zod32.z.string(),
4527
+ evidence: import_zod32.z.array(EvidenceItemSchema)
4528
+ });
4529
+ var CitationSchema = import_zod32.z.object({
4530
+ index: import_zod32.z.number().describe("Citation number [1], [2], etc."),
4531
+ chunkId: import_zod32.z.string().describe("Source chunk ID, e.g. doc-123:coverage:2"),
4532
+ documentId: import_zod32.z.string(),
4533
+ documentType: import_zod32.z.enum(["policy", "quote"]).optional(),
4534
+ field: import_zod32.z.string().optional().describe("Specific field path, e.g. coverages[0].deductible"),
4535
+ quote: import_zod32.z.string().describe("Exact text from source that supports the claim"),
4536
+ relevance: import_zod32.z.number().min(0).max(1)
4537
+ });
4538
+ var SubAnswerSchema = import_zod32.z.object({
4539
+ subQuestion: import_zod32.z.string(),
4540
+ answer: import_zod32.z.string(),
4541
+ citations: import_zod32.z.array(CitationSchema),
4542
+ confidence: import_zod32.z.number().min(0).max(1),
4543
+ needsMoreContext: import_zod32.z.boolean().describe("True if evidence was insufficient to answer fully")
4544
+ });
4545
+ var VerifyResultSchema = import_zod32.z.object({
4546
+ approved: import_zod32.z.boolean().describe("Whether all sub-answers are adequately grounded"),
4547
+ issues: import_zod32.z.array(import_zod32.z.string()).describe("Specific grounding or consistency issues found"),
4548
+ retrySubQuestions: import_zod32.z.array(import_zod32.z.string()).optional().describe("Sub-questions that need additional retrieval or re-reasoning")
4549
+ });
4550
+ var QueryResultSchema = import_zod32.z.object({
4551
+ answer: import_zod32.z.string(),
4552
+ citations: import_zod32.z.array(CitationSchema),
4553
+ intent: QueryIntentSchema,
4554
+ confidence: import_zod32.z.number().min(0).max(1),
4555
+ followUp: import_zod32.z.string().optional().describe("Suggested follow-up question if applicable")
4556
+ });
4557
+
4558
+ // src/query/retriever.ts
4559
+ async function retrieve(subQuestion, conversationId, config) {
4560
+ const { documentStore, memoryStore, retrievalLimit, log } = config;
4561
+ const evidence = [];
4562
+ const tasks = [];
4563
+ tasks.push(
4564
+ (async () => {
4565
+ try {
4566
+ const filter = {};
4567
+ if (subQuestion.chunkTypes?.length) {
4568
+ const chunkResults = await Promise.all(
4569
+ subQuestion.chunkTypes.map(
4570
+ (type) => memoryStore.search(subQuestion.question, {
4571
+ limit: Math.ceil(retrievalLimit / subQuestion.chunkTypes.length),
4572
+ filter: { ...filter, type }
4573
+ })
4574
+ )
4575
+ );
4576
+ for (const chunks of chunkResults) {
4577
+ for (const chunk of chunks) {
4578
+ evidence.push({
4579
+ source: "chunk",
4580
+ chunkId: chunk.id,
4581
+ documentId: chunk.documentId,
4582
+ text: chunk.text,
4583
+ relevance: 0.8,
4584
+ // Default — store doesn't expose scores directly
4585
+ metadata: chunk.metadata
4586
+ });
4587
+ }
4588
+ }
4589
+ } else {
4590
+ const chunks = await memoryStore.search(subQuestion.question, {
4591
+ limit: retrievalLimit
4592
+ });
4593
+ for (const chunk of chunks) {
4594
+ evidence.push({
4595
+ source: "chunk",
4596
+ chunkId: chunk.id,
4597
+ documentId: chunk.documentId,
4598
+ text: chunk.text,
4599
+ relevance: 0.8,
4600
+ metadata: chunk.metadata
4601
+ });
4602
+ }
4603
+ }
4604
+ } catch (e) {
4605
+ await log?.(`Chunk search failed for "${subQuestion.question}": ${e}`);
4606
+ }
4607
+ })()
4608
+ );
4609
+ if (subQuestion.documentFilters) {
4610
+ tasks.push(
4611
+ (async () => {
4612
+ try {
4613
+ const filters = {};
4614
+ if (subQuestion.documentFilters?.type) filters.type = subQuestion.documentFilters.type;
4615
+ if (subQuestion.documentFilters?.carrier) filters.carrier = subQuestion.documentFilters.carrier;
4616
+ if (subQuestion.documentFilters?.insuredName) filters.insuredName = subQuestion.documentFilters.insuredName;
4617
+ if (subQuestion.documentFilters?.policyNumber) filters.policyNumber = subQuestion.documentFilters.policyNumber;
4618
+ if (subQuestion.documentFilters?.quoteNumber) filters.quoteNumber = subQuestion.documentFilters.quoteNumber;
4619
+ const docs = await documentStore.query(filters);
4620
+ for (const doc of docs) {
4621
+ const summary = buildDocumentSummary(doc);
4622
+ evidence.push({
4623
+ source: "document",
4624
+ documentId: doc.id,
4625
+ text: summary,
4626
+ relevance: 0.9,
4627
+ // Direct lookup is high relevance
4628
+ metadata: {
4629
+ type: doc.type,
4630
+ carrier: doc.carrier ?? "",
4631
+ insuredName: doc.insuredName ?? ""
4632
+ }
4633
+ });
4634
+ }
4635
+ } catch (e) {
4636
+ await log?.(`Document lookup failed: ${e}`);
4637
+ }
4638
+ })()
4639
+ );
4640
+ }
4641
+ if (conversationId) {
4642
+ tasks.push(
4643
+ (async () => {
4644
+ try {
4645
+ const turns = await memoryStore.searchHistory(
4646
+ subQuestion.question,
4647
+ conversationId
4648
+ );
4649
+ for (const turn of turns.slice(0, 5)) {
4650
+ evidence.push({
4651
+ source: "conversation",
4652
+ turnId: turn.id,
4653
+ text: `[${turn.role}]: ${turn.content}`,
4654
+ relevance: 0.6
4655
+ // Conversation context is lower relevance than documents
4656
+ });
4657
+ }
4658
+ } catch (e) {
4659
+ await log?.(`Conversation history search failed: ${e}`);
4660
+ }
4661
+ })()
4662
+ );
4663
+ }
4664
+ await Promise.all(tasks);
4665
+ evidence.sort((a, b) => b.relevance - a.relevance);
4666
+ return {
4667
+ subQuestion: subQuestion.question,
4668
+ evidence: evidence.slice(0, retrievalLimit)
4669
+ };
4670
+ }
4671
+ function buildDocumentSummary(doc) {
4672
+ const parts = [];
4673
+ const type = doc.type;
4674
+ parts.push(`Document type: ${type}`);
4675
+ if (doc.carrier) parts.push(`Carrier: ${doc.carrier}`);
4676
+ if (doc.insuredName) parts.push(`Insured: ${doc.insuredName}`);
4677
+ if (type === "policy") {
4678
+ if (doc.policyNumber) parts.push(`Policy #: ${doc.policyNumber}`);
4679
+ if (doc.effectiveDate) parts.push(`Effective: ${doc.effectiveDate}`);
4680
+ if (doc.expirationDate) parts.push(`Expiration: ${doc.expirationDate}`);
4681
+ } else if (type === "quote") {
4682
+ if (doc.quoteNumber) parts.push(`Quote #: ${doc.quoteNumber}`);
4683
+ if (doc.proposedEffectiveDate) parts.push(`Proposed effective: ${doc.proposedEffectiveDate}`);
4684
+ }
4685
+ if (doc.premium) parts.push(`Premium: ${doc.premium}`);
4686
+ const coverages = doc.coverages;
4687
+ if (coverages?.length) {
4688
+ parts.push(`Coverages (${coverages.length}):`);
4689
+ for (const cov of coverages.slice(0, 10)) {
4690
+ const line = [cov.name, cov.limit ? `Limit: ${cov.limit}` : null, cov.deductible ? `Ded: ${cov.deductible}` : null].filter(Boolean).join(" | ");
4691
+ parts.push(` - ${line}`);
4692
+ }
4693
+ }
4694
+ return parts.join("\n");
4695
+ }
4696
+
4697
+ // src/prompts/query/reason.ts
4698
+ var INTENT_INSTRUCTIONS = {
4699
+ policy_question: `You are answering a question about a specific insurance policy or quote.
4700
+
4701
+ RULES:
4702
+ - Answer ONLY from the evidence provided. Do not use general knowledge.
4703
+ - When citing limits, deductibles, or amounts, use the exact values from the source.
4704
+ - If the evidence mentions an endorsement that modifies coverage, include that context.
4705
+ - If the evidence is insufficient, say what is missing rather than guessing.
4706
+ - Reference specific coverage names, form numbers, and endorsement titles when available.`,
4707
+ coverage_comparison: `You are comparing coverages across insurance documents.
4708
+
4709
+ RULES:
4710
+ - Answer ONLY from the evidence provided.
4711
+ - Structure your comparison around specific coverage attributes: limits, deductibles, forms, triggers.
4712
+ - Note differences clearly: "Policy A has X, while Policy B has Y."
4713
+ - Flag where one document has coverage the other lacks entirely.
4714
+ - If evidence for one side of the comparison is missing, state that explicitly.`,
4715
+ document_search: `You are helping locate a specific insurance document.
4716
+
4717
+ RULES:
4718
+ - Answer ONLY from the evidence provided.
4719
+ - Identify the document by carrier, policy/quote number, insured name, and effective dates.
4720
+ - If multiple documents match, list them with distinguishing details.
4721
+ - If no documents match, say so clearly.`,
4722
+ claims_inquiry: `You are answering a question about claims history or loss experience.
4723
+
4724
+ RULES:
4725
+ - Answer ONLY from the evidence provided.
4726
+ - Reference specific claim dates, amounts, descriptions, and statuses.
4727
+ - Include experience modification factors if available.
4728
+ - Be precise with dollar amounts and dates \u2014 do not approximate.
4729
+ - If the evidence shows no claims, state that explicitly.`,
4730
+ general_knowledge: `You are answering a general insurance question using available document context.
4731
+
4732
+ RULES:
4733
+ - You may use general insurance knowledge to frame your answer.
4734
+ - If the question can be answered from the evidence, prefer that over general knowledge.
4735
+ - When mixing general knowledge with document-specific data, make the distinction clear.
4736
+ - Still cite evidence when referencing specific documents.`
4737
+ };
4738
+ function buildReasonPrompt(subQuestion, intent, evidence) {
4739
+ return `${INTENT_INSTRUCTIONS[intent]}
4740
+
4741
+ SUB-QUESTION:
4742
+ ${subQuestion}
4743
+
4744
+ EVIDENCE:
4745
+ ${evidence}
4746
+
4747
+ Answer the sub-question based on the evidence above. For every factual claim, include a citation referencing the source evidence item by its chunkId or documentId. Rate your confidence from 0 to 1 based on how well the evidence supports your answer. Set needsMoreContext to true if the evidence was insufficient.`;
4748
+ }
4749
+
4750
+ // src/query/reasoner.ts
4751
+ async function reason(subQuestion, intent, evidence, config) {
4752
+ const { generateObject, providerOptions } = config;
4753
+ const evidenceText = evidence.map((e, i) => {
4754
+ const sourceLabel = e.source === "chunk" ? `[chunk:${e.chunkId}]` : e.source === "document" ? `[doc:${e.documentId}]` : `[turn:${e.turnId}]`;
4755
+ return `Evidence ${i + 1} ${sourceLabel} (relevance: ${e.relevance.toFixed(2)}):
4756
+ ${e.text}`;
4757
+ }).join("\n\n");
4758
+ const prompt = buildReasonPrompt(subQuestion, intent, evidenceText);
4759
+ const { object, usage } = await withRetry(
4760
+ () => generateObject({
4761
+ prompt,
4762
+ schema: SubAnswerSchema,
4763
+ maxTokens: 4096,
4764
+ providerOptions
4765
+ })
4766
+ );
4767
+ return { subAnswer: object, usage };
4768
+ }
4769
+
4770
+ // src/prompts/query/verify.ts
4771
+ function buildVerifyPrompt(originalQuestion, subAnswersJson, evidenceJson) {
4772
+ return `You are a verification agent for an insurance document intelligence system. Your job is to check that answers are accurate, grounded, and complete.
4773
+
4774
+ ORIGINAL QUESTION:
4775
+ ${originalQuestion}
4776
+
4777
+ SUB-ANSWERS:
4778
+ ${subAnswersJson}
4779
+
4780
+ AVAILABLE EVIDENCE:
4781
+ ${evidenceJson}
4782
+
4783
+ CHECK EACH SUB-ANSWER FOR:
4784
+
4785
+ 1. GROUNDING: Every factual claim must be supported by a citation that references actual evidence. Flag any claim that:
4786
+ - Has no citation
4787
+ - Cites a source that doesn't actually contain the claimed information
4788
+ - Extrapolates beyond what the evidence states
4789
+
4790
+ 2. CONSISTENCY: Sub-answers should not contradict each other. Flag any contradictions, noting which sub-answers conflict and what the discrepancy is.
4791
+
4792
+ 3. COMPLETENESS: Did each sub-question get an adequate answer? Flag any sub-question where:
4793
+ - The answer is vague or hedged when the evidence supports a specific answer
4794
+ - Important details from the evidence were omitted
4795
+ - The confidence rating seems miscalibrated (high confidence with weak evidence, or low confidence with strong evidence)
4796
+
4797
+ RESPOND WITH:
4798
+ - approved: true only if ALL sub-answers pass all three checks
4799
+ - issues: list every specific issue found (empty array if approved)
4800
+ - retrySubQuestions: sub-questions that need re-retrieval or re-reasoning (only if not approved)`;
4801
+ }
4802
+
4803
+ // src/query/verifier.ts
4804
+ async function verify(originalQuestion, subAnswers, allEvidence, config) {
4805
+ const { generateObject, providerOptions } = config;
4806
+ const subAnswersJson = JSON.stringify(
4807
+ subAnswers.map((sa) => ({
4808
+ subQuestion: sa.subQuestion,
4809
+ answer: sa.answer,
4810
+ citations: sa.citations,
4811
+ confidence: sa.confidence,
4812
+ needsMoreContext: sa.needsMoreContext
4813
+ })),
4814
+ null,
4815
+ 2
4816
+ );
4817
+ const evidenceJson = JSON.stringify(
4818
+ allEvidence.map((e) => ({
4819
+ source: e.source,
4820
+ id: e.chunkId ?? e.documentId ?? e.turnId,
4821
+ text: e.text.slice(0, 500),
4822
+ // Truncate for context efficiency
4823
+ relevance: e.relevance
4824
+ })),
4825
+ null,
4826
+ 2
4827
+ );
4828
+ const prompt = buildVerifyPrompt(originalQuestion, subAnswersJson, evidenceJson);
4829
+ const { object, usage } = await withRetry(
4830
+ () => generateObject({
4831
+ prompt,
4832
+ schema: VerifyResultSchema,
4833
+ maxTokens: 2048,
4834
+ providerOptions
4835
+ })
4836
+ );
4837
+ return { result: object, usage };
4838
+ }
4839
+
4840
+ // src/query/coordinator.ts
4841
+ function createQueryAgent(config) {
4842
+ const {
4843
+ generateText,
4844
+ generateObject,
4845
+ documentStore,
4846
+ memoryStore,
4847
+ concurrency = 3,
4848
+ maxVerifyRounds = 1,
4849
+ retrievalLimit = 10,
4850
+ onTokenUsage,
4851
+ onProgress,
4852
+ log,
4853
+ providerOptions
4854
+ } = config;
4855
+ const limit = pLimit(concurrency);
4856
+ let totalUsage = { inputTokens: 0, outputTokens: 0 };
4857
+ function trackUsage(usage) {
4858
+ if (usage) {
4859
+ totalUsage.inputTokens += usage.inputTokens;
4860
+ totalUsage.outputTokens += usage.outputTokens;
4861
+ onTokenUsage?.(usage);
4862
+ }
4863
+ }
4864
+ async function query(input) {
4865
+ totalUsage = { inputTokens: 0, outputTokens: 0 };
4866
+ const { question, conversationId, context } = input;
4867
+ onProgress?.("Classifying query...");
4868
+ const classification = await classify(question, conversationId);
4869
+ onProgress?.(`Retrieving evidence for ${classification.subQuestions.length} sub-question(s)...`);
4870
+ const retrieverConfig = {
4871
+ documentStore,
4872
+ memoryStore,
4873
+ retrievalLimit,
4874
+ log
4875
+ };
4876
+ const retrievalResults = await Promise.all(
4877
+ classification.subQuestions.map(
4878
+ (sq) => limit(() => retrieve(sq, conversationId, retrieverConfig))
4879
+ )
4880
+ );
4881
+ const allEvidence = retrievalResults.flatMap((r) => r.evidence);
4882
+ onProgress?.("Reasoning over evidence...");
4883
+ const reasonerConfig = { generateObject, providerOptions };
4884
+ let subAnswers = await Promise.all(
4885
+ classification.subQuestions.map(
4886
+ (sq, i) => limit(async () => {
4887
+ const { subAnswer, usage } = await reason(
4888
+ sq.question,
4889
+ sq.intent,
4890
+ retrievalResults[i].evidence,
4891
+ reasonerConfig
4892
+ );
4893
+ trackUsage(usage);
4894
+ return subAnswer;
4895
+ })
4896
+ )
4897
+ );
4898
+ onProgress?.("Verifying answer grounding...");
4899
+ const verifierConfig = { generateObject, providerOptions };
4900
+ for (let round = 0; round < maxVerifyRounds; round++) {
4901
+ const { result: verifyResult, usage } = await verify(
4902
+ question,
4903
+ subAnswers,
4904
+ allEvidence,
4905
+ verifierConfig
4906
+ );
4907
+ trackUsage(usage);
4908
+ if (verifyResult.approved) {
4909
+ onProgress?.("Verification passed.");
4910
+ break;
4911
+ }
4912
+ onProgress?.(`Verification found ${verifyResult.issues.length} issue(s), round ${round + 1}/${maxVerifyRounds}`);
4913
+ await log?.(`Verify issues: ${verifyResult.issues.join("; ")}`);
4914
+ if (verifyResult.retrySubQuestions?.length) {
4915
+ const retryQuestions = classification.subQuestions.filter(
4916
+ (sq) => verifyResult.retrySubQuestions.includes(sq.question)
4917
+ );
4918
+ if (retryQuestions.length > 0) {
4919
+ const retryRetrievals = await Promise.all(
4920
+ retryQuestions.map(
4921
+ (sq) => limit(
4922
+ () => retrieve(sq, conversationId, {
4923
+ ...retrieverConfig,
4924
+ retrievalLimit: retrievalLimit * 2
4925
+ // Broader retrieval on retry
4926
+ })
4927
+ )
4928
+ )
4929
+ );
4930
+ for (const r of retryRetrievals) {
4931
+ allEvidence.push(...r.evidence);
4932
+ }
4933
+ const retrySubAnswers = await Promise.all(
4934
+ retryQuestions.map(
4935
+ (sq, i) => limit(async () => {
4936
+ const { subAnswer, usage: u } = await reason(
4937
+ sq.question,
4938
+ sq.intent,
4939
+ retryRetrievals[i].evidence,
4940
+ reasonerConfig
4941
+ );
4942
+ trackUsage(u);
4943
+ return subAnswer;
4944
+ })
4945
+ )
4946
+ );
4947
+ const retryQSet = new Set(retryQuestions.map((sq) => sq.question));
4948
+ subAnswers = subAnswers.map((sa) => {
4949
+ if (retryQSet.has(sa.subQuestion)) {
4950
+ const replacement = retrySubAnswers.find((r) => r.subQuestion === sa.subQuestion);
4951
+ return replacement ?? sa;
4952
+ }
4953
+ return sa;
4954
+ });
4955
+ }
4956
+ }
4957
+ }
4958
+ onProgress?.("Composing final answer...");
4959
+ const queryResult = await respond(
4960
+ question,
4961
+ subAnswers,
4962
+ classification,
4963
+ context?.platform
4964
+ );
4965
+ if (conversationId) {
4966
+ try {
4967
+ await memoryStore.addTurn({
4968
+ id: `turn-${Date.now()}-q`,
4969
+ conversationId,
4970
+ role: "user",
4971
+ content: question,
4972
+ timestamp: Date.now()
4973
+ });
4974
+ await memoryStore.addTurn({
4975
+ id: `turn-${Date.now()}-a`,
4976
+ conversationId,
4977
+ role: "assistant",
4978
+ content: queryResult.answer,
4979
+ timestamp: Date.now()
4980
+ });
4981
+ } catch (e) {
4982
+ await log?.(`Failed to store conversation turn: ${e}`);
4983
+ }
4984
+ }
4985
+ return { ...queryResult, tokenUsage: totalUsage };
4986
+ }
4987
+ async function classify(question, conversationId) {
4988
+ let conversationContext;
4989
+ if (conversationId) {
4990
+ try {
4991
+ const history = await memoryStore.getHistory(conversationId, { limit: 5 });
4992
+ if (history.length > 0) {
4993
+ conversationContext = history.map((t) => `[${t.role}]: ${t.content}`).join("\n");
4994
+ }
4995
+ } catch {
4996
+ }
4997
+ }
4998
+ const prompt = buildQueryClassifyPrompt(question, conversationContext);
4999
+ const { object, usage } = await withRetry(
5000
+ () => generateObject({
5001
+ prompt,
5002
+ schema: QueryClassifyResultSchema,
5003
+ maxTokens: 2048,
5004
+ providerOptions
5005
+ })
5006
+ );
5007
+ trackUsage(usage);
5008
+ return object;
5009
+ }
5010
+ async function respond(originalQuestion, subAnswers, classification, platform) {
5011
+ const subAnswersJson = JSON.stringify(
5012
+ subAnswers.map((sa) => ({
5013
+ subQuestion: sa.subQuestion,
5014
+ answer: sa.answer,
5015
+ citations: sa.citations,
5016
+ confidence: sa.confidence,
5017
+ needsMoreContext: sa.needsMoreContext
5018
+ })),
5019
+ null,
5020
+ 2
5021
+ );
5022
+ const prompt = buildRespondPrompt(originalQuestion, subAnswersJson, platform);
5023
+ const { object, usage } = await withRetry(
5024
+ () => generateObject({
5025
+ prompt,
5026
+ schema: QueryResultSchema,
5027
+ maxTokens: 4096,
5028
+ providerOptions
5029
+ })
5030
+ );
5031
+ trackUsage(usage);
5032
+ const result = object;
5033
+ result.intent = classification.intent;
5034
+ return result;
5035
+ }
5036
+ return { query };
5037
+ }
5038
+
3756
5039
  // src/prompts/intent.ts
3757
5040
  function buildClassifyMessagePrompt(platform) {
3758
5041
  const platformFields = {
@@ -3879,9 +5162,16 @@ var AGENT_TOOLS = [
3879
5162
  AGENT_TOOLS,
3880
5163
  APPLICATION_CLASSIFY_PROMPT,
3881
5164
  AUDIT_TYPES,
5165
+ AcroFormMappingSchema,
3882
5166
  AddressSchema,
3883
5167
  AdmittedStatusSchema,
5168
+ AnswerParsingResultSchema,
5169
+ ApplicationClassifyResultSchema,
5170
+ ApplicationFieldSchema,
5171
+ ApplicationStateSchema,
3884
5172
  AuditTypeSchema,
5173
+ AutoFillMatchSchema,
5174
+ AutoFillResultSchema,
3885
5175
  BOAT_TYPES,
3886
5176
  BindingAuthoritySchema,
3887
5177
  BoatTypeSchema,
@@ -3895,6 +5185,7 @@ var AGENT_TOOLS = [
3895
5185
  COVERAGE_FORMS,
3896
5186
  COVERAGE_TRIGGERS,
3897
5187
  ChunkTypeSchema,
5188
+ CitationSchema,
3898
5189
  ClaimRecordSchema,
3899
5190
  ClaimStatusSchema,
3900
5191
  ClassificationCodeSchema,
@@ -3937,12 +5228,16 @@ var AGENT_TOOLS = [
3937
5228
  EnrichedSubjectivitySchema,
3938
5229
  EnrichedUnderwritingConditionSchema,
3939
5230
  EntityTypeSchema,
5231
+ EvidenceItemSchema,
3940
5232
  ExclusionSchema,
3941
5233
  ExperienceModSchema,
3942
5234
  ExtendedReportingPeriodSchema,
3943
5235
  FLOOD_ZONES,
3944
5236
  FOUNDATION_TYPES,
3945
5237
  FarmRanchDeclarationsSchema,
5238
+ FieldExtractionResultSchema,
5239
+ FieldTypeSchema,
5240
+ FlatPdfPlacementSchema,
3946
5241
  FloodDeclarationsSchema,
3947
5242
  FloodZoneSchema,
3948
5243
  FormReferenceSchema,
@@ -3961,6 +5256,9 @@ var AGENT_TOOLS = [
3961
5256
  LimitScheduleSchema,
3962
5257
  LimitTypeSchema,
3963
5258
  LocationPremiumSchema,
5259
+ LookupFillResultSchema,
5260
+ LookupFillSchema,
5261
+ LookupRequestSchema,
3964
5262
  LossSettlementSchema,
3965
5263
  LossSummarySchema,
3966
5264
  NamedInsuredSchema,
@@ -3970,6 +5268,7 @@ var AGENT_TOOLS = [
3970
5268
  POLICY_SECTION_TYPES,
3971
5269
  POLICY_TERM_TYPES,
3972
5270
  POLICY_TYPES,
5271
+ ParsedAnswerSchema,
3973
5272
  PaymentInstallmentSchema,
3974
5273
  PaymentPlanSchema,
3975
5274
  PersonalArticlesDeclarationsSchema,
@@ -3989,6 +5288,10 @@ var AGENT_TOOLS = [
3989
5288
  ProducerInfoSchema,
3990
5289
  ProfessionalLiabilityDeclarationsSchema,
3991
5290
  QUOTE_SECTION_TYPES,
5291
+ QueryClassifyResultSchema,
5292
+ QueryIntentSchema,
5293
+ QueryResultSchema,
5294
+ QuestionBatchResultSchema,
3992
5295
  QuoteDocumentSchema,
3993
5296
  QuoteSectionTypeSchema,
3994
5297
  RATING_BASIS_TYPES,
@@ -3998,12 +5301,16 @@ var AGENT_TOOLS = [
3998
5301
  RatingBasisSchema,
3999
5302
  RatingBasisTypeSchema,
4000
5303
  RecreationalVehicleDeclarationsSchema,
5304
+ ReplyIntentSchema,
5305
+ RetrievalResultSchema,
4001
5306
  RoofTypeSchema,
4002
5307
  SCHEDULED_ITEM_CATEGORIES,
4003
5308
  SUBJECTIVITY_CATEGORIES,
4004
5309
  ScheduledItemCategorySchema,
4005
5310
  SectionSchema,
4006
5311
  SharedLimitSchema,
5312
+ SubAnswerSchema,
5313
+ SubQuestionSchema,
4007
5314
  SubjectivityCategorySchema,
4008
5315
  SubjectivitySchema,
4009
5316
  SublimitSchema,
@@ -4020,6 +5327,7 @@ var AGENT_TOOLS = [
4020
5327
  ValuationMethodSchema,
4021
5328
  VehicleCoverageSchema,
4022
5329
  VehicleCoverageTypeSchema,
5330
+ VerifyResultSchema,
4023
5331
  WatercraftDeclarationsSchema,
4024
5332
  WorkersCompDeclarationsSchema,
4025
5333
  buildAcroFormMappingPrompt,
@@ -4039,12 +5347,18 @@ var AGENT_TOOLS = [
4039
5347
  buildIdentityPrompt,
4040
5348
  buildIntentPrompt,
4041
5349
  buildLookupFillPrompt,
5350
+ buildQueryClassifyPrompt,
4042
5351
  buildQuestionBatchPrompt,
4043
5352
  buildQuotesPoliciesPrompt,
5353
+ buildReasonPrompt,
4044
5354
  buildReplyIntentClassificationPrompt,
5355
+ buildRespondPrompt,
4045
5356
  buildSafetyPrompt,
5357
+ buildVerifyPrompt,
4046
5358
  chunkDocument,
5359
+ createApplicationPipeline,
4047
5360
  createExtractor,
5361
+ createQueryAgent,
4048
5362
  extractPageRange,
4049
5363
  fillAcroForm,
4050
5364
  getAcroFormFields,