feat(pi): display inline tool-result images (#2060)

Build on tiann/hapi#1831, retain bounded image reads, and support current
Pi nested and failed tool results through the existing generated-image path.
Keep conversion synchronous to preserve transport ordering without a new
publication queue.

Co-authored-by: Debin <luodeb@outlook.com>
This commit is contained in:
SSU-WEI HUANG
2026-10-26 10:01:55 +08:00
committed by GitHub
co-authored by Debin
parent 5a626616a2
commit d3f7c107d5
8 changed files with 594 additions and 32 deletions
@@ -170,9 +170,9 @@ describe('generatedImages', () => {
clearGeneratedImages()
})
it('registers images from ACP base64 image blocks after MIME sniffing', async () => {
it('registers images from ACP base64 image blocks synchronously after MIME sniffing', () => {
const pngHeader = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x00])
const image = await registerGeneratedImageFromAcpBlock({
const image = registerGeneratedImageFromAcpBlock({
type: 'image',
mimeType: 'image/png',
data: pngHeader.toString('base64')
@@ -186,11 +186,11 @@ describe('generatedImages', () => {
it('rejects oversized base64 before allocating a decoded buffer', async () => {
const oversized = 'A'.repeat(MAX_GENERATED_IMAGE_BASE64_CHARS + 1)
expect(decodeGeneratedImageBase64(oversized)).toBeNull()
await expect(registerGeneratedImageFromAcpBlock({
expect(registerGeneratedImageFromAcpBlock({
type: 'image',
mimeType: 'image/png',
data: oversized,
})).resolves.toBeNull()
})).toBeNull()
})
it('ignores URI-only ACP image blocks that would read local disk without a permission prompt', async () => {
@@ -199,15 +199,15 @@ describe('generatedImages', () => {
const path = join(dir, 'secret.png')
writeFileSync(path, Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00]))
await expect(registerGeneratedImageFromAcpBlock({
expect(registerGeneratedImageFromAcpBlock({
type: 'image',
uri: `file://${path}`
})).resolves.toBeNull()
})).toBeNull()
await expect(registerGeneratedImageFromAcpBlock({
expect(registerGeneratedImageFromAcpBlock({
type: 'image',
url: path
})).resolves.toBeNull()
})).toBeNull()
})
it('registers images from local file paths in ACP uri blocks', async () => {
@@ -250,7 +250,7 @@ describe('generatedImages', () => {
expect(safeAcpFileName('file:///tmp/photos/icon.png')).toBe('icon.png')
const pngHeader = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x00])
const image = await registerGeneratedImageFromAcpBlock({
const image = registerGeneratedImageFromAcpBlock({
type: 'image',
mimeType: 'image/png',
data: pngHeader.toString('base64'),
+1 -1
View File
@@ -415,7 +415,7 @@ export function safeAcpFileName(uri: string | undefined | null): string | null {
return null
}
export async function registerGeneratedImageFromAcpBlock(block: unknown): Promise<GeneratedImageMetadata | null> {
export function registerGeneratedImageFromAcpBlock(block: unknown): GeneratedImageMetadata | null {
if (!isObject(block) || block.type !== 'image') {
return null
}