Skip to content

Commit 62e30a1

Browse files
committed
feat(custom-agent): add basic support for custom agent execution #51
Implement a new service for executing custom agents using OkHttpClient and add a new Python server example for mocking market agent responses. Also, add a Kotlin test case to verify the execution service.
1 parent 65e9e5e commit 62e30a1

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

example/custom_agent/requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fastapi==0.110.0
2+
uvicorn~=0.27.1
3+
pydantic~=2.6.3
4+
starlette~=0.36.3

example/custom_agent/server.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import List
2+
3+
import uvicorn
4+
from fastapi import FastAPI
5+
from fastapi.exceptions import RequestValidationError
6+
from pydantic import BaseModel
7+
from starlette import status
8+
from starlette.requests import Request
9+
from starlette.responses import JSONResponse
10+
11+
app = FastAPI()
12+
13+
14+
class Message(BaseModel):
15+
role: str
16+
message: str
17+
18+
19+
class Messages(BaseModel):
20+
messages: List[Message]
21+
22+
@app.post("/api/agent/market")
23+
def mock_market(messages: Messages):
24+
return "I am a mock market agent for intention: " + messages.messages[0].message
25+
26+
27+
@app.exception_handler(RequestValidationError)
28+
async def validation_exception_handler(request: Request, exc: RequestValidationError):
29+
exc_str = f'{exc}'.replace('\n', ' ').replace(' ', ' ')
30+
print(f"{request}: {exc_str}")
31+
content = {'status_code': 10422, 'message': exc_str, 'data': None}
32+
return JSONResponse(content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
33+
34+
35+
if __name__ == "__main__":
36+
uvicorn.run(app, port=8765)

src/main/kotlin/cc/unitmesh/devti/counit/CustomAgentExecutor.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import cc.unitmesh.devti.counit.model.CustomAgentConfig
66
import cc.unitmesh.devti.llms.custom.CustomRequest
77
import cc.unitmesh.devti.llms.custom.Message
88
import com.intellij.openapi.components.Service
9+
import com.intellij.openapi.diagnostic.logger
910
import com.intellij.openapi.project.Project
1011
import kotlinx.serialization.encodeToString
1112
import kotlinx.serialization.json.Json
@@ -17,6 +18,7 @@ import okhttp3.RequestBody.Companion.toRequestBody
1718
@Service(Service.Level.PROJECT)
1819
class CustomAgentExecutor(val project: Project) {
1920
private var client = OkHttpClient()
21+
private val logger = logger<CustomAgentExecutor>()
2022

2123
fun execute(input: String, agent: CustomAgentConfig): String? {
2224
val customRequest = CustomRequest(listOf(Message("user", input)))
@@ -32,7 +34,9 @@ class CustomAgentExecutor(val project: Project) {
3234
builder.addHeader("Content-Type", "application/json")
3335
}
3436

35-
null -> TODO()
37+
null -> {
38+
logger.info("No auth type found for agent ${agent.name}")
39+
}
3640
}
3741

3842
client = client.newBuilder().build()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cc.unitmesh.devti.counit
2+
3+
import cc.unitmesh.devti.counit.model.CustomAgentConfig
4+
import com.intellij.testFramework.LightPlatformTestCase
5+
6+
7+
class CustomAgentExecutorTest : LightPlatformTestCase() {
8+
fun testExecute() {
9+
val customAgentExecutor = CustomAgentExecutor(project)
10+
val response = customAgentExecutor.execute(
11+
"test",
12+
CustomAgentConfig("test", "test", "http://127.0.0.1:8765/api/agent/market")
13+
)
14+
15+
// assertEquals("test", response)
16+
}
17+
}

0 commit comments

Comments
 (0)