@posthog/ai 1.2.1 → 1.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/package.json +10 -6
- package/src/vercel/middleware.ts +20 -20
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@posthog/ai",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "PostHog Node.js AI integrations",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/PostHog/posthog-js-lite.git",
|
|
8
8
|
"directory": "posthog-ai"
|
|
9
9
|
},
|
|
10
|
-
"main": "./lib/index.js",
|
|
11
|
-
"module": "./lib/index.
|
|
10
|
+
"main": "./lib/index.cjs.js",
|
|
11
|
+
"module": "./lib/index.esm.js",
|
|
12
12
|
"types": "./lib/index.d.ts",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"devDependencies": {
|
|
@@ -17,7 +17,9 @@
|
|
|
17
17
|
"jest": "^29.0.0",
|
|
18
18
|
"node-fetch": "^3.3.2",
|
|
19
19
|
"ts-jest": "^29.0.0",
|
|
20
|
-
"typescript": "^4.7.4"
|
|
20
|
+
"typescript": "^4.7.4",
|
|
21
|
+
"ai": "^4.0.0",
|
|
22
|
+
"openai": "^4.0.0"
|
|
21
23
|
},
|
|
22
24
|
"keywords": [
|
|
23
25
|
"posthog",
|
|
@@ -27,11 +29,13 @@
|
|
|
27
29
|
"llm"
|
|
28
30
|
],
|
|
29
31
|
"dependencies": {
|
|
30
|
-
"ai": "^4.1.0",
|
|
31
|
-
"openai": "^4.79.1",
|
|
32
32
|
"uuid": "^11.0.5",
|
|
33
33
|
"zod": "^3.24.1"
|
|
34
34
|
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"ai": "^4.0.0",
|
|
37
|
+
"openai": "^4.0.0"
|
|
38
|
+
},
|
|
35
39
|
"scripts": {
|
|
36
40
|
"test": "jest",
|
|
37
41
|
"prepublishOnly": "cd .. && yarn build"
|
package/src/vercel/middleware.ts
CHANGED
|
@@ -9,11 +9,11 @@ import type { PostHog } from 'posthog-node'
|
|
|
9
9
|
import { sendEventToPosthog } from '../utils'
|
|
10
10
|
|
|
11
11
|
interface CreateInstrumentationMiddlewareOptions {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
posthogDistinctId?: string
|
|
13
|
+
posthogTraceId: string
|
|
14
|
+
posthogProperties?: Record<string, any>
|
|
15
|
+
posthogPrivacyMode?: boolean
|
|
16
|
+
posthogGroups?: string[]
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export const createInstrumentationMiddleware = (
|
|
@@ -31,11 +31,11 @@ export const createInstrumentationMiddleware = (
|
|
|
31
31
|
|
|
32
32
|
sendEventToPosthog({
|
|
33
33
|
client: phClient,
|
|
34
|
-
distinctId: options.
|
|
35
|
-
traceId: options.
|
|
34
|
+
distinctId: options.posthogDistinctId,
|
|
35
|
+
traceId: options.posthogTraceId,
|
|
36
36
|
model: model.modelId,
|
|
37
37
|
provider: 'vercel',
|
|
38
|
-
input: options.
|
|
38
|
+
input: options.posthogPrivacyMode ? '' : params.prompt,
|
|
39
39
|
output: [{ content: result.text, role: 'assistant' }],
|
|
40
40
|
latency,
|
|
41
41
|
baseURL: '',
|
|
@@ -51,11 +51,11 @@ export const createInstrumentationMiddleware = (
|
|
|
51
51
|
} catch (error) {
|
|
52
52
|
sendEventToPosthog({
|
|
53
53
|
client: phClient,
|
|
54
|
-
distinctId: options.
|
|
55
|
-
traceId: options.
|
|
54
|
+
distinctId: options.posthogDistinctId,
|
|
55
|
+
traceId: options.posthogTraceId,
|
|
56
56
|
model: model.modelId,
|
|
57
57
|
provider: 'vercel',
|
|
58
|
-
input: options.
|
|
58
|
+
input: options.posthogPrivacyMode ? '' : params.prompt,
|
|
59
59
|
output: [],
|
|
60
60
|
latency: 0,
|
|
61
61
|
baseURL: '',
|
|
@@ -96,11 +96,11 @@ export const createInstrumentationMiddleware = (
|
|
|
96
96
|
const latency = (Date.now() - startTime) / 1000
|
|
97
97
|
sendEventToPosthog({
|
|
98
98
|
client: phClient,
|
|
99
|
-
distinctId: options.
|
|
100
|
-
traceId: options.
|
|
99
|
+
distinctId: options.posthogDistinctId,
|
|
100
|
+
traceId: options.posthogTraceId,
|
|
101
101
|
model: model.modelId,
|
|
102
102
|
provider: 'vercel',
|
|
103
|
-
input: options.
|
|
103
|
+
input: options.posthogPrivacyMode ? '' : params.prompt,
|
|
104
104
|
output: [{ content: generatedText, role: 'assistant' }],
|
|
105
105
|
latency,
|
|
106
106
|
baseURL: '',
|
|
@@ -118,11 +118,11 @@ export const createInstrumentationMiddleware = (
|
|
|
118
118
|
} catch (error) {
|
|
119
119
|
sendEventToPosthog({
|
|
120
120
|
client: phClient,
|
|
121
|
-
distinctId: options.
|
|
122
|
-
traceId: options.
|
|
121
|
+
distinctId: options.posthogDistinctId,
|
|
122
|
+
traceId: options.posthogTraceId,
|
|
123
123
|
model: model.modelId,
|
|
124
124
|
provider: 'vercel',
|
|
125
|
-
input: options.
|
|
125
|
+
input: options.posthogPrivacyMode ? '' : params.prompt,
|
|
126
126
|
output: [],
|
|
127
127
|
latency: 0,
|
|
128
128
|
baseURL: '',
|
|
@@ -146,11 +146,11 @@ export const wrapVercelLanguageModel = (
|
|
|
146
146
|
phClient: PostHog,
|
|
147
147
|
options: CreateInstrumentationMiddlewareOptions
|
|
148
148
|
): LanguageModelV1 => {
|
|
149
|
-
const traceId = options.
|
|
149
|
+
const traceId = options.posthogTraceId ?? uuidv4()
|
|
150
150
|
const middleware = createInstrumentationMiddleware(phClient, model, {
|
|
151
151
|
...options,
|
|
152
|
-
|
|
153
|
-
|
|
152
|
+
posthogTraceId: traceId,
|
|
153
|
+
posthogDistinctId: options.posthogDistinctId ?? traceId,
|
|
154
154
|
})
|
|
155
155
|
|
|
156
156
|
const wrappedModel = wrapLanguageModel({
|