Nirmos
Prompt management

Roll back a prompt release

Restore an earlier immutable release while preserving saved branch heads.

This disposable example creates and deletes its own prompt, so it needs prompt:create, prompt:read, prompt:publish and prompt:delete. Generate a key in the Dashboard. Install @nirmos/sdk and tsx, save the code as prompt-rollback.ts, set NIRMOS_API_KEY, NIRMOS_API_URL=https://api.nirmos.com and NIRMOS_RUNTIME_URL=https://runtime.nirmos.com, then run npx tsx prompt-rollback.ts. No provider inference is needed.

This disposable example creates V1 and V2, leaves V3 saved, rolls back to V1 and restores V2. It checks active content and saved head, then deletes its own prompt. Set NIRMOS_API_KEY and optional API/runtime URLs, then run with tsx or use the packaged examples/prompt-rollback.mjs. Expected output is V1, V2, V1, V2.

prompt-rollback.ts
import { Nirmos } from "@nirmos/sdk";

/** Disposable management rollback example. No provider call is needed. */
export async function promptRollback(nirmos: Nirmos) {
  const suffix = `rollback-${Date.now()}`;
  const prompt = await nirmos.prompts.create({ name: suffix, slug: suffix });
  const save = async (text: string, parentVersionId?: string) => {
    const version = await nirmos.prompts.versions.create(prompt.promptId, {
      branchId: prompt.branchId,
      parentVersionId,
      commitMessage: text,
      temperature: 0,
      maxTokens: 64,
      topP: 1,
      messages: [
        {
          order: 0,
          role: "system",
          contentBlocks: [{ order: 0, type: "text", text }],
        },
      ],
    });
    if (!version.versionId) throw new Error("Missing saved version ID");
    return version.versionId;
  };
  const active = async (versionId: string, text: string) => {
    const released = await nirmos.managedPrompts.retrieveActive(
      prompt.promptId,
      { cache: "reload" },
    );
    const rendered = await nirmos.managedPrompts.render(released, {});
    if (
      released.version.id !== versionId ||
      rendered.messages[0]?.content !== text
    )
      throw new Error("Published content differs from the requested release");
    console.log(rendered.messages[0].content);
  };
  try {
    const v1 = await save("V1: Answer concisely.");
    await nirmos.prompts.versions.publish(prompt.promptId, v1);
    await active(v1, "V1: Answer concisely.");
    const v2 = await save("V2: Include policy citations.", v1);
    await nirmos.prompts.versions.publish(prompt.promptId, v2);
    await active(v2, "V2: Include policy citations.");
    const saved = await save("V3: Unpublished work.", v2);
    await nirmos.prompts.versions.publish(prompt.promptId, v1);
    await active(v1, "V1: Answer concisely.");
    const branches = await nirmos.prompts.branches.list(prompt.promptId);
    if (
      branches.items.find((branch) => branch.id === prompt.branchId)
        ?.headVersionId !== saved
    )
      throw new Error("Rollback changed the saved branch head");
    await nirmos.prompts.versions.publish(prompt.promptId, v2);
    await active(v2, "V2: Include policy citations.");
    return { v1, v2, saved };
  } finally {
    await nirmos.prompts.delete(prompt.promptId);
  }
}

// This example remains type-checkable without Node type dependencies.
declare const process: { env: Record<string, string | undefined> };

// NIRMOS_API_KEY=... node prompt-rollback.mjs (or run the .ts file with tsx).
if (process.env.NIRMOS_API_KEY) {
  await promptRollback(
    new Nirmos({
      auth: { type: "api-key", key: process.env.NIRMOS_API_KEY },
      apiBaseUrl: process.env.NIRMOS_API_URL ?? "http://localhost:4001",
      runtimeBaseUrl: process.env.NIRMOS_RUNTIME_URL ?? "http://localhost:4000",
      maxRetries: 0,
    }),
  );
}

The example prints V1, V2, V1, V2 and deletes only its disposable prompt. Set both URL variables explicitly: this packaged example defaults to local development URLs when they are absent. Release behavior explains eligibility and conflicts.