@tangle-network/agent-runtime 0.93.1 → 0.94.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/README.md +4 -3
- package/dist/agent.d.ts +1 -1
- package/dist/agent.js +4 -5
- package/dist/agent.js.map +1 -1
- package/dist/analyst-loop.d.ts +1 -1
- package/dist/candidate-execution/index.d.ts +10 -3
- package/dist/candidate-execution/index.js +8 -2
- package/dist/{chunk-XDSWWUKE.js → chunk-4E2GM63H.js} +6 -29
- package/dist/chunk-4E2GM63H.js.map +1 -0
- package/dist/{chunk-LQQPGRKT.js → chunk-4GBT4WQ6.js} +2 -2
- package/dist/{chunk-TKJ3Q6CQ.js → chunk-55P6V5JT.js} +2 -2
- package/dist/{chunk-OX3QRJOA.js → chunk-7I2AOKCI.js} +280 -250
- package/dist/chunk-7I2AOKCI.js.map +1 -0
- package/dist/{chunk-UO2L5VTP.js → chunk-FHZHADIM.js} +1328 -20
- package/dist/chunk-FHZHADIM.js.map +1 -0
- package/dist/{chunk-VMHKMNEU.js → chunk-H2D3DCEB.js} +8 -161
- package/dist/chunk-H2D3DCEB.js.map +1 -0
- package/dist/chunk-ISTDY47H.js +849 -0
- package/dist/chunk-ISTDY47H.js.map +1 -0
- package/dist/{chunk-P6B3Z7PR.js → chunk-MRYPFDHJ.js} +3 -3
- package/dist/{chunk-BVVRQ4YC.js → chunk-OO4EK3JB.js} +1228 -18
- package/dist/chunk-OO4EK3JB.js.map +1 -0
- package/dist/{chunk-CMYMTRGA.js → chunk-PX6SXX3M.js} +26 -3
- package/dist/chunk-PX6SXX3M.js.map +1 -0
- package/dist/{chunk-FRBHUNQ7.js → chunk-RTVVH2KL.js} +2 -2
- package/dist/{chunk-R5GWDTM3.js → chunk-ZEYAT33L.js} +2 -2
- package/dist/{completion-gate-BLaiN0-X.d.ts → completion-gate-BDkEYB_-.d.ts} +1 -1
- package/dist/{coordination-DxJ83oZA.d.ts → coordination-EGoRbbsd.d.ts} +4 -4
- package/dist/environment-provider.d.ts +2 -2
- package/dist/{improve-DDhQaaJT.d.ts → improve-ZzTEkpF_.d.ts} +11 -14
- package/dist/index.d.ts +35 -106
- package/dist/index.js +91 -35
- package/dist/index.js.map +1 -1
- package/dist/intelligence.d.ts +22 -5
- package/dist/intelligence.js +113 -19
- package/dist/intelligence.js.map +1 -1
- package/dist/knowledge.d.ts +6 -6
- package/dist/knowledge.js +4 -4
- package/dist/lifecycle.js +2 -2
- package/dist/{loop-runner-bin-kKUNGLyV.d.ts → loop-runner-bin-BRXC1RwC.d.ts} +2 -2
- package/dist/loop-runner-bin.d.ts +5 -5
- package/dist/loop-runner-bin.js +5 -6
- package/dist/loops.d.ts +456 -17
- package/dist/loops.js +38 -40
- package/dist/mcp/bin.js +4 -4
- package/dist/mcp/index.d.ts +8 -8
- package/dist/mcp/index.js +6 -7
- package/dist/mcp/index.js.map +1 -1
- package/dist/{openai-tools-E3woykz9.d.ts → openai-tools-D0ZSRCC6.d.ts} +1 -1
- package/dist/{prepare-DiVGKcwS.d.ts → prepare-BKxAiUcH.d.ts} +1 -1
- package/dist/profiles.d.ts +1 -1
- package/dist/{sanitize-C9go6tXj.d.ts → sanitize-C2jicjNf.d.ts} +1 -1
- package/dist/{supervise-T2pazU3G.d.ts → supervise-Gmf709kn.d.ts} +4 -4
- package/dist/{types-DAdIm4AC.d.ts → types-Cg_teUPO.d.ts} +2 -2
- package/dist/{types-B00NtbCs.d.ts → types-DWA64rbJ.d.ts} +1 -1
- package/dist/{worktree-fanout-BUb2Ag02.d.ts → worktree-fanout-BrB4att3.d.ts} +233 -233
- package/package.json +4 -4
- package/skills/build-with-agent-runtime/SKILL.md +1 -1
- package/dist/chunk-BVVRQ4YC.js.map +0 -1
- package/dist/chunk-CMYMTRGA.js.map +0 -1
- package/dist/chunk-FDJ7AHXG.js +0 -1229
- package/dist/chunk-FDJ7AHXG.js.map +0 -1
- package/dist/chunk-J2K6WIG6.js +0 -2172
- package/dist/chunk-J2K6WIG6.js.map +0 -1
- package/dist/chunk-OX3QRJOA.js.map +0 -1
- package/dist/chunk-UO2L5VTP.js.map +0 -1
- package/dist/chunk-VMHKMNEU.js.map +0 -1
- package/dist/chunk-XDSWWUKE.js.map +0 -1
- package/dist/structural-rollout-DHGDbhvR.d.ts +0 -446
- /package/dist/{chunk-LQQPGRKT.js.map → chunk-4GBT4WQ6.js.map} +0 -0
- /package/dist/{chunk-TKJ3Q6CQ.js.map → chunk-55P6V5JT.js.map} +0 -0
- /package/dist/{chunk-P6B3Z7PR.js.map → chunk-MRYPFDHJ.js.map} +0 -0
- /package/dist/{chunk-FRBHUNQ7.js.map → chunk-RTVVH2KL.js.map} +0 -0
- /package/dist/{chunk-R5GWDTM3.js.map → chunk-ZEYAT33L.js.map} +0 -0
|
@@ -0,0 +1,849 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConfigError
|
|
3
|
+
} from "./chunk-YEJR7IXO.js";
|
|
4
|
+
|
|
5
|
+
// src/runtime/supervise/model-policy.ts
|
|
6
|
+
function assertModelAllowed(model, allowed) {
|
|
7
|
+
if (!allowed || model === void 0) return;
|
|
8
|
+
if (!allowed.includes(model)) {
|
|
9
|
+
throw new ConfigError(
|
|
10
|
+
`model ${JSON.stringify(model)} is not in the allowed set ${JSON.stringify([...allowed])}`
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// src/sanitize.ts
|
|
16
|
+
function sanitizeKnowledgeReadinessReport(report, options = {}) {
|
|
17
|
+
return {
|
|
18
|
+
taskId: report.taskId,
|
|
19
|
+
readinessScore: report.readinessScore,
|
|
20
|
+
recommendedAction: report.recommendedAction,
|
|
21
|
+
severity: report.severity,
|
|
22
|
+
reason: report.reason,
|
|
23
|
+
blockingMissingRequirements: report.blockingMissingRequirements.map(
|
|
24
|
+
(requirement) => sanitizeKnowledgeRequirement(requirement, options)
|
|
25
|
+
),
|
|
26
|
+
nonBlockingGaps: report.nonBlockingGaps.map(
|
|
27
|
+
(requirement) => sanitizeKnowledgeRequirement(requirement, options)
|
|
28
|
+
),
|
|
29
|
+
evidenceCount: report.bundle.evidenceIds.length,
|
|
30
|
+
evidenceIds: options.includeEvidenceIds ? report.bundle.evidenceIds : void 0,
|
|
31
|
+
missingRequirementIds: report.bundle.missing.map((requirement) => requirement.id)
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function sanitizeAgentRuntimeEvent(event, options = {}) {
|
|
35
|
+
const base = { type: event.type, task: sanitizeTask(event.task, options) };
|
|
36
|
+
if (event.type === "readiness_start" || event.type === "task_start" || event.type === "control_start") {
|
|
37
|
+
return event.type === "control_start" ? { ...base, knowledge: sanitizeKnowledgeReadinessReport(event.knowledge, options) } : base;
|
|
38
|
+
}
|
|
39
|
+
if (event.type === "readiness_end") {
|
|
40
|
+
return { ...base, knowledge: sanitizeKnowledgeReadinessReport(event.knowledge, options) };
|
|
41
|
+
}
|
|
42
|
+
if (event.type === "questions_start") {
|
|
43
|
+
return {
|
|
44
|
+
...base,
|
|
45
|
+
questions: event.questions.map((question) => sanitizeQuestion(question, options))
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (event.type === "questions_end") {
|
|
49
|
+
return {
|
|
50
|
+
...base,
|
|
51
|
+
questions: event.questions.map((question) => sanitizeQuestion(question, options)),
|
|
52
|
+
userAnswers: options.includeUserAnswers ? event.userAnswers : redactRecord(event.userAnswers)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (event.type === "acquisition_start") {
|
|
56
|
+
return { ...base, acquisitionPlans: event.acquisitionPlans.map(sanitizeAcquisitionPlan) };
|
|
57
|
+
}
|
|
58
|
+
if (event.type === "acquisition_end") {
|
|
59
|
+
return {
|
|
60
|
+
...base,
|
|
61
|
+
acquisitionPlans: event.acquisitionPlans.map(sanitizeAcquisitionPlan),
|
|
62
|
+
acquiredEvidenceCount: event.acquiredEvidenceIds.length,
|
|
63
|
+
acquiredEvidenceIds: options.includeEvidenceIds ? event.acquiredEvidenceIds : void 0
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
if (event.type === "control_step") {
|
|
67
|
+
return { ...base, step: sanitizeControlStep(event.step, options) };
|
|
68
|
+
}
|
|
69
|
+
if (event.type === "control_end") {
|
|
70
|
+
return { ...base, control: sanitizeControlRun(event.control, options) };
|
|
71
|
+
}
|
|
72
|
+
return { ...base, status: event.status, reason: event.reason };
|
|
73
|
+
}
|
|
74
|
+
function sanitizeRuntimeStreamEvent(event, options = {}) {
|
|
75
|
+
const withTask = "task" in event && event.task ? { task: sanitizeTask(event.task, options) } : {};
|
|
76
|
+
const withSession = "session" in event && event.session ? { session: sanitizeRuntimeSession(event.session, options) } : {};
|
|
77
|
+
if (event.type === "readiness_end") {
|
|
78
|
+
return {
|
|
79
|
+
type: event.type,
|
|
80
|
+
...withTask,
|
|
81
|
+
timestamp: event.timestamp,
|
|
82
|
+
decision: event.decision,
|
|
83
|
+
knowledge: sanitizeKnowledgeReadinessReport(event.knowledge, options)
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
if (event.type === "questions_start") {
|
|
87
|
+
return {
|
|
88
|
+
type: event.type,
|
|
89
|
+
...withTask,
|
|
90
|
+
timestamp: event.timestamp,
|
|
91
|
+
questions: event.questions.map((question) => sanitizeQuestion(question, options))
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
if (event.type === "questions_end") {
|
|
95
|
+
return {
|
|
96
|
+
type: event.type,
|
|
97
|
+
...withTask,
|
|
98
|
+
timestamp: event.timestamp,
|
|
99
|
+
questions: event.questions.map((question) => sanitizeQuestion(question, options)),
|
|
100
|
+
userAnswers: options.includeUserAnswers ? event.userAnswers : redactRecord(event.userAnswers)
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
if (event.type === "acquisition_start") {
|
|
104
|
+
return {
|
|
105
|
+
type: event.type,
|
|
106
|
+
...withTask,
|
|
107
|
+
timestamp: event.timestamp,
|
|
108
|
+
acquisitionPlans: event.acquisitionPlans.map(sanitizeAcquisitionPlan)
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (event.type === "acquisition_end") {
|
|
112
|
+
return {
|
|
113
|
+
type: event.type,
|
|
114
|
+
...withTask,
|
|
115
|
+
timestamp: event.timestamp,
|
|
116
|
+
acquisitionPlans: event.acquisitionPlans.map(sanitizeAcquisitionPlan),
|
|
117
|
+
acquiredEvidenceCount: event.acquiredEvidenceIds.length,
|
|
118
|
+
acquiredEvidenceIds: options.includeEvidenceIds ? event.acquiredEvidenceIds : void 0
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (event.type === "tool_call") {
|
|
122
|
+
return {
|
|
123
|
+
type: event.type,
|
|
124
|
+
...withTask,
|
|
125
|
+
...withSession,
|
|
126
|
+
timestamp: event.timestamp,
|
|
127
|
+
toolName: event.toolName,
|
|
128
|
+
toolCallId: event.toolCallId,
|
|
129
|
+
args: options.includeControlPayloads ? event.args : void 0
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
if (event.type === "tool_result") {
|
|
133
|
+
return {
|
|
134
|
+
type: event.type,
|
|
135
|
+
...withTask,
|
|
136
|
+
...withSession,
|
|
137
|
+
timestamp: event.timestamp,
|
|
138
|
+
toolName: event.toolName,
|
|
139
|
+
toolCallId: event.toolCallId,
|
|
140
|
+
result: options.includeControlPayloads ? event.result : void 0
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
if (event.type === "llm_call") {
|
|
144
|
+
return {
|
|
145
|
+
type: event.type,
|
|
146
|
+
...withTask,
|
|
147
|
+
...withSession,
|
|
148
|
+
timestamp: event.timestamp,
|
|
149
|
+
model: event.model,
|
|
150
|
+
tokensIn: event.tokensIn,
|
|
151
|
+
tokensOut: event.tokensOut,
|
|
152
|
+
costUsd: event.costUsd,
|
|
153
|
+
latencyMs: event.latencyMs,
|
|
154
|
+
finishReason: event.finishReason
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
if (event.type === "artifact") {
|
|
158
|
+
return {
|
|
159
|
+
type: event.type,
|
|
160
|
+
...withTask,
|
|
161
|
+
...withSession,
|
|
162
|
+
timestamp: event.timestamp,
|
|
163
|
+
artifactId: event.artifactId,
|
|
164
|
+
name: event.name,
|
|
165
|
+
mimeType: event.mimeType,
|
|
166
|
+
uri: options.includeEvidenceIds ? event.uri : void 0,
|
|
167
|
+
content: options.includeControlPayloads ? event.content : void 0,
|
|
168
|
+
metadata: options.includeMetadata ? event.metadata : void 0
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
if (event.type === "proposal_created") {
|
|
172
|
+
return {
|
|
173
|
+
type: event.type,
|
|
174
|
+
...withTask,
|
|
175
|
+
...withSession,
|
|
176
|
+
timestamp: event.timestamp,
|
|
177
|
+
proposalId: event.proposalId,
|
|
178
|
+
title: options.includeControlPayloads ? event.title : void 0,
|
|
179
|
+
content: options.includeControlPayloads ? event.content : void 0,
|
|
180
|
+
status: event.status
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
if (event.type === "final") {
|
|
184
|
+
const sanitizedError = event.error !== void 0 ? {
|
|
185
|
+
kind: event.error.kind,
|
|
186
|
+
message: event.error.message,
|
|
187
|
+
status: event.error.status,
|
|
188
|
+
body: options.includeControlPayloads ? event.error.body : void 0
|
|
189
|
+
} : void 0;
|
|
190
|
+
return {
|
|
191
|
+
type: event.type,
|
|
192
|
+
...withTask,
|
|
193
|
+
...withSession,
|
|
194
|
+
timestamp: event.timestamp,
|
|
195
|
+
status: event.status,
|
|
196
|
+
reason: event.reason,
|
|
197
|
+
text: options.includeControlPayloads ? event.text : void 0,
|
|
198
|
+
metadata: options.includeMetadata ? event.metadata : void 0,
|
|
199
|
+
...sanitizedError !== void 0 ? { error: sanitizedError } : {}
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
return {
|
|
203
|
+
type: event.type,
|
|
204
|
+
...withTask,
|
|
205
|
+
...withSession,
|
|
206
|
+
timestamp: "timestamp" in event ? event.timestamp : void 0,
|
|
207
|
+
...pickPublicStreamFields(event)
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function sanitizeTask(task, options) {
|
|
211
|
+
return {
|
|
212
|
+
id: task.id,
|
|
213
|
+
intent: task.intent,
|
|
214
|
+
domain: task.domain,
|
|
215
|
+
inputs: options.includeInputs ? task.inputs : task.inputs ? "[redacted]" : void 0,
|
|
216
|
+
requiredKnowledge: task.requiredKnowledge?.map(
|
|
217
|
+
(requirement) => sanitizeKnowledgeRequirement(requirement, options)
|
|
218
|
+
),
|
|
219
|
+
metadata: options.includeMetadata ? task.metadata : task.metadata ? "[redacted]" : void 0
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
function sanitizeRuntimeSession(session, options) {
|
|
223
|
+
return {
|
|
224
|
+
id: session.id,
|
|
225
|
+
backend: session.backend,
|
|
226
|
+
status: session.status,
|
|
227
|
+
hasResumeToken: Boolean(session.resumeToken),
|
|
228
|
+
createdAt: session.createdAt,
|
|
229
|
+
updatedAt: session.updatedAt,
|
|
230
|
+
metadata: options.includeMetadata ? session.metadata : session.metadata ? "[redacted]" : void 0
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
function sanitizeKnowledgeRequirement(requirement, options) {
|
|
234
|
+
const includeDescription = options.includeRequirementDescriptions && requirement.sensitivity !== "secret";
|
|
235
|
+
return {
|
|
236
|
+
id: requirement.id,
|
|
237
|
+
description: includeDescription ? requirement.description : void 0,
|
|
238
|
+
requiredFor: requirement.requiredFor,
|
|
239
|
+
category: requirement.category,
|
|
240
|
+
acquisitionMode: requirement.acquisitionMode,
|
|
241
|
+
importance: requirement.importance,
|
|
242
|
+
freshness: requirement.freshness,
|
|
243
|
+
sensitivity: requirement.sensitivity,
|
|
244
|
+
confidenceNeeded: requirement.confidenceNeeded,
|
|
245
|
+
currentConfidence: requirement.currentConfidence,
|
|
246
|
+
evidenceCount: requirement.evidenceIds.length,
|
|
247
|
+
evidenceIds: options.includeEvidenceIds ? requirement.evidenceIds : void 0,
|
|
248
|
+
fallbackPolicy: requirement.fallbackPolicy
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
function sanitizeQuestion(question, options) {
|
|
252
|
+
return {
|
|
253
|
+
id: question.id,
|
|
254
|
+
question: options.includeRequirementDescriptions && question.answerType !== "credential" ? question.question : void 0,
|
|
255
|
+
reason: options.includeRequirementDescriptions ? question.reason : void 0,
|
|
256
|
+
requirementId: question.requirementId,
|
|
257
|
+
importance: question.importance,
|
|
258
|
+
answerType: question.answerType,
|
|
259
|
+
impactIfUnknown: options.includeRequirementDescriptions ? question.impactIfUnknown : void 0,
|
|
260
|
+
optionCount: question.options?.length ?? 0
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
function sanitizeAcquisitionPlan(plan) {
|
|
264
|
+
return {
|
|
265
|
+
id: plan.id,
|
|
266
|
+
requirementIds: plan.requirementIds,
|
|
267
|
+
mode: plan.mode,
|
|
268
|
+
priority: plan.priority,
|
|
269
|
+
expectedEvidenceCount: plan.expectedEvidenceIds?.length ?? 0,
|
|
270
|
+
questionCount: plan.questions?.length ?? 0
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function sanitizeControlStep(step, options) {
|
|
274
|
+
const actionOutcome = step.actionOutcome;
|
|
275
|
+
return {
|
|
276
|
+
index: step.index,
|
|
277
|
+
decisionType: step.decision.type,
|
|
278
|
+
reason: step.decision.reason,
|
|
279
|
+
action: options.includeControlPayloads && step.decision.type === "continue" ? step.decision.action : void 0,
|
|
280
|
+
result: options.includeControlPayloads && actionOutcome?.ok ? actionOutcome.result : void 0,
|
|
281
|
+
actionOk: actionOutcome?.ok,
|
|
282
|
+
actionError: actionOutcome?.ok === false ? actionOutcome.error : void 0,
|
|
283
|
+
durationMs: actionOutcome?.durationMs,
|
|
284
|
+
evalsBefore: summarizeEvals(step.evalsBefore, options),
|
|
285
|
+
evalsAfter: summarizeEvals(step.evalsAfter, options),
|
|
286
|
+
startedAt: step.startedAt,
|
|
287
|
+
endedAt: step.endedAt
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
function sanitizeControlRun(control, options) {
|
|
291
|
+
return {
|
|
292
|
+
pass: control.pass,
|
|
293
|
+
completed: control.completed,
|
|
294
|
+
reason: control.reason,
|
|
295
|
+
score: control.score,
|
|
296
|
+
stepCount: control.steps.length,
|
|
297
|
+
wallMs: control.wallMs,
|
|
298
|
+
spentCostUsd: control.spentCostUsd,
|
|
299
|
+
failureClass: control.failureClass,
|
|
300
|
+
stoppedBy: control.stoppedBy,
|
|
301
|
+
runId: control.runId,
|
|
302
|
+
runtimeErrorCount: control.runtimeErrors.length,
|
|
303
|
+
finalEvals: summarizeEvals(control.finalEvals, options)
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
function summarizeEvals(evals, options) {
|
|
307
|
+
return evals.map((evalResult) => ({
|
|
308
|
+
id: evalResult.id,
|
|
309
|
+
passed: evalResult.passed,
|
|
310
|
+
score: evalResult.score,
|
|
311
|
+
severity: evalResult.severity,
|
|
312
|
+
objective: evalResult.objective,
|
|
313
|
+
detail: options.includeEvalDetails ? evalResult.detail : void 0,
|
|
314
|
+
evidence: options.includeEvalDetails ? evalResult.evidence : void 0
|
|
315
|
+
}));
|
|
316
|
+
}
|
|
317
|
+
function redactRecord(record) {
|
|
318
|
+
return Object.fromEntries(Object.keys(record).map((key) => [key, "[redacted]"]));
|
|
319
|
+
}
|
|
320
|
+
function pickPublicStreamFields(event) {
|
|
321
|
+
if (event.type === "session_created" || event.type === "session_resumed") return {};
|
|
322
|
+
if (event.type === "backend_start" || event.type === "backend_end")
|
|
323
|
+
return { backend: event.backend };
|
|
324
|
+
if (event.type === "backend_error") {
|
|
325
|
+
const sanitizedError = event.error !== void 0 ? {
|
|
326
|
+
kind: event.error.kind,
|
|
327
|
+
status: event.error.status
|
|
328
|
+
} : void 0;
|
|
329
|
+
return {
|
|
330
|
+
backend: event.backend,
|
|
331
|
+
message: event.message,
|
|
332
|
+
recoverable: event.recoverable,
|
|
333
|
+
...sanitizedError !== void 0 ? { error: sanitizedError } : {}
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
if (event.type === "task_end") return { status: event.status, reason: event.reason };
|
|
337
|
+
if (event.type === "text_delta" || event.type === "reasoning_delta") return { text: event.text };
|
|
338
|
+
return {};
|
|
339
|
+
}
|
|
340
|
+
function createRuntimeEventCollector(options = {}) {
|
|
341
|
+
const events = [];
|
|
342
|
+
return {
|
|
343
|
+
events,
|
|
344
|
+
onEvent: (event) => {
|
|
345
|
+
events.push(sanitizeAgentRuntimeEvent(event, options));
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
function createRuntimeStreamEventCollector(options = {}) {
|
|
350
|
+
const events = [];
|
|
351
|
+
const eventCountsByType = {};
|
|
352
|
+
let firstSessionId;
|
|
353
|
+
let finalStatus;
|
|
354
|
+
let finalReason;
|
|
355
|
+
let finalText = "";
|
|
356
|
+
return {
|
|
357
|
+
events,
|
|
358
|
+
onEvent: (event) => {
|
|
359
|
+
events.push(sanitizeRuntimeStreamEvent(event, options));
|
|
360
|
+
eventCountsByType[event.type] = (eventCountsByType[event.type] ?? 0) + 1;
|
|
361
|
+
if (event.type === "text_delta") finalText += event.text;
|
|
362
|
+
if (!firstSessionId && (event.type === "session_created" || event.type === "session_resumed")) {
|
|
363
|
+
firstSessionId = event.session.id;
|
|
364
|
+
}
|
|
365
|
+
if (event.type === "final") {
|
|
366
|
+
finalStatus = event.status;
|
|
367
|
+
finalReason = event.reason;
|
|
368
|
+
}
|
|
369
|
+
},
|
|
370
|
+
summary() {
|
|
371
|
+
return {
|
|
372
|
+
eventCount: events.length,
|
|
373
|
+
eventCountsByType: { ...eventCountsByType },
|
|
374
|
+
firstSessionId,
|
|
375
|
+
finalStatus,
|
|
376
|
+
finalReason,
|
|
377
|
+
finalText
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// src/otel-export.ts
|
|
384
|
+
var SCOPE = { name: "@tangle-network/agent-runtime", version: "0.83.0" };
|
|
385
|
+
var GEN_AI = {
|
|
386
|
+
operation: "gen_ai.operation.name",
|
|
387
|
+
agentName: "gen_ai.agent.name",
|
|
388
|
+
conversationId: "gen_ai.conversation.id",
|
|
389
|
+
inputTokens: "gen_ai.usage.input_tokens",
|
|
390
|
+
outputTokens: "gen_ai.usage.output_tokens"
|
|
391
|
+
};
|
|
392
|
+
function createOtelExporter(config) {
|
|
393
|
+
const resolvedEndpoint = config?.endpoint ?? (typeof process !== "undefined" ? process.env.OTEL_EXPORTER_OTLP_ENDPOINT : void 0);
|
|
394
|
+
if (!resolvedEndpoint) return void 0;
|
|
395
|
+
const endpoint = resolvedEndpoint;
|
|
396
|
+
const headers = config?.headers ?? parseHeadersFromEnv();
|
|
397
|
+
const batchSize = config?.batchSize ?? 64;
|
|
398
|
+
const flushIntervalMs = config?.flushIntervalMs ?? 5e3;
|
|
399
|
+
const serviceName = config?.serviceName ?? "agent-runtime";
|
|
400
|
+
const resourceAttrs = config?.resourceAttributes ?? {};
|
|
401
|
+
const pending = [];
|
|
402
|
+
let timer;
|
|
403
|
+
let stopped = false;
|
|
404
|
+
const exporter = {
|
|
405
|
+
exportSpan(span) {
|
|
406
|
+
if (stopped) return;
|
|
407
|
+
pending.push(span);
|
|
408
|
+
if (pending.length >= batchSize) {
|
|
409
|
+
void doFlush();
|
|
410
|
+
}
|
|
411
|
+
},
|
|
412
|
+
async flush() {
|
|
413
|
+
await doFlush();
|
|
414
|
+
},
|
|
415
|
+
async shutdown() {
|
|
416
|
+
stopped = true;
|
|
417
|
+
if (timer !== void 0) {
|
|
418
|
+
clearInterval(timer);
|
|
419
|
+
timer = void 0;
|
|
420
|
+
}
|
|
421
|
+
await doFlush();
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
timer = setInterval(() => {
|
|
425
|
+
if (pending.length > 0) void doFlush();
|
|
426
|
+
}, flushIntervalMs);
|
|
427
|
+
if (typeof timer === "object" && "unref" in timer) {
|
|
428
|
+
;
|
|
429
|
+
timer.unref();
|
|
430
|
+
}
|
|
431
|
+
async function doFlush() {
|
|
432
|
+
if (pending.length === 0) return;
|
|
433
|
+
const batch = pending.splice(0);
|
|
434
|
+
const body = {
|
|
435
|
+
resourceSpans: [
|
|
436
|
+
{
|
|
437
|
+
resource: {
|
|
438
|
+
attributes: toAttributes({
|
|
439
|
+
"service.name": serviceName,
|
|
440
|
+
...resourceAttrs
|
|
441
|
+
})
|
|
442
|
+
},
|
|
443
|
+
scopeSpans: [{ scope: SCOPE, spans: batch }]
|
|
444
|
+
}
|
|
445
|
+
]
|
|
446
|
+
};
|
|
447
|
+
const url = `${endpoint.replace(/\/+$/, "")}/v1/traces`;
|
|
448
|
+
try {
|
|
449
|
+
await fetch(url, {
|
|
450
|
+
method: "POST",
|
|
451
|
+
headers: { "content-type": "application/json", ...headers },
|
|
452
|
+
body: JSON.stringify(body)
|
|
453
|
+
});
|
|
454
|
+
} catch {
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return exporter;
|
|
458
|
+
}
|
|
459
|
+
function loopEventToOtelSpan(event, traceId, parentSpanId) {
|
|
460
|
+
const spanId = generateSpanId();
|
|
461
|
+
const attrs = {
|
|
462
|
+
"loop.event_kind": event.kind,
|
|
463
|
+
"loop.run_id": event.runId
|
|
464
|
+
};
|
|
465
|
+
for (const [k, v] of Object.entries(event.payload)) {
|
|
466
|
+
if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") {
|
|
467
|
+
attrs[`loop.${k}`] = v;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
const ts = msToNs(event.timestamp);
|
|
471
|
+
return {
|
|
472
|
+
traceId: padTraceId(traceId),
|
|
473
|
+
spanId,
|
|
474
|
+
parentSpanId: parentSpanId ? padSpanId(parentSpanId) : void 0,
|
|
475
|
+
name: event.kind,
|
|
476
|
+
kind: 1,
|
|
477
|
+
startTimeUnixNano: ts,
|
|
478
|
+
endTimeUnixNano: ts,
|
|
479
|
+
attributes: toAttributes(attrs),
|
|
480
|
+
status: { code: 1 }
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
function flatOtelSpan(name, attributes, traceId, timestampMs, parentSpanId, endTimestampMs = timestampMs) {
|
|
484
|
+
const start = msToNs(timestampMs);
|
|
485
|
+
const end = msToNs(Math.max(timestampMs, endTimestampMs));
|
|
486
|
+
return {
|
|
487
|
+
traceId: padTraceId(traceId),
|
|
488
|
+
spanId: generateSpanId(),
|
|
489
|
+
parentSpanId: parentSpanId ? padSpanId(parentSpanId) : void 0,
|
|
490
|
+
name,
|
|
491
|
+
kind: 1,
|
|
492
|
+
startTimeUnixNano: start,
|
|
493
|
+
endTimeUnixNano: end,
|
|
494
|
+
attributes: toAttributes(attributes),
|
|
495
|
+
status: { code: 1 }
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
function eventTimestampMs(event) {
|
|
499
|
+
if ("timestamp" in event && typeof event.timestamp === "string") {
|
|
500
|
+
const parsed = Date.parse(event.timestamp);
|
|
501
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
502
|
+
}
|
|
503
|
+
return Date.now();
|
|
504
|
+
}
|
|
505
|
+
function serialized(value) {
|
|
506
|
+
try {
|
|
507
|
+
return JSON.stringify(value) ?? String(value);
|
|
508
|
+
} catch {
|
|
509
|
+
return String(value);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
function mcpIdentity(toolName) {
|
|
513
|
+
if (!toolName.startsWith("mcp__")) return {};
|
|
514
|
+
const [, server, ...toolParts] = toolName.split("__");
|
|
515
|
+
return {
|
|
516
|
+
...server ? { server } : {},
|
|
517
|
+
...toolParts.length > 0 ? { tool: toolParts.join("__") } : {}
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
function buildRuntimeEventOtelSpans(events, traceId, parentSpanId, options = {}) {
|
|
521
|
+
return events.map((event) => {
|
|
522
|
+
const sanitized = sanitizeRuntimeStreamEvent(event, options);
|
|
523
|
+
const safe = options.redact ? options.redact(sanitized) : sanitized;
|
|
524
|
+
const record = safe && typeof safe === "object" && !Array.isArray(safe) ? safe : { value: safe };
|
|
525
|
+
const attrs = {
|
|
526
|
+
"tangle.runtime.event_type": event.type,
|
|
527
|
+
"tangle.runtime.event": serialized(record)
|
|
528
|
+
};
|
|
529
|
+
let name = `tangle.runtime.${event.type}`;
|
|
530
|
+
if (event.type === "tool_call" || event.type === "tool_result") {
|
|
531
|
+
name = `agent.${event.type}`;
|
|
532
|
+
attrs["tool.name"] = event.toolName;
|
|
533
|
+
if (event.toolCallId) attrs["tool.call_id"] = event.toolCallId;
|
|
534
|
+
const mcp = mcpIdentity(event.toolName);
|
|
535
|
+
if (mcp.server) attrs["mcp.server"] = mcp.server;
|
|
536
|
+
if (mcp.tool) attrs["mcp.tool.name"] = mcp.tool;
|
|
537
|
+
const payload = event.type === "tool_call" ? record.args : record.result;
|
|
538
|
+
if (payload !== void 0) {
|
|
539
|
+
attrs[event.type === "tool_call" ? "tool.input" : "tool.output"] = serialized(payload);
|
|
540
|
+
}
|
|
541
|
+
} else if (event.type === "llm_call") {
|
|
542
|
+
name = "gen_ai.client.inference";
|
|
543
|
+
attrs["gen_ai.request.model"] = event.model;
|
|
544
|
+
if (event.tokensIn !== void 0) attrs["gen_ai.usage.input_tokens"] = event.tokensIn;
|
|
545
|
+
if (event.tokensOut !== void 0) attrs["gen_ai.usage.output_tokens"] = event.tokensOut;
|
|
546
|
+
if (event.costUsd !== void 0) attrs["tangle.cost.usd"] = event.costUsd;
|
|
547
|
+
if (event.latencyMs !== void 0) attrs["tangle.latency_ms"] = event.latencyMs;
|
|
548
|
+
if (event.finishReason !== void 0)
|
|
549
|
+
attrs["gen_ai.response.finish_reasons"] = event.finishReason;
|
|
550
|
+
} else if (event.type === "backend_error") {
|
|
551
|
+
attrs["error.type"] = event.error?.kind ?? "backend";
|
|
552
|
+
attrs["error.message"] = event.message;
|
|
553
|
+
} else if (event.type === "final") {
|
|
554
|
+
attrs["tangle.outcome.status"] = event.status;
|
|
555
|
+
attrs["tangle.outcome.reason"] = event.reason;
|
|
556
|
+
if (event.error) {
|
|
557
|
+
attrs["error.type"] = event.error.kind;
|
|
558
|
+
attrs["error.message"] = event.error.message;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
const startMs = eventTimestampMs(event);
|
|
562
|
+
const endMs = event.type === "llm_call" && event.latencyMs !== void 0 && Number.isFinite(event.latencyMs) ? startMs + event.latencyMs : startMs;
|
|
563
|
+
const span = flatOtelSpan(name, attrs, traceId, startMs, parentSpanId, endMs);
|
|
564
|
+
if (event.type === "backend_error" || event.type === "final" && event.status !== "completed") {
|
|
565
|
+
span.status = { code: 2, message: attrs["error.message"]?.toString() ?? event.type };
|
|
566
|
+
}
|
|
567
|
+
return span;
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
function buildLoopOtelSpans(events, traceId, rootParentSpanId) {
|
|
571
|
+
const tid = padTraceId(traceId);
|
|
572
|
+
return buildLoopSpanNodes(events).map((node) => ({
|
|
573
|
+
traceId: tid,
|
|
574
|
+
spanId: node.spanId,
|
|
575
|
+
parentSpanId: node.parentSpanId ? padSpanId(node.parentSpanId) : rootParentSpanId ? padSpanId(rootParentSpanId) : void 0,
|
|
576
|
+
name: node.name,
|
|
577
|
+
kind: 1,
|
|
578
|
+
startTimeUnixNano: msToNs(node.startMs),
|
|
579
|
+
endTimeUnixNano: msToNs(node.endMs),
|
|
580
|
+
attributes: toAttributes(node.attrs),
|
|
581
|
+
status: { code: node.error ? 2 : 1 }
|
|
582
|
+
}));
|
|
583
|
+
}
|
|
584
|
+
function buildLoopSpanNodes(events) {
|
|
585
|
+
if (events.length === 0) return [];
|
|
586
|
+
const out = [];
|
|
587
|
+
const num = (v) => typeof v === "number" && Number.isFinite(v) ? v : void 0;
|
|
588
|
+
const str = (v) => typeof v === "string" && v.length > 0 ? v : void 0;
|
|
589
|
+
const rec = (v) => v && typeof v === "object" ? v : {};
|
|
590
|
+
const started = events.find((e) => e.kind === "loop.started");
|
|
591
|
+
const ended = events.find((e) => e.kind === "loop.ended");
|
|
592
|
+
const runId = events[0]?.runId ?? "";
|
|
593
|
+
const rootStart = started?.timestamp ?? events[0].timestamp;
|
|
594
|
+
const rootEnd = ended?.timestamp ?? events[events.length - 1].timestamp;
|
|
595
|
+
const rootId = generateSpanId();
|
|
596
|
+
const make = (spanId, parentSpanId, name, kind, startMs, endMs, attrs, error = false) => ({
|
|
597
|
+
spanId,
|
|
598
|
+
parentSpanId,
|
|
599
|
+
name,
|
|
600
|
+
kind,
|
|
601
|
+
startMs,
|
|
602
|
+
endMs,
|
|
603
|
+
attrs,
|
|
604
|
+
error
|
|
605
|
+
});
|
|
606
|
+
const sp = rec(started?.payload);
|
|
607
|
+
const rootAttrs = {
|
|
608
|
+
[GEN_AI.operation]: "invoke_workflow",
|
|
609
|
+
[GEN_AI.conversationId]: runId,
|
|
610
|
+
// Explicit run identity under the tangle namespace so a consuming system of
|
|
611
|
+
// record (Tangle Intelligence's run spine) maps this span tree to one run
|
|
612
|
+
// deterministically, instead of inferring it from the conversation id.
|
|
613
|
+
"tangle.run.id": runId,
|
|
614
|
+
"tangle.loop.driver": str(sp.driver) ?? "driver"
|
|
615
|
+
};
|
|
616
|
+
if (Array.isArray(sp.agentRunNames) && sp.agentRunNames.length > 0) {
|
|
617
|
+
rootAttrs["tangle.loop.agents"] = sp.agentRunNames.map(String).join(",");
|
|
618
|
+
rootAttrs["tangle.subject.key"] = String(sp.agentRunNames[0]);
|
|
619
|
+
}
|
|
620
|
+
if (ended) {
|
|
621
|
+
const ep = rec(ended.payload);
|
|
622
|
+
const win = num(ep.winnerIterationIndex);
|
|
623
|
+
if (win !== void 0) rootAttrs["tangle.loop.winner.iteration_index"] = win;
|
|
624
|
+
const cost = num(ep.totalCostUsd);
|
|
625
|
+
if (cost !== void 0) rootAttrs["tangle.cost.usd"] = cost;
|
|
626
|
+
const dur = num(ep.durationMs);
|
|
627
|
+
if (dur !== void 0) rootAttrs["tangle.loop.duration_ms"] = dur;
|
|
628
|
+
const iters = num(ep.iterations);
|
|
629
|
+
if (iters !== void 0) rootAttrs["tangle.loop.iterations"] = iters;
|
|
630
|
+
}
|
|
631
|
+
out.push(make(rootId, void 0, "loop", "loop", rootStart, rootEnd, rootAttrs));
|
|
632
|
+
const iterStartTs = /* @__PURE__ */ new Map();
|
|
633
|
+
const placementByIdx = /* @__PURE__ */ new Map();
|
|
634
|
+
let currentRoundId;
|
|
635
|
+
let pendingRound;
|
|
636
|
+
const flushRound = (endMs) => {
|
|
637
|
+
if (!pendingRound) return;
|
|
638
|
+
out.push(
|
|
639
|
+
make(
|
|
640
|
+
pendingRound.id,
|
|
641
|
+
rootId,
|
|
642
|
+
"loop.round",
|
|
643
|
+
"round",
|
|
644
|
+
pendingRound.start,
|
|
645
|
+
endMs,
|
|
646
|
+
pendingRound.attrs
|
|
647
|
+
)
|
|
648
|
+
);
|
|
649
|
+
pendingRound = void 0;
|
|
650
|
+
};
|
|
651
|
+
for (const e of events) {
|
|
652
|
+
const p = rec(e.payload);
|
|
653
|
+
switch (e.kind) {
|
|
654
|
+
case "loop.plan": {
|
|
655
|
+
flushRound(e.timestamp);
|
|
656
|
+
const id = generateSpanId();
|
|
657
|
+
const roundIdx = num(p.roundIndex) ?? 0;
|
|
658
|
+
const attrs = {
|
|
659
|
+
[GEN_AI.operation]: "invoke_workflow",
|
|
660
|
+
"tangle.loop.round.index": roundIdx,
|
|
661
|
+
"tangle.loop.move.kind": str(p.moveKind) ?? "unknown",
|
|
662
|
+
"tangle.loop.move.round": roundIdx,
|
|
663
|
+
"tangle.loop.move.width": num(p.plannedCount) ?? 0
|
|
664
|
+
};
|
|
665
|
+
const r = str(p.rationale);
|
|
666
|
+
if (r) attrs["tangle.loop.move.rationale"] = r;
|
|
667
|
+
const parent = num(p.parentIndex);
|
|
668
|
+
if (parent !== void 0) attrs["tangle.loop.move.parent_index"] = parent;
|
|
669
|
+
if (Array.isArray(p.childIndices) && p.childIndices.length > 0) {
|
|
670
|
+
attrs["tangle.loop.move.child_indices"] = p.childIndices.map(String).join(",");
|
|
671
|
+
}
|
|
672
|
+
pendingRound = { id, start: e.timestamp, attrs };
|
|
673
|
+
currentRoundId = id;
|
|
674
|
+
break;
|
|
675
|
+
}
|
|
676
|
+
case "loop.iteration.started": {
|
|
677
|
+
const idx = num(p.iterationIndex);
|
|
678
|
+
if (idx !== void 0) iterStartTs.set(idx, e.timestamp);
|
|
679
|
+
break;
|
|
680
|
+
}
|
|
681
|
+
case "loop.iteration.dispatch": {
|
|
682
|
+
const idx = num(p.iterationIndex);
|
|
683
|
+
if (idx === void 0) break;
|
|
684
|
+
const place = {};
|
|
685
|
+
const kind = str(p.placement);
|
|
686
|
+
if (kind) place["tangle.loop.placement.kind"] = kind;
|
|
687
|
+
const sid = str(p.sandboxId);
|
|
688
|
+
if (sid) place["tangle.sandbox.id"] = sid;
|
|
689
|
+
const fid = str(p.fleetId);
|
|
690
|
+
if (fid) place["tangle.fleet.id"] = fid;
|
|
691
|
+
const mid = str(p.machineId);
|
|
692
|
+
if (mid) place["tangle.machine.id"] = mid;
|
|
693
|
+
placementByIdx.set(idx, place);
|
|
694
|
+
break;
|
|
695
|
+
}
|
|
696
|
+
case "loop.iteration.ended": {
|
|
697
|
+
const idx = num(p.iterationIndex) ?? 0;
|
|
698
|
+
const start = iterStartTs.get(idx) ?? e.timestamp;
|
|
699
|
+
const err = str(p.error);
|
|
700
|
+
const attrs = {
|
|
701
|
+
[GEN_AI.operation]: "invoke_agent",
|
|
702
|
+
"tangle.loop.iteration.index": idx
|
|
703
|
+
};
|
|
704
|
+
const agent = str(p.agentRunName);
|
|
705
|
+
if (agent) attrs[GEN_AI.agentName] = agent;
|
|
706
|
+
const tu = rec(p.tokenUsage);
|
|
707
|
+
const inTok = num(tu.input);
|
|
708
|
+
if (inTok !== void 0) attrs[GEN_AI.inputTokens] = inTok;
|
|
709
|
+
const outTok = num(tu.output);
|
|
710
|
+
if (outTok !== void 0) attrs[GEN_AI.outputTokens] = outTok;
|
|
711
|
+
const cost = num(p.costUsd);
|
|
712
|
+
if (cost !== void 0) attrs["tangle.cost.usd"] = cost;
|
|
713
|
+
const verdict = rec(p.verdict);
|
|
714
|
+
if (typeof verdict.valid === "boolean") attrs["tangle.loop.verdict.valid"] = verdict.valid;
|
|
715
|
+
const score = num(verdict.score);
|
|
716
|
+
if (score !== void 0) attrs["tangle.loop.verdict.score"] = score;
|
|
717
|
+
if (err) attrs["tangle.loop.error"] = err;
|
|
718
|
+
const gid = num(p.groupId);
|
|
719
|
+
if (gid !== void 0) attrs["tangle.loop.iteration.group_id"] = gid;
|
|
720
|
+
const par = num(p.parentIndex);
|
|
721
|
+
if (par !== void 0) attrs["tangle.loop.iteration.parent_index"] = par;
|
|
722
|
+
const dur = num(p.durationMs);
|
|
723
|
+
if (dur !== void 0) attrs["tangle.loop.iteration.duration_ms"] = dur;
|
|
724
|
+
const preview = str(p.outputPreview);
|
|
725
|
+
if (preview) attrs["tangle.loop.iteration.output_preview"] = preview;
|
|
726
|
+
Object.assign(attrs, placementByIdx.get(idx) ?? {});
|
|
727
|
+
out.push(
|
|
728
|
+
make(
|
|
729
|
+
generateSpanId(),
|
|
730
|
+
currentRoundId ?? rootId,
|
|
731
|
+
"loop.iteration",
|
|
732
|
+
"branch",
|
|
733
|
+
start,
|
|
734
|
+
e.timestamp,
|
|
735
|
+
attrs,
|
|
736
|
+
err !== void 0
|
|
737
|
+
)
|
|
738
|
+
);
|
|
739
|
+
break;
|
|
740
|
+
}
|
|
741
|
+
case "loop.decision": {
|
|
742
|
+
if (pendingRound) {
|
|
743
|
+
const dec = str(p.decision);
|
|
744
|
+
if (dec) pendingRound.attrs["tangle.loop.decision"] = dec;
|
|
745
|
+
flushRound(e.timestamp);
|
|
746
|
+
}
|
|
747
|
+
currentRoundId = void 0;
|
|
748
|
+
break;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
flushRound(rootEnd);
|
|
753
|
+
return out;
|
|
754
|
+
}
|
|
755
|
+
function parseHeadersFromEnv() {
|
|
756
|
+
if (typeof process === "undefined") return {};
|
|
757
|
+
const raw = process.env.OTEL_EXPORTER_OTLP_HEADERS;
|
|
758
|
+
if (!raw) return {};
|
|
759
|
+
const out = {};
|
|
760
|
+
for (const pair of raw.split(",")) {
|
|
761
|
+
const eq = pair.indexOf("=");
|
|
762
|
+
if (eq < 0) continue;
|
|
763
|
+
const key = pair.slice(0, eq).trim();
|
|
764
|
+
const value = pair.slice(eq + 1).trim();
|
|
765
|
+
if (key) out[key] = value;
|
|
766
|
+
}
|
|
767
|
+
return out;
|
|
768
|
+
}
|
|
769
|
+
function toAttributes(record) {
|
|
770
|
+
return Object.entries(record).flatMap(([key, value]) => {
|
|
771
|
+
if (typeof value === "number" && !Number.isFinite(value)) return [];
|
|
772
|
+
return [
|
|
773
|
+
{
|
|
774
|
+
key,
|
|
775
|
+
value: typeof value === "number" ? Number.isInteger(value) ? { intValue: value.toString() } : { doubleValue: value } : typeof value === "boolean" ? { boolValue: value } : { stringValue: value }
|
|
776
|
+
}
|
|
777
|
+
];
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
function msToNs(ms) {
|
|
781
|
+
const safeMs = Number.isFinite(ms) ? ms : Date.now();
|
|
782
|
+
return (BigInt(Math.floor(safeMs)) * 1000000n).toString();
|
|
783
|
+
}
|
|
784
|
+
function padSpanId(id) {
|
|
785
|
+
const cleaned = id.replace(/-/g, "");
|
|
786
|
+
return cleaned.slice(0, 16).padEnd(16, "0");
|
|
787
|
+
}
|
|
788
|
+
function padTraceId(id) {
|
|
789
|
+
const cleaned = id.replace(/-/g, "");
|
|
790
|
+
return cleaned.slice(0, 32).padEnd(32, "0");
|
|
791
|
+
}
|
|
792
|
+
function generateSpanId() {
|
|
793
|
+
const bytes = new Uint8Array(8);
|
|
794
|
+
if (typeof globalThis.crypto?.getRandomValues === "function") {
|
|
795
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
796
|
+
} else {
|
|
797
|
+
for (let i = 0; i < 8; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
798
|
+
}
|
|
799
|
+
return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
800
|
+
}
|
|
801
|
+
var INTELLIGENCE_WIRE_VERSION = "2026-05-26.v1";
|
|
802
|
+
var DEFAULT_INTELLIGENCE_BASE = "https://intelligence.tangle.tools";
|
|
803
|
+
async function exportEvalRuns(events, config) {
|
|
804
|
+
if (events.length === 0) return { ok: true, status: 0, accepted: 0, rejected: [] };
|
|
805
|
+
const apiKey = config?.apiKey ?? (typeof process !== "undefined" ? process.env.TANGLE_API_KEY : void 0);
|
|
806
|
+
if (!apiKey)
|
|
807
|
+
throw new Error("exportEvalRuns: apiKey required (pass config.apiKey or set TANGLE_API_KEY)");
|
|
808
|
+
const base = config?.base ?? (typeof process !== "undefined" ? process.env.TANGLE_INTELLIGENCE_URL : void 0) ?? DEFAULT_INTELLIGENCE_BASE;
|
|
809
|
+
const url = `${base.replace(/\/+$/, "")}/v1/ingest/eval-runs`;
|
|
810
|
+
const res = await fetch(url, {
|
|
811
|
+
method: "POST",
|
|
812
|
+
headers: {
|
|
813
|
+
"content-type": "application/json",
|
|
814
|
+
authorization: `Bearer ${apiKey}`,
|
|
815
|
+
"X-Tangle-Wire-Version": INTELLIGENCE_WIRE_VERSION,
|
|
816
|
+
...config?.idempotencyKey ? { "Idempotency-Key": config.idempotencyKey } : {}
|
|
817
|
+
},
|
|
818
|
+
body: JSON.stringify({ wireVersion: INTELLIGENCE_WIRE_VERSION, events })
|
|
819
|
+
});
|
|
820
|
+
let parsed = {};
|
|
821
|
+
try {
|
|
822
|
+
parsed = await res.json();
|
|
823
|
+
} catch {
|
|
824
|
+
}
|
|
825
|
+
return {
|
|
826
|
+
ok: res.ok,
|
|
827
|
+
status: res.status,
|
|
828
|
+
accepted: parsed.accepted ?? (res.ok ? events.length : 0),
|
|
829
|
+
rejected: parsed.rejected ?? []
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
export {
|
|
834
|
+
assertModelAllowed,
|
|
835
|
+
sanitizeKnowledgeReadinessReport,
|
|
836
|
+
sanitizeAgentRuntimeEvent,
|
|
837
|
+
sanitizeRuntimeStreamEvent,
|
|
838
|
+
createRuntimeEventCollector,
|
|
839
|
+
createRuntimeStreamEventCollector,
|
|
840
|
+
createOtelExporter,
|
|
841
|
+
loopEventToOtelSpan,
|
|
842
|
+
flatOtelSpan,
|
|
843
|
+
buildRuntimeEventOtelSpans,
|
|
844
|
+
buildLoopOtelSpans,
|
|
845
|
+
buildLoopSpanNodes,
|
|
846
|
+
INTELLIGENCE_WIRE_VERSION,
|
|
847
|
+
exportEvalRuns
|
|
848
|
+
};
|
|
849
|
+
//# sourceMappingURL=chunk-ISTDY47H.js.map
|