Skip to content

Commit

Permalink
catch summarize on otherwise empty message with no image attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
kiliankoe committed Oct 25, 2024
1 parent 00eb6c1 commit 633db7f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Sources/SwiftDEBot/Command/Message Commands/Summarize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct SummarizeCommand: MessageCommand {
)
case .emptySummary:
try await client.send(
"Das kann ich leider nicht zusammenfassen 🫥: \(error)",
"Das kann ich leider nicht zusammenfassen 🫥",
to: message.channel_id
)
}
Expand Down Expand Up @@ -101,7 +101,7 @@ struct SummarizeCommand: MessageCommand {
let response = try await httpClient.post(
"https://api.openai.com/v1/chat/completions",
headers: ["Authorization": "Bearer \(apiToken)"],
body: OpenAIRequest(message: messageContent),
body: try OpenAIRequest(message: messageContent),
response: OpenAIResponse.self
)

Expand Down Expand Up @@ -157,7 +157,7 @@ private struct OpenAIRequest: Encodable {
let messages: [Message]
let temperature: Double

init(message: Gateway.MessageCreate) {
init(message: Gateway.MessageCreate) throws {
self.model = "gpt-4o-mini"

var content = message.attachments.compactMap { attachment -> Message.ContentUnion.ContentElement? in
Expand All @@ -169,6 +169,10 @@ private struct OpenAIRequest: Encodable {
content.append(.init(type: "text", text: message.content, image_url: nil))
}

guard !content.isEmpty else {
throw SummaryError.emptySummary
}

self.messages = [
.init(
role: "system",
Expand Down

0 comments on commit 633db7f

Please sign in to comment.