diff options
Diffstat (limited to 'packages/cli/src/message.ts')
| -rw-r--r-- | packages/cli/src/message.ts | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/packages/cli/src/message.ts b/packages/cli/src/message.ts new file mode 100644 index 0000000..0c3f538 --- /dev/null +++ b/packages/cli/src/message.ts @@ -0,0 +1,55 @@ +/** + * Pure message composition — zero I/O. + * + * Combines text and file content into a single message string, + * and builds a ChatRequest from a parsed command. + */ + +import type { ChatRequest } from "@dispatch/transport-contract"; + +interface ComposeInput { + readonly text?: string; + readonly file?: string; + readonly fileContent?: string; +} + +function basename(filePath: string): string { + const segments = filePath.split("/"); + return segments[segments.length - 1] ?? filePath; +} + +export function composeMessage(input: ComposeInput): string { + const { text, fileContent } = input; + const file = input.file; + + if (text && file) { + return `${text}\n\nAttached file (${basename(file)}):\n${fileContent ?? ""}`; + } + if (file) { + return `Attached file (${basename(file)}):\n${fileContent ?? ""}`; + } + return text ?? ""; +} + +interface ChatCmd { + readonly modelName: string; + readonly text?: string | undefined; + readonly file?: string | undefined; + readonly cwd?: string | undefined; + readonly conversationId?: string | undefined; + readonly showReasoning: boolean; +} + +interface BuildCtx { + readonly cwd: string; + readonly message: string; +} + +export function buildChatRequest(cmd: ChatCmd, ctx: BuildCtx): ChatRequest { + return { + message: ctx.message, + model: cmd.modelName, + ...(cmd.conversationId !== undefined && { conversationId: cmd.conversationId }), + ...(cmd.cwd !== undefined ? { cwd: cmd.cwd } : { cwd: ctx.cwd }), + }; +} |
