Knowledge: documents, sections, and evidence snapshots
One document contract supports plain objects and anchored text; a selection yields an evidence snapshot with an explicit version.
defineKnowledgeCatalog(documents, options?) compiles the document tree and rejects duplicate documents/sections, missing revisions, and cycles. read({documentId,selection}) returns {documentId,selection,revision,locale,content,id}. selection.mode can be document, self, or subtree; self/subtree also need non-empty, duplicate-free sectionIds. self reads only that section's own text, while subtree includes its child sections.
maxChars limits the selected body text after trimming; going over the limit throws instead of silently truncating. assertSelection only checks references and the selection shape and does not enforce the body budget. sectionIds lists the real sections; ordinary get/require return the compiled document definitions.
revision is provided by the App, and id is a snapshot identifier serialized from document identity, revision, language, and selection; it is not a body hash, permission credential, or database ID. Content must not change quietly under the same revision; subsequent generation and review should pass the same snapshot. File loading, source trust, permissions, and publication are outside this Kit.
Progressive examples for the same business case
First read one plain-object document
Beginner · Node.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);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
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);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.
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);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);