openpets icon indicating copy to clipboard operation
openpets copied to clipboard

Architecture v2: OpenPets Event Kernel (Local Event Runtime Design)

Open kanade-EdTech opened this issue 5 months ago • 0 comments

OpenPets Event Kernel(架构 v2)

1. 定义升级

OpenPets × Everywhere bridge 已从“插件适配层”升级为:

OpenPets Event Kernel(OEK)——本地事件驱动运行时内核

它不再只是桥接层,而是统一管理:

  • 事件输入(Everywhere / MCP / CLI / UI)
  • 事件调度(优先级 / 合并 / 去重)
  • 事件语义(状态机映射)
  • 事件输出(OpenPets 动画 / UI / 音效 / MCP)

2. 核心设计目标

2.1 统一事件运行时

将所有外部输入抽象为 Event Stream,而不是“API调用”。

2.2 可预测调度

保证桌宠行为在高频事件下仍然稳定、不抖动、不重复响应。

2.3 防循环反馈

系统级防止 OpenPets ↔ Everywhere 双向触发死循环。

2.4 插件化事件处理链

允许第三方扩展:filter / reducer / renderer。


3. Kernel 总体架构

                +----------------------+
                |  External Sources    |
                |----------------------|
                | Everywhere Plugin    |
                | MCP CLI UI Events    |
                +----------+-----------+
                           |
                           v
                +----------------------+
                |   Ingress Layer      |
                | (Normalization)      |
                +----------+-----------+
                           |
                           v
        +--------------------------------------+
        |        Event Kernel Core             |
        |--------------------------------------|
        |  1. Guard (filter / security)        |
        |  2. Fingerprint Deduplication        |
        |  3. Priority Scheduler               |
        |  4. Coalescing Engine                |
        |  5. State Reducer (FSM)              |
        +----------------+---------------------+
                         |
                         v
                +----------------------+
                |   Egress Layer       |
                |----------------------|
                | OpenPets MCP Bridge  |
                | UI Renderer          |
                | Sound/Animation      |
                +----------------------+

4. 核心对象模型

4.1 Event

interface KernelEvent {
  id: string;
  type: string;
  source: 'everywhere' | 'openpets' | 'cli' | 'ui';
  timestamp: number;
  payload: any;
}

4.2 NormalizedEvent

interface NormalizedEvent {
  type: string;
  priority: number;
  fingerprint: string;
  payload: {
    mood: 'idle' | 'thinking' | 'working' | 'happy' | 'sad';
    bubble: string;
    count?: number;
  };
}

5. Kernel 核心算法(Event Execution Model)

5.1 Ingress Pipeline

function ingest(event: KernelEvent) {
  if (!guard(event)) return;

  const normalized = normalize(event);
  enqueue(normalized);
}

5.2 Guard(系统防火墙)

function guard(e: KernelEvent) {
  // 防循环
  if (e.payload?.fromKernel) return false;

  // 白名单
  const allowed = new Set([
    'TaskStarted',
    'TaskSucceeded',
    'TaskFailed',
    'TaskWaitingApproval',
    'UserClickedPet'
  ]);

  return allowed.has(e.type);
}

5.3 Fingerprint 去重

function fingerprint(e: KernelEvent) {
  return `${e.type}:${e.source}:${hash(e.payload)}`;
}

5.4 Priority Scheduler

const priority = {
  TaskFailed: 5,
  TaskWaitingApproval: 4,
  TaskStarted: 3,
  TaskSucceeded: 2,
  Idle: 1
};

5.5 Coalescing Engine

function coalesce(events) {
  const map = new Map();

  for (const e of events) {
    if (!map.has(e.type)) {
      map.set(e.type, e);
    } else {
      map.get(e.type).payload.count++;
    }
  }

  return [...map.values()];
}

5.6 Event Loop Execution

async function flush(queue) {
  queue.sort(byPriority);

  const merged = coalesce(queue);

  for (const event of merged) {
    const state = reduce(event);
    await dispatch(state);
  }
}

6. State Reducer(FSM 核心)

function reduce(event) {
  switch (event.type) {
    case 'TaskStarted':
      return { mood: 'working', bubble: 'Working...' };
    case 'TaskSucceeded':
      return { mood: 'happy', bubble: 'Done' };
    case 'TaskFailed':
      return { mood: 'sad', bubble: 'Failed' };
    case 'TaskWaitingApproval':
      return { mood: 'thinking', bubble: 'Waiting...' };
    default:
      return { mood: 'idle', bubble: '' };
  }
}

7. Egress Layer

输出通道

  • OpenPets MCP (openpets_react, openpets_say)
  • UI 状态渲染
  • 动画系统

失败降级策略

  • MCP 不可用 → 本地 queue buffer
  • UI 不可用 → silent mode

8. 防循环机制(关键升级)

function loopGuard(event) {
  return !event.payload?.fromKernel;
}
  • fingerprint cache(TTL 5s)
  • source tracking graph

9. 性能模型

  • batch window: 100–200ms
  • debounce UI events: 300–500ms
  • max queue size: 1000
  • overflow strategy: drop low priority events

10. 插件扩展模型

Pipeline 插件

  • filter(event)
  • map(event)
  • reduce(events)

允许用户扩展 Kernel 行为


11. 系统演进路径

Phase 1

单向 Everywhere → Kernel → OpenPets

Phase 2

加入 UI / CLI 输入

Phase 3

双向事件流

Phase 4

插件市场 + DSL 规则引擎


12. 总结

OpenPets Event Kernel 的本质是:

一个本地事件驱动的轻量 runtime(类似 mini ROS / Electron IPC + FSM + scheduler)

它将 OpenPets 从“桌宠应用”升级为:

可编排的本地 AI 交互运行时系统

kanade-EdTech avatar Jul 04 '26 04:07 kanade-EdTech