import { log } from './log';

export const util = {
  parseJson(json: string) {
    if (json) {
      try {
        return JSON.parse(json);
      } catch (error) {
        log.error('Error parsing JSON:', error);
        return null;
      }
    }
    return null;
  },

  removeThinkConetnt(content: string) {
    // const idx = content.indexOf("</think>");
    // return idx !== -1 ? content.slice(idx + 8).trim() : content.trim();
    return content.replace(/([\s\S]*?)<\/think>/, '').trim();
  },

  handlerNotFoundError(error: any) {
    // 如果是方法未找到错误(-32601)，继续处理其他模块
    if (error.code === -32601) {
      log.info('方法未找到', error);
    } else {
      // 其他错误重新抛出
      throw error;
    }
  },
};
