Files
deepseek-harness/packages/experimental/client-ui-forge/src/client/changes-address.ts
T
Yichen Jiang b60e5985ab feat(forge-panel): open the changes view to Client plugins with lenses, annotations, and slots
Provide ctx.forgeChanges, which mints and reads changes addresses, opens a
view in the shown Session, takes the titles of Host lenses, and registers
line annotation sources. The changes toolbar gains a lens menu that keeps
the files a chosen Host lens keeps, folds the hunks it is not about, and
folds into the More menu at level 2 while no lens is chosen; failed,
refused, and interrupted runs show a notice with a retry and a clear.

Five slots open the view to plugins: forge.changes.toolbar,
forge.changes.file.menu, forge.diff.line.annotation (keyed by source),
forge.diff.comment.actions, and forge.change-request.card.actions.
Annotations stack after the comment form, the browser drafts, and the host
threads of their last line, in the column of their side; none draw before
a section comes within reach, and maxAnnotationsPerFile bounds a file.
2026-11-10 14:33:33 +08:00

113 lines
4.3 KiB
TypeScript

/**
* The changes view's tab type: the right-Sidebar view of one Session's local
* changes or of one change request bound to it. Its addresses name the Session
* and the source, `dsh-resource://forge-changes/<session>/local` or
* `dsh-resource://forge-changes/<session>/change/<url>`, each part
* percent-encoded; the source names the tab.
*/
import type { SidebarRightTabDefinition } from '@deepseek-ai/dsh-client-ui-sidebar-right/client'
import type { TranslateNS } from '@deepseek-ai/dsh-client-ui-slots'
import type { SessionId } from '@deepseek-ai/dsh-session/types'
import type { NS } from './locales.ts'
import { numberOf } from './status.ts'
/** Kind of the changes tab. */
export const CHANGES_KIND = 'forge-changes'
/** Registry id of the changes tab type, also the key its body and title register under. */
export const CHANGES_ID = '@deepseek-ai/dsh-experimental-client-ui-forge/changes'
const PREFIX = 'dsh-resource://forge-changes/'
/** What a changes view shows. */
export type ChangesSource =
/** The changes of the Session's checkout. */
| { readonly kind: 'local' }
/** The changes of one change request bound to the Session, by its binding URL. */
| { readonly kind: 'change'; readonly url: string }
/** The Session and the source one address names. */
export interface ChangesTarget {
/** Session whose checkout or binding the view reads. */
readonly sessionId: SessionId
/** What the view shows. */
readonly source: ChangesSource
}
/** Navigation parameters of a changes tab. */
export interface ChangesParams {
/** File to scroll to, by its path in the diff. */
readonly path?: string
/** Identity of the filter lens to choose. */
readonly lens?: string
}
declare module '@deepseek-ai/dsh-client-ui-sidebar-right/client' {
interface SidebarRightResourceParamsMap {
/** The file a changes tab scrolls to and the lens it chooses. */
'forge-changes': ChangesParams
}
}
/**
* Mint the address of a changes view.
* @param target - Session and source.
* @returns the `dsh-resource://forge-changes/…` address.
*/
export function changesAddress(target: ChangesTarget): string {
const session = `${PREFIX}${encodeURIComponent(target.sessionId)}/`
return target.source.kind === 'local' ? `${session}local` : `${session}change/${encodeURIComponent(target.source.url)}`
}
/**
* Read the Session and source back out of a changes address.
* @param address - a resource address.
* @returns the target, or undefined for an address this package did not mint.
*/
export function parseChangesAddress(address: string): ChangesTarget | undefined {
if (!address.startsWith(PREFIX)) return undefined
const [session = '', kind, url, ...rest] = address.slice(PREFIX.length).split('/')
if (session === '' || rest.length > 0) return undefined
try {
const sessionId = decodeURIComponent(session) as SessionId
if (kind === 'local' && url === undefined) return { sessionId, source: { kind: 'local' } }
if (kind === 'change' && url !== undefined && url !== '') return { sessionId, source: { kind: 'change', url: decodeURIComponent(url) } }
return undefined
} catch (error) {
void error // A malformed percent sequence is not an address this package minted.
return undefined
}
}
/**
* Name a changes view: 本地变更 for the local changes, #N 的变更 for a change request with a number.
* @param source - what the view shows.
* @param t - forge panel copy.
* @returns the title in the current language.
*/
export function changesTitle(source: ChangesSource, t: TranslateNS<typeof NS>): string {
if (source.kind === 'local') return t('changes.local.title')
const number = numberOf({ ref: null, url: source.url })
return number === null ? t('diff.tab') : t('diff.tabNumbered', { number })
}
/**
* The changes type's registry definition.
* @param t - forge panel copy, read on every title call.
* @returns the definition to register.
*/
export function changesDefinition(t: TranslateNS<typeof NS>): SidebarRightTabDefinition {
return {
id: CHANGES_ID,
kind: CHANGES_KIND,
keepMounted: true,
patterns: [`${PREFIX}**`],
canOpen: address => parseChangesAddress(address) !== undefined,
title: (address) => {
const target = parseChangesAddress(address)
return target === undefined ? address : changesTitle(target.source, t)
},
}
}