Skip to content

Commit 6060104

Browse files
committed
Update WebChatContextCollector to detect links from memory
1 parent 7c75599 commit 6060104

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

Core/Sources/ChatContextCollectors/WebChatContextCollector/WebChatContextCollector.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import ChatContextCollector
22
import Foundation
33
import OpenAIService
4-
import SuggestionModel
54

65
public final class WebChatContextCollector: ChatContextCollector {
76
var recentLinks = [String]()
8-
7+
98
public init() {}
109

1110
public func generateContext(
@@ -28,8 +27,24 @@ public final class WebChatContextCollector: ChatContextCollector {
2827
}
2928

3029
extension WebChatContextCollector {
31-
static func detectLinks(from: [ChatMessage]) -> [String] {
32-
return []
30+
static func detectLinks(from messages: [ChatMessage]) -> [String] {
31+
var links = [String]()
32+
let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
33+
for message in messages {
34+
guard let content = message.content else { continue }
35+
let matches = detector?.matches(
36+
in: content,
37+
options: [],
38+
range: NSRange(content.startIndex..., in: content)
39+
)
40+
41+
for match in matches ?? [] {
42+
guard let range = Range(match.range, in: content) else { continue }
43+
let url = content[range]
44+
links.append(String(url))
45+
}
46+
}
47+
return links
3348
}
3449
}
3550

0 commit comments

Comments
 (0)