@posthog/ai 3.1.1 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.esm.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import OpenAIOrignal, { AzureOpenAI } from 'openai';
2
2
  import * as uuid from 'uuid';
3
3
  import { v4 } from 'uuid';
4
- import { PassThrough } from 'stream';
5
4
  import { wrapLanguageModel } from 'ai';
6
5
  import AnthropicOriginal from '@anthropic-ai/sdk';
7
6
 
@@ -159,19 +158,16 @@ class WrappedCompletions$1 extends OpenAIOrignal.Chat.Completions {
159
158
  const parentPromise = super.create(openAIParams, options);
160
159
  if (openAIParams.stream) {
161
160
  return parentPromise.then(value => {
162
- const passThroughStream = new PassThrough({
163
- objectMode: true
164
- });
165
- let accumulatedContent = '';
166
- let usage = {
167
- inputTokens: 0,
168
- outputTokens: 0
169
- };
170
161
  if ('tee' in value) {
171
- const openAIStream = value;
162
+ const [stream1, stream2] = value.tee();
172
163
  (async () => {
173
164
  try {
174
- for await (const chunk of openAIStream) {
165
+ let accumulatedContent = '';
166
+ let usage = {
167
+ inputTokens: 0,
168
+ outputTokens: 0
169
+ };
170
+ for await (const chunk of stream1) {
175
171
  const delta = chunk?.choices?.[0]?.delta?.content ?? '';
176
172
  accumulatedContent += delta;
177
173
  if (chunk.usage) {
@@ -180,7 +176,6 @@ class WrappedCompletions$1 extends OpenAIOrignal.Chat.Completions {
180
176
  outputTokens: chunk.usage.completion_tokens ?? 0
181
177
  };
182
178
  }
183
- passThroughStream.write(chunk);
184
179
  }
185
180
  const latency = (Date.now() - startTime) / 1000;
186
181
  sendEventToPosthog({
@@ -200,9 +195,7 @@ class WrappedCompletions$1 extends OpenAIOrignal.Chat.Completions {
200
195
  httpStatus: 200,
201
196
  usage
202
197
  });
203
- passThroughStream.end();
204
198
  } catch (error) {
205
- // error handling
206
199
  sendEventToPosthog({
207
200
  client: this.phClient,
208
201
  distinctId: posthogDistinctId ?? traceId,
@@ -222,11 +215,12 @@ class WrappedCompletions$1 extends OpenAIOrignal.Chat.Completions {
222
215
  isError: true,
223
216
  error: JSON.stringify(error)
224
217
  });
225
- passThroughStream.emit('error', error);
226
218
  }
227
219
  })();
220
+ // Return the other stream to the user
221
+ return stream2;
228
222
  }
229
- return passThroughStream;
223
+ return value;
230
224
  });
231
225
  } else {
232
226
  const wrappedPromise = parentPromise.then(result => {
@@ -316,9 +310,6 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
316
310
  const parentPromise = super.create(openAIParams, options);
317
311
  if (openAIParams.stream) {
318
312
  return parentPromise.then(value => {
319
- const passThroughStream = new PassThrough({
320
- objectMode: true
321
- });
322
313
  let accumulatedContent = '';
323
314
  let usage = {
324
315
  inputTokens: 0,
@@ -326,10 +317,10 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
326
317
  };
327
318
  let model = openAIParams.model;
328
319
  if ('tee' in value) {
329
- const openAIStream = value;
320
+ const [stream1, stream2] = value.tee();
330
321
  (async () => {
331
322
  try {
332
- for await (const chunk of openAIStream) {
323
+ for await (const chunk of stream1) {
333
324
  const delta = chunk?.choices?.[0]?.delta?.content ?? '';
334
325
  accumulatedContent += delta;
335
326
  if (chunk.usage) {
@@ -341,7 +332,6 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
341
332
  outputTokens: chunk.usage.completion_tokens ?? 0
342
333
  };
343
334
  }
344
- passThroughStream.write(chunk);
345
335
  }
346
336
  const latency = (Date.now() - startTime) / 1000;
347
337
  sendEventToPosthog({
@@ -361,7 +351,6 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
361
351
  httpStatus: 200,
362
352
  usage
363
353
  });
364
- passThroughStream.end();
365
354
  } catch (error) {
366
355
  // error handling
367
356
  sendEventToPosthog({
@@ -383,11 +372,12 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
383
372
  isError: true,
384
373
  error: JSON.stringify(error)
385
374
  });
386
- passThroughStream.emit('error', error);
387
375
  }
388
376
  })();
377
+ // Return the other stream to the user
378
+ return stream2;
389
379
  }
390
- return passThroughStream;
380
+ return value;
391
381
  });
392
382
  } else {
393
383
  const wrappedPromise = parentPromise.then(result => {
@@ -674,19 +664,16 @@ class WrappedMessages extends AnthropicOriginal.Messages {
674
664
  const parentPromise = super.create(anthropicParams, options);
675
665
  if (anthropicParams.stream) {
676
666
  return parentPromise.then(value => {
677
- const passThroughStream = new PassThrough({
678
- objectMode: true
679
- });
680
667
  let accumulatedContent = '';
681
668
  const usage = {
682
669
  inputTokens: 0,
683
670
  outputTokens: 0
684
671
  };
685
672
  if ('tee' in value) {
686
- const anthropicStream = value;
673
+ const [stream1, stream2] = value.tee();
687
674
  (async () => {
688
675
  try {
689
- for await (const chunk of anthropicStream) {
676
+ for await (const chunk of stream1) {
690
677
  if ('delta' in chunk) {
691
678
  if ('text' in chunk.delta) {
692
679
  const delta = chunk?.delta?.text ?? '';
@@ -699,7 +686,6 @@ class WrappedMessages extends AnthropicOriginal.Messages {
699
686
  if ('usage' in chunk) {
700
687
  usage.outputTokens = chunk.usage.output_tokens ?? 0;
701
688
  }
702
- passThroughStream.write(chunk);
703
689
  }
704
690
  const latency = (Date.now() - startTime) / 1000;
705
691
  sendEventToPosthog({
@@ -719,7 +705,6 @@ class WrappedMessages extends AnthropicOriginal.Messages {
719
705
  httpStatus: 200,
720
706
  usage
721
707
  });
722
- passThroughStream.end();
723
708
  } catch (error) {
724
709
  // error handling
725
710
  sendEventToPosthog({
@@ -741,11 +726,12 @@ class WrappedMessages extends AnthropicOriginal.Messages {
741
726
  isError: true,
742
727
  error: JSON.stringify(error)
743
728
  });
744
- passThroughStream.emit('error', error);
745
729
  }
746
730
  })();
731
+ // Return the other stream to the user
732
+ return stream2;
747
733
  }
748
- return passThroughStream;
734
+ return value;
749
735
  });
750
736
  } else {
751
737
  const wrappedPromise = parentPromise.then(result => {