mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
93 lines
2.6 KiB
TypeScript
93 lines
2.6 KiB
TypeScript
import type { PromptAgentInput, PromptFileInput, PromptSkillInput } from '@/api/opencode'
|
|
import type { FileAttachmentInfo, ImageAttachment } from '@/api/types'
|
|
|
|
export const MENTION_PATTERN = /@([A-Za-z0-9_\-./]+)/g
|
|
export const MENTION_TRIGGER_PATTERN = /(^|\s)@([A-Za-z0-9_\-./]*)$/
|
|
|
|
export interface MentionTrigger {
|
|
start: number
|
|
end: number
|
|
query: string
|
|
}
|
|
|
|
export interface MentionItem {
|
|
type: 'file' | 'agent' | 'skill'
|
|
value: string
|
|
}
|
|
|
|
export function detectMentionTrigger(
|
|
text: string,
|
|
cursorPosition: number
|
|
): MentionTrigger | null {
|
|
const textBeforeCursor = text.slice(0, cursorPosition)
|
|
const match = textBeforeCursor.match(MENTION_TRIGGER_PATTERN)
|
|
|
|
if (!match || match.index === undefined) return null
|
|
|
|
const atIndex = match.index + match[1].length
|
|
return {
|
|
start: atIndex,
|
|
end: cursorPosition,
|
|
query: match[2]
|
|
}
|
|
}
|
|
|
|
export interface ParsedPromptInput {
|
|
text: string
|
|
files: PromptFileInput[]
|
|
agents: PromptAgentInput[]
|
|
skills: PromptSkillInput[]
|
|
}
|
|
|
|
export function parsePromptToInput(
|
|
rawInput: string,
|
|
fileMap: Map<string, FileAttachmentInfo>,
|
|
agentNames: string[],
|
|
skillIds: string[],
|
|
imageAttachments?: ImageAttachment[]
|
|
): ParsedPromptInput {
|
|
const files: PromptFileInput[] = []
|
|
const agents: PromptAgentInput[] = []
|
|
const skills: PromptSkillInput[] = []
|
|
const agentNameByLowercase = new Map(agentNames.map((name) => [name.toLowerCase(), name]))
|
|
const skillIdByLowercase = new Map(skillIds.map((id) => [id.toLowerCase(), id]))
|
|
|
|
for (const match of rawInput.matchAll(MENTION_PATTERN)) {
|
|
const matchIndex = match.index!
|
|
const mentionText = match[1]
|
|
const mention = { start: matchIndex, end: matchIndex + match[0].length, text: match[0] }
|
|
|
|
const file = fileMap.get(mentionText.toLowerCase())
|
|
if (file) {
|
|
files.push({ uri: `file://${file.path}`, name: file.name, mention })
|
|
continue
|
|
}
|
|
|
|
const agentName = agentNameByLowercase.get(mentionText.toLowerCase())
|
|
if (agentName) {
|
|
agents.push({ name: agentName, mention })
|
|
continue
|
|
}
|
|
|
|
const skillId = skillIdByLowercase.get(mentionText.toLowerCase())
|
|
if (skillId && !skills.some((skill) => skill.id === skillId)) {
|
|
skills.push({ id: skillId, mention })
|
|
}
|
|
}
|
|
|
|
for (const attachment of imageAttachments ?? []) {
|
|
files.push({ uri: attachment.dataUrl, name: attachment.filename })
|
|
}
|
|
|
|
return { text: rawInput, files, agents, skills }
|
|
}
|
|
|
|
export function getFilename(path: string): string {
|
|
return path.split('/').pop() || path
|
|
}
|
|
|
|
export function getDirectory(path: string): string {
|
|
const parts = path.split('/')
|
|
return parts.slice(0, -1).join('/') || '.'
|
|
}
|