haijun-agent-sdk 0.1.0__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.
- haijun_agent_sdk-0.1.0/.gitignore +57 -0
- haijun_agent_sdk-0.1.0/LICENSE +21 -0
- haijun_agent_sdk-0.1.0/PKG-INFO +405 -0
- haijun_agent_sdk-0.1.0/README.md +363 -0
- haijun_agent_sdk-0.1.0/examples/agents.py +124 -0
- haijun_agent_sdk-0.1.0/examples/filesystem_agents.py +107 -0
- haijun_agent_sdk-0.1.0/examples/hooks.py +344 -0
- haijun_agent_sdk-0.1.0/examples/include_partial_messages.py +58 -0
- haijun_agent_sdk-0.1.0/examples/max_budget_usd.py +95 -0
- haijun_agent_sdk-0.1.0/examples/mcp_calculator.py +193 -0
- haijun_agent_sdk-0.1.0/examples/plugin_example.py +71 -0
- haijun_agent_sdk-0.1.0/examples/plugins/demo-plugin/.haijun-plugin/plugin.json +8 -0
- haijun_agent_sdk-0.1.0/examples/plugins/demo-plugin/commands/greet.md +5 -0
- haijun_agent_sdk-0.1.0/examples/quick_start.py +75 -0
- haijun_agent_sdk-0.1.0/examples/session_stores/README.md +412 -0
- haijun_agent_sdk-0.1.0/examples/session_stores/__init__.py +0 -0
- haijun_agent_sdk-0.1.0/examples/session_stores/postgres_session_store.py +256 -0
- haijun_agent_sdk-0.1.0/examples/session_stores/redis_session_store.py +177 -0
- haijun_agent_sdk-0.1.0/examples/session_stores/s3_session_store.py +423 -0
- haijun_agent_sdk-0.1.0/examples/setting_sources.py +204 -0
- haijun_agent_sdk-0.1.0/examples/stderr_callback_example.py +42 -0
- haijun_agent_sdk-0.1.0/examples/streaming_mode.py +518 -0
- haijun_agent_sdk-0.1.0/examples/streaming_mode_ipython.py +229 -0
- haijun_agent_sdk-0.1.0/examples/streaming_mode_trio.py +80 -0
- haijun_agent_sdk-0.1.0/examples/system_prompt.py +87 -0
- haijun_agent_sdk-0.1.0/examples/tool_permission_callback.py +156 -0
- haijun_agent_sdk-0.1.0/examples/tools_option.py +111 -0
- haijun_agent_sdk-0.1.0/pyproject.toml +142 -0
- haijun_agent_sdk-0.1.0/scripts/_cli_version_validation.py +119 -0
- haijun_agent_sdk-0.1.0/scripts/build_wheel.py +429 -0
- haijun_agent_sdk-0.1.0/scripts/check_pypi_quota.py +128 -0
- haijun_agent_sdk-0.1.0/scripts/download_cli.py +366 -0
- haijun_agent_sdk-0.1.0/scripts/edit-issue-labels.sh +80 -0
- haijun_agent_sdk-0.1.0/scripts/gh.sh +95 -0
- haijun_agent_sdk-0.1.0/scripts/initial-setup.sh +22 -0
- haijun_agent_sdk-0.1.0/scripts/install-haijun-cli.ps1 +149 -0
- haijun_agent_sdk-0.1.0/scripts/install-haijun-cli.sh +199 -0
- haijun_agent_sdk-0.1.0/scripts/pre-push +33 -0
- haijun_agent_sdk-0.1.0/scripts/test-docker.sh +73 -0
- haijun_agent_sdk-0.1.0/scripts/trust_workspace.py +67 -0
- haijun_agent_sdk-0.1.0/scripts/update_cli_version.py +69 -0
- haijun_agent_sdk-0.1.0/scripts/update_version.py +49 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/__init__.py +770 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_bundled/.gitignore +3 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_cli_version.py +3 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_errors.py +131 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/__init__.py +1 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/_mcp_compat.py +151 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/_task_compat.py +187 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/client.py +193 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/message_parser.py +389 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/query.py +983 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/sdk_mcp_bridge.py +382 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/session_import.py +162 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/session_mutations.py +957 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/session_resume.py +612 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/session_store.py +193 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/session_store_validation.py +45 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/session_summary.py +229 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/sessions.py +2007 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/transcript_mirror_batcher.py +218 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/transport/__init__.py +87 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_internal/transport/subprocess_cli.py +1183 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/_version.py +3 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/client.py +597 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/py.typed +0 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/query.py +127 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/telemetry.py +181 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/testing/__init__.py +9 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/testing/session_store_conformance.py +327 -0
- haijun_agent_sdk-0.1.0/src/haijun_agent_sdk/types.py +2456 -0
- haijun_agent_sdk-0.1.0/tests/conftest.py +9 -0
- haijun_agent_sdk-0.1.0/tests/test_build_wheel.py +152 -0
- haijun_agent_sdk-0.1.0/tests/test_changelog.py +85 -0
- haijun_agent_sdk-0.1.0/tests/test_client.py +366 -0
- haijun_agent_sdk-0.1.0/tests/test_close_cancellation.py +215 -0
- haijun_agent_sdk-0.1.0/tests/test_download_cli.py +682 -0
- haijun_agent_sdk-0.1.0/tests/test_errors.py +112 -0
- haijun_agent_sdk-0.1.0/tests/test_example_postgres_session_store.py +252 -0
- haijun_agent_sdk-0.1.0/tests/test_example_redis_session_store.py +344 -0
- haijun_agent_sdk-0.1.0/tests/test_example_redis_session_store_live.py +176 -0
- haijun_agent_sdk-0.1.0/tests/test_example_s3_session_store.py +598 -0
- haijun_agent_sdk-0.1.0/tests/test_example_s3_session_store_live.py +211 -0
- haijun_agent_sdk-0.1.0/tests/test_integration.py +308 -0
- haijun_agent_sdk-0.1.0/tests/test_mcp_large_output.py +335 -0
- haijun_agent_sdk-0.1.0/tests/test_message_parser.py +1358 -0
- haijun_agent_sdk-0.1.0/tests/test_option_warnings.py +427 -0
- haijun_agent_sdk-0.1.0/tests/test_query.py +2024 -0
- haijun_agent_sdk-0.1.0/tests/test_rate_limit_event_repro.py +108 -0
- haijun_agent_sdk-0.1.0/tests/test_sdk_mcp_integration.py +2186 -0
- haijun_agent_sdk-0.1.0/tests/test_session_helpers_store.py +872 -0
- haijun_agent_sdk-0.1.0/tests/test_session_import.py +347 -0
- haijun_agent_sdk-0.1.0/tests/test_session_mutations.py +810 -0
- haijun_agent_sdk-0.1.0/tests/test_session_resume.py +1486 -0
- haijun_agent_sdk-0.1.0/tests/test_session_store_anyio.py +204 -0
- haijun_agent_sdk-0.1.0/tests/test_session_store_conformance.py +267 -0
- haijun_agent_sdk-0.1.0/tests/test_session_summary.py +882 -0
- haijun_agent_sdk-0.1.0/tests/test_sessions.py +1950 -0
- haijun_agent_sdk-0.1.0/tests/test_streaming_client.py +1271 -0
- haijun_agent_sdk-0.1.0/tests/test_subprocess_buffering.py +545 -0
- haijun_agent_sdk-0.1.0/tests/test_task_compat.py +256 -0
- haijun_agent_sdk-0.1.0/tests/test_telemetry.py +104 -0
- haijun_agent_sdk-0.1.0/tests/test_tool_callbacks.py +927 -0
- haijun_agent_sdk-0.1.0/tests/test_transcript_mirror.py +939 -0
- haijun_agent_sdk-0.1.0/tests/test_transport.py +2996 -0
- haijun_agent_sdk-0.1.0/tests/test_types.py +697 -0
- haijun_agent_sdk-0.1.0/tests/test_update_cli_version.py +329 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
.Python
|
|
7
|
+
build/
|
|
8
|
+
develop-eggs/
|
|
9
|
+
dist/
|
|
10
|
+
downloads/
|
|
11
|
+
eggs/
|
|
12
|
+
.eggs/
|
|
13
|
+
lib/
|
|
14
|
+
lib64/
|
|
15
|
+
parts/
|
|
16
|
+
sdist/
|
|
17
|
+
var/
|
|
18
|
+
wheels/
|
|
19
|
+
*.egg-info/
|
|
20
|
+
.installed.cfg
|
|
21
|
+
*.egg
|
|
22
|
+
MANIFEST
|
|
23
|
+
|
|
24
|
+
# Virtual environments
|
|
25
|
+
venv/
|
|
26
|
+
ENV/
|
|
27
|
+
env/
|
|
28
|
+
.venv
|
|
29
|
+
uv.lock
|
|
30
|
+
|
|
31
|
+
# Local env files (live-backend test credentials etc.)
|
|
32
|
+
.env
|
|
33
|
+
.env.*
|
|
34
|
+
|
|
35
|
+
# IDEs
|
|
36
|
+
.vscode/
|
|
37
|
+
.idea/
|
|
38
|
+
*.swp
|
|
39
|
+
*.swo
|
|
40
|
+
*~
|
|
41
|
+
**/.DS_Store
|
|
42
|
+
|
|
43
|
+
# Testing
|
|
44
|
+
.tox/
|
|
45
|
+
.coverage
|
|
46
|
+
.coverage.*
|
|
47
|
+
.cache
|
|
48
|
+
.pytest_cache/
|
|
49
|
+
htmlcov/
|
|
50
|
+
|
|
51
|
+
# Type checking
|
|
52
|
+
.mypy_cache/
|
|
53
|
+
.dmypy.json
|
|
54
|
+
dmypy.json
|
|
55
|
+
.pyre/
|
|
56
|
+
.ruff_cache/
|
|
57
|
+
.haijun/worktrees/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 JuGloW, PBC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: haijun-agent-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python SDK for Haijun Code via platform gateway
|
|
5
|
+
Project-URL: Homepage, https://github.com/takebox/haijun-agent-sdk-python
|
|
6
|
+
Project-URL: Documentation, https://raw.haijun.my.id/docs/en/docs/haijun-code/sdk
|
|
7
|
+
Project-URL: Issues, https://github.com/takebox/haijun-agent-sdk-python/issues
|
|
8
|
+
Author-email: JuGloW <support@juglow.my.id>
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: ai,haijun,juglow,sdk
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Requires-Dist: anyio>=4.0.0
|
|
23
|
+
Requires-Dist: jsonschema>=4.20.0
|
|
24
|
+
Requires-Dist: mcp<3.0.0,>=1.23.0
|
|
25
|
+
Requires-Dist: sniffio>=1.0.0
|
|
26
|
+
Requires-Dist: typing-extensions>=4.0.0; python_version < '3.11'
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: anyio[trio]>=4.0.0; extra == 'dev'
|
|
29
|
+
Requires-Dist: mypy>=1.0.0; extra == 'dev'
|
|
30
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
|
|
31
|
+
Requires-Dist: pytest>=7.0.0; extra == 'dev'
|
|
32
|
+
Requires-Dist: ruff>=0.1.0; extra == 'dev'
|
|
33
|
+
Provides-Extra: examples
|
|
34
|
+
Requires-Dist: asyncpg>=0.27.0; extra == 'examples'
|
|
35
|
+
Requires-Dist: boto3>=1.28.0; extra == 'examples'
|
|
36
|
+
Requires-Dist: fakeredis>=2.20.0; extra == 'examples'
|
|
37
|
+
Requires-Dist: moto[s3]>=5.0.0; extra == 'examples'
|
|
38
|
+
Requires-Dist: redis>=4.2.0; extra == 'examples'
|
|
39
|
+
Provides-Extra: otel
|
|
40
|
+
Requires-Dist: opentelemetry-api>=1.20.0; extra == 'otel'
|
|
41
|
+
Description-Content-Type: text/markdown
|
|
42
|
+
|
|
43
|
+
<p align="center">
|
|
44
|
+
<img src="assets/haijun-logo.png" alt="Haijun" width="500">
|
|
45
|
+
</p>
|
|
46
|
+
|
|
47
|
+
<p align="center"><strong>Haijun Agent SDK for Python</strong> — Python SDK for Haijun Agent. See the [Haijun Agent SDK documentation](https://raw.haijun.my.id/docs/en/agent-sdk/python) for more information.
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pip install haijun-agent-sdk
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**Prerequisites:**
|
|
56
|
+
|
|
57
|
+
- Python 3.10+
|
|
58
|
+
|
|
59
|
+
**Note:** The Haijun Code CLI is automatically bundled with the package - no separate installation required! The SDK will use the bundled CLI by default. If you prefer to use a system-wide installation or a specific version, you can:
|
|
60
|
+
|
|
61
|
+
- Install Haijun Code separately: `curl -fsSL https://downloads.haijun.my.id/cli/install.sh | bash`
|
|
62
|
+
- Specify a custom path: `HaijunAgentOptions(cli_path="/path/to/haijun")`
|
|
63
|
+
|
|
64
|
+
## Quick Start
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
import anyio
|
|
68
|
+
from haijun_agent_sdk import query
|
|
69
|
+
|
|
70
|
+
async def main():
|
|
71
|
+
async for message in query(prompt="What is 2 + 2?"):
|
|
72
|
+
print(message)
|
|
73
|
+
|
|
74
|
+
anyio.run(main)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Basic Usage: query()
|
|
78
|
+
|
|
79
|
+
`query()` is an async function for querying Haijun Code. It returns an `AsyncIterator` of response messages. See [src/haijun_agent_sdk/query.py](src/haijun_agent_sdk/query.py).
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from haijun_agent_sdk import query, HaijunAgentOptions, AssistantMessage, TextBlock
|
|
83
|
+
|
|
84
|
+
# Simple query
|
|
85
|
+
async for message in query(prompt="Hello Haijun"):
|
|
86
|
+
if isinstance(message, AssistantMessage):
|
|
87
|
+
for block in message.content:
|
|
88
|
+
if isinstance(block, TextBlock):
|
|
89
|
+
print(block.text)
|
|
90
|
+
|
|
91
|
+
# With options
|
|
92
|
+
options = HaijunAgentOptions(
|
|
93
|
+
system_prompt="You are a helpful assistant",
|
|
94
|
+
max_turns=1 )
|
|
95
|
+
|
|
96
|
+
async for message in query(prompt="Tell me a joke", options=options):
|
|
97
|
+
print(message)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Using Tools
|
|
101
|
+
|
|
102
|
+
By default, Haijun has access to the full [Haijun Code toolset](https://raw.haijun.my.id/docs/en/settings#tools-available-to-haijun) (Read, Write, Edit, Bash, and others). `allowed_tools` is a permission allowlist: listed tools are auto-approved, and unlisted tools fall through to `permission_mode` and `can_use_tool` for a decision. It does not remove tools from Haijun's toolset. To block specific tools, use `disallowed_tools`. See the [permissions guide](https://raw.haijun.my.id/docs/en/agent-sdk/permissions) for the full evaluation order.
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
options = HaijunAgentOptions(
|
|
106
|
+
allowed_tools=["Read", "Write", "Bash"], # auto-approve these tools
|
|
107
|
+
permission_mode='acceptEdits' # auto-accept file edits )
|
|
108
|
+
|
|
109
|
+
async for message in query(
|
|
110
|
+
prompt="Create a hello.py file",
|
|
111
|
+
options=options ):
|
|
112
|
+
# Process tool use and results
|
|
113
|
+
pass
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Working Directory
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
from pathlib import Path
|
|
120
|
+
|
|
121
|
+
options = HaijunAgentOptions(
|
|
122
|
+
cwd="/path/to/project" # or Path("/path/to/project")
|
|
123
|
+
)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## HaijunSDKClient
|
|
127
|
+
|
|
128
|
+
`HaijunSDKClient` supports bidirectional, interactive conversations with Haijun
|
|
129
|
+
Code. See [src/haijun_agent_sdk/client.py](src/haijun_agent_sdk/client.py).
|
|
130
|
+
|
|
131
|
+
Unlike `query()`, `HaijunSDKClient` additionally enables **custom tools** and **hooks**, both of which can be defined as Python functions.
|
|
132
|
+
|
|
133
|
+
### Custom Tools (as In-Process SDK MCP Servers)
|
|
134
|
+
|
|
135
|
+
A **custom tool** is a Python function that you can offer to Haijun, for Haijun to invoke as needed.
|
|
136
|
+
|
|
137
|
+
Custom tools are implemented in-process MCP servers that run directly within your Python application, eliminating the need for separate processes that regular MCP servers require.
|
|
138
|
+
|
|
139
|
+
For an end-to-end example, see [MCP Calculator](examples/mcp_calculator.py).
|
|
140
|
+
|
|
141
|
+
#### Creating a Simple Tool
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
from haijun_agent_sdk import tool, create_sdk_mcp_server, HaijunAgentOptions, HaijunSDKClient
|
|
145
|
+
|
|
146
|
+
# Define a tool using the @tool decorator
|
|
147
|
+
@tool("greet", "Greet a user", {"name": str})
|
|
148
|
+
async def greet_user(args):
|
|
149
|
+
return {
|
|
150
|
+
"content": [
|
|
151
|
+
{"type": "text", "text": f"Hello, {args['name']}!"}
|
|
152
|
+
]
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
# Create an SDK MCP server
|
|
156
|
+
server = create_sdk_mcp_server(
|
|
157
|
+
name="my-tools",
|
|
158
|
+
version="1.0.0",
|
|
159
|
+
tools=[greet_user]
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
# Use it with Haijun. allowed_tools pre-approves the tool so it runs
|
|
163
|
+
# without a permission prompt; it does not control tool availability.
|
|
164
|
+
options = HaijunAgentOptions(
|
|
165
|
+
mcp_servers={"tools": server},
|
|
166
|
+
allowed_tools=["mcp__tools__greet"]
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
async with HaijunSDKClient(options=options) as client:
|
|
170
|
+
await client.query("Greet Alice")
|
|
171
|
+
|
|
172
|
+
# Extract and print response
|
|
173
|
+
async for msg in client.receive_response():
|
|
174
|
+
print(msg)
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
#### Benefits Over External MCP Servers
|
|
178
|
+
|
|
179
|
+
- **No subprocess management** - Runs in the same process as your application
|
|
180
|
+
- **Better performance** - No IPC overhead for tool calls
|
|
181
|
+
- **Simpler deployment** - Single Python process instead of multiple
|
|
182
|
+
- **Easier debugging** - All code runs in the same process
|
|
183
|
+
- **Type safety** - Direct Python function calls with type hints
|
|
184
|
+
|
|
185
|
+
#### Migration from External Servers
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
# BEFORE: External MCP server (separate process)
|
|
189
|
+
options = HaijunAgentOptions(
|
|
190
|
+
mcp_servers={
|
|
191
|
+
"calculator": {
|
|
192
|
+
"type": "stdio",
|
|
193
|
+
"command": "python",
|
|
194
|
+
"args": ["-m", "calculator_server"]
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
# AFTER: SDK MCP server (in-process)
|
|
200
|
+
from my_tools import add, subtract # Your tool functions
|
|
201
|
+
|
|
202
|
+
calculator = create_sdk_mcp_server(
|
|
203
|
+
name="calculator",
|
|
204
|
+
tools=[add, subtract]
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
options = HaijunAgentOptions(
|
|
208
|
+
mcp_servers={"calculator": calculator}
|
|
209
|
+
)
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
#### Mixed Server Support
|
|
213
|
+
|
|
214
|
+
You can use both SDK and external MCP servers together:
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
options = HaijunAgentOptions(
|
|
218
|
+
mcp_servers={
|
|
219
|
+
"internal": sdk_server, # In-process SDK server
|
|
220
|
+
"external": { # External subprocess server
|
|
221
|
+
"type": "stdio",
|
|
222
|
+
"command": "external-server"
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
)
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Hooks
|
|
229
|
+
|
|
230
|
+
A **hook** is a Python function that the Haijun Code _application_ (_not_ Haijun) invokes at specific points of the Haijun agent loop. Hooks can provide deterministic processing and automated feedback for Haijun. Read more in [Intercept and control agent behavior with hooks](https://raw.haijun.my.id/docs/en/agent-sdk/hooks).
|
|
231
|
+
|
|
232
|
+
For more examples, see examples/hooks.py.
|
|
233
|
+
|
|
234
|
+
#### Example
|
|
235
|
+
|
|
236
|
+
```python
|
|
237
|
+
from haijun_agent_sdk import HaijunAgentOptions, HaijunSDKClient, HookMatcher
|
|
238
|
+
|
|
239
|
+
async def check_bash_command(input_data, tool_use_id, context):
|
|
240
|
+
tool_name = input_data["tool_name"]
|
|
241
|
+
tool_input = input_data["tool_input"]
|
|
242
|
+
if tool_name != "Bash":
|
|
243
|
+
return {}
|
|
244
|
+
command = tool_input.get("command", "")
|
|
245
|
+
block_patterns = ["foo.sh"]
|
|
246
|
+
for pattern in block_patterns:
|
|
247
|
+
if pattern in command:
|
|
248
|
+
return {
|
|
249
|
+
"hookSpecificOutput": {
|
|
250
|
+
"hookEventName": "PreToolUse",
|
|
251
|
+
"permissionDecision": "deny",
|
|
252
|
+
"permissionDecisionReason": f"Command contains invalid pattern: {pattern}",
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return {}
|
|
256
|
+
|
|
257
|
+
options = HaijunAgentOptions(
|
|
258
|
+
allowed_tools=["Bash"],
|
|
259
|
+
hooks={
|
|
260
|
+
"PreToolUse": [
|
|
261
|
+
HookMatcher(matcher="Bash", hooks=[check_bash_command]),
|
|
262
|
+
],
|
|
263
|
+
}
|
|
264
|
+
)
|
|
265
|
+
|
|
266
|
+
async with HaijunSDKClient(options=options) as client:
|
|
267
|
+
# Test 1: Command with forbidden pattern (will be blocked)
|
|
268
|
+
await client.query("Run the bash command: ./foo.sh --help")
|
|
269
|
+
async for msg in client.receive_response():
|
|
270
|
+
print(msg)
|
|
271
|
+
|
|
272
|
+
print("\n" + "=" * 50 + "\n")
|
|
273
|
+
|
|
274
|
+
# Test 2: Safe command that should work
|
|
275
|
+
await client.query("Run the bash command: echo 'Hello from hooks example!'")
|
|
276
|
+
async for msg in client.receive_response():
|
|
277
|
+
print(msg)
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## Types
|
|
281
|
+
|
|
282
|
+
See [src/haijun_agent_sdk/types.py](src/haijun_agent_sdk/types.py) for complete type definitions:
|
|
283
|
+
|
|
284
|
+
- `HaijunAgentOptions` - Configuration options
|
|
285
|
+
- `AssistantMessage`, `UserMessage`, `SystemMessage`, `ResultMessage` - Message types
|
|
286
|
+
- `TextBlock`, `ToolUseBlock`, `ToolResultBlock` - Content blocks
|
|
287
|
+
|
|
288
|
+
## Error Handling
|
|
289
|
+
|
|
290
|
+
```python
|
|
291
|
+
from haijun_agent_sdk import (
|
|
292
|
+
HaijunSDKError, # Base error
|
|
293
|
+
CLINotFoundError, # Haijun Code not installed
|
|
294
|
+
CLIConnectionError, # Connection issues
|
|
295
|
+
ProcessError, # Process failed
|
|
296
|
+
ResultError, # Run ended with an error result (subclass of ProcessError)
|
|
297
|
+
CLIJSONDecodeError, # JSON parsing issues )
|
|
298
|
+
|
|
299
|
+
try:
|
|
300
|
+
async for message in query(prompt="Hello"):
|
|
301
|
+
pass
|
|
302
|
+
except CLINotFoundError:
|
|
303
|
+
print("Please install Haijun Code")
|
|
304
|
+
except ResultError as e:
|
|
305
|
+
# The CLI reported a terminal error result (also yielded as the final
|
|
306
|
+
# ResultMessage) and exited. Structured fields are on the exception.
|
|
307
|
+
print(f"Run failed: {e.subtype=} {e.terminal_reason=} {e.api_error_status=}")
|
|
308
|
+
print(e.result or e.errors)
|
|
309
|
+
except ProcessError as e:
|
|
310
|
+
print(f"Process failed with exit code: {e.exit_code}")
|
|
311
|
+
except CLIJSONDecodeError as e:
|
|
312
|
+
print(f"Failed to parse response: {e}")
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
See [src/haijun_agent_sdk/\_errors.py](src/haijun_agent_sdk/_errors.py) for all error types.
|
|
316
|
+
|
|
317
|
+
## Available Tools
|
|
318
|
+
|
|
319
|
+
See the [Haijun Code documentation](https://raw.haijun.my.id/docs/en/settings#tools-available-to-haijun) for a complete list of available tools.
|
|
320
|
+
|
|
321
|
+
## Examples
|
|
322
|
+
|
|
323
|
+
See [examples/quick_start.py](examples/quick_start.py) for a complete working example.
|
|
324
|
+
|
|
325
|
+
See [examples/streaming_mode.py](examples/streaming_mode.py) for comprehensive examples involving `HaijunSDKClient`. You can even run interactive examples in IPython from [examples/streaming_mode_ipython.py](examples/streaming_mode_ipython.py).
|
|
326
|
+
|
|
327
|
+
## Migrating from Haijun Code SDK
|
|
328
|
+
|
|
329
|
+
If you're upgrading from the Haijun Code SDK (versions < 0.1.0), please see the [CHANGELOG.md](CHANGELOG.md#010) for details on breaking changes and new features, including:
|
|
330
|
+
|
|
331
|
+
- `HaijunCodeOptions` → `HaijunAgentOptions` rename
|
|
332
|
+
- Merged system prompt configuration
|
|
333
|
+
- Settings isolation and explicit control
|
|
334
|
+
- New programmatic subagents and session forking features
|
|
335
|
+
|
|
336
|
+
## Development
|
|
337
|
+
|
|
338
|
+
If you're contributing to this project, run the initial setup script to install git hooks:
|
|
339
|
+
|
|
340
|
+
```bash
|
|
341
|
+
./scripts/initial-setup.sh
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
This installs a pre-push hook that runs lint checks before pushing, matching the CI workflow. To skip the hook temporarily, use `git push --no-verify`.
|
|
345
|
+
|
|
346
|
+
### Building Wheels Locally
|
|
347
|
+
|
|
348
|
+
To build wheels with the bundled Haijun Code CLI:
|
|
349
|
+
|
|
350
|
+
```bash
|
|
351
|
+
# Install build dependencies
|
|
352
|
+
pip install build twine
|
|
353
|
+
|
|
354
|
+
# Build wheel with bundled CLI
|
|
355
|
+
python scripts/build_wheel.py
|
|
356
|
+
|
|
357
|
+
# Build with specific version
|
|
358
|
+
python scripts/build_wheel.py --version 0.1.4
|
|
359
|
+
|
|
360
|
+
# Build with specific CLI version
|
|
361
|
+
python scripts/build_wheel.py --cli-version 2.0.0
|
|
362
|
+
|
|
363
|
+
# Clean bundled CLI after building
|
|
364
|
+
python scripts/build_wheel.py --clean
|
|
365
|
+
|
|
366
|
+
# Skip CLI download (use existing)
|
|
367
|
+
python scripts/build_wheel.py --skip-download
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
The build script:
|
|
371
|
+
|
|
372
|
+
1. Downloads Haijun Code CLI for your platform
|
|
373
|
+
2. Bundles it in the wheel
|
|
374
|
+
3. Builds both wheel and source distribution
|
|
375
|
+
4. Checks the package with twine
|
|
376
|
+
|
|
377
|
+
See `python scripts/build_wheel.py --help` for all options.
|
|
378
|
+
|
|
379
|
+
### Release Workflow
|
|
380
|
+
|
|
381
|
+
The package is published to PyPI via the GitHub Actions workflow in `.github/workflows/publish.yml`. To create a new release:
|
|
382
|
+
|
|
383
|
+
1. **Trigger the workflow** manually from the Actions tab with two inputs:
|
|
384
|
+
- `version`: The package version to publish (e.g., `0.1.5`)
|
|
385
|
+
- `haijun_code_version`: The Haijun Code CLI version to bundle (e.g., `2.0.0` or `latest`)
|
|
386
|
+
|
|
387
|
+
2. **The workflow will**:
|
|
388
|
+
- Build platform-specific wheels for macOS, Linux, and Windows
|
|
389
|
+
- Bundle the specified Haijun Code CLI version in each wheel
|
|
390
|
+
- Build a source distribution
|
|
391
|
+
- Publish all artifacts to PyPI
|
|
392
|
+
- Create a release branch with version updates
|
|
393
|
+
- Open a PR to main with:
|
|
394
|
+
- Updated `pyproject.toml` version
|
|
395
|
+
- Updated `src/haijun_agent_sdk/_version.py`
|
|
396
|
+
- Updated `src/haijun_agent_sdk/_cli_version.py` with bundled CLI version
|
|
397
|
+
- Auto-generated `CHANGELOG.md` entry
|
|
398
|
+
|
|
399
|
+
3. **Review and merge** the release PR to update main with the new version information
|
|
400
|
+
|
|
401
|
+
The workflow tracks both the package version and the bundled CLI version separately, allowing you to release a new package version with an updated CLI without code changes.
|
|
402
|
+
|
|
403
|
+
## License and terms
|
|
404
|
+
|
|
405
|
+
Use of this SDK is governed by JuGloW's [Commercial Terms of Service](https://docs.juglow.my.id/legal/commercial-terms), including when you use it to power products and services that you make available to your own customers and end users, except to the extent a specific component or dependency is covered by a different license as indicated in that component's LICENSE file.
|