@clue-ai/cli 0.0.4 → 0.0.5
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/README.md +43 -0
- package/bin/clue-cli.mjs +844 -68
- package/package.json +1 -1
- package/src/contracts.mjs +5 -0
- package/src/init-tool.mjs +154 -55
- package/src/lifecycle-guard.mjs +141 -0
- package/src/lifecycle-init.mjs +210 -167
- package/src/path-policy.mjs +2 -0
- package/src/public-schema.cjs +26 -0
- package/src/semantic-ci.mjs +722 -32
- package/src/setup-check.mjs +435 -0
- package/src/setup-detect.mjs +198 -0
- package/src/setup-prepare.mjs +170 -0
- package/src/setup-tool.mjs +231 -166
package/src/lifecycle-init.mjs
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { readFile, writeFile } from "node:fs/promises";
|
|
2
2
|
import { isAbsolute, relative, resolve } from "node:path";
|
|
3
|
+
import { findLifecycleGuardViolations } from "./lifecycle-guard.mjs";
|
|
3
4
|
import { listAllowedSourceFiles } from "./path-policy.mjs";
|
|
4
5
|
|
|
5
6
|
const API_NAMES = new Set([
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
"ClueInit",
|
|
8
|
+
"ClueIdentify",
|
|
9
|
+
"ClueSetAccount",
|
|
10
|
+
"ClueLogout",
|
|
10
11
|
]);
|
|
11
12
|
|
|
12
13
|
const SOURCE_EXTENSIONS = [".py", ".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"];
|
|
@@ -15,192 +16,234 @@ const MAX_FILE_CHARS = 12_000;
|
|
|
15
16
|
const MAX_TOTAL_CHARS = 360_000;
|
|
16
17
|
|
|
17
18
|
const nonEmpty = (value, field) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
20
|
+
throw new Error(`${field} is required`);
|
|
21
|
+
}
|
|
22
|
+
return value.trim();
|
|
22
23
|
};
|
|
23
24
|
|
|
24
25
|
const safeRelativePath = (repoRoot, filePath) => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
throw new Error(`edit path escapes repo root: ${filePath}`);
|
|
33
|
-
}
|
|
34
|
-
return { absolutePath, relativePath };
|
|
26
|
+
const root = resolve(repoRoot);
|
|
27
|
+
const absolutePath = resolve(root, filePath);
|
|
28
|
+
const relativePath = relative(root, absolutePath);
|
|
29
|
+
if (relativePath.startsWith("..") || isAbsolute(relativePath)) {
|
|
30
|
+
throw new Error(`edit path escapes repo root: ${filePath}`);
|
|
31
|
+
}
|
|
32
|
+
return { absolutePath, relativePath };
|
|
35
33
|
};
|
|
36
34
|
|
|
37
35
|
const assertApiName = (apiName) => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
36
|
+
if (!API_NAMES.has(apiName)) {
|
|
37
|
+
throw new Error(`unsupported lifecycle API: ${apiName}`);
|
|
38
|
+
}
|
|
39
|
+
return apiName;
|
|
42
40
|
};
|
|
43
41
|
|
|
44
42
|
const assertNoForbiddenInstrumentation = (replacement) => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
43
|
+
if (replacement.includes("ClueTrack")) {
|
|
44
|
+
throw new Error("init tool must not add broad ClueTrack instrumentation");
|
|
45
|
+
}
|
|
46
|
+
if (/data-clue-(id|key)/i.test(replacement)) {
|
|
47
|
+
throw new Error("init tool must not add data-clue-id or data-clue-key");
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const assertLifecycleCallsAreGuarded = (replacement) => {
|
|
52
|
+
const violations = findLifecycleGuardViolations(replacement);
|
|
53
|
+
if (violations.length > 0) {
|
|
54
|
+
throw new Error(
|
|
55
|
+
`Clue lifecycle calls must be failure-isolated with try/catch, try/except, or an explicit safe Clue helper: ${JSON.stringify(violations)}`,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
51
58
|
};
|
|
52
59
|
|
|
53
60
|
const normalizeLifecycleInsertion = (input) => ({
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
61
|
+
api_name: assertApiName(nonEmpty(input.api_name, "api_name")),
|
|
62
|
+
file_path: nonEmpty(input.file_path, "file_path"),
|
|
63
|
+
confidence: Math.max(0, Math.min(1, Number(input.confidence ?? 0))),
|
|
64
|
+
reason: nonEmpty(input.reason, "reason"),
|
|
58
65
|
});
|
|
59
66
|
|
|
60
67
|
const normalizePlan = (input) => {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
68
|
+
if (!input || typeof input !== "object") {
|
|
69
|
+
throw new Error("AI lifecycle plan must be an object");
|
|
70
|
+
}
|
|
71
|
+
if (!Array.isArray(input.edits)) {
|
|
72
|
+
throw new Error("AI lifecycle plan must include edits");
|
|
73
|
+
}
|
|
74
|
+
const edits = input.edits.map((edit) => ({
|
|
75
|
+
file_path: nonEmpty(edit.file_path, "edit.file_path"),
|
|
76
|
+
find: nonEmpty(edit.find, "edit.find"),
|
|
77
|
+
replace: nonEmpty(edit.replace, "edit.replace"),
|
|
78
|
+
}));
|
|
79
|
+
const lifecycleInsertions = Array.isArray(input.lifecycle_insertions)
|
|
80
|
+
? input.lifecycle_insertions.map(normalizeLifecycleInsertion)
|
|
81
|
+
: [];
|
|
82
|
+
return {
|
|
83
|
+
edits,
|
|
84
|
+
lifecycleInsertions,
|
|
85
|
+
warnings: Array.isArray(input.warnings)
|
|
86
|
+
? input.warnings
|
|
87
|
+
.filter((warning) => typeof warning === "string" && warning.trim())
|
|
88
|
+
.map((warning) => warning.trim())
|
|
89
|
+
: [],
|
|
90
|
+
};
|
|
84
91
|
};
|
|
85
92
|
|
|
86
93
|
export const applyLifecyclePlan = async ({ repoRoot, plan: rawPlan }) => {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
94
|
+
const plan = normalizePlan(rawPlan);
|
|
95
|
+
for (const edit of plan.edits) {
|
|
96
|
+
const { absolutePath } = safeRelativePath(repoRoot, edit.file_path);
|
|
97
|
+
assertNoForbiddenInstrumentation(edit.replace);
|
|
98
|
+
const current = await readFile(absolutePath, "utf8");
|
|
99
|
+
const occurrences = current.split(edit.find).length - 1;
|
|
100
|
+
if (occurrences !== 1) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`edit.find must match exactly once in ${edit.file_path}; matched ${occurrences}`,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
assertLifecycleCallsAreGuarded(edit.replace);
|
|
106
|
+
await writeFile(
|
|
107
|
+
absolutePath,
|
|
108
|
+
current.replace(edit.find, edit.replace),
|
|
109
|
+
"utf8",
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
lifecycleInsertions: plan.lifecycleInsertions,
|
|
114
|
+
warnings: plan.warnings,
|
|
115
|
+
};
|
|
104
116
|
};
|
|
105
117
|
|
|
106
118
|
const readContextFiles = async ({ repoRoot, request }) => {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
119
|
+
const files = await listAllowedSourceFiles({
|
|
120
|
+
repoRoot,
|
|
121
|
+
allowedSourcePaths: request.allowed_source_paths,
|
|
122
|
+
excludedSourcePaths: request.excluded_source_paths,
|
|
123
|
+
extensions: SOURCE_EXTENSIONS,
|
|
124
|
+
});
|
|
125
|
+
const context = [];
|
|
126
|
+
let totalChars = 0;
|
|
127
|
+
for (const absolutePath of files.slice(0, MAX_CONTEXT_FILES)) {
|
|
128
|
+
const text = await readFile(absolutePath, "utf8");
|
|
129
|
+
const snippet = text.slice(0, MAX_FILE_CHARS);
|
|
130
|
+
totalChars += snippet.length;
|
|
131
|
+
if (totalChars > MAX_TOTAL_CHARS) {
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
context.push({
|
|
135
|
+
file_path: relative(resolve(repoRoot), absolutePath),
|
|
136
|
+
source: snippet,
|
|
137
|
+
truncated: text.length > snippet.length,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return context;
|
|
129
141
|
};
|
|
130
142
|
|
|
131
|
-
const buildLifecyclePrompt = ({ request, files }) =>
|
|
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
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
143
|
+
const buildLifecyclePrompt = ({ request, files }) =>
|
|
144
|
+
JSON.stringify({
|
|
145
|
+
task: "Add Clue SDK lifecycle API calls to this repository using exact text replacements.",
|
|
146
|
+
rules: [
|
|
147
|
+
"Return JSON only.",
|
|
148
|
+
"Use only exact replacements. Each find string must be copied exactly from source.",
|
|
149
|
+
"Add ClueInit, ClueIdentify, ClueSetAccount, and ClueLogout where repository code has clear lifecycle points.",
|
|
150
|
+
"Every Clue lifecycle call must be failure-isolated. If Clue fails, the host service must continue without throwing, rejecting, or blocking login/API behavior.",
|
|
151
|
+
"Use a small safe Clue helper, local try/catch/try/except wrapper, or direct .catch handler around Clue lifecycle calls.",
|
|
152
|
+
"Never await a Clue lifecycle call in a way that can block login, logout, account selection, request handling, page rendering, or API responses.",
|
|
153
|
+
"Find all clear login success paths and add ClueIdentify to every one of them. Do not stop after the first login flow.",
|
|
154
|
+
"Find all clear account, workspace, organization, or tenant resolution paths and add ClueSetAccount to every one of them.",
|
|
155
|
+
"Find all clear logout or session reset paths and add ClueLogout to every one of them.",
|
|
156
|
+
"Inspect backend lifecycle points as carefully as frontend lifecycle points. Backend login/session/account code is especially important.",
|
|
157
|
+
"For FastAPI backends, add the clue-fastapi-sdk dependency when missing, import clue_init_fastapi plus ClueIdentify/ClueSetAccount/ClueLogout where needed, and initialize the SDK at FastAPI app creation.",
|
|
158
|
+
"For Django backends, add the clue-django-sdk dependency when missing, import the Django SDK lifecycle helpers where needed, and initialize the SDK in the Django integration point.",
|
|
159
|
+
"For other backend frameworks, use the matching Clue backend SDK if one exists; if no backend SDK exists, report a blocker instead of silently frontend-only setup.",
|
|
160
|
+
"Do not add broad ClueTrack instrumentation.",
|
|
161
|
+
"Do not add data-clue-id, data-clue-key, or similar DOM tags.",
|
|
162
|
+
"Do not create route semantics files or layer files.",
|
|
163
|
+
"Do not copy project keys, API keys, service secrets, or environment-specific values into code.",
|
|
164
|
+
"Do not send raw email, raw person names, tokens, workspace names, organization names, or tenant names as lifecycle traits unless the repository already has an explicit Clue privacy policy allowing them.",
|
|
165
|
+
"Prefer stable ids and non-PII booleans/counts for ClueIdentify and ClueSetAccount traits.",
|
|
166
|
+
"Use environment variable names for Clue configuration values.",
|
|
167
|
+
"For Python/FastAPI code, read CLUE_PROJECT_KEY, CLUE_ENVIRONMENT, CLUE_API_KEY, and CLUE_INGEST_ENDPOINT from environment variables.",
|
|
168
|
+
"For browser code, read NEXT_PUBLIC_CLUE_PROJECT_KEY, NEXT_PUBLIC_CLUE_ENVIRONMENT, and NEXT_PUBLIC_CLUE_INGEST_ENDPOINT from environment variables.",
|
|
169
|
+
"Prefer minimal edits that engineers can review in one PR.",
|
|
170
|
+
"If a lifecycle point is unclear, skip that edit and include a warning.",
|
|
171
|
+
],
|
|
172
|
+
repository_context: {
|
|
173
|
+
target_tool: request.target_tool,
|
|
174
|
+
framework: request.framework,
|
|
175
|
+
project_key_env: "CLUE_PROJECT_KEY",
|
|
176
|
+
browser_project_key_env: "NEXT_PUBLIC_CLUE_PROJECT_KEY",
|
|
177
|
+
environment_env: "CLUE_ENVIRONMENT",
|
|
178
|
+
browser_environment_env: "NEXT_PUBLIC_CLUE_ENVIRONMENT",
|
|
179
|
+
clue_api_base_url_env: "CLUE_API_BASE_URL",
|
|
180
|
+
clue_ingest_endpoint_env: "CLUE_INGEST_ENDPOINT",
|
|
181
|
+
browser_ingest_endpoint_env: "NEXT_PUBLIC_CLUE_INGEST_ENDPOINT",
|
|
182
|
+
service_key: request.service_key,
|
|
183
|
+
},
|
|
184
|
+
output_shape: {
|
|
185
|
+
edits: [
|
|
186
|
+
{
|
|
187
|
+
file_path: "app/main.py",
|
|
188
|
+
find: "exact original text",
|
|
189
|
+
replace: "exact replacement text",
|
|
190
|
+
},
|
|
191
|
+
],
|
|
192
|
+
lifecycle_insertions: [
|
|
193
|
+
{
|
|
194
|
+
api_name: "ClueInit",
|
|
195
|
+
file_path: "app/main.py",
|
|
196
|
+
confidence: 0.8,
|
|
197
|
+
reason: "SDK initialized where FastAPI app is created.",
|
|
198
|
+
},
|
|
199
|
+
],
|
|
200
|
+
warnings: ["short engineer review note"],
|
|
201
|
+
},
|
|
202
|
+
files,
|
|
203
|
+
});
|
|
170
204
|
|
|
171
205
|
export const planLifecycleInsertions = async ({ repoRoot, request, env }) => {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
+
const apiKey = env.AI_PROVIDER_API_KEY;
|
|
207
|
+
if (!apiKey) {
|
|
208
|
+
throw new Error(
|
|
209
|
+
"AI_PROVIDER_API_KEY is required for lifecycle API insertion",
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
const files = await readContextFiles({ repoRoot, request });
|
|
213
|
+
const baseUrl = String(
|
|
214
|
+
env.AI_PROVIDER_BASE_URL || "https://api.openai.com/v1",
|
|
215
|
+
).replace(/\/+$/, "");
|
|
216
|
+
const model = String(
|
|
217
|
+
env.CLUE_INIT_AI_MODEL || env.CLUE_AI_MODEL || "gpt-5.4-mini",
|
|
218
|
+
);
|
|
219
|
+
const response = await fetch(`${baseUrl}/chat/completions`, {
|
|
220
|
+
method: "POST",
|
|
221
|
+
headers: {
|
|
222
|
+
"content-type": "application/json",
|
|
223
|
+
authorization: `Bearer ${apiKey}`,
|
|
224
|
+
},
|
|
225
|
+
body: JSON.stringify({
|
|
226
|
+
model,
|
|
227
|
+
messages: [
|
|
228
|
+
{
|
|
229
|
+
role: "system",
|
|
230
|
+
content:
|
|
231
|
+
"You are a safe code-edit planner for Clue SDK initialization. Return schema-valid JSON only.",
|
|
232
|
+
},
|
|
233
|
+
{ role: "user", content: buildLifecyclePrompt({ request, files }) },
|
|
234
|
+
],
|
|
235
|
+
response_format: { type: "json_object" },
|
|
236
|
+
}),
|
|
237
|
+
});
|
|
238
|
+
if (!response.ok) {
|
|
239
|
+
throw new Error(
|
|
240
|
+
`AI provider failed during lifecycle planning: ${response.status}`,
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
const body = await response.json();
|
|
244
|
+
const content = body?.choices?.[0]?.message?.content;
|
|
245
|
+
if (typeof content !== "string" || content.trim() === "") {
|
|
246
|
+
throw new Error("AI provider returned empty lifecycle plan");
|
|
247
|
+
}
|
|
248
|
+
return JSON.parse(content);
|
|
206
249
|
};
|
package/src/path-policy.mjs
CHANGED
package/src/public-schema.cjs
CHANGED
|
@@ -137,6 +137,15 @@ const semanticSnapshotSelectedSourceValues = [
|
|
|
137
137
|
"deterministic",
|
|
138
138
|
"ai",
|
|
139
139
|
];
|
|
140
|
+
const semanticSnapshotRouteOriginValues = [
|
|
141
|
+
"unchanged_route_reused",
|
|
142
|
+
"changed_route_semantic_reused",
|
|
143
|
+
"changed_route_semantic_regenerated",
|
|
144
|
+
"new_route_ai_generated",
|
|
145
|
+
"deleted_route_removed",
|
|
146
|
+
"changed_route_needs_review",
|
|
147
|
+
"fallback",
|
|
148
|
+
];
|
|
140
149
|
const targetObjectKeySchema = nonEmptyStringSchema.regex(snakeSegmentPattern, {
|
|
141
150
|
message: "must be a lowercase snake_case key",
|
|
142
151
|
});
|
|
@@ -190,6 +199,16 @@ const semanticSnapshotAnalysisSummarySchema = zod_1.z
|
|
|
190
199
|
routes_generated: zod_1.z.number().int().nonnegative(),
|
|
191
200
|
uncertain_routes: zod_1.z.number().int().nonnegative(),
|
|
192
201
|
failed_routes: zod_1.z.number().int().nonnegative(),
|
|
202
|
+
routes_reused: zod_1.z.number().int().nonnegative().default(0),
|
|
203
|
+
routes_ai_generated: zod_1.z.number().int().nonnegative().default(0),
|
|
204
|
+
routes_deleted: zod_1.z.number().int().nonnegative().default(0),
|
|
205
|
+
routes_needs_review: zod_1.z.number().int().nonnegative().default(0),
|
|
206
|
+
changed_routes_semantic_reused: zod_1.z.number().int().nonnegative().default(0),
|
|
207
|
+
changed_routes_semantic_regenerated: zod_1.z
|
|
208
|
+
.number()
|
|
209
|
+
.int()
|
|
210
|
+
.nonnegative()
|
|
211
|
+
.default(0),
|
|
193
212
|
})
|
|
194
213
|
.strict();
|
|
195
214
|
const semanticCandidateSchema = zod_1.z
|
|
@@ -375,8 +394,15 @@ const routeSemanticSnapshotEntrySchema = zod_1.z
|
|
|
375
394
|
.object({
|
|
376
395
|
operation_source_key: nonEmptyStringSchema,
|
|
377
396
|
semantic_snapshot_version: nonEmptyStringSchema.optional(),
|
|
397
|
+
previous_semantic_snapshot_version: nonEmptyStringSchema.optional(),
|
|
378
398
|
method: nonEmptyStringSchema.optional(),
|
|
379
399
|
path_template: nonEmptyStringSchema.optional(),
|
|
400
|
+
route_input_hash: semanticSnapshotHashSchema.optional(),
|
|
401
|
+
previous_route_input_hash: semanticSnapshotHashSchema.optional(),
|
|
402
|
+
route_semantic_hash: semanticSnapshotHashSchema.optional(),
|
|
403
|
+
previous_route_semantic_hash: semanticSnapshotHashSchema.optional(),
|
|
404
|
+
semantic_origin: zod_1.z.enum(semanticSnapshotRouteOriginValues).optional(),
|
|
405
|
+
semantic_change_reason: nonEmptyStringSchema.optional(),
|
|
380
406
|
semantics: zod_1.z
|
|
381
407
|
.object({
|
|
382
408
|
route_summary: nonEmptyStringSchema,
|