feat: align conversation state, composer and scoped approvals
This commit is contained in:
1 parent
771ca540f7
commit
308b98df23
61 files changed
+1701
-292
No files matched your search
@@ -8,12 +8,12 @@ import {
|
||||
tool,
|
||||
type CanUseTool,
|
||||
type PermissionResult,
|
||||
type PermissionUpdate,
|
||||
type Query,
|
||||
type SDKMessage,
|
||||
type SDKUserMessage
|
||||
} from '@anthropic-ai/claude-agent-sdk'
|
||||
import { z } from 'zod'
|
||||
import { sessionGrantProposal, sessionGrantRules, rememberSessionGrant, clearSessionGrants } from './session-grants'
|
||||
import type { AgentMode, PermissionMode } from '@shared/conversation'
|
||||
import type { ChatEvent, ContextUsageBreakdown, MessagePart, TokenUsage, ToolCallRecord } from '@shared/types'
|
||||
import type { ResolvedModel } from '@shared/models'
|
||||
@@ -327,6 +327,7 @@ export class ClaudeKernel {
|
||||
private consuming: Promise<void> | null = null
|
||||
private active: ActiveTurn | null = null
|
||||
private starting = false
|
||||
private permissionUpdates: Promise<void> = Promise.resolve()
|
||||
private draining: Promise<void> | null = null
|
||||
private contextQuery: ReturnType<Query['getContextUsage']> | null = null
|
||||
private lastRequestId?: string
|
||||
@@ -394,6 +395,17 @@ export class ClaudeKernel {
|
||||
canUseTool: this.canUseTool,
|
||||
hooks: {
|
||||
PreToolUse: [{
|
||||
hooks: [async (hook) => {
|
||||
if (hook.hook_event_name !== 'PreToolUse') return {}
|
||||
const input = hook.tool_input && typeof hook.tool_input === 'object' ? hook.tool_input as Record<string, unknown> : {}
|
||||
// SDK allow rules can bypass canUseTool, but never this host mode boundary.
|
||||
if (!this.active || this.active.request.signal.aborted || (this.agentMode !== 'craft' && !isModeAllowedCall(this.agentMode, hook.tool_name, input))) {
|
||||
return { hookSpecificOutput: { hookEventName: 'PreToolUse' as const, permissionDecision: 'deny' as const,
|
||||
permissionDecisionReason: '当前回合或对话模式不允许此操作。' } }
|
||||
}
|
||||
return {}
|
||||
}]
|
||||
}, {
|
||||
matcher: 'ExitPlanMode',
|
||||
hooks: [async (input) => {
|
||||
const active = await this.waitForSegment()
|
||||
@@ -503,6 +515,20 @@ export class ClaudeKernel {
|
||||
void consuming.finally(() => {
|
||||
if (this.consuming === consuming) this.consuming = null
|
||||
})
|
||||
const grants = sessionGrantRules(this.options.sessionId, this.options.workspaceRoot)
|
||||
if (grants.length) {
|
||||
try { await instance.applyFlagSettings({ permissions: { allow: grants } }) }
|
||||
catch (error) {
|
||||
// Fail closed: no prompt is queued on a kernel whose replay was not acknowledged.
|
||||
instance.close()
|
||||
if (this.instance === instance) { this.instance = null; this.input = null; this.processAbort = null }
|
||||
// A new run must not race the old consumer's terminal cleanup.
|
||||
const draining = consuming.catch(() => undefined)
|
||||
this.draining = draining
|
||||
void draining.finally(() => { if (this.draining === draining) this.draining = null })
|
||||
throw new Error(`恢复本会话授权失败,未执行请求:${error instanceof Error ? error.message : String(error)}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async consume(instance: Query): Promise<void> {
|
||||
@@ -540,13 +566,14 @@ export class ClaudeKernel {
|
||||
}
|
||||
return { behavior: 'deny', message: '计划已显示在 YCode 中,请切换到 craft 后开始实施。' }
|
||||
}
|
||||
if (this.agentMode === 'ask' && !isReadOnlyCall(toolName, input)) {
|
||||
if (this.agentMode === 'ask' && !isModeAllowedCall('ask', toolName, input)) {
|
||||
return { behavior: 'deny', message: 'Ask 模式只允许读取资料、搜索和提问,不能修改文件或执行命令。' }
|
||||
}
|
||||
if (this.agentMode === 'plan' && !isReadOnlyCall(toolName, input)) {
|
||||
if (this.agentMode === 'plan' && !isModeAllowedCall('plan', toolName, input)) {
|
||||
return { behavior: 'deny', message: 'Plan 模式只允许分析和制定计划,不能修改文件或执行命令。' }
|
||||
}
|
||||
if (toolName === 'AskUserQuestion') {
|
||||
active.request.emit({ runId: active.request.runId, type: 'progress', progress: { source: 'interaction', phase: 'waiting-answer', toolName } })
|
||||
const questions = questionsFromInput(input)
|
||||
if (!questions.length) return { behavior: 'deny', message: '问题内容为空。' }
|
||||
const response = await active.request.interactions.question(
|
||||
@@ -562,33 +589,67 @@ export class ClaudeKernel {
|
||||
for (const answer of response.answer.questions) {
|
||||
answers[questionText.get(answer.id) ?? answer.id] = answer.answers.join(', ')
|
||||
}
|
||||
if (this.active !== active || active.settled || options.signal.aborted || active.request.signal.aborted) return { behavior: 'deny', message: '操作已取消。' }
|
||||
active.request.emit({ runId: active.request.runId, type: 'progress', progress: { source: 'agent', phase: 'thinking' } })
|
||||
return { behavior: 'allow', updatedInput: { ...input, answers } }
|
||||
}
|
||||
if (this.permissionMode === 'full-access' && this.agentMode === 'craft' && !mustAskInFullAccess(options)) {
|
||||
return { behavior: 'allow', updatedInput: input }
|
||||
}
|
||||
const grant = !options.suppressAlwaysAllowRule ? sessionGrantProposal(options.suggestions) : undefined
|
||||
const optionsForApproval = [
|
||||
{ optionId: 'allow-once', name: '允许一次', kind: 'allow_once' },
|
||||
...(!options.suppressAlwaysAllowRule && options.suggestions?.length
|
||||
? [{ optionId: 'allow-always', name: '始终允许', kind: 'allow_always' }]
|
||||
...(grant
|
||||
? [{ optionId: 'allow-always', name: '本次会话内始终允许', kind: 'allow_always' }]
|
||||
: []),
|
||||
{ optionId: 'reject-once', name: '拒绝', kind: 'reject_once' }
|
||||
]
|
||||
active.request.emit({ runId: active.request.runId, type: 'progress', progress: { source: 'interaction', phase: 'waiting-approval', toolName } })
|
||||
const response = await active.request.interactions.approval(
|
||||
options.toolUseID,
|
||||
options.title ?? options.displayName ?? `运行 ${toolName}`,
|
||||
optionsForApproval,
|
||||
options.description,
|
||||
options.signal
|
||||
options.signal,
|
||||
{ requestKind: options.matchedAskRule ? 'rule-intercept' : 'permission', toolName,
|
||||
...(options.matchedAskRule ? { rule: { description: `${options.matchedAskRule.source}: ${options.matchedAskRule.toolName}${options.matchedAskRule.ruleContent ? `(${options.matchedAskRule.ruleContent})` : ''}` } } : {}),
|
||||
...(typeof input.command === 'string' ? { command: input.command } : {}),
|
||||
...(typeof input.file_path === 'string' ? { target: input.file_path } : {}),
|
||||
...(options.decisionReason || options.description ? { reason: options.decisionReason || options.description } : {}),
|
||||
defaultToNo: options.defaultToNo,
|
||||
...(grant ? { suggestedRules: grant.rules } : options.suggestions?.length ? { sessionUnavailableReason: '当前建议包含无法安全恢复的授权类型,仅支持允许一次。' } : {}),
|
||||
allowScope: optionsForApproval.some(option => option.kind === 'allow_always') ? 'session' : 'once' },
|
||||
async decision => {
|
||||
if (decision.kind !== 'approval' || decision.optionId !== 'allow-always') return
|
||||
if (!grant) throw new Error('会话授权不可用。')
|
||||
const apply = async (): Promise<void> => {
|
||||
if (this.active !== active || active.settled || options.signal.aborted || active.request.signal.aborted) throw abortError()
|
||||
const instance = this.instance
|
||||
if (!instance) throw new Error('内核已断开,授权未应用。')
|
||||
const rules = [...new Set([...sessionGrantRules(this.options.sessionId, this.options.workspaceRoot), ...grant.rules])]
|
||||
await instance.applyFlagSettings({ permissions: { allow: rules } })
|
||||
if (this.active !== active || active.settled || options.signal.aborted || active.request.signal.aborted) {
|
||||
instance.close()
|
||||
throw abortError()
|
||||
}
|
||||
rememberSessionGrant(this.options.sessionId, grant, this.options.workspaceRoot)
|
||||
}
|
||||
const applying = this.permissionUpdates.then(apply)
|
||||
this.permissionUpdates = applying.catch(() => undefined)
|
||||
await applying
|
||||
}
|
||||
)
|
||||
if (this.active !== active || active.settled || options.signal.aborted || active.request.signal.aborted) return { behavior: 'deny', message: '操作已取消。' }
|
||||
if (response.kind !== 'approval' || response.optionId === 'reject-once') {
|
||||
return { behavior: 'deny', message: '用户拒绝了此操作。' }
|
||||
}
|
||||
if (response.optionId === 'allow-always' && !grant) return { behavior: 'deny', message: '会话授权不可用。' }
|
||||
active.request.emit({ runId: active.request.runId, type: 'progress', progress: { source: 'agent', phase: 'tool', toolName } })
|
||||
return {
|
||||
behavior: 'allow',
|
||||
updatedInput: input,
|
||||
...(response.optionId === 'allow-always' && options.suggestions?.length
|
||||
? { updatedPermissions: options.suggestions as PermissionUpdate[] }
|
||||
...(response.optionId === 'allow-always' && grant
|
||||
? { updatedPermissions: grant.updates }
|
||||
: {})
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -654,6 +715,7 @@ export class ClaudeKernel {
|
||||
if (!active) return
|
||||
const index = typeof event.index === 'number' ? event.index : 0
|
||||
if (event.type === 'message_start') {
|
||||
active.request.emit({ runId: active.request.runId, type: 'progress', progress: { source: 'agent', phase: 'thinking' } })
|
||||
active.assistantSequence++
|
||||
const message = event.message as Record<string, unknown> | undefined
|
||||
active.nativeUsage = message?.usage && typeof message.usage === 'object' ? message.usage as Record<string, unknown> : undefined
|
||||
@@ -734,6 +796,7 @@ export class ClaudeKernel {
|
||||
argsRaw: JSON.stringify(item.input ?? {}),
|
||||
status: 'running'
|
||||
}
|
||||
active.request.emit({ runId: active.request.runId, type: 'progress', progress: { source: 'agent', phase: 'tool', toolName: item.name } })
|
||||
active.tools.set(item.id, record)
|
||||
active.toolOrder.push(record)
|
||||
active.request.emit({ runId: active.request.runId, type: 'tool_call', call: { ...record } })
|
||||
@@ -769,6 +832,7 @@ export class ClaudeKernel {
|
||||
if (value.type === 'result') {
|
||||
const active = this.active
|
||||
if (!active) return
|
||||
active.request.emit({ runId: active.request.runId, type: 'progress', progress: { source: 'agent', phase: 'finishing' } })
|
||||
// A result aggregates multiple API calls; it is not the last request's context size.
|
||||
const usage = active.streamUsage
|
||||
if (usage && !active.request.command) active.request.emit({ runId: active.request.runId, type: 'usage', usage })
|
||||
@@ -891,6 +955,7 @@ export class ClaudeKernel {
|
||||
originalReject(error)
|
||||
}
|
||||
this.active = active
|
||||
request.emit({ runId: request.runId, type: 'progress', progress: { source: 'agent', phase: 'thinking' } })
|
||||
this.lastRequestId = request.runId
|
||||
this.lastGroupKey = request.userMessageId
|
||||
try {
|
||||
@@ -1037,6 +1102,11 @@ function mustAskInFullAccess(options: Parameters<CanUseTool>[2]): boolean {
|
||||
(raw.decision_reason_code != null && raw.decision_reason_code !== '')
|
||||
}
|
||||
|
||||
function isModeAllowedCall(mode: AgentMode, toolName: string, input: Record<string, unknown>): boolean {
|
||||
if (mode === 'ask' && ['TodoWrite', 'EnterPlanMode'].includes(toolName)) return false
|
||||
return isReadOnlyCall(toolName, input)
|
||||
}
|
||||
|
||||
function isReadOnlyCall(toolName: string, input: Record<string, unknown>): boolean {
|
||||
if (READ_ONLY_TOOLS.has(toolName)) return true
|
||||
if (['mcp__ycode_capabilities__list_capabilities', 'mcp__ycode_capabilities__load_skill', 'mcp__ycode_capabilities__read_skill_resource'].includes(toolName)) return true
|
||||
@@ -1116,6 +1186,7 @@ export async function disposeClaudeKernels(): Promise<void> {
|
||||
shuttingDown = true
|
||||
const entries = [...kernels.values()]
|
||||
kernels.clear()
|
||||
clearSessionGrants()
|
||||
sessionViews.clear()
|
||||
await Promise.all(entries.map(({ kernel }) => kernel.close().catch(() => undefined)))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import type { PermissionUpdate } from '@anthropic-ai/claude-agent-sdk'
|
||||
|
||||
/** Process-lifetime YCode session scope. Never persisted to user/project settings. */
|
||||
const sessions = new Map<string, string[]>()
|
||||
export interface SessionGrantProposal { updates: PermissionUpdate[]; rules: string[] }
|
||||
|
||||
/** Preserve the ENTIRE suggested rule group or offer once only; never partial grants.
|
||||
* Only scoped additive allow rules can be replayed losslessly through SDK flag settings.
|
||||
* Mode/directory changes and unscoped tool grants deliberately require another approval.
|
||||
*/
|
||||
export function sessionGrantProposal(updates: PermissionUpdate[] | undefined): SessionGrantProposal | undefined {
|
||||
if (!updates?.length) return undefined
|
||||
const rules: string[] = []
|
||||
const normalized: PermissionUpdate[] = []
|
||||
for (const update of updates) {
|
||||
if (update.type !== 'addRules' || update.behavior !== 'allow' || !update.rules.length) return undefined
|
||||
for (const rule of update.rules) {
|
||||
if (!/^[A-Za-z0-9_-]+$/.test(rule.toolName) || !rule.ruleContent?.trim() || /[\r\n\0]/.test(rule.ruleContent)) return undefined
|
||||
rules.push(`${rule.toolName}(${rule.ruleContent})`)
|
||||
}
|
||||
normalized.push({ ...structuredClone(update), destination: 'session' })
|
||||
}
|
||||
return { updates: normalized, rules: [...new Set(rules)] }
|
||||
}
|
||||
function scopeKey(sessionId: string, workspaceRoot: string): string { return JSON.stringify([sessionId, workspaceRoot]) }
|
||||
export function rememberSessionGrant(sessionId: string, proposal: SessionGrantProposal, workspaceRoot = ''): void {
|
||||
const key = scopeKey(sessionId, workspaceRoot)
|
||||
sessions.set(key, [...new Set([...(sessions.get(key) ?? []), ...proposal.rules])])
|
||||
}
|
||||
export function sessionGrantRules(sessionId: string, workspaceRoot = ''): string[] { return [...(sessions.get(scopeKey(sessionId, workspaceRoot)) ?? [])] }
|
||||
export function clearSessionGrants(sessionId?: string): void {
|
||||
if (sessionId) { for (const key of sessions.keys()) if (JSON.parse(key)[0] === sessionId) sessions.delete(key) } else sessions.clear()
|
||||
}
|
||||
@@ -237,7 +237,7 @@ export function createChatTurnService({ projects: projectRepository, sessionRoot
|
||||
const createCheckpoint = () => createMessageCheckpoint(
|
||||
(message) => sessions.patchMessage(sessionId, message.id, {
|
||||
content: message.content, tools: message.tools, parts: message.parts, interactions: message.interactions,
|
||||
status: message.status, turnStartedAt: message.turnStartedAt, turnDurationMs: message.turnDurationMs,
|
||||
status: message.status, turnStartedAt: message.turnStartedAt, turnDurationMs: message.turnDurationMs, turnFinishedAt: message.turnFinishedAt,
|
||||
contextUsage: message.contextUsage, contextBreakdown: message.contextBreakdown, error: message.error,
|
||||
claudeEntryId: message.claudeEntryId
|
||||
}),
|
||||
@@ -249,7 +249,7 @@ export function createChatTurnService({ projects: projectRepository, sessionRoot
|
||||
const emit = (event: ChatEvent): void => {
|
||||
const isTerminal = event.type === 'done' || event.type === 'cancelled' || event.type === 'error'
|
||||
let observedEvent: ChatEvent = isTerminal
|
||||
? { ...event, sequence: ++eventSequence, turnDurationMs: Math.max(0, Date.now() - userMessage.createdAt) }
|
||||
? { ...event, sequence: ++eventSequence, turnFinishedAt: Date.now(), turnDurationMs: Math.max(0, Date.now() - userMessage.createdAt) }
|
||||
: { ...event, sequence: ++eventSequence }
|
||||
if (observedEvent.type === 'usage') {
|
||||
observedEvent = { ...observedEvent, contextWindow: contextWindowAtStart, source: 'upstream', sampledAt: Date.now(), modelId: assistant.modelId }
|
||||
@@ -274,6 +274,7 @@ export function createChatTurnService({ projects: projectRepository, sessionRoot
|
||||
|
||||
const task = Promise.resolve().then(async () => {
|
||||
try {
|
||||
emit({ runId, type: 'progress', progress: { source: 'preparation', phase: 'preparing' } })
|
||||
await sessions.setClaudeFork(sessionId, forkAt, forceNew)
|
||||
if (payload.action) await releaseClaudeKernel(sessionId, true)
|
||||
const kernel = await getClaudeKernel({
|
||||
@@ -326,7 +327,7 @@ export function createChatTurnService({ projects: projectRepository, sessionRoot
|
||||
emit, interactions: interactionBroker, signal: controller.signal, ownerWebContentsId: recipient?.id,
|
||||
onSegmentComplete: async result => {
|
||||
assistant = { ...assistant, content: result.content, tools: result.tools, claudeEntryId: result.entryId,
|
||||
status: 'done', turnDurationMs: Math.max(0, Date.now() - userMessage.createdAt), turnStartedAt: undefined }
|
||||
status: 'done', runtimeProgress: undefined, turnFinishedAt: Date.now(), turnDurationMs: Math.max(0, Date.now() - userMessage.createdAt), turnStartedAt: undefined }
|
||||
betweenSegments = true
|
||||
await checkpoint.finish(assistant)
|
||||
},
|
||||
@@ -371,7 +372,7 @@ export function createChatTurnService({ projects: projectRepository, sessionRoot
|
||||
controller.abort()
|
||||
try {
|
||||
if (terminalEvent) {
|
||||
terminalEvent = { ...terminalEvent, sequence: ++eventSequence, turnDurationMs: Math.max(0, Date.now() - userMessage.createdAt) }
|
||||
terminalEvent = { ...terminalEvent, sequence: ++eventSequence, turnFinishedAt: terminalEvent.turnFinishedAt ?? Date.now(), turnDurationMs: Math.max(0, Date.now() - userMessage.createdAt) }
|
||||
if (!betweenSegments) assistant = applyAssistantEvent(assistant, terminalEvent)
|
||||
}
|
||||
await checkpoint.finish(assistant)
|
||||
|
||||
@@ -77,6 +77,24 @@ function decryptKey(stored: StoredConfig): { key: string; storage: 'encrypted' |
|
||||
return { key: '', storage: 'none' }
|
||||
}
|
||||
|
||||
/** Only migrate explicitly stored legacy model settings, never in-memory defaults. */
|
||||
export async function getLegacyModelConfig(): Promise<GatewayConfig | null> {
|
||||
let raw: string
|
||||
try { raw = await fs.readFile(filePath(), 'utf8') }
|
||||
catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') return null; throw error }
|
||||
const stored = JSON.parse(raw) as Partial<StoredConfig> | null
|
||||
if (!stored || typeof stored !== 'object' || Array.isArray(stored)) throw new Error('旧配置文件内容无效')
|
||||
if (typeof stored.model !== 'string' || !stored.model.trim()
|
||||
|| typeof stored.baseUrl !== 'string' || !stored.baseUrl.trim()) return null
|
||||
// Older preference writes also serialized DEFAULTS; without credentials they are not a configured model.
|
||||
if (stored.model === DEFAULTS.model && stored.baseUrl === DEFAULTS.baseUrl && !stored.apiKeyEnc && !stored.apiKeyPlain) return null
|
||||
const complete = { ...DEFAULTS, ...stored } as StoredConfig
|
||||
const { key } = decryptKey(complete)
|
||||
return { baseUrl: complete.baseUrl, model: complete.model, apiKey: key,
|
||||
temperature: complete.temperature, maxToolRounds: complete.maxToolRounds,
|
||||
speechBaseUrl: complete.speechBaseUrl, speechModel: complete.speechModel }
|
||||
}
|
||||
|
||||
/** 供主进程内部使用:拿到可直接请求上游的完整配置 */
|
||||
export async function getGatewayConfig(): Promise<GatewayConfig> {
|
||||
const stored = await load()
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import type { ChatInteraction, InteractionResponse, PermissionOption, QuestionItem } from '@shared/conversation'
|
||||
import type { ApprovalDetails, ChatInteraction, InteractionResponse, PermissionOption, QuestionItem } from '@shared/conversation'
|
||||
import type { ChatEvent } from '@shared/types'
|
||||
|
||||
interface PendingInteraction { request: ChatInteraction; resolve(value: InteractionResponse): void; reject(error: Error): void; detach(): void }
|
||||
interface PendingInteraction { applying?: boolean; beforeResolve?: (response: InteractionResponse) => Promise<void>; request: ChatInteraction; resolve(value: InteractionResponse): void; reject(error: Error): void; detach(): void }
|
||||
const pending = new Map<string, PendingInteraction>()
|
||||
|
||||
function abortError(): Error { return Object.assign(new Error('交互已取消'), { name: 'AbortError' }) }
|
||||
|
||||
export function createInteractionBroker(runId: string, signal: AbortSignal, emit: (event: ChatEvent) => void) {
|
||||
async function request(input: ChatInteraction, cancellation?: AbortSignal): Promise<InteractionResponse> {
|
||||
async function request(input: ChatInteraction, cancellation?: AbortSignal, beforeResolve?: (response: InteractionResponse) => Promise<void>): Promise<InteractionResponse> {
|
||||
const activeSignal = cancellation ? AbortSignal.any([signal, cancellation]) : signal
|
||||
if (activeSignal.aborted) throw abortError()
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -20,7 +20,7 @@ export function createInteractionBroker(runId: string, signal: AbortSignal, emit
|
||||
emit({ runId, type: 'interaction', interaction: { ...input, status: 'cancelled' } })
|
||||
reject(abortError())
|
||||
}
|
||||
pending.set(input.id, { request: input, resolve, reject, detach: () => activeSignal.removeEventListener('abort', onAbort) })
|
||||
pending.set(input.id, { beforeResolve, request: input, resolve, reject, detach: () => activeSignal.removeEventListener('abort', onAbort) })
|
||||
activeSignal.addEventListener('abort', onAbort, { once: true })
|
||||
emit({ runId, type: 'interaction', interaction: input })
|
||||
})
|
||||
@@ -34,9 +34,9 @@ export function createInteractionBroker(runId: string, signal: AbortSignal, emit
|
||||
if (response.kind !== 'question') throw new Error('问答响应类型不匹配')
|
||||
return response
|
||||
},
|
||||
async approval(toolCallId: string, title: string, options: PermissionOption[], scope?: string, cancellation?: AbortSignal) {
|
||||
async approval(toolCallId: string, title: string, options: PermissionOption[], scope?: string, cancellation?: AbortSignal, details?: ApprovalDetails, beforeResolve?: (response: InteractionResponse) => Promise<void>) {
|
||||
const id = randomUUID()
|
||||
const response = await request({ id, requestId: id, runId, toolCallId, kind: 'approval', title, options, scope, status: 'pending' }, cancellation)
|
||||
const response = await request({ id, requestId: id, runId, toolCallId, kind: 'approval', title, options, scope, details, status: 'pending' }, cancellation, beforeResolve)
|
||||
if (response.kind !== 'approval') throw new Error('审批响应类型不匹配')
|
||||
return response
|
||||
}
|
||||
@@ -45,9 +45,9 @@ export function createInteractionBroker(runId: string, signal: AbortSignal, emit
|
||||
export type InteractionBroker = ReturnType<typeof createInteractionBroker>
|
||||
|
||||
/** Exact run/id binding makes stale or duplicated button submissions harmless. */
|
||||
export function respondToInteraction(runId: string, id: string, response: InteractionResponse, emit: (event: ChatEvent) => void): boolean {
|
||||
export function respondToInteraction(runId: string, id: string, response: InteractionResponse, emit: (event: ChatEvent) => void): boolean | Promise<boolean> {
|
||||
const entry = pending.get(id)
|
||||
if (!entry || entry.request.runId !== runId) return false
|
||||
if (!entry || entry.applying || entry.request.runId !== runId) return false
|
||||
const request = entry.request
|
||||
if (request.kind !== response.kind) throw new Error('交互响应类型不匹配')
|
||||
let resolved: ChatInteraction
|
||||
@@ -73,9 +73,24 @@ export function respondToInteraction(runId: string, id: string, response: Intera
|
||||
finalResponse = { ...response, answer }
|
||||
resolved = { ...request, answer, skipped: response.skipped, status: 'answered' }
|
||||
} else throw new Error('交互响应类型不匹配')
|
||||
pending.delete(id)
|
||||
entry.detach()
|
||||
emit({ runId, type: 'interaction', interaction: resolved })
|
||||
entry.resolve(finalResponse)
|
||||
return true
|
||||
const finish = (): boolean => {
|
||||
if (pending.get(id) !== entry) return false
|
||||
pending.delete(id)
|
||||
entry.detach()
|
||||
emit({ runId, type: 'interaction', interaction: resolved })
|
||||
entry.resolve(finalResponse)
|
||||
return true
|
||||
}
|
||||
if (entry.beforeResolve) {
|
||||
entry.applying = true
|
||||
emit({ runId, type: 'interaction', interaction: { ...request, status: 'responding' } })
|
||||
return Promise.resolve().then(() => entry.beforeResolve!(finalResponse)).then(finish, error => {
|
||||
if (pending.get(id) === entry) {
|
||||
entry.applying = false
|
||||
emit({ runId, type: 'interaction', interaction: { ...request, status: 'error', error: error instanceof Error ? error.message : String(error) } })
|
||||
}
|
||||
throw error
|
||||
})
|
||||
}
|
||||
return finish()
|
||||
}
|
||||
+2
-2
@@ -306,9 +306,9 @@ export function registerIpc(getWindow: () => BrowserWindow | null): void {
|
||||
return readWorkspaceResource(dirname(attachment.path!), attachment.name)
|
||||
})
|
||||
ipcMain.handle('speech:transcribe', async (_event, payload: { audioBase64: string; mimeType: string }) => transcribeAudio(await config.getGatewayConfig(), payload))
|
||||
ipcMain.handle('chat:respond', (_event, runId: string, interactionId: string, response: InteractionResponse) => {
|
||||
ipcMain.handle('chat:respond', async (_event, runId: string, interactionId: string, response: InteractionResponse) => {
|
||||
const emit = runEmitters.get(runId)
|
||||
return { accepted: !!emit && respondToInteraction(runId, interactionId, response, emit) }
|
||||
return { accepted: !!emit && await respondToInteraction(runId, interactionId, response, emit) }
|
||||
})
|
||||
const saveDownload = async (payload: DownloadPayload): Promise<DownloadResult> => {
|
||||
const { filename, bytes } = decodeDownload(payload)
|
||||
|
||||
+8
-3
@@ -9,7 +9,7 @@ import { apiProtocol, endpointCandidates, endpointUrl, inferModelApi, modelEndpo
|
||||
import { cachedConnection, cachedGeneration, discoverModelConnection, modelFetch, readModelJson, rememberGeneration } from './model-connection'
|
||||
import { modelMetadata } from './model-metadata'
|
||||
import { resolveModelTokenLimits } from '@shared/model-token-limits'
|
||||
import { getGatewayConfig } from './config'
|
||||
import { getGatewayConfig, getLegacyModelConfig } from './config'
|
||||
|
||||
interface StoredModel extends Omit<CustomModelProfile, 'hasApiKey' | 'keyStorage'> {
|
||||
apiKeyEnc?: string
|
||||
@@ -75,7 +75,12 @@ async function readRegistry(persistMigration: boolean): Promise<StoredRegistry>
|
||||
try { raw = await fs.readFile(filePath(), 'utf8') }
|
||||
catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error
|
||||
const legacy = await getGatewayConfig()
|
||||
const legacy = await getLegacyModelConfig()
|
||||
if (!legacy) {
|
||||
const initial: StoredRegistry = { version: 1, models: [], selectedModelId: null }
|
||||
if (persistMigration) await persist(initial)
|
||||
return initial
|
||||
}
|
||||
const migrated: StoredModel = {
|
||||
id: 'legacy-default', name: legacy.model, providerId: 'custom',
|
||||
model: legacy.model, baseUrl: legacy.baseUrl, authMode: 'api-key', protocol: 'anthropic',
|
||||
@@ -230,7 +235,7 @@ export function saveModel(input: CustomModelInput, overwrite = false): Promise<M
|
||||
const remaining = registry.models.filter(model => model.id !== previousId && model.id !== next.id)
|
||||
const oldIndex = registry.models.findIndex(model => model.id === previousId || model.id === next.id)
|
||||
remaining.splice(oldIndex < 0 ? remaining.length : Math.min(oldIndex, remaining.length), 0, next)
|
||||
const selectedModelId = !registry.selectedModelId || registry.selectedModelId === previousId ? next.id : registry.selectedModelId
|
||||
const selectedModelId = (!existing && !target) || !registry.selectedModelId || registry.selectedModelId === previousId ? next.id : registry.selectedModelId
|
||||
const updated: StoredRegistry = { version: 1, models: remaining, selectedModelId }
|
||||
await persist(updated)
|
||||
return registryView(updated)
|
||||
|
||||
@@ -7,11 +7,12 @@ import * as workspaces from './workspaces'
|
||||
import { agentModeContextPatch, thinkingLevel } from './agent/mode-policy'
|
||||
export async function createManagedSession(projects: ProjectRepository, title?: string, context: SessionContext = {}) {
|
||||
context = agentModeContextPatch({}, context)
|
||||
if (context.permissionMode !== undefined && context.permissionMode !== 'default-sandbox' && context.permissionMode !== 'full-access') throw new Error('权限模式无效')
|
||||
const project = context.projectId ? await projects.get(context.projectId) : undefined
|
||||
if (project?.workspaceId && context.workspaceId && context.workspaceId !== project.workspaceId) throw new Error('项目任务必须使用项目所属工作空间')
|
||||
const workspaceId = project?.workspaceId ?? context.workspaceId ?? (await workspaces.listWorkspaces()).selectedWorkspaceId ?? undefined
|
||||
const cwd = await workspaces.workspaceRoot(workspaceId)
|
||||
const modelId = context.modelId ?? (await models.listModels()).selectedModelId ?? undefined
|
||||
if (modelId) await models.resolveModel(modelId)
|
||||
return sessions.createSession(title, { workspaceId, projectId: context.projectId, cwd, modelId, agentMode: context.agentMode, ...(context.thinkingLevel !== undefined ? { thinkingLevel: thinkingLevel(context.thinkingLevel) } : {}), ...(context.expertId !== undefined ? { expertId: context.expertId } : {}) })
|
||||
return sessions.createSession(title, { permissionMode: context.permissionMode, workspaceId, projectId: context.projectId, cwd, modelId, agentMode: context.agentMode, ...(context.thinkingLevel !== undefined ? { thinkingLevel: thinkingLevel(context.thinkingLevel) } : {}), ...(context.expertId !== undefined ? { expertId: context.expertId } : {}) })
|
||||
}
|
||||
@@ -16,7 +16,7 @@ export function formatMessageTime(time: number | null | undefined, now = new Dat
|
||||
return `${year}${month}月${day}日 ${hoursMinutes}`
|
||||
}
|
||||
|
||||
function MessageTime({ time }: { time: number }): React.JSX.Element | null {
|
||||
function MessageTime({ time }: { time: number | undefined }): React.JSX.Element | null {
|
||||
const formatted = formatMessageTime(time)
|
||||
return formatted ? <span className="_message_time_tip_1p6pj_1 cb-message-time-tip">{formatted}</span> : null
|
||||
}
|
||||
@@ -29,6 +29,6 @@ export function AssistantMessageDetails({ message }: { message: ChatMessage }):
|
||||
<span className="_modelName_1q2ou_90">{message.modelName}</span>
|
||||
</span>
|
||||
</span>}
|
||||
<MessageTime time={message.createdAt} />
|
||||
<MessageTime time={message.turnFinishedAt ?? (message.turnStartedAt !== undefined && message.turnDurationMs !== undefined ? message.turnStartedAt + message.turnDurationMs : undefined)} />
|
||||
</span>
|
||||
}
|
||||
@@ -2,6 +2,10 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import MessageBubble, { planMarkdown } from './MessageBubble'
|
||||
import { useChatStore } from '../stores/chat'
|
||||
import { useSettingsStore } from '../stores/settings'
|
||||
import { resolveSelectedModel } from '@shared/model-selection'
|
||||
import { ModeChip } from '../workbuddy/composer/ModeChip'
|
||||
import { SessionLoading } from './SessionLoading'
|
||||
import './session-loading.css'
|
||||
import SourceHome, { type SourceHomeMode } from './SourceHome'
|
||||
import SourceChatHeader from './SourceChatHeader'
|
||||
import { useConversationSearch } from '../workbuddy/conversation/useConversationSearch'
|
||||
@@ -97,6 +101,8 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
const messages = useChatStore((s) => s.messages)
|
||||
const running = useChatStore((s) => s.running)
|
||||
const error = useChatStore((s) => s.error)
|
||||
const loading = useChatStore((s) => s.loading)
|
||||
const loadError = useChatStore((s) => s.loadError)
|
||||
const sending = useChatStore((s) => s.sending)
|
||||
const sessions = useChatStore((s) => s.sessions)
|
||||
const currentId = useChatStore((s) => s.currentSessionId)
|
||||
@@ -108,13 +114,11 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
const respond = useChatStore((s) => s.respond)
|
||||
const revert = useChatStore((s) => s.revert)
|
||||
const feedback = useChatStore((s) => s.feedback)
|
||||
const config = useSettingsStore((s) => s.config)
|
||||
const models = useSettingsStore((s) => s.models)
|
||||
const selectedModelId = useSettingsStore((s) => s.selectedModelId)
|
||||
const workspaces = useSettingsStore((s) => s.workspaces)
|
||||
const selectedWorkspaceId = useSettingsStore((s) => s.selectedWorkspaceId)
|
||||
const defaultWorkspaceRoot = useSettingsStore((s) => s.workspace)
|
||||
const [permissionMode, setPermissionMode] = useState<PermissionMode>(initialPermissionMode)
|
||||
|
||||
useEffect(() => {
|
||||
if (!currentId) return
|
||||
@@ -207,9 +211,7 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
const effectivePermissionMode = currentId
|
||||
? activeSessionPermission ?? 'default-sandbox'
|
||||
: activeSessionPermission ?? initialPermissionMode()
|
||||
useEffect(() => {
|
||||
setPermissionMode(effectivePermissionMode)
|
||||
}, [effectivePermissionMode, currentId])
|
||||
|
||||
/**
|
||||
* 原版 resolveRevertEntryVisible(ConversationTimelineImpl.hasRevision):
|
||||
* 有 requestId 且不是“历史已全部加载时的首条用户消息”才显示回退入口;
|
||||
@@ -257,14 +259,17 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
}, [currentId, setDraft])
|
||||
|
||||
const title = sessions.find((s) => s.id === currentId)?.title ?? '新对话'
|
||||
const isHome = currentId === null || messages.length === 0
|
||||
const isHome = currentId === null && !sending && messages.length === 0
|
||||
const conversationSearch = useConversationSearch(messages)
|
||||
const currentSession = sessions.find((session) => session.id === currentId)
|
||||
const workspaceRoot = currentSession?.cwd ?? draftContext.cwd ?? defaultWorkspaceRoot
|
||||
const artifactCount = useMemo(() => collectWorkbenchArtifacts(messages, workspaceRoot).artifacts.length, [messages, workspaceRoot])
|
||||
const effectiveModelId = currentSession?.modelId ?? draftContext.modelId ?? selectedModelId
|
||||
const effectiveModel = resolveSelectedModel(models, currentId ? currentSession?.modelId : draftContext.modelId, selectedModelId)
|
||||
const effectiveModelId = effectiveModel?.id
|
||||
const effectiveWorkspaceId = currentSession?.workspaceId ?? draftContext.workspaceId ?? selectedWorkspaceId
|
||||
const effectiveAgentMode = (currentId ? currentSession?.agentMode : draftContext.agentMode) ?? 'craft'
|
||||
// Plan changes the effective policy, never the saved preference restored on exit.
|
||||
const permissionMode = effectiveAgentMode === 'plan' ? 'default-sandbox' : effectivePermissionMode
|
||||
// Left undefined when the session has no explicit choice; the selector then uses the model's own default.
|
||||
const effectiveThinkingLevel = currentSession?.thinkingLevel ?? draftContext.thinkingLevel
|
||||
const capabilityCatalog = useCapabilityCatalog(workspaceRoot)
|
||||
@@ -420,7 +425,7 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
})
|
||||
|
||||
// The selected model label comes from the local registry.
|
||||
const modelLabel = models.find((model) => model.id === effectiveModelId)?.name || config?.model || '未配置模型'
|
||||
const modelLabel = effectiveModel?.name ?? '配置自定义模型'
|
||||
useEffect(() => {
|
||||
const el = scrollRef.current
|
||||
if (!el) return
|
||||
@@ -459,6 +464,8 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
|
||||
const submit = async ({ text, attachments, resources, quotes, userContent, skillIds, mcpServerIds, command, queueEditId }: ComposerSendPayload): Promise<void> => {
|
||||
if (sending) return
|
||||
if (loading || loadError) throw new Error('会话尚未就绪,请重试或等待加载完成')
|
||||
if (!effectiveModel) throw new Error('请先配置自定义模型')
|
||||
if (composition.input.blockedByInteraction) throw new Error('请先处理当前交互')
|
||||
followBottom.current = true
|
||||
const submitKey = draftKey
|
||||
@@ -470,7 +477,7 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
useChatStore.setState(state => ({
|
||||
draftContext: {
|
||||
...state.draftContext,
|
||||
permissionMode: state.draftContext.permissionMode ?? permissionMode
|
||||
permissionMode: state.draftContext.permissionMode ?? effectivePermissionMode
|
||||
}
|
||||
}))
|
||||
}
|
||||
@@ -482,7 +489,7 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
setDrafts(cleared)
|
||||
}
|
||||
try {
|
||||
await send(text, { attachments, resources, quotes, userContent, skillIds, mcpServerIds, command, queueEditId: handoffId, permissionMode })
|
||||
await send(text, { attachments, resources, quotes, userContent, skillIds, mcpServerIds, command, queueEditId: handoffId, permissionMode: effectivePermissionMode })
|
||||
draftMoves.current.delete(submitKey)
|
||||
} catch (error) {
|
||||
const destination = draftMoves.current.get(submitKey)
|
||||
@@ -549,7 +556,7 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
await send(text, { attachments: edited?.attachments ?? message.attachments, resources: edited?.resources ?? message.resources, quotes: edited?.quotes ?? message.quotes,
|
||||
skillIds: edited?.skillIds ?? capabilities.skillIds, mcpServerIds: edited?.mcpServerIds ?? capabilities.mcpServerIds,
|
||||
command: edited?.command ?? capabilities.command, userContent: edited?.userContent ?? message.userContent,
|
||||
permissionMode, fromMessageId: message.id, action: 'edit', rollbackArtifacts })
|
||||
permissionMode: effectivePermissionMode, fromMessageId: message.id, action: 'edit', rollbackArtifacts })
|
||||
setEditingMessageId(null)
|
||||
}
|
||||
const precedingUserMessage = (message: ChatMessage): ChatMessage | undefined => {
|
||||
@@ -568,7 +575,7 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
}
|
||||
return preceding ? send(preceding.content, { attachments: preceding.attachments, resources: preceding.resources, quotes: preceding.quotes,
|
||||
...messageCapabilityRefs(preceding),
|
||||
userContent: preceding.userContent, permissionMode, fromMessageId: message.id, action: 'regenerate' }) : Promise.resolve()
|
||||
userContent: preceding.userContent, permissionMode: effectivePermissionMode, fromMessageId: message.id, action: 'regenerate' }) : Promise.resolve()
|
||||
}
|
||||
const canRegenerateMessage = (message: ChatMessage): boolean => {
|
||||
const preceding = precedingUserMessage(message)
|
||||
@@ -609,12 +616,12 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
responding={activeInteraction.status === 'responding'} error={activeInteraction.error}
|
||||
labels={{ title: '需要你确认', skip: '跳过', next: '下一题', send: '发送回答', customAnswer: '输入其他回答', skipAll: '跳过全部', previousQuestion: '上一题', nextQuestion: '下一题', dismiss: '收起', notAnswered: '未回答' }}
|
||||
onSelect={(_text, answer) => respond(activeInteraction.id, { kind: 'question', answer })}
|
||||
onSkip={() => void respond(activeInteraction.id, { kind: 'question', answer: { questions: activeInteraction.questions.map((item) => ({ id: item.id, question: item.question, answers: [] })) }, skipped: true }).catch(() => undefined)} />
|
||||
onSkip={() => respond(activeInteraction.id, { kind: 'question', answer: { questions: activeInteraction.questions.map((item) => ({ id: item.id, question: item.question, answers: [] })) }, skipped: true })} />
|
||||
: activeInteraction?.kind === 'approval'
|
||||
? <PermissionConfirm key={activeInteraction.id} className="permission-floating"
|
||||
pendingId={activeInteraction.id} toolName={activeInteraction.toolCallId} title={activeInteraction.title}
|
||||
pendingId={activeInteraction.id} details={activeInteraction.details} toolName={activeInteraction.details?.toolName ?? messages.flatMap(message => message.tools ?? []).find(tool => tool.id === activeInteraction.toolCallId)?.name ?? '工具操作'} title={activeInteraction.title}
|
||||
options={activeInteraction.options} status={activeInteraction.status} error={activeInteraction.error}
|
||||
labels={{ confirm: '允许一次', alwaysAllow: '始终允许', reject: '拒绝', working: '提交中', approved: '已允许', rejected: '已拒绝', cancelled: '已取消', failed: '提交失败' }}
|
||||
labels={{ confirm: '允许一次', alwaysAllow: '本次会话内始终允许', reject: '拒绝', working: '提交中', approved: '已允许', rejected: '已拒绝', cancelled: '已取消', failed: '提交失败' }}
|
||||
resolve={(id, decision) => respond(id, { kind: 'approval', optionId: decision.optionId })}
|
||||
reject={(id) => {
|
||||
const option = activeInteraction.options.find((item) => item.kind.startsWith('reject'))
|
||||
@@ -644,7 +651,9 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
onSelectCapability={selectCapability}
|
||||
onRemoveCapability={removeCapability}
|
||||
capabilityReferences={queuedEditReference}
|
||||
disabled={sending || composition.input.disabled}
|
||||
sendDisabled={!effectiveModel}
|
||||
disabled={sending || loading || !!loadError || composition.input.disabled}
|
||||
modeSlot={<ModeChip mode={effectiveAgentMode} disabled={sending || loading || !!running} onClear={() => useChatStore.getState().selectAgentMode('craft').catch(addMenuError)} />}
|
||||
branchLabelSlot={<ComposerBranchLabel sessionId={currentId} />}
|
||||
contextUsageSlot={<ContextUsageRing sessionId={currentId} hasMessages={messages.length > 0} running={sending || !!running} />}
|
||||
addMenuScopeKey={`${draftKey}:${hidden ? 'hidden' : 'visible'}`}
|
||||
@@ -658,7 +667,7 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
const resourcePath = file.path
|
||||
setDrafts(all => { const previous = all[draftKey] ?? { text: '', attachments: [] }; return { ...all, [draftKey]: { ...previous, resources: [...new Set([...(previous.resources ?? []), resourcePath])] } } })
|
||||
} }} onCloseMenu={closeMenu} />,
|
||||
mode: () => <SourceModeSubmenu key={`${effectiveAgentMode}:${modeRevision}`} selectedId={effectiveAgentMode} provider={{ disabled: sending || !!running, onChange: mode => {
|
||||
mode: () => <SourceModeSubmenu key={`${effectiveAgentMode}:${modeRevision}`} selectedId={effectiveAgentMode} provider={{ disabled: sending || loading || !!running, onChange: mode => {
|
||||
void useChatStore.getState().selectAgentMode(mode.id as AgentMode).catch(reason => { addMenuError(reason); setModeRevision(value => value + 1) })
|
||||
} }} />
|
||||
}}
|
||||
@@ -689,19 +698,10 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
speech={{ transcribe: ({ wavBase64, mimeType }) => window.ycode.speech.transcribe({ audioBase64: wavBase64, mimeType }) }}
|
||||
onTranscript={(text) => setDraft((current) => `${current}${current ? ' ' : ''}${text}`)}
|
||||
permissionPlacement="toolbar"
|
||||
permissionSlot={<SourcePermissionControl mode={permissionMode} disabled={sending || !!running || composition.input.disabled}
|
||||
permissionSlot={<SourcePermissionControl mode={permissionMode} disabled={sending || loading || !!loadError || !!running || effectiveAgentMode !== 'craft' || composition.input.disabled}
|
||||
disabledReason={effectiveAgentMode !== 'craft' ? '计划和仅问答模式下不能修改权限' : undefined}
|
||||
onChange={async (mode) => {
|
||||
const owner = useChatStore.getState().currentSessionId
|
||||
if (owner) {
|
||||
const session = await window.ycode.session.updateContext(owner, { permissionMode: mode })
|
||||
useChatStore.setState((state) => ({ sessions: state.sessions.map((item) => item.id === owner ? session : item) }))
|
||||
if (useChatStore.getState().currentSessionId !== owner) return
|
||||
setPermissionMode(mode)
|
||||
try { window.localStorage.setItem(PERMISSION_MODE_KEY, mode) } catch { /* Storage is optional. */ }
|
||||
return
|
||||
}
|
||||
useChatStore.setState((state) => ({ draftContext: { ...state.draftContext, permissionMode: mode } }))
|
||||
setPermissionMode(mode)
|
||||
await useChatStore.getState().selectPermissionMode(mode)
|
||||
try { window.localStorage.setItem(PERMISSION_MODE_KEY, mode) } catch { /* Storage is optional. */ }
|
||||
}}
|
||||
onError={(message) => useChatStore.setState({ error: message })} />}
|
||||
@@ -722,7 +722,7 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
|
||||
return (
|
||||
<section className="chat-pane cr-theme" style={hidden ? { display: 'none' } : undefined} data-view-id="main-content" data-cr-theme={darkMode ? 'dark' : 'light'}>
|
||||
{!isHome && <SourceChatHeader key={currentId} title={title} rightVisible={rightVisible} onToggleRight={onToggleRight}
|
||||
{!isHome && <SourceChatHeader key={currentId} title={title} loadingTitle={loading && !currentSession?.title} rightVisible={rightVisible} onToggleRight={onToggleRight}
|
||||
search={conversationSearch} onTitleChange={async (nextTitle) => {
|
||||
if (!currentId) return
|
||||
await window.ycode.session.rename(currentId, nextTitle)
|
||||
@@ -741,6 +741,9 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
followBottom.current = el.scrollHeight - el.scrollTop - el.clientHeight < 80
|
||||
}}>
|
||||
<div className="chat-thread cr-message-list__content"><div className="cr-document">
|
||||
{!loadError && <SessionLoading key={currentId} loading={loading && messages.length === 0} />}
|
||||
{loadError && <div className="yc-session-load-error" role="alert"><p>会话加载失败:{loadError}</p><button type="button" onClick={() => currentId && void useChatStore.getState().selectSession(currentId)}>重试</button></div>}
|
||||
{!loading && !loadError && messages.length === 0 && <p className="yc-session-empty">此会话暂无消息</p>}
|
||||
{messages.map((message) => (
|
||||
<MessageBubble key={message.id} message={message} sessionId={currentId} onQuote={quoteTable} darkMode={darkMode}
|
||||
quotedSourceIds={quotedSourceIds}
|
||||
@@ -750,7 +753,7 @@ export default function ChatPane({ rightVisible, onToggleRight, onOpenSettings,
|
||||
onBuildPlan={async (plan) => {
|
||||
if (running || sending) throw new Error('请等待当前运行结束')
|
||||
await useChatStore.getState().selectAgentMode('craft')
|
||||
await send(`请执行以下计划:\n\n${planMarkdown(plan)}`, { planId: plan.id, permissionMode })
|
||||
await send(`请执行以下计划:\n\n${planMarkdown(plan)}`, { planId: plan.id, permissionMode: effectivePermissionMode })
|
||||
}}
|
||||
actions={{ busyState: running || sending ? 'talking' : 'idle', canRevert: rewindEntryVisible(message) && !rewinding,
|
||||
rollbackPending: rewinding,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { ChatMessage, MessagePart } from '@shared/types'
|
||||
import { projectQuestionAnswers } from '../workbuddy/conversation/question-projection'
|
||||
import SourceAgentAvatar from './SourceAgentAvatar'
|
||||
import SourceArtifactLink from './SourceArtifactLink'
|
||||
import { AssistantMessageDetails } from './AssistantMessageDetails'
|
||||
@@ -49,7 +50,7 @@ interface Props {
|
||||
|
||||
export default function MessageBubble({ message, assistantAvatar, onQuote, quotedSourceIds, darkMode, actions, editingMessageId, onStartEdit, onCancelEdit, onSubmitEdit, sessionId, onBuildPlan, buildingPlanId, artifactCount = 0, onShowArtifacts }: Props): React.JSX.Element {
|
||||
const [actionError, setActionError] = useState<string | null>(null)
|
||||
const [liveEventsExpanded, setLiveEventsExpanded] = useState(false)
|
||||
const [expandedGroups, setExpandedGroups] = useState<Record<string, boolean>>({})
|
||||
const perform = (action: () => Promise<unknown>): void => {
|
||||
setActionError(null)
|
||||
void action().catch((error) => setActionError(error instanceof Error ? error.message : String(error)))
|
||||
@@ -77,7 +78,8 @@ export default function MessageBubble({ message, assistantAvatar, onQuote, quote
|
||||
|
||||
const streaming = message.status === 'streaming'
|
||||
const parts = getMessageParts(message)
|
||||
const tools = (message.tools ?? []).map(tool => tool.metadata?.importedHistory ? projectWorkBuddyHistoryTool(tool)
|
||||
const questionProjection = projectQuestionAnswers(message)
|
||||
const tools = questionProjection.tools.map(tool => tool.metadata?.importedHistory ? projectWorkBuddyHistoryTool(tool)
|
||||
: tool.metadata?.capability === 'mcp' ? { ...tool, name: 'mcp_call_tool', args: { serverName: tool.metadata.serverName, toolName: tool.metadata.toolName, arguments: tool.args } } : tool)
|
||||
const activeApprovals = new Map(tools.flatMap(tool => {
|
||||
const request = findToolApproval(message.interactions, tool.id)
|
||||
@@ -86,14 +88,18 @@ export default function MessageBubble({ message, assistantAvatar, onQuote, quote
|
||||
const activeQuestionIds = new Set((message.interactions ?? []).flatMap(interaction =>
|
||||
interaction.kind === 'question' && ['pending', 'responding', 'error'].includes(interaction.status) ? [interaction.toolCallId] : []))
|
||||
const classifyTool = (part: Extract<MessagePart, { type: 'tool' }>): CompletedFoldToolCallClassification | undefined => {
|
||||
const name = tools.find(tool => tool.id === part.toolId)?.name ?? ''
|
||||
if (questionProjection.positionedAnswers.has(part.toolId) || activeQuestionIds.has(part.toolId)) return { role: 'inline-question' }
|
||||
const name = tools.find(tool => tool.id === part.toolId)?.name.toLowerCase() ?? ''
|
||||
if (['show_widget', 'visualize:show_widget', 'visualizer:show_widget'].includes(name)) return { role: 'show-widget' }
|
||||
if (['ask_followup_question', 'ask_user_question', 'askfollowupquestion', 'askuserquestion'].includes(name)) return { role: 'inline-question' }
|
||||
return undefined
|
||||
}
|
||||
const renderTool = (id: string): React.ReactNode => {
|
||||
const tool = tools.find((item) => item.id === id)
|
||||
if (!tool) return null
|
||||
if (!tool) {
|
||||
const answer = questionProjection.positionedAnswers.get(id)
|
||||
return answer ? <QuestionAnswerTranscript questionAnswer={answer} /> : null
|
||||
}
|
||||
if (activeQuestionIds.has(tool.id)) return null
|
||||
const nativeResult = tool.result ?? tool.output ?? tool.summary
|
||||
const result = nativeResult && typeof nativeResult === 'object' && 'result' in nativeResult ? nativeResult : { result: nativeResult }
|
||||
@@ -130,65 +136,41 @@ export default function MessageBubble({ message, assistantAvatar, onQuote, quote
|
||||
<CompletedFold parts={parts} foldId={message.id}
|
||||
turnFinished={message.status === 'done' || message.status === 'cancelled' || message.status === 'error'}
|
||||
turnCancelled={message.status === 'cancelled'} turnDurationMs={message.turnDurationMs}
|
||||
expandFinishedEvents={liveEventsExpanded}
|
||||
classifyToolCall={classifyTool}
|
||||
renderGroup={(group, children) => {
|
||||
if (!group.isRunning) return <Fragment key={group.key}>{children}</Fragment>
|
||||
const groupedTools = group.units.flatMap(unit => {
|
||||
const part = unit.part as MessagePart
|
||||
return part.type === 'tool' ? tools.filter(tool => tool.id === part.toolId) : []
|
||||
})
|
||||
if (groupedTools.some(tool => activeApprovals.has(tool.id))) return <Fragment key={group.key}>{children}</Fragment>
|
||||
|
||||
const latestProgressIndex = parts.reduce((latest, part, index) => part.type === 'tool' || part.type === 'reasoning' ? index : latest, -1)
|
||||
if (!group.units.some(unit => unit.index === latestProgressIndex)) return null
|
||||
|
||||
const latestProgressIndex = group.units.at(-1)!.index
|
||||
const latestPart = parts[latestProgressIndex]
|
||||
const latestTool = latestPart?.type === 'tool' ? tools.find(tool => tool.id === latestPart.toolId) : undefined
|
||||
const toolArgs = latestTool?.args && typeof latestTool.args === 'object' ? latestTool.args as Record<string, unknown> : {}
|
||||
const summaryArgs = latestTool && ['list_dir', 'list_files'].includes(latestTool.name) && !toolArgs.target_directory && !toolArgs.root && typeof toolArgs.path === 'string'
|
||||
? { ...toolArgs, target_directory: toolArgs.path }
|
||||
: toolArgs
|
||||
const pendingConfirmation = !!latestTool && activeApprovals.has(latestTool.id)
|
||||
const summaryStatus = !latestTool ? 'success' : pendingConfirmation ? 'pending'
|
||||
: latestTool.status === 'ok' ? 'executed'
|
||||
: ['running', 'executing'].includes(latestTool.status) ? 'stream_executing'
|
||||
: latestTool.status
|
||||
const actionSummary = latestTool ? computeSourceFoldSummary([{ type: 'tool-call', tool: {
|
||||
name: latestTool.name, args: summaryArgs, result: latestTool.result ?? latestTool.output,
|
||||
pendingConfirmation, status: summaryStatus
|
||||
} }], { isRunning: true,
|
||||
t: (key, params) => sourceConversationLabel(key as SourceConversationLabelKey, params) }) : undefined
|
||||
const summaryParts = group.units.map(unit => {
|
||||
if (unit.part.type !== 'tool') return { type: unit.part.type }
|
||||
const toolId = unit.part.toolId
|
||||
const tool = tools.find(item => item.id === toolId)
|
||||
const args = tool?.args && typeof tool.args === 'object' ? tool.args as Record<string, unknown> : {}
|
||||
const summaryArgs = tool && ['list_dir', 'list_files'].includes(tool.name) && !args.target_directory && !args.root && typeof args.path === 'string'
|
||||
? { ...args, target_directory: args.path } : args
|
||||
return { type: 'tool-call', tool: tool ? { name: tool.name, args: summaryArgs,
|
||||
result: tool.result ?? tool.output, pendingConfirmation: activeApprovals.has(tool.id),
|
||||
status: tool.status === 'ok' ? 'executed' : tool.status === 'running' ? 'stream_executing' : tool.status } : undefined }
|
||||
})
|
||||
const actionSummary = computeSourceFoldSummary(summaryParts, { isRunning: group.isRunning, adjacentText: group.adjacentText,
|
||||
t: (key, params) => sourceConversationLabel(key as SourceConversationLabelKey, params) })
|
||||
const actionText = actionSummary?.text || conversationLabel('message.loading.thinking')
|
||||
const activityIcon = latestTool ? resolveSourceToolIcon(latestTool.name) : undefined
|
||||
const liveEventParts: Array<{ part: Extract<MessagePart, { type: 'tool' | 'reasoning' }>; index: number }> = []
|
||||
parts.forEach((part, eventIndex) => {
|
||||
if (part.type === 'reasoning') {
|
||||
if (part.text.trim()) liveEventParts.push({ part, index: eventIndex })
|
||||
return
|
||||
}
|
||||
if (part.type !== 'tool') return
|
||||
const tool = tools.find(item => item.id === part.toolId)
|
||||
if (!tool || classifyTool(part)?.role) return
|
||||
if (activeApprovals.has(tool.id) || activeQuestionIds.has(tool.id)) return
|
||||
liveEventParts.push({ part, index: eventIndex })
|
||||
})
|
||||
const activityHeader = <div className="cr-captured-tool cr-streaming-activity" role="status" aria-live="polite" data-activity-tool-id={latestTool?.id}>
|
||||
<SourceToolHeader icon={activityIcon} showIconWhileExecuting
|
||||
isExecuting={actionSummary?.status === 'running'} primaryLoading={actionSummary?.status === 'running'}
|
||||
primaryContent={actionText} />
|
||||
</div>
|
||||
return <LiveEventDisclosure id={`live-events-${encodeURIComponent(message.id)}`}
|
||||
header={activityHeader} expanded={liveEventsExpanded} onExpandedChange={setLiveEventsExpanded}>
|
||||
{liveEventParts.map(({ part, index: eventIndex }) => part.type === 'tool'
|
||||
? <div className="cr-live-event-list__item" data-event-type="tool" key={`tool:${part.toolId}`}>
|
||||
{renderTool(part.toolId)}
|
||||
</div>
|
||||
: <div className="cr-live-event-list__item" data-event-type="reasoning" key={`reasoning:${part.id}`}>
|
||||
<ThinkingBlock text={part.text} isFinished={part.finished || !streaming}
|
||||
isLastContent={eventIndex === parts.length - 1} title={sourceConversationLabel('reasoning.title')}
|
||||
renderMarkdown={text => <MarkdownContent content={text} darkMode={darkMode} />} />
|
||||
</div>)}
|
||||
return <LiveEventDisclosure id={`events-${encodeURIComponent(message.id)}-${group.startIndex}`}
|
||||
header={activityHeader} expanded={expandedGroups[group.key] ?? false}
|
||||
onExpandedChange={expanded => setExpandedGroups(previous => ({ ...previous, [group.key]: expanded }))}>
|
||||
{children}
|
||||
</LiveEventDisclosure>
|
||||
}}
|
||||
renderUnit={(candidate, index) => {
|
||||
@@ -222,11 +204,11 @@ export default function MessageBubble({ message, assistantAvatar, onQuote, quote
|
||||
)
|
||||
}} />
|
||||
|
||||
{message.interactions?.map((interaction) => interaction.kind === 'question' && interaction.answer &&
|
||||
!message.tools?.some((tool) => tool.id === interaction.toolCallId && tool.metadata?.questionAnswer)
|
||||
? <QuestionAnswerTranscript key={interaction.id} questionAnswer={interaction.answer} /> : null)}
|
||||
{questionProjection.unpositioned.length > 0 && <section className="question-history-unpositioned" aria-label="历史问答(原始位置不可用)">
|
||||
{questionProjection.unpositioned.map(item => <QuestionAnswerTranscript key={item.id} questionAnswer={item.answer} />)}
|
||||
</section>}
|
||||
|
||||
{streaming && <StreamingFooter prepareText={resolvePrepareText(message.prepareStage)} />}
|
||||
{streaming && <StreamingFooter prepareText={resolvePrepareText(message.prepareStage)} progress={message.runtimeProgress} reasoningOnly={parts.length > 0 && parts.every(part => part.type === 'reasoning')} />}
|
||||
|
||||
{message.status === 'error' && message.error ? (
|
||||
<div className="msg__status msg__status--error">{message.error}</div>
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
/** Only mounts for an uncached session. The tail fades without delaying readiness. */
|
||||
export function SessionLoading({ loading }: { loading: boolean }): React.JSX.Element | null {
|
||||
const [visible, setVisible] = useState(loading)
|
||||
useEffect(() => {
|
||||
if (loading) { setVisible(true); return }
|
||||
const timer = window.setTimeout(() => setVisible(false), 300)
|
||||
return () => window.clearTimeout(timer)
|
||||
}, [loading])
|
||||
if (!loading && !visible) return null
|
||||
return <div className={`yc-session-skeleton${loading ? '' : ' yc-session-skeleton--leaving'}`} role="status" aria-label="正在加载会话" aria-live="polite">
|
||||
{[0, 1].map(index => <div className="yc-session-skeleton__group" key={index} aria-hidden="true">
|
||||
<div className="yc-session-skeleton__user yc-session-shimmer" />
|
||||
<div className="yc-session-skeleton__identity"><span className="yc-session-skeleton__avatar yc-session-shimmer" /><span className="yc-session-skeleton__name yc-session-shimmer" /></div>
|
||||
<div className="yc-session-skeleton__line yc-session-shimmer" /><div className="yc-session-skeleton__line yc-session-shimmer" /><div className="yc-session-skeleton__line yc-session-skeleton__line--short yc-session-shimmer" />
|
||||
</div>)}
|
||||
</div>
|
||||
}
|
||||
@@ -27,8 +27,7 @@ import { AccountMenu } from '../workbuddy/user-menu/AccountMenu'
|
||||
import { UpdateSidebarCard } from './updates/UpdateSidebarCard'
|
||||
import ArchivedTasksDialog from './ArchivedTasksDialog'
|
||||
import { TaskContextMenu } from './task-menu'
|
||||
import { TaskConfirmDialog } from './task-confirm-dialog'
|
||||
import type { TaskConfirmKind } from './task-confirm-dialog'
|
||||
import { TaskPopconfirm } from './TaskPopconfirm'
|
||||
import { IconTaskArchive, IconTaskMore as TaskMoreIcon, IconTaskPinBadge as TaskPinIcon } from './task-menu-icons'
|
||||
import type { TaskMenuAction, TaskMenuAnchor } from './task-menu'
|
||||
import { MOCK_ACCOUNT, MOCK_UNREAD_COUNT, NAV_ITEMS } from '../mock'
|
||||
@@ -119,7 +118,8 @@ export default function Sidebar({ collapsed, onToggleCollapse, activeRoute = 'ch
|
||||
const [workspaceDialog, setWorkspaceDialog] = useState<{ mode: 'create' | 'rename' | 'remove'; space?: WorkspaceRecord } | null>(null)
|
||||
const [spaceMenu, setSpaceMenu] = useState<{ space: WorkspaceRecord; top: number; left: number; anchorTop: number } | null>(null)
|
||||
const [taskMenu, setTaskMenu] = useState<{ session: SessionMeta; anchor: TaskMenuAnchor } | null>(null)
|
||||
const [taskConfirm, setTaskConfirm] = useState<{ session: SessionMeta; kind: TaskConfirmKind; running: boolean } | null>(null)
|
||||
const [taskConfirm, setTaskConfirm] = useState<{ session: SessionMeta; kind: 'archive' | 'delete'; running: boolean; anchor: HTMLElement } | null>(null)
|
||||
const taskRows = useRef(new Map<string, HTMLDivElement>())
|
||||
const [archivedOpen, setArchivedOpen] = useState(false)
|
||||
const [renaming, setRenaming] = useState<{ sessionId: string; value: string } | null>(null)
|
||||
const spaceMenuRef = useRef<HTMLDivElement>(null)
|
||||
@@ -269,13 +269,15 @@ export default function Sidebar({ collapsed, onToggleCollapse, activeRoute = 'ch
|
||||
if (action === 'unpin') { void setPinned(session.id, false).catch((error) => report(error, '取消置顶失败')); return }
|
||||
if (action === 'openWindow') { void window.ycode.session.openWindow(session.id).catch((error) => report(error, '无法在独立窗口中打开')); return }
|
||||
if (action === 'openFolder') { void window.ycode.session.openFolder(session.id).catch((error) => report(error, '无法打开文件夹')); return }
|
||||
const anchor = taskRows.current.get(session.id)
|
||||
if (!anchor?.isConnected) return
|
||||
if (action === 'archive') {
|
||||
const running = isSessionRunning(session.id)
|
||||
if (running) { setHint('任务进行中,无法归档'); return }
|
||||
setTaskConfirm({ session, kind: 'archive', running: false })
|
||||
setTaskConfirm({ session, kind: 'archive', running: false, anchor })
|
||||
return
|
||||
}
|
||||
setTaskConfirm({ session, kind: 'delete', running: isSessionRunning(session.id) })
|
||||
setTaskConfirm({ session, kind: 'delete', running: isSessionRunning(session.id), anchor })
|
||||
}
|
||||
|
||||
const renderSession = (session: SessionMeta, child = false): React.JSX.Element => {
|
||||
@@ -283,7 +285,7 @@ export default function Sidebar({ collapsed, onToggleCollapse, activeRoute = 'ch
|
||||
const pinned = session.pinnedAt !== undefined
|
||||
const status = Object.values(runs).some(run => run.sessionId === session.id) ? '运行中' : sendingSessions.includes(session.id) ? '发送中' : relTime(session.updatedAt)
|
||||
return (
|
||||
<div key={session.id} className={`sidebar__item workspace-source-task conversation-agent-card${child ? ' conversation-agent-card--group-child' : ' conversation-agent-card--standalone'}${pinned ? ' conversation-pinned-item' : ''}${session.id === currentId ? ' selected' : ''}${taskMenu?.session.id === session.id ? ' sidebar__item--menu-open' : ''}`}
|
||||
<div key={session.id} ref={element => { if (element) taskRows.current.set(session.id, element); else taskRows.current.delete(session.id) }} className={`sidebar__item workspace-source-task conversation-agent-card${child ? ' conversation-agent-card--group-child' : ' conversation-agent-card--standalone'}${pinned ? ' conversation-pinned-item' : ''}${session.id === currentId ? ' selected' : ''}${taskMenu?.session.id === session.id ? ' sidebar__item--menu-open' : ''}`}
|
||||
data-selected={session.id === currentId} data-session-id={session.id} role="button" tabIndex={0} aria-haspopup="menu"
|
||||
onClick={() => { if (editing) return; onNavigate?.('chat'); void selectSession(session.id) }}
|
||||
onContextMenu={(event) => {
|
||||
@@ -520,10 +522,22 @@ export default function Sidebar({ collapsed, onToggleCollapse, activeRoute = 'ch
|
||||
onSelect={(action) => handleTaskMenu(taskMenu.session, action)}
|
||||
onClose={() => setTaskMenu(null)} /> : null}
|
||||
|
||||
{taskConfirm ? <TaskConfirmDialog
|
||||
{taskConfirm ? <TaskPopconfirm
|
||||
anchor={taskConfirm.anchor}
|
||||
kind={taskConfirm.kind}
|
||||
running={taskConfirm.running}
|
||||
onClose={() => setTaskConfirm(null)}
|
||||
running={isSessionRunning(taskConfirm.session.id)}
|
||||
onClose={() => {
|
||||
const previous = taskConfirm.anchor
|
||||
setTaskConfirm(null)
|
||||
// Removed rows cannot receive focus after a successful archive/delete.
|
||||
const fallback = document.querySelector<HTMLElement>('.sidebar [data-session-id], .sidebar button')
|
||||
;(previous.isConnected ? previous : fallback)?.focus({ preventScroll: true })
|
||||
requestAnimationFrame(() => {
|
||||
if (!previous.isConnected && (document.activeElement === document.body || !document.activeElement?.isConnected)) {
|
||||
document.querySelector<HTMLElement>('.sidebar [data-session-id], .sidebar button')?.focus({ preventScroll: true })
|
||||
}
|
||||
})
|
||||
}}
|
||||
onConfirm={async () => {
|
||||
if (taskConfirm.kind === 'archive') await setArchived(taskConfirm.session.id, true)
|
||||
else await removeSession(taskConfirm.session.id)
|
||||
|
||||
@@ -26,6 +26,7 @@ export interface SourceChatHeaderPromptList {
|
||||
}
|
||||
export interface SourceChatHeaderProps {
|
||||
title: string
|
||||
loadingTitle?: boolean
|
||||
rightVisible: boolean
|
||||
onToggleRight(): void
|
||||
/** Kept for the existing parent contract. The source header has no standalone settings gear. */
|
||||
@@ -52,7 +53,7 @@ function HeaderIconButton({ label, onClick, disabled, children }: {
|
||||
}
|
||||
const iconProps = { width: 16, height: 16, 'aria-hidden': true, className: 'wb-icon' } as const
|
||||
|
||||
export default function SourceChatHeader({ title, rightVisible, onToggleRight, onTitleChange, search,
|
||||
export default function SourceChatHeader({ title, loadingTitle, rightVisible, onToggleRight, onTitleChange, search,
|
||||
onShare, shareLabel = labels['contextMenu.shareTask'], promptList, onActionError, actions }: SourceChatHeaderProps): React.JSX.Element {
|
||||
const [editing, setEditing] = useState(false), [draftTitle, setDraftTitle] = useState(title)
|
||||
const titleInput = useRef<HTMLInputElement>(null), searchInput = useRef<HTMLInputElement>(null)
|
||||
@@ -113,8 +114,8 @@ export default function SourceChatHeader({ title, rightVisible, onToggleRight, o
|
||||
if (event.key === 'Enter') { event.preventDefault(); finishTitle() }
|
||||
if (event.key === 'Escape') { event.preventDefault(); editActive.current = false; setEditing(false); setDraftTitle(title) }
|
||||
}} /> : <span className="workbuddy-topbar-title-area">
|
||||
<span className="workbuddy-topbar-title" title={title}>{title}</span>
|
||||
<button type="button" className="workbuddy-topbar-edit-button" aria-label="Edit title" disabled={!onTitleChange}
|
||||
<span className={loadingTitle ? "workbuddy-topbar-title yc-session-title-placeholder" : "workbuddy-topbar-title"} title={loadingTitle ? undefined : title} aria-label={loadingTitle ? "正在加载任务标题" : undefined}>{loadingTitle ? "\u00a0" : title}</span>
|
||||
<button type="button" className="workbuddy-topbar-edit-button" aria-label="Edit title" disabled={loadingTitle || !onTitleChange}
|
||||
onClick={event => { event.stopPropagation(); if (!onTitleChange || saving.current) return; setDraftTitle(title); editActive.current = true; setEditing(true) }}>
|
||||
<SourceHeaderEditIcon className={undefined} style={undefined} aria-hidden="true" />
|
||||
</button>
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import { useEffect, useId, useRef, useState } from 'react'
|
||||
import { Popover } from '../ui/Popover'
|
||||
import './task-popconfirm.css'
|
||||
|
||||
export interface TaskPopconfirmProps {
|
||||
kind: 'archive' | 'delete'
|
||||
running: boolean
|
||||
anchor: HTMLElement
|
||||
onConfirm(): Promise<void>
|
||||
onClose(): void
|
||||
}
|
||||
|
||||
/** Destination restored from the recorded REA observation, not a claim that YCode has that page. */
|
||||
export function taskConfirmationCopy(kind: 'archive' | 'delete', running: boolean) {
|
||||
return kind === 'archive'
|
||||
? { title: '归档任务', description: '确认将该任务归档吗?归档后可在“系统设置-数据管理”中查看。', confirm: '确认归档' }
|
||||
: running
|
||||
? { title: '删除任务?', description: '该任务正在执行中,删除将立即终止运行,且无法恢复。', confirm: '停止并删除' }
|
||||
: { title: '删除任务?', description: '删除后,这些任务将从你的所有设备中移除,且无法恢复。', confirm: '确认' }
|
||||
}
|
||||
|
||||
/** Task-row confirmation. Queue editing and full-access confirmation keep their modal shells. */
|
||||
export function TaskPopconfirm({ kind, running, anchor, onConfirm, onClose }: TaskPopconfirmProps): React.JSX.Element {
|
||||
const copy = taskConfirmationCopy(kind, running)
|
||||
const titleId = useId(), descriptionId = useId(), errorId = useId()
|
||||
const lock = useRef(false), mounted = useRef(true)
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
useEffect(() => { mounted.current = true; return () => { mounted.current = false } }, [])
|
||||
const close = (): void => { if (!lock.current) onClose() }
|
||||
const confirm = async (): Promise<void> => {
|
||||
if (lock.current) return
|
||||
lock.current = true
|
||||
setBusy(true); setError(null)
|
||||
try {
|
||||
await onConfirm()
|
||||
if (mounted.current) onClose()
|
||||
} catch (cause) {
|
||||
if (mounted.current) setError(cause instanceof Error ? cause.message : '操作失败,请重试')
|
||||
} finally {
|
||||
lock.current = false
|
||||
if (mounted.current) setBusy(false)
|
||||
}
|
||||
}
|
||||
return <Popover anchorElement={anchor} returnFocusElement={anchor} open dismissible={!busy} onOpenChange={next => { if (!next) { if (!anchor.isConnected) onClose(); else close() } }}
|
||||
placement="right" portalRoot="body" arrow consumeOutsidePointer manageFocus initialFocus={1}
|
||||
hostClassName="workspace-source task-popconfirm-source" className="task-popconfirm" role="dialog"
|
||||
ariaLabelledBy={titleId} ariaDescribedBy={`${descriptionId}${error ? ` ${errorId}` : ''}`}>
|
||||
<div aria-busy={busy} data-task-confirm-kind={kind} data-task-confirm-running={running || undefined}>
|
||||
<h2 className="task-popconfirm__title" id={titleId}>{copy.title}</h2>
|
||||
<p className="task-popconfirm__description" id={descriptionId}>{copy.description}</p>
|
||||
{kind === 'archive' && <p className="task-popconfirm__compatibility">YCode 暂无此设置入口,请从侧栏“已归档”查看和恢复。</p>}
|
||||
{error && <div className="task-popconfirm__error" id={errorId} role="alert">{error}</div>}
|
||||
<div className="task-popconfirm__actions">
|
||||
<button type="button" className="wb-button wb-button--secondary wb-button--small task-popconfirm__cancel" disabled={busy} onClick={close}>取消</button>
|
||||
<button type="button" className={`wb-button wb-button--primary wb-button--small task-popconfirm__confirm${kind === 'delete' ? ' wb-button--danger' : ''}`}
|
||||
disabled={busy} onClick={() => void confirm()}>{busy ? '处理中…' : copy.confirm}</button>
|
||||
</div>
|
||||
</div>
|
||||
</Popover>
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
.chat-thread .cr-document { position:relative; }
|
||||
.yc-session-skeleton { position:relative; width:100%; opacity:1; transition:opacity 300ms ease; }
|
||||
.yc-session-skeleton--leaving { position:absolute; inset:0; opacity:0; pointer-events:none; }
|
||||
.yc-session-skeleton__group { display:flex; flex-direction:column; gap:var(--yc-space-4); margin-bottom:var(--yc-space-9); }
|
||||
.yc-session-shimmer, .yc-session-title-placeholder { background:var(--cr-bg-tertiary); border-radius:var(--yc-radius-md); overflow:hidden; position:relative; }
|
||||
.yc-session-shimmer::after, .yc-session-title-placeholder::after { content:""; position:absolute; inset:0; transform:translateX(-100%); background:linear-gradient(90deg,transparent,var(--cr-bg-primary),transparent); animation:yc-session-shimmer 1.5s infinite; }
|
||||
.yc-session-skeleton__user { width:38%; height:var(--yc-space-9); align-self:flex-end; border-radius:var(--yc-radius-2xl); margin-bottom:var(--yc-space-4); }
|
||||
.yc-session-skeleton__identity { display:flex; align-items:center; gap:var(--yc-space-3); }
|
||||
.yc-session-skeleton__avatar { width:var(--yc-space-7); height:var(--yc-space-7); border-radius:50%; }
|
||||
.yc-session-skeleton__name { width:var(--wb-spacing-12); height:var(--yc-font-size-5); }
|
||||
.yc-session-skeleton__line { height:var(--yc-font-size-5); width:100%; }
|
||||
.yc-session-skeleton__line--short { width:65%; }
|
||||
.yc-session-title-placeholder { display:inline-block; width:calc(var(--yc-space-9) * 3); height:var(--yc-space-6); }
|
||||
.yc-session-empty, .yc-session-load-error { color:var(--cr-text-secondary); padding:var(--yc-space-7) 0; }
|
||||
@keyframes yc-session-shimmer { to { transform:translateX(100%); } }
|
||||
@media(prefers-reduced-motion:reduce) { .yc-session-shimmer::after,.yc-session-title-placeholder::after { animation:none; } .yc-session-skeleton { transition:none; } }
|
||||
@@ -0,0 +1,30 @@
|
||||
/* Plan-defined task-row popconfirm; no modal overlay. Original full-text/pixel parity pending REA. */
|
||||
.task-popconfirm-source .task-popconfirm.wb-popover {
|
||||
box-sizing: border-box;
|
||||
width: calc(var(--yc-space-10) * 5);
|
||||
max-width: calc(100vw - var(--yc-space-5));
|
||||
padding: var(--yc-space-5);
|
||||
border-radius: var(--yc-radius-xl);
|
||||
overflow: visible;
|
||||
color: var(--wb-color-text-primary);
|
||||
background: var(--wb-bg-popover);
|
||||
box-shadow: var(--wb-popover-shadow);
|
||||
}
|
||||
.task-popconfirm__title {
|
||||
margin: 0 0 var(--yc-space-3);
|
||||
font-size: var(--yc-font-size-5);
|
||||
line-height: var(--yc-line-height-5-2);
|
||||
font-weight: 500;
|
||||
}
|
||||
.task-popconfirm__description,
|
||||
.task-popconfirm__compatibility {
|
||||
margin: 0;
|
||||
color: var(--wb-color-text-secondary);
|
||||
font-size: var(--yc-font-size-4);
|
||||
line-height: var(--yc-line-height-5);
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.task-popconfirm__compatibility { margin-top: var(--yc-space-3); color: var(--wb-color-text-tertiary); }
|
||||
.task-popconfirm__error { margin-top: var(--yc-space-3); color: var(--wb-color-text-error-default); font-size: var(--yc-font-size-4); line-height: var(--yc-line-height-5); }
|
||||
.task-popconfirm__actions { display: flex; justify-content: flex-end; gap: var(--yc-space-3); margin-top: var(--yc-space-5); }
|
||||
.task-popconfirm-source .task-popconfirm__actions .wb-button { min-width: calc(var(--yc-space-10) + var(--yc-space-2)); height: var(--yc-line-height-7); padding: 0 var(--yc-space-3); }
|
||||
@@ -1,4 +1,6 @@
|
||||
import { create } from 'zustand'
|
||||
import { resolveSelectedModel } from '@shared/model-selection'
|
||||
import { useSettingsStore } from './settings'
|
||||
import type { ChatEvent, ChatMessage, ContextUsageSnapshot, SessionMeta, TokenUsage } from '@shared/types'
|
||||
import type { ActiveChatRun, RewindOptions, SendPayload } from '@shared/ipc'
|
||||
import type { InteractionResponse } from '@shared/conversation'
|
||||
@@ -6,7 +8,7 @@ import { applyAssistantEvent } from '@shared/message-parts'
|
||||
import { lastContextUsage, normalizeContextUsageSnapshot } from '@shared/context-usage'
|
||||
import type { SessionContext } from '@shared/workspaces'
|
||||
import type { ThinkingLevel } from '@shared/workspaces'
|
||||
import type { AgentMode } from '@shared/conversation'
|
||||
import type { AgentMode, PermissionMode } from '@shared/conversation'
|
||||
import { expertText, type ExpertActivation } from '@shared/capabilities'
|
||||
import { ChatStreamCommitter, type ChatStreamKey } from './chat-stream-committer'
|
||||
import { PENDING_START_STAGE } from '../workbuddy/conversation/prepare-phases'
|
||||
@@ -67,14 +69,16 @@ interface ChatState {
|
||||
running: RunningState | null
|
||||
sending: boolean
|
||||
loading: boolean
|
||||
loadError: string | null
|
||||
error: string | null
|
||||
draftContext: SessionContext
|
||||
draftId: number
|
||||
init(): Promise<void>
|
||||
selectSession(id: string): Promise<void>
|
||||
selectSession(id: string, options?: { reload?: boolean }): Promise<void>
|
||||
newSession(context?: SessionContext, seedMessages?: ChatMessage[]): Promise<SessionMeta>
|
||||
startTask(context?: SessionContext): void
|
||||
selectModel(id: string): Promise<void>
|
||||
selectPermissionMode(mode: PermissionMode): Promise<void>
|
||||
selectExpert(id: string | null): Promise<void>
|
||||
selectAgentMode(mode: AgentMode): Promise<void>
|
||||
selectThinkingLevel(level: ThinkingLevel): Promise<void>
|
||||
@@ -123,6 +127,39 @@ let selectionVersion = 0
|
||||
let expertSelectionVersion = 0
|
||||
let expertWrites: Promise<unknown> = Promise.resolve()
|
||||
let requestSettingsWrites: Promise<unknown> = Promise.resolve()
|
||||
// Serialization is global, but a failed/pending setting only gates its own owner.
|
||||
const requestSettingsByOwner = new Map<string, Promise<unknown>>()
|
||||
const requestSettingErrors = new Map<string, Map<keyof SessionContext, unknown>>()
|
||||
function trackRequestSettings(owner: string, write: Promise<unknown>, keys: readonly (keyof SessionContext)[]): void {
|
||||
const tracked = write.then(value => {
|
||||
const failures = requestSettingErrors.get(owner)
|
||||
for (const key of keys) failures?.delete(key)
|
||||
if (failures?.size === 0) requestSettingErrors.delete(owner)
|
||||
return value
|
||||
}, error => {
|
||||
const failures = requestSettingErrors.get(owner) ?? new Map<keyof SessionContext, unknown>()
|
||||
for (const key of keys) failures.set(key, error)
|
||||
requestSettingErrors.set(owner, failures)
|
||||
throw error
|
||||
})
|
||||
requestSettingsWrites = tracked
|
||||
requestSettingsByOwner.set(owner, tracked)
|
||||
void tracked.catch(() => {})
|
||||
}
|
||||
async function awaitRequestSettings(owner: string): Promise<void> {
|
||||
// A later setting may be queued while an earlier save is pending. Await the
|
||||
// newest owner tail, not another conversation's tail or a stale snapshot.
|
||||
for (;;) {
|
||||
const pending = requestSettingsByOwner.get(owner)
|
||||
try { await pending } catch (error) {
|
||||
if (pending !== requestSettingsByOwner.get(owner)) continue
|
||||
throw error
|
||||
}
|
||||
if (pending === requestSettingsByOwner.get(owner)) break
|
||||
}
|
||||
const failures = requestSettingErrors.get(owner)
|
||||
if (failures?.size) throw failures.values().next().value
|
||||
}
|
||||
const activatingExperts = new Map<string, Promise<ExpertActivation>>()
|
||||
const cancelRequestedRuns = new Set<string>()
|
||||
function prepareExpert(id: string): Promise<ExpertActivation> {
|
||||
@@ -143,11 +180,11 @@ function sortSessions(sessions: SessionMeta[]): SessionMeta[] {
|
||||
}
|
||||
|
||||
export const useChatStore = create<ChatState>((set, get) => {
|
||||
const saveRequestSettings = async (patch: Pick<SessionContext, 'modelId' | 'thinkingLevel'>): Promise<void> => {
|
||||
const saveRequestSettings = async (patch: Pick<SessionContext, 'modelId' | 'thinkingLevel' | 'permissionMode'>): Promise<void> => {
|
||||
const owner = get().currentSessionId
|
||||
if (!owner) { set(state => ({ draftContext: { ...state.draftContext, ...patch } })); return }
|
||||
const write = requestSettingsWrites.catch(() => {}).then(() => window.ycode.session.updateContext(owner, patch))
|
||||
requestSettingsWrites = write.catch(() => {})
|
||||
trackRequestSettings(owner, write, Object.keys(patch) as (keyof SessionContext)[])
|
||||
await write
|
||||
set(state => ({ sessions: state.sessions.map(session => session.id === owner ? { ...session, ...patch } : session),
|
||||
...contextUsageState(state.sessionUsage, state.sessionContextWindow, owner,
|
||||
@@ -235,7 +272,7 @@ export const useChatStore = create<ChatState>((set, get) => {
|
||||
sessions: [], currentSessionId: null, messages: [], sessionMessages: {}, sessionUsage: {}, sessionContextWindow: {},
|
||||
runs: {}, runSequences: {},
|
||||
sendingSessions: [],
|
||||
running: null, sending: false, loading: false, error: null, draftContext: {}, draftId: 0,
|
||||
running: null, sending: false, loading: false, loadError: null, error: null, draftContext: {}, draftId: 0,
|
||||
async init() {
|
||||
try {
|
||||
const sessions = await window.ycode.session.list()
|
||||
@@ -244,7 +281,8 @@ export const useChatStore = create<ChatState>((set, get) => {
|
||||
else get().startTask()
|
||||
} catch (error) { set({ loading: false, error: error instanceof Error ? error.message : String(error) }) }
|
||||
},
|
||||
async selectSession(id) {
|
||||
async selectSession(id, options = {}) {
|
||||
if (!options.reload && get().currentSessionId === id && !get().loading && !get().loadError) return
|
||||
++expertSelectionVersion
|
||||
const version = ++selectionVersion
|
||||
void window.ycode.chat.setSessionVisible(id)
|
||||
@@ -257,7 +295,7 @@ export const useChatStore = create<ChatState>((set, get) => {
|
||||
const hydration: SessionHydration = { version, events: [], started: [] }
|
||||
sessionHydrations.set(id, hydration)
|
||||
streamCommitter.clearSession(id)
|
||||
set((state) => ({ currentSessionId: id, messages: cached ?? [], loading: true, error: null,
|
||||
set((state) => ({ currentSessionId: id, messages: cached ?? [], loading: true, loadError: null, error: null,
|
||||
running: currentRun(state.runs, id), sending: state.sendingSessions.includes(id) }))
|
||||
try {
|
||||
const active = typeof window.ycode.chat.activeRun === 'function' ? await window.ycode.chat.activeRun(id) : null
|
||||
@@ -283,7 +321,7 @@ export const useChatStore = create<ChatState>((set, get) => {
|
||||
}
|
||||
} catch (error) {
|
||||
if (version === selectionVersion && get().currentSessionId === id) {
|
||||
set({ loading: false, error: errorText(error) })
|
||||
set({ loading: false, loadError: errorText(error), error: errorText(error) })
|
||||
}
|
||||
} finally {
|
||||
if (sessionHydrations.get(id) === hydration) sessionHydrations.delete(id)
|
||||
@@ -298,12 +336,18 @@ export const useChatStore = create<ChatState>((set, get) => {
|
||||
++expertSelectionVersion
|
||||
++selectionVersion
|
||||
void window.ycode.chat.setSessionVisible(null)
|
||||
set((state) => ({ currentSessionId: null, messages: [], running: null, sending: false, loading: false, error: null,
|
||||
set((state) => ({ currentSessionId: null, messages: [], running: null, sending: false, loading: false, loadError: null, error: null,
|
||||
draftContext: context, draftId: state.draftId + 1 }))
|
||||
},
|
||||
async selectModel(id) {
|
||||
await saveRequestSettings({ modelId: id })
|
||||
},
|
||||
async selectPermissionMode(permissionMode) {
|
||||
if (get().loading || get().loadError || get().running || get().sending) throw new Error('当前不能修改权限')
|
||||
const mode = (get().currentSessionId ? get().sessions.find(item => item.id === get().currentSessionId) : get().draftContext)?.agentMode
|
||||
if (mode && mode !== 'craft') throw new Error('计划和仅问答模式下不能修改权限')
|
||||
await saveRequestSettings({ permissionMode })
|
||||
},
|
||||
async selectExpert(id) {
|
||||
await get().summonExpert(id)
|
||||
},
|
||||
@@ -317,13 +361,14 @@ export const useChatStore = create<ChatState>((set, get) => {
|
||||
set(state => ({ draftContext: { ...state.draftContext, agentMode, ...(agentMode !== 'craft' ? { expertId: null } : {}) } }))
|
||||
return
|
||||
}
|
||||
const write = expertWrites.catch(() => {}).then(async () => {
|
||||
const write = Promise.all([expertWrites.catch(() => {}), requestSettingsWrites.catch(() => {})]).then(async () => {
|
||||
if (!isCurrent()) throw new Error('任务或模式选择已切换')
|
||||
if (get().running || get().sending) throw new Error('请先停止当前运行')
|
||||
if (get().running) throw new Error('请先停止当前运行')
|
||||
const session = await window.ycode.session.updateContext(owner, { agentMode })
|
||||
set(state => ({ sessions: state.sessions.map(item => item.id === owner ? session : item) }))
|
||||
})
|
||||
expertWrites = write
|
||||
trackRequestSettings(owner, write, ['agentMode'])
|
||||
await write
|
||||
if (isCurrent() && agentMode !== 'craft' && typeof window.dispatchEvent === 'function') window.dispatchEvent(new CustomEvent('ycode:expert-input', { detail: { owner, clear: true } }))
|
||||
},
|
||||
@@ -381,7 +426,7 @@ export const useChatStore = create<ChatState>((set, get) => {
|
||||
if (Object.keys(patch).length) {
|
||||
const id = created.id
|
||||
const write = requestSettingsWrites.catch(() => {}).then(() => window.ycode.session.updateContext(id, patch))
|
||||
requestSettingsWrites = write
|
||||
trackRequestSettings(id, write, Object.keys(patch) as (keyof SessionContext)[])
|
||||
created = { ...created, ...patch }
|
||||
}
|
||||
}
|
||||
@@ -391,7 +436,7 @@ export const useChatStore = create<ChatState>((set, get) => {
|
||||
sessionMessages: { ...state.sessionMessages, [created.id]: seedMessages },
|
||||
...contextUsageState(state.sessionUsage, state.sessionContextWindow, created.id, lastContextUsage(seedMessages)),
|
||||
...(version === selectionVersion ? { currentSessionId: created.id, messages: seedMessages, running: null,
|
||||
sending: false, loading: false, error: null } : {})
|
||||
sending: false, loading: false, loadError: null, error: null } : {})
|
||||
}))
|
||||
return created
|
||||
},
|
||||
@@ -422,6 +467,8 @@ export const useChatStore = create<ChatState>((set, get) => {
|
||||
async removeSession(id) {
|
||||
if (get().currentSessionId === id) void window.ycode.chat.setSessionVisible(null)
|
||||
await window.ycode.session.remove(id)
|
||||
requestSettingsByOwner.delete(id)
|
||||
requestSettingErrors.delete(id)
|
||||
pendingEvents.delete(id)
|
||||
sessionHydrations.delete(id)
|
||||
streamCommitter.clearSession(id)
|
||||
@@ -452,6 +499,17 @@ export const useChatStore = create<ChatState>((set, get) => {
|
||||
async send(text, options = {}) {
|
||||
const trimmed = (text.trim() ? text : (options.userContent ?? []).flatMap(item => item.type === 'text' ? [item.text] : []).join('')).trim()
|
||||
if (get().loading) throw new Error('会话正在加载,请稍后发送')
|
||||
if (get().loadError) throw new Error('会话加载失败,请重试后发送')
|
||||
const settings = useSettingsStore.getState()
|
||||
const context = get().currentSessionId ? get().sessions.find(item => item.id === get().currentSessionId) : get().draftContext
|
||||
const model = resolveSelectedModel(settings.models, context?.modelId, settings.selectedModelId)
|
||||
if (!model) throw new Error('请先配置自定义模型')
|
||||
const modelWrite = context?.modelId !== model.id ? get().selectModel(model.id) : null
|
||||
// Observe rejection immediately; the guarded submission below surfaces it.
|
||||
void modelWrite?.catch(() => {})
|
||||
// Read the saved choice at submission time; a React effect is not authoritative.
|
||||
const savedPermission = context?.permissionMode ?? options.permissionMode ?? 'default-sandbox'
|
||||
options = { ...options, permissionMode: savedPermission }
|
||||
if (options.userContent) options = { ...options, userContent: trimUserContent(options.userContent) }
|
||||
if (!trimmed && !options.attachments?.length && !options.quotes?.length && !options.resources?.length && !options.skillIds?.length && !options.mcpServerIds?.length && !options.command) return
|
||||
let sessionId = get().currentSessionId
|
||||
@@ -478,14 +536,18 @@ export const useChatStore = create<ChatState>((set, get) => {
|
||||
}
|
||||
if (sessionId && currentRun(get().runs, sessionId)) {
|
||||
if (options.action || options.command) throw new Error('运行期间不能重新发送或压缩上下文')
|
||||
await window.ycode.chat.queue.enqueue({ ...options, sessionId, text: trimmed })
|
||||
if (modelWrite) await modelWrite
|
||||
await awaitRequestSettings(sessionId)
|
||||
await window.ycode.chat.queue.enqueue({ ...options, permissionMode: get().sessions.find(item => item.id === sessionId)?.permissionMode ?? savedPermission, sessionId, text: trimmed })
|
||||
return
|
||||
}
|
||||
if (!sessionId || get().sendingSessions.includes(sessionId)) throw new Error('消息正在提交,请稍后重试')
|
||||
const queue = window.ycode.chat.queue ? await window.ycode.chat.queue.get(sessionId) : undefined
|
||||
if (queue && (queue.items.length || queue.editingDraft && (options.command || queue.editingDraft.id !== options.queueEditId))) {
|
||||
if (options.action || options.command) throw new Error('请先处理待发送队列')
|
||||
await window.ycode.chat.queue.enqueue({ ...options, sessionId, text: trimmed })
|
||||
if (modelWrite) await modelWrite
|
||||
await awaitRequestSettings(sessionId)
|
||||
await window.ycode.chat.queue.enqueue({ ...options, permissionMode: get().sessions.find(item => item.id === sessionId)?.permissionMode ?? savedPermission, sessionId, text: trimmed })
|
||||
return
|
||||
}
|
||||
const owner = sessionId
|
||||
@@ -493,8 +555,9 @@ export const useChatStore = create<ChatState>((set, get) => {
|
||||
set((state) => ({ sendingSessions: [...state.sendingSessions, owner],
|
||||
...(state.currentSessionId === owner ? { sending: true, error: null } : {}) }))
|
||||
try {
|
||||
await requestSettingsWrites
|
||||
const result = await window.ycode.chat.send({ ...options, sessionId: owner, text: trimmed })
|
||||
if (modelWrite) await modelWrite
|
||||
await awaitRequestSettings(owner)
|
||||
const result = await window.ycode.chat.send({ ...options, permissionMode: get().sessions.find(item => item.id === owner)?.permissionMode ?? savedPermission, sessionId: owner, text: trimmed })
|
||||
const placeholder: ChatMessage = { id: result.assistantMessageId, requestId: result.runId, role: 'assistant',
|
||||
content: '', tools: [], parts: [], modelId: result.modelId, modelName: result.modelName,
|
||||
assistantDisplay: result.assistantDisplay, createdAt: Date.now(),
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { create } from 'zustand'
|
||||
import { useChatStore } from './chat'
|
||||
import { resolveSelectedModel } from '@shared/model-selection'
|
||||
import type { AppInfo, GatewayConfigView } from '@shared/types'
|
||||
import type { ConfigPatchLike } from '@shared/ipc'
|
||||
import type { CustomModelInput, ModelRegistryView } from '@shared/models'
|
||||
@@ -27,7 +29,7 @@ interface SettingsState {
|
||||
chooseWorkspace(): Promise<void>
|
||||
}
|
||||
|
||||
export const useSettingsStore = create<SettingsState>((set) => ({
|
||||
export const useSettingsStore = create<SettingsState>((set, get) => ({
|
||||
config: null,
|
||||
workspace: '',
|
||||
appInfo: null,
|
||||
@@ -44,8 +46,22 @@ export const useSettingsStore = create<SettingsState>((set) => ({
|
||||
])
|
||||
set({ config, workspace: workspace.root, appInfo, ...registry, ...spaces, loaded: true })
|
||||
},
|
||||
async saveModel(input, overwrite) { set(await window.ycode.models.save(input, overwrite)) },
|
||||
async deleteModel(id) { set(await window.ycode.models.remove(id)) },
|
||||
async saveModel(input, overwrite) {
|
||||
const owner = useChatStore.getState().currentSessionId
|
||||
const draftId = useChatStore.getState().draftId
|
||||
const isNew = !get().models.some(model => model.id === (input.previousId || input.id))
|
||||
const registry = await window.ycode.models.save(input, overwrite)
|
||||
set(registry)
|
||||
reconcileModels(registry, input.previousId && input.previousId !== input.id ? { from: input.previousId, to: input.id } : undefined)
|
||||
// An edit preserves selection; a successful addition becomes the next request's model.
|
||||
const current = useChatStore.getState()
|
||||
if (isNew && current.currentSessionId === owner && (owner !== null || current.draftId === draftId)) await current.selectModel(input.id)
|
||||
},
|
||||
async deleteModel(id) {
|
||||
const registry = await window.ycode.models.remove(id)
|
||||
set(registry)
|
||||
reconcileModels(registry)
|
||||
},
|
||||
async selectModel(id) { set(await window.ycode.models.select(id)) },
|
||||
async createWorkspace(name) { set(await window.ycode.workspace.generate(name)) },
|
||||
async attachWorkspace(path) {
|
||||
@@ -67,3 +83,16 @@ export const useSettingsStore = create<SettingsState>((set) => ({
|
||||
set({ workspace: result.root })
|
||||
}
|
||||
}))
|
||||
|
||||
/** Only request settings change; in-flight and historical message snapshots stay untouched. */
|
||||
function reconcileModels(registry: ModelRegistryView, renamed?: { from: string; to: string }): void {
|
||||
const valid = new Set(registry.models.map(model => model.id))
|
||||
const fallback = resolveSelectedModel(registry.models, registry.selectedModelId)?.id
|
||||
const nextId = (id: string | undefined): string | undefined => renamed && id === renamed.from ? renamed.to : id && !valid.has(id) ? fallback : id
|
||||
useChatStore.setState(state => ({
|
||||
draftContext: nextId(state.draftContext.modelId) !== state.draftContext.modelId
|
||||
? { ...state.draftContext, modelId: nextId(state.draftContext.modelId) } : state.draftContext,
|
||||
sessions: state.sessions.map(session => nextId(session.modelId) !== session.modelId
|
||||
? { ...session, modelId: nextId(session.modelId) } : session)
|
||||
}))
|
||||
}
|
||||
@@ -1,23 +1,24 @@
|
||||
/**
|
||||
* YCode UI Popover / PopoverTooltip(P4)。
|
||||
*
|
||||
* 实现逐行迁移自 `components/source-select/SourceSelectPopover.tsx` 的 REA 适配壳,
|
||||
* 基础实现迁移自 `components/source-select/SourceSelectPopover.tsx` 的 REA 适配壳,
|
||||
* 只改文件位置与导出名:DOM class、floating middleware 顺序、inline/portal 策略、
|
||||
* focus/Escape/outside pointer、transitionPreset 与 aria 合并逻辑一律未改动。
|
||||
* 新增显式锚点、箭头及点击吞掉等可选能力;默认保持旧调用行为。
|
||||
* 迁移保留的既有字面量(数值已存在,token 无法立即表达,本次不发明 token、不改行为):
|
||||
* viewportPadding/offset 默认 8、退出动画 150(reduced-motion 1)、zIndex 1100,
|
||||
* 以及按浮层测量结果写入的行内尺寸。
|
||||
*/
|
||||
import { cloneElement, useCallback, useEffect, useId, useLayoutEffect, useRef, useState, type ReactElement, type ReactNode } from 'react'
|
||||
import { createPortal } from 'react-dom'
|
||||
import { autoUpdate, flip, FloatingFocusManager, offset, shift, size, useFloating, type Padding, type Placement } from '@floating-ui/react'
|
||||
import { arrow as floatingArrow, autoUpdate, flip, FloatingFocusManager, offset, shift, size, useFloating, type Padding, type Placement } from '@floating-ui/react'
|
||||
import { sourceFloatingPlatform } from '../components/source-select/SourceFloatingPlatform'
|
||||
export { RecoveredSelectTooltip as PopoverTooltip } from '../components/source-select/RecoveredSelectTooltip'
|
||||
|
||||
export interface PopoverProps {
|
||||
trigger: ReactElement<any>; children: ReactNode; open: boolean; onOpenChange(open: boolean): void
|
||||
trigger?: ReactElement<any>; anchorElement?: HTMLElement | null; children: ReactNode; open: boolean; onOpenChange(open: boolean): void
|
||||
disabled?: boolean; placement?: Placement; portalRoot?: 'inline' | 'body' | HTMLElement | (() => HTMLElement | null)
|
||||
className?: string; role?: string; ariaLabel?: string; manageFocus?: boolean; initialFocus?: number
|
||||
dismissible?: boolean; arrow?: boolean; consumeOutsidePointer?: boolean; hostClassName?: string; ariaLabelledBy?: string; ariaDescribedBy?: string; returnFocusElement?: HTMLElement | null
|
||||
matchReferenceWidth?: boolean; viewportPadding?: Padding; offset?: number; transitionPreset?: 'expand' | 'fade'
|
||||
}
|
||||
// Host adaptation of the original PopoverLayer: identical middleware order, inline strategy,
|
||||
@@ -35,7 +36,7 @@ export function Popover(props: PopoverProps): React.JSX.Element {
|
||||
return () => clearTimeout(timer)
|
||||
}, [open, transitionPreset])
|
||||
const close = useCallback(() => onOpenChange(false), [onOpenChange])
|
||||
const mergedTrigger = cloneElement(trigger, {
|
||||
const mergedTrigger = trigger ? cloneElement(trigger, {
|
||||
ref: (element: HTMLElement | null) => {
|
||||
anchor.current = element
|
||||
const ref = trigger.props.ref
|
||||
@@ -44,41 +45,67 @@ export function Popover(props: PopoverProps): React.JSX.Element {
|
||||
'aria-label': props.ariaLabel ?? trigger.props['aria-label'],
|
||||
'aria-haspopup': role, 'aria-expanded': open ? 'true' : 'false', 'aria-controls': open ? id : undefined,
|
||||
onClick: (event: React.MouseEvent) => { trigger.props.onClick?.(event); if (!event.defaultPrevented && !disabled) onOpenChange(!open) }
|
||||
})
|
||||
}) : null
|
||||
return <>{mergedTrigger}{(open || exiting) && <PopoverLayer {...props} anchor={anchor} close={close} id={id} exiting={exiting} placement={placement} portalRoot={portalRoot} className={className} role={role} manageFocus={manageFocus} initialFocus={initialFocus} matchReferenceWidth={matchReferenceWidth} viewportPadding={viewportPadding} transitionPreset={transitionPreset}>{children}</PopoverLayer>}</>
|
||||
}
|
||||
function PopoverLayer({ anchor, close, id, exiting, placement, portalRoot, className, role, manageFocus, initialFocus, matchReferenceWidth, viewportPadding, offset: offsetDistance = 8, transitionPreset, children, ariaLabel }: PopoverProps & { anchor: React.RefObject<HTMLElement | null>; close(): void; id: string; exiting: boolean }): React.JSX.Element {
|
||||
function PopoverLayer({ anchor, close, id, exiting, placement, portalRoot, className, role, manageFocus, initialFocus, matchReferenceWidth, viewportPadding, offset: offsetDistance = 8, transitionPreset, children, ariaLabel, ariaLabelledBy, ariaDescribedBy, anchorElement, arrow, consumeOutsidePointer, hostClassName, returnFocusElement, dismissible = true }: PopoverProps & { anchor: React.RefObject<HTMLElement | null>; close(): void; id: string; exiting: boolean }): React.JSX.Element {
|
||||
const inline = portalRoot === 'inline'
|
||||
const arrowRef = useRef<HTMLSpanElement>(null)
|
||||
const reference = anchorElement ?? anchor.current
|
||||
const padding = typeof viewportPadding === 'number' ? viewportPadding : { top: 8, right: 8, bottom: 8, left: 8, ...viewportPadding }
|
||||
const { refs, floatingStyles, context, placement: actual, isPositioned } = useFloating({
|
||||
open: true, onOpenChange: next => { if (!next && !exiting) close() }, placement,
|
||||
middleware: [offset(offsetDistance), flip(), shift({ padding }), ...(matchReferenceWidth ? [size({ padding, apply({ elements, rects, availableHeight }) { Object.assign(elements.floating.style, { width: `${rects.reference.width}px`, maxHeight: `${Math.max(0, availableHeight)}px` }) } })] : [])],
|
||||
const { refs, floatingStyles, context, placement: actual, isPositioned, middlewareData } = useFloating({
|
||||
open: true, onOpenChange: next => { if (!next && !exiting && dismissible) close() }, placement,
|
||||
middleware: [offset(offsetDistance), flip(), shift({ padding }), ...(arrow ? [floatingArrow({ element: arrowRef, padding: 12 })] : []), ...(matchReferenceWidth ? [size({ padding, apply({ elements, rects, availableHeight }) { Object.assign(elements.floating.style, { width: `${rects.reference.width}px`, maxHeight: `${Math.max(0, availableHeight)}px` }) } })] : [])],
|
||||
whileElementsMounted: autoUpdate, strategy: inline ? 'absolute' : 'fixed', transform: false, platform: sourceFloatingPlatform
|
||||
})
|
||||
useLayoutEffect(() => { refs.setReference(anchor.current) }, [refs, anchor])
|
||||
useLayoutEffect(() => { refs.setReference(anchorElement ?? anchor.current) }, [refs, anchorElement, anchor, reference])
|
||||
// External anchors may disappear after filtering, archiving or collapsing the sidebar.
|
||||
useEffect(() => {
|
||||
if (!anchorElement) return
|
||||
const check = (): void => { if (!anchorElement.isConnected) close() }
|
||||
const observer = new MutationObserver(check)
|
||||
observer.observe(document.body, { childList: true, subtree: true })
|
||||
check()
|
||||
return () => observer.disconnect()
|
||||
}, [anchorElement, close])
|
||||
useEffect(() => {
|
||||
if (exiting) return
|
||||
const outside = (event: PointerEvent): void => {
|
||||
if (event.button !== 0 || !(event.target instanceof Node)) return
|
||||
if (!refs.floating.current?.contains(event.target) && !anchor.current?.contains(event.target)) close()
|
||||
if (!refs.floating.current?.contains(event.target) && !reference?.contains(event.target)) {
|
||||
if (consumeOutsidePointer) {
|
||||
event.preventDefault(); event.stopImmediatePropagation()
|
||||
// pointerdown prevention alone does not suppress the subsequent click in all browsers.
|
||||
// Keep this one-shot listener alive through the closing/unmounting of the layer.
|
||||
const outsideTarget = event.target
|
||||
const swallow = (click: MouseEvent): void => {
|
||||
if (click.target === outsideTarget) { click.preventDefault(); click.stopImmediatePropagation() }
|
||||
cleanup()
|
||||
}
|
||||
const cleanup = (): void => { document.removeEventListener('click', swallow, true); clearTimeout(timer) }
|
||||
const timer = window.setTimeout(cleanup, 750)
|
||||
document.addEventListener('click', swallow, true)
|
||||
}
|
||||
if (dismissible) close()
|
||||
}
|
||||
}
|
||||
const escape = (event: KeyboardEvent): void => {
|
||||
if (event.key !== 'Escape') return
|
||||
event.preventDefault(); event.stopPropagation(); close(); anchor.current?.focus({ preventScroll: true })
|
||||
event.preventDefault(); event.stopPropagation(); if (!dismissible) return; close(); (returnFocusElement?.isConnected ? returnFocusElement : reference)?.focus({ preventScroll: true })
|
||||
}
|
||||
document.addEventListener('pointerdown', outside)
|
||||
document.addEventListener('pointerdown', outside, !!consumeOutsidePointer)
|
||||
// Capture prevents the model editor's document Escape listener from also closing the editor.
|
||||
document.addEventListener('keydown', escape, true)
|
||||
return () => { document.removeEventListener('pointerdown', outside); document.removeEventListener('keydown', escape, true) }
|
||||
}, [anchor, close, exiting, refs.floating])
|
||||
return () => { document.removeEventListener('pointerdown', outside, !!consumeOutsidePointer); document.removeEventListener('keydown', escape, true) }
|
||||
}, [reference, close, exiting, refs.floating, consumeOutsidePointer, returnFocusElement, dismissible])
|
||||
useEffect(() => { if (refs.floating.current) refs.floating.current.inert = exiting }, [exiting, refs.floating])
|
||||
const [side, alignment] = actual.split('-'), expand = transitionPreset === 'expand'
|
||||
const origin = side === 'bottom' || side === 'top' ? `${alignment === 'start' ? 'left' : alignment === 'end' ? 'right' : 'center'} ${side === 'bottom' ? 'top' : 'bottom'}` : `${side === 'left' ? 'right' : 'left'} ${alignment === 'start' ? 'top' : alignment === 'end' ? 'bottom' : 'center'}`
|
||||
const element = <div ref={refs.setFloating} id={id} role={role} aria-label={ariaLabel} aria-hidden={exiting || undefined} tabIndex={manageFocus && !exiting ? -1 : undefined}
|
||||
const element = <div ref={refs.setFloating} id={id} role={role} aria-label={ariaLabel} aria-labelledby={ariaLabelledBy} aria-describedby={ariaDescribedBy} aria-hidden={exiting || undefined} tabIndex={manageFocus && !exiting ? -1 : undefined}
|
||||
className={['wb-popover', 'wb-popover--placement-' + side, 'cb-font-size-capped', expand ? 'wb-popover--expand-' + (exiting ? 'close' : isPositioned ? 'open' : 'initial') : '', className].filter(Boolean).join(' ')}
|
||||
style={{ ...floatingStyles, zIndex: 1100, visibility: isPositioned || exiting ? 'visible' : 'hidden', ...(expand ? { transformOrigin: origin } : {}) }}>{children}</div>
|
||||
const managed = manageFocus ? <FloatingFocusManager context={context} modal={false} initialFocus={initialFocus ?? refs.floating}>{element}</FloatingFocusManager> : element
|
||||
style={{ ...floatingStyles, zIndex: 1100, visibility: isPositioned || exiting ? 'visible' : 'hidden', ...(expand ? { transformOrigin: origin } : {}) }}>{arrow && <span ref={arrowRef} aria-hidden="true" className="wb-popover__arrow" style={{ position: 'absolute', pointerEvents: 'none', width: 8, height: 8, background: 'inherit', transform: 'rotate(45deg)', left: middlewareData.arrow?.x, top: middlewareData.arrow?.y, [side === 'left' ? 'right' : side === 'right' ? 'left' : side === 'top' ? 'bottom' : 'top']: -4 }} />}{children}</div>
|
||||
const managed = manageFocus ? <FloatingFocusManager disabled={!isPositioned || exiting} context={context} modal={false} returnFocus={returnFocusElement ? { current: returnFocusElement } : true} initialFocus={initialFocus ?? refs.floating}>{element}</FloatingFocusManager> : element
|
||||
if (inline) return managed
|
||||
const target = typeof portalRoot === 'function' ? portalRoot() : portalRoot instanceof HTMLElement ? portalRoot : document.body
|
||||
return createPortal(<span className="source-select-host">{managed}</span>, target ?? document.body)
|
||||
return createPortal(<span className={["source-select-host", hostClassName].filter(Boolean).join(" ")}>{managed}</span>, target ?? document.body)
|
||||
}
|
||||
@@ -65,6 +65,7 @@ export interface ComposerControlsProps {
|
||||
/** 右组最前:上下文用量环(TODO 21)。 */
|
||||
contextUsageSlot?: ReactNode
|
||||
permissionSlot?: ReactNode
|
||||
modeSlot?: ReactNode
|
||||
expertSlot?: ReactNode
|
||||
floatingSlot?: ReactNode
|
||||
queueSlot?: ReactNode
|
||||
@@ -82,6 +83,7 @@ export interface ComposerControlsProps {
|
||||
workspaceFooter?: ReactNode
|
||||
permissionPlacement?: 'toolbar' | 'footer'
|
||||
disabled?: boolean
|
||||
sendDisabled?: boolean
|
||||
sending?: boolean
|
||||
onStop?(): void | Promise<void>
|
||||
placeholder?: string
|
||||
@@ -167,6 +169,7 @@ export function ComposerControls({
|
||||
branchLabelSlot,
|
||||
contextUsageSlot,
|
||||
permissionSlot,
|
||||
modeSlot,
|
||||
expertSlot,
|
||||
floatingSlot,
|
||||
queueSlot,
|
||||
@@ -184,6 +187,7 @@ export function ComposerControls({
|
||||
workspaceFooter,
|
||||
permissionPlacement = 'toolbar',
|
||||
disabled = false,
|
||||
sendDisabled = false,
|
||||
sending = false,
|
||||
onStop,
|
||||
placeholder = '发送消息…',
|
||||
@@ -423,7 +427,7 @@ export function ComposerControls({
|
||||
const submit = async (): Promise<void> => {
|
||||
const editorContent = richEditorRef.current?.content() ?? userContent
|
||||
const plainText = draftPlainText({ text: value, userContent: editorContent })
|
||||
if (sendInFlightRef.current || disabled || sending || busyCountRef.current > 0 || pendingAttachments.length ||
|
||||
if (sendInFlightRef.current || disabled || sendDisabled || sending || busyCountRef.current > 0 || pendingAttachments.length ||
|
||||
(!plainText.trim() && attachments.length === 0 && resourcesRef.current.length === 0 && quotesRef.current.length === 0 &&
|
||||
!skills.length && !selectedCapabilities.length && !hasDraftUserContent({ userContent: editorContent }))) return
|
||||
const tooLarge = attachments.filter((attachment) => attachment.size > MAX_ATTACHMENT_BYTES)
|
||||
@@ -574,8 +578,9 @@ export function ComposerControls({
|
||||
if (anchor) (id === 'experts' ? onOpenExperts : onOpenSkills)?.(anchor)
|
||||
}
|
||||
}} />
|
||||
{permissionSlot}
|
||||
{branchLabelSlot}
|
||||
{permissionSlot}
|
||||
{modeSlot}
|
||||
{expertSlot}
|
||||
</div>
|
||||
|
||||
@@ -594,7 +599,7 @@ export function ComposerControls({
|
||||
<span className="cr-send-button__icon"><IconStop /></span>
|
||||
</button>
|
||||
) : (
|
||||
<button type="button" className="wb-composer__send cr-send-button cr-input-toolbar__send" title="发送" aria-label="发送" onClick={() => void submit()} disabled={disabled || sending || busy || pendingAttachments.length > 0 || !hasSendableContent}>
|
||||
<button type="button" className="wb-composer__send cr-send-button cr-input-toolbar__send" title="发送" aria-label="发送" onClick={() => void submit()} disabled={disabled || sendDisabled || sending || busy || pendingAttachments.length > 0 || !hasSendableContent}>
|
||||
<span className="cr-send-button__icon"><RuntimeSendIcon /></span>
|
||||
</button>
|
||||
)}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { AgentMode } from '@shared/conversation'
|
||||
import './mode-chip.css'
|
||||
|
||||
/** Host chip. Icon geometry is local; pending same-version REA SVG/pixel acceptance. */
|
||||
export function ModeChip({ mode, disabled, onClear }: { mode: AgentMode; disabled?: boolean; onClear(): void | Promise<void> }): React.JSX.Element | null {
|
||||
if (mode === 'craft') return null
|
||||
const label = mode === 'plan' ? '计划' : '仅问答'
|
||||
return <span className="yc-mode-chip" role="group" tabIndex={0} aria-label={`${label}模式`}>
|
||||
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.2" aria-hidden="true">
|
||||
{mode === 'plan' ? <><rect x="3" y="3" width="10" height="11" rx="1.5" /><rect x="5.5" y="1.5" width="5" height="3" rx="1" /><path d="M5.5 7h5M5.5 10h5" /></> : <path d="M13.5 7.5a5.5 5.5 0 0 1-5.5 5.5H3l-1.5 1v-6.5A5.5 5.5 0 0 1 7 2h1a5.5 5.5 0 0 1 5.5 5.5Z" />}
|
||||
</svg><span>{label}</span>
|
||||
{!disabled && <button type="button" className="yc-mode-chip__clear" aria-label={`退出${label}模式`} title="切换到工作模式" onClick={() => void onClear()}>×</button>}
|
||||
</span>
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
.yc-mode-chip { position:relative; display:inline-flex; flex-shrink:0; align-items:center; gap:var(--yc-space-2); height:var(--yc-space-8); padding:0 var(--yc-space-3); border-radius:var(--yc-space-3); background:var(--cr-bg-tertiary); color:var(--cr-text-primary); font-size:var(--yc-font-size-4); line-height:var(--yc-line-height-5); font-weight:500; }
|
||||
.yc-mode-chip > svg { flex-shrink:0; width:var(--yc-space-5); height:var(--yc-space-5); }
|
||||
.yc-mode-chip:focus-visible { outline:var(--yc-space-1) solid var(--cr-text-secondary); outline-offset:var(--yc-space-1); }
|
||||
.yc-mode-chip__clear { display:inline-flex; align-items:center; justify-content:center; width:var(--yc-space-5); height:var(--yc-space-5); padding:0; border:0; border-radius:50%; background:var(--cr-bg-tertiary); color:inherit; font-size:var(--yc-space-5); cursor:pointer; opacity:0; position:absolute; right:calc(-1 * var(--wb-spacing-2-5)); top:calc(-1.5 * var(--yc-space-1)); }
|
||||
.yc-mode-chip:hover .yc-mode-chip__clear, .yc-mode-chip:focus-within .yc-mode-chip__clear { opacity:1; }
|
||||
.yc-mode-chip__clear:focus-visible { outline:var(--yc-space-1) solid var(--cr-text-secondary); }
|
||||
@@ -19,6 +19,7 @@ const CheckGlyph = SelectCheckGlyph as ComponentType<SVGProps<SVGSVGElement>>
|
||||
interface Props {
|
||||
mode: PermissionMode
|
||||
disabled?: boolean
|
||||
disabledReason?: string
|
||||
labels?: Partial<Record<PermissionMode, string>>
|
||||
confirmFullAccessOnChange?: boolean
|
||||
onChange(mode: PermissionMode): Promise<void>
|
||||
@@ -29,7 +30,7 @@ interface Props {
|
||||
* The source panel has one full-access switch, not a two-option permission list.
|
||||
* Its checkbox acknowledges risk; it does not change the global confirmation preference.
|
||||
*/
|
||||
export default function SourcePermissionControl({ mode, disabled, labels, confirmFullAccessOnChange = true, onChange, onError }: Props): React.JSX.Element {
|
||||
export default function SourcePermissionControl({ mode, disabled, disabledReason, labels, confirmFullAccessOnChange = true, onChange, onError }: Props): React.JSX.Element {
|
||||
const [open, setOpen] = useState(false)
|
||||
const [confirming, setConfirming] = useState(false)
|
||||
const [acknowledged, setAcknowledged] = useState(false)
|
||||
@@ -39,6 +40,9 @@ export default function SourcePermissionControl({ mode, disabled, labels, confir
|
||||
const trigger = useRef<HTMLButtonElement>(null)
|
||||
const dialog = useRef<HTMLDivElement>(null)
|
||||
const id = useId()
|
||||
useEffect(() => {
|
||||
if (disabled) { setOpen(false); setConfirming(false); setAcknowledged(false) }
|
||||
}, [disabled])
|
||||
const full = mode === 'full-access'
|
||||
const label = labels?.[mode] ?? t(full ? 'chatInput.permission.fullAccess' : 'chatInput.permission.label')
|
||||
useEffect(() => {
|
||||
@@ -79,9 +83,9 @@ export default function SourcePermissionControl({ mode, disabled, labels, confir
|
||||
else { setAcknowledged(false); setConfirming(true) }
|
||||
}
|
||||
return <div className="wb-composer__menu-anchor yc-source-permission-trigger">
|
||||
<SourceSelectPopover open={open} onOpenChange={next => { if (!confirming) setOpen(next) }} placement="top-start" portalRoot="body" offset={4}
|
||||
<SourceSelectPopover open={open && !disabled} onOpenChange={next => { if (!disabled && !confirming) setOpen(next) }} placement="top-start" portalRoot="body" offset={4}
|
||||
role="dialog" className="cr-permission-setting-popover" disabled={disabled || pending}
|
||||
trigger={<button ref={trigger} type="button" disabled={disabled || pending} aria-label={label} title={label}
|
||||
trigger={<button ref={trigger} type="button" disabled={disabled || pending} aria-label={label} title={disabledReason ?? label}
|
||||
className={['wb-composer__button', 'cr-input-footer-item', 'cr-permission-setting', full && 'cr-permission-setting--danger', open && 'cr-input-footer-item--open'].filter(Boolean).join(' ')}>
|
||||
<span className="cr-input-footer-item__icon" aria-hidden="true">{full ? <ShieldDangerIcon /> : <ShieldSecuredIcon />}</span>
|
||||
<span className="cr-input-footer-item__label" data-text={label}><span>{label}</span></span>
|
||||
@@ -114,7 +118,7 @@ export default function SourcePermissionControl({ mode, disabled, labels, confir
|
||||
</div>
|
||||
</div>
|
||||
</SourceSelectPopover>
|
||||
{confirming && createPortal(<div className="workspace-source source-select-host yc-source-model-selector yc-source-permission cr-theme" data-cr-theme={theme} data-theme={theme}>
|
||||
{confirming && !disabled && createPortal(<div className="workspace-source source-select-host yc-source-model-selector yc-source-permission cr-theme" data-cr-theme={theme} data-theme={theme}>
|
||||
<div className="wb-modal__overlay wb-modal__overlay--center wb-modal-anim-open" onMouseDown={event => {
|
||||
if (event.target === event.currentTarget && !pending) { setConfirming(false); setAcknowledged(false) }
|
||||
}}>
|
||||
|
||||
@@ -5,7 +5,6 @@ import {
|
||||
classifyCompletedFoldPart,
|
||||
type CompletedFoldOptions,
|
||||
type CompletedFoldPart,
|
||||
type CompletedFoldRenderUnit,
|
||||
type CompletedFoldSegment,
|
||||
type CompletedFoldSummaryGroup,
|
||||
type CompletedFoldUnit
|
||||
@@ -159,6 +158,7 @@ export function CompletedFold({
|
||||
renderUnit: render,
|
||||
renderGroup
|
||||
}: CompletedFoldProps) {
|
||||
const [completedExpanded, setCompletedExpanded] = useState(expandFinishedEvents)
|
||||
const reactId = useId()
|
||||
const foldKey = encodeURIComponent(foldId ?? reactId)
|
||||
|
||||
@@ -180,6 +180,19 @@ export function CompletedFold({
|
||||
})}</Fragment>
|
||||
})}</>
|
||||
|
||||
// The compact model groups hidden process around visible anchors. Once opened,
|
||||
// show the original recorded sequence, rather than moving trailing work before A.
|
||||
if (completedExpanded && model.segments.some(segment => segment.kind === 'completed')) {
|
||||
return <CollapseSection id={`completed-${foldKey}`} variant="completed" title={model.title}
|
||||
controlledExpanded onExpandedChange={setCompletedExpanded}>
|
||||
{parts.map((part, index) => {
|
||||
const classification = classifyCompletedFoldPart(part, index, classifyToolCall)
|
||||
if (!classification.hasRootBlock) return null
|
||||
return <Fragment key={`${part.type}:${index}`}>{render(part, index, { kind: 'part', classification })}</Fragment>
|
||||
})}
|
||||
</CollapseSection>
|
||||
}
|
||||
|
||||
const renderSegments = (segments: readonly CompletedFoldSegment[]) => segments.map((segment, segmentIndex) => {
|
||||
if (segment.kind === 'unit') return renderPresentationUnit(segment.unit, render, renderGroup)
|
||||
if (segment.kind === 'completed') {
|
||||
@@ -189,7 +202,8 @@ export function CompletedFold({
|
||||
id={`completed-${foldKey}`}
|
||||
variant="completed"
|
||||
title={model.title}
|
||||
defaultExpanded={expandFinishedEvents && segment.units.some(renderUnitContainsEvent)}
|
||||
controlledExpanded={completedExpanded}
|
||||
onExpandedChange={setCompletedExpanded}
|
||||
>
|
||||
{segment.units.map(unit => renderPresentationUnit(unit, render, renderGroup))}
|
||||
</CollapseSection>
|
||||
@@ -210,10 +224,3 @@ export function CompletedFold({
|
||||
|
||||
return <>{renderSegments(model.segments)}</>
|
||||
}
|
||||
|
||||
function renderUnitContainsEvent(unit: CompletedFoldUnit | CompletedFoldRenderUnit): boolean {
|
||||
if ('part' in unit) return unit.part.type === 'tool' || unit.part.type === 'reasoning'
|
||||
return unit.kind === 'part'
|
||||
? unit.unit.part.type === 'tool' || unit.unit.part.type === 'reasoning'
|
||||
: unit.group.units.some(member => member.part.type === 'tool' || member.part.type === 'reasoning')
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { useEffect, useMemo, useState, useRef } from 'react'
|
||||
import type { ApprovalDetails } from '@shared/conversation'
|
||||
import './permission-intercept.css'
|
||||
import { ApprovalChevronIcon } from './ApprovalSourceIcon'
|
||||
import type { PermissionLabels, PermissionOption } from './types'
|
||||
|
||||
@@ -40,6 +42,7 @@ export interface PermissionConfirmProps {
|
||||
pendingId: string
|
||||
toolName: string
|
||||
toolKind?: string
|
||||
details?: ApprovalDetails
|
||||
approvalScope?: 'once' | 'session'
|
||||
options: PermissionOption[]
|
||||
resolve: (pendingId: string, result: { optionId: string }) => void | Promise<void>
|
||||
@@ -57,6 +60,7 @@ export function PermissionConfirm({
|
||||
pendingId,
|
||||
toolName,
|
||||
toolKind,
|
||||
details,
|
||||
approvalScope,
|
||||
options,
|
||||
resolve,
|
||||
@@ -74,11 +78,25 @@ export function PermissionConfirm({
|
||||
const [localStatus, setLocalStatus] = useState<PermissionInteractionStatus>('pending')
|
||||
const [localError, setLocalError] = useState<string>()
|
||||
const [busy, setBusy] = useState(false)
|
||||
const busyRef = useRef(false)
|
||||
const mounted = useRef(true)
|
||||
const externalStatusRef = useRef(externalStatus)
|
||||
externalStatusRef.current = externalStatus
|
||||
const currentId = useRef(pendingId)
|
||||
currentId.current = pendingId
|
||||
const firstRef = useRef<HTMLButtonElement>(null)
|
||||
const rejectRef = useRef<HTMLButtonElement>(null)
|
||||
const [showRule, setShowRule] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
mounted.current = true
|
||||
setLocalStatus('pending')
|
||||
setLocalError(undefined)
|
||||
setBusy(false)
|
||||
busyRef.current = false
|
||||
setShowRule(false)
|
||||
if (details?.defaultToNo) rejectRef.current?.focus(); else firstRef.current?.focus()
|
||||
return () => { mounted.current = false }
|
||||
}, [pendingId])
|
||||
|
||||
const status = externalStatus !== 'pending' ? externalStatus : localStatus
|
||||
@@ -88,36 +106,42 @@ export function PermissionConfirm({
|
||||
const isResponding = busy || status === 'responding'
|
||||
|
||||
const choose = async (option: PermissionOption | undefined) => {
|
||||
if (!option || busy || !isOpen) return
|
||||
if (!option || busyRef.current || !isOpen) return
|
||||
busyRef.current = true
|
||||
setBusy(true)
|
||||
setLocalError(undefined)
|
||||
setLocalStatus('responding')
|
||||
try {
|
||||
await resolve(pendingId, { optionId: option.optionId })
|
||||
if (!mounted.current || currentId.current !== pendingId || ['cancelled', 'rejected'].includes(externalStatusRef.current)) return
|
||||
setLocalStatus('answered')
|
||||
onDecisionComplete?.('answered')
|
||||
} catch (caught) {
|
||||
if (!mounted.current || currentId.current !== pendingId || ['cancelled', 'rejected'].includes(externalStatusRef.current)) return
|
||||
setLocalError(caught instanceof Error ? caught.message : String(caught))
|
||||
setLocalStatus('error')
|
||||
} finally {
|
||||
setBusy(false)
|
||||
if (mounted.current && currentId.current === pendingId) { busyRef.current = false; setBusy(false) }
|
||||
}
|
||||
}
|
||||
|
||||
const decline = async () => {
|
||||
if (busy || !isOpen) return
|
||||
if (busyRef.current || !isOpen) return
|
||||
busyRef.current = true
|
||||
setBusy(true)
|
||||
setLocalError(undefined)
|
||||
setLocalStatus('responding')
|
||||
try {
|
||||
await reject(pendingId)
|
||||
if (!mounted.current || currentId.current !== pendingId || ['cancelled', 'rejected'].includes(externalStatusRef.current)) return
|
||||
setLocalStatus('rejected')
|
||||
onDecisionComplete?.('rejected')
|
||||
} catch (caught) {
|
||||
if (!mounted.current || currentId.current !== pendingId || ['cancelled', 'rejected'].includes(externalStatusRef.current)) return
|
||||
setLocalError(caught instanceof Error ? caught.message : String(caught))
|
||||
setLocalStatus('error')
|
||||
} finally {
|
||||
setBusy(false)
|
||||
if (mounted.current && currentId.current === pendingId) { busyRef.current = false; setBusy(false) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,43 +153,60 @@ export function PermissionConfirm({
|
||||
|
||||
return (
|
||||
<section
|
||||
className={['conversation-high-credit-approval', `conversation-high-credit-approval--${status}`, className].filter(Boolean).join(' ')}
|
||||
className={['conversation-intercept-card', `conversation-intercept-card--${status}`, className].filter(Boolean).join(' ')}
|
||||
role="dialog"
|
||||
aria-label={heading}
|
||||
aria-busy={isResponding}
|
||||
data-pending-id={pendingId}
|
||||
data-request-kind={details?.requestKind ?? 'permission'}
|
||||
onKeyDown={event => {
|
||||
if (event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement) return
|
||||
if (event.key === 'Escape' && isOpen && !isResponding) { event.preventDefault(); event.stopPropagation(); void decline(); return }
|
||||
const index = Number(event.key) - 1
|
||||
const action = actionOptions[index]
|
||||
if (!details?.defaultToNo && action && !event.altKey && !event.ctrlKey && !event.metaKey && !isResponding) {
|
||||
event.preventDefault()
|
||||
if (action.kind === 'reject') void decline(); else void choose(action.option)
|
||||
}
|
||||
}}
|
||||
>
|
||||
<header className="conversation-high-credit-approval__header">
|
||||
<div className="conversation-high-credit-approval__title-row"><span className="conversation-high-credit-approval__title">{heading}</span></div>
|
||||
{description && <p className="conversation-high-credit-approval__description">{description}</p>}
|
||||
<header className="conversation-intercept-card__header">
|
||||
<div className="conversation-intercept-card__title-row"><span className="conversation-intercept-card__title">{heading}</span>
|
||||
<button type="button" className="conversation-intercept-card__rule-action" aria-expanded={showRule} onClick={() => setShowRule(value => !value)}>查看拦截规则</button>
|
||||
</div>
|
||||
{showRule && <div className="conversation-intercept-card__rule" role="status">{details?.rule ? [details.rule.id, details.rule.description].filter(Boolean).join(':') : '当前请求未提供命中规则详情,无法查看。'}</div>}
|
||||
{(details?.command || details?.target) && <pre className="conversation-intercept-card__description">{details.command || details.target}</pre>}
|
||||
{details?.suggestedRules?.length ? <pre className="conversation-intercept-card__description">本次会话授权范围:{details.suggestedRules.join('、')}</pre> : details?.sessionUnavailableReason ? <p className="conversation-intercept-card__description">{details.sessionUnavailableReason}</p> : null}
|
||||
{description && <p className="conversation-intercept-card__description">{description}</p>}
|
||||
</header>
|
||||
{status === 'pending' || status === 'responding' || status === 'error' ? (
|
||||
<>
|
||||
{status === 'error' && <div className="conversation-high-credit-approval__error" role="alert">{labels.failed}{error ? `: ${error}` : ''}</div>}
|
||||
<div className="conversation-high-credit-approval__content">
|
||||
{status === 'error' && <div className="conversation-intercept-card__error" role="alert">{labels.failed}{error ? `: ${error}` : ''}</div>}
|
||||
<div className="conversation-intercept-card__content">
|
||||
{actionOptions.map((action, index) => (
|
||||
<button
|
||||
key={`${action.kind}-${action.option?.optionId || 'reject'}`}
|
||||
type="button"
|
||||
ref={action.kind === 'reject' ? rejectRef : index === 0 ? firstRef : undefined}
|
||||
className={[
|
||||
'conversation-high-credit-approval__option',
|
||||
action.kind === 'reject' && 'conversation-high-credit-approval__option--reject'
|
||||
'conversation-intercept-card__option',
|
||||
action.kind === 'reject' && 'conversation-intercept-card__option--reject'
|
||||
].filter(Boolean).join(' ')}
|
||||
disabled={isResponding || (action.kind !== 'reject' && !action.option)}
|
||||
aria-label={action.label}
|
||||
data-option-id={action.option?.optionId}
|
||||
onClick={() => action.kind === 'reject' ? void decline() : void choose(action.option)}
|
||||
>
|
||||
<span className="conversation-high-credit-approval__option-num" aria-hidden="true">{index + 1}</span>
|
||||
<span className="conversation-high-credit-approval__option-text">{action.label}</span>
|
||||
{action.option && <ApprovalChevronIcon className="conversation-high-credit-approval__option-arrow" />}
|
||||
<span className="conversation-intercept-card__option-num" aria-hidden="true">{index + 1}</span>
|
||||
<span className="conversation-intercept-card__option-text">{action.label}</span>
|
||||
{action.option && <ApprovalChevronIcon className="conversation-intercept-card__option-arrow" />}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
{isResponding && <div className="conversation-high-credit-approval__working" role="status">{labels.working}</div>}
|
||||
{isResponding && <div className="conversation-intercept-card__working" role="status">{labels.working}</div>}
|
||||
</>
|
||||
) : (
|
||||
<div className={`conversation-high-credit-approval__decision conversation-high-credit-approval__decision--${status}`} role="status">
|
||||
<div className={`conversation-intercept-card__decision conversation-intercept-card__decision--${status}`} role="status">
|
||||
{status === 'answered' ? labels.approved
|
||||
: status === 'rejected' ? labels.rejected
|
||||
: status === 'cancelled' ? labels.cancelled
|
||||
|
||||
@@ -53,8 +53,8 @@ export interface QuestionFloatingHandle {
|
||||
export interface QuestionFloatingProps {
|
||||
config: QuestionFloatingConfig
|
||||
onSelect?: (answerText: string, answer: QuestionAnswerPayload) => void | Promise<void>
|
||||
onDismiss?: () => void
|
||||
onSkip?: () => void
|
||||
onDismiss?: () => void | Promise<void>
|
||||
onSkip?: () => void | Promise<void>
|
||||
className?: string
|
||||
confirmVariant?: boolean
|
||||
titleHintIcon?: ReactNode
|
||||
@@ -68,11 +68,11 @@ type CustomAnswerMap = Record<string, string>
|
||||
type CustomToggleMap = Record<string, boolean>
|
||||
|
||||
export function getQuestionOptionText(option: QuestionOption): string {
|
||||
return typeof option === 'string' ? option : option.question || option.option || ''
|
||||
return typeof option === 'string' ? option : option.question || option.option || option.label || ''
|
||||
}
|
||||
|
||||
export function getQuestionOptionValue(option: QuestionOption): string {
|
||||
return typeof option === 'string' ? option : option.id || option.question || option.option || ''
|
||||
return typeof option === 'string' ? option : option.id || option.question || option.option || option.label || ''
|
||||
}
|
||||
|
||||
export function getDefaultQuestionSelections(questions: ConversationQuestion[]): AnswerMap {
|
||||
@@ -126,7 +126,7 @@ function isValidQuestion(question: ConversationQuestion): boolean {
|
||||
function isValidOption(option: QuestionOption): boolean {
|
||||
if (typeof option === 'string') return true
|
||||
if (!option || typeof option !== 'object') return false
|
||||
return typeof option.question === 'string' || typeof option.option === 'string'
|
||||
return typeof option.question === 'string' || typeof option.option === 'string' || typeof option.label === 'string'
|
||||
}
|
||||
|
||||
function answerText(payload: QuestionAnswerPayload, labels: QuestionLabels): string {
|
||||
@@ -153,16 +153,25 @@ export const QuestionFloating = forwardRef<QuestionFloatingHandle, QuestionFloat
|
||||
) {
|
||||
const labels = { ...defaultQuestionLabels, ...labelOverrides }
|
||||
const questions = useMemo(() => (config.questions || []).filter(isValidQuestion), [config.questions])
|
||||
const questionKey = questions.map(question => question.id).join('|')
|
||||
const questionKey = `${config.requestId ?? config.toolCallId ?? ''}:${questions.map(question => question.id).join('|')}`
|
||||
const [index, setIndex] = useState(0)
|
||||
const [selected, setSelected] = useState<AnswerMap>(() => getDefaultQuestionSelections(config.questions || []))
|
||||
const [selected, setSelected] = useState<AnswerMap>(() => getDefaultQuestionSelections(questions))
|
||||
const [custom, setCustom] = useState<CustomAnswerMap>({})
|
||||
const [includeCustom, setIncludeCustom] = useState<CustomToggleMap>({})
|
||||
const [submitted, setSubmitted] = useState(false)
|
||||
const [localError, setLocalError] = useState<string | null>(null)
|
||||
const submitting = useRef(false)
|
||||
const generation = useRef(0)
|
||||
const advanceTimer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)
|
||||
const rootRef = useRef<HTMLElement>(null)
|
||||
const [customFocused, setCustomFocused] = useState(false)
|
||||
const customInputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
useEffect(() => {
|
||||
generation.current += 1
|
||||
if (advanceTimer.current) clearTimeout(advanceTimer.current)
|
||||
submitting.current = false
|
||||
setLocalError(null)
|
||||
setIndex(0)
|
||||
setCustom({})
|
||||
setIncludeCustom({})
|
||||
@@ -171,6 +180,26 @@ export const QuestionFloating = forwardRef<QuestionFloatingHandle, QuestionFloat
|
||||
setSelected(getDefaultQuestionSelections(questions))
|
||||
}, [questionKey])
|
||||
|
||||
useEffect(() => () => { if (advanceTimer.current) clearTimeout(advanceTimer.current) }, [])
|
||||
useEffect(() => { if (error) { submitting.current = false; setSubmitted(false) } }, [error])
|
||||
useEffect(() => {
|
||||
rootRef.current?.querySelector<HTMLElement>('[role="button"][tabindex="0"], input:not(:disabled)')?.focus()
|
||||
}, [index, questionKey])
|
||||
|
||||
const commit = (action: () => void | Promise<void>) => {
|
||||
if (submitting.current || responding) return
|
||||
submitting.current = true
|
||||
setSubmitted(true)
|
||||
setLocalError(null)
|
||||
const owner = generation.current
|
||||
void Promise.resolve().then(action).catch(cause => {
|
||||
if (owner !== generation.current) return
|
||||
submitting.current = false
|
||||
setSubmitted(false)
|
||||
setLocalError(cause instanceof Error ? cause.message : String(cause))
|
||||
})
|
||||
}
|
||||
|
||||
const currentIndex = Math.min(Math.max(index, 0), Math.max(questions.length - 1, 0))
|
||||
const current = questions[currentIndex]
|
||||
const isLast = currentIndex === questions.length - 1
|
||||
@@ -191,8 +220,7 @@ export const QuestionFloating = forwardRef<QuestionFloatingHandle, QuestionFloat
|
||||
if (submitted || responding) return
|
||||
const payload = getPayload(nextSelected, nextCustom, nextIncludeCustom)
|
||||
if (!payload) return
|
||||
setSubmitted(true)
|
||||
void Promise.resolve(onSelect?.(answerText(payload, labels), payload)).catch(() => setSubmitted(false))
|
||||
commit(() => onSelect?.(answerText(payload, labels), payload))
|
||||
}
|
||||
|
||||
useImperativeHandle(ref, () => ({
|
||||
@@ -223,7 +251,8 @@ export const QuestionFloating = forwardRef<QuestionFloatingHandle, QuestionFloat
|
||||
if (isLast) {
|
||||
submit(nextSelected, nextCustom)
|
||||
} else {
|
||||
window.setTimeout(() => setIndex(value => Math.min(value + 1, questions.length - 1)), 120)
|
||||
if (advanceTimer.current) clearTimeout(advanceTimer.current)
|
||||
advanceTimer.current = setTimeout(() => setIndex(value => Math.min(value + 1, questions.length - 1)), 120)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -255,8 +284,7 @@ export const QuestionFloating = forwardRef<QuestionFloatingHandle, QuestionFloat
|
||||
})),
|
||||
...(config.title ? { title: config.title } : {})
|
||||
}
|
||||
setSubmitted(true)
|
||||
void Promise.resolve(onSelect?.(answerText(payload, labels), payload)).catch(() => setSubmitted(false))
|
||||
commit(() => onSelect?.(answerText(payload, labels), payload))
|
||||
} else {
|
||||
setIndex(value => Math.min(value + 1, questions.length - 1))
|
||||
}
|
||||
@@ -264,8 +292,7 @@ export const QuestionFloating = forwardRef<QuestionFloatingHandle, QuestionFloat
|
||||
|
||||
const dismiss = () => {
|
||||
if (responding || submitted) return
|
||||
if (onSkip) onSkip()
|
||||
else onDismiss?.()
|
||||
commit(() => onSkip ? onSkip() : onDismiss?.())
|
||||
}
|
||||
|
||||
const showSingleSkip = !current.multiSelect && questions.length !== 1 && !isLast && !hasCustomText
|
||||
@@ -274,6 +301,10 @@ export const QuestionFloating = forwardRef<QuestionFloatingHandle, QuestionFloat
|
||||
|
||||
return (
|
||||
<section
|
||||
ref={rootRef}
|
||||
onKeyDown={event => {
|
||||
if (event.key === 'Escape') { event.preventDefault(); event.stopPropagation(); dismiss() }
|
||||
}}
|
||||
className={[
|
||||
'question-floating',
|
||||
confirmVariant && 'question-floating--confirm',
|
||||
@@ -433,7 +464,7 @@ export const QuestionFloating = forwardRef<QuestionFloatingHandle, QuestionFloat
|
||||
onFocus={() => setCustomFocused(true)}
|
||||
onBlur={() => setCustomFocused(false)}
|
||||
onKeyDown={event => {
|
||||
if (event.key === 'Enter' && !current.multiSelect && hasCustomText) {
|
||||
if (event.key === 'Enter' && !event.nativeEvent.isComposing && !current.multiSelect && hasCustomText) {
|
||||
event.preventDefault()
|
||||
advance()
|
||||
}
|
||||
@@ -442,7 +473,7 @@ export const QuestionFloating = forwardRef<QuestionFloatingHandle, QuestionFloat
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{error && <div className="question-floating__error" role="alert">{error}</div>}
|
||||
{(error || localError) && <div className="question-floating__error" role="alert">{error || localError}</div>}
|
||||
<footer className={[
|
||||
'question-floating__footer',
|
||||
!current.multiSelect && 'question-floating__footer--single'
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { RuntimeProgress } from '@shared/types'
|
||||
import { useEffect, useMemo, useRef, useState, useSyncExternalStore, type CSSProperties } from 'react'
|
||||
import { SOURCE_LOADING_TIPS } from './streaming-loading-tips'
|
||||
import { conversationLabel } from './labels'
|
||||
@@ -100,27 +101,33 @@ export interface StreamingFooterProps {
|
||||
locale?: string
|
||||
/** 新任务启动阶段文案(原版 chat.prepare.<stage>);提供时替换「思考中」。 */
|
||||
prepareText?: string | null
|
||||
progress?: RuntimeProgress
|
||||
reasoningOnly?: boolean
|
||||
onCloseTips?: () => void
|
||||
}
|
||||
|
||||
export function StreamingFooter({ locale, prepareText }: StreamingFooterProps): React.JSX.Element {
|
||||
export function StreamingFooter({ locale, prepareText, progress, reasoningOnly = false, onCloseTips }: StreamingFooterProps): React.JSX.Element {
|
||||
const resolvedLocale = locale ?? activeLocale()
|
||||
const brandName = activeBrandName()
|
||||
const tips = useMemo(() => getSourceLoadingTips(resolvedLocale, brandName), [brandName, resolvedLocale])
|
||||
// 设置 → 个性化「欢迎语」:关闭后不再轮播加载提示(服务端 token 用量与思考中状态保持不变)。
|
||||
const tipsEnabled = useSyncExternalStore(subscribeLoadingTips, loadingTipsEnabled, () => true)
|
||||
const [tip, setTip] = useState<{ text: string; index: number } | null>(null)
|
||||
const isPaused = useRef(false)
|
||||
const pause = useRef({ hovered: false, focused: false })
|
||||
const [tipsClosed, setTipsClosed] = useState(false)
|
||||
const showTips = tipsEnabled && !tipsClosed && !prepareText && progress?.phase !== 'preparing' && progress?.phase !== 'finishing'
|
||||
|
||||
useEffect(() => {
|
||||
if (!tipsEnabled) { setTip(null); return }
|
||||
if (!showTips) { setTip(null); return }
|
||||
return scheduleLoadingTips({
|
||||
tips,
|
||||
onTip: (text, index) => setTip({ text, index }),
|
||||
isPaused: () => isPaused.current
|
||||
isPaused: () => isLoadingTipPaused(pause.current)
|
||||
})
|
||||
}, [tips, tipsEnabled])
|
||||
}, [tips, showTips])
|
||||
|
||||
const thinkingLabel = prepareText ?? conversationLabel('message.loading.thinking', {}, resolveLoadingTipLocale(resolvedLocale) === 'en' ? 'en' : 'zh-cn')
|
||||
const thinkingLabel = resolveRuntimeStatusText(prepareText, progress, resolveLoadingTipLocale(resolvedLocale))
|
||||
if (reasoningOnly && !prepareText && (!progress || progress.phase === 'thinking')) return <></>
|
||||
|
||||
return <div className="wb-cr-streaming-footer">
|
||||
<div className="_loadingWithCredit_ttf8p_463" style={{ '--cb-assistant-inline-padding': '0px' } as CSSProperties}>
|
||||
@@ -131,16 +138,36 @@ export function StreamingFooter({ locale, prepareText }: StreamingFooterProps):
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{tipsEnabled && tip && <span
|
||||
{showTips && tip && <span
|
||||
className="_tipPhrase_13r4w_108 _trailingTipPhrase_13r4w_179 cb-loading-tip cb-loading-tip--trailing"
|
||||
onMouseEnter={() => { isPaused.current = true }}
|
||||
onMouseLeave={() => { isPaused.current = false }}
|
||||
tabIndex={0}
|
||||
onFocus={() => { pause.current.focused = true }}
|
||||
onBlur={event => { if (!event.currentTarget.contains(event.relatedTarget)) pause.current.focused = false }}
|
||||
onMouseEnter={() => { pause.current.hovered = true }}
|
||||
onMouseLeave={() => { pause.current.hovered = false }}
|
||||
>
|
||||
<span className="_tipDivider_13r4w_121" aria-hidden="true">·</span>
|
||||
<span key={tip.index} className="_tipText_13r4w_126">{tip.text}</span>
|
||||
<button type="button" className="cb-loading-tip__close" aria-label="关闭提示" onClick={() => { setTipsClosed(true); onCloseTips?.() }}>×</button>
|
||||
</span>}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
export default StreamingFooter
|
||||
|
||||
/** Only observed local phases; no fabricated cloud, network, credit or timeout state. */
|
||||
export function resolveRuntimeStatusText(prepareText: string | null | undefined, progress: RuntimeProgress | undefined, locale: 'en' | 'zh' = 'zh'): string {
|
||||
if (prepareText?.trim()) return prepareText
|
||||
const en = locale === 'en'
|
||||
if (progress?.phase === 'preparing') return progress.detail || (en ? 'Preparing' : '准备中')
|
||||
if (progress?.phase === 'tool') return progress.toolName ? `${en ? 'Running' : '正在运行'} ${progress.toolName}` : (en ? 'Running tool' : '正在运行工具')
|
||||
if (progress?.phase === 'waiting-approval') return en ? 'Waiting for approval' : '等待授权'
|
||||
if (progress?.phase === 'waiting-answer') return en ? 'Waiting for your answer' : '等待回答'
|
||||
if (progress?.phase === 'finishing') return en ? 'Finishing' : '正在收尾'
|
||||
return conversationLabel('message.loading.thinking', {}, en ? 'en' : 'zh-cn')
|
||||
}
|
||||
|
||||
export function isLoadingTipPaused(state: { hovered: boolean; focused: boolean }): boolean {
|
||||
return state.hovered || state.focused
|
||||
}
|
||||
@@ -163,7 +163,7 @@ export function ToolCallRenderer({
|
||||
const kind = getCapturedToolKind(tool.name)
|
||||
const body = resultBody(tool.result)
|
||||
|
||||
if (kind === 'question') return withToolOutput(tool, <AskUserQuestion tool={tool} labels={{
|
||||
if (kind === 'question') return withToolOutput((tool.metaData ?? tool.metadata)?.questionAnswer ? { ...tool, output: undefined } : tool, <AskUserQuestion tool={tool} labels={{
|
||||
waiting: t('tool.question.waitingAnswer'), generating: t('tool.question.generating'),
|
||||
title: t('tool.question.title'), cancelled: t('tool.question.cancelled'), failed: t('tool.question.askError'),
|
||||
skipped: t('tool.question.skipped'), skippedAnswer: t('tool.question.skippedAnswer')
|
||||
|
||||
@@ -258,7 +258,7 @@ function buildSourceSegments(
|
||||
const segments: BuildSegment[] = []
|
||||
let foldable: CompletedFoldUnit[] = []
|
||||
const flush = (): void => {
|
||||
if (foldable.length >= 2 || !turnFinished && foldable.some(unit => unit.part.type === 'tool')) segments.push({ kind: 'foldable', units: foldable })
|
||||
if (foldable.length >= 2) segments.push({ kind: 'foldable', units: foldable })
|
||||
else if (foldable.length === 1) segments.push({ kind: 'normal', units: foldable })
|
||||
foldable = []
|
||||
}
|
||||
@@ -305,8 +305,8 @@ function buildSourceSegments(
|
||||
const segment = segments[index]
|
||||
if (segment.kind === 'foldable') {
|
||||
if (segment.summaryGroup) {
|
||||
const containsToolCall = segment.units.some(unit => unit.part.type === 'tool')
|
||||
segment.summaryGroup.shouldFold = bodyTextAfter || turnFinished || containsToolCall
|
||||
segment.summaryGroup.shouldFold = bodyTextAfter || turnFinished
|
||||
segment.summaryGroup.isRunning = !turnFinished && !bodyTextAfter
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -338,3 +338,11 @@
|
||||
.cb-plan-task--clickable { cursor: pointer; }
|
||||
.cb-plan-task--clickable:hover { background: color-mix(in srgb, var(--conversation-text) 4%, transparent); }
|
||||
.cb-plan-task--clickable:focus-visible { outline: 2px solid var(--conversation-accent); outline-offset: 2px; }
|
||||
|
||||
/* 10/21 alignment plan: answered QuestionView typography, not a tool summary. */
|
||||
.ask-user-question--completed { background: var(--cb-content-background, var(--cb-gray-color)); border-radius: var(--yc-radius-2xl); padding: var(--yc-space-3) var(--yc-space-4); }
|
||||
.ask-user-question__content { max-height: 22.5rem; overflow-y: auto; padding: var(--yc-space-3) 0 0; }
|
||||
.question-answer-transcript { gap: var(--yc-space-4); }
|
||||
.question-answer-transcript__question { color: var(--cb-text-tertiary, var(--cb-text-secondary)); font-size: var(--yc-font-size-5); font-weight: 400; line-height: 1.75; }
|
||||
.question-answer-transcript__answer { color: var(--cb-text-primary); font-size: var(--yc-font-size-5); font-weight: 400; line-height: 1.75; }
|
||||
.question-history-unpositioned { margin-block: var(--yc-space-3); }
|
||||
@@ -0,0 +1,16 @@
|
||||
/* Host permission card: no credit/risk claim is inferred from an approval. */
|
||||
.conversation-intercept-card{box-sizing:border-box;width:100%;border:0.0625rem solid var(--cb-question-floating-card-border);background:var(--cb-question-floating-card-bg);border-radius:1.5rem;overflow:hidden}
|
||||
.conversation-intercept-card__header{padding:1.25rem 1.5rem 0.75rem}
|
||||
.conversation-intercept-card__title-row{display:flex;align-items:start;justify-content:space-between;gap:0.75rem}
|
||||
.conversation-intercept-card__title{font-size:1rem;font-weight:500;line-height:1.5625rem;color:var(--wb-color-text-primary)}
|
||||
.conversation-intercept-card__description,.conversation-intercept-card__rule{font-size:0.875rem;line-height:1.375rem;color:var(--wb-color-text-tertiary);overflow-wrap:anywhere;margin:0.5rem 0 0;white-space:pre-wrap}
|
||||
.conversation-intercept-card__rule-action{flex:none;border:0;background:none;color:var(--wb-color-text-secondary);font:inherit;font-size:0.75rem;cursor:pointer}
|
||||
.conversation-intercept-card__content{padding:0 0.75rem 1rem}
|
||||
.conversation-intercept-card__option{display:flex;align-items:center;gap:0.5rem;width:100%;min-height:2.5rem;padding:0.5rem;border:0;border-radius:0.75rem;background:none;color:var(--cb-question-floating-text-primary);text-align:left;cursor:pointer;font:inherit}
|
||||
.conversation-intercept-card__option:hover:not(:disabled),.conversation-intercept-card__option:focus-visible{background:var(--cb-question-floating-hover-bg)}
|
||||
.conversation-intercept-card__option:disabled{opacity:.45;cursor:not-allowed}
|
||||
.conversation-intercept-card__option-num{display:flex;align-items:center;justify-content:center;flex:none;width:1.5rem;height:1.5rem;border-radius:0.5rem;background:var(--cb-question-floating-number-bg);font-size:0.875rem}
|
||||
.conversation-intercept-card__option-text{flex:1;font-size:0.875rem;line-height:1.375rem}
|
||||
.conversation-intercept-card__option-arrow{width:1rem;height:1rem;opacity:0}
|
||||
.conversation-intercept-card__option:hover .conversation-intercept-card__option-arrow,.conversation-intercept-card__option:focus-visible .conversation-intercept-card__option-arrow{opacity:1}
|
||||
.conversation-intercept-card__error,.conversation-intercept-card__working,.conversation-intercept-card__decision{padding:0.75rem 1.5rem;font-size:0.8125rem;line-height:1.25rem;color:var(--wb-color-text-secondary)}
|
||||
@@ -0,0 +1,30 @@
|
||||
import type { ChatMessage, ToolCallRecord } from '@shared/types'
|
||||
import type { QuestionAnswer } from '@shared/conversation'
|
||||
import { getMessageParts } from '@shared/message-parts'
|
||||
|
||||
/** Use the recorded tool position; never guess a timestamp/order for old unlinked answers. */
|
||||
export function projectQuestionAnswers(message: ChatMessage): {
|
||||
tools: ToolCallRecord[]
|
||||
positionedAnswers: Map<string, QuestionAnswer>
|
||||
unpositioned: Array<{ id: string; answer: QuestionAnswer }>
|
||||
} {
|
||||
const tools = (message.tools ?? []).map(tool => ({ ...tool }))
|
||||
const positions = new Set(getMessageParts(message).flatMap(part => part.type === 'tool' ? [part.toolId] : []))
|
||||
const positionedAnswers = new Map<string, QuestionAnswer>()
|
||||
const unpositioned: Array<{ id: string; answer: QuestionAnswer }> = []
|
||||
const seen = new Set<string>()
|
||||
for (const interaction of [...(message.interactions ?? [])].reverse()) {
|
||||
if (interaction.kind !== 'question' || !interaction.answer) continue
|
||||
const key = interaction.toolCallId || interaction.id
|
||||
if (seen.has(key)) continue
|
||||
seen.add(key)
|
||||
const tool = tools.find(item => item.id === interaction.toolCallId)
|
||||
if (positions.has(interaction.toolCallId)) {
|
||||
positionedAnswers.set(interaction.toolCallId, interaction.answer)
|
||||
if (tool) tool.metadata = { ...tool.metadata, questionAnswer: interaction.answer, questionSkipped: interaction.skipped ?? false }
|
||||
} else {
|
||||
unpositioned.unshift({ id: interaction.id, answer: interaction.answer })
|
||||
}
|
||||
}
|
||||
return { tools, positionedAnswers, unpositioned }
|
||||
}
|
||||
@@ -3,3 +3,7 @@
|
||||
* Runtime CSS identities: src-CBaV_A3S.css=2090ed21045c8f8b0fc388d76424c8751bf18fcd065cbbd5d0ef485163a2e36c (mismatch); contexts-BkjwYSZT.css=50cbd673f7f35fc7f966bdddc6c9f5677fdfe944cb904888d214619fca259380 (match)
|
||||
*/
|
||||
._loadingWithCredit_ttf8p_463{align-items:center;width:min(100%,760px);margin-bottom:12px;display:flex;overflow:hidden}._loadingWithCredit_ttf8p_463>:first-child{flex:none;width:auto;min-width:max-content}._loadingWithCredit_ttf8p_463 .cb-credit-usage-text{white-space:nowrap;font-variant-numeric:tabular-nums;flex:none;justify-content:flex-end;min-width:96px}._loadingWithCredit_ttf8p_463 .cb-loading-tip--trailing{flex:auto;min-width:0;max-width:100%;overflow:hidden}._loadingMessage_13r4w_60{box-sizing:border-box;width:100%;padding:0 var(--cb-assistant-inline-padding,12px);contain:layout style;align-items:flex-start;line-height:1.75;display:flex}._loadingMessage_13r4w_60 ._loadingContent_13r4w_69{width:100%;min-width:0;color:var(--cb-text-disabled,#0000004d);flex:auto;gap:6px;font-family:PingFang SC,-apple-system,BlinkMacSystemFont,Helvetica Neue,sans-serif;font-size:15px;font-weight:400;line-height:1.75;display:flex}._loadingMessage_13r4w_60 ._loadingContent_13r4w_69 ._loadingText_13r4w_81{white-space:nowrap;width:100%;min-width:0;max-width:100%;color:inherit;font-size:inherit;line-height:inherit;align-items:center;gap:0;display:flex;overflow:hidden}._loadingMessage_13r4w_60 ._loadingContent_13r4w_69 ._mainText_13r4w_94{white-space:nowrap;background:linear-gradient(110deg, color-mix(in srgb, var(--cb-text-primary,#d2d3e0) 26%, transparent) 0%, color-mix(in srgb, var(--cb-text-primary,#d2d3e0) 26%, transparent) 35%, color-mix(in srgb, var(--cb-text-primary,#d2d3e0) 55%, transparent) 50%, color-mix(in srgb, var(--cb-text-primary,#d2d3e0) 26%, transparent) 65%, color-mix(in srgb, var(--cb-text-primary,#d2d3e0) 26%, transparent) 100%);-webkit-text-fill-color:transparent;color:#0000;background-size:200% 100%;background-clip:text;flex:none;width:fit-content;animation:2.2s linear infinite _cb-shining-text-sweep_13r4w_1;display:inline-block;position:relative}._loadingMessage_13r4w_60 ._loadingContent_13r4w_69 ._tipPhrase_13r4w_108{width:auto;max-width:none;color:inherit;font-size:inherit;line-height:inherit;flex:auto;align-items:center;gap:8px;min-width:0;margin-left:8px;display:inline-flex}._loadingMessage_13r4w_60 ._loadingContent_13r4w_69 ._tipDivider_13r4w_121{color:var(--cb-border,#d0d0d0);user-select:none;flex-shrink:0}._loadingMessage_13r4w_60 ._loadingContent_13r4w_69 ._tipText_13r4w_126{text-overflow:ellipsis;white-space:nowrap;min-width:0;color:var(--cb-text-disabled,#ffffff4d);opacity:0;flex:auto;font-size:15px;font-style:normal;line-height:22px;text-decoration-line:none;animation:.32s ease-out forwards _cb-tip-fade-in_13r4w_1;overflow:hidden}._loadingMessage_13r4w_60 ._loadingContent_13r4w_69 ._tipDismissButton_13r4w_140{appearance:none;color:var(--cb-text-secondary,#0000008c);font:inherit;line-height:inherit;white-space:nowrap;cursor:pointer;opacity:0;pointer-events:none;background:0 0;border:0;flex:none;margin:0 0 0 auto;padding:0;transition:opacity .16s ease-out,color .16s ease-out}._loadingMessage_13r4w_60 ._loadingContent_13r4w_69 ._tipDismissButton_13r4w_140:hover{color:var(--cb-text-primary,#000000e0)}._loadingMessage_13r4w_60 ._loadingContent_13r4w_69 ._tipPhrase_13r4w_108:hover ._tipDismissButton_13r4w_140,._loadingMessage_13r4w_60 ._loadingContent_13r4w_69 ._tipPhrase_13r4w_108:focus-within ._tipDismissButton_13r4w_140{opacity:1;pointer-events:auto}._tipPhrase_13r4w_108{color:var(--cb-text-disabled,#0000004d);flex:auto;align-items:center;gap:8px;min-width:0;margin-left:8px;font-family:PingFang SC,-apple-system,BlinkMacSystemFont,Helvetica Neue,sans-serif;font-size:15px;font-weight:400;line-height:1.75;display:inline-flex}._trailingTipPhrase_13r4w_179{flex:0 auto;margin-left:0}._trailingTipPhrase_13r4w_179 ._tipText_13r4w_126{flex:0 auto}._trailingTipPhrase_13r4w_179 ._tipDismissButton_13r4w_140{margin-left:8px}._tipDivider_13r4w_121{color:var(--cb-border,#d0d0d0);user-select:none;flex-shrink:0}._tipText_13r4w_126{text-overflow:ellipsis;white-space:nowrap;min-width:0;color:var(--cb-text-disabled,#ffffff4d);opacity:0;flex:auto;font-size:15px;font-style:normal;line-height:22px;text-decoration-line:none;animation:.32s ease-out forwards _cb-tip-fade-in_13r4w_1;overflow:hidden}._tipDismissButton_13r4w_140{appearance:none;color:var(--cb-text-secondary,#0000008c);font:inherit;line-height:inherit;white-space:nowrap;cursor:pointer;opacity:0;pointer-events:none;background:0 0;border:0;flex:none;margin:0 0 0 auto;padding:0;transition:opacity .16s ease-out,color .16s ease-out}._tipDismissButton_13r4w_140:hover{color:var(--cb-text-primary,#000000e0)}._tipPhrase_13r4w_108:hover ._tipDismissButton_13r4w_140,._tipPhrase_13r4w_108:focus-within ._tipDismissButton_13r4w_140{opacity:1;pointer-events:auto}@keyframes _cb-shining-text-sweep_13r4w_1{0%{background-position:200% 0}to{background-position:-200% 0}}@keyframes _cb-tip-fade-in_13r4w_1{0%{opacity:0}to{opacity:1}}.conversation-streaming-footer>.assistant-message{flex:none;width:auto;min-width:max-content}.conversation-streaming-footer>.cb-loading-tip--trailingLine truncated
|
||||
|
||||
/* Accessible dismissal belongs to the tip only, never the running status. */
|
||||
.cb-loading-tip__close { background: transparent; border: 0; color: inherit; cursor: pointer; padding: 0 4px; font: inherit; }
|
||||
.cb-loading-tip__close:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; }
|
||||
@@ -63,6 +63,7 @@ export type QuestionOption =
|
||||
id?: string
|
||||
question?: string
|
||||
option?: string
|
||||
label?: string
|
||||
description?: string
|
||||
hint?: string
|
||||
}
|
||||
|
||||
@@ -58,6 +58,19 @@ export interface QuestionAnswer {
|
||||
questions: Array<{ id: string; question: string; answers: string[] }>
|
||||
}
|
||||
export interface PermissionOption { optionId: string; name: string; kind: string }
|
||||
export interface ApprovalDetails {
|
||||
requestKind: 'permission' | 'rule-intercept'
|
||||
toolName: string
|
||||
command?: string
|
||||
target?: string
|
||||
reason?: string
|
||||
/** Only present when the upstream supplies actual matched-rule identity/details. */
|
||||
rule?: { id?: string; description: string }
|
||||
allowScope: 'once' | 'session'
|
||||
defaultToNo?: boolean
|
||||
suggestedRules?: string[]
|
||||
sessionUnavailableReason?: string
|
||||
}
|
||||
export type InteractionStatus = 'pending' | 'responding' | 'answered' | 'rejected' | 'cancelled' | 'error'
|
||||
interface InteractionBase {
|
||||
id: string
|
||||
@@ -69,7 +82,7 @@ interface InteractionBase {
|
||||
}
|
||||
export type ChatInteraction =
|
||||
| (InteractionBase & { kind: 'question'; questions: QuestionItem[]; answer?: QuestionAnswer; skipped?: boolean })
|
||||
| (InteractionBase & { kind: 'approval'; requestId: string; options: PermissionOption[]; selectedOptionId?: string; scope?: string })
|
||||
| (InteractionBase & { kind: 'approval'; requestId: string; options: PermissionOption[]; selectedOptionId?: string; scope?: string; details?: ApprovalDetails })
|
||||
export type InteractionResponse =
|
||||
| { kind: 'question'; answer: QuestionAnswer; skipped?: boolean }
|
||||
| { kind: 'approval'; optionId: string }
|
||||
|
||||
@@ -13,6 +13,11 @@ export function getMessageParts(message: ChatMessage): MessagePart[] {
|
||||
/** 主进程持久化和渲染进程使用同一事件归约,避免重载前后内容顺序不同。 */
|
||||
export function applyAssistantEvent(message: ChatMessage, event: ChatEvent): ChatMessage {
|
||||
switch (event.type) {
|
||||
case 'progress': {
|
||||
if (message.requestId && message.requestId !== event.runId) return message
|
||||
if (message.status && message.status !== 'streaming') return message
|
||||
return { ...message, runtimeProgress: event.progress ?? undefined }
|
||||
}
|
||||
case 'usage': {
|
||||
const contextUsage = normalizeContextUsageSnapshot({ usage: event.usage, contextWindow: event.contextWindow,
|
||||
source:event.source,sampledAt:event.sampledAt,requestId:event.runId,modelId:event.modelId })
|
||||
@@ -83,7 +88,8 @@ export function applyAssistantEvent(message: ChatMessage, event: ChatEvent): Cha
|
||||
parts.push({ type: 'tool', toolId: tool.id })
|
||||
}
|
||||
}
|
||||
return { ...message, content: event.content, tools: event.tools, parts, status: 'done', ...terminalDuration(event) }
|
||||
const interactions = message.interactions?.map(item => ['pending', 'responding', 'error'].includes(item.status) ? { ...item, status: 'cancelled' as const } : item)
|
||||
return { ...message, content: event.content, tools: event.tools, parts, interactions, runtimeProgress: undefined, status: 'done', ...terminalDuration(event) }
|
||||
}
|
||||
case 'cancelled':
|
||||
case 'error': {
|
||||
@@ -91,18 +97,19 @@ export function applyAssistantEvent(message: ChatMessage, event: ChatEvent): Cha
|
||||
const tools = message.tools?.map((tool): ToolCallRecord =>
|
||||
tool.status === 'running' ? { ...tool, status } : tool
|
||||
)
|
||||
const interactions = message.interactions?.map((item) => item.status === 'pending' || item.status === 'responding' ? { ...item, status: 'cancelled' as const } : item)
|
||||
return { ...message, tools, interactions, parts: message.parts, status, error: event.type === 'error' ? event.message : message.error, ...terminalDuration(event) }
|
||||
const interactions = message.interactions?.map((item) => ['pending', 'responding', 'error'].includes(item.status) ? { ...item, status: 'cancelled' as const } : item)
|
||||
return { ...message, tools, interactions, parts: message.parts, runtimeProgress: undefined, status, error: event.type === 'error' ? event.message : message.error, ...terminalDuration(event) }
|
||||
}
|
||||
default:
|
||||
return message
|
||||
}
|
||||
}
|
||||
|
||||
function terminalDuration(event: ChatEvent): Pick<ChatMessage, 'turnDurationMs' | 'turnStartedAt'> {
|
||||
function terminalDuration(event: ChatEvent): Pick<ChatMessage, 'turnDurationMs' | 'turnStartedAt' | 'turnFinishedAt'> {
|
||||
return {
|
||||
...(typeof event.turnDurationMs === 'number' && Number.isFinite(event.turnDurationMs) && event.turnDurationMs >= 0
|
||||
? { turnDurationMs: event.turnDurationMs } : {}),
|
||||
...(typeof event.turnFinishedAt === 'number' && Number.isFinite(event.turnFinishedAt) ? { turnFinishedAt: event.turnFinishedAt } : {}),
|
||||
turnStartedAt: undefined
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import type { CustomModelProfile } from './models'
|
||||
|
||||
/** Resolve only real registry entries; display and the next request share this order. */
|
||||
export function resolveSelectedModel(
|
||||
models: readonly CustomModelProfile[], preferredId?: string | null, defaultId?: string | null
|
||||
): CustomModelProfile | null {
|
||||
return models.find(model => model.id === preferredId)
|
||||
?? models.find(model => model.id === defaultId)
|
||||
?? models[0] ?? null
|
||||
}
|
||||
+14
-1
@@ -34,6 +34,8 @@ export interface ChatMessage {
|
||||
/** Independent local counts of one prepared provider input; never a split of billed usage. */
|
||||
contextBreakdown?: ContextUsageBreakdown
|
||||
interactions?: ChatInteraction[]
|
||||
/** Live, observed runtime state; cleared on every terminal event. */
|
||||
runtimeProgress?: RuntimeProgress
|
||||
requestId?: string
|
||||
/** Last top-level Claude chain entry for this assistant turn, used only as a fork point. */
|
||||
claudeEntryId?: string
|
||||
@@ -45,6 +47,8 @@ export interface ChatMessage {
|
||||
turnStartedAt?: number
|
||||
/** Measured user-send to terminal-event duration, used by the source completed fold. */
|
||||
turnDurationMs?: number
|
||||
/** Actual observed terminal timestamp, absent for legacy history. */
|
||||
turnFinishedAt?: number
|
||||
/** 本回合的终态 */
|
||||
status?: 'streaming' | 'done' | 'error' | 'cancelled'
|
||||
error?: string
|
||||
@@ -215,9 +219,18 @@ export interface MemorySettingsView extends MemoryPreferences {
|
||||
|
||||
export type MemoryPreferencesPatch = Partial<MemoryPreferences>
|
||||
|
||||
/** Only stages observed by this host. No synthetic cloud/network/credit states. */
|
||||
export interface RuntimeProgress {
|
||||
source: 'preparation' | 'agent' | 'interaction'
|
||||
phase: 'preparing' | 'thinking' | 'tool' | 'waiting-approval' | 'waiting-answer' | 'finishing'
|
||||
toolName?: string
|
||||
detail?: string
|
||||
}
|
||||
|
||||
/** 主进程 → 渲染进程的流式事件 */
|
||||
export type ChatEvent = { sessionId?: string; turnDurationMs?: number; sequence?: number } & (
|
||||
export type ChatEvent = { sessionId?: string; turnDurationMs?: number; turnFinishedAt?: number; sequence?: number } & (
|
||||
| { runId: string; type: 'started'; messageId: string }
|
||||
| { runId: string; type: 'progress'; progress: RuntimeProgress | null }
|
||||
| { runId: string; type: 'text'; delta: string }
|
||||
| { runId: string; type: 'part'; part: Exclude<MessagePart, { type: 'text' } | { type: 'tool' }> }
|
||||
| { runId: string; type: 'interaction'; interaction: ChatInteraction }
|
||||
|
||||
Reference in new issue
Block a user