# @anythingai/teleprompt 中文文档

规范地址：https://npmcn.com/package/%40anythingai/teleprompt/

版本：0\.3\.0
npm 元数据更新时间：2026-07-28T03:57:24.593Z
中文译文更新时间：2026-11-06T08:04:33.651Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40anythingai/teleprompt
原文对照：https://npmcn.com/package/%40anythingai/teleprompt/?lang=original
项目主页：https://github.com/Create-Inc/teleprompt#readme
源码仓库：https://github.com/Create-Inc/teleprompt

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：65.9 kB，18 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=20
- 运行依赖：0 个
- 首次发布：2026-02-20T00:04:50.356Z
- 最新版本发布：2026-03-25T19:12:01.528Z
- 近 30 天下载量：17141（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：4（近 12 个月 4 个）

## 中文简介

LLM 系统可编译、可声明的快速构建器 :

## 安装

```sh
npm install @anythingai/teleprompt
```

也可以使用：`pnpm add @anythingai/teleprompt`、`yarn add @anythingai/teleprompt`、`bun add @anythingai/teleprompt`

## README

# 远程瞬间

可编译,以区段为基础的LLM系统提示.

每个部分拥有其内容和条件逻辑. 区段可以跨即时变体再利用,有条件地包含,并作为纯文本或XML提供.

```bash
pnpm add @anythingai/teleprompt
```

## 快速启动

```ts
import { PromptBuilder, section, type PromptContext } from '@anythingai/teleprompt';

// Define your context shape
type MyFlags = { webSearchEnabled: boolean };
type MyVars = { assistantName: string };
type MyContext = PromptContext<MyFlags, MyVars>;

// Static section — no context needed, no type parameter
const guidelines = section('guidelines', () => `# Guidelines
- Be concise and direct.
- Cite sources when making factual claims.
- Ask for clarification when a request is ambiguous.`);

// Dynamic section — uses context
const identity = section('identity', (ctx: MyContext) =>
  `You are ${ctx.vars.assistantName}, a helpful AI assistant.`
);

// Conditional section — return null to exclude
const webSearch = section('web-search', (ctx: MyContext) => {
  if (!ctx.flags.webSearchEnabled) return null;
  return `You have access to web search. Use it when the user asks about
current events or information that may have changed after your training cutoff.`;
});

// Compose and build
const prompt = new PromptBuilder<MyContext>()
  .use(identity)
  .use(guidelines)
  .use(webSearch)
  .build({
    flags: { webSearchEnabled: true },
    vars: { assistantName: 'Daniel' },
  });
```

## 节

`section(id, render)` 使用 ID 和 渲染 函数。 返回要包含的字符串, `null` 排除:

```ts
// Always included
const rules = section('rules', () => 'Be helpful and concise.');

// Conditional — null means excluded
const citation = section('citation', (ctx: MyContext) => {
  if (!ctx.flags.citationEnabled) return null;
  return 'Always include citations with links when referencing external sources.';
});
```

区域按照您调用的顺序执行 `.use()`。要重新排序,请更改呼叫顺序.

静态段( 无类型参数) 在任何构建器中工作 :

```ts
const disclaimer = section('disclaimer', () => 'Responses are not legal advice.');

// Works in any builder regardless of context type
new PromptBuilder<MyContext>().use(disclaimer)
new PromptBuilder<OtherContext>().use(disclaimer)
```

## A. 背景情况

区域接收带有布尔标记和任意变量的输入上下文 :

```ts
type MyFlags = {
  webSearchEnabled: boolean;
  citationEnabled: boolean;
};

type MyVars = {
  assistantName: string;
  language: string;
};

type MyContext = PromptContext<MyFlags, MyVars>;
```

`PromptContext` 财务报告和审定财务报表 `PromptBuilder` 有默认值,所以类型参数是可选的:

```ts
const builder = new PromptBuilder();
```

建立上下文一次,然后传递给 `.build(ctx)`。每个区域接收相同的对象.

## 伪造

从共享碱基创建变量 :

```ts
const base = new PromptBuilder<MyContext>()
  .use(identity)
  .use(guidelines)
  .use(tone);

// Customer support agent — adds escalation rules
const supportAgent = base.fork()
  .use(escalationPolicy)
  .use(ticketFormat);

// Code assistant — swaps guidelines, drops tone
const codeAssistant = base.fork()
  .without(guidelines)
  .without(tone)
  .use(codingGuidelines)
  .use(outputFormat);
```

每分叉相独立. 修改一个不会影响另一个.

## XML 格式

Claude和双子座都建议用XML标记来构建提示. 过 `{ format: 'xml' }` 改为 `.build()` 将每个区域围入 `<id>` 标记 :

```ts
builder.build(ctx, { format: 'xml' })
```

区域 id 成为标签名称 。 内容留在标签内.

## 组

组合相关章节。 在文本模式中,分组是透明的. 在XML模式中,它们会用标记来包裹孩子:

```ts
const prompt = new PromptBuilder<MyContext>()
  .use(identity)
  .group('tools', b => b
    .use(webSearch)
    .use(calculator)
  )
  .use(guidelines)
  .build(ctx, { format: 'xml' });
```

```xml
<identity>
You are Daniel, a helpful AI assistant.
</identity>

<tools>
<web-search>
You have access to web search...
</web-search>

<calculator>
You can evaluate math expressions...
</calculator>
</tools>

<guidelines>
# Guidelines
...
</guidelines>
```

组合可以被嵌入 :

```ts
builder.group('capabilities', b => b
  .group('tools', b => b
    .use(webSearch)
    .use(calculator)
  )
  .group('integrations', b => b
    .use(slack)
    .use(linear)
  )
)
```

## 相互排斥部分

使用 `.useOneOf()` 确切地说,几节中应该有一节。 第一个返回非空字符串的候选人获胜:

```ts
const hasTasks = section('has-tasks', (ctx: MyContext) => {
  if (ctx.vars.tasks.length === 0) return null;
  return `## Active Tasks\n\n${ctx.vars.tasks.map(t => `- ${t.title}`).join('\n')}`;
});

const noTasks = section('no-tasks', () => '## Active Tasks\n\nNo tasks currently running.');

builder.useOneOf(hasTasks, noTasks);
```

## 构建器 API

```ts
new PromptBuilder<MyContext>()
  .use(section)                      // append (replaces if same id)
  .useOneOf(sectionA, sectionB)      // first match wins
  .group('name', b => b.use(...))    // named group (XML wrapper)
  .without(section)                  // remove by object or string id
  .has(section)                      // check existence
  .ids()                             // list all section ids
  .fork()                            // independent copy
  .build(ctx)                        // render to string
  .build(ctx, { format: 'xml' })     // render with XML tags
  .buildWithMeta(ctx)                // render + { included, excluded }
```

## 测试

```ts
import { mockContext, renderSection } from '@anythingai/teleprompt/testing';

// Render a section in isolation
const output = renderSection(webSearch, { flags: { webSearchEnabled: true } });
expect(output).toContain('web search');

// Assert on prompt structure
const { included, excluded } = builder.buildWithMeta(ctx);
expect(included).toContain('web-search');
expect(excluded).toContain('citation');
```

## 许可证

麻省理工学院

## 常见问题

### @anythingai/teleprompt 是什么？

LLM 系统可编译、可声明的快速构建器 :

### 如何安装 @anythingai/teleprompt？

在项目目录运行 npm install @anythingai/teleprompt，或使用 pnpm add / yarn add / bun add。

### @anythingai/teleprompt 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @anythingai/teleprompt 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @anythingai/teleprompt 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=20。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @anythingai/teleprompt 可以商用吗？

@anythingai/teleprompt 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @anythingai/teleprompt 的下载量有多少？

根据 npm 官方下载统计，@anythingai/teleprompt 在 2026/10/06 – 2026/11/04 这 30 天内被下载 17,141 次，日均约 571 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @anythingai/teleprompt 还在维护吗？

最新版本 0\.3\.0 发布于 2026/03/26（7 个月前）；至今共发布 4 个版本，其中过去 12 个月发布了 4 个；首次发布于 2026/02/20。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

