@mandujs/core 0.42.0 → 0.43.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/package.json +21 -4
- package/src/auth/__tests__/login.test.ts +420 -419
- package/src/auth/__tests__/reset.test.ts +296 -296
- package/src/brain/adapters/anthropic-oauth.ts +421 -420
- package/src/brain/adapters/index.ts +2 -1
- package/src/brain/adapters/ollama.ts +1 -1
- package/src/brain/adapters/openai-oauth.ts +534 -533
- package/src/brain/brain.ts +2 -1
- package/src/brain/redactor.ts +196 -196
- package/src/bundler/__tests__/cli-bench-utils.test.ts +149 -149
- package/src/bundler/__tests__/cold-start.test.ts +504 -504
- package/src/bundler/__tests__/fast-refresh.test.ts +607 -606
- package/src/bundler/__tests__/hdr.test.ts +1 -1
- package/src/bundler/analyzer.ts +958 -958
- package/src/bundler/build.ts +104 -14
- package/src/bundler/dev.ts +125 -0
- package/src/bundler/hmr-types.ts +1 -0
- package/src/bundler/plugins/__tests__/react-compiler-lint.test.ts +110 -0
- package/src/bundler/plugins/index.ts +14 -0
- package/src/bundler/plugins/react-compiler-lint.ts +253 -0
- package/src/bundler/plugins/react-compiler.ts +162 -0
- package/src/bundler/types.ts +12 -0
- package/src/change/integrity.ts +2 -1
- package/src/client/index.ts +10 -0
- package/src/client/island.ts +38 -11
- package/src/client/router.ts +6 -1
- package/src/config/mandu.ts +57 -0
- package/src/config/validate.ts +42 -0
- package/src/content/collection.ts +844 -809
- package/src/content/content-layer.ts +316 -314
- package/src/content/content.test.ts +433 -433
- package/src/content/digest.ts +133 -133
- package/src/content/generate-types.ts +168 -168
- package/src/content/index.ts +6 -1
- package/src/content/llms-txt.ts +277 -277
- package/src/contract/define.ts +474 -474
- package/src/contract/route-helpers.ts +2 -1
- package/src/contract/zod-utils.ts +158 -155
- package/src/db/index.ts +513 -513
- package/src/desktop/__tests__/smoke.test.ts +100 -100
- package/src/desktop/webview-fallback.ts +583 -583
- package/src/desktop/window.ts +3 -1
- package/src/dev-error-overlay/overlay-client.ts +300 -300
- package/src/devtools/ai/mcp-connector.ts +499 -498
- package/src/devtools/client/components/kitchen-root.tsx +7 -2
- package/src/email/resend.ts +163 -163
- package/src/guard/__tests__/tsgolint-bridge.test.ts +347 -0
- package/src/guard/ast-analyzer.ts +806 -806
- package/src/guard/graph.ts +898 -898
- package/src/guard/index.ts +16 -0
- package/src/guard/statistics.ts +578 -578
- package/src/guard/tsgolint-bridge.ts +512 -0
- package/src/i18n/locale-resolver.ts +214 -214
- package/src/id/__tests__/id.test.ts +120 -120
- package/src/intent/index.ts +321 -321
- package/src/island/index.ts +39 -23
- package/src/kitchen/api/contract-api.ts +15 -8
- package/src/kitchen/kitchen-ui.ts +2137 -2137
- package/src/lockfile/index.ts +3 -2
- package/src/middleware/oauth/__tests__/oauth.test.ts +575 -574
- package/src/middleware/rate-limit/__tests__/rate-limit.test.ts +642 -642
- package/src/middleware/secure/index.ts +417 -417
- package/src/observability/metrics.ts +334 -334
- package/src/observability/tracing.ts +694 -694
- package/src/openapi/generator.ts +1 -1
- package/src/perf/user-marks.ts +553 -553
- package/src/plugins/registry.ts +387 -387
- package/src/resource/ddl/diff.ts +392 -392
- package/src/resource/ddl/snapshot.ts +448 -447
- package/src/resource/generator-schema.ts +477 -476
- package/src/resource/parser.ts +4 -2
- package/src/resource/schema.ts +1 -1
- package/src/router/fs-patterns.ts +422 -422
- package/src/runtime/fast-refresh-types.ts +126 -128
- package/src/runtime/image-handler.ts +206 -195
- package/src/runtime/router.test.ts +476 -476
- package/src/runtime/security.ts +155 -155
- package/src/runtime/server.ts +36 -19
- package/src/runtime/session-key.ts +328 -328
- package/src/scheduler/__tests__/scheduler.test.ts +514 -514
- package/src/seo/resolve/index.ts +353 -353
- package/src/spec/load.ts +1 -1
- package/src/testing/reporter.ts +676 -676
- package/src/testing/server.ts +196 -196
- package/src/testing/snapshot.ts +444 -444
- package/src/utils/__tests__/lru-cache.test.ts +186 -186
- package/src/utils/bun.ts +8 -8
|
@@ -1,498 +1,499 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Mandu Kitchen DevTools - MCP Connector
|
|
3
|
-
* @version 1.1.0
|
|
4
|
-
*
|
|
5
|
-
* Model Context Protocol (MCP) 연동
|
|
6
|
-
* - AI 에이전트와의 통신
|
|
7
|
-
* - 컨텍스트 전달
|
|
8
|
-
* - 수정 제안 수신
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type { AIContextPayload, NormalizedError } from '../types';
|
|
12
|
-
import { AIContextBuilder
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
| '
|
|
80
|
-
| '
|
|
81
|
-
| '
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
//
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
//
|
|
105
|
-
//
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
private
|
|
110
|
-
private
|
|
111
|
-
private
|
|
112
|
-
private
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
private
|
|
122
|
-
private
|
|
123
|
-
private
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
this.
|
|
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
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
this.
|
|
171
|
-
|
|
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
|
-
this.reconnectTimer
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
this.ws
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
pending.
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
//
|
|
240
|
-
//
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
*
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
'',
|
|
308
|
-
|
|
309
|
-
`**
|
|
310
|
-
`**
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
parts.push('');
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
parts.push('
|
|
322
|
-
parts.push(
|
|
323
|
-
parts.push(
|
|
324
|
-
parts.push('');
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
parts.push('
|
|
330
|
-
parts.push(
|
|
331
|
-
parts.push(
|
|
332
|
-
parts.push('');
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
parts.push(
|
|
338
|
-
parts.push(`-
|
|
339
|
-
parts.push(`-
|
|
340
|
-
parts.push(
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
parts.push(
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
//
|
|
360
|
-
//
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
this.
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
this.reconnectTimer
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
this.
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
//
|
|
482
|
-
//
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
globalMCPConnector
|
|
497
|
-
|
|
498
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Mandu Kitchen DevTools - MCP Connector
|
|
3
|
+
* @version 1.1.0
|
|
4
|
+
*
|
|
5
|
+
* Model Context Protocol (MCP) 연동
|
|
6
|
+
* - AI 에이전트와의 통신
|
|
7
|
+
* - 컨텍스트 전달
|
|
8
|
+
* - 수정 제안 수신
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { AIContextPayload, NormalizedError } from '../types';
|
|
12
|
+
import type { AIContextBuilder} from './context-builder';
|
|
13
|
+
import { getContextBuilder } from './context-builder';
|
|
14
|
+
|
|
15
|
+
// ============================================================================
|
|
16
|
+
// Types
|
|
17
|
+
// ============================================================================
|
|
18
|
+
|
|
19
|
+
export interface MCPConnectorOptions {
|
|
20
|
+
/** MCP 서버 URL */
|
|
21
|
+
serverUrl?: string;
|
|
22
|
+
/** 연결 타임아웃 (ms) */
|
|
23
|
+
connectionTimeout?: number;
|
|
24
|
+
/** 요청 타임아웃 (ms) */
|
|
25
|
+
requestTimeout?: number;
|
|
26
|
+
/** 자동 재연결 */
|
|
27
|
+
autoReconnect?: boolean;
|
|
28
|
+
/** 재연결 간격 (ms) */
|
|
29
|
+
reconnectInterval?: number;
|
|
30
|
+
/** 최대 재연결 시도 횟수 */
|
|
31
|
+
maxReconnectAttempts?: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface MCPMessage {
|
|
35
|
+
id: string;
|
|
36
|
+
type: 'request' | 'response' | 'notification';
|
|
37
|
+
method?: string;
|
|
38
|
+
params?: unknown;
|
|
39
|
+
result?: unknown;
|
|
40
|
+
error?: {
|
|
41
|
+
code: number;
|
|
42
|
+
message: string;
|
|
43
|
+
data?: unknown;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface AnalysisRequest {
|
|
48
|
+
context: AIContextPayload;
|
|
49
|
+
options?: {
|
|
50
|
+
includeFixSuggestion?: boolean;
|
|
51
|
+
includeExplanation?: boolean;
|
|
52
|
+
language?: string;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface AnalysisResponse {
|
|
57
|
+
success: boolean;
|
|
58
|
+
analysis?: {
|
|
59
|
+
rootCause: string;
|
|
60
|
+
explanation: string;
|
|
61
|
+
severity: 'critical' | 'high' | 'medium' | 'low';
|
|
62
|
+
category: string;
|
|
63
|
+
};
|
|
64
|
+
fixSuggestion?: {
|
|
65
|
+
description: string;
|
|
66
|
+
code?: string;
|
|
67
|
+
file?: string;
|
|
68
|
+
lineRange?: [number, number];
|
|
69
|
+
confidence: number;
|
|
70
|
+
};
|
|
71
|
+
relatedDocs?: Array<{
|
|
72
|
+
title: string;
|
|
73
|
+
url: string;
|
|
74
|
+
}>;
|
|
75
|
+
error?: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type MCPConnectionStatus =
|
|
79
|
+
| 'disconnected'
|
|
80
|
+
| 'connecting'
|
|
81
|
+
| 'connected'
|
|
82
|
+
| 'error';
|
|
83
|
+
|
|
84
|
+
// ============================================================================
|
|
85
|
+
// Constants
|
|
86
|
+
// ============================================================================
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* #181: 이전에는 `ws://{host}:{port}/mcp` 형태로 자동 유추했으나, dev 서버는
|
|
90
|
+
* 해당 엔드포인트를 제공하지 않아 브라우저가 reconnect 루프에 빠져 멈추는 문제가 있었음.
|
|
91
|
+
* 이제는 명시적인 `serverUrl`을 받지 못하면 connect() 자체가 실패하도록 한다.
|
|
92
|
+
* 호출자가 의도적으로 MCP 엔드포인트 URL을 넘겨야만 WebSocket을 시도한다.
|
|
93
|
+
*/
|
|
94
|
+
const DEFAULT_OPTIONS: Required<MCPConnectorOptions> = {
|
|
95
|
+
serverUrl: '',
|
|
96
|
+
connectionTimeout: 5000,
|
|
97
|
+
requestTimeout: 30000,
|
|
98
|
+
// #175: MCP 서버가 없을 수 있으므로 기본 비활성화
|
|
99
|
+
autoReconnect: false,
|
|
100
|
+
reconnectInterval: 1000,
|
|
101
|
+
maxReconnectAttempts: 3,
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// ============================================================================
|
|
105
|
+
// MCP Connector
|
|
106
|
+
// ============================================================================
|
|
107
|
+
|
|
108
|
+
export class MCPConnector {
|
|
109
|
+
private options: Required<MCPConnectorOptions>;
|
|
110
|
+
private ws: WebSocket | null = null;
|
|
111
|
+
private status: MCPConnectionStatus = 'disconnected';
|
|
112
|
+
private requestIdCounter = 0;
|
|
113
|
+
private pendingRequests = new Map<
|
|
114
|
+
string,
|
|
115
|
+
{
|
|
116
|
+
resolve: (result: unknown) => void;
|
|
117
|
+
reject: (error: Error) => void;
|
|
118
|
+
timeout: ReturnType<typeof setTimeout>;
|
|
119
|
+
}
|
|
120
|
+
>();
|
|
121
|
+
private reconnectAttempts = 0;
|
|
122
|
+
private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
|
123
|
+
private statusListeners = new Set<(status: MCPConnectionStatus) => void>();
|
|
124
|
+
private contextBuilder: AIContextBuilder;
|
|
125
|
+
|
|
126
|
+
constructor(options: MCPConnectorOptions = {}) {
|
|
127
|
+
this.options = { ...DEFAULT_OPTIONS, ...options };
|
|
128
|
+
this.contextBuilder = getContextBuilder();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// --------------------------------------------------------------------------
|
|
132
|
+
// Connection Management
|
|
133
|
+
// --------------------------------------------------------------------------
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* MCP 서버에 연결
|
|
137
|
+
*/
|
|
138
|
+
connect(): Promise<void> {
|
|
139
|
+
return new Promise((resolve, reject) => {
|
|
140
|
+
if (this.status === 'connected' && this.ws) {
|
|
141
|
+
resolve();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// #181: 명시적 serverUrl이 없으면 즉시 실패. 자동 URL 유추는
|
|
146
|
+
// 존재하지 않는 /mcp 엔드포인트로 향해 브라우저를 멈추게 했음.
|
|
147
|
+
const url = this.options.serverUrl;
|
|
148
|
+
if (!url) {
|
|
149
|
+
const err = new Error(
|
|
150
|
+
'[Mandu Kitchen] MCPConnector.connect() requires an explicit serverUrl. ' +
|
|
151
|
+
'No MCP WebSocket endpoint is started by the dev server.',
|
|
152
|
+
);
|
|
153
|
+
this.handleConnectionError(err);
|
|
154
|
+
reject(err);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
this.setStatus('connecting');
|
|
159
|
+
|
|
160
|
+
const timeout = setTimeout(() => {
|
|
161
|
+
reject(new Error('Connection timeout'));
|
|
162
|
+
this.handleConnectionError(new Error('Connection timeout'));
|
|
163
|
+
}, this.options.connectionTimeout);
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
this.ws = new WebSocket(url);
|
|
167
|
+
|
|
168
|
+
this.ws.onopen = () => {
|
|
169
|
+
clearTimeout(timeout);
|
|
170
|
+
this.setStatus('connected');
|
|
171
|
+
this.reconnectAttempts = 0;
|
|
172
|
+
resolve();
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
this.ws.onclose = () => {
|
|
176
|
+
this.handleDisconnect();
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
this.ws.onerror = (event) => {
|
|
180
|
+
clearTimeout(timeout);
|
|
181
|
+
this.handleConnectionError(new Error('WebSocket error'));
|
|
182
|
+
reject(new Error('WebSocket connection failed'));
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
this.ws.onmessage = (event) => {
|
|
186
|
+
this.handleMessage(event.data);
|
|
187
|
+
};
|
|
188
|
+
} catch (error) {
|
|
189
|
+
clearTimeout(timeout);
|
|
190
|
+
this.handleConnectionError(
|
|
191
|
+
error instanceof Error ? error : new Error(String(error))
|
|
192
|
+
);
|
|
193
|
+
reject(error);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* 연결 종료
|
|
200
|
+
*/
|
|
201
|
+
disconnect(): void {
|
|
202
|
+
if (this.reconnectTimer) {
|
|
203
|
+
clearTimeout(this.reconnectTimer);
|
|
204
|
+
this.reconnectTimer = null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (this.ws) {
|
|
208
|
+
this.ws.close();
|
|
209
|
+
this.ws = null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// 대기 중인 요청 모두 reject
|
|
213
|
+
for (const [id, pending] of this.pendingRequests) {
|
|
214
|
+
clearTimeout(pending.timeout);
|
|
215
|
+
pending.reject(new Error('Connection closed'));
|
|
216
|
+
}
|
|
217
|
+
this.pendingRequests.clear();
|
|
218
|
+
|
|
219
|
+
this.setStatus('disconnected');
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 연결 상태 조회
|
|
224
|
+
*/
|
|
225
|
+
getStatus(): MCPConnectionStatus {
|
|
226
|
+
return this.status;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 상태 변경 리스너 등록
|
|
231
|
+
*/
|
|
232
|
+
onStatusChange(
|
|
233
|
+
listener: (status: MCPConnectionStatus) => void
|
|
234
|
+
): () => void {
|
|
235
|
+
this.statusListeners.add(listener);
|
|
236
|
+
return () => this.statusListeners.delete(listener);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// --------------------------------------------------------------------------
|
|
240
|
+
// MCP Methods
|
|
241
|
+
// --------------------------------------------------------------------------
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* 에러 분석 요청
|
|
245
|
+
*/
|
|
246
|
+
async analyzeError(
|
|
247
|
+
error: NormalizedError,
|
|
248
|
+
options?: AnalysisRequest['options']
|
|
249
|
+
): Promise<AnalysisResponse> {
|
|
250
|
+
// 컨텍스트 빌드
|
|
251
|
+
const context = await this.contextBuilder.buildContext(error);
|
|
252
|
+
|
|
253
|
+
return this.sendRequest<AnalysisResponse>('mandu/analyze', {
|
|
254
|
+
context,
|
|
255
|
+
options: {
|
|
256
|
+
includeFixSuggestion: true,
|
|
257
|
+
includeExplanation: true,
|
|
258
|
+
language: 'ko',
|
|
259
|
+
...options,
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* 수정 제안 요청
|
|
266
|
+
*/
|
|
267
|
+
async getSuggestion(
|
|
268
|
+
error: NormalizedError,
|
|
269
|
+
codeContext: string
|
|
270
|
+
): Promise<AnalysisResponse['fixSuggestion']> {
|
|
271
|
+
const result = await this.sendRequest<{
|
|
272
|
+
suggestion: AnalysisResponse['fixSuggestion'];
|
|
273
|
+
}>('mandu/suggest', {
|
|
274
|
+
error: {
|
|
275
|
+
message: error.message,
|
|
276
|
+
type: error.type,
|
|
277
|
+
source: error.source,
|
|
278
|
+
line: error.line,
|
|
279
|
+
},
|
|
280
|
+
codeContext,
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
return result.suggestion;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* 관련 문서 검색
|
|
288
|
+
*/
|
|
289
|
+
async searchDocs(
|
|
290
|
+
query: string
|
|
291
|
+
): Promise<AnalysisResponse['relatedDocs']> {
|
|
292
|
+
const result = await this.sendRequest<{
|
|
293
|
+
docs: AnalysisResponse['relatedDocs'];
|
|
294
|
+
}>('mandu/docs', { query });
|
|
295
|
+
|
|
296
|
+
return result.docs;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* 클립보드용 컨텍스트 생성
|
|
301
|
+
* (AI 에이전트에 붙여넣기 위한 포맷)
|
|
302
|
+
*/
|
|
303
|
+
async formatForClipboard(error: NormalizedError): Promise<string> {
|
|
304
|
+
const context = await this.contextBuilder.buildContext(error);
|
|
305
|
+
|
|
306
|
+
const parts: string[] = [
|
|
307
|
+
'## Error Report',
|
|
308
|
+
'',
|
|
309
|
+
`**Type**: ${error.type}`,
|
|
310
|
+
`**Severity**: ${error.severity}`,
|
|
311
|
+
`**Message**: ${error.message}`,
|
|
312
|
+
'',
|
|
313
|
+
];
|
|
314
|
+
|
|
315
|
+
if (error.source) {
|
|
316
|
+
parts.push(`**Location**: ${error.source}:${error.line ?? '?'}:${error.column ?? '?'}`);
|
|
317
|
+
parts.push('');
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (error.stack) {
|
|
321
|
+
parts.push('### Stack Trace');
|
|
322
|
+
parts.push('```');
|
|
323
|
+
parts.push(error.stack);
|
|
324
|
+
parts.push('```');
|
|
325
|
+
parts.push('');
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (context.codeContext?.snippet) {
|
|
329
|
+
parts.push('### Source Code');
|
|
330
|
+
parts.push('```typescript');
|
|
331
|
+
parts.push(context.codeContext.snippet.content);
|
|
332
|
+
parts.push('```');
|
|
333
|
+
parts.push('');
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (context.island) {
|
|
337
|
+
parts.push('### Island Info');
|
|
338
|
+
parts.push(`- Name: ${context.island.name}`);
|
|
339
|
+
parts.push(`- Status: ${context.island.status}`);
|
|
340
|
+
parts.push(`- Strategy: ${context.island.strategy}`);
|
|
341
|
+
parts.push('');
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (context.recentErrors && context.recentErrors.length > 0) {
|
|
345
|
+
parts.push('### Recent Related Errors');
|
|
346
|
+
for (const e of context.recentErrors) {
|
|
347
|
+
const timeAgo = Math.round((Date.now() - e.timestamp) / 1000);
|
|
348
|
+
parts.push(`- ${e.message} (${timeAgo}s ago)`);
|
|
349
|
+
}
|
|
350
|
+
parts.push('');
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
parts.push('---');
|
|
354
|
+
parts.push(`*Generated by Mandu Kitchen DevTools v${context.devtools.version}*`);
|
|
355
|
+
|
|
356
|
+
return parts.join('\n');
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// --------------------------------------------------------------------------
|
|
360
|
+
// Internal
|
|
361
|
+
// --------------------------------------------------------------------------
|
|
362
|
+
|
|
363
|
+
private setStatus(status: MCPConnectionStatus): void {
|
|
364
|
+
if (this.status !== status) {
|
|
365
|
+
this.status = status;
|
|
366
|
+
for (const listener of this.statusListeners) {
|
|
367
|
+
try {
|
|
368
|
+
listener(status);
|
|
369
|
+
} catch {
|
|
370
|
+
// 리스너 에러 무시
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
private handleMessage(data: string): void {
|
|
377
|
+
try {
|
|
378
|
+
const message: MCPMessage = JSON.parse(data);
|
|
379
|
+
|
|
380
|
+
if (message.type === 'response' && message.id) {
|
|
381
|
+
const pending = this.pendingRequests.get(message.id);
|
|
382
|
+
if (pending) {
|
|
383
|
+
clearTimeout(pending.timeout);
|
|
384
|
+
this.pendingRequests.delete(message.id);
|
|
385
|
+
|
|
386
|
+
if (message.error) {
|
|
387
|
+
pending.reject(new Error(message.error.message));
|
|
388
|
+
} else {
|
|
389
|
+
pending.resolve(message.result);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
} catch (error) {
|
|
394
|
+
console.warn('[Mandu Kitchen] Failed to parse MCP message:', error);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
private handleDisconnect(): void {
|
|
399
|
+
this.ws = null;
|
|
400
|
+
|
|
401
|
+
// #175: handleConnectionError에서 이미 reconnect 예약한 경우 중복 방지
|
|
402
|
+
if (this.options.autoReconnect && this.status !== 'disconnected' && !this.reconnectTimer) {
|
|
403
|
+
this.setStatus('error');
|
|
404
|
+
this.scheduleReconnect();
|
|
405
|
+
} else if (!this.reconnectTimer) {
|
|
406
|
+
this.setStatus('disconnected');
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
private handleConnectionError(error: Error): void {
|
|
411
|
+
console.warn('[Mandu Kitchen] MCP connection error:', error.message);
|
|
412
|
+
this.setStatus('error');
|
|
413
|
+
|
|
414
|
+
if (this.options.autoReconnect && !this.reconnectTimer) {
|
|
415
|
+
this.scheduleReconnect();
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
private scheduleReconnect(): void {
|
|
420
|
+
// #175: 기존 타이머 취소 (중복 방지)
|
|
421
|
+
if (this.reconnectTimer) {
|
|
422
|
+
clearTimeout(this.reconnectTimer);
|
|
423
|
+
this.reconnectTimer = null;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (this.reconnectAttempts >= this.options.maxReconnectAttempts) {
|
|
427
|
+
console.warn('[Mandu Kitchen] Max reconnection attempts reached');
|
|
428
|
+
this.setStatus('disconnected');
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
this.reconnectAttempts++;
|
|
433
|
+
// #175: exponential backoff (1s → 2s → 4s → ... → max 30s)
|
|
434
|
+
const delay = Math.min(
|
|
435
|
+
this.options.reconnectInterval * Math.pow(2, this.reconnectAttempts - 1),
|
|
436
|
+
30_000,
|
|
437
|
+
);
|
|
438
|
+
console.log(
|
|
439
|
+
`[Mandu Kitchen] Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts}/${this.options.maxReconnectAttempts})`
|
|
440
|
+
);
|
|
441
|
+
|
|
442
|
+
this.reconnectTimer = setTimeout(() => {
|
|
443
|
+
this.reconnectTimer = null;
|
|
444
|
+
this.connect().catch(() => {
|
|
445
|
+
// 에러는 handleConnectionError에서 처리됨
|
|
446
|
+
});
|
|
447
|
+
}, delay);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
private async sendRequest<T>(method: string, params: unknown): Promise<T> {
|
|
451
|
+
if (!this.ws || this.status !== 'connected') {
|
|
452
|
+
throw new Error('Not connected to MCP server');
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
const id = `req-${++this.requestIdCounter}`;
|
|
456
|
+
|
|
457
|
+
return new Promise<T>((resolve, reject) => {
|
|
458
|
+
const timeout = setTimeout(() => {
|
|
459
|
+
this.pendingRequests.delete(id);
|
|
460
|
+
reject(new Error('Request timeout'));
|
|
461
|
+
}, this.options.requestTimeout);
|
|
462
|
+
|
|
463
|
+
this.pendingRequests.set(id, {
|
|
464
|
+
resolve: resolve as (result: unknown) => void,
|
|
465
|
+
reject,
|
|
466
|
+
timeout,
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
const message: MCPMessage = {
|
|
470
|
+
id,
|
|
471
|
+
type: 'request',
|
|
472
|
+
method,
|
|
473
|
+
params,
|
|
474
|
+
};
|
|
475
|
+
|
|
476
|
+
this.ws!.send(JSON.stringify(message));
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// ============================================================================
|
|
482
|
+
// Singleton Instance
|
|
483
|
+
// ============================================================================
|
|
484
|
+
|
|
485
|
+
let globalMCPConnector: MCPConnector | null = null;
|
|
486
|
+
|
|
487
|
+
export function getMCPConnector(options?: MCPConnectorOptions): MCPConnector {
|
|
488
|
+
if (!globalMCPConnector) {
|
|
489
|
+
globalMCPConnector = new MCPConnector(options);
|
|
490
|
+
}
|
|
491
|
+
return globalMCPConnector;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
export function destroyMCPConnector(): void {
|
|
495
|
+
if (globalMCPConnector) {
|
|
496
|
+
globalMCPConnector.disconnect();
|
|
497
|
+
globalMCPConnector = null;
|
|
498
|
+
}
|
|
499
|
+
}
|