@tansr/protocol 0.2.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 +21 -0
- package/README.md +74 -0
- package/dist/canonical/common.d.ts +37 -0
- package/dist/canonical/common.js +149 -0
- package/dist/canonical/digest.d.ts +12 -0
- package/dist/canonical/digest.js +58 -0
- package/dist/canonical/encode.d.ts +15 -0
- package/dist/canonical/encode.js +157 -0
- package/dist/canonical/errors.d.ts +81 -0
- package/dist/canonical/errors.js +89 -0
- package/dist/canonical/index.d.ts +21 -0
- package/dist/canonical/index.js +21 -0
- package/dist/canonical/parse.d.ts +21 -0
- package/dist/canonical/parse.js +295 -0
- package/dist/client.d.ts +42 -0
- package/dist/client.js +1 -0
- package/dist/config.d.ts +4990 -0
- package/dist/config.js +657 -0
- package/dist/daemon.d.ts +70 -0
- package/dist/daemon.js +48 -0
- package/dist/events.d.ts +2152 -0
- package/dist/events.js +625 -0
- package/dist/governance.d.ts +445 -0
- package/dist/governance.js +183 -0
- package/dist/headless.d.ts +97 -0
- package/dist/headless.js +39 -0
- package/dist/identity.d.ts +96 -0
- package/dist/identity.js +45 -0
- package/dist/index.d.ts +525 -0
- package/dist/index.js +529 -0
- package/dist/ir.d.ts +1418 -0
- package/dist/ir.js +283 -0
- package/dist/journal.d.ts +297 -0
- package/dist/journal.js +150 -0
- package/dist/media.d.ts +563 -0
- package/dist/media.js +190 -0
- package/dist/metering.d.ts +225 -0
- package/dist/metering.js +90 -0
- package/dist/orchestration.d.ts +1551 -0
- package/dist/orchestration.js +333 -0
- package/dist/redact-patterns.d.ts +49 -0
- package/dist/redact-patterns.js +141 -0
- package/dist/storage.d.ts +507 -0
- package/dist/storage.js +110 -0
- package/dist/tool-result-artifact.d.ts +257 -0
- package/dist/tool-result-artifact.js +263 -0
- package/package.json +30 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* canonical 单模块 — 统一错误对象与码表(RFC-UAPI-1 §4;D7 解冻迁入,UAPI-01 U3-PROTO)。
|
|
3
|
+
*
|
|
4
|
+
* 错误对象恒为 `{ code, path?, offset?, message }`(+ `reason?` 仅 `not_canonical`):
|
|
5
|
+
* - `code`:下表之一(稳定小码表;三份薄封装各自映射到既有消息 / 既有码);
|
|
6
|
+
* - `path`:RFC 6901 JSON Pointer(根 = `''`,`/a/0/b`),编码侧指向违规 JS 值,解码侧指向违规节点或其容器;
|
|
7
|
+
* - `offset`:仅解码侧;原始输入的 **UTF-8 字节偏移**(非 UTF-16 码元),跨语言实现可直接对照;
|
|
8
|
+
* - `reason`:仅 `not_canonical` 附带,`'whitespace' | 'key_order' | 'escape'`(首个违规的类别)。
|
|
9
|
+
*
|
|
10
|
+
* 零 Node 依赖:本文件只用 ECMAScript 内建。
|
|
11
|
+
*/
|
|
12
|
+
/** 码表。解码侧 / 编码侧 / 双侧 见各行注释。 */
|
|
13
|
+
export const CANONICAL_ERROR_CODES = Object.freeze({
|
|
14
|
+
/** 双侧:选项非法(maxBytes 缺失 / 非正安全整数、maxDepth > 32、maxNodes > 100000、未知选项、sha256 非函数…) */
|
|
15
|
+
invalid_options: 'invalid_options',
|
|
16
|
+
/** 解码侧:输入既非 Uint8Array 亦非 string;摘要侧:bytes 非 Uint8Array */
|
|
17
|
+
invalid_input: 'invalid_input',
|
|
18
|
+
/** 双侧:解码 = 原始输入字节 > maxBytes(先于 UTF-8 解码);编码 = 累计输出 UTF-8 字节 > maxBytes */
|
|
19
|
+
bytes_exceeded: 'bytes_exceeded',
|
|
20
|
+
/** 解码侧:非法 UTF-8(过长编码、截断、CESU-8 代理项、> U+10FFFF) */
|
|
21
|
+
invalid_utf8: 'invalid_utf8',
|
|
22
|
+
/** 解码侧:输入在值 / 字符串 / 字面量 / 转义中途结束(含空输入) */
|
|
23
|
+
unexpected_end: 'unexpected_end',
|
|
24
|
+
/** 解码侧:结构错误(非法起始字符、缺冒号 / 逗号、未引号键、大写字面量、NaN / Infinity、BOM、非 JSON 空白) */
|
|
25
|
+
unexpected_token: 'unexpected_token',
|
|
26
|
+
/** 解码侧:根值之后仍有非空白字节 */
|
|
27
|
+
trailing_data: 'trailing_data',
|
|
28
|
+
/** 双侧:数字 token 不是 `0 | [1-9][0-9]*`(-0、负数、+1、.5、1e3、1.0、01、0x10);编码 = 非整数 / 负数 / -0 / NaN / Infinity */
|
|
29
|
+
invalid_number: 'invalid_number',
|
|
30
|
+
/** 双侧:整数超过 2^53−1 */
|
|
31
|
+
unsafe_integer: 'unsafe_integer',
|
|
32
|
+
/** 解码侧:字符串词法错误(非法转义、`\u` 不足 4 位十六进制、未转义控制字符 < 0x20) */
|
|
33
|
+
invalid_string: 'invalid_string',
|
|
34
|
+
/** 双侧:孤立 / 颠倒 / 高后非低的代理项(转义或 JS 字符串内) */
|
|
35
|
+
lone_surrogate: 'lone_surrogate',
|
|
36
|
+
/** 双侧:键不是非空可打印 ASCII(`^[\x21-\x7e]+$`) */
|
|
37
|
+
invalid_key: 'invalid_key',
|
|
38
|
+
/** 解码侧:同一对象内解码后相同的键(含 `\u0061` 与 `a`、`__proto__` 重复) */
|
|
39
|
+
duplicate_key: 'duplicate_key',
|
|
40
|
+
/** 双侧:节点祖先容器数 > maxDepth(根 = 0) */
|
|
41
|
+
depth_exceeded: 'depth_exceeded',
|
|
42
|
+
/** 双侧:节点总数(含根、容器与标量;键不计)> maxNodes */
|
|
43
|
+
nodes_exceeded: 'nodes_exceeded',
|
|
44
|
+
/** 编码侧:JS 值不可表示(undefined / function / symbol / bigint / 非 plain 对象 / Proxy) */
|
|
45
|
+
unsupported_value: 'unsupported_value',
|
|
46
|
+
/** 编码侧:属性形态非法(accessor、不可枚举、Symbol 键、稀疏数组、length 与自有键不符) */
|
|
47
|
+
invalid_property: 'invalid_property',
|
|
48
|
+
/** 编码侧:同一对象出现在自己的祖先链上 */
|
|
49
|
+
cyclic_reference: 'cyclic_reference',
|
|
50
|
+
/** 仅 parseStrict:输入是合法可归一化文档,但字节 ≠ 其 canonical 字节(reason = whitespace / key_order / escape) */
|
|
51
|
+
not_canonical: 'not_canonical',
|
|
52
|
+
});
|
|
53
|
+
/** `not_canonical` 的首个违规类别 */
|
|
54
|
+
export const NOT_CANONICAL_REASONS = Object.freeze(['whitespace', 'key_order', 'escape']);
|
|
55
|
+
/** 统一错误:`{ code, path?, offset?, message }`(+ `reason?` 仅 not_canonical);`toJSON()` 只输出这些字段。 */
|
|
56
|
+
export class CanonicalError extends Error {
|
|
57
|
+
code;
|
|
58
|
+
path;
|
|
59
|
+
offset;
|
|
60
|
+
reason;
|
|
61
|
+
constructor(code, message, detail = {}) {
|
|
62
|
+
super(message);
|
|
63
|
+
this.name = 'CanonicalError';
|
|
64
|
+
this.code = code;
|
|
65
|
+
if (detail.path !== undefined)
|
|
66
|
+
this.path = detail.path;
|
|
67
|
+
if (detail.offset !== undefined)
|
|
68
|
+
this.offset = detail.offset;
|
|
69
|
+
if (detail.reason !== undefined)
|
|
70
|
+
this.reason = detail.reason;
|
|
71
|
+
}
|
|
72
|
+
toJSON() {
|
|
73
|
+
const json = { code: this.code, message: this.message };
|
|
74
|
+
if (this.path !== undefined)
|
|
75
|
+
json.path = this.path;
|
|
76
|
+
if (this.offset !== undefined)
|
|
77
|
+
json.offset = this.offset;
|
|
78
|
+
if (this.reason !== undefined)
|
|
79
|
+
json.reason = this.reason;
|
|
80
|
+
return json;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** RFC 6901:`~` → `~0`,`/` → `~1`;键已限定为可打印 ASCII,其余字符原样。 */
|
|
84
|
+
export function pointerSegment(key) {
|
|
85
|
+
return typeof key === 'number' ? String(key) : key.replaceAll('~', '~0').replaceAll('/', '~1');
|
|
86
|
+
}
|
|
87
|
+
export function childPointer(parent, key) {
|
|
88
|
+
return parent + '/' + pointerSegment(key);
|
|
89
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @tansr/protocol/canonical — canonical 单模块(RFC-UAPI-1 §4;会签 D7 结案后自 `scripts/api/draft-canonical/` 迁入,
|
|
3
|
+
* UAPI-01 U3-PROTO,2026-10-01;`@tansr/protocol` 0.1.0 → 0.2.0,见 `packages/protocol/CHANGELOG.md`)。
|
|
4
|
+
*
|
|
5
|
+
* 纯 ESM、零 Node 依赖:只用 TextEncoder / TextDecoder / JSON / Reflect;SHA-256 由调用方注入。
|
|
6
|
+
* 本子入口不从 `@tansr/protocol` 根入口再导出,根导出集零变化。
|
|
7
|
+
*
|
|
8
|
+
* 公开 API:
|
|
9
|
+
* - `encodeCanonical(value, { maxBytes, maxDepth?, maxNodes?, isProxy? }) → Uint8Array`
|
|
10
|
+
* - `encodeCanonicalText(value, options) → string`(同字节的文本形)
|
|
11
|
+
* - `parseStrict(input, { maxBytes, maxDepth?, maxNodes? }) → unknown`(严格 canonical;非 canonical 即 `not_canonical`)
|
|
12
|
+
* - `decodeCanonical(input, options) → unknown`(归一化解码;kernel / providers 现语义)
|
|
13
|
+
* - `domainDigest(domain, bytes, sha256) → Uint8Array | Promise<Uint8Array>`(`SHA256(UTF8(domain) ‖ 0x00 ‖ bytes)`)
|
|
14
|
+
* - `frameDomain` / `toHex` / `subtleSha256`(摘要辅助)
|
|
15
|
+
* - `CanonicalError` / `CANONICAL_ERROR_CODES` / `NOT_CANONICAL_REASONS` / `CANONICAL_MAX_DEPTH` / `CANONICAL_MAX_NODES` / `CANONICAL_KEY_PATTERN`
|
|
16
|
+
*/
|
|
17
|
+
export { encodeCanonical, encodeCanonicalText } from './encode.js';
|
|
18
|
+
export { decodeCanonical, parseStrict } from './parse.js';
|
|
19
|
+
export { domainDigest, frameDomain, subtleSha256, toHex, type Sha256Function } from './digest.js';
|
|
20
|
+
export { CanonicalError, CANONICAL_ERROR_CODES, NOT_CANONICAL_REASONS, type CanonicalErrorCode, type CanonicalErrorDetail, type CanonicalErrorJson, type NotCanonicalReason, } from './errors.js';
|
|
21
|
+
export { CANONICAL_KEY_PATTERN, CANONICAL_MAX_DEPTH, CANONICAL_MAX_NODES, type CanonicalDecodeOptions, type CanonicalEncodeOptions, } from './common.js';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @tansr/protocol/canonical — canonical 单模块(RFC-UAPI-1 §4;会签 D7 结案后自 `scripts/api/draft-canonical/` 迁入,
|
|
3
|
+
* UAPI-01 U3-PROTO,2026-10-01;`@tansr/protocol` 0.1.0 → 0.2.0,见 `packages/protocol/CHANGELOG.md`)。
|
|
4
|
+
*
|
|
5
|
+
* 纯 ESM、零 Node 依赖:只用 TextEncoder / TextDecoder / JSON / Reflect;SHA-256 由调用方注入。
|
|
6
|
+
* 本子入口不从 `@tansr/protocol` 根入口再导出,根导出集零变化。
|
|
7
|
+
*
|
|
8
|
+
* 公开 API:
|
|
9
|
+
* - `encodeCanonical(value, { maxBytes, maxDepth?, maxNodes?, isProxy? }) → Uint8Array`
|
|
10
|
+
* - `encodeCanonicalText(value, options) → string`(同字节的文本形)
|
|
11
|
+
* - `parseStrict(input, { maxBytes, maxDepth?, maxNodes? }) → unknown`(严格 canonical;非 canonical 即 `not_canonical`)
|
|
12
|
+
* - `decodeCanonical(input, options) → unknown`(归一化解码;kernel / providers 现语义)
|
|
13
|
+
* - `domainDigest(domain, bytes, sha256) → Uint8Array | Promise<Uint8Array>`(`SHA256(UTF8(domain) ‖ 0x00 ‖ bytes)`)
|
|
14
|
+
* - `frameDomain` / `toHex` / `subtleSha256`(摘要辅助)
|
|
15
|
+
* - `CanonicalError` / `CANONICAL_ERROR_CODES` / `NOT_CANONICAL_REASONS` / `CANONICAL_MAX_DEPTH` / `CANONICAL_MAX_NODES` / `CANONICAL_KEY_PATTERN`
|
|
16
|
+
*/
|
|
17
|
+
export { encodeCanonical, encodeCanonicalText } from './encode.js';
|
|
18
|
+
export { decodeCanonical, parseStrict } from './parse.js';
|
|
19
|
+
export { domainDigest, frameDomain, subtleSha256, toHex } from './digest.js';
|
|
20
|
+
export { CanonicalError, CANONICAL_ERROR_CODES, NOT_CANONICAL_REASONS, } from './errors.js';
|
|
21
|
+
export { CANONICAL_KEY_PATTERN, CANONICAL_MAX_DEPTH, CANONICAL_MAX_NODES, } from './common.js';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* canonical 单模块 — 解码器双入口(RFC-UAPI-1 §4.1、§4.3、§6 第 5 条)。
|
|
3
|
+
*
|
|
4
|
+
* - `decodeCanonical(input, options)`:归一化解码。严格词法(重复解码键、`-0` / `1e3` / `1.0` / `01`、非法 UTF-8、
|
|
5
|
+
* 孤立代理项、深度 / 节点 / 原始字节上限、`__proto__` 自有属性)之上,**接受**空白 / 键乱序 / 等价转义等合法 JSON,
|
|
6
|
+
* 返回值再经 `encodeCanonical` 即得 canonical 字节。语义 = kernel `decodeArchiveMetadata` / providers `decodeCacheControl`。
|
|
7
|
+
* - `parseStrict(input, options)`:严格 canonical。规则同上,且输入字节必须已等于其 canonical 字节;否则以
|
|
8
|
+
* `not_canonical` 拒绝并给出**首个**违规的字节偏移与类别(whitespace / key_order / escape)。
|
|
9
|
+
* 语义 = api-client `decodeControl`(canonical-only),但错误可区分词法错误与非 canonical。
|
|
10
|
+
*
|
|
11
|
+
* 两入口共用一趟扫描:词法 / 规则错误立即抛出(优先于 not_canonical);canonical 违规只记录首个,文档解析成功后再抛,
|
|
12
|
+
* 因此 `parseStrict(x)` 成功 ⇔ `decodeCanonical(x)` 成功 ∧ `x == encodeCanonical(decodeCanonical(x))`。
|
|
13
|
+
*
|
|
14
|
+
* 输入可为 Uint8Array(原始字节,fatal UTF-8 解码)或 string(视作该串的 UTF-8 编码);`offset` 恒为 UTF-8 字节偏移。
|
|
15
|
+
* 返回对象为 null 原型,键为自有数据属性(含 `__proto__`)。
|
|
16
|
+
*/
|
|
17
|
+
import { type CanonicalDecodeOptions } from './common.js';
|
|
18
|
+
/** 归一化解码(接受合法非 canonical 输入;kernel / providers 现语义)。 */
|
|
19
|
+
export declare function decodeCanonical(input: Uint8Array | string, options: CanonicalDecodeOptions): unknown;
|
|
20
|
+
/** 严格 canonical 解码(字节须已是 canonical;api-client 现语义 + `not_canonical` 定位)。 */
|
|
21
|
+
export declare function parseStrict(input: Uint8Array | string, options: CanonicalDecodeOptions): unknown;
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* canonical 单模块 — 解码器双入口(RFC-UAPI-1 §4.1、§4.3、§6 第 5 条)。
|
|
3
|
+
*
|
|
4
|
+
* - `decodeCanonical(input, options)`:归一化解码。严格词法(重复解码键、`-0` / `1e3` / `1.0` / `01`、非法 UTF-8、
|
|
5
|
+
* 孤立代理项、深度 / 节点 / 原始字节上限、`__proto__` 自有属性)之上,**接受**空白 / 键乱序 / 等价转义等合法 JSON,
|
|
6
|
+
* 返回值再经 `encodeCanonical` 即得 canonical 字节。语义 = kernel `decodeArchiveMetadata` / providers `decodeCacheControl`。
|
|
7
|
+
* - `parseStrict(input, options)`:严格 canonical。规则同上,且输入字节必须已等于其 canonical 字节;否则以
|
|
8
|
+
* `not_canonical` 拒绝并给出**首个**违规的字节偏移与类别(whitespace / key_order / escape)。
|
|
9
|
+
* 语义 = api-client `decodeControl`(canonical-only),但错误可区分词法错误与非 canonical。
|
|
10
|
+
*
|
|
11
|
+
* 两入口共用一趟扫描:词法 / 规则错误立即抛出(优先于 not_canonical);canonical 违规只记录首个,文档解析成功后再抛,
|
|
12
|
+
* 因此 `parseStrict(x)` 成功 ⇔ `decodeCanonical(x)` 成功 ∧ `x == encodeCanonical(decodeCanonical(x))`。
|
|
13
|
+
*
|
|
14
|
+
* 输入可为 Uint8Array(原始字节,fatal UTF-8 解码)或 string(视作该串的 UTF-8 编码);`offset` 恒为 UTF-8 字节偏移。
|
|
15
|
+
* 返回对象为 null 原型,键为自有数据属性(含 `__proto__`)。
|
|
16
|
+
*/
|
|
17
|
+
import { CANONICAL_KEY_PATTERN, byteLengthOf, firstInvalidUtf8, isUint8Array, normalizeOptions, utf8Length, wellFormed } from './common.js';
|
|
18
|
+
import { CanonicalError, childPointer } from './errors.js';
|
|
19
|
+
const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
|
|
20
|
+
/** 整体匹配一个 JSON 字符串 token(粘性);未转义控制字符、非法转义、未终止均不匹配,再由 diagnoseString 定位。 */
|
|
21
|
+
const STRING_TOKEN = /"(?:[^"\\\u0000-\u001f]|\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4}))*"/y;
|
|
22
|
+
const HEX4 = /^[0-9a-fA-F]{4}$/;
|
|
23
|
+
const SHORT_ESCAPED = new Set([0x08, 0x09, 0x0a, 0x0c, 0x0d]);
|
|
24
|
+
const isWhitespace = (code) => code === 0x20 || code === 0x09 || code === 0x0a || code === 0x0d;
|
|
25
|
+
const isDigit = (code) => code >= 0x30 && code <= 0x39;
|
|
26
|
+
class Parser {
|
|
27
|
+
text;
|
|
28
|
+
strict;
|
|
29
|
+
maxDepth;
|
|
30
|
+
maxNodes;
|
|
31
|
+
at = 0;
|
|
32
|
+
nodes = 0;
|
|
33
|
+
/** 首个 canonical 违规(仅 strict) */
|
|
34
|
+
violation = null;
|
|
35
|
+
constructor(text, strict, { maxDepth, maxNodes }) {
|
|
36
|
+
this.text = text;
|
|
37
|
+
this.strict = strict;
|
|
38
|
+
this.maxDepth = maxDepth;
|
|
39
|
+
this.maxNodes = maxNodes;
|
|
40
|
+
}
|
|
41
|
+
byteOffset(at) {
|
|
42
|
+
return utf8Length(this.text.slice(0, at));
|
|
43
|
+
}
|
|
44
|
+
fail(code, message, path, at = this.at) {
|
|
45
|
+
throw new CanonicalError(code, message, { path, offset: this.byteOffset(at) });
|
|
46
|
+
}
|
|
47
|
+
note(reason, path, at) {
|
|
48
|
+
if (this.strict && this.violation === null)
|
|
49
|
+
this.violation = { reason, path, at };
|
|
50
|
+
}
|
|
51
|
+
document() {
|
|
52
|
+
const value = this.value(0, '');
|
|
53
|
+
this.whitespace('');
|
|
54
|
+
if (this.at !== this.text.length)
|
|
55
|
+
this.fail('trailing_data', 'unexpected bytes after the root value', '');
|
|
56
|
+
if (this.violation !== null) {
|
|
57
|
+
const { reason, path, at } = this.violation;
|
|
58
|
+
throw new CanonicalError('not_canonical', `input is valid but not canonical (${reason})`, { path, offset: this.byteOffset(at), reason });
|
|
59
|
+
}
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
whitespace(path) {
|
|
63
|
+
const start = this.at;
|
|
64
|
+
while (this.at < this.text.length && isWhitespace(this.text.charCodeAt(this.at)))
|
|
65
|
+
this.at++;
|
|
66
|
+
if (this.at !== start)
|
|
67
|
+
this.note('whitespace', path, start);
|
|
68
|
+
}
|
|
69
|
+
value(depth, path) {
|
|
70
|
+
this.whitespace(path);
|
|
71
|
+
if (depth > this.maxDepth)
|
|
72
|
+
this.fail('depth_exceeded', `nesting depth exceeds maxDepth ${this.maxDepth}`, path);
|
|
73
|
+
if (++this.nodes > this.maxNodes)
|
|
74
|
+
this.fail('nodes_exceeded', `node count exceeds maxNodes ${this.maxNodes}`, path);
|
|
75
|
+
if (this.at >= this.text.length)
|
|
76
|
+
this.fail('unexpected_end', 'expected a value', path);
|
|
77
|
+
const code = this.text.charCodeAt(this.at);
|
|
78
|
+
if (code === 0x22)
|
|
79
|
+
return this.string(path);
|
|
80
|
+
if (code === 0x7b)
|
|
81
|
+
return this.object(depth, path);
|
|
82
|
+
if (code === 0x5b)
|
|
83
|
+
return this.array(depth, path);
|
|
84
|
+
if (code === 0x74)
|
|
85
|
+
return this.literal('true', true, path);
|
|
86
|
+
if (code === 0x66)
|
|
87
|
+
return this.literal('false', false, path);
|
|
88
|
+
if (code === 0x6e)
|
|
89
|
+
return this.literal('null', null, path);
|
|
90
|
+
if (isDigit(code))
|
|
91
|
+
return this.number(path);
|
|
92
|
+
if (code === 0x2d || code === 0x2b || code === 0x2e)
|
|
93
|
+
this.fail('invalid_number', 'number token must be 0 or [1-9][0-9]* (no sign, fraction or exponent)', path);
|
|
94
|
+
this.fail('unexpected_token', 'unexpected character at value position', path);
|
|
95
|
+
}
|
|
96
|
+
literal(token, result, path) {
|
|
97
|
+
if (this.text.startsWith(token, this.at)) {
|
|
98
|
+
this.at += token.length;
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
if (token.startsWith(this.text.slice(this.at)))
|
|
102
|
+
this.fail('unexpected_end', `truncated literal ${token}`, path);
|
|
103
|
+
this.fail('unexpected_token', `invalid literal (expected ${token})`, path);
|
|
104
|
+
}
|
|
105
|
+
number(path) {
|
|
106
|
+
const start = this.at;
|
|
107
|
+
if (this.text.charCodeAt(this.at) === 0x30)
|
|
108
|
+
this.at++;
|
|
109
|
+
else
|
|
110
|
+
while (this.at < this.text.length && isDigit(this.text.charCodeAt(this.at)))
|
|
111
|
+
this.at++;
|
|
112
|
+
if (this.at < this.text.length) {
|
|
113
|
+
const next = this.text.charCodeAt(this.at);
|
|
114
|
+
if (!(next === 0x2c || next === 0x5d || next === 0x7d || isWhitespace(next))) {
|
|
115
|
+
this.fail('invalid_number', 'number token must be 0 or [1-9][0-9]* (no leading zero, fraction or exponent)', path, start);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const result = Number(this.text.slice(start, this.at));
|
|
119
|
+
if (!Number.isSafeInteger(result))
|
|
120
|
+
this.fail('unsafe_integer', 'integer exceeds 2^53-1', path, start);
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
/** `keyMode`:被解析的是对象键,canonical 违规按成员路径登记(键本身在 JSON.parse 后才可知)。 */
|
|
124
|
+
string(path, keyMode = false) {
|
|
125
|
+
const start = this.at;
|
|
126
|
+
STRING_TOKEN.lastIndex = start;
|
|
127
|
+
const match = STRING_TOKEN.exec(this.text);
|
|
128
|
+
if (match === null)
|
|
129
|
+
this.diagnoseString(path, start);
|
|
130
|
+
const token = match[0];
|
|
131
|
+
this.at = STRING_TOKEN.lastIndex;
|
|
132
|
+
const result = JSON.parse(token);
|
|
133
|
+
if (!wellFormed(result))
|
|
134
|
+
this.fail('lone_surrogate', 'string contains an unpaired surrogate', path, start);
|
|
135
|
+
if (this.strict && this.violation === null && token.includes('\\'))
|
|
136
|
+
this.escapes(token, start, keyMode ? childPointer(path, result) : path);
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
/** 正则整体不匹配时逐码元定位:未转义控制字符 / 非法转义 / 截断。 */
|
|
140
|
+
diagnoseString(path, start) {
|
|
141
|
+
const text = this.text;
|
|
142
|
+
let index = start + 1;
|
|
143
|
+
while (index < text.length) {
|
|
144
|
+
const code = text.charCodeAt(index);
|
|
145
|
+
if (code === 0x22)
|
|
146
|
+
break;
|
|
147
|
+
if (code < 0x20)
|
|
148
|
+
this.fail('invalid_string', 'unescaped control character in string', path, index);
|
|
149
|
+
if (code === 0x5c) {
|
|
150
|
+
if (index + 1 >= text.length)
|
|
151
|
+
this.fail('unexpected_end', 'input ends inside an escape sequence', path, index);
|
|
152
|
+
const escaped = text.charCodeAt(index + 1);
|
|
153
|
+
if (escaped === 0x75) {
|
|
154
|
+
const hex = text.slice(index + 2, index + 6);
|
|
155
|
+
if (!HEX4.test(hex)) {
|
|
156
|
+
if (index + 6 > text.length)
|
|
157
|
+
this.fail('unexpected_end', 'input ends inside a \\u escape', path, index);
|
|
158
|
+
this.fail('invalid_string', '\\u escape requires four hexadecimal digits', path, index);
|
|
159
|
+
}
|
|
160
|
+
index += 6;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (!'"\\/bfnrt'.includes(text[index + 1]))
|
|
164
|
+
this.fail('invalid_string', 'invalid escape sequence', path, index);
|
|
165
|
+
index += 2;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
index++;
|
|
169
|
+
}
|
|
170
|
+
this.fail('unexpected_end', 'unterminated string', path, start);
|
|
171
|
+
}
|
|
172
|
+
/** canonical 转义形:仅 `\"` `\\` `\b` `\t` `\n` `\f` `\r` 与小写 `\u00xx`(其它控制符);`\/`、`\uXXXX` ≥ 0x20、短转义写长、大写十六进制均非 canonical。 */
|
|
173
|
+
escapes(token, start, path) {
|
|
174
|
+
for (let index = 1; index < token.length - 1; index++) {
|
|
175
|
+
if (token.charCodeAt(index) !== 0x5c)
|
|
176
|
+
continue;
|
|
177
|
+
const escaped = token[index + 1];
|
|
178
|
+
if (escaped === 'u') {
|
|
179
|
+
const hex = token.slice(index + 2, index + 6);
|
|
180
|
+
const code = Number.parseInt(hex, 16);
|
|
181
|
+
if (hex !== hex.toLowerCase() || code >= 0x20 || SHORT_ESCAPED.has(code)) {
|
|
182
|
+
this.note('escape', path, start + index);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
index += 5;
|
|
186
|
+
}
|
|
187
|
+
else if (escaped === '/') {
|
|
188
|
+
this.note('escape', path, start + index);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
else
|
|
192
|
+
index += 1;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
expect(code, message, path) {
|
|
196
|
+
if (this.at >= this.text.length)
|
|
197
|
+
this.fail('unexpected_end', message, path);
|
|
198
|
+
if (this.text.charCodeAt(this.at) !== code)
|
|
199
|
+
this.fail('unexpected_token', message, path);
|
|
200
|
+
this.at++;
|
|
201
|
+
}
|
|
202
|
+
object(depth, path) {
|
|
203
|
+
this.at++;
|
|
204
|
+
const result = Object.create(null);
|
|
205
|
+
this.whitespace(path);
|
|
206
|
+
if (this.at >= this.text.length)
|
|
207
|
+
this.fail('unexpected_end', 'unterminated object', path);
|
|
208
|
+
if (this.text.charCodeAt(this.at) === 0x7d) {
|
|
209
|
+
this.at++;
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
let previous = null;
|
|
213
|
+
for (;;) {
|
|
214
|
+
this.whitespace(path);
|
|
215
|
+
if (this.at >= this.text.length)
|
|
216
|
+
this.fail('unexpected_end', 'unterminated object', path);
|
|
217
|
+
if (this.text.charCodeAt(this.at) !== 0x22)
|
|
218
|
+
this.fail('unexpected_token', 'expected a string key', path);
|
|
219
|
+
const keyStart = this.at;
|
|
220
|
+
const key = this.string(path, true);
|
|
221
|
+
const member = childPointer(path, key);
|
|
222
|
+
if (!CANONICAL_KEY_PATTERN.test(key))
|
|
223
|
+
this.fail('invalid_key', 'keys must be nonempty printable ASCII (0x21-0x7e)', member, keyStart);
|
|
224
|
+
if (Object.hasOwn(result, key))
|
|
225
|
+
this.fail('duplicate_key', 'duplicate key after escape decoding', member, keyStart);
|
|
226
|
+
if (previous !== null && key < previous)
|
|
227
|
+
this.note('key_order', member, keyStart);
|
|
228
|
+
previous = key;
|
|
229
|
+
this.whitespace(path);
|
|
230
|
+
this.expect(0x3a, 'expected ":" after key', member);
|
|
231
|
+
result[key] = this.value(depth + 1, member);
|
|
232
|
+
this.whitespace(path);
|
|
233
|
+
if (this.at >= this.text.length)
|
|
234
|
+
this.fail('unexpected_end', 'unterminated object', path);
|
|
235
|
+
const separator = this.text.charCodeAt(this.at++);
|
|
236
|
+
if (separator === 0x7d)
|
|
237
|
+
return result;
|
|
238
|
+
if (separator !== 0x2c)
|
|
239
|
+
this.fail('unexpected_token', 'expected "," or "}"', path, this.at - 1);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
array(depth, path) {
|
|
243
|
+
this.at++;
|
|
244
|
+
const result = [];
|
|
245
|
+
this.whitespace(path);
|
|
246
|
+
if (this.at >= this.text.length)
|
|
247
|
+
this.fail('unexpected_end', 'unterminated array', path);
|
|
248
|
+
if (this.text.charCodeAt(this.at) === 0x5d) {
|
|
249
|
+
this.at++;
|
|
250
|
+
return result;
|
|
251
|
+
}
|
|
252
|
+
for (let index = 0;; index++) {
|
|
253
|
+
result.push(this.value(depth + 1, childPointer(path, index)));
|
|
254
|
+
this.whitespace(path);
|
|
255
|
+
if (this.at >= this.text.length)
|
|
256
|
+
this.fail('unexpected_end', 'unterminated array', path);
|
|
257
|
+
const separator = this.text.charCodeAt(this.at++);
|
|
258
|
+
if (separator === 0x5d)
|
|
259
|
+
return result;
|
|
260
|
+
if (separator !== 0x2c)
|
|
261
|
+
this.fail('unexpected_token', 'expected "," or "]"', path, this.at - 1);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function parse(input, options, strict) {
|
|
266
|
+
const normalized = normalizeOptions(options, 'decode');
|
|
267
|
+
let text;
|
|
268
|
+
if (typeof input === 'string') {
|
|
269
|
+
if (utf8Length(input) > normalized.maxBytes)
|
|
270
|
+
throw new CanonicalError('bytes_exceeded', `input exceeds maxBytes ${normalized.maxBytes}`, { path: '', offset: 0 });
|
|
271
|
+
text = input;
|
|
272
|
+
}
|
|
273
|
+
else if (isUint8Array(input)) {
|
|
274
|
+
if (byteLengthOf(input) > normalized.maxBytes)
|
|
275
|
+
throw new CanonicalError('bytes_exceeded', `input exceeds maxBytes ${normalized.maxBytes}`, { path: '', offset: 0 });
|
|
276
|
+
try {
|
|
277
|
+
text = decoder.decode(input);
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
throw new CanonicalError('invalid_utf8', 'input is not valid UTF-8', { path: '', offset: Math.max(0, firstInvalidUtf8(input)) });
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
throw new CanonicalError('invalid_input', 'input must be a Uint8Array or a string');
|
|
285
|
+
}
|
|
286
|
+
return new Parser(text, strict, normalized).document();
|
|
287
|
+
}
|
|
288
|
+
/** 归一化解码(接受合法非 canonical 输入;kernel / providers 现语义)。 */
|
|
289
|
+
export function decodeCanonical(input, options) {
|
|
290
|
+
return parse(input, options, false);
|
|
291
|
+
}
|
|
292
|
+
/** 严格 canonical 解码(字节须已是 canonical;api-client 现语义 + `not_canonical` 定位)。 */
|
|
293
|
+
export function parseStrict(input, options) {
|
|
294
|
+
return parse(input, options, true);
|
|
295
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ModelClient — 模型客户端接口(依赖倒置的关键缝)。
|
|
3
|
+
* providers 包实现本接口;kernel 只依赖本接口,经构造注入,绝不 import 具体适配器。
|
|
4
|
+
*/
|
|
5
|
+
import type { IRRequest, IRStreamEvent } from './ir.js';
|
|
6
|
+
/** 单次模型调用的选项:取消信号与分层超时(总闸 / 建连闸 / 空闲闸)。 */
|
|
7
|
+
export interface ModelCallOptions {
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
/**
|
|
10
|
+
* 单次尝试总闸(毫秒)。分层超时落地后语义收敛为「总闸兼容位」:
|
|
11
|
+
* 消费分层超时的适配器(openai/openai-responses)把本值并入总闸
|
|
12
|
+
* (totalTimeoutMs 优先);未跟进的适配器沿用「单闸覆盖整次尝试」旧语义。
|
|
13
|
+
* 压缩路径恒传本位(90s 总闸,kernel context/compaction.ts)。
|
|
14
|
+
*/
|
|
15
|
+
timeoutMs?: number;
|
|
16
|
+
/**
|
|
17
|
+
* 建连/响应头到达闸(毫秒)。覆盖「发出请求 → 响应头到达」区间;
|
|
18
|
+
* 响应头到达后退场,交棒 idle 闸。缺席时消费方适配器用内建缺省
|
|
19
|
+
* (openai 系 15_000)。
|
|
20
|
+
*/
|
|
21
|
+
connectTimeoutMs?: number;
|
|
22
|
+
/**
|
|
23
|
+
* 流式分片间空闲闸(毫秒)。响应头到达后启用,每到一段字节重臂;
|
|
24
|
+
* 分片持续到达的健康慢长流恒不误杀。缺席时消费方适配器用内建缺省
|
|
25
|
+
* (openai 系 90_000)。
|
|
26
|
+
*/
|
|
27
|
+
idleTimeoutMs?: number;
|
|
28
|
+
/**
|
|
29
|
+
* 可选总闸(毫秒)。整次尝试(建连 + 流式读取)的绝对上限;
|
|
30
|
+
* 缺省关闭(与 timeoutMs 同给时本位优先)。
|
|
31
|
+
*/
|
|
32
|
+
totalTimeoutMs?: number;
|
|
33
|
+
}
|
|
34
|
+
/** 模型客户端:把一次 IRRequest 变成 IRStreamEvent 流;providers 实现,kernel 经注入消费。 */
|
|
35
|
+
export interface ModelClient {
|
|
36
|
+
/**
|
|
37
|
+
* 流式调用。实现必须把 wire 协议(SSE/delta)解析为 IRStreamEvent 序列;
|
|
38
|
+
* 可恢复错误以 `{t:'error'}` 事件产出而非 throw(throw 仅保留给编程性错误)。
|
|
39
|
+
* 生成器正常结束 = 流完整;消费方以 message_stop 或 error 事件为语义终点。
|
|
40
|
+
*/
|
|
41
|
+
stream(request: IRRequest, options?: ModelCallOptions): AsyncGenerator<IRStreamEvent, void, undefined>;
|
|
42
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|