Skip to content

Commit e6865ab

Browse files
committed
fix(openai): fix duplicate cost tracking, local timezone day boundary, and add missing docstrings
- Remove duplicate addToTotalSessionCost call in message_stop handler - Fix startOfDay() to use local date components instead of UTC - Add JSDoc to all functions missing docstrings for 80%+ coverage Signed-off-by: guunergooner <tongchao0923@gmail.com>
1 parent b23bead commit e6865ab

4 files changed

Lines changed: 10 additions & 10 deletions

File tree

src/services/api/openai/__tests__/queryModelOpenAI.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function makeMessageStart(overrides: Record<string, any> = {}): BetaRawMessageSt
3737
} as any
3838
}
3939

40+
/** Build a content_block_start event for the given block type */
4041
function makeContentBlockStart(index: number, type: 'text' | 'tool_use' | 'thinking', extra: Record<string, any> = {}): BetaRawMessageStreamEvent {
4142
const block =
4243
type === 'text'
@@ -47,22 +48,27 @@ function makeContentBlockStart(index: number, type: 'text' | 'tool_use' | 'think
4748
return { type: 'content_block_start', index, content_block: { ...block, ...extra } } as any
4849
}
4950

51+
/** Build a text_delta content_block_delta event */
5052
function makeTextDelta(index: number, text: string): BetaRawMessageStreamEvent {
5153
return { type: 'content_block_delta', index, delta: { type: 'text_delta', text } } as any
5254
}
5355

56+
/** Build an input_json_delta content_block_delta event */
5457
function makeInputJsonDelta(index: number, json: string): BetaRawMessageStreamEvent {
5558
return { type: 'content_block_delta', index, delta: { type: 'input_json_delta', partial_json: json } } as any
5659
}
5760

61+
/** Build a thinking_delta content_block_delta event */
5862
function makeThinkingDelta(index: number, thinking: string): BetaRawMessageStreamEvent {
5963
return { type: 'content_block_delta', index, delta: { type: 'thinking_delta', thinking } } as any
6064
}
6165

66+
/** Build a content_block_stop event */
6267
function makeContentBlockStop(index: number): BetaRawMessageStreamEvent {
6368
return { type: 'content_block_stop', index } as any
6469
}
6570

71+
/** Build a message_delta event with stop_reason and output_tokens */
6672
function makeMessageDelta(stopReason: string, outputTokens: number): BetaRawMessageStreamEvent {
6773
return {
6874
type: 'message_delta',
@@ -71,6 +77,7 @@ function makeMessageDelta(stopReason: string, outputTokens: number): BetaRawMess
7177
} as any
7278
}
7379

80+
/** Build a message_stop event */
7481
function makeMessageStop(): BetaRawMessageStreamEvent {
7582
return { type: 'message_stop' } as any
7683
}

src/services/api/openai/__tests__/streamAdapter.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function makeChunk(overrides: Partial<ChatCompletionChunk> & any = {}): ChatComp
2929
} as ChatCompletionChunk
3030
}
3131

32+
/** Collect all emitted Anthropic events from the stream adapter for assertion */
3233
async function collectEvents(chunks: ChatCompletionChunk[]) {
3334
const events: any[] = []
3435
for await (const event of adaptOpenAIStreamToAnthropic(mockStream(chunks), 'gpt-4o')) {

src/services/api/openai/index.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,6 @@ export async function* queryModelOpenAI(
382382
}
383383
}
384384

385-
// Track cost and token usage (matching the Anthropic path in claude.ts)
386-
if (
387-
event.type === 'message_stop' &&
388-
usage.input_tokens + usage.output_tokens > 0
389-
) {
390-
const costUSD = calculateUSDCost(openaiModel, usage as any)
391-
addToTotalSessionCost(costUSD, usage as any, options.model)
392-
}
393-
394385
// Also yield as StreamEvent for real-time display (matching Anthropic path)
395386
yield {
396387
type: 'stream_event',

src/utils/formatBriefTimestamp.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function getLocale(): string | undefined {
7676
}
7777
}
7878

79+
/** Return the epoch-ms of the start of the local calendar day for `d`. */
7980
function startOfDay(d: Date): number {
80-
return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate())
81+
return new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime()
8182
}

0 commit comments

Comments
 (0)