agency-lang 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/dist/backends/baseGenerator.d.ts +54 -0
  2. package/dist/backends/graphGenerator.d.ts +23 -0
  3. package/dist/backends/graphGenerator.integration.test.d.ts +1 -0
  4. package/dist/backends/index.d.ts +3 -0
  5. package/dist/backends/typescriptGenerator/builtins.d.ts +13 -0
  6. package/dist/backends/typescriptGenerator/typeToString.d.ts +5 -0
  7. package/dist/backends/typescriptGenerator/typeToZodSchema.d.ts +5 -0
  8. package/dist/backends/typescriptGenerator.d.ts +56 -0
  9. package/dist/backends/typescriptGenerator.integration.test.d.ts +1 -0
  10. package/dist/backends/typescriptGenerator.test.d.ts +1 -0
  11. package/dist/backends/utils.d.ts +4 -0
  12. package/dist/generate-graph-file.d.ts +2 -0
  13. package/dist/generate-ts-file.d.ts +2 -0
  14. package/dist/index.d.ts +3 -0
  15. package/dist/parser.d.ts +7 -0
  16. package/dist/parser.test.d.ts +1 -0
  17. package/dist/parsers/access.d.ts +6 -0
  18. package/dist/parsers/access.test.d.ts +1 -0
  19. package/dist/parsers/assignment.d.ts +3 -0
  20. package/dist/parsers/body.test.d.ts +1 -0
  21. package/dist/parsers/comment.d.ts +3 -0
  22. package/dist/parsers/comment.test.d.ts +1 -0
  23. package/dist/parsers/dataStructures.d.ts +5 -0
  24. package/dist/parsers/dataStructures.test.d.ts +1 -0
  25. package/dist/parsers/function.d.ts +9 -0
  26. package/dist/parsers/function.test.d.ts +1 -0
  27. package/dist/parsers/functionCall.d.ts +3 -0
  28. package/dist/parsers/functionCall.test.d.ts +1 -0
  29. package/dist/parsers/importStatement.d.ts +3 -0
  30. package/dist/parsers/importStatement.test.d.ts +1 -0
  31. package/dist/parsers/literals.d.ts +9 -0
  32. package/dist/parsers/literals.test.d.ts +1 -0
  33. package/dist/parsers/matchBlock.d.ts +9 -0
  34. package/dist/parsers/matchBlock.test.d.ts +1 -0
  35. package/dist/parsers/parserUtils.d.ts +1 -0
  36. package/dist/parsers/returnStatement.d.ts +3 -0
  37. package/dist/parsers/tools.d.ts +3 -0
  38. package/dist/parsers/tools.test.d.ts +1 -0
  39. package/dist/parsers/typeHints.d.ts +22 -0
  40. package/dist/parsers/typeHints.test.d.ts +1 -0
  41. package/dist/parsers/utils.d.ts +5 -0
  42. package/dist/parsers/whileLoop.test.d.ts +1 -0
  43. package/dist/templates/backends/graphGenerator/builtinTools.d.ts +4 -0
  44. package/dist/templates/backends/graphGenerator/conditionalEdge.d.ts +7 -0
  45. package/dist/templates/backends/graphGenerator/edge.d.ts +7 -0
  46. package/dist/templates/backends/graphGenerator/goToNode.d.ts +7 -0
  47. package/dist/templates/backends/graphGenerator/graphNode.d.ts +9 -0
  48. package/dist/templates/backends/graphGenerator/imports.d.ts +6 -0
  49. package/dist/templates/backends/graphGenerator/node.d.ts +7 -0
  50. package/dist/templates/backends/graphGenerator/promptNode.d.ts +8 -0
  51. package/dist/templates/backends/graphGenerator/startNode.d.ts +6 -0
  52. package/dist/templates/backends/typescriptGenerator/builtinFunctions/fetch.d.ts +4 -0
  53. package/dist/templates/backends/typescriptGenerator/builtinFunctions/fetchJSON.d.ts +4 -0
  54. package/dist/templates/backends/typescriptGenerator/builtinFunctions/input.d.ts +4 -0
  55. package/dist/templates/backends/typescriptGenerator/builtinFunctions/read.d.ts +4 -0
  56. package/dist/templates/backends/typescriptGenerator/builtinTools.d.ts +4 -0
  57. package/dist/templates/backends/typescriptGenerator/functionDefinition.d.ts +8 -0
  58. package/dist/templates/backends/typescriptGenerator/imports.d.ts +4 -0
  59. package/dist/templates/backends/typescriptGenerator/promptFunction.d.ts +12 -0
  60. package/dist/templates/backends/typescriptGenerator/tool.d.ts +9 -0
  61. package/dist/templates/backends/typescriptGenerator/toolCall.d.ts +6 -0
  62. package/dist/types/access.d.ts +22 -0
  63. package/dist/types/dataStructures.d.ts +14 -0
  64. package/dist/types/function.d.ts +19 -0
  65. package/dist/types/graphNode.d.ts +14 -0
  66. package/dist/types/importStatement.d.ts +5 -0
  67. package/dist/types/literals.d.ts +26 -0
  68. package/dist/types/matchBlock.d.ts +17 -0
  69. package/dist/types/returnStatement.d.ts +6 -0
  70. package/dist/types/tools.d.ts +4 -0
  71. package/dist/types/typeHints.d.ts +48 -0
  72. package/dist/types/whileLoop.d.ts +6 -0
  73. package/dist/types.d.ts +37 -0
  74. package/dist/utils.d.ts +3 -0
  75. package/package.json +1 -1
@@ -0,0 +1,54 @@
1
+ import { AgencyComment, AgencyNode, AgencyProgram, Assignment, Literal, PromptLiteral, TypeAlias, TypeHint, TypeHintMap, VariableType } from "../types.js";
2
+ import { AccessExpression, DotFunctionCall, DotProperty, IndexAccess } from "../types/access.js";
3
+ import { AgencyArray, AgencyObject } from "../types/dataStructures.js";
4
+ import { FunctionCall, FunctionDefinition } from "../types/function.js";
5
+ import { GraphNodeDefinition } from "../types/graphNode.js";
6
+ import { ImportStatement } from "../types/importStatement.js";
7
+ import { MatchBlock } from "../types/matchBlock.js";
8
+ import { UsesTool } from "../types/tools.js";
9
+ import { WhileLoop } from "../types/whileLoop.js";
10
+ export declare class BaseGenerator {
11
+ protected typeHints: TypeHintMap;
12
+ protected graphNodes: string[];
13
+ protected generatedStatements: string[];
14
+ protected generatedTypeAliases: string[];
15
+ protected functionScopedVariables: string[];
16
+ protected toolsUsed: string[];
17
+ protected typeAliases: Record<string, VariableType>;
18
+ protected functionsUsed: Set<string>;
19
+ protected importStatements: string[];
20
+ protected functionSignatures: Record<string, string[]>;
21
+ constructor();
22
+ generate(program: AgencyProgram): {
23
+ output: string;
24
+ };
25
+ protected generateBuiltins(): string;
26
+ protected processTypeAlias(node: TypeAlias): string;
27
+ protected processTypeHint(node: TypeHint): string;
28
+ protected collectFunctionSignature(node: FunctionDefinition): void;
29
+ protected processGraphNodeName(node: GraphNodeDefinition): void;
30
+ protected processNode(node: AgencyNode): string;
31
+ protected processWhileLoop(node: WhileLoop): string;
32
+ protected processImportStatement(node: ImportStatement): string;
33
+ protected processTool(node: FunctionDefinition): string;
34
+ protected processUsesTool(node: UsesTool): string;
35
+ protected processGraphNode(node: GraphNodeDefinition): string;
36
+ protected processAgencyObject(node: AgencyObject): string;
37
+ protected processAgencyArray(node: AgencyArray): string;
38
+ protected processComment(node: AgencyComment): string;
39
+ protected processReturnStatement(node: AgencyNode): string;
40
+ protected processAccessExpression(node: AccessExpression): string;
41
+ protected processMatchBlock(node: MatchBlock): string;
42
+ protected processDotProperty(node: DotProperty): string;
43
+ protected processDotFunctionCall(node: DotFunctionCall): string;
44
+ protected processIndexAccess(node: IndexAccess): string;
45
+ protected processAssignment(node: Assignment): string;
46
+ protected processPromptLiteral(variableName: string, node: PromptLiteral): string;
47
+ protected processFunctionDefinition(node: FunctionDefinition): string;
48
+ protected processFunctionCall(node: FunctionCall): string;
49
+ protected generateFunctionCallExpression(node: FunctionCall): string;
50
+ protected generateLiteral(literal: Literal): string;
51
+ protected generateImports(): string;
52
+ protected preprocess(): string;
53
+ protected postprocess(): string;
54
+ }
@@ -0,0 +1,23 @@
1
+ import { AgencyProgram, FunctionCall, TypeHintMap, VariableType } from "../types.js";
2
+ import { TypeScriptGenerator } from "./typescriptGenerator.js";
3
+ import { GraphNodeDefinition } from "../types/graphNode.js";
4
+ import { ReturnStatement } from "../types/returnStatement.js";
5
+ export declare class GraphGenerator extends TypeScriptGenerator {
6
+ protected typeHints: TypeHintMap;
7
+ protected generatedStatements: string[];
8
+ protected generatedTypeAliases: string[];
9
+ protected typeAliases: Record<string, VariableType>;
10
+ protected functionsUsed: Set<string>;
11
+ protected adjacentNodes: Record<string, string[]>;
12
+ protected currentAdjacentNodes: string[];
13
+ protected isInsideGraphNode: boolean;
14
+ constructor();
15
+ protected processReturnStatement(node: ReturnStatement): string;
16
+ protected processGraphNodeName(node: GraphNodeDefinition): void;
17
+ protected processGraphNode(node: GraphNodeDefinition): string;
18
+ protected processFunctionCall(node: FunctionCall): string;
19
+ protected generateNodeCallExpression(node: FunctionCall): string;
20
+ protected generateImports(): string;
21
+ protected postprocess(): string;
22
+ }
23
+ export declare function generateGraph(program: AgencyProgram): string;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export * from "./baseGenerator.js";
2
+ export * from "./typescriptGenerator.js";
3
+ export * from "./graphGenerator.js";
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Maps Agency built-in function names to TypeScript equivalents
3
+ */
4
+ export declare const BUILTIN_FUNCTIONS: Record<string, string>;
5
+ /**
6
+ * Maps an Agency function name to its TypeScript equivalent
7
+ * Returns the original name if not a built-in
8
+ */
9
+ export declare function mapFunctionName(functionName: string): string;
10
+ /**
11
+ * Generates helper functions for built-in Agency functions
12
+ */
13
+ export declare function generateBuiltinHelpers(functionsUsed: Set<string>): string;
@@ -0,0 +1,5 @@
1
+ import { VariableType } from "../../types.js";
2
+ /**
3
+ * Converts a VariableType to a string representation for naming/logging
4
+ */
5
+ export declare function variableTypeToString(variableType: VariableType, typeAliases: Record<string, VariableType>): string;
@@ -0,0 +1,5 @@
1
+ import { VariableType } from "../../types.js";
2
+ /**
3
+ * Maps Agency types to Zod schema strings
4
+ */
5
+ export declare function mapTypeToZodSchema(variableType: VariableType, typeAliases: Record<string, VariableType>): string;
@@ -0,0 +1,56 @@
1
+ import { AgencyComment, AgencyProgram, Assignment, Literal, PromptLiteral, PromptSegment, TypeAlias, TypeHint, TypeHintMap } from "../types.js";
2
+ import { AccessExpression, DotFunctionCall, DotProperty, IndexAccess } from "../types/access.js";
3
+ import { AgencyArray, AgencyObject } from "../types/dataStructures.js";
4
+ import { FunctionCall, FunctionDefinition } from "../types/function.js";
5
+ import { MatchBlock } from "../types/matchBlock.js";
6
+ import { BaseGenerator } from "./baseGenerator.js";
7
+ import { ReturnStatement } from "../types/returnStatement.js";
8
+ import { UsesTool } from "../types/tools.js";
9
+ import { ImportStatement } from "../types/importStatement.js";
10
+ import { WhileLoop } from "../types/whileLoop.js";
11
+ export declare class TypeScriptGenerator extends BaseGenerator {
12
+ constructor();
13
+ protected generateBuiltins(): string;
14
+ protected processTypeAlias(node: TypeAlias): string;
15
+ protected typeAliasToString(node: TypeAlias): string;
16
+ protected processTypeHint(node: TypeHint): string;
17
+ protected processAgencyObject(node: AgencyObject): string;
18
+ protected processAgencyArray(node: AgencyArray): string;
19
+ protected processComment(node: AgencyComment): string;
20
+ protected processReturnStatement(node: ReturnStatement): string;
21
+ protected processAccessExpression(node: AccessExpression): string;
22
+ protected processMatchBlock(node: MatchBlock): string;
23
+ protected processDotProperty(node: DotProperty): string;
24
+ protected processDotFunctionCall(node: DotFunctionCall): string;
25
+ protected processIndexAccess(node: IndexAccess): string;
26
+ protected processAssignment(node: Assignment): string;
27
+ protected processPromptLiteral(variableName: string, node: PromptLiteral): string;
28
+ protected processTool(node: FunctionDefinition): string;
29
+ protected processUsesTool(node: UsesTool): string;
30
+ /**
31
+ * Process a function definition node
32
+ */
33
+ protected processFunctionDefinition(node: FunctionDefinition): string;
34
+ /**
35
+ * Process a function call node
36
+ */
37
+ protected processFunctionCall(node: FunctionCall): string;
38
+ /**
39
+ * Generates TypeScript expression for a function call (without semicolon)
40
+ */
41
+ protected generateFunctionCallExpression(node: FunctionCall): string;
42
+ protected generateLiteral(literal: Literal): string;
43
+ protected generateImports(): string;
44
+ buildPromptString(segments: PromptSegment[], typeHints: TypeHintMap): string;
45
+ /**
46
+ * Generates an async for prompt-based assignments
47
+ */
48
+ generatePromptFunction({ variableName, functionArgs, prompt, }: {
49
+ variableName: string;
50
+ functionArgs: string[];
51
+ prompt: PromptLiteral;
52
+ }): string;
53
+ protected processImportStatement(node: ImportStatement): string;
54
+ protected processWhileLoop(node: WhileLoop): string;
55
+ }
56
+ export declare function generateTypeScript(program: AgencyProgram): string;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import { AccessExpression, FunctionCall, Literal } from "../types.js";
2
+ import { AgencyObject, AgencyArray } from "../types/dataStructures.js";
3
+ import { ReturnStatement } from "../types/returnStatement.js";
4
+ export declare const wrapInReturn: (node: AccessExpression | FunctionCall | Literal | AgencyObject | AgencyArray) => ReturnStatement;
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,3 @@
1
+ export * from "./parser.js";
2
+ export * from "./types.js";
3
+ export * from "./backends/index.js";
@@ -0,0 +1,7 @@
1
+ import { AgencyNode, AgencyProgram } from "./types.js";
2
+ import { Parser, ParserResult } from "tarsec";
3
+ export declare const agencyNode: Parser<AgencyNode[]>;
4
+ export declare const agencyParser: Parser<AgencyProgram>;
5
+ export declare const _multilineCommentParser: Parser<string[]>;
6
+ export declare const multilineCommentParser: Parser<string[][]>;
7
+ export declare function parseAgency(input: string): ParserResult<AgencyProgram>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import { AccessExpression, DotFunctionCall, DotProperty, IndexAccess } from "../types/access.js";
2
+ import { Parser, ParserResult } from "tarsec";
3
+ export declare const dotPropertyParser: (input: string) => ParserResult<DotProperty>;
4
+ export declare const indexAccessParser: (input: string) => ParserResult<IndexAccess>;
5
+ export declare const dotFunctionCallParser: (input: string) => ParserResult<DotFunctionCall>;
6
+ export declare const accessExpressionParser: Parser<AccessExpression>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import { Assignment } from "../types.js";
2
+ import { Parser } from "tarsec";
3
+ export declare const assignmentParser: Parser<Assignment>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import { AgencyComment } from "../types.js";
2
+ import { Parser } from "tarsec";
3
+ export declare const commentParser: Parser<AgencyComment>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import { AgencyArray, AgencyObject, AgencyObjectKV } from "../types/dataStructures.js";
2
+ import { Parser } from "tarsec";
3
+ export declare const agencyArrayParser: Parser<AgencyArray>;
4
+ export declare const agencyObjectKVParser: Parser<AgencyObjectKV>;
5
+ export declare const agencyObjectParser: Parser<AgencyObject>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import { AgencyNode, DocString, FunctionDefinition } from "../types.js";
2
+ import { Parser } from "tarsec";
3
+ import { GraphNodeDefinition } from "../types/graphNode.js";
4
+ import { WhileLoop } from "../types/whileLoop.js";
5
+ export declare const docStringParser: Parser<DocString>;
6
+ export declare const bodyParser: Parser<AgencyNode[]>;
7
+ export declare const whileLoopParser: Parser<WhileLoop>;
8
+ export declare const functionParser: Parser<FunctionDefinition>;
9
+ export declare const graphNodeParser: Parser<GraphNodeDefinition>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import { FunctionCall } from "../types.js";
2
+ import { Parser } from "tarsec";
3
+ export declare const functionCallParser: Parser<FunctionCall>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import { ImportStatement } from "../types/importStatement.js";
2
+ import { Parser } from "tarsec";
3
+ export declare const importStatmentParser: Parser<ImportStatement>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import { InterpolationSegment, Literal, NumberLiteral, PromptLiteral, StringLiteral, TextSegment, VariableNameLiteral } from "../types.js";
2
+ import { Parser } from "tarsec";
3
+ export declare const textSegmentParser: Parser<TextSegment>;
4
+ export declare const interpolationSegmentParser: Parser<InterpolationSegment>;
5
+ export declare const promptParser: Parser<PromptLiteral>;
6
+ export declare const numberParser: Parser<NumberLiteral>;
7
+ export declare const stringParser: Parser<StringLiteral>;
8
+ export declare const variableNameParser: Parser<VariableNameLiteral>;
9
+ export declare const literalParser: Parser<Literal>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import { Parser } from "tarsec";
2
+ import { DefaultCase, MatchBlockCase } from "../types/matchBlock.js";
3
+ export declare const defaultCaseParser: Parser<DefaultCase>;
4
+ export declare const matchBlockParserCase: Parser<MatchBlockCase>;
5
+ export declare const matchBlockParser: Parser<{
6
+ type: "matchBlock";
7
+ expression: import("../types.js").Literal;
8
+ cases: (import("../types.js").AgencyComment | MatchBlockCase)[];
9
+ }>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare const optionalSemicolon: import("tarsec").Parser<";" | null>;
@@ -0,0 +1,3 @@
1
+ import { ReturnStatement } from "../types/returnStatement.js";
2
+ import { Parser } from "tarsec";
3
+ export declare const returnStatementParser: Parser<ReturnStatement>;
@@ -0,0 +1,3 @@
1
+ import { UsesTool } from "../types/tools.js";
2
+ import { Parser } from "tarsec";
3
+ export declare const usesToolParser: Parser<UsesTool>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,22 @@
1
+ import { ArrayType, BooleanLiteralType, NumberLiteralType, ObjectProperty, ObjectType, PrimitiveType, StringLiteralType, TypeAlias, TypeAliasVariable, TypeHint, UnionType, VariableType } from "../types.js";
2
+ import { Parser } from "tarsec";
3
+ export declare const primitiveTypeParser: Parser<PrimitiveType>;
4
+ export declare const typeAliasVariableParser: Parser<TypeAliasVariable>;
5
+ export declare const arrayTypeParser: Parser<ArrayType>;
6
+ export declare const angleBracketsArrayTypeParser: Parser<ArrayType>;
7
+ export declare const stringLiteralTypeParser: Parser<StringLiteralType>;
8
+ export declare const numberLiteralTypeParser: Parser<NumberLiteralType>;
9
+ export declare const booleanLiteralTypeParser: Parser<BooleanLiteralType>;
10
+ export declare const objectPropertyDelimiter: Parser<(string[] | ";")[]>;
11
+ export declare const objectPropertyParser: Parser<ObjectProperty>;
12
+ export declare const objectPropertyDescriptionParser: Parser<{
13
+ description: string;
14
+ }>;
15
+ export declare const objectPropertyWithDescriptionParser: Parser<ObjectProperty>;
16
+ export declare const objectTypeParser: Parser<ObjectType>;
17
+ export declare const unionItemParser: Parser<VariableType>;
18
+ export declare const _unionTypeParser: Parser<UnionType>;
19
+ export declare const unionTypeParser: Parser<UnionType>;
20
+ export declare const variableTypeParser: Parser<VariableType>;
21
+ export declare const typeHintParser: Parser<TypeHint>;
22
+ export declare const typeAliasParser: Parser<TypeAlias>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import { Parser } from "tarsec";
2
+ export declare const optionalSpaces: Parser<string[]>;
3
+ export declare const backtick: Parser<"`">;
4
+ export declare const comma: Parser<(string[] | ",")[]>;
5
+ export declare const varNameChar: Parser<string>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export declare const template = "function add({a, b}: {a:number, b:number}):number {\n return a + b;\n}\n\n// Define the function tool for OpenAI\nconst addTool = {\n type: \"function\" as const,\n function: {\n name: \"add\",\n description:\n \"Adds two numbers together and returns the result.\",\n parameters: {\n type: \"object\",\n properties: {\n a: {\n type: \"number\",\n description: \"The first number to add\",\n },\n b: {\n type: \"number\",\n description: \"The second number to add\",\n },\n },\n required: [\"a\", \"b\"],\n additionalProperties: false,\n },\n },\n };";
2
+ export type TemplateType = {};
3
+ declare const render: (args: TemplateType) => string;
4
+ export default render;
@@ -0,0 +1,7 @@
1
+ export declare const template = "graph.conditionalEdge(\"{{{fromNode}}}\", {{{toNodes}}});\n";
2
+ export type TemplateType = {
3
+ fromNode: string | boolean | number;
4
+ toNodes: string | boolean | number;
5
+ };
6
+ declare const render: (args: TemplateType) => string;
7
+ export default render;
@@ -0,0 +1,7 @@
1
+ export declare const template = "graph.edge(\"{{{fromNode}}}\", \"{{{toNode}}}\");\n";
2
+ export type TemplateType = {
3
+ fromNode: string | boolean | number;
4
+ toNode: string | boolean | number;
5
+ };
6
+ declare const render: (args: TemplateType) => string;
7
+ export default render;
@@ -0,0 +1,7 @@
1
+ export declare const template = "goToNode(\"{{{nodeName:string}}}\", { messages: state.messages, data: {{{data:string}}} });";
2
+ export type TemplateType = {
3
+ nodeName: string;
4
+ data: string;
5
+ };
6
+ declare const render: (args: TemplateType) => string;
7
+ export default render;
@@ -0,0 +1,9 @@
1
+ export declare const template = "graph.node(\"{{{name}}}\", async (state) => {\n {{#hasParam}}\n const {{{paramName}}} = state.data;\n {{/hasParam}}\n {{{body}}}\n});\n";
2
+ export type TemplateType = {
3
+ name: string | boolean | number;
4
+ hasParam: boolean;
5
+ paramName: string | boolean | number;
6
+ body: string | boolean | number;
7
+ };
8
+ declare const render: (args: TemplateType) => string;
9
+ export default render;
@@ -0,0 +1,6 @@
1
+ export declare const template = "import OpenAI from \"openai\";\nimport { zodResponseFormat } from \"openai/helpers/zod\";\nimport { z } from \"zod\";\nimport * as readline from \"readline\";\nimport fs from \"fs\";\nimport { Graph, goToNode } from \"simplemachine\";\nimport { StatelogClient } from \"statelog-client\";\nimport { nanoid } from \"nanoid\";\n\nconst statelogHost = \"http://localhost:1065\";\nconst traceId = nanoid();\nconst statelogClient = new StatelogClient({host: statelogHost, tid: traceId});\nconst model = \"gpt-4.1-nano-2025-04-14\";\n\nconst openai = new OpenAI({\n apiKey: process.env.OPENAI_API_KEY,\n});\n\ntype State = {\n messages: string[];\n data: any;\n}\n\n// enable debug logging\nconst graphConfig = {\n debug: {\n log: true,\n logData: true,\n },\n statelogHost,\n traceId\n};\n\n// Define the names of the nodes in the graph\n// Useful for type safety\nconst nodes = {{{nodes:string}}} as const;\ntype Node = (typeof nodes)[number];\n\nconst graph = new Graph<State, Node>(nodes, graphConfig);";
2
+ export type TemplateType = {
3
+ nodes: string;
4
+ };
5
+ declare const render: (args: TemplateType) => string;
6
+ export default render;
@@ -0,0 +1,7 @@
1
+ export declare const template = "let {{{name:string}}}:any;\n\ngraph.node(\"{{{name}}}\", async (state) => {\n const innerFunc = async () => {\n {{{body}}}\n };\n {{{name}}} = await innerFunc();\n return {{{name}}};\n});\n";
2
+ export type TemplateType = {
3
+ name: string;
4
+ body: string | boolean | number;
5
+ };
6
+ declare const render: (args: TemplateType) => string;
7
+ export default render;
@@ -0,0 +1,8 @@
1
+ export declare const template = "let {{{name:string}}}:any;\n\ngraph.node(\"{{{name}}}\", async (state) => {\n const innerFunc = {{{promptFunction:string}}};\n {{{name}}} = await innerFunc({{{argsStr:string}}});\n return {{{name}}};\n});\n";
2
+ export type TemplateType = {
3
+ name: string;
4
+ promptFunction: string;
5
+ argsStr: string;
6
+ };
7
+ declare const render: (args: TemplateType) => string;
8
+ export default render;
@@ -0,0 +1,6 @@
1
+ export declare const template = "const initialState: State = {messages: [], data: {}};\nconst finalState = graph.run(\"{{{startNode}}}\", initialState);";
2
+ export type TemplateType = {
3
+ startNode: string | boolean | number;
4
+ };
5
+ declare const render: (args: TemplateType) => string;
6
+ export default render;
@@ -0,0 +1,4 @@
1
+ export declare const template = "async function _builtinFetch(url: string, args: any): any {\n const result = await fetch(url, args);\n try {\n const text = await result.text();\n return text;\n } catch (e) {\n throw new Error(`Failed to get text from ${url}: ${e}`);\n }\n}";
2
+ export type TemplateType = {};
3
+ declare const render: (args: TemplateType) => string;
4
+ export default render;
@@ -0,0 +1,4 @@
1
+ export declare const template = "async function _builtinFetchJSON(url: string, args: any): any {\n const result = await fetch(url, args);\n try {\n const json = await result.json();\n return json;\n } catch (e) {\n throw new Error(`Failed to parse JSON from ${url}: ${e}`);\n }\n}";
2
+ export type TemplateType = {};
3
+ declare const render: (args: TemplateType) => string;
4
+ export default render;
@@ -0,0 +1,4 @@
1
+ export declare const template = "function _builtinInput(prompt: string): Promise<string> {\n const rl = readline.createInterface({\n input: process.stdin,\n output: process.stdout\n });\n\n return new Promise((resolve) => {\n rl.question(prompt, (answer: string) => {\n rl.close();\n resolve(answer);\n });\n });\n}";
2
+ export type TemplateType = {};
3
+ declare const render: (args: TemplateType) => string;
4
+ export default render;
@@ -0,0 +1,4 @@
1
+ export declare const template = "function _builtinRead(filename: string): string {\n const data = fs.readFileSync(filename);\n const contents = data.toString('utf8');\n return contents;\n}";
2
+ export type TemplateType = {};
3
+ declare const render: (args: TemplateType) => string;
4
+ export default render;
@@ -0,0 +1,4 @@
1
+ export declare const template = "function add({a, b}: {a:number, b:number}):number {\n return a + b;\n}\n\n// Define the function tool for OpenAI\nconst addTool = {\n type: \"function\" as const,\n function: {\n name: \"add\",\n description:\n \"Adds two numbers together and returns the result.\",\n parameters: {\n type: \"object\",\n properties: {\n a: {\n type: \"number\",\n description: \"The first number to add\",\n },\n b: {\n type: \"number\",\n description: \"The second number to add\",\n },\n },\n required: [\"a\", \"b\"],\n additionalProperties: false,\n },\n },\n };";
2
+ export type TemplateType = {};
3
+ declare const render: (args: TemplateType) => string;
4
+ export default render;
@@ -0,0 +1,8 @@
1
+ export declare const template = "async function {{{functionName:string}}}({{{args}}}) {\n {{{functionBody}}}\n}";
2
+ export type TemplateType = {
3
+ functionName: string;
4
+ args: string | boolean | number;
5
+ functionBody: string | boolean | number;
6
+ };
7
+ declare const render: (args: TemplateType) => string;
8
+ export default render;
@@ -0,0 +1,4 @@
1
+ export declare const template = "import OpenAI from \"openai\";\nimport { zodResponseFormat } from \"openai/helpers/zod\";\nimport { z } from \"zod\";\nimport * as readline from \"readline\";\nimport fs from \"fs\";\nimport { StatelogClient } from \"statelog-client\";\nimport { nanoid } from \"nanoid\";\n\nconst statelogHost = \"http://localhost:1065\";\nconst traceId = nanoid();\nconst statelogClient = new StatelogClient({host: statelogHost, tid: traceId});\n\nconst model = \"gpt-5-nano-2025-08-07\";\n\nconst openai = new OpenAI({\n apiKey: process.env.OPENAI_API_KEY,\n});";
2
+ export type TemplateType = {};
3
+ declare const render: (args: TemplateType) => string;
4
+ export default render;
@@ -0,0 +1,12 @@
1
+ export declare const template = "\nasync function _{{{variableName:string}}}({{{argsStr:string}}}): Promise<{{{typeString:string}}}> {\n const prompt = {{{promptCode:string}}};\n const startTime = performance.now();\n const messages:any[] = [{ role: \"user\", content: prompt }];\n const tools = {{{tools}}};\n\n let completion = await openai.chat.completions.create({\n model,\n messages,\n tools,\n response_format: zodResponseFormat(z.object({\n value: {{{zodSchema:string}}}\n }), \"{{{variableName:string}}}_response\"),\n });\n const endTime = performance.now();\n statelogClient.promptCompletion({\n messages,\n completion,\n model,\n timeTaken: endTime - startTime,\n });\n\n let responseMessage = completion.choices[0].message;\n // Handle function calls\n while (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {\n // Add assistant's response with tool calls to message history\n messages.push(responseMessage);\n let toolCallStartTime, toolCallEndTime;\n\n // Process each tool call\n for (const toolCall of responseMessage.tool_calls) {\n {{{functionCalls:string}}}\n }\n\n const nextStartTime = performance.now();\n // Get the next response from the model\n completion = await openai.chat.completions.create({\n model,\n messages: messages,\n tools: tools,\n });\n const nextEndTime = performance.now();\n\n statelogClient.promptCompletion({\n messages,\n completion,\n model,\n timeTaken: nextEndTime - nextStartTime,\n });\n\n responseMessage = completion.choices[0].message;\n }\n\n // Add final assistant response to history\n messages.push(responseMessage);\n\n try {\n const result = JSON.parse(completion.choices[0].message.content || \"\");\n return result.value;\n } catch (e) {\n return completion.choices[0].message.content;\n // console.error(\"Error parsing response for variable '{{{variableName:string}}}':\", e);\n // console.error(\"Full completion response:\", JSON.stringify(completion, null, 2));\n // throw e;\n }\n}\n";
2
+ export type TemplateType = {
3
+ variableName: string;
4
+ argsStr: string;
5
+ typeString: string;
6
+ promptCode: string;
7
+ tools: string | boolean | number;
8
+ zodSchema: string;
9
+ functionCalls: string;
10
+ };
11
+ declare const render: (args: TemplateType) => string;
12
+ export default render;
@@ -0,0 +1,9 @@
1
+ export declare const template = "const {{{name:string}}}Tool: OpenAI.Chat.Completions.ChatCompletionTool = {\n type: \"function\",\n function: {\n name: \"{{{name:string}}}\",\n description:\n \"{{{description:string}}}\",\n parameters: {\n type: \"object\",\n properties: {{{properties:string}}},\n required: [{{{requiredParameters:string}}}],\n additionalProperties: false,\n },\n },\n };\n";
2
+ export type TemplateType = {
3
+ name: string;
4
+ description: string;
5
+ properties: string;
6
+ requiredParameters: string;
7
+ };
8
+ declare const render: (args: TemplateType) => string;
9
+ export default render;
@@ -0,0 +1,6 @@
1
+ export declare const template = "if (toolCall.type === \"function\" &&\n toolCall.function.name === \"{{{name:string}}}\"\n) {\n const args = JSON.parse(toolCall.function.arguments);\n\n toolCallStartTime = performance.now();\n const result = await {{{name}}}(args);\n toolCallEndTime = performance.now();\n\n console.log(\"Tool '{{{name:string}}}' called with arguments:\", args);\n console.log(\"Tool '{{{name:string}}}' returned result:\", result);\n\nstatelogClient.toolCall({\n toolName: \"{{{name:string}}}\",\n args,\n output: result,\n model,\n timeTaken: toolCallEndTime - toolCallStartTime,\n });\n\n // Add function result to messages\n messages.push({\n role: \"tool\",\n tool_call_id: toolCall.id,\n content: JSON.stringify(result),\n });\n}";
2
+ export type TemplateType = {
3
+ name: string;
4
+ };
5
+ declare const render: (args: TemplateType) => string;
6
+ export default render;
@@ -0,0 +1,22 @@
1
+ import { FunctionCall } from "../types.js";
2
+ import { Literal } from "./literals.js";
3
+ import { AgencyArray } from "./dataStructures.js";
4
+ export type DotProperty = {
5
+ type: "dotProperty";
6
+ object: Literal | FunctionCall | AccessExpression;
7
+ propertyName: string;
8
+ };
9
+ export type IndexAccess = {
10
+ type: "indexAccess";
11
+ array: Literal | FunctionCall | AccessExpression | AgencyArray;
12
+ index: Literal | FunctionCall | AccessExpression;
13
+ };
14
+ export type DotFunctionCall = {
15
+ type: "dotFunctionCall";
16
+ object: Literal | FunctionCall | AccessExpression;
17
+ functionCall: FunctionCall;
18
+ };
19
+ export type AccessExpression = {
20
+ type: "accessExpression";
21
+ expression: DotProperty | IndexAccess | DotFunctionCall;
22
+ };
@@ -0,0 +1,14 @@
1
+ import { AccessExpression, Literal } from "../types.js";
2
+ import { FunctionCall } from "./function.js";
3
+ export type AgencyArray = {
4
+ type: "agencyArray";
5
+ items: (AccessExpression | Literal | FunctionCall | AgencyObject | AgencyArray | AccessExpression)[];
6
+ };
7
+ export type AgencyObjectKV = {
8
+ key: string;
9
+ value: AccessExpression | Literal | FunctionCall | AgencyObject | AgencyArray | AccessExpression;
10
+ };
11
+ export type AgencyObject = {
12
+ type: "agencyObject";
13
+ entries: AgencyObjectKV[];
14
+ };
@@ -0,0 +1,19 @@
1
+ import { AgencyNode } from "../types.js";
2
+ import { AccessExpression } from "./access.js";
3
+ import { Literal } from "./literals.js";
4
+ export type FunctionDefinition = {
5
+ type: "function";
6
+ functionName: string;
7
+ parameters: string[];
8
+ body: AgencyNode[];
9
+ docString?: DocString;
10
+ };
11
+ export type FunctionCall = {
12
+ type: "functionCall";
13
+ functionName: string;
14
+ arguments: (Literal | AccessExpression | FunctionCall)[];
15
+ };
16
+ export type DocString = {
17
+ type: "docString";
18
+ value: string;
19
+ };
@@ -0,0 +1,14 @@
1
+ import { AgencyNode, FunctionCall } from "../types.js";
2
+ import { AccessExpression } from "./access.js";
3
+ import { Literal } from "./literals.js";
4
+ export type GraphNodeDefinition = {
5
+ type: "graphNode";
6
+ nodeName: string;
7
+ parameters: string[];
8
+ body: AgencyNode[];
9
+ };
10
+ export type NodeCall = {
11
+ type: "nodeCall";
12
+ nodeName: string;
13
+ arguments: (Literal | AccessExpression | FunctionCall)[];
14
+ };
@@ -0,0 +1,5 @@
1
+ export type ImportStatement = {
2
+ type: "importStatement";
3
+ importedNames: string;
4
+ modulePath: string;
5
+ };
@@ -0,0 +1,26 @@
1
+ export type Literal = NumberLiteral | StringLiteral | VariableNameLiteral | PromptLiteral;
2
+ export type NumberLiteral = {
3
+ type: "number";
4
+ value: string;
5
+ };
6
+ export type StringLiteral = {
7
+ type: "string";
8
+ value: string;
9
+ };
10
+ export type VariableNameLiteral = {
11
+ type: "variableName";
12
+ value: string;
13
+ };
14
+ export type PromptSegment = TextSegment | InterpolationSegment;
15
+ export type TextSegment = {
16
+ type: "text";
17
+ value: string;
18
+ };
19
+ export type InterpolationSegment = {
20
+ type: "interpolation";
21
+ variableName: string;
22
+ };
23
+ export type PromptLiteral = {
24
+ type: "prompt";
25
+ segments: PromptSegment[];
26
+ };
@@ -0,0 +1,17 @@
1
+ import { Assignment, AgencyComment } from "../types.js";
2
+ import { AccessExpression } from "./access.js";
3
+ import { Literal } from "./literals.js";
4
+ import { FunctionCall } from "./function.js";
5
+ import { AgencyArray, AgencyObject } from "./dataStructures.js";
6
+ import { ReturnStatement } from "./returnStatement.js";
7
+ export type DefaultCase = "_";
8
+ export type MatchBlockCase = {
9
+ type: "matchBlockCase";
10
+ caseValue: AccessExpression | Literal | DefaultCase;
11
+ body: Assignment | Literal | FunctionCall | AccessExpression | AgencyArray | AgencyObject | ReturnStatement;
12
+ };
13
+ export type MatchBlock = {
14
+ type: "matchBlock";
15
+ expression: Literal;
16
+ cases: (MatchBlockCase | AgencyComment)[];
17
+ };
@@ -0,0 +1,6 @@
1
+ import { AccessExpression, FunctionCall, Literal } from "../types.js";
2
+ import { AgencyArray, AgencyObject } from "./dataStructures.js";
3
+ export type ReturnStatement = {
4
+ type: "returnStatement";
5
+ value: AccessExpression | FunctionCall | Literal | AgencyObject | AgencyArray;
6
+ };
@@ -0,0 +1,4 @@
1
+ export type UsesTool = {
2
+ type: "usesTool";
3
+ toolName: string;
4
+ };
@@ -0,0 +1,48 @@
1
+ export type TypeHint = {
2
+ type: "typeHint";
3
+ variableName: string;
4
+ variableType: VariableType;
5
+ };
6
+ export type VariableType = PrimitiveType | ArrayType | StringLiteralType | NumberLiteralType | BooleanLiteralType | UnionType | ObjectType | TypeAliasVariable;
7
+ export type PrimitiveType = {
8
+ type: "primitiveType";
9
+ value: string;
10
+ };
11
+ export type ArrayType = {
12
+ type: "arrayType";
13
+ elementType: VariableType;
14
+ };
15
+ export type StringLiteralType = {
16
+ type: "stringLiteralType";
17
+ value: string;
18
+ };
19
+ export type NumberLiteralType = {
20
+ type: "numberLiteralType";
21
+ value: string;
22
+ };
23
+ export type BooleanLiteralType = {
24
+ type: "booleanLiteralType";
25
+ value: "true" | "false";
26
+ };
27
+ export type UnionType = {
28
+ type: "unionType";
29
+ types: VariableType[];
30
+ };
31
+ export type ObjectProperty = {
32
+ key: string;
33
+ value: VariableType;
34
+ description?: string;
35
+ };
36
+ export type ObjectType = {
37
+ type: "objectType";
38
+ properties: ObjectProperty[];
39
+ };
40
+ export type TypeAlias = {
41
+ type: "typeAlias";
42
+ aliasName: string;
43
+ aliasedType: VariableType;
44
+ };
45
+ export type TypeAliasVariable = {
46
+ type: "typeAliasVariable";
47
+ aliasName: string;
48
+ };
@@ -0,0 +1,6 @@
1
+ import { AccessExpression, AgencyNode, FunctionCall, Literal } from "../types.js";
2
+ export type WhileLoop = {
3
+ type: "whileLoop";
4
+ condition: FunctionCall | AccessExpression | Literal;
5
+ body: AgencyNode[];
6
+ };
@@ -0,0 +1,37 @@
1
+ import { Literal } from "./types/literals.js";
2
+ import { TypeAlias, TypeHint, VariableType } from "./types/typeHints.js";
3
+ import { MatchBlock } from "./types/matchBlock.js";
4
+ import { AccessExpression } from "./types/access.js";
5
+ import { FunctionCall, FunctionDefinition } from "./types/function.js";
6
+ import { AgencyArray, AgencyObject } from "./types/dataStructures.js";
7
+ import { GraphNodeDefinition } from "./types/graphNode.js";
8
+ import { ReturnStatement } from "./types/returnStatement.js";
9
+ import { UsesTool } from "./types/tools.js";
10
+ import { ImportStatement } from "./types/importStatement.js";
11
+ import { WhileLoop } from "./types/whileLoop.js";
12
+ export * from "./types/access.js";
13
+ export * from "./types/dataStructures.js";
14
+ export * from "./types/function.js";
15
+ export * from "./types/graphNode.js";
16
+ export * from "./types/importStatement.js";
17
+ export * from "./types/literals.js";
18
+ export * from "./types/matchBlock.js";
19
+ export * from "./types/returnStatement.js";
20
+ export * from "./types/tools.js";
21
+ export * from "./types/typeHints.js";
22
+ export * from "./types/whileLoop.js";
23
+ export type Assignment = {
24
+ type: "assignment";
25
+ variableName: string;
26
+ value: AccessExpression | Literal | FunctionCall | AgencyObject | AgencyArray;
27
+ };
28
+ export type AgencyComment = {
29
+ type: "comment";
30
+ content: string;
31
+ };
32
+ export type AgencyNode = TypeHint | TypeAlias | UsesTool | GraphNodeDefinition | FunctionDefinition | Assignment | Literal | FunctionCall | MatchBlock | ReturnStatement | AccessExpression | AgencyComment | AgencyObject | AgencyArray | ImportStatement | WhileLoop;
33
+ export type AgencyProgram = {
34
+ type: "agencyProgram";
35
+ nodes: AgencyNode[];
36
+ };
37
+ export type TypeHintMap = Record<string, VariableType>;
@@ -0,0 +1,3 @@
1
+ export declare function escape(str: string): string;
2
+ export declare function deepCopy<T>(obj: T): T;
3
+ export declare function zip<T, U>(arr1: T[], arr2: U[]): Array<[T, U]>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agency-lang",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "The Agency language",
5
5
  "main": "index.js",
6
6
  "scripts": {