52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import { iterateTerminalInputChunks, TERMINAL_INPUT_CHUNK_MAX_BYTES } from './terminal-input'
|
|
|
|
export const AGENT_PROMPT_BRACKETED_PASTE_START = '\x1b[200~'
|
|
export const AGENT_PROMPT_BRACKETED_PASTE_END = '\x1b[201~'
|
|
export const AGENT_PROMPT_SUBMIT = '\r'
|
|
|
|
const DEFAULT_AGENT_PROMPT_SUBMIT_DELAY_MS = 500
|
|
const WINDOWS_AGENT_PROMPT_SUBMIT_DELAY_MS = 1_500
|
|
|
|
// Why: ConPTY renders long bracketed pastes more slowly; an early Enter leaves the task in the agent input buffer.
|
|
export function getAgentPromptSubmitDelayMs(platform: NodeJS.Platform): number {
|
|
return platform === 'win32'
|
|
? WINDOWS_AGENT_PROMPT_SUBMIT_DELAY_MS
|
|
: DEFAULT_AGENT_PROMPT_SUBMIT_DELAY_MS
|
|
}
|
|
|
|
export const AGENT_PROMPT_SUBMIT_DELAY_MS = getAgentPromptSubmitDelayMs(process.platform)
|
|
|
|
const ESCAPE = '\x1b'
|
|
const INERT_ESCAPE = '<ESC>'
|
|
|
|
export function sanitizeAgentPromptText(text: string): string {
|
|
let escapeIndex = text.indexOf(ESCAPE)
|
|
if (escapeIndex === -1) {
|
|
return text
|
|
}
|
|
|
|
let sanitized = ''
|
|
let start = 0
|
|
while (escapeIndex !== -1) {
|
|
sanitized += `${text.slice(start, escapeIndex)}${INERT_ESCAPE}`
|
|
start = escapeIndex + ESCAPE.length
|
|
escapeIndex = text.indexOf(ESCAPE, start)
|
|
}
|
|
return sanitized + text.slice(start)
|
|
}
|
|
|
|
export function buildAgentPromptPasteBytes(prompt: string): string {
|
|
return `${AGENT_PROMPT_BRACKETED_PASTE_START}${sanitizeAgentPromptText(prompt)}${AGENT_PROMPT_BRACKETED_PASTE_END}`
|
|
}
|
|
|
|
export function buildAgentPromptSubmitBytes(): string {
|
|
return AGENT_PROMPT_SUBMIT
|
|
}
|
|
|
|
export function* iterateAgentPromptPasteChunks(
|
|
prompt: string,
|
|
maxChunkBytes = TERMINAL_INPUT_CHUNK_MAX_BYTES
|
|
): Generator<string> {
|
|
yield* iterateTerminalInputChunks(buildAgentPromptPasteBytes(prompt), maxChunkBytes)
|
|
}
|