Flat config with two layers. Correctness (type-checked): the headline rules for this codebase are no-floating-promises / no-misused-promises (a lost promise in the agent loop is our primary bug class), switch-exhaustiveness-check (we switch over merge-extensible unions everywhere), no-unnecessary-condition, require-await, and no-explicit-any. Style (@stylistic): 2-space, no semicolons, single quotes, trailing commas, max-len 140 — the existing house style, now enforced instead of drifting between agents. vendor/ is excluded (vendored source keeps upstream style); tests relax the rules that fight test ergonomics (non-null assertions after expects, async mock signatures, non-Error throws). Code adjusted to pass: registry disposers wrap ctx.effect's promise-returning disposer behind a sync () => void (our public API), BlockAssembler gains an invariant-checking mustGet instead of non-null assertions, lastTurnNumber uses findLast, waterfall tails return Promise.resolve instead of async-without-await arrows, and the two deliberate suppressions (non-exhaustive derivation switch, unbound execute pass-through) carry justification comments. yarn lint / yarn lint:fix added.
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { createInterface } from 'node:readline'
|
|
import type { Context } from 'cordis'
|
|
import type {} from '@deepseek-ai/dsh-agent'
|
|
|
|
export const name = 'stdio-chat'
|
|
export const inject = ['agents']
|
|
|
|
/**
|
|
* Minimal UI plugin: reads lines from stdin → agent.send(); renders the
|
|
* agent's stream chunks and tool activity to stdout. Demonstrates that a UI
|
|
* is "just a plugin" — it only consumes the agent/* event taxonomy.
|
|
*/
|
|
export function apply(ctx: Context) {
|
|
ctx.on('agent/stream-chunk', (_agent, _turn, _step, chunk) => {
|
|
if (chunk.type === 'text-delta') process.stdout.write(chunk.text)
|
|
})
|
|
|
|
ctx.on('agent/turn-start', (agent, turn) => {
|
|
process.stdout.write(`\n[${agent.id} turn ${turn}] `)
|
|
})
|
|
|
|
ctx.on('agent/turn-end', () => {
|
|
process.stdout.write('\n> ')
|
|
})
|
|
|
|
ctx.on('session/event', (_session, event) => {
|
|
if (event.type === 'tool/call') {
|
|
const { name: toolName, arguments: args } = event.data
|
|
process.stdout.write(`\n [tool call] ${toolName}(${args})`)
|
|
} else if (event.type === 'tool/result') {
|
|
const { content } = event.data
|
|
const text = content.filter(b => b.type === 'text').map(b => b.text).join('')
|
|
process.stdout.write(`\n [tool result] ${text}\n `)
|
|
}
|
|
})
|
|
|
|
ctx.effect(() => {
|
|
const reader = createInterface({ input: process.stdin })
|
|
reader.on('line', (line) => {
|
|
const text = line.trim()
|
|
if (!text) return
|
|
const agent = ctx.agents.get('main')
|
|
if (!agent) {
|
|
console.error('agent "main" is not running')
|
|
return
|
|
}
|
|
if (agent.status === 'running') {
|
|
agent.steer([{ type: 'text', text }])
|
|
} else {
|
|
agent.send([{ type: 'text', text }])
|
|
}
|
|
})
|
|
reader.on('close', () => {
|
|
// allow the process to exit when stdin ends (piped input)
|
|
setTimeout(() => process.exit(0), 200)
|
|
})
|
|
process.stdout.write('echo-agent ready. Type a message ("echo <text>" triggers the tool).\n> ')
|
|
return () => { reader.close() }
|
|
}, 'stdio-chat')
|
|
}
|