Skip to content

Commit 0165419

Browse files
committed
init: opencode-java-sdk skeleton with HTTP/SSE/CLI channels
0 parents  commit 0165419

25 files changed

Lines changed: 1876 additions & 0 deletions

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*
24+
replay_pid*

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 luffy
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.

README.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# opencode-java-sdk
2+
3+
纯 Java 库(无 Spring):通过 HTTP REST API 和本地 CLI 与 [OpenCode](https://opencode.ai) Server 交互。
4+
5+
- **HTTP Server**:通过 `opencode serve` 启动的 REST API 进行会话管理、prompt 发送、agent 查询等
6+
- **SSE 事件流**:消费 `GET /event` 实时事件
7+
- **本地 CLI**:封装 `opencode run``opencode session` 等子命令
8+
9+
三条通道互不降级。入口类 [`OpenCodeClient`](src/main/java/io/github/hiwepy/opencode/OpenCodeClient.java)
10+
11+
Spring Boot 应用请使用 [opencode-spring-boot-starter](../opencode-spring-boot-starter)
12+
13+
## 快速开始
14+
15+
```java
16+
OpenCodeClientConfig config = new OpenCodeClientConfig();
17+
config.setServerUrl("http://localhost:4096");
18+
config.setPassword("your-password"); // OPENCODE_SERVER_PASSWORD
19+
20+
OpenCodeClient client = new OpenCodeClient(config);
21+
22+
// 健康检查
23+
HealthStatus health = client.health();
24+
System.out.println("version: " + health.getVersion());
25+
26+
// 创建会话
27+
Session session = client.createSession("my-task");
28+
29+
// 发送 prompt 并等待响应
30+
PromptResult result = client.prompt(session.getId(), "Explain how closures work in JavaScript");
31+
System.out.println(result.getTextContent());
32+
33+
// 异步发送(不等待)
34+
client.promptAsync(session.getId(), "Write a hello world in Python");
35+
36+
// 列出 agents
37+
List<Agent> agents = client.listAgents();
38+
39+
client.close();
40+
```
41+
42+
## HTTP Server API 映射
43+
44+
| Java 方法 | HTTP API | 说明 |
45+
|-----------|----------|------|
46+
| `health()` | `GET /global/health` | 健康检查 |
47+
| `createSession(title)` | `POST /session` | 创建会话 |
48+
| `getSession(id)` | `GET /session/:id` | 获取会话 |
49+
| `listSessions()` | `GET /session` | 列出会话 |
50+
| `deleteSession(id)` | `DELETE /session/:id` | 删除会话 |
51+
| `prompt(sessionId, request)` | `POST /session/:id/message` | 发送 prompt,同步等待 |
52+
| `promptAsync(sessionId, request)` | `POST /session/:id/prompt_async` | 异步发送,不等待 |
53+
| `getMessages(sessionId)` | `GET /session/:id/message` | 获取消息历史 |
54+
| `abort(sessionId)` | `POST /session/:id/abort` | 中止会话 |
55+
| `listAgents()` | `GET /agent` | 列出 agents |
56+
57+
完整 API 文档:https://opencode.ai/docs/server/
58+
59+
## SSE 事件流
60+
61+
```java
62+
OpenCodeSseClient sse = client.sse();
63+
sse.subscribe(event -> {
64+
System.out.println("event: " + event.getType());
65+
});
66+
67+
// 或使用阻塞队列
68+
BlockingQueue<Event> queue = sse.subscribeQueue();
69+
Event event = queue.take();
70+
```
71+
72+
## CLI 封装
73+
74+
```java
75+
OpenCodeCli cli = client.cli();
76+
77+
// 非交互模式执行
78+
OpenCodeCliResult result = cli.run("Explain async/await in JavaScript");
79+
System.out.println(result.getStdout());
80+
81+
// 指定模型
82+
cli.run("Hello", "anthropic/claude-sonnet-4-5");
83+
84+
// JSON 格式输出
85+
OpenCodeCliResult jsonResult = cli.runJson("Hello");
86+
87+
// 会话管理
88+
cli.sessionList();
89+
cli.sessionDelete("session-id");
90+
91+
// 其他命令
92+
cli.agentList();
93+
cli.models();
94+
cli.mcpList();
95+
cli.authList();
96+
```
97+
98+
## 配置
99+
100+
`OpenCodeClientConfig` 字段:
101+
102+
| 字段 | 默认值 | 说明 |
103+
|------|--------|------|
104+
| `serverUrl` | `http://localhost:4096` | OpenCode Server 地址 |
105+
| `username` | `opencode` | HTTP Basic Auth 用户名 |
106+
| `password` | `null` | HTTP Basic Auth 密码(`OPENCODE_SERVER_PASSWORD`|
107+
| `connectTimeoutMillis` | `15000` | 连接超时(毫秒) |
108+
| `readTimeoutMillis` | `300000` | 读超时(毫秒) |
109+
| `verifySsl` | `true` | 是否校验 HTTPS 证书 |
110+
| `localExecutable` | `opencode` | CLI 可执行文件路径 |
111+
| `localTimeoutSeconds` | `300` | CLI 命令超时(秒) |
112+
| `defaultModel` | `null` | 默认模型(`provider/model`|
113+
| `defaultAgent` | `null` | 默认 agent |
114+
115+
## 认证
116+
117+
OpenCode Server 支持 HTTP Basic Auth,通过环境变量配置:
118+
119+
```bash
120+
OPENCODE_SERVER_PASSWORD=your-password opencode serve
121+
```
122+
123+
Java 端对应 `OpenCodeClientConfig``username``password` 字段。
124+
125+
## 前置条件
126+
127+
1. 安装 OpenCode:`curl -fsSL https://opencode.ai/install | bash`
128+
2. 启动 Server:`opencode serve --port 4096`
129+
3. 配置 provider API key:`opencode auth login`
130+
131+
## 发布与 JDK
132+
133+
- 本模块要求 **JDK 17**
134+
- 发布快照/正式版:
135+
136+
```bash
137+
mvn clean deploy -DskipTests
138+
```

maven/maven-wrapper.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#Maven download properties
2+
#Fri Dec 01 21:36:18 MST 2017
3+
distributionUrl=https\://repository.apache.org/content/repositories/releases/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip

0 commit comments

Comments
 (0)