@uselemma/opencode 0.1.2 → 0.1.3
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/package.json +2 -2
- package/runtime/lemma.mjs +1331 -215
- package/scripts/setup.mjs +71 -2
package/runtime/lemma.mjs
CHANGED
|
@@ -44,10 +44,8 @@ var require_debug_delivery = __commonJS({
|
|
|
44
44
|
exports.isValidProjectId = isValidProjectId;
|
|
45
45
|
exports.apiKeySuffix = apiKeySuffix;
|
|
46
46
|
exports.buildConfigWarnings = buildConfigWarnings;
|
|
47
|
-
exports.ingestFailureHint = ingestFailureHint;
|
|
48
47
|
exports.pickResponseHeaders = pickResponseHeaders;
|
|
49
48
|
exports.pickResponseHeadersFromRecord = pickResponseHeadersFromRecord;
|
|
50
|
-
exports.sleep = sleep;
|
|
51
49
|
exports.PRODUCTION_BASE_URL = "https://api.uselemma.ai";
|
|
52
50
|
exports.INGEST_PATH = "/traces/ingest";
|
|
53
51
|
exports.INGEST_STATUS_PATH = "/traces/ingest-status";
|
|
@@ -87,20 +85,6 @@ var require_debug_delivery = __commonJS({
|
|
|
87
85
|
}
|
|
88
86
|
return warnings;
|
|
89
87
|
}
|
|
90
|
-
function ingestFailureHint(status) {
|
|
91
|
-
switch (status) {
|
|
92
|
-
case 401:
|
|
93
|
-
return "check LEMMA_API_KEY";
|
|
94
|
-
case 403:
|
|
95
|
-
return "API key doesn't own this project_id";
|
|
96
|
-
case 429:
|
|
97
|
-
return "ingest rate limit exceeded; retry with backoff";
|
|
98
|
-
case 404:
|
|
99
|
-
return "baseUrl likely wrong (not Lemma API)";
|
|
100
|
-
default:
|
|
101
|
-
return void 0;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
88
|
function pickResponseHeaders(headers) {
|
|
105
89
|
const picked = {};
|
|
106
90
|
for (const name of ["cf-ray", "server", "date"]) {
|
|
@@ -120,9 +104,6 @@ var require_debug_delivery = __commonJS({
|
|
|
120
104
|
}
|
|
121
105
|
return picked;
|
|
122
106
|
}
|
|
123
|
-
function sleep(ms) {
|
|
124
|
-
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
125
|
-
}
|
|
126
107
|
}
|
|
127
108
|
});
|
|
128
109
|
|
|
@@ -246,6 +227,181 @@ var require_error_message = __commonJS({
|
|
|
246
227
|
}
|
|
247
228
|
});
|
|
248
229
|
|
|
230
|
+
// ../../packages/ts/tracing/dist/delivery.js
|
|
231
|
+
var require_delivery = __commonJS({
|
|
232
|
+
"../../packages/ts/tracing/dist/delivery.js"(exports) {
|
|
233
|
+
"use strict";
|
|
234
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
235
|
+
exports.INGEST_RETRY_MAX_DELAY_MS = exports.INGEST_RETRY_BASE_DELAY_MS = exports.INGEST_REQUEST_TIMEOUT_MS = exports.INGEST_MAX_ATTEMPTS = exports.INGEST_STRICT_ATTEMPTS = void 0;
|
|
236
|
+
exports.ingestFailureHint = ingestFailureHint;
|
|
237
|
+
exports.isRetryableIngestStatus = isRetryableIngestStatus;
|
|
238
|
+
exports.parseRetryAfterMs = parseRetryAfterMs;
|
|
239
|
+
exports.ingestRetryDelayMs = ingestRetryDelayMs;
|
|
240
|
+
exports.sleep = sleep;
|
|
241
|
+
exports.withIngestTimeout = withIngestTimeout;
|
|
242
|
+
exports.failedBeforeSending = failedBeforeSending;
|
|
243
|
+
exports.deliver = deliver;
|
|
244
|
+
var debug_delivery_1 = require_debug_delivery();
|
|
245
|
+
var debug_mode_1 = require_debug_mode();
|
|
246
|
+
var error_message_1 = require_error_message();
|
|
247
|
+
exports.INGEST_STRICT_ATTEMPTS = 1;
|
|
248
|
+
exports.INGEST_MAX_ATTEMPTS = 3;
|
|
249
|
+
exports.INGEST_REQUEST_TIMEOUT_MS = 1e4;
|
|
250
|
+
exports.INGEST_RETRY_BASE_DELAY_MS = 500;
|
|
251
|
+
exports.INGEST_RETRY_MAX_DELAY_MS = 2e3;
|
|
252
|
+
function ingestFailureHint(status) {
|
|
253
|
+
switch (status) {
|
|
254
|
+
case 401:
|
|
255
|
+
return "check LEMMA_API_KEY";
|
|
256
|
+
case 403:
|
|
257
|
+
return "API key doesn't own this project_id";
|
|
258
|
+
case 429:
|
|
259
|
+
return "ingest rate limit exceeded; lower the trace rate if it persists";
|
|
260
|
+
case 404:
|
|
261
|
+
return "baseUrl likely wrong (not Lemma API)";
|
|
262
|
+
default:
|
|
263
|
+
return void 0;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function isRetryableIngestStatus(status) {
|
|
267
|
+
return status === 408 || status === 429 || status >= 500;
|
|
268
|
+
}
|
|
269
|
+
function parseRetryAfterMs(value) {
|
|
270
|
+
if (!value?.trim())
|
|
271
|
+
return null;
|
|
272
|
+
const seconds = Number(value);
|
|
273
|
+
return Number.isFinite(seconds) && seconds >= 0 ? seconds * 1e3 : null;
|
|
274
|
+
}
|
|
275
|
+
function ingestRetryDelayMs(retry, retryAfterMs, random = Math.random) {
|
|
276
|
+
const backoff = exports.INGEST_RETRY_BASE_DELAY_MS * 2 ** (retry - 1);
|
|
277
|
+
const jittered = backoff / 2 + random() * (backoff / 2);
|
|
278
|
+
return Math.min(exports.INGEST_RETRY_MAX_DELAY_MS, Math.max(jittered, retryAfterMs ?? 0));
|
|
279
|
+
}
|
|
280
|
+
function sleep(ms) {
|
|
281
|
+
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
282
|
+
}
|
|
283
|
+
async function withIngestTimeout(run) {
|
|
284
|
+
const controller = new AbortController();
|
|
285
|
+
let timer;
|
|
286
|
+
const timedOut = new Promise((_, reject) => {
|
|
287
|
+
timer = setTimeout(() => {
|
|
288
|
+
const error = new Error(`ingest request timed out after ${exports.INGEST_REQUEST_TIMEOUT_MS}ms`);
|
|
289
|
+
error.name = "TimeoutError";
|
|
290
|
+
controller.abort(error);
|
|
291
|
+
reject(error);
|
|
292
|
+
}, exports.INGEST_REQUEST_TIMEOUT_MS);
|
|
293
|
+
});
|
|
294
|
+
try {
|
|
295
|
+
return await Promise.race([run(controller.signal), timedOut]);
|
|
296
|
+
} finally {
|
|
297
|
+
clearTimeout(timer);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
function failedBeforeSending(error) {
|
|
301
|
+
return { ok: false, error, cause: `before sending: ${(0, error_message_1.describeError)(error)}` };
|
|
302
|
+
}
|
|
303
|
+
function failedAfter(attempts, attempt) {
|
|
304
|
+
return {
|
|
305
|
+
ok: false,
|
|
306
|
+
error: attempt.error,
|
|
307
|
+
cause: `after ${attempts} attempt${attempts === 1 ? "" : "s"}: ${attempt.reason}`
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
async function sendOnce(post, log) {
|
|
311
|
+
try {
|
|
312
|
+
return await withIngestTimeout(async (signal) => {
|
|
313
|
+
const response = await post(signal);
|
|
314
|
+
if (response.ok)
|
|
315
|
+
return { ok: true, response };
|
|
316
|
+
const body = await response.text().catch(() => "");
|
|
317
|
+
const hint = ingestFailureHint(response.status);
|
|
318
|
+
(0, debug_mode_1.lemmaDebug)("client", "trace ingest failed", {
|
|
319
|
+
...log,
|
|
320
|
+
status: response.status,
|
|
321
|
+
body,
|
|
322
|
+
...(0, debug_delivery_1.pickResponseHeaders)(response.headers),
|
|
323
|
+
...hint ? { hint } : {}
|
|
324
|
+
});
|
|
325
|
+
return {
|
|
326
|
+
ok: false,
|
|
327
|
+
error: new Error(`@uselemma/tracing: failed to ingest trace (${response.status})${body ? `: ${body}` : ""}`),
|
|
328
|
+
reason: `ingest returned ${response.status}${hint ? ` (${hint})` : ""}`,
|
|
329
|
+
retryable: isRetryableIngestStatus(response.status),
|
|
330
|
+
retryAfterMs: parseRetryAfterMs(response.headers.get("retry-after"))
|
|
331
|
+
};
|
|
332
|
+
});
|
|
333
|
+
} catch (error) {
|
|
334
|
+
(0, debug_mode_1.lemmaDebug)("client", "trace ingest failed", {
|
|
335
|
+
...log,
|
|
336
|
+
error: (0, error_message_1.failureMessage)(error) ?? String(error)
|
|
337
|
+
});
|
|
338
|
+
return {
|
|
339
|
+
ok: false,
|
|
340
|
+
error,
|
|
341
|
+
reason: (0, error_message_1.describeError)(error),
|
|
342
|
+
retryable: true,
|
|
343
|
+
retryAfterMs: null
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
async function deliver(post, log, maxAttempts) {
|
|
348
|
+
for (let attempts = 1; ; attempts += 1) {
|
|
349
|
+
const attempt = await sendOnce(post, log);
|
|
350
|
+
if (attempt.ok)
|
|
351
|
+
return { ok: true, response: attempt.response };
|
|
352
|
+
if (!attempt.retryable || attempts >= maxAttempts) {
|
|
353
|
+
return failedAfter(attempts, attempt);
|
|
354
|
+
}
|
|
355
|
+
const delayMs = ingestRetryDelayMs(attempts, attempt.retryAfterMs);
|
|
356
|
+
(0, debug_mode_1.lemmaDebug)("client", "retrying trace ingest", {
|
|
357
|
+
...log,
|
|
358
|
+
attempt: attempts,
|
|
359
|
+
maxAttempts,
|
|
360
|
+
delayMs
|
|
361
|
+
});
|
|
362
|
+
await sleep(delayMs);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
// ../../packages/ts/tracing/dist/dropped-traces.js
|
|
369
|
+
var require_dropped_traces = __commonJS({
|
|
370
|
+
"../../packages/ts/tracing/dist/dropped-traces.js"(exports) {
|
|
371
|
+
"use strict";
|
|
372
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
373
|
+
exports.DROPPED_TRACE_WARNING_WINDOW_MS = void 0;
|
|
374
|
+
exports.reportDroppedTrace = reportDroppedTrace;
|
|
375
|
+
exports.resetDroppedTraceWarnings = resetDroppedTraceWarnings;
|
|
376
|
+
var debug_mode_1 = require_debug_mode();
|
|
377
|
+
exports.DROPPED_TRACE_WARNING_WINDOW_MS = 6e4;
|
|
378
|
+
var PREFIX = "@uselemma/tracing:";
|
|
379
|
+
var WINDOW_SECONDS = exports.DROPPED_TRACE_WARNING_WINDOW_MS / 1e3;
|
|
380
|
+
var lastWarnedAt = null;
|
|
381
|
+
var suppressed = 0;
|
|
382
|
+
function reportDroppedTrace(traceId, cause) {
|
|
383
|
+
const line = `${PREFIX} dropped trace ${traceId} ${cause}.`;
|
|
384
|
+
if ((0, debug_mode_1.isDebugModeEnabled)()) {
|
|
385
|
+
console.warn(line);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
const now = Date.now();
|
|
389
|
+
if (lastWarnedAt !== null && now - lastWarnedAt < exports.DROPPED_TRACE_WARNING_WINDOW_MS) {
|
|
390
|
+
suppressed += 1;
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const since = suppressed > 0 ? ` ${suppressed} more dropped since the last warning.` : "";
|
|
394
|
+
console.warn(`${line}${since} Drops in the next ${WINDOW_SECONDS}s are counted, not logged; set LEMMA_DEBUG=1 to log each one.`);
|
|
395
|
+
lastWarnedAt = now;
|
|
396
|
+
suppressed = 0;
|
|
397
|
+
}
|
|
398
|
+
function resetDroppedTraceWarnings() {
|
|
399
|
+
lastWarnedAt = null;
|
|
400
|
+
suppressed = 0;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
|
|
249
405
|
// ../../packages/ts/tracing/dist/release.js
|
|
250
406
|
var require_release = __commonJS({
|
|
251
407
|
"../../packages/ts/tracing/dist/release.js"(exports) {
|
|
@@ -497,6 +653,16 @@ var require_usage = __commonJS({
|
|
|
497
653
|
}
|
|
498
654
|
});
|
|
499
655
|
|
|
656
|
+
// ../../packages/ts/tracing/dist/version.js
|
|
657
|
+
var require_version = __commonJS({
|
|
658
|
+
"../../packages/ts/tracing/dist/version.js"(exports) {
|
|
659
|
+
"use strict";
|
|
660
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
661
|
+
exports.SDK_USER_AGENT = void 0;
|
|
662
|
+
exports.SDK_USER_AGENT = "@uselemma/tracing/7.14.0";
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
|
|
500
666
|
// ../../packages/ts/tracing/dist/client.js
|
|
501
667
|
var require_client = __commonJS({
|
|
502
668
|
"../../packages/ts/tracing/dist/client.js"(exports) {
|
|
@@ -505,9 +671,12 @@ var require_client = __commonJS({
|
|
|
505
671
|
exports.Lemma = exports.TraceHandle = exports.TraceContext = exports.NoopSpanHandle = exports.SpanHandle = exports.toWireTokenUsage = exports.normalizeTokenUsage = exports.attachResultUsage = void 0;
|
|
506
672
|
var debug_delivery_1 = require_debug_delivery();
|
|
507
673
|
var debug_mode_1 = require_debug_mode();
|
|
674
|
+
var delivery_1 = require_delivery();
|
|
675
|
+
var dropped_traces_1 = require_dropped_traces();
|
|
508
676
|
var error_message_1 = require_error_message();
|
|
509
677
|
var release_1 = require_release();
|
|
510
678
|
var usage_1 = require_usage();
|
|
679
|
+
var version_1 = require_version();
|
|
511
680
|
var usage_2 = require_usage();
|
|
512
681
|
Object.defineProperty(exports, "attachResultUsage", { enumerable: true, get: function() {
|
|
513
682
|
return usage_2.attachResultUsage;
|
|
@@ -599,7 +768,10 @@ var require_client = __commonJS({
|
|
|
599
768
|
const attributes = {};
|
|
600
769
|
addDefined(attributes, "input.mime_type", options.inputMimeType);
|
|
601
770
|
addDefined(attributes, "output.mime_type", options.outputMimeType);
|
|
602
|
-
|
|
771
|
+
const modelIdentity = options.llmModelName ?? options.model;
|
|
772
|
+
addDefined(attributes, "llm.model_name", modelIdentity);
|
|
773
|
+
addDefined(attributes, "gen_ai.request.model", modelIdentity);
|
|
774
|
+
addDefined(attributes, "ai.model.id", modelIdentity);
|
|
603
775
|
addDefined(attributes, "llm.provider", options.llmProvider);
|
|
604
776
|
addDefined(attributes, "llm.system", options.llmSystem);
|
|
605
777
|
addDefined(attributes, "gen_ai.system", options.llmSystem ?? options.llmProvider);
|
|
@@ -700,6 +872,7 @@ var require_client = __commonJS({
|
|
|
700
872
|
startedAt: this.options.startedAt ?? /* @__PURE__ */ new Date(),
|
|
701
873
|
endedAt: this.options.endedAt ?? null
|
|
702
874
|
});
|
|
875
|
+
this.trace.registerSpanHandle(this);
|
|
703
876
|
}
|
|
704
877
|
end(options = {}) {
|
|
705
878
|
if (this.ended)
|
|
@@ -710,11 +883,30 @@ var require_client = __commonJS({
|
|
|
710
883
|
...options,
|
|
711
884
|
id: this.id,
|
|
712
885
|
startedAt: this.options.startedAt,
|
|
713
|
-
endedAt: options.endedAt ?? /* @__PURE__ */ new Date()
|
|
886
|
+
endedAt: options.endedAt ?? /* @__PURE__ */ new Date(),
|
|
887
|
+
// Keep a start-time model when end() omits it or would overwrite
|
|
888
|
+
// with undefined. Fill in from the response only when unset.
|
|
889
|
+
model: this.options.model ?? options.model
|
|
714
890
|
}, this.options.type ?? "span"));
|
|
715
891
|
this.trace.spanEnded(this.payload);
|
|
716
892
|
this.trace.changed();
|
|
717
893
|
}
|
|
894
|
+
/**
|
|
895
|
+
* Fill input or output after end() when a child model call finishes later.
|
|
896
|
+
* Input is written only while this span still has none. Output is replaced
|
|
897
|
+
* when the span did not carry its own output.
|
|
898
|
+
*/
|
|
899
|
+
fillContainerIo(input, output, ownOutput) {
|
|
900
|
+
if (!this.ended)
|
|
901
|
+
return;
|
|
902
|
+
if (this.payload.input === void 0 && input !== void 0) {
|
|
903
|
+
this.payload.input = input;
|
|
904
|
+
}
|
|
905
|
+
if (!ownOutput && output !== void 0) {
|
|
906
|
+
this.payload.output = output;
|
|
907
|
+
}
|
|
908
|
+
this.trace.changed();
|
|
909
|
+
}
|
|
718
910
|
/**
|
|
719
911
|
* Extend this span's end time if a child finished later.
|
|
720
912
|
* Only applies after end() — open parents are left alone so a later end()
|
|
@@ -824,6 +1016,7 @@ var require_client = __commonJS({
|
|
|
824
1016
|
options;
|
|
825
1017
|
onChange;
|
|
826
1018
|
spans = [];
|
|
1019
|
+
spanHandles = /* @__PURE__ */ new Map();
|
|
827
1020
|
traceOutput;
|
|
828
1021
|
traceError = null;
|
|
829
1022
|
id;
|
|
@@ -859,6 +1052,23 @@ var require_client = __commonJS({
|
|
|
859
1052
|
this.traceError = (0, error_message_1.failureMessage)(error);
|
|
860
1053
|
this.changed();
|
|
861
1054
|
}
|
|
1055
|
+
/** Root identity for a versioned turn context token. */
|
|
1056
|
+
identity() {
|
|
1057
|
+
return {
|
|
1058
|
+
name: this.options.name ?? "trace",
|
|
1059
|
+
threadId: this.options.threadId,
|
|
1060
|
+
userId: this.options.userId
|
|
1061
|
+
};
|
|
1062
|
+
}
|
|
1063
|
+
registerSpanHandle(handle) {
|
|
1064
|
+
this.spanHandles.set(handle.id, handle);
|
|
1065
|
+
}
|
|
1066
|
+
spanHandle(id) {
|
|
1067
|
+
return this.spanHandles.get(id);
|
|
1068
|
+
}
|
|
1069
|
+
hasSpan(id) {
|
|
1070
|
+
return this.spans.some((span) => span.id === id);
|
|
1071
|
+
}
|
|
862
1072
|
changed() {
|
|
863
1073
|
this.onChange?.();
|
|
864
1074
|
}
|
|
@@ -1015,14 +1225,20 @@ var require_client = __commonJS({
|
|
|
1015
1225
|
projectId;
|
|
1016
1226
|
baseUrl;
|
|
1017
1227
|
fetchImpl;
|
|
1228
|
+
/** Identity and credentials on every request; spread last so nothing overrides them. */
|
|
1229
|
+
baseHeaders;
|
|
1018
1230
|
release;
|
|
1019
1231
|
traces = /* @__PURE__ */ new Map();
|
|
1020
1232
|
configLogged = false;
|
|
1021
1233
|
constructor(options = {}) {
|
|
1022
1234
|
this.apiKey = required(options.apiKey ?? process.env.LEMMA_API_KEY, "LEMMA_API_KEY");
|
|
1023
1235
|
this.projectId = required(options.projectId ?? process.env.LEMMA_PROJECT_ID, "LEMMA_PROJECT_ID");
|
|
1024
|
-
this.baseUrl = (options.baseUrl ??
|
|
1236
|
+
this.baseUrl = (options.baseUrl ?? (process.env.LEMMA_BASE_URL?.trim() || debug_delivery_1.PRODUCTION_BASE_URL)).replace(/\/+$/, "");
|
|
1025
1237
|
this.fetchImpl = options.fetch ?? fetch;
|
|
1238
|
+
this.baseHeaders = {
|
|
1239
|
+
"User-Agent": options.userAgent ?? version_1.SDK_USER_AGENT,
|
|
1240
|
+
Authorization: `Bearer ${this.apiKey}`
|
|
1241
|
+
};
|
|
1026
1242
|
this.release = (0, release_1.normalizeRelease)(options.release ?? process.env.LEMMA_RELEASE);
|
|
1027
1243
|
this.logInitConfigOnce();
|
|
1028
1244
|
}
|
|
@@ -1054,14 +1270,7 @@ var require_client = __commonJS({
|
|
|
1054
1270
|
const url = `${this.baseUrl}${debug_delivery_1.INGEST_PATH}`;
|
|
1055
1271
|
let response;
|
|
1056
1272
|
try {
|
|
1057
|
-
response = await this.
|
|
1058
|
-
method: "POST",
|
|
1059
|
-
headers: {
|
|
1060
|
-
Authorization: `Bearer ${this.apiKey}`,
|
|
1061
|
-
"Content-Type": "application/json"
|
|
1062
|
-
},
|
|
1063
|
-
body
|
|
1064
|
-
});
|
|
1273
|
+
response = await (0, delivery_1.withIngestTimeout)((signal) => this.postIngest(body, signal));
|
|
1065
1274
|
} catch {
|
|
1066
1275
|
hints.push("ingest request failed (network error)");
|
|
1067
1276
|
return {
|
|
@@ -1074,7 +1283,7 @@ var require_client = __commonJS({
|
|
|
1074
1283
|
const ingestWarnings = [];
|
|
1075
1284
|
if (response.status !== debug_delivery_1.EXPECTED_INGEST_SUCCESS_STATUS) {
|
|
1076
1285
|
ingestWarnings.push(`expected status ${debug_delivery_1.EXPECTED_INGEST_SUCCESS_STATUS}, got ${response.status}`);
|
|
1077
|
-
const hint = (0,
|
|
1286
|
+
const hint = (0, delivery_1.ingestFailureHint)(response.status);
|
|
1078
1287
|
if (hint)
|
|
1079
1288
|
hints.push(hint);
|
|
1080
1289
|
}
|
|
@@ -1117,7 +1326,7 @@ var require_client = __commonJS({
|
|
|
1117
1326
|
if (!fn) {
|
|
1118
1327
|
const handle = new TraceHandle(traceOptions, async (trace, startedAt2, endedAt) => {
|
|
1119
1328
|
try {
|
|
1120
|
-
await this.
|
|
1329
|
+
await this.flushTraceAutomatic(trace, startedAt2, endedAt);
|
|
1121
1330
|
} finally {
|
|
1122
1331
|
this.traces.delete(trace.id);
|
|
1123
1332
|
}
|
|
@@ -1136,18 +1345,19 @@ var require_client = __commonJS({
|
|
|
1136
1345
|
name: traceOptions.name ?? "trace"
|
|
1137
1346
|
});
|
|
1138
1347
|
return (async () => {
|
|
1348
|
+
let result;
|
|
1139
1349
|
try {
|
|
1140
|
-
|
|
1141
|
-
if (traceOptions.output === void 0) {
|
|
1142
|
-
context.output(result);
|
|
1143
|
-
}
|
|
1144
|
-
await this.flushTrace(context, startedAt, /* @__PURE__ */ new Date());
|
|
1145
|
-
return result;
|
|
1350
|
+
result = await fn(context);
|
|
1146
1351
|
} catch (error) {
|
|
1147
1352
|
context.fail(error);
|
|
1148
|
-
await this.
|
|
1353
|
+
await this.flushTraceAutomatic(context, startedAt, /* @__PURE__ */ new Date());
|
|
1149
1354
|
throw error;
|
|
1150
1355
|
}
|
|
1356
|
+
if (traceOptions.output === void 0) {
|
|
1357
|
+
context.output(result);
|
|
1358
|
+
}
|
|
1359
|
+
await this.flushTraceAutomatic(context, startedAt, /* @__PURE__ */ new Date());
|
|
1360
|
+
return result;
|
|
1151
1361
|
})();
|
|
1152
1362
|
}
|
|
1153
1363
|
/**
|
|
@@ -1169,7 +1379,9 @@ var require_client = __commonJS({
|
|
|
1169
1379
|
* never mutates the trace's status, so a failed send can be retried as-is.
|
|
1170
1380
|
*/
|
|
1171
1381
|
async ingest(context, options) {
|
|
1172
|
-
await this.
|
|
1382
|
+
const failure = await this.sendTrace(context, options.startedAt, options.endedAt ?? /* @__PURE__ */ new Date(), delivery_1.INGEST_STRICT_ATTEMPTS);
|
|
1383
|
+
if (failure)
|
|
1384
|
+
throw failure.error;
|
|
1173
1385
|
}
|
|
1174
1386
|
recordSpan(options) {
|
|
1175
1387
|
const context = this.detachedTraceFor(options.traceId, "span");
|
|
@@ -1309,13 +1521,16 @@ var require_client = __commonJS({
|
|
|
1309
1521
|
async fetchIngestStatus(otelTraceId) {
|
|
1310
1522
|
const url = `${this.baseUrl}${debug_delivery_1.INGEST_STATUS_PATH}?project_id=${encodeURIComponent(this.projectId)}&otel_trace_id=${encodeURIComponent(otelTraceId)}`;
|
|
1311
1523
|
try {
|
|
1312
|
-
|
|
1313
|
-
|
|
1524
|
+
return await (0, delivery_1.withIngestTimeout)(async (signal) => {
|
|
1525
|
+
const response = await this.fetchImpl(url, {
|
|
1526
|
+
headers: { ...this.baseHeaders },
|
|
1527
|
+
signal
|
|
1528
|
+
});
|
|
1529
|
+
if (!response.ok)
|
|
1530
|
+
return null;
|
|
1531
|
+
const data = await response.json();
|
|
1532
|
+
return (0, debug_delivery_1.parseIngestStatus)(data.status);
|
|
1314
1533
|
});
|
|
1315
|
-
if (!response.ok)
|
|
1316
|
-
return null;
|
|
1317
|
-
const data = await response.json();
|
|
1318
|
-
return (0, debug_delivery_1.parseIngestStatus)(data.status);
|
|
1319
1534
|
} catch {
|
|
1320
1535
|
return null;
|
|
1321
1536
|
}
|
|
@@ -1335,7 +1550,7 @@ var require_client = __commonJS({
|
|
|
1335
1550
|
const remaining = deadline - Date.now();
|
|
1336
1551
|
if (remaining <= 0)
|
|
1337
1552
|
return status;
|
|
1338
|
-
await (0,
|
|
1553
|
+
await (0, delivery_1.sleep)(Math.min(debug_delivery_1.INGEST_STATUS_POLL_INTERVAL_MS, remaining));
|
|
1339
1554
|
}
|
|
1340
1555
|
}
|
|
1341
1556
|
async verifyIngestDelivery(otelTraceId) {
|
|
@@ -1355,41 +1570,73 @@ var require_client = __commonJS({
|
|
|
1355
1570
|
payload.trace.release = this.release;
|
|
1356
1571
|
return payload;
|
|
1357
1572
|
}
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
});
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1573
|
+
/**
|
|
1574
|
+
* Automatic SDK delivery. Retries transient failures, then fails open with
|
|
1575
|
+
* a rate-limited warning so Lemma being down cannot fail the caller's app.
|
|
1576
|
+
* {@link Lemma.ingest} sends once and throws, because queue/plugin producers
|
|
1577
|
+
* keep their own retry backlog.
|
|
1578
|
+
*/
|
|
1579
|
+
async flushTraceAutomatic(context, startedAt, endedAt) {
|
|
1580
|
+
const failure = await this.sendTrace(context, startedAt, endedAt, delivery_1.INGEST_MAX_ATTEMPTS);
|
|
1581
|
+
if (failure)
|
|
1582
|
+
(0, dropped_traces_1.reportDroppedTrace)(context.id, failure.cause);
|
|
1583
|
+
}
|
|
1584
|
+
/**
|
|
1585
|
+
* Build, send, and check one trace; undefined when it was delivered. A
|
|
1586
|
+
* payload that cannot be built comes back as a failure, so strict and
|
|
1587
|
+
* automatic delivery handle it once.
|
|
1588
|
+
*/
|
|
1589
|
+
async sendTrace(context, startedAt, endedAt, maxAttempts) {
|
|
1590
|
+
const built = this.buildIngestBody(context, startedAt, endedAt);
|
|
1591
|
+
if (!built.ok)
|
|
1592
|
+
return built;
|
|
1593
|
+
const result = await (0, delivery_1.deliver)((signal) => this.postIngest(built.body, signal), { traceId: context.id, projectId: this.projectId }, maxAttempts);
|
|
1594
|
+
if (!result.ok)
|
|
1595
|
+
return result;
|
|
1596
|
+
try {
|
|
1597
|
+
await this.afterTraceSent(built.payload, result.response);
|
|
1598
|
+
} catch (error) {
|
|
1599
|
+
console.warn(`@uselemma/tracing: trace ${context.id} was delivered, but its post-send check failed: ${(0, error_message_1.describeError)(error)}`);
|
|
1600
|
+
}
|
|
1601
|
+
return void 0;
|
|
1602
|
+
}
|
|
1603
|
+
/**
|
|
1604
|
+
* Serialize once, so every attempt resends the same span IDs and ingest
|
|
1605
|
+
* dedupes an earlier attempt that landed unseen.
|
|
1606
|
+
*/
|
|
1607
|
+
buildIngestBody(context, startedAt, endedAt) {
|
|
1608
|
+
try {
|
|
1609
|
+
const payload = this.stampRelease(context.toPayload(this.projectId, startedAt, endedAt));
|
|
1610
|
+
const body = JSON.stringify(payload);
|
|
1611
|
+
(0, debug_mode_1.lemmaDebug)("client", "sending trace", {
|
|
1384
1612
|
traceId: payload.trace.id,
|
|
1613
|
+
name: payload.trace.name,
|
|
1614
|
+
spanCount: payload.trace.spans.length,
|
|
1385
1615
|
projectId: payload.project_id,
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
...hint ? { hint } : {}
|
|
1616
|
+
bodyBytes: new TextEncoder().encode(body).length,
|
|
1617
|
+
requestedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1618
|
+
url: `${this.baseUrl}${debug_delivery_1.INGEST_PATH}`
|
|
1390
1619
|
});
|
|
1391
|
-
|
|
1620
|
+
return { ok: true, payload, body };
|
|
1621
|
+
} catch (error) {
|
|
1622
|
+
(0, debug_mode_1.lemmaDebug)("client", "trace ingest failed", {
|
|
1623
|
+
traceId: context.id,
|
|
1624
|
+
projectId: this.projectId,
|
|
1625
|
+
error: (0, error_message_1.failureMessage)(error) ?? String(error)
|
|
1626
|
+
});
|
|
1627
|
+
return (0, delivery_1.failedBeforeSending)(error);
|
|
1392
1628
|
}
|
|
1629
|
+
}
|
|
1630
|
+
postIngest(body, signal) {
|
|
1631
|
+
return this.fetchImpl(`${this.baseUrl}${debug_delivery_1.INGEST_PATH}`, {
|
|
1632
|
+
method: "POST",
|
|
1633
|
+
headers: { "Content-Type": "application/json", ...this.baseHeaders },
|
|
1634
|
+
body,
|
|
1635
|
+
signal
|
|
1636
|
+
});
|
|
1637
|
+
}
|
|
1638
|
+
async afterTraceSent(payload, response) {
|
|
1639
|
+
const responseHeaders = (0, debug_delivery_1.pickResponseHeaders)(response.headers);
|
|
1393
1640
|
const sentWarnings = [];
|
|
1394
1641
|
if (response.status !== debug_delivery_1.EXPECTED_INGEST_SUCCESS_STATUS) {
|
|
1395
1642
|
sentWarnings.push(`expected status ${debug_delivery_1.EXPECTED_INGEST_SUCCESS_STATUS}, got ${response.status} (wrong endpoint?)`);
|
|
@@ -1410,6 +1657,309 @@ var require_client = __commonJS({
|
|
|
1410
1657
|
}
|
|
1411
1658
|
});
|
|
1412
1659
|
|
|
1660
|
+
// ../../packages/ts/tracing/dist/turn.js
|
|
1661
|
+
var require_turn = __commonJS({
|
|
1662
|
+
"../../packages/ts/tracing/dist/turn.js"(exports) {
|
|
1663
|
+
"use strict";
|
|
1664
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1665
|
+
exports.TurnHandle = exports.AttachedTurn = exports.AttachedSpanHandle = exports.TURN_JOURNAL_VERSION = exports.TURN_CONTEXT_VERSION = void 0;
|
|
1666
|
+
exports.parseTurnContextToken = parseTurnContextToken;
|
|
1667
|
+
exports.applyTurnJournal = applyTurnJournal;
|
|
1668
|
+
exports.assembleTurn = assembleTurn;
|
|
1669
|
+
exports.startTurn = startTurn2;
|
|
1670
|
+
exports.attachTurn = attachTurn;
|
|
1671
|
+
var client_1 = require_client();
|
|
1672
|
+
exports.TURN_CONTEXT_VERSION = 1;
|
|
1673
|
+
exports.TURN_JOURNAL_VERSION = 1;
|
|
1674
|
+
function compact(value) {
|
|
1675
|
+
return Object.fromEntries(Object.entries(value).filter(([, entry]) => entry !== void 0));
|
|
1676
|
+
}
|
|
1677
|
+
function isoNow() {
|
|
1678
|
+
return (/* @__PURE__ */ new Date()).toISOString();
|
|
1679
|
+
}
|
|
1680
|
+
function asIso(value) {
|
|
1681
|
+
if (value == null)
|
|
1682
|
+
return value;
|
|
1683
|
+
return value instanceof Date ? value.toISOString() : value;
|
|
1684
|
+
}
|
|
1685
|
+
function parseTurnContextToken(value) {
|
|
1686
|
+
const token = typeof value === "string" ? JSON.parse(value) : value;
|
|
1687
|
+
if (!token || typeof token !== "object" || token.version !== exports.TURN_CONTEXT_VERSION || typeof token.traceId !== "string" || token.traceId.length === 0 || typeof token.startedAt !== "string" || token.startedAt.length === 0) {
|
|
1688
|
+
throw new Error("@uselemma/tracing: invalid turn context token");
|
|
1689
|
+
}
|
|
1690
|
+
return token;
|
|
1691
|
+
}
|
|
1692
|
+
function parseJournal(input) {
|
|
1693
|
+
const value = typeof input === "string" ? JSON.parse(input) : input;
|
|
1694
|
+
if (Array.isArray(value)) {
|
|
1695
|
+
return { records: value };
|
|
1696
|
+
}
|
|
1697
|
+
if (value && typeof value === "object" && "op" in value) {
|
|
1698
|
+
return { records: [value] };
|
|
1699
|
+
}
|
|
1700
|
+
if (value && typeof value === "object" && "records" in value && Array.isArray(value.records)) {
|
|
1701
|
+
const journal = value;
|
|
1702
|
+
if (journal.version !== void 0 && journal.version !== exports.TURN_JOURNAL_VERSION) {
|
|
1703
|
+
throw new Error(`@uselemma/tracing: unsupported turn journal version ${String(journal.version)}`);
|
|
1704
|
+
}
|
|
1705
|
+
return { token: journal.token, records: journal.records };
|
|
1706
|
+
}
|
|
1707
|
+
throw new Error("@uselemma/tracing: invalid turn journal");
|
|
1708
|
+
}
|
|
1709
|
+
var END_IDENTITY_KEYS = {
|
|
1710
|
+
id: true,
|
|
1711
|
+
parentId: true,
|
|
1712
|
+
parentSpanId: true,
|
|
1713
|
+
name: true,
|
|
1714
|
+
type: true,
|
|
1715
|
+
startedAt: true
|
|
1716
|
+
};
|
|
1717
|
+
function toSpanOptions(record, fallbackParentId) {
|
|
1718
|
+
const { op: _op, ...fields } = record;
|
|
1719
|
+
return compact({
|
|
1720
|
+
...fields,
|
|
1721
|
+
parentId: record.parentId ?? fallbackParentId,
|
|
1722
|
+
name: record.name ?? "span"
|
|
1723
|
+
});
|
|
1724
|
+
}
|
|
1725
|
+
function endOptionsFromRecord(record, fallbackParentId) {
|
|
1726
|
+
const options = toSpanOptions(record, fallbackParentId);
|
|
1727
|
+
return compact(Object.fromEntries(Object.entries(options).filter(([key]) => !Object.prototype.hasOwnProperty.call(END_IDENTITY_KEYS, key))));
|
|
1728
|
+
}
|
|
1729
|
+
function startFromRecord(context, options) {
|
|
1730
|
+
const type = options.type ?? "span";
|
|
1731
|
+
if (type === "generation") {
|
|
1732
|
+
return context.startGeneration(options);
|
|
1733
|
+
}
|
|
1734
|
+
if (type === "tool") {
|
|
1735
|
+
return context.startTool(options);
|
|
1736
|
+
}
|
|
1737
|
+
return context.startSpan(options);
|
|
1738
|
+
}
|
|
1739
|
+
function recordFromRecord(context, options) {
|
|
1740
|
+
const type = options.type ?? "span";
|
|
1741
|
+
if (type === "generation") {
|
|
1742
|
+
context.recordGeneration(options);
|
|
1743
|
+
return;
|
|
1744
|
+
}
|
|
1745
|
+
if (type === "tool") {
|
|
1746
|
+
context.recordTool(options);
|
|
1747
|
+
return;
|
|
1748
|
+
}
|
|
1749
|
+
context.recordSpan(options);
|
|
1750
|
+
}
|
|
1751
|
+
function applyTurnJournal(context, input) {
|
|
1752
|
+
const { token, records } = parseJournal(input);
|
|
1753
|
+
const fallbackParentId = token?.parentSpanId ?? null;
|
|
1754
|
+
for (const record of records) {
|
|
1755
|
+
if (!record?.id || !record.op)
|
|
1756
|
+
continue;
|
|
1757
|
+
const options = toSpanOptions(record, fallbackParentId);
|
|
1758
|
+
if (record.op === "start") {
|
|
1759
|
+
if (context.hasSpan(record.id))
|
|
1760
|
+
continue;
|
|
1761
|
+
startFromRecord(context, { ...options, endedAt: void 0 });
|
|
1762
|
+
continue;
|
|
1763
|
+
}
|
|
1764
|
+
if (record.op === "end") {
|
|
1765
|
+
const handle = context.spanHandle(record.id);
|
|
1766
|
+
if (handle) {
|
|
1767
|
+
handle.end(endOptionsFromRecord(record, fallbackParentId));
|
|
1768
|
+
continue;
|
|
1769
|
+
}
|
|
1770
|
+
if (context.hasSpan(record.id))
|
|
1771
|
+
continue;
|
|
1772
|
+
recordFromRecord(context, options);
|
|
1773
|
+
continue;
|
|
1774
|
+
}
|
|
1775
|
+
if (context.hasSpan(record.id))
|
|
1776
|
+
continue;
|
|
1777
|
+
recordFromRecord(context, options);
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
function assembleTurn(token, journal, options = {}) {
|
|
1781
|
+
const parsed = parseTurnContextToken(token);
|
|
1782
|
+
const context = new client_1.TraceContext({
|
|
1783
|
+
id: parsed.traceId,
|
|
1784
|
+
name: options.name ?? parsed.name ?? "trace",
|
|
1785
|
+
input: options.input,
|
|
1786
|
+
output: options.output,
|
|
1787
|
+
metadata: options.metadata,
|
|
1788
|
+
threadId: options.threadId ?? parsed.threadId,
|
|
1789
|
+
userId: options.userId ?? parsed.userId,
|
|
1790
|
+
durationMs: options.durationMs,
|
|
1791
|
+
startedAt: options.startedAt ?? parsed.startedAt
|
|
1792
|
+
});
|
|
1793
|
+
if (journal !== void 0)
|
|
1794
|
+
applyTurnJournal(context, journal);
|
|
1795
|
+
return { context, startedAt: new Date(parsed.startedAt) };
|
|
1796
|
+
}
|
|
1797
|
+
function spanFields(options, fallbackParentId) {
|
|
1798
|
+
const { parentSpanId, ...fields } = options;
|
|
1799
|
+
return compact({
|
|
1800
|
+
...fields,
|
|
1801
|
+
id: options.id ?? crypto.randomUUID(),
|
|
1802
|
+
parentId: options.parentId ?? parentSpanId ?? fallbackParentId ?? null,
|
|
1803
|
+
startedAt: asIso(options.startedAt),
|
|
1804
|
+
endedAt: asIso(options.endedAt)
|
|
1805
|
+
});
|
|
1806
|
+
}
|
|
1807
|
+
function asNamedOptions(options) {
|
|
1808
|
+
return typeof options === "string" ? { name: options } : options;
|
|
1809
|
+
}
|
|
1810
|
+
function attachedSpanOps(writeStart, writeRecord, defaultParentId) {
|
|
1811
|
+
const nest = (options) => ({
|
|
1812
|
+
...options,
|
|
1813
|
+
parentId: options.parentId ?? defaultParentId() ?? null
|
|
1814
|
+
});
|
|
1815
|
+
return {
|
|
1816
|
+
startSpan(options) {
|
|
1817
|
+
return writeStart("span", nest(asNamedOptions(options)));
|
|
1818
|
+
},
|
|
1819
|
+
startGeneration(options) {
|
|
1820
|
+
return writeStart("generation", nest(asNamedOptions(options)));
|
|
1821
|
+
},
|
|
1822
|
+
startTool(options) {
|
|
1823
|
+
return writeStart("tool", nest(asNamedOptions(options)));
|
|
1824
|
+
},
|
|
1825
|
+
recordSpan(options) {
|
|
1826
|
+
return writeRecord("span", nest(asNamedOptions(options)));
|
|
1827
|
+
},
|
|
1828
|
+
recordGeneration(options) {
|
|
1829
|
+
writeRecord("generation", nest(asNamedOptions(options)));
|
|
1830
|
+
},
|
|
1831
|
+
recordTool(options) {
|
|
1832
|
+
writeRecord("tool", nest(asNamedOptions(options)));
|
|
1833
|
+
}
|
|
1834
|
+
};
|
|
1835
|
+
}
|
|
1836
|
+
var AttachedSpanHandle = class {
|
|
1837
|
+
recorder;
|
|
1838
|
+
fields;
|
|
1839
|
+
ended;
|
|
1840
|
+
id;
|
|
1841
|
+
startSpan;
|
|
1842
|
+
startGeneration;
|
|
1843
|
+
startTool;
|
|
1844
|
+
recordSpan;
|
|
1845
|
+
recordGeneration;
|
|
1846
|
+
recordTool;
|
|
1847
|
+
constructor(recorder, fields, ended = false) {
|
|
1848
|
+
this.recorder = recorder;
|
|
1849
|
+
this.fields = fields;
|
|
1850
|
+
this.ended = ended;
|
|
1851
|
+
this.id = fields.id;
|
|
1852
|
+
const ops = attachedSpanOps((type, options) => this.recorder.writeStart(type, options), (type, options) => this.recorder.writeRecord(type, options), () => this.id);
|
|
1853
|
+
this.startSpan = ops.startSpan;
|
|
1854
|
+
this.startGeneration = ops.startGeneration;
|
|
1855
|
+
this.startTool = ops.startTool;
|
|
1856
|
+
this.recordSpan = ops.recordSpan;
|
|
1857
|
+
this.recordGeneration = ops.recordGeneration;
|
|
1858
|
+
this.recordTool = ops.recordTool;
|
|
1859
|
+
}
|
|
1860
|
+
end(options = {}) {
|
|
1861
|
+
if (this.ended)
|
|
1862
|
+
return;
|
|
1863
|
+
this.ended = true;
|
|
1864
|
+
this.recorder.append({
|
|
1865
|
+
op: "end",
|
|
1866
|
+
...spanFields({
|
|
1867
|
+
...this.fields,
|
|
1868
|
+
...options,
|
|
1869
|
+
id: this.id,
|
|
1870
|
+
name: this.fields.name ?? "span"
|
|
1871
|
+
}, this.fields.parentId),
|
|
1872
|
+
id: this.id,
|
|
1873
|
+
startedAt: this.fields.startedAt
|
|
1874
|
+
});
|
|
1875
|
+
}
|
|
1876
|
+
};
|
|
1877
|
+
exports.AttachedSpanHandle = AttachedSpanHandle;
|
|
1878
|
+
var AttachedTurn = class {
|
|
1879
|
+
token;
|
|
1880
|
+
journal = [];
|
|
1881
|
+
startSpan;
|
|
1882
|
+
startGeneration;
|
|
1883
|
+
startTool;
|
|
1884
|
+
recordSpan;
|
|
1885
|
+
recordGeneration;
|
|
1886
|
+
recordTool;
|
|
1887
|
+
constructor(token) {
|
|
1888
|
+
this.token = parseTurnContextToken(token);
|
|
1889
|
+
const ops = attachedSpanOps((type, options) => this.writeStart(type, options), (type, options) => this.writeRecord(type, options), () => this.token.parentSpanId);
|
|
1890
|
+
this.startSpan = ops.startSpan;
|
|
1891
|
+
this.startGeneration = ops.startGeneration;
|
|
1892
|
+
this.startTool = ops.startTool;
|
|
1893
|
+
this.recordSpan = ops.recordSpan;
|
|
1894
|
+
this.recordGeneration = ops.recordGeneration;
|
|
1895
|
+
this.recordTool = ops.recordTool;
|
|
1896
|
+
}
|
|
1897
|
+
get id() {
|
|
1898
|
+
return this.token.traceId;
|
|
1899
|
+
}
|
|
1900
|
+
append(record) {
|
|
1901
|
+
this.journal.push(record);
|
|
1902
|
+
}
|
|
1903
|
+
records() {
|
|
1904
|
+
return {
|
|
1905
|
+
version: exports.TURN_JOURNAL_VERSION,
|
|
1906
|
+
token: this.token,
|
|
1907
|
+
records: this.journal
|
|
1908
|
+
};
|
|
1909
|
+
}
|
|
1910
|
+
writeStart(type, options) {
|
|
1911
|
+
const fields = spanFields({
|
|
1912
|
+
...options,
|
|
1913
|
+
type,
|
|
1914
|
+
id: options.id ?? crypto.randomUUID(),
|
|
1915
|
+
startedAt: options.startedAt ?? isoNow()
|
|
1916
|
+
}, this.token.parentSpanId);
|
|
1917
|
+
this.append({ op: "start", ...fields, type });
|
|
1918
|
+
return new AttachedSpanHandle(this, { ...fields, type });
|
|
1919
|
+
}
|
|
1920
|
+
writeRecord(type, options) {
|
|
1921
|
+
const now = isoNow();
|
|
1922
|
+
const fields = spanFields({
|
|
1923
|
+
...options,
|
|
1924
|
+
type,
|
|
1925
|
+
id: options.id ?? crypto.randomUUID(),
|
|
1926
|
+
startedAt: options.startedAt ?? now,
|
|
1927
|
+
endedAt: options.endedAt === void 0 ? now : options.endedAt
|
|
1928
|
+
}, this.token.parentSpanId);
|
|
1929
|
+
this.append({ op: "record", ...fields, type });
|
|
1930
|
+
return new AttachedSpanHandle(this, { ...fields, type }, true);
|
|
1931
|
+
}
|
|
1932
|
+
};
|
|
1933
|
+
exports.AttachedTurn = AttachedTurn;
|
|
1934
|
+
var TurnHandle = class extends client_1.TraceHandle {
|
|
1935
|
+
export(options = {}) {
|
|
1936
|
+
const identity = this.identity();
|
|
1937
|
+
return compact({
|
|
1938
|
+
version: exports.TURN_CONTEXT_VERSION,
|
|
1939
|
+
traceId: this.id,
|
|
1940
|
+
parentSpanId: options.parentSpanId ?? null,
|
|
1941
|
+
threadId: identity.threadId,
|
|
1942
|
+
userId: identity.userId,
|
|
1943
|
+
startedAt: this.startedAt.toISOString(),
|
|
1944
|
+
name: identity.name
|
|
1945
|
+
});
|
|
1946
|
+
}
|
|
1947
|
+
apply(input) {
|
|
1948
|
+
applyTurnJournal(this, input);
|
|
1949
|
+
}
|
|
1950
|
+
};
|
|
1951
|
+
exports.TurnHandle = TurnHandle;
|
|
1952
|
+
function startTurn2(lemma, options = {}) {
|
|
1953
|
+
return new TurnHandle(options, async (trace, startedAt, endedAt) => {
|
|
1954
|
+
await lemma.ingest(trace, { startedAt, endedAt });
|
|
1955
|
+
});
|
|
1956
|
+
}
|
|
1957
|
+
function attachTurn(token) {
|
|
1958
|
+
return new AttachedTurn(token);
|
|
1959
|
+
}
|
|
1960
|
+
}
|
|
1961
|
+
});
|
|
1962
|
+
|
|
1413
1963
|
// ../../packages/ts/tracing/dist/coding-agent.js
|
|
1414
1964
|
var require_coding_agent = __commonJS({
|
|
1415
1965
|
"../../packages/ts/tracing/dist/coding-agent.js"(exports) {
|
|
@@ -1630,6 +2180,9 @@ var require_tool_result = __commonJS({
|
|
|
1630
2180
|
const message = nonEmptyString(record.message);
|
|
1631
2181
|
if (message)
|
|
1632
2182
|
return message;
|
|
2183
|
+
const encoded = encodedPayloadError(record);
|
|
2184
|
+
if (encoded)
|
|
2185
|
+
return encoded;
|
|
1633
2186
|
try {
|
|
1634
2187
|
return JSON.stringify(record);
|
|
1635
2188
|
} catch {
|
|
@@ -1639,7 +2192,7 @@ var require_tool_result = __commonJS({
|
|
|
1639
2192
|
return encodedPayloadError(record);
|
|
1640
2193
|
}
|
|
1641
2194
|
function isFlaggedFailure(record) {
|
|
1642
|
-
return record.isError === true || record.is_error === true || record.error === true;
|
|
2195
|
+
return record.isError === true || record.is_error === true || record.error === true || record.status === "error" || record.status === "failed" || record.success === false;
|
|
1643
2196
|
}
|
|
1644
2197
|
function encodedPayloadError(record) {
|
|
1645
2198
|
const direct = nonEmptyString(record.error);
|
|
@@ -1697,6 +2250,173 @@ var require_tool_result = __commonJS({
|
|
|
1697
2250
|
}
|
|
1698
2251
|
});
|
|
1699
2252
|
|
|
2253
|
+
// ../../packages/ts/tracing/dist/model.js
|
|
2254
|
+
var require_model = __commonJS({
|
|
2255
|
+
"../../packages/ts/tracing/dist/model.js"(exports) {
|
|
2256
|
+
"use strict";
|
|
2257
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2258
|
+
exports.pickModelIdentity = pickModelIdentity;
|
|
2259
|
+
exports.pickGenerationModelIdentity = pickGenerationModelIdentity;
|
|
2260
|
+
var MODEL_KEYS = [
|
|
2261
|
+
"model",
|
|
2262
|
+
"model_name",
|
|
2263
|
+
"modelName",
|
|
2264
|
+
"model_id",
|
|
2265
|
+
"modelId",
|
|
2266
|
+
"ls_model_name"
|
|
2267
|
+
];
|
|
2268
|
+
var NESTED_CONTAINERS = [
|
|
2269
|
+
"response_metadata",
|
|
2270
|
+
"responseMetadata",
|
|
2271
|
+
"llm_output",
|
|
2272
|
+
"llmOutput",
|
|
2273
|
+
"generationInfo",
|
|
2274
|
+
"generation_info",
|
|
2275
|
+
"kwargs",
|
|
2276
|
+
"additional_kwargs",
|
|
2277
|
+
"message",
|
|
2278
|
+
"response",
|
|
2279
|
+
"_response"
|
|
2280
|
+
];
|
|
2281
|
+
function asNonEmptyString(value) {
|
|
2282
|
+
if (typeof value === "string" && value.trim())
|
|
2283
|
+
return value.trim();
|
|
2284
|
+
return void 0;
|
|
2285
|
+
}
|
|
2286
|
+
function asRecord(value) {
|
|
2287
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
2288
|
+
return void 0;
|
|
2289
|
+
}
|
|
2290
|
+
return value;
|
|
2291
|
+
}
|
|
2292
|
+
function pickModelIdentity(raw) {
|
|
2293
|
+
const record = asRecord(raw);
|
|
2294
|
+
if (!record)
|
|
2295
|
+
return void 0;
|
|
2296
|
+
for (const key of MODEL_KEYS) {
|
|
2297
|
+
const value = record[key];
|
|
2298
|
+
const asString = asNonEmptyString(value);
|
|
2299
|
+
if (asString)
|
|
2300
|
+
return asString;
|
|
2301
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
2302
|
+
const nested = pickModelIdentity(value);
|
|
2303
|
+
if (nested)
|
|
2304
|
+
return nested;
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
for (const key of NESTED_CONTAINERS) {
|
|
2308
|
+
const nested = pickModelIdentity(record[key]);
|
|
2309
|
+
if (nested)
|
|
2310
|
+
return nested;
|
|
2311
|
+
}
|
|
2312
|
+
return void 0;
|
|
2313
|
+
}
|
|
2314
|
+
function pickGenerationModelIdentity(raw) {
|
|
2315
|
+
const direct = pickModelIdentity(raw);
|
|
2316
|
+
if (direct)
|
|
2317
|
+
return direct;
|
|
2318
|
+
const record = asRecord(raw);
|
|
2319
|
+
if (!record)
|
|
2320
|
+
return void 0;
|
|
2321
|
+
const generations = record.generations;
|
|
2322
|
+
if (!Array.isArray(generations))
|
|
2323
|
+
return void 0;
|
|
2324
|
+
for (const group of generations) {
|
|
2325
|
+
if (!Array.isArray(group)) {
|
|
2326
|
+
const fromGroup = pickModelIdentity(group);
|
|
2327
|
+
if (fromGroup)
|
|
2328
|
+
return fromGroup;
|
|
2329
|
+
continue;
|
|
2330
|
+
}
|
|
2331
|
+
for (const item of group) {
|
|
2332
|
+
const fromItem = pickModelIdentity(item);
|
|
2333
|
+
if (fromItem)
|
|
2334
|
+
return fromItem;
|
|
2335
|
+
}
|
|
2336
|
+
}
|
|
2337
|
+
return void 0;
|
|
2338
|
+
}
|
|
2339
|
+
}
|
|
2340
|
+
});
|
|
2341
|
+
|
|
2342
|
+
// ../../packages/ts/tracing/dist/reply-text.js
|
|
2343
|
+
var require_reply_text = __commonJS({
|
|
2344
|
+
"../../packages/ts/tracing/dist/reply-text.js"(exports) {
|
|
2345
|
+
"use strict";
|
|
2346
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2347
|
+
exports.isReplyReasoning = isReplyReasoning;
|
|
2348
|
+
exports.readContentReply = readContentReply;
|
|
2349
|
+
exports.textFromGenerationOutput = textFromGenerationOutput;
|
|
2350
|
+
function isReplyReasoning(record) {
|
|
2351
|
+
return record.type === "reasoning" || typeof record.encrypted_content === "string";
|
|
2352
|
+
}
|
|
2353
|
+
function readContentReply(content) {
|
|
2354
|
+
if (!Array.isArray(content))
|
|
2355
|
+
return void 0;
|
|
2356
|
+
const parts = [];
|
|
2357
|
+
let recognized = false;
|
|
2358
|
+
for (const block of content) {
|
|
2359
|
+
if (!block || typeof block !== "object")
|
|
2360
|
+
continue;
|
|
2361
|
+
const record = block;
|
|
2362
|
+
if (isReplyReasoning(record)) {
|
|
2363
|
+
recognized = true;
|
|
2364
|
+
continue;
|
|
2365
|
+
}
|
|
2366
|
+
if ((record.type === "text" || record.type === "output_text") && typeof record.text === "string") {
|
|
2367
|
+
parts.push(record.text);
|
|
2368
|
+
recognized = true;
|
|
2369
|
+
}
|
|
2370
|
+
}
|
|
2371
|
+
return { parts, recognized };
|
|
2372
|
+
}
|
|
2373
|
+
function textFromContentParts(content) {
|
|
2374
|
+
if (typeof content === "string")
|
|
2375
|
+
return content;
|
|
2376
|
+
const reply = readContentReply(content);
|
|
2377
|
+
if (!reply || !reply.recognized)
|
|
2378
|
+
return void 0;
|
|
2379
|
+
return reply.parts.join("");
|
|
2380
|
+
}
|
|
2381
|
+
function textFromGenerationOutput(output) {
|
|
2382
|
+
if (!Array.isArray(output))
|
|
2383
|
+
return output;
|
|
2384
|
+
let text = "";
|
|
2385
|
+
let sawUnhandled = false;
|
|
2386
|
+
for (const item of output) {
|
|
2387
|
+
if (!item || typeof item !== "object") {
|
|
2388
|
+
sawUnhandled = true;
|
|
2389
|
+
continue;
|
|
2390
|
+
}
|
|
2391
|
+
const record = item;
|
|
2392
|
+
if (isReplyReasoning(record))
|
|
2393
|
+
continue;
|
|
2394
|
+
if (typeof record.text === "string") {
|
|
2395
|
+
text += record.text;
|
|
2396
|
+
continue;
|
|
2397
|
+
}
|
|
2398
|
+
if (typeof record.content === "string") {
|
|
2399
|
+
text += record.content;
|
|
2400
|
+
continue;
|
|
2401
|
+
}
|
|
2402
|
+
if (Array.isArray(record.content)) {
|
|
2403
|
+
const fromParts = textFromContentParts(record.content);
|
|
2404
|
+
if (fromParts !== void 0) {
|
|
2405
|
+
text += fromParts;
|
|
2406
|
+
continue;
|
|
2407
|
+
}
|
|
2408
|
+
}
|
|
2409
|
+
sawUnhandled = true;
|
|
2410
|
+
}
|
|
2411
|
+
if (text)
|
|
2412
|
+
return text;
|
|
2413
|
+
if (!sawUnhandled && output.length > 0)
|
|
2414
|
+
return "";
|
|
2415
|
+
return output;
|
|
2416
|
+
}
|
|
2417
|
+
}
|
|
2418
|
+
});
|
|
2419
|
+
|
|
1700
2420
|
// ../../packages/ts/tracing/dist/langchain.js
|
|
1701
2421
|
var require_langchain = __commonJS({
|
|
1702
2422
|
"../../packages/ts/tracing/dist/langchain.js"(exports) {
|
|
@@ -1709,6 +2429,8 @@ var require_langchain = __commonJS({
|
|
|
1709
2429
|
var error_message_1 = require_error_message();
|
|
1710
2430
|
var schedule_1 = require_schedule();
|
|
1711
2431
|
var tool_result_1 = require_tool_result();
|
|
2432
|
+
var model_1 = require_model();
|
|
2433
|
+
var reply_text_1 = require_reply_text();
|
|
1712
2434
|
var usage_1 = require_usage();
|
|
1713
2435
|
var INTEGRATION_ATTRS = {
|
|
1714
2436
|
"lemma.sdk.integration": "langchain"
|
|
@@ -1765,26 +2487,6 @@ var require_langchain = __commonJS({
|
|
|
1765
2487
|
}
|
|
1766
2488
|
return fallback;
|
|
1767
2489
|
}
|
|
1768
|
-
function modelName(serialized, extraParams) {
|
|
1769
|
-
const kwargs = serialized?.kwargs;
|
|
1770
|
-
const sources = [kwargs, serialized, extraParams];
|
|
1771
|
-
for (const source of sources) {
|
|
1772
|
-
if (!source)
|
|
1773
|
-
continue;
|
|
1774
|
-
for (const key of [
|
|
1775
|
-
"model",
|
|
1776
|
-
"modelName",
|
|
1777
|
-
"model_name",
|
|
1778
|
-
"model_id",
|
|
1779
|
-
"modelId"
|
|
1780
|
-
]) {
|
|
1781
|
-
const value = source[key];
|
|
1782
|
-
if (typeof value === "string" && value)
|
|
1783
|
-
return value;
|
|
1784
|
-
}
|
|
1785
|
-
}
|
|
1786
|
-
return void 0;
|
|
1787
|
-
}
|
|
1788
2490
|
function lookupString(sources, keys) {
|
|
1789
2491
|
for (const source of sources) {
|
|
1790
2492
|
if (!source)
|
|
@@ -1970,11 +2672,24 @@ var require_langchain = __commonJS({
|
|
|
1970
2672
|
}
|
|
1971
2673
|
return input;
|
|
1972
2674
|
}
|
|
2675
|
+
function textFromContentBlocks(content) {
|
|
2676
|
+
const reply = (0, reply_text_1.readContentReply)(content);
|
|
2677
|
+
if (!reply || reply.parts.length === 0)
|
|
2678
|
+
return void 0;
|
|
2679
|
+
return reply.parts.join("\n");
|
|
2680
|
+
}
|
|
2681
|
+
function flattenRootDisplay(value) {
|
|
2682
|
+
const display = textFromContentBlocks(value);
|
|
2683
|
+
return display !== void 0 ? display : value;
|
|
2684
|
+
}
|
|
1973
2685
|
function rootTraceOutput(output) {
|
|
1974
2686
|
if (output == null)
|
|
1975
2687
|
return output;
|
|
1976
2688
|
if (typeof output === "string")
|
|
1977
2689
|
return output;
|
|
2690
|
+
const display = textFromContentBlocks(output);
|
|
2691
|
+
if (display !== void 0)
|
|
2692
|
+
return display;
|
|
1978
2693
|
if (output && typeof output === "object" && !Array.isArray(output)) {
|
|
1979
2694
|
const record = output;
|
|
1980
2695
|
if (record.role === "assistant" && (record.tool_calls != null || record.toolCalls != null)) {
|
|
@@ -1986,10 +2701,10 @@ var require_langchain = __commonJS({
|
|
|
1986
2701
|
for (let i = messages.length - 1; i >= 0; i--) {
|
|
1987
2702
|
const normalized = normalizeMessage(messages[i]);
|
|
1988
2703
|
if (normalized.role === "assistant") {
|
|
1989
|
-
return structuredAssistantOutput(normalized);
|
|
2704
|
+
return flattenRootDisplay(structuredAssistantOutput(normalized));
|
|
1990
2705
|
}
|
|
1991
2706
|
}
|
|
1992
|
-
return structuredAssistantOutput(normalizeMessage(messages[messages.length - 1]));
|
|
2707
|
+
return flattenRootDisplay(structuredAssistantOutput(normalizeMessage(messages[messages.length - 1])));
|
|
1993
2708
|
}
|
|
1994
2709
|
if (output && typeof output === "object" && !Array.isArray(output)) {
|
|
1995
2710
|
const record = output;
|
|
@@ -1997,10 +2712,16 @@ var require_langchain = __commonJS({
|
|
|
1997
2712
|
const value = record[key];
|
|
1998
2713
|
if (typeof value === "string" && value)
|
|
1999
2714
|
return value;
|
|
2715
|
+
const fromBlocks = textFromContentBlocks(value);
|
|
2716
|
+
if (fromBlocks !== void 0)
|
|
2717
|
+
return fromBlocks;
|
|
2000
2718
|
if (value && typeof value === "object") {
|
|
2001
2719
|
const nested = value;
|
|
2002
2720
|
if (typeof nested.content === "string")
|
|
2003
2721
|
return nested.content;
|
|
2722
|
+
const nestedBlocks = textFromContentBlocks(nested.content);
|
|
2723
|
+
if (nestedBlocks !== void 0)
|
|
2724
|
+
return nestedBlocks;
|
|
2004
2725
|
}
|
|
2005
2726
|
}
|
|
2006
2727
|
}
|
|
@@ -2145,9 +2866,14 @@ var require_langchain = __commonJS({
|
|
|
2145
2866
|
}
|
|
2146
2867
|
return void 0;
|
|
2147
2868
|
}
|
|
2148
|
-
function llmProvider(serialized, extraParams) {
|
|
2869
|
+
function llmProvider(serialized, extraParams, metadata) {
|
|
2149
2870
|
const kwargs = serialized?.kwargs;
|
|
2150
|
-
const sources = [
|
|
2871
|
+
const sources = [
|
|
2872
|
+
kwargs,
|
|
2873
|
+
serialized,
|
|
2874
|
+
extraParams,
|
|
2875
|
+
metadata
|
|
2876
|
+
];
|
|
2151
2877
|
for (const source of sources) {
|
|
2152
2878
|
if (!source)
|
|
2153
2879
|
continue;
|
|
@@ -2178,9 +2904,22 @@ var require_langchain = __commonJS({
|
|
|
2178
2904
|
}
|
|
2179
2905
|
return void 0;
|
|
2180
2906
|
}
|
|
2907
|
+
function resolveGenerationIdentity(serialized, extraParams, metadata) {
|
|
2908
|
+
return {
|
|
2909
|
+
provider: llmProvider(serialized, extraParams, metadata),
|
|
2910
|
+
model: (0, model_1.pickModelIdentity)(serialized?.kwargs) ?? (0, model_1.pickModelIdentity)(serialized) ?? (0, model_1.pickModelIdentity)(extraParams) ?? (0, model_1.pickModelIdentity)(metadata)
|
|
2911
|
+
};
|
|
2912
|
+
}
|
|
2181
2913
|
function durationMs(start, end) {
|
|
2182
2914
|
return Math.max(0, end.getTime() - start.getTime());
|
|
2183
2915
|
}
|
|
2916
|
+
var NO_OUTPUT = { result: "none" };
|
|
2917
|
+
function recordedSpanOutput(output, ownsTrace, status) {
|
|
2918
|
+
if (output == null && !ownsTrace && status !== "ERROR") {
|
|
2919
|
+
return { ...NO_OUTPUT };
|
|
2920
|
+
}
|
|
2921
|
+
return output;
|
|
2922
|
+
}
|
|
2184
2923
|
function langchainAttributes(runId, parentRunId, runType) {
|
|
2185
2924
|
return Object.fromEntries(Object.entries({
|
|
2186
2925
|
...INTEGRATION_ATTRS,
|
|
@@ -2223,6 +2962,12 @@ var require_langchain = __commonJS({
|
|
|
2223
2962
|
var LemmaLangChainCallbackHandler = class {
|
|
2224
2963
|
options;
|
|
2225
2964
|
name = "lemma";
|
|
2965
|
+
raise_error = false;
|
|
2966
|
+
run_inline = false;
|
|
2967
|
+
ignore_llm = false;
|
|
2968
|
+
ignore_chain = false;
|
|
2969
|
+
ignore_agent = false;
|
|
2970
|
+
ignore_retriever = false;
|
|
2226
2971
|
lemma;
|
|
2227
2972
|
runs = /* @__PURE__ */ new Map();
|
|
2228
2973
|
traces = /* @__PURE__ */ new Map();
|
|
@@ -2243,7 +2988,13 @@ var require_langchain = __commonJS({
|
|
|
2243
2988
|
}
|
|
2244
2989
|
resolveThreadId(metadata, tags) {
|
|
2245
2990
|
const key = this.options.threadIdKey ?? "threadId";
|
|
2246
|
-
const keys = [
|
|
2991
|
+
const keys = [
|
|
2992
|
+
key,
|
|
2993
|
+
"threadId",
|
|
2994
|
+
"thread_id",
|
|
2995
|
+
"conversation_id",
|
|
2996
|
+
"session_id"
|
|
2997
|
+
];
|
|
2247
2998
|
return lookupString([metadata, this.options.metadata], keys) ?? tagValue(tags, keys);
|
|
2248
2999
|
}
|
|
2249
3000
|
resolveUserId(metadata, tags) {
|
|
@@ -2374,6 +3125,14 @@ var require_langchain = __commonJS({
|
|
|
2374
3125
|
this.runs.delete(runId);
|
|
2375
3126
|
}
|
|
2376
3127
|
}
|
|
3128
|
+
endStoredRun(run, options) {
|
|
3129
|
+
if (!run.handle)
|
|
3130
|
+
return;
|
|
3131
|
+
run.handle.end({
|
|
3132
|
+
...options,
|
|
3133
|
+
output: recordedSpanOutput(options.output, run.ownsTrace, options.status)
|
|
3134
|
+
});
|
|
3135
|
+
}
|
|
2377
3136
|
async finalizeTrace(owningTraceId, stored) {
|
|
2378
3137
|
this.traces.delete(owningTraceId);
|
|
2379
3138
|
this.forgetTraceRuns(owningTraceId);
|
|
@@ -2477,13 +3236,11 @@ var require_langchain = __commonJS({
|
|
|
2477
3236
|
return;
|
|
2478
3237
|
const endedAt = /* @__PURE__ */ new Date();
|
|
2479
3238
|
const stored = this.storedTrace(run.owningTraceId);
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
});
|
|
2486
|
-
}
|
|
3239
|
+
this.endStoredRun(run, {
|
|
3240
|
+
output: outputs,
|
|
3241
|
+
endedAt,
|
|
3242
|
+
durationMs: durationMs(run.startedAt, endedAt)
|
|
3243
|
+
});
|
|
2487
3244
|
if (stored) {
|
|
2488
3245
|
this.noteBounds(stored, run.startedAt, endedAt);
|
|
2489
3246
|
if (run.ownsTrace) {
|
|
@@ -2502,14 +3259,12 @@ var require_langchain = __commonJS({
|
|
|
2502
3259
|
const endedAt = /* @__PURE__ */ new Date();
|
|
2503
3260
|
const message = (0, error_message_1.describeError)(error);
|
|
2504
3261
|
const stored = this.storedTrace(run.owningTraceId);
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
});
|
|
2512
|
-
}
|
|
3262
|
+
this.endStoredRun(run, {
|
|
3263
|
+
status: "ERROR",
|
|
3264
|
+
error: message,
|
|
3265
|
+
endedAt,
|
|
3266
|
+
durationMs: durationMs(run.startedAt, endedAt)
|
|
3267
|
+
});
|
|
2513
3268
|
if (stored) {
|
|
2514
3269
|
this.noteBounds(stored, run.startedAt, endedAt);
|
|
2515
3270
|
if (run.ownsTrace) {
|
|
@@ -2521,7 +3276,7 @@ var require_langchain = __commonJS({
|
|
|
2521
3276
|
await this.finalizeTrace(run.owningTraceId, stored);
|
|
2522
3277
|
}
|
|
2523
3278
|
}
|
|
2524
|
-
handleLLMStart(serialized, prompts, runId, parentRunId, extraParams, tags, metadata) {
|
|
3279
|
+
handleLLMStart(serialized, prompts, runId, parentRunId, extraParams, tags, metadata, _runName) {
|
|
2525
3280
|
const startedAt = /* @__PURE__ */ new Date();
|
|
2526
3281
|
const attachment = this.resolveAttachment(runId, parentRunId, () => this.createOwnedTrace(runId, this.traceName(serialized, "langchain-llm"), prompts, "llm", metadata, tags));
|
|
2527
3282
|
if (attachment.ownsTrace) {
|
|
@@ -2529,8 +3284,7 @@ var require_langchain = __commonJS({
|
|
|
2529
3284
|
}
|
|
2530
3285
|
this.applyIdentity(attachment.stored, metadata, tags);
|
|
2531
3286
|
this.noteBounds(attachment.stored, startedAt, void 0);
|
|
2532
|
-
const provider =
|
|
2533
|
-
const model = modelName(serialized, extraParams);
|
|
3287
|
+
const { provider, model } = resolveGenerationIdentity(serialized, extraParams, metadata);
|
|
2534
3288
|
const handle = attachment.stored.handle.startGeneration({
|
|
2535
3289
|
name: serializedName(serialized, "langchain-llm"),
|
|
2536
3290
|
parentId: attachment.parentId,
|
|
@@ -2553,7 +3307,7 @@ var require_langchain = __commonJS({
|
|
|
2553
3307
|
ownsTrace: attachment.ownsTrace
|
|
2554
3308
|
});
|
|
2555
3309
|
}
|
|
2556
|
-
handleChatModelStart(serialized, messages, runId, parentRunId, extraParams, tags, metadata) {
|
|
3310
|
+
handleChatModelStart(serialized, messages, runId, parentRunId, extraParams, tags, metadata, _runName) {
|
|
2557
3311
|
const startedAt = /* @__PURE__ */ new Date();
|
|
2558
3312
|
const flatMessages = messages.flat();
|
|
2559
3313
|
const normalized = normalizeMessages(flatMessages);
|
|
@@ -2563,8 +3317,7 @@ var require_langchain = __commonJS({
|
|
|
2563
3317
|
}
|
|
2564
3318
|
this.applyIdentity(attachment.stored, metadata, tags);
|
|
2565
3319
|
this.noteBounds(attachment.stored, startedAt, void 0);
|
|
2566
|
-
const provider =
|
|
2567
|
-
const model = modelName(serialized, extraParams);
|
|
3320
|
+
const { provider, model } = resolveGenerationIdentity(serialized, extraParams, metadata);
|
|
2568
3321
|
const handle = attachment.stored.handle.startGeneration({
|
|
2569
3322
|
name: serializedName(serialized, "langchain-chat-model"),
|
|
2570
3323
|
parentId: attachment.parentId,
|
|
@@ -2604,14 +3357,16 @@ var require_langchain = __commonJS({
|
|
|
2604
3357
|
const outputMessages = llmOutputMessages(output);
|
|
2605
3358
|
const softError = (0, tool_result_1.toolResultError)(structured);
|
|
2606
3359
|
const awaitingTools = !softError && hasToolCalls(structured);
|
|
2607
|
-
|
|
3360
|
+
const model = (0, model_1.pickGenerationModelIdentity)(output);
|
|
3361
|
+
this.endStoredRun(run, {
|
|
2608
3362
|
output: softError ? void 0 : structured,
|
|
2609
3363
|
error: softError ?? void 0,
|
|
2610
3364
|
status: softError ? "ERROR" : void 0,
|
|
2611
3365
|
endedAt,
|
|
2612
3366
|
durationMs: durationMs(run.startedAt, endedAt),
|
|
2613
3367
|
llmOutputMessages: softError ? void 0 : outputMessages,
|
|
2614
|
-
usage: llmTokenUsage(output)
|
|
3368
|
+
usage: llmTokenUsage(output),
|
|
3369
|
+
...model ? { model } : {}
|
|
2615
3370
|
});
|
|
2616
3371
|
const stored = this.storedTrace(run.owningTraceId);
|
|
2617
3372
|
if (stored) {
|
|
@@ -2657,7 +3412,7 @@ var require_langchain = __commonJS({
|
|
|
2657
3412
|
return;
|
|
2658
3413
|
const endedAt = /* @__PURE__ */ new Date();
|
|
2659
3414
|
const message = (0, error_message_1.describeError)(error);
|
|
2660
|
-
|
|
3415
|
+
this.endStoredRun(run, {
|
|
2661
3416
|
status: "ERROR",
|
|
2662
3417
|
error: message,
|
|
2663
3418
|
endedAt,
|
|
@@ -2676,7 +3431,7 @@ var require_langchain = __commonJS({
|
|
|
2676
3431
|
}
|
|
2677
3432
|
void this.finalizeDeferredOwner(run.owningTraceId, endedAt, message);
|
|
2678
3433
|
}
|
|
2679
|
-
handleToolStart(serialized, input, runId, parentRunId, tags, metadata) {
|
|
3434
|
+
handleToolStart(serialized, input, runId, parentRunId, tags, metadata, _runName, _toolCallId) {
|
|
2680
3435
|
const startedAt = /* @__PURE__ */ new Date();
|
|
2681
3436
|
const attachment = this.resolveAttachment(runId, parentRunId, () => this.createOwnedTrace(runId, this.traceName(serialized, "langchain-tool"), input, "tool", metadata, tags));
|
|
2682
3437
|
if (attachment.ownsTrace) {
|
|
@@ -2711,14 +3466,14 @@ var require_langchain = __commonJS({
|
|
|
2711
3466
|
const endedAt = /* @__PURE__ */ new Date();
|
|
2712
3467
|
const softError = (0, tool_result_1.toolResultError)(output);
|
|
2713
3468
|
if (softError) {
|
|
2714
|
-
|
|
3469
|
+
this.endStoredRun(run, {
|
|
2715
3470
|
status: "ERROR",
|
|
2716
3471
|
error: softError,
|
|
2717
3472
|
endedAt,
|
|
2718
3473
|
durationMs: durationMs(run.startedAt, endedAt)
|
|
2719
3474
|
});
|
|
2720
3475
|
} else {
|
|
2721
|
-
|
|
3476
|
+
this.endStoredRun(run, {
|
|
2722
3477
|
output,
|
|
2723
3478
|
endedAt,
|
|
2724
3479
|
durationMs: durationMs(run.startedAt, endedAt)
|
|
@@ -2743,7 +3498,7 @@ var require_langchain = __commonJS({
|
|
|
2743
3498
|
return;
|
|
2744
3499
|
const endedAt = /* @__PURE__ */ new Date();
|
|
2745
3500
|
const message = (0, error_message_1.describeError)(error);
|
|
2746
|
-
|
|
3501
|
+
this.endStoredRun(run, {
|
|
2747
3502
|
status: "ERROR",
|
|
2748
3503
|
error: message,
|
|
2749
3504
|
endedAt,
|
|
@@ -2762,7 +3517,7 @@ var require_langchain = __commonJS({
|
|
|
2762
3517
|
}
|
|
2763
3518
|
void this.finalizeDeferredOwner(run.owningTraceId, endedAt, message);
|
|
2764
3519
|
}
|
|
2765
|
-
handleRetrieverStart(serialized, query, runId, parentRunId, tags, metadata) {
|
|
3520
|
+
handleRetrieverStart(serialized, query, runId, parentRunId, tags, metadata, _name) {
|
|
2766
3521
|
const startedAt = /* @__PURE__ */ new Date();
|
|
2767
3522
|
const attachment = this.resolveAttachment(runId, parentRunId, () => this.createOwnedTrace(runId, this.traceName(serialized, "langchain-retriever"), query, "retriever", metadata, tags));
|
|
2768
3523
|
if (attachment.ownsTrace) {
|
|
@@ -2793,7 +3548,7 @@ var require_langchain = __commonJS({
|
|
|
2793
3548
|
if (!run)
|
|
2794
3549
|
return;
|
|
2795
3550
|
const endedAt = /* @__PURE__ */ new Date();
|
|
2796
|
-
|
|
3551
|
+
this.endStoredRun(run, {
|
|
2797
3552
|
output: documents,
|
|
2798
3553
|
endedAt,
|
|
2799
3554
|
durationMs: durationMs(run.startedAt, endedAt)
|
|
@@ -2813,7 +3568,7 @@ var require_langchain = __commonJS({
|
|
|
2813
3568
|
return;
|
|
2814
3569
|
const endedAt = /* @__PURE__ */ new Date();
|
|
2815
3570
|
const message = (0, error_message_1.describeError)(error);
|
|
2816
|
-
|
|
3571
|
+
this.endStoredRun(run, {
|
|
2817
3572
|
status: "ERROR",
|
|
2818
3573
|
error: message,
|
|
2819
3574
|
endedAt,
|
|
@@ -2863,6 +3618,7 @@ var require_mastra = __commonJS({
|
|
|
2863
3618
|
var client_1 = require_client();
|
|
2864
3619
|
var error_message_1 = require_error_message();
|
|
2865
3620
|
var schedule_1 = require_schedule();
|
|
3621
|
+
var model_1 = require_model();
|
|
2866
3622
|
var tool_result_1 = require_tool_result();
|
|
2867
3623
|
var usage_1 = require_usage();
|
|
2868
3624
|
var INTEGRATION_ATTRS = {
|
|
@@ -3113,7 +3869,7 @@ var require_mastra = __commonJS({
|
|
|
3113
3869
|
trace.recordGeneration({
|
|
3114
3870
|
...base,
|
|
3115
3871
|
name: this.options.generationName ?? span.name,
|
|
3116
|
-
model:
|
|
3872
|
+
model: (0, model_1.pickModelIdentity)(attrs) ?? (0, model_1.pickModelIdentity)(span.output),
|
|
3117
3873
|
llmProvider: attributeString(attrs, "provider"),
|
|
3118
3874
|
llmInvocationParameters: attrs?.parameters,
|
|
3119
3875
|
llmInputMessages,
|
|
@@ -3222,6 +3978,231 @@ var require_mastra = __commonJS({
|
|
|
3222
3978
|
}
|
|
3223
3979
|
});
|
|
3224
3980
|
|
|
3981
|
+
// ../../packages/ts/tracing/dist/openai-agents-generation.js
|
|
3982
|
+
var require_openai_agents_generation = __commonJS({
|
|
3983
|
+
"../../packages/ts/tracing/dist/openai-agents-generation.js"(exports) {
|
|
3984
|
+
"use strict";
|
|
3985
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3986
|
+
exports.asResponseRecord = asResponseRecord;
|
|
3987
|
+
exports.responseRecord = responseRecord;
|
|
3988
|
+
exports.spanDataForAttribute = spanDataForAttribute;
|
|
3989
|
+
exports.generationSnapshot = generationSnapshot;
|
|
3990
|
+
exports.replyFromResponse = replyFromResponse;
|
|
3991
|
+
var usage_1 = require_usage();
|
|
3992
|
+
var RESPONSE_SETTING_KEYS = [
|
|
3993
|
+
"temperature",
|
|
3994
|
+
"top_p",
|
|
3995
|
+
"max_output_tokens",
|
|
3996
|
+
"max_tool_calls",
|
|
3997
|
+
"tool_choice",
|
|
3998
|
+
"parallel_tool_calls",
|
|
3999
|
+
"truncation",
|
|
4000
|
+
"reasoning",
|
|
4001
|
+
"text",
|
|
4002
|
+
"service_tier"
|
|
4003
|
+
];
|
|
4004
|
+
var HISTORY_ITEM_ROLES = {
|
|
4005
|
+
function_call: "assistant",
|
|
4006
|
+
reasoning: "assistant",
|
|
4007
|
+
function_call_result: "tool",
|
|
4008
|
+
function_call_output: "tool"
|
|
4009
|
+
};
|
|
4010
|
+
function asResponseRecord(value) {
|
|
4011
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
4012
|
+
return void 0;
|
|
4013
|
+
}
|
|
4014
|
+
const toJSON = value.toJSON;
|
|
4015
|
+
if (typeof toJSON === "function") {
|
|
4016
|
+
try {
|
|
4017
|
+
const dumped = toJSON.call(value);
|
|
4018
|
+
if (dumped && typeof dumped === "object" && !Array.isArray(dumped)) {
|
|
4019
|
+
return { ...dumped };
|
|
4020
|
+
}
|
|
4021
|
+
} catch {
|
|
4022
|
+
}
|
|
4023
|
+
}
|
|
4024
|
+
return { ...value };
|
|
4025
|
+
}
|
|
4026
|
+
function responseRecord(data) {
|
|
4027
|
+
return asResponseRecord(data["response"] ?? data["_response"]);
|
|
4028
|
+
}
|
|
4029
|
+
var RESPONSE_BODY_KEYS = [
|
|
4030
|
+
"output",
|
|
4031
|
+
"output_text",
|
|
4032
|
+
"instructions",
|
|
4033
|
+
"input"
|
|
4034
|
+
];
|
|
4035
|
+
function spanDataForAttribute(data) {
|
|
4036
|
+
const copy = { ...data };
|
|
4037
|
+
delete copy._input;
|
|
4038
|
+
if (copy.type === "response") {
|
|
4039
|
+
for (const key of RESPONSE_BODY_KEYS)
|
|
4040
|
+
delete copy[key];
|
|
4041
|
+
}
|
|
4042
|
+
for (const key of ["response", "_response"]) {
|
|
4043
|
+
if (!(key in copy))
|
|
4044
|
+
continue;
|
|
4045
|
+
const response = asResponseRecord(copy[key]);
|
|
4046
|
+
if (!response)
|
|
4047
|
+
continue;
|
|
4048
|
+
for (const bodyKey of RESPONSE_BODY_KEYS)
|
|
4049
|
+
delete response[bodyKey];
|
|
4050
|
+
copy[key] = response;
|
|
4051
|
+
}
|
|
4052
|
+
return copy;
|
|
4053
|
+
}
|
|
4054
|
+
function instructionsText(value) {
|
|
4055
|
+
return typeof value === "string" && value ? value : void 0;
|
|
4056
|
+
}
|
|
4057
|
+
function isSameSystemMessage(message, instructions) {
|
|
4058
|
+
if (!message || typeof message !== "object" || Array.isArray(message)) {
|
|
4059
|
+
return false;
|
|
4060
|
+
}
|
|
4061
|
+
const record = message;
|
|
4062
|
+
return record.role === "system" && record.content === instructions;
|
|
4063
|
+
}
|
|
4064
|
+
function withHistoryRole(message) {
|
|
4065
|
+
if (!message || typeof message !== "object" || Array.isArray(message)) {
|
|
4066
|
+
return message;
|
|
4067
|
+
}
|
|
4068
|
+
const record = message;
|
|
4069
|
+
if (typeof record.role === "string" && record.role)
|
|
4070
|
+
return message;
|
|
4071
|
+
const role = typeof record.type === "string" ? HISTORY_ITEM_ROLES[record.type] : void 0;
|
|
4072
|
+
if (!role)
|
|
4073
|
+
return message;
|
|
4074
|
+
return { ...record, role };
|
|
4075
|
+
}
|
|
4076
|
+
function historyMessages(messages) {
|
|
4077
|
+
return messages.map(withHistoryRole);
|
|
4078
|
+
}
|
|
4079
|
+
function inputMessages(input, response) {
|
|
4080
|
+
const instructions = instructionsText(response?.instructions);
|
|
4081
|
+
const messages = Array.isArray(input) ? input : void 0;
|
|
4082
|
+
if (!instructions)
|
|
4083
|
+
return messages ? historyMessages(messages) : void 0;
|
|
4084
|
+
if (messages?.some((message) => isSameSystemMessage(message, instructions))) {
|
|
4085
|
+
return historyMessages(messages);
|
|
4086
|
+
}
|
|
4087
|
+
const system = { role: "system", content: instructions };
|
|
4088
|
+
if (!messages) {
|
|
4089
|
+
if (typeof input === "string" && input) {
|
|
4090
|
+
return historyMessages([system, { role: "user", content: input }]);
|
|
4091
|
+
}
|
|
4092
|
+
return historyMessages([system]);
|
|
4093
|
+
}
|
|
4094
|
+
return historyMessages([system, ...messages]);
|
|
4095
|
+
}
|
|
4096
|
+
function generationTools(data, response) {
|
|
4097
|
+
if (response && response.tools != null)
|
|
4098
|
+
return response.tools;
|
|
4099
|
+
if (data["tools"] != null)
|
|
4100
|
+
return data["tools"];
|
|
4101
|
+
return void 0;
|
|
4102
|
+
}
|
|
4103
|
+
function invocationParameters(data, response) {
|
|
4104
|
+
if (data["model_config"] != null)
|
|
4105
|
+
return data["model_config"];
|
|
4106
|
+
if (!response)
|
|
4107
|
+
return void 0;
|
|
4108
|
+
const settings = {};
|
|
4109
|
+
for (const key of RESPONSE_SETTING_KEYS) {
|
|
4110
|
+
const value = response[key];
|
|
4111
|
+
if (value != null)
|
|
4112
|
+
settings[key] = value;
|
|
4113
|
+
}
|
|
4114
|
+
return Object.keys(settings).length > 0 ? settings : void 0;
|
|
4115
|
+
}
|
|
4116
|
+
function outputMessages(data, response) {
|
|
4117
|
+
if (data.type !== "generation" && data.type !== "response")
|
|
4118
|
+
return void 0;
|
|
4119
|
+
if (data.type === "generation" && Array.isArray(data["output"])) {
|
|
4120
|
+
return data["output"];
|
|
4121
|
+
}
|
|
4122
|
+
if (response && Array.isArray(response.output)) {
|
|
4123
|
+
return response.output;
|
|
4124
|
+
}
|
|
4125
|
+
if (Array.isArray(data["output"]))
|
|
4126
|
+
return data["output"];
|
|
4127
|
+
return void 0;
|
|
4128
|
+
}
|
|
4129
|
+
function usageFor(data, response) {
|
|
4130
|
+
const direct = (0, usage_1.normalizeTokenUsage)(data["usage"]);
|
|
4131
|
+
if (direct)
|
|
4132
|
+
return direct;
|
|
4133
|
+
if (!response)
|
|
4134
|
+
return void 0;
|
|
4135
|
+
return (0, usage_1.normalizeTokenUsage)(response.usage);
|
|
4136
|
+
}
|
|
4137
|
+
function generationSnapshot(data, input) {
|
|
4138
|
+
const response = responseRecord(data);
|
|
4139
|
+
const messages = inputMessages(input, response);
|
|
4140
|
+
const parameters = invocationParameters(data, response);
|
|
4141
|
+
const tools = generationTools(data, response);
|
|
4142
|
+
return {
|
|
4143
|
+
response,
|
|
4144
|
+
fields: {
|
|
4145
|
+
...messages ? { llmInputMessages: messages } : {},
|
|
4146
|
+
...parameters !== void 0 ? { llmInvocationParameters: parameters } : {},
|
|
4147
|
+
...tools !== void 0 ? { llmTools: tools } : {}
|
|
4148
|
+
},
|
|
4149
|
+
outputMessages: outputMessages(data, response),
|
|
4150
|
+
usage: usageFor(data, response)
|
|
4151
|
+
};
|
|
4152
|
+
}
|
|
4153
|
+
function replyFromResponse(data, response, textFromList) {
|
|
4154
|
+
if (!response) {
|
|
4155
|
+
const raw = data["response"] ?? data["_response"];
|
|
4156
|
+
if (raw == null)
|
|
4157
|
+
return textFromList(data["output"]);
|
|
4158
|
+
return raw;
|
|
4159
|
+
}
|
|
4160
|
+
if (typeof response.output_text === "string")
|
|
4161
|
+
return response.output_text;
|
|
4162
|
+
if (Array.isArray(response.output))
|
|
4163
|
+
return textFromList(response.output);
|
|
4164
|
+
return response;
|
|
4165
|
+
}
|
|
4166
|
+
}
|
|
4167
|
+
});
|
|
4168
|
+
|
|
4169
|
+
// ../../packages/ts/tracing/dist/openai-agents-containers.js
|
|
4170
|
+
var require_openai_agents_containers = __commonJS({
|
|
4171
|
+
"../../packages/ts/tracing/dist/openai-agents-containers.js"(exports) {
|
|
4172
|
+
"use strict";
|
|
4173
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4174
|
+
exports.isContainerType = isContainerType;
|
|
4175
|
+
exports.propagateGenerationIo = propagateGenerationIo;
|
|
4176
|
+
function isContainerType(type) {
|
|
4177
|
+
return type === "agent" || type === "task" || type === "turn";
|
|
4178
|
+
}
|
|
4179
|
+
function propagateGenerationIo(parentId, io, open, ended) {
|
|
4180
|
+
if (io.input === void 0 && io.output === void 0)
|
|
4181
|
+
return;
|
|
4182
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4183
|
+
let id = parentId ?? null;
|
|
4184
|
+
while (id && !seen.has(id)) {
|
|
4185
|
+
seen.add(id);
|
|
4186
|
+
const endedRecord = ended.get(id);
|
|
4187
|
+
const record = endedRecord ?? open.get(id);
|
|
4188
|
+
if (!record)
|
|
4189
|
+
return;
|
|
4190
|
+
if (isContainerType(record.type)) {
|
|
4191
|
+
if (endedRecord) {
|
|
4192
|
+
endedRecord.handle.fillContainerIo(io.input, io.output, endedRecord.ownOutput);
|
|
4193
|
+
} else {
|
|
4194
|
+
if (record.childInput === void 0)
|
|
4195
|
+
record.childInput = io.input;
|
|
4196
|
+
if (io.output !== void 0)
|
|
4197
|
+
record.childOutput = io.output;
|
|
4198
|
+
}
|
|
4199
|
+
}
|
|
4200
|
+
id = record.parentId ?? null;
|
|
4201
|
+
}
|
|
4202
|
+
}
|
|
4203
|
+
}
|
|
4204
|
+
});
|
|
4205
|
+
|
|
3225
4206
|
// ../../packages/ts/tracing/dist/openai-agents.js
|
|
3226
4207
|
var require_openai_agents = __commonJS({
|
|
3227
4208
|
"../../packages/ts/tracing/dist/openai-agents.js"(exports) {
|
|
@@ -3232,7 +4213,10 @@ var require_openai_agents = __commonJS({
|
|
|
3232
4213
|
var error_message_1 = require_error_message();
|
|
3233
4214
|
var schedule_1 = require_schedule();
|
|
3234
4215
|
var tool_result_1 = require_tool_result();
|
|
3235
|
-
var
|
|
4216
|
+
var openai_agents_generation_1 = require_openai_agents_generation();
|
|
4217
|
+
var openai_agents_containers_1 = require_openai_agents_containers();
|
|
4218
|
+
var model_1 = require_model();
|
|
4219
|
+
var reply_text_1 = require_reply_text();
|
|
3236
4220
|
var INTEGRATION_ATTRS = {
|
|
3237
4221
|
"lemma.sdk.integration": "openai-agents"
|
|
3238
4222
|
};
|
|
@@ -3280,37 +4264,6 @@ var require_openai_agents = __commonJS({
|
|
|
3280
4264
|
}
|
|
3281
4265
|
return messageContent(input[input.length - 1]);
|
|
3282
4266
|
}
|
|
3283
|
-
function textFromGenerationOutput(output) {
|
|
3284
|
-
if (!Array.isArray(output))
|
|
3285
|
-
return output;
|
|
3286
|
-
const text = output.map((item) => {
|
|
3287
|
-
if (!item || typeof item !== "object")
|
|
3288
|
-
return "";
|
|
3289
|
-
const record = item;
|
|
3290
|
-
if (typeof record["text"] === "string")
|
|
3291
|
-
return record["text"];
|
|
3292
|
-
if (typeof record["content"] === "string")
|
|
3293
|
-
return record["content"];
|
|
3294
|
-
return "";
|
|
3295
|
-
}).join("");
|
|
3296
|
-
return text || output;
|
|
3297
|
-
}
|
|
3298
|
-
function responseOutput(data) {
|
|
3299
|
-
const response = data["response"] ?? data["_response"];
|
|
3300
|
-
if (response == null) {
|
|
3301
|
-
return textFromGenerationOutput(data["output"]);
|
|
3302
|
-
}
|
|
3303
|
-
if (typeof response === "object" && response !== null) {
|
|
3304
|
-
const record = response;
|
|
3305
|
-
if (typeof record.output_text === "string")
|
|
3306
|
-
return record.output_text;
|
|
3307
|
-
if (Array.isArray(record.output)) {
|
|
3308
|
-
return textFromGenerationOutput(record.output);
|
|
3309
|
-
}
|
|
3310
|
-
return response;
|
|
3311
|
-
}
|
|
3312
|
-
return response;
|
|
3313
|
-
}
|
|
3314
4267
|
function spanName(data) {
|
|
3315
4268
|
if (typeof data["name"] === "string" && data["name"]) {
|
|
3316
4269
|
return data["name"];
|
|
@@ -3343,22 +4296,9 @@ var require_openai_agents = __commonJS({
|
|
|
3343
4296
|
"openai.agents.parent_id": span.parentId,
|
|
3344
4297
|
"openai.agents.span_type": span.spanData.type,
|
|
3345
4298
|
"openai.agents.trace_metadata": span.traceMetadata ? JSON.stringify(span.traceMetadata) : void 0,
|
|
3346
|
-
"openai.agents.span_data": JSON.stringify(span.spanData)
|
|
4299
|
+
"openai.agents.span_data": JSON.stringify((0, openai_agents_generation_1.spanDataForAttribute)(span.spanData))
|
|
3347
4300
|
}).filter(([, value]) => value !== void 0 && value !== null));
|
|
3348
4301
|
}
|
|
3349
|
-
function generationUsage(data) {
|
|
3350
|
-
const direct = (0, usage_1.normalizeTokenUsage)(data["usage"]);
|
|
3351
|
-
if (direct)
|
|
3352
|
-
return direct;
|
|
3353
|
-
const response = data["response"] ?? data["_response"];
|
|
3354
|
-
if (response && typeof response === "object") {
|
|
3355
|
-
const record = response;
|
|
3356
|
-
const fromResponse = (0, usage_1.normalizeTokenUsage)(record.usage);
|
|
3357
|
-
if (fromResponse)
|
|
3358
|
-
return fromResponse;
|
|
3359
|
-
}
|
|
3360
|
-
return void 0;
|
|
3361
|
-
}
|
|
3362
4302
|
function coerceDate(value) {
|
|
3363
4303
|
if (value == null)
|
|
3364
4304
|
return void 0;
|
|
@@ -3478,6 +4418,15 @@ var require_openai_agents = __commonJS({
|
|
|
3478
4418
|
traces.set(trace.traceId, stored);
|
|
3479
4419
|
return stored;
|
|
3480
4420
|
}
|
|
4421
|
+
function blankRecord(span, handle) {
|
|
4422
|
+
return {
|
|
4423
|
+
handle,
|
|
4424
|
+
traceId: span.traceId,
|
|
4425
|
+
type: span.spanData.type,
|
|
4426
|
+
parentId: span.parentId,
|
|
4427
|
+
ownOutput: false
|
|
4428
|
+
};
|
|
4429
|
+
}
|
|
3481
4430
|
function startSpan(span) {
|
|
3482
4431
|
const trace = traces.get(span.traceId);
|
|
3483
4432
|
if (!trace)
|
|
@@ -3502,10 +4451,8 @@ var require_openai_agents = __commonJS({
|
|
|
3502
4451
|
if (isGenerationType(data.type)) {
|
|
3503
4452
|
return trace.handle.startGeneration({
|
|
3504
4453
|
...base,
|
|
3505
|
-
model:
|
|
3506
|
-
llmProvider: "openai"
|
|
3507
|
-
llmInputMessages: Array.isArray(input) ? input : void 0,
|
|
3508
|
-
llmInvocationParameters: data["model_config"]
|
|
4454
|
+
model: (0, model_1.pickModelIdentity)(data),
|
|
4455
|
+
llmProvider: "openai"
|
|
3509
4456
|
});
|
|
3510
4457
|
}
|
|
3511
4458
|
if (data.type === "function") {
|
|
@@ -3518,9 +4465,11 @@ var require_openai_agents = __commonJS({
|
|
|
3518
4465
|
}
|
|
3519
4466
|
function endSpan(span) {
|
|
3520
4467
|
const storedTrace = traces.get(span.traceId);
|
|
3521
|
-
const
|
|
4468
|
+
const existing = spans.get(span.spanId);
|
|
4469
|
+
const handle = existing?.handle ?? startSpan(span);
|
|
3522
4470
|
if (!handle || !storedTrace)
|
|
3523
4471
|
return;
|
|
4472
|
+
const record = existing ?? blankRecord(span, handle);
|
|
3524
4473
|
spans.delete(span.spanId);
|
|
3525
4474
|
applyIdentity(storedTrace, void 0, span.traceMetadata);
|
|
3526
4475
|
const data = span.spanData;
|
|
@@ -3528,11 +4477,12 @@ var require_openai_agents = __commonJS({
|
|
|
3528
4477
|
if (isGenerationType(data.type)) {
|
|
3529
4478
|
noteRootInput(storedTrace, input);
|
|
3530
4479
|
}
|
|
4480
|
+
const snapshot = isGenerationType(data.type) ? (0, openai_agents_generation_1.generationSnapshot)(data, input) : void 0;
|
|
3531
4481
|
let rawOutput;
|
|
3532
4482
|
if (data.type === "generation") {
|
|
3533
|
-
rawOutput = textFromGenerationOutput(data["output"]);
|
|
4483
|
+
rawOutput = (0, reply_text_1.textFromGenerationOutput)(data["output"]);
|
|
3534
4484
|
} else if (data.type === "response") {
|
|
3535
|
-
rawOutput =
|
|
4485
|
+
rawOutput = (0, openai_agents_generation_1.replyFromResponse)(data, snapshot?.response, reply_text_1.textFromGenerationOutput);
|
|
3536
4486
|
} else {
|
|
3537
4487
|
rawOutput = parseMaybeJson(data["output"] ?? data["_response"]);
|
|
3538
4488
|
}
|
|
@@ -3549,18 +4499,41 @@ var require_openai_agents = __commonJS({
|
|
|
3549
4499
|
if (!errorMessage && isGenerationType(data.type) && parsedOutput != null) {
|
|
3550
4500
|
noteRootOutput(storedTrace, parsedOutput);
|
|
3551
4501
|
}
|
|
4502
|
+
let endedInput = input;
|
|
4503
|
+
let endedOutput = parsedOutput;
|
|
4504
|
+
if ((0, openai_agents_containers_1.isContainerType)(data.type)) {
|
|
4505
|
+
record.ownOutput = data["output"] != null;
|
|
4506
|
+
if (endedInput == null)
|
|
4507
|
+
endedInput = record.childInput;
|
|
4508
|
+
if (!record.ownOutput && !errorMessage && record.childOutput !== void 0) {
|
|
4509
|
+
endedOutput = record.childOutput;
|
|
4510
|
+
}
|
|
4511
|
+
}
|
|
4512
|
+
const messages = snapshot?.outputMessages;
|
|
3552
4513
|
handle.end({
|
|
4514
|
+
// The Agents SDK fills spanData.input after onSpanStart (tool arguments,
|
|
4515
|
+
// generation messages), so re-read it here. Skip an absent input so it
|
|
4516
|
+
// can't erase one captured at start.
|
|
4517
|
+
...endedInput != null ? { input: endedInput } : {},
|
|
4518
|
+
...snapshot ? snapshot.fields : {},
|
|
4519
|
+
attributes: openAIAttributes(span),
|
|
3553
4520
|
// Failures must not invent an output — record error instead.
|
|
3554
|
-
output:
|
|
4521
|
+
output: endedOutput,
|
|
3555
4522
|
error: errorMessage,
|
|
3556
4523
|
status: errorMessage ? "ERROR" : void 0,
|
|
3557
|
-
model:
|
|
4524
|
+
model: (0, model_1.pickModelIdentity)(data),
|
|
3558
4525
|
endedAt: spanEndedAt,
|
|
3559
4526
|
durationMs: durationMs(spanStartedAt, spanEndedAt),
|
|
3560
|
-
|
|
3561
|
-
usage:
|
|
4527
|
+
...errorMessage || !messages ? {} : { llmOutputMessages: messages },
|
|
4528
|
+
usage: snapshot?.usage
|
|
3562
4529
|
});
|
|
3563
|
-
endedSpans.set(span.spanId,
|
|
4530
|
+
endedSpans.set(span.spanId, record);
|
|
4531
|
+
if (isGenerationType(data.type)) {
|
|
4532
|
+
(0, openai_agents_containers_1.propagateGenerationIo)(record.parentId, {
|
|
4533
|
+
input: input ?? void 0,
|
|
4534
|
+
output: parsedOutput
|
|
4535
|
+
}, spans, endedSpans);
|
|
4536
|
+
}
|
|
3564
4537
|
if (span.parentId && data.type === "function") {
|
|
3565
4538
|
const parent = spans.get(span.parentId)?.handle ?? endedSpans.get(span.parentId)?.handle;
|
|
3566
4539
|
parent?.ensureEndedAt(spanEndedAt);
|
|
@@ -3629,7 +4602,7 @@ var require_openai_agents = __commonJS({
|
|
|
3629
4602
|
return;
|
|
3630
4603
|
const handle = startSpan(span);
|
|
3631
4604
|
if (handle)
|
|
3632
|
-
spans.set(span.spanId,
|
|
4605
|
+
spans.set(span.spanId, blankRecord(span, handle));
|
|
3633
4606
|
},
|
|
3634
4607
|
onSpanEnd: async (span) => {
|
|
3635
4608
|
if (!traces.has(span.traceId))
|
|
@@ -3750,12 +4723,58 @@ var require_vercel_ai = __commonJS({
|
|
|
3750
4723
|
}
|
|
3751
4724
|
return void 0;
|
|
3752
4725
|
}
|
|
3753
|
-
function
|
|
3754
|
-
if (
|
|
3755
|
-
return
|
|
4726
|
+
function instructionText(value) {
|
|
4727
|
+
if (typeof value === "string" && value)
|
|
4728
|
+
return value;
|
|
4729
|
+
if (Array.isArray(value)) {
|
|
4730
|
+
const parts = value.map((part) => instructionText(part)).filter((part) => Boolean(part));
|
|
4731
|
+
return parts.length > 0 ? parts.join("\n") : void 0;
|
|
4732
|
+
}
|
|
4733
|
+
if (!value || typeof value !== "object")
|
|
4734
|
+
return void 0;
|
|
4735
|
+
const content = value.content;
|
|
4736
|
+
return typeof content === "string" && content ? content : void 0;
|
|
4737
|
+
}
|
|
4738
|
+
function siblingSystem(event) {
|
|
4739
|
+
if (!event || typeof event !== "object")
|
|
4740
|
+
return void 0;
|
|
4741
|
+
const record = event;
|
|
4742
|
+
return instructionText(record.instructions) ?? instructionText(record.system);
|
|
4743
|
+
}
|
|
4744
|
+
function firstMessageRole(messages) {
|
|
4745
|
+
const first = messages[0];
|
|
4746
|
+
if (!first || typeof first !== "object")
|
|
4747
|
+
return void 0;
|
|
4748
|
+
return first.role;
|
|
4749
|
+
}
|
|
4750
|
+
function firstMessageIsSystem(messages, system) {
|
|
4751
|
+
const first = messages[0];
|
|
4752
|
+
return Boolean(first && typeof first === "object" && first.role === "system" && first.content === system);
|
|
4753
|
+
}
|
|
4754
|
+
function resolveSystemForMessages(event, messages, fallbackSystem) {
|
|
4755
|
+
const sibling = siblingSystem(event);
|
|
4756
|
+
if (sibling)
|
|
4757
|
+
return sibling;
|
|
4758
|
+
if (Array.isArray(messages) && firstMessageRole(messages) === "system") {
|
|
4759
|
+
return void 0;
|
|
4760
|
+
}
|
|
4761
|
+
return fallbackSystem;
|
|
4762
|
+
}
|
|
4763
|
+
function withSystemMessage(system, messages) {
|
|
4764
|
+
if (!Array.isArray(messages))
|
|
4765
|
+
return void 0;
|
|
4766
|
+
if (!system || firstMessageIsSystem(messages, system))
|
|
4767
|
+
return messages;
|
|
4768
|
+
return [{ role: "system", content: system }, ...messages];
|
|
4769
|
+
}
|
|
4770
|
+
function v6NormalizedMessages(event, fallbackSystem) {
|
|
4771
|
+
const system = resolveSystemForMessages(event, "messages" in event ? event.messages : void 0, fallbackSystem);
|
|
4772
|
+
if (Array.isArray(event.messages)) {
|
|
4773
|
+
return withSystemMessage(system, event.messages);
|
|
4774
|
+
}
|
|
3756
4775
|
const messages = [];
|
|
3757
|
-
if (
|
|
3758
|
-
messages.push({ role: "system", content:
|
|
4776
|
+
if (system) {
|
|
4777
|
+
messages.push({ role: "system", content: system });
|
|
3759
4778
|
}
|
|
3760
4779
|
if ("prompt" in event && event.prompt !== void 0) {
|
|
3761
4780
|
if (Array.isArray(event.prompt)) {
|
|
@@ -3767,8 +4786,8 @@ var require_vercel_ai = __commonJS({
|
|
|
3767
4786
|
}
|
|
3768
4787
|
return messages.length > 0 ? messages : void 0;
|
|
3769
4788
|
}
|
|
3770
|
-
function v6Input(event) {
|
|
3771
|
-
const normalized = v6NormalizedMessages(event);
|
|
4789
|
+
function v6Input(event, fallbackSystem) {
|
|
4790
|
+
const normalized = v6NormalizedMessages(event, fallbackSystem);
|
|
3772
4791
|
if (normalized)
|
|
3773
4792
|
return normalized;
|
|
3774
4793
|
if ("prompt" in event)
|
|
@@ -4062,9 +5081,10 @@ var require_vercel_ai = __commonJS({
|
|
|
4062
5081
|
const name = typeof options.generationName === "function" ? options.generationName(event) : options.generationName ?? "vercel-ai-generation";
|
|
4063
5082
|
const output = v6Output(event);
|
|
4064
5083
|
const generationError = "error" in event && event.error != null ? (0, error_message_1.describeError)(event.error) : void 0;
|
|
5084
|
+
const fallbackSystem = siblingSystem(v6Starts.at(-1)?.event);
|
|
4065
5085
|
const generation = {
|
|
4066
5086
|
name,
|
|
4067
|
-
input: stored?.event && v6Input(stored.event),
|
|
5087
|
+
input: stored?.event && v6Input(stored.event, fallbackSystem),
|
|
4068
5088
|
output: generationError ? void 0 : output,
|
|
4069
5089
|
metadata: options.metadata,
|
|
4070
5090
|
attributes: withIntegrationAttrs(),
|
|
@@ -4073,7 +5093,7 @@ var require_vercel_ai = __commonJS({
|
|
|
4073
5093
|
endedAt,
|
|
4074
5094
|
durationMs,
|
|
4075
5095
|
llmProvider: event.model.provider,
|
|
4076
|
-
llmInputMessages: stored?.event ? v6NormalizedMessages(stored.event) : void 0,
|
|
5096
|
+
llmInputMessages: stored?.event ? v6NormalizedMessages(stored.event, fallbackSystem) : void 0,
|
|
4077
5097
|
llmOutputMessages: output === void 0 || generationError ? void 0 : [{ role: "assistant", content: output }],
|
|
4078
5098
|
llmTools: stored?.event.tools,
|
|
4079
5099
|
status: generationError ? "ERROR" : void 0,
|
|
@@ -4111,15 +5131,16 @@ var require_vercel_ai = __commonJS({
|
|
|
4111
5131
|
performance: {}
|
|
4112
5132
|
}) : options.generationName ?? "vercel-ai-generation";
|
|
4113
5133
|
const startedAt = /* @__PURE__ */ new Date();
|
|
5134
|
+
const messages = withSystemMessage(resolveSystemForMessages(event, event.messages, siblingSystem(v6Starts.at(-1)?.event)), event.messages);
|
|
4114
5135
|
const handle = trace.startGeneration({
|
|
4115
5136
|
name,
|
|
4116
|
-
input:
|
|
5137
|
+
input: messages,
|
|
4117
5138
|
metadata: options.metadata,
|
|
4118
5139
|
attributes: withIntegrationAttrs(),
|
|
4119
5140
|
model: event.modelId,
|
|
4120
5141
|
startedAt,
|
|
4121
5142
|
llmProvider: event.provider,
|
|
4122
|
-
llmInputMessages:
|
|
5143
|
+
llmInputMessages: messages,
|
|
4123
5144
|
llmTools: event.tools
|
|
4124
5145
|
});
|
|
4125
5146
|
const stored = { event, startedAt, handle };
|
|
@@ -4137,7 +5158,8 @@ var require_vercel_ai = __commonJS({
|
|
|
4137
5158
|
stepNumber: "stepNumber" in event ? event.stepNumber : 0,
|
|
4138
5159
|
model: event.model
|
|
4139
5160
|
}) : options.generationName ?? "vercel-ai-generation";
|
|
4140
|
-
const
|
|
5161
|
+
const fallbackSystem = siblingSystem(v6Starts.at(-1)?.event);
|
|
5162
|
+
const input = v6Input(event, fallbackSystem);
|
|
4141
5163
|
const handle = trace.startGeneration({
|
|
4142
5164
|
name,
|
|
4143
5165
|
input,
|
|
@@ -4146,7 +5168,7 @@ var require_vercel_ai = __commonJS({
|
|
|
4146
5168
|
model: event.model.modelId,
|
|
4147
5169
|
startedAt,
|
|
4148
5170
|
llmProvider: event.model.provider,
|
|
4149
|
-
llmInputMessages: v6NormalizedMessages(event),
|
|
5171
|
+
llmInputMessages: v6NormalizedMessages(event, fallbackSystem),
|
|
4150
5172
|
llmTools: event.tools
|
|
4151
5173
|
});
|
|
4152
5174
|
latestV6GenerationId = handle.id;
|
|
@@ -4452,7 +5474,7 @@ var require_dist = __commonJS({
|
|
|
4452
5474
|
"../../packages/ts/tracing/dist/index.js"(exports) {
|
|
4453
5475
|
"use strict";
|
|
4454
5476
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4455
|
-
exports.vercelAI = exports.openAIAgents = exports.mastra = exports.LemmaMastraExporter = exports.langGraph = exports.langChain = exports.LemmaLangChainCallbackHandler = exports.LangChainCallbackHandler = exports.lemmaDebug = exports.isDebugVerifyEnabled = exports.isDebugModeEnabled = exports.enableDebugMode = exports.disableDebugMode = exports.RELEASE_MAX_LENGTH = exports.normalizeRelease = exports.startCodingAgentTurn = exports.recordCodingAgentToolStart = exports.recordCodingAgentToolResult = exports.completeCodingAgentTurn = exports.codingAgentTurnTrace = exports.toWireTokenUsage = exports.normalizeTokenUsage = exports.attachResultUsage = exports.TraceHandle = exports.TraceContext = exports.SpanHandle = exports.NoopSpanHandle = exports.Lemma = void 0;
|
|
5477
|
+
exports.vercelAI = exports.openAIAgents = exports.mastra = exports.LemmaMastraExporter = exports.langGraph = exports.langChain = exports.LemmaLangChainCallbackHandler = exports.LangChainCallbackHandler = exports.lemmaDebug = exports.isDebugVerifyEnabled = exports.isDebugModeEnabled = exports.enableDebugMode = exports.disableDebugMode = exports.RELEASE_MAX_LENGTH = exports.normalizeRelease = exports.startCodingAgentTurn = exports.recordCodingAgentToolStart = exports.recordCodingAgentToolResult = exports.completeCodingAgentTurn = exports.codingAgentTurnTrace = exports.TurnHandle = exports.TURN_JOURNAL_VERSION = exports.TURN_CONTEXT_VERSION = exports.AttachedTurn = exports.AttachedSpanHandle = exports.startTurn = exports.parseTurnContextToken = exports.assembleTurn = exports.applyTurnJournal = exports.attachTurn = exports.toWireTokenUsage = exports.normalizeTokenUsage = exports.attachResultUsage = exports.TraceHandle = exports.TraceContext = exports.SpanHandle = exports.NoopSpanHandle = exports.Lemma = void 0;
|
|
4456
5478
|
var client_1 = require_client();
|
|
4457
5479
|
Object.defineProperty(exports, "Lemma", { enumerable: true, get: function() {
|
|
4458
5480
|
return client_1.Lemma;
|
|
@@ -4478,6 +5500,37 @@ var require_dist = __commonJS({
|
|
|
4478
5500
|
Object.defineProperty(exports, "toWireTokenUsage", { enumerable: true, get: function() {
|
|
4479
5501
|
return client_1.toWireTokenUsage;
|
|
4480
5502
|
} });
|
|
5503
|
+
var turn_1 = require_turn();
|
|
5504
|
+
Object.defineProperty(exports, "attachTurn", { enumerable: true, get: function() {
|
|
5505
|
+
return turn_1.attachTurn;
|
|
5506
|
+
} });
|
|
5507
|
+
Object.defineProperty(exports, "applyTurnJournal", { enumerable: true, get: function() {
|
|
5508
|
+
return turn_1.applyTurnJournal;
|
|
5509
|
+
} });
|
|
5510
|
+
Object.defineProperty(exports, "assembleTurn", { enumerable: true, get: function() {
|
|
5511
|
+
return turn_1.assembleTurn;
|
|
5512
|
+
} });
|
|
5513
|
+
Object.defineProperty(exports, "parseTurnContextToken", { enumerable: true, get: function() {
|
|
5514
|
+
return turn_1.parseTurnContextToken;
|
|
5515
|
+
} });
|
|
5516
|
+
Object.defineProperty(exports, "startTurn", { enumerable: true, get: function() {
|
|
5517
|
+
return turn_1.startTurn;
|
|
5518
|
+
} });
|
|
5519
|
+
Object.defineProperty(exports, "AttachedSpanHandle", { enumerable: true, get: function() {
|
|
5520
|
+
return turn_1.AttachedSpanHandle;
|
|
5521
|
+
} });
|
|
5522
|
+
Object.defineProperty(exports, "AttachedTurn", { enumerable: true, get: function() {
|
|
5523
|
+
return turn_1.AttachedTurn;
|
|
5524
|
+
} });
|
|
5525
|
+
Object.defineProperty(exports, "TURN_CONTEXT_VERSION", { enumerable: true, get: function() {
|
|
5526
|
+
return turn_1.TURN_CONTEXT_VERSION;
|
|
5527
|
+
} });
|
|
5528
|
+
Object.defineProperty(exports, "TURN_JOURNAL_VERSION", { enumerable: true, get: function() {
|
|
5529
|
+
return turn_1.TURN_JOURNAL_VERSION;
|
|
5530
|
+
} });
|
|
5531
|
+
Object.defineProperty(exports, "TurnHandle", { enumerable: true, get: function() {
|
|
5532
|
+
return turn_1.TurnHandle;
|
|
5533
|
+
} });
|
|
4481
5534
|
var coding_agent_1 = require_coding_agent();
|
|
4482
5535
|
Object.defineProperty(exports, "codingAgentTurnTrace", { enumerable: true, get: function() {
|
|
4483
5536
|
return coding_agent_1.codingAgentTurnTrace;
|
|
@@ -5059,8 +6112,7 @@ import {
|
|
|
5059
6112
|
import { join as join3 } from "node:path";
|
|
5060
6113
|
|
|
5061
6114
|
// src/delivery.ts
|
|
5062
|
-
|
|
5063
|
-
function createDeliveryFetch(fetchImplementation = fetch, timeoutMilliseconds = DELIVERY_TIMEOUT_MS) {
|
|
6115
|
+
function createDeliveryFetch(fetchImplementation, remainingMilliseconds) {
|
|
5064
6116
|
return async (request, init) => {
|
|
5065
6117
|
const controller = new AbortController();
|
|
5066
6118
|
const signal = init?.signal ? AbortSignal.any([init.signal, controller.signal]) : controller.signal;
|
|
@@ -5069,7 +6121,7 @@ function createDeliveryFetch(fetchImplementation = fetch, timeoutMilliseconds =
|
|
|
5069
6121
|
timeout = setTimeout(() => {
|
|
5070
6122
|
controller.abort();
|
|
5071
6123
|
reject(new Error("Lemma OpenCode trace delivery timed out"));
|
|
5072
|
-
},
|
|
6124
|
+
}, remainingMilliseconds);
|
|
5073
6125
|
});
|
|
5074
6126
|
const requestAndBody = (async () => {
|
|
5075
6127
|
const response = await fetchImplementation(request, { ...init, signal });
|
|
@@ -5089,6 +6141,69 @@ function createDeliveryFetch(fetchImplementation = fetch, timeoutMilliseconds =
|
|
|
5089
6141
|
};
|
|
5090
6142
|
}
|
|
5091
6143
|
|
|
6144
|
+
// package.json
|
|
6145
|
+
var package_default = {
|
|
6146
|
+
name: "@uselemma/opencode",
|
|
6147
|
+
version: "0.1.3",
|
|
6148
|
+
description: "Lemma SDK tracing for OpenCode coding-agent sessions",
|
|
6149
|
+
license: "MIT",
|
|
6150
|
+
author: "Lemma",
|
|
6151
|
+
repository: {
|
|
6152
|
+
type: "git",
|
|
6153
|
+
url: "https://github.com/uselemma/lemma",
|
|
6154
|
+
directory: "plugins/lemma-opencode"
|
|
6155
|
+
},
|
|
6156
|
+
homepage: "https://github.com/uselemma/lemma/tree/main/plugins/lemma-opencode#readme",
|
|
6157
|
+
bugs: {
|
|
6158
|
+
url: "https://github.com/uselemma/lemma/issues"
|
|
6159
|
+
},
|
|
6160
|
+
type: "module",
|
|
6161
|
+
main: "./runtime/lemma.mjs",
|
|
6162
|
+
types: "./runtime/lemma.d.ts",
|
|
6163
|
+
exports: {
|
|
6164
|
+
".": {
|
|
6165
|
+
types: "./runtime/lemma.d.ts",
|
|
6166
|
+
import: "./runtime/lemma.mjs",
|
|
6167
|
+
default: "./runtime/lemma.mjs"
|
|
6168
|
+
}
|
|
6169
|
+
},
|
|
6170
|
+
bin: {
|
|
6171
|
+
"lemma-opencode": "./scripts/setup.mjs"
|
|
6172
|
+
},
|
|
6173
|
+
files: [
|
|
6174
|
+
"runtime/lemma.mjs",
|
|
6175
|
+
"runtime/lemma.d.ts",
|
|
6176
|
+
"scripts/setup.mjs",
|
|
6177
|
+
"README.md"
|
|
6178
|
+
],
|
|
6179
|
+
keywords: [
|
|
6180
|
+
"opencode",
|
|
6181
|
+
"observability",
|
|
6182
|
+
"tracing"
|
|
6183
|
+
],
|
|
6184
|
+
scripts: {
|
|
6185
|
+
build: "node build.mjs",
|
|
6186
|
+
"type-check": "tsc --noEmit",
|
|
6187
|
+
test: "vitest run",
|
|
6188
|
+
"verify:local": "pnpm run build && node verify-local.mjs",
|
|
6189
|
+
prepublishOnly: "pnpm run type-check && pnpm run build"
|
|
6190
|
+
},
|
|
6191
|
+
dependencies: {
|
|
6192
|
+
"@opencode-ai/plugin": "1.18.19",
|
|
6193
|
+
"@uselemma/tracing": "workspace:^"
|
|
6194
|
+
},
|
|
6195
|
+
devDependencies: {
|
|
6196
|
+
"@opencode-ai/sdk": "1.18.19",
|
|
6197
|
+
"@types/node": "^22.18.8",
|
|
6198
|
+
esbuild: "^0.25.11",
|
|
6199
|
+
typescript: "^5.9.3",
|
|
6200
|
+
vitest: "^3.2.4"
|
|
6201
|
+
}
|
|
6202
|
+
};
|
|
6203
|
+
|
|
6204
|
+
// src/user-agent.ts
|
|
6205
|
+
var PLUGIN_USER_AGENT = `${package_default.name}/${package_default.version}`;
|
|
6206
|
+
|
|
5092
6207
|
// src/flush.ts
|
|
5093
6208
|
var FLUSH_LOCK_STALE_MS = 3e4;
|
|
5094
6209
|
var FLUSH_LOCK_OWNER_FILE = "owner.json";
|
|
@@ -5205,9 +6320,10 @@ async function flushPendingTurns(options = {}) {
|
|
|
5205
6320
|
projectId: credentials.projectId,
|
|
5206
6321
|
baseUrl: credentials.apiUrl,
|
|
5207
6322
|
fetch: createDeliveryFetch(
|
|
5208
|
-
options.fetch,
|
|
5209
|
-
|
|
5210
|
-
)
|
|
6323
|
+
options.fetch ?? fetch,
|
|
6324
|
+
remainingMilliseconds
|
|
6325
|
+
),
|
|
6326
|
+
userAgent: PLUGIN_USER_AGENT
|
|
5211
6327
|
}).ingest(trace.context, {
|
|
5212
6328
|
startedAt: new Date(trace.startedAt),
|
|
5213
6329
|
endedAt: new Date(trace.endedAt)
|