public class OpenCodeHttpClient extends Object implements AutoCloseable
Built on OkHttp; supports externally provided OkHttpClient instances for
connection pooling across plugins. All methods throw OpenCodeHttpException
on HTTP errors.
OpenCodeHttpClientConfig,
OpenCodeHttpException,
opencode server docs| Constructor and Description |
|---|
OpenCodeHttpClient(OpenCodeHttpClientConfig config,
com.fasterxml.jackson.databind.ObjectMapper objectMapper,
okhttp3.OkHttpClient httpClient)
创建 open code http client 实例,并按传入依赖确定资源所有权。
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
abortSession(String sessionId)
请求 OpenCode Server 中止指定会话中正在运行的生成任务。
|
McpStatus |
addMcpServer(String name,
Object config)
POST /mcp,动态添加 MCP server。 |
protected <T> T |
awaitFuture(CompletableFuture<T> future)
在同步兼容入口等待异步 HTTP 结果,并保留中断状态及 SDK 异常语义。
|
PromptResult |
chatCompletionWithSession(PromptRequest request,
String sessionKey)
复用或创建稳定会话后提交聊天请求并返回结果。
|
PromptResult |
chatCompletionWithSession(PromptRequest request,
String sessionKey,
HttpCallCancellation cancellation)
复用或创建稳定会话后提交聊天请求并返回结果。
|
CompletableFuture<Boolean> |
chatCompletionWithSessionAsync(PromptRequest request,
String sessionKey)
异步复用或创建稳定会话后提交聊天请求,不阻塞调用线程。
|
void |
close()
释放当前对象持有的连接、订阅或执行资源;重复调用是安全的。
|
Session |
createSession(String title)
通过 OpenCode Server HTTP API 创建会话。
|
Session |
createSession(String title,
OpenCodeRequestContext context)
通过 OpenCode Server HTTP API 创建会话。
|
Session |
createSession(String title,
OpenCodeRequestContext context,
HttpCallCancellation cancellation)
通过 OpenCode Server HTTP API 创建会话。
|
CompletableFuture<Session> |
createSessionAsync(String title,
OpenCodeRequestContext context,
HttpCallCancellation cancellation)
异步请求 OpenCode Server 创建会话;网络或解析失败通过返回的 Future 传播。
|
boolean |
deleteSession(String sessionId)
请求 OpenCode Server 删除会话。
|
boolean |
disposeInstance()
POST /instance/dispose,释放当前 instance。 |
String |
ensureSession(String sessionKey)
查找稳定会话键对应的会话;不存在时创建新会话。
|
String |
ensureSession(String sessionKey,
OpenCodeRequestContext context)
查找稳定会话键对应的会话;不存在时创建新会话。
|
String |
ensureSession(String sessionKey,
OpenCodeRequestContext context,
HttpCallCancellation cancellation)
查找稳定会话键对应的会话;不存在时创建新会话。
|
CompletableFuture<String> |
ensureSessionAsync(String sessionKey,
OpenCodeRequestContext context,
HttpCallCancellation cancellation)
异步查找或创建会话。
|
protected <T> CompletableFuture<T> |
executeAsync(okhttp3.Request request,
Class<T> type,
HttpCallCancellation cancellation)
使用 OkHttp enqueue 异步执行并反序列化对象。
|
List<FileSearchResult> |
find(String pattern)
GET /find,ripgrep 文本搜索。 |
List<String> |
findFiles(String query)
GET /find/file,按文件名/模式查找文件。 |
Optional<Session> |
findSessionByTitle(String title)
按 title 精确查找 session。
|
Optional<Session> |
findSessionByTitle(String title,
OpenCodeRequestContext context)
调用 OpenCode Server 搜索接口查找标题匹配的会话;未命中时返回空结果。
|
Optional<Session> |
findSessionByTitle(String title,
OpenCodeRequestContext context,
HttpCallCancellation cancellation)
调用 OpenCode Server 搜索接口查找标题匹配的会话;未命中时返回空结果。
|
List<Symbol> |
findSymbols(String query)
GET /find/symbol,通过 LSP 搜索工作区符号。 |
Session |
forkSession(String sessionId,
String messageId)
POST /session/:id/fork,从指定 message 处 fork session。 |
OpenCodeConfig |
getConfig()
GET /config,获取实例生效的配置。 |
ProviderList |
getConfigProviders()
GET /config/providers,获取已配置的 providers 列表与默认模型映射。 |
Project |
getCurrentProject()
GET /project/current,获取当前项目。 |
FileContent |
getFileContent(String path)
GET /file/content,读取文件内容。 |
List<FileNode> |
getFileStatus()
GET /file/status,获取项目所有文件的 git 状态。 |
OpenCodeConfig |
getGlobalConfig()
GET /global/config,获取全局配置。 |
MessageInfo |
getMessage(String sessionId,
String messageId)
GET /session/:id/message/:messageID,获取单条 message 详情。 |
List<PromptResult> |
getMessages(String sessionId)
通过 OpenCode Server HTTP API 获取会话消息。
|
com.fasterxml.jackson.databind.ObjectMapper |
getObjectMapper()
暴露 ObjectMapper 供转换器复用。
|
okhttp3.OkHttpClient |
getOkHttpClient()
暴露 OkHttpClient 供 SSE 客户端复用。
|
OpenCodePath |
getPath()
GET /path,获取 opencode 工作目录相关的所有路径。 |
Session |
getSession(String sessionId)
通过 OpenCode Server HTTP API 获取会话。
|
List<Session> |
getSessionChildren(String sessionId)
GET /session/:id/children,列出 session 的子(forked)session。 |
List<FileDiff> |
getSessionDiff(String sessionId,
String messageId)
GET /session/:id/diff,获取指定 message 之后的文件 diff。 |
Map<String,SessionStatus> |
getSessionStatusMap()
GET /session/status,获取所有 session 状态。 |
List<SessionTodo> |
getSessionTodo(String sessionId)
GET /session/:id/todo,获取 session 的 todo 列表。 |
VcsInfo |
getVcs()
GET /vcs,获取当前工作目录的 VCS 信息。 |
boolean |
globalDispose()
POST /global/dispose,释放所有 instance。 |
boolean |
globalUpgrade(String target)
POST /global/upgrade,升级 opencode。 |
HealthStatus |
health()
同步查询 OpenCode Server 的健康状态和版本信息。
|
CompletableFuture<HealthStatus> |
healthAsync()
异步检查 OpenCode Server 健康状态。
|
boolean |
initProjectGit()
POST /project/git/init,为当前项目初始化 git 仓库。 |
boolean |
initSession(String sessionId,
String messageId,
String providerId,
String modelId)
POST /session/:id/init,用首个 message 初始化 session(创建 AGENTS.md)。 |
List<Agent> |
listAgents()
通过 OpenCode Server HTTP API 查询智能体;无数据时返回空集合。
|
List<Command> |
listCommands()
GET /command,列出已注册的 slash commands。 |
List<FileNode> |
listFiles(String path)
GET /file,列出指定目录下的文件/目录树。 |
List<FormatterStatus> |
listFormatters()
GET /formatter,列出 formatter 状态。 |
List<LspStatus> |
listLsps()
GET /lsp,列出 LSP 服务器状态。 |
Map<String,McpStatus> |
listMcpServers()
GET /mcp,列出 MCP 服务器状态。 |
List<PermissionRequest> |
listPermissions()
GET /permission,列出所有待审批权限请求。 |
List<Project> |
listProjects()
GET /project,列出所有已知项目。 |
Map<String,List<ProviderAuthMethod>> |
listProviderAuthMethods()
GET /provider/auth,获取每个 provider 的可用认证方式。 |
ProviderList |
listProviders()
GET /provider,获取 providers 列表 + 默认 provider/model 映射 + 已连接 provider。 |
List<QuestionRequest> |
listQuestions()
GET /question,列出所有待回答的问题。 |
List<Session> |
listSessions()
通过 OpenCode Server HTTP API 查询会话;无数据时返回空集合。
|
List<Session> |
listSessions(String search,
Integer limit,
Integer start)
分页/过滤列出 sessions,对齐 Hermes
listSessions(limit, offset, source, includeChildren)。 |
List<Session> |
listSessions(String search,
Integer limit,
Integer start,
OpenCodeRequestContext context)
通过 OpenCode Server HTTP API 查询会话;无数据时返回空集合。
|
CompletableFuture<List<Session>> |
listSessionsAsync(String search,
Integer limit,
Integer start,
OpenCodeRequestContext context,
HttpCallCancellation cancellation)
异步分页列出会话。
|
List<Skill> |
listSkills()
GET /skill,列出已注册的 skills。 |
PromptResult |
prompt(String sessionId,
PromptRequest request)
向指定会话同步提交 Prompt,并等待 OpenCode Server 返回完整生成结果。
|
PromptResult |
prompt(String sessionId,
PromptRequest request,
HttpCallCancellation cancellation)
向指定会话同步提交 Prompt,并等待 OpenCode Server 返回完整生成结果。
|
CompletableFuture<Boolean> |
promptAsync(String sessionId,
PromptRequest request)
向指定会话提交无需等待正文的异步 Prompt 请求。
|
CompletableFuture<Boolean> |
promptAsync(String sessionId,
PromptRequest request,
OpenCodeRequestContext context)
向指定会话提交无需等待正文的异步 Prompt 请求。
|
CompletableFuture<PromptResult> |
promptCompletionAsync(String sessionId,
PromptRequest request,
HttpCallCancellation cancellation)
异步发送消息并等待完整 Prompt 结果。
|
ProviderAuthAuthorization |
providerOAuthAuthorize(String providerId,
String method)
POST /provider/:id/oauth/authorize,启动 provider OAuth 授权。 |
boolean |
providerOAuthCallback(String providerId,
String code)
POST /provider/:id/oauth/callback,处理 OAuth 回调。 |
CompletableFuture<Boolean> |
providerOAuthCallbackAsync(String providerId,
String code)
异步处理 provider OAuth 回调。
|
boolean |
rejectQuestion(String requestId)
POST /question/:id/reject,拒绝回答问题。 |
boolean |
removeAuth(String providerId)
DELETE /auth/:id,清除 provider 凭证。 |
boolean |
replyPermission(String requestId,
String response,
boolean remember)
POST /permission/:id/reply,回复权限请求。 |
boolean |
replyQuestion(String requestId,
List<String> answers)
POST /question/:id/reply,回复问题。 |
boolean |
revertSession(String sessionId,
String messageId,
String partId)
POST /session/:id/revert,回退到指定 message(可指定 part)。 |
PromptResult |
runSessionCommand(String sessionId,
String command,
String arguments,
String agent,
String model)
POST /session/:id/command,向 session 发送 slash command。 |
boolean |
setAuth(String providerId,
Object body)
PUT /auth/:id,设置 provider 凭证。 |
CompletableFuture<Boolean> |
setAuthAsync(String providerId,
Object body)
异步设置 provider 凭证。
|
Session |
shareSession(String sessionId)
POST /session/:id/share,创建可分享链接。 |
boolean |
summarizeSession(String sessionId,
String providerId,
String modelId)
POST /session/:id/summarize,对 session 进行 AI 摘要压缩。 |
boolean |
unrevertSession(String sessionId)
POST /session/:id/unrevert,撤销回退。 |
Session |
unshareSession(String sessionId)
DELETE /session/:id/share,取消分享。 |
OpenCodeConfig |
updateConfig(Object body)
PATCH /config,更新实例配置。 |
OpenCodeConfig |
updateGlobalConfig(Object body)
PATCH /global/config,更新全局配置。 |
Project |
updateProject(String projectId,
Object body)
PATCH /project/:id,更新项目(name / icon / commands 等)。 |
public OpenCodeHttpClient(OpenCodeHttpClientConfig config, com.fasterxml.jackson.databind.ObjectMapper objectMapper, okhttp3.OkHttpClient httpClient)
config - 客户端配置;不得为 nullobjectMapper - JSON 映射器;为 null 时使用 SDK 默认配置httpClient - 可复用的 OkHttp 客户端;为 null 时由 SDK 创建public HealthStatus health()
OpenCodeHttpException - HTTP 请求失败或响应无法解析public CompletableFuture<HealthStatus> healthAsync()
public Session createSession(String title)
title - 会话标题;用于展示和稳定会话查找OpenCodeHttpException - HTTP 请求失败或响应无法解析public Session createSession(String title, OpenCodeRequestContext context)
title - 会话标题;用于展示和稳定会话查找context - 请求上下文;为 null 时不附加目录头OpenCodeHttpException - HTTP 请求失败或响应无法解析public Session createSession(String title, OpenCodeRequestContext context, HttpCallCancellation cancellation)
title - 会话标题;用于展示和稳定会话查找context - 请求上下文;为 null 时不附加目录头cancellation - 取消信号;为 null 时不可由外部取消OpenCodeHttpException - HTTP 请求失败或响应无法解析public CompletableFuture<Session> createSessionAsync(String title, OpenCodeRequestContext context, HttpCallCancellation cancellation)
title - 会话标题;用于展示和稳定会话查找context - 请求上下文;为 null 时不附加目录头cancellation - 取消信号;为 null 时不可由外部取消public Session getSession(String sessionId)
sessionId - OpenCode 会话 ID;不得为空OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<Session> listSessions()
OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<Session> listSessions(String search, Integer limit, Integer start)
listSessions(limit, offset, source, includeChildren)。search - 标题搜索条件;为空时不限制limit - 最大返回数量;为 null 时使用服务端默认值start - 分页起始偏移;为 null 时从首项开始OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<Session> listSessions(String search, Integer limit, Integer start, OpenCodeRequestContext context)
search - 标题搜索条件;为空时不限制limit - 最大返回数量;为 null 时使用服务端默认值start - 分页起始偏移;为 null 时从首项开始context - 请求上下文;为 null 时不附加目录头OpenCodeHttpException - HTTP 请求失败或响应无法解析public CompletableFuture<List<Session>> listSessionsAsync(String search, Integer limit, Integer start, OpenCodeRequestContext context, HttpCallCancellation cancellation)
search - 标题搜索条件;为空时不限制limit - 最大返回数量;为 null 时使用服务端默认值start - 分页起始偏移;为 null 时从首项开始context - 请求上下文;为 null 时不附加目录头cancellation - 取消信号;为 null 时不可由外部取消public Optional<Session> findSessionByTitle(String title)
title - 会话标题;用于展示和稳定会话查找Optional.empty()OpenCodeHttpException - HTTP 请求失败或响应无法解析public Optional<Session> findSessionByTitle(String title, OpenCodeRequestContext context)
title - 会话标题;用于展示和稳定会话查找context - 请求上下文;为 null 时不附加目录头Optional.empty()OpenCodeHttpException - HTTP 请求失败或响应无法解析public Optional<Session> findSessionByTitle(String title, OpenCodeRequestContext context, HttpCallCancellation cancellation)
title - 会话标题;用于展示和稳定会话查找context - 请求上下文;为 null 时不附加目录头cancellation - 取消信号;为 null 时不可由外部取消Optional.empty()OpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean deleteSession(String sessionId)
sessionId - OpenCode 会话 ID;不得为空true,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public PromptResult prompt(String sessionId, PromptRequest request)
sessionId - OpenCode 会话 ID;不得为空request - 请求对象;不得为 nullOpenCodeHttpException - HTTP 请求失败或响应无法解析public PromptResult prompt(String sessionId, PromptRequest request, HttpCallCancellation cancellation)
sessionId - OpenCode 会话 ID;不得为空request - 请求对象;不得为 nullcancellation - 取消信号;为 null 时不可由外部取消OpenCodeHttpException - HTTP 请求失败或响应无法解析public CompletableFuture<PromptResult> promptCompletionAsync(String sessionId, PromptRequest request, HttpCallCancellation cancellation)
sessionId - OpenCode 会话 ID;不得为空request - 请求对象;不得为 nullcancellation - 取消信号;为 null 时不可由外部取消public PromptResult chatCompletionWithSession(PromptRequest request, String sessionKey)
request - 请求对象;不得为 nullsessionKey - 用于稳定复用会话的业务键;相同键映射到同一标题OpenCodeHttpException - HTTP 请求失败或响应无法解析public PromptResult chatCompletionWithSession(PromptRequest request, String sessionKey, HttpCallCancellation cancellation)
request - 请求对象;不得为 nullsessionKey - 用于稳定复用会话的业务键;相同键映射到同一标题cancellation - 取消信号;为 null 时不可由外部取消OpenCodeHttpException - HTTP 请求失败或响应无法解析public CompletableFuture<Boolean> chatCompletionWithSessionAsync(PromptRequest request, String sessionKey)
request - 请求对象;不得为 nullsessionKey - 用于稳定复用会话的业务键;相同键映射到同一标题public String ensureSession(String sessionKey)
sessionKey - 用于稳定复用会话的业务键;相同键映射到同一标题OpenCodeHttpException - HTTP 请求失败或响应无法解析public String ensureSession(String sessionKey, OpenCodeRequestContext context)
sessionKey - 用于稳定复用会话的业务键;相同键映射到同一标题context - 请求上下文;为 null 时不附加目录头OpenCodeHttpException - HTTP 请求失败或响应无法解析public String ensureSession(String sessionKey, OpenCodeRequestContext context, HttpCallCancellation cancellation)
sessionKey - 用于稳定复用会话的业务键;相同键映射到同一标题context - 请求上下文;为 null 时不附加目录头cancellation - 取消信号;为 null 时不可由外部取消OpenCodeHttpException - HTTP 请求失败或响应无法解析public CompletableFuture<String> ensureSessionAsync(String sessionKey, OpenCodeRequestContext context, HttpCallCancellation cancellation)
sessionKey - 用于稳定复用会话的业务键;相同键映射到同一标题context - 请求上下文;为 null 时不附加目录头cancellation - 取消信号;为 null 时不可由外部取消public CompletableFuture<Boolean> promptAsync(String sessionId, PromptRequest request)
sessionId - OpenCode 会话 ID;不得为空request - 请求对象;不得为 nullpublic CompletableFuture<Boolean> promptAsync(String sessionId, PromptRequest request, OpenCodeRequestContext context)
sessionId - OpenCode 会话 ID;不得为空request - 请求对象;不得为 nullcontext - 请求上下文;为 null 时不附加目录头public List<PromptResult> getMessages(String sessionId)
sessionId - OpenCode 会话 ID;不得为空OpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean abortSession(String sessionId)
sessionId - OpenCode 会话 ID;不得为空true,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public List<Agent> listAgents()
OpenCodeHttpException - HTTP 请求失败或响应无法解析public OpenCodeConfig getConfig()
GET /config,获取实例生效的配置。public OpenCodeConfig getGlobalConfig()
GET /global/config,获取全局配置。OpenCodeHttpException - HTTP 请求失败或响应无法解析public OpenCodeConfig updateConfig(Object body)
PATCH /config,更新实例配置。body - 提交给服务端的请求体OpenCodeHttpException - HTTP 请求失败或响应无法解析public OpenCodeConfig updateGlobalConfig(Object body)
PATCH /global/config,更新全局配置。body - 提交给服务端的请求体OpenCodeHttpException - HTTP 请求失败或响应无法解析public ProviderList getConfigProviders()
GET /config/providers,获取已配置的 providers 列表与默认模型映射。OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<Project> listProjects()
GET /project,列出所有已知项目。OpenCodeHttpException - HTTP 请求失败或响应无法解析public Project getCurrentProject()
GET /project/current,获取当前项目。OpenCodeHttpException - HTTP 请求失败或响应无法解析public Project updateProject(String projectId, Object body)
PATCH /project/:id,更新项目(name / icon / commands 等)。projectId - OpenCode 项目 ID;不得为空body - 提交给服务端的请求体OpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean initProjectGit()
POST /project/git/init,为当前项目初始化 git 仓库。true,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public ProviderList listProviders()
GET /provider,获取 providers 列表 + 默认 provider/model 映射 + 已连接 provider。OpenCodeHttpException - HTTP 请求失败或响应无法解析public Map<String,List<ProviderAuthMethod>> listProviderAuthMethods()
GET /provider/auth,获取每个 provider 的可用认证方式。OpenCodeHttpException - HTTP 请求失败或响应无法解析public ProviderAuthAuthorization providerOAuthAuthorize(String providerId, String method)
POST /provider/:id/oauth/authorize,启动 provider OAuth 授权。providerId - 模型提供方 IDmethod - 认证或升级方式OpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean providerOAuthCallback(String providerId, String code)
POST /provider/:id/oauth/callback,处理 OAuth 回调。providerId - 模型提供方 IDcode - OAuth 回调授权码true,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public CompletableFuture<Boolean> providerOAuthCallbackAsync(String providerId, String code)
providerId - 模型提供方 IDcode - OAuth 回调授权码public List<FileNode> listFiles(String path)
GET /file,列出指定目录下的文件/目录树。path - 文件或工作目录路径OpenCodeHttpException - HTTP 请求失败或响应无法解析public FileContent getFileContent(String path)
GET /file/content,读取文件内容。path - 文件或工作目录路径OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<FileNode> getFileStatus()
GET /file/status,获取项目所有文件的 git 状态。OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<FileSearchResult> find(String pattern)
GET /find,ripgrep 文本搜索。pattern - 文本搜索模式OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<String> findFiles(String query)
GET /find/file,按文件名/模式查找文件。query - 搜索或数据库查询表达式OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<Symbol> findSymbols(String query)
GET /find/symbol,通过 LSP 搜索工作区符号。query - 搜索或数据库查询表达式OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<Command> listCommands()
GET /command,列出已注册的 slash commands。OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<Skill> listSkills()
GET /skill,列出已注册的 skills。OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<FormatterStatus> listFormatters()
GET /formatter,列出 formatter 状态。OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<LspStatus> listLsps()
GET /lsp,列出 LSP 服务器状态。OpenCodeHttpException - HTTP 请求失败或响应无法解析public Map<String,McpStatus> listMcpServers()
GET /mcp,列出 MCP 服务器状态。OpenCodeHttpException - HTTP 请求失败或响应无法解析public McpStatus addMcpServer(String name, Object config)
POST /mcp,动态添加 MCP server。name - 资源名称config - 客户端配置;不得为 nullOpenCodeHttpException - HTTP 请求失败或响应无法解析public OpenCodePath getPath()
GET /path,获取 opencode 工作目录相关的所有路径。OpenCodeHttpException - HTTP 请求失败或响应无法解析public VcsInfo getVcs()
GET /vcs,获取当前工作目录的 VCS 信息。OpenCodeHttpException - HTTP 请求失败或响应无法解析public Map<String,SessionStatus> getSessionStatusMap()
GET /session/status,获取所有 session 状态。OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<Session> getSessionChildren(String sessionId)
GET /session/:id/children,列出 session 的子(forked)session。sessionId - OpenCode 会话 ID;不得为空OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<SessionTodo> getSessionTodo(String sessionId)
GET /session/:id/todo,获取 session 的 todo 列表。sessionId - OpenCode 会话 ID;不得为空OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<FileDiff> getSessionDiff(String sessionId, String messageId)
GET /session/:id/diff,获取指定 message 之后的文件 diff。sessionId - OpenCode 会话 ID;不得为空messageId - 会话消息 IDOpenCodeHttpException - HTTP 请求失败或响应无法解析public Session shareSession(String sessionId)
POST /session/:id/share,创建可分享链接。sessionId - OpenCode 会话 ID;不得为空OpenCodeHttpException - HTTP 请求失败或响应无法解析public Session unshareSession(String sessionId)
DELETE /session/:id/share,取消分享。sessionId - OpenCode 会话 ID;不得为空OpenCodeHttpException - HTTP 请求失败或响应无法解析public Session forkSession(String sessionId, String messageId)
POST /session/:id/fork,从指定 message 处 fork session。sessionId - OpenCode 会话 ID;不得为空messageId - 会话消息 IDOpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean initSession(String sessionId, String messageId, String providerId, String modelId)
POST /session/:id/init,用首个 message 初始化 session(创建 AGENTS.md)。sessionId - OpenCode 会话 ID;不得为空messageId - 会话消息 IDproviderId - 模型提供方 IDmodelId - 模型 IDtrue,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean summarizeSession(String sessionId, String providerId, String modelId)
POST /session/:id/summarize,对 session 进行 AI 摘要压缩。sessionId - OpenCode 会话 ID;不得为空providerId - 模型提供方 IDmodelId - 模型 IDtrue,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean revertSession(String sessionId, String messageId, String partId)
POST /session/:id/revert,回退到指定 message(可指定 part)。sessionId - OpenCode 会话 ID;不得为空messageId - 会话消息 IDpartId - 消息片段 IDtrue,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean unrevertSession(String sessionId)
POST /session/:id/unrevert,撤销回退。sessionId - OpenCode 会话 ID;不得为空true,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public MessageInfo getMessage(String sessionId, String messageId)
GET /session/:id/message/:messageID,获取单条 message 详情。sessionId - OpenCode 会话 ID;不得为空messageId - 会话消息 IDOpenCodeHttpException - HTTP 请求失败或响应无法解析public PromptResult runSessionCommand(String sessionId, String command, String arguments, String agent, String model)
POST /session/:id/command,向 session 发送 slash command。sessionId - OpenCode 会话 ID;不得为空command - OpenCode 斜杠命令或 CLI 子命令arguments - 传递给斜杠命令的参数文本;为空时不附加参数agent - 执行请求的智能体名称;为空时使用服务端默认智能体model - 模型标识,通常采用 provider/model 格式;为空时使用默认模型OpenCodeHttpException - HTTP 请求失败或响应无法解析public List<QuestionRequest> listQuestions()
GET /question,列出所有待回答的问题。OpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean replyQuestion(String requestId, List<String> answers)
POST /question/:id/reply,回复问题。requestId - 待回复的问题或权限请求 IDanswers - 按问题顺序提交的答案列表true,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean rejectQuestion(String requestId)
POST /question/:id/reject,拒绝回答问题。requestId - 待回复的问题或权限请求 IDtrue,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public List<PermissionRequest> listPermissions()
GET /permission,列出所有待审批权限请求。OpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean replyPermission(String requestId, String response, boolean remember)
POST /permission/:id/reply,回复权限请求。requestId - 待回复的问题或权限请求 IDresponse - 权限请求响应值,例如 allow 或 denyremember - 是否记住本次权限决定并应用于后续同类请求true,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean setAuth(String providerId, Object body)
PUT /auth/:id,设置 provider 凭证。providerId - 模型提供方 IDbody - 提交给服务端的请求体true,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public CompletableFuture<Boolean> setAuthAsync(String providerId, Object body)
providerId - 模型提供方 IDbody - 提交给服务端的请求体public boolean removeAuth(String providerId)
DELETE /auth/:id,清除 provider 凭证。providerId - 模型提供方 IDtrue,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean disposeInstance()
POST /instance/dispose,释放当前 instance。true,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean globalDispose()
POST /global/dispose,释放所有 instance。true,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public boolean globalUpgrade(String target)
POST /global/upgrade,升级 opencode。target 为 null 时升级到最新。target - 升级目标版本;为空时由 CLI 选择最新版本true,否则返回 falseOpenCodeHttpException - HTTP 请求失败或响应无法解析public okhttp3.OkHttpClient getOkHttpClient()
public com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
protected <T> CompletableFuture<T> executeAsync(okhttp3.Request request, Class<T> type, HttpCallCancellation cancellation)
T - 响应体反序列化后的目标类型request - 请求对象;不得为 nulltype - 目标 Java 类型,用于反序列化响应体cancellation - 取消信号;为 null 时不可由外部取消protected <T> T awaitFuture(CompletableFuture<T> future)
T - Future 正常完成时返回的值类型future - 需要同步等待的异步结果;异常完成时转换为 SDK 异常OpenCodeHttpException - HTTP 请求失败或响应无法解析public void close()
close in interface AutoCloseableCopyright © 2026. All rights reserved.