Files
goodbuddy/src/shared/runtime-selection-contracts.ts
T
mesalogo c037d7fb20 fix: complete runtime and workspace workflows
Restore Ask workspace reads and output paging without granting write access,
and let programming Subagents reuse their parent's browser page. Allocate
browser resources only when used, keep Agent pages aligned with workbar tabs,
recover released bindings, and remove unused automatic screenshot work.

Preserve inherited Runtime selections during remote usage persistence and
manual compression. Keep viable channel model fallbacks, display the actual
scheduled-task mode and Harness model source, and avoid rediscovery delays
when calling healthy MCP tools. Fix directory/history refresh, case-only
renames, rename-aware commit diffs, and FastGPT probe success reporting.

Add regression coverage and align feature documentation. The Agent-side
connection timeout fix requires deploying an updated Agent; existing Ask
read-only and Execute authorization boundaries remain unchanged.

Final validation: 3839 tests passed, 66 skipped; typecheck, lint, development
build, and diff checks passed. Isolated Electron UI/MCP workflows passed with
a loopback model fixture. Current-source Linux x64 Host checks passed with
three successful real model requests. Chromium loadURL rejection was not
reproduced in the UI; released-binding recovery has automated coverage.

Release note: 修复 Ask 读取项目文件、浏览器页面显示与恢复、文件管理及模型配置跟随中的不一致,并减少无用截图和 MCP 等待。定时任务显示实际工作模式,远程会话可正常保存继承 Runtime 的用量;远端连接超时修复需更新 Agent 后生效。
2026-09-10 12:48:30 +08:00

244 lines
7.4 KiB
TypeScript

import { z } from 'zod'
import { isDeepSeekHarnessModelProfile } from './deepseek-harness-compatibility'
const runtimeSelectionProfileIdSchema = z.string().uuid()
export const agentRuntimeSelectionSchema = z.discriminatedUnion(
'provider',
[
z.object({ provider: z.literal('auto') }).strict(),
z
.object({
provider: z.literal('model'),
profileId: runtimeSelectionProfileIdSchema.optional()
})
.strict(),
z
.object({
provider: z.literal('opencode'),
profileId: runtimeSelectionProfileIdSchema.optional()
})
.strict(),
z
.object({
provider: z.literal('continue'),
profileId: runtimeSelectionProfileIdSchema.optional()
})
.strict(),
z
.object({
provider: z.literal('deepseek-harness'),
profileId: runtimeSelectionProfileIdSchema.optional()
})
.strict()
]
)
export type AgentRuntimeSelection = z.infer<
typeof agentRuntimeSelectionSchema
>
export type RuntimeSelectionRepairSettings = {
modelProfiles: ReadonlyArray<{
id: string
baseUrl?: string
protocol?: string
authentication?: 'api-key' | 'none'
apiKeyConfigured?: boolean
}>
defaultModelProfileId: string
opencodeModelSource:
| { kind: 'default' }
| { kind: 'platform' }
| { kind: 'profile'; profileId: string }
continueModelSource:
| { kind: 'default' }
| { kind: 'platform' }
| { kind: 'profile'; profileId: string }
deepseekHarnessModelSource?:
| { kind: 'default' }
| { kind: 'platform' }
| { kind: 'profile'; profileId: string }
}
type RuntimeModelSource =
| { kind: 'default' }
| { kind: 'platform' }
| { kind: 'profile'; profileId: string }
export type RuntimeSelectionDefaultSettings = {
provider: AgentRuntimeSelection['provider']
defaultModelProfileId: string
modelProfiles?: RuntimeSelectionRepairSettings['modelProfiles']
opencodeBaseUrl: string
opencodeEmbedded: boolean
opencodeModelSource?: RuntimeModelSource
continueModelSource?: RuntimeModelSource
deepseekHarnessModelSource?: RuntimeModelSource
opencodeModelProfile?: { id: string }
continueModelProfile?: { id: string }
deepseekHarnessModelProfile?: { id: string }
}
type ChannelModelProfile = RuntimeSelectionRepairSettings['modelProfiles'][number]
export function getRuntimeSelectionForProvider(
provider: Exclude<AgentRuntimeSelection['provider'], 'auto'>,
_settings: RuntimeSelectionDefaultSettings
): AgentRuntimeSelection {
void _settings
return { provider }
}
export function getDefaultRuntimeSelection(
settings: RuntimeSelectionDefaultSettings
): AgentRuntimeSelection {
const provider = settings.provider
if (provider !== 'auto') {
return getRuntimeSelectionForProvider(provider, settings)
}
return settings.opencodeBaseUrl || settings.opencodeEmbedded
? getRuntimeSelectionForProvider('opencode', settings)
: getRuntimeSelectionForProvider('model', settings)
}
// Resolve only for presentation or request diagnostics, never for persisted defaults.
export function getRuntimeSelectionProfileId(
selection: AgentRuntimeSelection,
settings: RuntimeSelectionDefaultSettings
): string | undefined {
if (selection.provider === 'auto') return undefined
if (selection.profileId) return selection.profileId
if (selection.provider === 'model') return settings.defaultModelProfileId
const source = selection.provider === 'opencode' ? settings.opencodeModelSource
: selection.provider === 'continue' ? settings.continueModelSource : settings.deepseekHarnessModelSource
if (source?.kind === 'profile') return source.profileId
if (source?.kind !== 'default') return undefined
const profiles = settings.modelProfiles ?? []
const isCompatible = (profile: ChannelModelProfile): boolean =>
selection.provider === 'deepseek-harness'
? profile.protocol === 'openai-chat-completions' &&
profile.authentication === 'api-key' &&
Boolean(profile.baseUrl) &&
isDeepSeekHarnessModelProfile({
baseUrl: profile.baseUrl!,
protocol: profile.protocol,
authentication: profile.authentication
})
: Boolean(profile.protocol) &&
profile.protocol !== 'openai-images-generations'
return profiles.find(
(profile) =>
profile.id === settings.defaultModelProfileId && isCompatible(profile)
)?.id ?? profiles.find(isCompatible)?.id
}
export function isChannelModelProfileUsable(
profile: ChannelModelProfile
): boolean {
return (
profile.protocol !== 'openai-images-generations' &&
!(
profile.authentication === 'api-key' &&
profile.apiKeyConfigured === false
)
)
}
function isDeepSeekHarnessRepairProfileUsable(
profile: ChannelModelProfile
): boolean {
if (
profile.protocol !== 'openai-chat-completions' ||
profile.authentication !== 'api-key' ||
profile.apiKeyConfigured === false ||
!profile.baseUrl
) {
return false
}
return isDeepSeekHarnessModelProfile({
baseUrl: profile.baseUrl,
protocol: profile.protocol,
authentication: profile.authentication
})
}
export function repairChannelRuntimeSelection(
selection: AgentRuntimeSelection,
settings: RuntimeSelectionRepairSettings
): AgentRuntimeSelection {
const defaultDirectProfile =
settings.modelProfiles.find(
(profile) =>
profile.id === settings.defaultModelProfileId &&
isChannelModelProfileUsable(profile)
) ??
settings.modelProfiles.find(isChannelModelProfileUsable)
const defaultDirectSelection: AgentRuntimeSelection =
defaultDirectProfile &&
defaultDirectProfile.id !== settings.defaultModelProfileId
? { provider: 'model', profileId: defaultDirectProfile.id }
: { provider: 'model' }
if (selection.provider === 'auto') {
return defaultDirectSelection
}
if (
selection.provider === 'opencode' ||
selection.provider === 'continue'
) {
return repairAgentRuntimeSelection(selection, settings)
}
if (selection.provider === 'deepseek-harness') {
const repaired = repairAgentRuntimeSelection(selection, settings)
if (
repaired.provider === 'deepseek-harness' &&
repaired.profileId
) {
const profile = settings.modelProfiles.find(
(candidate) => candidate.id === repaired.profileId
)
if (profile && isDeepSeekHarnessRepairProfileUsable(profile)) {
return repaired
}
}
return { provider: 'deepseek-harness' }
}
const repaired = repairAgentRuntimeSelection(selection, settings)
if (repaired.provider !== 'model') {
return repaired
}
const profile = settings.modelProfiles.find(
(candidate) => candidate.id === repaired.profileId
)
return profile && isChannelModelProfileUsable(profile)
? repaired
: defaultDirectSelection
}
export function repairAgentRuntimeSelection(
selection: AgentRuntimeSelection,
settings: RuntimeSelectionRepairSettings
): AgentRuntimeSelection {
if (
!('profileId' in selection) ||
!selection.profileId ||
settings.modelProfiles.some(
(profile) => profile.id === selection.profileId
)
) {
return selection
}
if (selection.provider === 'model') {
return { provider: 'model' }
}
return { provider: selection.provider }
}
export function agentRuntimeSelectionKey(
selection: AgentRuntimeSelection
): string {
return `${selection.provider}:${'profileId' in selection
? selection.profileId ?? 'default'
: 'default'}`
}