ClientAgentJS

Initialization

ESM (Standard)

import { createAgent } from 'clientagentjs';
const agent = createAgent();

Global (Legacy/Simple)

Include the script in your HTML:

<script src="dist/clientagentjs.global.js"></script>

Then access it via the global variable:

const agent = ClientAgent.createAgent();

agent.ask(prompt, options?)

Sends a single request using the active profile and resolves to a response object.

Parameters

await agent.ask("Summarize this text", {
  context: "Optional extra context",
  signal: abortController.signal
})

Response shape

{
  text: "Assistant response text",
  raw: { /* provider response payload */ }
}

Error behavior

ask() throws when:

agent.stream(prompt, options?)

Sends a streaming request using the active profile and returns an AsyncIterable.

Parameters

for await (const chunk of agent.stream("Write a headline", {
  context: "Optional extra context",
  signal: abortController.signal
})) {
  console.log(chunk.text)
}

Chunk shape

{
  text: "Next visible text delta",
  delta: "Next visible text delta",
  raw: { /* provider event payload */ }
}

Completion behavior

Error behavior

stream() throws when:

Session methods

session.ask() and session.stream() follow the same response contracts as agent.ask() and agent.stream(), with conversation history automatically injected from the session memory.

Additional session methods:

session.getHistory()
session.clear()

Local tools

The public local tool methods are:

agent.registerTool(name, handler)
agent.unregisterTool(name)
agent.listTools()
agent.runTool(name, input)

Tool handlers may return plain JSON-compatible data or strings. When a compatible provider uses the tool through model tool-calling, the return value is serialized and injected back into the conversation.

Event contract

When createAgent({ onEvent }) is used, the agent may emit:

The event payload depends on the operation, but always follows this outer shape:

{
  type: "request:start",
  payload: { /* event-specific payload */ }
}

UI Module (Optional)

The library provides a built-in configuration panel exported from src/ui/profilePanel.js.

openConfigPanel({ agent, dialogClass? })

Opens the profile management dialog.

openMcpPanel({ agent, dialogClass? })

Opens the MCP server management dialog.

Customizing Styles

The UI uses :where() selectors for its default styling, meaning they have zero specificity. You can override any style by simply defining the class in your own CSS:

/* Example: change the look of the profile dialog */
.client-agent-js-profile-dlg {
  background: #1e1e1e;
  color: white;
  border: 1px solid #333;
}

Main classes available for customization:


See license: ClientAgentJS