@kaitranntt/ccs 7.63.0-dev.1 → 7.63.0-dev.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaitranntt/ccs",
3
- "version": "7.63.0-dev.1",
3
+ "version": "7.63.0-dev.2",
4
4
  "description": "Claude Code Switch - Instant profile switching between Claude, GLM, Kimi, and more",
5
5
  "keywords": [
6
6
  "cli",
@@ -0,0 +1,204 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+
5
+ const ASSESSMENTS = {
6
+ approved: '✅ APPROVED',
7
+ approved_with_notes: '⚠️ APPROVED WITH NOTES',
8
+ changes_requested: '❌ CHANGES REQUESTED',
9
+ };
10
+
11
+ const SEVERITY_ORDER = ['high', 'medium', 'low'];
12
+ const SEVERITY_HEADERS = {
13
+ high: '### 🔴 High',
14
+ medium: '### 🟡 Medium',
15
+ low: '### 🟢 Low',
16
+ };
17
+
18
+ function cleanText(value) {
19
+ return typeof value === 'string' ? value.trim().replace(/\s+/g, ' ') : '';
20
+ }
21
+
22
+ function escapeMarkdownText(value) {
23
+ return cleanText(value).replace(/\\/g, '\\\\').replace(/([`*_{}\[\]<>])/g, '\\$1');
24
+ }
25
+
26
+ function renderCode(value) {
27
+ const text = cleanText(value);
28
+ const longestFence = Math.max(...[...text.matchAll(/`+/g)].map((match) => match[0].length), 0);
29
+ const fence = '`'.repeat(longestFence + 1);
30
+ return `${fence}${text}${fence}`;
31
+ }
32
+
33
+ function readExecutionMetadata(executionFile) {
34
+ if (!executionFile || !fs.existsSync(executionFile)) {
35
+ return {};
36
+ }
37
+
38
+ try {
39
+ const turns = JSON.parse(fs.readFileSync(executionFile, 'utf8'));
40
+ const init = turns.find((turn) => turn?.type === 'system' && turn?.subtype === 'init');
41
+ const result = [...turns].reverse().find((turn) => turn?.type === 'result');
42
+ return {
43
+ runtimeTools: Array.isArray(init?.tools) ? init.tools : [],
44
+ turnsUsed: typeof result?.num_turns === 'number' ? result.num_turns : null,
45
+ };
46
+ } catch {
47
+ return {};
48
+ }
49
+ }
50
+
51
+ export function normalizeStructuredOutput(raw) {
52
+ if (!raw) {
53
+ return { ok: false, reason: 'missing structured output' };
54
+ }
55
+
56
+ let parsed;
57
+ try {
58
+ parsed = typeof raw === 'string' ? JSON.parse(raw) : raw;
59
+ } catch {
60
+ return { ok: false, reason: 'structured output is not valid JSON' };
61
+ }
62
+
63
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
64
+ return { ok: false, reason: 'structured output must be an object' };
65
+ }
66
+
67
+ const summary = cleanText(parsed.summary);
68
+ const overallAssessment = cleanText(parsed.overallAssessment);
69
+ const overallRationale = cleanText(parsed.overallRationale);
70
+ const notes = Array.isArray(parsed.notes) ? parsed.notes.map(cleanText).filter(Boolean) : [];
71
+ const findings = Array.isArray(parsed.findings) ? parsed.findings : null;
72
+
73
+ if (!summary || !ASSESSMENTS[overallAssessment] || !overallRationale || findings === null) {
74
+ return { ok: false, reason: 'structured output is missing required review fields' };
75
+ }
76
+
77
+ const normalizedFindings = [];
78
+ for (const finding of findings) {
79
+ const severity = cleanText(finding?.severity);
80
+ const title = cleanText(finding?.title);
81
+ const file = cleanText(finding?.file);
82
+ const what = cleanText(finding?.what);
83
+ const why = cleanText(finding?.why);
84
+ const fix = cleanText(finding?.fix);
85
+ const line =
86
+ typeof finding?.line === 'number' && Number.isInteger(finding.line) && finding.line > 0
87
+ ? finding.line
88
+ : null;
89
+
90
+ if (!SEVERITY_HEADERS[severity] || !title || !file || !what || !why || !fix) {
91
+ return { ok: false, reason: 'structured output contains an invalid finding' };
92
+ }
93
+
94
+ normalizedFindings.push({ severity, title, file, line, what, why, fix });
95
+ }
96
+
97
+ return {
98
+ ok: true,
99
+ value: {
100
+ summary,
101
+ findings: normalizedFindings,
102
+ overallAssessment,
103
+ overallRationale,
104
+ notes,
105
+ },
106
+ };
107
+ }
108
+
109
+ export function renderStructuredReview(review, { model }) {
110
+ const lines = ['## Summary', escapeMarkdownText(review.summary), '', '## Findings'];
111
+
112
+ if (review.findings.length === 0) {
113
+ lines.push('No confirmed issues found after reviewing the diff and surrounding code.');
114
+ } else {
115
+ for (const severity of SEVERITY_ORDER) {
116
+ const findings = review.findings.filter((finding) => finding.severity === severity);
117
+ if (findings.length === 0) continue;
118
+
119
+ lines.push(SEVERITY_HEADERS[severity]);
120
+ for (const finding of findings) {
121
+ const location = finding.line ? `${finding.file}:${finding.line}` : finding.file;
122
+ lines.push(`- **${renderCode(location)} — ${escapeMarkdownText(finding.title)}**`);
123
+ lines.push(` Problem: ${escapeMarkdownText(finding.what)}`);
124
+ lines.push(` Why it matters: ${escapeMarkdownText(finding.why)}`);
125
+ lines.push(` Suggested fix: ${escapeMarkdownText(finding.fix)}`);
126
+ }
127
+ lines.push('');
128
+ }
129
+ if (lines[lines.length - 1] === '') {
130
+ lines.pop();
131
+ }
132
+ }
133
+
134
+ if (review.notes.length > 0) {
135
+ lines.push('', '## Notes');
136
+ for (const note of review.notes) {
137
+ lines.push(`- ${escapeMarkdownText(note)}`);
138
+ }
139
+ }
140
+
141
+ lines.push(
142
+ '',
143
+ '## Overall Assessment',
144
+ `**${ASSESSMENTS[review.overallAssessment]}** — ${escapeMarkdownText(review.overallRationale)}`,
145
+ '',
146
+ `> Reviewed by \`${model}\``
147
+ );
148
+
149
+ return lines.join('\n');
150
+ }
151
+
152
+ export function renderIncompleteReview({ model, reason, runUrl, runtimeTools, turnsUsed }) {
153
+ const lines = [
154
+ '## AI Review Incomplete',
155
+ '',
156
+ 'Claude did not return validated structured review output, so this workflow did not publish raw scratch text.',
157
+ '',
158
+ `- Reason: ${escapeMarkdownText(reason)}`,
159
+ ];
160
+
161
+ if (runtimeTools?.length) {
162
+ lines.push(`- Runtime tools: ${runtimeTools.map(renderCode).join(', ')}`);
163
+ }
164
+ if (typeof turnsUsed === 'number') {
165
+ lines.push(`- Turns used: ${turnsUsed}`);
166
+ }
167
+
168
+ lines.push('', `Re-run \`/review\` or inspect [the workflow run](${runUrl}).`, '', `> Reviewed by \`${model}\``);
169
+ return lines.join('\n');
170
+ }
171
+
172
+ export function writeReviewFromEnv(env = process.env) {
173
+ const outputFile = env.AI_REVIEW_OUTPUT_FILE || 'pr_review.md';
174
+ const model = env.AI_REVIEW_MODEL || 'unknown-model';
175
+ const runUrl = env.AI_REVIEW_RUN_URL || '#';
176
+ const validation = normalizeStructuredOutput(env.AI_REVIEW_STRUCTURED_OUTPUT);
177
+ const metadata = readExecutionMetadata(env.AI_REVIEW_EXECUTION_FILE);
178
+ const content = validation.ok
179
+ ? renderStructuredReview(validation.value, { model })
180
+ : renderIncompleteReview({
181
+ model,
182
+ reason: validation.reason,
183
+ runUrl,
184
+ runtimeTools: metadata.runtimeTools,
185
+ turnsUsed: metadata.turnsUsed,
186
+ });
187
+
188
+ fs.mkdirSync(path.dirname(outputFile), { recursive: true });
189
+ fs.writeFileSync(outputFile, `${content}\n`, 'utf8');
190
+
191
+ if (!validation.ok) {
192
+ console.warn(`::warning::AI review output normalization fell back to incomplete comment: ${validation.reason}`);
193
+ }
194
+
195
+ return { usedFallback: !validation.ok, content };
196
+ }
197
+
198
+ const isMain =
199
+ process.argv[1] &&
200
+ path.resolve(process.argv[1]) === path.resolve(fileURLToPath(import.meta.url));
201
+
202
+ if (isMain) {
203
+ writeReviewFromEnv();
204
+ }