Extract agent-core as shared package with agentic loop, tool definitions, and MCP integration. Compass tools wrapped as MCP server using low-level Server API. Client manager connects multiple MCP servers (in-memory, stdio, HTTP) with unified tool routing. External MCP server configs stored in DB with CRUD actions. Both Workers and Bun runtimes use the new MCP client manager.
35 lines
932 B
TypeScript
35 lines
932 B
TypeScript
import type { SSEData } from "./types"
|
|
|
|
export function sseEncode(data: SSEData): string {
|
|
return `data: ${JSON.stringify(data)}\n\n`
|
|
}
|
|
|
|
export function createSSEStream(
|
|
generator: AsyncGenerator<SSEData>
|
|
): ReadableStream<Uint8Array> {
|
|
const encoder = new TextEncoder()
|
|
|
|
return new ReadableStream({
|
|
async start(controller) {
|
|
try {
|
|
for await (const event of generator) {
|
|
controller.enqueue(encoder.encode(sseEncode(event)))
|
|
}
|
|
controller.enqueue(encoder.encode("data: [DONE]\n\n"))
|
|
controller.close()
|
|
} catch (err) {
|
|
const errorEvent: SSEData = {
|
|
type: "error",
|
|
error:
|
|
err instanceof Error ? err.message : String(err),
|
|
}
|
|
controller.enqueue(
|
|
encoder.encode(sseEncode(errorEvent))
|
|
)
|
|
controller.enqueue(encoder.encode("data: [DONE]\n\n"))
|
|
controller.close()
|
|
}
|
|
},
|
|
})
|
|
}
|