Files
apes-sidebar/src/client/conversation-draft.ts
T
Menghuan1918 5ee81fb073 feat(clues): collect resources from files, Feishu and GitHub, adapted to DSH 0.2.2-rc.2
This lands the clue collection line on top of origin/dev (v0.32.1, DSH
0.2.2-rc.2). The 14 commits that carried it are squashed into this one
because every one of them overlapped upstream's own changes (version,
19 dictionaries, CHANGELOG), and each intermediate commit would have
failed to compile against rc.2's menu API. The pre-integration head is
kept at backup/clues-uiux-refresh-pre-rebase.

What the feature is:
- a Clues tab that reads local files, Feishu tasks/docs and GitHub
  issues/pulls through one source registry, with a scope row, shared
  search, filters, paging and stable [[dsh-clue:...]] references;
- one window host for detail, pack management, source configuration,
  new pack and sharing (preview and edit are separate paths: a read is
  never saved over);
- .cluepack export/import with per-row resolvability previews;
- a source runtime that owns dependency installs and connection
  authorization (gh, lark-cli device code) and only probes on an
  explicit read;
- the public betterSidebarClues service and the windows capability
  (features gains clues / windowHost).

Merged with upstream rather than over it:
- the settings section keeps upstream's start-page takeover group and
  this branch's clue sources group; navigation is now six groups;
- the dictionaries keep both sides' new keys (1211 each, verified equal
  across all 19), the external-plugin guide and CHANGELOG keep both
  histories, and service features list both capability sets.

Adapted to 0.2.2-rc.2:
- menu call sites use anchorPoint instead of getAnchorRect;
- the shared SelectMenu mounts the host menu only while open, because
  rc.2's Menu reads the DOM during render and broke server-rendered
  settings surfaces;
- the submenu flip workaround stays deleted (the host positions
  submenus now), pinned by an updated layout-css assertion.

Version 0.33.0 (three synced spots) with a new 未发布 / Unreleased
changelog section; package.json regains the ./clues export, lib/clues.js
and the uqr dependency that upstream never had.

Gates: typecheck, lint, 5410 unit tests, build, check:consumer-types and
the full mount lane (35 passed) against dsh 0.2.2-rc.2 in Chromium.
2026-11-24 16:43:26 +08:00

453 lines
20 KiB
TypeScript

/**
* Insert text into the current session's composer draft through the
* conversation service — the shared path behind the explorer's @-reference
* button and the viewer selection popup. The service is resolved lazily
* through `ctx.get` (the inject-free read the app's own plugins use); a
* missing service or scope degrades to a logged no-op, never a crash.
*
* Structured references (file chips, selection chips, and any append onto a
* draft that already carries chips) go through the host's span channels
* instead of plain draft text: `setDraft` rebuilds the document from a string
* and flattens every chip. A reference chip mints through
* `SidebarSessionInput.insertReference` (what the native `@` picker
* calls); plain-text insertion onto a chipped draft has no facade method and
* emits `slash/input-insert-text` directly. Plain `setDraft` is the fallback
* for chipless drafts only.
*
* Everything appends at the document end. The host composer is a Lexical
* contenteditable and this face publishes no caret, so a mid-draft insert
* would have to guess a position — see {@link appendOffset} for the one
* coordinate it does derive.
*/
import type {
Context,
SidebarConversation,
SidebarReferenceInsert,
SidebarSessionInput,
SidebarSessionOccurrence,
} from '../context-types.ts'
import { baseName, workspaceRelativePath } from './paths.ts'
/** Public host payload/span mirrors used by clue codecs. */
export type ComposerReference = SidebarReferenceInsert
export type ComposerSpan = Parameters<NonNullable<SidebarSessionInput['insertReference']>>[1]
/** Fold a draft end using the same command/reference logic as every insert. */
export function detectOffsetOfDraftEnd(draft: string, occurrences: readonly SidebarSessionOccurrence[] = [], commandToken?: string): number {
return appendOffset({ draft, occurrences, ...(commandToken === undefined ? {} : { phase: 'claimed' as const, claim: { token: commandToken } }) }) ?? 0
}
/**
* One structured reference to insert as a chip: the compact `label` the
* composer shows, and the `text` the model receives on send. The two are
* deliberately different — a selection reads as `docs/a.md:12-15` on screen
* while the model still gets the whole fenced block.
*
* Named `Pending*` to keep clear of the host's own `DraftReference`, which is
* the opposite thing: a chip ALREADY in the draft, carrying its offset and
* length.
*/
export interface PendingReference {
label: string
text: string
/**
* What the draft string carries for this chip (the clipboard projection).
* Defaults to `label`; a file reference spells its `@path` here instead.
* This is the text other draft consumers (copy, plain projections) see, so
* it stays short — the full payload rides in `text` and is only expanded
* on send.
*/
clipboardText?: string
/**
* Chip icon family. Omit for a reference whose `text` is not a path: the
* host marks `'file'` chips openable, and a click would hand the raw text
* (a fenced block, for a selection) to the file opener. A chip whose
* {@link PendingReference.source} is a plugin-owned source may declare one
* safely — that source answers the click itself.
*/
appearance?: 'file' | 'folder' | 'session'
/**
* The trigger source that OWNS this chip. Defaults to the host's built-in
* `'reference'`, whose `ref` is the payload itself (`text`). A
* plugin-owned source (see `selection-quote.ts`) passes its own name so the
* host routes chip clicks and send-time serialization there instead.
*/
source?: string
/**
* The chip's reference id under {@link PendingReference.source}. Defaults
* to `text` — the built-in source's spelling, where the ref IS the
* payload. A custom source passes a short opaque id and resolves the
* payload from its own store (it must also answer the serialization codec,
* or the host refuses the send rather than downgrading to the clipboard
* text).
*/
ref?: string
}
/**
* The separating space an append needs on its left — one space, but never
* doubled against existing whitespace. Both the string append and the span
* channel (which does no splice math of its own) obey this single rule. A
* blank draft takes no separator: it is replaced outright.
*/
function leadFor(draft: string): string {
if (draft.trim() === '') return ''
return /\s$/.test(draft) ? '' : ' '
}
/** `draft` with `text` appended behind one separating space when needed. */
export function appendText(draft: string, text: string): string {
return `${draft}${leadFor(draft)}${text}`
}
/**
* Fold one clipboard-projection offset to its detect-projection twin.
*
* The host maintains two coordinate systems over the same document: the
* clipboard projection (`InputState.draft`, `occurrences`) expands every chip
* to its whole `clipboardText`, while the detect projection — the one every
* insert/replace event's `span` must speak — collapses each chip to a single
* placeholder character. Deriving a span from a draft offset without folding
* therefore overshoots by `length - 1` per chip, and the host rejects the
* out-of-range span (`applied = false`) — the second chip insert of a session
* would silently fail.
*
* An offset inside a chip's expansion snaps to that chip's trailing edge: the
* host only ever replaces whole chips, never splits one. An offset exactly at
* a chip's START resolves to the position BEFORE the chip — the host's own
* `detectOffsetOfClipboardOffset` reads that case as "inside" and returns the
* trailing edge, which would move a caret one chip too far right.
*/
export function foldClipboardOffset(
draft: string,
occurrences: readonly SidebarSessionOccurrence[],
offset: number,
): number {
const target = Math.max(0, Math.min(offset, draft.length))
let shrink = 0
for (const occurrence of occurrences) {
const end = occurrence.offset + occurrence.length
if (target >= end) {
// The whole chip sits at or before the offset: it contributes one
// character instead of its expansion.
shrink += occurrence.length - 1
continue
}
if (target > occurrence.offset) {
// Inside the chip's expansion — the host only replaces whole chips, so
// snap to this chip's trailing edge.
return occurrence.offset - shrink + 1
}
break
}
return target - shrink
}
/**
* The detect-projection offset that appends at the document end, or
* `undefined` when it cannot be derived.
*
* `foldClipboardOffset` alone is not enough: a command chip (`/goal `, present
* while `phase === 'claimed'`) collapses to one detect character but never
* appears in `occurrences` — the host only collects reference chips. The chip
* covers `claim.token.trimEnd()`; its separator remains a separate text node,
* so only the command's own characters
* fold away and one placeholder takes their place.
*/
function appendOffset(snapshot: ReturnType<SidebarSessionInput['state']['getSnapshot']>): number | undefined {
const { draft, claim } = snapshot
const occurrences = snapshot.occurrences ?? []
const token = snapshot.phase === 'claimed' && claim !== undefined && draft.startsWith(claim.token.trimEnd())
? claim.token.trimEnd()
: ''
if (token === '') return foldClipboardOffset(draft, occurrences, draft.length)
// Fold the arguments behind the token, then add the command chip's one
// detect character in front.
const rest = draft.slice(token.length)
const offsetInRest = occurrences
.filter(occurrence => occurrence.offset >= token.length)
.map(occurrence => ({ offset: occurrence.offset - token.length, length: occurrence.length }))
return 1 + foldClipboardOffset(rest, offsetInRest, rest.length)
}
/**
* Mint one reference chip through the host's own `insertReference` — the
* same facade method the native `@` picker's event listener calls, so the
* composer shows a compact chip while the model receives `reference.text`.
* The host runs its own admission-phase and revision guards and reports
* whether the edit applied.
*
* The chip's `clipboardText` is the compact `label`, not the full payload —
* see {@link PendingReference.clipboardText}.
*
* Returns false when the host refused the insert (stale revision, a frozen
* submission phase, or no chip channel at all) so the caller can fall back.
*/
export function insertDraftReference(
ctx: Context,
sessionId: string,
reference: PendingReference | SidebarReferenceInsert,
): boolean {
let before: number | undefined
try {
const actx = ctx.sessions.scope(sessionId)
if (actx === undefined) return false
const conversation = ctx.get('conversation') as SidebarConversation | undefined
if (conversation === undefined) return false
const input = conversation.input.for(actx)
const snapshot = input.state.getSnapshot()
if (input.insertReference === undefined || snapshot.draftRev === undefined) return false
if (snapshot.phase === 'adjudicating' || snapshot.phase === 'submitting') return false
// Only the chip write is a successful reference insertion. Reserving a
// separator may change the revision but must not masquerade as a chip.
// The public contenteditable face has no caret API; every insert uses
// the one coordinate it exposes: the folded document end.
const at = appendOffset(snapshot)
if (at === undefined) return false
// The host's insertReference adds a space AFTER the chip but never before
// it (client.js:14758), so a chip appended to `hello` would read
// `hello[chip]`. Reserve the separator first through the same span
// channel — a stray trailing space is harmless if the chip then fails.
let rev = snapshot.draftRev
let start = at
if (leadFor(snapshot.draft) !== '') {
const actxEmit = actx as unknown as { emit(name: string, payload: unknown): void }
actxEmit.emit('slash/input-insert-text', {
text: ' ',
span: { draftRev: rev, start: at, end: at },
})
const afterSpace = input.state.getSnapshot()
if (afterSpace.draftRev === rev) return false
rev = afterSpace.draftRev ?? rev
start = at + 1
}
before = rev
const chip: SidebarReferenceInsert = {
source: reference.source ?? 'reference',
// A plain pending selection carries its payload in `text`; a plugin
// insert (which always has `ref`) keeps its own short reference id.
ref: reference.ref ?? ('text' in reference ? reference.text : reference.label),
label: reference.label,
// The built-in source's `ref` is a fenced block, not a path: leaving
// `appearance` unset on such a chip keeps the host from marking it
// openable (a click would feed that block to the file opener). A
// plugin-owned source answers its own clicks, so its chips may declare
// one.
...reference.appearance === undefined ? {} : { appearance: reference.appearance },
clipboardText: reference.clipboardText ?? reference.label,
}
return input.insertReference(chip, { draftRev: rev, start, end: start })
} catch (error) {
// The host may have applied the edit and THEN thrown from its own update
// listeners — reporting false would make the caller insert the same
// reference a second time as plain text. A changed revision means the
// chip is already in the document.
console.warn('[dsh-better-sidebar] reference chip insert failed:', error)
return revisionChanged(ctx, sessionId, before)
}
}
/**
* Whether the session's draft revision moved past `revision` — the only
* trustworthy success signal after an exception swallowed the host's answer.
* A missing service or snapshot counts as "no evidence of success".
*/
function revisionChanged(ctx: Context, sessionId: string, revision?: number): boolean {
try {
const actx = ctx.sessions.scope(sessionId)
if (actx === undefined) return false
const conversation = ctx.get('conversation') as SidebarConversation | undefined
const input = conversation?.input.for(actx)
const now = input?.state.getSnapshot().draftRev
if (now === undefined || revision === undefined) return false
return now !== revision
} catch {
return false
}
}
/**
* Append `text` to the session's composer draft. Returns false — and logs —
* when the conversation service or the session scope is unavailable.
*
* The host composer is a Lexical contenteditable with no caret API on this
* face, so this appends at the end; a mid-draft insert would need the caret
* the host does not publish here.
*/
export function appendToDraft(ctx: Context, sessionId: string, text: string): boolean {
let before: number | undefined
try {
const actx = ctx.sessions.scope(sessionId)
if (actx === undefined) {
console.warn('[dsh-better-sidebar] draft insert skipped: no session scope', sessionId)
return false
}
const conversation = ctx.get('conversation') as SidebarConversation | undefined
if (conversation === undefined) {
console.warn('[dsh-better-sidebar] draft insert skipped: conversation service unavailable')
return false
}
const input = conversation.input.for(actx)
const snapshot = input.state.getSnapshot()
if (snapshot.phase === 'adjudicating' || snapshot.phase === 'submitting') return false
before = snapshot.draftRev
const draft = snapshot.draft
const occurrences = snapshot.occurrences ?? []
// A command chip (`/goal`) is not a reference occurrence, but `setDraft`
// flattens it just the same — and flattening it also drops the claim, so
// the host re-reads the draft as plain text and cancels the command. Both
// chip kinds take the span path.
const hasChips = occurrences.length > 0 || snapshot.phase === 'claimed'
if (hasChips) {
// `setDraft` rebuilds the document from a plain string, which flattens
// every existing chip into its text — so on a draft that already
// carries chips, insert through the span event instead. The host's
// `SessionInput` has no plain-text facade method (`insertText` lives on
// the InputBar-private `InputActions`), so this event is the only
// channel that preserves chips.
const at = appendOffset(snapshot)
if (snapshot.draftRev === undefined || at === undefined) {
// No revision means no span channel either — the only remaining write
// path (`setDraft`) would flatten every chip, so refuse instead.
console.warn('[dsh-better-sidebar] draft insert skipped: chip draft without a revision')
return false
}
// The session-scope Context's typed `emit` is keyed to DSH's closed
// event map; this internal composer event is string-loose at runtime.
const actxEmit = actx as unknown as { emit(name: string, payload: unknown): void }
actxEmit.emit('slash/input-insert-text', {
text: `${leadFor(draft)}${text}`,
span: { draftRev: snapshot.draftRev, start: at, end: at },
})
return input.state.getSnapshot().draftRev !== snapshot.draftRev
}
input.setDraft(appendText(draft, text))
return true
} catch (error) {
console.warn('[dsh-better-sidebar] draft insert failed:', error)
return revisionChanged(ctx, sessionId, before)
}
}
/**
* Insert one reference as a chip, degrading to its plain-text form when the
* host has no chip channel or refuses the insert. This is the ONE place the
* fallback policy lives: the explorer's `@` button and the viewer selection
* popup both come through here.
*/
export function insertReferenceOrText(
ctx: Context,
sessionId: string,
reference: PendingReference,
): boolean {
return insertDraftReference(ctx, sessionId, reference) || appendToDraft(ctx, sessionId, reference.text)
}
/**
* The DSH `@` spelling for one relative path, mirroring the host grammar
* (`formatFileMention` in `@deepseek-ai/dsh-file-reference`): plain when
* there is no whitespace, quoted when there is, and `undefined` when the
* path contains a control character or an embedded quote the editor grammar
* cannot represent.
*
* Files and folders share this one constructor so the quoting rule cannot
* drift between them. A folder spells the trailing slash *inside* the quotes
* (`@"my dir/"`, `@docs/`): the host's folder token matcher
* (`FOLDER_REF_RE` in `dsh-client-ui-conversation`) only accepts a quoted
* directory once the quote closes, and the message bubble's tokenizer
* (`@"[^"\n]+"` in `dsh-client-ui-primitives`) only classifies a closed
* quoted token as a folder — unquoted `@my dir/` decorates as a file chip
* covering just `my`. The host's own picker emits the *open* form
* (`@"my dir/`) while the user is still descending through completion; a
* programmatic reference is a finished mention, so it closes the quote.
*/
export function mentionFor(
relativePath: string,
kind: 'file' | 'folder',
): { mention: string; label: string } | undefined {
const path = relativePath.replace(/[\\/]+$/, '')
// eslint-disable-next-line no-control-regex -- rejecting control characters is the point of this guard
if (/[\u0000-\u001f\u007f-\u009f"]/u.test(path)) return undefined
const target = kind === 'folder' ? `${path}/` : path
const mention = /\s/u.test(target) ? `@"${target}"` : `@${target}`
return { mention, label: baseName(path) }
}
/**
* A complete directory token (`@docs/`, `@"my dir/"`): the explorer inserts a
* finished mention, so the trailing slash sits inside the closing quote.
* Separators normalize to '/' first (the fs-tree spelling); the spelling
* itself is {@link mentionFor}'s single job, folder kind — this helper adds no
* quoting rule of its own.
*/
export function directoryMention(relativePath: string): string | undefined {
return mentionFor(relativePath.replace(/\\/g, '/'), 'folder')?.mention
}
/**
* Insert an explorer entry using WorkspaceView.path, selected by exact
* session membership (the same lookup as the native conversation shell).
* Read the public workspace snapshot at click time so reconnects and session
* changes cannot leave a captured root stale. Missing/unready membership or
* an unrepresentable path is a logged no-op; never guess from session cwd.
*/
function focusComposer(ctx: Context, sessionId: string): void {
try {
const actx = ctx.sessions.scope(sessionId)
const conversation = ctx.get('conversation') as SidebarConversation | undefined
const input = actx === undefined ? undefined : conversation?.input.for(actx)
;(input as { focus?(): void } | undefined)?.focus?.()
} catch { /* focus does not change a successful insertion */ }
}
export function insertWorkspaceReference(ctx: Context, sessionId: string, path: string, isDir: boolean): boolean {
try {
const snapshot = ctx.get('workspaces')?.list.getSnapshot()
const root = snapshot?.phase === 'ready' && snapshot.state === 'idle'
? snapshot.items.find(workspace => workspace.sessionIds.includes(sessionId))?.path
: undefined
const relative = workspaceRelativePath(root, path)
if (relative === undefined) {
console.warn('[dsh-better-sidebar] reference insert skipped: workspace root unavailable or path outside workspace')
return false
}
if (isDir) {
const mention = directoryMention(relative)
if (mention === undefined) {
console.warn('[dsh-better-sidebar] reference insert skipped: path cannot be represented as a mention')
return false
}
// The host adds space after a file chip, but not before it. Complete
// directory inserts need their own separator for the next @ click.
const inserted = appendToDraft(ctx, sessionId, `${mention} `)
if (inserted) focusComposer(ctx, sessionId)
return inserted
}
const file = mentionFor(relative, 'file')
if (file === undefined) {
console.warn('[dsh-better-sidebar] reference insert skipped: path cannot be represented as a mention')
return false
}
const inserted = insertReferenceOrText(ctx, sessionId, {
label: file.label,
text: file.mention,
clipboardText: file.mention,
appearance: 'file',
})
if (inserted) focusComposer(ctx, sessionId)
return inserted
} catch (error) {
console.warn('[dsh-better-sidebar] workspace-reference insert failed:', error)
return false
}
}
/** File references use the shared chip writer and its phase/revision guards. */
export function insertFileReference(ctx: Context, sessionId: string, relativePath: string): boolean {
const file = mentionFor(relativePath, 'file')
if (file === undefined) return false
const inserted = insertDraftReference(ctx, sessionId, { label: file.label, text: file.mention, clipboardText: file.mention, appearance: 'file' })
if (inserted) focusComposer(ctx, sessionId)
return inserted
}