Your agent from your backend
Your backend talks to your agent with the SDK, signing with its own key. spredr delivers what you send and what the agent answers; what you send and how it reads it is your code's business.
npm install @spredr/sdkLink your backend
With your spredr token, from a script or as you deploy. spredr writes the backend's key into its variables (SPREDR_CALLER_ID, SPREDR_CALLER_KEY, SPREDR_AGENT, SPREDR_AI_URL, SPREDR_AI_KEY) and deploys it: the private key never passes through you.
import { Spredr } from "@spredr/sdk";
const spredr = new Spredr({ token: process.env.SPREDR_TOKEN });
await spredr.agent("shop").addCaller("shop-api", { service: "shop-api" });You can also do it in the panel, in your agent's Connections tab. To change the key, remove the link and make it again: the old one stops working at once.
Ask it for a task
Caller reads the variables spredr left. for() is the customer it works for (a shop, an account), with its own memory and limits. With key, asking again is the same task; with thread, its tasks read as one conversation.
import { Caller } from "@spredr/sdk";
const agent = new Caller();
const shop = agent.for(shopId);
const { reply } = await shop.result(
"reply",
{ text },
{ thread: `chat:${customerId}`, key: messageId },
);Give it context
You decide what you give it and in what form; spredr makes sure it arrives. How the agent reads it is its code's business.
await shop.attach(`chat:${customerId}`, "prices.csv", csv, {
type: "text/csv",
});agent.on("reply", async (input, ctx) => {
const prices = await ctx.file("prices.csv");
const text = new TextDecoder().decode(prices.data);
return { reply: await answer(input.text, text) };
});The answer as it is written
If your agent uses ctx.stream, live() gives you each piece in order and then the finished task. If the connection drops, it goes on where it left off.
for await (const event of shop.live("reply", { text })) {
if (event.type === "delta") send(event.text);
if (event.type === "end") done(event.task);
}