-
Notifications
You must be signed in to change notification settings - Fork 350
Add model context length and manual model deletion #520
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
Open
roiding
wants to merge
2
commits into
cita-777:main
Choose a base branch
from
roiding:pr/upstream-main
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| import { describe, expect, it, beforeEach } from 'vitest'; | ||
| import { | ||
| setModelContextLength, | ||
| setModelContextLengths, | ||
| getModelContextLength, | ||
| hasModelContextLength, | ||
| clearModelContextLengthCache, | ||
| extractContextLengthsFromPayload, | ||
| getAllModelContextLengths, | ||
| } from './modelContextLengthCache.js'; | ||
|
|
||
| describe('modelContextLengthCache', () => { | ||
| beforeEach(() => { | ||
| clearModelContextLengthCache(); | ||
| }); | ||
|
|
||
| describe('setModelContextLength / getModelContextLength', () => { | ||
| it('stores and retrieves context length for a model', () => { | ||
| setModelContextLength('gpt-4o', 128000); | ||
| expect(getModelContextLength('gpt-4o')).toBe(128000); | ||
| }); | ||
|
|
||
| it('returns default 1_000_000 when model is not in cache', () => { | ||
| expect(getModelContextLength('unknown-model')).toBe(1_000_000); | ||
| }); | ||
|
|
||
| it('normalizes model name case-insensitively', () => { | ||
| setModelContextLength('GPT-4o', 128000); | ||
| expect(getModelContextLength('gpt-4o')).toBe(128000); | ||
| expect(getModelContextLength('GPT-4O')).toBe(128000); | ||
| }); | ||
|
|
||
| it('ignores invalid values', () => { | ||
| setModelContextLength('', 128000); | ||
| expect(hasModelContextLength('')).toBe(false); | ||
|
|
||
| setModelContextLength('model-a', NaN); | ||
| expect(hasModelContextLength('model-a')).toBe(false); | ||
|
|
||
| setModelContextLength('model-b', -100); | ||
| expect(hasModelContextLength('model-b')).toBe(false); | ||
|
|
||
| setModelContextLength('model-c', 0); | ||
| expect(hasModelContextLength('model-c')).toBe(false); | ||
| }); | ||
|
|
||
| it('rounds fractional values', () => { | ||
| setModelContextLength('model', 128000.7); | ||
| expect(getModelContextLength('model')).toBe(128001); | ||
| }); | ||
| }); | ||
|
|
||
| describe('setModelContextLengths (bulk)', () => { | ||
| it('stores multiple entries at once', () => { | ||
| const entries = new Map([ | ||
| ['model-a', 128000], | ||
| ['model-b', 200000], | ||
| ['model-c', 1_000_000], | ||
| ]); | ||
| setModelContextLengths(entries); | ||
|
|
||
| expect(getModelContextLength('model-a')).toBe(128000); | ||
| expect(getModelContextLength('model-b')).toBe(200000); | ||
| expect(getModelContextLength('model-c')).toBe(1_000_000); | ||
| }); | ||
|
|
||
| it('ignores invalid entries in bulk', () => { | ||
| const entries = new Map([ | ||
| ['valid-model', 128000], | ||
| ['', 200000], | ||
| ['nan-model', NaN], | ||
| ]); | ||
| setModelContextLengths(entries); | ||
|
|
||
| expect(getModelContextLength('valid-model')).toBe(128000); | ||
| expect(hasModelContextLength('')).toBe(false); | ||
| expect(hasModelContextLength('nan-model')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('hasModelContextLength', () => { | ||
| it('returns true only for cached models', () => { | ||
| expect(hasModelContextLength('gpt-4o')).toBe(false); | ||
| setModelContextLength('gpt-4o', 128000); | ||
| expect(hasModelContextLength('gpt-4o')).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('clearModelContextLengthCache', () => { | ||
| it('clears all entries', () => { | ||
| setModelContextLength('model-a', 128000); | ||
| setModelContextLength('model-b', 200000); | ||
| clearModelContextLengthCache(); | ||
| expect(hasModelContextLength('model-a')).toBe(false); | ||
| expect(hasModelContextLength('model-b')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('extractContextLengthsFromPayload', () => { | ||
| it('extracts context_length from OpenAI-compatible payload', () => { | ||
| const payload = { | ||
| data: [ | ||
| { id: 'gpt-4o', context_length: 128000 }, | ||
| { id: 'claude-3', context_length: 200000 }, | ||
| ], | ||
| }; | ||
| const result = extractContextLengthsFromPayload(payload); | ||
| expect(result.size).toBe(2); | ||
| expect(result.get('gpt-4o')).toBe(128000); | ||
| expect(result.get('claude-3')).toBe(200000); | ||
| }); | ||
|
|
||
| it('extracts contextLength (camelCase)', () => { | ||
| const payload = { | ||
| data: [ | ||
| { id: 'model-a', contextLength: 256000 }, | ||
| ], | ||
| }; | ||
| const result = extractContextLengthsFromPayload(payload); | ||
| expect(result.get('model-a')).toBe(256000); | ||
| }); | ||
|
|
||
| it('extracts max_context_length', () => { | ||
| const payload = { | ||
| data: [ | ||
| { id: 'model-b', max_context_length: 512000 }, | ||
| ], | ||
| }; | ||
| const result = extractContextLengthsFromPayload(payload); | ||
| expect(result.get('model-b')).toBe(512000); | ||
| }); | ||
|
|
||
| it('extracts context_window', () => { | ||
| const payload = { | ||
| data: [ | ||
| { id: 'model-c', context_window: 1_000_000 }, | ||
| ], | ||
| }; | ||
| const result = extractContextLengthsFromPayload(payload); | ||
| expect(result.get('model-c')).toBe(1_000_000); | ||
| }); | ||
|
|
||
| it('parses string values as numbers', () => { | ||
| const payload = { | ||
| data: [ | ||
| { id: 'model-str', context_length: '128000' }, | ||
| ], | ||
| }; | ||
| const result = extractContextLengthsFromPayload(payload); | ||
| expect(result.get('model-str')).toBe(128000); | ||
| }); | ||
|
|
||
| it('returns empty map for payload without data array', () => { | ||
| expect(extractContextLengthsFromPayload(null).size).toBe(0); | ||
| expect(extractContextLengthsFromPayload({}).size).toBe(0); | ||
| expect(extractContextLengthsFromPayload({ data: 'not-array' }).size).toBe(0); | ||
| }); | ||
|
|
||
| it('returns empty map when no items have context_length', () => { | ||
| const payload = { | ||
| data: [ | ||
| { id: 'model-a' }, | ||
| { id: 'model-b' }, | ||
| ], | ||
| }; | ||
| const result = extractContextLengthsFromPayload(payload); | ||
| expect(result.size).toBe(0); | ||
| }); | ||
|
|
||
| it('skips items without id', () => { | ||
| const payload = { | ||
| data: [ | ||
| { context_length: 128000 }, | ||
| { id: '', context_length: 200000 }, | ||
| { id: 'valid', context_length: 300000 }, | ||
| ], | ||
| }; | ||
| const result = extractContextLengthsFromPayload(payload); | ||
| expect(result.size).toBe(1); | ||
| expect(result.get('valid')).toBe(300000); | ||
| }); | ||
|
|
||
| it('skips zero or negative context_length', () => { | ||
| const payload = { | ||
| data: [ | ||
| { id: 'zero', context_length: 0 }, | ||
| { id: 'negative', context_length: -100 }, | ||
| ], | ||
| }; | ||
| const result = extractContextLengthsFromPayload(payload); | ||
| expect(result.size).toBe(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getAllModelContextLengths', () => { | ||
| it('returns all cached entries', () => { | ||
| setModelContextLength('a', 100); | ||
| setModelContextLength('b', 200); | ||
| const all = getAllModelContextLengths(); | ||
| expect(all.size).toBe(2); | ||
| expect(all.get('a')).toBe(100); | ||
| expect(all.get('b')).toBe(200); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Extract deletion workflow to a service and execute it atomically.
Line 1970-Line 1981 performs DB mutation orchestration directly in the route, and a mid-loop failure can leave partial deletions while returning an error.
Proposed refactor direction
As per coding guidelines: "Route files in
src/server/routes/**are adapters, not owners... must not own ... persistence."🤖 Prompt for AI Agents