@chronoai/toolkit/prompt — PromptLoaderFeature
Node.js only — 依赖
node:fs和node:path,不可用于浏览器环境。
PromptLoaderFeature 从文件系统加载 .prompt 文件,自动注册为 prompt fragments,并声明一条输出时间轴聚合匹配标签的片段。支持精确路径和 glob 模式。
快速上手
准备好 .prompt 文件后,一行 agent.use() 即可完成加载和注册:
import { Agent } from 'chronoai';
import { PromptLoaderFeature } from '@chronoai/toolkit/prompt';
const agent = new Agent();
agent.use(PromptLoaderFeature, {
files: ['./prompts/*.prompt'],
selectTag: 'system',
});
agent.initialize();
agent.advance();
// 输出时间轴自动聚合所有 system 标签的片段
const systemPrompt = agent.read('prompt::system-prompt', 'current')?.value;
console.log(systemPrompt);
.prompt 文件格式
每个 .prompt 文件由 YAML 前置元数据 + 正文内容组成:
---
name: system_identity
tags: [system, identity]
priority: 100
---
你是一个简洁、友好的 AI 助手。用中文回答问题。
详细语法参考 提示词系统文档。
Glob 支持
files 数组中的每一项可以是精确路径或单层 glob 模式:
agent.use(PromptLoaderFeature, {
files: [
'./prompts/*.prompt', // 匹配 prompts/ 下所有 .prompt 文件
'./extra/special.prompt', // 精确路径
],
selectTag: 'system',
});
Glob 仅支持单层通配(*),不支持递归通配(**)。匹配结果按文件名排序。
自定义排序
默认按 priority 降序排列(数字越大越靠前),可通过 sort 参数调整:
agent.use(PromptLoaderFeature, {
files: ['./prompts/*.prompt'],
selectTag: 'system',
sort: 'priority:asc', // 改为升序
});
时间轴
外部时间轴
| 名称 | 值类型 | 方向 | 说明 |
|---|---|---|---|
prompt::system-prompt | string | 输出 | 所有匹配 selectTag 的片段按排序规则拼接后的文本 |
内部时间轴
无。
配置参数
agent.use(PromptLoaderFeature, config) 的 config 支持以下参数:
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
files | string[] | 是 | — | .prompt 文件路径列表,支持单层 glob 模式 |
selectTag | string | 是 | — | 输出时间轴使用的 {% select %} 标签 |
sort | string | 否 | 'priority:desc' | 排序规则 |