Package io.github.easy4j.opencode.api
Class OpenCodeChatClient
java.lang.Object
io.github.easy4j.opencode.api.OpenCodeHttpClient
io.github.easy4j.opencode.api.OpenCodeChatClient
- All Implemented Interfaces:
AutoCloseable
OpenCode 聊天场景客户端,统一提供完整响应与流式响应。
普通 Prompt/Chat 调用继承自 OpenCodeHttpClient;事件订阅是流式聊天的
内部实现细节,业务调用方通过本类即可完成两种响应模式。
- Since:
- 1.0.0
- Author:
- Loong Wan
-
Constructor Summary
ConstructorsConstructorDescription创建 open code chat client 实例,并按传入依赖确定资源所有权。OpenCodeChatClient(OpenCodeHttpClientConfig config, com.fasterxml.jackson.databind.ObjectMapper objectMapper, okhttp3.OkHttpClient httpClient) 创建 open code chat client 实例,并按传入依赖确定资源所有权。OpenCodeChatClient(OpenCodeHttpClientConfig config, com.fasterxml.jackson.databind.ObjectMapper objectMapper, okhttp3.OkHttpClient httpClient, OpenCodeSseClient sseClient) 创建 open code chat client 实例,并按传入依赖确定资源所有权。 -
Method Summary
Modifier and TypeMethodDescriptionchatCompletion(String sessionId, ChatRequest request) 同步提交聊天请求并返回完整响应。chatCompletionAsync(String sessionId, ChatRequest request) 异步完成指定会话的聊天请求。chatCompletionStream(ChatRequest request, String sessionKey) 创建流式聊天响应,异步订阅会话事件并持续交付文本增量。chatCompletionStream(ChatRequest request, String sessionKey, OpenCodeRequestContext context) 创建流式聊天响应,异步订阅会话事件并持续交付文本增量。chatCompletionStream(ChatRequest request, String sessionKey, OpenCodeRequestContext context, Consumer<String> deltaConsumer) 在事件订阅启动前绑定增量回调,避免丢失首批分片。chatCompletionWithSession(ChatRequest request, String sessionKey) 复用或创建稳定会话后提交聊天请求并返回结果。chatCompletionWithSession(ChatRequest request, String sessionKey, HttpCallCancellation cancellation) 复用或创建稳定会话后提交聊天请求并返回结果。chatCompletionWithSessionAsync(ChatRequest request, String sessionKey, HttpCallCancellation cancellation) 异步查找会话并完成聊天请求。voidclose()释放当前对象持有的连接、订阅或执行资源;重复调用是安全的。Methods inherited from class io.github.easy4j.opencode.api.OpenCodeHttpClient
abortSession, addMcpServer, awaitFuture, chatCompletionWithSession, chatCompletionWithSession, chatCompletionWithSessionAsync, createSession, createSession, createSession, createSessionAsync, deleteSession, disposeInstance, ensureSession, ensureSession, ensureSession, ensureSessionAsync, executeAsync, find, findFiles, findSessionByTitle, findSessionByTitle, findSessionByTitle, findSymbols, forkSession, getConfig, getConfigProviders, getCurrentProject, getFileContent, getFileStatus, getGlobalConfig, getMessage, getMessages, getObjectMapper, getOkHttpClient, getPath, getSession, getSessionChildren, getSessionDiff, getSessionStatusMap, getSessionTodo, getVcs, globalDispose, globalUpgrade, health, healthAsync, initProjectGit, initSession, listAgents, listCommands, listFiles, listFormatters, listLsps, listMcpServers, listPermissions, listProjects, listProviderAuthMethods, listProviders, listQuestions, listSessions, listSessions, listSessions, listSessionsAsync, listSkills, prompt, prompt, promptAsync, promptAsync, promptCompletionAsync, providerOAuthAuthorize, providerOAuthCallback, providerOAuthCallbackAsync, rejectQuestion, removeAuth, replyPermission, replyQuestion, revertSession, runSessionCommand, setAuth, setAuthAsync, shareSession, summarizeSession, unrevertSession, unshareSession, updateConfig, updateGlobalConfig, updateProject
-
Constructor Details
-
OpenCodeChatClient
创建 open code chat client 实例,并按传入依赖确定资源所有权。- Parameters:
config- 客户端配置;不得为null
-
OpenCodeChatClient
public OpenCodeChatClient(OpenCodeHttpClientConfig config, com.fasterxml.jackson.databind.ObjectMapper objectMapper, okhttp3.OkHttpClient httpClient) 创建 open code chat client 实例,并按传入依赖确定资源所有权。- Parameters:
config- 客户端配置;不得为nullobjectMapper- JSON 映射器;为null时使用 SDK 默认配置httpClient- 可复用的 OkHttp 客户端;为null时由 SDK 创建
-
OpenCodeChatClient
public OpenCodeChatClient(OpenCodeHttpClientConfig config, com.fasterxml.jackson.databind.ObjectMapper objectMapper, okhttp3.OkHttpClient httpClient, OpenCodeSseClient sseClient) 创建 open code chat client 实例,并按传入依赖确定资源所有权。- Parameters:
config- 客户端配置;不得为nullobjectMapper- JSON 映射器;为null时使用 SDK 默认配置httpClient- 可复用的 OkHttp 客户端;为null时由 SDK 创建sseClient- SSE 客户端;为null时按配置创建
-
-
Method Details
-
chatCompletion
同步提交聊天请求并返回完整响应。- Parameters:
sessionId- OpenCode 会话 ID;不得为空request- 请求对象;不得为null- Returns:
- OpenCode SDK 返回的聊天响应对象
- Throws:
OpenCodeHttpException- HTTP 请求失败或响应无法解析
-
chatCompletionAsync
异步完成指定会话的聊天请求。- Parameters:
sessionId- OpenCode 会话 ID;不得为空request- 请求对象;不得为null- Returns:
- 异步结果;失败时以异常方式完成
-
chatCompletionWithSession
复用或创建稳定会话后提交聊天请求并返回结果。- Parameters:
request- 请求对象;不得为nullsessionKey- 用于稳定复用会话的业务键;相同键映射到同一标题- Returns:
- OpenCode SDK 返回的聊天响应对象
- Throws:
OpenCodeHttpException- HTTP 请求失败或响应无法解析
-
chatCompletionWithSession
public ChatResponse chatCompletionWithSession(ChatRequest request, String sessionKey, HttpCallCancellation cancellation) 复用或创建稳定会话后提交聊天请求并返回结果。- Parameters:
request- 请求对象;不得为nullsessionKey- 用于稳定复用会话的业务键;相同键映射到同一标题cancellation- 取消信号;为null时不可由外部取消- Returns:
- OpenCode SDK 返回的聊天响应对象
- Throws:
OpenCodeHttpException- HTTP 请求失败或响应无法解析
-
chatCompletionWithSessionAsync
public CompletableFuture<ChatResponse> chatCompletionWithSessionAsync(ChatRequest request, String sessionKey, HttpCallCancellation cancellation) 异步查找会话并完成聊天请求。- Parameters:
request- 请求对象;不得为nullsessionKey- 用于稳定复用会话的业务键;相同键映射到同一标题cancellation- 取消信号;为null时不可由外部取消- Returns:
- 异步结果;失败时以异常方式完成
-
chatCompletionStream
创建流式聊天响应,异步订阅会话事件并持续交付文本增量。- Parameters:
request- 请求对象;不得为nullsessionKey- 用于稳定复用会话的业务键;相同键映射到同一标题- Returns:
- 可观察文本增量、等待完成或主动取消的流式响应
-
chatCompletionStream
public StreamingChatResponse chatCompletionStream(ChatRequest request, String sessionKey, OpenCodeRequestContext context) 创建流式聊天响应,异步订阅会话事件并持续交付文本增量。- Parameters:
request- 请求对象;不得为nullsessionKey- 用于稳定复用会话的业务键;相同键映射到同一标题context- 请求上下文;为null时不附加目录头- Returns:
- 可观察文本增量、等待完成或主动取消的流式响应
-
chatCompletionStream
public StreamingChatResponse chatCompletionStream(ChatRequest request, String sessionKey, OpenCodeRequestContext context, Consumer<String> deltaConsumer) 在事件订阅启动前绑定增量回调,避免丢失首批分片。- Parameters:
request- 请求对象;不得为nullsessionKey- 用于稳定复用会话的业务键;相同键映射到同一标题context- 请求上下文;为null时不附加目录头deltaConsumer- 文本增量消费者;不得为null- Returns:
- 可观察文本增量、等待完成或主动取消的流式响应
-
close
public void close()释放当前对象持有的连接、订阅或执行资源;重复调用是安全的。- Specified by:
closein interfaceAutoCloseable- Overrides:
closein classOpenCodeHttpClient
-