@linnlabs/linnkit 0.10.0 → 0.21.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/CHANGELOG.md +156 -1
- package/dist/{agent-invocation-BHcNfrBV.d.cts → agent-invocation-CAlm9qOw.d.cts} +1 -1
- package/dist/{agent-invocation-BznDaXDs.d.ts → agent-invocation-DKoEDLkl.d.ts} +1 -1
- package/dist/{ai-engine.types-vZRnQcJa.d.ts → ai-engine.types-CeLoM624.d.cts} +9 -1
- package/dist/{ai-engine.types-BpeU_XQG.d.cts → ai-engine.types-LqmnfR2W.d.ts} +9 -1
- package/dist/{audit-CtcfART1.d.ts → audit-BuWHoMEr.d.ts} +4 -2
- package/dist/{audit-LeOrm2hX.d.cts → audit-DnzNQzwq.d.cts} +4 -2
- package/dist/cli.cjs +565 -194
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +565 -194
- package/dist/cli.js.map +1 -1
- package/dist/context-manager.cjs +590 -26
- package/dist/context-manager.cjs.map +1 -1
- package/dist/context-manager.d.cts +84 -7
- package/dist/context-manager.d.ts +84 -7
- package/dist/context-manager.js +590 -26
- package/dist/context-manager.js.map +1 -1
- package/dist/{context-trace-BHKDS-eq.d.cts → context-trace-DrtvKac4.d.cts} +36 -3
- package/dist/{context-trace-CHbqHmyE.d.ts → context-trace-WV1HAZeB.d.ts} +36 -3
- package/dist/contracts.cjs +222 -1
- package/dist/contracts.cjs.map +1 -1
- package/dist/contracts.d.cts +5 -3
- package/dist/contracts.d.ts +5 -3
- package/dist/contracts.js +202 -2
- package/dist/contracts.js.map +1 -1
- package/dist/{defaultGraphExecutor-C2E59v_R.d.cts → defaultGraphExecutor-L1ri3oij.d.cts} +35 -7
- package/dist/{defaultGraphExecutor-B29_qTHy.d.ts → defaultGraphExecutor-QzzdudLQ.d.ts} +35 -7
- package/dist/{index-BAaUP9yU.d.cts → index-BeQO0rx2.d.cts} +328 -263
- package/dist/{index-Cvr23YCl.d.cts → index-CDofGr_4.d.ts} +24 -6
- package/dist/{index-CKQzzZ5Y.d.cts → index-Crl5E4eF.d.cts} +26 -3
- package/dist/{index-C0DAjsdX.d.ts → index-DWGKvssS.d.ts} +2 -2
- package/dist/{index-D0mKxTR5.d.ts → index-X5lYU1Lj.d.ts} +26 -3
- package/dist/{index-DDzuSb0n.d.ts → index-uCNCpJBg.d.cts} +24 -6
- package/dist/{index-BaVpVNi2.d.ts → index-wOZD-p2-.d.ts} +328 -263
- package/dist/{index-BnYCS8Zg.d.cts → index-wXVAHZ0D.d.cts} +2 -2
- package/dist/index.cjs +893 -312
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -14
- package/dist/index.d.ts +17 -14
- package/dist/index.js +893 -312
- package/dist/index.js.map +1 -1
- package/dist/{messages-XthmnHZ3.d.cts → messages-Br5SiLD3.d.cts} +706 -1
- package/dist/{messages-XthmnHZ3.d.ts → messages-Br5SiLD3.d.ts} +706 -1
- package/dist/{ports-s-tSp3sB.d.cts → ports-Dugizzje.d.cts} +4 -0
- package/dist/{ports-DpPTFhSd.d.ts → ports-fhp84x_C.d.ts} +4 -0
- package/dist/ports.d.cts +8 -5
- package/dist/ports.d.ts +8 -5
- package/dist/quickstart.cjs +564 -193
- package/dist/quickstart.cjs.map +1 -1
- package/dist/quickstart.d.cts +9 -8
- package/dist/quickstart.d.ts +9 -8
- package/dist/quickstart.js +564 -193
- package/dist/quickstart.js.map +1 -1
- package/dist/{runAgent-C6F-399C.d.ts → runAgent-BmMSju8I.d.ts} +4 -4
- package/dist/{runAgent-ilEj66Ik.d.cts → runAgent-CKgcRSRH.d.cts} +4 -4
- package/dist/{runHandle-BNOqS-Bl.d.cts → runHandle-DH4VmQx6.d.cts} +4 -2
- package/dist/{runHandle-BdLXOFqF.d.ts → runHandle-o3p06Nva.d.ts} +4 -2
- package/dist/runtime-kernel/events.cjs +201 -1
- package/dist/runtime-kernel/events.cjs.map +1 -1
- package/dist/runtime-kernel/events.d.cts +3 -3
- package/dist/runtime-kernel/events.d.ts +3 -3
- package/dist/runtime-kernel/events.js +201 -1
- package/dist/runtime-kernel/events.js.map +1 -1
- package/dist/runtime-kernel.cjs +824 -264
- package/dist/runtime-kernel.cjs.map +1 -1
- package/dist/runtime-kernel.d.cts +14 -12
- package/dist/runtime-kernel.d.ts +14 -12
- package/dist/runtime-kernel.js +816 -265
- package/dist/runtime-kernel.js.map +1 -1
- package/dist/testkit.cjs +847 -264
- package/dist/testkit.cjs.map +1 -1
- package/dist/testkit.d.cts +14 -12
- package/dist/testkit.d.ts +14 -12
- package/dist/testkit.js +847 -264
- package/dist/testkit.js.map +1 -1
- package/dist/{agentSpec-Du4Iye0q.d.cts → token-accounting-CwpSPg4y.d.cts} +1265 -1
- package/dist/{agentSpec-Du4Iye0q.d.ts → token-accounting-CwpSPg4y.d.ts} +1265 -1
- package/dist/token-calibration-Cxv6dDYF.d.cts +327 -0
- package/dist/token-calibration-Cxv6dDYF.d.ts +327 -0
- package/dist/token-counter-CFfP8PQb.d.cts +26 -0
- package/dist/token-counter-SFyyTeaG.d.ts +26 -0
- package/dist/token-usage-CqwT7-5K.d.cts +133 -0
- package/dist/token-usage-CqwT7-5K.d.ts +133 -0
- package/dist/{tokenizer-DFL4I7-I.d.ts → tokenizer-BFJNCHFX.d.ts} +1 -1
- package/dist/{tokenizer-DH_JXv-H.d.cts → tokenizer-DoGsBE3A.d.cts} +1 -1
- package/docs/README.md +1 -1
- package/docs/integration/README.md +2 -2
- package/docs/integration/agent-registration-guide.md +2 -0
- package/docs/integration/context-engineering.md +53 -1
- package/docs/integration/context-fences.md +12 -0
- package/package.json +2 -2
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { T as TokenRoute, b as TokenCountSource, a as TokenCountConfidence } from './token-usage-CqwT7-5K.js';
|
|
2
|
+
import { a as LlmRequestMessage } from './ai-engine.types-LqmnfR2W.js';
|
|
3
|
+
|
|
4
|
+
interface TokenCountResult {
|
|
5
|
+
inputTokens: number;
|
|
6
|
+
imageInputTokens?: number;
|
|
7
|
+
toolSchemaTokens?: number;
|
|
8
|
+
source: Extract<TokenCountSource, 'provider-preflight-count' | 'host-supplied' | 'test-fixture'>;
|
|
9
|
+
confidence: Extract<TokenCountConfidence, 'provider-estimate' | 'actual'>;
|
|
10
|
+
raw?: unknown;
|
|
11
|
+
}
|
|
12
|
+
interface TokenCounterPort {
|
|
13
|
+
/**
|
|
14
|
+
* Route-aware 的发送前 token 计数。
|
|
15
|
+
*
|
|
16
|
+
* 中文备注:host 必须按 input.route 调用对应 endpoint;禁止只看 modelId 后绕去“模型官网”。
|
|
17
|
+
*/
|
|
18
|
+
countMessages(input: {
|
|
19
|
+
route: TokenRoute;
|
|
20
|
+
messages: LlmRequestMessage[];
|
|
21
|
+
tools?: unknown;
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
}): Promise<TokenCountResult>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type { TokenCounterPort as T, TokenCountResult as a };
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Token 计数来源:每个 token 数字都必须说明从哪里来,避免把估算当成账单事实。
|
|
5
|
+
*/
|
|
6
|
+
declare const TokenCountSource: z.ZodEnum<["local-estimate", "provider-preflight-count", "provider-response-usage", "host-supplied", "test-fixture"]>;
|
|
7
|
+
type TokenCountSource = z.infer<typeof TokenCountSource>;
|
|
8
|
+
/**
|
|
9
|
+
* Token 计数可信度:只有 actual 才能进入真实计费与后续校准。
|
|
10
|
+
*/
|
|
11
|
+
declare const TokenCountConfidence: z.ZodEnum<["estimate", "provider-estimate", "actual"]>;
|
|
12
|
+
type TokenCountConfidence = z.infer<typeof TokenCountConfidence>;
|
|
13
|
+
/**
|
|
14
|
+
* 当前调用 route 的 token 能力声明。
|
|
15
|
+
*
|
|
16
|
+
* 这些能力由 host 的 model catalog 声明;linnkit 不内置 provider 到能力的映射表。
|
|
17
|
+
*/
|
|
18
|
+
declare const TokenRouteCapabilities: z.ZodObject<{
|
|
19
|
+
supportsRemoteTokenCount: z.ZodOptional<z.ZodBoolean>;
|
|
20
|
+
supportsResponseUsage: z.ZodOptional<z.ZodBoolean>;
|
|
21
|
+
supportsImageInput: z.ZodOptional<z.ZodBoolean>;
|
|
22
|
+
supportsCachedInputBilling: z.ZodOptional<z.ZodBoolean>;
|
|
23
|
+
supportsReasoningTokens: z.ZodOptional<z.ZodBoolean>;
|
|
24
|
+
}, "strip", z.ZodTypeAny, {
|
|
25
|
+
supportsRemoteTokenCount?: boolean | undefined;
|
|
26
|
+
supportsResponseUsage?: boolean | undefined;
|
|
27
|
+
supportsImageInput?: boolean | undefined;
|
|
28
|
+
supportsCachedInputBilling?: boolean | undefined;
|
|
29
|
+
supportsReasoningTokens?: boolean | undefined;
|
|
30
|
+
}, {
|
|
31
|
+
supportsRemoteTokenCount?: boolean | undefined;
|
|
32
|
+
supportsResponseUsage?: boolean | undefined;
|
|
33
|
+
supportsImageInput?: boolean | undefined;
|
|
34
|
+
supportsCachedInputBilling?: boolean | undefined;
|
|
35
|
+
supportsReasoningTokens?: boolean | undefined;
|
|
36
|
+
}>;
|
|
37
|
+
type TokenRouteCapabilities = z.infer<typeof TokenRouteCapabilities>;
|
|
38
|
+
/**
|
|
39
|
+
* Token 计算路由:同一个模型经不同 gateway 时,计数能力可能完全不同。
|
|
40
|
+
*/
|
|
41
|
+
declare const TokenRoute: z.ZodObject<{
|
|
42
|
+
providerId: z.ZodString;
|
|
43
|
+
baseURL: z.ZodOptional<z.ZodString>;
|
|
44
|
+
modelId: z.ZodString;
|
|
45
|
+
providerModelId: z.ZodOptional<z.ZodString>;
|
|
46
|
+
capabilities: z.ZodOptional<z.ZodObject<{
|
|
47
|
+
supportsRemoteTokenCount: z.ZodOptional<z.ZodBoolean>;
|
|
48
|
+
supportsResponseUsage: z.ZodOptional<z.ZodBoolean>;
|
|
49
|
+
supportsImageInput: z.ZodOptional<z.ZodBoolean>;
|
|
50
|
+
supportsCachedInputBilling: z.ZodOptional<z.ZodBoolean>;
|
|
51
|
+
supportsReasoningTokens: z.ZodOptional<z.ZodBoolean>;
|
|
52
|
+
}, "strip", z.ZodTypeAny, {
|
|
53
|
+
supportsRemoteTokenCount?: boolean | undefined;
|
|
54
|
+
supportsResponseUsage?: boolean | undefined;
|
|
55
|
+
supportsImageInput?: boolean | undefined;
|
|
56
|
+
supportsCachedInputBilling?: boolean | undefined;
|
|
57
|
+
supportsReasoningTokens?: boolean | undefined;
|
|
58
|
+
}, {
|
|
59
|
+
supportsRemoteTokenCount?: boolean | undefined;
|
|
60
|
+
supportsResponseUsage?: boolean | undefined;
|
|
61
|
+
supportsImageInput?: boolean | undefined;
|
|
62
|
+
supportsCachedInputBilling?: boolean | undefined;
|
|
63
|
+
supportsReasoningTokens?: boolean | undefined;
|
|
64
|
+
}>>;
|
|
65
|
+
}, "strip", z.ZodTypeAny, {
|
|
66
|
+
modelId: string;
|
|
67
|
+
providerId: string;
|
|
68
|
+
capabilities?: {
|
|
69
|
+
supportsRemoteTokenCount?: boolean | undefined;
|
|
70
|
+
supportsResponseUsage?: boolean | undefined;
|
|
71
|
+
supportsImageInput?: boolean | undefined;
|
|
72
|
+
supportsCachedInputBilling?: boolean | undefined;
|
|
73
|
+
supportsReasoningTokens?: boolean | undefined;
|
|
74
|
+
} | undefined;
|
|
75
|
+
baseURL?: string | undefined;
|
|
76
|
+
providerModelId?: string | undefined;
|
|
77
|
+
}, {
|
|
78
|
+
modelId: string;
|
|
79
|
+
providerId: string;
|
|
80
|
+
capabilities?: {
|
|
81
|
+
supportsRemoteTokenCount?: boolean | undefined;
|
|
82
|
+
supportsResponseUsage?: boolean | undefined;
|
|
83
|
+
supportsImageInput?: boolean | undefined;
|
|
84
|
+
supportsCachedInputBilling?: boolean | undefined;
|
|
85
|
+
supportsReasoningTokens?: boolean | undefined;
|
|
86
|
+
} | undefined;
|
|
87
|
+
baseURL?: string | undefined;
|
|
88
|
+
providerModelId?: string | undefined;
|
|
89
|
+
}>;
|
|
90
|
+
type TokenRoute = z.infer<typeof TokenRoute>;
|
|
91
|
+
/**
|
|
92
|
+
* 归一化后的 LLM 用量。
|
|
93
|
+
*
|
|
94
|
+
* 关键约束:
|
|
95
|
+
* - inputTokens 是非缓存普通输入 token,不含 cacheRead/cacheWrite,避免重复计费。
|
|
96
|
+
* - reasoning/cache 字段用 optional 表达“未上报”和“明确为 0”的差异。
|
|
97
|
+
* - totalTokens 可缺失,也不保证等于各分项之和。
|
|
98
|
+
* - rawUsage 仅供审计;业务逻辑应依赖 canonical 字段。
|
|
99
|
+
*/
|
|
100
|
+
declare const CanonicalLlmUsage: z.ZodObject<{
|
|
101
|
+
inputTokens: z.ZodNumber;
|
|
102
|
+
outputTokens: z.ZodNumber;
|
|
103
|
+
reasoningTokens: z.ZodOptional<z.ZodNumber>;
|
|
104
|
+
cacheReadTokens: z.ZodOptional<z.ZodNumber>;
|
|
105
|
+
cacheWriteTokens: z.ZodOptional<z.ZodNumber>;
|
|
106
|
+
totalTokens: z.ZodOptional<z.ZodNumber>;
|
|
107
|
+
source: z.ZodEnum<["local-estimate", "provider-preflight-count", "provider-response-usage", "host-supplied", "test-fixture"]>;
|
|
108
|
+
confidence: z.ZodEnum<["estimate", "provider-estimate", "actual"]>;
|
|
109
|
+
rawUsage: z.ZodOptional<z.ZodUnknown>;
|
|
110
|
+
}, "strip", z.ZodTypeAny, {
|
|
111
|
+
source: "local-estimate" | "provider-preflight-count" | "provider-response-usage" | "host-supplied" | "test-fixture";
|
|
112
|
+
inputTokens: number;
|
|
113
|
+
outputTokens: number;
|
|
114
|
+
confidence: "estimate" | "provider-estimate" | "actual";
|
|
115
|
+
reasoningTokens?: number | undefined;
|
|
116
|
+
cacheReadTokens?: number | undefined;
|
|
117
|
+
cacheWriteTokens?: number | undefined;
|
|
118
|
+
totalTokens?: number | undefined;
|
|
119
|
+
rawUsage?: unknown;
|
|
120
|
+
}, {
|
|
121
|
+
source: "local-estimate" | "provider-preflight-count" | "provider-response-usage" | "host-supplied" | "test-fixture";
|
|
122
|
+
inputTokens: number;
|
|
123
|
+
outputTokens: number;
|
|
124
|
+
confidence: "estimate" | "provider-estimate" | "actual";
|
|
125
|
+
reasoningTokens?: number | undefined;
|
|
126
|
+
cacheReadTokens?: number | undefined;
|
|
127
|
+
cacheWriteTokens?: number | undefined;
|
|
128
|
+
totalTokens?: number | undefined;
|
|
129
|
+
rawUsage?: unknown;
|
|
130
|
+
}>;
|
|
131
|
+
type CanonicalLlmUsage = z.infer<typeof CanonicalLlmUsage>;
|
|
132
|
+
|
|
133
|
+
export { CanonicalLlmUsage as C, TokenRoute as T, TokenCountConfidence as a, TokenCountSource as b, TokenRouteCapabilities as c };
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Token 计数来源:每个 token 数字都必须说明从哪里来,避免把估算当成账单事实。
|
|
5
|
+
*/
|
|
6
|
+
declare const TokenCountSource: z.ZodEnum<["local-estimate", "provider-preflight-count", "provider-response-usage", "host-supplied", "test-fixture"]>;
|
|
7
|
+
type TokenCountSource = z.infer<typeof TokenCountSource>;
|
|
8
|
+
/**
|
|
9
|
+
* Token 计数可信度:只有 actual 才能进入真实计费与后续校准。
|
|
10
|
+
*/
|
|
11
|
+
declare const TokenCountConfidence: z.ZodEnum<["estimate", "provider-estimate", "actual"]>;
|
|
12
|
+
type TokenCountConfidence = z.infer<typeof TokenCountConfidence>;
|
|
13
|
+
/**
|
|
14
|
+
* 当前调用 route 的 token 能力声明。
|
|
15
|
+
*
|
|
16
|
+
* 这些能力由 host 的 model catalog 声明;linnkit 不内置 provider 到能力的映射表。
|
|
17
|
+
*/
|
|
18
|
+
declare const TokenRouteCapabilities: z.ZodObject<{
|
|
19
|
+
supportsRemoteTokenCount: z.ZodOptional<z.ZodBoolean>;
|
|
20
|
+
supportsResponseUsage: z.ZodOptional<z.ZodBoolean>;
|
|
21
|
+
supportsImageInput: z.ZodOptional<z.ZodBoolean>;
|
|
22
|
+
supportsCachedInputBilling: z.ZodOptional<z.ZodBoolean>;
|
|
23
|
+
supportsReasoningTokens: z.ZodOptional<z.ZodBoolean>;
|
|
24
|
+
}, "strip", z.ZodTypeAny, {
|
|
25
|
+
supportsRemoteTokenCount?: boolean | undefined;
|
|
26
|
+
supportsResponseUsage?: boolean | undefined;
|
|
27
|
+
supportsImageInput?: boolean | undefined;
|
|
28
|
+
supportsCachedInputBilling?: boolean | undefined;
|
|
29
|
+
supportsReasoningTokens?: boolean | undefined;
|
|
30
|
+
}, {
|
|
31
|
+
supportsRemoteTokenCount?: boolean | undefined;
|
|
32
|
+
supportsResponseUsage?: boolean | undefined;
|
|
33
|
+
supportsImageInput?: boolean | undefined;
|
|
34
|
+
supportsCachedInputBilling?: boolean | undefined;
|
|
35
|
+
supportsReasoningTokens?: boolean | undefined;
|
|
36
|
+
}>;
|
|
37
|
+
type TokenRouteCapabilities = z.infer<typeof TokenRouteCapabilities>;
|
|
38
|
+
/**
|
|
39
|
+
* Token 计算路由:同一个模型经不同 gateway 时,计数能力可能完全不同。
|
|
40
|
+
*/
|
|
41
|
+
declare const TokenRoute: z.ZodObject<{
|
|
42
|
+
providerId: z.ZodString;
|
|
43
|
+
baseURL: z.ZodOptional<z.ZodString>;
|
|
44
|
+
modelId: z.ZodString;
|
|
45
|
+
providerModelId: z.ZodOptional<z.ZodString>;
|
|
46
|
+
capabilities: z.ZodOptional<z.ZodObject<{
|
|
47
|
+
supportsRemoteTokenCount: z.ZodOptional<z.ZodBoolean>;
|
|
48
|
+
supportsResponseUsage: z.ZodOptional<z.ZodBoolean>;
|
|
49
|
+
supportsImageInput: z.ZodOptional<z.ZodBoolean>;
|
|
50
|
+
supportsCachedInputBilling: z.ZodOptional<z.ZodBoolean>;
|
|
51
|
+
supportsReasoningTokens: z.ZodOptional<z.ZodBoolean>;
|
|
52
|
+
}, "strip", z.ZodTypeAny, {
|
|
53
|
+
supportsRemoteTokenCount?: boolean | undefined;
|
|
54
|
+
supportsResponseUsage?: boolean | undefined;
|
|
55
|
+
supportsImageInput?: boolean | undefined;
|
|
56
|
+
supportsCachedInputBilling?: boolean | undefined;
|
|
57
|
+
supportsReasoningTokens?: boolean | undefined;
|
|
58
|
+
}, {
|
|
59
|
+
supportsRemoteTokenCount?: boolean | undefined;
|
|
60
|
+
supportsResponseUsage?: boolean | undefined;
|
|
61
|
+
supportsImageInput?: boolean | undefined;
|
|
62
|
+
supportsCachedInputBilling?: boolean | undefined;
|
|
63
|
+
supportsReasoningTokens?: boolean | undefined;
|
|
64
|
+
}>>;
|
|
65
|
+
}, "strip", z.ZodTypeAny, {
|
|
66
|
+
modelId: string;
|
|
67
|
+
providerId: string;
|
|
68
|
+
capabilities?: {
|
|
69
|
+
supportsRemoteTokenCount?: boolean | undefined;
|
|
70
|
+
supportsResponseUsage?: boolean | undefined;
|
|
71
|
+
supportsImageInput?: boolean | undefined;
|
|
72
|
+
supportsCachedInputBilling?: boolean | undefined;
|
|
73
|
+
supportsReasoningTokens?: boolean | undefined;
|
|
74
|
+
} | undefined;
|
|
75
|
+
baseURL?: string | undefined;
|
|
76
|
+
providerModelId?: string | undefined;
|
|
77
|
+
}, {
|
|
78
|
+
modelId: string;
|
|
79
|
+
providerId: string;
|
|
80
|
+
capabilities?: {
|
|
81
|
+
supportsRemoteTokenCount?: boolean | undefined;
|
|
82
|
+
supportsResponseUsage?: boolean | undefined;
|
|
83
|
+
supportsImageInput?: boolean | undefined;
|
|
84
|
+
supportsCachedInputBilling?: boolean | undefined;
|
|
85
|
+
supportsReasoningTokens?: boolean | undefined;
|
|
86
|
+
} | undefined;
|
|
87
|
+
baseURL?: string | undefined;
|
|
88
|
+
providerModelId?: string | undefined;
|
|
89
|
+
}>;
|
|
90
|
+
type TokenRoute = z.infer<typeof TokenRoute>;
|
|
91
|
+
/**
|
|
92
|
+
* 归一化后的 LLM 用量。
|
|
93
|
+
*
|
|
94
|
+
* 关键约束:
|
|
95
|
+
* - inputTokens 是非缓存普通输入 token,不含 cacheRead/cacheWrite,避免重复计费。
|
|
96
|
+
* - reasoning/cache 字段用 optional 表达“未上报”和“明确为 0”的差异。
|
|
97
|
+
* - totalTokens 可缺失,也不保证等于各分项之和。
|
|
98
|
+
* - rawUsage 仅供审计;业务逻辑应依赖 canonical 字段。
|
|
99
|
+
*/
|
|
100
|
+
declare const CanonicalLlmUsage: z.ZodObject<{
|
|
101
|
+
inputTokens: z.ZodNumber;
|
|
102
|
+
outputTokens: z.ZodNumber;
|
|
103
|
+
reasoningTokens: z.ZodOptional<z.ZodNumber>;
|
|
104
|
+
cacheReadTokens: z.ZodOptional<z.ZodNumber>;
|
|
105
|
+
cacheWriteTokens: z.ZodOptional<z.ZodNumber>;
|
|
106
|
+
totalTokens: z.ZodOptional<z.ZodNumber>;
|
|
107
|
+
source: z.ZodEnum<["local-estimate", "provider-preflight-count", "provider-response-usage", "host-supplied", "test-fixture"]>;
|
|
108
|
+
confidence: z.ZodEnum<["estimate", "provider-estimate", "actual"]>;
|
|
109
|
+
rawUsage: z.ZodOptional<z.ZodUnknown>;
|
|
110
|
+
}, "strip", z.ZodTypeAny, {
|
|
111
|
+
source: "local-estimate" | "provider-preflight-count" | "provider-response-usage" | "host-supplied" | "test-fixture";
|
|
112
|
+
inputTokens: number;
|
|
113
|
+
outputTokens: number;
|
|
114
|
+
confidence: "estimate" | "provider-estimate" | "actual";
|
|
115
|
+
reasoningTokens?: number | undefined;
|
|
116
|
+
cacheReadTokens?: number | undefined;
|
|
117
|
+
cacheWriteTokens?: number | undefined;
|
|
118
|
+
totalTokens?: number | undefined;
|
|
119
|
+
rawUsage?: unknown;
|
|
120
|
+
}, {
|
|
121
|
+
source: "local-estimate" | "provider-preflight-count" | "provider-response-usage" | "host-supplied" | "test-fixture";
|
|
122
|
+
inputTokens: number;
|
|
123
|
+
outputTokens: number;
|
|
124
|
+
confidence: "estimate" | "provider-estimate" | "actual";
|
|
125
|
+
reasoningTokens?: number | undefined;
|
|
126
|
+
cacheReadTokens?: number | undefined;
|
|
127
|
+
cacheWriteTokens?: number | undefined;
|
|
128
|
+
totalTokens?: number | undefined;
|
|
129
|
+
rawUsage?: unknown;
|
|
130
|
+
}>;
|
|
131
|
+
type CanonicalLlmUsage = z.infer<typeof CanonicalLlmUsage>;
|
|
132
|
+
|
|
133
|
+
export { CanonicalLlmUsage as C, TokenRoute as T, TokenCountConfidence as a, TokenCountSource as b, TokenRouteCapabilities as c };
|
package/docs/README.md
CHANGED
|
@@ -128,7 +128,7 @@ import type { RuntimeEvent } from '@linnlabs/linnkit/contracts';
|
|
|
128
128
|
| 工具集接入面(`ToolRuntimePort` / `ObservationPreviewPort`) | [tools.md](./tools.md) |
|
|
129
129
|
| **工具开发规范**(`BaseTool` 设计哲学 / `data`-`observation` 分层 / 错误处理 / `getExecutionSummary`)⭐ | [tool-development-guide.md](./tool-development-guide.md) |
|
|
130
130
|
| **Agent 注册与装配**(`AgentSpec` 静态蓝图 / `defineAgent` quickstart helper / 多 agent 协作)⭐ | [agent-registration-guide.md](./agent-registration-guide.md) |
|
|
131
|
-
| **上下文工程总览**(所有作用在 messages 上的机制 + `contextPolicy` / `ContextTrace` 可观测闭环)⭐
|
|
131
|
+
| **上下文工程总览**(所有作用在 messages 上的机制 + `contextPolicy` 配置真相源 / `ContextTrace` 可观测闭环)⭐ | [context-engineering.md](./context-engineering.md) |
|
|
132
132
|
| **接 context engineering(fence 注册 + 注入)⭐ 一等接入面** | [context-fences.md](./context-fences.md) |
|
|
133
133
|
| 自定义 token 估算(`TokenizerPort` 替换默认 tokenizer · 0.8.0+) | [context-engineering.md §9.4](./context-engineering.md) / [agent-registration-guide.md](./agent-registration-guide.md) §4.1 |
|
|
134
134
|
| 配置工具历史保留策略(per-pair / per-run / none;drop / compress) | [tool-history.md](./tool-history.md) |
|
|
@@ -181,7 +181,7 @@ import type { RuntimeEvent } from '@linnlabs/linnkit/contracts';
|
|
|
181
181
|
| ……把工具注册到 agent 上 | [tools.md](./tools.md) + [agent-registration-guide.md §2](./agent-registration-guide.md) ⭐ |
|
|
182
182
|
| ……让工具产生的超长 observation 不占满上下文 | [tools.md §5](./tools.md)(`ObservationPreviewPort`)+ [context-engineering.md §6](./context-engineering.md) |
|
|
183
183
|
| ……定义一个新 agent / 写 `AgentSpec` | [agent-registration-guide.md](./agent-registration-guide.md) ⭐ |
|
|
184
|
-
| ……配 token 预算 / 控制每个 token
|
|
184
|
+
| ……配 token 预算 / 控制每个 token / 确认改预算要不要到处改 | [context-engineering.md §0.1](./context-engineering.md#context-policy-source-of-truth)(配置真相源与三层合并)+ [agent-registration-guide.md](./agent-registration-guide.md) §4.1 |
|
|
185
185
|
| ……用真实的 Claude / Gemini tokenizer 替代默认 | [context-engineering.md §9.4](./context-engineering.md)(`TokenizerPort`)+ [agent-registration-guide.md](./agent-registration-guide.md) §4.1 |
|
|
186
186
|
| ……被动摘要:先注册摘要 agent、再填 `summarization.agentId` | [agent-registration-guide.md](./agent-registration-guide.md) §4.2 + [context-engineering.md](./context-engineering.md) §5.4 |
|
|
187
187
|
| ……把 host 的"当前文件 / 项目状态 / 引用段落"喂给 agent | [context-fences.md](./context-fences.md) ⭐ |
|
|
@@ -128,6 +128,8 @@ const contextPolicy = defineContextPolicy({
|
|
|
128
128
|
|
|
129
129
|
12 大分组的详细说明见 [`context-engineering.md`](./context-engineering.md) ⭐。
|
|
130
130
|
|
|
131
|
+
如果你只是想确认“改预算要不要改很多地方”,先看 [`context-engineering.md §0.1`](./context-engineering.md#context-policy-source-of-truth):`contextPolicy` 是配置真相源,运行时会先做三层合并,再由 adapter 拆给各消费点。
|
|
132
|
+
|
|
131
133
|
> ⚠️ **不要手写 `AgentSpecContextPolicy` 对象**——`defineContextPolicy()` 不只补默认值,还**校验组合约束**(如 `summarization.enabled = true` 必须有 `agentId`)。手写绕过 helper 会在运行时崩溃。
|
|
132
134
|
|
|
133
135
|
### 4.1 `maxTokens` 谁来算?
|
|
@@ -11,6 +11,46 @@
|
|
|
11
11
|
> 本文是一份**全机制速查表**:列出 linnkit 当前所有作用在"发给 LLM 的 messages"上的机制,**说人话**讲清楚它在做什么、什么时候触发、在哪里改它、当前可以配到什么粒度。
|
|
12
12
|
>
|
|
13
13
|
> 想看 fence 一等接入面的具体落地骨架,跳到 [`context-fences.md`](./context-fences.md)。
|
|
14
|
+
> 想看 context-manager 的代码层架构、三阶段 provider 与预处理器实现细节,读 [`src/context-manager/README.md`](../../src/context-manager/README.md)。
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
<a id="context-policy-source-of-truth"></a>
|
|
19
|
+
|
|
20
|
+
## 0.1 配置真相源:改预算改哪里
|
|
21
|
+
|
|
22
|
+
先回答最常见的问题:**改预算不用到处跑**。预算、摘要、工具历史、工具输出截断、must-keep、checkpoint、reasoning 保留、token 估算、system reminder 与 trace 的声明入口,统一都是 `AgentSpec.contextPolicy`(host 侧通常落在 `AgentDefinition.config.contextPolicy`),类型真相源是 `AgentSpecContextPolicy`。
|
|
23
|
+
|
|
24
|
+
运行时只消费合并后的 effective policy:
|
|
25
|
+
|
|
26
|
+
```text
|
|
27
|
+
framework 默认
|
|
28
|
+
< host fallback
|
|
29
|
+
< 单个 agent 的 contextPolicy
|
|
30
|
+
└─ mergeContextPolicy(...) 字段级合并
|
|
31
|
+
└─ agentSpecAdapter 拆成运行时选项
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
这条链路的含义很直接:
|
|
35
|
+
|
|
36
|
+
| 你想改什么 | 改哪里 |
|
|
37
|
+
|------------|--------|
|
|
38
|
+
| 只改某个 agent 的总预算 | 该 agent 的 `config.contextPolicy.budget.maxTokens` |
|
|
39
|
+
| 改某个 agent 的响应预留 | 该 agent 的 `config.contextPolicy.budget.reservedForResponse` |
|
|
40
|
+
| 改所有 agent 的 host 默认策略 | host 的 `defaultContextPolicy` / context policy fallback |
|
|
41
|
+
| 改 framework 基线默认值 | linnkit 的 context policy 默认值 / agent context builder 默认配置 |
|
|
42
|
+
| 验证最终到底生效了什么 | 开 `contextPolicy.contextTrace.enabled=true`,看 `ContextTrace.effectivePolicy` |
|
|
43
|
+
|
|
44
|
+
`agentSpecAdapter` 会把同一个 policy 拆给几类消费点:context builder 配置、preprocessor 选项、provider registry 选项、execution 选项,以及 system reminder。**这些是运行时分发点,不是新的配置真相源**。如果发现某个字段改了但某个消费点没响应,应该补 adapter 或装配链路;不要在消费点旁边再加一份局部预算常量。
|
|
45
|
+
|
|
46
|
+
边界也要说清楚:`contextPolicy.budget.maxTokens` 是 linnkit 做上下文裁剪的预算,不是 provider 返回的真实计费 token,也不会自动反查当前模型的最大上下文窗口。换模型时,host 仍要确认模型能力、provider options 与 `contextPolicy.budget` 匹配。
|
|
47
|
+
|
|
48
|
+
相关入口:
|
|
49
|
+
|
|
50
|
+
- 定义 agent 与 `defineContextPolicy()`:[`agent-registration-guide.md §4`](./agent-registration-guide.md)
|
|
51
|
+
- 工具历史窗口:[`tool-history.md`](./tool-history.md)
|
|
52
|
+
- fence / must-keep 注入:[`context-fences.md`](./context-fences.md)
|
|
53
|
+
- 代码层实现说明:[`src/context-manager/README.md`](../../src/context-manager/README.md)
|
|
14
54
|
|
|
15
55
|
---
|
|
16
56
|
|
|
@@ -62,7 +102,7 @@ linnkit 的内部消息(`AiMessage` union)最终都会按 LLM 协议的三
|
|
|
62
102
|
|
|
63
103
|
| Role | 这里有什么 |
|
|
64
104
|
|------|-----------|
|
|
65
|
-
| `system` | `system_prompt`、`placement: 'after-system'` 的 fence
|
|
105
|
+
| `system` | `system_prompt`、`placement: 'after-system'` 的 fence(不常变化的固定上下文),例如长期规则、当前能力目录、用户偏好等 |
|
|
66
106
|
| `assistant` | LLM 自己产的 `final_answer` / `thought`(reasoning_content)/ `tool_calls`;以及配对的 `tool_output`(在物理 wire 上挂 `tool` role) |
|
|
67
107
|
| `user` | 用户的 `user_input`、`placement: 'before-current-user'` / `'after-current-user'` 的 fence(经常变化的高频上下文)、例如用户上传的文件、当前时间等以及触发后的 `<system-reminder>` 注入 |
|
|
68
108
|
|
|
@@ -78,6 +118,18 @@ linnkit 的内部消息(`AiMessage` union)最终都会按 LLM 协议的三
|
|
|
78
118
|
|
|
79
119
|
这里的 `<formatter(...)>` 不是字面输出;真正发给 LLM 的是 host 自己声明的 XML/tag,例如 `<document_context>`。未注入某类 fence 时,不会生成空标签或占位符。
|
|
80
120
|
|
|
121
|
+
host 也可以在持久化的 `user_input.content` 中预先写入结构化块,例如:
|
|
122
|
+
|
|
123
|
+
```xml
|
|
124
|
+
<local_time>2026-06-18 14:35:27</local_time>
|
|
125
|
+
|
|
126
|
+
<user_request>
|
|
127
|
+
用户原始请求
|
|
128
|
+
</user_request>
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
这种做法适合“每一轮发生时的历史事实”(例如本地时间)。UI 如需展示原始用户文本,应由 host 在自己的事件契约中保留原文字段;linnkit 只负责回放 `user_input.content` 给模型,不理解具体标签语义。
|
|
132
|
+
|
|
81
133
|
**重要不变量**:`tool_calls` 和 `tool_output` **必须成对出现**——任何一边丢了另一边就废了。这条不变量贯穿所有压缩 / 裁剪机制。
|
|
82
134
|
|
|
83
135
|
---
|
|
@@ -101,6 +101,18 @@ export const myFenceRegistry = createMyFenceRegistry();
|
|
|
101
101
|
|
|
102
102
|
没有注入的 fence 不会产生空 XML,也不会出现 `<formatter(before-current-user fence)>` 这类概念占位符。
|
|
103
103
|
|
|
104
|
+
如果 host 已经把当前用户请求预包装成:
|
|
105
|
+
|
|
106
|
+
```xml
|
|
107
|
+
<local_time>2026-06-18 14:35:27</local_time>
|
|
108
|
+
|
|
109
|
+
<user_request>
|
|
110
|
+
用户原始请求
|
|
111
|
+
</user_request>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`CurrentTurnMessageAssembler` 在合并当前轮 user-side fence 时会复用已有 `<user_request>`,不会再嵌套一层。这样 host 可以把某些历史事实持久化到 `user_input.content`,同时继续使用 fence 注入当前视图或选区。
|
|
115
|
+
|
|
104
116
|
## 4. 写一个 host 适配器:把请求字段转成 `FenceInjection[]`
|
|
105
117
|
|
|
106
118
|
这是把 host 自己的产品语义("项目名"、"被选中的段落"、"用户引用的句子"等)翻成通用 fence 注入的关键一层。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@linnlabs/linnkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A fine-grained context engineering framework for Agent applications — control every token sent to the model, with clear run lifecycle, audit records, and testable protocol boundaries.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -105,7 +105,7 @@
|
|
|
105
105
|
"vitest": "^3.2.4"
|
|
106
106
|
},
|
|
107
107
|
"linnkit": {
|
|
108
|
-
"phase": "released (0.
|
|
108
|
+
"phase": "released (0.21.0 — context component ledger telemetry)",
|
|
109
109
|
"sourceOfTruth": "CHANGELOG.md",
|
|
110
110
|
"notes": [
|
|
111
111
|
"子入口 ./runtime-kernel/events 是 browser-safe slim seam:仅暴露 events governance 纯函数,禁止扩展任何 Node-only 依赖(如 node:async_hooks / crypto / fs)",
|