Agents
History
Work with chat history and multi-turn agent sessions.
Anvia history is a plain Message[]. You choose where to store it, then pass it into each new prompt with .withHistory(...).
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 response = await agent
.prompt(userInput)
.withHistory(history)
.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 user 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: "user",
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.
