Ingest knowledge
Wire extraction, chunking, embeddings, summarization, and search storage into the ingestion pipeline.
@yolk-sdk/knowledge defines contracts. Your app provides concrete providers and stores.
Install
pnpm add @yolk-sdk/knowledge@canary @yolk-sdk/agent@canary effect@4.2.0Pipeline shape
ingestKnowledgeDocument requires these Effect services:
| Service | Host provides |
|---|---|
SearchIndexStore | scope/document/chunk persistence and search |
KnowledgeExtractor | extraction/normalization from caller-loaded LoadedKnowledgeSource.content |
KnowledgeChunker | text chunking; DefaultKnowledgeChunkerLive is available |
KnowledgeEmbedder | embedding provider |
KnowledgeSummarizer | title/summary provider; use NoopKnowledgeSummarizerLive for simple hosts |
For PDF, DOCX, XLSX, PPTX, and text bytes, @yolk-sdk/extractors can provide KnowledgeExtractor
(fragment):
import { Layer } from 'effect'
import { FileKnowledgeExtractorLayer } from '@yolk-sdk/extractors/knowledge'
import { FileExtractorLayer } from '@yolk-sdk/extractors/node'
const HostExtractorLayer = FileKnowledgeExtractorLayer.pipe(Layer.provide(FileExtractorLayer))@yolk-sdk/extractors/node is server-only, and XLSX needs the optional SheetJS peer. See
Extract file text for install, limits, worker isolation, and
errors.
Ingest a document
Fragment; supply the host layers and loadedSource below:
import { Effect, Layer } from 'effect'
import { DefaultKnowledgeChunkerLive } from '@yolk-sdk/knowledge/chunking'
import { KnowledgeDocumentId, KnowledgeScopeId } from '@yolk-sdk/knowledge/documents'
import { NoopKnowledgeSummarizerLive } from '@yolk-sdk/knowledge/summarization'
import { ingestKnowledgeDocument } from '@yolk-sdk/knowledge/ingestion'
const KnowledgeLayer = Layer.mergeAll(
HostSearchIndexStoreLayer,
HostExtractorLayer,
DefaultKnowledgeChunkerLive(),
HostEmbedderLayer,
NoopKnowledgeSummarizerLive
)
await Effect.runPromise(
ingestKnowledgeDocument({
scopeId: KnowledgeScopeId.make('workspace-docs'),
documentId: KnowledgeDocumentId.make('doc_123'),
source: loadedSource,
contentHash: 'sha256:...'
}).pipe(Effect.provide(KnowledgeLayer))
)DefaultKnowledgeChunkerLive splits with gpt-tokenizer's o200k_base encoding, about 2 MB of
code. It loads lazily: importing @yolk-sdk/knowledge/chunking or ./ingestion and providing the
layer evaluate none of it, and the first chunked document loads it once per process or Worker
isolate. Providing the layer on every request is cheap. To count tokens or chunk text yourself, run
the Effects countKnowledgeChunkTokens(text) and chunkKnowledgeText(input, maxTokens) from the
same subpath.
loadedSource is host-owned source metadata/content from @yolk-sdk/knowledge/extraction.
The package does not fetch Url sources or resolve File.ref; load bytes/text before ingestion.
KnowledgeFileBlobStore is not consumed automatically by ingestKnowledgeDocument.
What ingestion does
- Upserts an
IndexedKnowledgeDocumentwithprocessingstatus. - Extracts source text and metadata.
- Chunks text using
maxTokensor the default chunk size. - Embeds chunks and summarizes concurrently.
- Verifies one embedding exists per chunk, then calls
replaceDocumentChunks. - Marks the index document
readywith title, summary, token count, and chunk count.
On failure, the returned KnowledgeIngestionError carries a store, extract, chunk, embed,
or summarize stage. The pipeline best-effort calls markDocumentError with the error message; if
that call also fails, it preserves the original failure.
The calls are not one package-owned transaction. SearchIndexStore.replaceDocumentChunks names
the replacement operation, but the host adapter owns its atomicity and rollback behavior. For
example, a later markDocumentReady failure may leave replaced chunks beside an error status
unless the adapter coordinates those writes. Ingestion does not update the separate
KnowledgeStore document automatically.
Use .make for these trusted example constants. Decode request/persisted IDs with
Schema.decodeUnknownEffect(KnowledgeScopeId) / Schema.decodeUnknownEffect(KnowledgeDocumentId);
your store adapter must return those branded types too. JSON remains plain strings, and host
permission checks remain necessary.
Search after ingestion
Fragment continuing the setup above:
import { KnowledgeSearchScope } from '@yolk-sdk/knowledge/documents'
import { searchKnowledge } from '@yolk-sdk/knowledge/search'
const results = await Effect.runPromise(
searchKnowledge({
scope: KnowledgeSearchScope.make({ id: KnowledgeScopeId.make('workspace-docs') }),
query: 'How do approvals work?',
mode: 'hybrid',
limit: 5,
contextChunks: 1
}).pipe(Effect.provide(KnowledgeLayer))
)Hybrid mode combines vector and text search with reciprocal-rank fusion.
Add as an agent tool
import { Effect } from 'effect'
import { makeKnowledgeLookupTool } from '@yolk-sdk/knowledge/agent'
const lookupTool = makeKnowledgeLookupTool(HostKnowledgeLookupHandlers)Resolve this tool with @yolk-sdk/agent/tools and pass it to the loop.
Production notes
- Check permissions before choosing
scope. - Keep scope ids tenant-safe and host-owned.
- Store embedding model/dimensions in host-owned config.
- Filter search to host-approved
readyrecords and active availability; the search helper does not enforce document status or availability. - Re-ingest on content hash changes.
- Treat archived knowledge as retained but normally excluded from active search.