forked from mcintyre94/wisp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatView.swift
More file actions
400 lines (383 loc) · 16.5 KB
/
ChatView.swift
File metadata and controls
400 lines (383 loc) · 16.5 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
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
399
400
import PhotosUI
import SwiftUI
import UIKit
import UniformTypeIdentifiers
struct ChatView: View {
@Environment(SpritesAPIClient.self) private var apiClient
@Environment(\.modelContext) private var modelContext
@Environment(\.scenePhase) private var scenePhase
@Bindable var viewModel: ChatViewModel
var isReadOnly: Bool = false
var topAccessory: AnyView? = nil
var existingSessionIds: Set<String> = []
var onFork: ((String, UUID) -> Void)? = nil
@FocusState private var isInputFocused: Bool
@State private var contentOpacity: Double = 0
@State private var isAtBottom: Bool = true
// Attachment state
@State private var showFileBrowser = false
@State private var showPhotoPicker = false
@State private var showFilePicker = false
@State private var selectedPhotos: [PhotosPickerItem] = []
// Quick Actions
@State private var quickActionsViewModel: QuickActionsViewModel?
var body: some View {
ScrollViewReader { proxy in
ScrollView {
VStack(spacing: 12) {
if viewModel.messages.isEmpty && !isReadOnly && !viewModel.usesWorktree {
SessionSuggestionsView(
sessions: viewModel.remoteSessions,
hasAnySessions: viewModel.hasAnyRemoteSessions,
isLoading: viewModel.isLoadingRemoteSessions || viewModel.isLoadingHistory
) { entry in
contentOpacity = 0
viewModel.selectRemoteSession(entry, apiClient: apiClient, modelContext: modelContext)
}
}
ForEach(viewModel.messages) { message in
messageView(message)
}
if viewModel.isStreaming && !viewModel.status.isReconnecting && viewModel.pendingWispAskCard == nil {
ThinkingShimmerView(label: viewModel.status.isConnecting ? "Connecting…" : (viewModel.activeToolLabel ?? "Thinking…"))
.transition(.opacity.combined(with: .move(edge: .bottom)))
.id("shimmer")
}
if let pendingText = viewModel.queuedPrompt {
PendingUserBubbleView(
text: pendingText,
files: viewModel.queuedAttachments
) {
viewModel.inputText = pendingText
viewModel.attachedFiles = viewModel.queuedAttachments
viewModel.queuedPrompt = nil
viewModel.queuedAttachments = []
isInputFocused = true
} onCancel: {
viewModel.cancelQueuedPrompt()
}
}
Color.clear.frame(height: 1).id("bottom")
.onScrollVisibilityChange(threshold: 0.5) { visible in
isAtBottom = visible
}
}
.opacity(contentOpacity)
.padding()
}
.defaultScrollAnchor(.bottom)
.scrollDismissesKeyboard(.interactively)
.onChange(of: viewModel.messages.count) {
proxy.scrollTo("bottom")
}
.onChange(of: viewModel.messages.last?.content.count) {
if viewModel.isStreaming && isAtBottom {
proxy.scrollTo("bottom")
}
}
.onChange(of: viewModel.activeToolLabel) {
if viewModel.isStreaming && isAtBottom {
proxy.scrollTo("bottom")
}
}
.onChange(of: viewModel.queuedPrompt) {
proxy.scrollTo("bottom")
}
.onChange(of: viewModel.isStreaming) { _, streaming in
if streaming {
isAtBottom = true
proxy.scrollTo("bottom")
}
}
}
.safeAreaInset(edge: .top, spacing: 0) {
VStack(spacing: 0) {
if let topAccessory { topAccessory }
ChatStatusBar(
status: viewModel.status,
modelName: viewModel.modelName,
modelOverride: Bindable(viewModel).modelOverride,
hasPendingWispAsk: viewModel.pendingWispAskCard != nil
)
}
}
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
withAnimation(.easeOut(duration: 0.3)) {
contentOpacity = 1
}
}
}
.task {
// Small delay to let loadSession populate messages first
try? await Task.sleep(for: .milliseconds(100))
if viewModel.messages.isEmpty && !isReadOnly {
viewModel.fetchRemoteSessions(
apiClient: apiClient,
existingSessionIds: existingSessionIds
)
}
}
.onChange(of: viewModel.isLoadingHistory) {
if !viewModel.isLoadingHistory {
withAnimation(.easeOut(duration: 0.3)) {
contentOpacity = 1
}
}
}
.onChange(of: scenePhase) { _, newPhase in
if newPhase != .active {
viewModel.saveDraft(modelContext: modelContext)
}
}
.onChange(of: viewModel.inputText) {
viewModel.saveDraft(modelContext: modelContext)
}
.onChange(of: viewModel.attachedFiles.count) {
viewModel.saveDraft(modelContext: modelContext)
}
.onDisappear {
viewModel.saveDraft(modelContext: modelContext)
}
.safeAreaInset(edge: .bottom, spacing: 0) {
if isReadOnly {
closedChatBar
} else {
ChatInputBar(
text: $viewModel.inputText,
isStreaming: viewModel.isStreaming,
hasQueuedMessage: viewModel.queuedPrompt != nil,
onSend: {
isInputFocused = false
viewModel.sendMessage(apiClient: apiClient, modelContext: modelContext)
},
onInterrupt: {
viewModel.interrupt(apiClient: apiClient, modelContext: modelContext)
},
onBrowseSpriteFiles: { showFileBrowser = true },
onPickPhoto: { showPhotoPicker = true },
onPickFile: { showFilePicker = true },
onPasteFromClipboard: handlePasteFromClipboard,
isUploading: viewModel.isUploadingAttachment,
attachedFiles: viewModel.attachedFiles,
onRemoveAttachment: { file in
viewModel.attachedFiles.removeAll { $0.id == file.id }
},
lastUploadedFileName: viewModel.lastUploadedFileName,
onStash: { viewModel.stashDraft() },
isFocused: $isInputFocused
)
}
}
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button {
quickActionsViewModel = QuickActionsViewModel(
spriteName: viewModel.spriteName,
sessionId: viewModel.sessionId,
workingDirectory: viewModel.workingDirectory
)
} label: {
Image(systemName: "bolt")
}
}
}
.sheet(item: $quickActionsViewModel) { vm in
QuickActionsView(
viewModel: vm,
insertCallback: { text in
viewModel.inputText += (viewModel.inputText.isEmpty ? "" : "\n") + text
quickActionsViewModel = nil
}
)
.environment(apiClient)
.presentationDetents([.medium, .large])
.presentationDragIndicator(.visible)
}
.sheet(isPresented: $showFileBrowser) {
SpriteFileBrowserView(
spriteName: viewModel.spriteName,
startingDirectory: viewModel.workingDirectory,
apiClient: apiClient,
onFileSelected: { path in
let name = (path as NSString).lastPathComponent
viewModel.attachedFiles.append(AttachedFile(name: name, path: path))
}
)
}
.photosPicker(isPresented: $showPhotoPicker, selection: $selectedPhotos, maxSelectionCount: 1, matching: .images)
.fileImporter(isPresented: $showFilePicker, allowedContentTypes: [.item]) { result in
switch result {
case .success(let url):
Task {
if let remotePath = await viewModel.uploadFileFromDevice(apiClient: apiClient, fileURL: url) {
let name = (remotePath as NSString).lastPathComponent
viewModel.attachedFiles.append(AttachedFile(name: name, path: remotePath))
}
}
case .failure(let error):
viewModel.uploadAttachmentError = "Failed to pick file: \(error.localizedDescription)"
}
}
.alert("Upload Error", isPresented: .init(
get: { viewModel.uploadAttachmentError != nil },
set: { if !$0 { viewModel.uploadAttachmentError = nil } }
)) {
Button("OK") { viewModel.uploadAttachmentError = nil }
} message: {
if let error = viewModel.uploadAttachmentError {
Text(error)
}
}
.onChange(of: selectedPhotos) {
guard let item = selectedPhotos.first else { return }
selectedPhotos = []
Task {
guard let data = try? await item.loadTransferable(type: Data.self) else {
viewModel.uploadAttachmentError = "Failed to load photo data"
return
}
let ext = item.supportedContentTypes.first?.preferredFilenameExtension ?? "jpg"
if let remotePath = await viewModel.uploadPhotoData(apiClient: apiClient, data: data, fileExtension: ext) {
let name = (remotePath as NSString).lastPathComponent
viewModel.attachedFiles.append(AttachedFile(name: name, path: remotePath))
}
}
}
}
private static let pasteImageFormats: [(UTType, String)] = [
(.png, "png"),
(.jpeg, "jpg"),
(.gif, "gif"),
(.webP, "webp"),
(UTType("public.heic") ?? .image, "heic"),
]
private func handlePasteFromClipboard() {
let pasteboard = UIPasteboard.general
for (type, ext) in Self.pasteImageFormats {
if let data = pasteboard.data(forPasteboardType: type.identifier) {
Task {
if let remotePath = await viewModel.uploadPhotoData(apiClient: apiClient, data: data, fileExtension: ext) {
viewModel.addAttachedFile(remotePath: remotePath)
}
}
return
}
}
// Fallback: any image via UIImage
if pasteboard.hasImages, let image = pasteboard.image, let data = image.pngData() {
Task {
if let remotePath = await viewModel.uploadPhotoData(apiClient: apiClient, data: data, fileExtension: "png") {
viewModel.addAttachedFile(remotePath: remotePath)
}
}
return
}
// Try a file URL via item providers (handles security-scoped URLs from Files app)
for provider in pasteboard.itemProviders {
if provider.hasItemConformingToTypeIdentifier(UTType.fileURL.identifier) {
provider.loadItem(forTypeIdentifier: UTType.fileURL.identifier) { item, _ in
guard let url = item as? URL else { return }
Task { @MainActor in
if let remotePath = await viewModel.uploadFileFromDevice(apiClient: apiClient, fileURL: url) {
viewModel.addAttachedFile(remotePath: remotePath)
}
}
}
return
}
}
// Fallback: direct URL from pasteboard
if let url = pasteboard.url, url.isFileURL {
Task {
if let remotePath = await viewModel.uploadFileFromDevice(apiClient: apiClient, fileURL: url) {
viewModel.addAttachedFile(remotePath: remotePath)
}
}
return
}
// Fallback: load raw data from item providers (files from Files app use their
// content UTI directly — e.g. com.adobe.pdf — rather than public.file-url)
for provider in pasteboard.itemProviders {
guard let typeId = provider.registeredTypeIdentifiers.first(where: { id in
guard let type = UTType(id) else { return false }
return type.conforms(to: .data) && !type.conforms(to: .text)
}) else { continue }
let suggestedName = provider.suggestedName
let fileExt = UTType(typeId)?.preferredFilenameExtension
provider.loadDataRepresentation(forTypeIdentifier: typeId) { data, _ in
guard let data else { return }
let baseName = suggestedName ?? "pasted_file"
let filename: String
if let ext = fileExt, !baseName.hasSuffix(".\(ext)") {
filename = "\(baseName).\(ext)"
} else {
filename = baseName
}
let tempURL = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString)
.appendingPathComponent(filename)
try? FileManager.default.createDirectory(at: tempURL.deletingLastPathComponent(), withIntermediateDirectories: true)
guard (try? data.write(to: tempURL)) != nil else { return }
Task { @MainActor in
if let remotePath = await viewModel.uploadFileFromDevice(apiClient: apiClient, fileURL: tempURL) {
viewModel.addAttachedFile(remotePath: remotePath)
}
try? FileManager.default.removeItem(at: tempURL.deletingLastPathComponent())
}
}
return
}
viewModel.uploadAttachmentError = "No image or file found in clipboard"
}
@ViewBuilder
private func messageView(_ message: ChatMessage) -> some View {
let isLastAssistant = message.role == .assistant
&& message.id == viewModel.messages.last(where: { $0.role == .assistant })?.id
ChatMessageView(
message: message,
isStreaming: viewModel.isStreaming && message.id == viewModel.currentAssistantMessageId,
workingDirectory: viewModel.workingDirectory,
onCreateCheckpoint: isLastAssistant ? {
viewModel.createCheckpoint(for: message, modelContext: modelContext)
} : nil,
isCheckpointDisabled: viewModel.isCheckpointing,
onAnswerWispAsk: { answer in
viewModel.submitWispAskAnswer(answer)
}
)
.id(message.id)
if let checkpointId = message.checkpointId {
CheckpointMarkerView(
comment: message.checkpointComment
) {
onFork?(checkpointId, message.id)
}
}
}
private var closedChatBar: some View {
HStack {
Image(systemName: "archivebox")
.foregroundStyle(.secondary)
Text("This chat is closed")
.foregroundStyle(.secondary)
}
.font(.subheadline)
.frame(maxWidth: .infinity)
.padding(.vertical, 12)
.background(.bar)
}
}
#Preview {
let viewModel = ChatViewModel(
spriteName: "my-sprite",
chatId: UUID(),
currentServiceName: nil,
workingDirectory: "/home/sprite/project"
)
NavigationStack {
ChatView(viewModel: viewModel)
.environment(SpritesAPIClient())
.modelContainer(for: [SpriteChat.self, SpriteSession.self], inMemory: true)
}
}