@dani-builder/agent-contract

中文文档
1.1.0发布于 2026/11/04MITTypeScriptES Module零依赖近 30 天下载 2671

Zero-dependency wire contracts for Daniworks Agent clients.

原始使用文档

@dani-builder/agent-contract

Zero-dependency TypeScript wire contracts for Daniworks Agent clients.

import type { ChatRequest, ChatResponse } from "@dani-builder/agent-contract";
import { parseSseEvent } from "@dani-builder/agent-contract";

The package is safe to import from browser clients: it has no Strapi or plugin peer dependency. It defines chat request/response, retrieval citation, and SSE event shapes; it does not implement a chat runtime.

Follow-up question extensions support two wire versions. The existing FollowUpQuestionsExtension type remains version 1:

import type { FollowUpQuestionsExtension } from "@dani-builder/agent-contract";

const v1: FollowUpQuestionsExtension = {
  type: "follow_up_questions",
  version: 1,
  payload: { questions: ["Tell me more"] },
};

Version 2 separates the displayed label from an optional submitted prompt. Import FollowUpQuestionsV2Extension and SuggestedPrompt from the package root:

import type {
  FollowUpQuestionsExtension,
  FollowUpQuestionsV2Extension,
  ResponseExtension,
  SuggestedPrompt,
} from "@dani-builder/agent-contract";

const item: SuggestedPrompt = {
  label: "Pricing",
  prompt: "What does your service cost?",
};
const v2: FollowUpQuestionsV2Extension = {
  type: "follow_up_questions",
  version: 2,
  payload: { items: [item, { label: "Tell me more" }] },
};

function submittedPrompts(extension: ResponseExtension): string[] {
  if (extension.type !== "follow_up_questions") return [];
  if (extension.version === 1) return extension.payload.questions;
  return extension.payload.items.map((item) => item.prompt ?? item.label);
}

Discriminate on version after checking type: both versions belong to ResponseExtension, so exhaustive consumers must account for version 2. parseSseEvent accepts both standalone extension events and extensions in done events. For version 2, labels must be nonblank strings; missing, empty, or whitespace-only prompts are omitted so item.prompt ?? item.label falls back to the label. Nonblank strings are preserved, including surrounding whitespace. Non-string prompts and malformed items reject the extension; done events filter invalid extensions. Empty item arrays are valid. Version 1 parsing is unchanged.

Persistent sessions and inquiry drafts

SessionResponse contains the issued sessionId, plaintext token, and expiresAt. Send this token as Bearer auth for chat, restore, and cancel. Chat adds optional requestId for compatibility with standalone runtimes; the installed plugin content API requires it for admission and idempotent replay. ConversationRestoreResponse contains completed history, activePersona, and expiration. History messages can carry persona, previousPersona, and requestId, so the UI can identify the speaker after restoration.

persona_routing v1 carries persona, searchQuery and string topic. The plugin projects a structured planner topic to its title. Legacy intent and explicitInquiryRequest properties are ignored rather than propagated.

Response extensions

See Agent output extensions for the wire table and consumer-owned action/form migration. ResponseExtension contains UI blocks, persona routing, generic blocks and both follow-up versions. Removed inquiry_draft extensions are treated like unknown extension types: an individual extension event is ignored, and done.extensions retains valid siblings. InquiryDraftExtension and InquiryPrefill are no longer exported here; form prefill belongs to @dani-builder/blocks/forms.

版本与发布记录

@dani-builder/agent-contract 共发布 8 个版本,首次发布于 2026/09/14;过去 12 个月发布了 8 个版本。

版本发布日期距今
1.1.0latest2026/11/047 天前
1.0.02026/11/019 天前
0.6.02026/10/1725 天前
0.5.02026/10/1428 天前
0.4.02026/10/091 个月前
0.3.02026/10/071 个月前

关于 @dani-builder/agent-contract 的常见问题

@dani-builder/agent-contract 是什么?

Zero-dependency wire contracts for Daniworks Agent clients.

如何安装 @dani-builder/agent-contract?

在项目目录运行 npm install @dani-builder/agent-contract,或使用 pnpm add / yarn add / bun add。

@dani-builder/agent-contract 支持 TypeScript 吗?

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

@dani-builder/agent-contract 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dani-builder/agent-contract 需要什么版本的 Node.js?

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

@dani-builder/agent-contract 可以商用吗?

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

@dani-builder/agent-contract 的下载量有多少?

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

@dani-builder/agent-contract 还在维护吗?

最新版本 1.1.0 发布于 2026/11/04(7 天前);至今共发布 8 个版本,其中过去 12 个月发布了 8 个;首次发布于 2026/09/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。