@ginkgolab/harness-core 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/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/telemetry.js
CHANGED
|
@@ -1,308 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
function compositeSink(...sinks) {
|
|
15
|
-
return {
|
|
16
|
-
emit(event) {
|
|
17
|
-
for (const s of sinks) {
|
|
18
|
-
try {
|
|
19
|
-
s.emit(event);
|
|
20
|
-
} catch {
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
function newRunId() {
|
|
27
|
-
return globalThis.crypto.randomUUID();
|
|
28
|
-
}
|
|
29
|
-
function jsonByteSize(value) {
|
|
30
|
-
try {
|
|
31
|
-
return Buffer.byteLength(JSON.stringify(value) ?? "", "utf-8");
|
|
32
|
-
} catch {
|
|
33
|
-
return -1;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// src/telemetry/telemetryMetrics.ts
|
|
38
|
-
var DURATION_BUCKETS_MS = [100, 250, 500, 1e3, 2500, 5e3, 1e4, 3e4, 6e4, 3e5];
|
|
39
|
-
var TOKEN_BUCKETS = [1e3, 5e3, 1e4, 25e3, 5e4, 1e5, 25e4, 5e5, 1e6];
|
|
40
|
-
function createMetricsSink() {
|
|
41
|
-
const runsByMode = /* @__PURE__ */ new Map();
|
|
42
|
-
const taskFinish = /* @__PURE__ */ new Map();
|
|
43
|
-
const abortsByReason = /* @__PURE__ */ new Map();
|
|
44
|
-
const tools = /* @__PURE__ */ new Map();
|
|
45
|
-
const stepDuration = {
|
|
46
|
-
help: "LLM harness per-step duration",
|
|
47
|
-
buckets: DURATION_BUCKETS_MS,
|
|
48
|
-
counts: /* @__PURE__ */ new Map(),
|
|
49
|
-
sums: /* @__PURE__ */ new Map(),
|
|
50
|
-
totals: /* @__PURE__ */ new Map()
|
|
51
|
-
};
|
|
52
|
-
const taskDuration = {
|
|
53
|
-
help: "LLM harness task wall-clock duration",
|
|
54
|
-
buckets: DURATION_BUCKETS_MS,
|
|
55
|
-
counts: /* @__PURE__ */ new Map(),
|
|
56
|
-
sums: /* @__PURE__ */ new Map(),
|
|
57
|
-
totals: /* @__PURE__ */ new Map()
|
|
58
|
-
};
|
|
59
|
-
const taskTokens = {
|
|
60
|
-
help: "LLM harness task total tokens (prompt+completion)",
|
|
61
|
-
buckets: TOKEN_BUCKETS,
|
|
62
|
-
counts: /* @__PURE__ */ new Map(),
|
|
63
|
-
sums: /* @__PURE__ */ new Map(),
|
|
64
|
-
totals: /* @__PURE__ */ new Map()
|
|
65
|
-
};
|
|
66
|
-
function observe(h, labelKey, value) {
|
|
67
|
-
let counts = h.counts.get(labelKey);
|
|
68
|
-
if (!counts) {
|
|
69
|
-
counts = new Array(h.buckets.length + 1).fill(0);
|
|
70
|
-
h.counts.set(labelKey, counts);
|
|
71
|
-
h.sums.set(labelKey, 0);
|
|
72
|
-
h.totals.set(labelKey, 0);
|
|
73
|
-
}
|
|
74
|
-
for (let i = 0; i < h.buckets.length; i++) {
|
|
75
|
-
if (value <= h.buckets[i]) counts[i]++;
|
|
76
|
-
}
|
|
77
|
-
counts[h.buckets.length]++;
|
|
78
|
-
h.sums.set(labelKey, (h.sums.get(labelKey) ?? 0) + value);
|
|
79
|
-
h.totals.set(labelKey, (h.totals.get(labelKey) ?? 0) + 1);
|
|
80
|
-
}
|
|
81
|
-
function handleEvent(event) {
|
|
82
|
-
switch (event.type) {
|
|
83
|
-
case "run_start": {
|
|
84
|
-
runsByMode.set(event.mode, (runsByMode.get(event.mode) ?? 0) + 1);
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
case "task_finish": {
|
|
88
|
-
const key = `status="${event.status}",termination="${event.termination}"`;
|
|
89
|
-
taskFinish.set(key, (taskFinish.get(key) ?? 0) + 1);
|
|
90
|
-
observe(taskDuration, `status="${event.status}"`, event.durationMs);
|
|
91
|
-
if (event.totalPromptTokens !== void 0 || event.totalCompletionTokens !== void 0) {
|
|
92
|
-
observe(
|
|
93
|
-
taskTokens,
|
|
94
|
-
`status="${event.status}"`,
|
|
95
|
-
(event.totalPromptTokens ?? 0) + (event.totalCompletionTokens ?? 0)
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
break;
|
|
99
|
-
}
|
|
100
|
-
case "abort": {
|
|
101
|
-
abortsByReason.set(event.reason, (abortsByReason.get(event.reason) ?? 0) + 1);
|
|
102
|
-
break;
|
|
103
|
-
}
|
|
104
|
-
case "tool_result": {
|
|
105
|
-
const stat = tools.get(event.tool) ?? { ok: 0, failed: 0, totalDurationMs: 0 };
|
|
106
|
-
if (event.ok) stat.ok++;
|
|
107
|
-
else stat.failed++;
|
|
108
|
-
stat.totalDurationMs += event.durationMs;
|
|
109
|
-
tools.set(event.tool, stat);
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
case "step_finish": {
|
|
113
|
-
observe(stepDuration, `finish_reason="${event.finishReason}"`, event.durationMs);
|
|
114
|
-
break;
|
|
115
|
-
}
|
|
116
|
-
default:
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
const sink = {
|
|
121
|
-
emit: (event) => {
|
|
122
|
-
try {
|
|
123
|
-
handleEvent(event);
|
|
124
|
-
} catch {
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
function renderHistogram(name, h) {
|
|
129
|
-
const lines = [`# HELP ${name} ${h.help}`, `# TYPE ${name} histogram`];
|
|
130
|
-
for (const [labelKey, counts] of h.counts) {
|
|
131
|
-
const labels = labelKey ? `{${labelKey},` : "{";
|
|
132
|
-
for (let i = 0; i < h.buckets.length; i++) {
|
|
133
|
-
const le = h.buckets[i] === Infinity ? "+Inf" : String(h.buckets[i]);
|
|
134
|
-
lines.push(`${name}_bucket${labels}le="${le}"} ${counts[i]}`);
|
|
135
|
-
}
|
|
136
|
-
lines.push(`${name}_bucket${labels}le="+Inf"} ${counts[h.buckets.length]}`);
|
|
137
|
-
lines.push(`${name}_sum${labelKey ? `{${labelKey}}` : ""} ${h.sums.get(labelKey) ?? 0}`);
|
|
138
|
-
lines.push(`${name}_count${labelKey ? `{${labelKey}}` : ""} ${h.totals.get(labelKey) ?? 0}`);
|
|
139
|
-
}
|
|
140
|
-
return lines.join("\n");
|
|
141
|
-
}
|
|
142
|
-
return {
|
|
143
|
-
sink,
|
|
144
|
-
snapshot() {
|
|
145
|
-
return {
|
|
146
|
-
runsByMode: Object.fromEntries(runsByMode),
|
|
147
|
-
taskFinish: Object.fromEntries(taskFinish),
|
|
148
|
-
abortsByReason: Object.fromEntries(abortsByReason),
|
|
149
|
-
tools: Object.fromEntries(tools)
|
|
150
|
-
};
|
|
151
|
-
},
|
|
152
|
-
renderPrometheus(extraGauges = {}) {
|
|
153
|
-
const lines = [];
|
|
154
|
-
lines.push("# HELP harness_runs_total Analysis runs started");
|
|
155
|
-
lines.push("# TYPE harness_runs_total counter");
|
|
156
|
-
for (const [mode, count] of runsByMode) {
|
|
157
|
-
lines.push(`harness_runs_total{mode="${mode}"} ${count}`);
|
|
158
|
-
}
|
|
159
|
-
lines.push("# HELP harness_task_finish_total Tasks finished by status and termination");
|
|
160
|
-
lines.push("# TYPE harness_task_finish_total counter");
|
|
161
|
-
for (const [key, count] of taskFinish) {
|
|
162
|
-
lines.push(`harness_task_finish_total{${key}} ${count}`);
|
|
163
|
-
}
|
|
164
|
-
lines.push("# HELP harness_abort_total Tasks aborted by reason");
|
|
165
|
-
lines.push("# TYPE harness_abort_total counter");
|
|
166
|
-
for (const [reason, count] of abortsByReason) {
|
|
167
|
-
lines.push(`harness_abort_total{reason="${reason}"} ${count}`);
|
|
168
|
-
}
|
|
169
|
-
lines.push("# HELP harness_tool_results_total Tool executions by outcome");
|
|
170
|
-
lines.push("# TYPE harness_tool_results_total counter");
|
|
171
|
-
for (const [tool, stat] of tools) {
|
|
172
|
-
lines.push(`harness_tool_results_total{tool="${tool}",ok="true"} ${stat.ok}`);
|
|
173
|
-
lines.push(`harness_tool_results_total{tool="${tool}",ok="false"} ${stat.failed}`);
|
|
174
|
-
}
|
|
175
|
-
lines.push(renderHistogram("harness_step_duration_ms", stepDuration));
|
|
176
|
-
lines.push(renderHistogram("harness_task_duration_ms", taskDuration));
|
|
177
|
-
lines.push(renderHistogram("harness_task_total_tokens", taskTokens));
|
|
178
|
-
for (const [name, value] of Object.entries(extraGauges)) {
|
|
179
|
-
lines.push(`# TYPE ${name} gauge`);
|
|
180
|
-
lines.push(`${name} ${value}`);
|
|
181
|
-
}
|
|
182
|
-
return lines.join("\n") + "\n";
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// src/telemetry/otelSink.ts
|
|
188
|
-
var ROOT_SPAN_MAX_AGE_MS = 30 * 60 * 1e3;
|
|
189
|
-
function createOtelSink(opts) {
|
|
190
|
-
const instanceId = opts.instanceId ?? process.env.LLM_INSTANCE_ID;
|
|
191
|
-
const runs = /* @__PURE__ */ new Map();
|
|
192
|
-
const baseAttributes = () => ({
|
|
193
|
-
"service.name": opts.serviceName ?? "ginkgo",
|
|
194
|
-
...instanceId !== void 0 ? { "instance.id": instanceId } : {}
|
|
195
|
-
});
|
|
196
|
-
const pruneStale = () => {
|
|
197
|
-
const now = Date.now();
|
|
198
|
-
for (const [runId, entry] of runs) {
|
|
199
|
-
if (now - entry.startedAt > ROOT_SPAN_MAX_AGE_MS) {
|
|
200
|
-
entry.span.end?.();
|
|
201
|
-
runs.delete(runId);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
};
|
|
205
|
-
function handleEvent(event) {
|
|
206
|
-
pruneStale();
|
|
207
|
-
const root = event.runId !== void 0 ? runs.get(event.runId) : void 0;
|
|
208
|
-
switch (event.type) {
|
|
209
|
-
case "run_start": {
|
|
210
|
-
const span = opts.tracer.startSpan("harness.run", {
|
|
211
|
-
attributes: {
|
|
212
|
-
...baseAttributes(),
|
|
213
|
-
"run.id": event.runId,
|
|
214
|
-
...event.sessionId !== void 0 ? { "session.id": event.sessionId } : {},
|
|
215
|
-
"harness.mode": event.mode,
|
|
216
|
-
"harness.model": event.model,
|
|
217
|
-
"harness.max_steps": event.maxSteps
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
runs.set(event.runId, { span, startedAt: Date.now() });
|
|
221
|
-
break;
|
|
222
|
-
}
|
|
223
|
-
case "step_finish": {
|
|
224
|
-
const span = opts.tracer.startSpan("harness.step", {
|
|
225
|
-
attributes: {
|
|
226
|
-
...baseAttributes(),
|
|
227
|
-
"run.id": event.runId,
|
|
228
|
-
"harness.step": event.step,
|
|
229
|
-
"harness.finish_reason": event.finishReason,
|
|
230
|
-
"harness.tool_calls": event.toolCalls.join(","),
|
|
231
|
-
...event.promptTokens !== void 0 ? { "harness.prompt_tokens": event.promptTokens } : {},
|
|
232
|
-
"harness.duration_ms": event.durationMs
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
span.end?.();
|
|
236
|
-
break;
|
|
237
|
-
}
|
|
238
|
-
case "tool_result": {
|
|
239
|
-
const span = opts.tracer.startSpan("harness.tool", {
|
|
240
|
-
attributes: {
|
|
241
|
-
...baseAttributes(),
|
|
242
|
-
...event.runId !== void 0 ? { "run.id": event.runId } : {},
|
|
243
|
-
"harness.tool": event.tool,
|
|
244
|
-
...event.sourceId !== void 0 ? { "datasource.id": event.sourceId } : {},
|
|
245
|
-
"harness.ok": event.ok,
|
|
246
|
-
"harness.result_bytes": event.bytes,
|
|
247
|
-
"harness.duration_ms": event.durationMs
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
span.end?.();
|
|
251
|
-
break;
|
|
252
|
-
}
|
|
253
|
-
case "context_projection": {
|
|
254
|
-
root?.span.setAttribute("harness.context_pruned", event.pruned);
|
|
255
|
-
root?.span.setAttribute("harness.context_summarized", event.summarized);
|
|
256
|
-
root?.span.setAttribute("harness.context_estimated_tokens", event.estimated);
|
|
257
|
-
break;
|
|
258
|
-
}
|
|
259
|
-
case "warn": {
|
|
260
|
-
root?.span.setAttribute("harness.last_warn_code", event.code);
|
|
261
|
-
break;
|
|
262
|
-
}
|
|
263
|
-
case "error": {
|
|
264
|
-
root?.span.setAttribute("harness.last_error_code", event.code);
|
|
265
|
-
root?.span.setAttribute("error", true);
|
|
266
|
-
root?.span.recordException?.(new Error(event.detail ?? event.code));
|
|
267
|
-
break;
|
|
268
|
-
}
|
|
269
|
-
case "task_finish": {
|
|
270
|
-
if (!root) break;
|
|
271
|
-
root.span.setAttribute("harness.termination", event.termination);
|
|
272
|
-
root.span.setAttribute("harness.status", event.status);
|
|
273
|
-
if (event.totalPromptTokens !== void 0) {
|
|
274
|
-
root.span.setAttribute("harness.total_prompt_tokens", event.totalPromptTokens);
|
|
275
|
-
}
|
|
276
|
-
if (event.totalCompletionTokens !== void 0) {
|
|
277
|
-
root.span.setAttribute("harness.total_completion_tokens", event.totalCompletionTokens);
|
|
278
|
-
}
|
|
279
|
-
root.span.setAttribute("harness.duration_ms", event.durationMs);
|
|
280
|
-
root.span.end?.();
|
|
281
|
-
runs.delete(event.runId);
|
|
282
|
-
break;
|
|
283
|
-
}
|
|
284
|
-
case "abort": {
|
|
285
|
-
if (!root) break;
|
|
286
|
-
root.span.setAttribute("harness.abort_reason", event.reason);
|
|
287
|
-
root.span.end?.();
|
|
288
|
-
if (event.runId) runs.delete(event.runId);
|
|
289
|
-
break;
|
|
290
|
-
}
|
|
291
|
-
case "reconnect": {
|
|
292
|
-
root?.span.setAttribute("harness.reconnect_replayed_chunks", event.replayedChunks);
|
|
293
|
-
break;
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
return {
|
|
298
|
-
emit: (event) => {
|
|
299
|
-
try {
|
|
300
|
-
handleEvent(event);
|
|
301
|
-
} catch {
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
};
|
|
305
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
createMetricsSink,
|
|
3
|
+
createOtelSink
|
|
4
|
+
} from "./chunk-K2XRWRKF.js";
|
|
5
|
+
import {
|
|
6
|
+
compositeSink,
|
|
7
|
+
createConsoleSink,
|
|
8
|
+
jsonByteSize,
|
|
9
|
+
newRunId
|
|
10
|
+
} from "./chunk-A7RQFPRO.js";
|
|
11
|
+
import "./chunk-6OYMWYZS.js";
|
|
306
12
|
export {
|
|
307
13
|
compositeSink,
|
|
308
14
|
createConsoleSink,
|
package/dist/tokenizer.js
CHANGED
|
@@ -1,76 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var HeuristicTokenizer = class {
|
|
12
|
-
encode(text) {
|
|
13
|
-
return estimateTextTokensHeuristic(text);
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
// src/tokenizer/tiktoken.ts
|
|
18
|
-
var warnedMissing = false;
|
|
19
|
-
var encoderCache = /* @__PURE__ */ new Map();
|
|
20
|
-
async function createTiktokenEstimator(encoding = DEFAULT_TIKTOKEN_ENCODING, options) {
|
|
21
|
-
try {
|
|
22
|
-
const pkgName = "js-tiktoken";
|
|
23
|
-
const mod = await import(
|
|
24
|
-
/* @vite-ignore */
|
|
25
|
-
pkgName
|
|
26
|
-
);
|
|
27
|
-
if (typeof mod.getEncoding !== "function") {
|
|
28
|
-
throw new Error("js-tiktoken \u683C\u5F0F\u4E0D\u5339\u914D\uFF08\u7F3A\u5C11 getEncoding \u65B9\u6CD5\uFF09");
|
|
29
|
-
}
|
|
30
|
-
let encoder = encoderCache.get(encoding);
|
|
31
|
-
if (!encoder) {
|
|
32
|
-
encoder = mod.getEncoding(encoding);
|
|
33
|
-
encoderCache.set(encoding, encoder);
|
|
34
|
-
}
|
|
35
|
-
return {
|
|
36
|
-
encode: (text) => encoder.encode(text).length
|
|
37
|
-
};
|
|
38
|
-
} catch (error) {
|
|
39
|
-
if (!warnedMissing) {
|
|
40
|
-
warnedMissing = true;
|
|
41
|
-
if (options?.onWarnMissing) {
|
|
42
|
-
options.onWarnMissing(error);
|
|
43
|
-
} else {
|
|
44
|
-
console.warn(
|
|
45
|
-
`[tokenizer] Optional peer dependency "js-tiktoken" is not available (${error instanceof Error ? error.message : String(error)}). Falling back to heuristic estimation.`
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return void 0;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// src/tokenizer/encodingResolver.ts
|
|
54
|
-
function resolveEncodingForModel(model, mappingOrPath) {
|
|
55
|
-
let table;
|
|
56
|
-
if (typeof mappingOrPath === "object" && mappingOrPath !== null) {
|
|
57
|
-
table = mappingOrPath;
|
|
58
|
-
}
|
|
59
|
-
if (!table) {
|
|
60
|
-
return DEFAULT_TIKTOKEN_ENCODING;
|
|
61
|
-
}
|
|
62
|
-
const lower = model.toLowerCase();
|
|
63
|
-
let bestKey;
|
|
64
|
-
for (const key of Object.keys(table)) {
|
|
65
|
-
if (lower.startsWith(key.toLowerCase()) && (bestKey === void 0 || key.length > bestKey.length)) {
|
|
66
|
-
bestKey = key;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
if (bestKey !== void 0 && typeof table[bestKey] === "string") {
|
|
70
|
-
return table[bestKey];
|
|
71
|
-
}
|
|
72
|
-
return DEFAULT_TIKTOKEN_ENCODING;
|
|
73
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_TIKTOKEN_ENCODING,
|
|
3
|
+
createTiktokenEstimator,
|
|
4
|
+
resolveEncodingForModel
|
|
5
|
+
} from "./chunk-2N25X2GE.js";
|
|
6
|
+
import {
|
|
7
|
+
HeuristicTokenizer,
|
|
8
|
+
estimateTextTokensHeuristic
|
|
9
|
+
} from "./chunk-VNSZH3H7.js";
|
|
10
|
+
import "./chunk-6OYMWYZS.js";
|
|
74
11
|
export {
|
|
75
12
|
DEFAULT_TIKTOKEN_ENCODING,
|
|
76
13
|
HeuristicTokenizer,
|
package/dist/tools.js
CHANGED
|
@@ -1,161 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
const origExecute = orig.execute.bind(t);
|
|
14
|
-
wrapped[name] = {
|
|
15
|
-
...t,
|
|
16
|
-
execute: async (args, callOpts) => {
|
|
17
|
-
const extraCtx = getContext ? getContext(name) : {};
|
|
18
|
-
const ctx = {
|
|
19
|
-
toolName: name,
|
|
20
|
-
...extraCtx
|
|
21
|
-
};
|
|
22
|
-
let index = -1;
|
|
23
|
-
const dispatch = async (i, currentArgs) => {
|
|
24
|
-
if (i <= index) {
|
|
25
|
-
throw new Error("next() called multiple times in tool interceptor");
|
|
26
|
-
}
|
|
27
|
-
index = i;
|
|
28
|
-
if (i === interceptors.length) {
|
|
29
|
-
return origExecute(currentArgs, callOpts);
|
|
30
|
-
}
|
|
31
|
-
const interceptor = interceptors[i];
|
|
32
|
-
return interceptor(
|
|
33
|
-
currentArgs,
|
|
34
|
-
ctx,
|
|
35
|
-
(nextArgs) => dispatch(i + 1, nextArgs !== void 0 ? nextArgs : currentArgs)
|
|
36
|
-
);
|
|
37
|
-
};
|
|
38
|
-
return dispatch(0, args);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
return wrapped;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// src/telemetry/telemetry.ts
|
|
46
|
-
function jsonByteSize(value) {
|
|
47
|
-
try {
|
|
48
|
-
return Buffer.byteLength(JSON.stringify(value) ?? "", "utf-8");
|
|
49
|
-
} catch {
|
|
50
|
-
return -1;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// src/tools/telemetryAspect.ts
|
|
55
|
-
function createTelemetryInterceptor(opts) {
|
|
56
|
-
return async (args, ctx, next) => {
|
|
57
|
-
const t0 = Date.now();
|
|
58
|
-
const runId = ctx.runId ?? opts.runId;
|
|
59
|
-
const sessionId = ctx.sessionId ?? opts.sessionId;
|
|
60
|
-
const sourceId = ctx.sourceId ?? opts.sourceId;
|
|
61
|
-
try {
|
|
62
|
-
const result = await next(args);
|
|
63
|
-
const isErrorObj = result && typeof result === "object" && result.isError === true;
|
|
64
|
-
if (runId) {
|
|
65
|
-
opts.telemetry.emit({
|
|
66
|
-
type: "tool_result",
|
|
67
|
-
runId,
|
|
68
|
-
sessionId,
|
|
69
|
-
tool: ctx.toolName,
|
|
70
|
-
sourceId,
|
|
71
|
-
ok: !isErrorObj,
|
|
72
|
-
bytes: jsonByteSize(result),
|
|
73
|
-
durationMs: Date.now() - t0
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
return result;
|
|
77
|
-
} catch (err) {
|
|
78
|
-
if (runId) {
|
|
79
|
-
opts.telemetry.emit({
|
|
80
|
-
type: "tool_result",
|
|
81
|
-
runId,
|
|
82
|
-
sessionId,
|
|
83
|
-
tool: ctx.toolName,
|
|
84
|
-
sourceId,
|
|
85
|
-
ok: false,
|
|
86
|
-
bytes: -1,
|
|
87
|
-
durationMs: Date.now() - t0
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
throw err;
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// src/tools/timeoutAspect.ts
|
|
96
|
-
var ToolTimeoutError = class extends Error {
|
|
97
|
-
constructor(toolName, timeoutMs) {
|
|
98
|
-
super(`Tool "${toolName}" timed out after ${timeoutMs}ms`);
|
|
99
|
-
this.toolName = toolName;
|
|
100
|
-
this.timeoutMs = timeoutMs;
|
|
101
|
-
this.name = "ToolTimeoutError";
|
|
102
|
-
}
|
|
103
|
-
toolName;
|
|
104
|
-
timeoutMs;
|
|
105
|
-
};
|
|
106
|
-
function createTimeoutInterceptor(timeoutMs) {
|
|
107
|
-
if (timeoutMs <= 0) {
|
|
108
|
-
return async (args, _ctx, next) => next(args);
|
|
109
|
-
}
|
|
110
|
-
return async (args, ctx, next) => {
|
|
111
|
-
let timer;
|
|
112
|
-
const timeoutPromise = new Promise((_, reject) => {
|
|
113
|
-
timer = setTimeout(() => {
|
|
114
|
-
reject(new ToolTimeoutError(ctx.toolName, timeoutMs));
|
|
115
|
-
}, timeoutMs);
|
|
116
|
-
});
|
|
117
|
-
try {
|
|
118
|
-
return await Promise.race([next(args), timeoutPromise]);
|
|
119
|
-
} finally {
|
|
120
|
-
if (timer) {
|
|
121
|
-
clearTimeout(timer);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// src/tools/zodTool.ts
|
|
128
|
-
import { tool, jsonSchema } from "ai";
|
|
129
|
-
import { z } from "zod";
|
|
130
|
-
function zodTool(def) {
|
|
131
|
-
let modelSchema;
|
|
132
|
-
try {
|
|
133
|
-
modelSchema = z.toJSONSchema(def.parameters, {
|
|
134
|
-
target: "draft-7",
|
|
135
|
-
io: "input"
|
|
136
|
-
});
|
|
137
|
-
} catch {
|
|
138
|
-
modelSchema = { type: "object" };
|
|
139
|
-
}
|
|
140
|
-
return tool({
|
|
141
|
-
description: def.description,
|
|
142
|
-
// jsonSchema() 不带 validate = 模型侧宽松入口:类型小错的参数直达 execute,
|
|
143
|
-
// 由下方 zod 校验反馈(而非 parseToolCall 处抛 InvalidToolArgumentsError)
|
|
144
|
-
parameters: jsonSchema(modelSchema),
|
|
145
|
-
execute: async (raw) => {
|
|
146
|
-
const parsed = def.parameters.safeParse(raw);
|
|
147
|
-
if (!parsed.success) {
|
|
148
|
-
return {
|
|
149
|
-
error: `\u53C2\u6570\u6821\u9A8C\u5931\u8D25: ${JSON.stringify(
|
|
150
|
-
parsed.error.issues
|
|
151
|
-
)}\u3002\u8BF7\u6309\u5DE5\u5177\u53C2\u6570\u5B9A\u4E49\u4FEE\u6B63\u5FC5\u586B\u9879\u4E0E\u7C7B\u578B\u540E\u91CD\u65B0\u8C03\u7528\u3002`,
|
|
152
|
-
isError: true
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
return def.execute(parsed.data);
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
ToolTimeoutError,
|
|
3
|
+
composeToolInterceptors,
|
|
4
|
+
createTelemetryInterceptor,
|
|
5
|
+
createTimeoutInterceptor
|
|
6
|
+
} from "./chunk-PPR3G4HX.js";
|
|
7
|
+
import {
|
|
8
|
+
zodTool
|
|
9
|
+
} from "./chunk-SCHUPQP4.js";
|
|
10
|
+
import "./chunk-A7RQFPRO.js";
|
|
11
|
+
import "./chunk-6OYMWYZS.js";
|
|
159
12
|
export {
|
|
160
13
|
ToolTimeoutError,
|
|
161
14
|
composeToolInterceptors,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ginkgolab/harness-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"@ai-sdk/anthropic": "1.2.12",
|
|
61
61
|
"@ai-sdk/openai": "1.3.24",
|
|
62
|
-
"@ginkgolab/models": "0.1.
|
|
62
|
+
"@ginkgolab/models": "0.1.1",
|
|
63
63
|
"@modelcontextprotocol/sdk": "^1.32.1",
|
|
64
64
|
"ai": "4.3.19",
|
|
65
65
|
"zod": "4.6.7"
|