{"version":3,"file":"load-text.mjs","names":[],"sources":["../../../../../../../../ai/src/rag/loaders/load-text.ts"],"sourcesContent":["import type { RagDocument } from \"../contracts/rag-document.type\";\nimport type { LoadTextOptions, RagLoaderResult } from \"./loader.type\";\n\n/** Default `id` when the caller supplies neither `id` nor an item id. */\nconst DEFAULT_ID = \"document\";\n\n/**\n * One raw text item — a bare string, or a `{ id, text, … }` record giving the\n * item its own id / metadata / tags. Passing records lets a single\n * {@link loadText} call turn many strings into many distinctly-identified\n * {@link RagDocument}s.\n */\nexport type TextInput =\n  | string\n  | {\n      /** Stable id for this item. Falls back to the option `id` + index. */\n      id?: string;\n      /** The text body. */\n      text: string;\n      /** Per-item metadata, merged under the shared option `metadata`. */\n      metadata?: Record<string, unknown>;\n      /** Per-item tags (override the shared option `tags` when present). */\n      tags?: string[];\n    };\n\n/**\n * Load plain text into {@link RagDocument}(s) — the zero-dependency base\n * loader every other loader ultimately funnels into. Accepts a single\n * string, a single `{ id, text }` record, or an array mixing both; each\n * input becomes one document carrying `metadata.loader = \"text\"` plus a\n * `metadata.source` (the resolved id).\n *\n * Caller `metadata` always wins over the loader-derived keys, and per-item\n * `metadata` / `tags` (when an item is a record) layer on top of the shared\n * option values. Empty / whitespace-only items are dropped — they would\n * chunk to nothing anyway, so the result never carries a no-op document.\n *\n * The output is the exact shape `index()` consumes:\n *\n * @example\n * const kb = ai.rag({ embedder, store });\n * await kb.index(loadText(\"a long string of notes…\"));\n *\n * @example\n * await kb.index(loadText([\n *   { id: \"faq-1\", text: \"…\", metadata: { section: \"billing\" } },\n *   { id: \"faq-2\", text: \"…\" },\n * ]));\n *\n * @param input - A string, a `{ id, text }` record, or an array of either.\n * @param options - Shared `id` / `metadata` / `tags` ({@link LoadTextOptions}).\n * @returns A {@link RagLoaderResult} ready to hand to `rag.index()`.\n */\nexport function loadText(\n  input: TextInput | TextInput[],\n  options: LoadTextOptions = {},\n): RagLoaderResult {\n  const items = Array.isArray(input) ? input : [input];\n  const baseId = options.id ?? DEFAULT_ID;\n  const multiple = items.length > 1;\n\n  const docs: RagDocument[] = [];\n\n  items.forEach((item, index) => {\n    const text = typeof item === \"string\" ? item : item.text;\n\n    // Drop empties up front — they chunk to nothing, so emitting them would\n    // only add a no-op document for index() to skip.\n    if (text.trim().length === 0) {\n      return;\n    }\n\n    const itemId =\n      typeof item === \"string\" ? undefined : item.id;\n    // A single input keeps the bare base id; multiple inputs are suffixed so\n    // every emitted document has a distinct, stable id.\n    const id = itemId ?? (multiple ? `${baseId}#${index}` : baseId);\n\n    const itemMetadata =\n      typeof item === \"string\" ? undefined : item.metadata;\n    const itemTags = typeof item === \"string\" ? undefined : item.tags;\n\n    docs.push({\n      id,\n      text,\n      // Loader-derived keys first, then the shared option metadata, then the\n      // per-item metadata — caller intent always overrides the derived keys.\n      metadata: {\n        source: id,\n        loader: \"text\",\n        ...options.metadata,\n        ...itemMetadata,\n      },\n      tags: itemTags ?? options.tags,\n    });\n  });\n\n  return docs;\n}\n"],"mappings":";;AAIA,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiDnB,SAAgB,SACd,OACA,UAA2B,CAAC,GACX;CACjB,MAAM,QAAQ,MAAM,QAAQ,KAAK,IAAI,QAAQ,CAAC,KAAK;CACnD,MAAM,SAAS,QAAQ,MAAM;CAC7B,MAAM,WAAW,MAAM,SAAS;CAEhC,MAAM,OAAsB,CAAC;CAE7B,MAAM,SAAS,MAAM,UAAU;EAC7B,MAAM,OAAO,OAAO,SAAS,WAAW,OAAO,KAAK;EAIpD,IAAI,KAAK,KAAK,CAAC,CAAC,WAAW,GACzB;EAOF,MAAM,MAHJ,OAAO,SAAS,WAAW,SAAY,KAAK,QAGxB,WAAW,GAAG,OAAO,GAAG,UAAU;EAExD,MAAM,eACJ,OAAO,SAAS,WAAW,SAAY,KAAK;EAC9C,MAAM,WAAW,OAAO,SAAS,WAAW,SAAY,KAAK;EAE7D,KAAK,KAAK;GACR;GACA;GAGA,UAAU;IACR,QAAQ;IACR,QAAQ;IACR,GAAG,QAAQ;IACX,GAAG;GACL;GACA,MAAM,YAAY,QAAQ;EAC5B,CAAC;CACH,CAAC;CAED,OAAO;AACT"}