@apart-tech/intelligence-core 1.17.3 → 1.19.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.
Files changed (47) hide show
  1. package/dist/auth/ability.d.ts +1 -1
  2. package/dist/auth/ability.d.ts.map +1 -1
  3. package/dist/auth/ability.js +19 -0
  4. package/dist/auth/ability.js.map +1 -1
  5. package/dist/auth/ability.test.js +58 -4
  6. package/dist/auth/ability.test.js.map +1 -1
  7. package/dist/db/tenant.d.ts.map +1 -1
  8. package/dist/db/tenant.js +10 -0
  9. package/dist/db/tenant.js.map +1 -1
  10. package/dist/index.d.ts +13 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +7 -0
  13. package/dist/index.js.map +1 -1
  14. package/dist/services/__tests__/agent-instruction-service.test.d.ts +2 -0
  15. package/dist/services/__tests__/agent-instruction-service.test.d.ts.map +1 -0
  16. package/dist/services/__tests__/agent-instruction-service.test.js +408 -0
  17. package/dist/services/__tests__/agent-instruction-service.test.js.map +1 -0
  18. package/dist/services/agent-instruction-service.d.ts +82 -0
  19. package/dist/services/agent-instruction-service.d.ts.map +1 -0
  20. package/dist/services/agent-instruction-service.js +249 -0
  21. package/dist/services/agent-instruction-service.js.map +1 -0
  22. package/dist/services/agent-run-service.d.ts +61 -0
  23. package/dist/services/agent-run-service.d.ts.map +1 -1
  24. package/dist/services/agent-run-service.js +72 -0
  25. package/dist/services/agent-run-service.js.map +1 -1
  26. package/dist/services/chat-context-service.d.ts +33 -0
  27. package/dist/services/chat-context-service.d.ts.map +1 -0
  28. package/dist/services/chat-context-service.js +110 -0
  29. package/dist/services/chat-context-service.js.map +1 -0
  30. package/dist/services/chat-service.d.ts +83 -0
  31. package/dist/services/chat-service.d.ts.map +1 -0
  32. package/dist/services/chat-service.js +103 -0
  33. package/dist/services/chat-service.js.map +1 -0
  34. package/dist/services/chat-usage-service.d.ts +36 -0
  35. package/dist/services/chat-usage-service.d.ts.map +1 -0
  36. package/dist/services/chat-usage-service.js +89 -0
  37. package/dist/services/chat-usage-service.js.map +1 -0
  38. package/dist/services/llm-router-service.d.ts +29 -0
  39. package/dist/services/llm-router-service.d.ts.map +1 -0
  40. package/dist/services/llm-router-service.js +70 -0
  41. package/dist/services/llm-router-service.js.map +1 -0
  42. package/dist/services/org-llm-provider-service.d.ts +47 -0
  43. package/dist/services/org-llm-provider-service.d.ts.map +1 -0
  44. package/dist/services/org-llm-provider-service.js +126 -0
  45. package/dist/services/org-llm-provider-service.js.map +1 -0
  46. package/package.json +1 -1
  47. package/prisma/schema.prisma +317 -162
@@ -20,30 +20,36 @@ model Organization {
20
20
  stripeSubscriptionId String? @map("stripe_subscription_id") @db.VarChar(255)
21
21
  currentPeriodEnd DateTime? @map("current_period_end") @db.Timestamptz
22
22
 
23
- nodes Node[]
24
- edges Edge[]
25
- domains Domain[]
26
- apiKeys ApiKey[]
27
- workspaces Workspace[]
28
- embeddingConfig OrgEmbeddingConfig?
29
- piiConfig OrgPiiConfig?
30
- agentConfig OrgAgentConfig?
31
- agentRuns AgentRun[]
32
- agentSchedules AgentSchedule[]
33
- memberships Membership[]
34
- invites Invite[]
35
- orgAgentTypes OrgAgentType[]
36
- orgMcpServers OrgMcpServer[]
37
- usageRecords UsageRecord[]
38
- quotaOverrides QuotaOverride[]
39
- onboardingPackages OnboardingPackage[]
40
- billingEvents BillingEvent[]
41
- searchMetrics SearchMetric[]
42
- tagDefinitions TagDefinition[]
43
- nodeTags NodeTag[]
44
- nodeChunks NodeChunk[]
45
- piiQueryLogs PiiQueryLog[]
46
- documents Document[]
23
+ nodes Node[]
24
+ edges Edge[]
25
+ domains Domain[]
26
+ apiKeys ApiKey[]
27
+ workspaces Workspace[]
28
+ embeddingConfig OrgEmbeddingConfig?
29
+ piiConfig OrgPiiConfig?
30
+ agentConfig OrgAgentConfig?
31
+ agentRuns AgentRun[]
32
+ agentSchedules AgentSchedule[]
33
+ memberships Membership[]
34
+ invites Invite[]
35
+ orgAgentTypes OrgAgentType[]
36
+ orgMcpServers OrgMcpServer[]
37
+ usageRecords UsageRecord[]
38
+ quotaOverrides QuotaOverride[]
39
+ onboardingPackages OnboardingPackage[]
40
+ billingEvents BillingEvent[]
41
+ searchMetrics SearchMetric[]
42
+ agentInstructions AgentInstruction[]
43
+ tagDefinitions TagDefinition[]
44
+ nodeTags NodeTag[]
45
+ nodeChunks NodeChunk[]
46
+ piiQueryLogs PiiQueryLog[]
47
+ documents Document[]
48
+ llmProviders OrgLlmProvider[]
49
+ chatSessions ChatSession[]
50
+ chatMessages ChatMessage[]
51
+ chatGuidelines OrgChatGuideline[]
52
+ priorityDocuments OrgPriorityDocument[]
47
53
 
48
54
  @@map("organizations")
49
55
  }
@@ -173,22 +179,22 @@ model Domain {
173
179
  }
174
180
 
175
181
  model Node {
176
- id String @id @default(uuid()) @db.Uuid
177
- type String @db.VarChar(100)
178
- title String @db.VarChar(500)
179
- content String @default("") @db.Text
180
- embedding Unsupported("vector")?
181
- searchVector Unsupported("tsvector")? @map("search_vector")
182
- metadata Json @default("{}")
183
- status String @default("draft") @db.VarChar(20)
184
- createdBy String @map("created_by") @db.VarChar(255)
185
- createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
186
- updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamptz
187
- version Int @default(1)
188
- hasPii Boolean @default(false) @map("has_pii")
189
- embeddingPiiMode String? @map("embedding_pii_mode") @db.VarChar(20)
190
- domainId String? @map("domain_id") @db.Uuid
191
- organizationId String @map("organization_id") @db.Uuid
182
+ id String @id @default(uuid()) @db.Uuid
183
+ type String @db.VarChar(100)
184
+ title String @db.VarChar(500)
185
+ content String @default("") @db.Text
186
+ embedding Unsupported("vector")?
187
+ searchVector Unsupported("tsvector")? @map("search_vector")
188
+ metadata Json @default("{}")
189
+ status String @default("draft") @db.VarChar(20)
190
+ createdBy String @map("created_by") @db.VarChar(255)
191
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
192
+ updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamptz
193
+ version Int @default(1)
194
+ hasPii Boolean @default(false) @map("has_pii")
195
+ embeddingPiiMode String? @map("embedding_pii_mode") @db.VarChar(20)
196
+ domainId String? @map("domain_id") @db.Uuid
197
+ organizationId String @map("organization_id") @db.Uuid
192
198
 
193
199
  sourceEdges Edge[] @relation("SourceNode")
194
200
  targetEdges Edge[] @relation("TargetNode")
@@ -207,19 +213,19 @@ model Node {
207
213
  }
208
214
 
209
215
  model NodeChunk {
210
- id String @id @default(uuid()) @db.Uuid
211
- nodeId String @map("node_id") @db.Uuid
212
- chunkIndex Int @map("chunk_index")
213
- content String @db.Text
214
- tokenCount Int @map("token_count")
215
- headingContext String? @map("heading_context") @db.VarChar(500)
216
- startOffset Int @map("start_offset")
217
- endOffset Int @map("end_offset")
218
- overlapPrev Int @default(0) @map("overlap_prev")
216
+ id String @id @default(uuid()) @db.Uuid
217
+ nodeId String @map("node_id") @db.Uuid
218
+ chunkIndex Int @map("chunk_index")
219
+ content String @db.Text
220
+ tokenCount Int @map("token_count")
221
+ headingContext String? @map("heading_context") @db.VarChar(500)
222
+ startOffset Int @map("start_offset")
223
+ endOffset Int @map("end_offset")
224
+ overlapPrev Int @default(0) @map("overlap_prev")
219
225
  embedding Unsupported("vector")?
220
- embeddingPiiMode String? @map("embedding_pii_mode") @db.VarChar(20)
221
- organizationId String @map("organization_id") @db.Uuid
222
- createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
226
+ embeddingPiiMode String? @map("embedding_pii_mode") @db.VarChar(20)
227
+ organizationId String @map("organization_id") @db.Uuid
228
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
223
229
 
224
230
  node Node @relation(fields: [nodeId], references: [id], onDelete: Cascade)
225
231
  organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
@@ -326,11 +332,12 @@ model OrgAgentType {
326
332
  secretEnvMapping Json @default("{}") @map("secret_env_mapping")
327
333
  dynamicSecrets String[] @default([]) @map("dynamic_secrets")
328
334
 
329
- organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
330
- createdByUser User? @relation("OrgAgentTypeCreatedBy", fields: [createdByUserId], references: [id], onDelete: SetNull)
331
- agentRuns AgentRun[] @relation("AgentRunAgent")
332
- schedules AgentSchedule[] @relation("AgentScheduleAgent")
333
- apiKeys ApiKey[] @relation("ApiKeyAgent")
335
+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
336
+ createdByUser User? @relation("OrgAgentTypeCreatedBy", fields: [createdByUserId], references: [id], onDelete: SetNull)
337
+ agentRuns AgentRun[] @relation("AgentRunAgent")
338
+ schedules AgentSchedule[] @relation("AgentScheduleAgent")
339
+ apiKeys ApiKey[] @relation("ApiKeyAgent")
340
+ instructions AgentInstruction[] @relation("AgentTypeInstructions")
334
341
 
335
342
  @@unique([organizationId, slug], map: "uq_org_agent_type_org_slug")
336
343
  @@index([organizationId], map: "idx_org_agent_types_organization")
@@ -376,13 +383,21 @@ model AgentRun {
376
383
  parentAgentRunId String? @map("parent_agent_run_id") @db.Uuid
377
384
  actClaim Json? @map("act_claim")
378
385
  capturedAbility Json? @map("captured_ability")
379
-
380
- organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
381
- schedule AgentSchedule? @relation(fields: [scheduleId], references: [id], onDelete: SetNull)
382
- agent OrgAgentType? @relation("AgentRunAgent", fields: [agentId], references: [id], onDelete: SetNull)
383
- invokedByUser User? @relation("AgentRunInvokedBy", fields: [invokedByUserId], references: [id], onDelete: SetNull)
384
- parent AgentRun? @relation("AgentRunParent", fields: [parentAgentRunId], references: [id], onDelete: SetNull)
385
- children AgentRun[] @relation("AgentRunParent")
386
+ instructionId String? @map("instruction_id") @db.Uuid
387
+ reasoningTrace Json? @map("reasoning_trace")
388
+ toolsCalled Json? @map("tools_called")
389
+ outcome Json?
390
+ confidence Decimal? @db.Decimal
391
+ taskType String? @map("task_type") @db.VarChar(100)
392
+ outcomeStatus String? @map("outcome_status") @db.VarChar(30)
393
+
394
+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
395
+ schedule AgentSchedule? @relation(fields: [scheduleId], references: [id], onDelete: SetNull)
396
+ agent OrgAgentType? @relation("AgentRunAgent", fields: [agentId], references: [id], onDelete: SetNull)
397
+ invokedByUser User? @relation("AgentRunInvokedBy", fields: [invokedByUserId], references: [id], onDelete: SetNull)
398
+ parent AgentRun? @relation("AgentRunParent", fields: [parentAgentRunId], references: [id], onDelete: SetNull)
399
+ children AgentRun[] @relation("AgentRunParent")
400
+ instruction AgentInstruction? @relation(fields: [instructionId], references: [id], onDelete: SetNull)
386
401
 
387
402
  @@index([organizationId], map: "idx_agent_runs_organization")
388
403
  @@index([status], map: "idx_agent_runs_status")
@@ -391,9 +406,46 @@ model AgentRun {
391
406
  @@index([agentId], map: "idx_agent_runs_agent_id")
392
407
  @@index([invokedByUserId], map: "idx_agent_runs_invoked_by_user_id")
393
408
  @@index([parentAgentRunId], map: "idx_agent_runs_parent_agent_run_id")
409
+ @@index([instructionId], map: "idx_agent_runs_instruction")
410
+ @@index([taskType, status], map: "idx_agent_runs_task_type")
394
411
  @@map("agent_runs")
395
412
  }
396
413
 
414
+ model AgentInstruction {
415
+ id String @id @default(uuid()) @db.Uuid
416
+ organizationId String @map("organization_id") @db.Uuid
417
+ agentTypeId String? @map("agent_type_id") @db.Uuid
418
+ taskType String? @map("task_type") @db.VarChar(100)
419
+ version String @db.VarChar(20)
420
+ status String @default("draft") @db.VarChar(20)
421
+ systemPrompt String @default("") @map("system_prompt") @db.Text
422
+ tools Json @default("[]")
423
+ guardrails Json @default("{}")
424
+ modelConfig Json @default("{}") @map("model_config")
425
+ contentHash String? @map("content_hash") @db.VarChar(64)
426
+ parentVersionId String? @map("parent_version_id") @db.Uuid
427
+ changeNote String? @map("change_note") @db.Text
428
+ publishedAt DateTime? @map("published_at") @db.Timestamptz
429
+ deprecatedAt DateTime? @map("deprecated_at") @db.Timestamptz
430
+ effectiveFrom DateTime? @map("effective_from") @db.Timestamptz
431
+ effectiveTo DateTime? @map("effective_to") @db.Timestamptz
432
+ createdBy String @map("created_by") @db.VarChar(255)
433
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
434
+ updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamptz
435
+
436
+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
437
+ agentType OrgAgentType? @relation("AgentTypeInstructions", fields: [agentTypeId], references: [id], onDelete: SetNull)
438
+ parentVersion AgentInstruction? @relation("InstructionVersionChain", fields: [parentVersionId], references: [id], onDelete: SetNull)
439
+ childVersions AgentInstruction[] @relation("InstructionVersionChain")
440
+ agentRuns AgentRun[]
441
+
442
+ @@index([organizationId], map: "idx_agent_instructions_org")
443
+ @@index([agentTypeId], map: "idx_agent_instructions_agent_type")
444
+ @@index([organizationId, agentTypeId, taskType, status, publishedAt(sort: Desc)], map: "idx_agent_instructions_org_agent_status")
445
+ @@index([contentHash], map: "idx_agent_instructions_hash")
446
+ @@map("agent_instructions")
447
+ }
448
+
397
449
  model AgentSchedule {
398
450
  id String @id @default(uuid()) @db.Uuid
399
451
  organizationId String @map("organization_id") @db.Uuid
@@ -494,32 +546,32 @@ model UsageRecord {
494
546
  // ── Tag System ──────────────────────────────────────────────────────────────
495
547
 
496
548
  model TagDefinition {
497
- id String @id @default(uuid()) @db.Uuid
498
- organizationId String? @map("organization_id") @db.Uuid
499
- tagName String @map("tag_name") @db.VarChar(100)
500
- tagType String @map("tag_type") @db.VarChar(20)
501
- cardinality String @default("singleton") @db.VarChar(10)
502
- source String @default("extracted") @db.VarChar(20)
503
- status String @default("active") @map("status") @db.VarChar(20)
504
- description String @default("") @db.Text
505
- category String? @map("category") @db.VarChar(50)
506
- labels Json? @map("labels") @db.JsonB
507
- applicableNodeTypes Json @default("[]") @map("applicable_node_types")
508
- requiredForNodeTypes Json @default("[]") @map("required_for_node_types")
509
- enumValues Json? @map("enum_values")
510
- enumVersion Int @default(1) @map("enum_version")
511
- dependsOn Json @default("[]") @map("depends_on")
512
- autoAcceptThreshold Float? @map("auto_accept_threshold") @db.Real
513
- parentDefinitionId String? @map("parent_definition_id") @db.Uuid
514
- replacedBy String? @map("replaced_by") @db.Uuid
515
- deprecatedAt DateTime? @map("deprecated_at") @db.Timestamptz
516
- createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
517
- createdBy String @map("created_by") @db.VarChar(255)
518
-
519
- organization Organization? @relation(fields: [organizationId], references: [id], onDelete: Cascade)
520
- parentDef TagDefinition? @relation("TagDefHierarchy", fields: [parentDefinitionId], references: [id])
549
+ id String @id @default(uuid()) @db.Uuid
550
+ organizationId String? @map("organization_id") @db.Uuid
551
+ tagName String @map("tag_name") @db.VarChar(100)
552
+ tagType String @map("tag_type") @db.VarChar(20)
553
+ cardinality String @default("singleton") @db.VarChar(10)
554
+ source String @default("extracted") @db.VarChar(20)
555
+ status String @default("active") @map("status") @db.VarChar(20)
556
+ description String @default("") @db.Text
557
+ category String? @map("category") @db.VarChar(50)
558
+ labels Json? @map("labels") @db.JsonB
559
+ applicableNodeTypes Json @default("[]") @map("applicable_node_types")
560
+ requiredForNodeTypes Json @default("[]") @map("required_for_node_types")
561
+ enumValues Json? @map("enum_values")
562
+ enumVersion Int @default(1) @map("enum_version")
563
+ dependsOn Json @default("[]") @map("depends_on")
564
+ autoAcceptThreshold Float? @map("auto_accept_threshold") @db.Real
565
+ parentDefinitionId String? @map("parent_definition_id") @db.Uuid
566
+ replacedBy String? @map("replaced_by") @db.Uuid
567
+ deprecatedAt DateTime? @map("deprecated_at") @db.Timestamptz
568
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
569
+ createdBy String @map("created_by") @db.VarChar(255)
570
+
571
+ organization Organization? @relation(fields: [organizationId], references: [id], onDelete: Cascade)
572
+ parentDef TagDefinition? @relation("TagDefHierarchy", fields: [parentDefinitionId], references: [id])
521
573
  childDefs TagDefinition[] @relation("TagDefHierarchy")
522
- replacementDef TagDefinition? @relation("TagDefReplacement", fields: [replacedBy], references: [id])
574
+ replacementDef TagDefinition? @relation("TagDefReplacement", fields: [replacedBy], references: [id])
523
575
  replacedDefs TagDefinition[] @relation("TagDefReplacement")
524
576
  tags NodeTag[]
525
577
 
@@ -530,25 +582,25 @@ model TagDefinition {
530
582
  }
531
583
 
532
584
  model NodeTag {
533
- id String @id @default(uuid()) @db.Uuid
534
- nodeId String @map("node_id") @db.Uuid
535
- organizationId String @map("organization_id") @db.Uuid
536
- tagDefinitionId String @map("tag_definition_id") @db.Uuid
537
- tagName String @map("tag_name") @db.VarChar(100)
538
- isSingleton Boolean @default(false) @map("is_singleton")
539
-
540
- valueText String? @map("value_text") @db.Text
541
- valueNum Float? @map("value_num") @db.DoublePrecision
542
- valueCurrency String? @map("value_currency") @db.Char(3)
543
- valueUnit String? @map("value_unit") @db.VarChar(20)
544
- valueDate DateTime? @map("value_date") @db.Timestamptz
545
- valueRef String? @map("value_ref") @db.Uuid
546
-
547
- textSpan String? @map("text_span") @db.Text
548
- offsetStart Int? @map("offset_start")
549
- offsetEnd Int? @map("offset_end")
550
- contentVersion Int? @map("content_version")
551
- attrs Json @default("{}") @db.JsonB
585
+ id String @id @default(uuid()) @db.Uuid
586
+ nodeId String @map("node_id") @db.Uuid
587
+ organizationId String @map("organization_id") @db.Uuid
588
+ tagDefinitionId String @map("tag_definition_id") @db.Uuid
589
+ tagName String @map("tag_name") @db.VarChar(100)
590
+ isSingleton Boolean @default(false) @map("is_singleton")
591
+
592
+ valueText String? @map("value_text") @db.Text
593
+ valueNum Float? @map("value_num") @db.DoublePrecision
594
+ valueCurrency String? @map("value_currency") @db.Char(3)
595
+ valueUnit String? @map("value_unit") @db.VarChar(20)
596
+ valueDate DateTime? @map("value_date") @db.Timestamptz
597
+ valueRef String? @map("value_ref") @db.Uuid
598
+
599
+ textSpan String? @map("text_span") @db.Text
600
+ offsetStart Int? @map("offset_start")
601
+ offsetEnd Int? @map("offset_end")
602
+ contentVersion Int? @map("content_version")
603
+ attrs Json @default("{}") @db.JsonB
552
604
 
553
605
  taggedBy String @map("tagged_by") @db.VarChar(255)
554
606
  modelId String? @map("model_id") @db.VarChar(100)
@@ -560,14 +612,14 @@ model NodeTag {
560
612
  supersededBy String? @map("superseded_by") @db.Uuid
561
613
  batchId String? @map("batch_id") @db.Uuid
562
614
 
563
- createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
564
- updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamptz
615
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
616
+ updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamptz
565
617
 
566
- node Node @relation(fields: [nodeId], references: [id], onDelete: Cascade)
567
- organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
568
- tagDefinition TagDefinition @relation(fields: [tagDefinitionId], references: [id])
569
- supersedes NodeTag? @relation("TagSupersession", fields: [supersededBy], references: [id])
570
- supersededTags NodeTag[] @relation("TagSupersession")
618
+ node Node @relation(fields: [nodeId], references: [id], onDelete: Cascade)
619
+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
620
+ tagDefinition TagDefinition @relation(fields: [tagDefinitionId], references: [id])
621
+ supersedes NodeTag? @relation("TagSupersession", fields: [supersededBy], references: [id])
622
+ supersededTags NodeTag[] @relation("TagSupersession")
571
623
 
572
624
  @@index([nodeId], map: "idx_node_tags_node")
573
625
  @@index([organizationId, tagName], map: "idx_node_tags_org_name")
@@ -577,25 +629,25 @@ model NodeTag {
577
629
  }
578
630
 
579
631
  model NodeTagHistory {
580
- id String @id @default(uuid()) @db.Uuid
581
- tagId String @map("tag_id") @db.Uuid
582
- nodeId String @map("node_id") @db.Uuid
583
- organizationId String @map("organization_id") @db.Uuid
584
- tagName String @map("tag_name") @db.VarChar(100)
585
-
586
- valueText String? @map("value_text") @db.Text
587
- valueNum Float? @map("value_num") @db.DoublePrecision
588
- valueCurrency String? @map("value_currency") @db.Char(3)
589
- valueUnit String? @map("value_unit") @db.VarChar(20)
590
- valueDate DateTime? @map("value_date") @db.Timestamptz
591
- valueRef String? @map("value_ref") @db.Uuid
592
-
593
- validFrom DateTime @map("valid_from") @db.Timestamptz
594
- validUntil DateTime @map("valid_until") @db.Timestamptz
595
- systemFrom DateTime @map("system_from") @db.Timestamptz
596
- systemTo DateTime? @map("system_to") @db.Timestamptz
597
-
598
- replacedBy String @map("replaced_by") @db.VarChar(255)
632
+ id String @id @default(uuid()) @db.Uuid
633
+ tagId String @map("tag_id") @db.Uuid
634
+ nodeId String @map("node_id") @db.Uuid
635
+ organizationId String @map("organization_id") @db.Uuid
636
+ tagName String @map("tag_name") @db.VarChar(100)
637
+
638
+ valueText String? @map("value_text") @db.Text
639
+ valueNum Float? @map("value_num") @db.DoublePrecision
640
+ valueCurrency String? @map("value_currency") @db.Char(3)
641
+ valueUnit String? @map("value_unit") @db.VarChar(20)
642
+ valueDate DateTime? @map("value_date") @db.Timestamptz
643
+ valueRef String? @map("value_ref") @db.Uuid
644
+
645
+ validFrom DateTime @map("valid_from") @db.Timestamptz
646
+ validUntil DateTime @map("valid_until") @db.Timestamptz
647
+ systemFrom DateTime @map("system_from") @db.Timestamptz
648
+ systemTo DateTime? @map("system_to") @db.Timestamptz
649
+
650
+ replacedBy String @map("replaced_by") @db.VarChar(255)
599
651
 
600
652
  @@index([nodeId, tagName, validFrom], map: "idx_tag_history_node_name_time")
601
653
  @@index([organizationId, tagName, validFrom], map: "idx_tag_history_org_name_time")
@@ -604,16 +656,16 @@ model NodeTagHistory {
604
656
  }
605
657
 
606
658
  model NodeTagAudit {
607
- id String @id @default(uuid()) @db.Uuid
608
- tagId String @map("tag_id") @db.Uuid
609
- nodeId String @map("node_id") @db.Uuid
610
- orgId String @map("organization_id") @db.Uuid
611
- action String @map("action") @db.VarChar(20)
612
- oldValue Json? @map("old_value") @db.JsonB
613
- newValue Json? @map("new_value") @db.JsonB
614
- actorId String @map("actor_id") @db.Uuid
615
- reason String? @db.Text
616
- createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
659
+ id String @id @default(uuid()) @db.Uuid
660
+ tagId String @map("tag_id") @db.Uuid
661
+ nodeId String @map("node_id") @db.Uuid
662
+ orgId String @map("organization_id") @db.Uuid
663
+ action String @map("action") @db.VarChar(20)
664
+ oldValue Json? @map("old_value") @db.JsonB
665
+ newValue Json? @map("new_value") @db.JsonB
666
+ actorId String @map("actor_id") @db.Uuid
667
+ reason String? @db.Text
668
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
617
669
 
618
670
  @@index([tagId], map: "idx_tag_audit_tag")
619
671
  @@index([nodeId], map: "idx_tag_audit_node")
@@ -642,25 +694,25 @@ model QuotaOverride {
642
694
  // ── Document Archive ────────────────────────────────────────────────────────
643
695
 
644
696
  model Document {
645
- id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
646
- organizationId String @map("organization_id") @db.Uuid
647
- fileName String @map("file_name") @db.VarChar(500)
648
- mimeType String @map("mime_type") @db.VarChar(100)
649
- fileSizeBytes Int @map("file_size_bytes")
650
- storagePath String @map("storage_path") @db.VarChar(1000)
651
- contentHash String? @map("content_hash") @db.VarChar(64)
652
- status String @default("uploading") @db.VarChar(20)
653
- ocrModel String? @map("ocr_model") @db.VarChar(100)
654
- ocrTokensIn Int? @map("ocr_tokens_in")
655
- ocrTokensOut Int? @map("ocr_tokens_out")
656
- pageCount Int? @map("page_count")
657
- nodeId String? @map("node_id") @db.Uuid
658
- errorMessage String? @map("error_message")
659
- createdBy String @map("created_by") @db.VarChar(255)
660
- createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
661
- updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
697
+ id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
698
+ organizationId String @map("organization_id") @db.Uuid
699
+ fileName String @map("file_name") @db.VarChar(500)
700
+ mimeType String @map("mime_type") @db.VarChar(100)
701
+ fileSizeBytes Int @map("file_size_bytes")
702
+ storagePath String @map("storage_path") @db.VarChar(1000)
703
+ contentHash String? @map("content_hash") @db.VarChar(64)
704
+ status String @default("uploading") @db.VarChar(20)
705
+ ocrModel String? @map("ocr_model") @db.VarChar(100)
706
+ ocrTokensIn Int? @map("ocr_tokens_in")
707
+ ocrTokensOut Int? @map("ocr_tokens_out")
708
+ pageCount Int? @map("page_count")
709
+ nodeId String? @map("node_id") @db.Uuid
710
+ errorMessage String? @map("error_message")
711
+ createdBy String @map("created_by") @db.VarChar(255)
712
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
713
+ updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
662
714
 
663
- organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
715
+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
664
716
 
665
717
  @@index([organizationId, contentHash], map: "idx_documents_org_hash")
666
718
  @@index([organizationId, status], map: "idx_documents_org_status")
@@ -728,3 +780,106 @@ model SearchMetric {
728
780
  @@index([organizationId, createdAt], map: "idx_search_metrics_org_time")
729
781
  @@map("search_metrics")
730
782
  }
783
+
784
+ // ── Chat Gateway ────────────────────────────────────────────────────────────
785
+
786
+ model OrgLlmProvider {
787
+ id String @id @default(uuid()) @db.Uuid
788
+ organizationId String @map("organization_id") @db.Uuid
789
+ provider String @db.VarChar(30)
790
+ displayName String @map("display_name") @db.VarChar(200)
791
+ encryptedApiKey String @map("encrypted_api_key") @db.Text
792
+ baseUrl String? @map("base_url") @db.VarChar(500)
793
+ models String[]
794
+ defaultModel String? @map("default_model") @db.VarChar(100)
795
+ dataResidency String @default("any") @map("data_residency") @db.VarChar(10)
796
+ enabled Boolean @default(true)
797
+ priority Int @default(0)
798
+ maxTokensPerMin Int? @map("max_tokens_per_min")
799
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
800
+ updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamptz
801
+
802
+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
803
+
804
+ @@unique([organizationId, provider], map: "uq_org_llm_provider_org_provider")
805
+ @@index([organizationId], map: "idx_org_llm_providers_org")
806
+ @@map("org_llm_providers")
807
+ }
808
+
809
+ model ChatSession {
810
+ id String @id @default(uuid()) @db.Uuid
811
+ organizationId String @map("organization_id") @db.Uuid
812
+ userId String @map("user_id") @db.Uuid
813
+ title String? @db.VarChar(500)
814
+ privacy String @default("private") @db.VarChar(20)
815
+ status String @default("active") @db.VarChar(20)
816
+ workspaceId String? @map("workspace_id") @db.Uuid
817
+ graphCaptured Boolean @default(false) @map("graph_captured")
818
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
819
+ updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamptz
820
+
821
+ messages ChatMessage[]
822
+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
823
+
824
+ @@index([organizationId, userId, createdAt], map: "idx_chat_sessions_org_user_time")
825
+ @@index([organizationId, privacy, createdAt], map: "idx_chat_sessions_org_privacy_time")
826
+ @@map("chat_sessions")
827
+ }
828
+
829
+ model ChatMessage {
830
+ id String @id @default(uuid()) @db.Uuid
831
+ sessionId String @map("session_id") @db.Uuid
832
+ organizationId String @map("organization_id") @db.Uuid
833
+ role String @db.VarChar(20)
834
+ content String @db.Text
835
+ contentClean String? @map("content_clean") @db.Text
836
+ model String? @db.VarChar(100)
837
+ providerId String? @map("provider_id") @db.Uuid
838
+ tokensIn Int? @map("tokens_in")
839
+ tokensOut Int? @map("tokens_out")
840
+ costCents Int? @map("cost_cents")
841
+ durationMs Int? @map("duration_ms")
842
+ piiDetected Boolean @default(false) @map("pii_detected")
843
+ piiReport Json? @map("pii_report")
844
+ contextNodeIds String[] @default([]) @map("context_node_ids")
845
+ documentIds String[] @default([]) @map("document_ids")
846
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
847
+
848
+ session ChatSession @relation(fields: [sessionId], references: [id], onDelete: Cascade)
849
+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
850
+
851
+ @@index([sessionId, createdAt], map: "idx_chat_messages_session_time")
852
+ @@index([organizationId, createdAt], map: "idx_chat_messages_org_time")
853
+ @@map("chat_messages")
854
+ }
855
+
856
+ model OrgChatGuideline {
857
+ id String @id @default(uuid()) @db.Uuid
858
+ organizationId String @map("organization_id") @db.Uuid
859
+ title String @db.VarChar(200)
860
+ content String @db.Text
861
+ priority Int @default(0)
862
+ enabled Boolean @default(true)
863
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
864
+ updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamptz
865
+
866
+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
867
+
868
+ @@index([organizationId], map: "idx_org_chat_guidelines_org")
869
+ @@map("org_chat_guidelines")
870
+ }
871
+
872
+ model OrgPriorityDocument {
873
+ id String @id @default(uuid()) @db.Uuid
874
+ organizationId String @map("organization_id") @db.Uuid
875
+ nodeId String @map("node_id") @db.Uuid
876
+ boostWeight Int @default(1) @map("boost_weight")
877
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
878
+ updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamptz
879
+
880
+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
881
+
882
+ @@unique([organizationId, nodeId], map: "uq_org_priority_doc_org_node")
883
+ @@index([organizationId], map: "idx_org_priority_documents_org")
884
+ @@map("org_priority_documents")
885
+ }