@agentcomms/events

中文文档
0.37.0发布于 2026/10/29MITTypeScriptES Module构建来源已验证近 30 天下载 3520

The event catalogue, conditions and mapping templates of agent-communications' local event emission: isomorphic, with no I/O and no Node imports.

原始使用文档
文档目录12 节
  1. API
  2. JSON, text and errors
  3. Event identity
  4. Unicode, domains and semantic formats
  5. JSON Pointers and patterns
  6. Catalogue and source schemas
  7. Conditions
  8. Mapping and provenance
  9. CloudEvents wire format
  10. Workflow text
  11. Unicode data and licence
  12. Licence

@agentcomms/events

@agentcomms/events is the isomorphic library behind agent-communications’ local event emission: the version-1 event catalogue, pointer patterns, semantic formats, deterministic conditions, mapping templates and CloudEvents wire format. Appendix A of the event-emission design is its normative contract.

It has no I/O. The same build runs in Node and browser webviews, imports no node: module, and makes no network, file-system, clock or randomness call. Sanitising, untrusted-content envelopes, signing and delivery belong to the daemon that uses this library; this package reports source-field and mapped-value provenance instead.

It is published with every release from 0.16.0, with @agentcomms/events-daemon, the local service that uses it.

API

Import the pieces you need from the package root. The barrel is the complete supported surface; internal modules are not public API.

JSON, text and errors

canonicalJson is byte-identical to core’s canonical JSON. It sorts object keys by UTF-16 code units, drops undefined object members and refuses non-JSON input with EventsError; validators otherwise return Result<T> with closed IssueCode values.

import { canonicalJson, codePointLength, isJsonValue } from '@agentcomms/events';

const value = { second: undefined, first: ['é'] };
if (isJsonValue(value)) {
  canonicalJson(value); // '{"first":["é"]}'
  codePointLength('🛰'); // 1
}

Event identity

eventId derives D3’s stable, account-scoped 32-character ID through WebCrypto. Keep its canonical preimage with the ID so compareEventIdentities can distinguish a repeat from the theoretical truncated-hash collision.

import { eventId, eventIdPreimage } from '@agentcomms/events';

const identity = {
  installationId: 'installation-id',
  accountId: 'account-id',
  eventType: 'gmail.message.received',
  typeVersion: 1,
  dedupeKey: '["provider-occurrence","message","received"]',
};
const preimage = eventIdPreimage(identity);
const id = await eventId(identity);

Unicode, domains and semantic formats

The library pins NFC, full case folding and IDNA handling to Unicode 15.1. toAsciiDomain returns a Result; use isFormat for the catalogue’s five semantic formats.

import { UNICODE_VERSION, caseFold, isFormat, toAsciiDomain } from '@agentcomms/events';

UNICODE_VERSION; // '15.1.0'
caseFold('STRASSE'); // 'strasse'
toAsciiDomain('Bücher.example'); // { ok: true, value: 'xn--bcher-kva.example' }
isFormat('date-time', '2026-10-07T12:00:00Z'); // true

JSON Pointers and patterns

Pointers follow RFC 6901. Patterns add { any: true } only for array indices and expand to the concrete non-null values present in an event.

import { expandPattern, getPointer } from '@agentcomms/events';

const source = { recipients: [{ address: '[email protected]' }] };
getPointer(source, '/recipients/0/address'); // { found: true, value: '[email protected]' }
expandPattern(['recipients', { any: true }, 'address'], source); // ['/recipients/0/address']

Catalogue and source schemas

CATALOGUE contains precisely the seven version-1 definitions. A definition owns its Zod schema, metadata patterns, examples, source JSON Schema and named invariants.

import { CATALOGUE, catalogueEntry, sourceSchema, validateEvent } from '@agentcomms/events';

const found = catalogueEntry('gmail.message.received', 1);
if (found.ok) {
  const event = found.value.examples[0];
  validateEvent(found.value, event); // { ok: true, value: event }
  sourceSchema(found.value); // JSON Schema 2020-12 for this exact source type
}
CATALOGUE.length; // 7

Conditions

Canonicalise a condition against the selected definition before persisting it. The omitted case-sensitivity flag becomes explicit false, so canonical JSON and workflow digests are stable.

import { CATALOGUE, canonicaliseCondition, evaluateCondition } from '@agentcomms/events';

const definition = CATALOGUE[0];
const condition = canonicaliseCondition(definition, { path: '/subject', op: 'contains', value: 'status' });
if (condition.ok) evaluateCondition(definition, condition.value, definition.examples[0]);

canonicaliseAgenticCondition checks the static agentic form and its deterministic content-field prefilter. It does not execute a judge.

Mapping and provenance

Mapping copies clean, typed source values; constants never inherit provenance. Classifying the mapped result tells a daemon exactly which output pointers are untrusted, addresses or scoped handles before it applies its chosen representation.

import { CATALOGUE, classifyMapped, compileMapping, evaluateMapping } from '@agentcomms/events';

const definition = CATALOGUE[0];
const mapped = compileMapping(definition, { subject: { $path: '/subject' } });
if (mapped.ok) {
  const value = evaluateMapping(mapped.value, definition.examples[0]);
  classifyMapped(definition, definition.examples[0], value);
}

deliverySchema and deliverySchemaId describe the exact plain or enveloped delivery representation. The daemon selects that representation and its transport boundary; this package supplies the deterministic mapping primitives.

CloudEvents wire format

After mapping and representation, the daemon supplies stable delivery fields to buildCloudEvent. The library derives the non-overridable type, subject, time and canonical agentcommsuntrusted extension; cloudEventBytes gives the canonical JSON string to UTF-8 encode for transport.

import { CATALOGUE, buildCloudEvent, cloudEventBytes } from '@agentcomms/events';

const definition = CATALOGUE[0];
const event = definition.examples[0];
const envelope = buildCloudEvent(definition, event, {
  deliveryId: 'delivery-id',
  installationId: 'installation-id',
  ruleId: 'rule-id',
  ruleVersion: 1,
  targetId: 'target-id',
  targetVersion: 1,
  data: { subject: event.subject },
  untrusted: ['/subject'],
});
cloudEventBytes(envelope);

TEST_CLOUD_EVENT, TEST_CLOUD_EVENT_BYTES and JUDGE_TEST_INPUT are fixed test-only contract values, not source events and not caller-configurable templates.

Workflow text

printRuleText writes a workflow (its document, a rule document in the code's names) as a short text a person can read and edit, and parseRuleText reads it back to exactly the same document. Printing the same document always gives the same text. Anything the readable form has no words for is written as raw JSON in its place, so nothing is dropped. The parser checks the text only: the event service still checks the workflow when the document is submitted, and the canonical JSON is still what is approved. The grammar is in src/rule-text/GRAMMAR.md; from 0.36.0 the printer starts a document with workflow, and the parser still reads the older rule. From 0.33.0 a target may be a Linear issue: linear issue billing-issues, version 1, in team CUE, label Billing, assigned to me, with its ids, title and description on the lines below.

import { parseRuleText, printRuleText } from '@agentcomms/events';

const text = printRuleText(ruleDocument);
// workflow invoices, version 4
//
// when a new message arrives in cue/gmail, in the inbox
// match any
//   subject contains "invoice"
//   subject contains "payment"
// …
const parsed = parseRuleText(text);
if (parsed.ok) parsed.doc; // deep-equals ruleDocument
else parsed.errors; // [{ line, column, message }], e.g. 'unknown keyword "thne"; did you mean "then"? …'

Unicode data and licence

The generated tables and their pinned sources are Unicode 15.1.0. In a repository checkout, run pnpm sync:unicode to regenerate the checked-in tables from the pinned vendor files. The Unicode Character Database is licensed under Unicode-3.0; its complete notice is in THIRD_PARTY_LICENSES.

Licence

MIT. See LICENSE and THIRD_PARTY_LICENSES.

版本与发布记录

@agentcomms/events 共发布 28 个版本,首次发布于 2026/10/13;过去 12 个月发布了 28 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.37.0latest2026/10/299 天前
0.36.02026/10/2810 天前
0.35.02026/10/2811 天前
0.34.02026/10/2711 天前
0.33.02026/10/2712 天前
0.32.02026/10/2612 天前

关于 @agentcomms/events 的常见问题

@agentcomms/events 是什么?

The event catalogue, conditions and mapping templates of agent-communications' local event emission: isomorphic, with no I/O and no Node imports.

如何安装 @agentcomms/events?

在项目目录运行 npm install @agentcomms/events,或使用 pnpm add / yarn add / bun add。

@agentcomms/events 支持 TypeScript 吗?

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

@agentcomms/events 支持 ESM 还是 CommonJS?

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

@agentcomms/events 需要什么版本的 Node.js?

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

@agentcomms/events 可以商用吗?

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

@agentcomms/events 的下载量有多少?

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

@agentcomms/events 还在维护吗?

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