This repository was archived by the owner on May 20, 2026. It is now read-only.
Commit 17426d7
authored
Add APIUsage tracking to extChatEndpoint
fix(ext-endpoint): propagate usage from DataPart stream in ExtensionContributedChatEndpoint
The extension-contributed chat endpoint previously returned a hardcoded
zeroed-out usage object on every successful response, causing the
Copilot Chat context-window meter to never update when routing through
third-party language model providers.
Add a `usageFromDataPart()` helper that checks incoming
`LanguageModelDataPart` chunks for a native usage shape
(`prompt_tokens`, `completion_tokens`, `total_tokens` — all numbers)
without keying on any MIME type. This mirrors the shape-based check
already present in the non-provider (internal) streaming path.
In `makeChatRequest2()`:
- Declare `usage: APIUsage | undefined` before the stream loop.
- After handling known MIME types (StatefulMarker, ContextManagement),
call `usageFromDataPart()` on every DataPart and capture the result.
- Return `usage ?? <zeroed fallback>` so existing behaviour is
preserved when no usage chunk arrives.
No new MIME branch is introduced; any provider that emits a DataPart
whose JSON payload matches the OpenAI usage shape will automatically
populate the meter.1 parent 9e668cb commit 17426d7
1 file changed
Lines changed: 39 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
40 | 71 | | |
41 | 72 | | |
42 | 73 | | |
| |||
204 | 235 | | |
205 | 236 | | |
206 | 237 | | |
| 238 | + | |
207 | 239 | | |
208 | 240 | | |
209 | 241 | | |
| |||
231 | 263 | | |
232 | 264 | | |
233 | 265 | | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
234 | 271 | | |
235 | 272 | | |
236 | 273 | | |
| |||
250 | 287 | | |
251 | 288 | | |
252 | 289 | | |
253 | | - | |
| 290 | + | |
254 | 291 | | |
255 | 292 | | |
256 | 293 | | |
| |||
0 commit comments