* feat(preset): declare Agent compositions in profile YAML * fix(preset): share scope binding cleanup and refresh module graph * fix(preset): use host syntax colors in the YAML editor * fix(preset): write new profile declarations as block YAML * fix(preset): align eager activation with browser and worker surfaces * fix(ui-agent-preset): retain the existing preset card layout * fix(preset): keep rows waiting for a Host service mounted and re-audit after settlement A preset row whose Host provider was still activating at declaration time was reported as waiting for a missing service, and the registry recorded the definition as broken for the rest of the process. Split the mount audit into failed and pending rows: failures and root-realm leaks still reject the mount, while pending rows stay mounted and every read and binding re-audits them after the Host Loader tree settles. Remove the accidental self-dependency of @deepseek-ai/dsh-web-app and its lockfile self-link. Archive the copy-only preset authoring note, which the declarative-preset note fully supersedes, and update the ralph note's restore recipe to the profile-patch mechanism. * chore(lint): re-key and prune the unknown-cast baseline after the master merge * refactor(web-app): ship each Agent preset as its own bundle patch file `dsh.bundle.patch` accepts an ordered file list, so the four shipped preset declarations move out of the 1000-line web patch into `presets/<id>.patch.yml`, one insert per file, applied after the host patch. The host patch keeps the preset registry row. Gates and test scaffolds that read the web layer resolve every patch file through `bundlePatchPaths` instead of one hardcoded path. * refactor(preset): drop the save notice injected into live Agents The notice duplicated the save result the Web editor already shows, and after the producer-owned message sources it would have needed its own persisted source kind. Saving now only persists and reports the activation diagnostic; the persistence catalog is unchanged from master. * docs: regenerate the module graph after dropping the registry's llm dependency * test(ui-agent-preset): cover the guide dialog's keyboard handling on the declarative section * fix(preset): address the remaining review comments Apply the lowercase id rule only when creating a declaration, so presets declared with other ids stay editable. Export `isBuiltInPreset` from the display fold and key the client card groups on it. Remove the dead `error` and `retry` locale keys, the JSDoc orphaned by the `trust` removal, the `trust` fields in the plugin-inventory test fixtures, and the stale `dsh-agent-presets` package name in comments and READMEs. * refactor(preset): make the Web preset page view-and-select only Remove the child-plugin YAML editor, the `agent_preset` tool and the registry's read/save Remote methods. Presets are declared and changed only by bundle patches; the settings page keeps the roster, the new-task default and the mode help, and its question icon explains that and offers to start a Creator-mode task. The `editing-cordis-compositions` skill and the Creator persona describe the bundle-based path, and the authored skill-load Session and Web snapshots are refreshed for it. * docs(skill): make the composition skill self-sufficient for preset authoring State where the shipped declarations live, list every declaration field, inline a complete minimal bundle and an override example, and name the verification calls, so Creator mode no longer reads DSH package sources to infer the format. Refresh the authored skill-load Session for the new text. * test(tools): drop the removed agent_preset tool from the catalog expectation
342 lines
13 KiB
TypeScript
342 lines
13 KiB
TypeScript
/** Cold-safe Session list and search projection. */
|
|
|
|
import type { Context } from '@deepseek-ai/cordis'
|
|
import type {} from '@deepseek-ai/dsh-agent-preset-registry'
|
|
import type { ImageAttachmentLimits } from '@deepseek-ai/dsh-attachment'
|
|
import type { Session, SessionEvent, SessionHeader, SessionId } from '@deepseek-ai/dsh-session'
|
|
import type { ProjectionSnapshot } from '@deepseek-ai/dsh-session-projection'
|
|
import type {} from '@deepseek-ai/dsh-session-projection-cache'
|
|
import { SessionQueryError, type SessionSearchCursor } from '@deepseek-ai/dsh-session-query'
|
|
import { RemoteError } from '@deepseek-ai/dsh-typert-protocol'
|
|
import { z } from 'zod'
|
|
import {
|
|
SESSION_SEARCH_RESULT_LIMIT,
|
|
SESSION_SEARCH_SNIPPET_MAX_CODE_POINTS,
|
|
} from './types.ts'
|
|
import type {
|
|
SessionListMetadata, SessionProjectionHints, SessionProjectionValues, SessionSearchItem,
|
|
SessionSearchValue, SessionSummary,
|
|
} from './types.ts'
|
|
|
|
const SEARCH_PROVIDER_CALL_LIMIT = 100
|
|
const SESSION_SEARCH_QUERY_MAX_CHARS = 500
|
|
const MESSAGE_TYPES = new Set(['user/message', 'assistant/message'])
|
|
|
|
const sessionListMetadataSchema: z.ZodType<SessionListMetadata> = z.object({
|
|
blank: z.boolean(),
|
|
lastPromptAt: z.number().nullable(),
|
|
})
|
|
|
|
const imageLimitsSchema = z.object({
|
|
maxImageBytes: z.number().int().positive(),
|
|
maxImagesPerMessage: z.number().int().positive(),
|
|
maxMessageImageBytes: z.number().int().positive(),
|
|
maxImagePixels: z.number().int().positive(),
|
|
maxImageDimension: z.number().int().positive(),
|
|
mediaTypes: z.array(z.string()),
|
|
}) as unknown as z.ZodType<ImageAttachmentLimits>
|
|
|
|
/**
|
|
* Advance the Session-list metadata projection by one committed event.
|
|
* @param state - metadata before the event.
|
|
* @param event - next committed Session event.
|
|
* @returns the original or advanced metadata value.
|
|
*/
|
|
export function applySessionListMetadata(
|
|
state: SessionListMetadata,
|
|
event: SessionEvent,
|
|
): SessionListMetadata {
|
|
const blank = state.blank && event.type !== 'turn/start'
|
|
const lastPromptAt = event.type === 'user/message' && event.data.source.kind === 'user'
|
|
? event.time
|
|
: state.lastPromptAt
|
|
return blank === state.blank && lastPromptAt === state.lastPromptAt
|
|
? state
|
|
: { blank, lastPromptAt }
|
|
}
|
|
|
|
/**
|
|
* Return the longest prefix containing at most `maximum` Unicode code points.
|
|
* @param value - source text.
|
|
* @param maximum - maximum number of Unicode code points.
|
|
* @returns the source text or its longest allowed prefix.
|
|
*/
|
|
export function truncateUnicodeCodePoints(value: string, maximum: number): string {
|
|
let count = 0
|
|
let end = 0
|
|
for (const codePoint of value) {
|
|
if (count === maximum) return value.slice(0, end)
|
|
count++
|
|
end += codePoint.length
|
|
}
|
|
return value
|
|
}
|
|
|
|
/** Owns list projection registration, bounded cold summaries, and authorized search. */
|
|
export class ApiSessionList {
|
|
/** @param ctx - Host context carrying Session, query, persistence, and projection services. */
|
|
constructor(private readonly ctx: Context) {
|
|
ctx.sessionProjections.register<'sessionListMetadata', SessionListMetadata>({
|
|
key: 'sessionListMetadata',
|
|
stateSchema: sessionListMetadataSchema,
|
|
init: () => ({ blank: true, lastPromptAt: null }),
|
|
apply: applySessionListMetadata,
|
|
wire: { viewSchema: sessionListMetadataSchema, view: state => state },
|
|
stateVersion: 1,
|
|
})
|
|
ctx.inject(['attachments'], (attachmentCtx) => {
|
|
ctx.sessionProjections.register<'imageLimits', null>({
|
|
key: 'imageLimits',
|
|
stateSchema: z.null(),
|
|
init: () => null,
|
|
apply: state => state,
|
|
wire: {
|
|
viewSchema: imageLimitsSchema,
|
|
view: () => attachmentCtx.attachments.imageLimits,
|
|
},
|
|
stateVersion: 1,
|
|
})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Build one current attached-Session summary.
|
|
* @param session - attached Session to summarize.
|
|
* @returns current list metadata and available projections.
|
|
*/
|
|
summaryFor(session: Session): SessionSummary {
|
|
const projections = this.projectionsFor(session.header, session)
|
|
const metadata = projections?.values.sessionListMetadata
|
|
return {
|
|
sessionId: session.id,
|
|
updatedAt: updatedAt(session.header, metadata),
|
|
agentAvailable: this.ctx.agents.get(session.id)?.session === session,
|
|
running: this.ctx.agents.get(session.id)?.status === 'running',
|
|
blank: metadata?.blank ?? session.seq === 0,
|
|
...listFields(session.header),
|
|
...(projections === undefined ? {} : { projections }),
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Read every visible attached and persisted Session without activating an Agent.
|
|
* @param signal - optional cancellation for persistence reads.
|
|
* @returns visible Session summaries ordered by activity.
|
|
*/
|
|
async list(signal?: AbortSignal): Promise<SessionSummary[]> {
|
|
signal?.throwIfAborted()
|
|
const records = await this.ctx.sessionQuery.listSessions(signal)
|
|
signal?.throwIfAborted()
|
|
const items: SessionSummary[] = []
|
|
const cold: SessionHeader[] = []
|
|
for (const record of records) {
|
|
const live = this.ctx.sessions.get(record.header.id)
|
|
if (live !== undefined) {
|
|
items.push(this.summaryFor(live))
|
|
continue
|
|
}
|
|
if (record.header.cwd === undefined) continue
|
|
cold.push(record.header)
|
|
}
|
|
for (const header of cold) items.push(this.summarizeCold(header))
|
|
items.sort((left, right) => right.updatedAt - left.updatedAt)
|
|
return items
|
|
}
|
|
|
|
private summarizeCold(header: SessionHeader): SessionSummary {
|
|
const projections = this.projectionsFor(header, undefined)
|
|
const metadata = projections?.values.sessionListMetadata
|
|
return {
|
|
sessionId: header.id,
|
|
updatedAt: updatedAt(header, metadata),
|
|
agentAvailable: false,
|
|
running: false,
|
|
// A large, metadata-less, or inaccessible cache miss remains unknown and visible.
|
|
blank: metadata?.blank ?? false,
|
|
...listFields(header),
|
|
...(projections === undefined ? {} : { projections }),
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Search current visible message content without activating any matching Session.
|
|
* @param query - literal message-content query.
|
|
* @param signal - cancellation for list and search reads.
|
|
* @returns authorized bounded Session search results.
|
|
*/
|
|
async search(query: string, signal: AbortSignal): Promise<SessionSearchValue> {
|
|
const normalizedQuery = normalizeSearchQuery(query)
|
|
signal.throwIfAborted()
|
|
const provider = this.ctx.get('sessionQuery')
|
|
if (provider === undefined) {
|
|
throw new RemoteError(
|
|
'gateway/internal',
|
|
'session search is unavailable: this deployment does not mount @deepseek-ai/dsh-session-query',
|
|
{},
|
|
)
|
|
}
|
|
try {
|
|
const visible = await provider.listSessions(signal)
|
|
signal.throwIfAborted()
|
|
const visibleIds = new Set(visible
|
|
.filter(record => record.header.cwd !== undefined)
|
|
.map(record => record.header.id))
|
|
if (visibleIds.size === 0) return { items: [], hasMore: false }
|
|
const authorized: SessionSearchItem[] = []
|
|
const acceptedIds = new Set<SessionId>()
|
|
const seenCursors = new Set<SessionSearchCursor>()
|
|
let cursor: SessionSearchCursor | undefined
|
|
let providerCalls = 0
|
|
let pageLimit = SESSION_SEARCH_RESULT_LIMIT
|
|
while (authorized.length <= SESSION_SEARCH_RESULT_LIMIT) {
|
|
signal.throwIfAborted()
|
|
if (providerCalls >= SEARCH_PROVIDER_CALL_LIMIT) {
|
|
throw new Error(`session search provider exceeded the ${SEARCH_PROVIDER_CALL_LIMIT}-call work budget`)
|
|
}
|
|
providerCalls++
|
|
const requestedCursor = cursor
|
|
const requestedLimit = pageLimit
|
|
let page
|
|
try {
|
|
page = await provider.searchSessions({
|
|
query: normalizedQuery,
|
|
eventFilters: [
|
|
{ kind: 'type', values: ['user/message', 'assistant/message'] },
|
|
{ kind: 'surface', values: ['current'] },
|
|
],
|
|
limit: requestedLimit,
|
|
...(requestedCursor === undefined ? {} : { cursor: requestedCursor }),
|
|
}, { signal })
|
|
signal.throwIfAborted()
|
|
} catch (error: unknown) {
|
|
signal.throwIfAborted()
|
|
if (requestedCursor === undefined
|
|
&& error instanceof SessionQueryError
|
|
&& error.code === 'SESSION_QUERY_INVALID_LIMIT'
|
|
&& requestedLimit > 1) {
|
|
pageLimit = Math.max(1, Math.floor(requestedLimit / 2))
|
|
continue
|
|
}
|
|
if (requestedCursor !== undefined
|
|
&& error instanceof SessionQueryError
|
|
&& error.code === 'SESSION_QUERY_STALE_CURSOR') {
|
|
authorized.length = 0
|
|
acceptedIds.clear()
|
|
seenCursors.clear()
|
|
cursor = undefined
|
|
continue
|
|
}
|
|
throw error
|
|
}
|
|
if (page.items.length > requestedLimit) {
|
|
throw new Error(`session search provider returned ${String(page.items.length)} items; maximum is ${String(requestedLimit)}`)
|
|
}
|
|
for (const hit of page.items) {
|
|
if (authorized.length > SESSION_SEARCH_RESULT_LIMIT) continue
|
|
if (!visibleIds.has(hit.header.id)
|
|
|| hit.bestMatch.sessionId !== hit.header.id
|
|
|| hit.bestMatch.surface !== 'current'
|
|
|| !MESSAGE_TYPES.has(hit.bestMatch.type)
|
|
|| acceptedIds.has(hit.header.id)) continue
|
|
acceptedIds.add(hit.header.id)
|
|
authorized.push({
|
|
sessionId: hit.header.id,
|
|
snippet: truncateUnicodeCodePoints(hit.bestMatch.snippet, SESSION_SEARCH_SNIPPET_MAX_CODE_POINTS),
|
|
})
|
|
}
|
|
if (page.nextCursor !== undefined) {
|
|
if (seenCursors.has(page.nextCursor)) {
|
|
throw new Error('session search provider repeated a continuation cursor')
|
|
}
|
|
seenCursors.add(page.nextCursor)
|
|
}
|
|
if (authorized.length > SESSION_SEARCH_RESULT_LIMIT || page.nextCursor === undefined) break
|
|
cursor = page.nextCursor
|
|
}
|
|
return {
|
|
items: authorized.slice(0, SESSION_SEARCH_RESULT_LIMIT),
|
|
hasMore: authorized.length > SESSION_SEARCH_RESULT_LIMIT,
|
|
}
|
|
} catch (error: unknown) {
|
|
signal.throwIfAborted()
|
|
if (error instanceof SessionQueryError && error.code === 'SESSION_QUERY_ABORTED') {
|
|
throw new RemoteError('gateway/cancelled', 'session search was aborted', {})
|
|
}
|
|
throw new RemoteError('gateway/internal', `session search failed: ${String(error)}`, {})
|
|
}
|
|
}
|
|
|
|
private projectionsFor(
|
|
header: SessionHeader,
|
|
session: Session | undefined,
|
|
): SessionProjectionHints | undefined {
|
|
try {
|
|
if (session !== undefined) {
|
|
// The live registry computed the block for this Session: its watermark
|
|
// shares the sequence space of the Session's baselines and frames.
|
|
return hintsOf('sequenced', this.ctx.sessionProjections.cachedSnapshot(session))
|
|
}
|
|
// A cold row reads the persisted cache by header alone; the cache serves
|
|
// seeded and unseeded lifecycles alike because a listing never seeds a
|
|
// fold. The watermark is the stored record's own.
|
|
const cache = this.ctx.get('sessionProjectionCache')
|
|
return hintsOf('cached', cache?.cachedSnapshot(header) ?? cache?.cachedPredecessorTitle(header))
|
|
} catch (error) {
|
|
this.ctx.logger.warn(
|
|
`api-session.list: projection column for "${header.id}" failed; serving the row without it: ${String(error)}`,
|
|
)
|
|
return undefined
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Wrap one projection block as Session-list hints of the named sequence space.
|
|
* @param kind - which sequence space the block's watermark belongs to.
|
|
* @param block - the block, or `undefined` when no source served one.
|
|
* @returns the hints, or `undefined` when the block is absent or carries no value.
|
|
*/
|
|
function hintsOf(
|
|
kind: SessionProjectionHints['kind'],
|
|
block: ProjectionSnapshot | undefined,
|
|
): SessionProjectionHints | undefined {
|
|
if (block === undefined || Object.keys(block.values).length === 0) return undefined
|
|
// Listing hints contain every wire value the source currently holds but
|
|
// remain partial: missing cells and cache rows are never materialized here.
|
|
return { kind, asOfSeq: block.asOfSeq, values: block.values as SessionProjectionValues }
|
|
}
|
|
|
|
function normalizeSearchQuery(query: string): string {
|
|
const normalized = query.trim()
|
|
if (normalized.length === 0) {
|
|
throw new RemoteError('gateway/bad-request', 'session search query must not be empty', {})
|
|
}
|
|
if (normalized.length > SESSION_SEARCH_QUERY_MAX_CHARS) {
|
|
throw new RemoteError(
|
|
'gateway/bad-request',
|
|
`session search query must contain at most ${SESSION_SEARCH_QUERY_MAX_CHARS} UTF-16 code units`,
|
|
{},
|
|
)
|
|
}
|
|
if (normalized.includes('\0')) {
|
|
throw new RemoteError('gateway/bad-request', 'session search query must not contain NUL', {})
|
|
}
|
|
return normalized
|
|
}
|
|
|
|
function updatedAt(header: SessionHeader, metadata: SessionListMetadata | undefined): number {
|
|
return Math.max(header.createdAt, metadata?.lastPromptAt ?? 0)
|
|
}
|
|
|
|
function listFields(header: SessionHeader): {
|
|
readonly parentSessionId?: SessionId
|
|
readonly origin?: 'subagent'
|
|
readonly cwd?: string
|
|
} {
|
|
return {
|
|
...(header.parentSession === undefined ? {} : { parentSessionId: header.parentSession }),
|
|
...(header.origin === undefined ? {} : { origin: header.origin }),
|
|
...(header.cwd === undefined ? {} : { cwd: header.cwd }),
|
|
}
|
|
}
|