fix: stateful bootstrap, snapshot turn lookup, per-endpoint auth, stream metadata (v0.2.2) - #6
Conversation
…eam metadata
Close out the stateful path for the demo's production usage. Four changes:
1. Inject conversation_id (snake_case) into /v1/chat/completions body.
The OpenAI-compatible base spreads providerOptions.<name> into the
request body, stripping only its own schema keys. Replace
providerOptions.clarit with a clean snake_case body on every standard
generation and stream, plus the compatibility fallback. Engram keys
WARM-tier state by conversation_id, so this seeds state for the next
turn's restore on turn 1.
2. Fix off-by-one in the compatibility bridge snapshot lookup. The
caller's turnNumber is the turn this generation will produce; the
snapshot we want was saved at turnNumber - 1. /get_snapshot_info
requires an exact turn (no "latest"), so the arithmetic happens
there; the restore call now omits turn_number, letting the server
resolve it to the latest snapshot. When turnNumber is 0 or undefined
the bridge short-circuits to no-snapshot (no prior turn to restore
from).
3. Per-endpoint API key routing on the snapshot client. /v1/tokenize
takes the regular key; snapshot routes (save/list/get_info/restore/
delete) take the admin key. Provider factory wires both. Aligns with
Engram's intentional auth split and lets the temporary server-side
Option A patch be reverted.
4. Stream wrapper now surfaces clarit metadata. The standard streaming
path threads conversationId/turnNumber/branchName/rid and (when
autoSaveSnapshot is set) the save outcome into the finish part's
providerMetadata, so streamText sees {clarit:{...}} just like
generateText does. wrapGenerateResultAsStream (used by restore-and-
generate and the compatibility fast-path) also forwards
providerMetadata on its finish chunk.
Code Review SummaryStatus: No Issues Found | Recommendation: Merge This PR implements four targeted fixes for the stateful path. All changes are well-reasoned and the new tests provide good coverage. Changes Reviewed
Key Logic Verified
Test CoverageNew tests cover:
Files Reviewed (6 files)
Reviewed by minimax-m2.7 · 206,360 tokens |
Summary
Closes out the stateful path so the comparative demo can validate end-to-end. Four targeted fixes; all blockers are provider-side (server is confirmed done).
1. Inject
conversation_idon the generation request (bootstrap-critical)Engram keys WARM-tier state by
conversation_idon/v1/chat/completions. The compatibility fallback was generating viabaseModel.doGeneratewithout aconversation_id, so turn 1 never seeded WARM state and the next turn had nothing to restore. NewprepareBaseCallOptionshelper replacesproviderOptions.claritwith a clean snake_case body ({ conversation_id }) before delegating —@ai-sdk/openai-compatiblespreads that into the request body. Applied todoStandardGenerate,doStandardStream, anddoCompatibilityFallback.2. Fix off-by-one in compatibility bridge snapshot lookup
The caller's
turnNumberis the turn this generation will produce; the snapshot we want was saved at the end of the previous turn (turnNumber - 1)./get_snapshot_inforequires an exact turn, so the arithmetic happens there. The restore call now omitsturn_numberentirely — server resolves to the latest snapshot for the conversation, avoiding a second round of arithmetic. WhenturnNumberis 0 or undefined the bridge short-circuits tono-snapshot(no prior turn to restore from).3. Per-endpoint API keys
/v1/tokenizetakes the regular key; snapshot routes take the admin key. Provider factory wires both via the newtokenizeApiKeyon the snapshot client. Aligns with Engram's intentional auth split and lets the temporary server-side Option A patch be reverted.4. Stream wrapper preserves
providerMetadatadoStandardStreamnow threadsconversationId/turnNumber/branchName/ridand (whenautoSaveSnapshotis set) the save outcome into the finish part'sproviderMetadata, sostreamTextsees{clarit:{...}}just likegenerateTextdoes.wrapGenerateResultAsStream(restore-and-generate and compatibility fast-path) also forwardsproviderMetadataon its finish chunk.Test plan
npm test— 67 / 67 passing (was 56)npm run typecheck— cleannpm run build— clean (dist/ verified)conversation_idinjection ondoGenerateanddoStreamproviderOptions.clarittokenizeApiKey, snapshot endpoints use adminapiKeyclaritmetadata on finish partgenerateTextANDstreamText, fixedconversationId. ExpectsnapshotSaved:trueon turn 1, snapshot list grows, fast-path on turn ≥ 2, flat new-token count, streamText surfaces clarit metadata.This is the
Engram-Vercel-Providerrepo (separate fromClarit-AI/Engram); merge-gate from PR #95 does NOT apply.