From e7b67e0efbda34bf32512e74cfe31d13079d5d1e Mon Sep 17 00:00:00 2001 From: nuthalapativarun Date: Thu, 18 Jun 2026 02:00:04 -0700 Subject: [PATCH] feat(examples): add load-and-edit workflow SDK example in Python and TypeScript (#441) Closes #370 --- examples/README.md | 5 ++ examples/python/load_and_edit_workflow.py | 75 +++++++++++++++++++ examples/typescript/load_and_edit_workflow.ts | 63 ++++++++++++++++ examples/typescript/package.json | 3 +- 4 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 examples/python/load_and_edit_workflow.py create mode 100644 examples/typescript/load_and_edit_workflow.ts diff --git a/examples/README.md b/examples/README.md index 802ed730..b15977d8 100644 --- a/examples/README.md +++ b/examples/README.md @@ -26,6 +26,10 @@ python python/fetch_workflow_and_call.py # Create a new workflow from a definition. python python/create_workflow.py + +# Load an existing workflow, edit the startCall prompt, and save as a draft. +# Edit WORKFLOW_ID at the top of the file first. +python python/load_and_edit_workflow.py ``` ## TypeScript @@ -41,4 +45,5 @@ export DOGRAH_API_TOKEN=sk-... npm run call # fetch_workflow_and_call.ts npm run create # create_workflow.ts +npm run edit # load_and_edit_workflow.ts (edit WORKFLOW_ID in the file first) ``` diff --git a/examples/python/load_and_edit_workflow.py b/examples/python/load_and_edit_workflow.py new file mode 100644 index 00000000..e5f92157 --- /dev/null +++ b/examples/python/load_and_edit_workflow.py @@ -0,0 +1,75 @@ +"""Load an existing workflow, edit a node prompt, and save it as a draft. + +Requirements: + pip install -r requirements.txt + +Environment variables (loaded from `.env` in this directory): + DOGRAH_API_ENDPOINT - Dograh API base URL (e.g. http://localhost:8000) + DOGRAH_API_TOKEN - API token sent as X-API-Key + +Run: + python load_and_edit_workflow.py +""" + +from __future__ import annotations + +import os +import sys +from pathlib import Path + +from dotenv import load_dotenv + +from dograh_sdk import DograhClient +from dograh_sdk._generated_models import UpdateWorkflowRequest + +load_dotenv(Path(__file__).parent / ".env") + +# Replace with the numeric ID of an existing agent in your Dograh account. +WORKFLOW_ID = 0 + +# Sentence appended to the startCall node's prompt when the script runs. +PROMPT_SUFFIX = " Please be concise — keep all responses under two sentences." + + +def main() -> int: + api_endpoint = os.environ.get("DOGRAH_API_ENDPOINT", "http://localhost:8000") + api_token = os.environ.get("DOGRAH_API_TOKEN") + + if not api_token: + print("DOGRAH_API_TOKEN is required", file=sys.stderr) + return 1 + + if WORKFLOW_ID == 0: + print("Set WORKFLOW_ID at the top of this file to an existing workflow ID", file=sys.stderr) + return 1 + + with DograhClient(base_url=api_endpoint, api_key=api_token) as client: + existing = client.get_workflow(WORKFLOW_ID) + print(f"Loaded workflow {existing.id}: {existing.name!r} (status={existing.status})") + + definition = dict(existing.workflow_definition) + + for node in definition.get("nodes", []): + if node.get("type") == "startCall": + data = dict(node.get("data") or {}) + data["prompt"] = (data.get("prompt") or "") + PROMPT_SUFFIX + node["data"] = data + break + + result = client.update_workflow( + WORKFLOW_ID, + body=UpdateWorkflowRequest( + name=existing.name, + workflow_definition=definition, + ), + ) + print( + f"Saved draft for workflow {result.id}: {result.name!r} " + f"(version={result.version_number}, status={result.version_status})" + ) + + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/examples/typescript/load_and_edit_workflow.ts b/examples/typescript/load_and_edit_workflow.ts new file mode 100644 index 00000000..4c620bca --- /dev/null +++ b/examples/typescript/load_and_edit_workflow.ts @@ -0,0 +1,63 @@ +// Load an existing workflow, edit a node prompt, and save it as a draft. +// +// Requirements: +// npm install @dograh/sdk +// +// Environment variables: +// DOGRAH_API_ENDPOINT - Dograh API base URL (e.g. http://localhost:8000) +// DOGRAH_API_TOKEN - API token sent as X-API-Key +// +// Run: +// npx tsx load_and_edit_workflow.ts + +import { DograhClient } from "@dograh/sdk"; + +// Replace with the numeric ID of an existing agent in your Dograh account. +const WORKFLOW_ID = 0; + +// Sentence appended to the startCall node's prompt when the script runs. +const PROMPT_SUFFIX = " Please be concise — keep all responses under two sentences."; + +async function main(): Promise { + const apiEndpoint = process.env.DOGRAH_API_ENDPOINT ?? "http://localhost:8000"; + const apiToken = process.env.DOGRAH_API_TOKEN; + + if (!apiToken) throw new Error("DOGRAH_API_TOKEN is required"); + if (WORKFLOW_ID === 0) throw new Error("Set WORKFLOW_ID at the top of this file to an existing workflow ID"); + + const client = new DograhClient({ + baseUrl: apiEndpoint, + apiKey: apiToken, + }); + + const existing = await client.getWorkflow(WORKFLOW_ID); + console.log(`Loaded workflow ${existing.id}: ${JSON.stringify(existing.name)} (status=${existing.status})`); + + const definition = structuredClone(existing.workflow_definition) as { + nodes?: Array<{ type?: string; data?: Record }>; + }; + + for (const node of definition.nodes ?? []) { + if (node.type === "startCall") { + node.data = node.data ?? {}; + node.data.prompt = ((node.data.prompt as string) ?? "") + PROMPT_SUFFIX; + break; + } + } + + const result = await client.updateWorkflow(WORKFLOW_ID, { + body: { + name: existing.name, + workflow_definition: definition as Record, + }, + }); + console.log( + `Saved draft for workflow ${result.id}: ${JSON.stringify(result.name)} ` + + `(version=${result.version_number}, status=${result.version_status})`, + ); +} + +main().catch((err) => { + console.error(err); + process.exit(1); +}); diff --git a/examples/typescript/package.json b/examples/typescript/package.json index c48f2281..75d95e5a 100644 --- a/examples/typescript/package.json +++ b/examples/typescript/package.json @@ -5,7 +5,8 @@ "type": "module", "scripts": { "call": "tsx --env-file=.env fetch_workflow_and_call.ts", - "create": "tsx --env-file=.env create_workflow.ts" + "create": "tsx --env-file=.env create_workflow.ts", + "edit": "tsx --env-file=.env load_and_edit_workflow.ts" }, "dependencies": { "@dograh/sdk": "latest"