mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-25 11:55:47 -06:00
7a5de93228
Amp-Thread-ID: https://ampcode.com/threads/T-01a037b7-9244-7298-b368-3faab8a11cbf Co-authored-by: Amp <amp@ampcode.com>
76 lines
2.6 KiB
Swift
76 lines
2.6 KiB
Swift
import OpenClawKit
|
|
|
|
public enum OpenClawChatEventText {
|
|
public static func assistantText(from event: OpenClawChatEventPayload) -> String? {
|
|
self.assistantText(fromMessage: event.message)
|
|
}
|
|
|
|
public static func assistantText(fromMessage message: AnyCodable?) -> String? {
|
|
guard let message else { return nil }
|
|
return self.assistantText(fromValue: message.value)
|
|
}
|
|
|
|
private static func assistantText(fromValue value: Any) -> String? {
|
|
if let text = value as? String {
|
|
return ChatPayloadDecoding.trimmedNonEmptyString(text)
|
|
}
|
|
|
|
guard let object = self.dictionary(from: value) else { return nil }
|
|
if let role = self.stringValue(object["role"])?.trimmingCharacters(in: .whitespacesAndNewlines),
|
|
!role.isEmpty,
|
|
role.lowercased() != "assistant"
|
|
{
|
|
return nil
|
|
}
|
|
|
|
guard let content = object["content"] else { return nil }
|
|
return self.textContent(from: content)
|
|
}
|
|
|
|
private static func textContent(from value: Any) -> String? {
|
|
if let text = value as? String {
|
|
return ChatPayloadDecoding.trimmedNonEmptyString(text)
|
|
}
|
|
|
|
let parts: [String] = if let array = value as? [AnyCodable] {
|
|
array.compactMap { self.textContentPart(from: $0.value) }
|
|
} else if let array = value as? [Any] {
|
|
array.compactMap { self.textContentPart(from: $0) }
|
|
} else {
|
|
self.textContentPart(from: value).map { [$0] } ?? []
|
|
}
|
|
|
|
return ChatPayloadDecoding.trimmedNonEmptyString(parts.joined(separator: "\n"))
|
|
}
|
|
|
|
private static func textContentPart(from value: Any) -> String? {
|
|
if let text = value as? String {
|
|
return ChatPayloadDecoding.trimmedNonEmptyString(text)
|
|
}
|
|
guard let object = self.dictionary(from: value) else { return nil }
|
|
guard ChatMessageVisibleText.isVisibleContentType(
|
|
self.stringValue(object["type"]),
|
|
role: "assistant")
|
|
else { return nil }
|
|
return ChatPayloadDecoding.trimmedNonEmptyString(self.stringValue(object["text"]))
|
|
}
|
|
|
|
private static func dictionary(from value: Any) -> [String: Any]? {
|
|
if let dict = value as? [String: AnyCodable] {
|
|
return dict.mapValues(\.value)
|
|
}
|
|
if let dict = value as? [String: Any] {
|
|
return dict
|
|
}
|
|
return nil
|
|
}
|
|
|
|
private static func stringValue(_ value: Any?) -> String? {
|
|
if let string = value as? String { return string }
|
|
if let wrapped = value as? AnyCodable {
|
|
return self.stringValue(wrapped.value)
|
|
}
|
|
return nil
|
|
}
|
|
}
|