-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix openai cache tracking and cost estimates #2616
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,7 +153,12 @@ describe("OpenAiNativeHandler", () => { | |
| results.push(result) | ||
| } | ||
|
|
||
| expect(results).toEqual([{ type: "usage", inputTokens: 0, outputTokens: 0 }]) | ||
| // Verify essential fields directly | ||
| expect(results.length).toBe(1) | ||
| expect(results[0].type).toBe("usage") | ||
| // Use type assertion to avoid TypeScript errors | ||
| expect((results[0] as any).inputTokens).toBe(0) | ||
| expect((results[0] as any).outputTokens).toBe(0) | ||
|
|
||
| // Verify developer role is used for system prompt with o1 model | ||
| expect(mockCreate).toHaveBeenCalledWith({ | ||
|
|
@@ -221,12 +226,18 @@ describe("OpenAiNativeHandler", () => { | |
| results.push(result) | ||
| } | ||
|
|
||
| expect(results).toEqual([ | ||
| { type: "text", text: "Hello" }, | ||
| { type: "text", text: " there" }, | ||
| { type: "text", text: "!" }, | ||
| { type: "usage", inputTokens: 10, outputTokens: 5 }, | ||
| ]) | ||
| // Verify text responses individually | ||
| expect(results.length).toBe(4) | ||
| expect(results[0]).toMatchObject({ type: "text", text: "Hello" }) | ||
| expect(results[1]).toMatchObject({ type: "text", text: " there" }) | ||
| expect(results[2]).toMatchObject({ type: "text", text: "!" }) | ||
|
|
||
| // Check usage data fields but use toBeCloseTo for floating point comparison | ||
| expect(results[3].type).toBe("usage") | ||
| // Use type assertion to avoid TypeScript errors | ||
| expect((results[3] as any).inputTokens).toBe(10) | ||
| expect((results[3] as any).outputTokens).toBe(5) | ||
| expect((results[3] as any).totalCost).toBeCloseTo(0.00006, 6) | ||
|
|
||
| expect(mockCreate).toHaveBeenCalledWith({ | ||
| model: "gpt-4.1", | ||
|
|
@@ -261,10 +272,16 @@ describe("OpenAiNativeHandler", () => { | |
| results.push(result) | ||
| } | ||
|
|
||
| expect(results).toEqual([ | ||
| { type: "text", text: "Hello" }, | ||
| { type: "usage", inputTokens: 10, outputTokens: 5 }, | ||
| ]) | ||
| // Verify responses individually | ||
| expect(results.length).toBe(2) | ||
| expect(results[0]).toMatchObject({ type: "text", text: "Hello" }) | ||
|
|
||
| // Check usage data fields but use toBeCloseTo for floating point comparison | ||
| expect(results[1].type).toBe("usage") | ||
| // Use type assertion to avoid TypeScript errors | ||
| expect((results[1] as any).inputTokens).toBe(10) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider defining proper result types to eliminate repeated 'as any' assertions, enhancing maintainability.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Repeated type assertions for usage properties appear in multiple tests. Consider extracting these assertions into a helper function to reduce duplication and improve maintainability. |
||
| expect((results[1] as any).outputTokens).toBe(5) | ||
| expect((results[1] as any).totalCost).toBeCloseTo(0.00006, 6) | ||
| }) | ||
| }) | ||
|
|
||
|
|
||
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.
Consider replacing the
anycast with a more specific type orunknownto improve type safety in these assertions.