Skip to content

Commit

Permalink
Merge branch 'feature/remove_deprecation#24' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Sep 12, 2023
2 parents 3e87fff + a3b1690 commit e9b20c8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 66 deletions.
36 changes: 23 additions & 13 deletions PlantUML/PlantUML+OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class LILOFixedSizeQueue<T> : LILOQueue<T> {

class OpenAIService : ObservableObject {

enum Status {
enum Status : Equatable {
case Ready
case Error( String )
case Editing
Expand All @@ -100,7 +100,7 @@ class OpenAIService : ObservableObject {
// @Published public var inputModel:String


@AppStorage("openaiModel") private var openAIModel:String = "text-davinci-edit-001"
@AppStorage("openaiModel") private var openAIModel:String = "gpt-3.5-turbo"
@AppSecureStorage("openaikey") private var openAIKey:String?
@AppSecureStorage("openaiorg") private var openAIOrg:String?

Expand Down Expand Up @@ -166,9 +166,8 @@ class OpenAIService : ObservableObject {

}


@MainActor
func generateEdit( input: String, instruction: String ) async -> String? {
func generateChatCompletion( input: String, instruction: String ) async -> String? {

guard let openAI /*, let openAIModel */, case .Ready = status else {
return nil
Expand All @@ -177,17 +176,28 @@ class OpenAIService : ObservableObject {
self.status = .Editing

do {
let editParameter = EditParameters(

let chat: [ChatMessage] = [
ChatMessage(role: .system, content:
"""
You are my plantUML assistant.
You must answer exclusively with diagram syntax.
"""),
ChatMessage(role: .assistant, content: input),
ChatMessage(role: .user, content: instruction),
]

let chatParameters = ChatParameters(
model: openAIModel,
input: input,
instruction: instruction,
messages: chat,
temperature: 0.0,
topP: 1.0
)
topP: 1.0)

let editResponse = try await openAI.generateEdit(parameters: editParameter)

let result = editResponse.choices[0].text
let chatCompletion = try await openAI.generateChatCompletion(
parameters: chatParameters
)

let result = chatCompletion.choices[0].message.content

return result
}
Expand Down Expand Up @@ -340,7 +350,7 @@ extension OpenAIView {
Task {
let input = "@startuml\n\(result)\n@enduml"

if let res = await service.generateEdit( input: input, instruction: instruction ) {
if let res = await service.generateChatCompletion( input: input, instruction: instruction ) {
service.status = .Ready

service.clipboard.push( result )
Expand Down
65 changes: 29 additions & 36 deletions PlantUML/PlantUMLContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,31 @@ struct PlantUMLContentView: View {
@State private var saving = false
@State private var diagramImage:UIImage?

@State private var editorViewId = 1
var body: some View {

VStack {
GeometryReader { geometry in

if( isEditorVisible ) {
EditorView_Fragment
VStack {
PlantUMLEditorView( content: $document.text,
darkTheme: CodeWebView.Theme(rawValue: darkTheme)!,
lightTheme: CodeWebView.Theme(rawValue: lightTheme)!,
isReadOnly: false,
fontSize: CGFloat(fontSize),
showGutter: showLine
)
.id( editorViewId )
.if( isRunningTests ) { /// this need for catching current editor data from UI test
$0.overlay(alignment: .bottom) {
Text( document.text )
.frame( width: 0, height: 0)
.opacity(0)
.accessibilityIdentifier("editor-text")
}
}
}
}

if isDiagramVisible {
Expand All @@ -71,8 +90,16 @@ struct PlantUMLContentView: View {
}
}
if isOpenAIVisible /* && interfaceOrientation.value.isPortrait */ {
OpenAIView_Fragment
OpenAIView( service: openAIService, result: $document.text )
.frame( height: 200 )
.onChange(of: openAIService.status ) { newStatus in
if( .Ready == newStatus ) {
// Force rendering editor view
// print( "FORCE RENDERING OF EDITOR VIEW")
editorViewId += 1
}
}

}
}
.onChange(of: document.text ) { _ in
Expand Down Expand Up @@ -127,45 +154,11 @@ struct PlantUMLContentView: View {
}
}

//
// MARK: - OpenAI extension -
//
extension PlantUMLContentView {

var OpenAIView_Fragment: some View {

OpenAIView( service: openAIService, result: $document.text )

}

}

//
// MARK: - Editor extension -
//
extension PlantUMLContentView {

var EditorView_Fragment: some View {

VStack {
PlantUMLEditorView( content: $document.text,
darkTheme: CodeWebView.Theme(rawValue: darkTheme)!,
lightTheme: CodeWebView.Theme(rawValue: lightTheme)!,
isReadOnly: false,
fontSize: CGFloat(fontSize),
showGutter: showLine
)
.if( isRunningTests ) { /// this need for catching current editor data from UI test
$0.overlay(alignment: .bottom) {
Text( document.text )
.frame( width: 0, height: 0)
.opacity(0)
.accessibilityIdentifier("editor-text")
}
}
}
}

// [SwiftUI Let View disappear automatically](https://stackoverflow.com/a/60820491/521197)
struct SavedStateView: View {
@Binding var visible: Bool
Expand Down
10 changes: 5 additions & 5 deletions PlantUML/Settings.bundle/Root.plist
Original file line number Diff line number Diff line change
Expand Up @@ -232,22 +232,22 @@
</dict>
<dict>
<key>DefaultValue</key>
<string>text-davinci-edit-001</string>
<string>gpt-3.5-turbo</string>
<key>Key</key>
<string>openaiModel</string>
<key>Title</key>
<string>Model</string>
<key>Titles</key>
<array>
<string>code-davinci-edit-001</string>
<string>text-davinci-edit-001</string>
<string>gpt-3.5-turbo</string>
<string>gpt-4</string>
</array>
<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>Values</key>
<array>
<string>code-davinci-edit-001</string>
<string>text-davinci-edit-001</string>
<string>gpt-3.5-turbo</string>
<string>gpt-4</string>
</array>
</dict>
</array>
Expand Down
22 changes: 11 additions & 11 deletions PlantUMLEditor/Sources/CodeViewer/CodeViewer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public struct CodeViewer: ViewRepresentable {
}

public func makeCoordinator() -> Coordinator {
Coordinator(content: $content,
Coordinator(content: content,
colorScheme: colorScheme,
lightTheme: lightTheme,
darkTheme: darkTheme,
Expand All @@ -78,7 +78,8 @@ public struct CodeViewer: ViewRepresentable {
codeView.clearSelection()
codeView.setShowGutter(showGutter)
codeView.textDidChanged = { text in
context.coordinator.set(content: text)
self.content = text
context.coordinator.content = text
self.textDidChanged?(text)
}
codeView.setTheme( colorScheme == .dark ? darkTheme : lightTheme )
Expand All @@ -87,6 +88,11 @@ public struct CodeViewer: ViewRepresentable {
}

private func updateView(_ webview: CodeWebView, context: Context) {
if context.coordinator.content != content {
context.coordinator.content = content
webview.setContent(content)
print(content)
}

if context.coordinator.fontSize != fontSize {
context.coordinator.fontSize = fontSize
Expand Down Expand Up @@ -145,34 +151,28 @@ public struct CodeViewer: ViewRepresentable {
public extension CodeViewer {

class Coordinator: NSObject {
@Binding private(set) var content: String
var content: String
var colorScheme: ColorScheme
var fontSize: CGFloat
var showGutter: Bool
var darkTheme:CodeWebView.Theme
var lightTheme:CodeWebView.Theme

init(content: Binding<String>,
init(content: String,
colorScheme: ColorScheme,
lightTheme:CodeWebView.Theme,
darkTheme:CodeWebView.Theme,
fontSize: CGFloat,
showGutter: Bool ) {

_content = content
self.content = content
self.colorScheme = colorScheme
self.darkTheme = darkTheme
self.lightTheme = lightTheme
self.fontSize = fontSize
self.showGutter = showGutter
}

func set(content: String) {
if self.content != content {
self.content = content
}
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion PlantUMLEditor/Sources/CodeViewer/CodeWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class CodeWebView: CustomView {
// And use String.raw to prevent escape some special string -> String will show exactly how it's
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
//
let first = "var content = String.raw`"
let first = "const content = String.raw`"
let content = """
\(value)
"""
Expand Down

0 comments on commit e9b20c8

Please sign in to comment.