mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-24 04:47:03 +00:00
fix(opencode): describe display tools via Code Mode execute for OpenCode v2 (#2307)
* fix(opencode): describe display tools via Code Mode execute for OpenCode v2
OpenCode v2 can list MCP tools only in a Code Mode catalog whose entries
are callable as JavaScript inside the execute tool, so the hapi_display_*
names and aliases HAPI advertised may not exist there. Replace the three
per-tool display hints used by the OpenCode instructions with one block
that prefers a direct tool and otherwise calls tools.hapi.<tool>({ path })
through execute. The block appears once in both the ACP first prompt and
the local hapi-instructions.md. Hints for other agents are unchanged.
* fix(opencode): do not re-display user-provided files unless asked
Agents sometimes called the display tools on a file the user had just
attached or asked them to read, which only echoes it back. Say in the
display tools block that the tools are for files the user has not been
shown, and that files the user attached, pasted, or pointed to are not
displayed again unless the user asks.
This commit is contained in:
@@ -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_<tool>');
|
||||
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.<tool>');
|
||||
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() }
|
||||
|
||||
@@ -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.<tool>({ 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_<tool>')
|
||||
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')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user