| 1 | import { Service } from '@deepseek-ai/cordis' |
| 2 | import { ErrorCode } from '../protocol.ts' |
| 3 | import { RpcError } from '../rpc.ts' |
| 4 | import type { DshAdapter } from './adapters.ts' |
| 5 | |
| 6 | const SESSION = /^[A-Za-z0-9._-]{1,64}$/ |
| 7 | |
| 8 | function invalid(message: string): never { |
| 9 | throw new RpcError(ErrorCode.InvalidParams, message) |
| 10 | } |
| 11 | |
| 12 | function session(value: unknown): string { |
| 13 | if (typeof value !== 'string' || !SESSION.test(value)) invalid('a terminal name is 1 to 64 letters, digits, `_`, `-` or `.`') |
| 14 | return value as string |
| 15 | } |
| 16 | |
| 17 | function text(value: unknown, what: string): string { |
| 18 | if (typeof value !== 'string' || value.length === 0) invalid(`${what} is a non-empty string`) |
| 19 | return value as string |
| 20 | } |
| 21 | |
| 22 | function bounded(value: unknown, what: string, min: number, max: number): number | undefined { |
| 23 | if (value === undefined) return undefined |
| 24 | if (typeof value !== 'number' || !Number.isInteger(value) || value < min || value > max) invalid(`${what} is an integer between ${min} and ${max}`) |
| 25 | return value as number |
| 26 | } |
| 27 | |
| 28 | function compact(input: Record<string, unknown>): Record<string, unknown> { |
| 29 | return Object.fromEntries(Object.entries(input).filter(([, value]) => value !== undefined)) |
| 30 | } |
| 31 | |
| 32 | export const terminalAdapter: DshAdapter = { |
| 33 | service: 'terminal', |
| 34 | define(host) { |
| 35 | class TerminalService extends Service { |
| 36 | constructor(ctx: any) { super(ctx, 'terminal') } |
| 37 | list() { return host.broker(this.ctx, 'terminal', 'list', {}) } |
| 38 | run(command: unknown, options?: { session?: unknown, timeoutSecs?: unknown }) { |
| 39 | return host.broker(this.ctx, 'terminal', 'run', compact({ |
| 40 | command: text(command, 'a command'), |
| 41 | session: options?.session === undefined ? undefined : session(options.session), |
| 42 | timeout_secs: bounded(options?.timeoutSecs, 'timeoutSecs', 1, 600), |
| 43 | })) |
| 44 | } |
| 45 | send(name: unknown, input: unknown, options?: { waitMs?: unknown }) { |
| 46 | return host.broker(this.ctx, 'terminal', 'send', compact({ |
| 47 | session: session(name), |
| 48 | text: text(input, 'terminal input'), |
| 49 | wait_ms: bounded(options?.waitMs, 'waitMs', 0, 60_000), |
| 50 | })) |
| 51 | } |
| 52 | wait(name: unknown, options?: { timeoutSecs?: unknown }) { |
| 53 | return host.broker(this.ctx, 'terminal', 'wait', compact({ |
| 54 | session: session(name), |
| 55 | timeout_secs: bounded(options?.timeoutSecs, 'timeoutSecs', 1, 600), |
| 56 | })) |
| 57 | } |
| 58 | cancel(name: unknown) { return host.broker(this.ctx, 'terminal', 'cancel', { session: session(name) }) } |
| 59 | reset(name: unknown) { return host.broker(this.ctx, 'terminal', 'reset', { session: session(name) }) } |
| 60 | } |
| 61 | Object.freeze(TerminalService.prototype) |
| 62 | return TerminalService |
| 63 | }, |
| 64 | } |
| 65 |