Merge master into feat/subagent-names

This commit is contained in:
Dudu-0223 committed 2026-10-29 17:43:46 +08:00
commit 4514b99d9c
2829 files changed
+180072 -13757

No files matched your search

@@ -7,7 +7,7 @@
* must stub); implementation-internal entry points (history staging, wire-frame
* dispatch) stay on the class, invisible out here.
*/
import type { AttachmentIdType, FileAttachmentRef, ImageAttachmentRef } from '@deepseek-ai/dsh-attachment'
import type { FileAttachmentRef, ImageAttachmentRef } from '@deepseek-ai/dsh-attachment'
import type { MessageId } from '@deepseek-ai/dsh-llm/brand'
import type { SessionId, SessionSeq } from '@deepseek-ai/dsh-session/types'
import type { RemoteResult } from '@deepseek-ai/dsh-typert-protocol'
@@ -36,6 +36,11 @@ export interface BeginSubmissionInput {
readonly text: string
/** Ordered image previews and durable file metadata matching the upcoming prompt attachments. */
readonly attachments: readonly PendingSubmissionAttachment[]
/**
* Called once when the Host accepted the prompt, in the same update that
* publishes the echo, whether the receipt or an Inbox row showed it first.
*/
readonly onAccept?: () => void
/** Settlement callback fired exactly once when the echo retires. */
readonly onRetire?: (retirement: PendingSubmissionRetirement) => void
}
@@ -66,18 +71,24 @@ export interface ISession {
/** Host-computed projection values by key (the useProjection seat). */
readonly projections: ProjectionsFace
/**
* Register one local submission echo in `snapshot.pendingSubmissions`,
* synchronously, before the caller serializes and sends the prompt. The
* Chat echoes persist until durable admission; transcript identities also
* wait for the Inbox claim watermark so stale queue rows stay suppressed.
* Queued echoes retire on queue acceptance. Identified failures retire
* submissions that have not already reached durable admission.
* Register one local submission echo before the caller serializes and sends
* the prompt. The echo reconciles against Host events from then on, and
* enters `snapshot.pendingSubmissions` once the Host accepted the prompt:
* when {@link prompt} reports the receipt or an Inbox row carrying the
* request id arrives, whichever comes first. The Chat echoes persist until
* durable admission; transcript identities also wait for the Inbox claim
* watermark so stale queue rows stay suppressed. Queued echoes retire on
* queue acceptance. Identified failures retire submissions that have not
* already reached durable admission.
* @param input - echo content and the optional settlement callback.
* @returns the minted identity for {@link prompt} plus the pre-prompt abandon path.
*/
beginSubmission(input: BeginSubmissionInput): SubmissionHandle
/**
* Send a prompt into the session.
* Send a prompt into the session. The Host can hold the prompt at its
* admission stage, so the call can stay open; acceptance publishes the
* prompt's echo and moves a blank Session to engaging, before the call
* returns when an Inbox row carrying the request id arrives first.
* @param content - text plus browser-owned temporary image uploads.
* @param mode - 'queue' appends a turn; 'steer' interrupts the running one.
* @param signal - optional caller cancellation for the complete admission round-trip.
@@ -91,12 +102,12 @@ export interface ISession {
requestId?: SessionRequestId,
): Promise<RemoteResult<{ accepted: true }>>
/**
* Resolve one durable image referenced by this session.
* @param attachmentId - opaque id found in the folded session log.
* @returns the authenticated reference and decoded bytes.
* Resolve one stored image using this Session's gateway permission scope.
* @param attachment - complete image reference from the folded session log.
* @returns the supplied reference after storage verification, and decoded bytes.
*/
readAttachment(
attachmentId: AttachmentIdType,
attachment: ImageAttachmentRef,
): Promise<RemoteResult<{ attachment: ImageAttachmentRef; data: Uint8Array }>>
/**
* Apply one edit, remove, or Steer action to a still-pending queue occurrence.
@@ -38,10 +38,10 @@ export type PendingSubmissionAttachment =
export type PendingSubmissionPlacement = 'transcript' | 'queued' | 'steering'
/**
* One local prompt-submission echo: inserted synchronously when a submission
* begins, so the conversation can show the message before serialization,
* transport, and durable admission complete. Client-memory only — reload and
* reconnect rebuild the conversation from durable events alone.
* One local prompt-submission echo: registered when a submission begins and
* published once the Host accepts the prompt, so the conversation shows the
* message before its durable admission completes. Client-memory only — reload
* and reconnect rebuild the conversation from durable events alone.
*/
export interface PendingSubmission {
/** The prompt RPC identity; the durable `user/message` source echoes it as `rpcId`. */
@@ -68,7 +68,7 @@ export interface PromptError {
/** Immutable Session lifecycle and control snapshot. */
export interface SessionSnapshot {
readonly sessionId: SessionId
/** Local prompt-submission echoes not yet observed as durable events or queue occurrences. */
/** Local prompt-submission echoes the Host accepted and that are not yet observed as durable events or queue occurrences. */
readonly pendingSubmissions: readonly PendingSubmission[]
readonly running: boolean
readonly subagent: {
@@ -3,7 +3,7 @@
import type { Context } from '@deepseek-ai/cordis'
import type { InboxState, InboxTarget } from '@deepseek-ai/dsh-agent/types'
import { randomUUID } from '@deepseek-ai/dsh-util-crypto'
import type { AttachmentIdType, FileAttachmentRef, ImageAttachmentRef } from '@deepseek-ai/dsh-attachment'
import type { FileAttachmentRef, ImageAttachmentRef } from '@deepseek-ai/dsh-attachment'
import type { SubagentAddress } from '@deepseek-ai/dsh-subagent/client'
import type { MessageId } from '@deepseek-ai/dsh-llm/brand'
import type { UserMessage } from '@deepseek-ai/dsh-llm/types'
@@ -113,8 +113,8 @@ export class Session implements SessionFace {
private address: SubagentAddress | undefined
/**
* Sticky send marker, private input of the composerPhase derivation: set
* synchronously before prompt()'s first await, never reset — the blank →
* engaging edge of the phase machine (see ComposerPhase).
* when the Host accepts a prompt, never reset — the blank → engaging edge of
* the phase machine (see ComposerPhase).
*/
private promptAttempted = false
/** A first accepted prompt stays in the engaging phase until its turn is observable. */
@@ -124,8 +124,10 @@ export class Session implements SessionFace {
private removed = false
private promptError: PromptError | null = null
private lastAgentError: string | null = null
/** Local submission echoes, insertion-ordered (see SessionSnapshot.pendingSubmissions). */
/** Local submission echoes, insertion-ordered, including those the Host has not accepted yet. */
private pendingSubmissions: readonly PendingSubmission[] = []
/** The accepted echoes, published as SessionSnapshot.pendingSubmissions; rebuilt when one is accepted or removed. */
private acceptedSubmissions: readonly PendingSubmission[] = []
/** Per-echo settlement state; `retiring` latches the first observation so a
* Inbox projection and its durable event cannot both retire one echo. */
private readonly submissionSettlements = new Map<SessionRequestId, {
@@ -139,6 +141,9 @@ export class Session implements SessionFace {
}
/** Durable admission may precede the Inbox projection acknowledging its claim. */
admitted?: readonly (ImageAttachmentRef | FileAttachmentRef)[]
/** Whether the Host accepted the prompt; the echo is published only from then on. */
accepted: boolean
readonly onAccept?: (() => void) | undefined
readonly onRetire?: ((retirement: PendingSubmissionRetirement) => void) | undefined
retiring: boolean
}>()
@@ -215,9 +220,10 @@ export class Session implements SessionFace {
// ---- Operations ----
/**
* Register one local submission echo (see the ISession declaration).
* Synchronous through markDirty: the echo is in the very next snapshot, so
* the conversation can paint it before the caller starts serializing.
* Register one local submission echo (see the ISession declaration). The
* echo reconciles against Host events from now on, but it enters the
* snapshot only once the prompt receipt or an Inbox row carrying its
* request id shows that the Host accepted it.
* @param input - echo content and the optional settlement callback.
* @returns the minted identity for {@link prompt} plus the pre-prompt abandon path.
*/
@@ -231,16 +237,17 @@ export class Session implements SessionFace {
text: input.text,
attachments: input.attachments,
}]
this.submissionSettlements.set(requestId, { placement, onRetire: input.onRetire, retiring: false })
// The blank → engaging edge flips here, ahead of prompt(): the composer
// docks and the echo renders on the click's own frame.
this.promptAttempted = true
this.notifier.markDirty()
this.submissionSettlements.set(requestId, {
placement, accepted: false, onAccept: input.onAccept, onRetire: input.onRetire, retiring: false,
})
return { requestId, abandon: () => { this.retireFailedSubmission(requestId) } }
}
/**
* Send (queue/steer passed through 1:1); failures land in the snapshot's promptError.
* Send (queue/steer passed through 1:1); failures land in the snapshot's
* promptError. The Host can hold the prompt at its admission stage, so the
* call can stay open; the echo appears and the blank → engaging edge flips
* only once the Host accepts the prompt.
* @param content - text, browser-owned temporary image uploads, and staged-file receipts.
* @param mode - queue appends after the current turn; steer interrupts it.
* @param signal - optional caller cancellation for the complete admission round-trip.
@@ -255,12 +262,9 @@ export class Session implements SessionFace {
): Promise<RemoteResult<{ accepted: true }>> {
this.promptError = null
this.lastAgentError = null
// Synchronous, before the first await: the blank → engaging edge must be
// visible on the session area's very first frame when a caller sends
// ahead of navigation (first-send flow).
this.promptAttempted = true
if (this.blankBit) this.firstPromptPendingTurn = true
this.notifier.markDirty()
// Before the first await, so a turn observed ahead of the receipt clears it.
const firstPrompt = this.blankBit && !this.firstPromptPendingTurn
if (firstPrompt) this.firstPromptPendingTurn = true
let result: RemoteResult<{ accepted: true }>
if (this.address === undefined) {
const clientTimeZone = resolvedClientTimeZone()
@@ -294,31 +298,50 @@ export class Session implements SessionFace {
result = routed.ok ? { ok: true, value: { accepted: true } } : routed
}
if (!result.ok) {
// Rejection must leave a first prompt blank and eligible for workspace reuse.
if (firstPrompt) this.firstPromptPendingTurn = false
if (requestId !== undefined) this.retireFailedSubmission(requestId)
this.promptError = { op: 'send', error: result.error }
this.notifier.markDirty()
return result
}
// Rejection must leave a first prompt blank and eligible for workspace reuse.
if (this.blankBit) {
this.blankBit = false
this.notifier.markDirty()
}
this.options.onEngaged?.(this)
this.acceptPrompt(requestId)
return result
}
/**
* Resolve one image referenced by this session into browser-consumable bytes.
* @param attachmentId - opaque id found in the folded session log.
* @returns the authenticated reference and decoded bytes.
* Apply the Host's acceptance of one prompt: publish its echo, move a blank
* Session to engaging, and tell the submitter. The prompt receipt and an
* Inbox row carrying the prompt's request id both prove acceptance.
* Whichever arrives first publishes the echo; a later proof for an echo
* still pending changes nothing.
* @param requestId - identity from {@link beginSubmission}; absent for a prompt sent without an echo.
*/
private acceptPrompt(requestId: SessionRequestId | undefined): void {
const settlement = requestId === undefined ? undefined : this.submissionSettlements.get(requestId)
if (settlement?.accepted === true) return
if (settlement !== undefined) {
settlement.accepted = true
this.refreshAcceptedSubmissions()
}
this.promptAttempted = true
this.blankBit = false
this.notifier.markDirty()
this.options.onEngaged?.(this)
settlement?.onAccept?.()
}
/**
* Resolve one stored image into browser-consumable bytes using this Session's gateway permission scope.
* @param attachment - complete image reference from the folded session log.
* @returns the supplied reference after storage verification, and decoded bytes.
*/
async readAttachment(
attachmentId: AttachmentIdType,
attachment: ImageAttachmentRef,
): Promise<RemoteResult<{ attachment: ImageAttachmentRef; data: Uint8Array }>> {
const result = await this.remote.session.attachment({
sessionId: this.sessionId,
attachmentId,
attachment,
})
if (!result.ok) return result
const binary = atob(result.value.data)
@@ -755,7 +778,10 @@ export class Session implements SessionFace {
}
}
this.observeSubmissionInsertions(target, inserted, start, event.seq)
for (const message of inserted) this.observeSubmissionMessage(message, false)
// A queued echo waits for the inbox projection the queue dock renders, which can arrive after this
// event; retiring it here would empty the dock for a frame. A claim in the same projection notification
// still retires it through the durable user message.
for (const message of inserted) this.observeSubmissionMessage(message, false, false)
return
}
if (event.type === 'request/context' || event.type === 'turn/end') {
@@ -773,19 +799,24 @@ export class Session implements SessionFace {
const source = message.source
if (source.kind !== 'user' || !('rpcId' in source)) continue
const settlement = this.submissionSettlements.get(source.rpcId)
if (settlement === undefined || settlement.placement === 'queued'
|| settlement.retiring || (settlement.receipt?.seq ?? -1) > seq) continue
if (settlement === undefined || settlement.retiring || (settlement.receipt?.seq ?? -1) > seq) continue
// Either proof accepts a prompt; an Inbox row can reach the Client before the prompt receipt. Later splices
// move the recorded position, so a canceled splice that removes the row retires its echo, queued ones
// included; a queued echo otherwise leaves once the queue dock renders its row or its message is admitted.
settlement.receipt = { target, seq, index: start + index, attachments: attachmentRefsIn(message.content) }
this.acceptPrompt(source.rpcId)
}
}
private observeSubmissionMessage(message: UserMessage, admitted: boolean): void {
private observeSubmissionMessage(message: UserMessage, admitted: boolean, retireQueued = true): void {
const source = message.source
if (source.kind !== 'user' || !('rpcId' in source)) return
const settlement = this.submissionSettlements.get(source.rpcId)
if (settlement === undefined || settlement.retiring) return
if (!admitted) {
if (settlement.placement === 'queued') this.scheduleObservedRetirement(source.rpcId, attachmentRefsIn(message.content))
if (settlement.placement === 'queued' && retireQueued) {
this.scheduleObservedRetirement(source.rpcId, attachmentRefsIn(message.content))
}
return
}
settlement.admitted = attachmentRefsIn(message.content)
@@ -839,6 +870,11 @@ export class Session implements SessionFace {
this.finishSubmission(requestId, { reason: 'failed' })
}
private refreshAcceptedSubmissions(): void {
this.acceptedSubmissions = this.pendingSubmissions
.filter(echo => this.submissionSettlements.get(echo.requestId)?.accepted === true)
}
/** Single removal point: drop the echo, publish, then notify the owner. */
private finishSubmission(requestId: SessionRequestId, retirement: PendingSubmissionRetirement): void {
const settlement = this.submissionSettlements.get(requestId)
@@ -846,6 +882,7 @@ export class Session implements SessionFace {
if (settlement === undefined) return
this.submissionSettlements.delete(requestId)
this.pendingSubmissions = this.pendingSubmissions.filter(echo => echo.requestId !== requestId)
if (settlement.accepted) this.refreshAcceptedSubmissions()
this.notifier.markDirty()
settlement.onRetire?.(retirement)
}
@@ -867,7 +904,7 @@ export class Session implements SessionFace {
const identity = this.projections.values().subagent
return {
sessionId: this.sessionId,
pendingSubmissions: this.pendingSubmissions,
pendingSubmissions: this.acceptedSubmissions,
running: this.running,
subagent: this.address === undefined
? null
@@ -885,7 +922,8 @@ export class Session implements SessionFace {
blank: this.blankBit,
lastAgentError: this.lastAgentError,
promptAttempted: this.promptAttempted,
awaitingFirstTurn: this.firstPromptPendingTurn,
// The flag is set before the receipt; the Session leaves blank only once the Host accepted the prompt.
awaitingFirstTurn: this.firstPromptPendingTurn && !this.blankBit,
}
}
@@ -178,6 +178,7 @@ export class SessionEventStream extends RemoteJournalStream<
let assistantRevision: number | undefined
for await (const frame of this.remote.session.follow({
address: this.address,
activate: true,
assistantStream: true,
...this.repairRequest(request),
}, signal)) {