feat: generate and edit conversation images across runtimes
Add model-controlled image invocation and read-only runtime assignments. Route image tools through Main so accepted operations retain their originating conversation when chat runtimes change, with saved references, cancellation, and manual recovery. Keep Node hashing outside browser-shared contracts to restore production builds. Validation includes the production build, typecheck, lint, focused regressions, real Host transport checks, and two successful real image requests. Full UI-driven runtime switching acceptance remains documented as pending. Release note: 开启图片模型的会话调用后,聊天助手可调用图片生成与编辑工具,Runtime 分配自动管理。图片成果保存在原会话,可继续修改,并独立显示生成状态和取消入口。
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { z } from 'zod'
|
||||
import type { ImageToolBinding } from '../agent/image-tool-binding'
|
||||
import { MainImageToolSession } from './main-image-tool-session'
|
||||
import { remoteQuestionResponseSchema, type RemoteQuestionResponse } from '../../shared/remote-question-contracts'
|
||||
import {
|
||||
AGENT_PROTOCOL_LIMITS,
|
||||
@@ -584,38 +586,51 @@ export class ProtocolRemoteRuntimeChannel
|
||||
}
|
||||
|
||||
async preparePrompt(
|
||||
preparation: z.input<typeof remotePromptOperationPreparationSchema>
|
||||
preparation: z.input<typeof remotePromptOperationPreparationSchema>,
|
||||
imageToolBinding?: ImageToolBinding,
|
||||
waitSignal?: AbortSignal
|
||||
): Promise<
|
||||
z.infer<typeof remotePromptOperationAcceptanceSchema>
|
||||
> {
|
||||
const parsed = remotePromptOperationPreparationSchema.parse(
|
||||
preparation
|
||||
)
|
||||
this.#assertPreparationIdentity(parsed)
|
||||
const raw = await this.#request(
|
||||
'runtime/preparePrompt',
|
||||
parsed
|
||||
)
|
||||
const acceptance = parseRemoteResult(
|
||||
remotePromptOperationAcceptanceSchema,
|
||||
raw,
|
||||
'Remote Runtime prompt acceptance is invalid'
|
||||
)
|
||||
this.#imageTool?.close()
|
||||
this.#imageTool = undefined
|
||||
const description = preparation.workMode === 'execute' ? await imageToolBinding?.describe() : undefined
|
||||
waitSignal?.throwIfAborted()
|
||||
let imageTool: z.infer<typeof remotePromptOperationPreparationSchema>['imageTool']
|
||||
if (description && imageToolBinding) {
|
||||
this.#assertCurrent()
|
||||
const binary = this.#state.client.allocateBinaryChannel?.({ kind: 'blob' })
|
||||
if (!binary) throw new Error('Remote image tool transport is unavailable')
|
||||
imageTool = { channelId: binary.channelId, channelEpoch: binary.channelEpoch, description }
|
||||
this.#imageTool = new MainImageToolSession(binary, imageToolBinding, waitSignal)
|
||||
}
|
||||
try {
|
||||
assertRemotePromptAcceptanceMatchesPreparation(
|
||||
parsed,
|
||||
acceptance
|
||||
const parsed = remotePromptOperationPreparationSchema.parse({ ...preparation, imageTool })
|
||||
this.#assertPreparationIdentity(parsed)
|
||||
const raw = await this.#request('runtime/preparePrompt', parsed)
|
||||
const acceptance = parseRemoteResult(
|
||||
remotePromptOperationAcceptanceSchema, raw,
|
||||
'Remote Runtime prompt acceptance is invalid'
|
||||
)
|
||||
} catch {
|
||||
throw new ProtocolRemoteRuntimeChannelError(
|
||||
'Remote Runtime prompt acceptance identity is invalid',
|
||||
'binding-mismatch'
|
||||
)
|
||||
try {
|
||||
assertRemotePromptAcceptanceMatchesPreparation(parsed, acceptance)
|
||||
if (Boolean(imageTool) !== Boolean(acceptance.imageToolUrl)) throw new Error('Image tool registration mismatch')
|
||||
} catch {
|
||||
throw new ProtocolRemoteRuntimeChannelError(
|
||||
'Remote Runtime prompt acceptance identity is invalid', 'binding-mismatch'
|
||||
)
|
||||
}
|
||||
this.#assertCurrent()
|
||||
return acceptance
|
||||
} catch (error) {
|
||||
this.#imageTool?.close()
|
||||
this.#imageTool = undefined
|
||||
throw error
|
||||
}
|
||||
this.#assertCurrent()
|
||||
return acceptance
|
||||
}
|
||||
|
||||
#imageTool?: MainImageToolSession
|
||||
|
||||
async startOwnedPrompt(
|
||||
request: z.input<typeof remoteOwnedPromptStartRequestSchema>
|
||||
): Promise<z.infer<typeof remoteOwnedPromptStartResultSchema>> {
|
||||
@@ -754,7 +769,8 @@ export class ProtocolRemoteRuntimeChannel
|
||||
'protocol'
|
||||
)
|
||||
}
|
||||
this.clearRecoveryBoundary()
|
||||
this.#removeRecoveryAbort?.()
|
||||
this.#removeRecoveryAbort = undefined
|
||||
this.#recoveryDeadlineAt = deadline
|
||||
this.#recoverySignal = signal
|
||||
const abort = (): void => {
|
||||
@@ -774,6 +790,8 @@ export class ProtocolRemoteRuntimeChannel
|
||||
}
|
||||
|
||||
clearRecoveryBoundary(): void {
|
||||
this.#imageTool?.close()
|
||||
this.#imageTool = undefined
|
||||
this.#removeRecoveryAbort?.()
|
||||
this.#removeRecoveryAbort = undefined
|
||||
this.#recoverySignal = undefined
|
||||
@@ -1628,6 +1646,7 @@ export class ProtocolRemoteRuntimeChannel
|
||||
this.#state.capabilityGeneration
|
||||
)
|
||||
this.#closing = true
|
||||
this.#imageTool?.close()
|
||||
this.clearRecoveryBoundary()
|
||||
this.#lifetime.abort(
|
||||
new DOMException('Remote Runtime channel closed', 'AbortError')
|
||||
|
||||
Reference in New Issue
Block a user