Agent versions

Edit and publish an agent. Restore a version into the draft or return to an earlier publication, then check the changes in the history.

Before you start

  • A Platica API key in PLATIC_API_KEY.
  • Node.js 20.3 or newer and npm install platic-sdk.

Draft and publication changes

  • Creates an agent with v1 as its draft and published version.
  • Updates the draft to v2. The agent keeps replying with the published version, v1.
  • Publishes the draft. From the next message the agent answers with v2.
  • Restores v1 into the draft and creates v3. The published version remains v2.
  • Returns to v1 with rollback because it was previously published.
  • Tries to roll back to v3, which was never published: a ConflictError with NOT_PREVIOUSLY_PUBLISHED, and nothing changes.
  • Lists the versions and the publication history with iterate.

Important: This example keeps the agent so you can look at its history. Delete it when you are done with platic.agents.delete(agentId).

The code

import assert from "node:assert/strict";
import { ConflictError, Platic } from "platic-sdk";

const apiKey = process.env.PLATIC_API_KEY;
if (!apiKey) {
  console.error("Set PLATIC_API_KEY to a Platica API key (pl_key_…).");
  process.exit(1);
}

const platic = new Platic({ apiKey });

// 1. Create. The agent is born published: v1 is both its draft and its publication.
const agent = await platic.agents.create({ name: `SDK example ${new Date().toISOString()}`, instructions: "You are the sales assistant." });
assert.equal(agent.publication.versionId, "v1");

// 2. Change the draft. This saves v2; production keeps answering with v1.
const saved = await platic.agents.draft.update(agent.id, { instructions: "You are the sales assistant. Answer in English.", note: "First prompt" });
assert.equal(saved.version.versionId, "v2");
assert.equal((await platic.agents.publication.get(agent.id)).publication.versionId, "v1");

// 3. Publish the draft. From the next message the agent answers with v2.
const published = await platic.agents.publish(agent.id, { note: "Go live" });
assert.equal(published.publication.versionId, "v2");

// 4. Restore v1 into the draft. It becomes v3; production still runs v2.
const restored = await platic.agents.draft.restore(agent.id, { versionId: "v1" });
assert.equal(restored.version.restoredFromVersionId, "v1");
assert.equal((await platic.agents.publication.get(agent.id)).publication.versionId, "v2");

// 5. Roll production back to v1, a version that was published before.
const rolledBack = await platic.agents.rollback(agent.id, { versionId: "v1" });
assert.equal(rolledBack.publication.versionId, "v1");

// 6. Rolling back to a version never published is a conflict you can see, and nothing changes.
try {
  await platic.agents.rollback(agent.id, { versionId: "v3" });
  assert.fail("v3 was never published");
} catch (error) {
  if (!(error instanceof ConflictError)) throw error;
  assert.equal(error.code, "NOT_PREVIOUSLY_PUBLISHED");
}

const versions: string[] = [];
for await (const version of platic.agents.versions.iterate(agent.id)) versions.push(version.versionId);
const history: string[] = [];
for await (const entry of platic.agents.publications.iterate(agent.id)) history.push(`${entry.action} ${entry.versionId}`);
assert.deepEqual(versions, ["v3", "v2", "v1"]);
assert.deepEqual(history, ["rollback v1", "publish v2", "create v1"]);

console.log(JSON.stringify({ agentId: agent.id, versions, history }));

Run it

Save the code in versions.ts and run it with tsx.

Set your API key and run the script:

export PLATIC_API_KEY=pl_key_...
npx tsx versions.ts

What to expect

{"agentId":"ag_…","versions":["v3","v2","v1"],"history":["rollback v1","publish v2","create v1"]}

Each step includes a check. If it fails, Node shows which one and stops the script. The agent is kept so you can inspect it in Platica. See also Versions and Publish history.

API reference used

Next steps