Agents
Agent History
Work with chat history and multi-turn agent sessions.
Anvia history is a plain Message[]. For explicit stateless history, pass the whole transcript to agent.prompt([...]); the last message is the active prompt and earlier messages are history.
Basic Shape
import type { Message } from "@anvia/core";
const history: Message[] = [
{
role: "user",
content: [{ type: "text", text: "My checkout failed." }],
},
{
role: "assistant",
content: [{ type: "text", text: "What error did you see?" }],
},
];The helper API creates the same shape:
import { Message } from "@anvia/core";
const history = [
Message.user("My checkout failed."),
Message.assistant("What error did you see?"),
];Use History In a Prompt
const history = await conversations.loadMessages(conversationId);
const currentPrompt = Message.user(userInput);
const response = await agent.prompt([...history, currentPrompt]).send();
await conversations.saveMessages(conversationId, [
...history,
...response.messages,
]);response.messages is the new part of the conversation created by the run. Append it to your stored history if you want the next request to remember it.
History With Tool Calls
Tool calls are assistant content. Tool results are tool content.
import type { Message } from "@anvia/core";
const history: Message[] = [
{
role: "user",
content: [{ type: "text", text: "Where is order A-100?" }],
},
{
role: "assistant",
content: [
{
type: "tool_call",
id: "call_1",
function: {
name: "lookup_order",
arguments: { orderId: "A-100" },
},
},
],
},
{
role: "tool",
content: [
{
type: "tool_result",
id: "call_1",
content: [{ type: "text", text: "{\"status\":\"shipped\"}" }],
},
],
},
{
role: "assistant",
content: [{ type: "text", text: "Order A-100 has shipped." }],
},
];Persist the plain message objects returned by Anvia. Only create tool-call history yourself if you are importing an existing transcript or replaying a known run.
