{"version":3,"file":"composerCompletion.mjs","names":[],"sources":["../../../src/web/lib/composerCompletion.ts"],"sourcesContent":["import type { CommandInfo } from './sessionModel';\n\n/**\n * The composer's `/command`, `$skill` and `@file` completion, shared by the\n * cockpit and the phone so both offer the same items for the same draft.\n */\n\n/** Bound the result set so searches and their scrollable list stay quick even in large workspaces. */\nexport const MAX_COMPLETION_ITEMS = 20;\n/** Pi names skill commands `skill:<name>`; the composer browses them under `$`. */\nconst SKILL_PREFIX = 'skill:';\n\nexport type CompletionKind = 'command' | 'skill' | 'file';\nexport const TRIGGER_CHARS: Record<CompletionKind, string> = { command: '/', skill: '$', file: '@' };\n\nexport interface CompletionItem {\n  /** What replaces the trigger token when accepted. */\n  insert: string;\n  label: string;\n  detail?: string;\n}\n\nexport interface CompletionTrigger {\n  kind: CompletionKind;\n  /** Index of the trigger character in the draft. */\n  start: number;\n  query: string;\n}\n\n/** The /command, $skill or @file token the caret sits in, or null when there is none. */\nexport function triggerTokenAt(draft: string, caret: number): CompletionTrigger | null {\n  const before = draft.slice(0, caret);\n  const start = Math.max(before.lastIndexOf(' '), before.lastIndexOf('\\n')) + 1;\n  const token = before.slice(start);\n  if (token.startsWith('/') && start === 0) return { kind: 'command', start, query: token.slice(1) };\n  if (token.startsWith('$') && start === 0) return { kind: 'skill', start, query: token.slice(1) };\n  if (token.startsWith('@') && token.length <= 64) return { kind: 'file', start, query: token.slice(1) };\n  return null;\n}\n\n/** The session's commands or skills whose shown name starts with the query, sorted. */\nexport function commandCompletions(\n  commands: readonly CommandInfo[],\n  kind: 'command' | 'skill',\n  query: string,\n): CompletionItem[] {\n  const needle = query.toLowerCase();\n  return (\n    commands\n      .flatMap((command) => {\n        const skill = command.name.startsWith(SKILL_PREFIX);\n        if (skill !== (kind === 'skill')) return [];\n        const shown = skill ? command.name.slice(SKILL_PREFIX.length) : command.name;\n        if (!shown.toLowerCase().startsWith(needle)) return [];\n        // Skills are browsed as `$name` but Pi only answers to the real\n        // `/skill:name`, so the label and the insert differ for them.\n        return [{ insert: `/${command.name} `, label: `${TRIGGER_CHARS[kind]}${shown}`, detail: command.description }];\n      })\n      // Sort before truncating. The server answers in registration order, so an\n      // unsorted slice drops an arbitrary tail once a workspace has more than\n      // MAX_COMPLETION_ITEMS matches, which looks exactly like a missing skill.\n      .sort((left, right) => left.label.localeCompare(right.label))\n      .slice(0, MAX_COMPLETION_ITEMS)\n  );\n}\n\n/**\n * The hub's file search answer as completion items, after any references\n * plugins offer for files outside the workspace, such as `artifact:plan.md`.\n */\nexport function fileCompletions(\n  files: readonly string[],\n  owned: readonly { path: string; detail?: string }[] = [],\n): CompletionItem[] {\n  return [\n    ...owned.map(({ path, detail }) => ({ insert: `@${path} `, label: `@${path}`, ...(detail ? { detail } : {}) })),\n    ...files.map((file) => ({ insert: `@${file} `, label: `@${file}` })),\n  ].slice(0, MAX_COMPLETION_ITEMS);\n}\n"],"mappings":";;;;;;AAQA,MAAa,uBAAuB;;AAEpC,MAAM,eAAe;AAGrB,MAAa,gBAAgD;CAAE,SAAS;CAAK,OAAO;CAAK,MAAM;AAAI;;AAiBnG,SAAgB,eAAe,OAAe,OAAyC;CACrF,MAAM,SAAS,MAAM,MAAM,GAAG,KAAK;CACnC,MAAM,QAAQ,KAAK,IAAI,OAAO,YAAY,GAAG,GAAG,OAAO,YAAY,IAAI,CAAC,IAAI;CAC5E,MAAM,QAAQ,OAAO,MAAM,KAAK;CAChC,IAAI,MAAM,WAAW,GAAG,KAAK,UAAU,GAAG,OAAO;EAAE,MAAM;EAAW;EAAO,OAAO,MAAM,MAAM,CAAC;CAAE;CACjG,IAAI,MAAM,WAAW,GAAG,KAAK,UAAU,GAAG,OAAO;EAAE,MAAM;EAAS;EAAO,OAAO,MAAM,MAAM,CAAC;CAAE;CAC/F,IAAI,MAAM,WAAW,GAAG,KAAK,MAAM,UAAU,IAAI,OAAO;EAAE,MAAM;EAAQ;EAAO,OAAO,MAAM,MAAM,CAAC;CAAE;CACrG,OAAO;AACT;;AAGA,S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