@ai-sdk/swarm
Version:
> **Warning** > This is an experimental package. It is not maintained and the API is not stable.
72 lines (68 loc) • 2.87 kB
text/typescript
import { LanguageModel, ToolChoice, Schema, CoreMessage, StepResult, FinishReason } from 'ai';
import { z } from 'zod';
type Parameters = z.ZodTypeAny | Schema<any>;
type inferParameters<PARAMETERS extends Parameters> = PARAMETERS extends Schema<any> ? PARAMETERS['_type'] : PARAMETERS extends z.ZodTypeAny ? z.infer<PARAMETERS> : never;
type AgentFunctionTool<CONTEXT = any, PARAMETERS extends Parameters = any, RESULT = any> = {
type?: undefined | 'function';
description?: string;
parameters: PARAMETERS;
execute: (args: inferParameters<PARAMETERS>, options: {
abortSignal?: AbortSignal;
context: CONTEXT;
}) => PromiseLike<RESULT>;
};
declare function functionTool<CONTEXT = any, PARAMETERS extends Parameters = any>(tool: {
type?: 'function';
description?: string;
parameters: PARAMETERS;
execute: AgentFunctionTool<CONTEXT, PARAMETERS>['execute'];
}): AgentFunctionTool<CONTEXT, PARAMETERS>;
type AgentHandoverTool<CONTEXT = any, PARAMETERS extends Parameters = any> = {
type: 'handover';
description?: string;
parameters: PARAMETERS;
execute: (args: inferParameters<PARAMETERS>, options: {
abortSignal?: AbortSignal;
context: CONTEXT;
}) => {
agent: Agent<CONTEXT>;
context?: CONTEXT;
};
};
declare function handoverTool<CONTEXT = any, PARAMETERS extends Parameters = any>(tool: {
type: 'handover';
description?: string;
parameters: PARAMETERS;
execute: AgentHandoverTool<CONTEXT, PARAMETERS>['execute'];
}): AgentHandoverTool<CONTEXT, PARAMETERS>;
type AgentTool<CONTEXT = any> = AgentFunctionTool<CONTEXT, any, any> | AgentHandoverTool<CONTEXT, any>;
declare class Agent<CONTEXT = any> {
readonly name: string;
readonly model: LanguageModel | undefined;
readonly system: ((context: CONTEXT) => string) | string | undefined;
readonly tools: Record<string, AgentTool<CONTEXT>> | undefined;
readonly toolChoice: ToolChoice<any> | undefined;
constructor(options: {
name: string;
system?: ((context: CONTEXT) => string) | string | undefined;
model?: LanguageModel;
tools?: Record<string, AgentTool>;
toolChoice?: ToolChoice<any>;
});
}
declare function runSwarm<CONTEXT = any>({ agent: activeAgent, prompt, context, model, maxSteps, toolChoice, debug, onStepFinish, }: {
agent: Agent;
prompt: CoreMessage[] | string;
context?: CONTEXT;
model: LanguageModel;
maxSteps?: number;
toolChoice?: ToolChoice<any>;
debug?: boolean;
onStepFinish?: (event: StepResult<any>) => Promise<void> | void;
}): Promise<{
text: string;
responseMessages: CoreMessage[];
activeAgent: Agent;
finishReason: FinishReason;
}>;
export { Agent, type AgentFunctionTool, type AgentHandoverTool, type AgentTool, functionTool, handoverTool, runSwarm };