AI治理事件流

实时流式传输AI代理行为的治理事件,包括目的绑定、策略检查和证据追踪。

  • by weops-io
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 weops-io/stream-types

使用说明

@weops/流类型

代理人提议。策略引擎进行了部署。

与AI代理响应一起流式传输的类型化治理事件。目的约束、政策检查、证据追踪——实时。

问题

您的AI代理已连接57个工具。它可以读取文件、写入数据库、推送提交和调用外部API。你的经纪人和 git push to main 是共鸣。

没有记录哪些工具被调用以及为什么被调用。无法确定每个任务的权限范围。当代理超出范围时,不会升级。只是。..执行。

解决方案

@weops/stream-types 定义 7种类型的治理事件 该流与您的AI响应并排。每次工具调用都包含一个目的声明、一个策略检查结果(PERMIT/DENY+reason)和一个证据包。你可以实时看到代理在做什么,为什么它认为这是允许的,以及策略引擎是否同意。

npm install @weops/stream-types

七种事件类型

# 事件 它带来了什么
1 工单 目的+角色+生命周期(草稿→ 批准→ 跑步→ 已完成)
2 政策 允许或拒绝+人类可读的理由+匹配的规则+义务
3 升级 7级严重性阶梯+触发+所需解决方案
4 证据 不可变哈希链接包+来源引用
5 文本Delta AI响应块(实际答案)
6 工具调用 工具调用生命周期(建议→ 允许→ 执行→ 已完成)
7 源 引用+证据URI

快速开始

导入类型

import type {
  GovernanceEvent,
  WorkOrderEvent,
  PolicyEvent,
  EscalationEvent,
} from '@weops/stream-types';

function handleEvent(event: GovernanceEvent) {
  switch (event.type) {
    case 'work-order':
      console.log(`[${event.status}] ${event.purpose} as ${event.role}`);
      break;
    case 'policy':
      console.log(`[${event.decision}] ${event.reason}`);
      break;
    case 'escalation':
      console.log(`[RUNG ${event.rung}] ${event.trigger}: ${event.action}`);
      break;
    case 'tool-call':
      console.log(`[${event.status}] ${event.tool}(${JSON.stringify(event.args)})`);
      break;
  }
}

使用类型防护装置

import {
  isPolicy,
  isDeny,
  isEscalation,
  isBlocking,
} from '@weops/stream-types';

function processEvent(event: GovernanceEvent) {
  if (isPolicy(event) && isDeny(event)) {
    showDenialBanner(event.reason);
  }

  if (isEscalation(event) && isBlocking(event)) {
    pauseAndRequestApproval(event.action);
  }
}

使用流工具

import {
  filterEvents,
  getDenials,
  assembleText,
  getFinalStatus,
} from '@weops/stream-types';

// Get all denials from a completed stream
const denials = getDenials(stream);

// Reconstruct the full AI response
const fullText = assembleText(stream);

// Check how the interaction ended
const status = getFinalStatus(stream); // 'COMPLETED' | 'FAILED' | ...

消费SSE流

import { SSE_EVENT_NAMES } from '@weops/stream-types';
import type { GovernanceEvent } from '@weops/stream-types';

const source = new EventSource('/api/chat');

// Listen to all governance event types
SSE_EVENT_NAMES.forEach(name => {
  if (name === 'done') {
    source.addEventListener('done', () => source.close());
  } else {
    source.addEventListener(name, (e) => {
      const event: GovernanceEvent = JSON.parse(e.data);
      handleEvent(event);
    });
  }
});

模拟发展事件

在没有运行代理的情况下构建和测试具有治理意识的UI:

import {
  mockStream,
  mockDeniedStream,
  replayStream,
  mockWorkOrder,
  mockDeny,
  mockEscalation,
} from '@weops/stream-types/mock';

// Generate a complete successful interaction
const events = mockStream('FileSystem.Read', 'DEVELOPER');

// Generate an interaction where a tool call gets denied
const denied = mockDeniedStream('FileSystem.Read', 'git.push', 'DEVELOPER');

// Replay with realistic timing (great for demos)
const { cancel } = replayStream(denied, (event, index) => {
  console.log(`Event ${index}:`, event.type, event);
}, 400); // 400ms between events

// Build individual events
const wo = mockWorkOrder({ purpose: 'Git.Write', role: 'DEVELOPER', status: 'DRAFT' });
const deny = mockDeny('git.push', 'Git.Write not in scope for FileSystem.Read');
const esc = mockEscalation({ rung: 3, trigger: 'HIGH_IMPACT_ACTION' });

建筑

Your Client ──► WeOps Proxy ──► AI Backend (Anthropic, OpenAI, local)
                    │
                    ├─ Intercepts tool calls
                    ├─ Evaluates against purpose-scoped policies
                    ├─ Emits governance events into the stream
                    └─ Passes AI response through unchanged

治理代理位于您的客户端和AI后端之间。它读取响应流,根据策略评估工具调用,并注入治理事件。 后端无任何更改。

治理差距

Capability  ──────────────────────────────────  MCP handles this.
                                                Tool discovery, transport, execution.

            ◄── THE GAP ──►

Authority   ──────────────────────────────────  @weops/stream-types handles this.
                                                Purpose binding, policy evaluation,
                                                evidence trails, escalation.

MCP告诉您的代理人有哪些工具可用。 @weops/stream-types 告诉你代理人是否 应该 使用它们 这个特定的任务.

扶梯

Rung 名称 发生了什么
0 澄清 代理人在继续之前要求提供缺失的事实
1 通知 通知治理所有者
2 批准 需要明确批准才能继续
3 双重控制 需要两个独立的批准
4 暂停 工单暂停,上报给保单持有人
5 停止 完全停止,需要手动审查
6 打破玻璃 强制事后审计的紧急旁路

生态系统

@weops/stream-types 是领域中立的治理层。特定于域的程序集添加特定于上下文的策略:

  • LoveFlow --具有原子性保证的多供应商商务协调
  • MiddleCare --符合HIPAA标准的医疗保健人工智能治理
  • CareGraph --作为建筑属性的护理连续性

链接

许可证

麻省理工学院——见 许可证.