1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
import { PostHog } from "posthog-node"
const MCP_SERVER_VERSION = "4.0.0"
/**
* PostHog singleton for analytics.
*/
let instance: PostHog | null = null
let initialized = false
/**
* Initialize PostHog with the provided API key.
*/
export function initPosthog(apiKey?: string): void {
if (initialized) return
initialized = true
if (!apiKey) {
return
}
instance = new PostHog(apiKey, {
host: "https://us.i.posthog.com",
})
}
function getInstance(): PostHog | null {
if (!initialized) {
console.warn(
"PostHog not initialized. Call initPosthog(apiKey) during worker startup.",
)
}
return instance
}
export async function memoryAdded(props: {
type: "note" | "link" | "file"
project_id?: string
content_length?: number
file_size?: number
file_type?: string
source?: string
userId: string
mcp_client_name?: string
mcp_client_version?: string
sessionId?: string
containerTag?: string
}): Promise<void> {
const client = getInstance()
if (!client) return
try {
client.capture({
distinctId: props.userId,
event: "memory_added",
properties: {
...props,
mcp_server_version: MCP_SERVER_VERSION,
},
})
} catch (error) {
console.error("PostHog tracking error:", error)
}
}
export async function memorySearch(props: {
query_length: number
results_count: number
search_duration_ms: number
container_tags_count?: number
source?: string
userId: string
mcp_client_name?: string
mcp_client_version?: string
sessionId?: string
containerTag?: string
}): Promise<void> {
const client = getInstance()
if (!client) return
try {
client.capture({
distinctId: props.userId,
event: "memory_search",
properties: {
...props,
mcp_server_version: MCP_SERVER_VERSION,
},
})
} catch (error) {
console.error("PostHog tracking error:", error)
}
}
export async function memoryForgot(props: {
userId: string
content_length?: number
source?: string
mcp_client_name?: string
mcp_client_version?: string
sessionId?: string
containerTag?: string
}): Promise<void> {
const client = getInstance()
if (!client) return
try {
client.capture({
distinctId: props.userId,
event: "memory_forgot",
properties: {
...props,
mcp_server_version: MCP_SERVER_VERSION,
},
})
} catch (error) {
console.error("PostHog tracking error:", error)
}
}
export async function shutdown(): Promise<void> {
if (instance) {
await instance.shutdown()
instance = null
initialized = false
}
}
export const posthog = {
init: initPosthog,
memoryAdded,
memorySearch,
memoryForgot,
shutdown,
}
|