mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-28 05:57:13 +00:00
feat(cli): add image display MCP tool (#700)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import React from 'react';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { lstat, readFile } from 'node:fs/promises';
|
||||
|
||||
import { CodexAppServerClient } from './codexAppServerClient';
|
||||
import { CodexPermissionHandler } from './utils/permissionHandler';
|
||||
@@ -13,7 +14,7 @@ import type { CodexSession } from './session';
|
||||
import type { EnhancedMode } from './loop';
|
||||
import { hasCodexCliOverrides } from './utils/codexCliOverrides';
|
||||
import { AppServerEventConverter } from './utils/appServerEventConverter';
|
||||
import { registerGeneratedImage } from '@/modules/common/generatedImages';
|
||||
import { detectImageMimeType, registerGeneratedImage } from '@/modules/common/generatedImages';
|
||||
import { registerAppServerPermissionHandlers } from './utils/appServerPermissionAdapter';
|
||||
import { buildThreadStartParams, buildTurnStartParams } from './utils/appServerConfig';
|
||||
import type { ThreadGoal, ThreadGoalStatus } from './appServerTypes';
|
||||
@@ -25,6 +26,35 @@ import {
|
||||
type RemoteLauncherExitReason
|
||||
} from '@/modules/common/remote/RemoteLauncherBase';
|
||||
|
||||
|
||||
async function registerGeneratedImageFromPath(args: { id: string; path: string; fileName?: string | null }): Promise<ReturnType<typeof registerGeneratedImage> | null> {
|
||||
try {
|
||||
const info = await lstat(args.path);
|
||||
if (!info.isFile()) {
|
||||
throw new Error('Path is not a regular file');
|
||||
}
|
||||
const maxImageBytes = 25 * 1024 * 1024;
|
||||
if (info.size > maxImageBytes) {
|
||||
throw new Error('Image is too large to display inline');
|
||||
}
|
||||
const bytes = await readFile(args.path);
|
||||
const mimeType = detectImageMimeType(bytes);
|
||||
if (!mimeType) {
|
||||
throw new Error('Unsupported image content');
|
||||
}
|
||||
return registerGeneratedImage({
|
||||
id: args.id,
|
||||
path: args.path,
|
||||
fileName: args.fileName,
|
||||
mimeType,
|
||||
bytes
|
||||
});
|
||||
} catch (error) {
|
||||
logger.debug('[CodexRemoteLauncher] Failed to register generated image:', error instanceof Error ? error.message : String(error));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
type HappyServer = Awaited<ReturnType<typeof buildHapiMcpBridge>>['server'];
|
||||
type QueuedMessage = { message: string; mode: EnhancedMode; isolate: boolean; hash: string };
|
||||
type ChildAgentRuntime = {
|
||||
@@ -1924,7 +1954,9 @@ class CodexRemoteLauncher extends RemoteLauncherBase {
|
||||
return true;
|
||||
};
|
||||
|
||||
const handleCodexEvent = (msg: Record<string, unknown>) => {
|
||||
let codexEventQueue: Promise<void> | null = null;
|
||||
|
||||
const handleCodexEvent = async (msg: Record<string, unknown>): Promise<void> => {
|
||||
const msgType = asString(msg.type);
|
||||
if (!msgType) return;
|
||||
const eventTurnId = asString(msg.turn_id ?? msg.turnId);
|
||||
@@ -2183,12 +2215,13 @@ class CodexRemoteLauncher extends RemoteLauncherBase {
|
||||
const imageId = randomUUID();
|
||||
const savedPath = asString(msg.saved_path ?? msg.savedPath);
|
||||
if (savedPath) {
|
||||
const image = registerGeneratedImage({
|
||||
const image = await registerGeneratedImageFromPath({
|
||||
id: imageId,
|
||||
path: savedPath,
|
||||
fileName: asString(msg.file_name ?? msg.fileName),
|
||||
mimeType: asString(msg.mime_type ?? msg.mimeType)
|
||||
fileName: asString(msg.file_name ?? msg.fileName)
|
||||
});
|
||||
if (!image) return;
|
||||
|
||||
messageBuffer.addMessage(`Generated image: ${image.fileName}`, 'assistant');
|
||||
session.sendAgentMessage({
|
||||
type: 'generated-image',
|
||||
@@ -2467,7 +2500,25 @@ class CodexRemoteLauncher extends RemoteLauncherBase {
|
||||
const events = appServerEventConverter.handleNotification(method, params);
|
||||
for (const event of events) {
|
||||
const eventRecord = asRecord(event) ?? { type: undefined };
|
||||
handleCodexEvent(eventRecord);
|
||||
const msgType = asString(eventRecord.type);
|
||||
const hasGeneratedImagePath = msgType === 'generated_image' && Boolean(asString(eventRecord.saved_path ?? eventRecord.savedPath));
|
||||
|
||||
if (codexEventQueue || hasGeneratedImagePath) {
|
||||
const previousQueue = codexEventQueue ?? Promise.resolve();
|
||||
const nextQueue = previousQueue
|
||||
.then(() => handleCodexEvent(eventRecord))
|
||||
.catch((error) => logger.debug('[Codex] Failed to handle app-server event:', error instanceof Error ? error.message : String(error)));
|
||||
const queued = nextQueue.finally(() => {
|
||||
if (codexEventQueue === queued) {
|
||||
codexEventQueue = null;
|
||||
}
|
||||
});
|
||||
codexEventQueue = queued;
|
||||
} else {
|
||||
void handleCodexEvent(eventRecord).catch((error) => {
|
||||
logger.debug('[Codex] Failed to handle app-server event:', error instanceof Error ? error.message : String(error));
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* HAPI MCP STDIO Bridge
|
||||
*
|
||||
* Minimal STDIO MCP server exposing a single tool `change_title`.
|
||||
* Minimal STDIO MCP server exposing HAPI tools such as `change_title` and `display_image`.
|
||||
* On invocation it forwards the tool call to an existing HAPI HTTP MCP server
|
||||
* using the StreamableHTTPClientTransport.
|
||||
*
|
||||
@@ -64,7 +64,7 @@ export async function runHappyMcpStdioBridge(argv: string[]): Promise<void> {
|
||||
version: '1.0.0',
|
||||
});
|
||||
|
||||
// Register the single tool and forward to HTTP MCP
|
||||
// Register tools and forward to HTTP MCP
|
||||
const changeTitleInputSchema: z.ZodTypeAny = z.object({
|
||||
title: z.string().describe('The new title for the chat session'),
|
||||
});
|
||||
@@ -93,6 +93,36 @@ export async function runHappyMcpStdioBridge(argv: string[]): Promise<void> {
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
const displayImageInputSchema: z.ZodTypeAny = z.object({
|
||||
path: z.string().describe('Local filesystem path of the image to display to the user'),
|
||||
title: z.string().optional().describe('Optional display title or filename for the image'),
|
||||
});
|
||||
|
||||
server.registerTool<any, any>(
|
||||
'display_image',
|
||||
{
|
||||
description: 'Display a local image file inline in the current HAPI chat session',
|
||||
title: 'Display Image',
|
||||
inputSchema: displayImageInputSchema,
|
||||
},
|
||||
async (args: Record<string, unknown>) => {
|
||||
try {
|
||||
const client = await ensureHttpClient();
|
||||
const response = await client.callTool({ name: 'display_image', arguments: args });
|
||||
return response as any;
|
||||
} catch (error) {
|
||||
return {
|
||||
content: [
|
||||
{ type: 'text' as const, text: `Failed to display image: ${error instanceof Error ? error.message : String(error)}` },
|
||||
],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Start STDIO transport
|
||||
const stdio = new StdioServerTransport();
|
||||
await server.connect(stdio);
|
||||
|
||||
@@ -17,6 +17,7 @@ export const TITLE_INSTRUCTION = trimIdent(`
|
||||
Prefer calling functions.hapi__change_title.
|
||||
If that exact tool name is unavailable, call an equivalent alias such as hapi__change_title, mcp__hapi__change_title, or hapi_change_title.
|
||||
If the task focus changes significantly later, call the title tool again with a better title.
|
||||
When you create or find a local image file that the user should see, call functions.hapi__display_image with the image path. If that exact tool name is unavailable, use an equivalent alias such as hapi__display_image, mcp__hapi__display_image, or hapi_display_image.
|
||||
`);
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user