-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea917df
commit 563a6cb
Showing
6 changed files
with
124 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 80 additions & 6 deletions
86
.../SwiftProjectTemplateAppPackage/Sources/SwiftProjectTemplateAppPackage/Content View.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,89 @@ | ||
import SwiftUI | ||
import Claude | ||
|
||
struct ContentView: View { | ||
|
||
let claude: Claude | ||
|
||
var body: some View { | ||
ClaudeProvider( | ||
defaultModel: .claude35Sonnet20241022 | ||
) { claude in | ||
Text("Authenticated!") | ||
ScrollView { | ||
LazyVStack(alignment: .leading) { | ||
ForEach(conversation.messages) { message in | ||
switch message { | ||
case .user(let message): | ||
Text("USER:") | ||
Text("\(message.text)") | ||
case .assistant(let message): | ||
Text("ASSISTANT:") | ||
ForEach(message.currentContentBlocks) { contentBlock in | ||
switch contentBlock { | ||
case .textBlock(let textBlock): | ||
Text(textBlock.currentText) | ||
} | ||
} | ||
} | ||
} | ||
|
||
switch conversation.state { | ||
case .idle: | ||
HStack(alignment: .bottom) { | ||
TextEditor(text: $text) | ||
.onSubmit { | ||
send(text) | ||
} | ||
.focused($isTextEditorFocused) | ||
Button("Send") { | ||
send(text) | ||
} | ||
} | ||
case .streaming, .waitingForToolInvocationResults: | ||
ProgressView() | ||
case .toolInvocationResultsAvailable: | ||
Button("Provide tool invocation results") { | ||
send() | ||
} | ||
} | ||
} | ||
} | ||
.padding() | ||
.onAppear { | ||
isTextEditorFocused = true | ||
} | ||
} | ||
|
||
private func send(_ message: String? = nil) { | ||
text = "" | ||
if let message { | ||
conversation.messages.append(.user(.init(text: message))) | ||
} | ||
let message = claude.nextMessage(in: conversation) | ||
conversation.messages.append(.assistant(message)) | ||
} | ||
|
||
@State | ||
private var conversation = Conversation() | ||
|
||
@State | ||
private var text = "" | ||
|
||
@FocusState | ||
private var isTextEditorFocused | ||
|
||
} | ||
|
||
#Preview { | ||
ContentView() | ||
private struct Conversation: Claude.Conversation { | ||
|
||
var messages: [Message] = [] | ||
|
||
final class UserMessage: Identifiable { | ||
init(text: String) { | ||
self.text = text | ||
} | ||
let text: String | ||
} | ||
|
||
static func userMessageContent(for message: UserMessage) -> Claude.UserMessageContent { | ||
.init(message.text) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters