@opencode/util 2.2.7 → 2.3.1
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.
- package/dist/execute-output.d.ts +10 -0
- package/dist/execute-output.js +22 -0
- package/dist/session-turn.d.ts +9 -1
- package/dist/session-turn.js +17 -6
- package/dist/text-boundary.d.ts +13 -0
- package/dist/text-boundary.js +35 -0
- package/package.json +1 -1
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code Mode's `execute` output for display. The tool returns its result (compact JSON for structured values) followed
|
|
3
|
+
* by optional "Warnings:" and "Logs:" sections. A JSON result comes back indented as `json`, the sections as `rest`,
|
|
4
|
+
* and `text` joins them.
|
|
5
|
+
*/
|
|
6
|
+
export declare function formatExecuteOutput(output: string): {
|
|
7
|
+
json: string | undefined;
|
|
8
|
+
rest: string | undefined;
|
|
9
|
+
text: string;
|
|
10
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
const decodeJson = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown));
|
|
3
|
+
/**
|
|
4
|
+
* Code Mode's `execute` output for display. The tool returns its result (compact JSON for structured values) followed
|
|
5
|
+
* by optional "Warnings:" and "Logs:" sections. A JSON result comes back indented as `json`, the sections as `rest`,
|
|
6
|
+
* and `text` joins them.
|
|
7
|
+
*/
|
|
8
|
+
export function formatExecuteOutput(output) {
|
|
9
|
+
const end = output.search(/\n\n(Warnings|Logs):\n/);
|
|
10
|
+
const result = end === -1 ? output : output.slice(0, end);
|
|
11
|
+
const rest = end === -1 ? undefined : output.slice(end).trim();
|
|
12
|
+
const json = indentedJson(result);
|
|
13
|
+
return { json, rest, text: rest === undefined ? (json ?? result) : `${json ?? result}\n\n${rest}` };
|
|
14
|
+
}
|
|
15
|
+
function indentedJson(text) {
|
|
16
|
+
if (!text.startsWith("{") && !text.startsWith("["))
|
|
17
|
+
return;
|
|
18
|
+
const decoded = decodeJson(text);
|
|
19
|
+
if (Option.isNone(decoded))
|
|
20
|
+
return;
|
|
21
|
+
return JSON.stringify(decoded.value, null, 2);
|
|
22
|
+
}
|
package/dist/session-turn.d.ts
CHANGED
|
@@ -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,12 @@ 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
|
-
/**
|
|
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;
|
|
29
37
|
export declare function legacy(messages: readonly Message[]): boolean;
|
package/dist/session-turn.js
CHANGED
|
@@ -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
|
-
/**
|
|
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
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
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);
|
|
@@ -26,6 +31,12 @@ export function tokensPerSecond(messages, message, options = {}) {
|
|
|
26
31
|
export function legacy(messages) {
|
|
27
32
|
return !messages.some((message) => message.type === "idle");
|
|
28
33
|
}
|
|
34
|
+
function turnSteps(messages, message, options) {
|
|
35
|
+
const index = options.index ?? messages.findIndex((item) => item.id === message.id);
|
|
36
|
+
const end = index === -1 ? messages.length : index + 1;
|
|
37
|
+
const start = inputIndex(messages, end, options.legacy ?? legacy(messages));
|
|
38
|
+
return messages.slice(start + 1, end).filter((item) => item.type === "assistant");
|
|
39
|
+
}
|
|
29
40
|
function inputIndex(messages, end, legacy) {
|
|
30
41
|
// Reading a sliced prefix subscribes every caller to unrelated historical messages, so walk
|
|
31
42
|
// back only as far as the turn boundary: the nearest input in legacy sessions, otherwise the
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * as TextBoundary from "./text-boundary.js";
|
|
2
|
+
/**
|
|
3
|
+
* Clip text to a budget without ending or starting on half a character. `head` and `tail` count UTF-16 code units,
|
|
4
|
+
* like `.length`; `utf8Tail` counts bytes of UTF-8 output that has not been decoded yet. The result can be shorter
|
|
5
|
+
* than the budget by the part of the character at the cut, and `head("😀", 1)` is empty. Boundaries are code points,
|
|
6
|
+
* not graphemes, and malformed input is kept as it is everywhere except at the cut.
|
|
7
|
+
*/
|
|
8
|
+
/** The first `limit` code units of `text`, without a high surrogate whose pair the limit cut off. */
|
|
9
|
+
export declare function head(text: string, limit: number): string;
|
|
10
|
+
/** The last `limit` code units of `text`, without a low surrogate whose pair the limit cut off. */
|
|
11
|
+
export declare function tail(text: string, limit: number): string;
|
|
12
|
+
/** The last `limit` bytes of `bytes`, decoded, after skipping up to 3 continuation bytes of a cut character. */
|
|
13
|
+
export declare function utf8Tail(bytes: Uint8Array, limit: number): string;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export * as TextBoundary from "./text-boundary.js";
|
|
2
|
+
/**
|
|
3
|
+
* Clip text to a budget without ending or starting on half a character. `head` and `tail` count UTF-16 code units,
|
|
4
|
+
* like `.length`; `utf8Tail` counts bytes of UTF-8 output that has not been decoded yet. The result can be shorter
|
|
5
|
+
* than the budget by the part of the character at the cut, and `head("😀", 1)` is empty. Boundaries are code points,
|
|
6
|
+
* not graphemes, and malformed input is kept as it is everywhere except at the cut.
|
|
7
|
+
*/
|
|
8
|
+
/** The first `limit` code units of `text`, without a high surrogate whose pair the limit cut off. */
|
|
9
|
+
export function head(text, limit) {
|
|
10
|
+
if (text.length <= limit)
|
|
11
|
+
return text;
|
|
12
|
+
const code = text.charCodeAt(limit - 1);
|
|
13
|
+
return text.slice(0, code >= 0xd800 && code <= 0xdbff ? limit - 1 : limit);
|
|
14
|
+
}
|
|
15
|
+
/** The last `limit` code units of `text`, without a low surrogate whose pair the limit cut off. */
|
|
16
|
+
export function tail(text, limit) {
|
|
17
|
+
if (text.length <= limit)
|
|
18
|
+
return text;
|
|
19
|
+
const start = text.length - limit;
|
|
20
|
+
const code = text.charCodeAt(start);
|
|
21
|
+
return text.slice(code >= 0xdc00 && code <= 0xdfff ? start + 1 : start);
|
|
22
|
+
}
|
|
23
|
+
/** The last `limit` bytes of `bytes`, decoded, after skipping up to 3 continuation bytes of a cut character. */
|
|
24
|
+
export function utf8Tail(bytes, limit) {
|
|
25
|
+
if (bytes.length <= limit)
|
|
26
|
+
return decoder.decode(bytes);
|
|
27
|
+
const start = bytes.length - limit;
|
|
28
|
+
const end = Math.min(bytes.length, start + 3);
|
|
29
|
+
let index = start;
|
|
30
|
+
while (index < end && (bytes[index] & 0xc0) === 0x80)
|
|
31
|
+
index++;
|
|
32
|
+
return decoder.decode(bytes.subarray(index));
|
|
33
|
+
}
|
|
34
|
+
// Like Buffer#toString, keep a leading byte order mark.
|
|
35
|
+
const decoder = new TextDecoder("utf-8", { ignoreBOM: true });
|