import type { AutocompleteItem } from "@earendil-works/pi-tui"; import type { AgentConnectionCommandArgument } from "./agent-connection/types.js"; const firstToken = (value: string): string => value.trimEnd().split(" ")[0] ?? ""; /** * Complete an argument from the candidates a remote host published for a command. * * `value` replaces the whole typed argument, so candidates carry their own leading tokens * ("policy deepseek-economic"). A prefix without a space completes the first token only, * which keeps deeper candidates out of the first popup. */ export function getPublishedArgumentCompletions( candidates: readonly AgentConnectionCommandArgument[], prefix: string, ): AutocompleteItem[] | null { const lowered = prefix.toLowerCase(); const completingFirstToken = !prefix.includes(" "); const matched = candidates.filter((candidate) => { const value = candidate.value.toLowerCase(); // A candidate identical to what is already typed adds nothing. if (value === lowered) return false; if (completingFirstToken && candidate.value.trimEnd() !== firstToken(candidate.value)) return false; return value.startsWith(lowered); }); if (matched.length === 0) return null; return matched.map((candidate) => ({ value: candidate.value, label: candidate.label ?? candidate.value, ...(candidate.description && { description: candidate.description }), })); }