opik 0.1.0 → 0.1.2

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.
@@ -4666,6 +4666,12 @@ declare class Span {
4666
4666
  private opik;
4667
4667
  constructor(data: SavedSpan, opik: OpikClient);
4668
4668
  end: () => this;
4669
+ score: (score: {
4670
+ name: string;
4671
+ categoryName?: string;
4672
+ value: number;
4673
+ reason?: string;
4674
+ }) => void;
4669
4675
  update: (updates: Omit<SpanUpdate$1, "traceId" | "parentSpanId" | "projectId" | "projectName">) => this;
4670
4676
  }
4671
4677
 
@@ -4681,14 +4687,17 @@ declare class Trace {
4681
4687
  private spans;
4682
4688
  constructor(data: SavedTrace, opik: OpikClient);
4683
4689
  end: () => this;
4690
+ score: (score: {
4691
+ name: string;
4692
+ categoryName?: string;
4693
+ value: number;
4694
+ reason?: string;
4695
+ }) => void;
4684
4696
  span: (spanData: SpanData) => Span;
4685
4697
  update: (updates: Omit<TraceUpdate, "projectId">) => this;
4686
4698
  }
4687
4699
 
4688
- type CreateEntity = {
4689
- id: string;
4690
- };
4691
- declare abstract class BatchQueue<EntityData = {}> {
4700
+ declare abstract class BatchQueue<EntityData = object, EntityId = string> {
4692
4701
  private readonly createQueue;
4693
4702
  private readonly updateQueue;
4694
4703
  private readonly deleteQueue;
@@ -4700,13 +4709,14 @@ declare abstract class BatchQueue<EntityData = {}> {
4700
4709
  name?: string;
4701
4710
  });
4702
4711
  protected abstract createEntities(entities: EntityData[]): Promise<void>;
4703
- protected abstract getEntity(id: string): Promise<EntityData | undefined>;
4704
- protected abstract updateEntity(id: string, updates: Partial<EntityData>): Promise<void>;
4705
- protected abstract deleteEntities(ids: string[]): Promise<void>;
4706
- create: (entity: CreateEntity & EntityData) => void;
4707
- get: (id: string) => Promise<EntityData | undefined>;
4708
- update: (id: string, updates: Partial<EntityData>) => void;
4709
- delete: (id: string) => void;
4712
+ protected abstract getEntity(id: EntityId): Promise<EntityData | undefined>;
4713
+ protected abstract updateEntity(id: EntityId, updates: Partial<EntityData>): Promise<void>;
4714
+ protected abstract deleteEntities(ids: EntityId[]): Promise<void>;
4715
+ protected abstract getId(entity: EntityData): EntityId;
4716
+ create: (entity: EntityData) => void;
4717
+ get: (id: EntityId) => Promise<EntityData | undefined>;
4718
+ update: (id: EntityId, updates: Partial<EntityData>) => void;
4719
+ delete: (id: EntityId) => void;
4710
4720
  flush: () => Promise<void>;
4711
4721
  }
4712
4722
 
@@ -4716,21 +4726,57 @@ type SpanUpdate = Partial<SavedSpan> & {
4716
4726
  declare class SpanBatchQueue extends BatchQueue<SavedSpan> {
4717
4727
  private readonly api;
4718
4728
  constructor(api: OpikApiClient, delay?: number);
4729
+ protected getId(entity: SavedSpan): string;
4719
4730
  protected createEntities(spans: SavedSpan[]): Promise<void>;
4720
4731
  protected getEntity(id: string): Promise<SavedSpan>;
4721
4732
  protected updateEntity(id: string, updates: SpanUpdate): Promise<void>;
4722
4733
  protected deleteEntities(ids: string[]): Promise<void>;
4723
4734
  }
4724
4735
 
4736
+ type FeedbackScoreId$1 = {
4737
+ id: string;
4738
+ name: string;
4739
+ };
4740
+ declare class SpanFeedbackScoresBatchQueue extends BatchQueue<FeedbackScoreBatchItem, FeedbackScoreId$1> {
4741
+ private readonly api;
4742
+ constructor(api: OpikApiClient, delay?: number);
4743
+ protected getId(entity: FeedbackScoreBatchItem): {
4744
+ id: string;
4745
+ name: string;
4746
+ };
4747
+ protected createEntities(scores: FeedbackScoreBatchItem[]): Promise<void>;
4748
+ protected getEntity(): Promise<FeedbackScoreBatchItem | undefined>;
4749
+ protected updateEntity(): Promise<void>;
4750
+ protected deleteEntities(scoreIds: FeedbackScoreId$1[]): Promise<void>;
4751
+ }
4752
+
4725
4753
  declare class TraceBatchQueue extends BatchQueue<SavedTrace> {
4726
4754
  private readonly api;
4727
4755
  constructor(api: OpikApiClient, delay?: number);
4756
+ protected getId(entity: SavedTrace): string;
4728
4757
  protected createEntities(traces: SavedTrace[]): Promise<void>;
4729
4758
  protected getEntity(id: string): Promise<SavedTrace>;
4730
4759
  protected updateEntity(id: string, updates: Partial<SavedTrace>): Promise<void>;
4731
4760
  protected deleteEntities(ids: string[]): Promise<void>;
4732
4761
  }
4733
4762
 
4763
+ type FeedbackScoreId = {
4764
+ id: string;
4765
+ name: string;
4766
+ };
4767
+ declare class TraceFeedbackScoresBatchQueue extends BatchQueue<FeedbackScoreBatchItem, FeedbackScoreId> {
4768
+ private readonly api;
4769
+ constructor(api: OpikApiClient, delay?: number);
4770
+ protected getId(entity: FeedbackScoreBatchItem): {
4771
+ id: string;
4772
+ name: string;
4773
+ };
4774
+ protected createEntities(scores: FeedbackScoreBatchItem[]): Promise<void>;
4775
+ protected getEntity(): Promise<FeedbackScoreBatchItem | undefined>;
4776
+ protected updateEntity(): Promise<void>;
4777
+ protected deleteEntities(scoreIds: FeedbackScoreId[]): Promise<void>;
4778
+ }
4779
+
4734
4780
  interface TraceData extends Omit<Trace$1, "startTime"> {
4735
4781
  startTime?: Date;
4736
4782
  }
@@ -4739,9 +4785,11 @@ declare class OpikClient {
4739
4785
  config: OpikConfig;
4740
4786
  spanBatchQueue: SpanBatchQueue;
4741
4787
  traceBatchQueue: TraceBatchQueue;
4788
+ spanFeedbackScoresBatchQueue: SpanFeedbackScoresBatchQueue;
4789
+ traceFeedbackScoresBatchQueue: TraceFeedbackScoresBatchQueue;
4742
4790
  constructor(explicitConfig?: Partial<OpikConfig>);
4743
4791
  trace: (traceData: TraceData) => Trace;
4744
4792
  flush: () => Promise<void>;
4745
4793
  }
4746
4794
 
4747
- export { OpikClient as O, SpanType as S, Trace as T, type OpikConfig as a, Span as b };
4795
+ export { OpikClient as O, Span as S, Trace as T, SpanType as a, type OpikConfig as b };
@@ -4666,6 +4666,12 @@ declare class Span {
4666
4666
  private opik;
4667
4667
  constructor(data: SavedSpan, opik: OpikClient);
4668
4668
  end: () => this;
4669
+ score: (score: {
4670
+ name: string;
4671
+ categoryName?: string;
4672
+ value: number;
4673
+ reason?: string;
4674
+ }) => void;
4669
4675
  update: (updates: Omit<SpanUpdate$1, "traceId" | "parentSpanId" | "projectId" | "projectName">) => this;
4670
4676
  }
4671
4677
 
@@ -4681,14 +4687,17 @@ declare class Trace {
4681
4687
  private spans;
4682
4688
  constructor(data: SavedTrace, opik: OpikClient);
4683
4689
  end: () => this;
4690
+ score: (score: {
4691
+ name: string;
4692
+ categoryName?: string;
4693
+ value: number;
4694
+ reason?: string;
4695
+ }) => void;
4684
4696
  span: (spanData: SpanData) => Span;
4685
4697
  update: (updates: Omit<TraceUpdate, "projectId">) => this;
4686
4698
  }
4687
4699
 
4688
- type CreateEntity = {
4689
- id: string;
4690
- };
4691
- declare abstract class BatchQueue<EntityData = {}> {
4700
+ declare abstract class BatchQueue<EntityData = object, EntityId = string> {
4692
4701
  private readonly createQueue;
4693
4702
  private readonly updateQueue;
4694
4703
  private readonly deleteQueue;
@@ -4700,13 +4709,14 @@ declare abstract class BatchQueue<EntityData = {}> {
4700
4709
  name?: string;
4701
4710
  });
4702
4711
  protected abstract createEntities(entities: EntityData[]): Promise<void>;
4703
- protected abstract getEntity(id: string): Promise<EntityData | undefined>;
4704
- protected abstract updateEntity(id: string, updates: Partial<EntityData>): Promise<void>;
4705
- protected abstract deleteEntities(ids: string[]): Promise<void>;
4706
- create: (entity: CreateEntity & EntityData) => void;
4707
- get: (id: string) => Promise<EntityData | undefined>;
4708
- update: (id: string, updates: Partial<EntityData>) => void;
4709
- delete: (id: string) => void;
4712
+ protected abstract getEntity(id: EntityId): Promise<EntityData | undefined>;
4713
+ protected abstract updateEntity(id: EntityId, updates: Partial<EntityData>): Promise<void>;
4714
+ protected abstract deleteEntities(ids: EntityId[]): Promise<void>;
4715
+ protected abstract getId(entity: EntityData): EntityId;
4716
+ create: (entity: EntityData) => void;
4717
+ get: (id: EntityId) => Promise<EntityData | undefined>;
4718
+ update: (id: EntityId, updates: Partial<EntityData>) => void;
4719
+ delete: (id: EntityId) => void;
4710
4720
  flush: () => Promise<void>;
4711
4721
  }
4712
4722
 
@@ -4716,21 +4726,57 @@ type SpanUpdate = Partial<SavedSpan> & {
4716
4726
  declare class SpanBatchQueue extends BatchQueue<SavedSpan> {
4717
4727
  private readonly api;
4718
4728
  constructor(api: OpikApiClient, delay?: number);
4729
+ protected getId(entity: SavedSpan): string;
4719
4730
  protected createEntities(spans: SavedSpan[]): Promise<void>;
4720
4731
  protected getEntity(id: string): Promise<SavedSpan>;
4721
4732
  protected updateEntity(id: string, updates: SpanUpdate): Promise<void>;
4722
4733
  protected deleteEntities(ids: string[]): Promise<void>;
4723
4734
  }
4724
4735
 
4736
+ type FeedbackScoreId$1 = {
4737
+ id: string;
4738
+ name: string;
4739
+ };
4740
+ declare class SpanFeedbackScoresBatchQueue extends BatchQueue<FeedbackScoreBatchItem, FeedbackScoreId$1> {
4741
+ private readonly api;
4742
+ constructor(api: OpikApiClient, delay?: number);
4743
+ protected getId(entity: FeedbackScoreBatchItem): {
4744
+ id: string;
4745
+ name: string;
4746
+ };
4747
+ protected createEntities(scores: FeedbackScoreBatchItem[]): Promise<void>;
4748
+ protected getEntity(): Promise<FeedbackScoreBatchItem | undefined>;
4749
+ protected updateEntity(): Promise<void>;
4750
+ protected deleteEntities(scoreIds: FeedbackScoreId$1[]): Promise<void>;
4751
+ }
4752
+
4725
4753
  declare class TraceBatchQueue extends BatchQueue<SavedTrace> {
4726
4754
  private readonly api;
4727
4755
  constructor(api: OpikApiClient, delay?: number);
4756
+ protected getId(entity: SavedTrace): string;
4728
4757
  protected createEntities(traces: SavedTrace[]): Promise<void>;
4729
4758
  protected getEntity(id: string): Promise<SavedTrace>;
4730
4759
  protected updateEntity(id: string, updates: Partial<SavedTrace>): Promise<void>;
4731
4760
  protected deleteEntities(ids: string[]): Promise<void>;
4732
4761
  }
4733
4762
 
4763
+ type FeedbackScoreId = {
4764
+ id: string;
4765
+ name: string;
4766
+ };
4767
+ declare class TraceFeedbackScoresBatchQueue extends BatchQueue<FeedbackScoreBatchItem, FeedbackScoreId> {
4768
+ private readonly api;
4769
+ constructor(api: OpikApiClient, delay?: number);
4770
+ protected getId(entity: FeedbackScoreBatchItem): {
4771
+ id: string;
4772
+ name: string;
4773
+ };
4774
+ protected createEntities(scores: FeedbackScoreBatchItem[]): Promise<void>;
4775
+ protected getEntity(): Promise<FeedbackScoreBatchItem | undefined>;
4776
+ protected updateEntity(): Promise<void>;
4777
+ protected deleteEntities(scoreIds: FeedbackScoreId[]): Promise<void>;
4778
+ }
4779
+
4734
4780
  interface TraceData extends Omit<Trace$1, "startTime"> {
4735
4781
  startTime?: Date;
4736
4782
  }
@@ -4739,9 +4785,11 @@ declare class OpikClient {
4739
4785
  config: OpikConfig;
4740
4786
  spanBatchQueue: SpanBatchQueue;
4741
4787
  traceBatchQueue: TraceBatchQueue;
4788
+ spanFeedbackScoresBatchQueue: SpanFeedbackScoresBatchQueue;
4789
+ traceFeedbackScoresBatchQueue: TraceFeedbackScoresBatchQueue;
4742
4790
  constructor(explicitConfig?: Partial<OpikConfig>);
4743
4791
  trace: (traceData: TraceData) => Trace;
4744
4792
  flush: () => Promise<void>;
4745
4793
  }
4746
4794
 
4747
- export { OpikClient as O, SpanType as S, Trace as T, type OpikConfig as a, Span as b };
4795
+ export { OpikClient as O, Span as S, Trace as T, SpanType as a, type OpikConfig as b };
@@ -11945,6 +11945,15 @@ var Span2 = class {
11945
11945
  this.end = () => {
11946
11946
  return this.update({ endTime: /* @__PURE__ */ new Date() });
11947
11947
  };
11948
+ this.score = (score) => {
11949
+ var _a;
11950
+ this.opik.spanFeedbackScoresBatchQueue.create({
11951
+ ...score,
11952
+ projectName: (_a = this.data.projectName) != null ? _a : this.opik.config.projectName,
11953
+ id: this.data.id,
11954
+ source: "sdk"
11955
+ });
11956
+ };
11948
11957
  this.update = (updates) => {
11949
11958
  var _a;
11950
11959
  const spanUpdates = {
@@ -11969,6 +11978,15 @@ var Trace2 = class {
11969
11978
  this.end = () => {
11970
11979
  return this.update({ endTime: /* @__PURE__ */ new Date() });
11971
11980
  };
11981
+ this.score = (score) => {
11982
+ var _a;
11983
+ this.opik.traceFeedbackScoresBatchQueue.create({
11984
+ ...score,
11985
+ projectName: (_a = this.data.projectName) != null ? _a : this.opik.config.projectName,
11986
+ id: this.data.id,
11987
+ source: "sdk"
11988
+ });
11989
+ };
11972
11990
  this.span = (spanData) => {
11973
11991
  var _a, _b;
11974
11992
  const projectName = (_b = (_a = this.data.projectName) != null ? _a : spanData.projectName) != null ? _b : this.opik.config.projectName;
@@ -12060,7 +12078,8 @@ var BatchQueue = class {
12060
12078
  name = "BatchQueue"
12061
12079
  }) {
12062
12080
  this.create = (entity) => {
12063
- this.createQueue.add(entity.id, entity);
12081
+ const id = this.getId(entity);
12082
+ this.createQueue.add(id, entity);
12064
12083
  };
12065
12084
  this.get = async (id) => {
12066
12085
  const entity = this.createQueue.queue.get(id);
@@ -12129,6 +12148,9 @@ var SpanBatchQueue = class extends BatchQueue {
12129
12148
  super({ delay, enableDeleteBatch: false, name: "SpanBatchQueue" });
12130
12149
  this.api = api;
12131
12150
  }
12151
+ getId(entity) {
12152
+ return entity.id;
12153
+ }
12132
12154
  async createEntities(spans) {
12133
12155
  await this.api.spans.createSpans({ spans });
12134
12156
  }
@@ -12145,12 +12167,46 @@ var SpanBatchQueue = class extends BatchQueue {
12145
12167
  }
12146
12168
  };
12147
12169
 
12170
+ // src/opik/client/SpanFeedbackScoresBatchQueue.ts
12171
+ var SpanFeedbackScoresBatchQueue = class extends BatchQueue {
12172
+ constructor(api, delay) {
12173
+ super({
12174
+ delay,
12175
+ enableDeleteBatch: false,
12176
+ name: "SpanFeedbackScoresBatchQueue"
12177
+ });
12178
+ this.api = api;
12179
+ }
12180
+ getId(entity) {
12181
+ return { id: entity.id, name: entity.name };
12182
+ }
12183
+ async createEntities(scores) {
12184
+ await this.api.spans.scoreBatchOfSpans({ scores });
12185
+ }
12186
+ async getEntity() {
12187
+ throw new Error("Not implemented");
12188
+ }
12189
+ async updateEntity() {
12190
+ throw new Error("Not implemented");
12191
+ }
12192
+ async deleteEntities(scoreIds) {
12193
+ for (const scoreId of scoreIds) {
12194
+ await this.api.spans.deleteSpanFeedbackScore(scoreId.id, {
12195
+ name: scoreId.name
12196
+ });
12197
+ }
12198
+ }
12199
+ };
12200
+
12148
12201
  // src/opik/client/TraceBatchQueue.ts
12149
12202
  var TraceBatchQueue = class extends BatchQueue {
12150
12203
  constructor(api, delay) {
12151
12204
  super({ delay, name: "TraceBatchQueue" });
12152
12205
  this.api = api;
12153
12206
  }
12207
+ getId(entity) {
12208
+ return entity.id;
12209
+ }
12154
12210
  async createEntities(traces) {
12155
12211
  await this.api.traces.createTraces({ traces });
12156
12212
  }
@@ -12165,6 +12221,37 @@ var TraceBatchQueue = class extends BatchQueue {
12165
12221
  }
12166
12222
  };
12167
12223
 
12224
+ // src/opik/client/TraceFeedbackScoresBatchQueue.ts
12225
+ var TraceFeedbackScoresBatchQueue = class extends BatchQueue {
12226
+ constructor(api, delay) {
12227
+ super({
12228
+ delay,
12229
+ enableDeleteBatch: false,
12230
+ name: "TraceFeedbackScoresBatchQueue"
12231
+ });
12232
+ this.api = api;
12233
+ }
12234
+ getId(entity) {
12235
+ return { id: entity.id, name: entity.name };
12236
+ }
12237
+ async createEntities(scores) {
12238
+ await this.api.traces.scoreBatchOfTraces({ scores });
12239
+ }
12240
+ async getEntity() {
12241
+ throw new Error("Not implemented");
12242
+ }
12243
+ async updateEntity() {
12244
+ throw new Error("Not implemented");
12245
+ }
12246
+ async deleteEntities(scoreIds) {
12247
+ for (const scoreId of scoreIds) {
12248
+ await this.api.traces.deleteTraceFeedbackScore(scoreId.id, {
12249
+ name: scoreId.name
12250
+ });
12251
+ }
12252
+ }
12253
+ };
12254
+
12168
12255
  // src/opik/client/Client.ts
12169
12256
  var clients = [];
12170
12257
  var OpikClient = class {
@@ -12187,6 +12274,8 @@ var OpikClient = class {
12187
12274
  this.flush = async () => {
12188
12275
  await this.traceBatchQueue.flush();
12189
12276
  await this.spanBatchQueue.flush();
12277
+ await this.traceFeedbackScoresBatchQueue.flush();
12278
+ await this.spanFeedbackScoresBatchQueue.flush();
12190
12279
  };
12191
12280
  this.config = loadConfig(explicitConfig);
12192
12281
  this.api = new OpikApiClient({
@@ -12196,10 +12285,24 @@ var OpikClient = class {
12196
12285
  });
12197
12286
  this.spanBatchQueue = new SpanBatchQueue(this.api);
12198
12287
  this.traceBatchQueue = new TraceBatchQueue(this.api);
12288
+ this.spanFeedbackScoresBatchQueue = new SpanFeedbackScoresBatchQueue(
12289
+ this.api
12290
+ );
12291
+ this.traceFeedbackScoresBatchQueue = new TraceFeedbackScoresBatchQueue(
12292
+ this.api
12293
+ );
12199
12294
  clients.push(this);
12200
12295
  }
12201
12296
  };
12297
+ var DEFAULT_TRACK_NAME = "track.decorator";
12202
12298
  var trackStorage = new AsyncLocalStorage();
12299
+ var getTrackContext = () => {
12300
+ const { span, trace } = trackStorage.getStore() || {};
12301
+ if (!span || !trace) {
12302
+ return void 0;
12303
+ }
12304
+ return { span, trace };
12305
+ };
12203
12306
  function isPromise(obj) {
12204
12307
  return !!obj && (typeof obj === "object" || typeof obj === "function") && typeof obj.then === "function";
12205
12308
  }
@@ -12225,76 +12328,139 @@ function logSpan({
12225
12328
  });
12226
12329
  return { span, trace: spanTrace };
12227
12330
  }
12228
- function withTrack({
12331
+ function logStart({
12332
+ args,
12333
+ span,
12334
+ trace
12335
+ }) {
12336
+ if (args.length === 0) {
12337
+ return;
12338
+ }
12339
+ const input = { arguments: args };
12340
+ span.update({ input });
12341
+ if (trace) {
12342
+ trace.update({ input });
12343
+ }
12344
+ }
12345
+ function logSuccess({
12346
+ result,
12347
+ span,
12348
+ trace
12349
+ }) {
12350
+ const output = typeof result === "object" ? result : { result };
12351
+ const endTime = /* @__PURE__ */ new Date();
12352
+ span.update({ endTime, output });
12353
+ if (trace) {
12354
+ trace.update({ endTime, output });
12355
+ }
12356
+ }
12357
+ function logError({
12358
+ span,
12359
+ error,
12360
+ trace
12361
+ }) {
12362
+ var _a, _b;
12363
+ if (error instanceof Error) {
12364
+ span.update({
12365
+ errorInfo: {
12366
+ message: error.message,
12367
+ exceptionType: error.name,
12368
+ traceback: (_a = error.stack) != null ? _a : ""
12369
+ }
12370
+ });
12371
+ }
12372
+ span.end();
12373
+ if (trace) {
12374
+ trace.update({
12375
+ errorInfo: {
12376
+ message: error.message,
12377
+ exceptionType: error.name,
12378
+ traceback: (_b = error.stack) != null ? _b : ""
12379
+ }
12380
+ });
12381
+ trace.end();
12382
+ }
12383
+ }
12384
+ function executeTrack({
12229
12385
  name,
12230
12386
  projectName,
12231
12387
  type
12232
- } = {}) {
12233
- return function trackDecorator(originalFn) {
12234
- const wrappedFn = function(...args) {
12235
- const context = trackStorage.getStore();
12236
- const { span, trace } = logSpan({
12237
- name: name != null ? name : originalFn.name,
12238
- parentSpan: context == null ? void 0 : context.span,
12239
- projectName,
12240
- trace: context == null ? void 0 : context.trace,
12241
- type
12242
- });
12243
- const isRootSpan = !context;
12244
- const fnThis = this;
12245
- return trackStorage.run({ span, trace }, () => {
12246
- try {
12247
- const result = originalFn.apply(fnThis, args);
12248
- if (isPromise(result)) {
12249
- return result.then(
12250
- (res) => {
12251
- span.end();
12252
- if (isRootSpan) {
12253
- trace.end();
12254
- }
12255
- return res;
12256
- },
12257
- (err) => {
12258
- span.end();
12259
- if (isRootSpan) {
12260
- trace.end();
12261
- }
12262
- throw err;
12263
- }
12264
- );
12265
- }
12266
- span.end();
12267
- if (isRootSpan) {
12268
- trace.end();
12269
- }
12270
- return result;
12271
- } catch (e) {
12272
- if (isRootSpan) {
12273
- trace.end();
12274
- }
12275
- span.end();
12276
- throw e;
12388
+ } = {}, originalFn) {
12389
+ const wrappedFn = function(...args) {
12390
+ const context = trackStorage.getStore();
12391
+ const { span, trace } = logSpan({
12392
+ name: name != null ? name : originalFn.name || DEFAULT_TRACK_NAME,
12393
+ parentSpan: context == null ? void 0 : context.span,
12394
+ projectName,
12395
+ trace: context == null ? void 0 : context.trace,
12396
+ type
12397
+ });
12398
+ const isRootSpan = !context;
12399
+ const fnThis = this;
12400
+ return trackStorage.run({ span, trace }, () => {
12401
+ try {
12402
+ logStart({ args, span, trace: isRootSpan ? trace : void 0 });
12403
+ const result = originalFn.apply(fnThis, args);
12404
+ if (isPromise(result)) {
12405
+ return result.then(
12406
+ (res) => {
12407
+ logSuccess({
12408
+ span,
12409
+ result: res,
12410
+ trace: isRootSpan ? trace : void 0
12411
+ });
12412
+ return res;
12413
+ },
12414
+ (err) => {
12415
+ logError({
12416
+ span,
12417
+ error: err,
12418
+ trace: isRootSpan ? trace : void 0
12419
+ });
12420
+ throw err;
12421
+ }
12422
+ );
12277
12423
  }
12278
- });
12279
- };
12280
- return wrappedFn;
12424
+ logSuccess({
12425
+ span,
12426
+ result,
12427
+ trace: isRootSpan ? trace : void 0
12428
+ });
12429
+ return result;
12430
+ } catch (error) {
12431
+ logError({
12432
+ span,
12433
+ error,
12434
+ trace: isRootSpan ? trace : void 0
12435
+ });
12436
+ throw error;
12437
+ }
12438
+ });
12281
12439
  };
12440
+ return wrappedFn;
12282
12441
  }
12283
- function track(options = {}) {
12442
+ function track(optionsOrOriginalFunction, originalFunction) {
12443
+ if (typeof optionsOrOriginalFunction === "function") {
12444
+ return executeTrack({}, optionsOrOriginalFunction);
12445
+ }
12446
+ const options = optionsOrOriginalFunction;
12447
+ if (originalFunction) {
12448
+ return executeTrack(options, originalFunction);
12449
+ }
12284
12450
  return function(...args) {
12285
12451
  if (args.length === 2 && typeof args[1] === "object" && args[1] !== null && "kind" in args[1]) {
12286
12452
  const [originalMethod2, context] = args;
12287
12453
  if (context.kind !== "method") {
12288
12454
  throw new Error("track decorator is only applicable to methods");
12289
12455
  }
12290
- return withTrack(options)(originalMethod2);
12456
+ return executeTrack(options, originalMethod2);
12291
12457
  }
12292
- const [target, propertyKey, descriptor] = args;
12458
+ const [, , descriptor] = args;
12293
12459
  if (!descriptor || typeof descriptor.value !== "function") {
12294
12460
  throw new Error("track decorator can only be applied to methods");
12295
12461
  }
12296
12462
  const originalMethod = descriptor.value;
12297
- descriptor.value = withTrack(options)(originalMethod);
12463
+ descriptor.value = executeTrack(options, originalMethod);
12298
12464
  return descriptor;
12299
12465
  };
12300
12466
  }
@@ -12308,6 +12474,6 @@ var flushAll = async () => {
12308
12474
  ]);
12309
12475
  };
12310
12476
 
12311
- export { OpikClient, flushAll, track, trackOpikClient, withTrack };
12312
- //# sourceMappingURL=chunk-MCPL44VT.js.map
12313
- //# sourceMappingURL=chunk-MCPL44VT.js.map
12477
+ export { OpikClient, flushAll, getTrackContext, track, trackOpikClient };
12478
+ //# sourceMappingURL=chunk-ZB4ZJRLY.js.map
12479
+ //# sourceMappingURL=chunk-ZB4ZJRLY.js.map