here
This commit is contained in:
+36
-9
@@ -10,13 +10,17 @@ class Ai{
|
||||
this.bot = args.bot;
|
||||
this.promptName = args.promptName;
|
||||
this.prompCustom = args.prompCustom || '';
|
||||
this.intervalLength = args.intervalLength || 30;
|
||||
// interval takes precedence over intervalLength (both are valid config names)
|
||||
this.intervalLength = args.interval || args.intervalLength || 30;
|
||||
this.intervalStop;
|
||||
this.messageListener;
|
||||
this.provider = null;
|
||||
|
||||
// Bot-specific AI config (overrides global config)
|
||||
this.botConfig = args.botConfig || {};
|
||||
// When loaded via config, args contains provider, model, baseUrl, etc. directly
|
||||
// When loaded via /ai command, only promptName/prompCustom are passed
|
||||
const { bot, promptName, prompCustom, intervalLength, interval, ...configProps } = args;
|
||||
this.botConfig = args.botConfig || configProps || {};
|
||||
}
|
||||
|
||||
// Get merged config: bot-specific settings override global settings
|
||||
@@ -58,14 +62,31 @@ class Ai{
|
||||
|
||||
try{
|
||||
messages = [''];
|
||||
if(!this.provider.getResponse(result)) return;
|
||||
const responseText = this.provider.getResponse(result);
|
||||
if(!responseText) return;
|
||||
|
||||
for(let message of JSON.parse(this.provider.getResponse(result))){
|
||||
console.log('toSay', message.delay, message.text);
|
||||
if(message.text === '___') return;
|
||||
setTimeout(async (message)=>{
|
||||
await this.bot.sayAiSafe(message.text);
|
||||
}, message.delay*1000, message);
|
||||
// Try to parse JSON response
|
||||
try {
|
||||
const parsed = JSON.parse(responseText);
|
||||
if(Array.isArray(parsed)){
|
||||
for(let message of parsed){
|
||||
console.log('toSay', message.delay, message.text);
|
||||
if(message.text.trim().startsWith('_')) return;
|
||||
setTimeout(async (message)=>{
|
||||
await this.bot.sayAiSafe(message.text);
|
||||
}, 0*1000, message);
|
||||
}
|
||||
} else {
|
||||
throw new Error('Response is not an array');
|
||||
}
|
||||
} catch(jsonError){
|
||||
// JSON parsing failed, treat as plain text
|
||||
console.log('JSON parse failed, treating as plain text:', responseText.substring(0, 100));
|
||||
// Skip empty responses, underscore signals, and single dash signals
|
||||
const text = responseText.trim();
|
||||
if(text && text !== '___' && !text.match(/^[-_]+$/)){
|
||||
await this.bot.sayAiSafe(text);
|
||||
}
|
||||
}
|
||||
}catch(error){
|
||||
console.log('Error in AI message loop', error, result);
|
||||
@@ -108,6 +129,8 @@ class Ai{
|
||||
model: config.model,
|
||||
promptName: this.promptName,
|
||||
baseUrl: config.baseUrl,
|
||||
maxOutputTokens: config.maxOutputTokens,
|
||||
interval: config.interval,
|
||||
});
|
||||
|
||||
const prompt = conf.ai.prompts[this.promptName](
|
||||
@@ -142,4 +165,8 @@ class Ai{
|
||||
|
||||
|
||||
|
||||
const AiWeb = require('./ai/web');
|
||||
Ai.createRouter = AiWeb.createRouter;
|
||||
Ai.webUI = AiWeb.webUI;
|
||||
|
||||
module.exports = Ai;
|
||||
Reference in New Issue
Block a user