Step 1 of adding a backend proxy server (API keys held server-side instead of shipped in the installer). Moves everything app-specific into "Max Avatar/" via git mv (preserves history): src/, tests/, scripts/, assets/, build/, resources/, package.json/-lock.json, electron-builder.yml, electron.vite.config.ts, eslint.config.mjs, playwright.config.ts, vitest.config.mts, sonar-project.properties, tsconfig*.json, Dockerfile, .editorconfig/.env*/.gitignore/.prettier*. CLAUDE.md, README.md, docs/, .claude/, .gitea/, .husky/, .vscode/, and the docker-compose files stay at the repo root, since they'll apply to both the app and the future backend. No application code needed changes (all its internal paths are relative to its own tree). Adjusted what referenced the old root from outside: - .husky/pre-commit: cd into "Max Avatar" before lint-staged - .gitea/workflows/*.yml: working-directory + cache-dependency-path + artifact paths + SonarQube projectBaseDir - docker-compose.yml: build context -> ./Max Avatar - Max Avatar/Dockerfile: node:20-alpine -> node:24-alpine (same node:sqlite issue already fixed elsewhere, never applied here before) - .vscode/launch.json, README.md: updated paths/instructions Verified from Max Avatar/ after the move: npm ci, typecheck, lint, test:coverage (473/473, same coverage numbers), build, and dev all work unchanged. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
87 lines
2.7 KiB
TypeScript
87 lines
2.7 KiB
TypeScript
/**
|
|
* Recuperation des appels d'outils mal formes renvoyes par le modele en texte brut au lieu d'un
|
|
* vrai `tool_calls` structure (bug recurrent observe en test reel avec deepseek/qwen). Extrait dans
|
|
* son propre module, sans aucune dependance Electron/IPC, pour rester testable unitairement.
|
|
*/
|
|
|
|
/**
|
|
* Constate en test reel : au lieu d'un vrai appel de fonction structure (tool_calls), le modele
|
|
* ecrit parfois le nom de l'outil suivi d'un objet JSON directement dans le texte de sa reponse.
|
|
* Recupere ce cas en retrouvant le nom d'un outil connu suivi d'un objet JSON valide dans le texte,
|
|
* pour l'executer comme un vrai appel.
|
|
*/
|
|
export function extractFakeToolCall(
|
|
content: string,
|
|
toolNames: string[]
|
|
): { name: string; arguments: string } | null {
|
|
for (const name of toolNames) {
|
|
const nameIndex = content.lastIndexOf(name)
|
|
if (nameIndex === -1) continue
|
|
const braceStart = content.indexOf('{', nameIndex)
|
|
if (braceStart === -1) continue
|
|
|
|
let depth = 0
|
|
let braceEnd = -1
|
|
for (let i = braceStart; i < content.length; i++) {
|
|
const char = content.charAt(i)
|
|
if (char === '{') depth++
|
|
else if (char === '}') {
|
|
depth--
|
|
if (depth === 0) {
|
|
braceEnd = i
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if (braceEnd === -1) continue
|
|
|
|
const jsonText = content.slice(braceStart, braceEnd + 1)
|
|
try {
|
|
JSON.parse(jsonText)
|
|
return { name, arguments: jsonText }
|
|
} catch {
|
|
continue
|
|
}
|
|
}
|
|
return null
|
|
}
|
|
|
|
/**
|
|
* Filet de secours quand extractFakeToolCall ne trouve aucun nom d'outil dans le texte (constate en
|
|
* test reel : apres plusieurs echecs de validation de write_module_content, le modele a fini par
|
|
* ecrire juste "json\n{...}" sans jamais reciter le nom de l'outil, lu a voix haute pendant 46
|
|
* secondes au lieu d'etre rappele). Si le dernier outil appele ce tour a echoue et que le texte
|
|
* contient un objet JSON exploitable, on suppose que c'est une nouvelle tentative du MEME outil.
|
|
*/
|
|
export function extractBareJsonRetry(
|
|
content: string,
|
|
retryToolName: string
|
|
): { name: string; arguments: string } | null {
|
|
const braceStart = content.indexOf('{')
|
|
if (braceStart === -1) return null
|
|
|
|
let depth = 0
|
|
let braceEnd = -1
|
|
for (let i = braceStart; i < content.length; i++) {
|
|
const char = content.charAt(i)
|
|
if (char === '{') depth++
|
|
else if (char === '}') {
|
|
depth--
|
|
if (depth === 0) {
|
|
braceEnd = i
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if (braceEnd === -1) return null
|
|
|
|
const jsonText = content.slice(braceStart, braceEnd + 1)
|
|
try {
|
|
const parsed: unknown = JSON.parse(jsonText)
|
|
if (typeof parsed !== 'object' || parsed === null) return null
|
|
return { name: retryToolName, arguments: jsonText }
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|