The-Agency/lib/src/chat/advisor_service.dart

57 lines
1.9 KiB
Dart

import "../api/openrouter_client.dart";
const _advisorSystemPrompt =
"You are an advisor reviewing an AI agent's work in progress. "
"You will be shown the full conversation history including tool calls and results. "
"Your job is to give concise, actionable guidance: identify mistakes, "
"suggest better approaches, flag assumptions that need verifying, or confirm "
"the agent is on the right track. Be direct and specific.";
const advisorToolDescription =
"Call the advisor model for a second opinion on your current approach. "
"Takes no parameters — your full conversation history is forwarded automatically. "
"Call BEFORE committing to a significant approach, BEFORE declaring done, or when stuck.";
class AdvisorService {
Future<String> run({
required String advisorModel,
required String apiKey,
required List<Map<String, dynamic>> conversationSoFar,
void Function(String toolName, Map<String, dynamic> input)? onToolCall,
void Function(String toolName, String result)? onToolResult,
}) async {
onToolCall?.call("Advisor", {"model": advisorModel});
OpenRouterClient? client;
try {
client = OpenRouterClient(
config: OpenRouterConfig(apiKey: apiKey),
);
final response = await client.createMessage(
model: advisorModel,
maxTokens: 2048,
messages: conversationSoFar,
system: _advisorSystemPrompt,
);
final text = response.content
.whereType<Map<String, dynamic>>()
.where((b) => b["type"] == "text")
.map((b) => b["text"] as String? ?? "")
.join("\n")
.trim();
final result = text.isEmpty ? "Advisor returned no guidance." : text;
onToolResult?.call("Advisor", result);
return result;
} catch (e) {
final err = "Advisor call failed: $e";
onToolResult?.call("Advisor", err);
return err;
} finally {
client?.close();
}
}
}