ai-cmg 0.1.3 → 0.1.4
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 +90 -11
- package/dist/index.js +18 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -81,7 +81,24 @@ export default {
|
|
|
81
81
|
|
|
82
82
|
try {
|
|
83
83
|
// 3. Parse request data (diff, hint, prompt)
|
|
84
|
-
|
|
84
|
+
let requestData;
|
|
85
|
+
try {
|
|
86
|
+
requestData = await request.json();
|
|
87
|
+
} catch (parseError) {
|
|
88
|
+
console.error('JSON parse error:', parseError);
|
|
89
|
+
return new Response(
|
|
90
|
+
JSON.stringify({
|
|
91
|
+
error: 'Invalid request format',
|
|
92
|
+
details: 'Failed to parse request body as JSON.'
|
|
93
|
+
}),
|
|
94
|
+
{
|
|
95
|
+
status: 400,
|
|
96
|
+
headers: { 'Content-Type': 'application/json' }
|
|
97
|
+
}
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const { diff, hint, prompt } = requestData;
|
|
85
102
|
|
|
86
103
|
if (!diff) {
|
|
87
104
|
return new Response(JSON.stringify({ error: 'Diff data is missing' }), { status: 400 });
|
|
@@ -145,20 +162,82 @@ export default {
|
|
|
145
162
|
}
|
|
146
163
|
|
|
147
164
|
// 5. Run AI model
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
165
|
+
let response;
|
|
166
|
+
try {
|
|
167
|
+
response = await env.AI.run(model, {
|
|
168
|
+
messages: [
|
|
169
|
+
{ role: 'system', content: systemInstruction },
|
|
170
|
+
{ role: 'user', content: diff }
|
|
171
|
+
]
|
|
172
|
+
});
|
|
173
|
+
} catch (aiError) {
|
|
174
|
+
console.error('AI model error:', aiError);
|
|
175
|
+
const errorMessage = aiError?.message || String(aiError);
|
|
176
|
+
|
|
177
|
+
// Check for context length exceeded error
|
|
178
|
+
const isContextLengthError = errorMessage.includes('maximum context length') ||
|
|
179
|
+
errorMessage.includes('3030') ||
|
|
180
|
+
errorMessage.includes('context length');
|
|
181
|
+
|
|
182
|
+
if (isContextLengthError) {
|
|
183
|
+
return new Response(
|
|
184
|
+
JSON.stringify({
|
|
185
|
+
error: 'Context length exceeded',
|
|
186
|
+
details: errorMessage,
|
|
187
|
+
hint: 'The diff is too large. Please reduce the number of staged changes or split into multiple commits. The CLI should automatically truncate large diffs, but you may need to stage fewer files.'
|
|
188
|
+
}),
|
|
189
|
+
{
|
|
190
|
+
status: 400,
|
|
191
|
+
headers: { 'Content-Type': 'application/json' }
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return new Response(
|
|
197
|
+
JSON.stringify({
|
|
198
|
+
error: 'AI model error',
|
|
199
|
+
details: errorMessage,
|
|
200
|
+
hint: 'Check if the model is available and your Workers AI quota is sufficient.'
|
|
201
|
+
}),
|
|
202
|
+
{
|
|
203
|
+
status: 500,
|
|
204
|
+
headers: { 'Content-Type': 'application/json' }
|
|
205
|
+
}
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (!response || !response.response) {
|
|
210
|
+
console.error('Invalid AI response:', response);
|
|
211
|
+
return new Response(
|
|
212
|
+
JSON.stringify({
|
|
213
|
+
error: 'Invalid AI response',
|
|
214
|
+
details: 'The AI model returned an unexpected response format.'
|
|
215
|
+
}),
|
|
216
|
+
{
|
|
217
|
+
status: 500,
|
|
218
|
+
headers: { 'Content-Type': 'application/json' }
|
|
219
|
+
}
|
|
220
|
+
);
|
|
221
|
+
}
|
|
154
222
|
|
|
155
223
|
return Response.json({ message: response.response });
|
|
156
224
|
|
|
157
225
|
} catch (error) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
226
|
+
console.error('Worker error:', error);
|
|
227
|
+
const errorMessage = error?.message || String(error);
|
|
228
|
+
const errorStack = error?.stack;
|
|
229
|
+
|
|
230
|
+
return new Response(
|
|
231
|
+
JSON.stringify({
|
|
232
|
+
error: 'Server error',
|
|
233
|
+
details: errorMessage,
|
|
234
|
+
...(errorStack && { stack: errorStack })
|
|
235
|
+
}),
|
|
236
|
+
{
|
|
237
|
+
status: 500,
|
|
238
|
+
headers: { 'Content-Type': 'application/json' }
|
|
239
|
+
}
|
|
240
|
+
);
|
|
162
241
|
}
|
|
163
242
|
}
|
|
164
243
|
};
|
package/dist/index.js
CHANGED
|
@@ -23,7 +23,7 @@ const LOCK_FILES = new Set([
|
|
|
23
23
|
const GENERATED_PREFIXES = ['dist/', 'build/', '.next/', 'coverage/', 'node_modules/'];
|
|
24
24
|
const MAX_BLOCK_LINES = 400;
|
|
25
25
|
const MAX_BLOCK_CHARS = 20000;
|
|
26
|
-
const MAX_INPUT_CHARS =
|
|
26
|
+
const MAX_INPUT_CHARS = 50000;
|
|
27
27
|
function getPackageVersion() {
|
|
28
28
|
try {
|
|
29
29
|
const currentFile = fileURLToPath(import.meta.url);
|
|
@@ -486,7 +486,23 @@ async function main() {
|
|
|
486
486
|
return;
|
|
487
487
|
}
|
|
488
488
|
if (!response.ok) {
|
|
489
|
-
|
|
489
|
+
let errorMessage = `Server Error: ${response.status}`;
|
|
490
|
+
try {
|
|
491
|
+
const errorData = await response.json();
|
|
492
|
+
if (errorData.error) {
|
|
493
|
+
errorMessage = errorData.error;
|
|
494
|
+
if (errorData.details) {
|
|
495
|
+
errorMessage += `\nDetails: ${errorData.details}`;
|
|
496
|
+
}
|
|
497
|
+
if (errorData.hint) {
|
|
498
|
+
errorMessage += `\nHint: ${errorData.hint}`;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
catch {
|
|
503
|
+
// If JSON parsing fails, use the default error message
|
|
504
|
+
}
|
|
505
|
+
throw new Error(errorMessage);
|
|
490
506
|
}
|
|
491
507
|
const { message } = await response.json();
|
|
492
508
|
// 5. 결과 출력 및 액션 선택
|