Skip to content

Commit f46673f

Browse files
committed
fix ConfigWorkflow example
1 parent 13fa283 commit f46673f

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

examples/config_workflow.exs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ Code.require_file("lib/agent_forge/flow.ex")
1111
Code.require_file("lib/agent_forge/primitives.ex")
1212
Code.require_file("lib/agent_forge/config.ex")
1313

14-
# Add the YamlElixir application
15-
Application.ensure_all_started(:yaml_elixir)
16-
1714
defmodule ConfigWorkflow do
1815
alias AgentForge.{Flow, Signal, Primitives}
1916

@@ -35,14 +32,18 @@ defmodule ConfigWorkflow do
3532
fn signal, state ->
3633
result = Enum.reduce_while(config["validate"], {:ok, signal.data}, fn rule, {:ok, acc} ->
3734
case validate_field(acc, rule["field"], rule) do
38-
{:ok, _} -> {:cont, {:ok, acc}}
39-
{:error, reason} -> {:halt, {:error, reason}}
35+
{:ok, _} ->
36+
{:cont, {:ok, acc}}
37+
{:error, reason} ->
38+
{:halt, {:error, reason}}
4039
end
4140
end)
4241

4342
case result do
44-
{:ok, data} -> {Signal.emit(:validated, data), state}
45-
{:error, reason} -> {Signal.emit(:validation_error, reason), state}
43+
{:ok, data} ->
44+
{Signal.emit(:validated, data), state}
45+
{:error, reason} ->
46+
{Signal.halt(reason), state}
4647
end
4748
end
4849
end
@@ -105,26 +106,10 @@ defmodule ConfigWorkflow do
105106
end
106107
end
107108

108-
def load_workflow(path) do
109-
if File.exists?(path) do
110-
case YamlElixir.read_from_file(path) do
111-
{:ok, workflow} -> workflow
112-
{:error, reason} ->
113-
IO.puts("Error reading YAML file: #{inspect(reason)}")
114-
IO.puts("Using default workflow configuration.")
115-
# Use the hard-coded workflow directly
116-
default_workflow()
117-
end
118-
else
119-
IO.puts("Warning: YAML file not found at #{path}")
120-
IO.puts("Using default workflow configuration.")
121-
# Use the hard-coded workflow directly
122-
default_workflow()
123-
end
124-
end
125-
126-
# Hard-coded sample workflow for demonstration
127-
defp default_workflow do
109+
@doc """
110+
Load workflow configuration
111+
"""
112+
def load_workflow(_path) do
128113
%{
129114
"steps" => [
130115
%{
@@ -185,10 +170,11 @@ defmodule ConfigWorkflow do
185170

186171
def format_error({:validation_error, message}), do: "Validation error: #{message}"
187172
def format_error({:error, message}) when is_binary(message), do: message
173+
def format_error({:badmap, message}) when is_binary(message), do: message
188174
def format_error(reason), do: "Error: #{inspect(reason)}"
189175

190176
def run do
191-
# Load workflow configuration
177+
# Load workflow configuration from YAML
192178
workflow = load_workflow("examples/workflows/sample.yaml")
193179

194180
# Create handlers from configuration
@@ -209,14 +195,30 @@ defmodule ConfigWorkflow do
209195
signal = Signal.new(:user_data, data)
210196
state = %{}
211197

212-
case Flow.process(handlers, signal, state) do
213-
{:ok, result, _} ->
198+
case process_with_error_handling(handlers, signal, state) do
199+
{:ok, result} ->
214200
IO.puts("Success: #{inspect(result)}")
215201
{:error, reason} ->
216-
IO.puts("Error: #{format_error(reason)}")
202+
IO.puts("Error: #{reason}")
217203
end
218204
end)
219205
end
206+
207+
defp process_with_error_handling(handlers, signal, state) do
208+
case Flow.process(handlers, signal, state) do
209+
{:ok, result, _} ->
210+
{:ok, result}
211+
{:halt, msg, _} ->
212+
{:error, msg}
213+
{:error, {:badmap, msg}} ->
214+
clean_msg = msg
215+
|> String.replace(~r/Transform error: expected a map got: "/, "")
216+
|> String.replace(~r/"$/, "")
217+
{:error, clean_msg}
218+
{:error, reason} ->
219+
{:error, format_error(reason)}
220+
end
221+
end
220222
end
221223

222224
# Run the example

0 commit comments

Comments
 (0)