@ginkgolab/harness-core 0.1.0 → 0.1.2
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/README.md +11 -0
- package/dist/built-JAK7YFRG.js +10825 -0
- package/dist/chunk-2N25X2GE.js +66 -0
- package/dist/chunk-6OYMWYZS.js +19 -0
- package/dist/chunk-A7RQFPRO.js +42 -0
- package/dist/chunk-DIWHNBPO.js +139 -0
- package/dist/chunk-HYD33JX5.js +264 -0
- package/dist/chunk-JLMNLT6T.js +345 -0
- package/dist/chunk-JYNDOV22.js +790 -0
- package/dist/chunk-K2XRWRKF.js +274 -0
- package/dist/chunk-LASGGBG6.js +371 -0
- package/dist/chunk-MWOHX4WC.js +254 -0
- package/dist/chunk-OEJYZ6IB.js +357 -0
- package/dist/chunk-PPR3G4HX.js +127 -0
- package/dist/chunk-RYIWJPIR.js +196 -0
- package/dist/chunk-SCHUPQP4.js +36 -0
- package/dist/chunk-VIREJDUO.js +306 -0
- package/dist/chunk-VNSZH3H7.js +17 -0
- package/dist/chunk-ZO2UWHNE.js +22 -0
- package/dist/config.js +15 -356
- package/dist/context.js +21 -252
- package/dist/finops.js +10 -11057
- package/dist/graph.js +8 -298
- package/dist/index.js +131 -14348
- package/dist/lifecycle.js +6 -165
- package/dist/mcp.js +7 -338
- package/dist/pipeline.js +12 -348
- package/dist/registry.js +5 -5
- package/dist/stream.js +16 -11908
- package/dist/telemetry.js +11 -305
- package/dist/tokenizer.js +10 -73
- package/dist/tools.js +11 -158
- package/package.json +2 -2
package/dist/config.js
CHANGED
|
@@ -1,359 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
var PipelineConfigSchema = z.object({
|
|
17
|
-
concurrency: ConcurrencyConfigSchema.default({
|
|
18
|
-
maxConcurrent: 10,
|
|
19
|
-
queueTimeoutMs: 3e4
|
|
20
|
-
}),
|
|
21
|
-
credentialGuard: CredentialGuardConfigSchema.default({
|
|
22
|
-
allowedHosts: [],
|
|
23
|
-
requireApiKey: true
|
|
24
|
-
}),
|
|
25
|
-
tenantQuota: TenantQuotaConfigSchema.default({
|
|
26
|
-
defaultDailyBudgetUsd: 50,
|
|
27
|
-
defaultTokenLimit: 1e7,
|
|
28
|
-
windowMs: 864e5
|
|
29
|
-
})
|
|
30
|
-
});
|
|
31
|
-
var GraphConfigSchema = z.object({
|
|
32
|
-
maxSteps: z.number().int().positive().default(25),
|
|
33
|
-
recursionLimit: z.number().int().positive().default(50),
|
|
34
|
-
checkpointerType: z.enum(["memory", "redis", "file"]).default("memory")
|
|
35
|
-
});
|
|
36
|
-
var ResilienceConfigSchema = z.object({
|
|
37
|
-
timeoutMs: z.number().int().positive().default(3e4),
|
|
38
|
-
maxRetries: z.number().int().nonnegative().default(3),
|
|
39
|
-
initialDelayMs: z.number().int().nonnegative().default(1e3),
|
|
40
|
-
maxDelayMs: z.number().int().positive().default(1e4),
|
|
41
|
-
backoffFactor: z.number().positive().default(2),
|
|
42
|
-
fallbackModels: z.array(z.string()).default([])
|
|
43
|
-
});
|
|
44
|
-
var AutoHealingConfigSchema = z.object({
|
|
45
|
-
maxAttempts: z.number().int().nonnegative().default(2),
|
|
46
|
-
temperature: z.number().min(0).max(2).default(0.1)
|
|
47
|
-
});
|
|
48
|
-
var GuardrailsConfigSchema = z.object({
|
|
49
|
-
autoHealing: AutoHealingConfigSchema.default({
|
|
50
|
-
maxAttempts: 2,
|
|
51
|
-
temperature: 0.1
|
|
52
|
-
})
|
|
53
|
-
});
|
|
54
|
-
var FinOpsConfigSchema = z.object({
|
|
55
|
-
storeType: z.enum(["memory", "ndjson", "redis"]).default("memory"),
|
|
56
|
-
storagePath: z.string().default("./data/cost-ledger.ndjson"),
|
|
57
|
-
redisUrl: z.string().optional(),
|
|
58
|
-
pricingOverrides: z.record(
|
|
59
|
-
z.string(),
|
|
60
|
-
z.object({
|
|
61
|
-
promptPer1M: z.number().nonnegative(),
|
|
62
|
-
completionPer1M: z.number().nonnegative()
|
|
63
|
-
})
|
|
64
|
-
).default({})
|
|
65
|
-
});
|
|
66
|
-
var StreamConfigSchema = z.object({
|
|
67
|
-
storeType: z.enum(["memory", "file", "redis"]).default("memory"),
|
|
68
|
-
taskTtlMs: z.number().int().positive().default(36e5),
|
|
69
|
-
fileStoreDir: z.string().optional(),
|
|
70
|
-
redisUrl: z.string().optional(),
|
|
71
|
-
heartbeatIntervalMs: z.number().int().positive().default(15e3)
|
|
72
|
-
});
|
|
73
|
-
var McpReconnectConfigSchema = z.object({
|
|
74
|
-
maxRetries: z.number().int().nonnegative().default(3),
|
|
75
|
-
delayMs: z.number().int().nonnegative().default(1e3)
|
|
76
|
-
});
|
|
77
|
-
var McpConfigSchema = z.object({
|
|
78
|
-
defaultTimeoutMs: z.number().int().positive().default(3e4),
|
|
79
|
-
maxPoolSize: z.number().int().positive().default(5),
|
|
80
|
-
reconnect: McpReconnectConfigSchema.default({
|
|
81
|
-
maxRetries: 3,
|
|
82
|
-
delayMs: 1e3
|
|
83
|
-
})
|
|
84
|
-
});
|
|
85
|
-
var TelemetryConfigSchema = z.object({
|
|
86
|
-
enabled: z.boolean().default(true),
|
|
87
|
-
sampleRate: z.number().min(0).max(1).default(1),
|
|
88
|
-
exporter: z.enum(["console", "otel", "none"]).default("console"),
|
|
89
|
-
otelEndpoint: z.string().optional()
|
|
90
|
-
});
|
|
91
|
-
var HarnessConfigSchema = z.object({
|
|
92
|
-
pipeline: PipelineConfigSchema.default({
|
|
93
|
-
concurrency: { maxConcurrent: 10, queueTimeoutMs: 3e4 },
|
|
94
|
-
credentialGuard: { allowedHosts: [], requireApiKey: true },
|
|
95
|
-
tenantQuota: {
|
|
96
|
-
defaultDailyBudgetUsd: 50,
|
|
97
|
-
defaultTokenLimit: 1e7,
|
|
98
|
-
windowMs: 864e5
|
|
99
|
-
}
|
|
100
|
-
}),
|
|
101
|
-
graph: GraphConfigSchema.default({
|
|
102
|
-
maxSteps: 25,
|
|
103
|
-
recursionLimit: 50,
|
|
104
|
-
checkpointerType: "memory"
|
|
105
|
-
}),
|
|
106
|
-
resilience: ResilienceConfigSchema.default({
|
|
107
|
-
timeoutMs: 3e4,
|
|
108
|
-
maxRetries: 3,
|
|
109
|
-
initialDelayMs: 1e3,
|
|
110
|
-
maxDelayMs: 1e4,
|
|
111
|
-
backoffFactor: 2,
|
|
112
|
-
fallbackModels: []
|
|
113
|
-
}),
|
|
114
|
-
guardrails: GuardrailsConfigSchema.default({
|
|
115
|
-
autoHealing: { maxAttempts: 2, temperature: 0.1 }
|
|
116
|
-
}),
|
|
117
|
-
finops: FinOpsConfigSchema.default({
|
|
118
|
-
storeType: "memory",
|
|
119
|
-
storagePath: "./data/cost-ledger.ndjson",
|
|
120
|
-
pricingOverrides: {}
|
|
121
|
-
}),
|
|
122
|
-
stream: StreamConfigSchema.default({
|
|
123
|
-
storeType: "memory",
|
|
124
|
-
taskTtlMs: 36e5,
|
|
125
|
-
heartbeatIntervalMs: 15e3
|
|
126
|
-
}),
|
|
127
|
-
mcp: McpConfigSchema.default({
|
|
128
|
-
defaultTimeoutMs: 3e4,
|
|
129
|
-
maxPoolSize: 5,
|
|
130
|
-
reconnect: { maxRetries: 3, delayMs: 1e3 }
|
|
131
|
-
}),
|
|
132
|
-
telemetry: TelemetryConfigSchema.default({
|
|
133
|
-
enabled: true,
|
|
134
|
-
sampleRate: 1,
|
|
135
|
-
exporter: "console"
|
|
136
|
-
})
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
// src/config/defaults.ts
|
|
140
|
-
var DEFAULT_HARNESS_CONFIG = HarnessConfigSchema.parse({});
|
|
141
|
-
var DEFAULT_PIPELINE_CONFIG = DEFAULT_HARNESS_CONFIG.pipeline;
|
|
142
|
-
var DEFAULT_GRAPH_CONFIG = DEFAULT_HARNESS_CONFIG.graph;
|
|
143
|
-
var DEFAULT_RESILIENCE_CONFIG = DEFAULT_HARNESS_CONFIG.resilience;
|
|
144
|
-
var DEFAULT_GUARDRAILS_CONFIG = DEFAULT_HARNESS_CONFIG.guardrails;
|
|
145
|
-
var DEFAULT_FINOPS_CONFIG = DEFAULT_HARNESS_CONFIG.finops;
|
|
146
|
-
var DEFAULT_STREAM_CONFIG = DEFAULT_HARNESS_CONFIG.stream;
|
|
147
|
-
var DEFAULT_MCP_CONFIG = DEFAULT_HARNESS_CONFIG.mcp;
|
|
148
|
-
var DEFAULT_TELEMETRY_CONFIG = DEFAULT_HARNESS_CONFIG.telemetry;
|
|
149
|
-
|
|
150
|
-
// src/config/envLoader.ts
|
|
151
|
-
function parsePositiveInt(val) {
|
|
152
|
-
if (!val) return void 0;
|
|
153
|
-
const num = parseInt(val, 10);
|
|
154
|
-
return Number.isFinite(num) && num > 0 ? num : void 0;
|
|
155
|
-
}
|
|
156
|
-
function parseNonNegativeInt(val) {
|
|
157
|
-
if (!val) return void 0;
|
|
158
|
-
const num = parseInt(val, 10);
|
|
159
|
-
return Number.isFinite(num) && num >= 0 ? num : void 0;
|
|
160
|
-
}
|
|
161
|
-
function parseNumber(val) {
|
|
162
|
-
if (!val) return void 0;
|
|
163
|
-
const num = parseFloat(val);
|
|
164
|
-
return Number.isFinite(num) ? num : void 0;
|
|
165
|
-
}
|
|
166
|
-
function parseBoolean(val) {
|
|
167
|
-
if (!val) return void 0;
|
|
168
|
-
const lower = val.trim().toLowerCase();
|
|
169
|
-
if (lower === "true" || lower === "1") return true;
|
|
170
|
-
if (lower === "false" || lower === "0") return false;
|
|
171
|
-
return void 0;
|
|
172
|
-
}
|
|
173
|
-
function parseCsv(val) {
|
|
174
|
-
if (!val) return void 0;
|
|
175
|
-
const items = val.split(",").map((s) => s.trim()).filter(Boolean);
|
|
176
|
-
return items.length > 0 ? items : void 0;
|
|
177
|
-
}
|
|
178
|
-
function parseEnum(val, allowed) {
|
|
179
|
-
if (val && allowed.includes(val)) {
|
|
180
|
-
return val;
|
|
181
|
-
}
|
|
182
|
-
return void 0;
|
|
183
|
-
}
|
|
184
|
-
var CHECKPOINTER_TYPES = ["memory", "redis", "file"];
|
|
185
|
-
var FINOPS_STORE_TYPES = ["memory", "ndjson", "redis"];
|
|
186
|
-
var STREAM_STORE_TYPES = ["memory", "file", "redis"];
|
|
187
|
-
var TELEMETRY_EXPORTERS = ["console", "otel", "none"];
|
|
188
|
-
function loadHarnessConfigFromEnv(env = process.env) {
|
|
189
|
-
const redisUrl = env.HARNESS_REDIS_URL || env.REDIS_URL;
|
|
190
|
-
const config = {
|
|
191
|
-
pipeline: {
|
|
192
|
-
concurrency: {
|
|
193
|
-
maxConcurrent: parsePositiveInt(
|
|
194
|
-
env.HARNESS_CONCURRENCY_MAX || env.CONCURRENCY_MAX
|
|
195
|
-
),
|
|
196
|
-
queueTimeoutMs: parseNonNegativeInt(
|
|
197
|
-
env.HARNESS_CONCURRENCY_TIMEOUT_MS
|
|
198
|
-
)
|
|
199
|
-
},
|
|
200
|
-
credentialGuard: {
|
|
201
|
-
allowedHosts: parseCsv(env.HARNESS_CREDENTIAL_ALLOWED_HOSTS),
|
|
202
|
-
requireApiKey: parseBoolean(env.HARNESS_REQUIRE_API_KEY)
|
|
203
|
-
},
|
|
204
|
-
tenantQuota: {
|
|
205
|
-
defaultDailyBudgetUsd: parseNumber(
|
|
206
|
-
env.HARNESS_TENANT_DAILY_BUDGET_USD
|
|
207
|
-
),
|
|
208
|
-
defaultTokenLimit: parsePositiveInt(
|
|
209
|
-
env.HARNESS_TENANT_TOKEN_LIMIT
|
|
210
|
-
),
|
|
211
|
-
windowMs: parsePositiveInt(
|
|
212
|
-
env.HARNESS_TENANT_QUOTA_WINDOW_MS
|
|
213
|
-
)
|
|
214
|
-
}
|
|
215
|
-
},
|
|
216
|
-
graph: {
|
|
217
|
-
maxSteps: parsePositiveInt(
|
|
218
|
-
env.HARNESS_GRAPH_MAX_STEPS || env.LLM_MAX_STEPS
|
|
219
|
-
),
|
|
220
|
-
recursionLimit: parsePositiveInt(
|
|
221
|
-
env.HARNESS_GRAPH_RECURSION_LIMIT
|
|
222
|
-
),
|
|
223
|
-
checkpointerType: parseEnum(
|
|
224
|
-
env.HARNESS_GRAPH_CHECKPOINTER,
|
|
225
|
-
CHECKPOINTER_TYPES
|
|
226
|
-
)
|
|
227
|
-
},
|
|
228
|
-
resilience: {
|
|
229
|
-
timeoutMs: parsePositiveInt(
|
|
230
|
-
env.HARNESS_RESILIENCE_TIMEOUT_MS || env.LLM_TIMEOUT_MS
|
|
231
|
-
),
|
|
232
|
-
maxRetries: parseNonNegativeInt(
|
|
233
|
-
env.HARNESS_RESILIENCE_MAX_RETRIES
|
|
234
|
-
),
|
|
235
|
-
initialDelayMs: parseNonNegativeInt(
|
|
236
|
-
env.HARNESS_RESILIENCE_INITIAL_DELAY_MS
|
|
237
|
-
),
|
|
238
|
-
maxDelayMs: parsePositiveInt(
|
|
239
|
-
env.HARNESS_RESILIENCE_MAX_DELAY_MS
|
|
240
|
-
),
|
|
241
|
-
backoffFactor: parseNumber(
|
|
242
|
-
env.HARNESS_RESILIENCE_BACKOFF_FACTOR
|
|
243
|
-
),
|
|
244
|
-
fallbackModels: parseCsv(
|
|
245
|
-
env.HARNESS_FALLBACK_MODELS || env.LLM_FALLBACK_MODELS
|
|
246
|
-
)
|
|
247
|
-
},
|
|
248
|
-
guardrails: {
|
|
249
|
-
autoHealing: {
|
|
250
|
-
maxAttempts: parseNonNegativeInt(
|
|
251
|
-
env.HARNESS_AUTO_HEALING_MAX_ATTEMPTS
|
|
252
|
-
),
|
|
253
|
-
temperature: parseNumber(
|
|
254
|
-
env.HARNESS_AUTO_HEALING_TEMPERATURE
|
|
255
|
-
)
|
|
256
|
-
}
|
|
257
|
-
},
|
|
258
|
-
finops: {
|
|
259
|
-
storeType: parseEnum(
|
|
260
|
-
env.HARNESS_FINOPS_STORE_TYPE || env.LLM_COST_STORE,
|
|
261
|
-
FINOPS_STORE_TYPES
|
|
262
|
-
),
|
|
263
|
-
storagePath: env.HARNESS_FINOPS_STORAGE_PATH || env.LLM_COST_STORE_PATH,
|
|
264
|
-
redisUrl
|
|
265
|
-
},
|
|
266
|
-
stream: {
|
|
267
|
-
storeType: parseEnum(
|
|
268
|
-
env.HARNESS_STREAM_STORE_TYPE || env.LLM_TASK_STORE,
|
|
269
|
-
STREAM_STORE_TYPES
|
|
270
|
-
),
|
|
271
|
-
taskTtlMs: parsePositiveInt(
|
|
272
|
-
env.HARNESS_STREAM_TASK_TTL_MS
|
|
273
|
-
),
|
|
274
|
-
fileStoreDir: env.HARNESS_STREAM_FILE_DIR || env.LLM_TASK_STORE_DIR,
|
|
275
|
-
redisUrl,
|
|
276
|
-
heartbeatIntervalMs: parsePositiveInt(
|
|
277
|
-
env.HARNESS_STREAM_HEARTBEAT_MS
|
|
278
|
-
)
|
|
279
|
-
},
|
|
280
|
-
mcp: {
|
|
281
|
-
defaultTimeoutMs: parsePositiveInt(
|
|
282
|
-
env.HARNESS_MCP_TIMEOUT_MS
|
|
283
|
-
),
|
|
284
|
-
maxPoolSize: parsePositiveInt(
|
|
285
|
-
env.HARNESS_MCP_MAX_POOL_SIZE
|
|
286
|
-
),
|
|
287
|
-
reconnect: {
|
|
288
|
-
maxRetries: parseNonNegativeInt(
|
|
289
|
-
env.HARNESS_MCP_RECONNECT_MAX_RETRIES
|
|
290
|
-
),
|
|
291
|
-
delayMs: parseNonNegativeInt(
|
|
292
|
-
env.HARNESS_MCP_RECONNECT_DELAY_MS
|
|
293
|
-
)
|
|
294
|
-
}
|
|
295
|
-
},
|
|
296
|
-
telemetry: {
|
|
297
|
-
enabled: parseBoolean(
|
|
298
|
-
env.HARNESS_TELEMETRY_ENABLED
|
|
299
|
-
),
|
|
300
|
-
sampleRate: parseNumber(
|
|
301
|
-
env.HARNESS_TELEMETRY_SAMPLE_RATE
|
|
302
|
-
),
|
|
303
|
-
exporter: parseEnum(
|
|
304
|
-
env.HARNESS_TELEMETRY_EXPORTER,
|
|
305
|
-
TELEMETRY_EXPORTERS
|
|
306
|
-
),
|
|
307
|
-
otelEndpoint: env.HARNESS_OTEL_ENDPOINT || env.OTEL_EXPORTER_OTLP_ENDPOINT
|
|
308
|
-
}
|
|
309
|
-
};
|
|
310
|
-
return cleanUndefined(config);
|
|
311
|
-
}
|
|
312
|
-
function cleanUndefined(obj) {
|
|
313
|
-
if (obj === null || typeof obj !== "object" || Array.isArray(obj)) {
|
|
314
|
-
return obj;
|
|
315
|
-
}
|
|
316
|
-
const result = {};
|
|
317
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
318
|
-
if (value === void 0) continue;
|
|
319
|
-
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
320
|
-
const cleaned = cleanUndefined(value);
|
|
321
|
-
if (Object.keys(cleaned).length > 0) {
|
|
322
|
-
result[key] = cleaned;
|
|
323
|
-
}
|
|
324
|
-
} else {
|
|
325
|
-
result[key] = value;
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
return result;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// src/config/resolver.ts
|
|
332
|
-
function deepMerge(target, source) {
|
|
333
|
-
if (!source) return target;
|
|
334
|
-
const output = { ...target };
|
|
335
|
-
for (const [key, val] of Object.entries(source)) {
|
|
336
|
-
if (val === void 0) continue;
|
|
337
|
-
const targetVal = output[key];
|
|
338
|
-
if (val !== null && typeof val === "object" && !Array.isArray(val) && targetVal !== null && typeof targetVal === "object" && !Array.isArray(targetVal)) {
|
|
339
|
-
output[key] = deepMerge(
|
|
340
|
-
targetVal,
|
|
341
|
-
val
|
|
342
|
-
);
|
|
343
|
-
} else {
|
|
344
|
-
output[key] = val;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
return output;
|
|
348
|
-
}
|
|
349
|
-
function resolveHarnessConfig(userConfig, env) {
|
|
350
|
-
const envConfig = loadHarnessConfigFromEnv(env);
|
|
351
|
-
const merged = deepMerge(
|
|
352
|
-
deepMerge({}, envConfig),
|
|
353
|
-
userConfig
|
|
354
|
-
);
|
|
355
|
-
return HarnessConfigSchema.parse(merged);
|
|
356
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_FINOPS_CONFIG,
|
|
3
|
+
DEFAULT_GRAPH_CONFIG,
|
|
4
|
+
DEFAULT_GUARDRAILS_CONFIG,
|
|
5
|
+
DEFAULT_HARNESS_CONFIG,
|
|
6
|
+
DEFAULT_MCP_CONFIG,
|
|
7
|
+
DEFAULT_PIPELINE_CONFIG,
|
|
8
|
+
DEFAULT_RESILIENCE_CONFIG,
|
|
9
|
+
DEFAULT_STREAM_CONFIG,
|
|
10
|
+
DEFAULT_TELEMETRY_CONFIG,
|
|
11
|
+
HarnessConfigSchema,
|
|
12
|
+
loadHarnessConfigFromEnv,
|
|
13
|
+
resolveHarnessConfig
|
|
14
|
+
} from "./chunk-LASGGBG6.js";
|
|
15
|
+
import "./chunk-6OYMWYZS.js";
|
|
357
16
|
export {
|
|
358
17
|
DEFAULT_FINOPS_CONFIG,
|
|
359
18
|
DEFAULT_GRAPH_CONFIG,
|
package/dist/context.js
CHANGED
|
@@ -1,256 +1,25 @@
|
|
|
1
|
-
// src/providers/registry.ts
|
|
2
1
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
ContextEngine,
|
|
3
|
+
DEFAULT_PRUNED_PLACEHOLDER,
|
|
4
|
+
DEFAULT_SUMMARY_MAX_TOKENS,
|
|
5
|
+
buildGenericSummaryPrompt,
|
|
6
|
+
computeContextBudget,
|
|
7
|
+
estimateMessageTokens,
|
|
8
|
+
estimateMessagesTokens,
|
|
9
|
+
projectMessagesWithPruning,
|
|
10
|
+
resolveContextWindow,
|
|
11
|
+
serializeMessagesForSummary,
|
|
12
|
+
trimOrphanToolMessages
|
|
13
|
+
} from "./chunk-MWOHX4WC.js";
|
|
14
|
+
import {
|
|
15
|
+
estimateTextTokensHeuristic
|
|
16
|
+
} from "./chunk-VNSZH3H7.js";
|
|
17
|
+
import {
|
|
5
18
|
CONTEXT_WINDOW_MAX,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
resolveContextWindow
|
|
11
|
-
} from "@ginkgolab/models";
|
|
12
|
-
|
|
13
|
-
// src/context/budget.ts
|
|
14
|
-
function resolveContextWindow2(model, clientHint, catalog = defaultModelCatalog) {
|
|
15
|
-
return catalog.resolveContextWindow(model, clientHint);
|
|
16
|
-
}
|
|
17
|
-
function computeContextBudget(window, maxOutputTokens = 4096, reserveRatio = 0.25) {
|
|
18
|
-
const triggerAt = Math.min(
|
|
19
|
-
Math.floor(window * 0.7),
|
|
20
|
-
window - maxOutputTokens - 8192
|
|
21
|
-
);
|
|
22
|
-
return {
|
|
23
|
-
window,
|
|
24
|
-
triggerAt: Math.max(triggerAt, 1024),
|
|
25
|
-
retainTokens: Math.floor(window * Math.max(0.1, Math.min(reserveRatio, 0.5)))
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// src/tokenizer/heuristic.ts
|
|
30
|
-
var CJK_RE = /[一-鿿㐀-䶿䶿豈--ヿ가-]/g;
|
|
31
|
-
function estimateTextTokensHeuristic(text) {
|
|
32
|
-
if (!text) return 0;
|
|
33
|
-
const cjk = (text.match(CJK_RE) ?? []).length;
|
|
34
|
-
return Math.ceil((cjk / 1.5 + (text.length - cjk) / 4) * 1.1);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// src/context/estimator.ts
|
|
38
|
-
function safeSerialize(value) {
|
|
39
|
-
try {
|
|
40
|
-
return JSON.stringify(value) ?? "";
|
|
41
|
-
} catch {
|
|
42
|
-
return "";
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
function estimateMessageTokens(message, countText = estimateTextTokensHeuristic) {
|
|
46
|
-
const m = message;
|
|
47
|
-
if (!m || typeof m !== "object") return 0;
|
|
48
|
-
let tokens = 8;
|
|
49
|
-
const rest = m;
|
|
50
|
-
if (Array.isArray(rest.parts) && rest.parts.length > 0) {
|
|
51
|
-
tokens += countText(safeSerialize(rest.parts));
|
|
52
|
-
} else {
|
|
53
|
-
if (typeof m.content === "string") {
|
|
54
|
-
tokens += countText(m.content);
|
|
55
|
-
} else if (m.content !== void 0 && m.content !== null) {
|
|
56
|
-
tokens += countText(safeSerialize(m.content));
|
|
57
|
-
}
|
|
58
|
-
if (rest.toolInvocations !== void 0) {
|
|
59
|
-
tokens += countText(safeSerialize(rest.toolInvocations));
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (rest.annotations !== void 0) {
|
|
63
|
-
tokens += countText(safeSerialize(rest.annotations));
|
|
64
|
-
}
|
|
65
|
-
return tokens;
|
|
66
|
-
}
|
|
67
|
-
function estimateMessagesTokens(messages, countText = estimateTextTokensHeuristic) {
|
|
68
|
-
let total = 0;
|
|
69
|
-
for (const m of messages) {
|
|
70
|
-
total += estimateMessageTokens(m, countText);
|
|
71
|
-
}
|
|
72
|
-
return total;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// src/context/pruning.ts
|
|
76
|
-
var DEFAULT_PRUNED_PLACEHOLDER = (toolName) => `[\u5DF2\u88C1\u526A: ${toolName}]`;
|
|
77
|
-
function roleOf(m) {
|
|
78
|
-
return m?.role ?? "";
|
|
79
|
-
}
|
|
80
|
-
function toolNamesOf(m) {
|
|
81
|
-
const content = m?.content;
|
|
82
|
-
if (!Array.isArray(content)) return [];
|
|
83
|
-
return content.filter(
|
|
84
|
-
(p) => p !== null && typeof p === "object"
|
|
85
|
-
).filter((p) => p.type === "tool-result").map((p) => typeof p.toolName === "string" ? p.toolName : "tool");
|
|
86
|
-
}
|
|
87
|
-
function projectMessagesWithPruning(opts) {
|
|
88
|
-
const { messages, budget } = opts;
|
|
89
|
-
const overhead = opts.systemTokens ?? 0;
|
|
90
|
-
const countText = opts.tokenizer ? opts.tokenizer.encode.bind(opts.tokenizer) : estimateTextTokensHeuristic;
|
|
91
|
-
const heuristic = estimateMessagesTokens(messages, countText);
|
|
92
|
-
const estimated = Math.max(heuristic, opts.anchorTokens ?? 0) + overhead;
|
|
93
|
-
if (estimated <= budget.triggerAt) {
|
|
94
|
-
return {
|
|
95
|
-
messages,
|
|
96
|
-
pruned: false,
|
|
97
|
-
prunedCount: 0,
|
|
98
|
-
estimatedPromptTokens: estimated,
|
|
99
|
-
summarizeUpToIndex: -1
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
let acc = 0;
|
|
103
|
-
let retainFrom = messages.length;
|
|
104
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
105
|
-
const t = estimateMessageTokens(messages[i], countText);
|
|
106
|
-
if (acc + t > budget.retainTokens && retainFrom < messages.length) {
|
|
107
|
-
retainFrom = i + 1;
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
acc += t;
|
|
111
|
-
retainFrom = i;
|
|
112
|
-
}
|
|
113
|
-
if (retainFrom <= 0) {
|
|
114
|
-
return {
|
|
115
|
-
messages,
|
|
116
|
-
pruned: false,
|
|
117
|
-
prunedCount: 0,
|
|
118
|
-
estimatedPromptTokens: estimated,
|
|
119
|
-
summarizeUpToIndex: -1
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
const formatPlaceholder = opts.placeholderFormatter ?? DEFAULT_PRUNED_PLACEHOLDER;
|
|
123
|
-
let prunedCount = 0;
|
|
124
|
-
const projected = messages.map((m, i) => {
|
|
125
|
-
if (i >= retainFrom) return m;
|
|
126
|
-
if (roleOf(m) !== "tool") return m;
|
|
127
|
-
if (opts.isProtectedMessage?.(m, i, messages.length)) {
|
|
128
|
-
return m;
|
|
129
|
-
}
|
|
130
|
-
const content = m.content;
|
|
131
|
-
if (!Array.isArray(content)) return m;
|
|
132
|
-
const names = toolNamesOf(m);
|
|
133
|
-
prunedCount += names.length;
|
|
134
|
-
return {
|
|
135
|
-
...m,
|
|
136
|
-
content: content.map(
|
|
137
|
-
(p) => p !== null && typeof p === "object" && p.type === "tool-result" ? {
|
|
138
|
-
...p,
|
|
139
|
-
result: formatPlaceholder(
|
|
140
|
-
typeof p.toolName === "string" ? p.toolName : "tool"
|
|
141
|
-
)
|
|
142
|
-
} : p
|
|
143
|
-
)
|
|
144
|
-
};
|
|
145
|
-
});
|
|
146
|
-
const afterPrune = estimateMessagesTokens(projected, countText) + overhead;
|
|
147
|
-
const needSummary = afterPrune > budget.triggerAt;
|
|
148
|
-
return {
|
|
149
|
-
messages: projected,
|
|
150
|
-
pruned: prunedCount > 0,
|
|
151
|
-
prunedCount,
|
|
152
|
-
estimatedPromptTokens: afterPrune,
|
|
153
|
-
summarizeUpToIndex: needSummary ? retainFrom - 1 : -1
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// src/context/sanitizer.ts
|
|
158
|
-
function roleOf2(m) {
|
|
159
|
-
return m?.role ?? "";
|
|
160
|
-
}
|
|
161
|
-
function trimOrphanToolMessages(messages, fallbackUserPrompt = "\uFF08\u4EE5\u4E0A\u662F\u6B64\u524D\u4F1A\u8BDD\u7684\u6458\u8981\uFF0C\u8BF7\u57FA\u4E8E\u6B64\u7EE7\u7EED\u5F53\u524D\u4EFB\u52A1\u3002\uFF09") {
|
|
162
|
-
let start = 0;
|
|
163
|
-
while (start < messages.length && roleOf2(messages[start]) === "tool") {
|
|
164
|
-
start++;
|
|
165
|
-
}
|
|
166
|
-
const trimmed = messages.slice(start);
|
|
167
|
-
if (trimmed.length === 0 || roleOf2(trimmed[0]) !== "user") {
|
|
168
|
-
return [
|
|
169
|
-
{
|
|
170
|
-
role: "user",
|
|
171
|
-
content: fallbackUserPrompt
|
|
172
|
-
},
|
|
173
|
-
...trimmed
|
|
174
|
-
];
|
|
175
|
-
}
|
|
176
|
-
return trimmed;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// src/context/summary.ts
|
|
180
|
-
function roleOf3(m) {
|
|
181
|
-
return m?.role ?? "";
|
|
182
|
-
}
|
|
183
|
-
function textOf(m) {
|
|
184
|
-
const content = m?.content;
|
|
185
|
-
if (typeof content === "string") return content;
|
|
186
|
-
if (Array.isArray(content)) {
|
|
187
|
-
return content.map(
|
|
188
|
-
(p) => p && typeof p === "object" && typeof p.text === "string" ? p.text : ""
|
|
189
|
-
).join("");
|
|
190
|
-
}
|
|
191
|
-
return "";
|
|
192
|
-
}
|
|
193
|
-
function serializeMessagesForSummary(messages, maxCharsPerMessage = 4e3) {
|
|
194
|
-
return messages.map((m) => {
|
|
195
|
-
const text = textOf(m);
|
|
196
|
-
const clipped = text.length > maxCharsPerMessage ? `${text.slice(0, maxCharsPerMessage)}\u2026` : text;
|
|
197
|
-
return `[${roleOf3(m)}] ${clipped}`;
|
|
198
|
-
}).join("\n\n");
|
|
199
|
-
}
|
|
200
|
-
var DEFAULT_SUMMARY_MAX_TOKENS = 2048;
|
|
201
|
-
function buildGenericSummaryPrompt(opts) {
|
|
202
|
-
const { covered, previousSummary, domainGuidelines, maxWords = 1200 } = opts;
|
|
203
|
-
const guidelinesBlock = domainGuidelines && domainGuidelines.length > 0 ? `
|
|
204
|
-
\u7279\u522B\u5173\u6CE8\u5E76\u5FC5\u987B\u4FDD\u7559\uFF1A
|
|
205
|
-
${domainGuidelines.map((g, i) => `${i + 1}. ${g}`).join("\n")}
|
|
206
|
-
` : "";
|
|
207
|
-
return `\u4F60\u662F\u4E00\u4E2A\u4F1A\u8BDD\u5386\u53F2\u538B\u7F29\u5668\u3002\u628A\u4E0B\u9762\u7684\u4F1A\u8BDD\u5386\u53F2\u538B\u7F29\u6210\u4E00\u4EFD\u8FDE\u8D2F\u7D27\u51D1\u7684\u4E2D\u6587\u6458\u8981\uFF08\u4E0D\u8D85\u8FC7 ${maxWords} \u5B57\uFF09\uFF0C\u4F9B\u540E\u7EED\u5BF9\u8BDD\u4F5C\u4E3A\u4E0A\u4E0B\u6587\u80CC\u666F\u4F7F\u7528\u3002
|
|
208
|
-
${guidelinesBlock}
|
|
209
|
-
\u5FC5\u987B\u4E22\u5F03\uFF1A\u5197\u4F59\u5DE5\u5177\u8C03\u7528\u7EC6\u8282\u3001\u5E95\u5C42\u4EE3\u7801/\u6570\u636E\u539F\u6587\u5B57\u8282\u3001\u91CD\u590D\u5BD2\u6684\u4E0E\u65E0\u610F\u4E49\u8FC7\u6E21\u3002
|
|
210
|
-
${previousSummary ? `
|
|
211
|
-
\u6B64\u524D\u7684\u5386\u53F2\u6458\u8981\uFF08\u8BF7\u5728\u5176\u57FA\u7840\u4E0A\u589E\u91CF\u5408\u5E76\u66F4\u65B0\uFF0C\u907F\u514D\u9057\u6F0F\u5173\u952E\u7ED3\u8BBA\uFF09\uFF1A
|
|
212
|
-
${previousSummary}
|
|
213
|
-
` : ""}
|
|
214
|
-
\u9700\u8981\u538B\u7F29\u7684\u4F1A\u8BDD\u7247\u6BB5\uFF1A
|
|
215
|
-
${serializeMessagesForSummary(covered)}`;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// src/context/engine.ts
|
|
219
|
-
var ContextEngine = class {
|
|
220
|
-
constructor(options = {}) {
|
|
221
|
-
this.options = options;
|
|
222
|
-
}
|
|
223
|
-
options;
|
|
224
|
-
/**
|
|
225
|
-
* 解析指定模型上下文窗口大小
|
|
226
|
-
*/
|
|
227
|
-
resolveWindow(model, clientHint) {
|
|
228
|
-
return resolveContextWindow2(model, clientHint, this.options.catalog);
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* 计算指定窗口的保留水位预算
|
|
232
|
-
*/
|
|
233
|
-
computeBudget(window, maxOutputTokens = 4096, reserveRatio = 0.25) {
|
|
234
|
-
return computeContextBudget(window, maxOutputTokens, reserveRatio);
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
* 执行消息投影与第一道 Pruning
|
|
238
|
-
*/
|
|
239
|
-
project(opts) {
|
|
240
|
-
return projectMessagesWithPruning({
|
|
241
|
-
...opts,
|
|
242
|
-
tokenizer: this.options.tokenizer,
|
|
243
|
-
isProtectedMessage: this.options.isProtectedMessage,
|
|
244
|
-
placeholderFormatter: this.options.placeholderFormatter
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* 净化摘要/截断后的消息边界,移除孤儿工具结果并确保首条为 user
|
|
249
|
-
*/
|
|
250
|
-
trimBoundary(messages, fallbackUserPrompt) {
|
|
251
|
-
return trimOrphanToolMessages(messages, fallbackUserPrompt);
|
|
252
|
-
}
|
|
253
|
-
};
|
|
19
|
+
CONTEXT_WINDOW_MIN,
|
|
20
|
+
DEFAULT_CONTEXT_WINDOW
|
|
21
|
+
} from "./chunk-ZO2UWHNE.js";
|
|
22
|
+
import "./chunk-6OYMWYZS.js";
|
|
254
23
|
export {
|
|
255
24
|
CONTEXT_WINDOW_MAX,
|
|
256
25
|
CONTEXT_WINDOW_MIN,
|
|
@@ -264,7 +33,7 @@ export {
|
|
|
264
33
|
estimateMessagesTokens,
|
|
265
34
|
estimateTextTokensHeuristic,
|
|
266
35
|
projectMessagesWithPruning,
|
|
267
|
-
|
|
36
|
+
resolveContextWindow,
|
|
268
37
|
serializeMessagesForSummary,
|
|
269
38
|
trimOrphanToolMessages
|
|
270
39
|
};
|