61 lines
2.0 KiB
JavaScript
61 lines
2.0 KiB
JavaScript
'use strict';
|
||
|
||
const os = require('os');
|
||
const path = require('path');
|
||
const { execSync } = require('child_process');
|
||
|
||
function getTerminalType() {
|
||
if (process.platform === 'win32') {
|
||
if (process.env.PSModulePath || process.env.POWERSHELL_DISTRIBUTION_CHANNEL) return 'powershell';
|
||
return 'cmd';
|
||
}
|
||
return 'terminal';
|
||
}
|
||
|
||
function getGitInfo(workspace) {
|
||
try {
|
||
const branch = execSync('git rev-parse --abbrev-ref HEAD', { cwd: workspace, stdio: ['ignore', 'pipe', 'ignore'] })
|
||
.toString()
|
||
.trim();
|
||
if (!branch) return '未初始化';
|
||
const status = execSync('git status --porcelain', { cwd: workspace, stdio: ['ignore', 'pipe', 'ignore'] })
|
||
.toString()
|
||
.trim();
|
||
const dirty = status ? 'dirty' : 'clean';
|
||
return `${branch} (${dirty})`;
|
||
} catch (_) {
|
||
return '未初始化';
|
||
}
|
||
}
|
||
|
||
function buildSystemPrompt(basePrompt, opts) {
|
||
const now = new Date();
|
||
const tzOffset = now.getTimezoneOffset();
|
||
const localMs = now.getTime() - tzOffset * 60 * 1000;
|
||
const localIso = new Date(localMs).toISOString().slice(0, 16);
|
||
const platform = os.platform();
|
||
const systemName = platform === 'darwin' ? 'macos' : platform === 'win32' ? 'windows' : 'linux';
|
||
let prompt = basePrompt;
|
||
const allowModeValue = opts.allowMode === 'read_only'
|
||
? `${opts.allowMode}\n 已禁用 edit_file、run_command。若用户要求使用,请告知当前无权限需要用户输入 /allow 切换权限。`
|
||
: opts.allowMode;
|
||
const replacements = {
|
||
current_time: localIso,
|
||
path: opts.workspace,
|
||
workspace: opts.workspace,
|
||
system: `${systemName} (${os.release()})`,
|
||
terminal: getTerminalType(),
|
||
allow_mode: allowModeValue,
|
||
permissions: allowModeValue,
|
||
git: getGitInfo(opts.workspace),
|
||
model_id: opts.modelId || '',
|
||
};
|
||
for (const [key, value] of Object.entries(replacements)) {
|
||
const token = `{${key}}`;
|
||
prompt = prompt.split(token).join(String(value));
|
||
}
|
||
return prompt.trim();
|
||
}
|
||
|
||
module.exports = { buildSystemPrompt };
|