@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,75 +1,314 @@
1
- import { loadPosthogEvidenceChunks } from "./posthog-signals.js";
2
- import fs from "node:fs";
3
- import path from "node:path";
4
- import { fileURLToPath } from "node:url";
1
+ // @bun
2
+ // src/posthog-signals.ts
3
+ import { FunnelAnalyzer } from "@contractspec/lib.analytics/funnel";
4
+ import { PosthogAnalyticsProvider } from "@contractspec/integration.providers-impls/impls/posthog";
5
+ async function loadPosthogEvidenceChunks(options) {
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;
21
+ }
22
+ async function loadEventSummary(options, range) {
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
+ };
68
+ }
69
+ async function loadFunnelEvidence(options, range) {
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
+ };
114
+ }
115
+ async function loadFeatureFlagEvidence(options) {
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
+ };
137
+ }
138
+ function resolveRange(dateRange) {
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 };
143
+ }
144
+ function buildEventFilter(eventNames) {
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
+ };
162
+ }
163
+ function mapRows(result) {
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
+ });
177
+ }
178
+ function asString(value) {
179
+ if (typeof value === "string" && value.trim())
180
+ return value;
181
+ if (typeof value === "number")
182
+ return String(value);
183
+ return null;
184
+ }
185
+ function asNumber(value) {
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;
194
+ }
195
+ function resolvePosthogEvidenceOptionsFromEnv(options = {}) {
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
+ };
226
+ }
227
+ function resolveCsvEnv(key) {
228
+ const value = process.env[key];
229
+ if (!value)
230
+ return;
231
+ return value.split(",").map((item) => item.trim()).filter(Boolean);
232
+ }
233
+ function resolveNumberEnv(key, 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;
239
+ }
240
+ function resolveBooleanEnv(key, fallback) {
241
+ const value = process.env[key];
242
+ if (value === undefined)
243
+ return fallback;
244
+ return value.toLowerCase() === "true";
245
+ }
5
246
 
6
- //#region src/load-evidence.ts
7
- const MODULE_DIR = path.dirname(fileURLToPath(import.meta.url));
8
- const DEFAULT_EVIDENCE_ROOT = path.join(MODULE_DIR, "../evidence");
9
- const DEFAULT_TRANSCRIPT_DIRS = [
10
- "interviews",
11
- "tickets",
12
- "public"
13
- ];
14
- const DEFAULT_CHUNK_SIZE = 800;
15
- /**
16
- * Remove YAML front matter from a file. Synthetic interview and ticket
17
- * files include a YAML header delimited by triple dashes.
18
- */
247
+ // src/load-evidence.ts
248
+ import fs from "fs";
249
+ import path from "path";
250
+ import { fileURLToPath } from "url";
251
+ var MODULE_DIR = path.dirname(fileURLToPath(import.meta.url));
252
+ var DEFAULT_EVIDENCE_ROOT = path.join(MODULE_DIR, "../evidence");
253
+ var DEFAULT_TRANSCRIPT_DIRS = ["interviews", "tickets", "public"];
254
+ var DEFAULT_CHUNK_SIZE = 800;
19
255
  function stripYamlFrontMatter(contents) {
20
- const start = contents.indexOf("---");
21
- if (start === -1) return contents;
22
- const end = contents.indexOf("---", start + 3);
23
- if (end === -1) return contents;
24
- return contents.slice(end + 3).trimStart();
25
- }
26
- /**
27
- * Split a transcript into fixed-size chunks.
28
- */
256
+ const start = contents.indexOf("---");
257
+ if (start === -1)
258
+ return contents;
259
+ const end = contents.indexOf("---", start + 3);
260
+ if (end === -1)
261
+ return contents;
262
+ return contents.slice(end + 3).trimStart();
263
+ }
29
264
  function chunkTranscript(fileId, text, chunkSize) {
30
- const chunks = [];
31
- const clean = text.trim();
32
- for (let offset = 0, idx = 0; offset < clean.length; idx += 1) {
33
- const slice = clean.slice(offset, offset + chunkSize);
34
- chunks.push({
35
- chunkId: `${fileId}#c_${String(idx).padStart(2, "0")}`,
36
- text: slice,
37
- meta: { source: fileId }
38
- });
39
- offset += chunkSize;
40
- }
41
- return chunks;
42
- }
43
- /**
44
- * Load all transcript files under the given directories and return
45
- * EvidenceChunk objects ready for prompt formatting.
46
- */
265
+ const chunks = [];
266
+ const clean = text.trim();
267
+ for (let offset = 0, idx = 0;offset < clean.length; idx += 1) {
268
+ const slice = clean.slice(offset, offset + chunkSize);
269
+ chunks.push({
270
+ chunkId: `${fileId}#c_${String(idx).padStart(2, "0")}`,
271
+ text: slice,
272
+ meta: { source: fileId }
273
+ });
274
+ offset += chunkSize;
275
+ }
276
+ return chunks;
277
+ }
47
278
  function loadEvidenceChunks(options = {}) {
48
- const evidenceRoot = options.evidenceRoot ?? DEFAULT_EVIDENCE_ROOT;
49
- const transcriptDirs = options.transcriptDirs ?? DEFAULT_TRANSCRIPT_DIRS;
50
- const chunkSize = options.chunkSize ?? DEFAULT_CHUNK_SIZE;
51
- const chunks = [];
52
- for (const dir of transcriptDirs) {
53
- const fullDir = path.join(evidenceRoot, dir);
54
- if (!fs.existsSync(fullDir)) continue;
55
- for (const fileName of fs.readdirSync(fullDir)) {
56
- if (path.extname(fileName).toLowerCase() !== ".md") continue;
57
- const filePath = path.join(fullDir, fileName);
58
- const withoutFrontMatter = stripYamlFrontMatter(fs.readFileSync(filePath, "utf8"));
59
- const baseId = path.parse(fileName).name;
60
- const fileChunks = chunkTranscript(baseId, withoutFrontMatter, chunkSize);
61
- chunks.push(...fileChunks);
62
- }
63
- }
64
- return chunks;
279
+ const evidenceRoot = options.evidenceRoot ?? DEFAULT_EVIDENCE_ROOT;
280
+ const transcriptDirs = options.transcriptDirs ?? DEFAULT_TRANSCRIPT_DIRS;
281
+ const chunkSize = options.chunkSize ?? DEFAULT_CHUNK_SIZE;
282
+ const chunks = [];
283
+ for (const dir of transcriptDirs) {
284
+ const fullDir = path.join(evidenceRoot, dir);
285
+ if (!fs.existsSync(fullDir))
286
+ continue;
287
+ for (const fileName of fs.readdirSync(fullDir)) {
288
+ const ext = path.extname(fileName).toLowerCase();
289
+ if (ext !== ".md")
290
+ continue;
291
+ const filePath = path.join(fullDir, fileName);
292
+ const raw = fs.readFileSync(filePath, "utf8");
293
+ const withoutFrontMatter = stripYamlFrontMatter(raw);
294
+ const baseId = path.parse(fileName).name;
295
+ const fileChunks = chunkTranscript(baseId, withoutFrontMatter, chunkSize);
296
+ chunks.push(...fileChunks);
297
+ }
298
+ }
299
+ return chunks;
65
300
  }
66
301
  async function loadEvidenceChunksWithSignals(options = {}) {
67
- const baseChunks = loadEvidenceChunks(options);
68
- if (!options.posthog) return baseChunks;
69
- const posthogChunks = await loadPosthogEvidenceChunks(options.posthog);
70
- return [...baseChunks, ...posthogChunks];
302
+ const baseChunks = loadEvidenceChunks(options);
303
+ if (!options.posthog)
304
+ return baseChunks;
305
+ const posthogChunks = await loadPosthogEvidenceChunks(options.posthog);
306
+ return [...baseChunks, ...posthogChunks];
71
307
  }
72
-
73
- //#endregion
74
- export { DEFAULT_CHUNK_SIZE, DEFAULT_EVIDENCE_ROOT, DEFAULT_TRANSCRIPT_DIRS, loadEvidenceChunks, loadEvidenceChunksWithSignals };
75
- //# sourceMappingURL=load-evidence.js.map
308
+ export {
309
+ loadEvidenceChunksWithSignals,
310
+ loadEvidenceChunks,
311
+ DEFAULT_TRANSCRIPT_DIRS,
312
+ DEFAULT_EVIDENCE_ROOT,
313
+ DEFAULT_CHUNK_SIZE
314
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=load-evidence.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"load-evidence.test.d.ts","sourceRoot":"","sources":["../src/load-evidence.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,28 @@
1
+ // src/example.ts
2
+ import { defineExample } from "@contractspec/lib.contracts";
3
+ var example = defineExample({
4
+ meta: {
5
+ key: "product-intent",
6
+ version: "1.0.0",
7
+ title: "Product Intent Discovery",
8
+ description: "Evidence ingestion and product-intent workflow for PM discovery.",
9
+ kind: "script",
10
+ visibility: "public",
11
+ stability: "experimental",
12
+ owners: ["@platform.core"],
13
+ tags: ["product-intent", "discovery", "pm", "evidence", "llm"]
14
+ },
15
+ entrypoints: {
16
+ packageName: "@contractspec/example.product-intent"
17
+ },
18
+ surfaces: {
19
+ templates: false,
20
+ sandbox: { enabled: false, modes: [] },
21
+ studio: { enabled: false, installable: false },
22
+ mcp: { enabled: false }
23
+ }
24
+ });
25
+ var example_default = example;
26
+ export {
27
+ example_default as default
28
+ };