fixes for invalid json response

This commit is contained in:
2025-05-18 15:50:51 -04:00
parent 9dae5c0001
commit 6ab0edb5d6
2 changed files with 17 additions and 4 deletions

View File

@@ -21,6 +21,9 @@ export const redis = createClient({
url: `redis://${Keys.redisHost}:${Keys.redisPort}`, url: `redis://${Keys.redisHost}:${Keys.redisPort}`,
}) })
redis.on('error', err => console.log(`Redis error: ${err}`));
redis.connect();
// Initialize Ollama connection // Initialize Ollama connection
export const ollama = new Ollama({ export const ollama = new Ollama({
host: `http://${Keys.ipAddress}:${Keys.portAddress}`, host: `http://${Keys.ipAddress}:${Keys.portAddress}`,

View File

@@ -4,10 +4,10 @@ import {
getChannelInfo, getServerConfig, getUserConfig, openChannelInfo, getChannelInfo, getServerConfig, getUserConfig, openChannelInfo,
openConfig, UserConfig, getAttachmentData, getTextFileAttachmentData openConfig, UserConfig, getAttachmentData, getTextFileAttachmentData
} from '../utils/index.js' } 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 fs from 'fs/promises'
import path from 'path' import path from 'path'
import { fileURLToPath } from 'url'
// Define interface for model response to improve type safety // Define interface for model response to improve type safety
interface ModelResponse { interface ModelResponse {
@@ -160,7 +160,11 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
// Load personality // Load personality
let personality: string let personality: string
try { 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) const personalityJson = JSON.parse(personalityData)
personality = personalityJson.character || 'You are a friendly and helpful AI assistant.' personality = personalityJson.character || 'You are a friendly and helpful AI assistant.'
} catch (error) { } catch (error) {
@@ -229,11 +233,17 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
// Parse JSON response // Parse JSON response
let jsonResponse: ModelResponse let jsonResponse: ModelResponse
try { 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) { if (!jsonResponse.status || !jsonResponse.reply) {
throw new Error('Missing status or reply in model response') throw new Error('Missing status or reply in model response')
} }
} catch (error) { } catch (error) {
log(`Failed to parse model response: ${error}`)
throw new Error(`Invalid JSON response from model: ${error}`) throw new Error(`Invalid JSON response from model: ${error}`)
} }