chore(cleanup): remove tool-calling plumbing, no skills remain
Remove the general-purpose tool/function-calling machinery from openrouter.ts (loadSkills usage, buildTools, executeTool, tool_calls request/response handling) and the recovery helpers it existed to patch (extractFakeToolCall/extractBareJsonRetry in toolCallRecovery.ts), since every skill that plumbing could call has now been deleted. Delete src/main/skills/ entirely (types.ts, argUtils.ts, index.ts), now empty. Strip the system prompt paragraphs describing tool usage. getAssistantReply() keeps the core chat loop: send the user's message to OpenRouter with DeepSeek/Qwen fallback, the malformed-response guard, degenerate-text detection and requireLoggedIn(), and return the assistant's plain-text reply. Drop the now-stale coverage exclude for the already-removed memory/store.ts. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
acdd8a08d3
commit
d0c730fa6f
@@ -1,25 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { asString } from '../../src/main/skills/argUtils'
|
||||
|
||||
describe('asString()', () => {
|
||||
it('should return the value unchanged when it is already a string', () => {
|
||||
expect(asString('bonjour')).toBe('bonjour')
|
||||
})
|
||||
|
||||
it('should return the empty string by default when the value is not a string', () => {
|
||||
expect(asString(undefined)).toBe('')
|
||||
expect(asString(null)).toBe('')
|
||||
expect(asString(42)).toBe('')
|
||||
expect(asString({ foo: 'bar' })).toBe('')
|
||||
})
|
||||
|
||||
it('should return the provided fallback when the value is not a string', () => {
|
||||
expect(asString(undefined, 'module')).toBe('module')
|
||||
expect(asString(123, 'defaut')).toBe('defaut')
|
||||
})
|
||||
|
||||
it('should never coerce a non-string value via toString (no "[object Object]" leak)', () => {
|
||||
const result = asString({ toString: () => 'should not appear' })
|
||||
expect(result).toBe('')
|
||||
})
|
||||
})
|
||||
@@ -3,26 +3,6 @@ import { setCurrentUserEmail, clearSession } from '../../src/main/auth/session'
|
||||
|
||||
vi.mock('@electron-toolkit/utils', () => ({ is: { dev: false } }))
|
||||
|
||||
interface FakeSkill {
|
||||
name: string
|
||||
description: string
|
||||
parameters: object
|
||||
execute: (args: Record<string, unknown>) => Promise<string>
|
||||
}
|
||||
|
||||
let currentSkills: FakeSkill[] = []
|
||||
const loadSkills = vi.fn(() => currentSkills)
|
||||
vi.mock('../../src/main/skills', () => ({ loadSkills }))
|
||||
|
||||
const getMood = vi.fn(() => 0)
|
||||
vi.mock('../../src/main/memory/store', () => ({ getMood }))
|
||||
|
||||
const synthesizeSpeech = vi.fn(async () => Buffer.from([1, 2, 3]))
|
||||
vi.mock('../../src/main/ai/elevenlabs', () => ({ synthesizeSpeech }))
|
||||
|
||||
const sendToRenderer = vi.fn()
|
||||
vi.mock('../../src/main/windowBridge', () => ({ sendToRenderer }))
|
||||
|
||||
const { getAssistantReply } = await import('../../src/main/ai/openrouter')
|
||||
|
||||
function okResponse(body: unknown): Response {
|
||||
@@ -33,27 +13,10 @@ function textMessage(content: string): unknown {
|
||||
return { choices: [{ message: { role: 'assistant', content } }] }
|
||||
}
|
||||
|
||||
function toolCallMessage(name: string, args: Record<string, unknown>, content = ''): unknown {
|
||||
return {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content,
|
||||
tool_calls: [
|
||||
{ id: 'call-1', type: 'function', function: { name, arguments: JSON.stringify(args) } }
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
describe('getAssistantReply()', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
vi.stubGlobal('fetch', vi.fn())
|
||||
currentSkills = []
|
||||
process.env.OPENROUTER_API_KEY = 'test-key'
|
||||
setCurrentUserEmail('test@example.com')
|
||||
})
|
||||
@@ -67,51 +30,12 @@ describe('getAssistantReply()', () => {
|
||||
await expect(getAssistantReply('Bonjour')).rejects.toThrow(/OPENROUTER_API_KEY/)
|
||||
})
|
||||
|
||||
it('should return a direct text reply when the model calls no tool', async () => {
|
||||
it('should return the assistant text reply when the API responds successfully', async () => {
|
||||
vi.mocked(fetch).mockResolvedValue(okResponse(textMessage('Bonjour, comment puis-je aider ?')))
|
||||
const reply = await getAssistantReply('Salut Max')
|
||||
expect(reply).toBe('Bonjour, comment puis-je aider ?')
|
||||
})
|
||||
|
||||
it('should execute a requested tool and use its result for the follow-up reply', async () => {
|
||||
const execute = vi.fn(async () => 'Notepad ouvert.')
|
||||
currentSkills = [
|
||||
{ name: 'open_notepad', description: 'ouvre notepad', parameters: {}, execute }
|
||||
]
|
||||
vi.mocked(fetch)
|
||||
.mockResolvedValueOnce(okResponse(toolCallMessage('open_notepad', {})))
|
||||
.mockResolvedValueOnce(okResponse(textMessage("C'est fait, le bloc-notes est ouvert.")))
|
||||
|
||||
const reply = await getAssistantReply('ouvre le bloc-notes')
|
||||
expect(execute).toHaveBeenCalled()
|
||||
expect(reply).toBe("C'est fait, le bloc-notes est ouvert.")
|
||||
})
|
||||
|
||||
it('should report an unknown/unauthorized tool without crashing', async () => {
|
||||
currentSkills = []
|
||||
vi.mocked(fetch)
|
||||
.mockResolvedValueOnce(okResponse(toolCallMessage('outil_interdit', {})))
|
||||
.mockResolvedValueOnce(okResponse(textMessage('Compris.')))
|
||||
|
||||
const reply = await getAssistantReply('fais un truc interdit')
|
||||
expect(reply).toBe('Compris.')
|
||||
})
|
||||
|
||||
it('should report a tool execution failure without crashing the turn', async () => {
|
||||
const execute = vi.fn(async () => {
|
||||
throw new Error('disque plein')
|
||||
})
|
||||
currentSkills = [
|
||||
{ name: 'create_file', description: 'cree un fichier', parameters: {}, execute }
|
||||
]
|
||||
vi.mocked(fetch)
|
||||
.mockResolvedValueOnce(okResponse(toolCallMessage('create_file', { name: 'test' })))
|
||||
.mockResolvedValueOnce(okResponse(textMessage('Echec note.')))
|
||||
|
||||
const reply = await getAssistantReply('cree un fichier')
|
||||
expect(reply).toBe('Echec note.')
|
||||
})
|
||||
|
||||
it('should transparently fall back to the secondary model when the primary one errors out', async () => {
|
||||
vi.mocked(fetch)
|
||||
.mockResolvedValueOnce({
|
||||
@@ -144,20 +68,6 @@ describe('getAssistantReply()', () => {
|
||||
await expect(getAssistantReply('question')).rejects.toThrow(/Aucun modele disponible/)
|
||||
})
|
||||
|
||||
it('should recover a fake tool call written as plain text instead of real tool_calls', async () => {
|
||||
const execute = vi.fn(async () => 'Notepad ouvert.')
|
||||
currentSkills = [
|
||||
{ name: 'open_notepad', description: 'ouvre notepad', parameters: {}, execute }
|
||||
]
|
||||
vi.mocked(fetch)
|
||||
.mockResolvedValueOnce(okResponse(textMessage('open_notepad\n{"text": "bonjour"}')))
|
||||
.mockResolvedValueOnce(okResponse(textMessage('Fait.')))
|
||||
|
||||
const reply = await getAssistantReply('ouvre le bloc-notes')
|
||||
expect(execute).toHaveBeenCalled()
|
||||
expect(reply).toBe('Fait.')
|
||||
})
|
||||
|
||||
it('should replace a leaked English boilerplate reply with the safe fallback message', async () => {
|
||||
vi.mocked(fetch).mockResolvedValue(
|
||||
okResponse(
|
||||
@@ -177,12 +87,4 @@ describe('getAssistantReply()', () => {
|
||||
const reply = await getAssistantReply('question')
|
||||
expect(reply).toMatch(/bien avance avec toi/)
|
||||
})
|
||||
|
||||
it('should throw once the tool-call round limit is exceeded', async () => {
|
||||
const execute = vi.fn(async () => 'ok')
|
||||
currentSkills = [{ name: 'loop_tool', description: 'boucle', parameters: {}, execute }]
|
||||
vi.mocked(fetch).mockImplementation(async () => okResponse(toolCallMessage('loop_tool', {})))
|
||||
|
||||
await expect(getAssistantReply('boucle indefiniment')).rejects.toThrow(/Trop d'appels d'outils/)
|
||||
}, 20000)
|
||||
})
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { mkdtempSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
|
||||
const tempDir = mkdtempSync(join(tmpdir(), 'forge-avatar-skillsindex-test-'))
|
||||
|
||||
vi.mock('electron', () => ({
|
||||
app: { getPath: () => tempDir },
|
||||
shell: { openPath: vi.fn(), openExternal: vi.fn() },
|
||||
clipboard: { readText: vi.fn(), writeText: vi.fn() },
|
||||
Notification: class {
|
||||
show = vi.fn()
|
||||
},
|
||||
desktopCapturer: { getSources: vi.fn(async () => []) },
|
||||
screen: { getPrimaryDisplay: () => ({ size: { width: 0, height: 0 } }) },
|
||||
BrowserWindow: class {
|
||||
static getAllWindows(): unknown[] {
|
||||
return []
|
||||
}
|
||||
on = vi.fn()
|
||||
webContents = { on: vi.fn(), send: vi.fn(), isLoading: () => false, once: vi.fn() }
|
||||
loadURL = vi.fn()
|
||||
loadFile = vi.fn()
|
||||
setAlwaysOnTop = vi.fn()
|
||||
maximize = vi.fn()
|
||||
},
|
||||
ipcMain: { on: vi.fn(), handle: vi.fn() },
|
||||
session: { defaultSession: { webRequest: { onHeadersReceived: vi.fn() } } }
|
||||
}))
|
||||
|
||||
// @electron-toolkit/utils fait lui-meme `require('electron')` en interne : hors runtime Electron,
|
||||
// le vrai paquet 'electron' exporte juste une chaine (chemin du binaire), ce qui casse l'interop
|
||||
// ESM/CJS pour tout import nomme ulterieur de 'electron' dans le graphe de modules. On le mocke
|
||||
// directement pour ne jamais toucher le vrai paquet.
|
||||
vi.mock('@electron-toolkit/utils', () => ({
|
||||
is: { dev: false },
|
||||
electronApp: { setAppUserModelId: vi.fn() },
|
||||
optimizer: { watchWindowShortcuts: vi.fn() }
|
||||
}))
|
||||
|
||||
const { loadSkills, findSkill } = await import('../../src/main/skills')
|
||||
|
||||
describe('loadSkills()', () => {
|
||||
it('should load a non-empty list of skills without throwing', () => {
|
||||
const skills = loadSkills()
|
||||
expect(skills.length).toBeGreaterThan(5)
|
||||
})
|
||||
|
||||
it('should not register the same skill name twice', () => {
|
||||
const names = loadSkills().map((s) => s.name)
|
||||
expect(new Set(names).size).toBe(names.length)
|
||||
})
|
||||
|
||||
it('should include a transversal skill', () => {
|
||||
expect(loadSkills().map((s) => s.name)).toContain('search_web')
|
||||
})
|
||||
})
|
||||
|
||||
describe('findSkill()', () => {
|
||||
it('should find a registered skill by name', () => {
|
||||
expect(findSkill('search_web')?.name).toBe('search_web')
|
||||
})
|
||||
|
||||
it('should return undefined for an unknown skill name', () => {
|
||||
expect(findSkill('inexistant')).toBeUndefined()
|
||||
})
|
||||
})
|
||||
@@ -1,42 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { extractFakeToolCall, extractBareJsonRetry } from '../../src/main/ai/toolCallRecovery'
|
||||
|
||||
describe('extractFakeToolCall()', () => {
|
||||
it('should recover a tool call written as plain text with a known tool name', () => {
|
||||
const content = 'generate_course_plan\n{"subject": "algebre", "objective": "resoudre"}'
|
||||
const result = extractFakeToolCall(content, ['generate_course_plan', 'research_topic'])
|
||||
expect(result).not.toBeNull()
|
||||
expect(result?.name).toBe('generate_course_plan')
|
||||
expect(JSON.parse(result?.arguments ?? '{}')).toEqual({
|
||||
subject: 'algebre',
|
||||
objective: 'resoudre'
|
||||
})
|
||||
})
|
||||
|
||||
it('should return null when no known tool name appears in the text', () => {
|
||||
const content = 'Je vais y reflechir et te revenir avec une reponse.'
|
||||
expect(extractFakeToolCall(content, ['generate_course_plan'])).toBeNull()
|
||||
})
|
||||
|
||||
it('should return null when the tool name is present but the JSON is malformed', () => {
|
||||
const content = 'generate_course_plan\n{subject: algebre'
|
||||
expect(extractFakeToolCall(content, ['generate_course_plan'])).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('extractBareJsonRetry()', () => {
|
||||
it('should recover a bare JSON object as a retry of the given tool, with no tool name in the text', () => {
|
||||
const content = 'Voici une nouvelle tentative :\n{"subject": "algebre", "module": "fractions"}'
|
||||
const result = extractBareJsonRetry(content, 'write_module_content')
|
||||
expect(result).toEqual({
|
||||
name: 'write_module_content',
|
||||
arguments: '{"subject": "algebre", "module": "fractions"}'
|
||||
})
|
||||
})
|
||||
|
||||
it('should return null when the text contains no JSON object at all', () => {
|
||||
expect(
|
||||
extractBareJsonRetry('Je ne sais pas comment continuer.', 'write_module_content')
|
||||
).toBeNull()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user