@kvsig/core 0.0.1 → 0.1.0
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/LICENSE +202 -0
- package/NOTICE +7 -0
- package/README.md +142 -3
- package/dist/index.cjs +1653 -0
- package/dist/index.d.cts +240 -0
- package/dist/index.d.ts +240 -0
- package/dist/index.js +1622 -0
- package/package.json +76 -6
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/** A JSON value with objects as order-preserving Maps. */
|
|
2
|
+
type JsonValue = null | boolean | number | string | JsonValue[] | JsonObject;
|
|
3
|
+
type JsonObject = Map<string, JsonValue>;
|
|
4
|
+
/**
|
|
5
|
+
* Parse JSON text strictly (RFC 8259): no NaN or Infinity, no leading zeros, no
|
|
6
|
+
* trailing text. Objects keep member order; a repeated member name keeps its
|
|
7
|
+
* first position and takes the last value. Lone surrogates become U+FFFD.
|
|
8
|
+
* Throws JsonSyntaxError on invalid text and NoIds when nested deeper than
|
|
9
|
+
* `maxDepth` containers.
|
|
10
|
+
*/
|
|
11
|
+
declare function parseJson(text: string | Uint8Array, maxDepth?: number): JsonValue;
|
|
12
|
+
/**
|
|
13
|
+
* The canonical JSON text of `v` (decision 0002): no whitespace, members in
|
|
14
|
+
* order (or sorted by code point with `sort`), minimal string escapes,
|
|
15
|
+
* ECMAScript number printing. Throws NoIds past 128 nested containers.
|
|
16
|
+
*/
|
|
17
|
+
declare function canonicalJson(v: JsonValue, sort?: boolean): string;
|
|
18
|
+
|
|
19
|
+
interface TextPart {
|
|
20
|
+
readonly t: "text";
|
|
21
|
+
readonly v: string;
|
|
22
|
+
}
|
|
23
|
+
interface ReasoningPart {
|
|
24
|
+
readonly t: "reasoning";
|
|
25
|
+
readonly v: string;
|
|
26
|
+
}
|
|
27
|
+
interface MediaPart {
|
|
28
|
+
readonly t: "media";
|
|
29
|
+
/** 32 raw bytes: SHA-256 of the decoded data, the URL or the file id. */
|
|
30
|
+
readonly sha256: Uint8Array;
|
|
31
|
+
readonly mime: string;
|
|
32
|
+
readonly src: "bytes" | "url" | "file_id";
|
|
33
|
+
}
|
|
34
|
+
interface ToolCallPart {
|
|
35
|
+
readonly t: "tool_call";
|
|
36
|
+
readonly name: string;
|
|
37
|
+
/** Canonical JSON text of the arguments, when they parse. */
|
|
38
|
+
readonly args?: string;
|
|
39
|
+
/** The arguments exactly as sent, when they do not parse. */
|
|
40
|
+
readonly args_raw?: string;
|
|
41
|
+
readonly ref: number;
|
|
42
|
+
}
|
|
43
|
+
interface ToolResultPart {
|
|
44
|
+
readonly t: "tool_result";
|
|
45
|
+
readonly ref: number;
|
|
46
|
+
readonly parts: readonly Part[];
|
|
47
|
+
readonly error?: true;
|
|
48
|
+
}
|
|
49
|
+
interface OpaquePart {
|
|
50
|
+
readonly t: "opaque";
|
|
51
|
+
readonly type: string;
|
|
52
|
+
readonly sha256: Uint8Array;
|
|
53
|
+
}
|
|
54
|
+
type Part = TextPart | ReasoningPart | MediaPart | ToolCallPart | ToolResultPart | OpaquePart;
|
|
55
|
+
interface Message {
|
|
56
|
+
readonly role: string;
|
|
57
|
+
readonly parts: readonly Part[];
|
|
58
|
+
/** Present only when the request gives a non-empty name. */
|
|
59
|
+
readonly name?: string;
|
|
60
|
+
}
|
|
61
|
+
interface FunctionTool {
|
|
62
|
+
readonly name: string;
|
|
63
|
+
readonly desc: string;
|
|
64
|
+
/** Canonical JSON text of the parameter schema ("null" when absent). */
|
|
65
|
+
readonly schema: string;
|
|
66
|
+
}
|
|
67
|
+
interface OtherTool {
|
|
68
|
+
readonly type: string;
|
|
69
|
+
/** Canonical JSON text of the whole definition. */
|
|
70
|
+
readonly def: string;
|
|
71
|
+
}
|
|
72
|
+
type Tool = FunctionTool | OtherTool;
|
|
73
|
+
/** The canonical form of one request. */
|
|
74
|
+
interface Canon {
|
|
75
|
+
readonly head: readonly Message[];
|
|
76
|
+
readonly tools: readonly Tool[];
|
|
77
|
+
readonly messages: readonly Message[];
|
|
78
|
+
/** True for a Responses request that continues server-side history. */
|
|
79
|
+
readonly stateful: boolean;
|
|
80
|
+
}
|
|
81
|
+
declare const HEAD_ROLES: ReadonlySet<string>;
|
|
82
|
+
/** The default size cap: 2 MiB of canonical CBOR (decision 0002). */
|
|
83
|
+
declare const DEFAULT_MAX_BYTES: number;
|
|
84
|
+
|
|
85
|
+
interface LooseProfile {
|
|
86
|
+
/** The profile label, for example "loose/1". Hashed into every loose label. */
|
|
87
|
+
readonly version: string;
|
|
88
|
+
/** (open, close) tag pairs whose blocks are removed, ASCII case-insensitively. */
|
|
89
|
+
readonly blockTags: readonly (readonly [string, string])[];
|
|
90
|
+
/** The code points that collapse to one space. */
|
|
91
|
+
readonly whitespace: ReadonlySet<number>;
|
|
92
|
+
/** (from, to) roles renamed in the loose form. Empty in loose/1. */
|
|
93
|
+
readonly roleMap: readonly (readonly [string, string])[];
|
|
94
|
+
}
|
|
95
|
+
/** Unicode White_Space plus U+001C..U+001F. */
|
|
96
|
+
declare const WHITESPACE_V1: ReadonlySet<number>;
|
|
97
|
+
/** The default profile, pinned by vectors/v1. */
|
|
98
|
+
declare const LOOSE_V1: LooseProfile;
|
|
99
|
+
/** loose/1 plus: a `developer` message reads as `system`. Experimental (decision 0005). */
|
|
100
|
+
declare const LOOSE_V2: LooseProfile;
|
|
101
|
+
/** Throws a TypeError when `p` is not a valid profile table. */
|
|
102
|
+
declare function validateProfile(p: LooseProfile): void;
|
|
103
|
+
/** 8 bytes that change with any part of the table (for keying caches). */
|
|
104
|
+
declare function profileFingerprint(p: LooseProfile): Uint8Array;
|
|
105
|
+
/** One text in its loose form ("" means the text is dropped). */
|
|
106
|
+
declare function looseText(v: string, profile?: LooseProfile): string;
|
|
107
|
+
/** One message in its loose form. */
|
|
108
|
+
declare function looseMessage(m: Message, profile?: LooseProfile): Message;
|
|
109
|
+
/** `canon` in its loose form. Never mutates its input. */
|
|
110
|
+
declare function loose(canon: Canon, profile?: LooseProfile): Canon;
|
|
111
|
+
|
|
112
|
+
/** The spec label hashed into every id. */
|
|
113
|
+
declare const SPEC_LABEL = "kvsig/1";
|
|
114
|
+
declare const KEY_BYTES = 32;
|
|
115
|
+
/** A deployment key: 32 secret bytes and the epoch byte that names them. */
|
|
116
|
+
declare class Key {
|
|
117
|
+
#private;
|
|
118
|
+
readonly epoch: number;
|
|
119
|
+
constructor(epoch: number, bytes: Uint8Array);
|
|
120
|
+
/**
|
|
121
|
+
* A new random key from Web Crypto `getRandomValues`. Epoch 0 is reserved for
|
|
122
|
+
* test keys. On Node 18, where `globalThis.crypto` is not defined by default,
|
|
123
|
+
* use `new Key(epoch, crypto.randomBytes(32))` from `node:crypto` instead.
|
|
124
|
+
*/
|
|
125
|
+
static generate(epoch: number): Key;
|
|
126
|
+
/** The secret bytes (a copy). */
|
|
127
|
+
bytes(): Uint8Array;
|
|
128
|
+
/** @internal */
|
|
129
|
+
mac(...parts: Uint8Array[]): Uint8Array;
|
|
130
|
+
toString(): string;
|
|
131
|
+
toJSON(): string;
|
|
132
|
+
}
|
|
133
|
+
/** The published test key of the spec (epoch 0, bytes 0x00..0x1f). Never protects real data. */
|
|
134
|
+
declare const TEST_KEY: Key;
|
|
135
|
+
/** The current key and the older keys still accepted during a rotation. */
|
|
136
|
+
declare class KeyRing {
|
|
137
|
+
readonly current: Key;
|
|
138
|
+
readonly accepted: readonly Key[];
|
|
139
|
+
constructor(current: Key, accepted?: readonly Key[]);
|
|
140
|
+
keys(): readonly Key[];
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* The scope bytes: empty, or SHA-256 of the CBOR map `{"cache_salt": text,
|
|
144
|
+
* "tenant": bytes}` with absent or empty members left out.
|
|
145
|
+
*/
|
|
146
|
+
declare function scopeFrom(opts?: {
|
|
147
|
+
cacheSalt?: string | null;
|
|
148
|
+
tenant?: Uint8Array | null;
|
|
149
|
+
}): Uint8Array;
|
|
150
|
+
/** Every id of one request, as 34-character lowercase hex strings. */
|
|
151
|
+
interface Ids {
|
|
152
|
+
readonly head: string;
|
|
153
|
+
readonly sys: string;
|
|
154
|
+
readonly tools: string;
|
|
155
|
+
readonly msgs: readonly string[];
|
|
156
|
+
/** Prefix ids; empty for a stateful request. */
|
|
157
|
+
readonly nodes: readonly string[];
|
|
158
|
+
readonly loose_head: string;
|
|
159
|
+
readonly loose_sys: string;
|
|
160
|
+
readonly loose_tools: string;
|
|
161
|
+
readonly loose_msgs: readonly string[];
|
|
162
|
+
readonly loose_nodes: readonly string[];
|
|
163
|
+
/** The conversation id, or null when there is none (or the request is stateful). */
|
|
164
|
+
readonly conv: string | null;
|
|
165
|
+
readonly epoch: number;
|
|
166
|
+
/** The loose profile the loose ids were computed under. */
|
|
167
|
+
readonly profile: string;
|
|
168
|
+
}
|
|
169
|
+
interface IdsOptions {
|
|
170
|
+
/** Scope bytes from {@link scopeFrom}; empty by default. */
|
|
171
|
+
scope?: Uint8Array;
|
|
172
|
+
/** The loose profile; `LOOSE_V1` by default. */
|
|
173
|
+
profile?: LooseProfile;
|
|
174
|
+
}
|
|
175
|
+
/** Every id of `canon` under `key`. */
|
|
176
|
+
declare function ids(canon: Canon, key: Key, opts?: IdsOptions): Ids;
|
|
177
|
+
/** The ids under every key of a ring, current key first. */
|
|
178
|
+
declare function idsAllEpochs(canon: Canon, ring: KeyRing, opts?: IdsOptions): Ids[];
|
|
179
|
+
interface MessageIdOptions {
|
|
180
|
+
scope?: Uint8Array;
|
|
181
|
+
/** Give the loose id under this profile; omit for the exact id. */
|
|
182
|
+
profile?: LooseProfile;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* The id of one message: exact, or loose with `profile`. For a reply from
|
|
186
|
+
* {@link canonicalizeReply} it equals the id of the same message in the next request.
|
|
187
|
+
*/
|
|
188
|
+
declare function messageId(m: Message, key: Key, opts?: MessageIdOptions): string;
|
|
189
|
+
|
|
190
|
+
/** SHA-256 of `data`. */
|
|
191
|
+
declare function sha256(data: Uint8Array): Uint8Array;
|
|
192
|
+
/** HMAC-SHA-256 of the concatenation of `parts` under `key`. */
|
|
193
|
+
declare function hmacSha256(key: Uint8Array, ...parts: Uint8Array[]): Uint8Array;
|
|
194
|
+
|
|
195
|
+
/** The library version; it matches the spec draft revision (0.1). */
|
|
196
|
+
declare const VERSION = "0.1.0";
|
|
197
|
+
/** The dialects this release implements. */
|
|
198
|
+
declare const SUPPORTED: readonly ["openai.chat", "openai.responses", "anthropic.messages"];
|
|
199
|
+
type Dialect = (typeof SUPPORTED)[number];
|
|
200
|
+
interface CanonicalizeOptions {
|
|
201
|
+
/** The size cap in bytes of canonical CBOR (default 2 MiB). */
|
|
202
|
+
maxBytes?: number;
|
|
203
|
+
/** Called with an unexpected internal error (a bug); the result is still null. */
|
|
204
|
+
onError?: (error: unknown) => void;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* A request or response body: a parsed value (as from `JSON.parse`), or the
|
|
208
|
+
* raw JSON text as a string or bytes. Pass the raw text when you have it:
|
|
209
|
+
* JavaScript objects reorder integer-like keys (`{"b":1,"10":2}`), and the
|
|
210
|
+
* exact form keeps the wire order of tool arguments and schemas.
|
|
211
|
+
*/
|
|
212
|
+
type Body = unknown;
|
|
213
|
+
/**
|
|
214
|
+
* The canonical form of a request body, or null when the request gets no ids:
|
|
215
|
+
* not a chat body, over `maxBytes` of canonical CBOR, or nested too deep.
|
|
216
|
+
* Throws only for an unknown dialect.
|
|
217
|
+
*/
|
|
218
|
+
declare function canonicalize(body: Body, dialect?: Dialect, opts?: CanonicalizeOptions): Canon | null;
|
|
219
|
+
/**
|
|
220
|
+
* A response's assistant message, canonicalised alone, so that its
|
|
221
|
+
* {@link messageId} equals the id of the same message in the next request.
|
|
222
|
+
*
|
|
223
|
+
* `message` is the assistant message (`openai.chat`: `choices[i].message`),
|
|
224
|
+
* the whole response (`anthropic.messages`), or the whole response with its
|
|
225
|
+
* `output` items (`openai.responses`).
|
|
226
|
+
*/
|
|
227
|
+
declare function canonicalizeReply(message: Body, dialect?: Dialect, opts?: CanonicalizeOptions): Message | null;
|
|
228
|
+
/** The canonical CBOR encoding (decision 0001) of a canonical value. */
|
|
229
|
+
declare function encode(v: unknown): Uint8Array;
|
|
230
|
+
/**
|
|
231
|
+
* The canonical bytes of a whole request, as the test vectors give them: the
|
|
232
|
+
* CBOR of `{"head": [...], "tools": [...], "messages": [...]}`.
|
|
233
|
+
*/
|
|
234
|
+
declare function canonicalBytes(c: Canon): Uint8Array;
|
|
235
|
+
/** The conversation id of a set of ids (null when there is none). */
|
|
236
|
+
declare function conversationId(i: {
|
|
237
|
+
readonly conv: string | null;
|
|
238
|
+
}): string | null;
|
|
239
|
+
|
|
240
|
+
export { type Body, type Canon, type CanonicalizeOptions, DEFAULT_MAX_BYTES, type Dialect, type FunctionTool, HEAD_ROLES, type Ids, type IdsOptions, type JsonObject, type JsonValue, KEY_BYTES, Key, KeyRing, LOOSE_V1, LOOSE_V2, type LooseProfile, type MediaPart, type Message, type MessageIdOptions, type OpaquePart, type OtherTool, type Part, type ReasoningPart, SPEC_LABEL, SUPPORTED, TEST_KEY, type TextPart, type Tool, type ToolCallPart, type ToolResultPart, VERSION, WHITESPACE_V1, canonicalBytes, canonicalJson, canonicalize, canonicalizeReply, conversationId, encode, hmacSha256, ids, idsAllEpochs, loose, looseMessage, looseText, messageId, parseJson, profileFingerprint, scopeFrom, sha256, validateProfile };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/** A JSON value with objects as order-preserving Maps. */
|
|
2
|
+
type JsonValue = null | boolean | number | string | JsonValue[] | JsonObject;
|
|
3
|
+
type JsonObject = Map<string, JsonValue>;
|
|
4
|
+
/**
|
|
5
|
+
* Parse JSON text strictly (RFC 8259): no NaN or Infinity, no leading zeros, no
|
|
6
|
+
* trailing text. Objects keep member order; a repeated member name keeps its
|
|
7
|
+
* first position and takes the last value. Lone surrogates become U+FFFD.
|
|
8
|
+
* Throws JsonSyntaxError on invalid text and NoIds when nested deeper than
|
|
9
|
+
* `maxDepth` containers.
|
|
10
|
+
*/
|
|
11
|
+
declare function parseJson(text: string | Uint8Array, maxDepth?: number): JsonValue;
|
|
12
|
+
/**
|
|
13
|
+
* The canonical JSON text of `v` (decision 0002): no whitespace, members in
|
|
14
|
+
* order (or sorted by code point with `sort`), minimal string escapes,
|
|
15
|
+
* ECMAScript number printing. Throws NoIds past 128 nested containers.
|
|
16
|
+
*/
|
|
17
|
+
declare function canonicalJson(v: JsonValue, sort?: boolean): string;
|
|
18
|
+
|
|
19
|
+
interface TextPart {
|
|
20
|
+
readonly t: "text";
|
|
21
|
+
readonly v: string;
|
|
22
|
+
}
|
|
23
|
+
interface ReasoningPart {
|
|
24
|
+
readonly t: "reasoning";
|
|
25
|
+
readonly v: string;
|
|
26
|
+
}
|
|
27
|
+
interface MediaPart {
|
|
28
|
+
readonly t: "media";
|
|
29
|
+
/** 32 raw bytes: SHA-256 of the decoded data, the URL or the file id. */
|
|
30
|
+
readonly sha256: Uint8Array;
|
|
31
|
+
readonly mime: string;
|
|
32
|
+
readonly src: "bytes" | "url" | "file_id";
|
|
33
|
+
}
|
|
34
|
+
interface ToolCallPart {
|
|
35
|
+
readonly t: "tool_call";
|
|
36
|
+
readonly name: string;
|
|
37
|
+
/** Canonical JSON text of the arguments, when they parse. */
|
|
38
|
+
readonly args?: string;
|
|
39
|
+
/** The arguments exactly as sent, when they do not parse. */
|
|
40
|
+
readonly args_raw?: string;
|
|
41
|
+
readonly ref: number;
|
|
42
|
+
}
|
|
43
|
+
interface ToolResultPart {
|
|
44
|
+
readonly t: "tool_result";
|
|
45
|
+
readonly ref: number;
|
|
46
|
+
readonly parts: readonly Part[];
|
|
47
|
+
readonly error?: true;
|
|
48
|
+
}
|
|
49
|
+
interface OpaquePart {
|
|
50
|
+
readonly t: "opaque";
|
|
51
|
+
readonly type: string;
|
|
52
|
+
readonly sha256: Uint8Array;
|
|
53
|
+
}
|
|
54
|
+
type Part = TextPart | ReasoningPart | MediaPart | ToolCallPart | ToolResultPart | OpaquePart;
|
|
55
|
+
interface Message {
|
|
56
|
+
readonly role: string;
|
|
57
|
+
readonly parts: readonly Part[];
|
|
58
|
+
/** Present only when the request gives a non-empty name. */
|
|
59
|
+
readonly name?: string;
|
|
60
|
+
}
|
|
61
|
+
interface FunctionTool {
|
|
62
|
+
readonly name: string;
|
|
63
|
+
readonly desc: string;
|
|
64
|
+
/** Canonical JSON text of the parameter schema ("null" when absent). */
|
|
65
|
+
readonly schema: string;
|
|
66
|
+
}
|
|
67
|
+
interface OtherTool {
|
|
68
|
+
readonly type: string;
|
|
69
|
+
/** Canonical JSON text of the whole definition. */
|
|
70
|
+
readonly def: string;
|
|
71
|
+
}
|
|
72
|
+
type Tool = FunctionTool | OtherTool;
|
|
73
|
+
/** The canonical form of one request. */
|
|
74
|
+
interface Canon {
|
|
75
|
+
readonly head: readonly Message[];
|
|
76
|
+
readonly tools: readonly Tool[];
|
|
77
|
+
readonly messages: readonly Message[];
|
|
78
|
+
/** True for a Responses request that continues server-side history. */
|
|
79
|
+
readonly stateful: boolean;
|
|
80
|
+
}
|
|
81
|
+
declare const HEAD_ROLES: ReadonlySet<string>;
|
|
82
|
+
/** The default size cap: 2 MiB of canonical CBOR (decision 0002). */
|
|
83
|
+
declare const DEFAULT_MAX_BYTES: number;
|
|
84
|
+
|
|
85
|
+
interface LooseProfile {
|
|
86
|
+
/** The profile label, for example "loose/1". Hashed into every loose label. */
|
|
87
|
+
readonly version: string;
|
|
88
|
+
/** (open, close) tag pairs whose blocks are removed, ASCII case-insensitively. */
|
|
89
|
+
readonly blockTags: readonly (readonly [string, string])[];
|
|
90
|
+
/** The code points that collapse to one space. */
|
|
91
|
+
readonly whitespace: ReadonlySet<number>;
|
|
92
|
+
/** (from, to) roles renamed in the loose form. Empty in loose/1. */
|
|
93
|
+
readonly roleMap: readonly (readonly [string, string])[];
|
|
94
|
+
}
|
|
95
|
+
/** Unicode White_Space plus U+001C..U+001F. */
|
|
96
|
+
declare const WHITESPACE_V1: ReadonlySet<number>;
|
|
97
|
+
/** The default profile, pinned by vectors/v1. */
|
|
98
|
+
declare const LOOSE_V1: LooseProfile;
|
|
99
|
+
/** loose/1 plus: a `developer` message reads as `system`. Experimental (decision 0005). */
|
|
100
|
+
declare const LOOSE_V2: LooseProfile;
|
|
101
|
+
/** Throws a TypeError when `p` is not a valid profile table. */
|
|
102
|
+
declare function validateProfile(p: LooseProfile): void;
|
|
103
|
+
/** 8 bytes that change with any part of the table (for keying caches). */
|
|
104
|
+
declare function profileFingerprint(p: LooseProfile): Uint8Array;
|
|
105
|
+
/** One text in its loose form ("" means the text is dropped). */
|
|
106
|
+
declare function looseText(v: string, profile?: LooseProfile): string;
|
|
107
|
+
/** One message in its loose form. */
|
|
108
|
+
declare function looseMessage(m: Message, profile?: LooseProfile): Message;
|
|
109
|
+
/** `canon` in its loose form. Never mutates its input. */
|
|
110
|
+
declare function loose(canon: Canon, profile?: LooseProfile): Canon;
|
|
111
|
+
|
|
112
|
+
/** The spec label hashed into every id. */
|
|
113
|
+
declare const SPEC_LABEL = "kvsig/1";
|
|
114
|
+
declare const KEY_BYTES = 32;
|
|
115
|
+
/** A deployment key: 32 secret bytes and the epoch byte that names them. */
|
|
116
|
+
declare class Key {
|
|
117
|
+
#private;
|
|
118
|
+
readonly epoch: number;
|
|
119
|
+
constructor(epoch: number, bytes: Uint8Array);
|
|
120
|
+
/**
|
|
121
|
+
* A new random key from Web Crypto `getRandomValues`. Epoch 0 is reserved for
|
|
122
|
+
* test keys. On Node 18, where `globalThis.crypto` is not defined by default,
|
|
123
|
+
* use `new Key(epoch, crypto.randomBytes(32))` from `node:crypto` instead.
|
|
124
|
+
*/
|
|
125
|
+
static generate(epoch: number): Key;
|
|
126
|
+
/** The secret bytes (a copy). */
|
|
127
|
+
bytes(): Uint8Array;
|
|
128
|
+
/** @internal */
|
|
129
|
+
mac(...parts: Uint8Array[]): Uint8Array;
|
|
130
|
+
toString(): string;
|
|
131
|
+
toJSON(): string;
|
|
132
|
+
}
|
|
133
|
+
/** The published test key of the spec (epoch 0, bytes 0x00..0x1f). Never protects real data. */
|
|
134
|
+
declare const TEST_KEY: Key;
|
|
135
|
+
/** The current key and the older keys still accepted during a rotation. */
|
|
136
|
+
declare class KeyRing {
|
|
137
|
+
readonly current: Key;
|
|
138
|
+
readonly accepted: readonly Key[];
|
|
139
|
+
constructor(current: Key, accepted?: readonly Key[]);
|
|
140
|
+
keys(): readonly Key[];
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* The scope bytes: empty, or SHA-256 of the CBOR map `{"cache_salt": text,
|
|
144
|
+
* "tenant": bytes}` with absent or empty members left out.
|
|
145
|
+
*/
|
|
146
|
+
declare function scopeFrom(opts?: {
|
|
147
|
+
cacheSalt?: string | null;
|
|
148
|
+
tenant?: Uint8Array | null;
|
|
149
|
+
}): Uint8Array;
|
|
150
|
+
/** Every id of one request, as 34-character lowercase hex strings. */
|
|
151
|
+
interface Ids {
|
|
152
|
+
readonly head: string;
|
|
153
|
+
readonly sys: string;
|
|
154
|
+
readonly tools: string;
|
|
155
|
+
readonly msgs: readonly string[];
|
|
156
|
+
/** Prefix ids; empty for a stateful request. */
|
|
157
|
+
readonly nodes: readonly string[];
|
|
158
|
+
readonly loose_head: string;
|
|
159
|
+
readonly loose_sys: string;
|
|
160
|
+
readonly loose_tools: string;
|
|
161
|
+
readonly loose_msgs: readonly string[];
|
|
162
|
+
readonly loose_nodes: readonly string[];
|
|
163
|
+
/** The conversation id, or null when there is none (or the request is stateful). */
|
|
164
|
+
readonly conv: string | null;
|
|
165
|
+
readonly epoch: number;
|
|
166
|
+
/** The loose profile the loose ids were computed under. */
|
|
167
|
+
readonly profile: string;
|
|
168
|
+
}
|
|
169
|
+
interface IdsOptions {
|
|
170
|
+
/** Scope bytes from {@link scopeFrom}; empty by default. */
|
|
171
|
+
scope?: Uint8Array;
|
|
172
|
+
/** The loose profile; `LOOSE_V1` by default. */
|
|
173
|
+
profile?: LooseProfile;
|
|
174
|
+
}
|
|
175
|
+
/** Every id of `canon` under `key`. */
|
|
176
|
+
declare function ids(canon: Canon, key: Key, opts?: IdsOptions): Ids;
|
|
177
|
+
/** The ids under every key of a ring, current key first. */
|
|
178
|
+
declare function idsAllEpochs(canon: Canon, ring: KeyRing, opts?: IdsOptions): Ids[];
|
|
179
|
+
interface MessageIdOptions {
|
|
180
|
+
scope?: Uint8Array;
|
|
181
|
+
/** Give the loose id under this profile; omit for the exact id. */
|
|
182
|
+
profile?: LooseProfile;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* The id of one message: exact, or loose with `profile`. For a reply from
|
|
186
|
+
* {@link canonicalizeReply} it equals the id of the same message in the next request.
|
|
187
|
+
*/
|
|
188
|
+
declare function messageId(m: Message, key: Key, opts?: MessageIdOptions): string;
|
|
189
|
+
|
|
190
|
+
/** SHA-256 of `data`. */
|
|
191
|
+
declare function sha256(data: Uint8Array): Uint8Array;
|
|
192
|
+
/** HMAC-SHA-256 of the concatenation of `parts` under `key`. */
|
|
193
|
+
declare function hmacSha256(key: Uint8Array, ...parts: Uint8Array[]): Uint8Array;
|
|
194
|
+
|
|
195
|
+
/** The library version; it matches the spec draft revision (0.1). */
|
|
196
|
+
declare const VERSION = "0.1.0";
|
|
197
|
+
/** The dialects this release implements. */
|
|
198
|
+
declare const SUPPORTED: readonly ["openai.chat", "openai.responses", "anthropic.messages"];
|
|
199
|
+
type Dialect = (typeof SUPPORTED)[number];
|
|
200
|
+
interface CanonicalizeOptions {
|
|
201
|
+
/** The size cap in bytes of canonical CBOR (default 2 MiB). */
|
|
202
|
+
maxBytes?: number;
|
|
203
|
+
/** Called with an unexpected internal error (a bug); the result is still null. */
|
|
204
|
+
onError?: (error: unknown) => void;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* A request or response body: a parsed value (as from `JSON.parse`), or the
|
|
208
|
+
* raw JSON text as a string or bytes. Pass the raw text when you have it:
|
|
209
|
+
* JavaScript objects reorder integer-like keys (`{"b":1,"10":2}`), and the
|
|
210
|
+
* exact form keeps the wire order of tool arguments and schemas.
|
|
211
|
+
*/
|
|
212
|
+
type Body = unknown;
|
|
213
|
+
/**
|
|
214
|
+
* The canonical form of a request body, or null when the request gets no ids:
|
|
215
|
+
* not a chat body, over `maxBytes` of canonical CBOR, or nested too deep.
|
|
216
|
+
* Throws only for an unknown dialect.
|
|
217
|
+
*/
|
|
218
|
+
declare function canonicalize(body: Body, dialect?: Dialect, opts?: CanonicalizeOptions): Canon | null;
|
|
219
|
+
/**
|
|
220
|
+
* A response's assistant message, canonicalised alone, so that its
|
|
221
|
+
* {@link messageId} equals the id of the same message in the next request.
|
|
222
|
+
*
|
|
223
|
+
* `message` is the assistant message (`openai.chat`: `choices[i].message`),
|
|
224
|
+
* the whole response (`anthropic.messages`), or the whole response with its
|
|
225
|
+
* `output` items (`openai.responses`).
|
|
226
|
+
*/
|
|
227
|
+
declare function canonicalizeReply(message: Body, dialect?: Dialect, opts?: CanonicalizeOptions): Message | null;
|
|
228
|
+
/** The canonical CBOR encoding (decision 0001) of a canonical value. */
|
|
229
|
+
declare function encode(v: unknown): Uint8Array;
|
|
230
|
+
/**
|
|
231
|
+
* The canonical bytes of a whole request, as the test vectors give them: the
|
|
232
|
+
* CBOR of `{"head": [...], "tools": [...], "messages": [...]}`.
|
|
233
|
+
*/
|
|
234
|
+
declare function canonicalBytes(c: Canon): Uint8Array;
|
|
235
|
+
/** The conversation id of a set of ids (null when there is none). */
|
|
236
|
+
declare function conversationId(i: {
|
|
237
|
+
readonly conv: string | null;
|
|
238
|
+
}): string | null;
|
|
239
|
+
|
|
240
|
+
export { type Body, type Canon, type CanonicalizeOptions, DEFAULT_MAX_BYTES, type Dialect, type FunctionTool, HEAD_ROLES, type Ids, type IdsOptions, type JsonObject, type JsonValue, KEY_BYTES, Key, KeyRing, LOOSE_V1, LOOSE_V2, type LooseProfile, type MediaPart, type Message, type MessageIdOptions, type OpaquePart, type OtherTool, type Part, type ReasoningPart, SPEC_LABEL, SUPPORTED, TEST_KEY, type TextPart, type Tool, type ToolCallPart, type ToolResultPart, VERSION, WHITESPACE_V1, canonicalBytes, canonicalJson, canonicalize, canonicalizeReply, conversationId, encode, hmacSha256, ids, idsAllEpochs, loose, looseMessage, looseText, messageId, parseJson, profileFingerprint, scopeFrom, sha256, validateProfile };
|