@posthog/ai 6.3.2 → 6.4.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/dist/anthropic/index.cjs +48 -24
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.d.ts +2 -2
- package/dist/anthropic/index.mjs +48 -24
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +51 -30
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.mjs +51 -30
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +69 -95
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +19 -19
- package/dist/index.mjs +69 -95
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +1 -1
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.mjs +1 -1
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/openai/index.cjs +40 -41
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.mjs +40 -41
- package/dist/openai/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +1 -1
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +1 -1
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +10 -10
package/dist/index.d.ts
CHANGED
|
@@ -3,8 +3,7 @@ import { PostHog } from 'posthog-node';
|
|
|
3
3
|
import { Stream } from 'openai/streaming';
|
|
4
4
|
import { ParsedResponse } from 'openai/resources/responses/responses';
|
|
5
5
|
import { LanguageModelV2 } from '@ai-sdk/provider';
|
|
6
|
-
import AnthropicOriginal from '@anthropic-ai/sdk';
|
|
7
|
-
import { RequestOptions as RequestOptions$2, APIPromise as APIPromise$1 } from '@anthropic-ai/sdk/core';
|
|
6
|
+
import AnthropicOriginal, { APIPromise as APIPromise$1 } from '@anthropic-ai/sdk';
|
|
8
7
|
import { Stream as Stream$1 } from '@anthropic-ai/sdk/streaming';
|
|
9
8
|
import { GoogleGenAI, GenerateContentParameters, GenerateContentResponse } from '@google/genai';
|
|
10
9
|
import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
|
|
@@ -54,7 +53,7 @@ interface MonitoringOpenAIConfig$1 extends ClientOptions$1 {
|
|
|
54
53
|
posthog: PostHog;
|
|
55
54
|
baseURL?: string;
|
|
56
55
|
}
|
|
57
|
-
type RequestOptions$
|
|
56
|
+
type RequestOptions$2 = Record<string, any>;
|
|
58
57
|
declare class PostHogOpenAI extends OpenAI {
|
|
59
58
|
private readonly phClient;
|
|
60
59
|
chat: WrappedChat$1;
|
|
@@ -70,24 +69,24 @@ declare class WrappedCompletions$1 extends Completions {
|
|
|
70
69
|
private readonly phClient;
|
|
71
70
|
private readonly baseURL;
|
|
72
71
|
constructor(client: OpenAI, phClient: PostHog);
|
|
73
|
-
create(body: ChatCompletionCreateParamsNonStreaming$1 & MonitoringParams, options?: RequestOptions$
|
|
74
|
-
create(body: ChatCompletionCreateParamsStreaming$1 & MonitoringParams, options?: RequestOptions$
|
|
75
|
-
create(body: ChatCompletionCreateParamsBase$1 & MonitoringParams, options?: RequestOptions$
|
|
72
|
+
create(body: ChatCompletionCreateParamsNonStreaming$1 & MonitoringParams, options?: RequestOptions$2): APIPromise<ChatCompletion$1>;
|
|
73
|
+
create(body: ChatCompletionCreateParamsStreaming$1 & MonitoringParams, options?: RequestOptions$2): APIPromise<Stream<ChatCompletionChunk$1>>;
|
|
74
|
+
create(body: ChatCompletionCreateParamsBase$1 & MonitoringParams, options?: RequestOptions$2): APIPromise<ChatCompletion$1 | Stream<ChatCompletionChunk$1>>;
|
|
76
75
|
}
|
|
77
76
|
declare class WrappedResponses extends Responses {
|
|
78
77
|
private readonly phClient;
|
|
79
78
|
private readonly baseURL;
|
|
80
79
|
constructor(client: OpenAI, phClient: PostHog);
|
|
81
|
-
create(body: ResponsesCreateParamsNonStreaming & MonitoringParams, options?: RequestOptions$
|
|
82
|
-
create(body: ResponsesCreateParamsStreaming & MonitoringParams, options?: RequestOptions$
|
|
83
|
-
create(body: ResponsesCreateParamsBase & MonitoringParams, options?: RequestOptions$
|
|
84
|
-
parse<Params extends ResponsesCreateParamsBase, ParsedT = any>(body: Params & MonitoringParams, options?: RequestOptions$
|
|
80
|
+
create(body: ResponsesCreateParamsNonStreaming & MonitoringParams, options?: RequestOptions$2): APIPromise<OpenAI.Responses.Response>;
|
|
81
|
+
create(body: ResponsesCreateParamsStreaming & MonitoringParams, options?: RequestOptions$2): APIPromise<Stream<OpenAI.Responses.ResponseStreamEvent>>;
|
|
82
|
+
create(body: ResponsesCreateParamsBase & MonitoringParams, options?: RequestOptions$2): APIPromise<OpenAI.Responses.Response | Stream<OpenAI.Responses.ResponseStreamEvent>>;
|
|
83
|
+
parse<Params extends ResponsesCreateParamsBase, ParsedT = any>(body: Params & MonitoringParams, options?: RequestOptions$2): APIPromise<ParsedResponse<ParsedT>>;
|
|
85
84
|
}
|
|
86
85
|
declare class WrappedEmbeddings$1 extends Embeddings {
|
|
87
86
|
private readonly phClient;
|
|
88
87
|
private readonly baseURL;
|
|
89
88
|
constructor(client: OpenAI, phClient: PostHog);
|
|
90
|
-
create(body: EmbeddingCreateParams$1 & MonitoringParams, options?: RequestOptions$
|
|
89
|
+
create(body: EmbeddingCreateParams$1 & MonitoringParams, options?: RequestOptions$2): APIPromise<CreateEmbeddingResponse$1>;
|
|
91
90
|
}
|
|
92
91
|
|
|
93
92
|
type ChatCompletion = OpenAIOrignal.ChatCompletion;
|
|
@@ -102,7 +101,7 @@ interface MonitoringOpenAIConfig {
|
|
|
102
101
|
posthog: PostHog;
|
|
103
102
|
baseURL?: string;
|
|
104
103
|
}
|
|
105
|
-
type RequestOptions = Record<string, any>;
|
|
104
|
+
type RequestOptions$1 = Record<string, any>;
|
|
106
105
|
declare class PostHogAzureOpenAI extends AzureOpenAI {
|
|
107
106
|
private readonly phClient;
|
|
108
107
|
chat: WrappedChat;
|
|
@@ -117,15 +116,15 @@ declare class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
117
116
|
private readonly phClient;
|
|
118
117
|
private readonly baseURL;
|
|
119
118
|
constructor(client: AzureOpenAI, phClient: PostHog);
|
|
120
|
-
create(body: ChatCompletionCreateParamsNonStreaming & MonitoringParams, options?: RequestOptions): APIPromise<ChatCompletion>;
|
|
121
|
-
create(body: ChatCompletionCreateParamsStreaming & MonitoringParams, options?: RequestOptions): APIPromise<Stream<ChatCompletionChunk>>;
|
|
122
|
-
create(body: ChatCompletionCreateParamsBase & MonitoringParams, options?: RequestOptions): APIPromise<ChatCompletion | Stream<ChatCompletionChunk>>;
|
|
119
|
+
create(body: ChatCompletionCreateParamsNonStreaming & MonitoringParams, options?: RequestOptions$1): APIPromise<ChatCompletion>;
|
|
120
|
+
create(body: ChatCompletionCreateParamsStreaming & MonitoringParams, options?: RequestOptions$1): APIPromise<Stream<ChatCompletionChunk>>;
|
|
121
|
+
create(body: ChatCompletionCreateParamsBase & MonitoringParams, options?: RequestOptions$1): APIPromise<ChatCompletion | Stream<ChatCompletionChunk>>;
|
|
123
122
|
}
|
|
124
123
|
declare class WrappedEmbeddings extends AzureOpenAI.Embeddings {
|
|
125
124
|
private readonly phClient;
|
|
126
125
|
private readonly baseURL;
|
|
127
126
|
constructor(client: AzureOpenAI, phClient: PostHog);
|
|
128
|
-
create(body: EmbeddingCreateParams & MonitoringParams, options?: RequestOptions): APIPromise<CreateEmbeddingResponse>;
|
|
127
|
+
create(body: EmbeddingCreateParams & MonitoringParams, options?: RequestOptions$1): APIPromise<CreateEmbeddingResponse>;
|
|
129
128
|
}
|
|
130
129
|
|
|
131
130
|
interface ClientOptions {
|
|
@@ -146,6 +145,7 @@ type MessageCreateParamsStreaming = AnthropicOriginal.Messages.MessageCreatePara
|
|
|
146
145
|
type Message = AnthropicOriginal.Messages.Message;
|
|
147
146
|
type RawMessageStreamEvent = AnthropicOriginal.Messages.RawMessageStreamEvent;
|
|
148
147
|
type MessageCreateParamsBase = AnthropicOriginal.Messages.MessageCreateParams;
|
|
148
|
+
type RequestOptions = AnthropicOriginal.RequestOptions;
|
|
149
149
|
|
|
150
150
|
interface MonitoringAnthropicConfig {
|
|
151
151
|
apiKey: string;
|
|
@@ -161,9 +161,9 @@ declare class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
161
161
|
private readonly phClient;
|
|
162
162
|
private readonly baseURL;
|
|
163
163
|
constructor(parentClient: PostHogAnthropic, phClient: PostHog);
|
|
164
|
-
create(body: MessageCreateParamsNonStreaming, options?: RequestOptions
|
|
165
|
-
create(body: MessageCreateParamsStreaming & MonitoringParams, options?: RequestOptions
|
|
166
|
-
create(body: MessageCreateParamsBase & MonitoringParams, options?: RequestOptions
|
|
164
|
+
create(body: MessageCreateParamsNonStreaming, options?: RequestOptions): APIPromise$1<Message>;
|
|
165
|
+
create(body: MessageCreateParamsStreaming & MonitoringParams, options?: RequestOptions): APIPromise$1<Stream$1<RawMessageStreamEvent>>;
|
|
166
|
+
create(body: MessageCreateParamsBase & MonitoringParams, options?: RequestOptions): APIPromise$1<Stream$1<RawMessageStreamEvent> | Message>;
|
|
167
167
|
}
|
|
168
168
|
|
|
169
169
|
interface MonitoringGeminiConfig {
|
package/dist/index.mjs
CHANGED
|
@@ -6,7 +6,7 @@ import { wrapLanguageModel } from 'ai';
|
|
|
6
6
|
import AnthropicOriginal from '@anthropic-ai/sdk';
|
|
7
7
|
import { GoogleGenAI } from '@google/genai';
|
|
8
8
|
|
|
9
|
-
var version = "6.
|
|
9
|
+
var version = "6.4.0";
|
|
10
10
|
|
|
11
11
|
// limit large outputs by truncating to 200kb (approx 200k bytes)
|
|
12
12
|
const MAX_OUTPUT_SIZE = 200000;
|
|
@@ -291,6 +291,41 @@ function sanitizeValues(obj) {
|
|
|
291
291
|
}
|
|
292
292
|
return jsonSafe;
|
|
293
293
|
}
|
|
294
|
+
const POSTHOG_PARAMS_MAP = {
|
|
295
|
+
posthogDistinctId: 'distinctId',
|
|
296
|
+
posthogTraceId: 'traceId',
|
|
297
|
+
posthogProperties: 'properties',
|
|
298
|
+
posthogPrivacyMode: 'privacyMode',
|
|
299
|
+
posthogGroups: 'groups',
|
|
300
|
+
posthogModelOverride: 'modelOverride',
|
|
301
|
+
posthogProviderOverride: 'providerOverride',
|
|
302
|
+
posthogCostOverride: 'costOverride',
|
|
303
|
+
posthogCaptureImmediate: 'captureImmediate'
|
|
304
|
+
};
|
|
305
|
+
function extractPosthogParams(body) {
|
|
306
|
+
const providerParams = {};
|
|
307
|
+
const posthogParams = {};
|
|
308
|
+
for (const [key, value] of Object.entries(body)) {
|
|
309
|
+
if (POSTHOG_PARAMS_MAP[key]) {
|
|
310
|
+
posthogParams[POSTHOG_PARAMS_MAP[key]] = value;
|
|
311
|
+
} else if (key.startsWith('posthog')) {
|
|
312
|
+
console.warn(`Unknown Posthog parameter ${key}`);
|
|
313
|
+
} else {
|
|
314
|
+
providerParams[key] = value;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return {
|
|
318
|
+
providerParams: providerParams,
|
|
319
|
+
posthogParams: addDefaults(posthogParams)
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
function addDefaults(params) {
|
|
323
|
+
return {
|
|
324
|
+
...params,
|
|
325
|
+
privacyMode: params.privacyMode ?? false,
|
|
326
|
+
traceId: params.traceId ?? v4()
|
|
327
|
+
};
|
|
328
|
+
}
|
|
294
329
|
const sendEventToPosthog = async ({
|
|
295
330
|
client,
|
|
296
331
|
eventType = AIEvent.Generation,
|
|
@@ -584,42 +619,6 @@ const sanitizeLangChain = data => {
|
|
|
584
619
|
return processMessages(data, sanitizeLangChainImage);
|
|
585
620
|
};
|
|
586
621
|
|
|
587
|
-
const POSTHOG_PARAMS_MAP = {
|
|
588
|
-
posthogDistinctId: 'distinctId',
|
|
589
|
-
posthogTraceId: 'traceId',
|
|
590
|
-
posthogProperties: 'properties',
|
|
591
|
-
posthogPrivacyMode: 'privacyMode',
|
|
592
|
-
posthogGroups: 'groups',
|
|
593
|
-
posthogModelOverride: 'modelOverride',
|
|
594
|
-
posthogProviderOverride: 'providerOverride',
|
|
595
|
-
posthogCostOverride: 'costOverride',
|
|
596
|
-
posthogCaptureImmediate: 'captureImmediate'
|
|
597
|
-
};
|
|
598
|
-
function extractPosthogParams(body) {
|
|
599
|
-
const openAIParams = {};
|
|
600
|
-
const posthogParams = {};
|
|
601
|
-
for (const [key, value] of Object.entries(body)) {
|
|
602
|
-
if (POSTHOG_PARAMS_MAP[key]) {
|
|
603
|
-
posthogParams[POSTHOG_PARAMS_MAP[key]] = value;
|
|
604
|
-
} else if (key.startsWith('posthog')) {
|
|
605
|
-
console.warn(`Unknown Posthog parameter ${key}`);
|
|
606
|
-
} else {
|
|
607
|
-
openAIParams[key] = value;
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
return {
|
|
611
|
-
openAIParams: openAIParams,
|
|
612
|
-
posthogParams: addDefaults(posthogParams)
|
|
613
|
-
};
|
|
614
|
-
}
|
|
615
|
-
function addDefaults(params) {
|
|
616
|
-
return {
|
|
617
|
-
...params,
|
|
618
|
-
privacyMode: params.privacyMode ?? false,
|
|
619
|
-
traceId: params.traceId ?? v4()
|
|
620
|
-
};
|
|
621
|
-
}
|
|
622
|
-
|
|
623
622
|
const Chat = OpenAI.Chat;
|
|
624
623
|
const Completions = Chat.Completions;
|
|
625
624
|
const Responses = OpenAI.Responses;
|
|
@@ -652,7 +651,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
652
651
|
// --- Implementation Signature
|
|
653
652
|
create(body, options) {
|
|
654
653
|
const {
|
|
655
|
-
openAIParams,
|
|
654
|
+
providerParams: openAIParams,
|
|
656
655
|
posthogParams
|
|
657
656
|
} = extractPosthogParams(body);
|
|
658
657
|
const startTime = Date.now();
|
|
@@ -853,7 +852,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
853
852
|
// --- Implementation Signature
|
|
854
853
|
create(body, options) {
|
|
855
854
|
const {
|
|
856
|
-
openAIParams,
|
|
855
|
+
providerParams: openAIParams,
|
|
857
856
|
posthogParams
|
|
858
857
|
} = extractPosthogParams(body);
|
|
859
858
|
const startTime = Date.now();
|
|
@@ -983,7 +982,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
983
982
|
}
|
|
984
983
|
parse(body, options) {
|
|
985
984
|
const {
|
|
986
|
-
openAIParams,
|
|
985
|
+
providerParams: openAIParams,
|
|
987
986
|
posthogParams
|
|
988
987
|
} = extractPosthogParams(body);
|
|
989
988
|
const startTime = Date.now();
|
|
@@ -1054,7 +1053,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
|
|
|
1054
1053
|
}
|
|
1055
1054
|
create(body, options) {
|
|
1056
1055
|
const {
|
|
1057
|
-
openAIParams,
|
|
1056
|
+
providerParams: openAIParams,
|
|
1058
1057
|
posthogParams
|
|
1059
1058
|
} = extractPosthogParams(body);
|
|
1060
1059
|
const startTime = Date.now();
|
|
@@ -1133,7 +1132,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1133
1132
|
// --- Implementation Signature
|
|
1134
1133
|
create(body, options) {
|
|
1135
1134
|
const {
|
|
1136
|
-
openAIParams,
|
|
1135
|
+
providerParams: openAIParams,
|
|
1137
1136
|
posthogParams
|
|
1138
1137
|
} = extractPosthogParams(body);
|
|
1139
1138
|
const startTime = Date.now();
|
|
@@ -1330,7 +1329,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1330
1329
|
// --- Implementation Signature
|
|
1331
1330
|
create(body, options) {
|
|
1332
1331
|
const {
|
|
1333
|
-
openAIParams,
|
|
1332
|
+
providerParams: openAIParams,
|
|
1334
1333
|
posthogParams
|
|
1335
1334
|
} = extractPosthogParams(body);
|
|
1336
1335
|
const startTime = Date.now();
|
|
@@ -1454,7 +1453,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1454
1453
|
}
|
|
1455
1454
|
parse(body, options) {
|
|
1456
1455
|
const {
|
|
1457
|
-
openAIParams,
|
|
1456
|
+
providerParams: openAIParams,
|
|
1458
1457
|
posthogParams
|
|
1459
1458
|
} = extractPosthogParams(body);
|
|
1460
1459
|
const startTime = Date.now();
|
|
@@ -1514,7 +1513,7 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
|
|
|
1514
1513
|
}
|
|
1515
1514
|
create(body, options) {
|
|
1516
1515
|
const {
|
|
1517
|
-
openAIParams,
|
|
1516
|
+
providerParams: openAIParams,
|
|
1518
1517
|
posthogParams
|
|
1519
1518
|
} = extractPosthogParams(body);
|
|
1520
1519
|
const startTime = Date.now();
|
|
@@ -2030,12 +2029,9 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2030
2029
|
}
|
|
2031
2030
|
create(body, options) {
|
|
2032
2031
|
const {
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
...anthropicParams
|
|
2037
|
-
} = body;
|
|
2038
|
-
const traceId = posthogTraceId ?? v4();
|
|
2032
|
+
providerParams: anthropicParams,
|
|
2033
|
+
posthogParams
|
|
2034
|
+
} = extractPosthogParams(body);
|
|
2039
2035
|
const startTime = Date.now();
|
|
2040
2036
|
const parentPromise = super.create(anthropicParams, options);
|
|
2041
2037
|
if (anthropicParams.stream) {
|
|
@@ -2145,8 +2141,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2145
2141
|
}];
|
|
2146
2142
|
await sendEventToPosthog({
|
|
2147
2143
|
client: this.phClient,
|
|
2148
|
-
|
|
2149
|
-
traceId,
|
|
2144
|
+
...posthogParams,
|
|
2150
2145
|
model: anthropicParams.model,
|
|
2151
2146
|
provider: 'anthropic',
|
|
2152
2147
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams, 'anthropic')),
|
|
@@ -2156,15 +2151,13 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2156
2151
|
params: body,
|
|
2157
2152
|
httpStatus: 200,
|
|
2158
2153
|
usage,
|
|
2159
|
-
tools: availableTools
|
|
2160
|
-
captureImmediate: posthogCaptureImmediate
|
|
2154
|
+
tools: availableTools
|
|
2161
2155
|
});
|
|
2162
2156
|
} catch (error) {
|
|
2163
2157
|
// error handling
|
|
2164
2158
|
await sendEventToPosthog({
|
|
2165
2159
|
client: this.phClient,
|
|
2166
|
-
|
|
2167
|
-
traceId,
|
|
2160
|
+
...posthogParams,
|
|
2168
2161
|
model: anthropicParams.model,
|
|
2169
2162
|
provider: 'anthropic',
|
|
2170
2163
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
@@ -2178,8 +2171,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2178
2171
|
outputTokens: 0
|
|
2179
2172
|
},
|
|
2180
2173
|
isError: true,
|
|
2181
|
-
error: JSON.stringify(error)
|
|
2182
|
-
captureImmediate: posthogCaptureImmediate
|
|
2174
|
+
error: JSON.stringify(error)
|
|
2183
2175
|
});
|
|
2184
2176
|
}
|
|
2185
2177
|
})();
|
|
@@ -2195,8 +2187,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2195
2187
|
const availableTools = extractAvailableToolCalls('anthropic', anthropicParams);
|
|
2196
2188
|
await sendEventToPosthog({
|
|
2197
2189
|
client: this.phClient,
|
|
2198
|
-
|
|
2199
|
-
traceId,
|
|
2190
|
+
...posthogParams,
|
|
2200
2191
|
model: anthropicParams.model,
|
|
2201
2192
|
provider: 'anthropic',
|
|
2202
2193
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
@@ -2211,16 +2202,14 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2211
2202
|
cacheCreationInputTokens: result.usage.cache_creation_input_tokens ?? 0,
|
|
2212
2203
|
cacheReadInputTokens: result.usage.cache_read_input_tokens ?? 0
|
|
2213
2204
|
},
|
|
2214
|
-
tools: availableTools
|
|
2215
|
-
captureImmediate: posthogCaptureImmediate
|
|
2205
|
+
tools: availableTools
|
|
2216
2206
|
});
|
|
2217
2207
|
}
|
|
2218
2208
|
return result;
|
|
2219
2209
|
}, async error => {
|
|
2220
2210
|
await sendEventToPosthog({
|
|
2221
2211
|
client: this.phClient,
|
|
2222
|
-
|
|
2223
|
-
traceId,
|
|
2212
|
+
...posthogParams,
|
|
2224
2213
|
model: anthropicParams.model,
|
|
2225
2214
|
provider: 'anthropic',
|
|
2226
2215
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
@@ -2234,8 +2223,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2234
2223
|
outputTokens: 0
|
|
2235
2224
|
},
|
|
2236
2225
|
isError: true,
|
|
2237
|
-
error: JSON.stringify(error)
|
|
2238
|
-
captureImmediate: posthogCaptureImmediate
|
|
2226
|
+
error: JSON.stringify(error)
|
|
2239
2227
|
});
|
|
2240
2228
|
throw error;
|
|
2241
2229
|
});
|
|
@@ -2262,12 +2250,9 @@ class WrappedModels {
|
|
|
2262
2250
|
}
|
|
2263
2251
|
async generateContent(params) {
|
|
2264
2252
|
const {
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
...geminiParams
|
|
2269
|
-
} = params;
|
|
2270
|
-
const traceId = posthogTraceId ?? v4();
|
|
2253
|
+
providerParams: geminiParams,
|
|
2254
|
+
posthogParams
|
|
2255
|
+
} = extractPosthogParams(params);
|
|
2271
2256
|
const startTime = Date.now();
|
|
2272
2257
|
try {
|
|
2273
2258
|
const response = await this.client.models.generateContent(geminiParams);
|
|
@@ -2276,8 +2261,7 @@ class WrappedModels {
|
|
|
2276
2261
|
const metadata = response.usageMetadata;
|
|
2277
2262
|
await sendEventToPosthog({
|
|
2278
2263
|
client: this.phClient,
|
|
2279
|
-
|
|
2280
|
-
traceId,
|
|
2264
|
+
...posthogParams,
|
|
2281
2265
|
model: geminiParams.model,
|
|
2282
2266
|
provider: 'gemini',
|
|
2283
2267
|
input: this.formatInputForPostHog(geminiParams.contents),
|
|
@@ -2292,16 +2276,14 @@ class WrappedModels {
|
|
|
2292
2276
|
reasoningTokens: metadata?.thoughtsTokenCount ?? 0,
|
|
2293
2277
|
cacheReadInputTokens: metadata?.cachedContentTokenCount ?? 0
|
|
2294
2278
|
},
|
|
2295
|
-
tools: availableTools
|
|
2296
|
-
captureImmediate: posthogCaptureImmediate
|
|
2279
|
+
tools: availableTools
|
|
2297
2280
|
});
|
|
2298
2281
|
return response;
|
|
2299
2282
|
} catch (error) {
|
|
2300
2283
|
const latency = (Date.now() - startTime) / 1000;
|
|
2301
2284
|
await sendEventToPosthog({
|
|
2302
2285
|
client: this.phClient,
|
|
2303
|
-
|
|
2304
|
-
traceId,
|
|
2286
|
+
...posthogParams,
|
|
2305
2287
|
model: geminiParams.model,
|
|
2306
2288
|
provider: 'gemini',
|
|
2307
2289
|
input: this.formatInputForPostHog(geminiParams.contents),
|
|
@@ -2315,20 +2297,16 @@ class WrappedModels {
|
|
|
2315
2297
|
outputTokens: 0
|
|
2316
2298
|
},
|
|
2317
2299
|
isError: true,
|
|
2318
|
-
error: JSON.stringify(error)
|
|
2319
|
-
captureImmediate: posthogCaptureImmediate
|
|
2300
|
+
error: JSON.stringify(error)
|
|
2320
2301
|
});
|
|
2321
2302
|
throw error;
|
|
2322
2303
|
}
|
|
2323
2304
|
}
|
|
2324
2305
|
async *generateContentStream(params) {
|
|
2325
2306
|
const {
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
...geminiParams
|
|
2330
|
-
} = params;
|
|
2331
|
-
const traceId = posthogTraceId ?? v4();
|
|
2307
|
+
providerParams: geminiParams,
|
|
2308
|
+
posthogParams
|
|
2309
|
+
} = extractPosthogParams(params);
|
|
2332
2310
|
const startTime = Date.now();
|
|
2333
2311
|
const accumulatedContent = [];
|
|
2334
2312
|
let usage = {
|
|
@@ -2400,8 +2378,7 @@ class WrappedModels {
|
|
|
2400
2378
|
}] : [];
|
|
2401
2379
|
await sendEventToPosthog({
|
|
2402
2380
|
client: this.phClient,
|
|
2403
|
-
|
|
2404
|
-
traceId,
|
|
2381
|
+
...posthogParams,
|
|
2405
2382
|
model: geminiParams.model,
|
|
2406
2383
|
provider: 'gemini',
|
|
2407
2384
|
input: this.formatInputForPostHog(geminiParams.contents),
|
|
@@ -2411,15 +2388,13 @@ class WrappedModels {
|
|
|
2411
2388
|
params: params,
|
|
2412
2389
|
httpStatus: 200,
|
|
2413
2390
|
usage,
|
|
2414
|
-
tools: availableTools
|
|
2415
|
-
captureImmediate: posthogCaptureImmediate
|
|
2391
|
+
tools: availableTools
|
|
2416
2392
|
});
|
|
2417
2393
|
} catch (error) {
|
|
2418
2394
|
const latency = (Date.now() - startTime) / 1000;
|
|
2419
2395
|
await sendEventToPosthog({
|
|
2420
2396
|
client: this.phClient,
|
|
2421
|
-
|
|
2422
|
-
traceId,
|
|
2397
|
+
...posthogParams,
|
|
2423
2398
|
model: geminiParams.model,
|
|
2424
2399
|
provider: 'gemini',
|
|
2425
2400
|
input: this.formatInputForPostHog(geminiParams.contents),
|
|
@@ -2433,8 +2408,7 @@ class WrappedModels {
|
|
|
2433
2408
|
outputTokens: 0
|
|
2434
2409
|
},
|
|
2435
2410
|
isError: true,
|
|
2436
|
-
error: JSON.stringify(error)
|
|
2437
|
-
captureImmediate: posthogCaptureImmediate
|
|
2411
|
+
error: JSON.stringify(error)
|
|
2438
2412
|
});
|
|
2439
2413
|
throw error;
|
|
2440
2414
|
}
|