@keystrokehq/posthog 0.0.1

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.
Files changed (51) hide show
  1. package/README.md +237 -0
  2. package/dist/_official/index.d.mts +2 -0
  3. package/dist/_official/index.mjs +3 -0
  4. package/dist/_runtime/index.d.mts +7 -0
  5. package/dist/_runtime/index.mjs +3 -0
  6. package/dist/actions.d.mts +244 -0
  7. package/dist/actions.mjs +129 -0
  8. package/dist/annotations.d.mts +185 -0
  9. package/dist/annotations.mjs +141 -0
  10. package/dist/capture.d.mts +177 -0
  11. package/dist/capture.mjs +241 -0
  12. package/dist/client.d.mts +51 -0
  13. package/dist/client.mjs +200 -0
  14. package/dist/cohorts.d.mts +229 -0
  15. package/dist/cohorts.mjs +185 -0
  16. package/dist/connection.d.mts +2 -0
  17. package/dist/connection.mjs +3 -0
  18. package/dist/dashboards.d.mts +434 -0
  19. package/dist/dashboards.mjs +356 -0
  20. package/dist/decide.d.mts +74 -0
  21. package/dist/decide.mjs +75 -0
  22. package/dist/errors.d.mts +58 -0
  23. package/dist/errors.mjs +120 -0
  24. package/dist/events-management.d.mts +294 -0
  25. package/dist/events-management.mjs +242 -0
  26. package/dist/factory-DYDvHOGb.mjs +8 -0
  27. package/dist/feature-flags.d.mts +416 -0
  28. package/dist/feature-flags.mjs +317 -0
  29. package/dist/index.d.mts +1 -0
  30. package/dist/index.mjs +1 -0
  31. package/dist/insights.d.mts +409 -0
  32. package/dist/insights.mjs +346 -0
  33. package/dist/integration-CsCBBu4d.d.mts +53 -0
  34. package/dist/integration-Doy2Dwli.mjs +30 -0
  35. package/dist/organizations.d.mts +257 -0
  36. package/dist/organizations.mjs +219 -0
  37. package/dist/persons.d.mts +369 -0
  38. package/dist/persons.mjs +345 -0
  39. package/dist/projects.d.mts +348 -0
  40. package/dist/projects.mjs +287 -0
  41. package/dist/schemas.d.mts +351 -0
  42. package/dist/schemas.mjs +302 -0
  43. package/dist/session-recordings.d.mts +391 -0
  44. package/dist/session-recordings.mjs +308 -0
  45. package/dist/surveys.d.mts +287 -0
  46. package/dist/surveys.mjs +208 -0
  47. package/dist/triggers.d.mts +115 -0
  48. package/dist/triggers.mjs +330 -0
  49. package/dist/webhook-management.d.mts +188 -0
  50. package/dist/webhook-management.mjs +152 -0
  51. package/package.json +147 -0
@@ -0,0 +1,356 @@
1
+ import { createPosthogManagementClient } from "./client.mjs";
2
+ import { dashboardCollaboratorSchema, dashboardSchema, dashboardTemplateSchema, paginatedResponseSchema, projectIdInputShape, sharingConfigurationSchema } from "./schemas.mjs";
3
+ import { t as posthogOperation } from "./factory-DYDvHOGb.mjs";
4
+ import { z } from "zod";
5
+
6
+ //#region src/dashboards.ts
7
+ /**
8
+ * posthog/dashboards.ts
9
+ *
10
+ * Dashboard + dashboard templates + collaborators + sharing.
11
+ *
12
+ * Docs: https://posthog.com/docs/api/dashboards
13
+ */
14
+ const idInput = {
15
+ ...projectIdInputShape,
16
+ id: z.number().int()
17
+ };
18
+ const writeShape = {
19
+ name: z.string().optional(),
20
+ description: z.string().nullable().optional(),
21
+ pinned: z.boolean().optional(),
22
+ tags: z.array(z.string()).optional(),
23
+ filters: z.record(z.string(), z.unknown()).optional(),
24
+ deleted: z.boolean().optional()
25
+ };
26
+ const listDashboards = posthogOperation({
27
+ id: "posthog_dashboards_list",
28
+ name: "PostHog List Dashboards",
29
+ description: "List dashboards in a project",
30
+ input: z.object({
31
+ ...projectIdInputShape,
32
+ limit: z.number().int().min(1).max(500).optional(),
33
+ offset: z.number().int().min(0).optional(),
34
+ search: z.string().optional(),
35
+ pinned: z.boolean().optional()
36
+ }),
37
+ output: paginatedResponseSchema(dashboardSchema),
38
+ run: async (input, credentials) => {
39
+ const client = createPosthogManagementClient(credentials);
40
+ const projectId = client.projectId(input.projectId);
41
+ return client.request({
42
+ method: "GET",
43
+ path: `/api/projects/${projectId}/dashboards/`,
44
+ query: {
45
+ limit: input.limit,
46
+ offset: input.offset,
47
+ search: input.search,
48
+ pinned: input.pinned
49
+ }
50
+ });
51
+ }
52
+ });
53
+ const getDashboard = posthogOperation({
54
+ id: "posthog_dashboards_get",
55
+ name: "PostHog Get Dashboard",
56
+ description: "Retrieve a dashboard by id",
57
+ input: z.object(idInput),
58
+ output: dashboardSchema,
59
+ run: async (input, credentials) => {
60
+ const client = createPosthogManagementClient(credentials);
61
+ const projectId = client.projectId(input.projectId);
62
+ return client.request({
63
+ method: "GET",
64
+ path: `/api/projects/${projectId}/dashboards/${input.id}/`
65
+ });
66
+ }
67
+ });
68
+ const createDashboard = posthogOperation({
69
+ id: "posthog_dashboards_create",
70
+ name: "PostHog Create Dashboard",
71
+ description: "Create a new dashboard",
72
+ input: z.object({
73
+ ...projectIdInputShape,
74
+ ...writeShape,
75
+ name: z.string().min(1)
76
+ }),
77
+ output: dashboardSchema,
78
+ needsApproval: true,
79
+ run: async (input, credentials) => {
80
+ const client = createPosthogManagementClient(credentials);
81
+ const projectId = client.projectId(input.projectId);
82
+ const { projectId: _pid, ...body } = input;
83
+ return client.request({
84
+ method: "POST",
85
+ path: `/api/projects/${projectId}/dashboards/`,
86
+ body
87
+ });
88
+ }
89
+ });
90
+ const updateDashboard = posthogOperation({
91
+ id: "posthog_dashboards_update",
92
+ name: "PostHog Update Dashboard",
93
+ description: "Partial update to a dashboard",
94
+ input: z.object({
95
+ ...idInput,
96
+ ...writeShape
97
+ }),
98
+ output: dashboardSchema,
99
+ needsApproval: true,
100
+ run: async (input, credentials) => {
101
+ const client = createPosthogManagementClient(credentials);
102
+ const projectId = client.projectId(input.projectId);
103
+ const { projectId: _pid, id, ...body } = input;
104
+ return client.request({
105
+ method: "PATCH",
106
+ path: `/api/projects/${projectId}/dashboards/${id}/`,
107
+ body
108
+ });
109
+ }
110
+ });
111
+ const deleteDashboard = posthogOperation({
112
+ id: "posthog_dashboards_delete",
113
+ name: "PostHog Delete Dashboard",
114
+ description: "Soft-delete a dashboard",
115
+ input: z.object(idInput),
116
+ output: dashboardSchema,
117
+ needsApproval: true,
118
+ run: async (input, credentials) => {
119
+ const client = createPosthogManagementClient(credentials);
120
+ const projectId = client.projectId(input.projectId);
121
+ return client.request({
122
+ method: "PATCH",
123
+ path: `/api/projects/${projectId}/dashboards/${input.id}/`,
124
+ body: { deleted: true }
125
+ });
126
+ }
127
+ });
128
+ const getDashboardSharing = posthogOperation({
129
+ id: "posthog_dashboards_sharing_get",
130
+ name: "PostHog Get Dashboard Sharing",
131
+ description: "Fetch sharing configuration for a dashboard",
132
+ input: z.object(idInput),
133
+ output: sharingConfigurationSchema,
134
+ run: async (input, credentials) => {
135
+ const client = createPosthogManagementClient(credentials);
136
+ const projectId = client.projectId(input.projectId);
137
+ return client.request({
138
+ method: "GET",
139
+ path: `/api/projects/${projectId}/dashboards/${input.id}/sharing/`
140
+ });
141
+ }
142
+ });
143
+ const updateDashboardSharing = posthogOperation({
144
+ id: "posthog_dashboards_sharing_update",
145
+ name: "PostHog Update Dashboard Sharing",
146
+ description: "Enable or disable sharing for a dashboard",
147
+ input: z.object({
148
+ ...idInput,
149
+ enabled: z.boolean()
150
+ }),
151
+ output: sharingConfigurationSchema,
152
+ needsApproval: true,
153
+ run: async (input, credentials) => {
154
+ const client = createPosthogManagementClient(credentials);
155
+ const projectId = client.projectId(input.projectId);
156
+ return client.request({
157
+ method: "PATCH",
158
+ path: `/api/projects/${projectId}/dashboards/${input.id}/sharing/`,
159
+ body: { enabled: input.enabled }
160
+ });
161
+ }
162
+ });
163
+ const listDashboardCollaborators = posthogOperation({
164
+ id: "posthog_dashboards_collaborators_list",
165
+ name: "PostHog List Dashboard Collaborators",
166
+ description: "List collaborators on a dashboard",
167
+ input: z.object(idInput),
168
+ output: paginatedResponseSchema(dashboardCollaboratorSchema),
169
+ run: async (input, credentials) => {
170
+ const client = createPosthogManagementClient(credentials);
171
+ const projectId = client.projectId(input.projectId);
172
+ return client.request({
173
+ method: "GET",
174
+ path: `/api/projects/${projectId}/dashboards/${input.id}/collaborators/`
175
+ });
176
+ }
177
+ });
178
+ const addDashboardCollaborator = posthogOperation({
179
+ id: "posthog_dashboards_collaborators_add",
180
+ name: "PostHog Add Dashboard Collaborator",
181
+ description: "Add a collaborator to a dashboard",
182
+ input: z.object({
183
+ ...idInput,
184
+ user_uuid: z.string().min(1),
185
+ access_level: z.enum(["can_view", "can_edit"]).default("can_view")
186
+ }),
187
+ output: dashboardCollaboratorSchema,
188
+ needsApproval: true,
189
+ run: async (input, credentials) => {
190
+ const client = createPosthogManagementClient(credentials);
191
+ const projectId = client.projectId(input.projectId);
192
+ return client.request({
193
+ method: "POST",
194
+ path: `/api/projects/${projectId}/dashboards/${input.id}/collaborators/`,
195
+ body: {
196
+ user_uuid: input.user_uuid,
197
+ level: input.access_level
198
+ }
199
+ });
200
+ }
201
+ });
202
+ const removeDashboardCollaborator = posthogOperation({
203
+ id: "posthog_dashboards_collaborators_remove",
204
+ name: "PostHog Remove Dashboard Collaborator",
205
+ description: "Remove a collaborator from a dashboard",
206
+ input: z.object({
207
+ ...idInput,
208
+ collaboratorId: z.number().int()
209
+ }),
210
+ output: z.object({ success: z.boolean() }),
211
+ needsApproval: true,
212
+ run: async (input, credentials) => {
213
+ const client = createPosthogManagementClient(credentials);
214
+ const projectId = client.projectId(input.projectId);
215
+ await client.request({
216
+ method: "DELETE",
217
+ path: `/api/projects/${projectId}/dashboards/${input.id}/collaborators/${input.collaboratorId}/`
218
+ });
219
+ return { success: true };
220
+ }
221
+ });
222
+ const listDashboardTemplates = posthogOperation({
223
+ id: "posthog_dashboard_templates_list",
224
+ name: "PostHog List Dashboard Templates",
225
+ description: "List dashboard templates available in a project",
226
+ input: z.object({
227
+ ...projectIdInputShape,
228
+ limit: z.number().int().min(1).max(200).optional(),
229
+ offset: z.number().int().min(0).optional()
230
+ }),
231
+ output: paginatedResponseSchema(dashboardTemplateSchema),
232
+ run: async (input, credentials) => {
233
+ const client = createPosthogManagementClient(credentials);
234
+ const projectId = client.projectId(input.projectId);
235
+ return client.request({
236
+ method: "GET",
237
+ path: `/api/projects/${projectId}/dashboard_templates/`,
238
+ query: {
239
+ limit: input.limit,
240
+ offset: input.offset
241
+ }
242
+ });
243
+ }
244
+ });
245
+ const getDashboardTemplate = posthogOperation({
246
+ id: "posthog_dashboard_templates_get",
247
+ name: "PostHog Get Dashboard Template",
248
+ description: "Retrieve a dashboard template by id",
249
+ input: z.object({
250
+ ...projectIdInputShape,
251
+ templateId: z.string().min(1)
252
+ }),
253
+ output: dashboardTemplateSchema,
254
+ run: async (input, credentials) => {
255
+ const client = createPosthogManagementClient(credentials);
256
+ const projectId = client.projectId(input.projectId);
257
+ return client.request({
258
+ method: "GET",
259
+ path: `/api/projects/${projectId}/dashboard_templates/${input.templateId}/`
260
+ });
261
+ }
262
+ });
263
+ const createDashboardTemplate = posthogOperation({
264
+ id: "posthog_dashboard_templates_create",
265
+ name: "PostHog Create Dashboard Template",
266
+ description: "Create a dashboard template from a dashboard",
267
+ input: z.object({
268
+ ...projectIdInputShape,
269
+ template_name: z.string().min(1),
270
+ dashboard_description: z.string().nullable().optional(),
271
+ dashboard_filters: z.record(z.string(), z.unknown()).nullable().optional(),
272
+ tags: z.array(z.string()).optional(),
273
+ tiles: z.array(z.record(z.string(), z.unknown())).optional()
274
+ }),
275
+ output: dashboardTemplateSchema,
276
+ needsApproval: true,
277
+ run: async (input, credentials) => {
278
+ const client = createPosthogManagementClient(credentials);
279
+ const projectId = client.projectId(input.projectId);
280
+ const { projectId: _pid, ...body } = input;
281
+ return client.request({
282
+ method: "POST",
283
+ path: `/api/projects/${projectId}/dashboard_templates/`,
284
+ body
285
+ });
286
+ }
287
+ });
288
+ const updateDashboardTemplate = posthogOperation({
289
+ id: "posthog_dashboard_templates_update",
290
+ name: "PostHog Update Dashboard Template",
291
+ description: "Partial update to a dashboard template",
292
+ input: z.object({
293
+ ...projectIdInputShape,
294
+ templateId: z.string().min(1),
295
+ template_name: z.string().optional(),
296
+ dashboard_description: z.string().nullable().optional(),
297
+ dashboard_filters: z.record(z.string(), z.unknown()).nullable().optional(),
298
+ tags: z.array(z.string()).optional(),
299
+ tiles: z.array(z.record(z.string(), z.unknown())).optional(),
300
+ deleted: z.boolean().optional()
301
+ }),
302
+ output: dashboardTemplateSchema,
303
+ needsApproval: true,
304
+ run: async (input, credentials) => {
305
+ const client = createPosthogManagementClient(credentials);
306
+ const projectId = client.projectId(input.projectId);
307
+ const { projectId: _pid, templateId, ...body } = input;
308
+ return client.request({
309
+ method: "PATCH",
310
+ path: `/api/projects/${projectId}/dashboard_templates/${templateId}/`,
311
+ body
312
+ });
313
+ }
314
+ });
315
+ const deleteDashboardTemplate = posthogOperation({
316
+ id: "posthog_dashboard_templates_delete",
317
+ name: "PostHog Delete Dashboard Template",
318
+ description: "Delete a dashboard template",
319
+ input: z.object({
320
+ ...projectIdInputShape,
321
+ templateId: z.string().min(1)
322
+ }),
323
+ output: z.object({ success: z.boolean() }),
324
+ needsApproval: true,
325
+ run: async (input, credentials) => {
326
+ const client = createPosthogManagementClient(credentials);
327
+ const projectId = client.projectId(input.projectId);
328
+ await client.request({
329
+ method: "DELETE",
330
+ path: `/api/projects/${projectId}/dashboard_templates/${input.templateId}/`
331
+ });
332
+ return { success: true };
333
+ }
334
+ });
335
+ const getDashboardActivity = posthogOperation({
336
+ id: "posthog_dashboards_activity",
337
+ name: "PostHog Dashboard Activity",
338
+ description: "Fetch activity log for a dashboard",
339
+ input: z.object({
340
+ ...idInput,
341
+ limit: z.number().int().min(1).max(200).optional()
342
+ }),
343
+ output: z.object({ results: z.array(z.record(z.string(), z.unknown())) }),
344
+ run: async (input, credentials) => {
345
+ const client = createPosthogManagementClient(credentials);
346
+ const projectId = client.projectId(input.projectId);
347
+ return client.request({
348
+ method: "GET",
349
+ path: `/api/projects/${projectId}/dashboards/${input.id}/activity/`,
350
+ query: { limit: input.limit }
351
+ });
352
+ }
353
+ });
354
+
355
+ //#endregion
356
+ export { addDashboardCollaborator, createDashboard, createDashboardTemplate, deleteDashboard, deleteDashboardTemplate, getDashboard, getDashboardActivity, getDashboardSharing, getDashboardTemplate, listDashboardCollaborators, listDashboardTemplates, listDashboards, removeDashboardCollaborator, updateDashboard, updateDashboardSharing, updateDashboardTemplate };
@@ -0,0 +1,74 @@
1
+ import { z } from "zod";
2
+ import * as _keystrokehq_core_credential_set0 from "@keystrokehq/core/credential-set";
3
+ import * as _keystrokehq_core0 from "@keystrokehq/core";
4
+
5
+ //#region src/decide.d.ts
6
+ /** Evaluate feature flags for a distinct_id (server-side). Maps Sim tool S8. */
7
+ declare const decideFeatureFlags: _keystrokehq_core0.Operation<z.ZodObject<{
8
+ distinctId: z.ZodString;
9
+ personProperties: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
10
+ groups: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
11
+ groupProperties: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
12
+ version: z.ZodDefault<z.ZodNumber>;
13
+ }, z.core.$strip>, z.ZodObject<{
14
+ config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
15
+ featureFlags: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodBoolean, z.ZodString, z.ZodNumber]>>>;
16
+ featureFlagPayloads: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
17
+ errorsWhileComputingFlags: z.ZodOptional<z.ZodBoolean>;
18
+ toolbarParams: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
19
+ sessionRecording: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodRecord<z.ZodString, z.ZodUnknown>]>>;
20
+ }, z.core.$strip>, readonly [_keystrokehq_core0.CredentialSet<"posthog", z.ZodObject<{
21
+ POSTHOG_PERSONAL_API_KEY: z.ZodString;
22
+ POSTHOG_PROJECT_API_KEY: z.ZodOptional<z.ZodString>;
23
+ POSTHOG_HOST: z.ZodDefault<z.ZodURL>;
24
+ POSTHOG_PROJECT_ID: z.ZodOptional<z.ZodString>;
25
+ POSTHOG_SCOPES: z.ZodOptional<z.ZodArray<z.ZodString>>;
26
+ }, z.core.$strip>, readonly _keystrokehq_core_credential_set0.CredentialConnection<z.ZodObject<{
27
+ POSTHOG_PERSONAL_API_KEY: z.ZodString;
28
+ POSTHOG_PROJECT_API_KEY: z.ZodOptional<z.ZodString>;
29
+ POSTHOG_HOST: z.ZodDefault<z.ZodURL>;
30
+ POSTHOG_PROJECT_ID: z.ZodOptional<z.ZodString>;
31
+ POSTHOG_SCOPES: z.ZodOptional<z.ZodArray<z.ZodString>>;
32
+ }, z.core.$strip>>[] | undefined>], undefined>;
33
+ /**
34
+ * Fetch the local-evaluation feature-flag payload for running a local SDK.
35
+ * Maps Sim tool S9 and Composio slug `manage_project_feature_flags_for_local_evaluation`.
36
+ */
37
+ declare const decideFeatureFlagsLocal: _keystrokehq_core0.Operation<z.ZodObject<{
38
+ projectId: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
39
+ sendCohorts: z.ZodDefault<z.ZodBoolean>;
40
+ }, z.core.$strip>, z.ZodObject<{
41
+ flags: z.ZodOptional<z.ZodArray<z.ZodObject<{
42
+ id: z.ZodOptional<z.ZodNumber>;
43
+ key: z.ZodString;
44
+ name: z.ZodOptional<z.ZodString>;
45
+ filters: z.ZodOptional<z.ZodObject<{
46
+ groups: z.ZodOptional<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
47
+ multivariate: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
48
+ payloads: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
49
+ }, z.core.$strip>>;
50
+ deleted: z.ZodOptional<z.ZodBoolean>;
51
+ active: z.ZodOptional<z.ZodBoolean>;
52
+ created_at: z.ZodOptional<z.ZodString>;
53
+ created_by: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
54
+ ensure_experience_continuity: z.ZodOptional<z.ZodBoolean>;
55
+ rollout_percentage: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
56
+ version: z.ZodOptional<z.ZodNumber>;
57
+ }, z.core.$strip>>>;
58
+ group_type_mapping: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
59
+ cohorts: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
60
+ }, z.core.$strip>, readonly [_keystrokehq_core0.CredentialSet<"posthog", z.ZodObject<{
61
+ POSTHOG_PERSONAL_API_KEY: z.ZodString;
62
+ POSTHOG_PROJECT_API_KEY: z.ZodOptional<z.ZodString>;
63
+ POSTHOG_HOST: z.ZodDefault<z.ZodURL>;
64
+ POSTHOG_PROJECT_ID: z.ZodOptional<z.ZodString>;
65
+ POSTHOG_SCOPES: z.ZodOptional<z.ZodArray<z.ZodString>>;
66
+ }, z.core.$strip>, readonly _keystrokehq_core_credential_set0.CredentialConnection<z.ZodObject<{
67
+ POSTHOG_PERSONAL_API_KEY: z.ZodString;
68
+ POSTHOG_PROJECT_API_KEY: z.ZodOptional<z.ZodString>;
69
+ POSTHOG_HOST: z.ZodDefault<z.ZodURL>;
70
+ POSTHOG_PROJECT_ID: z.ZodOptional<z.ZodString>;
71
+ POSTHOG_SCOPES: z.ZodOptional<z.ZodArray<z.ZodString>>;
72
+ }, z.core.$strip>>[] | undefined>], undefined>;
73
+ //#endregion
74
+ export { decideFeatureFlags, decideFeatureFlagsLocal };
@@ -0,0 +1,75 @@
1
+ import { createPosthogDecideClient, createPosthogManagementClient } from "./client.mjs";
2
+ import { decideResponseSchema, localEvaluationResponseSchema, propertiesBagSchema } from "./schemas.mjs";
3
+ import { t as posthogOperation } from "./factory-DYDvHOGb.mjs";
4
+ import { z } from "zod";
5
+
6
+ //#region src/decide.ts
7
+ /**
8
+ * posthog/decide.ts
9
+ *
10
+ * Server-side feature-flag evaluation.
11
+ *
12
+ * - `decideFeatureFlags` — hot-path evaluation via `/decide/`
13
+ * (Sim tool S8). Uses the project API key and returns resolved flag
14
+ * values plus payloads.
15
+ * - `decideFeatureFlagsLocal` — local-evaluation payload for running a
16
+ * local SDK (Sim tool S9). Uses the personal API key against
17
+ * `/api/feature_flag/local_evaluation/`.
18
+ *
19
+ * Docs: https://posthog.com/docs/api/decide
20
+ */
21
+ /** Evaluate feature flags for a distinct_id (server-side). Maps Sim tool S8. */
22
+ const decideFeatureFlags = posthogOperation({
23
+ id: "posthog_decide_feature_flags",
24
+ name: "PostHog Decide Feature Flags",
25
+ description: "Evaluate all feature flags for a distinct_id via the /decide endpoint",
26
+ input: z.object({
27
+ distinctId: z.string().min(1),
28
+ personProperties: propertiesBagSchema.optional(),
29
+ groups: z.record(z.string(), z.string()).optional(),
30
+ groupProperties: z.record(z.string(), propertiesBagSchema).optional(),
31
+ version: z.number().int().min(1).default(3)
32
+ }),
33
+ output: decideResponseSchema,
34
+ run: async (input, credentials) => {
35
+ const client = createPosthogDecideClient(credentials);
36
+ return client.request({
37
+ method: "POST",
38
+ path: "/decide/",
39
+ query: { v: input.version },
40
+ body: {
41
+ api_key: client.projectApiKey(),
42
+ distinct_id: input.distinctId,
43
+ ...input.personProperties ? { person_properties: input.personProperties } : {},
44
+ ...input.groups ? { groups: input.groups } : {},
45
+ ...input.groupProperties ? { group_properties: input.groupProperties } : {}
46
+ }
47
+ });
48
+ }
49
+ });
50
+ /**
51
+ * Fetch the local-evaluation feature-flag payload for running a local SDK.
52
+ * Maps Sim tool S9 and Composio slug `manage_project_feature_flags_for_local_evaluation`.
53
+ */
54
+ const decideFeatureFlagsLocal = posthogOperation({
55
+ id: "posthog_decide_feature_flags_local",
56
+ name: "PostHog Local Evaluation Payload",
57
+ description: "Fetch the local-evaluation payload for all feature flags in a project",
58
+ input: z.object({
59
+ projectId: z.union([z.string(), z.number().int()]).optional(),
60
+ sendCohorts: z.boolean().default(true)
61
+ }),
62
+ output: localEvaluationResponseSchema,
63
+ run: async (input, credentials) => {
64
+ const client = createPosthogManagementClient(credentials);
65
+ const projectId = client.projectId(input.projectId);
66
+ return client.request({
67
+ method: "GET",
68
+ path: `/api/projects/${projectId}/feature_flags/local_evaluation/`,
69
+ query: { send_cohorts: input.sendCohorts }
70
+ });
71
+ }
72
+ });
73
+
74
+ //#endregion
75
+ export { decideFeatureFlags, decideFeatureFlagsLocal };
@@ -0,0 +1,58 @@
1
+ //#region src/errors.d.ts
2
+ /**
3
+ * posthog/errors.ts
4
+ *
5
+ * Typed error surface for the PostHog client.
6
+ *
7
+ * `PosthogClientError` is a discriminated union on `kind` that wraps
8
+ * every failure the client can emit. It preserves the HTTP status,
9
+ * provider-reported error code, request id, and retryability so
10
+ * callers can make structured decisions rather than string-parsing.
11
+ */
12
+ type PosthogErrorKind = 'http' | 'rate_limit' | 'auth' | 'validation' | 'bad_credential_shape' | 'region_mismatch';
13
+ interface PosthogClientErrorInit {
14
+ readonly kind: PosthogErrorKind;
15
+ readonly message: string;
16
+ readonly status: number | null;
17
+ readonly providerCode: string | null;
18
+ readonly detail: string;
19
+ readonly requestId: string | null;
20
+ readonly retryable: boolean;
21
+ readonly retryAfterMs?: number | null;
22
+ readonly body?: string;
23
+ }
24
+ /**
25
+ * Typed error thrown by `createPosthogManagementClient`,
26
+ * `createPosthogIngestClient`, and `createPosthogDecideClient`.
27
+ *
28
+ * Use `isPosthogClientError` to type-narrow and read the `kind` field.
29
+ */
30
+ declare class PosthogClientError extends Error {
31
+ readonly name = "PosthogClientError";
32
+ readonly kind: PosthogErrorKind;
33
+ readonly status: number | null;
34
+ readonly providerCode: string | null;
35
+ readonly detail: string;
36
+ readonly requestId: string | null;
37
+ readonly retryable: boolean;
38
+ readonly retryAfterMs: number | null;
39
+ readonly body: string;
40
+ constructor(init: PosthogClientErrorInit);
41
+ }
42
+ /**
43
+ * Structural type guard. Tolerates cross-realm errors by checking `name`.
44
+ */
45
+ declare function isPosthogClientError(error: unknown): error is PosthogClientError;
46
+ /**
47
+ * Classify an HTTP status plus response body into a `PosthogClientError`.
48
+ * Exposed so the client can convert `JsonRestError` failures uniformly.
49
+ */
50
+ declare function classifyHttpFailure(params: {
51
+ status: number;
52
+ statusText: string;
53
+ body: string;
54
+ headers: Headers;
55
+ errorPrefix: string;
56
+ }): PosthogClientError;
57
+ //#endregion
58
+ export { PosthogClientError, PosthogClientErrorInit, PosthogErrorKind, classifyHttpFailure, isPosthogClientError };
@@ -0,0 +1,120 @@
1
+ //#region src/errors.ts
2
+ /**
3
+ * Typed error thrown by `createPosthogManagementClient`,
4
+ * `createPosthogIngestClient`, and `createPosthogDecideClient`.
5
+ *
6
+ * Use `isPosthogClientError` to type-narrow and read the `kind` field.
7
+ */
8
+ var PosthogClientError = class extends Error {
9
+ name = "PosthogClientError";
10
+ kind;
11
+ status;
12
+ providerCode;
13
+ detail;
14
+ requestId;
15
+ retryable;
16
+ retryAfterMs;
17
+ body;
18
+ constructor(init) {
19
+ super(init.message);
20
+ this.kind = init.kind;
21
+ this.status = init.status;
22
+ this.providerCode = init.providerCode;
23
+ this.detail = init.detail;
24
+ this.requestId = init.requestId;
25
+ this.retryable = init.retryable;
26
+ this.retryAfterMs = init.retryAfterMs ?? null;
27
+ this.body = init.body ?? "";
28
+ }
29
+ };
30
+ /**
31
+ * Structural type guard. Tolerates cross-realm errors by checking `name`.
32
+ */
33
+ function isPosthogClientError(error) {
34
+ if (!error || typeof error !== "object") return false;
35
+ const candidate = error;
36
+ return candidate.name === "PosthogClientError" && typeof candidate.kind === "string";
37
+ }
38
+ /**
39
+ * Classify an HTTP status plus response body into a `PosthogClientError`.
40
+ * Exposed so the client can convert `JsonRestError` failures uniformly.
41
+ */
42
+ function classifyHttpFailure(params) {
43
+ const { status, statusText, body, headers, errorPrefix } = params;
44
+ const requestId = headers.get("x-request-id") ?? headers.get("X-Request-Id") ?? null;
45
+ const retryAfterMs = parseRetryAfter(headers.get("retry-after"));
46
+ const { providerCode, detail } = parsePosthogErrorBody(body);
47
+ if (status === 429) return new PosthogClientError({
48
+ kind: "rate_limit",
49
+ message: `${errorPrefix}: 429 ${statusText}${detail ? ` — ${detail}` : ""}`,
50
+ status,
51
+ providerCode,
52
+ detail: detail ?? statusText,
53
+ requestId,
54
+ retryable: true,
55
+ retryAfterMs,
56
+ body
57
+ });
58
+ if (status === 401 || status === 403) return new PosthogClientError({
59
+ kind: "auth",
60
+ message: `${errorPrefix}: ${status} ${statusText}${detail ? ` — ${detail}` : ""}`,
61
+ status,
62
+ providerCode,
63
+ detail: detail ?? statusText,
64
+ requestId,
65
+ retryable: false,
66
+ body
67
+ });
68
+ if (status === 400 || status === 422) return new PosthogClientError({
69
+ kind: "validation",
70
+ message: `${errorPrefix}: ${status} ${statusText}${detail ? ` — ${detail}` : ""}`,
71
+ status,
72
+ providerCode,
73
+ detail: detail ?? statusText,
74
+ requestId,
75
+ retryable: false,
76
+ body
77
+ });
78
+ return new PosthogClientError({
79
+ kind: "http",
80
+ message: `${errorPrefix}: ${status} ${statusText}${detail ? ` — ${detail}` : ""}`,
81
+ status,
82
+ providerCode,
83
+ detail: detail ?? statusText,
84
+ requestId,
85
+ retryable: status >= 500,
86
+ body
87
+ });
88
+ }
89
+ function parseRetryAfter(value) {
90
+ if (!value) return null;
91
+ const seconds = Number.parseInt(value, 10);
92
+ if (Number.isFinite(seconds)) return seconds * 1e3;
93
+ const asDate = Date.parse(value);
94
+ if (Number.isFinite(asDate)) {
95
+ const delta = asDate - Date.now();
96
+ return delta > 0 ? delta : 0;
97
+ }
98
+ return null;
99
+ }
100
+ function parsePosthogErrorBody(body) {
101
+ if (!body) return {
102
+ providerCode: null,
103
+ detail: null
104
+ };
105
+ try {
106
+ const parsed = JSON.parse(body);
107
+ return {
108
+ providerCode: (typeof parsed.code === "string" ? parsed.code : null) ?? (typeof parsed.type === "string" ? parsed.type : null),
109
+ detail: (typeof parsed.detail === "string" ? parsed.detail : null) ?? (typeof parsed.error === "string" ? parsed.error : null) ?? (typeof parsed.message === "string" ? parsed.message : null)
110
+ };
111
+ } catch {
112
+ return {
113
+ providerCode: null,
114
+ detail: body.length < 500 ? body : null
115
+ };
116
+ }
117
+ }
118
+
119
+ //#endregion
120
+ export { PosthogClientError, classifyHttpFailure, isPosthogClientError };