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
14 changes: 3 additions & 11 deletions Sources/SwiftDEBot/Command/Message Commands/Summarize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ struct SummarizeCommand: MessageCommand {
} catch OpenRouterError.missingAPIKey {
try await client.send("💀 OPENROUTER_API_KEY fehlt", to: message.channel_id)
} catch let error {
log.error("Failure to send message: \(error)")
log.error("Failure to summarize: \(error)")
try await client.send("Ich hatte einen Fehler beim Zusammenfassen 😵", to: message.channel_id)
}
}

Expand All @@ -63,7 +64,7 @@ struct SummarizeCommand: MessageCommand {

func summarize(url: String) async throws -> String {
log.info("Summarizing \(url)")
guard let encodedURL = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
guard let encodedURL = url.addingPercentEncoding(withAllowedCharacters: .queryValueAllowed) else {
log.error("Unable to URL encode \(url)")
throw SummaryError.brokenURL
}
Expand Down Expand Up @@ -130,15 +131,6 @@ enum SummaryError: Error {
case emptySummary
}

extension String {
fileprivate var firstURL: String? {
if let found = self.firstMatch(of: #/(https?://\S+)/#) {
return String(found.output.1)
}
return nil
}
}

private struct KagiResponse: Decodable {
let data: ResponseData

Expand Down
19 changes: 17 additions & 2 deletions Sources/SwiftDEBot/Utils/HTTPClient+parsed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension HTTPClient {
let body = try await response.body.collect(upTo: 50 * 1024 * 1024) // 50 MB
return try JSONDecoder().decode(Response.self, from: body)
} else {
throw HTTPStatusError(status: response.status)
throw await HTTPStatusError(loggingBodyOf: response, from: url)
}
}

Expand All @@ -45,7 +45,7 @@ extension HTTPClient {
let body = try await response.body.collect(upTo: 50 * 1024 * 1024) // 50 MB
return try JSONDecoder().decode(Response.self, from: body)
} else {
throw HTTPStatusError(status: response.status)
throw await HTTPStatusError(loggingBodyOf: response, from: url)
}
}
}
Expand All @@ -54,4 +54,19 @@ struct HTTPStatusError: Error, CustomStringConvertible {
let status: HTTPResponseStatus

var description: String { status.description }

/// Logs the start of the body, where APIs explain their errors. It stays out of `description` because some
/// commands post that to the channel.
init(loggingBodyOf response: HTTPClientResponse, from url: String) async {
status = response.status
var body = ByteBuffer()
do {
for try await var chunk in response.body {
body.writeBuffer(&chunk)
if body.readableBytes >= 500 { break }
}
} catch {}
let host = URL(string: url)?.host ?? "unknown host"
log.warning("\(host) returned \(status): \(String(buffer: body).prefix(500))")
}
}
12 changes: 12 additions & 0 deletions Sources/SwiftDEBot/Utils/String.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import Foundation

extension String {
/// The first http or https URL in a message. Stops at `>` because Discord users wrap links in `<…>` to suppress
/// their preview, and the brackets are not part of the URL.
var firstURL: String? {
firstMatch(of: #/https?://[^\s>]+/#).map { String($0.output) }
}

/// Splits text into non-empty messages within Discord's length limit, preferring to cut at whitespace so line
/// breaks survive and words stay whole. Words longer than the limit are cut hard.
func maxMessageLengthChunks(maxChars: Int = 2000) -> [String] {
Expand All @@ -24,3 +30,9 @@ extension String {
return chunks
}
}

extension CharacterSet {
/// For a whole URL passed as one query value. `urlQueryAllowed` keeps `&`, `=` and `+`, which would split the
/// value into further parameters or turn into a space on the server.
static let queryValueAllowed = CharacterSet.urlQueryAllowed.subtracting(CharacterSet(charactersIn: "&=+"))
}
57 changes: 57 additions & 0 deletions Tests/SwiftDEBotTests/SummarizeLinkTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Foundation
import PropertyBased
import Testing

@testable import SwiftDEBot

private let whitespace = Gen<Character?>
.element(of: [" ", "\t", "\n"] as [Character])
.compactMap { $0 }
.string(of: 1...3)

private let words = Gen.letterOrNumber.string(of: 1...8).array(of: 0...4)

private let urlCharacter = Gen<Character?>
.element(of: Array("abcXYZ019-_.~?=&%#:/@+"))
.compactMap { $0 }

private let urls = zip(
Gen.bool,
Gen.lowercaseLetter.string(of: 1...10).array(of: 2...3),
urlCharacter.string(of: 0...20)
)
.map { secure, labels, path in
(secure ? "https" : "http") + "://" + labels.joined(separator: ".") + "/" + path
}

/// Joins tokens with the separators, taken cyclically.
private func joined(_ tokens: [String], separators: [String]) -> String {
tokens.enumerated().map { index, token in
index == 0 ? token : separators[index % separators.count] + token
}.joined()
}

struct SummarizeLinkTests {
@Test func returnsFirstURLWithoutEmbedSuppressingBrackets() async {
await propertyCheck(
count: 300,
input: words, urls, Gen.bool, words, zip(Gen.bool, urls), whitespace.array(of: 1...4)
) { before, url, wrapped, after, second, separators in
let (hasSecond, secondURL) = second
let tokens =
before + [wrapped ? "<\(url)>" : url] + after + (hasSecond ? [secondURL] : [])
let message = joined(tokens, separators: separators)
#expect(message.firstURL == url)
}
}

@Test func encodedLinkSurvivesAsOneQueryValue() async {
await propertyCheck(input: urls) { url in
let encoded = try #require(url.addingPercentEncoding(withAllowedCharacters: .queryValueAllowed))
let components = URLComponents(string: "https://kagi.com/api?target_language=DE&url=\(encoded)")
#expect(components?.queryItems?.last == URLQueryItem(name: "url", value: url))
// Servers commonly decode a literal + in a query as a space.
#expect(!encoded.contains("+"))
}
}
}
Loading