agency-lang 0.0.14 → 0.0.16
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.
- package/dist/lib/backends/agencyGenerator.js +20 -3
- package/dist/lib/backends/agencyGenerator.test.d.ts +1 -0
- package/dist/lib/backends/agencyGenerator.test.js +205 -0
- package/dist/lib/backends/baseGenerator.js +3 -1
- package/dist/lib/backends/graphGenerator.js +7 -5
- package/dist/lib/backends/typescriptGenerator.d.ts +3 -3
- package/dist/lib/backends/typescriptGenerator.js +20 -15
- package/dist/lib/parsers/access.d.ts +3 -5
- package/dist/lib/parsers/access.js +54 -13
- package/dist/lib/parsers/access.test.js +40 -500
- package/dist/lib/parsers/assignment.js +2 -2
- package/dist/lib/parsers/assignment.test.d.ts +1 -0
- package/dist/lib/parsers/assignment.test.js +279 -0
- package/dist/lib/parsers/dataStructures.js +3 -3
- package/dist/lib/parsers/function.d.ts +3 -1
- package/dist/lib/parsers/function.js +6 -4
- package/dist/lib/parsers/function.test.js +653 -8
- package/dist/lib/parsers/functionCall.js +3 -2
- package/dist/lib/parsers/functionCall.test.js +310 -0
- package/dist/lib/parsers/literals.js +11 -2
- package/dist/lib/parsers/literals.test.js +7 -7
- package/dist/lib/parsers/matchBlock.js +2 -2
- package/dist/lib/parsers/parserUtils.test.d.ts +1 -0
- package/dist/lib/parsers/parserUtils.test.js +46 -0
- package/dist/lib/parsers/returnStatement.js +2 -2
- package/dist/lib/parsers/returnStatement.test.d.ts +1 -0
- package/dist/lib/parsers/returnStatement.test.js +268 -0
- package/dist/lib/parsers/specialVar.test.d.ts +1 -0
- package/dist/lib/parsers/specialVar.test.js +219 -0
- package/dist/lib/templates/backends/graphGenerator/builtinTools.d.ts +1 -1
- package/dist/lib/templates/backends/graphGenerator/builtinTools.js +8 -23
- package/dist/lib/templates/backends/graphGenerator/imports.d.ts +1 -1
- package/dist/lib/templates/backends/graphGenerator/imports.js +1 -1
- package/dist/lib/templates/backends/typescriptGenerator/promptFunction.d.ts +1 -1
- package/dist/lib/templates/backends/typescriptGenerator/promptFunction.js +2 -2
- package/dist/lib/templates/backends/typescriptGenerator/tool.d.ts +2 -3
- package/dist/lib/templates/backends/typescriptGenerator/tool.js +5 -14
- package/dist/lib/templates/backends/typescriptGenerator/toolCall.d.ts +1 -1
- package/dist/lib/templates/backends/typescriptGenerator/toolCall.js +7 -8
- package/dist/lib/types/access.d.ts +5 -5
- package/dist/lib/types/access.js +6 -1
- package/dist/lib/types/dataStructures.d.ts +3 -3
- package/dist/lib/types/function.d.ts +10 -4
- package/dist/lib/types/matchBlock.d.ts +2 -2
- package/dist/lib/types/returnStatement.d.ts +2 -2
- package/dist/lib/types/whileLoop.d.ts +2 -2
- package/dist/lib/types.d.ts +3 -3
- package/package.json +4 -4
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { returnStatementParser } from "./returnStatement.js";
|
|
3
|
+
describe("returnStatementParser", () => {
|
|
4
|
+
const testCases = [
|
|
5
|
+
// Happy path - returning literals
|
|
6
|
+
{
|
|
7
|
+
input: "return 42",
|
|
8
|
+
expected: {
|
|
9
|
+
success: true,
|
|
10
|
+
result: {
|
|
11
|
+
type: "returnStatement",
|
|
12
|
+
value: { type: "number", value: "42" },
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
input: 'return "hello"',
|
|
18
|
+
expected: {
|
|
19
|
+
success: true,
|
|
20
|
+
result: {
|
|
21
|
+
type: "returnStatement",
|
|
22
|
+
value: { type: "string", value: "hello" },
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
input: "return `say hello`",
|
|
28
|
+
expected: {
|
|
29
|
+
success: true,
|
|
30
|
+
result: {
|
|
31
|
+
type: "returnStatement",
|
|
32
|
+
value: {
|
|
33
|
+
type: "prompt",
|
|
34
|
+
segments: [{ type: "text", value: "say hello" }],
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
input: "return myVar",
|
|
41
|
+
expected: {
|
|
42
|
+
success: true,
|
|
43
|
+
result: {
|
|
44
|
+
type: "returnStatement",
|
|
45
|
+
value: { type: "variableName", value: "myVar" },
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
// With optional semicolon
|
|
50
|
+
{
|
|
51
|
+
input: "return 123;",
|
|
52
|
+
expected: {
|
|
53
|
+
success: true,
|
|
54
|
+
result: {
|
|
55
|
+
type: "returnStatement",
|
|
56
|
+
value: { type: "number", value: "123" },
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
// With optional spaces
|
|
61
|
+
{
|
|
62
|
+
input: "return 42 ",
|
|
63
|
+
expected: {
|
|
64
|
+
success: true,
|
|
65
|
+
result: {
|
|
66
|
+
type: "returnStatement",
|
|
67
|
+
value: { type: "number", value: "42" },
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
input: "return 42",
|
|
73
|
+
expected: {
|
|
74
|
+
success: true,
|
|
75
|
+
result: {
|
|
76
|
+
type: "returnStatement",
|
|
77
|
+
value: { type: "number", value: "42" },
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
// Return function call
|
|
82
|
+
{
|
|
83
|
+
input: "return foo()",
|
|
84
|
+
expected: {
|
|
85
|
+
success: true,
|
|
86
|
+
result: {
|
|
87
|
+
type: "returnStatement",
|
|
88
|
+
value: {
|
|
89
|
+
type: "functionCall",
|
|
90
|
+
functionName: "foo",
|
|
91
|
+
arguments: [],
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
input: "return calculate(1, 2)",
|
|
98
|
+
expected: {
|
|
99
|
+
success: true,
|
|
100
|
+
result: {
|
|
101
|
+
type: "returnStatement",
|
|
102
|
+
value: {
|
|
103
|
+
type: "functionCall",
|
|
104
|
+
functionName: "calculate",
|
|
105
|
+
arguments: [
|
|
106
|
+
{ type: "number", value: "1" },
|
|
107
|
+
{ type: "number", value: "2" },
|
|
108
|
+
],
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
// Return access expression
|
|
114
|
+
{
|
|
115
|
+
input: "return obj.property",
|
|
116
|
+
expected: {
|
|
117
|
+
success: true,
|
|
118
|
+
result: {
|
|
119
|
+
type: "returnStatement",
|
|
120
|
+
value: {
|
|
121
|
+
type: "accessExpression",
|
|
122
|
+
expression: {
|
|
123
|
+
type: "dotProperty",
|
|
124
|
+
object: { type: "variableName", value: "obj" },
|
|
125
|
+
propertyName: "property",
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
// Return array
|
|
132
|
+
{
|
|
133
|
+
input: "return [1, 2, 3]",
|
|
134
|
+
expected: {
|
|
135
|
+
success: true,
|
|
136
|
+
result: {
|
|
137
|
+
type: "returnStatement",
|
|
138
|
+
value: {
|
|
139
|
+
type: "agencyArray",
|
|
140
|
+
items: [
|
|
141
|
+
{ type: "number", value: "1" },
|
|
142
|
+
{ type: "number", value: "2" },
|
|
143
|
+
{ type: "number", value: "3" },
|
|
144
|
+
],
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
input: "return []",
|
|
151
|
+
expected: {
|
|
152
|
+
success: true,
|
|
153
|
+
result: {
|
|
154
|
+
type: "returnStatement",
|
|
155
|
+
value: {
|
|
156
|
+
type: "agencyArray",
|
|
157
|
+
items: [],
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
// Return object
|
|
163
|
+
{
|
|
164
|
+
input: 'return { key: "value" }',
|
|
165
|
+
expected: {
|
|
166
|
+
success: true,
|
|
167
|
+
result: {
|
|
168
|
+
type: "returnStatement",
|
|
169
|
+
value: {
|
|
170
|
+
type: "agencyObject",
|
|
171
|
+
entries: [
|
|
172
|
+
{
|
|
173
|
+
key: "key",
|
|
174
|
+
value: { type: "string", value: "value" },
|
|
175
|
+
},
|
|
176
|
+
],
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
input: "return {}",
|
|
183
|
+
expected: {
|
|
184
|
+
success: true,
|
|
185
|
+
result: {
|
|
186
|
+
type: "returnStatement",
|
|
187
|
+
value: {
|
|
188
|
+
type: "agencyObject",
|
|
189
|
+
entries: [],
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
input: 'return { x: 1, y: 2 }',
|
|
196
|
+
expected: {
|
|
197
|
+
success: true,
|
|
198
|
+
result: {
|
|
199
|
+
type: "returnStatement",
|
|
200
|
+
value: {
|
|
201
|
+
type: "agencyObject",
|
|
202
|
+
entries: [
|
|
203
|
+
{ key: "x", value: { type: "number", value: "1" } },
|
|
204
|
+
{ key: "y", value: { type: "number", value: "2" } },
|
|
205
|
+
],
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
// Negative numbers
|
|
211
|
+
{
|
|
212
|
+
input: "return -100",
|
|
213
|
+
expected: {
|
|
214
|
+
success: true,
|
|
215
|
+
result: {
|
|
216
|
+
type: "returnStatement",
|
|
217
|
+
value: { type: "number", value: "-100" },
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
// Decimal numbers
|
|
222
|
+
{
|
|
223
|
+
input: "return 3.14159",
|
|
224
|
+
expected: {
|
|
225
|
+
success: true,
|
|
226
|
+
result: {
|
|
227
|
+
type: "returnStatement",
|
|
228
|
+
value: { type: "number", value: "3.14159" },
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
// With semicolon and spaces
|
|
233
|
+
{
|
|
234
|
+
input: "return someValue ;",
|
|
235
|
+
expected: {
|
|
236
|
+
success: true,
|
|
237
|
+
result: {
|
|
238
|
+
type: "returnStatement",
|
|
239
|
+
value: { type: "variableName", value: "someValue" },
|
|
240
|
+
},
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
// Failure cases
|
|
244
|
+
{ input: "return", expected: { success: false } },
|
|
245
|
+
{ input: "return;", expected: { success: false } },
|
|
246
|
+
{ input: "return ", expected: { success: false } },
|
|
247
|
+
{ input: "retur 5", expected: { success: false } },
|
|
248
|
+
{ input: "", expected: { success: false } },
|
|
249
|
+
{ input: "42", expected: { success: false } },
|
|
250
|
+
];
|
|
251
|
+
testCases.forEach(({ input, expected }) => {
|
|
252
|
+
if (expected.success) {
|
|
253
|
+
it(`should parse "${input}" successfully`, () => {
|
|
254
|
+
const result = returnStatementParser(input);
|
|
255
|
+
expect(result.success).toBe(true);
|
|
256
|
+
if (result.success) {
|
|
257
|
+
expect(result.result).toEqual(expected.result);
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
it(`should fail to parse "${input}"`, () => {
|
|
263
|
+
const result = returnStatementParser(input);
|
|
264
|
+
expect(result.success).toBe(false);
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { specialVarParser } from "./specialVar.js";
|
|
3
|
+
describe("specialVar parsers", () => {
|
|
4
|
+
describe("specialVarParser", () => {
|
|
5
|
+
const testCases = [
|
|
6
|
+
// Happy path - assigning literals to special vars
|
|
7
|
+
{
|
|
8
|
+
input: '@model = "gpt-4"',
|
|
9
|
+
expected: {
|
|
10
|
+
success: true,
|
|
11
|
+
result: {
|
|
12
|
+
type: "specialVar",
|
|
13
|
+
name: "model",
|
|
14
|
+
value: { type: "string", value: "gpt-4" },
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
input: "@model = 42",
|
|
20
|
+
expected: {
|
|
21
|
+
success: true,
|
|
22
|
+
result: {
|
|
23
|
+
type: "specialVar",
|
|
24
|
+
name: "model",
|
|
25
|
+
value: { type: "number", value: "42" },
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
input: "@model = `the best model`",
|
|
31
|
+
expected: {
|
|
32
|
+
success: true,
|
|
33
|
+
result: {
|
|
34
|
+
type: "specialVar",
|
|
35
|
+
name: "model",
|
|
36
|
+
value: {
|
|
37
|
+
type: "prompt",
|
|
38
|
+
segments: [{ type: "text", value: "the best model" }],
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
input: "@model = myVariable",
|
|
45
|
+
expected: {
|
|
46
|
+
success: true,
|
|
47
|
+
result: {
|
|
48
|
+
type: "specialVar",
|
|
49
|
+
name: "model",
|
|
50
|
+
value: { type: "variableName", value: "myVariable" },
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
// With optional semicolon
|
|
55
|
+
{
|
|
56
|
+
input: '@model = "gpt-4";',
|
|
57
|
+
expected: {
|
|
58
|
+
success: true,
|
|
59
|
+
result: {
|
|
60
|
+
type: "specialVar",
|
|
61
|
+
name: "model",
|
|
62
|
+
value: { type: "string", value: "gpt-4" },
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
input: '@model="gpt-4"',
|
|
68
|
+
expected: {
|
|
69
|
+
success: true,
|
|
70
|
+
result: {
|
|
71
|
+
type: "specialVar",
|
|
72
|
+
name: "model",
|
|
73
|
+
value: { type: "string", value: "gpt-4" },
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
// Assigning arrays to special vars
|
|
78
|
+
{
|
|
79
|
+
input: "@model = []",
|
|
80
|
+
expected: {
|
|
81
|
+
success: true,
|
|
82
|
+
result: {
|
|
83
|
+
type: "specialVar",
|
|
84
|
+
name: "model",
|
|
85
|
+
value: {
|
|
86
|
+
type: "agencyArray",
|
|
87
|
+
items: [],
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
input: '@model = ["gpt-4", "claude"]',
|
|
94
|
+
expected: {
|
|
95
|
+
success: true,
|
|
96
|
+
result: {
|
|
97
|
+
type: "specialVar",
|
|
98
|
+
name: "model",
|
|
99
|
+
value: {
|
|
100
|
+
type: "agencyArray",
|
|
101
|
+
items: [
|
|
102
|
+
{ type: "string", value: "gpt-4" },
|
|
103
|
+
{ type: "string", value: "claude" },
|
|
104
|
+
],
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
// Assigning objects to special vars
|
|
110
|
+
{
|
|
111
|
+
input: "@model = {}",
|
|
112
|
+
expected: {
|
|
113
|
+
success: true,
|
|
114
|
+
result: {
|
|
115
|
+
type: "specialVar",
|
|
116
|
+
name: "model",
|
|
117
|
+
value: {
|
|
118
|
+
type: "agencyObject",
|
|
119
|
+
entries: [],
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
input: '@model = { name: "gpt-4" }',
|
|
126
|
+
expected: {
|
|
127
|
+
success: true,
|
|
128
|
+
result: {
|
|
129
|
+
type: "specialVar",
|
|
130
|
+
name: "model",
|
|
131
|
+
value: {
|
|
132
|
+
type: "agencyObject",
|
|
133
|
+
entries: [
|
|
134
|
+
{
|
|
135
|
+
key: "name",
|
|
136
|
+
value: { type: "string", value: "gpt-4" },
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
input: '@model = { name: "gpt-4", version: 4 }',
|
|
145
|
+
expected: {
|
|
146
|
+
success: true,
|
|
147
|
+
result: {
|
|
148
|
+
type: "specialVar",
|
|
149
|
+
name: "model",
|
|
150
|
+
value: {
|
|
151
|
+
type: "agencyObject",
|
|
152
|
+
entries: [
|
|
153
|
+
{
|
|
154
|
+
key: "name",
|
|
155
|
+
value: { type: "string", value: "gpt-4" },
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
key: "version",
|
|
159
|
+
value: { type: "number", value: "4" },
|
|
160
|
+
},
|
|
161
|
+
],
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
// Negative numbers
|
|
167
|
+
{
|
|
168
|
+
input: "@model = -100",
|
|
169
|
+
expected: {
|
|
170
|
+
success: true,
|
|
171
|
+
result: {
|
|
172
|
+
type: "specialVar",
|
|
173
|
+
name: "model",
|
|
174
|
+
value: { type: "number", value: "-100" },
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
// Decimal numbers
|
|
179
|
+
{
|
|
180
|
+
input: "@model = 3.14",
|
|
181
|
+
expected: {
|
|
182
|
+
success: true,
|
|
183
|
+
result: {
|
|
184
|
+
type: "specialVar",
|
|
185
|
+
name: "model",
|
|
186
|
+
value: { type: "number", value: "3.14" },
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
// Failure cases
|
|
191
|
+
{ input: "@model =", expected: { success: false } },
|
|
192
|
+
{ input: '@notSpecial = "value"', expected: { success: false } },
|
|
193
|
+
{ input: 'model = "value"', expected: { success: false } }, // missing @
|
|
194
|
+
{ input: '@Model = "value"', expected: { success: false } }, // wrong case
|
|
195
|
+
{ input: '@ model = "value"', expected: { success: false } }, // space after @
|
|
196
|
+
{ input: "", expected: { success: false } },
|
|
197
|
+
{ input: "@", expected: { success: false } },
|
|
198
|
+
{ input: "@model", expected: { success: false } },
|
|
199
|
+
{ input: '@"model" = "value"', expected: { success: false } },
|
|
200
|
+
];
|
|
201
|
+
testCases.forEach(({ input, expected }) => {
|
|
202
|
+
if (expected.success) {
|
|
203
|
+
it(`should parse "${input}" successfully`, () => {
|
|
204
|
+
const result = specialVarParser(input);
|
|
205
|
+
expect(result.success).toBe(true);
|
|
206
|
+
if (result.success) {
|
|
207
|
+
expect(result.result).toEqual(expected.result);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
it(`should fail to parse "${input}"`, () => {
|
|
213
|
+
const result = specialVarParser(input);
|
|
214
|
+
expect(result.success).toBe(false);
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const template = "function add({a, b}: {a:number, b:number}):number {\n return a + b;\n}\n\
|
|
1
|
+
export declare const template = "function add({a, b}: {a:number, b:number}):number {\n return a + b;\n}\n\nconst addTool = {\n name: \"add\",\n description: \"Adds two numbers together and returns the result.\",\n schema: z.object({\n a: z.number().describe(\"The first number to add\"),\n b: z.number().describe(\"The second number to add\"),\n }),\n};\n";
|
|
2
2
|
export type TemplateType = {};
|
|
3
3
|
declare const render: (args: TemplateType) => string;
|
|
4
4
|
export default render;
|
|
@@ -6,30 +6,15 @@ export const template = `function add({a, b}: {a:number, b:number}):number {
|
|
|
6
6
|
return a + b;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
// Define the function tool for OpenAI
|
|
10
9
|
const addTool = {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
a: {
|
|
20
|
-
type: "number",
|
|
21
|
-
description: "The first number to add",
|
|
22
|
-
},
|
|
23
|
-
b: {
|
|
24
|
-
type: "number",
|
|
25
|
-
description: "The second number to add",
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
required: ["a", "b"],
|
|
29
|
-
additionalProperties: false,
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
};`;
|
|
10
|
+
name: "add",
|
|
11
|
+
description: "Adds two numbers together and returns the result.",
|
|
12
|
+
schema: z.object({
|
|
13
|
+
a: z.number().describe("The first number to add"),
|
|
14
|
+
b: z.number().describe("The second number to add"),
|
|
15
|
+
}),
|
|
16
|
+
};
|
|
17
|
+
`;
|
|
33
18
|
const render = (args) => {
|
|
34
19
|
return apply(template, args);
|
|
35
20
|
};
|
|
@@ -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 } 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);";
|
|
2
2
|
export type TemplateType = {
|
|
3
3
|
nodes: string;
|
|
4
4
|
};
|
|
@@ -8,7 +8,7 @@ import fs from "fs";
|
|
|
8
8
|
import { PieMachine, goToNode } from "piemachine";
|
|
9
9
|
import { StatelogClient } from "statelog-client";
|
|
10
10
|
import { nanoid } from "nanoid";
|
|
11
|
-
import { assistantMessage, getClient, userMessage } from "smoltalk";
|
|
11
|
+
import { assistantMessage, getClient, userMessage, toolMessage } from "smoltalk";
|
|
12
12
|
|
|
13
13
|
const statelogHost = "https://statelog.adit.io";
|
|
14
14
|
const traceId = nanoid();
|
|
@@ -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));\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));\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 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";
|
|
2
2
|
export type TemplateType = {
|
|
3
3
|
variableName: string;
|
|
4
4
|
argsStr: string;
|
|
@@ -44,7 +44,7 @@ async function _{{{variableName:string}}}({{{argsStr:string}}}): Promise<{{{type
|
|
|
44
44
|
// Handle function calls
|
|
45
45
|
while (responseMessage.toolCalls.length > 0) {
|
|
46
46
|
// Add assistant's response with tool calls to message history
|
|
47
|
-
__messages.push(assistantMessage(responseMessage.output));
|
|
47
|
+
__messages.push(assistantMessage(responseMessage.output, { toolCalls: responseMessage.toolCalls }));
|
|
48
48
|
let toolCallStartTime, toolCallEndTime;
|
|
49
49
|
|
|
50
50
|
// Process each tool call
|
|
@@ -77,7 +77,7 @@ 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));
|
|
80
|
+
__messages.push(assistantMessage(responseMessage.output, { toolCalls: responseMessage.toolCalls }));
|
|
81
81
|
{{#hasResponseFormat}}
|
|
82
82
|
try {
|
|
83
83
|
const result = JSON.parse(responseMessage.output || "");
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
export declare const template = "const {{{name:string}}}Tool
|
|
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;
|
|
5
|
-
|
|
6
|
-
requiredParameters: string;
|
|
5
|
+
schema: string;
|
|
7
6
|
};
|
|
8
7
|
declare const render: (args: TemplateType) => string;
|
|
9
8
|
export default render;
|
|
@@ -2,20 +2,11 @@
|
|
|
2
2
|
// Source: lib/templates/backends/typescriptGenerator/tool.mustache
|
|
3
3
|
// Any manual changes will be lost.
|
|
4
4
|
import { apply } from "typestache";
|
|
5
|
-
export const template = `const {{{name:string}}}Tool
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"{{{description:string}}}",
|
|
11
|
-
parameters: {
|
|
12
|
-
type: "object",
|
|
13
|
-
properties: {{{properties:string}}},
|
|
14
|
-
required: [{{{requiredParameters:string}}}],
|
|
15
|
-
additionalProperties: false,
|
|
16
|
-
},
|
|
17
|
-
},
|
|
18
|
-
};
|
|
5
|
+
export const template = `const {{{name:string}}}Tool = {
|
|
6
|
+
name: "{{{name:string}}}",
|
|
7
|
+
description: "{{{description:string}}}",
|
|
8
|
+
schema: z.object({{{schema:string}}})
|
|
9
|
+
};
|
|
19
10
|
`;
|
|
20
11
|
const render = (args) => {
|
|
21
12
|
return apply(template, args);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const template = "if (
|
|
1
|
+
export declare const template = "if (\n toolCall.name === \"{{{name:string}}}\"\n) {\n const args = toolCall.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: __client.getModel(),\n timeTaken: toolCallEndTime - toolCallStartTime,\n });\n\n // Add function result to messages\n __messages.push(toolMessage(result, {\n tool_call_id: toolCall.id,\n name: toolCall.name,\n }));\n}";
|
|
2
2
|
export type TemplateType = {
|
|
3
3
|
name: string;
|
|
4
4
|
};
|