acp-sdk 1.0.0rc2__tar.gz → 1.0.0rc4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- acp_sdk-1.0.0rc4/PKG-INFO +78 -0
- acp_sdk-1.0.0rc4/README.md +59 -0
- acp_sdk-1.0.0rc4/examples/servers/awaiting.py +23 -0
- acp_sdk-1.0.0rc4/examples/servers/echo.py +24 -0
- acp_sdk-1.0.0rc4/examples/servers/sync_echo.py +16 -0
- {acp_sdk-1.0.0rc2 → acp_sdk-1.0.0rc4}/pyproject.toml +3 -1
- acp_sdk-1.0.0rc4/src/acp_sdk/__init__.py +1 -0
- {acp_sdk-1.0.0rc2 → acp_sdk-1.0.0rc4}/src/acp_sdk/client/client.py +17 -1
- acp_sdk-1.0.0rc4/src/acp_sdk/models/__init__.py +3 -0
- acp_sdk-1.0.0rc4/src/acp_sdk/models/errors.py +23 -0
- {acp_sdk-1.0.0rc2/src/acp_sdk → acp_sdk-1.0.0rc4/src/acp_sdk/models}/models.py +6 -40
- acp_sdk-1.0.0rc4/src/acp_sdk/models/schemas.py +39 -0
- acp_sdk-1.0.0rc4/src/acp_sdk/server/__init__.py +8 -0
- acp_sdk-1.0.0rc4/src/acp_sdk/server/agent.py +74 -0
- acp_sdk-1.0.0rc2/src/acp_sdk/server/server.py → acp_sdk-1.0.0rc4/src/acp_sdk/server/app.py +34 -6
- {acp_sdk-1.0.0rc2 → acp_sdk-1.0.0rc4}/src/acp_sdk/server/bundle.py +9 -6
- acp_sdk-1.0.0rc4/src/acp_sdk/server/context.py +26 -0
- acp_sdk-1.0.0rc4/src/acp_sdk/server/errors.py +54 -0
- acp_sdk-1.0.0rc4/src/acp_sdk/server/logging.py +16 -0
- acp_sdk-1.0.0rc4/src/acp_sdk/server/server.py +110 -0
- acp_sdk-1.0.0rc4/src/acp_sdk/server/telemetry.py +52 -0
- acp_sdk-1.0.0rc4/src/acp_sdk/server/types.py +6 -0
- acp_sdk-1.0.0rc2/PKG-INFO +0 -53
- acp_sdk-1.0.0rc2/README.md +0 -35
- acp_sdk-1.0.0rc2/examples/servers/awaiting.py +0 -29
- acp_sdk-1.0.0rc2/examples/servers/echo.py +0 -29
- acp_sdk-1.0.0rc2/src/acp_sdk/__init__.py +0 -0
- acp_sdk-1.0.0rc2/src/acp_sdk/server/__init__.py +0 -2
- acp_sdk-1.0.0rc2/src/acp_sdk/server/agent.py +0 -30
- acp_sdk-1.0.0rc2/src/acp_sdk/server/context.py +0 -6
- acp_sdk-1.0.0rc2/src/acp_sdk/server/telemetry.py +0 -46
- {acp_sdk-1.0.0rc2 → acp_sdk-1.0.0rc4}/.gitignore +0 -0
- {acp_sdk-1.0.0rc2 → acp_sdk-1.0.0rc4}/.python-version +0 -0
- {acp_sdk-1.0.0rc2 → acp_sdk-1.0.0rc4}/examples/clients/advanced.py +0 -0
- {acp_sdk-1.0.0rc2 → acp_sdk-1.0.0rc4}/examples/clients/simple.py +0 -0
- {acp_sdk-1.0.0rc2 → acp_sdk-1.0.0rc4}/examples/clients/stream.py +0 -0
- {acp_sdk-1.0.0rc2 → acp_sdk-1.0.0rc4}/src/acp_sdk/client/__init__.py +0 -0
- {acp_sdk-1.0.0rc2 → acp_sdk-1.0.0rc4}/src/acp_sdk/py.typed +0 -0
- {acp_sdk-1.0.0rc2 → acp_sdk-1.0.0rc4}/src/acp_sdk/server/utils.py +0 -0
@@ -0,0 +1,78 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: acp-sdk
|
3
|
+
Version: 1.0.0rc4
|
4
|
+
Summary: Agent Communication Protocol SDK
|
5
|
+
Requires-Python: <4.0,>=3.11
|
6
|
+
Requires-Dist: opentelemetry-api>=1.31.1
|
7
|
+
Requires-Dist: pydantic>=2.11.1
|
8
|
+
Provides-Extra: client
|
9
|
+
Requires-Dist: httpx-sse>=0.4.0; extra == 'client'
|
10
|
+
Requires-Dist: httpx>=0.28.1; extra == 'client'
|
11
|
+
Requires-Dist: opentelemetry-instrumentation-httpx>=0.52b1; extra == 'client'
|
12
|
+
Provides-Extra: server
|
13
|
+
Requires-Dist: fastapi[standard]>=0.115.8; extra == 'server'
|
14
|
+
Requires-Dist: janus>=2.0.0; extra == 'server'
|
15
|
+
Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.31.1; extra == 'server'
|
16
|
+
Requires-Dist: opentelemetry-instrumentation-fastapi>=0.52b1; extra == 'server'
|
17
|
+
Requires-Dist: opentelemetry-sdk>=1.31.1; extra == 'server'
|
18
|
+
Description-Content-Type: text/markdown
|
19
|
+
|
20
|
+
# Agent Communication Protocol SDK for Python
|
21
|
+
|
22
|
+
Agent Communication Protocol SDK for Python provides allows developers to serve and consume agents over the Agent Communication Protocol.
|
23
|
+
|
24
|
+
## Prerequisites
|
25
|
+
|
26
|
+
✅ Python >= 3.11
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
|
30
|
+
Install to use client:
|
31
|
+
|
32
|
+
```shell
|
33
|
+
pip install acp-sdk[client]
|
34
|
+
```
|
35
|
+
|
36
|
+
Install to use server:
|
37
|
+
|
38
|
+
```shell
|
39
|
+
pip install acp-sdk[server]
|
40
|
+
```
|
41
|
+
|
42
|
+
## Overview
|
43
|
+
|
44
|
+
### Client
|
45
|
+
|
46
|
+
The `client` submodule exposes [httpx]() based client with simple methods for communication over ACP.
|
47
|
+
|
48
|
+
```python
|
49
|
+
async with Client(base_url="http://localhost:8000") as client:
|
50
|
+
run = await client.run_sync(agent="echo", input=Message(TextMessagePart(content="Howdy!")))
|
51
|
+
print(run.output)
|
52
|
+
```
|
53
|
+
|
54
|
+
### Server
|
55
|
+
|
56
|
+
The `server` submodule exposes [fastapi] application factory that makes it easy to expose any agent over ACP.
|
57
|
+
|
58
|
+
```python
|
59
|
+
class EchoAgent(Agent):
|
60
|
+
@property
|
61
|
+
def name(self) -> str:
|
62
|
+
return "echo"
|
63
|
+
|
64
|
+
@property
|
65
|
+
def description(self) -> str:
|
66
|
+
return "Echoes everything"
|
67
|
+
|
68
|
+
async def run(self, input: Message, *, context: Context) -> AsyncGenerator[Message | Await, AwaitResume]:
|
69
|
+
for part in input:
|
70
|
+
await asyncio.sleep(0.5)
|
71
|
+
yield {"thought": "I should echo everyting"}
|
72
|
+
yield Message(part)
|
73
|
+
|
74
|
+
|
75
|
+
serve(EchoAgent())
|
76
|
+
```
|
77
|
+
|
78
|
+
➡️ Explore more in our [examples library](/python/examples).
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# Agent Communication Protocol SDK for Python
|
2
|
+
|
3
|
+
Agent Communication Protocol SDK for Python provides allows developers to serve and consume agents over the Agent Communication Protocol.
|
4
|
+
|
5
|
+
## Prerequisites
|
6
|
+
|
7
|
+
✅ Python >= 3.11
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Install to use client:
|
12
|
+
|
13
|
+
```shell
|
14
|
+
pip install acp-sdk[client]
|
15
|
+
```
|
16
|
+
|
17
|
+
Install to use server:
|
18
|
+
|
19
|
+
```shell
|
20
|
+
pip install acp-sdk[server]
|
21
|
+
```
|
22
|
+
|
23
|
+
## Overview
|
24
|
+
|
25
|
+
### Client
|
26
|
+
|
27
|
+
The `client` submodule exposes [httpx]() based client with simple methods for communication over ACP.
|
28
|
+
|
29
|
+
```python
|
30
|
+
async with Client(base_url="http://localhost:8000") as client:
|
31
|
+
run = await client.run_sync(agent="echo", input=Message(TextMessagePart(content="Howdy!")))
|
32
|
+
print(run.output)
|
33
|
+
```
|
34
|
+
|
35
|
+
### Server
|
36
|
+
|
37
|
+
The `server` submodule exposes [fastapi] application factory that makes it easy to expose any agent over ACP.
|
38
|
+
|
39
|
+
```python
|
40
|
+
class EchoAgent(Agent):
|
41
|
+
@property
|
42
|
+
def name(self) -> str:
|
43
|
+
return "echo"
|
44
|
+
|
45
|
+
@property
|
46
|
+
def description(self) -> str:
|
47
|
+
return "Echoes everything"
|
48
|
+
|
49
|
+
async def run(self, input: Message, *, context: Context) -> AsyncGenerator[Message | Await, AwaitResume]:
|
50
|
+
for part in input:
|
51
|
+
await asyncio.sleep(0.5)
|
52
|
+
yield {"thought": "I should echo everyting"}
|
53
|
+
yield Message(part)
|
54
|
+
|
55
|
+
|
56
|
+
serve(EchoAgent())
|
57
|
+
```
|
58
|
+
|
59
|
+
➡️ Explore more in our [examples library](/python/examples).
|
@@ -0,0 +1,23 @@
|
|
1
|
+
from collections.abc import AsyncGenerator
|
2
|
+
from typing import Any
|
3
|
+
|
4
|
+
from acp_sdk.models import (
|
5
|
+
Await,
|
6
|
+
AwaitResume,
|
7
|
+
Message,
|
8
|
+
TextMessagePart,
|
9
|
+
)
|
10
|
+
from acp_sdk.server import Context, Server
|
11
|
+
|
12
|
+
server = Server()
|
13
|
+
|
14
|
+
|
15
|
+
@server.agent()
|
16
|
+
async def awaiting(input: Message, context: Context) -> AsyncGenerator[Message | Await | Any, AwaitResume]:
|
17
|
+
"""Greets and awaits for more data"""
|
18
|
+
yield Message(TextMessagePart(content="Hello!"))
|
19
|
+
data = yield Await()
|
20
|
+
yield Message(TextMessagePart(content=f"Thanks for {data}"))
|
21
|
+
|
22
|
+
|
23
|
+
server.run()
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import asyncio
|
2
|
+
from collections.abc import AsyncGenerator
|
3
|
+
from typing import Any
|
4
|
+
|
5
|
+
from acp_sdk.models import (
|
6
|
+
Await,
|
7
|
+
AwaitResume,
|
8
|
+
Message,
|
9
|
+
)
|
10
|
+
from acp_sdk.server import Context, Server
|
11
|
+
|
12
|
+
server = Server()
|
13
|
+
|
14
|
+
|
15
|
+
@server.agent()
|
16
|
+
async def echo(input: Message, context: Context) -> AsyncGenerator[Message | Await | Any, AwaitResume]:
|
17
|
+
"""Echoes everything"""
|
18
|
+
for part in input:
|
19
|
+
await asyncio.sleep(0.5)
|
20
|
+
yield {"thought": "I should echo everyting"}
|
21
|
+
yield Message(part)
|
22
|
+
|
23
|
+
|
24
|
+
server.run()
|
@@ -0,0 +1,16 @@
|
|
1
|
+
from acp_sdk.models import (
|
2
|
+
Message,
|
3
|
+
)
|
4
|
+
from acp_sdk.server import RunYield, Server, SyncContext
|
5
|
+
|
6
|
+
server = Server()
|
7
|
+
|
8
|
+
|
9
|
+
@server.agent()
|
10
|
+
def echo(input: Message, context: SyncContext) -> RunYield:
|
11
|
+
"""Echoes everything"""
|
12
|
+
context.yield_({"thought": "I should echo everyting"})
|
13
|
+
return input
|
14
|
+
|
15
|
+
|
16
|
+
server.run()
|
@@ -1,6 +1,6 @@
|
|
1
1
|
[project]
|
2
2
|
name = "acp-sdk"
|
3
|
-
version = "1.0.
|
3
|
+
version = "1.0.0rc4"
|
4
4
|
description = "Agent Communication Protocol SDK"
|
5
5
|
readme = "README.md"
|
6
6
|
authors = []
|
@@ -18,6 +18,7 @@ server = [
|
|
18
18
|
"opentelemetry-exporter-otlp-proto-http>=1.31.1",
|
19
19
|
"opentelemetry-instrumentation-fastapi>=0.52b1",
|
20
20
|
"opentelemetry-sdk>=1.31.1",
|
21
|
+
"janus>=2.0.0",
|
21
22
|
]
|
22
23
|
|
23
24
|
[tool.uv]
|
@@ -29,6 +30,7 @@ dev-dependencies = [
|
|
29
30
|
"opentelemetry-exporter-otlp-proto-http>=1.31.1",
|
30
31
|
"opentelemetry-instrumentation-fastapi>=0.52b1",
|
31
32
|
"opentelemetry-sdk>=1.31.1",
|
33
|
+
"janus>=2.0.0",
|
32
34
|
]
|
33
35
|
|
34
36
|
[build-system]
|
@@ -0,0 +1 @@
|
|
1
|
+
from acp_sdk.models import * # noqa: F403
|
@@ -8,11 +8,13 @@ from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
|
|
8
8
|
from pydantic import TypeAdapter
|
9
9
|
|
10
10
|
from acp_sdk.models import (
|
11
|
+
ACPError,
|
11
12
|
Agent,
|
12
13
|
AgentName,
|
13
14
|
AgentReadResponse,
|
14
15
|
AgentsListResponse,
|
15
16
|
AwaitResume,
|
17
|
+
Error,
|
16
18
|
Message,
|
17
19
|
Run,
|
18
20
|
RunCancelResponse,
|
@@ -32,7 +34,7 @@ class Client:
|
|
32
34
|
|
33
35
|
self._client = self._init_client(client)
|
34
36
|
|
35
|
-
def _init_client(self, client: httpx.AsyncClient | None = None) ->
|
37
|
+
def _init_client(self, client: httpx.AsyncClient | None = None) -> httpx.AsyncClient:
|
36
38
|
client = client or httpx.AsyncClient(base_url=self.base_url)
|
37
39
|
HTTPXClientInstrumentor.instrument_client(client)
|
38
40
|
return client
|
@@ -51,11 +53,13 @@ class Client:
|
|
51
53
|
|
52
54
|
async def agents(self) -> AsyncIterator[Agent]:
|
53
55
|
response = await self._client.get("/agents")
|
56
|
+
self._raise_error(response)
|
54
57
|
for agent in AgentsListResponse.model_validate(response.json()).agents:
|
55
58
|
yield agent
|
56
59
|
|
57
60
|
async def agent(self, *, name: AgentName) -> Agent:
|
58
61
|
response = await self._client.get(f"/agents/{name}")
|
62
|
+
self._raise_error(response)
|
59
63
|
return AgentReadResponse.model_validate(response.json())
|
60
64
|
|
61
65
|
async def run_sync(self, *, agent: AgentName, input: Message) -> Run:
|
@@ -63,6 +67,7 @@ class Client:
|
|
63
67
|
"/runs",
|
64
68
|
json=RunCreateRequest(agent_name=agent, input=input, mode=RunMode.SYNC).model_dump(),
|
65
69
|
)
|
70
|
+
self._raise_error(response)
|
66
71
|
return RunCreateResponse.model_validate(response.json())
|
67
72
|
|
68
73
|
async def run_async(self, *, agent: AgentName, input: Message) -> Run:
|
@@ -70,6 +75,7 @@ class Client:
|
|
70
75
|
"/runs",
|
71
76
|
json=RunCreateRequest(agent_name=agent, input=input, mode=RunMode.ASYNC).model_dump(),
|
72
77
|
)
|
78
|
+
self._raise_error(response)
|
73
79
|
return RunCreateResponse.model_validate(response.json())
|
74
80
|
|
75
81
|
async def run_stream(self, *, agent: AgentName, input: Message) -> AsyncIterator[RunEvent]:
|
@@ -84,10 +90,12 @@ class Client:
|
|
84
90
|
|
85
91
|
async def run_status(self, *, run_id: RunId) -> Run:
|
86
92
|
response = await self._client.get(f"/runs/{run_id}")
|
93
|
+
self._raise_error(response)
|
87
94
|
return Run.model_validate(response.json())
|
88
95
|
|
89
96
|
async def run_cancel(self, *, run_id: RunId) -> Run:
|
90
97
|
response = await self._client.post(f"/runs/{run_id}/cancel")
|
98
|
+
self._raise_error(response)
|
91
99
|
return RunCancelResponse.model_validate(response.json())
|
92
100
|
|
93
101
|
async def run_resume_sync(self, *, run_id: RunId, await_: AwaitResume) -> Run:
|
@@ -95,6 +103,7 @@ class Client:
|
|
95
103
|
f"/runs/{run_id}",
|
96
104
|
json=RunResumeRequest(await_=await_, mode=RunMode.SYNC).model_dump(),
|
97
105
|
)
|
106
|
+
self._raise_error(response)
|
98
107
|
return RunResumeResponse.model_validate(response.json())
|
99
108
|
|
100
109
|
async def run_resume_async(self, *, run_id: RunId, await_: AwaitResume) -> Run:
|
@@ -102,6 +111,7 @@ class Client:
|
|
102
111
|
f"/runs/{run_id}",
|
103
112
|
json=RunResumeRequest(await_=await_, mode=RunMode.ASYNC).model_dump(),
|
104
113
|
)
|
114
|
+
self._raise_error(response)
|
105
115
|
return RunResumeResponse.model_validate(response.json())
|
106
116
|
|
107
117
|
async def run_resume_stream(self, *, run_id: RunId, await_: AwaitResume) -> AsyncIterator[RunEvent]:
|
@@ -121,3 +131,9 @@ class Client:
|
|
121
131
|
async for event in event_source.aiter_sse():
|
122
132
|
event = TypeAdapter(RunEvent).validate_json(event.data)
|
123
133
|
yield event
|
134
|
+
|
135
|
+
def _raise_error(self, response: httpx.Response) -> None:
|
136
|
+
try:
|
137
|
+
response.raise_for_status()
|
138
|
+
except httpx.HTTPError:
|
139
|
+
raise ACPError(Error.model_validate(response.json()))
|
@@ -0,0 +1,23 @@
|
|
1
|
+
from enum import Enum
|
2
|
+
|
3
|
+
from pydantic import BaseModel
|
4
|
+
|
5
|
+
|
6
|
+
class ErrorCode(str, Enum):
|
7
|
+
SERVER_ERROR = "server_error"
|
8
|
+
INVALID_INPUT = "invalid_input"
|
9
|
+
NOT_FOUND = "not_found"
|
10
|
+
|
11
|
+
|
12
|
+
class Error(BaseModel):
|
13
|
+
code: ErrorCode
|
14
|
+
message: str
|
15
|
+
|
16
|
+
|
17
|
+
class ACPError(Exception):
|
18
|
+
def __init__(self, error: Error) -> None:
|
19
|
+
super().__init__()
|
20
|
+
self.error = error
|
21
|
+
|
22
|
+
def __str__(self) -> str:
|
23
|
+
return str(self.error.message)
|
@@ -5,10 +5,11 @@ from typing import Any, Literal, Union
|
|
5
5
|
|
6
6
|
from pydantic import AnyUrl, BaseModel, ConfigDict, Field, RootModel
|
7
7
|
|
8
|
+
from acp_sdk.models.errors import Error
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
|
11
|
+
class Metadata(BaseModel):
|
12
|
+
model_config = ConfigDict(extra="allow")
|
12
13
|
|
13
14
|
|
14
15
|
class AnyModel(BaseModel):
|
@@ -97,7 +98,7 @@ class Run(BaseModel):
|
|
97
98
|
status: RunStatus = RunStatus.CREATED
|
98
99
|
await_: Await | None = Field(None, alias="await")
|
99
100
|
output: Message | None = None
|
100
|
-
error:
|
101
|
+
error: Error | None = None
|
101
102
|
|
102
103
|
model_config = ConfigDict(populate_by_name=True)
|
103
104
|
|
@@ -174,42 +175,7 @@ RunEvent = Union[
|
|
174
175
|
]
|
175
176
|
|
176
177
|
|
177
|
-
class RunCreateRequest(BaseModel):
|
178
|
-
agent_name: AgentName
|
179
|
-
session_id: SessionId | None = None
|
180
|
-
input: Message
|
181
|
-
mode: RunMode = RunMode.SYNC
|
182
|
-
|
183
|
-
|
184
|
-
class RunCreateResponse(Run):
|
185
|
-
pass
|
186
|
-
|
187
|
-
|
188
|
-
class RunResumeRequest(BaseModel):
|
189
|
-
await_: AwaitResume = Field(alias="await")
|
190
|
-
mode: RunMode
|
191
|
-
|
192
|
-
|
193
|
-
class RunResumeResponse(Run):
|
194
|
-
pass
|
195
|
-
|
196
|
-
|
197
|
-
class RunReadResponse(Run):
|
198
|
-
pass
|
199
|
-
|
200
|
-
|
201
|
-
class RunCancelResponse(Run):
|
202
|
-
pass
|
203
|
-
|
204
|
-
|
205
178
|
class Agent(BaseModel):
|
206
179
|
name: str
|
207
180
|
description: str | None = None
|
208
|
-
|
209
|
-
|
210
|
-
class AgentsListResponse(BaseModel):
|
211
|
-
agents: list[Agent]
|
212
|
-
|
213
|
-
|
214
|
-
class AgentReadResponse(Agent):
|
215
|
-
pass
|
181
|
+
metadata: Metadata = Metadata()
|
@@ -0,0 +1,39 @@
|
|
1
|
+
from pydantic import BaseModel, Field
|
2
|
+
|
3
|
+
from acp_sdk.models.models import Agent, AgentName, AwaitResume, Message, Run, RunMode, SessionId
|
4
|
+
|
5
|
+
|
6
|
+
class AgentsListResponse(BaseModel):
|
7
|
+
agents: list[Agent]
|
8
|
+
|
9
|
+
|
10
|
+
class AgentReadResponse(Agent):
|
11
|
+
pass
|
12
|
+
|
13
|
+
|
14
|
+
class RunCreateRequest(BaseModel):
|
15
|
+
agent_name: AgentName
|
16
|
+
session_id: SessionId | None = None
|
17
|
+
input: Message
|
18
|
+
mode: RunMode = RunMode.SYNC
|
19
|
+
|
20
|
+
|
21
|
+
class RunCreateResponse(Run):
|
22
|
+
pass
|
23
|
+
|
24
|
+
|
25
|
+
class RunResumeRequest(BaseModel):
|
26
|
+
await_: AwaitResume = Field(alias="await")
|
27
|
+
mode: RunMode
|
28
|
+
|
29
|
+
|
30
|
+
class RunResumeResponse(Run):
|
31
|
+
pass
|
32
|
+
|
33
|
+
|
34
|
+
class RunReadResponse(Run):
|
35
|
+
pass
|
36
|
+
|
37
|
+
|
38
|
+
class RunCancelResponse(Run):
|
39
|
+
pass
|
@@ -0,0 +1,8 @@
|
|
1
|
+
from acp_sdk.server.agent import Agent as Agent
|
2
|
+
from acp_sdk.server.agent import SyncAgent as SyncAgent
|
3
|
+
from acp_sdk.server.app import create_app as create_app
|
4
|
+
from acp_sdk.server.context import Context as Context
|
5
|
+
from acp_sdk.server.context import SyncContext as SyncContext
|
6
|
+
from acp_sdk.server.server import Server as Server
|
7
|
+
from acp_sdk.server.types import RunYield as RunYield
|
8
|
+
from acp_sdk.server.types import RunYieldResume as RunYieldResume
|
@@ -0,0 +1,74 @@
|
|
1
|
+
import abc
|
2
|
+
import asyncio
|
3
|
+
from collections.abc import AsyncGenerator
|
4
|
+
from concurrent.futures import ThreadPoolExecutor
|
5
|
+
|
6
|
+
import janus
|
7
|
+
|
8
|
+
from acp_sdk.models import (
|
9
|
+
AgentName,
|
10
|
+
Message,
|
11
|
+
SessionId,
|
12
|
+
)
|
13
|
+
from acp_sdk.models.models import Metadata
|
14
|
+
from acp_sdk.server.context import Context, SyncContext
|
15
|
+
from acp_sdk.server.types import RunYield, RunYieldResume
|
16
|
+
|
17
|
+
|
18
|
+
class Agent(abc.ABC):
|
19
|
+
@property
|
20
|
+
def name(self) -> AgentName:
|
21
|
+
return self.__class__.__name__
|
22
|
+
|
23
|
+
@property
|
24
|
+
def description(self) -> str:
|
25
|
+
return ""
|
26
|
+
|
27
|
+
@property
|
28
|
+
def metadata(self) -> Metadata:
|
29
|
+
return Metadata()
|
30
|
+
|
31
|
+
@abc.abstractmethod
|
32
|
+
def run(
|
33
|
+
self, input: Message, context: Context, executor: ThreadPoolExecutor
|
34
|
+
) -> AsyncGenerator[RunYield, RunYieldResume]:
|
35
|
+
pass
|
36
|
+
|
37
|
+
async def session(self, session_id: SessionId | None) -> SessionId | None:
|
38
|
+
if session_id:
|
39
|
+
raise NotImplementedError()
|
40
|
+
return None
|
41
|
+
|
42
|
+
|
43
|
+
class SyncAgent(Agent):
|
44
|
+
@abc.abstractmethod
|
45
|
+
def run_sync(self, input: Message, context: SyncContext, executor: ThreadPoolExecutor) -> RunYield | None:
|
46
|
+
pass
|
47
|
+
|
48
|
+
async def run(
|
49
|
+
self, input: Message, context: Context, executor: ThreadPoolExecutor
|
50
|
+
) -> AsyncGenerator[RunYield, RunYieldResume]:
|
51
|
+
yield_queue: janus.Queue[RunYield] = janus.Queue()
|
52
|
+
yield_resume_queue: janus.Queue[RunYieldResume] = janus.Queue()
|
53
|
+
|
54
|
+
run_future = asyncio.get_running_loop().run_in_executor(
|
55
|
+
executor,
|
56
|
+
self.run_sync,
|
57
|
+
input,
|
58
|
+
SyncContext(
|
59
|
+
session_id=context.session_id,
|
60
|
+
yield_queue=yield_queue.sync_q,
|
61
|
+
yield_resume_queue=yield_resume_queue.sync_q,
|
62
|
+
),
|
63
|
+
executor,
|
64
|
+
)
|
65
|
+
|
66
|
+
while True:
|
67
|
+
yield_task = asyncio.create_task(yield_queue.async_q.get())
|
68
|
+
done, _ = await asyncio.wait([yield_task, run_future], return_when=asyncio.FIRST_COMPLETED)
|
69
|
+
if yield_task in done:
|
70
|
+
resume = yield await yield_task
|
71
|
+
await yield_resume_queue.async_q.put(resume)
|
72
|
+
if run_future in done:
|
73
|
+
yield await run_future
|
74
|
+
break
|
@@ -1,4 +1,7 @@
|
|
1
1
|
import asyncio
|
2
|
+
from collections.abc import AsyncGenerator
|
3
|
+
from concurrent.futures import ThreadPoolExecutor
|
4
|
+
from contextlib import asynccontextmanager
|
2
5
|
|
3
6
|
from fastapi import FastAPI, HTTPException, status
|
4
7
|
from fastapi.responses import JSONResponse, StreamingResponse
|
@@ -22,21 +25,42 @@ from acp_sdk.models import (
|
|
22
25
|
RunResumeResponse,
|
23
26
|
RunStatus,
|
24
27
|
)
|
28
|
+
from acp_sdk.models.errors import ACPError
|
25
29
|
from acp_sdk.server.agent import Agent
|
26
30
|
from acp_sdk.server.bundle import RunBundle
|
27
|
-
from acp_sdk.server.
|
31
|
+
from acp_sdk.server.errors import (
|
32
|
+
RequestValidationError,
|
33
|
+
StarletteHTTPException,
|
34
|
+
acp_error_handler,
|
35
|
+
catch_all_exception_handler,
|
36
|
+
http_exception_handler,
|
37
|
+
validation_exception_handler,
|
38
|
+
)
|
28
39
|
from acp_sdk.server.utils import stream_sse
|
29
40
|
|
30
41
|
|
31
42
|
def create_app(*agents: Agent) -> FastAPI:
|
32
|
-
|
43
|
+
executor: ThreadPoolExecutor
|
44
|
+
|
45
|
+
@asynccontextmanager
|
46
|
+
async def lifespan(app: FastAPI) -> AsyncGenerator[None]:
|
47
|
+
nonlocal executor
|
48
|
+
with ThreadPoolExecutor(max_workers=5) as exec:
|
49
|
+
executor = exec
|
50
|
+
yield
|
51
|
+
|
52
|
+
app = FastAPI(lifespan=lifespan)
|
33
53
|
|
34
|
-
configure_telemetry()
|
35
54
|
FastAPIInstrumentor.instrument_app(app)
|
36
55
|
|
37
56
|
agents: dict[AgentName, Agent] = {agent.name: agent for agent in agents}
|
38
57
|
runs: dict[RunId, RunBundle] = {}
|
39
58
|
|
59
|
+
app.exception_handler(ACPError)(acp_error_handler)
|
60
|
+
app.exception_handler(StarletteHTTPException)(http_exception_handler)
|
61
|
+
app.exception_handler(RequestValidationError)(validation_exception_handler)
|
62
|
+
app.exception_handler(Exception)(catch_all_exception_handler)
|
63
|
+
|
40
64
|
def find_run_bundle(run_id: RunId) -> RunBundle:
|
41
65
|
bundle = runs.get(run_id)
|
42
66
|
if not bundle:
|
@@ -52,13 +76,16 @@ def create_app(*agents: Agent) -> FastAPI:
|
|
52
76
|
@app.get("/agents")
|
53
77
|
async def list_agents() -> AgentsListResponse:
|
54
78
|
return AgentsListResponse(
|
55
|
-
agents=[
|
79
|
+
agents=[
|
80
|
+
AgentModel(name=agent.name, description=agent.description, metadata=agent.metadata)
|
81
|
+
for agent in agents.values()
|
82
|
+
]
|
56
83
|
)
|
57
84
|
|
58
85
|
@app.get("/agents/{name}")
|
59
86
|
async def read_agent(name: AgentName) -> AgentReadResponse:
|
60
87
|
agent = find_agent(name)
|
61
|
-
return AgentModel(name=agent.name, description=agent.description)
|
88
|
+
return AgentModel(name=agent.name, description=agent.description, metadata=agent.metadata)
|
62
89
|
|
63
90
|
@app.post("/runs")
|
64
91
|
async def create_run(request: RunCreateRequest) -> RunCreateResponse:
|
@@ -71,7 +98,8 @@ def create_app(*agents: Agent) -> FastAPI:
|
|
71
98
|
),
|
72
99
|
)
|
73
100
|
|
74
|
-
|
101
|
+
nonlocal executor
|
102
|
+
bundle.task = asyncio.create_task(bundle.execute(request.input, executor=executor))
|
75
103
|
runs[bundle.run.run_id] = bundle
|
76
104
|
|
77
105
|
match request.mode:
|
@@ -1,12 +1,12 @@
|
|
1
1
|
import asyncio
|
2
2
|
import logging
|
3
3
|
from collections.abc import AsyncGenerator
|
4
|
+
from concurrent.futures import ThreadPoolExecutor
|
4
5
|
|
5
6
|
from opentelemetry import trace
|
6
7
|
from pydantic import ValidationError
|
7
8
|
|
8
9
|
from acp_sdk.models import (
|
9
|
-
ACPError,
|
10
10
|
AnyModel,
|
11
11
|
Await,
|
12
12
|
AwaitEvent,
|
@@ -14,6 +14,7 @@ from acp_sdk.models import (
|
|
14
14
|
CancelledEvent,
|
15
15
|
CompletedEvent,
|
16
16
|
CreatedEvent,
|
17
|
+
Error,
|
17
18
|
FailedEvent,
|
18
19
|
GenericEvent,
|
19
20
|
InProgressEvent,
|
@@ -23,10 +24,10 @@ from acp_sdk.models import (
|
|
23
24
|
RunEvent,
|
24
25
|
RunStatus,
|
25
26
|
)
|
27
|
+
from acp_sdk.models.errors import ErrorCode
|
26
28
|
from acp_sdk.server.agent import Agent
|
27
29
|
from acp_sdk.server.context import Context
|
28
|
-
|
29
|
-
logger = logging.getLogger("uvicorn.error")
|
30
|
+
from acp_sdk.server.logging import logger
|
30
31
|
|
31
32
|
|
32
33
|
class RunBundle:
|
@@ -68,7 +69,7 @@ class RunBundle:
|
|
68
69
|
async def join(self) -> None:
|
69
70
|
await self.await_or_terminate_event.wait()
|
70
71
|
|
71
|
-
async def execute(self, input: Message) -> None:
|
72
|
+
async def execute(self, input: Message, *, executor: ThreadPoolExecutor) -> None:
|
72
73
|
with trace.get_tracer(__name__).start_as_current_span("execute"):
|
73
74
|
run_logger = logging.LoggerAdapter(logger, {"run_id": self.run.run_id})
|
74
75
|
|
@@ -77,7 +78,9 @@ class RunBundle:
|
|
77
78
|
self.run.session_id = await self.agent.session(self.run.session_id)
|
78
79
|
run_logger.info("Session loaded")
|
79
80
|
|
80
|
-
generator = self.agent.run(
|
81
|
+
generator = self.agent.run(
|
82
|
+
input=input, context=Context(session_id=self.run.session_id), executor=executor
|
83
|
+
)
|
81
84
|
run_logger.info("Run started")
|
82
85
|
|
83
86
|
self.run.status = RunStatus.IN_PROGRESS
|
@@ -122,7 +125,7 @@ class RunBundle:
|
|
122
125
|
await self.emit(CancelledEvent(run=self.run))
|
123
126
|
run_logger.info("Run cancelled")
|
124
127
|
except Exception as e:
|
125
|
-
self.run.error =
|
128
|
+
self.run.error = Error(code=ErrorCode.SERVER_ERROR, message=str(e))
|
126
129
|
self.run.status = RunStatus.FAILED
|
127
130
|
await self.emit(FailedEvent(run=self.run))
|
128
131
|
run_logger.exception("Run failed")
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import janus
|
2
|
+
|
3
|
+
from acp_sdk.models import SessionId
|
4
|
+
from acp_sdk.server.types import RunYield, RunYieldResume
|
5
|
+
|
6
|
+
|
7
|
+
class Context:
|
8
|
+
def __init__(self, *, session_id: SessionId | None = None) -> None:
|
9
|
+
self.session_id = session_id
|
10
|
+
|
11
|
+
|
12
|
+
class SyncContext(Context):
|
13
|
+
def __init__(
|
14
|
+
self,
|
15
|
+
*,
|
16
|
+
session_id: SessionId | None = None,
|
17
|
+
yield_queue: janus.SyncQueue[RunYield],
|
18
|
+
yield_resume_queue: janus.SyncQueue[RunYieldResume],
|
19
|
+
) -> None:
|
20
|
+
super().__init__(session_id=session_id)
|
21
|
+
self._yield_queue = yield_queue
|
22
|
+
self._yield_resume_queue = yield_resume_queue
|
23
|
+
|
24
|
+
def yield_(self, data: RunYield) -> RunYieldResume:
|
25
|
+
self._yield_queue.put(data)
|
26
|
+
return self._yield_resume_queue.get()
|
@@ -0,0 +1,54 @@
|
|
1
|
+
from fastapi import Request, status
|
2
|
+
from fastapi.exceptions import RequestValidationError
|
3
|
+
from fastapi.responses import JSONResponse
|
4
|
+
from starlette.exceptions import HTTPException as StarletteHTTPException
|
5
|
+
|
6
|
+
from acp_sdk.models import Error, ErrorCode
|
7
|
+
from acp_sdk.models.errors import ACPError
|
8
|
+
from acp_sdk.server.logging import logger
|
9
|
+
|
10
|
+
|
11
|
+
def error_code_to_status_code(error_code: ErrorCode) -> int:
|
12
|
+
match error_code:
|
13
|
+
case ErrorCode.NOT_FOUND:
|
14
|
+
return status.HTTP_404_NOT_FOUND
|
15
|
+
case ErrorCode.INVALID_INPUT:
|
16
|
+
return status.HTTP_422_UNPROCESSABLE_ENTITY
|
17
|
+
case _:
|
18
|
+
return status.HTTP_500_INTERNAL_SERVER_ERROR
|
19
|
+
|
20
|
+
|
21
|
+
def status_code_to_error_code(status_code: int) -> ErrorCode:
|
22
|
+
match status_code:
|
23
|
+
case status.HTTP_400_BAD_REQUEST:
|
24
|
+
return ErrorCode.INVALID_INPUT
|
25
|
+
case status.HTTP_404_NOT_FOUND:
|
26
|
+
return ErrorCode.NOT_FOUND
|
27
|
+
case status.HTTP_422_UNPROCESSABLE_ENTITY:
|
28
|
+
return ErrorCode.INVALID_INPUT
|
29
|
+
case _:
|
30
|
+
return ErrorCode.SERVER_ERROR
|
31
|
+
|
32
|
+
|
33
|
+
async def acp_error_handler(request: Request, exc: ACPError, *, status_code: int | None = None) -> JSONResponse:
|
34
|
+
error = exc.error
|
35
|
+
return JSONResponse(status_code=status_code or error_code_to_status_code(error.code), content=error.model_dump())
|
36
|
+
|
37
|
+
|
38
|
+
async def http_exception_handler(request: Request, exc: StarletteHTTPException) -> JSONResponse:
|
39
|
+
return await acp_error_handler(
|
40
|
+
request,
|
41
|
+
ACPError(Error(code=status_code_to_error_code(exc.status_code), message=exc.detail)),
|
42
|
+
status_code=exc.status_code,
|
43
|
+
)
|
44
|
+
|
45
|
+
|
46
|
+
async def validation_exception_handler(request: Request, exc: RequestValidationError) -> JSONResponse:
|
47
|
+
return await acp_error_handler(request, ACPError(Error(code=ErrorCode.INVALID_INPUT, message=str(exc))))
|
48
|
+
|
49
|
+
|
50
|
+
async def catch_all_exception_handler(request: Request, exc: Exception) -> JSONResponse:
|
51
|
+
logger.error(exc)
|
52
|
+
return await acp_error_handler(
|
53
|
+
request, ACPError(Error(code=ErrorCode.SERVER_ERROR, message="An unexpected error occurred"))
|
54
|
+
)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import logging
|
2
|
+
|
3
|
+
from uvicorn.logging import DefaultFormatter
|
4
|
+
|
5
|
+
logger = logging.getLogger("acp")
|
6
|
+
|
7
|
+
|
8
|
+
def configure_logger() -> None:
|
9
|
+
"""Utility that configures the root logger"""
|
10
|
+
root_logger = logging.getLogger()
|
11
|
+
|
12
|
+
handler = logging.StreamHandler()
|
13
|
+
handler.setFormatter(DefaultFormatter(fmt="%(levelprefix)s %(message)s"))
|
14
|
+
|
15
|
+
root_logger.addHandler(handler)
|
16
|
+
root_logger.setLevel(logging.INFO)
|
@@ -0,0 +1,110 @@
|
|
1
|
+
import inspect
|
2
|
+
from collections.abc import AsyncGenerator
|
3
|
+
from concurrent.futures import ThreadPoolExecutor
|
4
|
+
from typing import Any, Callable
|
5
|
+
|
6
|
+
from acp_sdk.models import Message
|
7
|
+
from acp_sdk.server.agent import Agent, SyncAgent
|
8
|
+
from acp_sdk.server.app import create_app
|
9
|
+
from acp_sdk.server.context import Context
|
10
|
+
from acp_sdk.server.logging import configure_logger as configure_logger_func
|
11
|
+
from acp_sdk.server.telemetry import configure_telemetry as configure_telemetry_func
|
12
|
+
from acp_sdk.server.types import RunYield, RunYieldResume
|
13
|
+
|
14
|
+
|
15
|
+
class Server:
|
16
|
+
def __init__(self) -> None:
|
17
|
+
self.agents: list[Agent] = []
|
18
|
+
|
19
|
+
def agent(self, name: str | None = None, description: str | None = None) -> Callable:
|
20
|
+
"""Decorator to register an agent."""
|
21
|
+
|
22
|
+
def decorator(fn: Callable) -> Callable:
|
23
|
+
# check agent's function signature
|
24
|
+
signature = inspect.signature(fn)
|
25
|
+
parameters = list(signature.parameters.values())
|
26
|
+
|
27
|
+
# validate agent's function
|
28
|
+
if inspect.isasyncgenfunction(fn):
|
29
|
+
if len(parameters) != 2:
|
30
|
+
raise TypeError(
|
31
|
+
"The agent generator function must have one 'input' argument and one 'context' argument"
|
32
|
+
)
|
33
|
+
else:
|
34
|
+
if len(parameters) != 2:
|
35
|
+
raise TypeError("The agent function must have one 'input' argument and one 'context' argument")
|
36
|
+
|
37
|
+
agent: Agent
|
38
|
+
if inspect.isasyncgenfunction(fn):
|
39
|
+
|
40
|
+
class DecoratedAgent(Agent):
|
41
|
+
@property
|
42
|
+
def name(self) -> str:
|
43
|
+
return name or fn.__name__
|
44
|
+
|
45
|
+
@property
|
46
|
+
def description(self) -> str:
|
47
|
+
return description or fn.__doc__ or ""
|
48
|
+
|
49
|
+
async def run(
|
50
|
+
self, input: Message, context: Context, executor: ThreadPoolExecutor
|
51
|
+
) -> AsyncGenerator[RunYield, RunYieldResume]:
|
52
|
+
gen: AsyncGenerator[RunYield, RunYieldResume] = fn(input, context)
|
53
|
+
value = None
|
54
|
+
while True:
|
55
|
+
try:
|
56
|
+
value = yield await gen.asend(value)
|
57
|
+
except StopAsyncIteration:
|
58
|
+
break
|
59
|
+
|
60
|
+
agent = DecoratedAgent()
|
61
|
+
elif inspect.iscoroutinefunction(fn):
|
62
|
+
|
63
|
+
class DecoratedAgent(Agent):
|
64
|
+
@property
|
65
|
+
def name(self) -> str:
|
66
|
+
return name or fn.__name__
|
67
|
+
|
68
|
+
@property
|
69
|
+
def description(self) -> str:
|
70
|
+
return description or fn.__doc__ or ""
|
71
|
+
|
72
|
+
async def run(
|
73
|
+
self, input: Message, context: Context, executor: ThreadPoolExecutor
|
74
|
+
) -> AsyncGenerator[RunYield, RunYieldResume]:
|
75
|
+
yield await fn(input, context)
|
76
|
+
|
77
|
+
agent = DecoratedAgent()
|
78
|
+
else:
|
79
|
+
|
80
|
+
class DecoratedAgent(SyncAgent):
|
81
|
+
@property
|
82
|
+
def name(self) -> str:
|
83
|
+
return name or fn.__name__
|
84
|
+
|
85
|
+
@property
|
86
|
+
def description(self) -> str:
|
87
|
+
return description or fn.__doc__ or ""
|
88
|
+
|
89
|
+
def run_sync(self, input: Message, context: Context, executor: ThreadPoolExecutor) -> None:
|
90
|
+
return fn(input, context)
|
91
|
+
|
92
|
+
agent = DecoratedAgent()
|
93
|
+
|
94
|
+
self.register(agent)
|
95
|
+
return fn
|
96
|
+
|
97
|
+
return decorator
|
98
|
+
|
99
|
+
def register(self, *agents: Agent) -> None:
|
100
|
+
self.agents.extend(agents)
|
101
|
+
|
102
|
+
def run(self, configure_logger: bool = True, configure_telemetry: bool = False, **kwargs: dict[str, Any]) -> None:
|
103
|
+
import uvicorn
|
104
|
+
|
105
|
+
if configure_logger:
|
106
|
+
configure_logger_func()
|
107
|
+
if configure_telemetry:
|
108
|
+
configure_telemetry_func()
|
109
|
+
|
110
|
+
uvicorn.run(create_app(*self.agents), **kwargs)
|
@@ -0,0 +1,52 @@
|
|
1
|
+
import logging
|
2
|
+
from importlib.metadata import version
|
3
|
+
|
4
|
+
from opentelemetry import metrics, trace
|
5
|
+
from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter
|
6
|
+
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
|
7
|
+
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
|
8
|
+
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
|
9
|
+
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
|
10
|
+
from opentelemetry.sdk.metrics import MeterProvider
|
11
|
+
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
|
12
|
+
from opentelemetry.sdk.resources import (
|
13
|
+
SERVICE_NAME,
|
14
|
+
SERVICE_NAMESPACE,
|
15
|
+
SERVICE_VERSION,
|
16
|
+
Resource,
|
17
|
+
)
|
18
|
+
from opentelemetry.sdk.trace import TracerProvider
|
19
|
+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
20
|
+
|
21
|
+
root_logger = logging.getLogger()
|
22
|
+
|
23
|
+
|
24
|
+
def configure_telemetry() -> None:
|
25
|
+
"""Utility that configures opentelemetry with OTLP exporter"""
|
26
|
+
|
27
|
+
resource = Resource(
|
28
|
+
attributes={
|
29
|
+
SERVICE_NAME: "acp-server",
|
30
|
+
SERVICE_NAMESPACE: "acp",
|
31
|
+
SERVICE_VERSION: version("acp-sdk"),
|
32
|
+
}
|
33
|
+
)
|
34
|
+
|
35
|
+
# Traces
|
36
|
+
provider = TracerProvider(resource=resource)
|
37
|
+
processor = BatchSpanProcessor(OTLPSpanExporter())
|
38
|
+
provider.add_span_processor(processor)
|
39
|
+
trace.set_tracer_provider(provider)
|
40
|
+
|
41
|
+
# Metrics
|
42
|
+
meter_provider = MeterProvider(
|
43
|
+
resource=resource,
|
44
|
+
metric_readers=[PeriodicExportingMetricReader(OTLPMetricExporter())],
|
45
|
+
)
|
46
|
+
metrics.set_meter_provider(meter_provider)
|
47
|
+
|
48
|
+
# Logs
|
49
|
+
logger_provider = LoggerProvider(resource=resource)
|
50
|
+
processor = BatchLogRecordProcessor(OTLPLogExporter())
|
51
|
+
logger_provider.add_log_record_processor(processor)
|
52
|
+
root_logger.addHandler(LoggingHandler(logger_provider=logger_provider))
|
acp_sdk-1.0.0rc2/PKG-INFO
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.4
|
2
|
-
Name: acp-sdk
|
3
|
-
Version: 1.0.0rc2
|
4
|
-
Summary: Agent Communication Protocol SDK
|
5
|
-
Requires-Python: <4.0,>=3.11
|
6
|
-
Requires-Dist: opentelemetry-api>=1.31.1
|
7
|
-
Requires-Dist: pydantic>=2.11.1
|
8
|
-
Provides-Extra: client
|
9
|
-
Requires-Dist: httpx-sse>=0.4.0; extra == 'client'
|
10
|
-
Requires-Dist: httpx>=0.28.1; extra == 'client'
|
11
|
-
Requires-Dist: opentelemetry-instrumentation-httpx>=0.52b1; extra == 'client'
|
12
|
-
Provides-Extra: server
|
13
|
-
Requires-Dist: fastapi[standard]>=0.115.8; extra == 'server'
|
14
|
-
Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.31.1; extra == 'server'
|
15
|
-
Requires-Dist: opentelemetry-instrumentation-fastapi>=0.52b1; extra == 'server'
|
16
|
-
Requires-Dist: opentelemetry-sdk>=1.31.1; extra == 'server'
|
17
|
-
Description-Content-Type: text/markdown
|
18
|
-
|
19
|
-
# Agent Communication Protocol SDK for Python
|
20
|
-
|
21
|
-
## Prerequisites
|
22
|
-
|
23
|
-
✅ Python >= 3.13
|
24
|
-
|
25
|
-
## Installation
|
26
|
-
|
27
|
-
Install using pip:
|
28
|
-
|
29
|
-
```shell
|
30
|
-
pip install acp-sdk
|
31
|
-
```
|
32
|
-
|
33
|
-
## Example
|
34
|
-
|
35
|
-
The SDK can be used to implement both clients and servers.
|
36
|
-
|
37
|
-
### Server
|
38
|
-
|
39
|
-
To run an example server use [FastAPI CLI](https://fastapi.tiangolo.com/fastapi-cli/) or other [deployment strategies](https://fastapi.tiangolo.com/deployment/):
|
40
|
-
|
41
|
-
```shell
|
42
|
-
fastapi dev examples/servers/echo.py
|
43
|
-
```
|
44
|
-
|
45
|
-
### Client
|
46
|
-
|
47
|
-
To run an example client:
|
48
|
-
|
49
|
-
```shell
|
50
|
-
python examples/clients/simple.py
|
51
|
-
```
|
52
|
-
|
53
|
-
➡️ Explore more in our [examples library](/python/examples).
|
acp_sdk-1.0.0rc2/README.md
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
# Agent Communication Protocol SDK for Python
|
2
|
-
|
3
|
-
## Prerequisites
|
4
|
-
|
5
|
-
✅ Python >= 3.13
|
6
|
-
|
7
|
-
## Installation
|
8
|
-
|
9
|
-
Install using pip:
|
10
|
-
|
11
|
-
```shell
|
12
|
-
pip install acp-sdk
|
13
|
-
```
|
14
|
-
|
15
|
-
## Example
|
16
|
-
|
17
|
-
The SDK can be used to implement both clients and servers.
|
18
|
-
|
19
|
-
### Server
|
20
|
-
|
21
|
-
To run an example server use [FastAPI CLI](https://fastapi.tiangolo.com/fastapi-cli/) or other [deployment strategies](https://fastapi.tiangolo.com/deployment/):
|
22
|
-
|
23
|
-
```shell
|
24
|
-
fastapi dev examples/servers/echo.py
|
25
|
-
```
|
26
|
-
|
27
|
-
### Client
|
28
|
-
|
29
|
-
To run an example client:
|
30
|
-
|
31
|
-
```shell
|
32
|
-
python examples/clients/simple.py
|
33
|
-
```
|
34
|
-
|
35
|
-
➡️ Explore more in our [examples library](/python/examples).
|
@@ -1,29 +0,0 @@
|
|
1
|
-
from collections.abc import AsyncGenerator
|
2
|
-
|
3
|
-
from acp_sdk.models import (
|
4
|
-
Await,
|
5
|
-
AwaitResume,
|
6
|
-
Message,
|
7
|
-
TextMessagePart,
|
8
|
-
)
|
9
|
-
from acp_sdk.server import Agent
|
10
|
-
from acp_sdk.server.context import Context
|
11
|
-
from acp_sdk.server.server import create_app
|
12
|
-
|
13
|
-
|
14
|
-
class AwaitingAgent(Agent):
|
15
|
-
@property
|
16
|
-
def name(self) -> str:
|
17
|
-
return "awaiting"
|
18
|
-
|
19
|
-
@property
|
20
|
-
def description(self) -> str:
|
21
|
-
return "Greets and awaits for more data"
|
22
|
-
|
23
|
-
async def run(self, input: Message, *, context: Context) -> AsyncGenerator[Message | Await, AwaitResume]:
|
24
|
-
yield Message(TextMessagePart(content="Hello!"))
|
25
|
-
data = yield Await()
|
26
|
-
yield Message(TextMessagePart(content=f"Thanks for {data}"))
|
27
|
-
|
28
|
-
|
29
|
-
app = create_app(AwaitingAgent())
|
@@ -1,29 +0,0 @@
|
|
1
|
-
import asyncio
|
2
|
-
from collections.abc import AsyncGenerator
|
3
|
-
|
4
|
-
from acp_sdk.models import (
|
5
|
-
Await,
|
6
|
-
AwaitResume,
|
7
|
-
Message,
|
8
|
-
)
|
9
|
-
from acp_sdk.server import Agent, create_app
|
10
|
-
from acp_sdk.server.context import Context
|
11
|
-
|
12
|
-
|
13
|
-
class EchoAgent(Agent):
|
14
|
-
@property
|
15
|
-
def name(self) -> str:
|
16
|
-
return "echo"
|
17
|
-
|
18
|
-
@property
|
19
|
-
def description(self) -> str:
|
20
|
-
return "Echoes everything"
|
21
|
-
|
22
|
-
async def run(self, input: Message, *, context: Context) -> AsyncGenerator[Message | Await, AwaitResume]:
|
23
|
-
for part in input:
|
24
|
-
await asyncio.sleep(0.5)
|
25
|
-
yield {"thought": "I should echo everyting"}
|
26
|
-
yield Message(part)
|
27
|
-
|
28
|
-
|
29
|
-
app = create_app(EchoAgent())
|
File without changes
|
@@ -1,30 +0,0 @@
|
|
1
|
-
import abc
|
2
|
-
from collections.abc import AsyncGenerator
|
3
|
-
|
4
|
-
from acp_sdk.models import (
|
5
|
-
AgentName,
|
6
|
-
Await,
|
7
|
-
AwaitResume,
|
8
|
-
Message,
|
9
|
-
SessionId,
|
10
|
-
)
|
11
|
-
from acp_sdk.server.context import Context
|
12
|
-
|
13
|
-
|
14
|
-
class Agent(abc.ABC):
|
15
|
-
@property
|
16
|
-
def name(self) -> AgentName:
|
17
|
-
return self.__class__.__name__
|
18
|
-
|
19
|
-
@property
|
20
|
-
def description(self) -> str:
|
21
|
-
return ""
|
22
|
-
|
23
|
-
@abc.abstractmethod
|
24
|
-
def run(self, input: Message, *, context: Context) -> AsyncGenerator[Message | Await, AwaitResume]:
|
25
|
-
pass
|
26
|
-
|
27
|
-
async def session(self, session_id: SessionId | None) -> SessionId | None:
|
28
|
-
if session_id:
|
29
|
-
raise NotImplementedError()
|
30
|
-
return None
|
@@ -1,46 +0,0 @@
|
|
1
|
-
import logging
|
2
|
-
from importlib.metadata import version
|
3
|
-
from typing import Any
|
4
|
-
|
5
|
-
from opentelemetry import trace
|
6
|
-
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
|
7
|
-
from opentelemetry.sdk.resources import (
|
8
|
-
SERVICE_NAME,
|
9
|
-
SERVICE_NAMESPACE,
|
10
|
-
SERVICE_VERSION,
|
11
|
-
Resource,
|
12
|
-
)
|
13
|
-
from opentelemetry.sdk.trace import TracerProvider
|
14
|
-
from opentelemetry.sdk.trace.export import BatchSpanProcessor, SpanExportResult
|
15
|
-
|
16
|
-
logger = logging.getLogger("uvicorn.error")
|
17
|
-
|
18
|
-
|
19
|
-
class SilentOTLPSpanExporter(OTLPSpanExporter):
|
20
|
-
def export(self, spans: Any) -> SpanExportResult:
|
21
|
-
try:
|
22
|
-
return super().export(spans)
|
23
|
-
except Exception as e:
|
24
|
-
logger.warning(f"OpenTelemetry Exporter failed silently: {e}")
|
25
|
-
return SpanExportResult.FAILURE
|
26
|
-
|
27
|
-
|
28
|
-
def configure_telemetry() -> None:
|
29
|
-
current_provider = trace.get_tracer_provider()
|
30
|
-
|
31
|
-
# Detect default provider and override
|
32
|
-
if isinstance(current_provider, trace.ProxyTracerProvider):
|
33
|
-
provider = TracerProvider(
|
34
|
-
resource=Resource(
|
35
|
-
attributes={
|
36
|
-
SERVICE_NAME: "acp-server",
|
37
|
-
SERVICE_NAMESPACE: "acp",
|
38
|
-
SERVICE_VERSION: version("acp-sdk"),
|
39
|
-
}
|
40
|
-
)
|
41
|
-
)
|
42
|
-
|
43
|
-
processor = BatchSpanProcessor(SilentOTLPSpanExporter())
|
44
|
-
provider.add_span_processor(processor)
|
45
|
-
|
46
|
-
trace.set_tracer_provider(provider)
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|