Skip to content

Commit 751a70a

Browse files
committed
feat(a2a): enhance A2A service initialization and error handling #443
1 parent fd331fd commit 751a70a

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

core/src/main/kotlin/cc/unitmesh/devti/a2a/A2AClientConsumer.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.unitmesh.devti.a2a
22

3+
import com.intellij.openapi.diagnostic.logger
34
import io.a2a.A2A
45
import io.a2a.client.*
56
import io.a2a.client.config.ClientConfig
@@ -63,7 +64,8 @@ class A2AClientConsumer {
6364
}
6465

6566
fun sendMessage(agentName: String, msgText: String): String {
66-
val client = clientMap[agentName] ?: throw IllegalArgumentException("No client found for $agentName")
67+
val client = clientMap[agentName]
68+
?: return "Failed to find A2A client for $agentName. Available clients: ${clientMap.keys}"
6769

6870
return try {
6971
val message = A2A.toUserMessage(msgText)
@@ -76,7 +78,8 @@ class A2AClientConsumer {
7678
responseFuture.get(30, java.util.concurrent.TimeUnit.SECONDS)
7779
} catch (e: Exception) {
7880
responseMap.remove(agentName)
79-
throw RuntimeException("Failed to send message to agent $agentName: ${e.message}", e)
81+
logger<A2AClientConsumer>().error("Failed to send message to $agentName: ${e.message}", e)
82+
return e.message ?: "Unknown error"
8083
}
8184
}
8285

core/src/main/kotlin/cc/unitmesh/devti/a2a/A2AService.kt

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cc.unitmesh.devti.a2a
22

3+
import cc.unitmesh.devti.mcp.client.McpServer
4+
import cc.unitmesh.devti.settings.customize.customizeSetting
35
import com.intellij.openapi.components.Service
46
import com.intellij.openapi.project.Project
57
import io.a2a.spec.AgentCard
@@ -11,17 +13,46 @@ import io.a2a.spec.AgentCard
1113
class A2AService(private val project: Project) {
1214
private var a2aClientConsumer: A2AClientConsumer? = null
1315
private var availableAgents: List<AgentCard> = emptyList()
14-
16+
private val cached = mutableMapOf<String, List<AgentCard>>()
17+
1518
companion object {
1619
fun getInstance(project: Project): A2AService {
1720
return project.getService(A2AService::class.java)
1821
}
1922
}
20-
23+
24+
/**
25+
* Initialize A2A service by reading configuration from project settings
26+
*/
27+
fun initialize() {
28+
val mcpServerConfig = project.customizeSetting.mcpServerConfig
29+
if (mcpServerConfig.isEmpty()) {
30+
a2aClientConsumer = null
31+
availableAgents = emptyList()
32+
return
33+
}
34+
35+
if (cached.containsKey(mcpServerConfig)) {
36+
availableAgents = cached[mcpServerConfig] ?: emptyList()
37+
return
38+
}
39+
40+
val mcpConfig = McpServer.load(mcpServerConfig)
41+
if (mcpConfig?.a2aServers.isNullOrEmpty()) {
42+
a2aClientConsumer = null
43+
availableAgents = emptyList()
44+
return
45+
}
46+
47+
val servers = mcpConfig.a2aServers.values.toList()
48+
initialize(servers)
49+
cached[mcpServerConfig] = availableAgents
50+
}
51+
2152
/**
2253
* Initialize A2A service with server configurations
2354
*/
24-
fun initialize(servers: List<A2aServer>) {
55+
private fun initialize(servers: List<A2aServer>) {
2556
try {
2657
a2aClientConsumer = A2AClientConsumer()
2758
a2aClientConsumer?.init(servers)
@@ -57,7 +88,20 @@ class A2AService(private val project: Project) {
5788
fun isAvailable(): Boolean {
5889
return a2aClientConsumer != null && availableAgents.isNotEmpty()
5990
}
60-
91+
92+
/**
93+
* Get enabled A2A servers from configuration content
94+
*/
95+
fun getEnabledServers(content: String): Map<String, A2aServer>? {
96+
val mcpConfig = McpServer.load(content)
97+
return mcpConfig?.a2aServers?.filter { entry ->
98+
// A2A servers don't have a disabled flag like MCP servers, so all are enabled
99+
true
100+
}?.mapValues { entry ->
101+
entry.value
102+
}
103+
}
104+
61105
/**
62106
* Refresh the list of available agents
63107
*/

core/src/main/kotlin/cc/unitmesh/devti/a2a/A2ASketchToolchainProvider.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class A2ASketchToolchainProvider : SketchToolchainProvider {
2020
fun collectA2ATools(project: Project): List<AgentTool> {
2121
return try {
2222
val a2aService = project.getService(A2AService::class.java)
23+
24+
// Initialize A2A service from configuration
25+
a2aService.initialize()
26+
2327
val agentCards = a2aService.getAvailableAgents()
2428

2529
agentCards.map { agentCard ->

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/exec/A2AInsCommand.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class A2AInsCommand(
3939

4040
override suspend fun execute(): String? {
4141
val a2aService = project.getService(A2AService::class.java)
42+
a2aService.initialize()
4243

4344
if (!a2aService.isAvailable()) {
4445
return "A2A service is not available. Please check your A2A configuration."

0 commit comments

Comments
 (0)