fix: remove duplicate handleAgents from menu.ts, cleanup imports

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
0xmrpeter 2026-03-22 20:53:57 +07:00
parent ccc44194e7
commit 2300a21dfc
4 changed files with 57 additions and 20 deletions

View File

@ -1,8 +1,6 @@
import type { Context } from "grammy";
import { InlineKeyboard } from "grammy";
import type { OpenACPCore } from "../../../core/index.js";
import type { AgentCommand } from "../../../core/index.js";
import { escapeHtml } from "../formatting.js";
import type { CommandsAssistantContext } from "../types.js";
export function buildMenuKeyboard(): InlineKeyboard {
@ -30,21 +28,6 @@ export async function handleMenu(ctx: Context): Promise<void> {
});
}
export async function handleAgents(ctx: Context, core: OpenACPCore): Promise<void> {
const agents = core.agentManager.getAvailableAgents();
const defaultAgent = core.configManager.get().defaultAgent;
const lines = agents.map(
(a) =>
`• <b>${escapeHtml(a.name)}</b>${a.name === defaultAgent ? " (default)" : ""}\n` +
` <code>${escapeHtml(a.command)} ${a.args.map((arg) => escapeHtml(arg)).join(" ")}</code>`,
);
const text =
lines.length > 0
? `<b>Available Agents:</b>\n\n${lines.join("\n")}`
: `<b>Available Agents:</b>\n\nNo agents configured.`;
await ctx.reply(text, { parse_mode: "HTML" });
}
export async function handleHelp(ctx: Context): Promise<void> {
await ctx.reply(
`📖 <b>OpenACP Help</b>\n\n` +

View File

@ -1,4 +1,4 @@
import { describe, it, expect } from 'vitest'
import { describe, it, expect, vi } from 'vitest'
import { PermissionGate } from '../permission-gate.js'
import type { PermissionRequest } from '../types.js'
@ -74,4 +74,36 @@ describe('PermissionGate', () => {
const result = await p2
expect(result).toBe('deny')
})
it('rejects with timeout when no response received', async () => {
vi.useFakeTimers()
const gate = new PermissionGate(5000) // 5s timeout
const promise = gate.setPending(mockRequest)
expect(gate.isPending).toBe(true)
// Advance past timeout
vi.advanceTimersByTime(5000)
await expect(promise).rejects.toThrow('Permission request timed out')
expect(gate.isPending).toBe(false)
vi.useRealTimers()
})
it('clears timeout when resolved before expiry', async () => {
vi.useFakeTimers()
const gate = new PermissionGate(5000)
const promise = gate.setPending(mockRequest)
gate.resolve('allow')
const result = await promise
expect(result).toBe('allow')
// Advancing past timeout should not cause issues
vi.advanceTimersByTime(10000)
expect(gate.isPending).toBe(false)
vi.useRealTimers()
})
})

View File

@ -1,4 +1,4 @@
import { spawn, execSync, type ChildProcess } from "node:child_process";
import { spawn, execFileSync, type ChildProcess } from "node:child_process";
import { Transform } from "node:stream";
import fs from "node:fs";
import path from "node:path";
@ -78,7 +78,7 @@ function resolveAgentCommand(cmd: string): { command: string; args: string[] } {
// 3. Try resolving from PATH using which
try {
const fullPath = execSync(`which ${cmd}`, { encoding: "utf-8" }).trim();
const fullPath = execFileSync("which", [cmd], { encoding: "utf-8" }).trim();
if (fullPath) {
const content = fs.readFileSync(fullPath, "utf-8");
if (content.startsWith("#!/usr/bin/env node")) {

View File

@ -1,5 +1,7 @@
import type { PermissionRequest } from "./types.js";
const DEFAULT_TIMEOUT_MS = 10 * 60 * 1000; // 10 minutes
/**
* Encapsulates pending permission state with a typed Promise API.
*/
@ -8,20 +10,32 @@ export class PermissionGate {
private resolveFn?: (optionId: string) => void;
private rejectFn?: (reason: Error) => void;
private settled = false;
private timeoutTimer?: ReturnType<typeof setTimeout>;
private timeoutMs: number;
constructor(timeoutMs?: number) {
this.timeoutMs = timeoutMs ?? DEFAULT_TIMEOUT_MS;
}
setPending(request: PermissionRequest): Promise<string> {
this.request = request;
this.settled = false;
this.clearTimeout();
return new Promise<string>((resolve, reject) => {
this.resolveFn = resolve;
this.rejectFn = reject;
this.timeoutTimer = setTimeout(() => {
this.reject("Permission request timed out (no response received)");
}, this.timeoutMs);
});
}
resolve(optionId: string): void {
if (this.settled || !this.resolveFn) return;
this.settled = true;
this.clearTimeout();
this.resolveFn(optionId);
this.cleanup();
}
@ -29,6 +43,7 @@ export class PermissionGate {
reject(reason?: string): void {
if (this.settled || !this.rejectFn) return;
this.settled = true;
this.clearTimeout();
this.rejectFn(new Error(reason ?? "Permission rejected"));
this.cleanup();
}
@ -46,6 +61,13 @@ export class PermissionGate {
return this.request?.id;
}
private clearTimeout(): void {
if (this.timeoutTimer) {
clearTimeout(this.timeoutTimer);
this.timeoutTimer = undefined;
}
}
private cleanup(): void {
this.request = undefined;
this.resolveFn = undefined;