|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { createServer } from "node:http"; |
| 4 | + |
| 5 | +import { WebSocketServer } from "ws"; |
| 6 | + |
| 7 | +import * as acp from "@agentclientprotocol/sdk"; |
| 8 | +import { createNodeHttpHandler } from "@agentclientprotocol/sdk/node"; |
| 9 | +import { AcpServer } from "@agentclientprotocol/sdk/server"; |
| 10 | + |
| 11 | +class HttpExampleAgent implements acp.Agent { |
| 12 | + private readonly connection: acp.AgentSideConnection; |
| 13 | + private readonly sessions = new Set<string>(); |
| 14 | + |
| 15 | + constructor(connection: acp.AgentSideConnection) { |
| 16 | + this.connection = connection; |
| 17 | + } |
| 18 | + |
| 19 | + async initialize( |
| 20 | + _params: acp.InitializeRequest, |
| 21 | + ): Promise<acp.InitializeResponse> { |
| 22 | + return { |
| 23 | + protocolVersion: acp.PROTOCOL_VERSION, |
| 24 | + agentCapabilities: { |
| 25 | + loadSession: false, |
| 26 | + }, |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + async newSession( |
| 31 | + _params: acp.NewSessionRequest, |
| 32 | + ): Promise<acp.NewSessionResponse> { |
| 33 | + const sessionId = crypto.randomUUID(); |
| 34 | + this.sessions.add(sessionId); |
| 35 | + return { sessionId }; |
| 36 | + } |
| 37 | + |
| 38 | + async authenticate( |
| 39 | + _params: acp.AuthenticateRequest, |
| 40 | + ): Promise<acp.AuthenticateResponse> { |
| 41 | + return {}; |
| 42 | + } |
| 43 | + |
| 44 | + async prompt(params: acp.PromptRequest): Promise<acp.PromptResponse> { |
| 45 | + if (!this.sessions.has(params.sessionId)) { |
| 46 | + throw new Error(`Session ${params.sessionId} not found`); |
| 47 | + } |
| 48 | + |
| 49 | + await this.connection.sessionUpdate({ |
| 50 | + sessionId: params.sessionId, |
| 51 | + update: { |
| 52 | + sessionUpdate: "agent_message_chunk", |
| 53 | + content: { |
| 54 | + type: "text", |
| 55 | + text: "Hello from the ACP HTTP/WebSocket example server.", |
| 56 | + }, |
| 57 | + }, |
| 58 | + }); |
| 59 | + |
| 60 | + return { stopReason: "end_turn" }; |
| 61 | + } |
| 62 | + |
| 63 | + async cancel(_params: acp.CancelNotification): Promise<void> {} |
| 64 | +} |
| 65 | + |
| 66 | +const acpServer = new AcpServer({ |
| 67 | + createAgent: (connection) => new HttpExampleAgent(connection), |
| 68 | +}); |
| 69 | +const acpHttpHandler = createNodeHttpHandler(acpServer); |
| 70 | +const webSocketServer = new WebSocketServer({ noServer: true }); |
| 71 | +const port = Number.parseInt(process.env.PORT ?? "7331", 10); |
| 72 | + |
| 73 | +const httpServer = createServer((req, res) => { |
| 74 | + if (!isAcpPath(req.url)) { |
| 75 | + res.writeHead(404, { "Content-Type": "text/plain" }); |
| 76 | + res.end("Not Found"); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // Put authentication or tenant-selection middleware here before routing to AcpServer. |
| 81 | + // For example, validate `req.headers.authorization` and reject unauthorized requests. |
| 82 | + if (!isAuthorized(req.headers.authorization)) { |
| 83 | + res.writeHead(401, { "Content-Type": "text/plain" }); |
| 84 | + res.end("Unauthorized"); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + acpHttpHandler(req, res); |
| 89 | +}); |
| 90 | + |
| 91 | +httpServer.on("upgrade", (req, socket, head) => { |
| 92 | + if (!isAcpPath(req.url) || !isAuthorized(req.headers.authorization)) { |
| 93 | + socket.destroy(); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + webSocketServer.handleUpgrade(req, socket, head, (ws) => { |
| 98 | + acpServer.handleWebSocket(ws); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +httpServer.listen(port, () => { |
| 103 | + console.log(`ACP HTTP endpoint listening at http://127.0.0.1:${port}/acp`); |
| 104 | + console.log(`ACP WebSocket endpoint listening at ws://127.0.0.1:${port}/acp`); |
| 105 | +}); |
| 106 | + |
| 107 | +function isAcpPath(url: string | undefined): boolean { |
| 108 | + return new URL(url ?? "/", "http://127.0.0.1").pathname === "/acp"; |
| 109 | +} |
| 110 | + |
| 111 | +function isAuthorized(authorization: string | undefined): boolean { |
| 112 | + return ( |
| 113 | + authorization === undefined || authorization === "Bearer example-token" |
| 114 | + ); |
| 115 | +} |
0 commit comments