import {z} from 'zod' export type GetApiV1WorkflowsWorkflowIdRevisionsDiffArgs = {workflow_id: string, base: string, target: string}; export type GetApiV1WorkflowsWorkflowIdRevisionsDiffOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const revisionDiffSide = z.object({revision_id: z.string(), name: z.string(), provenance: z.enum(["compiled", "hand_edited", "uncompiled"])}); export const textDiffSegment = z.object({op: z.enum(["equal", "insert", "delete"]), text: z.string()}); export const textDiff = z.object({segments: z.array(textDiffSegment)}); export const fieldChange = z.object({field: z.string(), op: z.enum(["added", "removed", "modified"]), text_diff: textDiff.nullable().optional(), before: z.unknown(), after: z.unknown()}); export const fieldChangeCollection = z.object({added: z.array(fieldChange).optional(), removed: z.array(fieldChange).optional(), changed: z.array(fieldChange).optional()}); export const policyNamesDiff = z.object({added: z.array(z.string()).optional(), removed: z.array(z.string()).optional()}); export const modelSettingsDiff = z.object({analyzer: z.array(fieldChange).optional(), router: z.array(fieldChange).optional()}); export const sourceDiff = z.object({instructions: textDiff, config: fieldChangeCollection.optional(), policies: policyNamesDiff.optional(), tool_input_defaults: fieldChangeCollection.optional(), model_settings: modelSettingsDiff.optional()}); export const graphSummary = z.object({nodes_added: z.number().int().optional(), nodes_removed: z.number().int().optional(), nodes_changed: z.number().int().optional(), edges_added: z.number().int().optional(), edges_removed: z.number().int().optional(), edges_changed: z.number().int().optional(), context_vars_added: z.number().int().optional(), context_vars_removed: z.number().int().optional(), context_vars_changed: z.number().int().optional()}); export const srcWorkflowDiffModelsGraphMeta = z.object({persona: textDiff}); export const projectedTool = z.object({canonical_key: z.string(), display_name: z.string(), produces_variables: z.array(z.string()).optional()}); export const projectedKnowledgeBase = z.object({kb_id: z.string(), display_name: z.string(), clearance_level: z.number().int().nullable().optional()}); export const projectedActionStep = z.object({id: z.string(), kind: z.enum(["speak", "tool_call", "knowledge_retrieval"]), text: z.string().nullable().optional(), tool_display_name: z.string().nullable().optional(), knowledge_base_names: z.array(z.string()).optional()}); export const projectedVoiceTarget = z.object({label: z.string(), when_to_use: z.string().nullable().optional()}); export const projectedVoiceHandoff = z.object({targets: z.array(projectedVoiceTarget).optional(), connecting_text: z.string().nullable().optional(), mode: z.string().optional(), max_retries: z.number().int().optional(), retry_text: z.string().nullable().optional()}); export const projectedNode = z.object({id: z.string(), kind: z.enum(["agent", "action", "handoff", "approval", "component"]), is_entry: z.boolean().optional(), name: z.string().nullable().optional(), instructions: z.string().nullable().optional(), required_writes: z.array(z.string()).optional(), writes: z.array(z.string()).optional(), done_when_readable: z.string().nullable().optional(), tools: z.array(projectedTool).optional(), knowledge_bases: z.array(projectedKnowledgeBase).optional(), allow_jump_back: z.boolean().optional(), jump_back_target: z.boolean().optional(), jump_back_when: z.string().nullable().optional(), description: z.string().nullable().optional(), failure_message: z.string().nullable().optional(), steps: z.array(projectedActionStep).optional(), result_write_names: z.array(z.string()).optional(), static_text: z.string().nullable().optional(), voice_config: projectedVoiceHandoff.nullable().optional(), on_failure: z.string().nullable().optional(), condition_readable: z.string().nullable().optional(), approver_role: z.string().nullable().optional(), suspend_message: z.string().nullable().optional(), timeout_seconds: z.number().int().nullable().optional(), on_approve: z.string().nullable().optional(), on_deny: z.string().nullable().optional(), component_workflow_id: z.string().nullable().optional(), pinned_revision_id: z.string().nullable().optional(), input_bindings: z.array(z.record(z.string(), z.string())).optional()}); export const nodeChange = z.object({id: z.string(), kind: z.enum(["agent", "action", "handoff", "approval", "component"]), changes: z.array(fieldChange).optional()}); export const nodeCollectionDiff = z.object({added: z.array(projectedNode).optional(), removed: z.array(projectedNode).optional(), changed: z.array(nodeChange).optional()}); export const projectedEdge = z.object({from_node: z.string(), to_node: z.string(), condition_readable: z.string().nullable().optional(), is_failure_path: z.boolean().optional()}); export const edgeChange = z.object({from_node: z.string(), to_node: z.string(), changes: z.array(fieldChange).optional()}); export const edgeCollectionDiff = z.object({added: z.array(projectedEdge).optional(), removed: z.array(projectedEdge).optional(), changed: z.array(edgeChange).optional()}); export const projectedContextVar = z.object({name: z.string(), type: z.string(), source: z.string(), enum_values: z.array(z.string()).nullable().optional(), reask_cap: z.number().int().nullable().optional(), description: z.string().nullable().optional()}); export const contextVarChange = z.object({name: z.string(), changes: z.array(fieldChange).optional()}); export const contextVarCollectionDiff = z.object({added: z.array(projectedContextVar).optional(), removed: z.array(projectedContextVar).optional(), changed: z.array(contextVarChange).optional()}); export const graphDiff = z.object({summary: graphSummary, graph_meta: srcWorkflowDiffModelsGraphMeta, nodes: nodeCollectionDiff.optional(), edges: edgeCollectionDiff.optional(), context_variables: contextVarCollectionDiff.optional()}); export const workflowRevisionDiffResponse = z.object({workflow_id: z.string(), base: revisionDiffSide, target: revisionDiffSide, fidelity: z.enum(["exact", "approximate", "source_only"]), source_diff: sourceDiff, graph_diff: graphDiff.nullable().optional(), component_update_only: z.boolean().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Diff two workflow revisions * * GET /v1/workflows/{workflow_id}/revisions/diff */ export const getApiV1WorkflowsWorkflowIdRevisionsDiff = async (args: GetApiV1WorkflowsWorkflowIdRevisionsDiffArgs, options: GetApiV1WorkflowsWorkflowIdRevisionsDiffOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.base != null) query.append("base", String(args.base)) if (args.target != null) query.append("target", String(args.target)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workflows/${encodeURIComponent(String(args.workflow_id))}/revisions/diff${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(workflowRevisionDiffResponse, await res.json()) };