Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
Loading...
Searching...
No Matches
workflow_loader.c File Reference

Declarative YAML/JSON loader for AI Workflows. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <yaml.h>
#include "cJSON.h"
#include "csilk/app/workflow.h"
Include dependency graph for workflow_loader.c:

Data Structures

struct  handler_entry_t
 

Functions

void csilk_wf_register_handler (const char *name, csilk_wf_handler_t handler)
 Register a global handler for use in declarative workflows.
 
static csilk_wf_handler_t find_handler (const char *name)
 Internal: look up a handler by name in the singly-linked list registry.
 
csilk_wf_t * csilk_wf_from_json (const char *json_str)
 Build a complete workflow from a JSON string.
 
static cJSON * parse_yaml_file (const char *path)
 Internal: parse a YAML file into a cJSON tree using libyaml.
 
csilk_wf_t * csilk_wf_load_yaml (const char *path)
 Load a workflow from a YAML file on disk.
 

Variables

static handler_entry_tg_handlers = NULL
 

Detailed Description

Declarative YAML/JSON loader for AI Workflows.


Data Structure Documentation

◆ handler_entry_t

struct handler_entry_t
Collaboration diagram for handler_entry_t:
Data Fields
csilk_wf_handler_t handler
char * name
struct handler_entry_s * next

Function Documentation

◆ csilk_wf_from_json()

csilk_wf_t * csilk_wf_from_json ( const char *  json_str)

Build a complete workflow from a JSON string.

Create a workflow from a JSON string.

Parses the JSON into a three-pass construction: Pass 1: Create all nodes from the "steps" array. Each step has an "id", optional "type" ("ai" or "handler"), and optional "config" for AI nodes. AI nodes use csilk_wf_add_ai() with prompt/model config; handler nodes use csilk_wf_add() with a registered callback. Pass 2: Create connections from the "connections" array. Each connection links a source ("from") to a target ("to"), optionally with a "condition" for conditional edges or "loop: true" for loops. Pass 3: Set error targets from each step's "on_error" field.

Parameters
json_strNull-terminated JSON string.
Returns
A new csilk_wf_t, or NULL on parse failure or empty workflow.
Note
The caller owns the returned workflow and must free it with csilk_wf_free(). Handler functions must be registered via csilk_wf_register_handler() before calling this function.

◆ csilk_wf_load_yaml()

csilk_wf_t * csilk_wf_load_yaml ( const char *  path)

Load a workflow from a YAML file on disk.

Load a workflow definition from a YAML file.

Parses the YAML into cJSON via parse_yaml_file(), then delegates to csilk_wf_from_json() for workflow construction. This two-step approach avoids duplicated parsing logic.

Parameters
pathPath to a .yaml or .yml file.
Returns
A new csilk_wf_t, or NULL if the file cannot be read or the YAML is invalid.
Note
The caller owns the returned workflow.

◆ csilk_wf_register_handler()

void csilk_wf_register_handler ( const char *  name,
csilk_wf_handler_t  handler 
)

Register a global handler for use in declarative workflows.

Parameters
nameUnique name (matches 'handler' key in YAML/JSON).
handlerFunction pointer.

◆ find_handler()

static csilk_wf_handler_t find_handler ( const char *  name)
static

Internal: look up a handler by name in the singly-linked list registry.

Parameters
nameHandler name (case-sensitive).
Returns
Handler function pointer, or NULL if not registered.

◆ parse_yaml_file()

static cJSON * parse_yaml_file ( const char *  path)
static

Internal: parse a YAML file into a cJSON tree using libyaml.

Algorithm (iterative, no recursion):

  1. Open the file and initialize a libyaml parser.
  2. Maintain a stack of cJSON containers (objects and arrays).
  3. For each YAML event:
    • MAPPING_START: push a new cJSON object onto the stack.
    • SEQUENCE_START: push a new cJSON array onto the stack.
    • SCALAR: if parent is an object, the first scalar is a key; the second scalar is the value paired with the key. If parent is an array, add the scalar directly.
    • MAPPING_END / SEQUENCE_END: pop the container stack.
  4. Return the root cJSON node.
Parameters
pathFilesystem path to the YAML file.
Returns
Root cJSON node (object or array), or NULL if the file cannot be opened or parsed.
Note
The caller must free the returned cJSON with cJSON_Delete(). YAML boolean values ("true"/"false") are converted to cJSON boolean. All other scalars are treated as strings.

Variable Documentation

◆ g_handlers

handler_entry_t* g_handlers = NULL
static