Add custom MCP profiles and reusable workspace libraries
This commit is contained in:
@@ -99,3 +99,56 @@ struct DocumentVector {
|
||||
return Self(language: "\(language.rawValue):\(model.revision)", values: values.map { Float($0 / magnitude) })
|
||||
}
|
||||
}
|
||||
|
||||
enum WebClipCapture {
|
||||
static func url(_ address: String) throws -> URL {
|
||||
let input = address.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard let parts = URLComponents(string: input), let scheme = parts.scheme?.lowercased(),
|
||||
["http", "https"].contains(scheme), let host = parts.host, !host.isEmpty,
|
||||
parts.user == nil, parts.password == nil, let url = parts.url else {
|
||||
throw StudioError("Enter an HTTP or HTTPS page URL.")
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
static func text(from address: String) async throws -> String {
|
||||
let session = URLSession(configuration: .ephemeral)
|
||||
defer { session.invalidateAndCancel() }
|
||||
var request = URLRequest(url: try url(address), timeoutInterval: 20)
|
||||
request.setValue("text/html, text/plain", forHTTPHeaderField: "Accept")
|
||||
let (bytes, response) = try await session.bytes(for: request)
|
||||
guard let response = response as? HTTPURLResponse, (200..<300).contains(response.statusCode),
|
||||
let type = response.mimeType, ["text/html", "text/plain", "application/xhtml+xml"].contains(type) else {
|
||||
throw StudioError("The server did not return a supported text page.")
|
||||
}
|
||||
var data = Data()
|
||||
for try await byte in bytes {
|
||||
try Task.checkCancellation()
|
||||
guard data.count < 1_048_576 else { throw StudioError("The page exceeds the 1 MB capture limit.") }
|
||||
data.append(byte)
|
||||
}
|
||||
guard let raw = String(data: data, encoding: .utf8) else { throw StudioError("The page is not UTF-8.") }
|
||||
let content = type == "text/plain" ? raw : plainText(raw)
|
||||
guard !content.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
|
||||
throw StudioError("The page contains no readable text.")
|
||||
}
|
||||
return String(content.prefix(12_000))
|
||||
}
|
||||
|
||||
static func plainText(_ html: String) -> String {
|
||||
html.replacingOccurrences(of: "(?is)<(script|style|head|noscript)\\b[^>]*>.*?</\\1\\s*>", with: " ",
|
||||
options: .regularExpression)
|
||||
.replacingOccurrences(of: "(?s)<!--.*?-->", with: " ", options: .regularExpression)
|
||||
.replacingOccurrences(of: "(?i)</(?:p|div|li|section|article|h[1-6])\\s*>|<br\\s*/?>", with: "\n",
|
||||
options: .regularExpression)
|
||||
.replacingOccurrences(of: "<[^>]+>", with: " ", options: .regularExpression)
|
||||
.replacingOccurrences(of: " ", with: " ")
|
||||
.replacingOccurrences(of: "<", with: "<")
|
||||
.replacingOccurrences(of: ">", with: ">")
|
||||
.replacingOccurrences(of: """, with: "\"")
|
||||
.replacingOccurrences(of: "&", with: "&")
|
||||
.components(separatedBy: .newlines)
|
||||
.map { $0.split(whereSeparator: \.isWhitespace).joined(separator: " ") }
|
||||
.filter { !$0.isEmpty }.joined(separator: "\n")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user