-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathThinkingView.swift
More file actions
144 lines (130 loc) · 4.79 KB
/
ThinkingView.swift
File metadata and controls
144 lines (130 loc) · 4.79 KB
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
import SwiftUI
import ComposableArchitecture
import ConversationServiceProvider
import SharedUIComponents
struct ThinkingView: View {
let thinking: MessageThinking
let isStreaming: Bool
@AppStorage(\.chatFontSize) var chatFontSize
@State private var isExpandedOverride: Bool? = nil
private var sections: [ThinkingSection] {
MessageThinking.parseSections(from: thinking.text?.joined() ?? "")
}
private var titleText: String {
if isStreaming {
return "Thinking..."
}
if let title = thinking.title, !title.isEmpty {
return title
}
return "Thinking"
}
private var isExpanded: Bool {
if let override = isExpandedOverride { return override }
return isStreaming
}
private var isAutoExpandedWhileStreaming: Bool {
isStreaming && isExpandedOverride == nil
}
private static let autoExpandMaxHeight: CGFloat = 180
private static let scrollAnchorID = "thinking-bottom-anchor"
var body: some View {
WithPerceptionTracking {
let sections = sections
let hasContent = sections.contains { $0.title != nil || !$0.body.isEmpty }
if hasContent || isStreaming {
content(sections: sections, hasContent: hasContent)
}
}
}
@ViewBuilder
private func content(sections: [ThinkingSection], hasContent: Bool) -> some View {
VStack(alignment: .leading, spacing: 8) {
Button {
isExpandedOverride = !isExpanded
} label: {
HStack(spacing: 2) {
Text(titleText)
.scaledFont(size: chatFontSize - 1)
.lineLimit(1)
.truncationMode(.tail)
Image(systemName: isExpanded ? "chevron.down" : "chevron.right")
.resizable()
.scaledToFit()
.padding(4)
.scaledFrame(width: 16, height: 16)
.scaledFont(size: 10, weight: .medium)
}
.foregroundStyle(.secondary)
}
.buttonStyle(.plain)
if isExpanded, hasContent {
sectionsContainer(sections: sections)
}
}
}
@ViewBuilder
private func sectionsContainer(sections: [ThinkingSection]) -> some View {
let stack = VStack(alignment: .leading, spacing: 8) {
ForEach(Array(sections.enumerated()), id: \.offset) { _, section in
sectionView(section)
}
Color.clear
.frame(height: 0)
.id(Self.scrollAnchorID)
}
.fixedSize(horizontal: false, vertical: true)
if isAutoExpandedWhileStreaming {
ScrollViewReader { proxy in
ScrollView(.vertical, showsIndicators: false) {
stack
}
.frame(maxHeight: Self.autoExpandMaxHeight)
.onChange(of: thinking.text?.joined() ?? "") { _ in
withAnimation(.easeOut(duration: 0.15)) {
proxy.scrollTo(Self.scrollAnchorID, anchor: .bottom)
}
}
.onAppear {
proxy.scrollTo(Self.scrollAnchorID, anchor: .bottom)
}
}
} else {
stack
}
}
@ViewBuilder
private func sectionView(_ section: ThinkingSection) -> some View {
HStack(alignment: .top, spacing: 8) {
VStack(spacing: 4) {
Circle()
.fill(Color.secondary.opacity(0.3))
.frame(width: 4, height: 4)
.padding(.top, 6)
Rectangle()
.fill(Color.secondary.opacity(0.3))
.frame(width: 1)
.frame(maxHeight: .infinity)
}
.frame(width: 4)
VStack(alignment: .leading, spacing: 4) {
if let title = section.title, !title.isEmpty {
Text(title)
.scaledFont(size: chatFontSize - 1)
.fontWeight(.semibold)
.foregroundStyle(.secondary)
.fixedSize(horizontal: false, vertical: true)
}
if !section.body.isEmpty {
ThemedMarkdownText(
text: section.body,
context: MarkdownActionProvider(supportInsert: false),
foregroundColor: .secondary
)
.fixedSize(horizontal: false, vertical: true)
}
}
}
.fixedSize(horizontal: false, vertical: true)
}
}