|
1 | | -import { execSync } from 'child_process'; |
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
2 | 4 |
|
3 | 5 | import { anthropic } from '@ai-sdk/anthropic'; |
4 | 6 | import { mistral } from '@ai-sdk/mistral'; |
@@ -62,19 +64,86 @@ export const DEFAULT_CONFIG = { |
62 | 64 | * Gets the default system prompt with contextual information about the environment |
63 | 65 | */ |
64 | 66 | export function getDefaultSystemPrompt(toolContext: ToolContext): string { |
65 | | - // Gather context with error handling |
66 | | - const getCommandOutput = (command: string, label: string): string => { |
| 67 | + // Gather context using Node.js APIs for cross-platform compatibility |
| 68 | + const getCurrentDirectory = (): string => { |
67 | 69 | try { |
68 | | - return execSync(command).toString().trim(); |
| 70 | + return process.cwd(); |
69 | 71 | } catch (error) { |
70 | | - return `[Error getting ${label}: ${(error as Error).message}]`; |
| 72 | + return `[Error getting current directory: ${(error as Error).message}]`; |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + const getFileList = (): string => { |
| 77 | + try { |
| 78 | + const currentDir = process.cwd(); |
| 79 | + const files = fs.readdirSync(currentDir, { withFileTypes: true }); |
| 80 | + |
| 81 | + // Sort directories first, then files |
| 82 | + files.sort((a, b) => { |
| 83 | + if (a.isDirectory() && !b.isDirectory()) return -1; |
| 84 | + if (!a.isDirectory() && b.isDirectory()) return 1; |
| 85 | + return a.name.localeCompare(b.name); |
| 86 | + }); |
| 87 | + |
| 88 | + // Format similar to 'ls -la' output |
| 89 | + const fileDetails = files.map((file) => { |
| 90 | + try { |
| 91 | + const stats = fs.statSync(path.join(currentDir, file.name)); |
| 92 | + const permissions = getPermissionString(stats); |
| 93 | + const links = stats.nlink || 1; |
| 94 | + const owner = |
| 95 | + process.platform === 'win32' ? 'OWNER' : stats.uid.toString(); |
| 96 | + const group = |
| 97 | + process.platform === 'win32' ? 'GROUP' : stats.gid.toString(); |
| 98 | + const size = stats.size; |
| 99 | + const mtime = stats.mtime |
| 100 | + .toDateString() |
| 101 | + .split(' ') |
| 102 | + .slice(1) |
| 103 | + .join(' '); |
| 104 | + const type = file.isDirectory() ? 'd' : '-'; |
| 105 | + |
| 106 | + return `${type}${permissions} ${links.toString().padStart(2)} ${owner.padEnd(10)} ${group.padEnd(10)} ${size.toString().padStart(8)} ${mtime} ${file.name}${file.isDirectory() ? '/' : ''}`; |
| 107 | + } catch (err) { |
| 108 | + return `[Error getting details for ${file.name}: ${(err as Error).message}]`; |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + return `total ${files.length}\n${fileDetails.join('\n')}`; |
| 113 | + } catch (error) { |
| 114 | + return `[Error getting file listing: ${(error as Error).message}]`; |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + const getPermissionString = (stats: fs.Stats): string => { |
| 119 | + const mode = stats.mode; |
| 120 | + const permissions = [ |
| 121 | + mode & 0o400 ? 'r' : '-', |
| 122 | + mode & 0o200 ? 'w' : '-', |
| 123 | + mode & 0o100 ? 'x' : '-', |
| 124 | + mode & 0o040 ? 'r' : '-', |
| 125 | + mode & 0o020 ? 'w' : '-', |
| 126 | + mode & 0o010 ? 'x' : '-', |
| 127 | + mode & 0o004 ? 'r' : '-', |
| 128 | + mode & 0o002 ? 'w' : '-', |
| 129 | + mode & 0o001 ? 'x' : '-', |
| 130 | + ].join(''); |
| 131 | + |
| 132 | + return permissions; |
| 133 | + }; |
| 134 | + |
| 135 | + const getSystemInfo = (): string => { |
| 136 | + try { |
| 137 | + return `${os.type()} ${os.hostname()} ${os.release()} ${os.platform()} ${os.arch()}`; |
| 138 | + } catch (error) { |
| 139 | + return `[Error getting system information: ${(error as Error).message}]`; |
71 | 140 | } |
72 | 141 | }; |
73 | 142 |
|
74 | 143 | const context = { |
75 | | - pwd: getCommandOutput('pwd', 'current directory'), |
76 | | - files: getCommandOutput('ls -la', 'file listing'), |
77 | | - system: getCommandOutput('uname -a', 'system information'), |
| 144 | + pwd: getCurrentDirectory(), |
| 145 | + files: getFileList(), |
| 146 | + system: getSystemInfo(), |
78 | 147 | datetime: new Date().toString(), |
79 | 148 | githubMode: toolContext.githubMode, |
80 | 149 | }; |
|
0 commit comments