-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add comprehensive LLM exchange logging #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
gary-van-woerkens
wants to merge
1
commit into
main
Choose a base branch
from
feature/add-llm-logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| import { describe, expect, it, vi, beforeEach } from 'vitest' | ||
|
|
||
| // Mock console.log to capture log outputs | ||
| const mockConsoleLog = vi.spyOn(console, 'log').mockImplementation(() => {}) | ||
|
|
||
| // Mock environment variables before importing the module | ||
| vi.stubEnv('LOG_LLM_EXCHANGES', 'metadata') | ||
|
|
||
| import { | ||
| logLLMRequestSent, | ||
| logLLMResponseReceived, | ||
| logLLMRequestFailed | ||
| } from '../src/utils/logger.ts' | ||
|
|
||
| describe('LLM Logging', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| describe('logLLMRequestSent', () => { | ||
| it('should log request with metadata level', () => { | ||
| const prompt = 'Test prompt' | ||
| const model = 'claude-sonnet-4-20250514' | ||
| const strategyName = 'line-comments' | ||
| const context = { pr_number: 123, repository: 'test/repo' } | ||
|
|
||
| logLLMRequestSent(prompt, model, strategyName, context) | ||
|
|
||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"event_type":"llm_request_sent"') | ||
| ) | ||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"model_used":"claude-sonnet-4-20250514"') | ||
| ) | ||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"strategy_name":"line-comments"') | ||
| ) | ||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"pr_number":123') | ||
| ) | ||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"repository":"test/repo"') | ||
| ) | ||
| }) | ||
|
|
||
| it('should include prompt preview in truncated mode', () => { | ||
| const longPrompt = 'a'.repeat(1000) | ||
| const model = 'claude-sonnet-4-20250514' | ||
| const strategyName = 'line-comments' | ||
|
|
||
| // Mock environment to use truncated mode | ||
| vi.stubEnv('LOG_LLM_EXCHANGES', 'truncated') | ||
|
|
||
| logLLMRequestSent(longPrompt, model, strategyName) | ||
|
|
||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"prompt_preview"') | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('logLLMResponseReceived', () => { | ||
| it('should log response with duration and token usage', () => { | ||
| const response = '{"summary": "Test response"}' | ||
| const model = 'claude-sonnet-4-20250514' | ||
| const strategyName = 'line-comments' | ||
| const durationMs = 1500 | ||
| const tokensUsed = { input: 100, output: 50 } | ||
| const context = { pr_number: 123, repository: 'test/repo' } | ||
|
|
||
| logLLMResponseReceived( | ||
| response, | ||
| model, | ||
| strategyName, | ||
| durationMs, | ||
| tokensUsed, | ||
| context | ||
| ) | ||
|
|
||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"event_type":"llm_response_received"') | ||
| ) | ||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"request_duration_ms":1500') | ||
| ) | ||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"tokens_used":{"input":100,"output":50}') | ||
| ) | ||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"pr_number":123') | ||
| ) | ||
| }) | ||
|
|
||
| it('should truncate long responses in truncated mode', () => { | ||
| const longResponse = 'b'.repeat(1000) | ||
| const model = 'claude-sonnet-4-20250514' | ||
| const strategyName = 'line-comments' | ||
| const durationMs = 1500 | ||
|
|
||
| // Mock environment to use truncated mode | ||
| vi.stubEnv('LOG_LLM_EXCHANGES', 'truncated') | ||
|
|
||
| logLLMResponseReceived(longResponse, model, strategyName, durationMs) | ||
|
|
||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"response_preview"') | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('logLLMRequestFailed', () => { | ||
| it('should log request failure with error details', () => { | ||
| const error = new Error('API request failed') | ||
| const model = 'claude-sonnet-4-20250514' | ||
| const strategyName = 'line-comments' | ||
| const context = { pr_number: 123, repository: 'test/repo' } | ||
|
|
||
| logLLMRequestFailed(error, model, strategyName, context) | ||
|
|
||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"event_type":"llm_request_failed"') | ||
| ) | ||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"level":"error"') | ||
| ) | ||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"error_message":"API request failed"') | ||
| ) | ||
| expect(mockConsoleLog).toHaveBeenCalledWith( | ||
| expect.stringContaining('"pr_number":123') | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('Log levels', () => { | ||
| it('should not log when disabled', () => { | ||
| vi.stubEnv('LOG_LLM_EXCHANGES', 'disabled') | ||
|
|
||
| logLLMRequestSent('test', 'claude-sonnet-4-20250514', 'line-comments') | ||
|
|
||
| expect(mockConsoleLog).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should log only metadata when in metadata mode', () => { | ||
| vi.stubEnv('LOG_LLM_EXCHANGES', 'metadata') | ||
|
|
||
| const prompt = 'Test prompt' | ||
| logLLMRequestSent(prompt, 'claude-sonnet-4-20250514', 'line-comments') | ||
|
|
||
| const logCall = mockConsoleLog.mock.calls[0][0] | ||
| expect(logCall).not.toContain('"prompt_preview"') | ||
| expect(logCall).not.toContain('"full_prompt"') | ||
| }) | ||
|
|
||
| it('should include full content in full mode', () => { | ||
| vi.stubEnv('LOG_LLM_EXCHANGES', 'full') | ||
|
|
||
| const prompt = 'Test prompt' | ||
| logLLMRequestSent(prompt, 'claude-sonnet-4-20250514', 'line-comments') | ||
|
|
||
| const logCall = mockConsoleLog.mock.calls[0][0] | ||
| expect(logCall).toContain('"full_prompt"') | ||
| expect(logCall).toContain('"prompt_preview"') | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These comment lines use
#which is invalid YAML syntax within thedatasection of a ConfigMap. YAML comments must be outside of string values or the ConfigMap will fail to parse during deployment. Either remove these lines or move them outside thedatasection as proper YAML comments.Fix it with Roo Code or mention @roomote and request a fix.