agency-lang 0.0.21 → 0.0.22

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 (82) hide show
  1. package/dist/lib/backends/agencyGenerator.d.ts +10 -2
  2. package/dist/lib/backends/agencyGenerator.js +86 -19
  3. package/dist/lib/backends/agencyGenerator.test.js +0 -48
  4. package/dist/lib/backends/baseGenerator.d.ts +11 -4
  5. package/dist/lib/backends/baseGenerator.js +35 -11
  6. package/dist/lib/backends/graphGenerator.js +19 -186
  7. package/dist/lib/backends/typescriptGenerator/builtins.js +6 -0
  8. package/dist/lib/backends/typescriptGenerator.d.ts +11 -3
  9. package/dist/lib/backends/typescriptGenerator.js +98 -26
  10. package/dist/lib/backends/utils.d.ts +1 -1
  11. package/dist/lib/parser.js +5 -5
  12. package/dist/lib/parser.test.js +15 -38
  13. package/dist/lib/parsers/access.test.js +14 -3
  14. package/dist/lib/parsers/assignment.test.js +159 -4
  15. package/dist/lib/parsers/await.d.ts +3 -0
  16. package/dist/lib/parsers/await.js +18 -0
  17. package/dist/lib/parsers/body.test.js +10 -1
  18. package/dist/lib/parsers/dataStructures.js +2 -2
  19. package/dist/lib/parsers/dataStructures.test.js +11 -11
  20. package/dist/lib/parsers/function.d.ts +8 -3
  21. package/dist/lib/parsers/function.js +64 -12
  22. package/dist/lib/parsers/function.test.js +284 -52
  23. package/dist/lib/parsers/functionCall.test.js +6 -6
  24. package/dist/lib/parsers/ifElse.test.d.ts +1 -0
  25. package/dist/lib/parsers/ifElse.test.js +376 -0
  26. package/dist/lib/parsers/importStatement.js +2 -2
  27. package/dist/lib/parsers/literals.d.ts +1 -0
  28. package/dist/lib/parsers/literals.js +6 -2
  29. package/dist/lib/parsers/literals.test.js +110 -21
  30. package/dist/lib/parsers/matchBlock.js +7 -4
  31. package/dist/lib/parsers/matchBlock.test.js +16 -16
  32. package/dist/lib/parsers/newline.d.ts +3 -0
  33. package/dist/lib/parsers/newline.js +2 -0
  34. package/dist/lib/parsers/parserUtils.d.ts +3 -1
  35. package/dist/lib/parsers/parserUtils.js +4 -1
  36. package/dist/lib/parsers/returnStatement.js +2 -1
  37. package/dist/lib/parsers/returnStatement.test.js +2 -2
  38. package/dist/lib/parsers/specialVar.test.js +7 -7
  39. package/dist/lib/parsers/typeHints.d.ts +1 -1
  40. package/dist/lib/parsers/typeHints.js +4 -4
  41. package/dist/lib/parsers/typeHints.test.js +0 -8
  42. package/dist/lib/parsers/utils.d.ts +1 -0
  43. package/dist/lib/parsers/utils.js +2 -1
  44. package/dist/lib/parsers/whileLoop.test.js +46 -1
  45. package/dist/lib/templates/backends/graphGenerator/goToNode.d.ts +2 -1
  46. package/dist/lib/templates/backends/graphGenerator/goToNode.js +11 -1
  47. package/dist/lib/templates/backends/graphGenerator/graphNode.d.ts +2 -2
  48. package/dist/lib/templates/backends/graphGenerator/graphNode.js +2 -1
  49. package/dist/lib/templates/backends/graphGenerator/imports.d.ts +1 -1
  50. package/dist/lib/templates/backends/graphGenerator/imports.js +14 -1
  51. package/dist/lib/templates/backends/typescriptGenerator/builtinFunctions/sleep.d.ts +4 -0
  52. package/dist/lib/templates/backends/typescriptGenerator/builtinFunctions/sleep.js +13 -0
  53. package/dist/lib/templates/backends/typescriptGenerator/builtinFunctions/time.d.ts +7 -0
  54. package/dist/lib/templates/backends/typescriptGenerator/builtinFunctions/time.js +12 -0
  55. package/dist/lib/templates/backends/typescriptGenerator/functionDefinition.d.ts +1 -1
  56. package/dist/lib/templates/backends/typescriptGenerator/functionDefinition.js +1 -0
  57. package/dist/lib/templates/backends/typescriptGenerator/promptFunction.d.ts +1 -1
  58. package/dist/lib/templates/backends/typescriptGenerator/promptFunction.js +3 -2
  59. package/dist/lib/templates/backends/typescriptGenerator/tool.d.ts +1 -1
  60. package/dist/lib/templates/backends/typescriptGenerator/tool.js +1 -1
  61. package/dist/lib/templates/backends/typescriptGenerator/toolCall.d.ts +1 -1
  62. package/dist/lib/templates/backends/typescriptGenerator/toolCall.js +2 -2
  63. package/dist/lib/types/await.d.ts +7 -0
  64. package/dist/lib/types/await.js +1 -0
  65. package/dist/lib/types/graphNode.d.ts +2 -1
  66. package/dist/lib/types/ifElse.d.ts +7 -0
  67. package/dist/lib/types/ifElse.js +1 -0
  68. package/dist/lib/types/literals.d.ts +1 -1
  69. package/dist/lib/types/returnStatement.d.ts +2 -1
  70. package/dist/lib/types/timeBlock.d.ts +5 -0
  71. package/dist/lib/types/timeBlock.js +1 -0
  72. package/dist/lib/types.d.ts +16 -8
  73. package/dist/lib/types.js +1 -0
  74. package/dist/lib/utils.d.ts +1 -0
  75. package/dist/lib/utils.js +3 -0
  76. package/package.json +3 -2
  77. package/dist/lib/parsers/assignment.d.ts +0 -3
  78. package/dist/lib/parsers/assignment.js +0 -8
  79. package/dist/lib/templates/backends/graphGenerator/node.d.ts +0 -7
  80. package/dist/lib/templates/backends/graphGenerator/node.js +0 -18
  81. package/dist/lib/templates/backends/graphGenerator/promptNode.d.ts +0 -8
  82. package/dist/lib/templates/backends/graphGenerator/promptNode.js +0 -16
@@ -66,8 +66,8 @@ describe("matchBlockParserCase", () => {
66
66
  success: true,
67
67
  result: {
68
68
  type: "matchBlockCase",
69
- caseValue: { type: "string", value: "hello" },
70
- body: { type: "string", value: "world" },
69
+ caseValue: { type: "string", segments: [{ type: "text", value: "hello" }] },
70
+ body: { type: "string", segments: [{ type: "text", value: "world" }] },
71
71
  },
72
72
  },
73
73
  },
@@ -272,13 +272,13 @@ describe("matchBlockParser", () => {
272
272
  cases: [
273
273
  {
274
274
  type: "matchBlockCase",
275
- caseValue: { type: "string", value: "active" },
276
- body: { type: "string", value: "running" },
275
+ caseValue: { type: "string", segments: [{ type: "text", value: "active" }] },
276
+ body: { type: "string", segments: [{ type: "text", value: "running" }] },
277
277
  },
278
278
  {
279
279
  type: "matchBlockCase",
280
- caseValue: { type: "string", value: "inactive" },
281
- body: { type: "string", value: "stopped" },
280
+ caseValue: { type: "string", segments: [{ type: "text", value: "inactive" }] },
281
+ body: { type: "string", segments: [{ type: "text", value: "stopped" }] },
282
282
  },
283
283
  ],
284
284
  },
@@ -300,17 +300,17 @@ describe("matchBlockParser", () => {
300
300
  {
301
301
  type: "matchBlockCase",
302
302
  caseValue: { type: "number", value: "200" },
303
- body: { type: "string", value: "OK" },
303
+ body: { type: "string", segments: [{ type: "text", value: "OK" }] },
304
304
  },
305
305
  {
306
306
  type: "matchBlockCase",
307
307
  caseValue: { type: "number", value: "404" },
308
- body: { type: "string", value: "Not Found" },
308
+ body: { type: "string", segments: [{ type: "text", value: "Not Found" }] },
309
309
  },
310
310
  {
311
311
  type: "matchBlockCase",
312
312
  caseValue: { type: "number", value: "500" },
313
- body: { type: "string", value: "Error" },
313
+ body: { type: "string", segments: [{ type: "text", value: "Error" }] },
314
314
  },
315
315
  ],
316
316
  },
@@ -364,20 +364,20 @@ describe("matchBlockParser", () => {
364
364
  cases: [
365
365
  {
366
366
  type: "matchBlockCase",
367
- caseValue: { type: "string", value: "start" },
367
+ caseValue: { type: "string", segments: [{ type: "text", value: "start" }] },
368
368
  body: {
369
369
  type: "functionCall",
370
370
  functionName: "print",
371
- arguments: [{ type: "string", value: "Starting" }],
371
+ arguments: [{ type: "string", segments: [{ type: "text", value: "Starting" }] }],
372
372
  },
373
373
  },
374
374
  {
375
375
  type: "matchBlockCase",
376
- caseValue: { type: "string", value: "stop" },
376
+ caseValue: { type: "string", segments: [{ type: "text", value: "stop" }] },
377
377
  body: {
378
378
  type: "functionCall",
379
379
  functionName: "print",
380
- arguments: [{ type: "string", value: "Stopping" }],
380
+ arguments: [{ type: "string", segments: [{ type: "text", value: "Stopping" }] }],
381
381
  },
382
382
  },
383
383
  ],
@@ -416,7 +416,7 @@ describe("matchBlockParser", () => {
416
416
  {
417
417
  type: "matchBlockCase",
418
418
  caseValue: { type: "number", value: "42" },
419
- body: { type: "string", value: "found" },
419
+ body: { type: "string", segments: [{ type: "text", value: "found" }] },
420
420
  },
421
421
  ],
422
422
  },
@@ -431,11 +431,11 @@ describe("matchBlockParser", () => {
431
431
  success: true,
432
432
  result: {
433
433
  type: "matchBlock",
434
- expression: { type: "string", value: "test" },
434
+ expression: { type: "string", segments: [{ type: "text", value: "test" }] },
435
435
  cases: [
436
436
  {
437
437
  type: "matchBlockCase",
438
- caseValue: { type: "string", value: "test" },
438
+ caseValue: { type: "string", segments: [{ type: "text", value: "test" }] },
439
439
  body: { type: "number", value: "1" },
440
440
  },
441
441
  ],
@@ -0,0 +1,3 @@
1
+ import { NewLine } from "../types.js";
2
+ import { Parser } from "tarsec";
3
+ export declare const newLineParser: Parser<NewLine>;
@@ -0,0 +1,2 @@
1
+ import { seqC, set, str } from "tarsec";
2
+ export const newLineParser = seqC(set("type", "newLine"), str("\n"));
@@ -1 +1,3 @@
1
- export declare const optionalSemicolon: import("tarsec").Parser<";" | null>;
1
+ import { Parser } from "tarsec";
2
+ export declare const optionalSemicolon: Parser<";" | null>;
3
+ export declare const oneOfStr: <T extends string>(strs: readonly T[]) => Parser<T>;
@@ -1,2 +1,5 @@
1
- import { char, optional } from "tarsec";
1
+ import { char, optional, or, str } from "tarsec";
2
2
  export const optionalSemicolon = optional(char(";"));
3
+ export const oneOfStr = (strs) => {
4
+ return or(...strs.map((s) => str(s)));
5
+ };
@@ -5,4 +5,5 @@ import { optionalSemicolon } from "./parserUtils.js";
5
5
  import { optionalSpaces } from "./utils.js";
6
6
  import { literalParser } from "./literals.js";
7
7
  import { agencyArrayParser, agencyObjectParser } from "./dataStructures.js";
8
- export const returnStatementParser = seqC(set("type", "returnStatement"), str("return"), optionalSpaces, capture(or(indexAccessParser, accessExpressionParser, functionCallParser, literalParser, agencyObjectParser, agencyArrayParser), "value"), optionalSemicolon);
8
+ import { awaitParser } from "./await.js";
9
+ export const returnStatementParser = seqC(set("type", "returnStatement"), str("return"), optionalSpaces, capture(or(awaitParser, indexAccessParser, accessExpressionParser, functionCallParser, literalParser, agencyObjectParser, agencyArrayParser), "value"), optionalSemicolon);
@@ -19,7 +19,7 @@ describe("returnStatementParser", () => {
19
19
  success: true,
20
20
  result: {
21
21
  type: "returnStatement",
22
- value: { type: "string", value: "hello" },
22
+ value: { type: "string", segments: [{ type: "text", value: "hello" }] },
23
23
  },
24
24
  },
25
25
  },
@@ -171,7 +171,7 @@ describe("returnStatementParser", () => {
171
171
  entries: [
172
172
  {
173
173
  key: "key",
174
- value: { type: "string", value: "value" },
174
+ value: { type: "string", segments: [{ type: "text", value: "value" }] },
175
175
  },
176
176
  ],
177
177
  },
@@ -11,7 +11,7 @@ describe("specialVar parsers", () => {
11
11
  result: {
12
12
  type: "specialVar",
13
13
  name: "model",
14
- value: { type: "string", value: "gpt-4" },
14
+ value: { type: "string", segments: [{ type: "text", value: "gpt-4" }] },
15
15
  },
16
16
  },
17
17
  },
@@ -59,7 +59,7 @@ describe("specialVar parsers", () => {
59
59
  result: {
60
60
  type: "specialVar",
61
61
  name: "model",
62
- value: { type: "string", value: "gpt-4" },
62
+ value: { type: "string", segments: [{ type: "text", value: "gpt-4" }] },
63
63
  },
64
64
  },
65
65
  },
@@ -70,7 +70,7 @@ describe("specialVar parsers", () => {
70
70
  result: {
71
71
  type: "specialVar",
72
72
  name: "model",
73
- value: { type: "string", value: "gpt-4" },
73
+ value: { type: "string", segments: [{ type: "text", value: "gpt-4" }] },
74
74
  },
75
75
  },
76
76
  },
@@ -99,8 +99,8 @@ describe("specialVar parsers", () => {
99
99
  value: {
100
100
  type: "agencyArray",
101
101
  items: [
102
- { type: "string", value: "gpt-4" },
103
- { type: "string", value: "claude" },
102
+ { type: "string", segments: [{ type: "text", value: "gpt-4" }] },
103
+ { type: "string", segments: [{ type: "text", value: "claude" }] },
104
104
  ],
105
105
  },
106
106
  },
@@ -133,7 +133,7 @@ describe("specialVar parsers", () => {
133
133
  entries: [
134
134
  {
135
135
  key: "name",
136
- value: { type: "string", value: "gpt-4" },
136
+ value: { type: "string", segments: [{ type: "text", value: "gpt-4" }] },
137
137
  },
138
138
  ],
139
139
  },
@@ -152,7 +152,7 @@ describe("specialVar parsers", () => {
152
152
  entries: [
153
153
  {
154
154
  key: "name",
155
- value: { type: "string", value: "gpt-4" },
155
+ value: { type: "string", segments: [{ type: "text", value: "gpt-4" }] },
156
156
  },
157
157
  {
158
158
  key: "version",
@@ -7,7 +7,7 @@ export declare const angleBracketsArrayTypeParser: Parser<ArrayType>;
7
7
  export declare const stringLiteralTypeParser: Parser<StringLiteralType>;
8
8
  export declare const numberLiteralTypeParser: Parser<NumberLiteralType>;
9
9
  export declare const booleanLiteralTypeParser: Parser<BooleanLiteralType>;
10
- export declare const objectPropertyDelimiter: Parser<"\n" | (string | string[])[]>;
10
+ export declare const objectPropertyDelimiter: Parser<(string | string[])[]>;
11
11
  export declare const objectPropertyParser: Parser<ObjectProperty>;
12
12
  export declare const objectPropertyDescriptionParser: Parser<{
13
13
  description: string;
@@ -1,7 +1,7 @@
1
- import { optionalSpaces, varNameChar } from "./utils.js";
1
+ import { optionalSpaces, optionalSpacesOrNewline, varNameChar, } from "./utils.js";
2
2
  import { capture, captureCaptures, char, count, digit, many1Till, many1WithJoin, oneOf, or, sepBy, seqC, seqR, set, space, spaces, str, trace, } from "tarsec";
3
3
  import { optionalSemicolon } from "./parserUtils.js";
4
- export const primitiveTypeParser = trace("primitiveTypeParser", seqC(set("type", "primitiveType"), capture(or(str("number"), str("string"), str("boolean"), str("undefined"), str("null")), "value")));
4
+ export const primitiveTypeParser = trace("primitiveTypeParser", seqC(set("type", "primitiveType"), capture(or(str("number"), str("string"), str("boolean"), str("undefined"), str("void"), str("null")), "value")));
5
5
  export const typeAliasVariableParser = trace("typeAliasVariableParser", seqC(set("type", "typeAliasVariable"), capture(many1WithJoin(varNameChar), "aliasName")));
6
6
  export const arrayTypeParser = (input) => {
7
7
  const parser = trace("arrayTypeParser", seqC(set("type", "arrayType"), capture(or(objectTypeParser, primitiveTypeParser, typeAliasVariableParser), "elementType"), capture(count(str("[]")), "arrayDepth")));
@@ -27,7 +27,7 @@ export const angleBracketsArrayTypeParser = trace("angleBracketsArrayTypeParser"
27
27
  export const stringLiteralTypeParser = trace("stringLiteralTypeParser", seqC(set("type", "stringLiteralType"), char('"'), capture(many1Till(char('"')), "value"), char('"')));
28
28
  export const numberLiteralTypeParser = trace("numberLiteralTypeParser", seqC(set("type", "numberLiteralType"), capture(many1WithJoin(or(char("-"), char("."), digit)), "value")));
29
29
  export const booleanLiteralTypeParser = trace("booleanLiteralTypeParser", seqC(set("type", "booleanLiteralType"), capture(or(str("true"), str("false")), "value")));
30
- export const objectPropertyDelimiter = or(char("\n"), seqR(optionalSpaces, oneOf(",;"), optionalSpaces));
30
+ export const objectPropertyDelimiter = seqR(optionalSpaces, oneOf(",;"), optionalSpacesOrNewline);
31
31
  export const objectPropertyParser = trace("objectPropertyParser", (input) => {
32
32
  const parser = seqC(capture(many1WithJoin(varNameChar), "key"), optionalSpaces, char(":"), optionalSpaces, capture(variableTypeParser, "value"));
33
33
  return parser(input);
@@ -35,7 +35,7 @@ export const objectPropertyParser = trace("objectPropertyParser", (input) => {
35
35
  export const objectPropertyDescriptionParser = seqC(char("#"), optionalSpaces, capture(many1Till(oneOf(",;\n")), "description"));
36
36
  export const objectPropertyWithDescriptionParser = trace("objectPropertyWithDescriptionParser", seqC(captureCaptures(objectPropertyParser), spaces, captureCaptures(objectPropertyDescriptionParser)));
37
37
  export const objectTypeParser = trace("objectTypeParser", (input) => {
38
- const parser = seqC(set("type", "objectType"), char("{"), optionalSpaces, capture(sepBy(objectPropertyDelimiter, or(objectPropertyWithDescriptionParser, objectPropertyParser)), "properties"), optionalSpaces, char("}"));
38
+ const parser = seqC(set("type", "objectType"), char("{"), optionalSpacesOrNewline, capture(sepBy(objectPropertyDelimiter, or(objectPropertyWithDescriptionParser, objectPropertyParser)), "properties"), optionalSpacesOrNewline, char("}"));
39
39
  return parser(input);
40
40
  });
41
41
  export const unionItemParser = trace("unionItemParser", or(objectTypeParser, angleBracketsArrayTypeParser, arrayTypeParser, stringLiteralTypeParser, numberLiteralTypeParser, booleanLiteralTypeParser, primitiveTypeParser, typeAliasVariableParser));
@@ -123,10 +123,6 @@ describe("arrayTypeParser", () => {
123
123
  input: "number",
124
124
  expected: { success: false },
125
125
  },
126
- {
127
- input: "invalid[]",
128
- expected: { success: false },
129
- },
130
126
  {
131
127
  input: "[]",
132
128
  expected: { success: false },
@@ -182,10 +178,6 @@ describe("angleBracketsArrayTypeParser", () => {
182
178
  },
183
179
  },
184
180
  },
185
- {
186
- input: "array<invalid>",
187
- expected: { success: false },
188
- },
189
181
  {
190
182
  input: "array<>",
191
183
  expected: { success: false },
@@ -1,4 +1,5 @@
1
1
  import { Parser } from "tarsec";
2
+ export declare const optionalSpacesOrNewline: Parser<string[]>;
2
3
  export declare const optionalSpaces: Parser<string[]>;
3
4
  export declare const backtick: Parser<"`">;
4
5
  export declare const comma: Parser<(string[] | ",")[]>;
@@ -1,5 +1,6 @@
1
1
  import { char, many, oneOf, seqR, space } from "tarsec";
2
- export const optionalSpaces = many(space);
2
+ export const optionalSpacesOrNewline = many(space);
3
+ export const optionalSpaces = many(oneOf(" \t"));
3
4
  export const backtick = char("`");
4
5
  export const comma = seqR(optionalSpaces, char(","), optionalSpaces);
5
6
  export const varNameChar = oneOf("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_");
@@ -16,6 +16,9 @@ describe("whileLoopParser", () => {
16
16
  variableName: "x",
17
17
  value: { type: "number", value: "1" },
18
18
  },
19
+ {
20
+ type: "newLine",
21
+ },
19
22
  ],
20
23
  },
21
24
  },
@@ -33,6 +36,9 @@ describe("whileLoopParser", () => {
33
36
  variableName: "y",
34
37
  value: { type: "number", value: "2" },
35
38
  },
39
+ {
40
+ type: "newLine",
41
+ },
36
42
  ],
37
43
  },
38
44
  },
@@ -50,6 +56,9 @@ describe("whileLoopParser", () => {
50
56
  variableName: "foo",
51
57
  value: { type: "number", value: "1" },
52
58
  },
59
+ {
60
+ type: "newLine",
61
+ },
53
62
  ],
54
63
  },
55
64
  },
@@ -67,6 +76,9 @@ describe("whileLoopParser", () => {
67
76
  variableName: "x",
68
77
  value: { type: "number", value: "2" },
69
78
  },
79
+ {
80
+ type: "newLine",
81
+ },
70
82
  ],
71
83
  },
72
84
  },
@@ -77,13 +89,16 @@ describe("whileLoopParser", () => {
77
89
  success: true,
78
90
  result: {
79
91
  type: "whileLoop",
80
- condition: { type: "string", value: "running" },
92
+ condition: { type: "string", segments: [{ type: "text", value: "running" }] },
81
93
  body: [
82
94
  {
83
95
  type: "assignment",
84
96
  variableName: "x",
85
97
  value: { type: "number", value: "3" },
86
98
  },
99
+ {
100
+ type: "newLine",
101
+ },
87
102
  ],
88
103
  },
89
104
  },
@@ -106,6 +121,9 @@ describe("whileLoopParser", () => {
106
121
  variableName: "x",
107
122
  value: { type: "number", value: "1" },
108
123
  },
124
+ {
125
+ type: "newLine",
126
+ },
109
127
  ],
110
128
  },
111
129
  },
@@ -127,6 +145,9 @@ describe("whileLoopParser", () => {
127
145
  functionName: "process",
128
146
  arguments: [],
129
147
  },
148
+ {
149
+ type: "newLine",
150
+ },
130
151
  ],
131
152
  },
132
153
  },
@@ -152,6 +173,9 @@ describe("whileLoopParser", () => {
152
173
  variableName: "x",
153
174
  value: { type: "number", value: "1" },
154
175
  },
176
+ {
177
+ type: "newLine",
178
+ },
155
179
  ],
156
180
  },
157
181
  },
@@ -176,6 +200,9 @@ describe("whileLoopParser", () => {
176
200
  functionName: "tick",
177
201
  arguments: [],
178
202
  },
203
+ {
204
+ type: "newLine",
205
+ },
179
206
  ],
180
207
  },
181
208
  },
@@ -206,16 +233,25 @@ describe("whileLoopParser", () => {
206
233
  variableName: "a",
207
234
  value: { type: "number", value: "1" },
208
235
  },
236
+ {
237
+ type: "newLine",
238
+ },
209
239
  {
210
240
  type: "assignment",
211
241
  variableName: "b",
212
242
  value: { type: "number", value: "2" },
213
243
  },
244
+ {
245
+ type: "newLine",
246
+ },
214
247
  {
215
248
  type: "assignment",
216
249
  variableName: "c",
217
250
  value: { type: "number", value: "3" },
218
251
  },
252
+ {
253
+ type: "newLine",
254
+ },
219
255
  ],
220
256
  },
221
257
  },
@@ -234,6 +270,9 @@ describe("whileLoopParser", () => {
234
270
  variableName: "foo",
235
271
  value: { type: "number", value: "1" },
236
272
  },
273
+ {
274
+ type: "newLine",
275
+ },
237
276
  ],
238
277
  },
239
278
  },
@@ -251,6 +290,9 @@ describe("whileLoopParser", () => {
251
290
  variableName: "foo",
252
291
  value: { type: "number", value: "1" },
253
292
  },
293
+ {
294
+ type: "newLine",
295
+ },
254
296
  ],
255
297
  },
256
298
  },
@@ -269,6 +311,9 @@ describe("whileLoopParser", () => {
269
311
  variableName: "foo",
270
312
  value: { type: "number", value: "1" },
271
313
  },
314
+ {
315
+ type: "newLine",
316
+ },
272
317
  ],
273
318
  },
274
319
  },
@@ -1,6 +1,7 @@
1
- export declare const template = "goToNode(\"{{{nodeName:string}}}\", { messages: state.messages, data: {{{data:string}}} });";
1
+ export declare const template = "goToNode(\"{{{nodeName:string}}}\",\n {\n messages: state.messages,\n {{#hasData}}\n data: {{{data:string}}}\n {{/hasData}}\n {{^hasData}}\n data: null\n {{/hasData}}\n }\n);";
2
2
  export type TemplateType = {
3
3
  nodeName: string;
4
+ hasData: boolean;
4
5
  data: string;
5
6
  };
6
7
  declare const render: (args: TemplateType) => string;
@@ -2,7 +2,17 @@
2
2
  // Source: lib/templates/backends/graphGenerator/goToNode.mustache
3
3
  // Any manual changes will be lost.
4
4
  import { apply } from "typestache";
5
- export const template = `goToNode("{{{nodeName:string}}}", { messages: state.messages, data: {{{data:string}}} });`;
5
+ export const template = `goToNode("{{{nodeName:string}}}",
6
+ {
7
+ messages: state.messages,
8
+ {{#hasData}}
9
+ data: {{{data:string}}}
10
+ {{/hasData}}
11
+ {{^hasData}}
12
+ data: null
13
+ {{/hasData}}
14
+ }
15
+ );`;
6
16
  const render = (args) => {
7
17
  return apply(template, args);
8
18
  };
@@ -1,8 +1,8 @@
1
- export declare const template = "graph.node(\"{{{name}}}\", async (state): Promise<any> => {\n {{#hasParam}}\n const {{{paramName}}} = state.data;\n {{/hasParam}}\n {{{body}}}\n});\n";
1
+ export declare const template = "graph.node(\"{{{name}}}\", async (state): Promise<any> => {\n const __messages: Message[] = [];\n {{#hasParam}}\n const {{{paramNames}}} = state.data;\n {{/hasParam}}\n {{{body}}}\n});\n";
2
2
  export type TemplateType = {
3
3
  name: string | boolean | number;
4
4
  hasParam: boolean;
5
- paramName: string | boolean | number;
5
+ paramNames: string | boolean | number;
6
6
  body: string | boolean | number;
7
7
  };
8
8
  declare const render: (args: TemplateType) => string;
@@ -3,8 +3,9 @@
3
3
  // Any manual changes will be lost.
4
4
  import { apply } from "typestache";
5
5
  export const template = `graph.node("{{{name}}}", async (state): Promise<any> => {
6
+ const __messages: Message[] = [];
6
7
  {{#hasParam}}
7
- const {{{paramName}}} = state.data;
8
+ const {{{paramNames}}} = state.data;
8
9
  {{/hasParam}}
9
10
  {{{body}}}
10
11
  });
@@ -1,4 +1,4 @@
1
- export declare const template = "import { z } from \"zod\";\nimport * as readline from \"readline\";\nimport fs from \"fs\";\nimport { PieMachine, goToNode } from \"piemachine\";\nimport { StatelogClient } from \"statelog-client\";\nimport { nanoid } from \"nanoid\";\nimport { assistantMessage, getClient, userMessage, toolMessage } from \"smoltalk\";\n\nconst statelogHost = \"https://statelog.adit.io\";\nconst traceId = nanoid();\nconst statelogConfig = {\n host: statelogHost,\n traceId: traceId,\n apiKey: process.env.STATELOG_API_KEY || \"\",\n projectId: \"agency-lang\",\n debugMode: false,\n };\nconst statelogClient = new StatelogClient(statelogConfig);\nconst __model: ModelName = \"gpt-4o-mini\";\n\n\nconst getClientWithConfig = (config = {}) => {\n const defaultConfig = {\n openAiApiKey: process.env.OPENAI_API_KEY || \"\",\n googleApiKey: process.env.GEMINI_API_KEY || \"\",\n model: __model,\n logLevel: \"warn\",\n };\n\n return getClient({ ...defaultConfig, ...config });\n};\n\nlet __client = getClientWithConfig();\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 statelog: statelogConfig,\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 PieMachine<State, Node>(__nodes, graphConfig);";
1
+ export declare const template = "import { z } from \"zod\";\nimport * as readline from \"readline\";\nimport fs from \"fs\";\nimport { PieMachine, goToNode } from \"piemachine\";\nimport { StatelogClient } from \"statelog-client\";\nimport { nanoid } from \"nanoid\";\nimport { assistantMessage, getClient, userMessage, toolMessage } from \"smoltalk\";\n\nconst statelogHost = \"https://statelog.adit.io\";\nconst traceId = nanoid();\nconst statelogConfig = {\n host: statelogHost,\n traceId: traceId,\n apiKey: process.env.STATELOG_API_KEY || \"\",\n projectId: \"agency-lang\",\n debugMode: false,\n };\nconst statelogClient = new StatelogClient(statelogConfig);\nconst __model: ModelName = \"gpt-4o-mini\";\n\n\nconst getClientWithConfig = (config = {}) => {\n const defaultConfig = {\n openAiApiKey: process.env.OPENAI_API_KEY || \"\",\n googleApiKey: process.env.GEMINI_API_KEY || \"\",\n model: __model,\n logLevel: \"warn\",\n };\n\n return getClient({ ...defaultConfig, ...config });\n};\n\nlet __client = getClientWithConfig();\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 statelog: statelogConfig,\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 PieMachine<State, Node>(__nodes, graphConfig);\n\n// builtins\n\nconst not = (val: any): boolean => !val;\nconst eq = (a: any, b: any): boolean => a === b;\nconst neq = (a: any, b: any): boolean => a !== b;\nconst lt = (a: any, b: any): boolean => a < b;\nconst lte = (a: any, b: any): boolean => a <= b;\nconst gt = (a: any, b: any): boolean => a > b;\nconst gte = (a: any, b: any): boolean => a >= b;\nconst and = (a: any, b: any): boolean => a && b;\nconst or = (a: any, b: any): boolean => a || b;\n";
2
2
  export type TemplateType = {
3
3
  nodes: string;
4
4
  };
@@ -55,7 +55,20 @@ const graphConfig = {
55
55
  const __nodes = {{{nodes:string}}} as const;
56
56
  type Node = (typeof __nodes)[number];
57
57
 
58
- const graph = new PieMachine<State, Node>(__nodes, graphConfig);`;
58
+ const graph = new PieMachine<State, Node>(__nodes, graphConfig);
59
+
60
+ // builtins
61
+
62
+ const not = (val: any): boolean => !val;
63
+ const eq = (a: any, b: any): boolean => a === b;
64
+ const neq = (a: any, b: any): boolean => a !== b;
65
+ const lt = (a: any, b: any): boolean => a < b;
66
+ const lte = (a: any, b: any): boolean => a <= b;
67
+ const gt = (a: any, b: any): boolean => a > b;
68
+ const gte = (a: any, b: any): boolean => a >= b;
69
+ const and = (a: any, b: any): boolean => a && b;
70
+ const or = (a: any, b: any): boolean => a || b;
71
+ `;
59
72
  const render = (args) => {
60
73
  return apply(template, args);
61
74
  };
@@ -0,0 +1,4 @@
1
+ export declare const template = "function _builtinSleep(seconds: number): Promise<void> {\n return new Promise((resolve) => {\n setTimeout(resolve, seconds * 1000);\n });\n}";
2
+ export type TemplateType = {};
3
+ declare const render: (args: TemplateType) => string;
4
+ export default render;
@@ -0,0 +1,13 @@
1
+ // THIS FILE WAS AUTO-GENERATED
2
+ // Source: lib/templates/backends/typescriptGenerator/builtinFunctions/sleep.mustache
3
+ // Any manual changes will be lost.
4
+ import { apply } from "typestache";
5
+ export const template = `function _builtinSleep(seconds: number): Promise<void> {
6
+ return new Promise((resolve) => {
7
+ setTimeout(resolve, seconds * 1000);
8
+ });
9
+ }`;
10
+ const render = (args) => {
11
+ return apply(template, args);
12
+ };
13
+ export default render;
@@ -0,0 +1,7 @@
1
+ export declare const template = "const {{{timingVarName:string}}}_startTime = performance.now();\n{{{bodyCodeStr:string}}}\nconst {{{timingVarName}}}_endTime = performance.now();\nconst {{{timingVarName}}} = {{{timingVarName}}}_endTime - {{{timingVarName}}}_startTime;";
2
+ export type TemplateType = {
3
+ timingVarName: string;
4
+ bodyCodeStr: string;
5
+ };
6
+ declare const render: (args: TemplateType) => string;
7
+ export default render;
@@ -0,0 +1,12 @@
1
+ // THIS FILE WAS AUTO-GENERATED
2
+ // Source: lib/templates/backends/typescriptGenerator/builtinFunctions/time.mustache
3
+ // Any manual changes will be lost.
4
+ import { apply } from "typestache";
5
+ export const template = `const {{{timingVarName:string}}}_startTime = performance.now();
6
+ {{{bodyCodeStr:string}}}
7
+ const {{{timingVarName}}}_endTime = performance.now();
8
+ const {{{timingVarName}}} = {{{timingVarName}}}_endTime - {{{timingVarName}}}_startTime;`;
9
+ const render = (args) => {
10
+ return apply(template, args);
11
+ };
12
+ export default render;
@@ -1,4 +1,4 @@
1
- export declare const template = "async function {{{functionName:string}}}({{{args}}}) : Promise<{{{returnType}}}> {\n {{{functionBody}}}\n}";
1
+ export declare const template = "async function {{{functionName:string}}}({{{args}}}) : Promise<{{{returnType}}}> {\n const __messages: Message[] = [];\n {{{functionBody}}}\n}";
2
2
  export type TemplateType = {
3
3
  functionName: string;
4
4
  args: string | boolean | number;
@@ -3,6 +3,7 @@
3
3
  // Any manual changes will be lost.
4
4
  import { apply } from "typestache";
5
5
  export const template = `async function {{{functionName:string}}}({{{args}}}) : Promise<{{{returnType}}}> {
6
+ const __messages: Message[] = [];
6
7
  {{{functionBody}}}
7
8
  }`;
8
9
  const render = (args) => {
@@ -1,4 +1,4 @@
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: Message[] = [userMessage(__prompt)];\n const __tools = {{{tools}}};\n\n {{#hasResponseFormat}}\n // Need to make sure this is always an object\n const __responseFormat = z.object({\n response: {{{zodSchema:string}}}\n });\n {{/hasResponseFormat}}\n {{^hasResponseFormat}}\n const __responseFormat = undefined;\n {{/hasResponseFormat}}\n\n let __completion = await __client.text({\n messages: __messages,\n tools: __tools,\n responseFormat: __responseFormat,\n });\n\n const endTime = performance.now();\n statelogClient.promptCompletion({\n messages: __messages,\n completion: __completion,\n model: __client.getModel(),\n timeTaken: endTime - startTime,\n });\n\n if (!__completion.success) {\n throw new Error(\n `Error getting response from ${__model}: ${__completion.error}`\n );\n }\n\n let responseMessage = __completion.value;\n\n // Handle function calls\n while (responseMessage.toolCalls.length > 0) {\n // Add assistant's response with tool calls to message history\n __messages.push(assistantMessage(responseMessage.output, { toolCalls: responseMessage.toolCalls }));\n let toolCallStartTime, toolCallEndTime;\n\n // Process each tool call\n for (const toolCall of responseMessage.toolCalls) {\n {{{functionCalls:string}}}\n }\n \n const nextStartTime = performance.now();\n let __completion = await __client.text({\n messages: __messages,\n tools: __tools,\n responseFormat: __responseFormat,\n });\n\n const nextEndTime = performance.now();\n\n statelogClient.promptCompletion({\n messages: __messages,\n completion: __completion,\n model: __client.getModel(),\n timeTaken: nextEndTime - nextStartTime,\n });\n\n if (!__completion.success) {\n throw new Error(\n `Error getting response from ${__model}: ${__completion.error}`\n );\n }\n responseMessage = __completion.value;\n }\n\n // Add final assistant response to history\n __messages.push(assistantMessage(responseMessage.output, { toolCalls: responseMessage.toolCalls }));\n {{#hasResponseFormat}}\n try {\n const result = JSON.parse(responseMessage.output || \"\");\n return result.response;\n } catch (e) {\n return responseMessage.output;\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 {{/hasResponseFormat}}\n\n {{^hasResponseFormat}}\n return responseMessage.output;\n {{/hasResponseFormat}}\n}\n";
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 __messages.push(userMessage(__prompt));\n const __tools = {{{tools}}};\n\n {{#hasResponseFormat}}\n // Need to make sure this is always an object\n const __responseFormat = z.object({\n response: {{{zodSchema:string}}}\n });\n {{/hasResponseFormat}}\n {{^hasResponseFormat}}\n const __responseFormat = undefined;\n {{/hasResponseFormat}}\n\n let __completion = await __client.text({\n messages: __messages,\n tools: __tools,\n responseFormat: __responseFormat,\n });\n\n const endTime = performance.now();\n statelogClient.promptCompletion({\n messages: __messages,\n completion: __completion,\n model: __client.getModel(),\n timeTaken: endTime - startTime,\n });\n\n if (!__completion.success) {\n throw new Error(\n `Error getting response from ${__model}: ${__completion.error}`\n );\n }\n\n let responseMessage = __completion.value;\n\n // Handle function calls\n while (responseMessage.toolCalls.length > 0) {\n // Add assistant's response with tool calls to message history\n __messages.push(assistantMessage(responseMessage.output, { toolCalls: responseMessage.toolCalls }));\n let toolCallStartTime, toolCallEndTime;\n\n // Process each tool call\n for (const toolCall of responseMessage.toolCalls) {\n {{{functionCalls:string}}}\n }\n \n const nextStartTime = performance.now();\n let __completion = await __client.text({\n messages: __messages,\n tools: __tools,\n responseFormat: __responseFormat,\n });\n\n const nextEndTime = performance.now();\n\n statelogClient.promptCompletion({\n messages: __messages,\n completion: __completion,\n model: __client.getModel(),\n timeTaken: nextEndTime - nextStartTime,\n });\n\n if (!__completion.success) {\n throw new Error(\n `Error getting response from ${__model}: ${__completion.error}`\n );\n }\n responseMessage = __completion.value;\n }\n\n // Add final assistant response to history\n // not passing tool calls back this time\n __messages.push(assistantMessage(responseMessage.output));\n {{#hasResponseFormat}}\n try {\n const result = JSON.parse(responseMessage.output || \"\");\n return result.response;\n } catch (e) {\n return responseMessage.output;\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 {{/hasResponseFormat}}\n\n {{^hasResponseFormat}}\n return responseMessage.output;\n {{/hasResponseFormat}}\n}\n";
2
2
  export type TemplateType = {
3
3
  variableName: string;
4
4
  argsStr: string;
@@ -6,7 +6,7 @@ export const template = `
6
6
  async function _{{{variableName:string}}}({{{argsStr:string}}}): Promise<{{{typeString:string}}}> {
7
7
  const __prompt = {{{promptCode:string}}};
8
8
  const startTime = performance.now();
9
- const __messages: Message[] = [userMessage(__prompt)];
9
+ __messages.push(userMessage(__prompt));
10
10
  const __tools = {{{tools}}};
11
11
 
12
12
  {{#hasResponseFormat}}
@@ -77,7 +77,8 @@ async function _{{{variableName:string}}}({{{argsStr:string}}}): Promise<{{{type
77
77
  }
78
78
 
79
79
  // Add final assistant response to history
80
- __messages.push(assistantMessage(responseMessage.output, { toolCalls: responseMessage.toolCalls }));
80
+ // not passing tool calls back this time
81
+ __messages.push(assistantMessage(responseMessage.output));
81
82
  {{#hasResponseFormat}}
82
83
  try {
83
84
  const result = JSON.parse(responseMessage.output || "");
@@ -1,4 +1,4 @@
1
- export declare const template = "const {{{name:string}}}Tool = {\n name: \"{{{name:string}}}\",\n description: \"{{{description:string}}}\",\n schema: z.object({{{schema:string}}})\n};\n";
1
+ export declare const template = "const {{{name:string}}}Tool = {\n name: \"{{{name:string}}}\",\n description: `{{{description:string}}}`,\n schema: z.object({{{schema:string}}})\n};\n";
2
2
  export type TemplateType = {
3
3
  name: string;
4
4
  description: string;