All files / src/objects Knowledge.tsx

83.33% Statements 5/6
37.5% Branches 3/8
100% Functions 1/1
83.33% Lines 5/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                3x       3x     16x 16x 16x                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
import { AIGenerated, Resource, SystemManaged } from "@/objects/Doc";
import { parseServerDate } from "@/util/time";
 
export interface NodePropertyDef {
  key: string;
  label: string;
  type: "string" | "number" | "date" | "email" | "url";
  required: boolean;
  placeholder: string;
}
 
export interface NodeTypeDefinition {
  name: string;
  display_name: string;
  description: string;
  category: string;
  color: string;
  property_schema: NodePropertyDef[];
}
 
export interface Knowledge extends Resource, AIGenerated, SystemManaged {
  /** Whether the viewer may change it here — edit, upload, find sources,
   *  delete. Served on a source's own page (`GET /knowledge/{id}`); false
   *  for a built-in, for what a project inherits, and for a level that
   *  does not allow it. Absent from listings. */
  can_change?: boolean;
  name: string;
  description: string;
  source_type: string;
  source_config: Record<string, unknown>;
  created_at: string;
}
 
/** What kind of knowledge a repository holds, which decides how an agent
 *  uses it: svc-core's `app/util/knowledge_categories.py`, named on the
 *  pages by `knowledge/categories.ts`. */
export type KnowledgeCategoryKey = "domain" | "temporal" | "reasoning";
 
/** A named collection of knowledge bases (sources) that can be attached to
 *  agents as a single unit. */
export interface KnowledgeRepository extends Resource, AIGenerated, SystemManaged {
  name: string;
  description?: string;
  /** What kind of knowledge it holds. Every source in it reaches an agent as
   *  this kind. Absent from an older server: read as Domain. */
  category?: KnowledgeCategoryKey;
  /** Presentation, chosen by the user (or proposed during generative setup):
   *  a name from `REPOSITORY_ICON_REGISTRY` and a key from the accent
   *  palette. Both fall back client-side, so an unknown value is never an
   *  error. */
  icon?: string | null;
  accent?: string | null;
  knowledge_ids: string[];
  created_at?: string;
  /** Served by `GET /repositories/{id}` only: whether the viewer may change
   *  it here — false for a built-in, what a project inherits, or a level
   *  that does not allow it. Absent on listings. */
  can_change?: boolean;
}
 
/** A source's documents (or a website's pages) by where they stand
 *  (`GET /knowledge/stats`, svc-core `services/knowledge/stats.py`). */
export interface KnowledgeDocumentCounts {
  total: number;
  /** `indexed` `pending` `indexing` `importing` `failed` `not_indexed`,
   *  and `held` for a pending document a budget holds. */
  by_state: Record<string, number>;
  unrecorded: number;
  /** A website's pages by status (`active` `gone` `blocked` `failed`). */
  pages: Record<string, number>;
  chunks: number;
  size_bytes: number;
  page_versions: number;
}
 
export interface KnowledgeCrawlFigures {
  /** The newest crawl's status, or `never`. */
  status: string;
  started_at?: string | null;
  finished_at?: string | null;
  pages_failed: number;
  last_success_at?: string | null;
  cadence?: string | null;
  /** When it is crawled next; null when nothing will (manual, or a
   *  built-in no agent here uses). */
  next_due_at?: string | null;
}
 
/** A source's figures: what it is, and — in this workspace only — who
 *  reaches it and how often answers cite it. */
export interface KnowledgeStats {
  knowledge_id: string;
  source_type: string;
  items: number;
  documents: KnowledgeDocumentCounts;
  crawl?: KnowledgeCrawlFigures | null;
  last_updated_at?: string | null;
  repositories: { id: string; name: string }[];
  agents: number;
  /** Answers citing it in the last 30 days, and per day, oldest first. */
  citations: number;
  citations_daily: number[];
  /** Answers in the last 30 days whose search kept passages from it (the
   *  retrieval record; agent chat only). Absent from an older server. */
  retrieved?: number;
  /** The trust signals asking for a look (`tools_rules`, `freshness`,
   *  `index_health`). */
  attention: string[];
  /** One line on where it points; never a credential. */
  config_summary?: string | null;
}
 
/** An agent, source or document by the answers citing it (`id` null for one
 *  deleted since, or a source cited whole). */
export interface UsageRanked {
  id?: string | null;
  name: string;
  answers: number;
  is_system?: boolean;
  url?: string | null;
  knowledge_id?: string | null;
}
 
/** What a source or repository is used for: answers in this workspace
 *  citing it over a window (`GET /knowledge/{id}/usage`). */
export interface KnowledgeUsage {
  days: number;
  since: string;
  answers: number;
  /** The same length of time before `since`. */
  previous_answers: number;
  daily: { day: string; answers: number }[];
  agents: UsageRanked[];
  documents: UsageRanked[];
  /** A repository's only. */
  sources: UsageRanked[];
  /** The viewer's own answers — a conversation is its person's. */
  recent: {
    message_id: string;
    conversation_id: string;
    agent_id?: string | null;
    agent_name?: string | null;
    cited_at: string;
  }[];
}
 
/** One trust signal, as the catalogue serves it. */
export interface TrustSignalSpec {
  key: string;
  label: string;
  description: string;
  /** Its unmet or conflict counts towards "Needs attention". */
  attention: boolean;
}
 
export type TrustState =
  | "met"
  | "partial"
  | "unmet"
  | "conflict"
  | "in_progress"
  | "unknown"
  | "not_applicable";
 
export interface TrustFact {
  label: string;
  value: string;
  /** What it links to: `rule`, `generation`, `user`. */
  ref_kind?: string | null;
  ref_id?: string | null;
  url?: string | null;
  /** A moment to show in the reader's zone (`value` then empty). */
  at?: string | null;
}
 
export interface TrustResult {
  key: string;
  state: TrustState;
  summary: string;
  facts: TrustFact[];
}
 
/** What is on record about relying on a source
 *  (`GET /knowledge/{id}/trust`). */
export interface KnowledgeTrust {
  knowledge_id: string;
  signals: TrustResult[];
  attention: string[];
}
 
/** A repository's figures, its sources' added up. */
export interface RepositoryStats {
  repository_id: string;
  sources: number;
  by_type: Record<string, number>;
  items: number;
  documents_indexed: number;
  documents_total: number;
  attention_sources: number;
  agents: number;
  citations: number;
  citations_daily: number[];
  /** Answers in the last 30 days whose search kept passages from any of
   *  its sources. */
  retrieved?: number;
  last_updated_at?: string | null;
}
 
/** A repository's figures with each member's, for its Sources table
 *  (`GET /repositories/{id}/stats`). */
export interface RepositoryStatsDetail {
  repository: RepositoryStats;
  sources: KnowledgeStats[];
}
 
/** Each member's trust signals, and per signal how many members are in
 *  each state (`GET /repositories/{id}/trust`). */
export interface RepositoryTrust {
  sources: KnowledgeTrust[];
  summary: Record<string, Partial<Record<TrustState, number>>>;
}
 
/** One kind of knowledge recommendation, as the catalogue serves it
 *  (svc-core `services/knowledge/recommendations`). */
export interface RecommendationKindSpec {
  key: string;
  label: string;
  description: string;
  section: string;
  section_label: string;
}
 
/** The flow an action opens; it acts only when the person does. */
export type RecommendationFlow =
  | "generate_knowledge"
  | "add_website"
  | "attach_repository"
  | "open_source"
  | "edit_source"
  | "open_repository"
  | "edit_repository";
 
/** What an action fills its flow with; each flow reads the fields it takes. */
export interface RecommendationPrefill {
  category?: string | null;
  /** The viewer's own answers a generation is described from. */
  from_turns?: string[];
  agent_id?: string | null;
  agent_name?: string | null;
  repository_id?: string | null;
  repository_name?: string | null;
  knowledge_id?: string | null;
  knowledge_name?: string | null;
  tab?: string | null;
  url?: string | null;
  name?: string | null;
  refresh?: string | null;
  max_age_days?: number | null;
}
 
export interface RecommendationAction {
  flow: RecommendationFlow | string;
  label: string;
  prefill: RecommendationPrefill;
}
 
export interface KnowledgeRecommendation {
  kind: string;
  section: string;
  subject: { kind: string; id: string; name: string };
  title: string;
  why: string;
  figures: { label: string; value: number }[];
  /** The viewer's own questions behind it, never another member's. */
  examples: { text: string; detail?: string | null; at?: string | null }[];
  actions: RecommendationAction[];
  /** What the evidence reads: sent back to set it aside. */
  fingerprint: string;
}
 
/** `GET /knowledge/recommendations` (everything, or one repository's or
 *  agent's). */
export interface KnowledgeRecommendations {
  items: KnowledgeRecommendation[];
  /** Kinds that could not be read now, and why — never "none". */
  unavailable: Record<string, string>;
  /** Whether the viewer may act on them and set them aside here. */
  can_change: boolean;
}
 
export interface RecommendationDismissed {
  id: string;
  kind: string;
  subject: string;
  dismissed_at: string;
}
 
/** One kind of evidence a connection can rest on, as the legend serves it
 *  (svc-core `source_map/evidence.py`). */
export interface SourceMapKind {
  key: string;
  label: string;
  description: string;
  /** How much one kind can weigh at most, 0–1. */
  strength: number;
  /** How much of it makes the score about two-thirds of its strength. */
  tau: number;
  /** What its count counts ("document in both"); null where the count is a
   *  weighting and the examples say it. */
  unit_one?: string | null;
  unit_many?: string | null;
}
 
export interface SourceMapExample {
  label: string;
  url?: string | null;
}
 
/** One kind's part in a connection: how much there is, its score
 *  (`1 − e^(−count/τ)`) and what it adds to the weight. */
export interface SourceMapEvidence {
  kind: string;
  count: number;
  score: number;
  contribution: number;
  examples: SourceMapExample[];
}
 
export type SourceMapNodeKind = "source" | "suggestion" | "agent";
 
export interface SourceMapNode {
  id: string;
  kind: SourceMapNodeKind;
  ref_id: string;
  label: string;
  /** A source's `source_type`, `items`, `attention`, `is_system`,
   *  `is_ai_generated`; an agent's `cited`. */
  meta: Record<string, unknown>;
}
 
export interface SourceMapEdge {
  source: string;
  target: string;
  /** `evidence` between sources; `cited_by` from an agent. */
  kind: "evidence" | "cited_by";
  /** 0–1: the kinds together for `evidence`, the agent's share of its
   *  citations for `cited_by`. */
  weight: number;
  evidence: SourceMapEvidence[];
}
 
/** A repository as a web of its sources, connected by evidence, with the
 *  workspace's sources the evidence ties to it as suggestions
 *  (`GET /repositories/{id}/map`). */
export interface SourceMap {
  nodes: SourceMapNode[];
  edges: SourceMapEdge[];
  kinds: SourceMapKind[];
  summary: {
    sources: number;
    connections: number;
    suggestions: number;
    isolated: number;
    duplicates: number;
    /** A kind that could not be read, and why — never read as none. */
    unavailable: Record<string, string>;
  };
  truncated: boolean;
}
 
/** One of the caller's own agents that a repository is granted to
 *  (`GET /repositories/{id}/agents`). Never another user's: a built-in
 *  repository is granted by many users' agents. */
export interface RepositoryAgentGrant {
  agent_id: string;
  name: string;
}
 
export interface KnowledgeGraphNode {
  id: string;
  label: string; // entity type e.g. "Person", "Company"
  name: string;
  properties: Record<string, string>;
  x?: number | null;
  y?: number | null;
}
 
export interface KnowledgeGraphEdge {
  id: string;
  source_id: string;
  target_id: string;
  relationship: string;
  properties: Record<string, string>;
}
 
export interface GraphData {
  nodes: KnowledgeGraphNode[];
  edges: KnowledgeGraphEdge[];
}
 
/**
 * Indexing lifecycle of an uploaded document (recorded by svc-core):
 *   pending     — queued for parse → embed → index
 *   indexing    — in progress
 *   indexed     — searchable (chunk_count chunks in the vector store)
 *   failed      — see index_error; reindex after fixing the cause
 *   not_indexed — uploaded before status tracking and absent from the index
 *   importing   — being downloaded from its link (`source_url`) first
 */
export type DocumentIndexStatus =
  | "importing"
  | "pending"
  | "indexing"
  | "indexed"
  | "failed"
  | "not_indexed";
 
export interface KnowledgeDocument {
  id: string;
  knowledge_id: string;
  source_id: string;
  folder_path: string;
  document_metadata?: {
    original_filename?: string;
    size_bytes?: number;
    mime_type?: string;
    stored_path?: string;
    folder_path?: string;
    index_status?: DocumentIndexStatus;
    index_error?: string;
    /** Why a `pending` document waits on something outside it: `budget`
     * when its workspace's budget is used up (svc-core `knowledge/held.py`). */
    index_refusal?: string;
    /** When a budget-held document is indexed, from its stored file. */
    index_hold_until?: string;
    chunk_count?: number;
    indexed_at?: string;
    /** A document imported from a link: where it came from, and what the
     * publisher calls it. */
    source_url?: string;
    title?: string;
    publisher?: string;
    published?: string;
  };
  created_at: string;
}
 
/** `index_refusal` for a document a used-up budget is holding. */
export const BUDGET_HOLD = "budget";
 
/** When a document a used-up budget holds is indexed — `null` when the
 * budget does not say — or `undefined` when no budget holds it. */
export const budgetHoldUntil = (
  doc: KnowledgeDocument,
): Date | null | undefined => {
  const meta = doc.document_metadata ?? {};
  Eif (meta.index_status !== "pending" || meta.index_refusal !== BUDGET_HOLD) {
    return undefined;
  }
  return meta.index_hold_until ? parseServerDate(meta.index_hold_until) : null;
};
 
export interface ReindexResult {
  queued: number;
  document_ids: string[];
}
 
// ─── Website sources ─────────────────────────────────────────────────────────
 
/** How far a crawl may wander from the URLs it was given. */
export type WebCrawlScope = "page" | "section" | "site";
export type WebRefreshCadence = "manual" | "hourly" | "daily" | "weekly";
 
/**
 * How the entries of an RSS or Atom feed become pages: `auto` keeps the
 * feed's own text when it carries the whole item and otherwise fetches the
 * page (keeping the entry if the page has nothing readable); `entry` never
 * fetches; `article` always does, and skips other sites' pages.
 */
export type WebFeedContent = "auto" | "entry" | "article";
 
/**
 * A website source's source_config, as svc-core stores it after
 * validating (services/knowledge/web_config.py).
 */
export interface WebSourceConfig {
  urls: string[];
  scope: WebCrawlScope;
  max_pages: number;
  max_depth: number;
  include: string[];
  exclude: string[];
  refresh: WebRefreshCadence;
  use_sitemap: boolean;
  /** Skip action, account, asset and feed links, and stop fetching a kind
   *  of page that keeps coming back empty or behind a login. */
  skip_low_value: boolean;
  /** Also read the pages on other sites that crawled pages link to — one
   *  hop, capped by max_external_pages. */
  follow_external: boolean;
  max_external_pages: number;
  /** When a URL is a feed, how its entries become pages. */
  feed_content?: WebFeedContent;
  /** A rolling window: pages older than this many days are removed with
   *  their snapshots. Null keeps everything. */
  max_age_days?: number | null;
  /** The credit every passage from this source carries into a citation. */
  attribution?: string | null;
  /** The licence, linked beside the attribution. */
  licence_url?: string | null;
}
 
/** `held`: not run — the source's workspace's budget was used up. */
export type WebCrawlStatus = "running" | "completed" | "failed" | "held";
export type WebCrawlTrigger =
  | "created"
  | "updated"
  | "manual"
  | "scheduled"
  | "granted";
 
/** One crawl of a website source. */
export interface KnowledgeCrawl {
  id: string;
  knowledge_id: string;
  trigger: WebCrawlTrigger;
  status: WebCrawlStatus;
  actor_user_id?: string | null;
  started_at: string;
  finished_at?: string | null;
  pages_fetched: number;
  pages_new: number;
  pages_changed: number;
  pages_unchanged: number;
  pages_failed: number;
  pages_gone: number;
  /** Pages removed for being older than the source's window. */
  pages_expired?: number;
  /** What the crawl's embeddings cost, as svc-ai measured it. */
  usage?: {
    calls: number;
    prompt_tokens: number;
    estimated_cost_usd: number | null;
    unpriced_calls: number;
  } | null;
  note?: string | null;
}
 
/**
 * A page's standing after its latest crawl: active, gone (404 or 410, out of
 * the index), blocked (robots.txt) or failed (last snapshot still searchable).
 */
export type WebPageStatus = "active" | "gone" | "blocked" | "failed";
 
export interface WebPage {
  document_id: string;
  url: string;
  title?: string | null;
  status: WebPageStatus;
  version_count: number;
  last_fetched_at?: string | null;
  last_changed_at?: string | null;
  last_error?: string | null;
  index_status?: DocumentIndexStatus | null;
  first_seen_at: string;
  /** crawled: in the source's scope. linked: on another site, read because a
   *  crawled page links to it. */
  origin?: "crawled" | "linked";
  /** For a linked page, the crawled pages that link to it. */
  linked_from?: { url: string; title?: string; text?: string }[];
}
 
/** A stored page in a website source's link graph. `id` is its document id. */
export interface WebGraphNode {
  id: string;
  url: string;
  title?: string | null;
  status: WebPageStatus;
  origin: "crawled" | "linked";
  host: string;
  is_seed: boolean;
  version_count: number;
  /** Shortest link path from a seed URL; null when no stored link reaches it
   *  (found in a sitemap, or the page that linked to it has gone). */
  depth: number | null;
  /** Links on the page to anything the source did not store. */
  unfollowed_links: number;
  in_links: number;
  out_links: number;
}
 
export interface WebGraphEdge {
  source: string;
  target: string;
  text: string;
  /** internal: same site. external: a hop to another site. */
  kind: "internal" | "external";
}
 
/** Which stored page links to which — built from stored records, no fetch. */
export interface WebLinkGraph {
  nodes: WebGraphNode[];
  edges: WebGraphEdge[];
  truncated: boolean;
}
 
/** One stored snapshot of a page, written when its text changed. */
export interface WebPageVersion {
  id: string;
  document_id: string;
  crawl_id?: string | null;
  version_number: number;
  url: string;
  title?: string | null;
  content_hash: string;
  http_status?: number | null;
  fetched_at: string;
  size_bytes: number;
  has_html: boolean;
}
 
export interface WebPageVersionContent {
  version: WebPageVersion;
  markdown: string;
}
 
/**
 * A snapshot's HTML after svc-core's allow-list sanitiser: text structure
 * only, no scripts, styles, forms, frames or external URLs. Still only ever
 * rendered inside the sandboxed, network-blocked preview frame.
 */
export interface WebPageVersionPreview {
  version: WebPageVersion;
  html: string;
  truncated: boolean;
}
 
/** A line of a snapshot diff; skip stands for a folded run of unchanged lines. */
export interface WebDiffLine {
  op: "equal" | "insert" | "delete" | "skip";
  line: string;
}
 
export interface WebPageDiff {
  from_version?: WebPageVersion | null;
  to_version: WebPageVersion;
  text_diff: WebDiffLine[];
  added_lines: number;
  removed_lines: number;
}
 
/** What a crawl did to a page: stored its first version, or a changed one. */
export type WebCrawlWritten = "new" | "changed";
 
/** How a page's newest version stands against its version after a crawl. */
export type WebPageSince = "added" | "changed" | "unchanged";
 
/** One page of a website source as a crawl left it, beside how it is now. */
export interface WebCrawlPage {
  document_id: string;
  url: string;
  title?: string | null;
  /** The page's standing now. */
  status: WebPageStatus;
  origin?: "crawled" | "linked";
  /** The version in force after the crawl; null when first stored later. */
  then?: WebPageVersion | null;
  /** The newest version. */
  now: WebPageVersion;
  /** What the crawl itself stored for the page, if anything. */
  written?: WebCrawlWritten | null;
  since: WebPageSince;
}
 
/** A website source as one crawl left it — see svc-core web_as_of.py. */
export interface WebCrawlPages {
  crawl: KnowledgeCrawl;
  pages: WebCrawlPage[];
  /** Pages later crawls removed for ageing out of the source's window; their
   *  versions went with them, so they are counted and cannot be listed. */
  expired_since: number;
}