-
Notifications
You must be signed in to change notification settings - Fork 3k
feat(telemetry): Phase 4a — TTFT capture + GenAI semconv dual-emit (#3731) #4417
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
Merged
doudouOUC
merged 2 commits into
QwenLM:main
from
doudouOUC:feat/telemetry-phase-4a-ttft
May 22, 2026
Merged
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
131 changes: 131 additions & 0 deletions
131
packages/core/src/core/loggingContentGenerator/streamContentDetection.test.ts
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,131 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Qwen Team | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, expect, it } from 'vitest'; | ||
| import { GenerateContentResponse } from '@google/genai'; | ||
| import { hasUserVisibleContent } from './streamContentDetection.js'; | ||
|
|
||
| function chunkWithParts(parts: unknown[]): GenerateContentResponse { | ||
| const r = new GenerateContentResponse(); | ||
| r.candidates = [ | ||
| { | ||
| content: { role: 'model', parts: parts as never }, | ||
| }, | ||
| ]; | ||
| return r; | ||
| } | ||
|
|
||
| describe('hasUserVisibleContent', () => { | ||
| it('returns true for non-empty text part', () => { | ||
| expect(hasUserVisibleContent(chunkWithParts([{ text: 'hi' }]))).toBe(true); | ||
| }); | ||
|
|
||
| it('returns false for empty text part', () => { | ||
| expect(hasUserVisibleContent(chunkWithParts([{ text: '' }]))).toBe(false); | ||
| }); | ||
|
|
||
| it('returns true for functionCall part', () => { | ||
| expect( | ||
| hasUserVisibleContent( | ||
| chunkWithParts([{ functionCall: { name: 'read', args: {} } }]), | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for inlineData part', () => { | ||
| expect( | ||
| hasUserVisibleContent( | ||
| chunkWithParts([ | ||
| { inlineData: { mimeType: 'image/png', data: 'abc' } }, | ||
| ]), | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for executableCode part', () => { | ||
| expect( | ||
| hasUserVisibleContent( | ||
| chunkWithParts([ | ||
| { executableCode: { language: 'PYTHON', code: 'print(1)' } }, | ||
| ]), | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for thought / reasoning part with thought: true', () => { | ||
| expect(hasUserVisibleContent(chunkWithParts([{ thought: true }]))).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| it('returns false for thought: false (explicit non-thought part)', () => { | ||
| // Codebase convention: `thought` is a boolean flag where false means | ||
| // "explicitly not a thought." A part with only `thought: false` and no | ||
| // other content must not trigger TTFT. | ||
| expect(hasUserVisibleContent(chunkWithParts([{ thought: false }]))).toBe( | ||
| false, | ||
| ); | ||
| }); | ||
|
|
||
| it('returns false for thought: undefined / missing (default non-thought)', () => { | ||
| // A bare object without the `thought` key is the common case for non-thinking | ||
| // chunks; must not match the thought branch. | ||
| expect(hasUserVisibleContent(chunkWithParts([{}]))).toBe(false); | ||
| }); | ||
|
|
||
| it('returns true when thought: true coexists with empty text', () => { | ||
| // First Anthropic <thinking> chunk often arrives as { text: '', thought: true }. | ||
| // Per design doc D1, "thought / reasoning content" is user-visible — TTFT fires. | ||
| expect( | ||
| hasUserVisibleContent(chunkWithParts([{ text: '', thought: true }])), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true when any part is user-visible (mixed)', () => { | ||
| expect( | ||
| hasUserVisibleContent(chunkWithParts([{ text: '' }, { text: 'hi' }])), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('returns false for empty parts array', () => { | ||
| expect(hasUserVisibleContent(chunkWithParts([]))).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false when candidates is missing', () => { | ||
| const r = new GenerateContentResponse(); | ||
| expect(hasUserVisibleContent(r)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false when content is missing', () => { | ||
| const r = new GenerateContentResponse(); | ||
| r.candidates = [{}]; | ||
| expect(hasUserVisibleContent(r)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false when parts is undefined', () => { | ||
| const r = new GenerateContentResponse(); | ||
| r.candidates = [{ content: { role: 'model' } }]; | ||
| expect(hasUserVisibleContent(r)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for usage-only / role-only chunks', () => { | ||
| const r = new GenerateContentResponse(); | ||
| r.candidates = [{ content: { role: 'model', parts: [] } }]; | ||
| r.usageMetadata = { totalTokenCount: 42 }; | ||
| expect(hasUserVisibleContent(r)).toBe(false); | ||
| }); | ||
|
|
||
| it('handles parts that are non-objects defensively', () => { | ||
| expect( | ||
| hasUserVisibleContent( | ||
| chunkWithParts([null, undefined, 'string', 42, { text: 'real' }]), | ||
| ), | ||
| ).toBe(true); | ||
| expect(hasUserVisibleContent(chunkWithParts([null, undefined, 'x']))).toBe( | ||
| false, | ||
| ); | ||
| }); | ||
| }); |
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.
[Suggestion] This call is inside the
for awaitloop that streams chunks to the user. IfhasUserVisibleContentever throws (e.g., an SDK update changes theParttype shape), the exception propagates into thecatch (error)block below, aborting the user's response stream. The error trace would point atstreamContentDetectioninternals — not obviously a telemetry bug.Since TTFT detection is best-effort telemetry, it should never affect the user-visible response. A try/catch here isolates detection failures:
— qwen-latest-series-invite-beta-v36 via Qwen Code /review