Files
2026-11-30 16:32:37 +00:00

98 lines
3.8 KiB
JavaScript

// Swarm.js Agent (corrected Node.js adaptation of OpenAI Swarm).
//
// This is a working, production-oriented rewrite of the `Agent` abstraction from
// the openai-swarm-node project (https://github.com/youseai/openai-swarm-node).
// The upstream repo is an unfinished educational sketch (its index.js still has
// Python leftovers like `len()` and an infinite loop, and its swarm.js imports
// the removed openai v3 `Configuration`/`OpenAIApi` API), so it cannot be used
// as-is. We keep its ergonomic design - an Agent owns instructions plus a set of
// named tool functions - but drive it with native function calling through
// ADPanel's existing multi-provider transport, and make every tool an async
// function with a strict JSON schema (no fragile JSDoc parsing).
//
// An Agent is intentionally a plain data holder: it carries no execution logic
// and no secrets. The Swarm runner (swarm.js) owns the loop, and the host app
// owns the actual tool side effects + permission enforcement.
class Agent {
/**
* @param {object} config
* @param {string} config.name Human-readable agent name.
* @param {string|Function} config.instructions
* System prompt. May be a function (contextVariables) => string so the
* prompt can adapt to runtime context (e.g. the user's permissions).
* @param {Array<object>} config.tools
* Tool definitions: { name, description, parameters (JSON schema), fn }.
* `fn(args, contextVariables)` performs the side effect and returns a
* JSON-serializable result. A tool may return a { agent } handoff to
* transfer control to another Agent (Swarm-style handoff).
* @param {string} [config.model] Optional model hint (the host usually
* passes the model explicitly to Swarm.run instead).
*/
constructor({ name, instructions = "You are a helpful agent.", tools = [], model = "" } = {}) {
this.name = String(name || "agent").trim() || "agent";
this.instructions = instructions;
this.model = String(model || "").trim();
this.tools = Array.isArray(tools) ? tools.filter(Boolean) : [];
// Fast lookup of a tool by name for the runner.
this.toolMap = new Map();
for (const tool of this.tools) {
const toolName = String(tool?.name || "").trim();
if (toolName) this.toolMap.set(toolName, tool);
}
}
// Resolve the system prompt for this turn, allowing function-form instructions.
resolveInstructions(contextVariables = {}) {
if (typeof this.instructions === "function") {
try {
return String(this.instructions(contextVariables) || "").trim();
} catch {
return "";
}
}
return String(this.instructions || "").trim();
}
hasTool(name) {
return this.toolMap.has(String(name || "").trim());
}
getTool(name) {
return this.toolMap.get(String(name || "").trim()) || null;
}
// OpenAI / Cohere style tool definitions.
toOpenAiToolDefinitions() {
return this.tools.map((tool) => ({
type: "function",
function: {
name: tool.name,
description: String(tool.description || "").trim(),
parameters: tool.parameters || { type: "object", properties: {}, required: [] },
},
}));
}
// Anthropic style tool definitions.
toAnthropicToolDefinitions() {
return this.tools.map((tool) => ({
name: tool.name,
description: String(tool.description || "").trim(),
input_schema: tool.parameters || { type: "object", properties: {}, required: [] },
}));
}
// Google Gemini style function declarations.
toGoogleFunctionDeclarations() {
return this.tools.map((tool) => ({
name: tool.name,
description: String(tool.description || "").trim(),
parameters: tool.parameters || { type: "object", properties: {}, required: [] },
}));
}
}
module.exports = Agent;