@lmnr-ai/lmnr 0.8.21 → 0.8.22-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  const require_chunk = require("../../chunk-Do9eywBl.cjs");
2
- const require_decorators = require("../../decorators-CaTC0lJq.cjs");
2
+ const require_decorators = require("../../decorators-Bb2owkCc.cjs");
3
3
  const require_cli_worker_build = require("./build.cjs");
4
4
  let readline = require("readline");
5
5
  readline = require_chunk.__toESM(readline);
@@ -1,4 +1,4 @@
1
- import { d as Laminar, h as consumeStreamResult } from "../../decorators-BDFgXfgI.mjs";
1
+ import { d as Laminar, h as consumeStreamResult } from "../../decorators-Bq6Sf8vt.mjs";
2
2
  import { buildFile, loadModule, selectRolloutFunction } from "./build.mjs";
3
3
  import * as readline from "readline";
4
4
  //#region src/cli/worker/index.ts
package/dist/cli.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  const require_chunk = require("./chunk-Do9eywBl.cjs");
3
- const require_dist = require("./dist-B5cKzRzq.cjs");
3
+ const require_dist = require("./dist-DZMJR04H.cjs");
4
4
  const require_utils = require("./utils-C8Tl1vKD.cjs");
5
5
  const require_file_utils = require("./file-utils-yJ5ephze.cjs");
6
6
  const require_proxy_to_lmnr_cli = require("./proxy-to-lmnr-cli-OA7sXzcz.cjs");
package/dist/cli.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { r as __require } from "./chunk-BEJ448es.mjs";
3
- import { n as version, t as LaminarClient } from "./dist-B5CmOQ4s.mjs";
3
+ import { n as version, t as LaminarClient } from "./dist-TLHkfisO.mjs";
4
4
  import { i as getDirname, s as initializeLogger } from "./utils-CHJ0KZUR.mjs";
5
5
  import { i as writeToFile, n as loadFromPaths, r as printToConsole } from "./file-utils-CHoR9VB8.mjs";
6
6
  import { t as proxyToLmnrCli } from "./proxy-to-lmnr-cli-DNDxudgt.mjs";
@@ -1,5 +1,5 @@
1
1
  const require_chunk = require("./chunk-Do9eywBl.cjs");
2
- const require_dist = require("./dist-B5cKzRzq.cjs");
2
+ const require_dist = require("./dist-DZMJR04H.cjs");
3
3
  const require_utils = require("./utils-C8Tl1vKD.cjs");
4
4
  let _opentelemetry_api = require("@opentelemetry/api");
5
5
  let _opentelemetry_context_async_hooks = require("@opentelemetry/context-async-hooks");
@@ -2174,7 +2174,7 @@ const injectScript = (sessionRecordingOptions, stringifyCallbackArgs) => {
2174
2174
  };
2175
2175
  if (!window.lmnrStartedRecordingEvents) {
2176
2176
  setInterval(sendBatchIfReady, BATCH_TIMEOUT);
2177
- window.lmnrRrweb.record({
2177
+ const recordOptions = {
2178
2178
  emit(event) {
2179
2179
  window.lmnrRrwebEventsBatch.push(event);
2180
2180
  },
@@ -2190,12 +2190,17 @@ const injectScript = (sessionRecordingOptions, stringifyCallbackArgs) => {
2190
2190
  email: sessionRecordingOptions?.maskInputOptions?.email || false,
2191
2191
  tel: sessionRecordingOptions?.maskInputOptions?.tel || false
2192
2192
  }
2193
- });
2193
+ };
2194
+ window.lmnrRrweb.record(recordOptions);
2194
2195
  setInterval(() => {
2195
- window.lmnrRrweb.record.addCustomEvent("heartbeat", {
2196
- title: document.title,
2197
- url: document.URL
2198
- });
2196
+ try {
2197
+ window.lmnrRrweb.record.addCustomEvent("heartbeat", {
2198
+ title: document.title,
2199
+ url: document.URL
2200
+ });
2201
+ } catch {
2202
+ window.lmnrRrweb.record(recordOptions);
2203
+ }
2199
2204
  }, HEARTBEAT_INTERVAL);
2200
2205
  window.lmnrStartedRecordingEvents = true;
2201
2206
  }
@@ -3118,6 +3123,29 @@ function createTargetInfoChangedHandler(context, state, sessionRecordingOptions)
3118
3123
  }
3119
3124
  //#endregion
3120
3125
  //#region src/browser/stagehand/v3/instrumentation.ts
3126
+ /**
3127
+ * Guarded _wrap: only wraps `target[methodName]` if it exists as a function.
3128
+ * Prevents the "Cannot wrap non-existent method" warning from
3129
+ * InstrumentationBase._wrap when a handler/method was renamed or removed
3130
+ * in the upstream library (e.g. Stagehand v2 vs v3 differences).
3131
+ */
3132
+ const safeWrap = (instrumentation, target, methodName, wrapper) => {
3133
+ if (!target) return false;
3134
+ if (typeof target[methodName] !== "function") return false;
3135
+ instrumentation._wrap(target, methodName, wrapper);
3136
+ return true;
3137
+ };
3138
+ /**
3139
+ * Guarded _unwrap counterpart to safeWrap. Only unwraps when the target method
3140
+ * exists AND was previously wrapped via shimmer (has the `__original` marker).
3141
+ */
3142
+ const safeUnwrap = (instrumentation, target, methodName) => {
3143
+ if (!target) return;
3144
+ const current = target[methodName];
3145
+ if (typeof current !== "function") return;
3146
+ if (!current.__original) return;
3147
+ instrumentation._unwrap(target, methodName);
3148
+ };
3121
3149
  var StagehandInstrumentation = class extends _opentelemetry_instrumentation.InstrumentationBase {
3122
3150
  constructor(sessionRecordingOptions) {
3123
3151
  super("@lmnr/browserbase-stagehand-instrumentation", require_dist.version, { enabled: true });
@@ -3169,21 +3197,28 @@ var StagehandInstrumentation = class extends _opentelemetry_instrumentation.Inst
3169
3197
  }
3170
3198
  unpatch(moduleExports, moduleVersion) {
3171
3199
  _opentelemetry_api.diag.debug(`unpatching stagehand ${moduleVersion}`);
3172
- this._unwrap(moduleExports, "Stagehand");
3200
+ safeUnwrap(this, moduleExports, "Stagehand");
3173
3201
  if (moduleExports.Stagehand && moduleExports.Stagehand.prototype) {
3174
- this._unwrap(moduleExports.Stagehand.prototype, "init");
3175
- this._unwrap(moduleExports.Stagehand.prototype, "close");
3176
- this._unwrap(moduleExports.Stagehand.prototype, "act");
3177
- this._unwrap(moduleExports.Stagehand.prototype, "extract");
3178
- this._unwrap(moduleExports.Stagehand.prototype, "observe");
3179
3202
  const prototype = moduleExports.Stagehand.prototype;
3203
+ safeUnwrap(this, prototype, "init");
3204
+ safeUnwrap(this, prototype, "close");
3205
+ safeUnwrap(this, prototype, "act");
3206
+ safeUnwrap(this, prototype, "extract");
3207
+ safeUnwrap(this, prototype, "observe");
3208
+ safeUnwrap(this, prototype, "agent");
3180
3209
  if (prototype.actHandler) {
3181
- this._unwrap(prototype.actHandler, "act");
3182
- this._unwrap(prototype.actHandler, "actFromObserveResult");
3210
+ safeUnwrap(this, prototype.actHandler, "act");
3211
+ safeUnwrap(this, prototype.actHandler, "actFromObserveResult");
3212
+ }
3213
+ if (prototype.extractHandler) safeUnwrap(this, prototype.extractHandler, "extract");
3214
+ if (prototype.observeHandler) safeUnwrap(this, prototype.observeHandler, "observe");
3215
+ if (prototype.llmClient) safeUnwrap(this, prototype.llmClient, "createChatCompletion");
3216
+ if (prototype.apiClient) {
3217
+ safeUnwrap(this, prototype.apiClient, "act");
3218
+ safeUnwrap(this, prototype.apiClient, "extract");
3219
+ safeUnwrap(this, prototype.apiClient, "observe");
3220
+ safeUnwrap(this, prototype.apiClient, "agentExecute");
3183
3221
  }
3184
- if (prototype.extractHandler) this._unwrap(prototype.extractHandler, "extract");
3185
- if (prototype.observeHandler) this._unwrap(prototype.observeHandler, "observe");
3186
- if (prototype.llmClient) this._unwrap(prototype.llmClient, "createChatCompletion");
3187
3222
  }
3188
3223
  return moduleExports;
3189
3224
  }
@@ -3215,9 +3250,9 @@ var StagehandInstrumentation = class extends _opentelemetry_instrumentation.Inst
3215
3250
  const otelTraceId = parentSpan.spanContext().traceId;
3216
3251
  const traceId = require_utils.otelTraceIdToUUID(otelTraceId);
3217
3252
  const result = await original.bind(this).apply(this);
3218
- instrumentation._wrap(this, "act", instrumentation.patchStagehandGlobalMethod("act", sessionId, parentSpan));
3219
- instrumentation._wrap(this, "extract", instrumentation.patchStagehandGlobalMethod("extract", sessionId, parentSpan));
3220
- instrumentation._wrap(this, "observe", instrumentation.patchStagehandGlobalMethod("observe", sessionId, parentSpan));
3253
+ safeWrap(instrumentation, this, "act", instrumentation.patchStagehandGlobalMethod("act", sessionId, parentSpan));
3254
+ safeWrap(instrumentation, this, "extract", instrumentation.patchStagehandGlobalMethod("extract", sessionId, parentSpan));
3255
+ safeWrap(instrumentation, this, "observe", instrumentation.patchStagehandGlobalMethod("observe", sessionId, parentSpan));
3221
3256
  if (this.actHandler) instrumentation.patchActHandler(this.actHandler);
3222
3257
  if (this.extractHandler) instrumentation.patchExtractHandler(this.extractHandler);
3223
3258
  if (this.observeHandler) instrumentation.patchObserveHandler(this.observeHandler);
@@ -3226,9 +3261,10 @@ var StagehandInstrumentation = class extends _opentelemetry_instrumentation.Inst
3226
3261
  provider: this.llmClient.type,
3227
3262
  model: this.llmClient.modelName
3228
3263
  });
3229
- instrumentation._wrap(this.llmClient, "createChatCompletion", instrumentation.patchStagehandLLMClientCreateChatCompletion());
3264
+ safeWrap(instrumentation, this.llmClient, "createChatCompletion", instrumentation.patchStagehandLLMClientCreateChatCompletion());
3230
3265
  }
3231
- instrumentation._wrap(this, "agent", instrumentation.patchStagehandAgentInitializer(sessionId, parentSpan));
3266
+ if (this.apiClient) instrumentation.patchStagehandApiClient(this.apiClient, sessionId, parentSpan, this);
3267
+ safeWrap(instrumentation, this, "agent", instrumentation.patchStagehandAgentInitializer(sessionId, parentSpan));
3232
3268
  if (instrumentation._client) await instrumentation.setupSessionRecording(this, sessionId, traceId);
3233
3269
  if (this.context && this.context.pages) for (const page of this.context.pages()) instrumentation.patchStagehandPage(page, parentSpan);
3234
3270
  if (this.context && this.context.on) this.context.on("page", (page) => {
@@ -3363,8 +3399,8 @@ var StagehandInstrumentation = class extends _opentelemetry_instrumentation.Inst
3363
3399
  };
3364
3400
  }
3365
3401
  patchActHandler(actHandler) {
3366
- this._wrap(actHandler, "act", this.patchActHandlerAct());
3367
- this._wrap(actHandler, "actFromObserveResult", this.patchActHandlerActFromObserveResult());
3402
+ safeWrap(this, actHandler, "act", this.patchActHandlerAct());
3403
+ safeWrap(this, actHandler, "actFromObserveResult", this.patchActHandlerActFromObserveResult());
3368
3404
  }
3369
3405
  patchActHandlerAct() {
3370
3406
  return (original) => async function act(...args) {
@@ -3393,7 +3429,7 @@ var StagehandInstrumentation = class extends _opentelemetry_instrumentation.Inst
3393
3429
  };
3394
3430
  }
3395
3431
  patchExtractHandler(extractHandler) {
3396
- this._wrap(extractHandler, "extract", this.patchExtractHandlerExtract());
3432
+ safeWrap(this, extractHandler, "extract", this.patchExtractHandlerExtract());
3397
3433
  }
3398
3434
  patchExtractHandlerExtract() {
3399
3435
  return (original) => async function extract(...args) {
@@ -3417,7 +3453,7 @@ var StagehandInstrumentation = class extends _opentelemetry_instrumentation.Inst
3417
3453
  };
3418
3454
  }
3419
3455
  patchObserveHandler(observeHandler) {
3420
- this._wrap(observeHandler, "observe", this.patchObserveHandlerObserve());
3456
+ safeWrap(this, observeHandler, "observe", this.patchObserveHandlerObserve());
3421
3457
  }
3422
3458
  patchObserveHandlerObserve() {
3423
3459
  return (original) => async function observe$1(...args) {
@@ -3445,7 +3481,7 @@ var StagehandInstrumentation = class extends _opentelemetry_instrumentation.Inst
3445
3481
  };
3446
3482
  }
3447
3483
  patchStagehandAgent(agent, sessionId, parentSpan) {
3448
- this._wrap(agent, "execute", this.patchStagehandAgentExecute(sessionId, parentSpan));
3484
+ safeWrap(this, agent, "execute", this.patchStagehandAgentExecute(sessionId, parentSpan));
3449
3485
  this.wrapAgentMethod(agent, "captureScreenshot", parentSpan, {
3450
3486
  spanType: "TOOL",
3451
3487
  ignoreOutput: true
@@ -3512,8 +3548,7 @@ var StagehandInstrumentation = class extends _opentelemetry_instrumentation.Inst
3512
3548
  };
3513
3549
  }
3514
3550
  wrapAgentMethod(agent, methodName, parentSpan, options) {
3515
- if (typeof agent[methodName] !== "function") return;
3516
- this._wrap(agent, methodName, (original) => async function method(...args) {
3551
+ safeWrap(this, agent, methodName, (original) => async function method(...args) {
3517
3552
  const currentSpan = _opentelemetry_api.trace.getSpan(LaminarContextManager.getContext()) ?? _opentelemetry_api.trace.getActiveSpan();
3518
3553
  return await Laminar.withSpan(currentSpan ?? parentSpan, async () => await observe({
3519
3554
  name: `stagehand.agent.${methodName}`,
@@ -3535,13 +3570,77 @@ var StagehandInstrumentation = class extends _opentelemetry_instrumentation.Inst
3535
3570
  "keyPress",
3536
3571
  "keyDown",
3537
3572
  "keyUp"
3538
- ]) {
3539
- if (typeof page[methodName] !== "function") continue;
3540
- this._wrap(page, methodName, (original) => async function method(...args) {
3573
+ ]) safeWrap(this, page, methodName, (original) => async function method(...args) {
3574
+ const currentSpan = _opentelemetry_api.trace.getSpan(LaminarContextManager.getContext()) ?? _opentelemetry_api.trace.getActiveSpan();
3575
+ return await Laminar.withSpan(currentSpan ?? parentSpan, async () => await observe({ name: `stagehand.page.${methodName}` }, async (argVals) => await original.bind(this).apply(this, argVals), args));
3576
+ });
3577
+ }
3578
+ /**
3579
+ * In BROWSERBASE remote mode Stagehand's top-level `act`/`extract`/`observe`/
3580
+ * agent `execute` are routed through an internal `StagehandAPIClient` which
3581
+ * makes HTTP calls to the Stagehand Cloud API. The local `llmClient` and the
3582
+ * local `actHandler`/`extractHandler`/`observeHandler` are therefore never
3583
+ * invoked and our existing wrappers do not fire.
3584
+ *
3585
+ * Wrap the apiClient methods directly so we still emit child spans for the
3586
+ * LLM-backed calls and surface token usage from the response where available.
3587
+ */
3588
+ patchStagehandApiClient(apiClient, _sessionId, parentSpan, stagehandInstance) {
3589
+ const instrumentation = this;
3590
+ const baseProvider = stagehandInstance?.llmClient?.type;
3591
+ const baseModel = stagehandInstance?.llmClient?.modelName;
3592
+ const wrapApiMethod = (methodName) => {
3593
+ safeWrap(instrumentation, apiClient, methodName, (original) => async function method(...args) {
3541
3594
  const currentSpan = _opentelemetry_api.trace.getSpan(LaminarContextManager.getContext()) ?? _opentelemetry_api.trace.getActiveSpan();
3542
- return await Laminar.withSpan(currentSpan ?? parentSpan, async () => await observe({ name: `stagehand.page.${methodName}` }, async (argVals) => await original.bind(this).apply(this, argVals), args));
3595
+ const input = nameArgsOrCopy(args);
3596
+ const spanName = methodName === "agentExecute" ? "stagehand.apiClient.agentExecute" : `stagehand.apiClient.${methodName}`;
3597
+ return await Laminar.withSpan(currentSpan ?? parentSpan, async () => await observe({
3598
+ name: spanName,
3599
+ input,
3600
+ spanType: "LLM",
3601
+ ignoreInput: true,
3602
+ ignoreOutput: true
3603
+ }, async () => {
3604
+ const span = _opentelemetry_api.trace.getSpan(LaminarContextManager.getContext()) ?? _opentelemetry_api.trace.getActiveSpan();
3605
+ span?.setAttribute(require_utils.TRACE_HAS_BROWSER_SESSION, true);
3606
+ if (baseProvider) span?.setAttribute("gen_ai.system", baseProvider);
3607
+ if (baseModel) span?.setAttribute("gen_ai.request.model", baseModel);
3608
+ try {
3609
+ const firstArg = args[0];
3610
+ const instruction = typeof firstArg === "string" ? firstArg : firstArg?.input ?? firstArg?.instruction;
3611
+ if (typeof instruction === "string" && instruction.length > 0) span?.setAttributes({
3612
+ "gen_ai.prompt.0.role": "user",
3613
+ "gen_ai.prompt.0.content": instruction
3614
+ });
3615
+ } catch {}
3616
+ const result = await original.bind(this).apply(this, args);
3617
+ try {
3618
+ const usage = result?.usage;
3619
+ if (usage) {
3620
+ const inputTokens = usage.input_tokens ?? usage.prompt_tokens ?? 0;
3621
+ const outputTokens = usage.output_tokens ?? usage.completion_tokens ?? 0;
3622
+ span?.setAttributes({
3623
+ "gen_ai.usage.input_tokens": inputTokens,
3624
+ "gen_ai.usage.output_tokens": outputTokens,
3625
+ "llm.usage.total_tokens": inputTokens + outputTokens
3626
+ });
3627
+ }
3628
+ if (methodName === "agentExecute") {
3629
+ const message = result?.message;
3630
+ if (message) span?.setAttributes({
3631
+ "gen_ai.completion.0.role": "assistant",
3632
+ "gen_ai.completion.0.content": typeof message === "string" ? message : JSON.stringify(message)
3633
+ });
3634
+ }
3635
+ } catch {}
3636
+ return result;
3637
+ }));
3543
3638
  });
3544
- }
3639
+ };
3640
+ wrapApiMethod("act");
3641
+ wrapApiMethod("extract");
3642
+ wrapApiMethod("observe");
3643
+ wrapApiMethod("agentExecute");
3545
3644
  }
3546
3645
  };
3547
3646
  //#endregion
@@ -5877,4 +5976,4 @@ Object.defineProperty(exports, "withTracingLevel", {
5877
5976
  }
5878
5977
  });
5879
5978
 
5880
- //# sourceMappingURL=decorators-CaTC0lJq.cjs.map
5979
+ //# sourceMappingURL=decorators-Bb2owkCc.cjs.map