Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class AITutorViewModel {
}

func presentChatBot() {
let vc = CoreHostingController(AIAssembly.makeChatBotView())
let vc = CoreHostingController(ChatBotAssembly.makeChatBotView())
router.show(vc, from: controller, options: .modal(isDismissable: false))
}

Expand Down Expand Up @@ -73,12 +73,12 @@ final class AITutorViewModel {
}

private func presentAIQuiz() {
let vc = CoreHostingController(AIAssembly.makeAIQuizView())
let vc = CoreHostingController(ChatBotAssembly.makeAIQuizView())
router.show(vc, from: controller, options: .modal(isDismissable: false))
}

private func presentFlashCard() {
let vc = CoreHostingController(AIAssembly.makeAIFlashCardView())
let vc = CoreHostingController(ChatBotAssembly.makeAIFlashCardView())
router.show(vc, from: controller, options: .modal(isDismissable: false))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// This file is part of Canvas.
// Copyright (C) 2025-present Instructure, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

import Core

class CedarAnswerPromptMutation: APIGraphQLRequestable {
let variables: Input

private let cedarJwtToken: String

var path: String {
"/graphql"
}

var headers: [String: String?] {
[
"x-apollo-operation-name": "AnswerPrompt",
HttpHeader.accept: "application/json"
]
}

public init(
cedarJwtToken: String,
prompt: String,
model: AIModel = .claude3Sonnet20240229V10
) {
self.variables = Variables(model: model.rawValue, prompt: prompt)
self.cedarJwtToken = cedarJwtToken
}

public static let operationName: String = "AnswerPrompt"
public static var query: String = """
mutation \(operationName)($model: String!, $prompt: String!) {
answerPrompt(input: { model: $model, prompt: $prompt })
}
"""

typealias Response = CedarAnswerPromptMutationResponse

struct Input: Codable, Equatable {
let model: String
let prompt: String
}
}

// MARK: - Codeables

struct CedarAnswerPromptMutationResponse: Codable {
struct ResponseData: Codable {
let answerPrompt: String
}

let data: ResponseData
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// This file is part of Canvas.
// Copyright (C) 2025-present Instructure, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

import Core

struct GetCedarJWTTokenRequest: APIRequestable {
typealias Response = GetCedarJWTTokenResponse

var path = "/api/v1/jwts?audience=cedar-api-dev.domain-svcs.nonprod.inseng.io&workflows[]=cedar"

var method: APIMethod { .post }
}

struct GetCedarJWTTokenResponse: Codable {
let token: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//
// This file is part of Canvas.
// Copyright (C) 2025-present Instructure, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

import Combine
import Core
import Foundation

protocol ChatBotInteractor {
func send(message: ChatBotMessage) -> AnyPublisher<String, Error>
}

class ChatBotInteractorLive: ChatBotInteractor {
// MARK: - Dependencies

private let canvasApi: API
private let cedarBaseUrl: String
private let model: AIModel

// MARK: - init

init(
canvasApi: API = AppEnvironment.shared.api,
cedarBaseUrl: String = "https://cedar-api-dev.domain-svcs.nonprod.inseng.io",
model: AIModel = .claude3Sonnet20240229V10
) {
self.canvasApi = canvasApi
self.cedarBaseUrl = cedarBaseUrl
self.model = model
}

// MARK: - Public

func send(message: ChatBotMessage) -> AnyPublisher<String, Error> {
getCedarJWTToken()
.flatMap { cedarJwtToken in
self.getAnswer(cedarJwtToken: cedarJwtToken, prompt: message.serialize())
}
.eraseToAnyPublisher()
}

// MARK: - Private

private func getCedarJWTToken() -> AnyPublisher<String, Error> {
canvasApi
.makeRequest(GetCedarJWTTokenRequest())
.tryMap(tokenResponseToUtf8String)
.eraseToAnyPublisher()
}

private func getAnswer(cedarJwtToken: String, prompt: String) -> AnyPublisher<String, Error> {
guard let baseUrl = URL(string: "https://cedar-api-dev.domain-svcs.nonprod.inseng.io") else {
return Fail(error: ChatBotInteractorError.invalidUrl).eraseToAnyPublisher()
}
return API(
LoginSession(
accessToken: cedarJwtToken,
baseURL: baseUrl,
userID: "",
userName: ""
),
baseURL: baseUrl
)
.makeRequest(CedarAnswerPromptMutation(cedarJwtToken: cedarJwtToken, prompt: prompt))
.map { graphQlResponse, _ in graphQlResponse.data.answerPrompt }
.eraseToAnyPublisher()
}

private func tokenResponseToUtf8String(tokenResponse: GetCedarJWTTokenResponse, urlResponse _: HTTPURLResponse?) throws -> String {
guard let decodedToken = Data(base64Encoded: tokenResponse.token) else {
throw ChatBotInteractorError.unableToGetCedarToken
}

let utf8EncodedToken = String(data: decodedToken, encoding: .utf8)

guard let utf8EncodedToken else {
throw ChatBotInteractorError.unableToGetCedarToken
}

return utf8EncodedToken
}
}

// MARK: - Enums

enum ChatBotInteractorError: Error {
case failedToDecodeToken
case unableToGetCedarToken
case invalidUrl
case unknownError
}

enum AIModel: String {
case claude3Sonnet20240229V10 = "anthropic.claude-3-sonnet-20240229-v1:0"
}

// MARK: - Extensions

extension ChatBotMessage {
func serialize() -> String {
text
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// This file is part of Canvas.
// Copyright (C) 2025-present Instructure, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

import Combine

class ChatBotInteractorPreview: ChatBotInteractor {
func send(message _: ChatBotMessage) -> AnyPublisher<String, Error> {
Just("Hello, world!")
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// This file is part of Canvas.
// Copyright (C) 2025-present Instructure, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

enum ChatBotRole: String {
case user
case system
case assistant
}

struct ChatBotMessage {
let text: String
let role: ChatBotRole
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ struct ChatBotView: View {

#if DEBUG
#Preview {
ChatBotView(viewModel: .init(router: AppEnvironment.shared.router))
ChatBotView(
viewModel: .init(
chatbotInteractor: ChatBotInteractorPreview(),
router: AppEnvironment.shared.router
)
)
}
#endif
Loading