-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path01_basic_agent.py
More file actions
77 lines (61 loc) · 2.32 KB
/
Copy path01_basic_agent.py
File metadata and controls
77 lines (61 loc) · 2.32 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
71
72
73
74
75
76
77
# /// script
# requires-python = ">=3.11"
# dependencies = ["band-sdk[opencode]", "python-dotenv"]
#
# [tool.uv.sources]
# band-sdk = { git = "https://github.com/band-ai/band-sdk-python.git" }
# ///
"""
Basic OpenCode adapter agent example.
Prerequisites:
1. Install OpenCode: `npm install -g opencode-ai`
2. Start the server: `opencode serve --hostname=127.0.0.1 --port=4096`
3. Set `BAND_WS_URL` and `BAND_REST_URL`
4. Add agent credentials to `agent_config.yaml`
5. The example defaults to the locally available free model `opencode/minimax-m2.5-free`
Run with:
uv run examples/opencode/01_basic_agent.py
"""
from __future__ import annotations
import asyncio
import logging
import os
import sys
from dotenv import load_dotenv
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from setup_logging import setup_logging # pyrefly: ignore[missing-import]
from band import Agent
from band.adapters.opencode import OpencodeAdapter, OpencodeAdapterConfig
from band.core.types import AdapterFeatures, Emit
setup_logging()
logger = logging.getLogger(__name__)
async def main() -> None:
load_dotenv()
ws_url = os.getenv("BAND_WS_URL")
rest_url = os.getenv("BAND_REST_URL")
if not ws_url:
raise ValueError("BAND_WS_URL environment variable is required")
if not rest_url:
raise ValueError("BAND_REST_URL environment variable is required")
agent_key = os.getenv("AGENT_KEY", "darter")
adapter = OpencodeAdapter(
config=OpencodeAdapterConfig(
base_url=os.getenv("OPENCODE_BASE_URL", "http://127.0.0.1:4096"),
provider_id=os.getenv("OPENCODE_PROVIDER_ID", "opencode"),
model_id=os.getenv("OPENCODE_MODEL_ID", "minimax-m2.5-free"),
agent=os.getenv("OPENCODE_AGENT") or None,
custom_section="You are a helpful assistant. Keep replies concise.",
approval_mode=os.getenv("OPENCODE_APPROVAL_MODE", "manual"), # type: ignore[arg-type] # env var is str; invalid values fall through to manual mode
features=AdapterFeatures(emit={Emit.EXECUTION}),
)
)
agent = Agent.from_config(
agent_key,
adapter=adapter,
ws_url=ws_url,
rest_url=rest_url,
)
logger.info("Starting OpenCode agent: %s", agent_key)
await agent.run()
if __name__ == "__main__":
asyncio.run(main())