-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathserver-utils.ts
More file actions
70 lines (61 loc) · 2.07 KB
/
server-utils.ts
File metadata and controls
70 lines (61 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import {
A2AExpressApp,
DefaultRequestHandler,
InMemoryTaskStore,
} from "@a2a-js/sdk"
import { colors, createLogger, type Logger } from "@repo/cli-tools"
import express from "express"
import type { Agent } from "../agent"
type Options = {
logger?: Logger
host?: string
port?: number
}
/**
* Simple utility to start an A2A server with DID document hosting
*/
export function startAgentServer(
agent: Agent,
{
logger = createLogger("server"),
host = "0.0.0.0",
port = 3001,
}: Options = {},
) {
logger.log(`🏦 Starting ${agent.constructor.name} on port ${port}...`)
logger.log(`🆔 Bank Teller DID: ${colors.dim(agent.did)}`)
// Debug: Check if DID document exists
const didDocument = agent.didDocument
logger.log("✅ DID document created successfully")
logger.log(" DID Document ID:", colors.dim(didDocument.id))
// Create A2A server with the original AgentCard
// The DID is now the top-level identifier, referenced in the DID document services
const requestHandler = new DefaultRequestHandler(
agent.agentCard,
new InMemoryTaskStore(),
agent,
)
const appBuilder = new A2AExpressApp(requestHandler)
// Get the Express app and store it to ensure we use the same instance
const app = appBuilder.setupRoutes(express(), "")
// Add DID document endpoint for did:web resolution
app.get("/.well-known/did.json", (req, res) => {
logger.log("🔍 Request for DID document:", colors.dim(req.url))
const didDocument = agent.didDocument
logger.log("🌐 Serving DID document for did:web resolution")
res.json(didDocument)
})
logger.log(
"🌐 DID document endpoint added at:",
colors.dim("/.well-known/did.json"),
)
// Start the server using the same app instance
const expressServer = app.listen(port, host, () => {
logger.log(`A2A Server running at ${colors.dim(`http://${host}:${port}`)}`)
logger.log(
`🌐 DID document available at: ${colors.dim(`http://localhost:${port}/.well-known/did.json`)}`,
)
})
// Return server instance for programmatic control
return expressServer
}