mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-16 03:16:29 +00:00
fix(codex): handle remote request_user_input approvals (#382)
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { registerAppServerPermissionHandlers } from './appServerPermissionAdapter';
|
||||
|
||||
type UserInputHandler = NonNullable<Parameters<typeof registerAppServerPermissionHandlers>[0]['onUserInputRequest']>;
|
||||
|
||||
function createClient() {
|
||||
const handlers = new Map<string, (params: unknown) => Promise<unknown> | unknown>();
|
||||
return {
|
||||
client: {
|
||||
registerRequestHandler(method: string, handler: (params: unknown) => Promise<unknown> | unknown) {
|
||||
handlers.set(method, handler);
|
||||
}
|
||||
},
|
||||
handlers
|
||||
};
|
||||
}
|
||||
|
||||
describe('registerAppServerPermissionHandlers', () => {
|
||||
it('forwards request_user_input answers through the callback', async () => {
|
||||
const { client, handlers } = createClient();
|
||||
const permissionHandler = {
|
||||
handleToolCall: vi.fn()
|
||||
};
|
||||
const onUserInputRequest: UserInputHandler = async ({ id, input }) => {
|
||||
expect(id).toBe('tool-123');
|
||||
expect(input).toEqual({
|
||||
itemId: 'tool-123',
|
||||
questions: [{ id: 'approve_nav', question: 'Approve app tool call?' }]
|
||||
});
|
||||
return {
|
||||
decision: 'accept',
|
||||
answers: {
|
||||
approve_nav: {
|
||||
answers: ['Allow']
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
registerAppServerPermissionHandlers({
|
||||
client: client as never,
|
||||
permissionHandler: permissionHandler as never,
|
||||
onUserInputRequest: vi.fn(onUserInputRequest)
|
||||
});
|
||||
|
||||
const handler = handlers.get('item/tool/requestUserInput');
|
||||
expect(handler).toBeTypeOf('function');
|
||||
|
||||
await expect(handler?.({
|
||||
itemId: 'tool-123',
|
||||
questions: [{ id: 'approve_nav', question: 'Approve app tool call?' }]
|
||||
})).resolves.toEqual({
|
||||
decision: 'accept',
|
||||
answers: {
|
||||
approve_nav: {
|
||||
answers: ['Allow']
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('cancels request_user_input when no callback is registered', async () => {
|
||||
const { client, handlers } = createClient();
|
||||
const permissionHandler = {
|
||||
handleToolCall: vi.fn()
|
||||
};
|
||||
|
||||
registerAppServerPermissionHandlers({
|
||||
client: client as never,
|
||||
permissionHandler: permissionHandler as never
|
||||
});
|
||||
|
||||
const handler = handlers.get('item/tool/requestUserInput');
|
||||
expect(handler).toBeTypeOf('function');
|
||||
|
||||
await expect(handler?.({ itemId: 'tool-123' })).resolves.toEqual({
|
||||
decision: 'cancel'
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user