UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

41 lines (40 loc) • 1.5 kB
export class ToolDetachHub { controllers = new Map(); static MAX_CONTROLLERS = 256; firedTombstones = new Set(); controllerFor(toolCallId) { let c = this.controllers.get(toolCallId); if (!c) { c = new AbortController(); if (this.firedTombstones.has(toolCallId)) c.abort(); this.controllers.set(toolCallId, c); if (this.controllers.size > ToolDetachHub.MAX_CONTROLLERS) { const oldest = this.controllers.keys().next().value; if (oldest !== undefined) { const evicted = this.controllers.get(oldest); if (evicted?.signal.aborted) { this.firedTombstones.add(oldest); if (this.firedTombstones.size > ToolDetachHub.MAX_CONTROLLERS) { const t = this.firedTombstones.values().next().value; if (t !== undefined) this.firedTombstones.delete(t); } } this.controllers.delete(oldest); } } } return c; } signalFor(toolCallId) { return this.controllerFor(toolCallId).signal; } request(toolCallId) { this.controllerFor(toolCallId).abort(); } gc(toolCallId) { this.controllers.delete(toolCallId); this.firedTombstones.delete(toolCallId); } }