summaryrefslogtreecommitdiffhomepage
path: root/packages/tui/internal/components/chat/message.go
blob: c78dd8e2f14df9307c5150f87b299015d8e9607c (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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package chat

import (
	"fmt"
	"path/filepath"
	"strings"
	"time"

	"github.com/charmbracelet/lipgloss"
	"github.com/charmbracelet/x/ansi"
	"github.com/sst/opencode/internal/components/diff"
	"github.com/sst/opencode/internal/styles"
	"github.com/sst/opencode/internal/theme"
	"github.com/sst/opencode/pkg/client"
	"golang.org/x/text/cases"
	"golang.org/x/text/language"
)

const (
	maxResultHeight = 10
)

func toMarkdown(content string, width int) string {
	r := styles.GetMarkdownRenderer(width)
	rendered, _ := r.Render(content)
	lines := strings.Split(rendered, "\n")
	if len(lines) > 0 {
		firstLine := lines[0]
		cleaned := ansi.Strip(firstLine)
		nospace := strings.ReplaceAll(cleaned, " ", "")
		if nospace == "" {
			lines = lines[1:]
		}
		if len(lines) > 0 {
			lastLine := lines[len(lines)-1]
			cleaned = ansi.Strip(lastLine)
			nospace = strings.ReplaceAll(cleaned, " ", "")
			if nospace == "" {
				lines = lines[:len(lines)-1]
			}
		}
	}
	return strings.TrimSuffix(strings.Join(lines, "\n"), "\n")
}

func renderUserMessage(user string, msg client.MessageInfo, width int) string {
	t := theme.CurrentTheme()
	style := styles.BaseStyle().
		PaddingLeft(1).
		BorderLeft(true).
		Foreground(t.TextMuted()).
		BorderForeground(t.Secondary()).
		BorderStyle(lipgloss.ThickBorder())

	// var styledAttachments []string
	// attachmentStyles := baseStyle.
	// 	MarginLeft(1).
	// 	Background(t.TextMuted()).
	// 	Foreground(t.Text())
	// for _, attachment := range msg.BinaryContent() {
	// 	file := filepath.Base(attachment.Path)
	// 	var filename string
	// 	if len(file) > 10 {
	// 		filename = fmt.Sprintf(" %s %s...", styles.DocumentIcon, file[0:7])
	// 	} else {
	// 		filename = fmt.Sprintf(" %s %s", styles.DocumentIcon, file)
	// 	}
	// 	styledAttachments = append(styledAttachments, attachmentStyles.Render(filename))
	// }

	timestamp := time.UnixMilli(int64(msg.Metadata.Time.Created)).Local().Format("02 Jan 2006 03:04 PM")
	if time.Now().Format("02 Jan 2006") == timestamp[:11] {
		timestamp = timestamp[12:]
	}
	info := styles.BaseStyle().
		Foreground(t.TextMuted()).
		Render(fmt.Sprintf("%s (%s)", user, timestamp))

	content := ""
	// if len(styledAttachments) > 0 {
	// 	attachmentContent := baseStyle.Width(width).Render(lipgloss.JoinHorizontal(lipgloss.Left, styledAttachments...))
	// 	content = renderMessage(msg.Content().String(), true, isFocused, width, append(info, attachmentContent)...)
	// } else {
	for _, p := range msg.Parts {
		part, err := p.ValueByDiscriminator()
		if err != nil {
			continue //TODO: handle error?
		}

		switch part.(type) {
		case client.MessagePartText:
			textPart := part.(client.MessagePartText)
			text := toMarkdown(textPart.Text, width)
			content = style.Render(lipgloss.JoinVertical(lipgloss.Left, text, info))
		}
	}

	return styles.ForceReplaceBackgroundWithLipgloss(content, t.Background())
}

func renderAssistantMessage(
	msg client.MessageInfo,
	width int,
	showToolMessages bool,
	appInfo client.AppInfo,
) string {
	t := theme.CurrentTheme()
	style := styles.BaseStyle().
		PaddingLeft(1).
		BorderLeft(true).
		Foreground(t.TextMuted()).
		BorderForeground(t.Primary()).
		BorderStyle(lipgloss.ThickBorder())
	messages := []string{}

	timestamp := time.UnixMilli(int64(msg.Metadata.Time.Created)).Local().Format("02 Jan 2006 03:04 PM")
	if time.Now().Format("02 Jan 2006") == timestamp[:11] {
		timestamp = timestamp[12:]
	}
	modelName := msg.Metadata.Assistant.ModelID
	info := styles.BaseStyle().
		Foreground(t.TextMuted()).
		Render(fmt.Sprintf("%s (%s)", modelName, timestamp))

	for _, p := range msg.Parts {
		part, err := p.ValueByDiscriminator()
		if err != nil {
			continue //TODO: handle error?
		}

		switch part.(type) {
		// case client.MessagePartReasoning:
		// 	reasoningPart := part.(client.MessagePartReasoning)

		case client.MessagePartText:
			textPart := part.(client.MessagePartText)
			text := toMarkdown(textPart.Text, width)
			content := style.Render(lipgloss.JoinVertical(lipgloss.Left, text, info))
			message := styles.ForceReplaceBackgroundWithLipgloss(content, t.Background())
			messages = append(messages, message)

		case client.MessagePartToolInvocation:
			if !showToolMessages {
				continue
			}

			toolInvocationPart := part.(client.MessagePartToolInvocation)
			toolCall, _ := toolInvocationPart.ToolInvocation.AsMessageToolInvocationToolCall()
			var result *string
			resultPart, resultError := toolInvocationPart.ToolInvocation.AsMessageToolInvocationToolResult()
			if resultError == nil {
				result = &resultPart.Result
			}
			metadata := map[string]any{}
			if _, ok := msg.Metadata.Tool[toolCall.ToolCallId]; ok {
				metadata = msg.Metadata.Tool[toolCall.ToolCallId].(map[string]any)
			}
			message := renderToolInvocation(toolCall, result, metadata, appInfo, width)
			messages = append(messages, message)
		}
	}

	return strings.Join(messages, "\n\n")
}

func renderToolInvocation(toolCall client.MessageToolInvocationToolCall, result *string, metadata map[string]any, appInfo client.AppInfo, width int) string {
	t := theme.CurrentTheme()
	style := styles.BaseStyle().
		BorderLeft(true).
		PaddingLeft(1).
		Foreground(t.TextMuted()).
		BorderForeground(t.TextMuted()).
		BorderStyle(lipgloss.ThickBorder())

	toolName := renderToolName(toolCall.ToolName)
	toolArgs := ""
	toolArgsMap := make(map[string]any)
	if toolCall.Args != nil {
		value := *toolCall.Args
		m, ok := value.(map[string]any)
		if ok {
			toolArgsMap = m
			firstKey := ""
			for key := range toolArgsMap {
				firstKey = key
				break
			}
			toolArgs = renderArgs(&toolArgsMap, appInfo, firstKey)
		}
	}

	title := fmt.Sprintf("%s: %s", toolName, toolArgs)
	finished := result != nil
	body := styles.BaseStyle().Render("In progress...")
	if finished {
		body = *result
	}
	footer := ""
	if metadata["time"] != nil {
		timeMap := metadata["time"].(map[string]any)
		start := timeMap["start"].(float64)
		end := timeMap["end"].(float64)
		durationMs := end - start
		duration := time.Duration(durationMs * float64(time.Millisecond))
		roundedDuration := time.Duration(duration.Round(time.Millisecond))
		if durationMs > 1000 {
			roundedDuration = time.Duration(duration.Round(time.Second))
		}
		footer = styles.Muted().Render(fmt.Sprintf("%s", roundedDuration))
	}

	switch toolCall.ToolName {
	case "opencode_edit":
		filename := toolArgsMap["filePath"].(string)
		filename = strings.TrimPrefix(filename, appInfo.Path.Root+"/")
		title = fmt.Sprintf("%s: %s", toolName, filename)
		if finished && metadata["diff"] != nil {
			patch := metadata["diff"].(string)
			formattedDiff, _ := diff.FormatDiff(patch, diff.WithTotalWidth(width))
			body = strings.TrimSpace(formattedDiff)
			return style.Render(lipgloss.JoinVertical(lipgloss.Left,
				title,
				body,
				styles.ForceReplaceBackgroundWithLipgloss(footer, t.Background()),
			))
		}
	case "opencode_read":
		toolArgs = renderArgs(&toolArgsMap, appInfo, "filePath")
		title = fmt.Sprintf("%s: %s", toolName, toolArgs)
		filename := toolArgsMap["filePath"].(string)
		ext := filepath.Ext(filename)
		if ext == "" {
			ext = ""
		} else {
			ext = strings.ToLower(ext[1:])
		}
		if finished {
			if metadata["preview"] != nil {
				body = metadata["preview"].(string)
			}
			body = fmt.Sprintf("```%s\n%s\n```", ext, truncateHeight(body, 10))
			body = toMarkdown(body, width)
		}
	case "opencode_write":
		filename := toolArgsMap["filePath"].(string)
		filename = strings.TrimPrefix(filename, appInfo.Path.Root+"/")
		title = fmt.Sprintf("%s: %s", toolName, filename)
		ext := filepath.Ext(filename)
		if ext == "" {
			ext = ""
		} else {
			ext = strings.ToLower(ext[1:])
		}
		content := toolArgsMap["content"].(string)
		body = fmt.Sprintf("```%s\n%s\n```", ext, truncateHeight(content, 10))
		body = toMarkdown(body, width)
	case "opencode_bash":
		if finished && metadata["stdout"] != nil {
			description := toolArgsMap["description"].(string)
			title = fmt.Sprintf("%s: %s", toolName, description)
			command := toolArgsMap["command"].(string)
			stdout := metadata["stdout"].(string)
			body = fmt.Sprintf("```console\n$ %s\n%s```", command, stdout)
			body = toMarkdown(body, width)
		}
	case "opencode_todoread":
		title = fmt.Sprintf("%s", toolName)
		if finished && metadata["todos"] != nil {
			body = ""
			todos := metadata["todos"].([]any)
			for _, todo := range todos {
				t := todo.(map[string]any)
				content := t["content"].(string)
				switch t["status"].(string) {
				case "completed":
					body += fmt.Sprintf("- [x] %s\n", content)
				// case "in-progress":
				// 	body += fmt.Sprintf("- [ ] _%s_\n", content)
				default:
					body += fmt.Sprintf("- [ ] %s\n", content)
				}
			}
			body = toMarkdown(body, width)
		}
	case "opencode_todowrite":
		title = fmt.Sprintf("%s", toolName)
		if finished && metadata["todos"] != nil {
			body = ""
			todos := metadata["todos"].([]any)
			for _, todo := range todos {
				t := todo.(map[string]any)
				content := t["content"].(string)
				switch t["status"].(string) {
				case "completed":
					body += fmt.Sprintf("- [x] %s\n", content)
				// case "in-progress":
				// 	body += fmt.Sprintf("- [ ] _%s_\n", content)
				default:
					body += fmt.Sprintf("- [ ] %s\n", content)
				}
			}
			body = toMarkdown(body, width)
		}
	default:
		body = fmt.Sprintf("```txt\n%s\n```", truncateHeight(body, 10))
		body = toMarkdown(body, width)
	}

	if metadata["error"] != nil && metadata["message"] != nil {
		body = styles.BaseStyle().Foreground(t.Error()).Render(metadata["message"].(string))
	}

	content := style.Render(lipgloss.JoinVertical(lipgloss.Left,
		title,
		body,
		footer,
	))
	return styles.ForceReplaceBackgroundWithLipgloss(content, t.Background())
}

func renderToolName(name string) string {
	switch name {
	// case agent.AgentToolName:
	// 	return "Task"
	case "opencode_ls":
		return "List"
	case "opencode_webfetch":
		return "Fetch"
	case "opencode_todoread":
		return "Read TODOs"
	case "opencode_todowrite":
		return "Update TODOs"
	default:
		normalizedName := name
		if strings.HasPrefix(name, "opencode_") {
			normalizedName = strings.TrimPrefix(name, "opencode_")
		}
		return cases.Title(language.Und).String(normalizedName)
	}
}

func renderToolAction(name string) string {
	switch name {
	// case agent.AgentToolName:
	// 	return "Preparing prompt..."
	case "opencode_bash":
		return "Building command..."
	case "opencode_edit":
		return "Preparing edit..."
	case "opencode_fetch":
		return "Writing fetch..."
	case "opencode_glob":
		return "Finding files..."
	case "opencode_grep":
		return "Searching content..."
	case "opencode_ls":
		return "Listing directory..."
	case "opencode_read":
		return "Reading file..."
	case "opencode_write":
		return "Preparing write..."
	case "opencode_patch":
		return "Preparing patch..."
	case "opencode_batch":
		return "Running batch operations..."
	}
	return "Working..."
}

func renderArgs(args *map[string]any, appInfo client.AppInfo, titleKey string) string {
	if args == nil || len(*args) == 0 {
		return ""
	}
	title := ""
	parts := []string{}
	for key, value := range *args {
		if key == "filePath" || key == "path" {
			value = strings.TrimPrefix(value.(string), appInfo.Path.Root+"/")
		}
		if key == titleKey {
			title = fmt.Sprintf("%s", value)
			continue
		}
		parts = append(parts, fmt.Sprintf("%s=%v", key, value))
	}
	if len(parts) == 0 {
		return title
	}
	return fmt.Sprintf("%s (%s)", title, strings.Join(parts, ", "))
}

func truncateHeight(content string, height int) string {
	lines := strings.Split(content, "\n")
	if len(lines) > height {
		return strings.Join(lines[:height], "\n")
	}
	return content
}