Skip to content

Commit fdecd8a

Browse files
committed
fix(posthog): remove dead response.ok checks, fix real schema/endpoint bugs
The prior commit added if (!response.ok) branches inside transformResponse across ~40 tool files. This is dead code — tools/index.ts already throws on non-2xx (and error-payload) responses before transformResponse is ever invoked, so transformResponse only ever receives already-successful responses. Reverted to the idiomatic no-error-branch pattern used elsewhere in the codebase (e.g. hunter/email_verifier.ts), and added a posthog-errors errorExtractor so the framework's own error throw surfaces PostHog's real {type, code, detail, attr} error shape instead of a generic status message. Verified against PostHog's live OpenAPI schema, fixed real bugs: - delete_feature_flag: DELETE always returns 405 (hard delete not allowed); switched to PATCH with deleted: true - delete_person: no single-person DELETE endpoint exists; switched to POST .../persons/bulk_delete/ with ids: [personId] - create_annotation: insight_short_id is read-only on create; the writable field is dashboard_id - removed nonexistent fields (experiment variants, insight filters/saved) not present in the current schema - added missing trailing slashes on feature-flag/experiment URLs
1 parent b67ea54 commit fdecd8a

51 files changed

Lines changed: 140 additions & 898 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: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -717,8 +717,6 @@ Return ONLY the description text.`,
717717
condition: {
718718
field: 'operation',
719719
value: [
720-
'posthog_create_insight',
721-
'posthog_update_insight',
722720
'posthog_create_feature_flag',
723721
'posthog_update_feature_flag',
724722
'posthog_create_cohort',
@@ -930,13 +928,6 @@ Return ONLY the timestamp string - no explanations, no quotes, no extra text.`,
930928
value: ['posthog_create_experiment', 'posthog_update_experiment'],
931929
},
932930
},
933-
{
934-
id: 'variants',
935-
title: 'Variants (JSON)',
936-
type: 'long-input',
937-
placeholder: '{"control": 50, "test": 50}',
938-
condition: { field: 'operation', value: 'posthog_create_experiment' },
939-
},
940931
{
941932
id: 'archived',
942933
title: 'Archived',

apps/sim/tools/error-extractors.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,17 @@ const ERROR_EXTRACTORS: ErrorExtractorConfig[] = [
243243
return undefined
244244
},
245245
},
246+
{
247+
id: 'posthog-errors',
248+
description: 'PostHog API error format with type/code/detail/attr fields',
249+
examples: ['PostHog API'],
250+
extract: (errorInfo) => {
251+
const detail = errorInfo?.data?.detail
252+
if (typeof detail !== 'string' || !detail.trim()) return undefined
253+
const attr = errorInfo?.data?.attr
254+
return typeof attr === 'string' && attr ? `${detail} (${attr})` : detail
255+
},
256+
},
246257
{
247258
id: 'plain-text-data',
248259
description: 'Plain text error response',
@@ -320,6 +331,7 @@ export const ErrorExtractorId = {
320331
SOAP_FAULT: 'soap-fault',
321332
OAUTH_ERROR_DESCRIPTION: 'oauth-error-description',
322333
NESTED_ERROR_OBJECT: 'nested-error-object',
334+
POSTHOG_ERRORS: 'posthog-errors',
323335
PLAIN_TEXT_DATA: 'plain-text-data',
324336
HTTP_STATUS_TEXT: 'http-status-text',
325337
} as const

apps/sim/tools/posthog/batch_events.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,31 +78,19 @@ export const batchEventsTool: ToolConfig<PostHogBatchEventsParams, PostHogBatchE
7878
},
7979

8080
transformResponse: async (response: Response, params) => {
81-
if (response.ok) {
82-
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-
}
89-
return {
90-
success: true,
91-
output: {
92-
status: 'Batch events captured successfully',
93-
events_processed: data.status === 1 ? eventsProcessed : 0,
94-
},
95-
}
81+
const data = await response.json()
82+
let eventsProcessed = 0
83+
try {
84+
eventsProcessed = params ? JSON.parse(params.batch).length : 0
85+
} catch {
86+
eventsProcessed = 0
9687
}
97-
98-
const error = await response.text()
9988
return {
100-
success: false,
89+
success: true,
10190
output: {
102-
status: 'Failed to capture batch events',
103-
events_processed: 0,
91+
status: 'Batch events captured successfully',
92+
events_processed: data.status === 1 ? eventsProcessed : 0,
10493
},
105-
error: error || 'Unknown error occurred',
10694
}
10795
},
10896

apps/sim/tools/posthog/capture_event.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,11 @@ export const captureEventTool: ToolConfig<PostHogCaptureEventParams, PostHogCapt
109109
},
110110

111111
transformResponse: async (response: Response) => {
112-
if (response.ok) {
113-
return {
114-
success: true,
115-
output: {
116-
status: 'Event captured successfully',
117-
},
118-
}
119-
}
120-
121-
const error = await response.text()
122112
return {
123-
success: false,
113+
success: true,
124114
output: {
125-
status: 'Failed to capture event',
115+
status: 'Event captured successfully',
126116
},
127-
error: error || 'Unknown error occurred',
128117
}
129118
},
130119

apps/sim/tools/posthog/create_annotation.ts

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface PostHogCreateAnnotationParams {
1010
date_marker: string
1111
scope?: string
1212
dashboard_item?: string
13-
insight_short_id?: string
13+
dashboard_id?: string
1414
}
1515

1616
interface PostHogCreateAnnotationResponse {
@@ -23,6 +23,7 @@ interface PostHogCreateAnnotationResponse {
2323
updated_at: string
2424
created_by: Record<string, any> | null
2525
dashboard_item: number | null
26+
dashboard_id: number | null
2627
insight_short_id: string | null
2728
insight_name: string | null
2829
scope: string
@@ -39,6 +40,7 @@ export const createAnnotationTool: ToolConfig<
3940
description:
4041
'Create a new annotation in PostHog. Mark important events on your graphs with date and description.',
4142
version: '1.0.0',
43+
errorExtractor: 'posthog-errors',
4244

4345
params: {
4446
apiKey: {
@@ -84,19 +86,22 @@ export const createAnnotationTool: ToolConfig<
8486
type: 'string',
8587
required: false,
8688
visibility: 'user-or-llm',
87-
description: 'Scope of the annotation: "project" or "dashboard_item" (default: "project")',
89+
description:
90+
'Scope of the annotation: "project", "organization", "dashboard", or "dashboard_item" (default: "project")',
8891
},
8992
dashboard_item: {
9093
type: 'string',
9194
required: false,
9295
visibility: 'user-or-llm',
93-
description: 'ID of dashboard item to attach this annotation to',
96+
description:
97+
'ID of the dashboard tile (insight) to attach this annotation to (used when scope is "dashboard_item")',
9498
},
95-
insight_short_id: {
99+
dashboard_id: {
96100
type: 'string',
97101
required: false,
98102
visibility: 'user-or-llm',
99-
description: 'Short ID of the insight to attach this annotation to',
103+
description:
104+
'ID of the dashboard to attach this annotation to (used when scope is "dashboard")',
100105
},
101106
},
102107

@@ -124,36 +129,15 @@ export const createAnnotationTool: ToolConfig<
124129
body.dashboard_item = Number(params.dashboard_item)
125130
}
126131

127-
if (params.insight_short_id) {
128-
body.insight_short_id = params.insight_short_id
132+
if (params.dashboard_id) {
133+
body.dashboard_id = Number(params.dashboard_id)
129134
}
130135

131136
return body
132137
},
133138
},
134139

135140
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-
157141
const data = await response.json()
158142

159143
return {
@@ -166,6 +150,7 @@ export const createAnnotationTool: ToolConfig<
166150
updated_at: data.updated_at,
167151
created_by: data.created_by || null,
168152
dashboard_item: data.dashboard_item || null,
153+
dashboard_id: data.dashboard_id || null,
169154
insight_short_id: data.insight_short_id || null,
170155
insight_name: data.insight_name || null,
171156
scope: data.scope || '',
@@ -205,6 +190,11 @@ export const createAnnotationTool: ToolConfig<
205190
description: 'ID of dashboard item this annotation is attached to',
206191
optional: true,
207192
},
193+
dashboard_id: {
194+
type: 'number',
195+
description: 'ID of the dashboard this annotation is attached to',
196+
optional: true,
197+
},
208198
insight_short_id: {
209199
type: 'string',
210200
description: 'Short ID of the insight this annotation is attached to',
@@ -217,7 +207,7 @@ export const createAnnotationTool: ToolConfig<
217207
},
218208
scope: {
219209
type: 'string',
220-
description: 'Scope of the annotation (project or dashboard_item)',
210+
description: 'Scope of the annotation (project, organization, dashboard, or dashboard_item)',
221211
},
222212
deleted: {
223213
type: 'boolean',

apps/sim/tools/posthog/create_cohort.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const createCohortTool: ToolConfig<PostHogCreateCohortParams, PostHogCrea
4040
description:
4141
'Create a new cohort in PostHog. Requires cohort name and filter or query configuration.',
4242
version: '1.0.0',
43+
errorExtractor: 'posthog-errors',
4344

4445
params: {
4546
apiKey: {
@@ -160,29 +161,6 @@ export const createCohortTool: ToolConfig<PostHogCreateCohortParams, PostHogCrea
160161
},
161162

162163
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-
186164
const data = await response.json()
187165

188166
return {

apps/sim/tools/posthog/create_dashboard.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const createDashboardTool: ToolConfig<
3636
description:
3737
'Create a new dashboard in PostHog. Optionally seed it from a built-in template, then attach insights to it afterward.',
3838
version: '1.0.0',
39+
errorExtractor: 'posthog-errors',
3940

4041
params: {
4142
apiKey: {
@@ -128,24 +129,6 @@ export const createDashboardTool: ToolConfig<
128129
},
129130

130131
transformResponse: async (response: Response) => {
131-
if (!response.ok) {
132-
const error = await response.text()
133-
return {
134-
success: false,
135-
output: {
136-
id: 0,
137-
name: '',
138-
description: '',
139-
pinned: false,
140-
created_at: '',
141-
tiles: [],
142-
filters: {},
143-
tags: [],
144-
},
145-
error: error || 'Failed to create dashboard',
146-
}
147-
}
148-
149132
const data = await response.json()
150133

151134
return {

apps/sim/tools/posthog/create_experiment.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ interface CreateExperimentParams {
1111
featureFlagKey: string
1212
parameters?: string
1313
filters?: string
14-
variants?: string
1514
startDate?: string
1615
endDate?: string
1716
}
@@ -24,7 +23,6 @@ interface Experiment {
2423
feature_flag: Record<string, any>
2524
parameters: Record<string, any>
2625
filters: Record<string, any>
27-
variants: Record<string, any>
2826
start_date: string | null
2927
end_date: string | null
3028
created_at: string
@@ -41,6 +39,7 @@ export const createExperimentTool: ToolConfig<CreateExperimentParams, CreateExpe
4139
name: 'PostHog Create Experiment',
4240
description: 'Create a new experiment in PostHog',
4341
version: '1.0.0',
42+
errorExtractor: 'posthog-errors',
4443

4544
params: {
4645
projectId: {
@@ -70,9 +69,9 @@ export const createExperimentTool: ToolConfig<CreateExperimentParams, CreateExpe
7069
},
7170
name: {
7271
type: 'string',
73-
required: false,
72+
required: true,
7473
visibility: 'user-or-llm',
75-
description: 'Experiment name (optional)',
74+
description: 'Experiment name',
7675
},
7776
description: {
7877
type: 'string',
@@ -98,12 +97,6 @@ export const createExperimentTool: ToolConfig<CreateExperimentParams, CreateExpe
9897
visibility: 'user-or-llm',
9998
description: 'Experiment filters as JSON string',
10099
},
101-
variants: {
102-
type: 'string',
103-
required: false,
104-
visibility: 'user-or-llm',
105-
description: 'Experiment variants as JSON string',
106-
},
107100
startDate: {
108101
type: 'string',
109102
required: false,
@@ -154,14 +147,6 @@ export const createExperimentTool: ToolConfig<CreateExperimentParams, CreateExpe
154147
}
155148
}
156149

157-
if (params.variants) {
158-
try {
159-
body.variants = JSON.parse(params.variants)
160-
} catch {
161-
body.variants = {}
162-
}
163-
}
164-
165150
if (params.startDate !== undefined) {
166151
body.start_date = params.startDate
167152
}
@@ -175,11 +160,6 @@ export const createExperimentTool: ToolConfig<CreateExperimentParams, CreateExpe
175160
},
176161

177162
transformResponse: async (response: Response) => {
178-
if (!response.ok) {
179-
const error = await response.text()
180-
throw new Error(error || 'Failed to create experiment')
181-
}
182-
183163
const data = await response.json()
184164

185165
return {
@@ -199,7 +179,6 @@ export const createExperimentTool: ToolConfig<CreateExperimentParams, CreateExpe
199179
feature_flag: { type: 'object', description: 'Feature flag details' },
200180
parameters: { type: 'object', description: 'Experiment parameters' },
201181
filters: { type: 'object', description: 'Experiment filters' },
202-
variants: { type: 'object', description: 'Experiment variants' },
203182
start_date: { type: 'string', description: 'Start date' },
204183
end_date: { type: 'string', description: 'End date' },
205184
created_at: { type: 'string', description: 'Creation timestamp' },

0 commit comments

Comments
 (0)