summaryrefslogtreecommitdiffhomepage
path: root/packages/cli/src/message.ts
blob: ddbec6baa2137eeb5336d2a75e9fd5187097fb96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * 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, ReasoningEffort } 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 reasoningEffort?: ReasoningEffort | undefined;
  readonly workspaceId?: 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 }),
    ...(cmd.reasoningEffort !== undefined && { reasoningEffort: cmd.reasoningEffort }),
    ...(cmd.workspaceId !== undefined && { workspaceId: cmd.workspaceId }),
  };
}