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:
@@ -0,0 +1,56 @@
|
||||
import type { ImageToolBinding } from '../agent/image-tool-binding'
|
||||
import type { RuntimeProtocolBinaryChannel } from './protocol-remote-runtime-channel'
|
||||
import {
|
||||
decodeRemoteImageToolMessage, encodeRemoteImageToolMessage,
|
||||
remoteImageToolCallSchema, remoteImageToolReplySchema
|
||||
} from '../../shared/remote-image-tool-contracts'
|
||||
|
||||
export class MainImageToolSession {
|
||||
private readonly wait = new AbortController()
|
||||
private readonly unsubscribe: () => void
|
||||
private readonly removeAbort: () => void
|
||||
|
||||
constructor(
|
||||
private readonly channel: RuntimeProtocolBinaryChannel,
|
||||
private readonly binding: ImageToolBinding,
|
||||
signal?: AbortSignal
|
||||
) {
|
||||
this.unsubscribe = channel.onClose(() => this.close())
|
||||
const abort = (): void => this.close()
|
||||
signal?.addEventListener('abort', abort, { once: true })
|
||||
this.removeAbort = () => signal?.removeEventListener('abort', abort)
|
||||
if (signal?.aborted) this.close()
|
||||
void this.receive().catch(() => this.close())
|
||||
}
|
||||
|
||||
close(): void {
|
||||
if (this.wait.signal.aborted) return
|
||||
// This aborts binding.call's wait, never the Main-owned image operation.
|
||||
this.wait.abort()
|
||||
this.removeAbort()
|
||||
this.unsubscribe()
|
||||
this.channel.close()
|
||||
}
|
||||
|
||||
private async receive(): Promise<void> {
|
||||
while (!this.wait.signal.aborted) {
|
||||
const frame = await this.channel.receive(this.wait.signal)
|
||||
const call = remoteImageToolCallSchema.parse(decodeRemoteImageToolMessage(frame.payload))
|
||||
await frame.consume()
|
||||
void this.dispatch(call).catch(() => this.close())
|
||||
}
|
||||
}
|
||||
|
||||
private async dispatch(call: ReturnType<typeof remoteImageToolCallSchema.parse>): Promise<void> {
|
||||
let reply: ReturnType<typeof remoteImageToolReplySchema.parse>
|
||||
try {
|
||||
this.wait.signal.throwIfAborted()
|
||||
if (this.binding.context.workMode !== 'execute') throw new Error('Image tools are unavailable in Ask mode')
|
||||
const result = await this.binding.call(call.input, call.callId, this.wait.signal)
|
||||
reply = remoteImageToolReplySchema.parse({ callId: call.callId, result })
|
||||
} catch (error) {
|
||||
reply = { callId: call.callId, error: (error instanceof Error ? error.message : 'Image tool failed').slice(0, 4_000) }
|
||||
}
|
||||
if (!this.wait.signal.aborted) await this.channel.send(encodeRemoteImageToolMessage(reply), this.wait.signal)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user