Use connected knowledge and Gitea from chat

This commit is contained in:
Mark Gregory
2026-11-16 16:03:22 +00:00
parent f7b1083f39
commit 00ea78671b
7 changed files with 187 additions and 66 deletions
+11
View File
@@ -40,6 +40,17 @@ class Handler(BaseHTTPRequestHandler):
payload = ''.join(f'data: {json.dumps(frame)}\n\n' for frame in frames)
self.reply(200, (payload + 'data: [DONE]\n\n').encode(), 'text/event-stream')
return
if self.path == '/chat-tools/api/chat':
system, user = message['messages']
valid = (system['role'] == 'system' and 'fixture/visible-repo' in system['content']
and 'source: knowledge.md' in system['content']
and user == {'role': 'user', 'content': 'can you see my git repos'})
if not valid:
self.reply(400)
return
self.reply(200, b'{"message":{"content":"Repository and knowledge received."},"done":true}\n',
'application/x-ndjson')
return
if self.path == '/gateway/api/chat':
if self.headers.get('Authorization') != 'Bearer fixture-token':
self.reply(401)
+58
View File
@@ -741,3 +741,61 @@ final class MultipleToolsetsTests: XCTestCase {
XCTAssertTrue(condition())
}
}
final class ConnectedChatTests: XCTestCase {
struct Server: MCPToolClient {
let name: String
let result: Data
func listTools() async throws -> [MCPTool] {
[MCPTool(name: name, description: "", inputSchemaJSON: Data("{}".utf8))]
}
func callTool(_ name: String, argumentsJSON: Data) async throws -> Data {
guard name == self.name else { throw StudioError("Unexpected tool.") }
return result
}
}
@MainActor
func testChatSendsConnectedKnowledgeAndGiteaWithoutExtraSelection() async throws {
guard let address = ProcessInfo.processInfo.environment["AISTUDIO_TEST_WEB_URL"],
let base = URL(string: address)?.deletingLastPathComponent() else {
throw XCTSkip("Run Scripts/test.sh to configure the fixture.")
}
let directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString)
defer { try? FileManager.default.removeItem(at: directory) }
let studio = StudioModel()
studio.open(databaseURL: directory.appending(path: "studio.sqlite"))
studio.connectMCP(.profile("knowledge"), name: "Knowledge") {
Server(name: "search_knowledge", result: Data(#"{"content":[{"text":"source: knowledge.md"}]}"#.utf8))
}
studio.connectMCP(.profile("gitea"), name: "Gitea") {
Server(name: "list_repositories", result: Data(#"{"structuredContent":{"result":[{"full_name":"fixture/visible-repo"}]}}"#.utf8))
}
let deadline = ContinuousClock.now + .seconds(8)
while studio.activeToolsets.count < 2 && ContinuousClock.now < deadline {
try await Task.sleep(for: .milliseconds(10))
}
XCTAssertEqual(studio.activeToolsets.count, 2)
studio.prompt = "can you see my git repos"
studio.send(endpoint: base.appending(path: "chat-tools").absoluteString, model: "fixture", mlxPath: "",
thinking: .off, failover: false, mlxOnly: false, compatible: false, recall: false,
useDocuments: false, instructions: "", settings: GenerationSettings())
while studio.running && ContinuousClock.now < deadline {
try await Task.sleep(for: .milliseconds(10))
}
XCTAssertEqual(studio.messages.last?.content, "Repository and knowledge received.")
XCTAssertEqual(studio.webStatus, "Gitea repositories checked.")
}
func testLiveConnectedGiteaRepositoryLookup() async throws {
let env = ProcessInfo.processInfo.environment
guard let path = env["AISTUDIO_TEST_REAL_MCP_CONFIG"], env["AISTUDIO_TEST_REAL_GITEA"] == "1" else {
throw XCTSkip("Enable the installed Gitea smoke test explicitly.")
}
let file = try MCPServerConfiguration.File.read(URL(fileURLWithPath: path))
let client = try XCTUnwrap(file.mcpServers["absdev-gitea"]).makeClient()
_ = try await client.listTools()
let result = try await GiteaRepositoryLookup.references(client: client)
XCTAssertFalse(result.contains("Gitea returned no repository entries."))
}
}