fixes for invalid json response
This commit is contained in:
@@ -21,6 +21,9 @@ export const redis = createClient({
|
||||
url: `redis://${Keys.redisHost}:${Keys.redisPort}`,
|
||||
})
|
||||
|
||||
redis.on('error', err => console.log(`Redis error: ${err}`));
|
||||
redis.connect();
|
||||
|
||||
// Initialize Ollama connection
|
||||
export const ollama = new Ollama({
|
||||
host: `http://${Keys.ipAddress}:${Keys.portAddress}`,
|
||||
|
||||
@@ -4,10 +4,10 @@ import {
|
||||
getChannelInfo, getServerConfig, getUserConfig, openChannelInfo,
|
||||
openConfig, UserConfig, getAttachmentData, getTextFileAttachmentData
|
||||
} from '../utils/index.js'
|
||||
import { redis } from '../client.js' // Fixed import: added .ts extension
|
||||
|
||||
import { redis } from '../client.js'
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
// Define interface for model response to improve type safety
|
||||
interface ModelResponse {
|
||||
@@ -160,7 +160,11 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
|
||||
// Load personality
|
||||
let personality: string
|
||||
try {
|
||||
const personalityData = await fs.readFile(path.join(__dirname, '../../personality.json'), 'utf-8')
|
||||
// Fix __dirname for ESM by using import.meta.url
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = path.dirname(__filename)
|
||||
const personalityPath = path.join(__dirname, '../../personality.json')
|
||||
const personalityData = await fs.readFile(personalityPath, 'utf-8')
|
||||
const personalityJson = JSON.parse(personalityData)
|
||||
personality = personalityJson.character || 'You are a friendly and helpful AI assistant.'
|
||||
} catch (error) {
|
||||
@@ -229,11 +233,17 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
|
||||
// Parse JSON response
|
||||
let jsonResponse: ModelResponse
|
||||
try {
|
||||
jsonResponse = JSON.parse(response.message.content)
|
||||
// Log raw response for debugging
|
||||
log(`Raw model response: ${response.message.content}`)
|
||||
// Strip Markdown code fences if present
|
||||
let content = response.message.content
|
||||
content = content.replace(/^```json\n|```$/g, '').trim()
|
||||
jsonResponse = JSON.parse(content)
|
||||
if (!jsonResponse.status || !jsonResponse.reply) {
|
||||
throw new Error('Missing status or reply in model response')
|
||||
}
|
||||
} catch (error) {
|
||||
log(`Failed to parse model response: ${error}`)
|
||||
throw new Error(`Invalid JSON response from model: ${error}`)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user