Show HN:Id-agent – AI 代理的代币高效 UUID 替代方案

2026-05-19 1 阅读 pranshuchittora
id-agent 用于 AI 代理的代币高效 ID 在 UUID 花费约 23 个代币并被 LLM 产生幻觉的情况下,id-agent 会以约 14 个代币生成令人难忘的基于单词的 ID,并具有同等的抗碰撞性。第一个为上下文窗口而不是数据库构建的 ID 库。人类可读——人类和法学硕士实际上可以记住的基于单词的 ID 令牌效率——单词列表中的每个单词恰好是 o200k_base 上的 1 个 BPE 令牌 碰撞安全——从 ~12 到 ~192 位的可配置熵 验证输入——所有公共 API 上由 zod 驱动的模式验证 令牌成本比较格式示例令牌 (o200k_base) 防冲突 UUID v4 89b842d9-6df9-4cf4-8db0-9dc3aed3cfd7 ~23 122 位 id-agent(默认,8 个字) urd-antes-sorry-pac-dire-total-expire-going ~14 ~96 位 id-agent(5 个字) frame-beer-bell-tog-hoot ~8 ~60 位 id-agent — 8 个字, 43 个字符,12 个令牌 UUID v4 — 36 个字符,26 个令牌 安装 npm install id-agent pnpm add id-agent 快速入门 import { idAgent } from 'id-agent' // 生成随机 ID(8 个字,~96 位熵) const id = idAgent ( ) // => "urd-antes-sorry-pac-dire-total-expire-going" // 使用类型前缀 const taskId = idAgent ( { prefix : 'task' } ) // => "task_slide-exact-cede-bury-linge-ease-bean-impact" // 短期 ID 的字数更少 const short = idAgent ( { Words : 3 } ) // => “front-reject-tho” API 参考 idAgent(opts?) 生成一个随机的、人类可读的 ID。 import { idAgent } from 'id-agent' idAgent ( ) // 8 个字,约 96 位 idAgent ( { Words : 5 } ) // 5 个字,约 60 位 idAgent ( { prefix : 'user' } ) // "user_cloud-train-scope-frame-match-level-paint-field" 选项: 选项 类型 默认 说明 prefix string undefined 类型前缀(仅限小写字母数字)Words number 8 字数 (1-16)。控制熵:字数 * 12 位 无效选项会引发 ZodError 并带有描述性消息。 idAgent.from(input, opts?) 使用 HMAC-SHA256 从字符串输入生成确定性 ID。相同的输入总是产生相同的 ID。 const id = 等待 idAgent 。 from ( 'user@example.com' ) // 对于相同的输入始终返回相同的 ID const namespaced = wait idAgent . from ( 'user@example.com' , { namespace : 'my-app' , prefix : 'user' , Words : 5 , } ) 选项: 选项 类型 默认 说明 prefix string undefined 类型前缀(仅限小写字母数字) 单词数 8 单词数 (1-16) 命名空间字符串 'id-agent' 用于域分离的 HMAC 密钥 parse(id) 将任何 id-agent ID 解析为其组件。支持连字符分隔和下划线分隔的单词。对于无法识别的格式返回 null。 import { parse } from 'id-agent' parse ( 'task_storm-delta-stone' ) // => { prefix: 'task', Words: ['storm', 'delta', 'stone'], wordCount: 3, bits: 36, raw: 'task_storm-delta-stone', format: 'read' } parse ( 'task_storm_delta_stone' ) // => { prefix: 'task', Words: ['storm', 'delta', 'stone'], wordCount: 3, 位: 36, raw: 'task_storm_delta_stone', format: 'readable' } validate(id) 检查字符串是否是有效的 id-agent ID。验证 WORDLIST 中是否存在所有单词。 import { validate } from 'id-agent' validate ( 'storm-delta-stone' ) // => { valid: true, prefix: undefined, wordCount: 3 } validate ( 'task_jump-notaword' ) // => { valid: false, Reason: '未知单词: notaword' } validate ( 'INVALID' ) // => { valid: false, Reason: '包含大写字符' } createAliasMap(opts) 创建双向别名映射以减少 LLM 上下文中的标记。将长 ID 映射到基于单词的短别名,并提供完整的替换/恢复支持。 import { createAliasMap } from 'id-agent' constaliases = createAliasMap({words:3})aliases 。 set ( '8cdda07b-85d2-459c-8a2a-83c8f9245dbe' ) // => "storm-delta-stone" (来自 WORDLIST 的 3 个随机单词)别名 . get ( 'storm-delta-stone' ) // => "8cdda07b-85d2-459c-8a2a-83c8f9245dbe" // 在发送到 LLM 之前替换文本中的所有 UUID const text = 'Process 8cdda07b-85d2-459c-8a2a-83c8f9245dbe then 6ba7b810-9dad-11d1-80b4-00c04fd430c8' const 缩短=别名。 Replace ( text , {pattern : / [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4