fix(ai): bypass v2 SDK session methods for structured output (#110)#140
Draft
kumar-shivang wants to merge 1 commit into
Draft
fix(ai): bypass v2 SDK session methods for structured output (#110)#140kumar-shivang wants to merge 1 commit into
kumar-shivang wants to merge 1 commit into
Conversation
…ickernelz#110) PR tickernelz#101 switched structured-output generation to client.session.create / client.session.prompt / client.session.delete on @opencode-ai/sdk v2. That broke auto-capture on @opencode-ai/sdk v1.14.48: client.session has shifted class layouts across releases (the only-with-list() class in v1.14.48 vs. the real one as a renamed class on a different property path), so callers see 'session.create returned no session id' on every idle event. Switch to raw fetch against the documented server endpoints, which are stable across server versions: POST /session, POST /session/{id}/ message, DELETE /session/{id}. The base URL is captured when createV2Client(serverUrl) is called, so callers do not need to change. The wire format (body shape, query params, error parsing) matches what tests/opencode-provider.test.ts already mocked, and we add new regression tests covering directory forwarding, trailing-slash normalization, URL-object base URL, non-2xx responses, and network errors. Ref: tickernelz#110
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PR #101 switched structured-output generation in
opencode-provider.tstoclient.session.create / client.session.prompt / client.session.deleteon@opencode-ai/sdkv2. On@opencode-ai/sdk@1.14.48this regresses auto-capture100% of the time: the only
Sessionclass the published bundle exposes atclient.sessionhas shifted across releases (in v1.14.48 it is a thinexperimental class with only
list(); the realcreate/prompt/deleteliveon a differently-named class reachable via a different property path), so the
client.session.create(...)call returns undefined andcreated?.data?.idisundefined, triggering:on every idle event. See #110.
Fix
Stop using the SDK's session methods for these three calls. Hit the
documented server endpoints directly with raw
fetch:POST /sessionPOST /session/{id}/messageDELETE /session/{id}These paths are stable across server versions and match exactly what
tests/opencode-provider.test.tsalready mocks, so the existing test suiteis preserved.
The base URL is captured when
createV2Client(serverUrl)is called andcached in a module-level
_v2BaseUrl; theclientargument onStructuredOutputOptionsis retained for API compatibility but is nolonger used. The
setV2Client/getV2Client/createV2Clientexportsare preserved so the rest of the codebase (e.g.
src/index.ts,src/services/auto-capture.ts,src/services/user-memory-learning.ts) isunaffected.
Tests
bun run typecheck— cleanbun test— 162 pass / 0 fail (was 154 baseline; +8 new tests)bunx prettier --checkon changed files — cleanNew tests in
tests/opencode-provider.test.ts(thegenerateStructuredOutput regression tests (issue #110)block) cover whatthe previous suite did not:
directoryis forwarded as a percent-encoded query param on all threecalls (create, prompt, delete).
directoryis omitted (no empty?directory=) when not provided.//sessiondoubleslash.
createV2Clientaccepts aURLobject (not just a string).POST /sessionsurfaces a clear error and skipsprompt/delete.
POST /session/{id}/messagesurfaces the errorand still runs the best-effort
DELETE.fetchrejection) fromPOST /sessionpropagates andskips prompt/delete.
generateStructuredOutput,createV2Client) ispreserved.
Notes
they stub
globalThis.fetch, and the v2 SDK's underlying hey-api clientultimately calls
globalThis.fetch. So the test suite stayed green eventhough the production call path was broken — the fix here is verified
by reproducing the same
globalThis.fetchmock in new tests.fetch) is whatthis PR implements. Option 1 (wait for the SDK to publish the methods)
is unnecessary once we talk to the server directly.
Refs