@opencode/util 0.0.0-dev-1180 → 0.0.0-dev-1216

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,8 @@ export interface Message {
5
5
  readonly type: string;
6
6
  readonly time: {
7
7
  readonly created: number;
8
+ /** When the provider streamed the first output token; steps recorded before this existed leave it unset. */
9
+ readonly firstToken?: number;
8
10
  readonly streamed?: number;
9
11
  readonly completed?: number;
10
12
  };
@@ -24,6 +26,17 @@ export interface Options {
24
26
  }
25
27
  /** Milliseconds from the prompt that started the turn until `message` completed, or 0 while it runs. */
26
28
  export declare function duration(messages: readonly Message[], message: Message, options?: Options): number;
27
- /** Output and reasoning tokens per second across the turn's steps up to `message`, or undefined until every step has streamed. */
29
+ /**
30
+ * Output and reasoning tokens per second while the provider streamed them, or undefined until every step has streamed.
31
+ *
32
+ * Each step counts from its first streamed token to the end of the response body, so the wait for that first token
33
+ * (time to first token) is not divided into the output rate. Steps recorded before `time.firstToken` existed count
34
+ * from request dispatch instead, which is what this measured for them before the field existed.
35
+ */
28
36
  export declare function tokensPerSecond(messages: readonly Message[], message: Message, options?: Options): number | undefined;
37
+ /**
38
+ * Milliseconds from the dispatch of the turn's first token-producing step to its first streamed token, or undefined
39
+ * until a step records one.
40
+ */
41
+ export declare function timeToFirstToken(messages: readonly Message[], message: Message, options?: Options): number | undefined;
29
42
  export declare function legacy(messages: readonly Message[]): boolean;
@@ -7,13 +7,18 @@ export function duration(messages, message, options = {}) {
7
7
  const input = messages[inputIndex(messages, index === -1 ? messages.length : index, options.legacy ?? legacy(messages))];
8
8
  return Math.max(0, message.time.completed - (input?.time.created ?? message.time.created));
9
9
  }
10
- /** Output and reasoning tokens per second across the turn's steps up to `message`, or undefined until every step has streamed. */
10
+ /**
11
+ * Output and reasoning tokens per second while the provider streamed them, or undefined until every step has streamed.
12
+ *
13
+ * Each step counts from its first streamed token to the end of the response body, so the wait for that first token
14
+ * (time to first token) is not divided into the output rate. Steps recorded before `time.firstToken` existed count
15
+ * from request dispatch instead, which is what this measured for them before the field existed.
16
+ */
11
17
  export function tokensPerSecond(messages, message, options = {}) {
12
- const index = options.index ?? messages.findIndex((item) => item.id === message.id);
13
- const end = index === -1 ? messages.length : index + 1;
14
- const start = inputIndex(messages, end, options.legacy ?? legacy(messages));
15
- const steps = messages.slice(start + 1, end).filter((item) => item.type === "assistant");
16
- const durations = steps.flatMap((step) => step.time.streamed === undefined ? [] : [Math.max(0, step.time.streamed - step.time.created)]);
18
+ const steps = turnSteps(messages, message, options);
19
+ const durations = steps.flatMap((step) => step.time.streamed === undefined
20
+ ? []
21
+ : [Math.max(0, step.time.streamed - (step.time.firstToken ?? step.time.created))]);
17
22
  if (steps.length === 0 || durations.length !== steps.length)
18
23
  return;
19
24
  const output = steps.reduce((total, step) => total + (step.tokens?.output ?? 0) + (step.tokens?.reasoning ?? 0), 0);
@@ -23,9 +28,25 @@ export function tokensPerSecond(messages, message, options = {}) {
23
28
  // Aggregate before dividing so each step is weighted by its provider-active duration.
24
29
  return output / (total / 1_000);
25
30
  }
31
+ /**
32
+ * Milliseconds from the dispatch of the turn's first token-producing step to its first streamed token, or undefined
33
+ * until a step records one.
34
+ */
35
+ export function timeToFirstToken(messages, message, options = {}) {
36
+ const step = turnSteps(messages, message, options).find((item) => item.time.firstToken !== undefined);
37
+ if (step?.time.firstToken === undefined)
38
+ return;
39
+ return Math.max(0, step.time.firstToken - step.time.created);
40
+ }
26
41
  export function legacy(messages) {
27
42
  return !messages.some((message) => message.type === "idle");
28
43
  }
44
+ function turnSteps(messages, message, options) {
45
+ const index = options.index ?? messages.findIndex((item) => item.id === message.id);
46
+ const end = index === -1 ? messages.length : index + 1;
47
+ const start = inputIndex(messages, end, options.legacy ?? legacy(messages));
48
+ return messages.slice(start + 1, end).filter((item) => item.type === "assistant");
49
+ }
29
50
  function inputIndex(messages, end, legacy) {
30
51
  // Reading a sliced prefix subscribes every caller to unrelated historical messages, so walk
31
52
  // back only as far as the turn boundary: the nearest input in legacy sessions, otherwise the
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@opencode/util",
4
- "version": "0.0.0-dev-1180",
4
+ "version": "0.0.0-dev-1216",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "repository": {