PtcRunner.Json (PtcRunner v0.4.1)
View SourceExecute PTC programs written in JSON DSL.
PtcRunner.Json enables LLMs to write safe programs that orchestrate tools and transform data inside a sandboxed environment using JSON syntax.
See the PTC-JSON Specification for the complete DSL reference.
Tool Registration
Tools are functions that receive a map of arguments and return results:
tools = %{
"get_user" => fn %{"id" => id} -> MyApp.Users.get(id) end,
"search" => fn %{"query" => q, "limit" => n} -> MyApp.Search.run(q, limit: n) end
}
PtcRunner.Json.run(program, tools: tools)Contract:
- Receives:
map()of arguments (may be empty%{}) - Returns: Any Elixir term (maps, lists, primitives)
- Should not raise (return
{:error, reason}for errors)
Error Handling
Use format_error/1 to convert errors into LLM-friendly messages:
case PtcRunner.Json.run(program, tools: tools) do
{:ok, result, _, _} -> handle_success(result)
{:error, error} -> retry_with_feedback(format_error(error))
endExamples
iex> program = ~s({"program": {"op": "literal", "value": 42}})
iex> {:ok, result, _memory_delta, _new_memory} = PtcRunner.Json.run(program)
iex> result
42
Summary
Functions
Formats an error into a human-readable message suitable for LLM feedback.
Runs a PTC program and returns the result with metrics and memory.
Runs a PTC program, raising on error.
Types
@type error() :: {:parse_error, String.t()} | {:validation_error, String.t()} | {:execution_error, String.t()} | {:timeout, non_neg_integer()} | {:memory_exceeded, non_neg_integer()}
Error types returned by PtcRunner.Json operations.
Execution metrics for a program run.
Functions
Formats an error into a human-readable message suitable for LLM feedback.
This function converts internal error tuples into clear, actionable messages that help LLMs understand what went wrong and how to fix their programs.
Examples
iex> PtcRunner.Json.format_error({:parse_error, "unexpected token"})
"ParseError: unexpected token"
iex> PtcRunner.Json.format_error({:validation_error, "unknown operation"})
"ValidationError: unknown operation"
Runs a PTC program and returns the result with metrics and memory.
Arguments
- program: JSON string or parsed map representing the program
- opts: Execution options
Options
:context- Map of external context data (default:%{}):memory- Map of initial memory state (default:%{}):tools- Tool registry (default:%{}):timeout- Timeout in milliseconds (default: 1000):max_heap- Max heap size in words (default: 1_250_000)
Returns
{:ok, result, memory_delta, new_memory}on success{:error, reason}on failure
The return format follows the memory contract:
- If result is not a map:
memory_deltais empty,new_memoryis unchanged - If result is a map with
"result"key:"result"value is returned, other keys merged to memory - If result is a map with
:resultkey::resultvalue is returned, other keys merged to memory - If result is a map without
"result"or:result: result is returned as-is, merged to memory
Examples
iex> {:ok, result, _memory_delta, _new_memory} = PtcRunner.Json.run(~s({"program": {"op": "literal", "value": 42}}))
iex> result
42
Runs a PTC program, raising on error.
Arguments
- program: JSON string or parsed map representing the program
- opts: Execution options (same as
run/2)
Returns
- The result value (memory is discarded)
Raises
- Raises an error if parsing, validation, or execution fails
Examples
iex> result = PtcRunner.Json.run!(~s({"program": {"op": "literal", "value": 42}}))
iex> result
42