The fork was pinned at upstream v0.18.0. That is the entire reason the window
ran TWO sidebars and TWO near-identical panel toggles: DSH 2.0.10 ships its own
right sidebar (@deepseek-ai/dsh-client-ui-sidebar-right), and upstream only
adopted it in v0.19.0-alpha.1 — which arrived after our fork point. v0.19.1 is
also the one upstream line whose support range actually matches our host
(engines.dsh >=0.1.5-rc.1, 14 DSH peers ^0.1.5-rc.1, host = 0.1.5-rc.2), so it
is the only tag the host's startup precheck accepts.
What upstream v0.18.0 → v0.19.1 brings
- src/client/native/{index,surface,tab-adapter}.tsx: every tab the plugin can
draw becomes an OFFICIAL native tab type + body, and an external plugin's
descriptor registered later gets its native type too. This is what retires
the fork's own right panel — and with it the duplicate toggle cluster
(`toggleCluster` is gone from v0.19.1).
- native tab-chip icons via the `sidebar.right.pane.tab.title` seat, coloured
built-in tab icons, file/folder glyphs from DSH's own `FileTypeIcon`.
- guide descriptions (DSH renders them while the guide lists <= 4 entries),
tab bodies that fill the panel, and the 0.1.5-rc.2 adaptation work we were
carrying by hand (peer/devDep pinning, primitives' transitive devDeps).
How the sync was done
Rebuilt the tree from the v0.19.1 tag rather than resolving 53 conflicted
files: our v0.18.0 delta was measured first and is only (a) one mechanical
rename `dsh-better-sidebar` -> `dsh-apes-sidebar` and (b) ten real edits. So:
tag tree + rename (scoped: code/config/scripts/tests/README, while the 46
historical docs/plans records and switch.sh's upstream package name stay
untouched) + re-applied deltas + our apes module.
Fork deltas re-applied on the new base
- src/client/apes/ (icons / locales / tabs): the three apes:* information-layer
tabs (agents / apps / profile), registered through the same BetterSidebarService
as the builtins — which is exactly why they now ride upstream's native adapter
into the official sidebar for free.
- identity: package.json (name/version 1.2.0/description/forkedFrom), dsh.plugin.json
(id/version), SIDEBAR_SERVICE_VERSION 1.2.0, README fork notice,
docs/external-plugin-guide.md.
- scripts/switch.sh + the pnpm switch* scripts (apes <-> upstream two-line switch).
- tests/apes-tabs.spec.ts (unescaped: it starts from the v0.19.1 base).
Dropped
- the Phase A native-registration spike (`apes/native-tabs.tsx` + its spec): it
re-derived by hand exactly what upstream's native layer already does — the
hand-written bridge is dead weight now, and v0.19.1's own e2e/jsdom lanes
cover the same seat contract far better.
- the unused apesProfileGuideDesc key and the two package.json.bak.* backups.
Verified on the new base: typecheck clean; build clean; 1351 passed / 9 skipped
(128 files), up from 1255 on the old base.
257 lines
11 KiB
TypeScript
257 lines
11 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.
|
|
*
|
|
* File references additionally use DSH's own structured insert event
|
|
* (`slash/input-insert-reference`, see `insertFileReference`) instead of
|
|
* plain draft text: the native `@file` picker emits this event, and the
|
|
* conversation input machine mints one occurrence whose chip covers the
|
|
* whole reference. Plain text `@folder/file.ts` only ever gets DSH's
|
|
* folder-ref decoration (`@folder/`) — the file name stays undecorated — so
|
|
* plain append is the fallback, not the primary path, for files.
|
|
*
|
|
* Insert position (fixes upstream issue #425): the draft store only exposes
|
|
* the whole string (`getSnapshot().draft` + `setDraft(text)`) — there is no
|
|
* caret API on the conversation service. The composer's `<textarea>` keeps
|
|
* its last selection even while unfocused, so the live caret is probed from
|
|
* the DOM (guarded by a value-sync check), and the text is spliced at that
|
|
* position, replacing any live selection — with whitespace-aware joins, an
|
|
* insert into the middle of a sentence keeps single-space separation like
|
|
* the append path. An unknown/stale caret falls back to appending at the
|
|
* end (the pre-fix behavior).
|
|
*
|
|
* The caret is also *restored* after the insert: committing a programmatic
|
|
* draft change resets the controlled textarea's caret (observed landing at
|
|
* the start of the value), which would make every later insert probe the
|
|
* reset position and drift the stack (A|B + C + D ended up as |DACB). The
|
|
* placement (`placeComposerCaretAfterInsert`) puts the caret right after the
|
|
* inserted text once the value commit lands — the index accounts for the
|
|
* separating space on the left, so stacked inserts stay at their running
|
|
* position (A|B + C + D → ACD|B).
|
|
*/
|
|
import type { Context, SidebarConversation } from '../context-types.ts'
|
|
|
|
/** A resolved composer caret/selection in draft coordinates. */
|
|
export interface DraftCaret {
|
|
start: number
|
|
end: number
|
|
}
|
|
|
|
/**
|
|
* The spliced draft plus the caret index (in that draft) right after the
|
|
* inserted text — the left-side separating space shifts the caret by one,
|
|
* which naive `start + text.length` misses (it would land before the tail).
|
|
*/
|
|
interface SpliceResult {
|
|
draft: string
|
|
caretAfter: number
|
|
}
|
|
|
|
/**
|
|
* Splice `text` into `draft` at `caret` (replacing any live selection) with
|
|
* whitespace-aware joins and report the caret position right after the
|
|
* inserted text. `caret === null` (position unknown) appends at the end,
|
|
* exactly like the original behavior.
|
|
*/
|
|
function spliceInsert(draft: string, text: string, caret: DraftCaret | null): SpliceResult {
|
|
if (caret === null || draft === '') {
|
|
const next = draft.trim() === '' ? text : `${draft} ${text}`
|
|
return { draft: next, caretAfter: next.length }
|
|
}
|
|
const prefix = draft.slice(0, caret.start)
|
|
const suffix = draft.slice(caret.end)
|
|
if (prefix === '' && suffix === '') return { draft: text, caretAfter: text.length }
|
|
// One separating space, but never doubled against adjacent whitespace
|
|
// (or the string edges) — mirrors how typing in the middle of a sentence
|
|
// behaves.
|
|
const left = prefix === '' || /\s$/.test(prefix) ? '' : ' '
|
|
const right = suffix === '' || /^\s/.test(suffix) ? '' : ' '
|
|
return {
|
|
draft: `${prefix}${left}${text}${right}${suffix}`,
|
|
caretAfter: prefix.length + left.length + text.length,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The spliced draft string (see {@link spliceInsert}); pure string math —
|
|
* unit-tested directly.
|
|
*/
|
|
export function insertAtCaret(draft: string, text: string, caret: DraftCaret | null): string {
|
|
return spliceInsert(draft, text, caret).draft
|
|
}
|
|
|
|
/**
|
|
* Locate the composer `<textarea>` in the conversation column: prefer the
|
|
* `data-phase`-tagged textarea (the composer's marker), falling back to any
|
|
* textarea in the column, then to a bare data-phase textarea (older host
|
|
* layouts without the column attribute). Null in jsdom-less hosts.
|
|
*/
|
|
function findComposerTextarea(): HTMLTextAreaElement | null {
|
|
if (typeof document === 'undefined') return null
|
|
const column = document.querySelector('#root [data-slot="conversation"]')
|
|
const find = (scope: ParentNode): HTMLTextAreaElement | null =>
|
|
scope.querySelector('textarea[data-phase]') ?? scope.querySelector('textarea')
|
|
return column !== null
|
|
? find(column)
|
|
: document.querySelector<HTMLTextAreaElement>('textarea[data-phase]')
|
|
}
|
|
|
|
/**
|
|
* Resolve the composer's live caret from its DOM `<textarea>`. The draft
|
|
* store has no caret API, so the sidebar reads the composed input's selection
|
|
* directly; the value-sync check (`el.value === draft`) discards stale or
|
|
* wrong-composer reads — a caret must never be applied against a draft it
|
|
* was not measured on.
|
|
*
|
|
* Returns null when the composer is missing, disabled/read-only, out of
|
|
* sync with the store draft, or has no measurable selection (jsdom/odd
|
|
* hosts report null selectionStart/End).
|
|
*/
|
|
export function probeComposerCaret(draft: string): DraftCaret | null {
|
|
const el = findComposerTextarea()
|
|
if (el === null || el.disabled || el.readOnly) return null
|
|
if (el.value !== draft) return null
|
|
let start = el.selectionStart
|
|
let end = el.selectionEnd
|
|
if (typeof start !== 'number' || typeof end !== 'number') return null
|
|
if (!Number.isFinite(start) || !Number.isFinite(end)) return null
|
|
start = Math.max(0, Math.min(start, draft.length))
|
|
end = Math.max(start, Math.min(end, draft.length))
|
|
return { start, end }
|
|
}
|
|
|
|
/**
|
|
* Restore the composer caret to `caretIndex` after a programmatic
|
|
* `setDraft` commit. A controlled textarea update resets the caret (React
|
|
* commits the value asynchronously and the browser moves the caret to the
|
|
* start/end), so the placement is scheduled and retried across at most two
|
|
* animation frames (setTimeout fallback for jsdom), and only applied when
|
|
* the textarea still matches `expectedDraft` — a newer edit or a different
|
|
* composer wins the race untouched. The caret is clamped into the value
|
|
* bounds, mirroring how browsers clamp type-in positions.
|
|
*/
|
|
export function placeComposerCaretAfterInsert(expectedDraft: string, caretIndex: number): void {
|
|
let remaining = 2
|
|
let scheduled = false
|
|
const schedule = (fn: () => void): void => {
|
|
if (scheduled) return
|
|
scheduled = true
|
|
if (typeof requestAnimationFrame === 'function') requestAnimationFrame(fn)
|
|
else setTimeout(fn, 0)
|
|
}
|
|
const place = (): void => {
|
|
scheduled = false
|
|
if (remaining <= 0) return
|
|
remaining -= 1
|
|
const el = findComposerTextarea()
|
|
if (el === null || el.disabled || el.readOnly) return
|
|
if (el.value !== expectedDraft) {
|
|
// The commit has not landed yet (or a competing edit won) — one more
|
|
// frame before giving up.
|
|
schedule(place)
|
|
return
|
|
}
|
|
const clamped = Math.max(0, Math.min(caretIndex, el.value.length))
|
|
el.setSelectionRange(clamped, clamped)
|
|
}
|
|
schedule(place)
|
|
}
|
|
|
|
/**
|
|
* Insert `text` into the session's composer draft at the composer's live
|
|
* caret (see {@link probeComposerCaret}), falling back to appending at the
|
|
* end when the caret cannot be resolved. Returns false — and logs — when the
|
|
* conversation service or the session scope is unavailable.
|
|
*/
|
|
export function appendToDraft(ctx: Context, sessionId: string, text: string): boolean {
|
|
try {
|
|
const actx = ctx.sessions.scope(sessionId)
|
|
if (actx === undefined) {
|
|
console.warn('[dsh-apes-sidebar] draft insert skipped: no session scope', sessionId)
|
|
return false
|
|
}
|
|
const conversation = ctx.get('conversation') as SidebarConversation | undefined
|
|
if (conversation === undefined) {
|
|
console.warn('[dsh-apes-sidebar] draft insert skipped: conversation service unavailable')
|
|
return false
|
|
}
|
|
const input = conversation.input.for(actx)
|
|
const draft = input.state.getSnapshot().draft
|
|
const caret = probeComposerCaret(draft)
|
|
const { draft: next, caretAfter } = spliceInsert(draft, text, caret)
|
|
input.setDraft(next)
|
|
// Put the caret right after the inserted text once the value commit
|
|
// lands — see the module doc for why this keeps stacked inserts at the
|
|
// running position.
|
|
placeComposerCaretAfterInsert(next, caretAfter)
|
|
return true
|
|
} catch (error) {
|
|
console.warn('[dsh-apes-sidebar] draft insert failed:', error)
|
|
return false
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The DSH `@file` 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.
|
|
*/
|
|
export function fileMention(relativePath: string): { 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 mention = /\s/u.test(path) ? `@"${path}"` : `@${path}`
|
|
const at = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'))
|
|
const label = at === -1 ? path : path.slice(at + 1)
|
|
return { mention, label }
|
|
}
|
|
|
|
/**
|
|
* Insert one FILE reference as a structured chip (like DSH's own `@` picker).
|
|
* The chip displays `@<basename>` but serializes to `@<relative path>` on
|
|
* send, so the reference stays a single link from trigger to basename.
|
|
*
|
|
* Directories are NOT handled here: DSH's folder grammar wants the trailing
|
|
* slash as plain text (`@dir/`) so completion can descend, which
|
|
* `appendToDraft` already covers.
|
|
*/
|
|
export function insertFileReference(ctx: Context, sessionId: string, relativePath: string): boolean {
|
|
const reference = fileMention(relativePath)
|
|
if (reference === undefined) return false
|
|
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 before = input.state.getSnapshot()
|
|
if (before.draftRev === undefined) return false
|
|
// The session-scope Context's typed `emit` is keyed to DSH's closed event
|
|
// map; this internal composer event is deliberately string-loose at runtime.
|
|
;(actx as unknown as { emit(name: string, payload: unknown): void }).emit('slash/input-insert-reference', {
|
|
reference: {
|
|
source: 'reference',
|
|
ref: reference.mention,
|
|
label: reference.label,
|
|
appearance: 'file',
|
|
clipboardText: reference.mention,
|
|
},
|
|
span: {
|
|
draftRev: before.draftRev,
|
|
start: before.draft.length,
|
|
end: before.draft.length,
|
|
},
|
|
})
|
|
const after = input.state.getSnapshot()
|
|
return after.draftRev !== before.draftRev
|
|
} catch (error) {
|
|
console.warn('[dsh-apes-sidebar] file-reference insert failed:', error)
|
|
return false
|
|
}
|
|
}
|