Files
hermes-agent/apps/shared/src/circular-buffer.ts
alt-glitch ab334f6fdd refactor(tui): move the gateway client into @hermes/shared so other clients skip Ink's source
A third-party Node frontend had to compile ui-tui's private source to talk to the gateway:
gatewayClient.ts, canonicalGateway.ts, gatewayTypes.ts and the slash, history and provider
helpers beside them. hermes-tern bundles 36 such imports through a `.hermes-src` alias.

The renderer-free client now lives in apps/shared:

- src/node/gateway-client.ts: transport, ticketed dial, runtime.describe, the session.create
  contract, reconnect and bounded owner recovery, typed request/events (Node-only).
- src/gateway-canonical.ts, src/gateway-types.ts, src/gateway-text.ts: the hermes-gateway-v1
  request/result translation, the typed results (SessionInfo and SlashCategory with them) and
  the client's English text, keyed by Ink's catalog keys.
- src/slash-input.ts, slash-score.ts, provider-names.ts, interpolation.ts, circular-buffer.ts and
  src/node/prompt-history.ts: helpers with no Ink or React import, moved as they were.

Everything Ink-specific became a constructor option that Ink's own GatewayClient
(ui-tui/src/gatewayClient.ts) passes exactly as before: `connect` (its gateway_bootstrap.py
helper), the terminal-failure predicate, HERMES_TUI_GATEWAY_URL / _SIDECAR_URL, the startup and
RPC timeouts, the launch tool-progress pin, the crash-log breadcrumbs, its i18n `t`, and undici
as the WebSocket when the runtime has none. The launcher's env parsing stays in Ink
(launchOptions.ts). Node-only modules sit in src/node, which the web app's typecheck excludes;
the package typechecks src and src/node with separate configs, and boundary.test.ts fails if
its program ever includes another workspace's file. apps/shared now runs its tests in `check`.
2026-11-06 11:14:34 -05:00

51 lines
1.0 KiB
TypeScript

export class CircularBuffer<T> {
private buf: T[]
private readonly capacity: number
private head = 0
private len = 0
constructor(capacity: number) {
if (!Number.isInteger(capacity) || capacity <= 0) {
throw new RangeError(`CircularBuffer capacity must be a positive integer, got ${capacity}`)
}
this.capacity = capacity
this.buf = new Array<T>(capacity)
}
push(item: T) {
this.buf[this.head] = item
this.head = (this.head + 1) % this.capacity
if (this.len < this.capacity) {
this.len++
}
}
tail(n = this.len): T[] {
const take = Math.min(Math.max(0, n), this.len)
const start = this.len < this.capacity ? 0 : this.head
const out: T[] = new Array<T>(take)
for (let i = 0; i < take; i++) {
out[i] = this.buf[(start + this.len - take + i) % this.capacity]!
}
return out
}
drain(): T[] {
const out = this.tail()
this.clear()
return out
}
clear() {
this.buf = new Array<T>(this.capacity)
this.head = 0
this.len = 0
}
}