@wsl233/dsh-gomoku 0.3.10 → 0.4.6

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/client.js CHANGED
@@ -3,12 +3,12 @@
3
3
  *
4
4
  * Registers:
5
5
  * - a sidebar row (sidebar.panellist) labelled 五子棋;
6
- * - a main panel (main, key "gomoku") that mounts the shared plain-DOM UI.
6
+ * - a main panel (main, key "gomoku") that mounts the shared plain-DOM UI;
7
+ * - a rich tool card (tool.call.toolview, key "gomoku_analyze") that draws
8
+ * the numbered board, win-rate chart, key local boards and variants.
7
9
  *
8
- * The UI itself lives in ui/gomoku-ui.js so the exact same code can be loaded
9
- * by preview.html outside DSH. This file only bridges React slots to that UI.
10
- *
11
- * Phase 1: no game logic, no network, no AI. The panel is a visual preview.
10
+ * The panel UI lives in ui/gomoku-ui.js. This file only bridges React slots to
11
+ * that UI and renders the conversation tool card from the tool result meta.
12
12
  */
13
13
 
14
14
  // Guard against the client bundle being evaluated twice in one renderer.
@@ -27,7 +27,7 @@ if (!window.__dshGomokuClientLoaded) {
27
27
  const PANEL_ID = 'gomoku';
28
28
  const UI_BASE = '/dsh-gomoku';
29
29
  const STYLE_ID = 'dsh-gomoku-style';
30
- const UI_VERSION = '0.2.0-32';
30
+ const UI_VERSION = '0.2.0-51';
31
31
  const withVersion = (src) => src + '?v=' + UI_VERSION;
32
32
 
33
33
  let scriptPromise = null;
@@ -180,7 +180,450 @@ if (!window.__dshGomokuClientLoaded) {
180
180
  }, '正在准备棋盘…'));
181
181
  }
182
182
 
183
+ function analysisText(block) {
184
+ if (!block || !Array.isArray(block.content)) return '';
185
+ return block.content
186
+ .filter((part) => part && part.type === 'text' && typeof part.text === 'string')
187
+ .map((part) => part.text)
188
+ .join('\n');
189
+ }
190
+
191
+ function setupCanvas(canvas, width, height) {
192
+ if (!canvas) return null;
193
+ const dpr = window.devicePixelRatio || 1;
194
+ canvas.width = Math.round(width * dpr);
195
+ canvas.height = Math.round(height * dpr);
196
+ canvas.style.width = '';
197
+ canvas.style.height = height + 'px';
198
+ const context = canvas.getContext('2d');
199
+ context.setTransform(dpr, 0, 0, dpr, 0, 0);
200
+ return context;
201
+ }
202
+
203
+ function drawAnalysisBoard(canvas, analysis) {
204
+ if (!canvas || !analysis) return;
205
+ const size = 360;
206
+ const pad = 24;
207
+ const step = (size - pad * 2) / 18;
208
+ const context = setupCanvas(canvas, size, size);
209
+ if (!context) return;
210
+ canvas.style.height = 'auto';
211
+ context.clearRect(0, 0, size, size);
212
+ context.fillStyle = '#f3c78d';
213
+ context.fillRect(0, 0, size, size);
214
+ context.strokeStyle = '#b97b32';
215
+ context.lineWidth = 1;
216
+ context.font = '9px "Segoe UI", "Microsoft YaHei", sans-serif';
217
+ context.fillStyle = '#8a5a22';
218
+ context.textAlign = 'center';
219
+ context.textBaseline = 'middle';
220
+ for (let i = 0; i < 19; i++) {
221
+ const p = pad + i * step;
222
+ context.beginPath();
223
+ context.moveTo(pad, p);
224
+ context.lineTo(size - pad, p);
225
+ context.stroke();
226
+ context.beginPath();
227
+ context.moveTo(p, pad);
228
+ context.lineTo(p, size - pad);
229
+ context.stroke();
230
+ context.fillText(String.fromCharCode(65 + i), p, 12);
231
+ context.fillText(String(i + 1), 11, p);
232
+ }
233
+ for (const [sx, sy] of [[3, 3], [9, 3], [15, 3], [3, 9], [9, 9], [15, 9], [3, 15], [9, 15], [15, 15]]) {
234
+ context.beginPath();
235
+ context.arc(pad + sx * step, pad + sy * step, 2.2, 0, Math.PI * 2);
236
+ context.fill();
237
+ }
238
+ const highlight = new Map();
239
+ for (const item of analysis.bestMoves || []) highlight.set(item.n, '#2f9e44');
240
+ for (const item of analysis.mistakes || []) highlight.set(item.n, '#d9480f');
241
+ const moves = analysis.moves || [];
242
+ for (const item of moves) {
243
+ const px = pad + item.move.x * step;
244
+ const py = pad + item.move.y * step;
245
+ const radius = step * 0.43;
246
+ context.beginPath();
247
+ context.arc(px, py, radius, 0, Math.PI * 2);
248
+ context.fillStyle = item.color === 1 ? '#252525' : '#fdfdfd';
249
+ context.fill();
250
+ context.strokeStyle = item.color === 1 ? '#111111' : '#b7a68f';
251
+ context.lineWidth = 1;
252
+ context.stroke();
253
+ context.fillStyle = item.color === 1 ? '#ffffff' : '#5c4429';
254
+ context.font = 'bold ' + Math.max(7, step * 0.46) + 'px "Segoe UI", sans-serif';
255
+ context.fillText(String(item.n), px, py + 0.5);
256
+ if (highlight.has(item.n)) {
257
+ context.beginPath();
258
+ context.arc(px, py, radius + 2.5, 0, Math.PI * 2);
259
+ context.strokeStyle = highlight.get(item.n);
260
+ context.lineWidth = 2;
261
+ context.stroke();
262
+ }
263
+ }
264
+ if (moves.length) {
265
+ const last = moves[moves.length - 1];
266
+ context.beginPath();
267
+ context.arc(pad + last.move.x * step, pad + last.move.y * step, step * 0.12, 0, Math.PI * 2);
268
+ context.fillStyle = '#f97316';
269
+ context.fill();
270
+ }
271
+ }
272
+
273
+ function localizedMoves(analysis, count, fromVariant) {
274
+ const source = fromVariant || (analysis.moves || []).slice(0, count).map((item) => ({
275
+ x: item.move.x,
276
+ y: item.move.y,
277
+ color: item.color,
278
+ n: item.n,
279
+ }));
280
+ return source;
281
+ }
282
+
283
+ function drawMiniBoard(canvas, moves, focus, recommend, tone, lastN) {
284
+ if (!canvas || !focus) return;
285
+ const size = 240;
286
+ const span = 9;
287
+ const half = Math.floor(span / 2);
288
+ const minX = Math.max(0, Math.min(19 - span, focus.x - half));
289
+ const minY = Math.max(0, Math.min(19 - span, focus.y - half));
290
+ const pad = 22;
291
+ const step = (size - pad * 2) / (span - 1);
292
+ const context = setupCanvas(canvas, size, size);
293
+ if (!context) return;
294
+ canvas.style.height = 'auto';
295
+ context.clearRect(0, 0, size, size);
296
+ context.fillStyle = '#f3c78d';
297
+ context.fillRect(0, 0, size, size);
298
+ context.strokeStyle = '#b97b32';
299
+ context.lineWidth = 1;
300
+ context.font = '8px "Segoe UI", "Microsoft YaHei", sans-serif';
301
+ context.fillStyle = '#8a5a22';
302
+ context.textAlign = 'center';
303
+ context.textBaseline = 'middle';
304
+ for (let i = 0; i < span; i++) {
305
+ const px = pad + i * step;
306
+ const py = pad + i * step;
307
+ context.beginPath();
308
+ context.moveTo(pad, py);
309
+ context.lineTo(size - pad, py);
310
+ context.stroke();
311
+ context.beginPath();
312
+ context.moveTo(px, pad);
313
+ context.lineTo(px, size - pad);
314
+ context.stroke();
315
+ context.fillText(String.fromCharCode(65 + minX + i), px, 10);
316
+ context.fillText(String(minY + i + 1), 10, py);
317
+ }
318
+ for (const [sx, sy] of [[3, 3], [9, 3], [15, 3], [3, 9], [9, 9], [15, 9], [3, 15], [9, 15], [15, 15]]) {
319
+ if (sx < minX || sx >= minX + span || sy < minY || sy >= minY + span) continue;
320
+ context.beginPath();
321
+ context.arc(pad + (sx - minX) * step, pad + (sy - minY) * step, 2, 0, Math.PI * 2);
322
+ context.fill();
323
+ }
324
+ const visible = moves.filter((move) => move.x >= minX && move.x < minX + span && move.y >= minY && move.y < minY + span);
325
+ for (const move of visible) {
326
+ const px = pad + (move.x - minX) * step;
327
+ const py = pad + (move.y - minY) * step;
328
+ const radius = step * 0.42;
329
+ context.beginPath();
330
+ context.arc(px, py, radius, 0, Math.PI * 2);
331
+ context.fillStyle = move.color === 1 ? '#252525' : '#fdfdfd';
332
+ context.fill();
333
+ context.strokeStyle = move.color === 1 ? '#111111' : '#b7a68f';
334
+ context.lineWidth = 1;
335
+ context.stroke();
336
+ context.fillStyle = move.color === 1 ? '#ffffff' : '#5c4429';
337
+ context.font = 'bold ' + Math.max(8, step * 0.4) + 'px "Segoe UI", sans-serif';
338
+ context.fillText(String(move.n), px, py + 0.5);
339
+ }
340
+ if (recommend && (recommend.x !== focus.x || recommend.y !== focus.y)) {
341
+ const rx = pad + (recommend.x - minX) * step;
342
+ const ry = pad + (recommend.y - minY) * step;
343
+ context.save();
344
+ context.setLineDash([3, 2]);
345
+ context.beginPath();
346
+ context.arc(rx, ry, step * 0.58, 0, Math.PI * 2);
347
+ context.strokeStyle = '#2f9e44';
348
+ context.lineWidth = 2;
349
+ context.stroke();
350
+ context.restore();
351
+ context.fillStyle = '#2f9e44';
352
+ context.font = 'bold 9px "Segoe UI", sans-serif';
353
+ context.fillText('荐', rx, ry - step * 0.78);
354
+ }
355
+ const fx = pad + (focus.x - minX) * step;
356
+ const fy = pad + (focus.y - minY) * step;
357
+ context.beginPath();
358
+ context.arc(fx, fy, step * 0.56, 0, Math.PI * 2);
359
+ context.strokeStyle = tone === 'best' ? '#2f9e44' : tone === 'miss' ? '#d9480f' : '#f97316';
360
+ context.lineWidth = 2.5;
361
+ context.stroke();
362
+ if (lastN) {
363
+ context.beginPath();
364
+ context.arc(fx, fy, 2, 0, Math.PI * 2);
365
+ context.fillStyle = '#f97316';
366
+ context.fill();
367
+ }
368
+ }
369
+
370
+ function drawAllMinis(container, analysis) {
371
+ if (!container || !analysis) return;
372
+ const best = (analysis.bestMoves || []).slice(0, 2);
373
+ const mistakes = (analysis.mistakes || []).filter((item) => item.loss >= 4).slice(0, 2);
374
+ const keyItems = [
375
+ ...best.map((item) => ({ ...item, tone: 'best' })),
376
+ ...mistakes.map((item) => ({ ...item, tone: 'miss' })),
377
+ ];
378
+ container.querySelectorAll('canvas[data-mini="key"]').forEach((canvas) => {
379
+ const n = Number(canvas.getAttribute('data-n'));
380
+ const tone = canvas.getAttribute('data-tone');
381
+ const item = keyItems.find((entry) => entry.n === n && entry.tone === tone);
382
+ if (!item) return;
383
+ const moves = localizedMoves(analysis, item.n);
384
+ const focus = { x: item.move.x, y: item.move.y };
385
+ drawMiniBoard(canvas, moves, focus, item.tone === 'miss' ? item.best : null, item.tone, true);
386
+ });
387
+ container.querySelectorAll('canvas[data-mini="variant"]').forEach((canvas) => {
388
+ const index = Number(canvas.getAttribute('data-index'));
389
+ const variant = (analysis.variants || [])[index];
390
+ if (!variant) return;
391
+ const moves = (Array.isArray(variant.moves) && variant.moves.length)
392
+ ? variant.moves
393
+ : decodeBranchMoves(variant.code);
394
+ if (!moves.length) return;
395
+ const last = moves[moves.length - 1];
396
+ drawMiniBoard(canvas, moves, { x: last.x, y: last.y }, null, 'branch', true);
397
+ });
398
+ }
399
+
400
+ function drawAnalysisChart(canvas, analysis) {
401
+ if (!canvas || !analysis || !Array.isArray(analysis.rates) || !analysis.rates.length) return;
402
+ const parentWidth = (canvas.parentElement && canvas.parentElement.clientWidth) || 420;
403
+ const width = Math.max(320, Math.round(parentWidth));
404
+ const height = Math.round(Math.max(180, Math.min(260, width * 0.42)));
405
+ const left = 30;
406
+ const right = 12;
407
+ const top = 10;
408
+ const bottom = 24;
409
+ const context = setupCanvas(canvas, width, height);
410
+ if (!context) return;
411
+ const plotW = width - left - right;
412
+ const plotH = height - top - bottom;
413
+ context.clearRect(0, 0, width, height);
414
+ context.fillStyle = '#fffdf9';
415
+ context.fillRect(0, 0, width, height);
416
+ context.strokeStyle = '#efe2d3';
417
+ context.fillStyle = '#9a826b';
418
+ context.font = '10px "Segoe UI", sans-serif';
419
+ context.textAlign = 'right';
420
+ context.textBaseline = 'middle';
421
+ for (const value of [0, 25, 50, 75, 100]) {
422
+ const y = top + plotH - (value / 100) * plotH;
423
+ context.beginPath();
424
+ context.moveTo(left, y);
425
+ context.lineTo(left + plotW, y);
426
+ context.stroke();
427
+ context.fillText(String(value), left - 5, y);
428
+ }
429
+ context.save();
430
+ context.setLineDash([3, 3]);
431
+ context.strokeStyle = '#d8c4ae';
432
+ const mid = top + plotH / 2;
433
+ context.beginPath();
434
+ context.moveTo(left, mid);
435
+ context.lineTo(left + plotW, mid);
436
+ context.stroke();
437
+ context.restore();
438
+ const rates = analysis.rates;
439
+ const total = Math.max(1, rates[rates.length - 1].n);
440
+ const xOf = (n) => left + (n / total) * plotW;
441
+ const yOf = (v) => top + plotH - (Math.max(0, Math.min(100, v)) / 100) * plotH;
442
+ context.beginPath();
443
+ rates.forEach((point, index) => {
444
+ if (index === 0) context.moveTo(xOf(point.n), yOf(point.black));
445
+ else context.lineTo(xOf(point.n), yOf(point.black));
446
+ });
447
+ context.strokeStyle = '#f97316';
448
+ context.lineWidth = 2;
449
+ context.stroke();
450
+ const last = rates[rates.length - 1];
451
+ context.beginPath();
452
+ context.arc(xOf(last.n), yOf(last.black), 3, 0, Math.PI * 2);
453
+ context.fillStyle = '#f97316';
454
+ context.fill();
455
+ context.fillStyle = '#9a826b';
456
+ context.textAlign = 'left';
457
+ context.textBaseline = 'top';
458
+ context.fillText('0', left, height - bottom + 6);
459
+ context.textAlign = 'right';
460
+ context.fillText(String(total), left + plotW, height - bottom + 6);
461
+ }
462
+
463
+ function copyAnalysisText(value, setCopied) {
464
+ const text = String(value || '');
465
+ if (!text) return;
466
+ const done = () => {
467
+ setCopied(text);
468
+ window.setTimeout(() => setCopied(''), 1200);
469
+ };
470
+ if (navigator.clipboard && typeof navigator.clipboard.writeText === 'function') {
471
+ navigator.clipboard.writeText(text).then(done).catch(() => copyFallback(text, done));
472
+ } else {
473
+ copyFallback(text, done);
474
+ }
475
+ }
476
+
477
+ function copyFallback(text, done) {
478
+ const area = document.createElement('textarea');
479
+ area.value = text;
480
+ area.style.position = 'fixed';
481
+ area.style.opacity = '0';
482
+ document.body.appendChild(area);
483
+ area.select();
484
+ try { document.execCommand('copy'); } catch (_) { /* ignore */ }
485
+ document.body.removeChild(area);
486
+ done();
487
+ }
488
+
489
+ function decodeBranchMoves(code) {
490
+ if (typeof code !== 'string' || code.indexOf('GM1.') !== 0) return [];
491
+ try {
492
+ let body = code.slice(4).replace(/-/g, '+').replace(/_/g, '/');
493
+ const pad = body.length % 4 ? '='.repeat(4 - (body.length % 4)) : '';
494
+ const data = JSON.parse(window.atob(body + pad));
495
+ if (!data || !Array.isArray(data.m)) return [];
496
+ return data.m
497
+ .filter((item) => Array.isArray(item) && item.length >= 3)
498
+ .map((item, index) => ({ x: item[0], y: item[1], color: item[2], n: index + 1 }));
499
+ } catch (_) {
500
+ return [];
501
+ }
502
+ }
503
+
504
+ function AnalysisCard(props) {
505
+ const analysis = props.analysis;
506
+ const boardRef = React.useRef(null);
507
+ const chartRef = React.useRef(null);
508
+ const minisRef = React.useRef(null);
509
+ const [copied, setCopied] = React.useState('');
510
+
511
+ React.useEffect(() => {
512
+ if (!analysis) return;
513
+ drawAnalysisBoard(boardRef.current, analysis);
514
+ drawAnalysisChart(chartRef.current, analysis);
515
+ drawAllMinis(minisRef.current, analysis);
516
+ }, [analysis]);
517
+
518
+ if (!analysis) return null;
519
+
520
+ const best = (analysis.bestMoves || []).slice(0, 3);
521
+ const mistakes = (analysis.mistakes || []).filter((item) => item.loss >= 4).slice(0, 3);
522
+ const variants = analysis.variants || [];
523
+ const keyItems = [
524
+ ...best.slice(0, 2).map((item) => ({ ...item, tone: 'best' })),
525
+ ...mistakes.slice(0, 2).map((item) => ({ ...item, tone: 'miss' })),
526
+ ].sort((a, b) => a.n - b.n);
527
+
528
+ function moveLine(item, tone) {
529
+ return h('div', { className: 'gk-an-move ' + tone, key: tone + item.n },
530
+ h('span', { className: 'gk-an-n' }, '第 ' + item.n + ' 手'),
531
+ h('span', { className: 'gk-an-coord' }, item.colorLabel + ' ' + item.move.coord),
532
+ h('span', { className: 'gk-an-meta' },
533
+ tone === 'best' ? '胜率 ' + item.afterRate + '%' : '建议 ' + (item.best ? item.best.coord : '无') + ' · 损失 ' + item.loss + '%',
534
+ ),
535
+ );
536
+ }
537
+
538
+ return h('div', { className: 'gk-an-card' },
539
+ h('div', { className: 'gk-an-head' },
540
+ h('span', { className: 'gk-an-title' }, '五子棋 · 棋局分析'),
541
+ h('span', { className: 'gk-an-pill' }, analysis.total + ' 手 · ' + analysis.winnerLabel),
542
+ ),
543
+ h('div', { className: 'gk-an-overview' },
544
+ h('span', null, '黑胜率 ' + analysis.finalBlackRate + '%'),
545
+ h('span', null, 'DeepSeek ' + (analysis.review ? '已复核' : '复核失败')),
546
+ ),
547
+ h('div', { className: 'gk-an-main' },
548
+ h('div', { className: 'gk-an-board' }, h('canvas', { ref: boardRef, 'aria-label': '按手数编号的棋局' })),
549
+ h('div', { className: 'gk-an-side' },
550
+ h('div', { className: 'gk-an-section-title' }, '值得肯定的手'),
551
+ best.length ? best.map((item) => moveLine(item, 'best')) : h('p', { className: 'gk-an-empty' }, '未发现明显高分决策'),
552
+ h('div', { className: 'gk-an-section-title' }, '值得复盘的手'),
553
+ mistakes.length ? mistakes.map((item) => moveLine(item, 'miss')) : h('p', { className: 'gk-an-empty' }, '未发现明显失误'),
554
+ ),
555
+ ),
556
+ h('div', { className: 'gk-an-minis', ref: minisRef },
557
+ h('div', { className: 'gk-an-section-title' }, '关键局面 · 局部棋盘'),
558
+ keyItems.length
559
+ ? h('div', { className: 'gk-an-mini-grid' },
560
+ keyItems.map((item) => h('figure', { className: 'gk-an-mini', key: item.tone + '-' + item.n },
561
+ h('canvas', {
562
+ 'data-mini': 'key',
563
+ 'data-n': item.n,
564
+ 'data-tone': item.tone,
565
+ 'aria-label': '第 ' + item.n + ' 手局部棋盘',
566
+ }),
567
+ h('figcaption', null,
568
+ h('strong', null, '第 ' + item.n + ' 手 · ' + item.colorLabel + (item.tone === 'best' ? ' 最佳' : ' 失误')),
569
+ h('span', null, item.tone === 'best'
570
+ ? item.move.coord + ' · 胜率 ' + item.afterRate + '%'
571
+ : '实际 ' + item.move.coord + ' → 推荐 ' + (item.best ? item.best.coord : '无') + '(-' + item.loss + '%)'),
572
+ ),
573
+ )),
574
+ )
575
+ : h('p', { className: 'gk-an-empty' }, '没有可放大的关键局面'),
576
+ ),
577
+ h('div', { className: 'gk-an-chart-wrap' }, h('canvas', { ref: chartRef, 'aria-label': '黑方胜率曲线' })),
578
+ analysis.review
579
+ ? h('p', { className: 'gk-an-review' }, analysis.review)
580
+ : h('p', { className: 'gk-an-review gk-an-warn' }, 'DeepSeek 总评暂缺:' + (analysis.reviewError || '模型未返回文字')),
581
+ h('div', { className: 'gk-an-variants' },
582
+ h('div', { className: 'gk-an-section-title' }, '改进分支(可复制后导入棋谱)'),
583
+ variants.length ? variants.map((variant, index) => {
584
+ const branchMoves = (Array.isArray(variant.moves) && variant.moves.length)
585
+ ? variant.moves
586
+ : decodeBranchMoves(variant.code);
587
+ return h('div', { className: 'gk-an-variant', key: index },
588
+ h('div', { className: 'gk-an-variant-head' },
589
+ h('span', null, variant.label + ' · ' + variant.length + ' 手' + (variant.endedInWin ? '(提前五连)' : '')),
590
+ h('button', {
591
+ className: 'gk-an-copy',
592
+ type: 'button',
593
+ onClick: () => copyAnalysisText(variant.code, setCopied),
594
+ }, copied === variant.code ? '已复制' : '复制棋谱'),
595
+ ),
596
+ h('div', { className: 'gk-an-variant-body' },
597
+ branchMoves.length
598
+ ? h('canvas', {
599
+ 'data-mini': 'variant',
600
+ 'data-index': index,
601
+ 'aria-label': '改进分支终局局部棋盘',
602
+ })
603
+ : null,
604
+ h('code', { className: 'gk-an-code' }, variant.code),
605
+ ),
606
+ );
607
+ }) : h('p', { className: 'gk-an-empty' }, '没有可生成的改进分支'),
608
+ ),
609
+ );
610
+ }
611
+
612
+ function AnalysisToolCard(props) {
613
+ // The tool.call.toolview slot passes ToolCallOwnerProps directly as props:
614
+ // { callId, toolName, phase, block, cwd, home, loadImage, openFile, ... }.
615
+ const block = props && props.block;
616
+ const meta = block && block.meta;
617
+ const analysis = meta && meta.analysis ? meta.analysis : (meta && Array.isArray(meta.moves) ? meta : null);
618
+ // This component only exists where gomoku_analyze was actually called.
619
+ // Do not render a placeholder here: the chat should not show a
620
+ // "thinking / analysing" card, only the finished illustrated analysis.
621
+ if (!analysis) return null;
622
+ return h(AnalysisCard, { analysis });
623
+ }
624
+
183
625
  function apply(ctx) {
626
+ ensureStyle();
184
627
  try {
185
628
  ctx.slots.inject('sidebar.panellist', () =>
186
629
  ctx.slots.register(
@@ -210,6 +653,21 @@ if (!window.__dshGomokuClientLoaded) {
210
653
  } catch (error) {
211
654
  console.warn('[dsh-gomoku] main panel registration failed:', error);
212
655
  }
656
+
657
+ try {
658
+ ctx.slots.inject('tool.call.toolview', () =>
659
+ ctx.slots.register(
660
+ {
661
+ name: 'tool.call.toolview',
662
+ key: 'gomoku_analyze',
663
+ },
664
+ AnalysisToolCard,
665
+ ),
666
+ );
667
+ } catch (error) {
668
+ console.warn('[dsh-gomoku] analysis tool card registration failed:', error);
669
+ }
670
+
213
671
  }
214
672
 
215
673
  return { inject: ['slots'], apply, name: '@wsl233/dsh-gomoku' };
package/index.js CHANGED
@@ -14,6 +14,7 @@ import { readFile } from 'node:fs/promises';
14
14
  import { extname, resolve, sep } from 'node:path';
15
15
  import { fileURLToPath } from 'node:url';
16
16
  import { startLanServer } from './lan-server.js';
17
+ import { analyzeGame } from './analysis.js';
17
18
 
18
19
  export const name = 'dsh-gomoku';
19
20
 
@@ -55,6 +56,51 @@ const MIME = {
55
56
  '.json': 'application/json; charset=utf-8',
56
57
  };
57
58
 
59
+ const GOMOKU_ANALYZE_TOOL = {
60
+ name: 'gomoku_analyze',
61
+ description:
62
+ '分析一盘五子棋。输入必须是 GM1. 开头的棋谱字符串(由五子棋插件的导出按钮生成)。' +
63
+ '返回逐手胜率、双方最佳手与失误手、DeepSeek 总评,以及 3 条可导入的改进分支。' +
64
+ '用户粘贴棋谱并要求“分析这盘棋”时调用。',
65
+ parameters: {
66
+ type: 'object',
67
+ additionalProperties: false,
68
+ required: ['code'],
69
+ properties: {
70
+ code: {
71
+ type: 'string',
72
+ description: '完整的 GM1. 棋谱字符串,例如 GM1.eyJ2IjoxLCJtIjpbWzk...',
73
+ },
74
+ },
75
+ },
76
+ output: {
77
+ schema: {
78
+ type: 'object',
79
+ additionalProperties: false,
80
+ required: ['summary', 'analysis'],
81
+ properties: {
82
+ summary: { type: 'string' },
83
+ analysis: { type: 'object' },
84
+ },
85
+ },
86
+ render(_args, value) {
87
+ return [{ type: 'text', text: value.summary }];
88
+ },
89
+ presentationMeta(_args, value) {
90
+ return { kind: 'gomoku-analysis', analysis: value.analysis };
91
+ },
92
+ },
93
+ async execute(args, exec) {
94
+ if (!args || typeof args.code !== 'string' || !args.code.trim()) {
95
+ throw new Error('请提供 GM1. 开头的五子棋棋谱。');
96
+ }
97
+ return analyzeGame(args.code, {
98
+ llm: llmService,
99
+ signal: exec && exec.signal,
100
+ });
101
+ },
102
+ };
103
+
58
104
  /**
59
105
  * Register the browser-facing routes.
60
106
  * @param {object} ctx - Cordis plugin context.
@@ -80,6 +126,20 @@ export function apply(ctx) {
80
126
  console.warn('[dsh-gomoku] LLM service unavailable:', error);
81
127
  }
82
128
 
129
+ try {
130
+ ctx.inject(['tools'], (toolCtx) => {
131
+ const tools = toolCtx.tools || (typeof toolCtx.get === 'function' ? toolCtx.get('tools') : null);
132
+ if (!tools || typeof tools.register !== 'function') return;
133
+ try {
134
+ toolCtx.effect(() => tools.register(GOMOKU_ANALYZE_TOOL));
135
+ } catch (error) {
136
+ console.warn('[dsh-gomoku] gomoku_analyze registration failed:', error);
137
+ }
138
+ });
139
+ } catch (error) {
140
+ console.warn('[dsh-gomoku] tools service unavailable:', error);
141
+ }
142
+
83
143
  ctx.inject(['webServer', 'connection'], (webCtx) => {
84
144
  const webServer = webCtx.get('webServer');
85
145
  if (!webServer) return;
@@ -240,7 +300,7 @@ async function handleRequest(webCtx, req, res) {
240
300
  sendJson(res, {
241
301
  ok: true,
242
302
  name,
243
- version: '0.3.8',
303
+ version: '0.4.6',
244
304
  phase: 'play',
245
305
  capabilities: {
246
306
  lan: true,
@@ -248,6 +308,7 @@ async function handleRequest(webCtx, req, res) {
248
308
  ai: true,
249
309
  local: true,
250
310
  hint: true,
311
+ analyze: true,
251
312
  review: true,
252
313
  save: true,
253
314
  import: true,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wsl233/dsh-gomoku",
3
- "version": "0.3.10",
4
- "description": "DSH 五子棋插件:大肥鱼算法 / DeepSeek / 局域网 / 本地双人对战",
3
+ "version": "0.4.6",
4
+ "description": "DSH 五子棋插件:算法对战 / DeepSeek / 局域网 / 本地双人对战 / 对话内棋局分析",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "exports": {
@@ -12,15 +12,14 @@
12
12
  },
13
13
  "files": [
14
14
  "index.js",
15
+ "analysis.js",
15
16
  "lan-server.js",
16
- "vendor",
17
17
  "client.js",
18
18
  "cordis.patch.yml",
19
- "ui",
20
- "preview.html",
21
- "assets",
22
- "docs",
23
- "README.md"
19
+ "README.md",
20
+ "ui/**",
21
+ "assets/**",
22
+ "vendor/**"
24
23
  ],
25
24
  "dsh": {
26
25
  "bundle": {
@@ -39,4 +38,3 @@
39
38
  },
40
39
  "license": "MIT"
41
40
  }
42
-