mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-29 01:31:41 +00:00
feat: new tui based on ink
This commit is contained in:
parent
624ad582a5
commit
2ea5345a7b
13 changed files with 4177 additions and 0 deletions
72
ui-tui/src/gatewayClient.ts
Normal file
72
ui-tui/src/gatewayClient.ts
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import { spawn, type ChildProcess } from 'node:child_process'
|
||||
import { createInterface } from 'node:readline'
|
||||
import { EventEmitter } from 'node:events'
|
||||
import { resolve } from 'node:path'
|
||||
|
||||
export interface GatewayEvent {
|
||||
type: string
|
||||
session_id?: string
|
||||
payload?: Record<string, unknown>
|
||||
}
|
||||
|
||||
interface Pending {
|
||||
resolve: (v: unknown) => void
|
||||
reject: (e: Error) => void
|
||||
}
|
||||
|
||||
export class GatewayClient extends EventEmitter {
|
||||
private proc: ChildProcess | null = null
|
||||
private reqId = 0
|
||||
private pending = new Map<string, Pending>()
|
||||
|
||||
start() {
|
||||
const root = resolve(import.meta.dirname, '../../')
|
||||
|
||||
this.proc = spawn(
|
||||
process.env.HERMES_PYTHON ?? resolve(root, 'venv/bin/python'),
|
||||
['-m', 'tui_gateway.entry'],
|
||||
{ cwd: root, stdio: ['pipe', 'pipe', 'inherit'] },
|
||||
)
|
||||
|
||||
createInterface({ input: this.proc.stdout! }).on('line', (raw) => {
|
||||
try { this.dispatch(JSON.parse(raw)) } catch {}
|
||||
})
|
||||
|
||||
this.proc.on('exit', (code) => this.emit('exit', code))
|
||||
}
|
||||
|
||||
private dispatch(msg: Record<string, unknown>) {
|
||||
const id = msg.id as string | undefined
|
||||
const p = id ? this.pending.get(id) : undefined
|
||||
|
||||
if (p) {
|
||||
this.pending.delete(id!)
|
||||
msg.error
|
||||
? p.reject(new Error((msg.error as any).message))
|
||||
: p.resolve(msg.result)
|
||||
return
|
||||
}
|
||||
|
||||
if (msg.method === 'event')
|
||||
this.emit('event', msg.params as GatewayEvent)
|
||||
}
|
||||
|
||||
request(method: string, params: Record<string, unknown> = {}): Promise<unknown> {
|
||||
const id = `r${++this.reqId}`
|
||||
|
||||
this.proc!.stdin!.write(
|
||||
JSON.stringify({ jsonrpc: '2.0', id, method, params }) + '\n',
|
||||
)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.pending.set(id, { resolve, reject })
|
||||
|
||||
setTimeout(() => {
|
||||
if (this.pending.delete(id))
|
||||
reject(new Error(`timeout: ${method}`))
|
||||
}, 30_000)
|
||||
})
|
||||
}
|
||||
|
||||
kill() { this.proc?.kill() }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue