聊天报错

This commit is contained in:
2025-09-21 19:41:49 +08:00
parent 35aa92d78a
commit 274f02b867
7 changed files with 243 additions and 221 deletions
@@ -27,6 +27,8 @@ export class ApiConfig {
private static _instance: ApiConfig;
private config: AIConfig;
private baseUrl: "https://generativelanguage.googleapis.com";
private constructor() {
this.initConfig();
}
@@ -104,6 +106,9 @@ export class ApiConfig {
public getTimeout() {
return this.config.timeout;
}
public getBaseUrl() {
return this.baseUrl;
}
/**
* 验证配置是否有效
+21 -4
View File
@@ -1,6 +1,11 @@
// 首先加载 polyfills 以确保兼容性
import "../utils/polyfills";
import { GoogleGenAI, HarmBlockThreshold, HarmCategory } from "@google/genai";
import {
Chat,
GoogleGenAI,
HarmBlockThreshold,
HarmCategory,
} from "@google/genai";
import { RoleConfigLoader } from "./RoleConfigLoader";
import { ChatHistoryManager } from "../manager/ChatHistoryManager";
import { ApiConfig } from "./ApiConfigLoader";
@@ -57,8 +62,16 @@ export class ChatAIService {
try {
this.ai = new GoogleGenAI({
apiKey: config.apiKey,
httpOptions: { timeout: ApiConfig.Instance.getTimeout() },
httpOptions: {
timeout: ApiConfig.Instance.getTimeout(),
baseUrl: ApiConfig.Instance.getBaseUrl(),
},
});
console.log("-------AI 启动成功-------");
console.log(this.ai);
this.ai.operations;
} catch (error) {
TipsPanel.show(LanguageUtils.getText("chat_error_code_2001"));
ErrorHandler.Instance.handleError(
@@ -88,7 +101,7 @@ export class ChatAIService {
* 创建或获取指定角色的聊天实例
* @param roleId 角色ID
*/
async createOrGetChat(roleId: number): Promise<any> {
async createOrGetChat(roleId: number): Promise<Chat> {
if (!this.chatInstances.has(roleId)) {
const systemInstruction = RoleConfigLoader.getRoleInstruction(roleId);
const config = ApiConfig.Instance.getAIConfig();
@@ -97,7 +110,7 @@ export class ChatAIService {
let savedHistory = await ChatHistoryManager.Instance.loadChatHistory(
roleId
);
let chat;
let chat: Chat;
if (savedHistory && savedHistory.length > 0) {
chat = this.ai.chats.create({
model: config.model,
@@ -130,6 +143,7 @@ export class ChatAIService {
},
history: savedHistory,
});
console.log(chat);
console.log(
`Loaded ${savedHistory.length} history messages for role ${roleId}`
);
@@ -164,6 +178,8 @@ export class ChatAIService {
],
},
});
console.log(chat);
console.log(`Created new chat instance for role ${roleId}`);
}
@@ -226,6 +242,7 @@ export class ChatAIService {
try {
const chat = await this.createOrGetChat(roleId);
const response = await chat.sendMessage({
message: message.trim(),
});