Skip to content
English

defineKnowledgeCatalog ​

KIT · function · 运行时值

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.

导入 ​

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

其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits/knowledge、@jangts/agent-core/kits。

定义 ​

调用 ​

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.

参数类型传递说明
documentsreadonly KnowledgeDocument[]必需—
optionsKnowledgeCatalogOptions可省略—
options 的字段
字段类型必需说明
maxCharsnumber | undefined可省略只读。Maximum characters per read; exceeding it throws a RangeError.
observe((snapshot: KnowledgeSnapshot) => void | Promise<void>) | undefined可省略只读。Observer invoked with every produced snapshot.

返回: KnowledgeCatalog。

返回值结构
字段类型必需说明
sectionIds(documentId: string) => readonly string[]必需Section ids of one document, in declaration order; throws for an unknown document.
assertSelection(read: KnowledgeRead) => void必需Validates a read without producing content; throws when the selection is invalid.
read(read: KnowledgeRead) => KnowledgeSnapshot必需Produces a snapshot for the read; throws for unknown documents, sections or budget excess.
definitionsreadonly Readonly<KnowledgeDocument>[]必需只读。Definitions in registration order; each entry is frozen by the implementation.
ids() => readonly string[]必需Ids in the same order as definitions.
values() => IterableIterator<Readonly<KnowledgeDocument>>必需见类型定义及调用场景。
entries() => IterableIterator<readonly [string, Readonly<KnowledgeDocument>]>必需见类型定义及调用场景。
has(id: string) => boolean必需见类型定义及调用场景。
get(id: string) => Readonly<KnowledgeDocument> | undefined必需Returns the definition or undefined; the caller decides whether absence is an error.
require(id: string) => Readonly<KnowledgeDocument>必需Returns the definition or throws; use when the id is a required reference.

使用示例 ​

先读取一份普通对象资料 ​

入门 · 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);

在相同 read 上增加章节与预算 ​

配置 · 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);

换成文本适配器,后续目录与读取完全相同 ​

扩展 · Node.js

成对锚点必须匹配;标记仅是文本适配器的一种输入,Core 不要求资料必须存成 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);

相关策略源码 ​

相关定义 ​

接口参考、使用示例与开发指南。