File size: 2,688 Bytes
1dbc34b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
 * Conversation history utilities for processing message history
 *
 * Provides standardized conversation history handling:
 * - Extract text from content (string or array format)
 * - Normalize content blocks to array format
 * - Format history as plain text for CLI-based providers
 * - Convert history to Claude SDK message format
 */

import type { ConversationMessage } from '@automaker/types';

/**
 * Extract plain text from message content (handles both string and array formats)
 *
 * @param content - Message content (string or array of content blocks)
 * @returns Extracted text content
 */
export function extractTextFromContent(
  content: string | Array<{ type: string; text?: string; source?: object }>
): string {
  if (typeof content === 'string') {
    return content;
  }

  // Extract text blocks only
  return content
    .filter((block) => block.type === 'text')
    .map((block) => block.text || '')
    .join('\n');
}

/**
 * Normalize message content to array format
 *
 * @param content - Message content (string or array)
 * @returns Content as array of blocks
 */
export function normalizeContentBlocks(
  content: string | Array<{ type: string; text?: string; source?: object }>
): Array<{ type: string; text?: string; source?: object }> {
  if (Array.isArray(content)) {
    return content;
  }
  return [{ type: 'text', text: content }];
}

/**
 * Format conversation history as plain text for CLI-based providers
 *
 * @param history - Array of conversation messages
 * @returns Formatted text with role labels
 */
export function formatHistoryAsText(history: ConversationMessage[]): string {
  if (history.length === 0) {
    return '';
  }

  let historyText = 'Previous conversation:\n\n';

  for (const msg of history) {
    const contentText = extractTextFromContent(msg.content);
    const role = msg.role === 'user' ? 'User' : 'Assistant';
    historyText += `${role}: ${contentText}\n\n`;
  }

  historyText += '---\n\n';
  return historyText;
}

/**
 * Convert conversation history to Claude SDK message format
 *
 * @param history - Array of conversation messages
 * @returns Array of Claude SDK formatted messages
 */
export function convertHistoryToMessages(history: ConversationMessage[]): Array<{
  type: 'user' | 'assistant';
  session_id: string;
  message: {
    role: 'user' | 'assistant';
    content: Array<{ type: string; text?: string; source?: object }>;
  };
  parent_tool_use_id: null;
}> {
  return history.map((historyMsg) => ({
    type: historyMsg.role,
    session_id: '',
    message: {
      role: historyMsg.role,
      content: normalizeContentBlocks(historyMsg.content),
    },
    parent_tool_use_id: null,
  }));
}