From aa01de6edeb63be6f05b4517029d858f897b2e21 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 12 Nov 2026 23:10:58 +0900 Subject: [PATCH] refac --- src/lib/components/chat/Chat.svelte | 11 + src/lib/components/chat/Tabs.svelte | 2 + src/lib/components/chat/Tabs/GitBar.svelte | 3 + src/lib/components/chat/Tabs/GitIssues.svelte | 90 +++++-- .../chat/Tabs/GitPullRequests.svelte | 237 ++++++++++++------ src/lib/components/chat/Tabs/gitPanel.ts | 152 +++++++++++ 6 files changed, 397 insertions(+), 98 deletions(-) diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index cef3e3d586..ce21dadc09 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -612,6 +612,16 @@ } }; + // Adds text to the composer for the user to review, keeping any draft already there. + const draftPrompt = async (text: string) => { + if (fullWidthTabs) { + terminalTabs?.focusMainChat(); + await tick(); + } + messageInput?.setText(prompt.trim() ? `${prompt.trimEnd()}\n\n${text}` : text); + messageInput?.focus({ preventScroll: true }); + }; + let params = {}; let chatVariables = {}; let showChatVariablesModal = false; @@ -6142,6 +6152,7 @@ systemPrompt={params?.system || $settings.system || ''} submitPrompt={submitHandler} onAskFile={askFile} + onDraftPrompt={draftPrompt} {stopResponse} {bridge} bind:callMode diff --git a/src/lib/components/chat/Tabs.svelte b/src/lib/components/chat/Tabs.svelte index 7bbe7143cb..7bcf3e07d3 100644 --- a/src/lib/components/chat/Tabs.svelte +++ b/src/lib/components/chat/Tabs.svelte @@ -141,6 +141,7 @@ export let onAskFile: | ((instruction: string, file: import('$lib/apis/terminal').FilesystemFile) => Promise) | null = null; + export let onDraftPrompt: ((text: string) => void) | null = null; export let stopResponse: Function = () => {}; export let showMessage: Function = () => {}; export let files: any[] = []; @@ -2189,6 +2190,7 @@ openFile(path, tab.terminalId!, null, fileTarget?.context.modelId)} onOpenDirectory={(path) => openFiles(tab.terminalId, path, false, fileTarget?.context.modelId)} + {onDraftPrompt} /> {/if} diff --git a/src/lib/components/chat/Tabs/GitBar.svelte b/src/lib/components/chat/Tabs/GitBar.svelte index bcc3877af3..de4ff91daa 100644 --- a/src/lib/components/chat/Tabs/GitBar.svelte +++ b/src/lib/components/chat/Tabs/GitBar.svelte @@ -29,6 +29,7 @@ export let chatId: string | null = null; export let active = false; export let onOpenFile: (path: string) => void = () => {}; + export let onDraftPrompt: ((text: string) => void) | null = null; export let onOpenDirectory: (path: string) => void = () => {}; const i18n = getContext('i18n'); export let status: GitStatus | null = null; @@ -837,6 +838,7 @@ branch={status.branch} defaultBranch={prCapabilities?.default_branch ?? 'main'} onCount={(count) => (openPullRequests = count)} + onDraft={onDraftPrompt} onChange={() => { void refresh(true); }} @@ -849,6 +851,7 @@ {target} directory={status.root} onCount={(count) => (openIssues = count)} + onDraft={onDraftPrompt} />{/key} {:else}
void = () => {}; export let onCount: (count: number) => void = () => {}; + // Drafts text into the chat composer; the chat actions are hidden without it. + export let onDraft: ((text: string) => void) | null = null; type Issue = { number: number; title: string; @@ -68,6 +78,23 @@ const people = (list?: { login?: string; name?: string }[]) => list?.map((person) => person.login || person.name).join(', ') || $i18n.t('None'); + const copy = async (text: string) => { + if (!(await copyToClipboard(text))) error = $i18n.t('Could not copy to clipboard'); + }; + const commentPrompt = (issue: Issue, item: NonNullable[number]) => + [ + $i18n.t('Comment from @{{author}} on issue #{{number}} ({{title}}):', { + author: item.author?.login ?? 'unknown', + number: issue.number, + title: issue.title + }), + item.body + .trim() + .split('\n') + .map((line) => `> ${line}`) + .join('\n') + ].join('\n\n'); + const request = ( operation: string, params: Record = {}, @@ -255,6 +282,11 @@ ? `#${selected.number} · ${selected.title}` : $i18n.t('New issue')} + {#if onDraft && selected && !creating && !editing && !loading}{/if} {#if selected && !creating && !editing}
{/if} @@ -367,32 +402,51 @@ > {$i18n.t('Description')} -
- {selected.body || $i18n.t('No description')} -
+ {#if selected.body?.trim()}
+ +
{:else}
+ {$i18n.t('No description')} +
{/if} {#if selected.comments?.length}
{$i18n.t('Comments')}
- {#each selected.comments as item}
-
- {item.author?.login ?? $i18n.t('Unknown')}{item.createdAt - ? ` · ${relativeTime(item.createdAt)}` - : ''} +
+ {item.author?.login ?? $i18n.t('Unknown')}{item.createdAt + ? ` · ${relativeTime(item.createdAt)}` + : ''}{#if onDraft && item.body?.trim()}{/if}
-

- {item.body} -

+ +
{/each}{/if}
void = () => {}; export let onChange: () => void = () => {}; export let onCount: (count: number) => void = () => {}; + // Drafts text into the chat composer; the chat actions are hidden without it. + export let onDraft: ((text: string) => void) | null = null; type PullRequest = { number: number; title: string; @@ -39,13 +59,8 @@ author?: { login: string }; comments?: { id: string; body: string; author?: { login: string } }[]; reviews?: { id: string; body: string; state: string; author?: { login: string } }[]; - statusCheckRollup?: { - name?: string; - context?: string; - status?: string; - conclusion?: string; - state?: string; - }[]; + statusCheckRollup?: Check[]; + updatedAt?: string; }; const i18n = getContext('i18n'); let prs: PullRequest[] = []; @@ -76,6 +91,7 @@ $: states = [ { value: 'open', label: $i18n.t('Open') }, { value: 'closed', label: $i18n.t('Closed') }, + { value: 'merged', label: $i18n.t('Merged') }, { value: 'all', label: $i18n.t('All') } ]; $: tabs = [ @@ -100,6 +116,12 @@ `${pr.number} ${pr.title}`.toLowerCase().includes(search.toLowerCase()) ); $: showingDetail = !!selected || creating || editing; + $: failingChecks = (selected?.statusCheckRollup ?? []).filter( + (check) => checkOutcome(check) === 'fail' + ); + $: feedback = [...(selected?.comments ?? []), ...(selected?.reviews ?? [])].filter((item) => + item.body?.trim() + ); const label = (value = '') => ({ @@ -116,23 +138,26 @@ })[value] ?? value.toLowerCase().replaceAll('_', ' '); const people = (list?: { login?: string; name?: string }[]) => list?.map((person) => person.login || person.name).join(', ') || $i18n.t('None'); - const checkResult = (check: NonNullable[number]) => - (check.conclusion || check.status || check.state || '').toUpperCase(); - const checkTone = (result: string) => - result === 'SUCCESS' - ? 'bg-green-500 dark:bg-green-400' - : [ - 'FAILURE', - 'ERROR', - 'CANCELLED', - 'TIMED_OUT', - 'ACTION_REQUIRED', - 'STARTUP_FAILURE' - ].includes(result) - ? 'bg-red-500 dark:bg-red-400' - : ['NEUTRAL', 'SKIPPED', 'STALE'].includes(result) - ? 'bg-gray-300 dark:bg-gray-600' - : 'bg-yellow-500 dark:bg-yellow-400'; + + const rowDetails = (pr: PullRequest) => + [ + `#${pr.number}`, + pr.author?.login, + pr.isDraft ? $i18n.t('Draft') : pr.state !== 'OPEN' ? label(pr.state) : '', + pr.state === 'OPEN' && pr.mergeable === 'CONFLICTING' ? $i18n.t('Conflicts') : '', + pr.state === 'OPEN' && ['APPROVED', 'CHANGES_REQUESTED'].includes(pr.reviewDecision ?? '') + ? label(pr.reviewDecision) + : '', + pr.updatedAt ? relativeTime(pr.updatedAt) : '' + ] + .filter(Boolean) + .join(' · '); + const checksTitle = (summary: NonNullable>) => + $i18n.t('Checks: {{failed}} failing, {{pending}} pending, {{passed}} passing', summary); + const copy = async (text: string) => { + if (!(await copyToClipboard(text))) error = $i18n.t('Could not copy to clipboard'); + }; + const sendToChat = (text: string) => onDraft?.(text); const request = ( operation: string, @@ -307,19 +332,22 @@ {listError} {/if}
- {#each filtered as pr (pr.number)}{/each} {#if !filtered.length}
+ {#if onDraft && selected?.state === 'OPEN' && !creating && !editing && (failingChecks.length || feedback.length)}{/if} {#if selected && !creating && !editing} +
{/if} @@ -531,13 +571,19 @@ > {$i18n.t('Description')}
-
- {selected.body || $i18n.t('No description')} -
+ {#if selected.body?.trim()}
+ +
{:else}
+ {$i18n.t('No description')} +
{/if} {#if selected.state === 'OPEN'}
mutate('ready')}>{$i18n.t('Mark ready')}{:else} {@const blocked = busy || selected.mergeable === 'CONFLICTING'} - - - selected && sendToChat(conflictPrompt($i18n.t, selected))} + >{$i18n.t('Resolve conflicts in chat')}{:else} -
- - {#each mergeMethods as method}{/each} - -
-
+ + +
+ + {#each mergeMethods as method}{/each} + +
+
{/if} {/if}
{:else if selected.state === 'CLOSED'}
{/if} {:else if view === 'checks'}
{#each selected.statusCheckRollup ?? [] as check} - {@const result = checkResult(check)} + {@const outcome = checkOutcome(check)} + {@const link = checkLink(check)}
- {check.name ?? check.context}{result.toLowerCase().replaceAll('_', ' ')}{#if link}{checkName(check)}{:else}{checkName(check)}{/if} + {#if onDraft && outcome === 'fail' && selected.state === 'OPEN'}{/if}{checkResult(check)}
{/each} @@ -612,19 +676,32 @@ ...(selected.reviews ?? []) ]}
- {#each activity as item}
-
- {item.author?.login ?? $i18n.t('Unknown')}{item.state - ? ` · ${label(item.state)}` - : ''} +
+ {item.author?.login ?? $i18n.t('Unknown')}{item.state + ? ` · ${label(item.state)}` + : ''}{#if onDraft && item.body?.trim()}{/if}
- {#if item.body}

- {item.body} -

{/if} + +
{/if}
{/each} {#if !activity.length}
{ // Pull request and issue lists return at most this many items. export const LIST_LIMIT = 50; export const countLabel = (count: number) => (count >= LIST_LIMIT ? `${LIST_LIMIT}+` : `${count}`); + +// Checks come from `pr/checks` (bucket, state, link) and from `pr/view` / `pr/list` +// (check runs: conclusion, status, detailsUrl; status contexts: context, state, targetUrl). +export type Check = { + name?: string; + context?: string; + bucket?: string; + status?: string; + conclusion?: string; + state?: string; + link?: string; + detailsUrl?: string; + targetUrl?: string; +}; +export type CheckOutcome = 'pass' | 'fail' | 'pending' | 'skip'; + +export const checkName = (check: Check) => check.name || check.context || ''; +export const checkLink = (check: Check) => check.link || check.detailsUrl || check.targetUrl || ''; +export const checkResult = (check: Check) => + (check.conclusion || check.state || check.status || check.bucket || '') + .toLowerCase() + .replaceAll('_', ' '); +export const checkOutcome = (check: Check): CheckOutcome => { + const bucket = check.bucket?.toLowerCase(); + if (bucket) + return bucket === 'pass' + ? 'pass' + : bucket === 'fail' + ? 'fail' + : bucket === 'pending' + ? 'pending' + : 'skip'; + const result = (check.conclusion || check.state || check.status || '').toUpperCase(); + if (result === 'SUCCESS') return 'pass'; + if (['FAILURE', 'ERROR', 'TIMED_OUT', 'ACTION_REQUIRED', 'STARTUP_FAILURE'].includes(result)) + return 'fail'; + if (['NEUTRAL', 'SKIPPED', 'STALE', 'CANCELLED'].includes(result)) return 'skip'; + return 'pending'; +}; +export const outcomeTone: Record = { + pass: 'bg-green-500 dark:bg-green-400', + fail: 'bg-red-500 dark:bg-red-400', + pending: 'bg-yellow-500 dark:bg-yellow-400', + skip: 'bg-gray-300 dark:bg-gray-600' +}; +export const checksSummary = (checks?: Check[]) => { + if (!checks?.length) return null; + const outcomes = checks.map(checkOutcome); + const failed = outcomes.filter((outcome) => outcome === 'fail').length; + const pending = outcomes.filter((outcome) => outcome === 'pending').length; + const passed = outcomes.filter((outcome) => outcome === 'pass').length; + const outcome: CheckOutcome = failed ? 'fail' : pending ? 'pending' : passed ? 'pass' : 'skip'; + return { outcome, failed, pending, passed }; +}; + +// Prompts the Git panel drafts into the chat composer. The user reviews them before sending. +type Translate = (key: string, vars?: Record) => string; +type Author = { login?: string } | undefined; +type Note = { body: string; author?: Author; state?: string }; + +const quote = (text: string) => + text + .trim() + .split('\n') + .map((line) => `> ${line}`) + .join('\n'); +const notes = (items: Note[]) => + items + .filter((item) => item.body?.trim()) + .map((item) => `@${item.author?.login ?? 'unknown'}:\n${quote(item.body)}`); + +export const issuePrompt = ( + t: Translate, + issue: { number: number; title: string; url: string; body?: string; comments?: Note[] } +) => + [ + t('Work on issue #{{number}}: {{title}}', issue), + issue.url, + issue.body?.trim() ? quote(issue.body) : '', + ...(issue.comments?.some((item) => item.body?.trim()) + ? [t('Comments on the issue:'), ...notes(issue.comments)] + : []), + t('Create a branch for it, make the change, and summarize what you changed.') + ] + .filter(Boolean) + .join('\n\n'); + +type PullRequestRef = { number: number; title: string; url: string; headRefName: string }; + +export const checkPrompt = (t: Translate, pr: PullRequestRef, check: Check) => + [ + t('The check "{{check}}" is failing on pull request #{{number}} ({{title}}).', { + check: checkName(check), + number: pr.number, + title: pr.title + }), + checkLink(check), + t( + 'Read its log, find out why it fails, and fix it on the branch {{branch}}. Then summarize the fix.', + { branch: pr.headRefName } + ) + ] + .filter(Boolean) + .join('\n\n'); + +export const notePrompt = (t: Translate, pr: PullRequestRef, item: Note) => + [ + t('Feedback from @{{author}} on pull request #{{number}} ({{title}}):', { + author: item.author?.login ?? 'unknown', + number: pr.number, + title: pr.title + }), + quote(item.body) + ].join('\n\n'); + +export const feedbackPrompt = ( + t: Translate, + pr: PullRequestRef, + failing: Check[], + feedback: Note[] +) => + [ + t( + 'Address the open feedback on pull request #{{number}} ({{title}}) on the branch {{branch}}.', + { + number: pr.number, + title: pr.title, + branch: pr.headRefName + } + ), + pr.url, + ...(failing.length + ? [ + t('Failing checks:'), + failing + .map( + (check) => `- ${checkName(check)}${checkLink(check) ? `: ${checkLink(check)}` : ''}` + ) + .join('\n') + ] + : []), + ...(feedback.some((item) => item.body?.trim()) + ? [t('Review comments:'), ...notes(feedback)] + : []), + t('Fix what is still relevant, then summarize what you changed and what you left alone.') + ].join('\n\n'); + +export const conflictPrompt = (t: Translate, pr: PullRequestRef & { baseRefName: string }) => + t( + 'Pull request #{{number}} ({{title}}) has merge conflicts with {{base}}. On the branch {{branch}}, merge {{base}} in, resolve the conflicts, run the tests, and push. Then summarize how you resolved them.', + { number: pr.number, title: pr.title, base: pr.baseRefName, branch: pr.headRefName } + );