diff --git a/cli/src/modules/common/displayImagePrompt.test.ts b/cli/src/modules/common/displayImagePrompt.test.ts index ebd759cc..38ad31d3 100644 --- a/cli/src/modules/common/displayImagePrompt.test.ts +++ b/cli/src/modules/common/displayImagePrompt.test.ts @@ -1,15 +1,21 @@ import { describe, expect, it } from 'vitest' +import { HAPI_MCP_BRIDGE_PROMPT } from './hapiMcpBridgePrompt' import { DISPLAY_IMAGE_PROMPT_CLAUDE, DISPLAY_IMAGE_PROMPT_CODEX, DISPLAY_IMAGE_PROMPT_CURSOR, - DISPLAY_IMAGE_PROMPT_HAPI_MCP, + DISPLAY_MEDIA_PROMPT_CLAUDE, + DISPLAY_MEDIA_PROMPT_CODEX, + DISPLAY_MEDIA_PROMPT_CURSOR, + DISPLAY_TOOLS_PROMPT_HAPI_MCP, + DISPLAY_VIDEO_PROMPT_CLAUDE, + DISPLAY_VIDEO_PROMPT_CODEX, + DISPLAY_VIDEO_PROMPT_CURSOR, } from './displayImagePrompt' const displayImagePrompts = [ DISPLAY_IMAGE_PROMPT_CLAUDE, DISPLAY_IMAGE_PROMPT_CODEX, - DISPLAY_IMAGE_PROMPT_HAPI_MCP, DISPLAY_IMAGE_PROMPT_CURSOR, ] @@ -27,7 +33,67 @@ describe('display_image prompt semantics', () => { it('keeps each agent flavor instruction pointed at the corresponding tool alias', () => { expect(DISPLAY_IMAGE_PROMPT_CLAUDE).toContain('mcp__hapi__display_image') expect(DISPLAY_IMAGE_PROMPT_CODEX).toContain('functions.hapi__display_image') - expect(DISPLAY_IMAGE_PROMPT_HAPI_MCP).toContain('hapi_display_image') expect(DISPLAY_IMAGE_PROMPT_CURSOR).toContain('"display_image"') }) }) + +const SCOPE_BULLET = "- Use them for files you created, or found on the user's behalf, that the user has not been shown. Files the user attached, pasted, or pointed you to are already visible to them, so do not display those again unless the user asks." + +const BLOCK_HEADER = 'HAPI display tools (display_image, display_video, display_media) show a local file to the user in the HAPI chat.' + +function countOccurrences(text: string, needle: string): number { + return text.split(needle).length - 1 +} + +describe('HAPI display tools block (OpenCode)', () => { + it('names all three tools once and explains the direct and Code Mode calls', () => { + expect(countOccurrences(DISPLAY_TOOLS_PROMPT_HAPI_MCP, BLOCK_HEADER)).toBe(1) + expect(DISPLAY_TOOLS_PROMPT_HAPI_MCP).toContain('hapi_') + expect(DISPLAY_TOOLS_PROMPT_HAPI_MCP).toContain('tools.hapi.') + expect(DISPLAY_TOOLS_PROMPT_HAPI_MCP).toContain('`await tools.hapi.({ path })`') + expect(DISPLAY_TOOLS_PROMPT_HAPI_MCP).toContain('`execute`') + expect(DISPLAY_TOOLS_PROMPT_HAPI_MCP).toContain('Do not guess other names') + expect(DISPLAY_TOOLS_PROMPT_HAPI_MCP.indexOf('hapi_')).toBeLessThan(DISPLAY_TOOLS_PROMPT_HAPI_MCP.indexOf('tools.hapi.')) + }) + + it('limits the tools to files the user has not been shown, unless the user asks', () => { + expect(countOccurrences(DISPLAY_TOOLS_PROMPT_HAPI_MCP, SCOPE_BULLET)).toBe(1) + expect(DISPLAY_TOOLS_PROMPT_HAPI_MCP).toContain('unless the user asks') + expect(DISPLAY_TOOLS_PROMPT_HAPI_MCP).not.toContain('never') + const bullets = DISPLAY_TOOLS_PROMPT_HAPI_MCP.split('\n') + expect(bullets[1]).toBe(SCOPE_BULLET) + expect(countOccurrences(HAPI_MCP_BRIDGE_PROMPT, SCOPE_BULLET)).toBe(1) + }) + + it('says display_image only shows the image to the user', () => { + expect(DISPLAY_TOOLS_PROMPT_HAPI_MCP).toContain('display_image only shows the image to the user; it does not let you see or analyze it.') + }) + + it('does not advertise aliases that OpenCode may not expose', () => { + expect(DISPLAY_TOOLS_PROMPT_HAPI_MCP).not.toContain('mcp__hapi__') + expect(DISPLAY_TOOLS_PROMPT_HAPI_MCP).not.toContain('equivalent alias') + }) + + it('is part of the bridge prompt exactly once', () => { + expect(countOccurrences(HAPI_MCP_BRIDGE_PROMPT, BLOCK_HEADER)).toBe(1) + }) +}) + +describe('display prompts for other agents', () => { + it('keep their own tool names and never mention Code Mode', () => { + const others = [ + DISPLAY_IMAGE_PROMPT_CLAUDE, DISPLAY_VIDEO_PROMPT_CLAUDE, DISPLAY_MEDIA_PROMPT_CLAUDE, + DISPLAY_IMAGE_PROMPT_CODEX, DISPLAY_VIDEO_PROMPT_CODEX, DISPLAY_MEDIA_PROMPT_CODEX, + DISPLAY_IMAGE_PROMPT_CURSOR, DISPLAY_VIDEO_PROMPT_CURSOR, DISPLAY_MEDIA_PROMPT_CURSOR, + ] + for (const prompt of others) { + expect(prompt).not.toContain('Code Mode') + expect(prompt).not.toContain('tools.hapi.') + expect(prompt).not.toContain('`execute`') + expect(prompt).not.toContain('already visible to them') + } + expect(DISPLAY_VIDEO_PROMPT_CLAUDE).toContain('mcp__hapi__display_video') + expect(DISPLAY_MEDIA_PROMPT_CODEX).toContain('functions.hapi__display_media') + expect(DISPLAY_MEDIA_PROMPT_CURSOR).toContain('"display_media"') + }) +}) diff --git a/cli/src/modules/common/displayImagePrompt.ts b/cli/src/modules/common/displayImagePrompt.ts index 48dfbf09..5de7ae5a 100644 --- a/cli/src/modules/common/displayImagePrompt.ts +++ b/cli/src/modules/common/displayImagePrompt.ts @@ -14,11 +14,6 @@ export const DISPLAY_IMAGE_PROMPT_CODEX = trimIdent(` This tool sends the image to the human user for inline display in HAPI. It is not an image-reading or image-understanding tool: it does not provide image input to the model and cannot be used to read, inspect, or analyze image contents. `); -export const DISPLAY_IMAGE_PROMPT_HAPI_MCP = trimIdent(` - When you create or find a local image file that the human user should see, call the tool "hapi_display_image" with the absolute filesystem path so HAPI can show it inline. If that exact tool name is unavailable, use an equivalent alias such as display_image or mcp__hapi__display_image. - This tool sends the image to the human user for inline display in HAPI. It is not an image-reading or image-understanding tool: it does not provide image input to the model and cannot be used to read, inspect, or analyze image contents. -`); - export const DISPLAY_VIDEO_PROMPT_CLAUDE = trimIdent(` When you create or find a local mp4 or webm recording the user should see, call the tool "mcp__hapi__display_video" with the file path so HAPI can show it inline. `); @@ -27,10 +22,6 @@ export const DISPLAY_VIDEO_PROMPT_CODEX = trimIdent(` When you create or find a local mp4 or webm file the user should see, call functions.hapi__display_video with the file path. If that exact tool name is unavailable, use an equivalent alias such as hapi__display_video, mcp__hapi__display_video, or hapi_display_video. `); -export const DISPLAY_VIDEO_PROMPT_HAPI_MCP = trimIdent(` - When you create or find a local mp4 or webm recording the user should see, call the tool "hapi_display_video" with the file path so HAPI can show it inline. If that exact tool name is unavailable, use an equivalent alias such as display_video or mcp__hapi__display_video. -`); - export const DISPLAY_IMAGE_PROMPT_CURSOR = trimIdent(` When you create or find a local image file that the human user should see, call the tool "display_image" with the absolute filesystem path so HAPI can show it inline. This tool sends the image to the human user for inline display in HAPI. It is not an image-reading or image-understanding tool: it does not provide image input to the model and cannot be used to read, inspect, or analyze image contents. @@ -48,10 +39,18 @@ export const DISPLAY_MEDIA_PROMPT_CODEX = trimIdent(` When you create or find a local audio file or other non-image file that the user should receive, call functions.hapi__display_media with the file path. If that exact tool name is unavailable, use an equivalent alias such as hapi__display_media, mcp__hapi__display_media, or hapi_display_media. `); -export const DISPLAY_MEDIA_PROMPT_HAPI_MCP = trimIdent(` - When you create or find a local audio file or other non-image file that the user should receive, call the tool "hapi_display_media" with the file path. If that exact tool name is unavailable, use an equivalent alias such as display_media or mcp__hapi__display_media. -`); - export const DISPLAY_MEDIA_PROMPT_CURSOR = trimIdent(` When you create or find a local audio file or other non-image file that the user should receive, call the tool "display_media" with the absolute filesystem path so HAPI can show a player or download card. `); + +/** + * Display tool guidance for OpenCode, written once for all three tools. OpenCode v2 can expose + * MCP tools only through a Code Mode catalog whose entries are callable as JavaScript inside the + * `execute` tool, while other setups list them as direct tools; the wording covers both. + */ +export const DISPLAY_TOOLS_PROMPT_HAPI_MCP = trimIdent(` + HAPI display tools (display_image, display_video, display_media) show a local file to the user in the HAPI chat. + - Use them for files you created, or found on the user's behalf, that the user has not been shown. Files the user attached, pasted, or pointed you to are already visible to them, so do not display those again unless the user asks. + - To call one: if hapi_ is in your tool list, call it directly. Otherwise, if your Code Mode catalog lists tools.hapi., call \`await tools.hapi.({ path })\` inside the \`execute\` tool. Do not guess other names. + - display_image only shows the image to the user; it does not let you see or analyze it. +`); diff --git a/cli/src/modules/common/hapiMcpBridgePrompt.ts b/cli/src/modules/common/hapiMcpBridgePrompt.ts index 3017c362..0b9ce224 100644 --- a/cli/src/modules/common/hapiMcpBridgePrompt.ts +++ b/cli/src/modules/common/hapiMcpBridgePrompt.ts @@ -1,9 +1,5 @@ import { trimIdent } from '@/utils/trimIdent'; -import { - DISPLAY_IMAGE_PROMPT_HAPI_MCP, - DISPLAY_MEDIA_PROMPT_HAPI_MCP, - DISPLAY_VIDEO_PROMPT_HAPI_MCP, -} from './displayImagePrompt'; +import { DISPLAY_TOOLS_PROMPT_HAPI_MCP } from './displayImagePrompt'; /** Shell fallback for `hapi doctor inline-media` only — not injected into agent prompts. */ export const INLINE_MEDIA_SHELL_FALLBACK = trimIdent(` @@ -18,13 +14,11 @@ export const INLINE_MEDIA_SHELL_FALLBACK = trimIdent(` * hapi-instructions.md. Cursor uses native MCP overlay + tool descriptions instead * (no user-turn prepend — that path was prompt-taint). */ -export const HAPI_MCP_TITLE_INSTRUCTION = trimIdent(` +const HAPI_MCP_TITLE_INSTRUCTION = trimIdent(` Use the title tool sparingly. For a new chat, call the tool "hapi_change_title" once after the user's initial request is clear, and set a concise task title. Do not rename the chat for routine progress, substeps, implementation details, or a slightly better wording. Rename only when the user's primary objective changes substantially and the existing title would be misleading. `); export const HAPI_MCP_BRIDGE_PROMPT = trimIdent(` ${HAPI_MCP_TITLE_INSTRUCTION} - ${DISPLAY_IMAGE_PROMPT_HAPI_MCP} - ${DISPLAY_VIDEO_PROMPT_HAPI_MCP} - ${DISPLAY_MEDIA_PROMPT_HAPI_MCP} + ${DISPLAY_TOOLS_PROMPT_HAPI_MCP} `); diff --git a/cli/src/opencode/opencodeRemoteLauncher.test.ts b/cli/src/opencode/opencodeRemoteLauncher.test.ts index 427bc1f3..66336f0d 100644 --- a/cli/src/opencode/opencodeRemoteLauncher.test.ts +++ b/cli/src/opencode/opencodeRemoteLauncher.test.ts @@ -1836,11 +1836,27 @@ describe('opencodeRemoteLauncher inline model switch', () => { expect(JSON.stringify(harness.promptContents[0])).toContain('$name'); expect(JSON.stringify(harness.promptContents[0])).toContain('skill_lookup'); - expect(JSON.stringify(harness.promptContents[0])).toContain('hapi_display_image'); + expect(JSON.stringify(harness.promptContents[0])).toContain('hapi_'); expect(JSON.stringify(harness.promptContents[0])).toContain('hapi_change_title'); expect(JSON.stringify(harness.promptContents[1])).not.toContain('skill_lookup'); }); + it.each(['v1', 'v2'] as const)('puts the display tools block once in the first prompt only on opencode %s', async (version) => { + harness.detectedVersion = version; + const { session } = createSessionStub([ + { message: 'first', mode: createMode() }, + { message: 'second', mode: createMode() } + ]); + + await opencodeRemoteLauncher(session as never); + + const header = 'HAPI display tools (display_image, display_video, display_media) show a local file to the user in the HAPI chat.'; + expect(JSON.stringify(harness.promptContents[0]).split(header)).toHaveLength(2); + expect(JSON.stringify(harness.promptContents[0])).toContain('tools.hapi.'); + expect(JSON.stringify(harness.promptContents[0])).toContain('do not display those again unless the user asks'); + expect(JSON.stringify(harness.promptContents[1])).not.toContain(header); + }); + it('spawns the ACP backend with an explicit --port/--hostname from allocateFreePort', async () => { const { session } = createSessionStub([ { message: 'first', mode: createMode() } diff --git a/cli/src/opencode/utils/systemPrompt.test.ts b/cli/src/opencode/utils/systemPrompt.test.ts index 6059e1cd..f61a58bf 100644 --- a/cli/src/opencode/utils/systemPrompt.test.ts +++ b/cli/src/opencode/utils/systemPrompt.test.ts @@ -3,7 +3,21 @@ import { mkdtemp, readFile, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { ensureOpencodeConfig } from './opencodeConfig' -import { TITLE_INSTRUCTION } from './systemPrompt' +import { getOpencodeNativeToolInstruction, getTitleInstruction, TITLE_INSTRUCTION } from './systemPrompt' + +const SCOPE_BULLET = "- Use them for files you created, or found on the user's behalf, that the user has not been shown. Files the user attached, pasted, or pointed you to are already visible to them, so do not display those again unless the user asks." + +const BLOCK_HEADER = 'HAPI display tools (display_image, display_video, display_media) show a local file to the user in the HAPI chat.' + +function expectDisplayToolsBlockOnce(text: string): void { + expect(text.split(BLOCK_HEADER)).toHaveLength(2) + for (const tool of ['display_image', 'display_video', 'display_media']) { + expect(text).toContain(tool) + } + expect(text).toContain('`await tools.hapi.({ path })`') + expect(text.split(SCOPE_BULLET)).toHaveLength(2) + expect(text).toContain('`execute`') +} describe('OpenCode local HAPI instructions', () => { let configDirectory: string | null = null @@ -42,7 +56,7 @@ describe('OpenCode ACP title and peer instructions', () => { for (const prompt of [TITLE_INSTRUCTION, OPENCODE_NATIVE_TOOL_INSTRUCTION]) { expect(prompt).toContain('hapi_change_title') - expect(prompt).toContain('hapi_display_image') + expect(prompt).toContain('hapi_') expect(prompt).toContain('skill_lookup') for (const tool of ['hapi_list_peers', 'hapi_inspect_peer', 'hapi_ping_peer']) { if (disabled) { @@ -54,3 +68,26 @@ describe('OpenCode ACP title and peer instructions', () => { } }) }) + +describe('OpenCode display tools block', () => { + it('appears once in the local hapi-instructions.md', async () => { + const dir = await mkdtemp(join(tmpdir(), 'hapi-opencode-display-')) + try { + const { instructionsPath } = ensureOpencodeConfig(dir, { command: 'hapi', args: ['mcp'] }, getTitleInstruction({})) + expectDisplayToolsBlockOnce(await readFile(instructionsPath, 'utf8')) + } finally { + await rm(dir, { recursive: true, force: true }) + } + }) + + it('appears once in the ACP first-prompt instruction', () => { + expectDisplayToolsBlockOnce(getOpencodeNativeToolInstruction({})) + }) + + it('keeps the title and skill lookup instructions next to it', () => { + for (const prompt of [getTitleInstruction({}), getOpencodeNativeToolInstruction({})]) { + expect(prompt).toContain('hapi_change_title') + expect(prompt).toContain('skill_lookup') + } + }) +}) diff --git a/cli/src/opencode/utils/systemPrompt.ts b/cli/src/opencode/utils/systemPrompt.ts index 94d97c3a..4ef52b89 100644 --- a/cli/src/opencode/utils/systemPrompt.ts +++ b/cli/src/opencode/utils/systemPrompt.ts @@ -8,12 +8,7 @@ import { trimIdent } from '@/utils/trimIdent'; import { buildSessionCitationSteerInstruction, SESSION_CITATION_DISABLED_STEER } from '@hapi/protocol/sessionCitation'; import { isPeerToolsDisabled } from '@/modules/common/peerTools'; -import { HAPI_MCP_BRIDGE_PROMPT, HAPI_MCP_TITLE_INSTRUCTION } from '@/modules/common/hapiMcpBridgePrompt'; -import { - DISPLAY_IMAGE_PROMPT_HAPI_MCP, - DISPLAY_MEDIA_PROMPT_HAPI_MCP, - DISPLAY_VIDEO_PROMPT_HAPI_MCP, -} from '@/modules/common/displayImagePrompt'; +import { HAPI_MCP_BRIDGE_PROMPT } from '@/modules/common/hapiMcpBridgePrompt'; import { SKILL_LOOKUP_INSTRUCTION } from '@/modules/common/skillLookupInstruction'; import { withSessionSummaryInstruction } from '@/modules/common/sessionSummaryInstruction'; @@ -40,10 +35,7 @@ export function getTitleInstruction(env: NodeJS.ProcessEnv = process.env): strin * same MCP bridge; manual titles take precedence over native ACP title sync. */ export const OPENCODE_NATIVE_TOOL_INSTRUCTION = trimIdent(` - ${HAPI_MCP_TITLE_INSTRUCTION} - ${DISPLAY_IMAGE_PROMPT_HAPI_MCP} - ${DISPLAY_VIDEO_PROMPT_HAPI_MCP} - ${DISPLAY_MEDIA_PROMPT_HAPI_MCP} + ${HAPI_MCP_BRIDGE_PROMPT} ${isPeerToolsDisabled() ? SESSION_CITATION_DISABLED_STEER : buildSessionCitationSteerInstruction({ inspectTool: 'hapi_inspect_peer', pingTool: 'hapi_ping_peer',