|
| 1 | +import { slash } from '@antfu/utils' |
| 2 | +import { stringifySlide } from '@slidev/parser/core' |
| 3 | +import { createSingletonComposable, useDisposable } from 'reactive-vscode' |
| 4 | +import { LanguageModelTextPart, LanguageModelToolResult, lm } from 'vscode' |
| 5 | +import { useEditingSlideSource } from './composables/useEditingSlideSource' |
| 6 | +import { useFocusedSlideNo } from './composables/useFocusedSlideNo' |
| 7 | +import { activeEntry, activeProject, projects } from './projects' |
| 8 | + |
| 9 | +export const useLmTools = createSingletonComposable(() => { |
| 10 | + const focusedSlideNo = useFocusedSlideNo() |
| 11 | + const editingSlide = useEditingSlideSource() |
| 12 | + |
| 13 | + registerSimpleTool('slidev_getActiveSlide', () => { |
| 14 | + const project = activeProject.value |
| 15 | + |
| 16 | + if (project == null) { |
| 17 | + throw new Error(`No active slide project found.`) |
| 18 | + } |
| 19 | + |
| 20 | + return formatObject({ |
| 21 | + 'Entry file': project.entry, |
| 22 | + 'Root directory': project.userRoot, |
| 23 | + 'Preview server port': project.port || 'Not running', |
| 24 | + 'Number of slides': project.data.slides.length, |
| 25 | + 'Focused slide no. in presentation (from 1)': focusedSlideNo.value, |
| 26 | + 'Editing file': editingSlide.markdown.value?.filepath || 'Not editing', |
| 27 | + 'Editing slide index in file (from 0)': editingSlide.index.value, |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + registerSimpleTool('slidev_getSlideContent', (input: { |
| 32 | + entrySlidePath: string |
| 33 | + slideNo: number |
| 34 | + }) => { |
| 35 | + const project = resolveProjectFromEntry(input.entrySlidePath) |
| 36 | + const slide = project.data.slides[input.slideNo - 1] |
| 37 | + |
| 38 | + if (slide == null) { |
| 39 | + throw new Error(`No content found for slide number ${input.slideNo} in entry: ${project.entry}. Available slides numbers: 1-${project.data.slides.length}`) |
| 40 | + } |
| 41 | + |
| 42 | + return `Content of slide number ${input.slideNo} in entry "${project.entry}" in file "${slide.source.filepath}":\n\n${stringifySlide(slide.source, 1)}` |
| 43 | + }) |
| 44 | + |
| 45 | + // Get all slide titles |
| 46 | + registerSimpleTool('slidev_getAllSlideTitles', (input: { entrySlidePath: string }) => { |
| 47 | + const project = resolveProjectFromEntry(input.entrySlidePath) |
| 48 | + const titles = project.data.slides.map((slide, idx) => `#${idx + 1}: ${slide.title || '(Untitled)'}`) |
| 49 | + return formatList(titles) |
| 50 | + }) |
| 51 | + |
| 52 | + // Find slide number by title |
| 53 | + registerSimpleTool('slidev_findSlideNoByTitle', (input: { entrySlidePath: string, title: string }) => { |
| 54 | + const project = resolveProjectFromEntry(input.entrySlidePath) |
| 55 | + const idx = project.data.slides.findIndex(slide => slide.title === input.title) |
| 56 | + if (idx === -1) { |
| 57 | + throw new Error(`No slide found with title: "${input.title}".`) |
| 58 | + } |
| 59 | + return formatObject({ |
| 60 | + 'Title': input.title, |
| 61 | + 'Slide number': idx + 1, |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + // List all loaded Slidev entries |
| 66 | + registerSimpleTool('slidev_listEntries', () => { |
| 67 | + const entries = [...projects.keys()] |
| 68 | + if (entries.length === 0) { |
| 69 | + return 'No loaded Slidev project entries.' |
| 70 | + } |
| 71 | + return formatList(entries) |
| 72 | + }) |
| 73 | + |
| 74 | + // Get project preview port |
| 75 | + registerSimpleTool('slidev_getPreviewPort', (input: { entrySlidePath: string }) => { |
| 76 | + const project = resolveProjectFromEntry(input.entrySlidePath) |
| 77 | + return formatObject({ |
| 78 | + 'Project entry': project.entry, |
| 79 | + 'Preview port': project.port || 'Not running', |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + // Choose active Slidev entry |
| 84 | + registerSimpleTool('slidev_chooseEntry', (input: { entrySlidePath: string }) => { |
| 85 | + if (!input.entrySlidePath) { |
| 86 | + throw new Error('entrySlidePath is required.') |
| 87 | + } |
| 88 | + const project = resolveProjectFromEntry(input.entrySlidePath) |
| 89 | + activeEntry.value = project.entry |
| 90 | + return formatObject({ |
| 91 | + 'Active entry switched to': project.entry, |
| 92 | + }) |
| 93 | + }) |
| 94 | +}) |
| 95 | + |
| 96 | +function registerSimpleTool<T>(name: string, invoke: (input: T) => string) { |
| 97 | + useDisposable(lm.registerTool<T>(name, { |
| 98 | + invoke({ input }) { |
| 99 | + try { |
| 100 | + const result = invoke(input) |
| 101 | + return new LanguageModelToolResult([ |
| 102 | + new LanguageModelTextPart(result), |
| 103 | + ]) |
| 104 | + } |
| 105 | + catch (error: any) { |
| 106 | + return new LanguageModelToolResult([ |
| 107 | + new LanguageModelTextPart(`Error: ${error.message || error.toString()}`), |
| 108 | + ]) |
| 109 | + } |
| 110 | + }, |
| 111 | + })) |
| 112 | +} |
| 113 | + |
| 114 | +function resolveProjectFromEntry(entry: string) { |
| 115 | + if (entry === '' || entry === '$ACTIVE_SLIDE_ENTRY') { |
| 116 | + if (!activeEntry.value) { |
| 117 | + throw new Error('No active slide entry found. Please set an active slide entry before using this tool.') |
| 118 | + } |
| 119 | + entry = activeEntry.value |
| 120 | + } |
| 121 | + |
| 122 | + let project = projects.get(entry) |
| 123 | + if (!project) { |
| 124 | + entry = slash(entry) |
| 125 | + const possibleProjects = [...projects.values()].filter(p => p.entry.includes(entry)) |
| 126 | + if (possibleProjects.length === 0) { |
| 127 | + throw new Error(`No project found for entry: ${entry}. All entries: ${formatList(projects.keys())}`) |
| 128 | + } |
| 129 | + else if (possibleProjects.length > 1) { |
| 130 | + throw new Error(`Multiple projects found for entry: ${entry}. Please specify the full path. All entries: ${formatList(projects.keys())}`) |
| 131 | + } |
| 132 | + else { |
| 133 | + project = possibleProjects[0] |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return project |
| 138 | +} |
| 139 | + |
| 140 | +function formatList(items: Iterable<string>): string { |
| 141 | + const itemsArray = [...items] |
| 142 | + if (itemsArray.length === 0) { |
| 143 | + return 'No items found.' |
| 144 | + } |
| 145 | + return itemsArray.map(item => `- ${item}\n`).join('') |
| 146 | +} |
| 147 | + |
| 148 | +function formatObject(obj: Record<string, string | number>): string { |
| 149 | + return Object.entries(obj) |
| 150 | + .map(([key, value]) => `- ${key}: ${value}\n`) |
| 151 | + .join('') |
| 152 | +} |
0 commit comments