snow-ai 0.3.32 → 0.3.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import { homedir } from 'os';
2
2
  import { join } from 'path';
3
- import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
3
+ import { readFileSync, writeFileSync, existsSync, mkdirSync, unlinkSync, } from 'fs';
4
4
  const DEFAULT_CONFIG = {
5
5
  snowcfg: {
6
6
  baseUrl: 'https://api.openai.com/v1',
@@ -21,7 +21,8 @@ const DEFAULT_MCP_CONFIG = {
21
21
  mcpServers: {},
22
22
  };
23
23
  const CONFIG_DIR = join(homedir(), '.snow');
24
- const SYSTEM_PROMPT_FILE = join(CONFIG_DIR, 'system-prompt.txt');
24
+ const SYSTEM_PROMPT_FILE = join(CONFIG_DIR, 'system-prompt.txt'); // 旧版本,保留用于迁移
25
+ const SYSTEM_PROMPT_JSON_FILE = join(CONFIG_DIR, 'system-prompt.json'); // 新版本
25
26
  const CUSTOM_HEADERS_FILE = join(CONFIG_DIR, 'custom-headers.json');
26
27
  function normalizeRequestMethod(method) {
27
28
  if (method === 'chat' ||
@@ -232,28 +233,92 @@ export function validateMCPConfig(config) {
232
233
  return errors;
233
234
  }
234
235
  /**
235
- * 读取自定义系统提示词
236
- * 如果 system-prompt.txt 文件存在且不为空,返回其内容
237
- * 否则返回 undefined (使用默认系统提示词)
236
+ * 从旧版本 system-prompt.txt 迁移到新版本 system-prompt.json
238
237
  */
239
- export function getCustomSystemPrompt() {
240
- ensureConfigDirectory();
238
+ function migrateSystemPromptFromTxt() {
241
239
  if (!existsSync(SYSTEM_PROMPT_FILE)) {
240
+ return;
241
+ }
242
+ try {
243
+ const txtContent = readFileSync(SYSTEM_PROMPT_FILE, 'utf8');
244
+ if (txtContent.trim().length === 0) {
245
+ return;
246
+ }
247
+ // 创建默认配置,将旧内容作为默认项
248
+ const config = {
249
+ active: 'default',
250
+ prompts: [
251
+ {
252
+ id: 'default',
253
+ name: 'Default',
254
+ content: txtContent,
255
+ createdAt: new Date().toISOString(),
256
+ },
257
+ ],
258
+ };
259
+ // 保存到新文件
260
+ writeFileSync(SYSTEM_PROMPT_JSON_FILE, JSON.stringify(config, null, 2), 'utf8');
261
+ // 删除旧文件
262
+ unlinkSync(SYSTEM_PROMPT_FILE);
263
+ // console.log('✅ Migrated system prompt from txt to json format.');
264
+ }
265
+ catch (error) {
266
+ console.error('Failed to migrate system prompt:', error);
267
+ }
268
+ }
269
+ /**
270
+ * 读取系统提示词配置
271
+ */
272
+ export function getSystemPromptConfig() {
273
+ ensureConfigDirectory();
274
+ // 先尝试迁移旧版本
275
+ if (existsSync(SYSTEM_PROMPT_FILE) && !existsSync(SYSTEM_PROMPT_JSON_FILE)) {
276
+ migrateSystemPromptFromTxt();
277
+ }
278
+ // 读取 JSON 配置
279
+ if (!existsSync(SYSTEM_PROMPT_JSON_FILE)) {
242
280
  return undefined;
243
281
  }
244
282
  try {
245
- const content = readFileSync(SYSTEM_PROMPT_FILE, 'utf8');
246
- // 只有当文件完全为空时才返回 undefined
247
- if (content.length === 0) {
283
+ const content = readFileSync(SYSTEM_PROMPT_JSON_FILE, 'utf8');
284
+ if (content.trim().length === 0) {
248
285
  return undefined;
249
286
  }
250
- // 返回原始内容,不做任何处理
251
- return content;
287
+ const config = JSON.parse(content);
288
+ return config;
252
289
  }
253
- catch {
290
+ catch (error) {
291
+ console.error('Failed to read system prompt config:', error);
254
292
  return undefined;
255
293
  }
256
294
  }
295
+ /**
296
+ * 保存系统提示词配置
297
+ */
298
+ export function saveSystemPromptConfig(config) {
299
+ ensureConfigDirectory();
300
+ try {
301
+ writeFileSync(SYSTEM_PROMPT_JSON_FILE, JSON.stringify(config, null, 2), 'utf8');
302
+ }
303
+ catch (error) {
304
+ console.error('Failed to save system prompt config:', error);
305
+ throw error;
306
+ }
307
+ }
308
+ /**
309
+ * 读取自定义系统提示词(当前激活的)
310
+ * 兼容旧版本 system-prompt.txt
311
+ * 新版本从 system-prompt.json 读取当前激活的提示词
312
+ */
313
+ export function getCustomSystemPrompt() {
314
+ const config = getSystemPromptConfig();
315
+ if (!config || !config.active) {
316
+ return undefined;
317
+ }
318
+ // 查找当前激活的提示词
319
+ const activePrompt = config.prompts.find(p => p.id === config.active);
320
+ return activePrompt?.content;
321
+ }
257
322
  /**
258
323
  * 读取自定义请求头配置
259
324
  * 如果 custom-headers.json 文件存在且有效,返回其内容
@@ -261,33 +326,17 @@ export function getCustomSystemPrompt() {
261
326
  */
262
327
  export function getCustomHeaders() {
263
328
  ensureConfigDirectory();
264
- if (!existsSync(CUSTOM_HEADERS_FILE)) {
265
- return {};
266
- }
267
- try {
268
- const content = readFileSync(CUSTOM_HEADERS_FILE, 'utf8');
269
- const headers = JSON.parse(content);
270
- // 验证格式:必须是对象,且所有值都是字符串
271
- if (typeof headers !== 'object' ||
272
- headers === null ||
273
- Array.isArray(headers)) {
274
- return {};
275
- }
276
- // 过滤掉非字符串的值
277
- const validHeaders = {};
278
- for (const [key, value] of Object.entries(headers)) {
279
- if (typeof value === 'string') {
280
- validHeaders[key] = value;
281
- }
282
- }
283
- return validHeaders;
284
- }
285
- catch {
329
+ const config = getCustomHeadersConfig();
330
+ if (!config || !config.active) {
286
331
  return {};
287
332
  }
333
+ // 查找当前激活的方案
334
+ const activeScheme = config.schemes.find(s => s.id === config.active);
335
+ return activeScheme?.headers || {};
288
336
  }
289
337
  /**
290
338
  * 保存自定义请求头配置
339
+ * @deprecated 使用 saveCustomHeadersConfig 替代
291
340
  */
292
341
  export function saveCustomHeaders(headers) {
293
342
  ensureConfigDirectory();
@@ -306,3 +355,69 @@ export function saveCustomHeaders(headers) {
306
355
  throw new Error(`Failed to save custom headers: ${error}`);
307
356
  }
308
357
  }
358
+ /**
359
+ * 获取自定义请求头配置(多方案)
360
+ */
361
+ export function getCustomHeadersConfig() {
362
+ ensureConfigDirectory();
363
+ if (!existsSync(CUSTOM_HEADERS_FILE)) {
364
+ return null;
365
+ }
366
+ try {
367
+ const content = readFileSync(CUSTOM_HEADERS_FILE, 'utf8');
368
+ const data = JSON.parse(content);
369
+ // 兼容旧版本格式 (直接是 Record<string, string>)
370
+ if (typeof data === 'object' &&
371
+ data !== null &&
372
+ !Array.isArray(data) &&
373
+ !('active' in data) &&
374
+ !('schemes' in data)) {
375
+ // 旧格式:转换为新格式
376
+ const headers = {};
377
+ for (const [key, value] of Object.entries(data)) {
378
+ if (typeof value === 'string') {
379
+ headers[key] = value;
380
+ }
381
+ }
382
+ if (Object.keys(headers).length > 0) {
383
+ // 创建默认方案
384
+ const defaultScheme = {
385
+ id: Date.now().toString(),
386
+ name: 'Default Headers',
387
+ headers,
388
+ createdAt: new Date().toISOString(),
389
+ };
390
+ return {
391
+ active: defaultScheme.id,
392
+ schemes: [defaultScheme],
393
+ };
394
+ }
395
+ return null;
396
+ }
397
+ // 新格式:验证结构
398
+ if (typeof data === 'object' &&
399
+ data !== null &&
400
+ 'active' in data &&
401
+ 'schemes' in data &&
402
+ Array.isArray(data.schemes)) {
403
+ return data;
404
+ }
405
+ return null;
406
+ }
407
+ catch {
408
+ return null;
409
+ }
410
+ }
411
+ /**
412
+ * 保存自定义请求头配置(多方案)
413
+ */
414
+ export function saveCustomHeadersConfig(config) {
415
+ ensureConfigDirectory();
416
+ try {
417
+ const content = JSON.stringify(config, null, 2);
418
+ writeFileSync(CUSTOM_HEADERS_FILE, content, 'utf8');
419
+ }
420
+ catch (error) {
421
+ throw new Error(`Failed to save custom headers config: ${error}`);
422
+ }
423
+ }
@@ -445,7 +445,8 @@ export async function getMCPServicesInfo() {
445
445
  if (!isCacheValid()) {
446
446
  await refreshToolsCache();
447
447
  }
448
- return toolsCache.servicesInfo;
448
+ // Ensure toolsCache is not null before accessing
449
+ return toolsCache?.servicesInfo || [];
449
450
  }
450
451
  /**
451
452
  * Quick probe of MCP service tools without maintaining connections
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "snow-ai",
3
- "version": "0.3.32",
3
+ "version": "0.3.34",
4
4
  "description": "Intelligent Command Line Assistant powered by AI",
5
5
  "license": "MIT",
6
6
  "bin": {