| 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 MAX_PATH = 512 |
| 7 | const MAX_WRITE = 1024 * 1024 |
| 8 | const MAX_ARGV = 9 |
| 9 | const MAX_ARG = 1024 |
| 10 | |
| 11 | function invalid(message: string): never { |
| 12 | throw new RpcError(ErrorCode.InvalidParams, message) |
| 13 | } |
| 14 | |
| 15 | function path(value: unknown): string { |
| 16 | if (typeof value !== 'string' || value.length === 0 || value.length > MAX_PATH || value.trim() !== value || /[\x00-\x1f\x7f]/.test(value)) { |
| 17 | invalid(`a path is 1 to ${MAX_PATH} characters on one line with no surrounding spaces`) |
| 18 | } |
| 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 argv(value: unknown): string[] { |
| 29 | if (!Array.isArray(value) || value.length === 0 || value.length > MAX_ARGV || value.some(arg => typeof arg !== 'string' || arg.length > MAX_ARG || /[\x00-\x1f\x7f]/.test(arg))) { |
| 30 | invalid(`argv is 1 to ${MAX_ARGV} strings of at most ${MAX_ARG} characters`) |
| 31 | } |
| 32 | return [...value as string[]] |
| 33 | } |
| 34 | |
| 35 | function compact(input: Record<string, unknown>): Record<string, unknown> { |
| 36 | return Object.fromEntries(Object.entries(input).filter(([, value]) => value !== undefined)) |
| 37 | } |
| 38 | |
| 39 | export const workspaceAdapter: DshAdapter = { |
| 40 | service: 'workspace', |
| 41 | define(host) { |
| 42 | class WorkspaceService extends Service { |
| 43 | constructor(ctx: any) { super(ctx, 'workspace') } |
| 44 | read(file: unknown, options?: { startLine?: unknown, maxLines?: unknown }) { |
| 45 | return host.broker(this.ctx, 'workspace', 'read', compact({ |
| 46 | path: path(file), |
| 47 | start_line: bounded(options?.startLine, 'startLine', 1, Number.MAX_SAFE_INTEGER), |
| 48 | max_lines: bounded(options?.maxLines, 'maxLines', 1, 10_000), |
| 49 | })) |
| 50 | } |
| 51 | list(dir?: unknown) { |
| 52 | return host.broker(this.ctx, 'workspace', 'list', compact({ path: dir === undefined ? undefined : path(dir) })) |
| 53 | } |
| 54 | write(file: unknown, content: unknown, options?: { expectedHash?: unknown }) { |
| 55 | if (typeof content !== 'string' || Buffer.byteLength(content) > MAX_WRITE) invalid(`content is a string of at most ${MAX_WRITE} bytes`) |
| 56 | const hash = options?.expectedHash |
| 57 | if (hash !== undefined && (typeof hash !== 'string' || hash.length === 0 || hash.length > 128)) invalid('expectedHash is 1 to 128 characters') |
| 58 | return host.broker(this.ctx, 'workspace', 'write', compact({ path: path(file), content, expected_hash: hash })) |
| 59 | } |
| 60 | exec(command: unknown, options?: { cwd?: unknown }) { |
| 61 | return host.broker(this.ctx, 'workspace', 'exec', compact({ |
| 62 | argv: argv(command), |
| 63 | cwd: options?.cwd === undefined ? undefined : path(options.cwd), |
| 64 | })) |
| 65 | } |
| 66 | } |
| 67 | Object.freeze(WorkspaceService.prototype) |
| 68 | return WorkspaceService |
| 69 | }, |
| 70 | } |
| 71 |