Skip to content

Commit

Permalink
beta2
Browse files Browse the repository at this point in the history
Fixes to selectable text, save text, use gpt4mini instead of classic chatgpt gpt 3.5, changes to make fully compatible with 10.15+
  • Loading branch information
dtseto committed Nov 10, 2024
1 parent ddd9abc commit 9c23326
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 25 deletions.
4 changes: 4 additions & 0 deletions MacGptChatLegacy.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
0FF026A12CDED50000778366 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0FF026A02CDED50000778366 /* Preview Assets.xcassets */; };
0FF026A42CDED50100778366 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0FF026A22CDED50100778366 /* Main.storyboard */; };
0FF026AD2CDED70600778366 /* TextViewWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0FF026AC2CDED70600778366 /* TextViewWrapper.swift */; };
0FF026AF2CDFD4F400778366 /* SelectableText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0FF026AE2CDFD4F400778366 /* SelectableText.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -25,6 +26,7 @@
0FF026A52CDED50100778366 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
0FF026A62CDED50100778366 /* MacGptChatLegacy.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = MacGptChatLegacy.entitlements; sourceTree = "<group>"; };
0FF026AC2CDED70600778366 /* TextViewWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextViewWrapper.swift; sourceTree = "<group>"; };
0FF026AE2CDFD4F400778366 /* SelectableText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectableText.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -65,6 +67,7 @@
0FF026A62CDED50100778366 /* MacGptChatLegacy.entitlements */,
0FF0269F2CDED50000778366 /* Preview Content */,
0FF026AC2CDED70600778366 /* TextViewWrapper.swift */,
0FF026AE2CDFD4F400778366 /* SelectableText.swift */,
);
path = MacGptChatLegacy;
sourceTree = "<group>";
Expand Down Expand Up @@ -147,6 +150,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0FF026AF2CDFD4F400778366 /* SelectableText.swift in Sources */,
0FF026AD2CDED70600778366 /* TextViewWrapper.swift in Sources */,
0FF0269C2CDED4FD00778366 /* ContentView.swift in Sources */,
0FF0269A2CDED4FD00778366 /* AppDelegate.swift in Sources */,
Expand Down
73 changes: 48 additions & 25 deletions MacGptChatLegacy/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
// includes a debug logging view to see the api
// ContentView.swift
import SwiftUI
import UniformTypeIdentifiers


struct ContentView: View {
@State private var apiKey: String = ""
@State private var message: String = ""
@State private var conversation: String = ""
@State private var conversation: String = "Chat started...\n" // Initialize with starting text
@State private var debugLog: String = "Debug Log:\n"
@State private var isLoading: Bool = false
@State private var showDebug: Bool = false
@State private var showAPIKeyInput: Bool = true


var body: some View {
VStack(spacing: 20) {
if showAPIKeyInput {
Expand Down Expand Up @@ -49,30 +52,29 @@ struct ContentView: View {
} else {
// Main Chat Interface
// Toggle for debug view
Toggle("Show Debug Log", isOn: $showDebug)
VStack {
HStack {
Toggle("Show Debug Log", isOn: $showDebug)
Spacer()
Button("Save Conversation") {
saveConversation()
}
}
.padding(.horizontal)

if showDebug {
// Debug log area
ScrollView {
Text(debugLog)
.font(.system(.body, design: .monospaced))
.frame(maxWidth: .infinity, alignment: .leading)
.padding()

if showDebug {
// Debug log area with selectable text
SelectableText(text: debugLog)
.frame(height: 200)
.border(Color.gray)
}
.frame(height: 200)
.border(Color.gray)
}

// Chat history area
ScrollView {
Text(conversation)
.frame(maxWidth: .infinity, alignment: .leading)
.padding()
}
.frame(height: showDebug ? 150 : 300)
.border(Color.gray)



// Chat history area with selectable text
SelectableText(text: conversation)
.frame(height: showDebug ? 150 : 300)
.border(Color.gray)

// Loading indicator
if isLoading {
Text("Loading...")
Expand Down Expand Up @@ -105,8 +107,29 @@ struct ContentView: View {
.foregroundColor(.blue)
}
}
.frame(width: 500, height: 500)
}
.frame(width: 500, height: 500)
}

private func saveConversation() {
let panel = NSSavePanel()
panel.allowedFileTypes = ["txt"] // Use this instead of allowedContentTypes
panel.canCreateDirectories = true
panel.title = "Save Conversation"
panel.nameFieldStringValue = "conversation.txt"

panel.begin { response in
if response == .OK, let url = panel.url {
do {
try conversation.write(to: url, atomically: true, encoding: .utf8)
appendToLog("Conversation saved to: \(url.path)")
} catch {
appendToLog("Error saving conversation: \(error.localizedDescription)")
}
}
}
}


private func appendToLog(_ text: String) {
debugLog += text + "\n"
Expand Down Expand Up @@ -135,7 +158,7 @@ struct ContentView: View {
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")

let requestBody: [String: Any] = [
"model": "gpt-3.5-turbo",
"model": "gpt-4o-mini",
"messages": [
["role": "user", "content": userMessage]
]
Expand Down
69 changes: 69 additions & 0 deletions MacGptChatLegacy/SelectableText.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// SelectableText.swift
// MacGptChatLegacy
//
// Created by User2 on 11/9/24.
//
import SwiftUI

struct SelectableText: NSViewRepresentable {
let text: String

final class Coordinator: NSObject {
var text: String
var textView: NSTextView?

init(text: String) {
self.text = text
}
}

func makeCoordinator() -> Coordinator {
Coordinator(text: text)
}

func makeNSView(context: Context) -> NSScrollView {
let scrollView = NSScrollView()
let textView = NSTextView()

scrollView.hasVerticalScroller = true
scrollView.hasHorizontalScroller = false
scrollView.autohidesScrollers = true

textView.isEditable = false
textView.isSelectable = true
textView.font = .systemFont(ofSize: NSFont.systemFontSize)

// Set up text container
let contentSize = scrollView.contentSize
textView.minSize = NSSize(width: 0, height: 0)
textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
textView.textContainer?.containerSize = NSSize(width: contentSize.width, height: CGFloat.greatestFiniteMagnitude)
textView.textContainer?.widthTracksTextView = true
textView.isVerticallyResizable = true
textView.isHorizontallyResizable = false
textView.autoresizingMask = [.width]

// Set initial text
textView.string = text

scrollView.documentView = textView
context.coordinator.textView = textView

return scrollView
}

func updateNSView(_ scrollView: NSScrollView, context: Context) {
guard let textView = scrollView.documentView as? NSTextView else { return }

// Only update if text has changed
if textView.string != text {
let selectedRanges = textView.selectedRanges
textView.string = text
textView.selectedRanges = selectedRanges

// Scroll to bottom
textView.scrollToEndOfDocument(nil)
}
}
}

0 comments on commit 9c23326

Please sign in to comment.