|
| 1 | +import * as fs from 'node:fs' |
| 2 | +import * as path from 'node:path' |
| 3 | +import { type ContextItem, ContextItemSource } from '@sourcegraph/cody-shared' |
| 4 | +import type { ContextItemWorkflows } from '@sourcegraph/cody-shared/src/codebase-context/messages' |
| 5 | +import { WORKFLOW_PROVIDER } from '@sourcegraph/cody-shared/src/mentions/api' |
| 6 | +import * as vscode from 'vscode' |
| 7 | +import { URI } from 'vscode-uri' |
| 8 | +import { CODY_WORKFLOWS_DIR } from '../../workflow/workflow-io' |
| 9 | + |
| 10 | +export function getWorkflowForMention(query: string): ContextItem[] { |
| 11 | + const workspaceFolders = vscode.workspace.workspaceFolders |
| 12 | + if (!workspaceFolders || workspaceFolders.length === 0) { |
| 13 | + return [] |
| 14 | + } |
| 15 | + |
| 16 | + const customLinks: Array<{ name: string; url: string; description: string }> = [] |
| 17 | + |
| 18 | + // Scan all workspace folders for workflows |
| 19 | + for (const folder of workspaceFolders) { |
| 20 | + const workflowsDir = path.join(folder.uri.path, CODY_WORKFLOWS_DIR) |
| 21 | + |
| 22 | + // Check if the workflows directory exists |
| 23 | + if (fs.existsSync(workflowsDir)) { |
| 24 | + try { |
| 25 | + const files = fs.readdirSync(workflowsDir) |
| 26 | + |
| 27 | + // Filter for JSON files |
| 28 | + const jsonFiles = files.filter(file => file.endsWith('.json')) |
| 29 | + |
| 30 | + // Create a link for each JSON file |
| 31 | + for (const file of jsonFiles) { |
| 32 | + const filePath = path.join(workflowsDir, file) |
| 33 | + const fileUri = vscode.Uri.file(filePath) |
| 34 | + const fileName = path.basename(file, '.json') |
| 35 | + |
| 36 | + customLinks.push({ |
| 37 | + name: fileName, |
| 38 | + url: fileUri.toString(), |
| 39 | + description: 'Workflow', |
| 40 | + }) |
| 41 | + } |
| 42 | + } catch (error) { |
| 43 | + console.error('Error reading workflows directory:', error) |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Only filter links if a non-empty query is provided |
| 49 | + let linksToReturn = customLinks |
| 50 | + if (query && query.trim().length > 0) { |
| 51 | + linksToReturn = customLinks.filter( |
| 52 | + link => |
| 53 | + link.name.toLowerCase().includes(query.toLowerCase()) || |
| 54 | + link.description.toLowerCase().includes(query.toLowerCase()) |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + // Sort the links alphabetically by name |
| 59 | + linksToReturn.sort((a, b) => a.name.localeCompare(b.name)) |
| 60 | + |
| 61 | + // Convert to ContextItems |
| 62 | + return linksToReturn.map( |
| 63 | + link => |
| 64 | + ({ |
| 65 | + type: 'workflows', |
| 66 | + uri: URI.parse(link.url), |
| 67 | + name: link.name, // Required property based on similar interfaces |
| 68 | + title: link.name, |
| 69 | + description: link.description, |
| 70 | + content: 'Workflow', |
| 71 | + source: ContextItemSource.User, |
| 72 | + provider: WORKFLOW_PROVIDER.id, |
| 73 | + }) as ContextItemWorkflows |
| 74 | + ) |
| 75 | +} |
0 commit comments