poe-code 3.0.216 → 3.0.217

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.
@@ -29756,7 +29756,7 @@ function formatError(err) {
29756
29756
  }
29757
29757
  }
29758
29758
 
29759
- // packages/braintrust/src/redact.ts
29759
+ // packages/acp-telemetry/src/redact.ts
29760
29760
  var MAX_STRING_BYTES = 65536;
29761
29761
  var MAX_JSON_BYTES = 262144;
29762
29762
  var BINARY_SCAN_BYTES = 1024;
@@ -29795,6 +29795,220 @@ function redactLeaf(value) {
29795
29795
  return value;
29796
29796
  }
29797
29797
 
29798
+ // packages/acp-telemetry/src/trace.ts
29799
+ function acpToTrace(ctx) {
29800
+ const spawnCtx = ctx;
29801
+ return {
29802
+ root: {
29803
+ name: `agent:${ctx.agent}:${ctx.model ?? "?"}`,
29804
+ kind: "agent",
29805
+ input: redact({
29806
+ prompt: ctx.prompt,
29807
+ mode: ctx.mode,
29808
+ cwd: ctx.cwd
29809
+ }),
29810
+ output: redact(accumulateAgentOutput(ctx.events)),
29811
+ metadata: {
29812
+ sessionId: ctx.sessionId,
29813
+ threadId: ctx.threadId,
29814
+ ...spawnCtx.metadata
29815
+ },
29816
+ metrics: buildMetrics(ctx),
29817
+ children: logToolSpans(ctx.events)
29818
+ }
29819
+ };
29820
+ }
29821
+ function logToolSpans(events) {
29822
+ const spans = [];
29823
+ for (const [index, event] of events.entries()) {
29824
+ const toolCall = asToolCall(event);
29825
+ if (toolCall === void 0) {
29826
+ continue;
29827
+ }
29828
+ const metadata = collectToolMeta(events, index, readString6(toolCall.toolCallId));
29829
+ spans.push({
29830
+ name: `tool_call:${readString6(toolCall.kind) ?? "unknown"}`,
29831
+ kind: "tool",
29832
+ input: redact(readToolInput(toolCall)),
29833
+ output: redact(assembleToolOutput(events, index, readString6(toolCall.toolCallId))),
29834
+ ...metadata ? { metadata } : {},
29835
+ ...readSpanTimestamps(metadata),
29836
+ children: []
29837
+ });
29838
+ }
29839
+ return spans;
29840
+ }
29841
+ function collectToolMeta(events, toolCallIndex, toolCallId) {
29842
+ const merged = {};
29843
+ const startMeta = asRecord2(asRecord2(events[toolCallIndex])?._meta);
29844
+ if (startMeta) {
29845
+ for (const [key2, value] of Object.entries(startMeta)) {
29846
+ merged[key2 === "ts" ? "startTs" : key2] = value;
29847
+ }
29848
+ }
29849
+ for (const event of events.slice(toolCallIndex + 1)) {
29850
+ const update = asToolCallUpdate(event);
29851
+ if (update === void 0) continue;
29852
+ if (toolCallId !== void 0 && update.toolCallId !== toolCallId) continue;
29853
+ const updateMeta = asRecord2(update._meta);
29854
+ if (!updateMeta) continue;
29855
+ for (const [key2, value] of Object.entries(updateMeta)) {
29856
+ merged[key2 === "ts" ? "endTs" : key2] = value;
29857
+ }
29858
+ }
29859
+ return Object.keys(merged).length > 0 ? merged : void 0;
29860
+ }
29861
+ function accumulateAgentOutput(events) {
29862
+ let output = "";
29863
+ for (const event of events) {
29864
+ const record = asRecord2(event);
29865
+ if (record === void 0) {
29866
+ continue;
29867
+ }
29868
+ if (record.event === "agent_message") {
29869
+ output += readString6(record.text) ?? "";
29870
+ continue;
29871
+ }
29872
+ if (record.sessionUpdate === "agent_message_chunk") {
29873
+ output += readContentText(record.content);
29874
+ }
29875
+ }
29876
+ return output;
29877
+ }
29878
+ function assembleToolOutput(events, toolCallIndex, toolCallId) {
29879
+ const outputs = [];
29880
+ let text5 = "";
29881
+ for (const event of events.slice(toolCallIndex + 1)) {
29882
+ const update = asToolCallUpdate(event);
29883
+ if (update === void 0) {
29884
+ continue;
29885
+ }
29886
+ if (toolCallId !== void 0 && update.toolCallId !== toolCallId) {
29887
+ continue;
29888
+ }
29889
+ if (Object.hasOwn(update, "rawOutput")) {
29890
+ outputs.push(update.rawOutput);
29891
+ }
29892
+ const contentText = readContentText(update.content);
29893
+ if (contentText.length > 0) {
29894
+ text5 += contentText;
29895
+ }
29896
+ }
29897
+ if (outputs.length === 0) {
29898
+ return text5;
29899
+ }
29900
+ if (text5.length > 0) {
29901
+ outputs.push(text5);
29902
+ }
29903
+ return outputs.length === 1 ? outputs[0] : outputs;
29904
+ }
29905
+ function buildMetrics(ctx) {
29906
+ const usage = ctx.usage;
29907
+ const metrics = {};
29908
+ const promptTokens = readNumber(usage.prompt_tokens) ?? readNumber(usage.inputTokens);
29909
+ const completionTokens = readNumber(usage.completion_tokens) ?? readNumber(usage.outputTokens);
29910
+ addMetric(metrics, "prompt_tokens", promptTokens);
29911
+ addMetric(metrics, "completion_tokens", completionTokens);
29912
+ addMetric(
29913
+ metrics,
29914
+ "tokens",
29915
+ readNumber(usage.tokens) ?? sumIfPresent(promptTokens, completionTokens)
29916
+ );
29917
+ addMetric(
29918
+ metrics,
29919
+ "prompt_cached_tokens",
29920
+ readNumber(usage.prompt_cached_tokens) ?? readNumber(usage.cachedTokens)
29921
+ );
29922
+ addMetric(
29923
+ metrics,
29924
+ "prompt_cache_creation_tokens",
29925
+ readNumber(usage.prompt_cache_creation_tokens)
29926
+ );
29927
+ addMetric(metrics, "durationMs", readNumber(usage.durationMs));
29928
+ return metrics;
29929
+ }
29930
+ function asToolCall(event) {
29931
+ const record = asRecord2(event);
29932
+ return record?.sessionUpdate === "tool_call" ? record : void 0;
29933
+ }
29934
+ function asToolCallUpdate(event) {
29935
+ const record = asRecord2(event);
29936
+ return record?.sessionUpdate === "tool_call_update" ? record : void 0;
29937
+ }
29938
+ function readToolInput(toolCall) {
29939
+ if (Object.hasOwn(toolCall, "input")) {
29940
+ return toolCall.input;
29941
+ }
29942
+ return toolCall.rawInput;
29943
+ }
29944
+ function readContentText(value) {
29945
+ if (Array.isArray(value)) {
29946
+ return value.map(readContentText).join("");
29947
+ }
29948
+ const record = asRecord2(value);
29949
+ if (record === void 0 || record.type !== "text") {
29950
+ return "";
29951
+ }
29952
+ return readString6(record.text) ?? "";
29953
+ }
29954
+ function readSpanTimestamps(metadata) {
29955
+ if (metadata === void 0) {
29956
+ return {};
29957
+ }
29958
+ const startTs = readNumber(metadata.startTs);
29959
+ const endTs = readNumber(metadata.endTs);
29960
+ return {
29961
+ ...startTs !== void 0 ? { startTs } : {},
29962
+ ...endTs !== void 0 ? { endTs } : {}
29963
+ };
29964
+ }
29965
+ function asRecord2(value) {
29966
+ return typeof value === "object" && value !== null ? value : void 0;
29967
+ }
29968
+ function readString6(value) {
29969
+ return typeof value === "string" ? value : void 0;
29970
+ }
29971
+ function readNumber(value) {
29972
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
29973
+ }
29974
+ function addMetric(metrics, key2, value) {
29975
+ if (value !== void 0) {
29976
+ metrics[key2] = value;
29977
+ }
29978
+ }
29979
+ function sumIfPresent(left, right) {
29980
+ return left !== void 0 && right !== void 0 ? left + right : void 0;
29981
+ }
29982
+
29983
+ // packages/acp-telemetry/src/emit-braintrust.ts
29984
+ function emitToBraintrust(trace, parent) {
29985
+ const root = parent.startSpan({ name: trace.root.name, type: "task" });
29986
+ try {
29987
+ emitSpan(root, trace.root);
29988
+ } finally {
29989
+ root.end();
29990
+ }
29991
+ }
29992
+ function emitSpan(span, traceSpan) {
29993
+ span.log(toBraintrustLogEvent(traceSpan));
29994
+ for (const child of traceSpan.children) {
29995
+ const childSpan = span.startSpan({ name: child.name, type: "tool" });
29996
+ try {
29997
+ emitSpan(childSpan, child);
29998
+ } finally {
29999
+ childSpan.end();
30000
+ }
30001
+ }
30002
+ }
30003
+ function toBraintrustLogEvent(span) {
30004
+ return {
30005
+ ...Object.hasOwn(span, "input") ? { input: span.input } : {},
30006
+ ...Object.hasOwn(span, "output") ? { output: span.output } : {},
30007
+ ...Object.hasOwn(span, "metadata") ? { metadata: span.metadata } : {},
30008
+ ...Object.hasOwn(span, "metrics") ? { metrics: span.metrics } : {}
30009
+ };
30010
+ }
30011
+
29798
30012
  // packages/braintrust/src/row-builder.ts
29799
30013
  function makePipelineRowState(client) {
29800
30014
  const rows = /* @__PURE__ */ new Map();
@@ -29950,8 +30164,8 @@ async function openCurrentChildSpan(client, args, ctx) {
29950
30164
  }
29951
30165
  }
29952
30166
  function buildPipelineCompletionLog(started, completed) {
29953
- const startRecord = asRecord2(started) ?? {};
29954
- const completeRecord = asRecord2(completed) ?? {};
30167
+ const startRecord = asRecord3(started) ?? {};
30168
+ const completeRecord = asRecord3(completed) ?? {};
29955
30169
  return {
29956
30170
  input: redact({
29957
30171
  step_name: readPipelineStep(started),
@@ -29974,7 +30188,7 @@ function buildPipelineCompletionLog(started, completed) {
29974
30188
  };
29975
30189
  }
29976
30190
  function buildSuperintendentLog(role, result) {
29977
- const record = asRecord2(result);
30191
+ const record = asRecord3(result);
29978
30192
  const event = {
29979
30193
  input: redact(record?.input),
29980
30194
  output: redact(record?.output ?? result)
@@ -29988,7 +30202,7 @@ function buildSuperintendentLog(role, result) {
29988
30202
  return event;
29989
30203
  }
29990
30204
  function buildExperimentLog(row, entry) {
29991
- const entryRecord = asRecord2(entry) ?? {};
30205
+ const entryRecord = asRecord3(entry) ?? {};
29992
30206
  const scores = buildExperimentScores(row.baseline, entry.scores);
29993
30207
  const metrics = { ...row.metrics };
29994
30208
  if (Number.isFinite(entry.durationMs)) {
@@ -30017,12 +30231,12 @@ function buildPipelineMetrics(progress) {
30017
30231
  const metrics = {};
30018
30232
  const usage = progress.usage;
30019
30233
  if (usage !== void 0) {
30020
- addMetric(metrics, "prompt_tokens", usage.inputTokens);
30021
- addMetric(metrics, "completion_tokens", usage.outputTokens);
30022
- addMetric(metrics, "tokens", usage.inputTokens + usage.outputTokens);
30023
- addMetric(metrics, "prompt_cached_tokens", usage.cachedTokens);
30234
+ addMetric2(metrics, "prompt_tokens", usage.inputTokens);
30235
+ addMetric2(metrics, "completion_tokens", usage.outputTokens);
30236
+ addMetric2(metrics, "tokens", usage.inputTokens + usage.outputTokens);
30237
+ addMetric2(metrics, "prompt_cached_tokens", usage.cachedTokens);
30024
30238
  }
30025
- addMetric(metrics, "durationMs", progress.durationMs);
30239
+ addMetric2(metrics, "durationMs", progress.durationMs);
30026
30240
  return metrics;
30027
30241
  }
30028
30242
  function buildExperimentScores(baseline, scores) {
@@ -30099,7 +30313,7 @@ function asSpanParent(value) {
30099
30313
  }
30100
30314
  return span;
30101
30315
  }
30102
- function asRecord2(value) {
30316
+ function asRecord3(value) {
30103
30317
  return typeof value === "object" && value !== null ? value : void 0;
30104
30318
  }
30105
30319
  function readFirstString(record, keys) {
@@ -30114,7 +30328,7 @@ function readFirstValue(record, keys) {
30114
30328
  }
30115
30329
  return void 0;
30116
30330
  }
30117
- function addMetric(metrics, key2, value) {
30331
+ function addMetric2(metrics, key2, value) {
30118
30332
  if (value !== void 0 && Number.isFinite(value)) {
30119
30333
  metrics[key2] = value;
30120
30334
  }
@@ -30166,197 +30380,6 @@ function createPipelineCallbacks(client) {
30166
30380
  };
30167
30381
  }
30168
30382
 
30169
- // packages/braintrust/src/span-builder.ts
30170
- async function logSpawnSession(client, ctx) {
30171
- try {
30172
- const { currentSpan } = await import("braintrust");
30173
- const agentSpan = asSpanParent2(currentSpan()).startSpan({
30174
- name: `agent:${ctx.agent}:${ctx.model ?? "?"}`,
30175
- type: "task"
30176
- });
30177
- try {
30178
- logToolSpans(agentSpan, ctx.events);
30179
- agentSpan.log({
30180
- input: redact({
30181
- prompt: ctx.prompt,
30182
- mode: ctx.mode,
30183
- cwd: ctx.cwd
30184
- }),
30185
- output: redact(accumulateAgentOutput(ctx.events)),
30186
- metadata: {
30187
- sessionId: ctx.sessionId,
30188
- threadId: ctx.threadId,
30189
- ...ctx.metadata
30190
- },
30191
- metrics: buildMetrics(ctx)
30192
- });
30193
- } finally {
30194
- agentSpan.end();
30195
- }
30196
- } catch (err) {
30197
- client.recordError(err, "log spawn session");
30198
- }
30199
- }
30200
- function logToolSpans(agentSpan, events) {
30201
- for (const [index, event] of events.entries()) {
30202
- const toolCall = asToolCall(event);
30203
- if (toolCall === void 0) {
30204
- continue;
30205
- }
30206
- const toolSpan = agentSpan.startSpan({
30207
- name: `tool_call:${readString6(toolCall.kind) ?? "unknown"}`,
30208
- type: "tool"
30209
- });
30210
- try {
30211
- const metadata = collectToolMeta(events, index, readString6(toolCall.toolCallId));
30212
- toolSpan.log({
30213
- input: redact(readToolInput(toolCall)),
30214
- output: redact(assembleToolOutput(events, index, readString6(toolCall.toolCallId))),
30215
- ...metadata ? { metadata } : {}
30216
- });
30217
- } finally {
30218
- toolSpan.end();
30219
- }
30220
- }
30221
- }
30222
- function collectToolMeta(events, toolCallIndex, toolCallId) {
30223
- const merged = {};
30224
- const startMeta = asRecord3(asRecord3(events[toolCallIndex])?._meta);
30225
- if (startMeta) {
30226
- for (const [key2, value] of Object.entries(startMeta)) {
30227
- merged[key2 === "ts" ? "startTs" : key2] = value;
30228
- }
30229
- }
30230
- for (const event of events.slice(toolCallIndex + 1)) {
30231
- const update = asToolCallUpdate(event);
30232
- if (update === void 0) continue;
30233
- if (toolCallId !== void 0 && update.toolCallId !== toolCallId) continue;
30234
- const updateMeta = asRecord3(update._meta);
30235
- if (!updateMeta) continue;
30236
- for (const [key2, value] of Object.entries(updateMeta)) {
30237
- merged[key2 === "ts" ? "endTs" : key2] = value;
30238
- }
30239
- }
30240
- return Object.keys(merged).length > 0 ? merged : void 0;
30241
- }
30242
- function accumulateAgentOutput(events) {
30243
- let output = "";
30244
- for (const event of events) {
30245
- const record = asRecord3(event);
30246
- if (record === void 0) {
30247
- continue;
30248
- }
30249
- if (record.event === "agent_message") {
30250
- output += readString6(record.text) ?? "";
30251
- continue;
30252
- }
30253
- if (record.sessionUpdate === "agent_message_chunk") {
30254
- output += readContentText(record.content);
30255
- }
30256
- }
30257
- return output;
30258
- }
30259
- function assembleToolOutput(events, toolCallIndex, toolCallId) {
30260
- const outputs = [];
30261
- let text5 = "";
30262
- for (const event of events.slice(toolCallIndex + 1)) {
30263
- const update = asToolCallUpdate(event);
30264
- if (update === void 0) {
30265
- continue;
30266
- }
30267
- if (toolCallId !== void 0 && update.toolCallId !== toolCallId) {
30268
- continue;
30269
- }
30270
- if (Object.hasOwn(update, "rawOutput")) {
30271
- outputs.push(update.rawOutput);
30272
- }
30273
- const contentText = readContentText(update.content);
30274
- if (contentText.length > 0) {
30275
- text5 += contentText;
30276
- }
30277
- }
30278
- if (outputs.length === 0) {
30279
- return text5;
30280
- }
30281
- if (text5.length > 0) {
30282
- outputs.push(text5);
30283
- }
30284
- return outputs.length === 1 ? outputs[0] : outputs;
30285
- }
30286
- function buildMetrics(ctx) {
30287
- const usage = ctx.usage;
30288
- const metrics = {};
30289
- const promptTokens = readNumber(usage.prompt_tokens) ?? readNumber(usage.inputTokens);
30290
- const completionTokens = readNumber(usage.completion_tokens) ?? readNumber(usage.outputTokens);
30291
- addMetric2(metrics, "prompt_tokens", promptTokens);
30292
- addMetric2(metrics, "completion_tokens", completionTokens);
30293
- addMetric2(
30294
- metrics,
30295
- "tokens",
30296
- readNumber(usage.tokens) ?? sumIfPresent(promptTokens, completionTokens)
30297
- );
30298
- addMetric2(
30299
- metrics,
30300
- "prompt_cached_tokens",
30301
- readNumber(usage.prompt_cached_tokens) ?? readNumber(usage.cachedTokens)
30302
- );
30303
- addMetric2(
30304
- metrics,
30305
- "prompt_cache_creation_tokens",
30306
- readNumber(usage.prompt_cache_creation_tokens)
30307
- );
30308
- addMetric2(metrics, "durationMs", readNumber(usage.durationMs));
30309
- return metrics;
30310
- }
30311
- function asSpanParent2(value) {
30312
- const span = value;
30313
- if (span === void 0 || typeof span.startSpan !== "function") {
30314
- throw new Error("Braintrust current span unavailable");
30315
- }
30316
- return span;
30317
- }
30318
- function asToolCall(event) {
30319
- const record = asRecord3(event);
30320
- return record?.sessionUpdate === "tool_call" ? record : void 0;
30321
- }
30322
- function asToolCallUpdate(event) {
30323
- const record = asRecord3(event);
30324
- return record?.sessionUpdate === "tool_call_update" ? record : void 0;
30325
- }
30326
- function readToolInput(toolCall) {
30327
- if (Object.hasOwn(toolCall, "input")) {
30328
- return toolCall.input;
30329
- }
30330
- return toolCall.rawInput;
30331
- }
30332
- function readContentText(value) {
30333
- if (Array.isArray(value)) {
30334
- return value.map(readContentText).join("");
30335
- }
30336
- const record = asRecord3(value);
30337
- if (record === void 0 || record.type !== "text") {
30338
- return "";
30339
- }
30340
- return readString6(record.text) ?? "";
30341
- }
30342
- function asRecord3(value) {
30343
- return typeof value === "object" && value !== null ? value : void 0;
30344
- }
30345
- function readString6(value) {
30346
- return typeof value === "string" ? value : void 0;
30347
- }
30348
- function readNumber(value) {
30349
- return typeof value === "number" && Number.isFinite(value) ? value : void 0;
30350
- }
30351
- function addMetric2(metrics, key2, value) {
30352
- if (value !== void 0) {
30353
- metrics[key2] = value;
30354
- }
30355
- }
30356
- function sumIfPresent(left, right) {
30357
- return left !== void 0 && right !== void 0 ? left + right : void 0;
30358
- }
30359
-
30360
30383
  // packages/braintrust/src/adapters/spawn.ts
30361
30384
  function createSpawnMiddleware(client) {
30362
30385
  return async (ctx, next) => {
@@ -30370,7 +30393,12 @@ function createSpawnMiddleware(client) {
30370
30393
  };
30371
30394
  throw err;
30372
30395
  } finally {
30373
- await logSpawnSession(client, ctx);
30396
+ try {
30397
+ const { currentSpan } = await import("braintrust");
30398
+ emitToBraintrust(acpToTrace(ctx), currentSpan());
30399
+ } catch (err) {
30400
+ client.recordError(err, "log spawn session");
30401
+ }
30374
30402
  }
30375
30403
  };
30376
30404
  }
@@ -30401,7 +30429,7 @@ function createSuperintendentCallbacks(client) {
30401
30429
  async function logFailedRole(client, role, error2, name) {
30402
30430
  try {
30403
30431
  const { currentSpan } = await import("braintrust");
30404
- const span = asSpanParent3(currentSpan()).startSpan({
30432
+ const span = asSpanParent2(currentSpan()).startSpan({
30405
30433
  name: name === void 0 ? `role:${role}:failed` : `role:${role}:${name}:failed`,
30406
30434
  type: "task"
30407
30435
  });
@@ -30423,7 +30451,7 @@ async function logFailedRole(client, role, error2, name) {
30423
30451
  client.recordError(err, `superintendent ${role} failed`);
30424
30452
  }
30425
30453
  }
30426
- function asSpanParent3(value) {
30454
+ function asSpanParent2(value) {
30427
30455
  const span = value;
30428
30456
  if (span === void 0 || typeof span.startSpan !== "function") {
30429
30457
  throw new Error("Braintrust current span unavailable");