@@ -13,7 +13,7 @@ public final class WebChatContextCollector: ChatContextCollector {
1313 content: String
1414 ) -> ChatContext ? {
1515 guard scopes. contains ( " web " ) else { return nil }
16- let links = Self . detectLinks ( from: history)
16+ let links = Self . detectLinks ( from: history) + Self . detectLinks ( from : content )
1717 let functions : [ ( any ChatGPTFunction ) ? ] = [
1818 SearchFunction ( ) ,
1919 // allow this function only when there is a link in the memory.
@@ -28,21 +28,22 @@ public final class WebChatContextCollector: ChatContextCollector {
2828
2929extension WebChatContextCollector {
3030 static func detectLinks( from messages: [ ChatMessage ] ) -> [ String ] {
31+ return messages. lazy. compactMap ( \. content) . map ( detectLinks ( from: ) ) . flatMap { $0 }
32+ }
33+
34+ static func detectLinks( from content: String ) -> [ String ] {
3135 var links = [ String] ( )
3236 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- )
37+ let matches = detector? . matches (
38+ in: content,
39+ options: [ ] ,
40+ range: NSRange ( content. startIndex... , in: content)
41+ )
4042
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- }
43+ for match in matches ?? [ ] {
44+ guard let range = Range ( match. range, in: content) else { continue }
45+ let url = content [ range]
46+ links. append ( String ( url) )
4647 }
4748 return links
4849 }
0 commit comments