mirror of
https://github.com/wu736139669/hapi.git
synced 2026-12-04 06:57:55 +00:00
feat(opencode): support OpenCode v2 alongside v1 sessions (#2042)
This commit is contained in:
@@ -37,6 +37,19 @@ export type OpencodeEventStreamOptions = {
|
||||
* web timeline folds), not as a failure — see its call site.
|
||||
*/
|
||||
onRetry: (retry: OpencodeRetryStatus) => void;
|
||||
/**
|
||||
* Extra request headers merged into every (re)connect attempt. Required
|
||||
* for opencode v2: the internal server is Basic-auth protected, and a
|
||||
* headerless subscription is rejected with 401 before any event arrives.
|
||||
*/
|
||||
headers?: Record<string, string>;
|
||||
/**
|
||||
* Wire dialect. v2 (measured 2026-10-16 against opencode 2.0.29): the
|
||||
* stream lives at `/api/event` (the v1 `/event` path answers with the
|
||||
* SPA HTML fallback) and every JSON payload wraps its v1-shaped body in
|
||||
* a `data` envelope (`{ id, type, data: { … } }`). Default v1.
|
||||
*/
|
||||
mode?: 'v1' | 'v2';
|
||||
fetchImpl?: FetchLike;
|
||||
sleep?: (ms: number) => Promise<void>;
|
||||
};
|
||||
@@ -226,6 +239,8 @@ export function subscribeToOpencodeEvents(options: OpencodeEventStreamOptions):
|
||||
directory,
|
||||
sessionId,
|
||||
onRetry,
|
||||
headers = {},
|
||||
mode = 'v1',
|
||||
fetchImpl = fetch as FetchLike,
|
||||
sleep = (ms: number) => new Promise<void>((resolve) => {
|
||||
const timer = setTimeout(resolve, ms);
|
||||
@@ -233,7 +248,7 @@ export function subscribeToOpencodeEvents(options: OpencodeEventStreamOptions):
|
||||
})
|
||||
} = options;
|
||||
|
||||
const url = `${baseUrl}/event?directory=${encodeURIComponent(directory)}`;
|
||||
const url = `${baseUrl}${mode === 'v2' ? '/api/event' : '/event'}?directory=${encodeURIComponent(directory)}`;
|
||||
const controller = new AbortController();
|
||||
let closed = false;
|
||||
// Structurally typed to just what close() needs: the global and
|
||||
@@ -348,6 +363,18 @@ export function subscribeToOpencodeEvents(options: OpencodeEventStreamOptions):
|
||||
logger.debug('[opencode-events] ignoring unparsable event payload', error);
|
||||
return;
|
||||
}
|
||||
// v2 wraps the v1-shaped body in an envelope: { id, type, data },
|
||||
// where `type` lives on the ENVELOPE and `data` carries only the
|
||||
// body (measured: { id, type: 'session.status', data: { sessionID,
|
||||
// status } }). Re-attach the envelope type so the downstream v1
|
||||
// handling (event.type + properties) works unchanged on both
|
||||
// dialects; unwrapping `data` alone would strip the discriminator
|
||||
// and every retry event would be silently rejected. The v2 body also
|
||||
// carries its fields directly (no v1 `properties` wrapper), so the
|
||||
// body becomes `properties`.
|
||||
if (mode === 'v2' && isObject(payload) && isObject(payload.data)) {
|
||||
payload = { type: payload.type, properties: payload.data };
|
||||
}
|
||||
// Dispatched outside the parse guard on purpose: everything
|
||||
// downstream of handleEvent is HAPI's own code (the launcher's
|
||||
// report, the hub client), and a throw from there is a bug in this
|
||||
@@ -425,6 +452,7 @@ export function subscribeToOpencodeEvents(options: OpencodeEventStreamOptions):
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
accept: 'text/event-stream',
|
||||
...headers,
|
||||
...(lastEventId ? { 'last-event-id': lastEventId } : {})
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user