dsh-remote-vs 0.15.1 → 0.16.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.
@@ -30,6 +30,11 @@
30
30
  import { useEffect, useMemo, useRef, useState, type KeyboardEvent, type MouseEvent, type ReactNode } from 'react'
31
31
  import type { GitCommit, GitCommitFile } from '../../protocol.ts'
32
32
  import type { Lang } from '../i18n.ts'
33
+ import { buildCommitReviewPrompt, diffByteLength, pickReviewMode } from '../ai-review.ts'
34
+ import { appendToDraft, canInsertDraft, sameLocalDir } from '../composer.ts'
35
+ import { GIT_CAP } from '../../git-ops.ts'
36
+ import { useExplorer } from '../store.tsx'
37
+ import { useRemoteChatTarget } from '../use-remote-chat-target.ts'
33
38
  import {
34
39
  ContextMenu,
35
40
  FileIcon,
@@ -474,6 +479,61 @@ function DiffTab(props: CommitPaneProps & { hidden: boolean; wrap: boolean; onTo
474
479
  }
475
480
  const blockRefs = useRef(new Map<string, HTMLElement>())
476
481
  const menu = useContextMenu()
482
+
483
+ // ------------------------------------------------- AI review (whole commit)
484
+ /**
485
+ * Hand THIS commit to the on-screen session as one review prompt (F6 v2).
486
+ *
487
+ * Same delivery rule as the single-file button in `DiffView`: draft only,
488
+ * never sent, and the same three reasons a click is refused. The patch is
489
+ * ALREADY in memory here (the 差异 tab renders with it), so this costs no
490
+ * extra remote round trip; `patchTruncated` comes from the loader that
491
+ * computed the byte cap — the one place that knows the read was cut.
492
+ */
493
+ const store = useExplorer()
494
+ const chat = useRemoteChatTarget(store.ctx)
495
+ const viewedAliasPath = store.workspace === undefined ? undefined : store.aliasPaths[store.workspace.id]
496
+ const chatEnabled = chat.sessionId !== undefined
497
+ && chat.sessionWorkspacePath !== undefined
498
+ && viewedAliasPath !== undefined
499
+ && sameLocalDir(chat.sessionWorkspacePath, viewedAliasPath)
500
+ let reviewBlocked: string | undefined
501
+ if (store.ctx === undefined) reviewBlocked = t('scm.aiReviewNoContext')
502
+ else if (!chatEnabled || chat.sessionId === undefined) reviewBlocked = t('scm.aiReviewNoSession')
503
+ else if (!canInsertDraft(store.ctx, chat.sessionId)) reviewBlocked = t('scm.aiReviewNoChannel')
504
+ else if (props.patch === undefined || props.patch === '') reviewBlocked = t('history.aiReviewNoPatch')
505
+ const reviewCommit = (): void => {
506
+ const ctx = store.ctx
507
+ const sessionId = chat.sessionId
508
+ if (reviewBlocked !== undefined || ctx === undefined || sessionId === undefined) {
509
+ toast(reviewBlocked ?? t('scm.aiReviewNoSession'), true)
510
+ return
511
+ }
512
+ const patch = props.patch ?? ''
513
+ // Bytes, not `.length`: the patch is a capped BYTE read (see `ai-review.ts`).
514
+ const bytes = diffByteLength(patch)
515
+ const capped = props.patchTruncated || bytes >= GIT_CAP
516
+ const mode = pickReviewMode({ bytes, hasDiff: patch !== '', capped })
517
+ const prompt = buildCommitReviewPrompt({
518
+ alias: store.alias,
519
+ root: store.repoRoot ?? store.root,
520
+ hash: props.commit.hash,
521
+ subject: props.commit.subject,
522
+ body: props.body,
523
+ files: props.files ?? [],
524
+ patch,
525
+ mode,
526
+ capped,
527
+ lang: props.lang,
528
+ })
529
+ if (!appendToDraft(ctx, sessionId, prompt)) {
530
+ toast(t('scm.aiReviewFailed'), true)
531
+ return
532
+ }
533
+ if (mode === 'full') toast(t('scm.aiReviewAdded'))
534
+ else if (mode === 'summary') toast(t('scm.aiReviewAddedSummary'))
535
+ else toast(t('scm.aiReviewAddedRead'))
536
+ }
477
537
  const resolvedFocus = props.focusPath === undefined ? undefined : blockForPath(model, props.focusPath)
478
538
  const currentPath = resolvedFocus ?? paths[0]
479
539
  const currentIndex = currentPath === undefined ? -1 : paths.indexOf(currentPath)
@@ -558,6 +618,17 @@ function DiffTab(props: CommitPaneProps & { hidden: boolean; wrap: boolean; onTo
558
618
  >
559
619
  <IconCopy size={13} />
560
620
  </IconButton>
621
+ {/* F6 v2: review the WHOLE commit (every file in one prompt) — the
622
+ history pane's counterpart of the single-file button in `DiffView`.
623
+ Text glyph for the same reason as there: the icon set has no "AI"
624
+ mark, and the `Aa` / `.*` marks set the precedent. */}
625
+ <IconButton
626
+ title={reviewBlocked ?? t('history.aiReview')}
627
+ onClick={reviewCommit}
628
+ disabled={reviewBlocked !== undefined}
629
+ >
630
+ <span className="rvs-glyph" aria-hidden="true" style={{ fontSize: 12 }}>AI</span>
631
+ </IconButton>
561
632
  </div>
562
633
  <div className="rvs-pscroll rvs-scroll">
563
634
  {diffBody(props, model, blocks, props.wrap, folded, toggleFold, register, openBlockMenu)}
@@ -22,9 +22,11 @@ import { useExplorer } from '../store.tsx'
22
22
  import { applyHunk, GitOpError } from './actions.ts'
23
23
  import { diffCounts, hunkAtRow, stepHunk } from './diff-model.ts'
24
24
  import type { DiffRow } from '../../protocol.ts'
25
+ import { GIT_CAP } from '../../git-ops.ts'
25
26
  import { api } from '../api.ts'
26
27
  import { CodeEditor, type CodeEditorHandle } from '../CodeEditor.tsx'
27
- import { sameLocalDir } from '../composer.ts'
28
+ import { buildReviewPrompt, diffByteLength, pickReviewMode, reviewScopeOf } from '../ai-review.ts'
29
+ import { appendToDraft, canInsertDraft, sameLocalDir } from '../composer.ts'
28
30
  import { useRemoteChatTarget } from '../use-remote-chat-target.ts'
29
31
  import { langFromPath, tokenizeDocument, type Token } from '../vsc/tokenize.ts'
30
32
  import {
@@ -421,6 +423,26 @@ export function DiffView(props: { tab: EditorTab; visible: boolean; offline?: bo
421
423
  && viewedAliasPath !== undefined
422
424
  && sameLocalDir(chat.sessionWorkspacePath, viewedAliasPath)
423
425
 
426
+ /**
427
+ * Why the "AI review" button cannot run, or `undefined` when it can.
428
+ *
429
+ * Three different absences, three different sentences, checked from the most
430
+ * fundamental down:
431
+ * - `store.ctx`: the shared store boots asynchronously, so before it lands
432
+ * there is nothing to write into at all;
433
+ * - `chatEnabled`: the same binding gate the selection popup uses — a prompt
434
+ * that names a remote path means nothing in a session that does not live in
435
+ * this binding;
436
+ * - `canInsertDraft`: the conversation service is a SOFT, version-dependent
437
+ * read. A host that does not expose it must say so up front instead of
438
+ * failing on click with the "composer is busy" sentence, which would name
439
+ * the wrong cause.
440
+ */
441
+ let reviewBlocked: string | undefined
442
+ if (store.ctx === undefined) reviewBlocked = t('scm.aiReviewNoContext')
443
+ else if (!chatEnabled || chat.sessionId === undefined) reviewBlocked = t('scm.aiReviewNoSession')
444
+ else if (!canInsertDraft(store.ctx, chat.sessionId)) reviewBlocked = t('scm.aiReviewNoChannel')
445
+
424
446
  // Seed the editable buffer the first time the "变更后" pane is shown.
425
447
  useEffect(() => {
426
448
  if (mode !== 'after' || tab.staged || tab.text !== '' || tab.loading) return
@@ -514,6 +536,56 @@ export function DiffView(props: { tab: EditorTab; visible: boolean; offline?: bo
514
536
  await store.reloadTab(tab.id)
515
537
  }
516
538
 
539
+ /**
540
+ * Append an AI-review prompt for THIS tab's diff to the composer draft (F6).
541
+ *
542
+ * v1 stops at the DRAFT on purpose: `appendToDraft` is the only delivery
543
+ * channel used here — no `submit`/`send` call — so the user always sees the
544
+ * exact prompt (and can edit or discard it) before anything costs a turn. The
545
+ * host does expose submit facades (see `docs/.feasibility/review.md` §2);
546
+ * wiring them is a separate, opt-in feature.
547
+ *
548
+ * The prompt is built on CLICK, not during render: `diffByteLength` encodes
549
+ * the whole diff and the prompt can be tens of kilobytes, which must not be
550
+ * recomputed per frame while the user scrolls the diff.
551
+ */
552
+ const reviewWithAi = (): void => {
553
+ const ctx = store.ctx
554
+ const sessionId = chat.sessionId
555
+ if (reviewBlocked !== undefined || ctx === undefined || sessionId === undefined) {
556
+ toast(reviewBlocked ?? t('scm.aiReviewNoSession'), true)
557
+ return
558
+ }
559
+ // Byte accounting, not `diff.length`: `GIT_CAP` is a `head -c` BYTE cap and
560
+ // the exec result does not flag the truncation (see `ai-review.ts`).
561
+ const diff = tab.diff?.diff ?? ''
562
+ const bytes = diffByteLength(diff)
563
+ const capped = bytes >= GIT_CAP
564
+ const mode = pickReviewMode({ bytes, hasDiff: diff !== '', capped })
565
+ const prompt = buildReviewPrompt({
566
+ alias: store.alias,
567
+ root: store.repoRoot ?? store.root,
568
+ rel: tab.rel,
569
+ fullPath: tab.fullPath,
570
+ scope: reviewScopeOf(tab.staged, tab.ref),
571
+ ...(tab.ref === undefined ? {} : { commitRef: tab.ref }),
572
+ diff,
573
+ mode,
574
+ capped,
575
+ lang,
576
+ })
577
+ if (!appendToDraft(ctx, sessionId, prompt)) {
578
+ toast(t('scm.aiReviewFailed'), true)
579
+ return
580
+ }
581
+ // One sentence per shape, because the three mean different things and the
582
+ // "large diff" wording would be a lie for a file that has no diff at all
583
+ // (untracked / new: the prompt asks the model to read the file itself).
584
+ if (mode === 'full') toast(t('scm.aiReviewAdded'))
585
+ else if (mode === 'summary') toast(t('scm.aiReviewAddedSummary'))
586
+ else toast(t('scm.aiReviewAddedRead'))
587
+ }
588
+
517
589
  if (tab.error !== undefined) {
518
590
  return (
519
591
  <div className="rvs-diff">
@@ -578,6 +650,18 @@ export function DiffView(props: { tab: EditorTab; visible: boolean; offline?: bo
578
650
  </span>
579
651
  )}
580
652
  {busy && <MiniSpinner />}
653
+ {/* F6: hand THIS diff to the on-screen session as a review prompt. It
654
+ only fills the composer draft (see `reviewWithAi`); the title carries
655
+ the exact reason whenever the binding gate refuses. The glyph is
656
+ text because the icon set has no "AI" mark — the same precedent as
657
+ the search options' `Aa` / `.*` glyphs in `kit.tsx`. */}
658
+ <IconButton
659
+ title={reviewBlocked ?? t('scm.aiReview')}
660
+ onClick={reviewWithAi}
661
+ disabled={reviewBlocked !== undefined}
662
+ >
663
+ <span className="rvs-glyph" aria-hidden="true" style={{ fontSize: 12 }}>AI</span>
664
+ </IconButton>
581
665
  <IconButton title={t('scm.reloadDiff')} onClick={() => { void reload() }} disabled={busy}>
582
666
  <IconRefresh size={13} />
583
667
  </IconButton>