fix(core): log streaming LLM calls to database #6296
Merged
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
Streaming model calls were not being logged to the database because the logging code was placed after the
returnstatement in the streaming path. This caused LLM calls (TEXT_SMALL, TEXT_LARGE) to not appear in the Model Calls panel.Changes
packages/core/src/runtime.ts
logModelCall()helper method to avoid duplicationpackages/client/src/hooks/use-query-hooks.ts
useModel:*andactiontypespackages/client/src/components/agent-action-viewer.tsx
REASONINGtype support in model usage categorizationgetModelUsageType()functionpackages/core/src/tests/runtime-streaming.test.ts
Test plan
bun test packages/core/src/__tests__/runtime-streaming.test.ts- 13 tests passScreenshots
Before: Only embeddings and events visible, LLM calls missing
After:
Greptile Summary
Fixed critical bug where streaming LLM calls (TEXT_SMALL, TEXT_LARGE) were not being logged to the database because the logging code was unreachable after the
returnstatement in the streaming path. The fix extracts logging logic into a reusablelogModelCall()helper method that is called in both streaming and non-streaming code paths.Key changes:
logModelCall()helper method inpackages/core/src/runtime.ts:2172-2222logModelCall()after accumulating full text at line 2390useModel:*typesREASONINGmodel type and improved UI categorizationConfidence Score: 5/5
Important Files Changed
logModelCall()helper and fixed streaming path bug by calling it after stream completesuseModel:*andactiontypes in Model Calls panelREASONINGmodel type support, simplified categorization logic, and added embedding filter optionSequence Diagram
sequenceDiagram participant Client as Client UI participant Runtime as AgentRuntime participant Handler as Model Handler participant DB as Database Adapter Note over Client,DB: Streaming Path (Before Fix) Client->>Runtime: useModel(TEXT_LARGE, {onStreamChunk, prompt}) Runtime->>Handler: handler(runtime, params) Handler-->>Runtime: TextStreamResult with textStream loop For each chunk Runtime->>Runtime: Iterate textStream Runtime->>Client: onStreamChunk(chunk) end Runtime->>Runtime: return fullText Note over Runtime,DB: ❌ Return happened before logging Note over Client,DB: Streaming Path (After Fix) Client->>Runtime: useModel(TEXT_LARGE, {onStreamChunk, prompt}) Runtime->>Handler: handler(runtime, params) Handler-->>Runtime: TextStreamResult with textStream loop For each chunk Runtime->>Runtime: Iterate textStream Runtime->>Client: onStreamChunk(chunk) end Runtime->>Runtime: logModelCall(modelType, params, fullText) Runtime->>DB: adapter.log({type: 'useModel:TEXT_LARGE', body: {...}}) Runtime->>Runtime: return fullText Note over Client,DB: Non-Streaming Path (Unchanged) Client->>Runtime: useModel(TEXT_LARGE, {prompt}) Runtime->>Handler: handler(runtime, params) Handler-->>Runtime: response string Runtime->>Runtime: logModelCall(modelType, params, response) Runtime->>DB: adapter.log({type: 'useModel:TEXT_LARGE', body: {...}}) Runtime->>Runtime: return response