57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
import { readConfig, saveConfig, resolveApiKey, DEFAULT_PROMPT } from './config.js';
|
|
import { streamChat } from './api.js';
|
|
import { printInfo, printError } from './output.js';
|
|
|
|
export async function setKey(args) {
|
|
if (args.length < 1) {
|
|
printError('请输入密钥');
|
|
process.exit(1);
|
|
}
|
|
const cfg = readConfig();
|
|
saveConfig({ apiKey: args[0], prompt: cfg.prompt });
|
|
printInfo('API 密钥已保存。');
|
|
}
|
|
|
|
export async function setPrompt(args) {
|
|
if (args.length < 1) {
|
|
printError('请输入新的 system prompt');
|
|
process.exit(1);
|
|
}
|
|
const cfg = readConfig();
|
|
const prompt = args.join(' ');
|
|
saveConfig({ apiKey: cfg.apiKey, prompt });
|
|
printInfo('system prompt 已更新。');
|
|
}
|
|
|
|
export function viewPrompt() {
|
|
const cfg = readConfig();
|
|
printInfo('当前 system prompt:');
|
|
printInfo(cfg.prompt || DEFAULT_PROMPT);
|
|
}
|
|
|
|
async function runChat(model, args) {
|
|
if (args.length < 1) {
|
|
printError('请输入要发送的内容');
|
|
process.exit(1);
|
|
}
|
|
const userInput = args.join(' ');
|
|
const cfg = readConfig();
|
|
const { apiKey, source } = resolveApiKey(cfg);
|
|
if (!apiKey) {
|
|
printError('未设置 API 密钥,请先运行: ds k <key> 或设置环境变量 DEEPSEEK_API_KEY');
|
|
process.exit(1);
|
|
}
|
|
if (source === 'env') {
|
|
printInfo('使用环境变量 DEEPSEEK_API_KEY');
|
|
}
|
|
await streamChat({ model, apiKey, prompt: cfg.prompt, userInput });
|
|
}
|
|
|
|
export async function chat(args) {
|
|
await runChat('deepseek-chat', args);
|
|
}
|
|
|
|
export async function reason(args) {
|
|
await runChat('deepseek-reasoner', args);
|
|
}
|