Skip to content

Commit

Permalink
feat: polishing ai demo (#95)
Browse files Browse the repository at this point in the history
* feat: polishing ai demo

* feat: repoint to remote Swift SDK
  • Loading branch information
louisjoecodes authored Nov 11, 2024
1 parent 86b1873 commit e48b250
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 647 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
BF5D8A802CDA218900E1EEE4 /* Starscream in Frameworks */ = {isa = PBXBuildFile; productRef = BF5D8A7F2CDA218900E1EEE4 /* Starscream */; };
BFB984172CE2501C00E9087A /* ElevenLabsSDK in Frameworks */ = {isa = PBXBuildFile; productRef = BFB984162CE2501C00E9087A /* ElevenLabsSDK */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -27,7 +27,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
BF5D8A802CDA218900E1EEE4 /* Starscream in Frameworks */,
BFB984172CE2501C00E9087A /* ElevenLabsSDK in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -70,7 +70,7 @@
);
name = ConversationalAISwift;
packageProductDependencies = (
BF5D8A7F2CDA218900E1EEE4 /* Starscream */,
BFB984162CE2501C00E9087A /* ElevenLabsSDK */,
);
productName = ConversationalAISwift;
productReference = BF5D8A6B2CDA213E00E1EEE4 /* ConversationalAISwift.app */;
Expand Down Expand Up @@ -101,7 +101,7 @@
mainGroup = BF5D8A622CDA213E00E1EEE4;
minimizedProjectReferenceProxies = 1;
packageReferences = (
BF5D8A7E2CDA218900E1EEE4 /* XCRemoteSwiftPackageReference "Starscream" */,
BF7C81012CE24FA600D2C589 /* XCRemoteSwiftPackageReference "ElevenLabsSwift" */,
);
preferredProjectObjectVersion = 77;
productRefGroup = BF5D8A6C2CDA213E00E1EEE4 /* Products */;
Expand Down Expand Up @@ -339,21 +339,21 @@
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
BF5D8A7E2CDA218900E1EEE4 /* XCRemoteSwiftPackageReference "Starscream" */ = {
BF7C81012CE24FA600D2C589 /* XCRemoteSwiftPackageReference "ElevenLabsSwift" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/daltoniam/Starscream";
repositoryURL = "https://github.com/elevenlabs/ElevenLabsSwift.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 4.0.8;
branch = main;
kind = branch;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
BF5D8A7F2CDA218900E1EEE4 /* Starscream */ = {
BFB984162CE2501C00E9087A /* ElevenLabsSDK */ = {
isa = XCSwiftPackageProductDependency;
package = BF5D8A7E2CDA218900E1EEE4 /* XCRemoteSwiftPackageReference "Starscream" */;
productName = Starscream;
package = BF7C81012CE24FA600D2C589 /* XCRemoteSwiftPackageReference "ElevenLabsSwift" */;
productName = ElevenLabsSDK;
};
/* End XCSwiftPackageProductDependency section */
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
{
"originHash" : "95d26df518f25802c8782300581011b8eafe521f7ab32e3995e57dccf6e16b58",
"originHash" : "f892ad40c2f1f3b79e4f79c2f5206408fac766cdf5e53ed062aa5831f442b39e",
"pins" : [
{
"identity" : "elevenlabsswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/elevenlabs/ElevenLabsSwift.git",
"state" : {
"branch" : "main",
"revision" : "afc53ff6dc6a205b879f6fa3ebe189474dd5e810"
}
},
{
"identity" : "starscream",
"kind" : "remoteSourceControl",
"location" : "https://github.com/daltoniam/Starscream",
"location" : "https://github.com/daltoniam/Starscream.git",
"state" : {
"revision" : "c6bfd1af48efcc9a9ad203665db12375ba6b145a",
"version" : "4.0.8"
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import SwiftUI
import ElevenLabsSDK
import _Concurrency

struct OrbView: View {
let mode: ElevenLabsSDK.ConversationMode
let mode: ElevenLabsSDK.Mode
let audioLevel: Float

private var iconName: String {
Expand Down Expand Up @@ -43,35 +46,134 @@ struct OrbView: View {
}
}

// Updated main view
struct ConversationalAIExampleView: View {
@ObservedObject private var sdk = ElevenLabsSDK.shared
@State private var currentAgentIndex = 0
@State private var conversation: ElevenLabsSDK.Conversation?
@State private var audioLevel: Float = 0.0
@State private var mode: ElevenLabsSDK.Mode = .listening
@State private var status: ElevenLabsSDK.Status = .disconnected

let agents = [
Agent(
id: "<REPLACE-WITH-YOUR-AGENT-ID>",
id: "<insert agent id here>",
name: "Matilda",
description: "Math tutor"
),
Agent(
id: "<insert agent id here>",
name: "Eric",
description: "Support agent"
),
Agent(
id: "<insert agent id here>",
name: "Callum",
description: "Video game character"
)
]

// MARK: - Functions
private func startConversation() {
let config = ElevenLabsSDK.SessionConfig(agentId: "wer")
conversation?.startRecording()

Task {
do {
var callbacks = ElevenLabsSDK.Callbacks()
callbacks.onConnect = { conversationId in
print("Connected with conversation ID: \(conversationId)")
}
callbacks.onDisconnect = {
print("Disconnected")
}
callbacks.onMessage = { message, role in
DispatchQueue.main.async {
print(message)
}
}
callbacks.onError = { errorMessage, _ in
print("Error: \(errorMessage)")
}
callbacks.onStatusChange = { newStatus in
DispatchQueue.main.async {

}
}
callbacks.onModeChange = { newMode in
DispatchQueue.main.async {

}
}

conversation = try await ElevenLabsSDK.Conversation.startSession(config: config, callbacks: callbacks)
} catch {
print("Failed to start conversation: \(error.localizedDescription)")
}
}
}
private func beginConversation(agent: Agent) {
if sdk.connectionStatus == .connected {
sdk.endConversation()
if status == .connected {
conversation?.endSession()
conversation = nil
} else {
sdk.configure(with: .init(agentId: agent.id))
sdk.startConversation()
Task {
do {
let config = ElevenLabsSDK.SessionConfig(agentId: agent.id)
var callbacks = ElevenLabsSDK.Callbacks()

callbacks.onConnect = { conversationId in

status = .connected
}
callbacks.onDisconnect = {

status = .disconnected
}
callbacks.onMessage = { message, role in
DispatchQueue.main.async {
print(message)
}
}

callbacks.onError = { errorMessage, _ in
print("Error: \(errorMessage)")
}

// Move these callbacks out from inside onStatusChange
callbacks.onStatusChange = { newStatus in
DispatchQueue.main.async {
status = newStatus
}
}

callbacks.onModeChange = { newMode in
DispatchQueue.main.async {
mode = newMode
}
}

callbacks.onVolumeUpdate = { newVolume in
DispatchQueue.main.async {
audioLevel = newVolume
}
}

conversation = try await ElevenLabsSDK.Conversation.startSession(config: config, callbacks: callbacks)
} catch {
print("Error starting conversation: \(error)")
}
}
}
}



var body: some View {
ZStack {
GeometryReader { geometry in
VStack {
Spacer()

OrbView(mode: sdk.currentMode, audioLevel: sdk.audioLevel)
OrbView(mode: mode, audioLevel: audioLevel)
.padding(.bottom, 20)

Text(agents[currentAgentIndex].name)
Expand All @@ -83,7 +185,7 @@ struct ConversationalAIExampleView: View {
.foregroundColor(.gray)

HStack(spacing: 8) {
ForEach(0..<agents.count, id: \.self) { index in
ForEach(0..<agents.count, id: \ .self) { index in
Circle()
.fill(index == currentAgentIndex ? Color.black : Color.gray)
.frame(width: 8, height: 8)
Expand All @@ -94,7 +196,7 @@ struct ConversationalAIExampleView: View {
Spacer()

CallButton(
connectionStatus: sdk.connectionStatus,
connectionStatus: status,
action: { beginConversation(agent: agents[currentAgentIndex]) }
)
}
Expand All @@ -114,7 +216,7 @@ struct ConversationalAIExampleView: View {
.gesture(
DragGesture()
.onEnded { value in
guard sdk.connectionStatus != .connected else { return }
guard status != .connected else { return }

if value.translation.width < 0 && currentAgentIndex < agents.count - 1 {
currentAgentIndex += 1
Expand All @@ -128,7 +230,7 @@ struct ConversationalAIExampleView: View {

// MARK: - Call Button Component
struct CallButton: View {
let connectionStatus: ElevenLabsSDK.ConnectionStatus
let connectionStatus: ElevenLabsSDK.Status
let action: () -> Void

private var buttonIcon: String {
Expand Down Expand Up @@ -172,7 +274,6 @@ struct CallButton: View {
}
}


// MARK: - Types and Preview
struct Agent {
let id: String
Expand Down
Loading

0 comments on commit e48b250

Please sign in to comment.