API Reference

agents/image-agent


Classes#

ImageAgent<I, O>#

Agent is the base class for all agents. It provides a mechanism for defining input/output schemas and implementing processing logic, serving as the foundation of the entire agent system.

By extending the Agent class and implementing the process method, you can create custom agents with various capabilities:

  • Process structured input and output data
  • Validate data formats using schemas
  • Communicate between agents through contexts
  • Support streaming or non-streaming responses
  • Maintain memory of past interactions
  • Output in multiple formats (JSON/text)
  • Forward tasks to other agents

Example#

Here's an example of how to create a custom agent:

class MyAgent extends Agent {
  process(input: Message): Message {
    console.log(input);

    return {
      text: "Hello, How can I assist you today?",
    };
  }
}

const agent = new MyAgent();

const result = await agent.invoke({ message: "hello" });

console.log(result); // { text: "Hello, How can I assist you today?" }

Extends#

Type Parameters#

Type Parameter

Default type

Description

I extends Message

any

The input message type the agent accepts

O extends ImageModelOutput

any

The output message type the agent returns

Indexable#

[key: symbol]: () => string | () => Promise<void>

Constructors#

Constructor#

new ImageAgent<I, O>(options): ImageAgent<I, O>

Parameters#

Parameter

Type

options

ImageAgentOptions<I, O>

Returns#

ImageAgent<I, O>

Overrides#

Agent.constructor

Properties#

tag#

tag: string = "ImageAgent"

Overrides#

Agent.tag

instructions#

instructions: PromptBuilder

inputFileKey?#

optional inputFileKey: string

modelOptions?#

optional modelOptions: Record<string, any>

outputFileType?#

optional outputFileType: FileType

Methods#

from()#

static from<I, O>(options): ImageAgent<I, O>

Type Parameters#

Type Parameter

Default type

I extends Message

any

O extends ImageModelOutput

any

Parameters#

Parameter

Type

options

ImageAgentOptions<I, O>

Returns#

ImageAgent<I, O>

process()#

process(input, options): Promise<O>

Core processing method of the agent, must be implemented in subclasses

This is the main functionality implementation of the agent, processing input and generating output. Can return various types of results:

  • Regular object response
  • Streaming response
  • Async generator
  • Another agent instance (transfer agent)
Parameters#

Parameter

Type

Description

input

I

Input message

options

AgentInvokeOptions

Options for agent invocation

Returns#

Promise<O>

Processing result

Examples#

Example of returning a direct object:

class DirectResponseAgent extends Agent {
  process(input: Message): Message {
    // Process input and return a direct object response
    return {
      text: `Hello, I received your message: ${JSON.stringify(input)}`,
      confidence: 0.95,
      timestamp: new Date().toISOString(),
    };
  }
}

const agent = new DirectResponseAgent();

const result = await agent.invoke({ message: "Hello" });

console.log(result); // { text: "Hello, I received your message: { message: 'Hello' }", confidence: 0.95, timestamp: "2023-10-01T12:00:00Z" }

Example of returning a streaming response:

class StreamResponseAgent extends Agent {
  process(_input: Message): AgentResponseStream<Message> {
    // Return a ReadableStream as a streaming response
    return new ReadableStream({
      start(controller) {
        controller.enqueue(textDelta({ text: "Hello" }));
        controller.enqueue(textDelta({ text: ", " }));
        controller.enqueue(textDelta({ text: "This" }));
        controller.enqueue(textDelta({ text: " is" }));
        controller.enqueue(textDelta({ text: "..." }));
        controller.close();
      },
    });
  }
}

const agent = new StreamResponseAgent();
const stream = await agent.invoke({ message: "Hello" }, { streaming: true });

let fullText = "";
for await (const chunk of stream) {
  if (isAgentResponseDelta(chunk)) {
    const text = chunk.delta.text?.text;
    if (text) fullText += text;
  }

See all 3 lines

Example of using an async generator:

class AsyncGeneratorAgent extends Agent {
  async *process(
    _input: Message,
    _options: AgentInvokeOptions,
  ): AgentProcessAsyncGenerator<Message> {
    // Use async generator to produce streaming results
    yield textDelta({ message: "This" });
    yield textDelta({ message: "," });
    yield textDelta({ message: " " });
    yield textDelta({ message: "This" });
    yield textDelta({ message: " " });
    yield textDelta({ message: "is" });
    yield textDelta({ message: "..." });

    // Optional return a JSON object at the end
    return { time: new Date().toISOString() };
  }
}

const agent = new AsyncGeneratorAgent();
const stream = await agent.invoke({ message: "Hello" }, { streaming: true });

const message: string[] = [];
let json: Message | undefined;

See all 10 lines

Example of transfer to another agent:

class SpecialistAgent extends Agent {
  process(_input: Message): Message {
    return {
      response: "This is a specialist response",
      expertise: "technical",
    };
  }
}

class MainAgent extends Agent {
  process(_input: Message): Agent {
    // Create a specialized agent for handling technical issues
    return new SpecialistAgent();
  }
}

const aigne = new AIGNE({});
const mainAgent = new MainAgent();

const result = await aigne.invoke(mainAgent, { message: "technical question" });
console.log(result); // { response: "This is a specialist response", expertise: "technical" }
Overrides#

Agent.process

Interfaces#

ImageAgentOptions<I, O>#

Extends#

Type Parameters#

Type Parameter

Default type

I extends Message

any

O extends ImageModelOutput

any

Properties#

Property

Type

instructions

string | PromptBuilder

inputFileKey?

string

modelOptions?

Record<string, any>

outputFileType?

FileType

Variables#

imageAgentOptionsSchema#

const imageAgentOptionsSchema: ZodObject<{ [key in keyof ImageAgentOptions]: ZodType<ImageAgentOptions[key]> }>