This commit is contained in:
2025-12-15 12:37:19 +00:00
parent 0ee110cf7d
commit 7403e769d0
4 changed files with 104 additions and 27 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@push.rocks/smartagent",
"version": "1.1.1",
"version": "1.2.0",
"private": false,
"description": "an agentic framework built on top of @push.rocks/smartai",
"main": "dist_ts/index.js",

View File

@@ -41,8 +41,14 @@ export class DriverAgent {
// Reset message history
this.messageHistory = [];
// Build the user message
const userMessage = `TASK: ${task}\n\nAnalyze this task and determine what actions are needed. If you need to use a tool, provide a tool call proposal.`;
// Build the user message based on available tools
const hasTools = this.tools.size > 0;
let userMessage: string;
if (hasTools) {
userMessage = `TASK: ${task}\n\nAnalyze this task and determine what actions are needed. If you need to use a tool, provide a tool call proposal.`;
} else {
userMessage = `TASK: ${task}\n\nComplete this task directly. When done, wrap your final output in <task_complete>your output here</task_complete> tags.`;
}
// Add to history
this.messageHistory.push({
@@ -50,9 +56,15 @@ export class DriverAgent {
content: userMessage,
});
// Build tool descriptions for the system message
const toolDescriptions = this.buildToolDescriptions();
const fullSystemMessage = `${this.systemMessage}\n\n## Available Tools\n${toolDescriptions}`;
// Build the system message - adapt based on available tools
let fullSystemMessage: string;
if (hasTools) {
const toolDescriptions = this.buildToolDescriptions();
fullSystemMessage = `${this.systemMessage}\n\n## Available Tools\n${toolDescriptions}`;
} else {
// Use a simpler system message when no tools are available
fullSystemMessage = this.getNoToolsSystemMessage();
}
// Get response from provider
const response = await this.provider.chat({
@@ -83,9 +95,15 @@ export class DriverAgent {
content: message,
});
// Build tool descriptions for the system message
const toolDescriptions = this.buildToolDescriptions();
const fullSystemMessage = `${this.systemMessage}\n\n## Available Tools\n${toolDescriptions}`;
// Build the system message - adapt based on available tools
const hasTools = this.tools.size > 0;
let fullSystemMessage: string;
if (hasTools) {
const toolDescriptions = this.buildToolDescriptions();
fullSystemMessage = `${this.systemMessage}\n\n## Available Tools\n${toolDescriptions}`;
} else {
fullSystemMessage = this.getNoToolsSystemMessage();
}
// Get response from provider (pass all but last user message as history)
const historyForChat = this.messageHistory.slice(0, -1);
@@ -312,6 +330,35 @@ When you need to use a tool, output a tool call proposal in this format:
- If you need clarification, ask using <needs_clarification>your question</needs_clarification>`;
}
/**
* Get the system message when no tools are available
* Used for direct task completion without tool usage
*/
private getNoToolsSystemMessage(): string {
// Use custom system message if provided, otherwise use a simple default
if (this.systemMessage && this.systemMessage !== this.getDefaultSystemMessage()) {
return this.systemMessage;
}
return `You are an AI assistant that completes tasks directly.
## Your Role
You analyze tasks and provide complete, high-quality outputs.
## Output Format
When you have completed the task, wrap your final output in task_complete tags:
<task_complete>
Your complete output here
</task_complete>
## Guidelines
1. Analyze the task requirements carefully
2. Provide a complete and accurate response
3. Always wrap your final output in <task_complete></task_complete> tags
4. If you need clarification, ask using <needs_clarification>your question</needs_clarification>`;
}
/**
* Reset the conversation state
*/

View File

@@ -23,6 +23,7 @@ export class DualAgentOrchestrator {
private tools: Map<string, BaseToolWrapper> = new Map();
private isRunning = false;
private conversationHistory: interfaces.IAgentMessage[] = [];
private ownsSmartAi = true; // true if we created the SmartAi instance, false if it was provided
constructor(options: interfaces.IDualAgentOptions) {
this.options = {
@@ -32,18 +33,15 @@ export class DualAgentOrchestrator {
...options,
};
// Create SmartAi instance
this.smartai = new plugins.smartai.SmartAi(options);
// Get providers
this.driverProvider = this.getProviderByName(this.options.defaultProvider!);
this.guardianProvider = this.options.guardianProvider
? this.getProviderByName(this.options.guardianProvider)
: this.driverProvider;
// Create agents
this.driver = new DriverAgent(this.driverProvider, options.driverSystemMessage);
this.guardian = new GuardianAgent(this.guardianProvider, options.guardianPolicyPrompt);
// Use existing SmartAi instance if provided, otherwise create a new one
if (options.smartAiInstance) {
this.smartai = options.smartAiInstance;
this.ownsSmartAi = false; // Don't manage lifecycle of provided instance
} else {
this.smartai = new plugins.smartai.SmartAi(options);
this.ownsSmartAi = true;
}
// Note: Don't access providers here - they don't exist until start() is called
}
/**
@@ -75,8 +73,13 @@ export class DualAgentOrchestrator {
*/
public registerTool(tool: BaseToolWrapper): void {
this.tools.set(tool.name, tool);
this.driver.registerTool(tool);
this.guardian.registerTool(tool);
// Register with agents if they exist (they're created in start())
if (this.driver) {
this.driver.registerTool(tool);
}
if (this.guardian) {
this.guardian.registerTool(tool);
}
}
/**
@@ -100,8 +103,26 @@ export class DualAgentOrchestrator {
* Initialize all tools (eager loading)
*/
public async start(): Promise<void> {
// Start smartai
await this.smartai.start();
// Start smartai only if we created it (external instances should already be started)
if (this.ownsSmartAi) {
await this.smartai.start();
}
// NOW get providers (after they've been initialized by smartai.start())
this.driverProvider = this.getProviderByName(this.options.defaultProvider!);
this.guardianProvider = this.options.guardianProvider
? this.getProviderByName(this.options.guardianProvider)
: this.driverProvider;
// NOW create agents with initialized providers
this.driver = new DriverAgent(this.driverProvider, this.options.driverSystemMessage);
this.guardian = new GuardianAgent(this.guardianProvider, this.options.guardianPolicyPrompt);
// Register any tools that were added before start() with the agents
for (const tool of this.tools.values()) {
this.driver.registerTool(tool);
this.guardian.registerTool(tool);
}
// Initialize all tools
const initPromises: Promise<void>[] = [];
@@ -124,9 +145,16 @@ export class DualAgentOrchestrator {
}
await Promise.all(cleanupPromises);
await this.smartai.stop();
// Only stop smartai if we created it (don't stop external instances)
if (this.ownsSmartAi) {
await this.smartai.stop();
}
this.isRunning = false;
this.driver.reset();
if (this.driver) {
this.driver.reset();
}
}
/**

View File

@@ -8,6 +8,8 @@ import * as plugins from './plugins.js';
* Configuration options for the DualAgentOrchestrator
*/
export interface IDualAgentOptions extends plugins.smartai.ISmartAiOptions {
/** Existing SmartAi instance to reuse (avoids creating duplicate providers) */
smartAiInstance?: plugins.smartai.SmartAi;
/** Name of the agent system */
name?: string;
/** Default AI provider for both Driver and Guardian */