3030public class OpenCodeHttpClient implements AutoCloseable {
3131
3232 private static final MediaType JSON = MediaType .get ("application/json; charset=utf-8" );
33+ private static final String HEADER_OPENCODE_DIRECTORY = "X-OpenCode-Directory" ;
3334
3435 private final OpenCodeHttpClientConfig config ;
3536 private final OkHttpClient httpClient ;
@@ -66,8 +67,12 @@ public HealthStatus health() {
6667 // ============================================================
6768
6869 public Session createSession (String title ) {
70+ return createSession (title , null );
71+ }
72+
73+ public Session createSession (String title , OpenCodeRequestContext context ) {
6974 Map <String , Object > body = title != null ? Collections .singletonMap ("title" , title ) : Collections .emptyMap ();
70- return post ("/session" , body , Session .class );
75+ return post ("/session" , body , Session .class , context );
7176 }
7277
7378 public Session getSession (String sessionId ) {
@@ -87,22 +92,31 @@ public List<Session> listSessions() {
8792 * @return 匹配的 session 列表
8893 */
8994 public List <Session > listSessions (String search , Integer limit , Integer start ) {
95+ return listSessions (search , limit , start , null );
96+ }
97+
98+ public List <Session > listSessions (String search , Integer limit , Integer start ,
99+ OpenCodeRequestContext context ) {
90100 HttpUrl .Builder urlBuilder = HttpUrl .get (url ("/session" )).newBuilder ();
91101 if (search != null ) urlBuilder .addQueryParameter ("search" , search );
92102 if (limit != null ) urlBuilder .addQueryParameter ("limit" , String .valueOf (limit ));
93103 if (start != null ) urlBuilder .addQueryParameter ("start" , String .valueOf (start ));
94- Request request = authedRequest (urlBuilder .build ().toString ()).get ().build ();
104+ Request request = authedRequest (urlBuilder .build ().toString (), context ).get ().build ();
95105 return executeList (request , new TypeReference <List <Session >>() {});
96106 }
97107
98108 /**
99109 * 按 title 精确查找 session。
100110 */
101111 public Optional <Session > findSessionByTitle (String title ) {
112+ return findSessionByTitle (title , null );
113+ }
114+
115+ public Optional <Session > findSessionByTitle (String title , OpenCodeRequestContext context ) {
102116 if (title == null || title .isEmpty ()) {
103117 return Optional .empty ();
104118 }
105- return listSessions (title , 50 , null ).stream ()
119+ return listSessions (title , 50 , null , context ).stream ()
106120 .filter (s -> Objects .equals (title , s .getTitle ()))
107121 .findFirst ();
108122 }
@@ -136,22 +150,30 @@ public boolean chatCompletionWithSessionAsync(PromptRequest request, String sess
136150 }
137151
138152 public String ensureSession (String sessionKey ) {
153+ return ensureSession (sessionKey , null );
154+ }
155+
156+ public String ensureSession (String sessionKey , OpenCodeRequestContext context ) {
139157 try {
140- Optional <Session > existing = findSessionByTitle (sessionKey );
158+ Optional <Session > existing = findSessionByTitle (sessionKey , context );
141159 if (existing .isPresent ()) {
142160 return existing .get ().getId ();
143161 }
144162 } catch (Exception e ) {
145163 log .debug ("findSessionByTitle failed, sessionKey={}, error={}" , sessionKey , e .getMessage ());
146164 }
147- Session session = createSession (sessionKey );
165+ Session session = createSession (sessionKey , context );
148166 return session .getId ();
149167 }
150168
151169 public boolean promptAsync (String sessionId , PromptRequest request ) {
170+ return promptAsync (sessionId , request , null );
171+ }
172+
173+ public boolean promptAsync (String sessionId , PromptRequest request , OpenCodeRequestContext context ) {
152174 try {
153175 RequestBody body = RequestBody .create (objectMapper .writeValueAsBytes (request ), JSON );
154- Request httpReq = new Request . Builder (). url (url ( "/session/" + sessionId + "/prompt_async" ))
176+ Request httpReq = authedRequest ( url ("/session/" + sessionId + "/prompt_async" ), context )
155177 .post (body ).build ();
156178 try (Response response = httpClient .newCall (httpReq ).execute ()) {
157179 return response .isSuccessful ();
@@ -206,12 +228,20 @@ private String url(String path) {
206228 }
207229
208230 private Request .Builder authedRequest (String url ) {
231+ return authedRequest (url , null );
232+ }
233+
234+ private Request .Builder authedRequest (String url , OpenCodeRequestContext context ) {
209235 Request .Builder builder = new Request .Builder ().url (url );
210236 String password = config .resolvePassword ();
211237 if (!password .isEmpty ()) {
212238 String credential = Credentials .basic (config .getUsername (), password );
213239 builder .header ("Authorization" , credential );
214240 }
241+ if (Objects .nonNull (context ) && context .getDirectory () != null
242+ && !context .getDirectory ().trim ().isEmpty ()) {
243+ builder .header (HEADER_OPENCODE_DIRECTORY , context .getDirectory ());
244+ }
215245 return builder ;
216246 }
217247
@@ -228,7 +258,11 @@ private <T> T getList(String path, TypeReference<T> typeRef) {
228258
229259
230260 private <T > T post (String path , Object body , Class <T > type ) {
231- Request request = authedRequest (url (path ))
261+ return post (path , body , type , null );
262+ }
263+
264+ private <T > T post (String path , Object body , Class <T > type , OpenCodeRequestContext context ) {
265+ Request request = authedRequest (url (path ), context )
232266 .post (RequestBody .create (toJson (body ), JSON ))
233267 .build ();
234268 return execute (request , type );
0 commit comments