fix: v2.22.0 - stable multi-agent workflow (API compatibility & architecture refactor)
This commit is contained in:
+83
-22
@@ -6,27 +6,45 @@ export abstract class BaseAgent {
|
||||
|
||||
protected async callLLM(persona: string, prompt: string, signal?: AbortSignal): Promise<string> {
|
||||
const { ollamaUrl } = getConfig();
|
||||
if (!ollamaUrl) {
|
||||
throw new Error('Ollama URL이 설정되지 않았습니다. 설정을 확인해주세요.');
|
||||
}
|
||||
|
||||
if (typeof fetch === 'undefined') {
|
||||
throw new Error('이 환경에서는 fetch 함수를 사용할 수 없습니다. Node.js 버전을 확인하거나 polyfill이 필요합니다.');
|
||||
}
|
||||
|
||||
const messages = [
|
||||
{ role: 'system', content: persona },
|
||||
{ role: 'user', content: prompt }
|
||||
];
|
||||
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 45000); // Increased to 45s for complex tasks
|
||||
// 엔진 자동 감지 (Ollama vs OpenAI/LM Studio)
|
||||
const isOllama = ollamaUrl.includes(':11434') || ollamaUrl.includes('ollama');
|
||||
const endpoint = isOllama ? `${ollamaUrl}/api/chat` : `${ollamaUrl}/v1/chat/completions`;
|
||||
|
||||
// Combine external signal with local timeout
|
||||
const combinedSignal = signal ?
|
||||
anySignal([signal, controller.signal]) :
|
||||
controller.signal;
|
||||
let lastError: any;
|
||||
for (let attempt = 1; attempt <= 3; attempt++) {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 45000);
|
||||
const combinedSignal = signal ? anySignal([signal, controller.signal]) : controller.signal;
|
||||
|
||||
try {
|
||||
const response = await fetch(`${ollamaUrl}/api/chat`, {
|
||||
try {
|
||||
if (attempt > 1) await new Promise(resolve => setTimeout(resolve, 1000 * attempt));
|
||||
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(isOllama ? {
|
||||
model: this.modelName,
|
||||
messages,
|
||||
stream: false,
|
||||
options: { temperature: 0.3 }
|
||||
} : {
|
||||
model: this.modelName,
|
||||
messages,
|
||||
stream: false,
|
||||
temperature: 0.3
|
||||
}),
|
||||
signal: combinedSignal
|
||||
});
|
||||
@@ -37,15 +55,25 @@ export abstract class BaseAgent {
|
||||
throw new Error(`Agent API Error: ${response.statusText} (${response.status})`);
|
||||
}
|
||||
|
||||
const data = await response.json() as any;
|
||||
return data.message?.content || data.choices?.[0]?.message?.content || '';
|
||||
} catch (error: any) {
|
||||
clearTimeout(timeoutId);
|
||||
if (error.name === 'AbortError') {
|
||||
throw new Error('Agent request was cancelled or timed out.');
|
||||
const data = await response.json() as any;
|
||||
|
||||
// 강력한 응답 추출 (Multi-path parsing)
|
||||
let content = '';
|
||||
if (data.message?.content) content = data.message.content;
|
||||
else if (data.choices?.[0]?.message?.content) content = data.choices[0].message.content;
|
||||
else if (data.choices?.[0]?.text) content = data.choices[0].text;
|
||||
else if (data.response) content = data.response;
|
||||
else if (typeof data === 'string') content = data;
|
||||
|
||||
return content || '';
|
||||
} catch (error: any) {
|
||||
clearTimeout(timeoutId);
|
||||
lastError = error;
|
||||
if (error.name === 'AbortError') break;
|
||||
if (attempt === 3) break;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
abstract execute(input: string, context?: string, signal?: AbortSignal): Promise<string>;
|
||||
@@ -65,22 +93,55 @@ function anySignal(signals: AbortSignal[]): AbortSignal {
|
||||
}
|
||||
|
||||
export class PlannerAgent extends BaseAgent {
|
||||
private readonly persona = `You are the [Planner Agent]. Analyze the request and output a structured <plan>.`;
|
||||
private readonly persona = `You are the [Master Strategist & Planner].
|
||||
Your sole purpose is to transform vague requests into flawless, high-resolution execution blueprints.
|
||||
- THINKING PROCESS: You must analyze the request from multiple angles (technical, logical, structural).
|
||||
- OUTPUT RULE: You MUST output a structured <blueprint> using Markdown.
|
||||
- COMPONENTS: Each blueprint must have [Objective], [Core Challenges], [Data Requirements], and [Step-by-Step Research Tasks].
|
||||
- CONSTRAINT: Do not be vague. Use professional terminology. If the request is too simple, expand it with relevant technical considerations.`;
|
||||
|
||||
async execute(input: string, brainContext?: string, signal?: AbortSignal): Promise<string> {
|
||||
return this.callLLM(this.persona, `Request: ${input}\nContext: ${brainContext}`, signal);
|
||||
const wrappedInput = `### SYSTEM INSTRUCTION: GENERATE EXECUTION BLUEPRINT
|
||||
1. Target Goal: ${input}
|
||||
2. Available Knowledge Base: ${brainContext}
|
||||
3. Mission: Create a comprehensive research roadmap.`;
|
||||
return this.callLLM(this.persona, wrappedInput, signal);
|
||||
}
|
||||
}
|
||||
|
||||
export class ResearcherAgent extends BaseAgent {
|
||||
private readonly persona = `You are the [Researcher Agent]. Gather facts based on the plan.`;
|
||||
private readonly persona = `You are the [Senior Technical Researcher].
|
||||
Your mission is to extract, filter, and synthesize critical data based on a strategic blueprint.
|
||||
- DATA INTEGRITY: Only provide high-quality, verified-style information.
|
||||
- FORMAT: Use [Key Facts], [Technical Deep-Dive], and [Summary of Knowledge] sections.
|
||||
- CRITICAL THINKING: Identify gaps in the plan and provide extra insights to fill those gaps.
|
||||
- NO FLUFF: Be concise but extremely dense with information.`;
|
||||
|
||||
async execute(input: string, brainContext?: string, signal?: AbortSignal): Promise<string> {
|
||||
return this.callLLM(this.persona, `Plan: ${input}\nContext: ${brainContext}`, signal);
|
||||
const wrappedInput = `### SYSTEM INSTRUCTION: DATA HARVESTING
|
||||
1. Blueprint to Follow: ${input}
|
||||
2. Contextual Constraints: ${brainContext}
|
||||
3. Mission: Provide a dense summary of facts and technical insights.`;
|
||||
return this.callLLM(this.persona, wrappedInput, signal);
|
||||
}
|
||||
}
|
||||
|
||||
export class WriterAgent extends BaseAgent {
|
||||
private readonly persona = `You are the [Writer Agent]. Synthesize research into a final report.`;
|
||||
private readonly persona = `You are the [Lead Synthesis Writer & Editor].
|
||||
Your goal is to produce a state-of-the-art final report that wows the user.
|
||||
- TONE: Authoritative yet accessible. Professional developer/consultant style.
|
||||
- STRUCTURE: Use an executive summary, detailed analysis sections, and a "Final Recommendation" block.
|
||||
- LANGUAGE: Always respond in the user's language (KOREAN).
|
||||
- POLISHING: Ensure logical flow between sections. Make it look like a premium report.`;
|
||||
|
||||
async execute(input: string, originalRequest?: string, signal?: AbortSignal): Promise<string> {
|
||||
return this.callLLM(this.persona, `Data: ${input}\nOriginal Request: ${originalRequest}`, signal);
|
||||
// Fix 3: Trim input if it's too long (Basic Context Diet)
|
||||
const trimmedData = input.length > 8000 ? input.substring(0, 8000) + '... [Data Trimmed for Performance]' : input;
|
||||
|
||||
const wrappedInput = `### SYSTEM INSTRUCTION: FINAL SYNTHESIS
|
||||
1. Gathered Research Data: ${trimmedData}
|
||||
2. User's Original Objective: ${originalRequest}
|
||||
3. Mission: Write the definitive final report in KOREAN.`;
|
||||
return this.callLLM(this.persona, wrappedInput, signal);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user