-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Stash * Prepare chat assistant * Rebase * Update server + txt + format
- Loading branch information
Showing
40 changed files
with
1,050 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,3 +74,5 @@ fastlane/metadata/trade_representative_contact_information | |
|
||
vendor/ | ||
.bundle/ | ||
|
||
Config.xcconfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
5.8 | ||
5.9 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Foundation | ||
import HTTPTypes | ||
import OpenAPIRuntime | ||
import OpenAPIURLSession | ||
|
||
protocol ChatAssistantProtocol { | ||
func generateSynthesis(content: String) async throws -> String | ||
func generateTags(content: String) async throws -> [String] | ||
} | ||
|
||
struct ChatAssistant: ChatAssistantProtocol { | ||
var client: Client { | ||
get throws { | ||
try Client( | ||
serverURL: Servers.server2(), | ||
transport: URLSessionTransport(), | ||
middlewares: [AuthenticationMiddleware()] | ||
) | ||
} | ||
} | ||
|
||
var locale = Locale.current.identifier | ||
|
||
func generateSynthesis(content: String) async throws -> String { | ||
let response = try await client.wallabagSynthesis(body: .json(.init(body: content, language: locale))) | ||
|
||
return try response.ok.body.json.content ?? "" | ||
} | ||
|
||
func generateTags(content: String) async throws -> [String] { | ||
let response = try await client.wallabagTags(body: .json(.init(body: content, language: locale))) | ||
|
||
return try response.ok.body.json.tags ?? [] | ||
} | ||
} | ||
|
||
private struct AuthenticationMiddleware: ClientMiddleware { | ||
@BundleKey("GPTBACK_KEY") | ||
private var gptBackKey | ||
|
||
func intercept( | ||
_ request: HTTPTypes.HTTPRequest, | ||
body: OpenAPIRuntime.HTTPBody?, | ||
baseURL: URL, | ||
operationID _: String, | ||
next: @Sendable (HTTPTypes.HTTPRequest, OpenAPIRuntime.HTTPBody?, URL) async throws -> ( | ||
HTTPTypes.HTTPResponse, | ||
OpenAPIRuntime.HTTPBody? | ||
) | ||
) async throws -> (HTTPTypes.HTTPResponse, OpenAPIRuntime.HTTPBody?) { | ||
var request = request | ||
request.headerFields[.authorization] = "Bearer \(gptBackKey)" | ||
|
||
return try await next(request, body, baseURL) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// SynthesisEntryView.swift | ||
// wallabag | ||
// | ||
// Created by maxime marinel on 11/12/2023. | ||
// | ||
|
||
import Factory | ||
import SwiftUI | ||
|
||
struct SynthesisEntryView: View { | ||
@StateObject private var viewModel = SynthesisEntryViewModel() | ||
let entry: Entry | ||
|
||
var body: some View { | ||
ScrollView { | ||
if viewModel.isLoading { | ||
Text("Your assistant is working") | ||
ProgressView() | ||
} else { | ||
Text(viewModel.synthesis) | ||
.padding() | ||
.fontDesign(.serif) | ||
} | ||
} | ||
.navigationTitle("Synthesis") | ||
.task { | ||
do { | ||
try await viewModel.generateSynthesis(from: entry) | ||
} catch { | ||
print(error) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// #Preview { | ||
// SynthesisEntryView(entry: .) | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// SynthesisEntryViewModel.swift | ||
// wallabag | ||
// | ||
// Created by maxime marinel on 22/01/2024. | ||
// | ||
|
||
import Factory | ||
import Foundation | ||
|
||
final class SynthesisEntryViewModel: ObservableObject { | ||
@Injected(\.chatAssistant) private var chatAssistant | ||
@Published var synthesis = "" | ||
@Published var isLoading = false | ||
|
||
@MainActor | ||
func generateSynthesis(from entry: Entry) async throws { | ||
defer { | ||
isLoading = false | ||
} | ||
isLoading = true | ||
|
||
guard let content = entry.content?.withoutHTML else { return } | ||
|
||
synthesis = try await chatAssistant.generateSynthesis(content: content) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import Factory | ||
import SwiftUI | ||
|
||
struct TagSuggestionView: View { | ||
@Environment(\.dismiss) private var dismiss | ||
@StateObject private var viewModel = TagSuggestionViewModel() | ||
|
||
let entry: Entry | ||
|
||
var body: some View { | ||
VStack { | ||
if viewModel.isLoading { | ||
Text("Your assistant is working") | ||
ProgressView() | ||
} else { | ||
List { | ||
ForEach(viewModel.suggestions, id: \.self) { suggestion in | ||
Button(action: { | ||
if viewModel.tagSelections.contains(suggestion) { | ||
viewModel.tagSelections.remove(suggestion) | ||
} else { | ||
viewModel.tagSelections.insert(suggestion) | ||
} | ||
}, label: { | ||
HStack { | ||
Text(suggestion) | ||
.padding() | ||
.fontDesign(.serif) | ||
Spacer() | ||
if viewModel.tagSelections.contains(suggestion) { | ||
Image(systemName: "checkmark.circle.fill") | ||
} else { | ||
Image(systemName: "circle") | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
.listStyle(.plain) | ||
if !viewModel.tagSelections.isEmpty { | ||
Button(action: { | ||
Task { | ||
try? await viewModel.addTags(to: entry) | ||
dismiss() | ||
} | ||
}, label: { | ||
if viewModel.addingTags { | ||
ProgressView() | ||
} else { | ||
Text("Add \(viewModel.tagSelections.count.formatted()) tags") | ||
} | ||
}) | ||
.buttonStyle(.borderedProminent) | ||
.disabled(viewModel.addingTags) | ||
} | ||
} | ||
} | ||
.navigationTitle("Tag suggestion") | ||
.task { | ||
do { | ||
try await viewModel.generateTags(from: entry) | ||
} catch { | ||
print(error) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// #Preview { | ||
// SynthesisEntryView(entry: .) | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// TagSuggestionViewModel.swift | ||
// wallabag | ||
// | ||
// Created by maxime marinel on 22/01/2024. | ||
// | ||
|
||
import Factory | ||
import Foundation | ||
|
||
final class TagSuggestionViewModel: ObservableObject { | ||
@Injected(\.wallabagSession) private var wallabagSession | ||
@Injected(\.chatAssistant) private var chatAssistant | ||
@Published var suggestions: [String] = [] | ||
@Published var isLoading = false | ||
@Published var addingTags = false | ||
@Published var tagSelections = Set<String>() | ||
|
||
@MainActor | ||
func generateTags(from entry: Entry) async throws { | ||
defer { | ||
isLoading = false | ||
} | ||
isLoading = true | ||
|
||
guard let content = entry.content?.withoutHTML else { return } | ||
|
||
suggestions = try await chatAssistant.generateTags(content: content) | ||
} | ||
|
||
@MainActor | ||
func addTags(to entry: Entry) async throws { | ||
defer { | ||
addingTags = false | ||
} | ||
addingTags = true | ||
for tag in tagSelections { | ||
wallabagSession.add(tag: tag, for: entry) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.