@cccarv82/freya 3.3.0 → 3.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/cli/web.js +40 -24
  2. package/package.json +1 -1
package/cli/web.js CHANGED
@@ -3322,20 +3322,21 @@ async function cmdWeb({ port, dir, open, dev }) {
3322
3322
  ]
3323
3323
  };
3324
3324
 
3325
- const prompt = `Você é o planner do sistema F.R.E.Y.A.\n\nContexto: vamos receber um input bruto do usuário e propor ações estruturadas.\nRegras: siga os arquivos de regras abaixo.\nSaída: retorne APENAS JSON válido no formato: ${JSON.stringify(schema)}\n\nRestrições:\n- NÃO use code fences (\`\`\`)\n- NÃO inclua texto extra antes/depois do JSON\n- NÃO use quebras de linha dentro de strings (transforme em uma frase única)${planImageContext}\n\nREGRAS:${rulesText}\n\nINPUT DO USUÁRIO:\n${text}\n`;
3325
+ // Build the system instructions (small, always fits in -p)
3326
+ const sysInstructions = `Você é o planner do sistema F.R.E.Y.A.\n\nContexto: vamos receber um input bruto do usuário e propor ações estruturadas.\nRegras: siga os arquivos de regras abaixo.\nSaída: retorne APENAS JSON válido no formato: ${JSON.stringify(schema)}\n\nRestrições:\n- NÃO use code fences (\`\`\`)\n- NÃO inclua texto extra antes/depois do JSON\n- NÃO use quebras de linha dentro de strings (transforme em uma frase única)${planImageContext}`;
3326
3327
 
3327
3328
  // Prefer COPILOT_CMD if provided, otherwise try 'copilot'
3328
3329
  const cmd = process.env.COPILOT_CMD || 'copilot';
3329
3330
 
3330
- // Best-effort: if Copilot CLI isn't available, return 200 with an explanatory plan
3331
- // so the UI can show actionable next steps instead of hard-failing.
3332
3331
  // BUG-48: pass FREYA_WORKSPACE_DIR so the Copilot subprocess uses correct DB
3333
3332
  const agentEnv = { FREYA_WORKSPACE_DIR: workspaceDir };
3334
3333
 
3335
- // ENAMETOOLONG fix: when prompt exceeds safe CLI arg length,
3336
- // write to temp file and pipe via stdin instead of -p argument.
3337
- const SAFE_ARG_LEN = 24000; // ~24KB safe threshold (Windows CreateProcess limit is 32KB)
3338
- const useTempFile = prompt.length > SAFE_ARG_LEN;
3334
+ // ENAMETOOLONG fix: when user text is large, write it to a temp file
3335
+ // and reference it in the prompt. System instructions stay in -p (small).
3336
+ // The user text is the large part; rules are moderate (~10KB).
3337
+ const fullPrompt = `${sysInstructions}\n\nREGRAS:${rulesText}\n\nINPUT DO USUÁRIO:\n${text}\n`;
3338
+ const SAFE_ARG_LEN = 24000; // ~24KB safe for Windows CreateProcess
3339
+ const needsFile = fullPrompt.length > SAFE_ARG_LEN;
3339
3340
  let tmpFile = null;
3340
3341
 
3341
3342
  try {
@@ -3343,13 +3344,27 @@ async function cmdWeb({ port, dir, open, dev }) {
3343
3344
  const baseArgs = ['-s', '--no-color', '--stream', 'off'];
3344
3345
  if (planImageDir) baseArgs.push('--add-dir', planImageDir);
3345
3346
 
3346
- if (useTempFile) {
3347
- tmpFile = path.join(os.tmpdir(), `freya-prompt-${Date.now()}.txt`);
3348
- fs.writeFileSync(tmpFile, prompt, 'utf8');
3349
- // Use -p with short hint + pipe full prompt via stdin
3350
- r = await run(cmd, [...baseArgs, '-p', `Read the full prompt from the file: ${tmpFile}`, '--allow-all-tools'], workspaceDir, agentEnv, prompt);
3347
+ if (needsFile) {
3348
+ // Write ONLY the user text to a temp file; keep instructions in -p
3349
+ tmpFile = path.join(os.tmpdir(), `freya-input-${Date.now()}.txt`);
3350
+ fs.writeFileSync(tmpFile, text, 'utf8');
3351
+ // -p contains: system instructions + rules + reference to the file
3352
+ const filePrompt = `${sysInstructions}\n\nREGRAS:${rulesText}\n\nINPUT DO USUÁRIO:\nO texto do usuário é MUITO GRANDE e foi salvo no arquivo abaixo. Você DEVE ler o conteúdo completo deste arquivo usando suas ferramentas de leitura de arquivo e processar TODO o conteúdo como input do usuário.\nARQUIVO: ${tmpFile}\n\nIMPORTANTE: NÃO descreva o arquivo. LEIA o conteúdo e processe-o gerando as ações JSON conforme as regras acima.\n`;
3353
+ // If even the filePrompt (instructions + rules + file ref) is too large,
3354
+ // also write rules to a separate file
3355
+ if (filePrompt.length > SAFE_ARG_LEN) {
3356
+ const rulesTmpFile = path.join(os.tmpdir(), `freya-rules-${Date.now()}.txt`);
3357
+ fs.writeFileSync(rulesTmpFile, rulesText, 'utf8');
3358
+ const minPrompt = `${sysInstructions}\n\nREGRAS: Leia as regras do arquivo: ${rulesTmpFile}\n\nINPUT DO USUÁRIO:\nO texto do usuário é MUITO GRANDE e foi salvo no arquivo abaixo. Você DEVE ler o conteúdo completo deste arquivo e processar TODO o conteúdo como input do usuário.\nARQUIVO: ${tmpFile}\n\nIMPORTANTE: NÃO descreva os arquivos. LEIA os conteúdos e processe-os gerando as ações JSON conforme as regras.\n`;
3359
+ baseArgs.push('--add-dir', os.tmpdir());
3360
+ r = await run(cmd, [...baseArgs, '-p', minPrompt, '--allow-all-tools'], workspaceDir, agentEnv);
3361
+ try { fs.unlinkSync(rulesTmpFile); } catch (_) { /* ignore */ }
3362
+ } else {
3363
+ baseArgs.push('--add-dir', os.tmpdir());
3364
+ r = await run(cmd, [...baseArgs, '-p', filePrompt, '--allow-all-tools'], workspaceDir, agentEnv);
3365
+ }
3351
3366
  } else {
3352
- r = await run(cmd, [...baseArgs, '-p', prompt, '--allow-all-tools'], workspaceDir, agentEnv);
3367
+ r = await run(cmd, [...baseArgs, '-p', fullPrompt, '--allow-all-tools'], workspaceDir, agentEnv);
3353
3368
  }
3354
3369
  const out = (r.stdout + r.stderr).trim();
3355
3370
  if (r.code !== 0) {
@@ -3820,7 +3835,8 @@ async function cmdWeb({ port, dir, open, dev }) {
3820
3835
  }
3821
3836
  }
3822
3837
 
3823
- const prompt = `Você é o agente Oracle do sistema F.R.E.Y.A.\n\nSiga estritamente os arquivos de regras abaixo.\nResponda de forma analítica e consultiva.\n${ragContext}${imageContext}\n\nREGRAS:${rulesText}\n\nCONSULTA DO USUÁRIO:\n${query}\n`;
3838
+ // System instructions (small, always fits in -p)
3839
+ const oracleSysInstr = `Você é o agente Oracle do sistema F.R.E.Y.A.\n\nSiga estritamente os arquivos de regras abaixo.\nResponda de forma analítica e consultiva.\n${ragContext}${imageContext}`;
3824
3840
 
3825
3841
  const cmd = process.env.COPILOT_CMD || 'copilot';
3826
3842
 
@@ -3835,21 +3851,21 @@ async function cmdWeb({ port, dir, open, dev }) {
3835
3851
  copilotArgs.push('--add-dir', path.dirname(absImg));
3836
3852
  }
3837
3853
  }
3838
- copilotArgs.push('--allow-all-tools', '-p', prompt);
3839
3854
 
3840
- // ENAMETOOLONG fix: use stdin for large prompts
3855
+ // ENAMETOOLONG fix: when prompt is large, write user query to temp file
3856
+ const fullOraclePrompt = `${oracleSysInstr}\n\nREGRAS:${rulesText}\n\nCONSULTA DO USUÁRIO:\n${query}\n`;
3841
3857
  const SAFE_ARG_LEN = 24000;
3842
3858
  let oracleTmpFile = null;
3843
3859
  let r;
3844
- if (prompt.length > SAFE_ARG_LEN) {
3845
- oracleTmpFile = path.join(os.tmpdir(), `freya-oracle-${Date.now()}.txt`);
3846
- fs.writeFileSync(oracleTmpFile, prompt, 'utf8');
3847
- // Replace -p with short hint; pipe full prompt via stdin
3848
- const idx = copilotArgs.indexOf('-p');
3849
- if (idx !== -1) copilotArgs.splice(idx, 2); // remove -p and prompt
3850
- copilotArgs.push('-p', `Read the full prompt from: ${oracleTmpFile}`);
3851
- r = await run(cmd, copilotArgs, workspaceDir, oracleEnv, prompt);
3860
+ if (fullOraclePrompt.length > SAFE_ARG_LEN) {
3861
+ oracleTmpFile = path.join(os.tmpdir(), `freya-oracle-input-${Date.now()}.txt`);
3862
+ fs.writeFileSync(oracleTmpFile, query, 'utf8');
3863
+ const filePrompt = `${oracleSysInstr}\n\nREGRAS:${rulesText}\n\nCONSULTA DO USUÁRIO:\nA consulta do usuário é grande e foi salva no arquivo abaixo. LEIA o conteúdo completo do arquivo e responda com base nele.\nARQUIVO: ${oracleTmpFile}\n\nIMPORTANTE: NÃO descreva o arquivo. LEIA e RESPONDA à consulta.\n`;
3864
+ copilotArgs.push('--add-dir', os.tmpdir());
3865
+ copilotArgs.push('--allow-all-tools', '-p', filePrompt);
3866
+ r = await run(cmd, copilotArgs, workspaceDir, oracleEnv);
3852
3867
  } else {
3868
+ copilotArgs.push('--allow-all-tools', '-p', fullOraclePrompt);
3853
3869
  r = await run(cmd, copilotArgs, workspaceDir, oracleEnv);
3854
3870
  }
3855
3871
  if (oracleTmpFile) { try { fs.unlinkSync(oracleTmpFile); } catch (_) { /* ignore */ } }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cccarv82/freya",
3
- "version": "3.3.0",
3
+ "version": "3.3.1",
4
4
  "description": "Personal AI Assistant with local-first persistence",
5
5
  "scripts": {
6
6
  "health": "node scripts/validate-data.js && node scripts/validate-structure.js",