@contractspec/example.product-intent 1.57.0 → 1.58.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.
- package/.turbo/turbo-build.log +21 -96
- package/.turbo/turbo-prebuild.log +1 -0
- package/CHANGELOG.md +15 -0
- package/dist/example.d.ts +2 -6
- package/dist/example.d.ts.map +1 -1
- package/dist/example.js +27 -37
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +342 -4
- package/dist/load-evidence.d.ts +13 -17
- package/dist/load-evidence.d.ts.map +1 -1
- package/dist/load-evidence.js +307 -68
- package/dist/load-evidence.test.d.ts +2 -0
- package/dist/load-evidence.test.d.ts.map +1 -0
- package/dist/node/example.js +28 -0
- package/dist/node/index.js +342 -0
- package/dist/node/load-evidence.js +313 -0
- package/dist/node/posthog-signals.js +248 -0
- package/dist/node/script.js +512 -0
- package/dist/node/sync-actions.js +491 -0
- package/dist/posthog-signals.d.ts +15 -19
- package/dist/posthog-signals.d.ts.map +1 -1
- package/dist/posthog-signals.js +222 -178
- package/dist/script.d.ts +2 -1
- package/dist/script.d.ts.map +1 -0
- package/dist/script.js +493 -152
- package/dist/sync-actions.d.ts +2 -1
- package/dist/sync-actions.d.ts.map +1 -0
- package/dist/sync-actions.js +466 -128
- package/package.json +57 -27
- package/tsdown.config.js +1 -2
- package/.turbo/turbo-build$colon$bundle.log +0 -99
- package/dist/example.js.map +0 -1
- package/dist/libs/analytics/dist/funnel/analyzer.js +0 -64
- package/dist/libs/analytics/dist/funnel/analyzer.js.map +0 -1
- package/dist/libs/analytics/dist/types.d.ts +0 -22
- package/dist/libs/analytics/dist/types.d.ts.map +0 -1
- package/dist/load-evidence.js.map +0 -1
- package/dist/posthog-signals.js.map +0 -1
- package/dist/script.js.map +0 -1
- package/dist/sync-actions.js.map +0 -1
- package/tsconfig.tsbuildinfo +0 -1
package/dist/script.js
CHANGED
|
@@ -1,172 +1,513 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
+
}
|
|
246
|
+
|
|
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;
|
|
255
|
+
function stripYamlFrontMatter(contents) {
|
|
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
|
+
}
|
|
264
|
+
function chunkTranscript(fileId, text, chunkSize) {
|
|
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
|
+
}
|
|
278
|
+
function loadEvidenceChunks(options = {}) {
|
|
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;
|
|
300
|
+
}
|
|
301
|
+
async function loadEvidenceChunksWithSignals(options = {}) {
|
|
302
|
+
const baseChunks = loadEvidenceChunks(options);
|
|
303
|
+
if (!options.posthog)
|
|
304
|
+
return baseChunks;
|
|
305
|
+
const posthogChunks = await loadPosthogEvidenceChunks(options.posthog);
|
|
306
|
+
return [...baseChunks, ...posthogChunks];
|
|
307
|
+
}
|
|
8
308
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
309
|
+
// src/script.ts
|
|
310
|
+
import fs2 from "fs";
|
|
311
|
+
import path2 from "path";
|
|
312
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
313
|
+
import { createAgentJsonRunner } from "@contractspec/lib.ai-agent";
|
|
314
|
+
import {
|
|
315
|
+
extractEvidence,
|
|
316
|
+
generateTickets,
|
|
317
|
+
groupProblems,
|
|
318
|
+
impactEngine,
|
|
319
|
+
suggestPatch
|
|
320
|
+
} from "@contractspec/lib.product-intent-utils";
|
|
321
|
+
var QUESTION = "Which activation and onboarding friction should we prioritize next?";
|
|
322
|
+
var DEFAULT_PROVIDER = "openai";
|
|
323
|
+
var DEFAULT_MODEL = "gpt-5.2";
|
|
324
|
+
var DEFAULT_TEMPERATURE = 0;
|
|
325
|
+
var DEFAULT_MAX_ATTEMPTS = 2;
|
|
326
|
+
var MODULE_DIR2 = path2.dirname(fileURLToPath2(import.meta.url));
|
|
327
|
+
var REPO_ROOT = path2.resolve(MODULE_DIR2, "../../../..");
|
|
328
|
+
var REPO_SCAN_FILES = [
|
|
329
|
+
"packages/examples/product-intent/src/load-evidence.ts",
|
|
330
|
+
"packages/examples/product-intent/src/script.ts",
|
|
331
|
+
"packages/libs/contracts/src/product-intent/contract-patch-intent.ts",
|
|
332
|
+
"packages/libs/contracts/src/product-intent/spec.ts",
|
|
333
|
+
"packages/libs/product-intent-utils/src/impact-engine.ts"
|
|
23
334
|
];
|
|
24
335
|
function collectRepoFiles(root, files) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
return collected;
|
|
336
|
+
const collected = [];
|
|
337
|
+
for (const relativePath of files) {
|
|
338
|
+
const fullPath = path2.join(root, relativePath);
|
|
339
|
+
if (!fs2.existsSync(fullPath))
|
|
340
|
+
continue;
|
|
341
|
+
const content = fs2.readFileSync(fullPath, "utf8");
|
|
342
|
+
collected.push({ path: relativePath, content });
|
|
343
|
+
}
|
|
344
|
+
return collected;
|
|
36
345
|
}
|
|
37
346
|
function resolveProviderName() {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
347
|
+
const raw = process.env.CONTRACTSPEC_AI_PROVIDER ?? process.env.AI_PROVIDER ?? DEFAULT_PROVIDER;
|
|
348
|
+
const normalized = raw.toLowerCase();
|
|
349
|
+
const allowed = [
|
|
350
|
+
"openai",
|
|
351
|
+
"anthropic",
|
|
352
|
+
"mistral",
|
|
353
|
+
"gemini",
|
|
354
|
+
"ollama"
|
|
355
|
+
];
|
|
356
|
+
if (!allowed.includes(normalized)) {
|
|
357
|
+
throw new Error(`Unsupported AI provider '${raw}'. Allowed: ${allowed.join(", ")}`);
|
|
358
|
+
}
|
|
359
|
+
return normalized;
|
|
49
360
|
}
|
|
50
361
|
function resolveApiKey(provider) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
362
|
+
switch (provider) {
|
|
363
|
+
case "openai":
|
|
364
|
+
return process.env.OPENAI_API_KEY;
|
|
365
|
+
case "anthropic":
|
|
366
|
+
return process.env.ANTHROPIC_API_KEY;
|
|
367
|
+
case "mistral":
|
|
368
|
+
return process.env.MISTRAL_API_KEY;
|
|
369
|
+
case "gemini":
|
|
370
|
+
return process.env.GOOGLE_API_KEY ?? process.env.GEMINI_API_KEY;
|
|
371
|
+
case "ollama":
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
58
374
|
}
|
|
59
375
|
function resolveTemperature() {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
376
|
+
const raw = process.env.CONTRACTSPEC_AI_TEMPERATURE ?? process.env.AI_TEMPERATURE;
|
|
377
|
+
if (!raw)
|
|
378
|
+
return DEFAULT_TEMPERATURE;
|
|
379
|
+
const value = Number.parseFloat(raw);
|
|
380
|
+
return Number.isNaN(value) ? DEFAULT_TEMPERATURE : value;
|
|
64
381
|
}
|
|
65
382
|
function resolveMaxAttempts() {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
383
|
+
const raw = process.env.CONTRACTSPEC_AI_MAX_ATTEMPTS ?? process.env.AI_MAX_ATTEMPTS;
|
|
384
|
+
if (!raw)
|
|
385
|
+
return DEFAULT_MAX_ATTEMPTS;
|
|
386
|
+
const value = Number.parseInt(raw, 10);
|
|
387
|
+
return Number.isNaN(value) ? DEFAULT_MAX_ATTEMPTS : Math.max(1, value);
|
|
70
388
|
}
|
|
71
389
|
function writeArtifact(dir, name, contents) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
390
|
+
const filePath = path2.join(dir, name);
|
|
391
|
+
fs2.writeFileSync(filePath, contents, "utf8");
|
|
392
|
+
return filePath;
|
|
75
393
|
}
|
|
76
394
|
function createPipelineLogger(logDir, runId) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
395
|
+
const tracePath = path2.join(logDir, "trace.jsonl");
|
|
396
|
+
return {
|
|
397
|
+
log(entry) {
|
|
398
|
+
const baseName = `${entry.stage}-attempt-${entry.attempt}-${entry.phase}`;
|
|
399
|
+
const payload = {
|
|
400
|
+
runId,
|
|
401
|
+
stage: entry.stage,
|
|
402
|
+
phase: entry.phase,
|
|
403
|
+
attempt: entry.attempt,
|
|
404
|
+
timestamp: entry.timestamp
|
|
405
|
+
};
|
|
406
|
+
if (entry.prompt) {
|
|
407
|
+
payload.promptPath = path2.relative(REPO_ROOT, writeArtifact(logDir, `${baseName}.prompt.txt`, entry.prompt));
|
|
408
|
+
}
|
|
409
|
+
if (entry.response) {
|
|
410
|
+
payload.responsePath = path2.relative(REPO_ROOT, writeArtifact(logDir, `${baseName}.response.txt`, entry.response));
|
|
411
|
+
}
|
|
412
|
+
if (entry.error) {
|
|
413
|
+
payload.errorPath = path2.relative(REPO_ROOT, writeArtifact(logDir, `${baseName}.error.txt`, entry.error));
|
|
414
|
+
}
|
|
415
|
+
fs2.appendFileSync(tracePath, `${JSON.stringify(payload)}
|
|
416
|
+
`, "utf8");
|
|
417
|
+
}
|
|
418
|
+
};
|
|
92
419
|
}
|
|
93
420
|
async function main() {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
421
|
+
const provider = resolveProviderName();
|
|
422
|
+
const temperature = resolveTemperature();
|
|
423
|
+
const maxAttempts = resolveMaxAttempts();
|
|
424
|
+
const apiKey = resolveApiKey(provider);
|
|
425
|
+
const proxyUrl = process.env.CONTRACTSPEC_AI_PROXY_URL;
|
|
426
|
+
const organizationId = process.env.CONTRACTSPEC_ORG_ID;
|
|
427
|
+
const baseUrl = process.env.OLLAMA_BASE_URL;
|
|
428
|
+
const model = process.env.CONTRACTSPEC_AI_MODEL ?? process.env.AI_MODEL ?? (provider === "mistral" ? DEFAULT_MODEL : undefined);
|
|
429
|
+
if (provider !== "ollama" && !apiKey && !proxyUrl && !organizationId) {
|
|
430
|
+
throw new Error(`Missing API credentials for ${provider}. Set the provider API key or CONTRACTSPEC_AI_PROXY_URL.`);
|
|
431
|
+
}
|
|
432
|
+
const runId = new Date().toISOString().replace(/[:.]/g, "-");
|
|
433
|
+
const logDir = path2.join(MODULE_DIR2, "../logs", `run-${runId}`);
|
|
434
|
+
fs2.mkdirSync(logDir, { recursive: true });
|
|
435
|
+
const logger = createPipelineLogger(logDir, runId);
|
|
436
|
+
const modelRunner = await createAgentJsonRunner({
|
|
437
|
+
provider: {
|
|
438
|
+
provider,
|
|
439
|
+
model,
|
|
440
|
+
apiKey,
|
|
441
|
+
baseUrl,
|
|
442
|
+
proxyUrl,
|
|
443
|
+
organizationId
|
|
444
|
+
},
|
|
445
|
+
temperature,
|
|
446
|
+
system: "You are a product discovery analyst. Respond with strict JSON only and use exact quotes for citations."
|
|
447
|
+
});
|
|
448
|
+
console.log(`AI provider: ${provider}`);
|
|
449
|
+
console.log(`Model: ${model ?? "(provider default)"}`);
|
|
450
|
+
console.log(`Temperature: ${temperature}`);
|
|
451
|
+
console.log(`Max attempts: ${maxAttempts}`);
|
|
452
|
+
console.log(`Trace log: ${path2.relative(REPO_ROOT, logDir)}/trace.jsonl`);
|
|
453
|
+
const posthogEvidence = resolvePosthogEvidenceOptionsFromEnv();
|
|
454
|
+
const evidenceChunks = await loadEvidenceChunksWithSignals({
|
|
455
|
+
posthog: posthogEvidence ?? undefined
|
|
456
|
+
});
|
|
457
|
+
console.log(`Loaded ${evidenceChunks.length} evidence chunks`);
|
|
458
|
+
const findings = await extractEvidence(evidenceChunks, QUESTION, {
|
|
459
|
+
maxFindings: 12,
|
|
460
|
+
modelRunner,
|
|
461
|
+
logger,
|
|
462
|
+
maxAttempts
|
|
463
|
+
});
|
|
464
|
+
console.log(`
|
|
465
|
+
Evidence findings:
|
|
466
|
+
`);
|
|
467
|
+
console.log(JSON.stringify(findings, null, 2));
|
|
468
|
+
const problems = await groupProblems(findings, QUESTION, {
|
|
469
|
+
modelRunner,
|
|
470
|
+
logger,
|
|
471
|
+
maxAttempts
|
|
472
|
+
});
|
|
473
|
+
console.log(`
|
|
474
|
+
Problems:
|
|
475
|
+
`);
|
|
476
|
+
console.log(JSON.stringify(problems, null, 2));
|
|
477
|
+
const tickets = await generateTickets(problems, findings, QUESTION, {
|
|
478
|
+
modelRunner,
|
|
479
|
+
logger,
|
|
480
|
+
maxAttempts
|
|
481
|
+
});
|
|
482
|
+
console.log(`
|
|
483
|
+
Tickets:
|
|
484
|
+
`);
|
|
485
|
+
console.log(JSON.stringify(tickets, null, 2));
|
|
486
|
+
if (!tickets[0]) {
|
|
487
|
+
console.log(`
|
|
488
|
+
No tickets generated.`);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
const patchIntent = await suggestPatch(tickets[0], {
|
|
492
|
+
modelRunner,
|
|
493
|
+
logger,
|
|
494
|
+
maxAttempts
|
|
495
|
+
});
|
|
496
|
+
console.log(`
|
|
497
|
+
Patch intent:
|
|
498
|
+
`);
|
|
499
|
+
console.log(JSON.stringify(patchIntent, null, 2));
|
|
500
|
+
const repoFiles = collectRepoFiles(REPO_ROOT, REPO_SCAN_FILES);
|
|
501
|
+
const impact = impactEngine(patchIntent, {
|
|
502
|
+
repoFiles,
|
|
503
|
+
maxHitsPerChange: 3
|
|
504
|
+
});
|
|
505
|
+
console.log(`
|
|
506
|
+
Impact report (deterministic):
|
|
507
|
+
`);
|
|
508
|
+
console.log(JSON.stringify(impact, null, 2));
|
|
165
509
|
}
|
|
166
510
|
main().catch((error) => {
|
|
167
|
-
|
|
168
|
-
|
|
511
|
+
console.error(error);
|
|
512
|
+
process.exitCode = 1;
|
|
169
513
|
});
|
|
170
|
-
|
|
171
|
-
//#endregion
|
|
172
|
-
//# sourceMappingURL=script.js.map
|
package/dist/sync-actions.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {};
|
|
2
|
+
//# sourceMappingURL=sync-actions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync-actions.d.ts","sourceRoot":"","sources":["../src/sync-actions.ts"],"names":[],"mappings":""}
|