Skip to content

Commit b67ea54

Browse files
committed
fix(posthog): validate integration against live API docs, add self-hosted support + CRUD coverage
Fix missing response.ok checks across ~36 tools that silently treated error bodies as success. Fix delete_feature_flag ignoring failure responses, evaluate_flags targeting the undocumented /decide endpoint without the required api_key body field, and batch_events reporting a hardcoded events_processed count. Add self-hosted host support (utils.ts) alongside the existing US/EU region selector, and complete CRUD coverage with 5 new tools: update_insight, update_cohort, update_experiment, delete_survey, create_dashboard.
1 parent b70b21a commit b67ea54

52 files changed

Lines changed: 2215 additions & 71 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apps/sim/blocks/blocks/posthog.ts

Lines changed: 195 additions & 15 deletions
Large diffs are not rendered by default.

apps/sim/tools/posthog/batch_events.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { getPostHogIngestBaseUrl } from '@/tools/posthog/utils'
12
import type { ToolConfig } from '@/tools/types'
23

34
export interface PostHogBatchEventsParams {
45
projectApiKey: string
56
region?: 'us' | 'eu'
7+
host?: string
68
batch: string
79
}
810

@@ -35,6 +37,13 @@ export const batchEventsTool: ToolConfig<PostHogBatchEventsParams, PostHogBatchE
3537
description: 'PostHog region: us (default) or eu',
3638
default: 'us',
3739
},
40+
host: {
41+
type: 'string',
42+
required: false,
43+
visibility: 'user-only',
44+
description:
45+
'Self-hosted PostHog instance host (e.g., "posthog.mycompany.com"). Overrides the region setting when provided.',
46+
},
3847
batch: {
3948
type: 'string',
4049
required: true,
@@ -46,8 +55,7 @@ export const batchEventsTool: ToolConfig<PostHogBatchEventsParams, PostHogBatchE
4655

4756
request: {
4857
url: (params) => {
49-
const baseUrl =
50-
params.region === 'eu' ? 'https://eu.i.posthog.com' : 'https://us.i.posthog.com'
58+
const baseUrl = getPostHogIngestBaseUrl(params.region, params.host)
5159
return `${baseUrl}/batch/`
5260
},
5361
method: 'POST',
@@ -69,14 +77,20 @@ export const batchEventsTool: ToolConfig<PostHogBatchEventsParams, PostHogBatchE
6977
},
7078
},
7179

72-
transformResponse: async (response: Response) => {
80+
transformResponse: async (response: Response, params) => {
7381
if (response.ok) {
7482
const data = await response.json()
83+
let eventsProcessed = 0
84+
try {
85+
eventsProcessed = params ? JSON.parse(params.batch).length : 0
86+
} catch {
87+
eventsProcessed = 0
88+
}
7589
return {
7690
success: true,
7791
output: {
7892
status: 'Batch events captured successfully',
79-
events_processed: data.status === 1 ? JSON.parse(data.batch || '[]').length : 0,
93+
events_processed: data.status === 1 ? eventsProcessed : 0,
8094
},
8195
}
8296
}

apps/sim/tools/posthog/capture_event.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { getPostHogIngestBaseUrl } from '@/tools/posthog/utils'
12
import type { ToolConfig } from '@/tools/types'
23

34
export interface PostHogCaptureEventParams {
45
projectApiKey: string
56
region?: 'us' | 'eu'
7+
host?: string
68
event: string
79
distinctId: string
810
properties?: string
@@ -38,6 +40,13 @@ export const captureEventTool: ToolConfig<PostHogCaptureEventParams, PostHogCapt
3840
description: 'PostHog region: us (default) or eu',
3941
default: 'us',
4042
},
43+
host: {
44+
type: 'string',
45+
required: false,
46+
visibility: 'user-only',
47+
description:
48+
'Self-hosted PostHog instance host (e.g., "posthog.mycompany.com"). Overrides the region setting when provided.',
49+
},
4150
event: {
4251
type: 'string',
4352
required: true,
@@ -69,8 +78,7 @@ export const captureEventTool: ToolConfig<PostHogCaptureEventParams, PostHogCapt
6978

7079
request: {
7180
url: (params) => {
72-
const baseUrl =
73-
params.region === 'eu' ? 'https://eu.i.posthog.com' : 'https://us.i.posthog.com'
81+
const baseUrl = getPostHogIngestBaseUrl(params.region, params.host)
7482
return `${baseUrl}/capture/`
7583
},
7684
method: 'POST',

apps/sim/tools/posthog/create_annotation.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { getPostHogAppBaseUrl } from '@/tools/posthog/utils'
12
import type { ToolConfig } from '@/tools/types'
23

34
interface PostHogCreateAnnotationParams {
45
apiKey: string
56
projectId: string
67
region: string
8+
host?: string
79
content: string
810
date_marker: string
911
scope?: string
@@ -58,6 +60,13 @@ export const createAnnotationTool: ToolConfig<
5860
description: 'PostHog cloud region: "us" or "eu" (default: "us")',
5961
default: 'us',
6062
},
63+
host: {
64+
type: 'string',
65+
required: false,
66+
visibility: 'user-only',
67+
description:
68+
'Self-hosted PostHog instance host (e.g., "posthog.mycompany.com"). Overrides the region setting when provided.',
69+
},
6170
content: {
6271
type: 'string',
6372
required: true,
@@ -93,7 +102,7 @@ export const createAnnotationTool: ToolConfig<
93102

94103
request: {
95104
url: (params) => {
96-
const baseUrl = params.region === 'eu' ? 'https://eu.posthog.com' : 'https://us.posthog.com'
105+
const baseUrl = getPostHogAppBaseUrl(params.region as 'us' | 'eu' | undefined, params.host)
97106
return `${baseUrl}/api/projects/${params.projectId}/annotations/`
98107
},
99108
method: 'POST',
@@ -124,6 +133,27 @@ export const createAnnotationTool: ToolConfig<
124133
},
125134

126135
transformResponse: async (response: Response) => {
136+
if (!response.ok) {
137+
const error = await response.text()
138+
return {
139+
success: false,
140+
output: {
141+
id: 0,
142+
content: '',
143+
date_marker: '',
144+
created_at: '',
145+
updated_at: '',
146+
created_by: null,
147+
dashboard_item: null,
148+
insight_short_id: null,
149+
insight_name: null,
150+
scope: '',
151+
deleted: false,
152+
},
153+
error: error || 'Failed to create annotation',
154+
}
155+
}
156+
127157
const data = await response.json()
128158

129159
return {

apps/sim/tools/posthog/create_cohort.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { getPostHogAppBaseUrl } from '@/tools/posthog/utils'
12
import type { ToolConfig } from '@/tools/types'
23

34
interface PostHogCreateCohortParams {
45
apiKey: string
56
projectId: string
67
region: string
8+
host?: string
79
name: string
810
description?: string
911
filters?: string
@@ -59,6 +61,13 @@ export const createCohortTool: ToolConfig<PostHogCreateCohortParams, PostHogCrea
5961
description: 'PostHog cloud region: "us" or "eu" (default: "us")',
6062
default: 'us',
6163
},
64+
host: {
65+
type: 'string',
66+
required: false,
67+
visibility: 'user-only',
68+
description:
69+
'Self-hosted PostHog instance host (e.g., "posthog.mycompany.com"). Overrides the region setting when provided.',
70+
},
6271
name: {
6372
type: 'string',
6473
required: false,
@@ -101,7 +110,7 @@ export const createCohortTool: ToolConfig<PostHogCreateCohortParams, PostHogCrea
101110

102111
request: {
103112
url: (params) => {
104-
const baseUrl = params.region === 'eu' ? 'https://eu.posthog.com' : 'https://us.posthog.com'
113+
const baseUrl = getPostHogAppBaseUrl(params.region as 'us' | 'eu' | undefined, params.host)
105114
return `${baseUrl}/api/projects/${params.projectId}/cohorts/`
106115
},
107116
method: 'POST',
@@ -151,6 +160,29 @@ export const createCohortTool: ToolConfig<PostHogCreateCohortParams, PostHogCrea
151160
},
152161

153162
transformResponse: async (response: Response) => {
163+
if (!response.ok) {
164+
const error = await response.text()
165+
return {
166+
success: false,
167+
output: {
168+
id: 0,
169+
name: '',
170+
description: '',
171+
groups: [],
172+
deleted: false,
173+
filters: {},
174+
query: null,
175+
created_at: '',
176+
created_by: null,
177+
is_calculating: false,
178+
count: 0,
179+
is_static: false,
180+
version: 0,
181+
},
182+
error: error || 'Failed to create cohort',
183+
}
184+
}
185+
154186
const data = await response.json()
155187

156188
return {

0 commit comments

Comments
 (0)