Skip to content

Commit

Permalink
feat: complete demo using UI test process
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Mar 23, 2024
1 parent bf7279e commit cc8c812
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 15 deletions.
104 changes: 104 additions & 0 deletions AIAgent/Sources/AIAgent/AgentExecutorDemo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// AgentExecutorDemo.swift
//
//
// Created by bsorrentino on 23/03/24.
//

import Foundation
import OSLog
import OpenAI
import LangGraph


struct AgentExecutorDemoState : AgentState {

var data: [String : Any]

init() {
data = [:]
}

init(_ initState: [String : Any]) {
data = initState
}

var diagramCode:String? {
data["diagram_code"] as? String
}

}

public func runTranslateDrawingToPlantUMLDemo<T:AgentExecutorDelegate>( openAI: OpenAI,
imageUrl: String,
delegate:T ) async throws -> String? {

let workflow = GraphState { AgentExecutorState() }

try workflow.addNode("agent_describer", action: { state in
await delegate.progress("starting analyze\ndiagram 👀")

try await Task.sleep( nanoseconds: 5_000_000_000 )

await delegate.progress( "diagram processed ✅")

return [ : ]

})

try workflow.addNode("agent_generic_plantuml", action: { state in
await delegate.progress("translating diagram to\nGeneric Diagram")

try await Task.sleep( nanoseconds: 5_000_000_000 )

let content =
"""
actor "User Initiating The Diagram Translation Process" as userInitiatingTheDiagramTranslationProcess<<User initiating the diagram translation process>>
rectangle "Provide Diagram Image" as provideDiagramImage<<Process of providing a diagram image>>
rectangle "Process Image" as processImage<<Process of processing the provided image>>
rectangle "Description" as description<<Block describing the image>>
rectangle "Check Type" as checkType<<Decision block to determine the type of diagram>>
rectangle "Sequence" as sequence<<Indicates a sequence diagram type>>
rectangle "Generic" as generic<<Indicates a generic diagram type>>
rectangle "Translate To Sequence" as translateToSequence<<Action to translate to a sequence diagram>>
rectangle "Translate To Generic" as translateToGeneric<<Action to translate to a generic diagram>>
legend
- 1. The USER initiates the process.
- 2. The USER provides the diagram image.
- 3. The PROVIDED DIAGRAM IMAGE is processed.
- 4. The process results in a DESCRIPTION of the image.
- 5. The DESCRIPTION leads to a CHECK TYPE decision.
- 6. Based on the decision, if the diagram is a sequence type, it proceeds to TRANSLATE TO SEQUENCE.
- 7. If the diagram is a generic type, it proceeds to TRANSLATE TO GENERIC.
end legend
userInitiatingTheDiagramTranslationProcess --> provideDiagramImage : User provides a diagram image
provideDiagramImage --> processImage : Provided image is processed
processImage --> description : Processed image is described
description --> checkType : Description leads to checking the type
checkType --> sequence : Decision made for sequence type
checkType --> generic : Decision made for generic type
sequence --> translateToSequence : Sequence type is translated
generic --> translateToGeneric : Generic type is translated
"""
return [ "diagram_code": content ]

})

try workflow.addEdge(sourceId: "agent_generic_plantuml", targetId: END)

try workflow.addEdge( sourceId: "agent_describer",
targetId: "agent_generic_plantuml" )

try workflow.setEntryPoint( "agent_describer")

let app = try workflow.compile()

let inputs:[String : Any] = [:]

let response = try await app.invoke( inputs: inputs)

return response.diagramCode
}


30 changes: 21 additions & 9 deletions PlantUML/OpenAIObservableService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,28 @@ extension OpenAIObservableService {

do {

if let content = try await runTranslateDrawingToPlantUML( openAI: openAI, imageUrl: imageUrl, delegate:delegate) {

status = .Ready

return content
.split( whereSeparator: \.isNewline )
.filter { $0 != "@startuml" && $0 != "@enduml" }
.joined(separator: "\n" )
if DEMO_MODE {
if let content = try await runTranslateDrawingToPlantUMLDemo( openAI: openAI, imageUrl: imageUrl, delegate:delegate) {

status = .Ready

return content
.split( whereSeparator: \.isNewline )
.filter { $0 != "@startuml" && $0 != "@enduml" }
.joined(separator: "\n" )
}
}
else {
if let content = try await runTranslateDrawingToPlantUML( openAI: openAI, imageUrl: imageUrl, delegate:delegate) {

status = .Ready

return content
.split( whereSeparator: \.isNewline )
.filter { $0 != "@startuml" && $0 != "@enduml" }
.joined(separator: "\n" )
}
}

delegate.progress("ERROR: invalid result!")
status = .Error( "invalid result!" )
}
Expand Down
5 changes: 5 additions & 0 deletions PlantUML/PlantUMLApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ struct PlantUMLApp: App {
// Config Settings
print( "DEMO_MODE: \(DEMO_MODE)" )
print( "SAVE_DRAWING_IMAGE: \(SAVE_DRAWING_IMAGE)" )
let documentDir = try? FileManager.default.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
print( "DOCUMENT DIRECTORY\n\(String(describing: documentDir))")
}

var body: some Scene {
Expand Down
2 changes: 1 addition & 1 deletion PlantUML/SwiftUI+PencilKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct DrawingView: UIViewRepresentable {
let drawing = try PKDrawing(data: data)

if DEMO_MODE {
slowDrawingForDemo(drawing, timeInterval: 0.4)
slowDrawingForDemo(drawing, timeInterval: 0.2)
}
else {
canvas.drawing = drawing
Expand Down
15 changes: 10 additions & 5 deletions PlantUMLAppUITests/DrawingUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class DrawingUITests: XCTestCase {

}

func testDrawDiagram() throws {
func testDrawDiagramForDemo() throws {
// UI tests must launch the application that they test.
let app = XCUIApplication()

Expand All @@ -59,14 +59,19 @@ final class DrawingUITests: XCTestCase {

app.buttons["drawing_tools"].tap()

wait(reason: "Wait for drawing graph", timeout: 20 )
wait(reason: "Wait for drawing graph", timeout: 38 )

app.buttons["drawing_process"].tap()

wait(reason: "Wait for process drawing", timeout: 30 )
XCTAssertTrue( app.buttons["diagram_preview"].waitForExistence(timeout: 60) )
XCTAssertTrue( app.buttons["cancel_processing"].waitForExistence(timeout: 10) )

app.buttons["diagram_preview"].tap()
wait(reason: "Wait until show preview", timeout: 10 )

XCTAssertTrue( app.buttons["diagram_preview"].waitForExistence(timeout: 10) )

app.buttons["diagram_preview"].forceTap()

wait(reason: "preview time", timeout: 20 )
}

func testLaunchPerformance() throws {
Expand Down

0 comments on commit cc8c812

Please sign in to comment.