parse()
Parse a .at file string into a TopologyAST
parse()
import { parse } from "agentopology";Parses a .at file string into a TopologyAST object. This is the entry point for all programmatic use of AgenTopology.
Signature
function parse(source: string): TopologyASTParameters
| Parameter | Type | Description |
|---|---|---|
source | string | The contents of a .at file |
Returns
A TopologyAST object representing the parsed topology. The AST contains all agents, flow edges, triggers, memory blocks, and metadata defined in the source.
Usage
import { parse } from "agentopology";
import { readFileSync } from "fs";
const source = readFileSync("my-topology.at", "utf-8");
const ast = parse(source);
console.log(ast.name); // "my-topology"
console.log(ast.patterns); // ["pipeline"]
console.log(ast.agents.length); // 3Parsing inline strings
You can also parse topology strings directly:
import { parse } from "agentopology";
const ast = parse(`
topology example : [pipeline] {
agent researcher { model: sonnet phase: 1 }
agent writer { model: sonnet phase: 2 }
flow {
intake -> researcher -> writer -> done
}
}
`);
console.log(ast.agents.map(a => a.name)); // ["researcher", "writer"]Error handling
parse() throws if the source contains syntax errors. Wrap calls in a try-catch to handle malformed input:
import { parse } from "agentopology";
try {
const ast = parse(source);
} catch (err) {
console.error("Parse error:", err.message);
}For semantic validation (missing agents, unreachable nodes, invalid conditions), use validate() after parsing.