Skip to content
中文

defineKnowledgeCatalog ​

KIT · function · Runtime value

Compile a knowledge catalog from document declarations.

Reads are declarative: document returns the whole text, self returns exactly the named sections and subtree includes their nested sections. Section ids are validated up front, so a typo fails at read time instead of silently returning partial content.

Import ​

ts
import { defineKnowledgeCatalog } from '@jangts/agent-core/kits/knowledge';
js
import { defineKnowledgeCatalog } from '@jangts/agent-core/kits/knowledge';

Other public entry points:@jangts/agent-core/types, @jangts/agent-core/kits/knowledge, @jangts/agent-core/kits.

Definition ​

Call ​

ts
defineKnowledgeCatalog(documents: readonly KnowledgeDocument[], options?: KnowledgeCatalogOptions): KnowledgeCatalog

Compile a knowledge catalog from document declarations.

Reads are declarative: document returns the whole text, self returns exactly the named sections and subtree includes their nested sections. Section ids are validated up front, so a typo fails at read time instead of silently returning partial content.

ParameterTypePassingDescription
documentsreadonly KnowledgeDocument[]Required—
optionsKnowledgeCatalogOptionsOptional—
Fields of options
FieldTypeRequiredDescription
maxCharsnumber | undefinedOptionalRead-only. Maximum characters per read; exceeding it throws a RangeError.
observe((snapshot: KnowledgeSnapshot) => void | Promise<void>) | undefinedOptionalRead-only. Observer invoked with every produced snapshot.

Returns: KnowledgeCatalog.

Return value structure
FieldTypeRequiredDescription
sectionIds(documentId: string) => readonly string[]RequiredSection ids of one document, in declaration order; throws for an unknown document.
assertSelection(read: KnowledgeRead) => voidRequiredValidates a read without producing content; throws when the selection is invalid.
read(read: KnowledgeRead) => KnowledgeSnapshotRequiredProduces a snapshot for the read; throws for unknown documents, sections or budget excess.
definitionsreadonly Readonly<KnowledgeDocument>[]RequiredRead-only. Definitions in registration order; each entry is frozen by the implementation.
ids() => readonly string[]RequiredIds in the same order as definitions.
values() => IterableIterator<Readonly<KnowledgeDocument>>RequiredSee the type definition and usage context.
entries() => IterableIterator<readonly [string, Readonly<KnowledgeDocument>]>RequiredSee the type definition and usage context.
has(id: string) => booleanRequiredSee the type definition and usage context.
get(id: string) => Readonly<KnowledgeDocument> | undefinedRequiredReturns the definition or undefined; the caller decides whether absence is an error.
require(id: string) => Readonly<KnowledgeDocument>RequiredReturns the definition or throws; use when the id is a required reference.

Examples ​

First read one plain-object document ​

Beginner · Node.js

ts
import { Knowledge } from '@jangts/agent-core/kits/knowledge';
const catalog = Knowledge.defineKnowledgeCatalog([
  { id: 'manual', revision: '1', parts: ['Agent Core 支持普通 Workflow 节点。'] },
]);
const snapshot = catalog.read({ documentId: 'manual', selection: { mode: 'document' } });
console.log(snapshot.content, snapshot.revision);
js
import { Knowledge } from '@jangts/agent-core/kits/knowledge';
const catalog = Knowledge.defineKnowledgeCatalog([
    { id: 'manual', revision: '1', parts: ['Agent Core 支持普通 Workflow 节点。'] },
]);
const snapshot = catalog.read({ documentId: 'manual', selection: { mode: 'document' } });
console.log(snapshot.content, snapshot.revision);

Add sections and a budget to the same read ​

Configuration · Node.js

ts
import { Knowledge } from '@jangts/agent-core/kits/knowledge';
const catalog = Knowledge.defineKnowledgeCatalog([
  { id: 'manual', revision: '2', parts: [
    { id: 'start', parts: ['先运行一个节点。', { id: 'advanced', parts: ['再增加模型和工具。'] }] },
  ] },
], { maxChars: 2000 });
console.log(catalog.sectionIds('manual'));
const snapshot = catalog.read({ documentId: 'manual', selection: { mode: 'subtree', sectionIds: ['start'] } });
console.log(snapshot.content, snapshot.id);
js
import { Knowledge } from '@jangts/agent-core/kits/knowledge';
const catalog = Knowledge.defineKnowledgeCatalog([
    { id: 'manual', revision: '2', parts: [
            { id: 'start', parts: ['先运行一个节点。', { id: 'advanced', parts: ['再增加模型和工具。'] }] },
        ] },
], { maxChars: 2000 });
console.log(catalog.sectionIds('manual'));
const snapshot = catalog.read({ documentId: 'manual', selection: { mode: 'subtree', sectionIds: ['start'] } });
console.log(snapshot.content, snapshot.id);

Switch to the text adapter; later catalog and read calls stay identical ​

Extension · Node.js

Paired anchors must match; markers are just one input for the text adapter, and Core does not require documents to be stored as Markdown.

ts
import { Knowledge } from '@jangts/agent-core/kits/knowledge';
const document = Knowledge.parseKnowledgeDocument({
  id: 'manual', revision: '3',
  text: ['<!-- agent-section:start -->', '先运行一个节点。', '<!-- /agent-section:start -->'].join('\n'),
});
const catalog = Knowledge.defineKnowledgeCatalog([document], { maxChars: 2000 });
const read = { documentId: 'manual', selection: { mode: 'self' as const, sectionIds: ['start'] } };
catalog.assertSelection(read);
console.log(catalog.read(read).content);
js
import { Knowledge } from '@jangts/agent-core/kits/knowledge';
const document = Knowledge.parseKnowledgeDocument({
    id: 'manual', revision: '3',
    text: ['<!-- agent-section:start -->', '先运行一个节点。', '<!-- /agent-section:start -->'].join('\n'),
});
const catalog = Knowledge.defineKnowledgeCatalog([document], { maxChars: 2000 });
const read = { documentId: 'manual', selection: { mode: 'self', sectionIds: ['start'] } };
catalog.assertSelection(read);
console.log(catalog.read(read).content);

API reference, usage examples and developer guides.