@contractspec/example.product-intent 1.57.0 → 1.59.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.
Files changed (42) hide show
  1. package/.turbo/turbo-build.log +21 -96
  2. package/.turbo/turbo-prebuild.log +1 -0
  3. package/CHANGELOG.md +29 -0
  4. package/dist/example.d.ts +2 -6
  5. package/dist/example.d.ts.map +1 -1
  6. package/dist/example.js +27 -37
  7. package/dist/index.d.ts +6 -4
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +342 -4
  10. package/dist/load-evidence.d.ts +13 -17
  11. package/dist/load-evidence.d.ts.map +1 -1
  12. package/dist/load-evidence.js +307 -68
  13. package/dist/load-evidence.test.d.ts +2 -0
  14. package/dist/load-evidence.test.d.ts.map +1 -0
  15. package/dist/node/example.js +28 -0
  16. package/dist/node/index.js +342 -0
  17. package/dist/node/load-evidence.js +313 -0
  18. package/dist/node/posthog-signals.js +248 -0
  19. package/dist/node/script.js +512 -0
  20. package/dist/node/sync-actions.js +491 -0
  21. package/dist/posthog-signals.d.ts +15 -19
  22. package/dist/posthog-signals.d.ts.map +1 -1
  23. package/dist/posthog-signals.js +222 -178
  24. package/dist/script.d.ts +2 -1
  25. package/dist/script.d.ts.map +1 -0
  26. package/dist/script.js +493 -152
  27. package/dist/sync-actions.d.ts +2 -1
  28. package/dist/sync-actions.d.ts.map +1 -0
  29. package/dist/sync-actions.js +466 -128
  30. package/package.json +57 -27
  31. package/tsdown.config.js +1 -2
  32. package/.turbo/turbo-build$colon$bundle.log +0 -99
  33. package/dist/example.js.map +0 -1
  34. package/dist/libs/analytics/dist/funnel/analyzer.js +0 -64
  35. package/dist/libs/analytics/dist/funnel/analyzer.js.map +0 -1
  36. package/dist/libs/analytics/dist/types.d.ts +0 -22
  37. package/dist/libs/analytics/dist/types.d.ts.map +0 -1
  38. package/dist/load-evidence.js.map +0 -1
  39. package/dist/posthog-signals.js.map +0 -1
  40. package/dist/script.js.map +0 -1
  41. package/dist/sync-actions.js.map +0 -1
  42. package/tsconfig.tsbuildinfo +0 -1
@@ -1,205 +1,249 @@
1
- import { FunnelAnalyzer } from "./libs/analytics/dist/funnel/analyzer.js";
1
+ // @bun
2
+ // src/posthog-signals.ts
3
+ import { FunnelAnalyzer } from "@contractspec/lib.analytics/funnel";
2
4
  import { PosthogAnalyticsProvider } from "@contractspec/integration.providers-impls/impls/posthog";
3
-
4
- //#region src/posthog-signals.ts
5
5
  async function loadPosthogEvidenceChunks(options) {
6
- const chunks = [];
7
- const range = resolveRange(options.dateRange);
8
- const eventSummary = await loadEventSummary(options, range);
9
- if (eventSummary) chunks.push(eventSummary);
10
- const funnelEvidence = await loadFunnelEvidence(options, range);
11
- if (funnelEvidence) chunks.push(funnelEvidence);
12
- const featureFlags = await loadFeatureFlagEvidence(options);
13
- if (featureFlags) chunks.push(featureFlags);
14
- return chunks;
6
+ const chunks = [];
7
+ const range = resolveRange(options.dateRange);
8
+ const eventSummary = await loadEventSummary(options, range);
9
+ if (eventSummary) {
10
+ chunks.push(eventSummary);
11
+ }
12
+ const funnelEvidence = await loadFunnelEvidence(options, range);
13
+ if (funnelEvidence) {
14
+ chunks.push(funnelEvidence);
15
+ }
16
+ const featureFlags = await loadFeatureFlagEvidence(options);
17
+ if (featureFlags) {
18
+ chunks.push(featureFlags);
19
+ }
20
+ return chunks;
15
21
  }
16
22
  async function loadEventSummary(options, range) {
17
- if (!options.reader.queryHogQL) return null;
18
- const eventFilter = buildEventFilter(options.eventNames);
19
- const limit = options.limit ?? 10;
20
- const rows = mapRows(await options.reader.queryHogQL({
21
- query: [
22
- "select",
23
- " event as eventName,",
24
- " count() as total",
25
- "from events",
26
- "where timestamp >= {dateFrom} and timestamp < {dateTo}",
27
- eventFilter.clause ? `and ${eventFilter.clause}` : "",
28
- "group by eventName",
29
- "order by total desc",
30
- `limit ${limit}`
31
- ].filter(Boolean).join("\n"),
32
- values: {
33
- dateFrom: range.from.toISOString(),
34
- dateTo: range.to.toISOString(),
35
- ...eventFilter.values
36
- }
37
- }));
38
- if (rows.length === 0) return null;
39
- const lines = rows.map((row) => {
40
- return `- ${asString(row.eventName) ?? "unknown"}: ${asNumber(row.total)}`;
41
- });
42
- return {
43
- chunkId: `posthog:event_summary:${range.from.toISOString()}`,
44
- text: [`PostHog event summary (${range.from.toISOString()} ${range.to.toISOString()}):`, ...lines].join("\n"),
45
- meta: {
46
- source: "posthog",
47
- kind: "event_summary",
48
- dateFrom: range.from.toISOString(),
49
- dateTo: range.to.toISOString()
50
- }
51
- };
23
+ if (!options.reader.queryHogQL)
24
+ return null;
25
+ const eventFilter = buildEventFilter(options.eventNames);
26
+ const limit = options.limit ?? 10;
27
+ const result = await options.reader.queryHogQL({
28
+ query: [
29
+ "select",
30
+ " event as eventName,",
31
+ " count() as total",
32
+ "from events",
33
+ "where timestamp >= {dateFrom} and timestamp < {dateTo}",
34
+ eventFilter.clause ? `and ${eventFilter.clause}` : "",
35
+ "group by eventName",
36
+ "order by total desc",
37
+ `limit ${limit}`
38
+ ].filter(Boolean).join(`
39
+ `),
40
+ values: {
41
+ dateFrom: range.from.toISOString(),
42
+ dateTo: range.to.toISOString(),
43
+ ...eventFilter.values
44
+ }
45
+ });
46
+ const rows = mapRows(result);
47
+ if (rows.length === 0)
48
+ return null;
49
+ const lines = rows.map((row) => {
50
+ const name = asString(row.eventName) ?? "unknown";
51
+ const total = asNumber(row.total);
52
+ return `- ${name}: ${total}`;
53
+ });
54
+ return {
55
+ chunkId: `posthog:event_summary:${range.from.toISOString()}`,
56
+ text: [
57
+ `PostHog event summary (${range.from.toISOString()} \u2192 ${range.to.toISOString()}):`,
58
+ ...lines
59
+ ].join(`
60
+ `),
61
+ meta: {
62
+ source: "posthog",
63
+ kind: "event_summary",
64
+ dateFrom: range.from.toISOString(),
65
+ dateTo: range.to.toISOString()
66
+ }
67
+ };
52
68
  }
53
69
  async function loadFunnelEvidence(options, range) {
54
- if (!options.funnel) return null;
55
- if (!options.reader.getEvents) return null;
56
- const events = [];
57
- const eventNames = options.funnel.steps.map((step) => step.eventName);
58
- for (const eventName of eventNames) (await options.reader.getEvents({
59
- event: eventName,
60
- dateRange: {
61
- from: range.from,
62
- to: range.to
63
- },
64
- limit: options.limit ?? 500
65
- })).results.forEach((event) => {
66
- events.push({
67
- name: event.event,
68
- userId: event.distinctId,
69
- tenantId: typeof event.properties?.tenantId === "string" ? event.properties.tenantId : void 0,
70
- timestamp: event.timestamp,
71
- properties: event.properties
72
- });
73
- });
74
- if (events.length === 0) return null;
75
- const lines = new FunnelAnalyzer().analyze(events, options.funnel).steps.map((step) => {
76
- return `- ${step.step.eventName}: ${step.count} (conversion ${step.conversionRate}, drop-off ${step.dropOffRate})`;
77
- });
78
- return {
79
- chunkId: `posthog:funnel:${options.funnel.name}`,
80
- text: [`PostHog funnel analysis — ${options.funnel.name}:`, ...lines].join("\n"),
81
- meta: {
82
- source: "posthog",
83
- kind: "funnel",
84
- funnelName: options.funnel.name,
85
- dateFrom: range.from.toISOString(),
86
- dateTo: range.to.toISOString()
87
- }
88
- };
70
+ if (!options.funnel)
71
+ return null;
72
+ if (!options.reader.getEvents)
73
+ return null;
74
+ const events = [];
75
+ const eventNames = options.funnel.steps.map((step) => step.eventName);
76
+ for (const eventName of eventNames) {
77
+ const response = await options.reader.getEvents({
78
+ event: eventName,
79
+ dateRange: {
80
+ from: range.from,
81
+ to: range.to
82
+ },
83
+ limit: options.limit ?? 500
84
+ });
85
+ response.results.forEach((event) => {
86
+ events.push({
87
+ name: event.event,
88
+ userId: event.distinctId,
89
+ tenantId: typeof event.properties?.tenantId === "string" ? event.properties.tenantId : undefined,
90
+ timestamp: event.timestamp,
91
+ properties: event.properties
92
+ });
93
+ });
94
+ }
95
+ if (events.length === 0)
96
+ return null;
97
+ const analyzer = new FunnelAnalyzer;
98
+ const analysis = analyzer.analyze(events, options.funnel);
99
+ const lines = analysis.steps.map((step) => {
100
+ return `- ${step.step.eventName}: ${step.count} (conversion ${step.conversionRate}, drop-off ${step.dropOffRate})`;
101
+ });
102
+ return {
103
+ chunkId: `posthog:funnel:${options.funnel.name}`,
104
+ text: [`PostHog funnel analysis \u2014 ${options.funnel.name}:`, ...lines].join(`
105
+ `),
106
+ meta: {
107
+ source: "posthog",
108
+ kind: "funnel",
109
+ funnelName: options.funnel.name,
110
+ dateFrom: range.from.toISOString(),
111
+ dateTo: range.to.toISOString()
112
+ }
113
+ };
89
114
  }
90
115
  async function loadFeatureFlagEvidence(options) {
91
- if (!options.includeFeatureFlags) return null;
92
- if (!options.reader.getFeatureFlags) return null;
93
- const response = await options.reader.getFeatureFlags({ limit: 10 });
94
- if (!response.results.length) return null;
95
- return {
96
- chunkId: "posthog:feature_flags",
97
- text: ["PostHog feature flags:", ...response.results.map((flag) => {
98
- return `- ${flag.key ?? "unknown"}: ${flag.active ? "active" : "inactive"}`;
99
- })].join("\n"),
100
- meta: {
101
- source: "posthog",
102
- kind: "feature_flags"
103
- }
104
- };
116
+ if (!options.includeFeatureFlags)
117
+ return null;
118
+ if (!options.reader.getFeatureFlags)
119
+ return null;
120
+ const response = await options.reader.getFeatureFlags({ limit: 10 });
121
+ if (!response.results.length)
122
+ return null;
123
+ const lines = response.results.map((flag) => {
124
+ const key = flag.key ?? "unknown";
125
+ const active = flag.active ? "active" : "inactive";
126
+ return `- ${key}: ${active}`;
127
+ });
128
+ return {
129
+ chunkId: "posthog:feature_flags",
130
+ text: ["PostHog feature flags:", ...lines].join(`
131
+ `),
132
+ meta: {
133
+ source: "posthog",
134
+ kind: "feature_flags"
135
+ }
136
+ };
105
137
  }
106
138
  function resolveRange(dateRange) {
107
- const now = /* @__PURE__ */ new Date();
108
- return {
109
- from: dateRange?.from instanceof Date ? dateRange.from : dateRange?.from ? new Date(dateRange.from) : /* @__PURE__ */ new Date(now.getTime() - 720 * 60 * 60 * 1e3),
110
- to: dateRange?.to instanceof Date ? dateRange.to : dateRange?.to ? new Date(dateRange.to) : now
111
- };
139
+ const now = new Date;
140
+ const from = dateRange?.from instanceof Date ? dateRange.from : dateRange?.from ? new Date(dateRange.from) : new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
141
+ const to = dateRange?.to instanceof Date ? dateRange.to : dateRange?.to ? new Date(dateRange.to) : now;
142
+ return { from, to };
112
143
  }
113
144
  function buildEventFilter(eventNames) {
114
- if (!eventNames || eventNames.length === 0) return {};
115
- if (eventNames.length === 1) return {
116
- clause: "event = {event0}",
117
- values: { event0: eventNames[0] }
118
- };
119
- const values = {};
120
- return {
121
- clause: `(${eventNames.map((eventName, index) => {
122
- values[`event${index}`] = eventName;
123
- return `event = {event${index}}`;
124
- }).join(" or ")})`,
125
- values
126
- };
145
+ if (!eventNames || eventNames.length === 0)
146
+ return {};
147
+ if (eventNames.length === 1) {
148
+ return {
149
+ clause: "event = {event0}",
150
+ values: { event0: eventNames[0] }
151
+ };
152
+ }
153
+ const values = {};
154
+ const clauses = eventNames.map((eventName, index) => {
155
+ values[`event${index}`] = eventName;
156
+ return `event = {event${index}}`;
157
+ });
158
+ return {
159
+ clause: `(${clauses.join(" or ")})`,
160
+ values
161
+ };
127
162
  }
128
163
  function mapRows(result) {
129
- if (!Array.isArray(result.results) || !Array.isArray(result.columns)) return [];
130
- const columns = result.columns;
131
- return result.results.flatMap((row) => {
132
- if (!Array.isArray(row)) return [];
133
- const record = {};
134
- columns.forEach((column, index) => {
135
- record[column] = row[index];
136
- });
137
- return [record];
138
- });
164
+ if (!Array.isArray(result.results) || !Array.isArray(result.columns)) {
165
+ return [];
166
+ }
167
+ const columns = result.columns;
168
+ return result.results.flatMap((row) => {
169
+ if (!Array.isArray(row))
170
+ return [];
171
+ const record = {};
172
+ columns.forEach((column, index) => {
173
+ record[column] = row[index];
174
+ });
175
+ return [record];
176
+ });
139
177
  }
140
178
  function asString(value) {
141
- if (typeof value === "string" && value.trim()) return value;
142
- if (typeof value === "number") return String(value);
143
- return null;
179
+ if (typeof value === "string" && value.trim())
180
+ return value;
181
+ if (typeof value === "number")
182
+ return String(value);
183
+ return null;
144
184
  }
145
185
  function asNumber(value) {
146
- if (typeof value === "number" && Number.isFinite(value)) return value;
147
- if (typeof value === "string" && value.trim()) {
148
- const parsed = Number(value);
149
- if (Number.isFinite(parsed)) return parsed;
150
- }
151
- return 0;
186
+ if (typeof value === "number" && Number.isFinite(value))
187
+ return value;
188
+ if (typeof value === "string" && value.trim()) {
189
+ const parsed = Number(value);
190
+ if (Number.isFinite(parsed))
191
+ return parsed;
192
+ }
193
+ return 0;
152
194
  }
153
195
  function resolvePosthogEvidenceOptionsFromEnv(options = {}) {
154
- const projectId = process.env.POSTHOG_PROJECT_ID;
155
- const personalApiKey = process.env.POSTHOG_PERSONAL_API_KEY;
156
- if (!projectId || !personalApiKey) return null;
157
- const lookbackDays = resolveNumberEnv("POSTHOG_EVIDENCE_LOOKBACK_DAYS", options.defaultLookbackDays ?? 30);
158
- const limit = resolveNumberEnv("POSTHOG_EVIDENCE_LIMIT", options.defaultLimit ?? 10);
159
- const now = /* @__PURE__ */ new Date();
160
- const from = /* @__PURE__ */ new Date(now.getTime() - lookbackDays * 24 * 60 * 60 * 1e3);
161
- const eventNames = resolveCsvEnv("POSTHOG_EVIDENCE_EVENTS");
162
- const funnelSteps = resolveCsvEnv("POSTHOG_EVIDENCE_FUNNEL_STEPS");
163
- const funnel = funnelSteps && funnelSteps.length ? {
164
- name: "posthog-evidence-funnel",
165
- steps: funnelSteps.map((eventName, index) => ({
166
- id: `step_${index + 1}`,
167
- eventName
168
- }))
169
- } : void 0;
170
- return {
171
- reader: new PosthogAnalyticsProvider({
172
- host: process.env.POSTHOG_HOST,
173
- projectId,
174
- personalApiKey
175
- }),
176
- dateRange: {
177
- from,
178
- to: now
179
- },
180
- eventNames,
181
- limit,
182
- funnel,
183
- includeFeatureFlags: resolveBooleanEnv("POSTHOG_EVIDENCE_FEATURE_FLAGS", true)
184
- };
196
+ const projectId = process.env.POSTHOG_PROJECT_ID;
197
+ const personalApiKey = process.env.POSTHOG_PERSONAL_API_KEY;
198
+ if (!projectId || !personalApiKey)
199
+ return null;
200
+ const lookbackDays = resolveNumberEnv("POSTHOG_EVIDENCE_LOOKBACK_DAYS", options.defaultLookbackDays ?? 30);
201
+ const limit = resolveNumberEnv("POSTHOG_EVIDENCE_LIMIT", options.defaultLimit ?? 10);
202
+ const now = new Date;
203
+ const from = new Date(now.getTime() - lookbackDays * 24 * 60 * 60 * 1000);
204
+ const eventNames = resolveCsvEnv("POSTHOG_EVIDENCE_EVENTS");
205
+ const funnelSteps = resolveCsvEnv("POSTHOG_EVIDENCE_FUNNEL_STEPS");
206
+ const funnel = funnelSteps && funnelSteps.length ? {
207
+ name: "posthog-evidence-funnel",
208
+ steps: funnelSteps.map((eventName, index) => ({
209
+ id: `step_${index + 1}`,
210
+ eventName
211
+ }))
212
+ } : undefined;
213
+ const reader = new PosthogAnalyticsProvider({
214
+ host: process.env.POSTHOG_HOST,
215
+ projectId,
216
+ personalApiKey
217
+ });
218
+ return {
219
+ reader,
220
+ dateRange: { from, to: now },
221
+ eventNames,
222
+ limit,
223
+ funnel,
224
+ includeFeatureFlags: resolveBooleanEnv("POSTHOG_EVIDENCE_FEATURE_FLAGS", true)
225
+ };
185
226
  }
186
227
  function resolveCsvEnv(key) {
187
- const value = process.env[key];
188
- if (!value) return void 0;
189
- return value.split(",").map((item) => item.trim()).filter(Boolean);
228
+ const value = process.env[key];
229
+ if (!value)
230
+ return;
231
+ return value.split(",").map((item) => item.trim()).filter(Boolean);
190
232
  }
191
233
  function resolveNumberEnv(key, fallback) {
192
- const value = process.env[key];
193
- if (!value) return fallback;
194
- const parsed = Number(value);
195
- return Number.isFinite(parsed) ? parsed : fallback;
234
+ const value = process.env[key];
235
+ if (!value)
236
+ return fallback;
237
+ const parsed = Number(value);
238
+ return Number.isFinite(parsed) ? parsed : fallback;
196
239
  }
197
240
  function resolveBooleanEnv(key, fallback) {
198
- const value = process.env[key];
199
- if (value === void 0) return fallback;
200
- return value.toLowerCase() === "true";
241
+ const value = process.env[key];
242
+ if (value === undefined)
243
+ return fallback;
244
+ return value.toLowerCase() === "true";
201
245
  }
202
-
203
- //#endregion
204
- export { loadPosthogEvidenceChunks, resolvePosthogEvidenceOptionsFromEnv };
205
- //# sourceMappingURL=posthog-signals.js.map
246
+ export {
247
+ resolvePosthogEvidenceOptionsFromEnv,
248
+ loadPosthogEvidenceChunks
249
+ };
package/dist/script.d.ts CHANGED
@@ -1 +1,2 @@
1
- export { };
1
+ export {};
2
+ //# sourceMappingURL=script.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script.d.ts","sourceRoot":"","sources":["../src/script.ts"],"names":[],"mappings":""}