agency-lang 0.0.13 → 0.0.15

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.
@@ -18,6 +18,7 @@ export declare class GraphGenerator extends TypeScriptGenerator {
18
18
  protected processFunctionCall(node: FunctionCall): string;
19
19
  protected generateNodeCallExpression(node: FunctionCall): string;
20
20
  protected generateImports(): string;
21
+ protected preprocess(): string;
21
22
  protected postprocess(): string;
22
23
  }
23
24
  export declare function generateGraph(program: AgencyProgram): string;
@@ -282,6 +282,9 @@ export class GraphGenerator extends TypeScriptGenerator {
282
282
  arr.push(builtinTools.default({}));
283
283
  return arr.join("\n");
284
284
  }
285
+ preprocess() {
286
+ return "// @ts-nocheck\n";
287
+ }
285
288
  postprocess() {
286
289
  const lines = [];
287
290
  Object.keys(this.adjacentNodes).forEach((node) => {
@@ -161,14 +161,17 @@ export class TypeScriptGenerator extends BaseGenerator {
161
161
  type: "primitiveType",
162
162
  value: "string",
163
163
  };
164
- const tsType = variableTypeToString(typeHint, this.typeAliases);
165
- properties[param] = { type: tsType, description: "" };
164
+ const tsType = mapTypeToZodSchema(typeHint, this.typeAliases);
165
+ properties[param] = tsType;
166
166
  });
167
+ let schema = "";
168
+ for (const [key, value] of Object.entries(properties)) {
169
+ schema += `"${key}": ${value}, `;
170
+ }
167
171
  return renderTool.default({
168
172
  name: functionName,
169
173
  description: node.docString?.value || "No description provided.",
170
- properties: Object.keys(properties).length > 0 ? JSON.stringify(properties) : "{}",
171
- requiredParameters: parameters.map((p) => `"${p}"`).join(","),
174
+ schema: Object.keys(properties).length > 0 ? `{${schema}}` : "{}",
172
175
  });
173
176
  }
174
177
  processUsesTool(node) {
@@ -1,4 +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 };";
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
- type: "function" as const,
12
- function: {
13
- name: "add",
14
- description:
15
- "Adds two numbers together and returns the result.",
16
- parameters: {
17
- type: "object",
18
- properties: {
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 = "// @ts-nocheck\nimport { 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
  };
@@ -2,14 +2,13 @@
2
2
  // Source: lib/templates/backends/graphGenerator/imports.mustache
3
3
  // Any manual changes will be lost.
4
4
  import { apply } from "typestache";
5
- export const template = `// @ts-nocheck
6
- import { z } from "zod";
5
+ export const template = `import { z } from "zod";
7
6
  import * as readline from "readline";
8
7
  import fs from "fs";
9
8
  import { PieMachine, goToNode } from "piemachine";
10
9
  import { StatelogClient } from "statelog-client";
11
10
  import { nanoid } from "nanoid";
12
- import { assistantMessage, getClient, userMessage } from "smoltalk";
11
+ import { assistantMessage, getClient, userMessage, toolMessage } from "smoltalk";
13
12
 
14
13
  const statelogHost = "https://statelog.adit.io";
15
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: 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";
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
- properties: string;
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: OpenAI.Chat.Completions.ChatCompletionTool = {
6
- type: "function",
7
- function: {
8
- name: "{{{name:string}}}",
9
- description:
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 (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: __client.getModel(),\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}";
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
  };
@@ -2,10 +2,10 @@
2
2
  // Source: lib/templates/backends/typescriptGenerator/toolCall.mustache
3
3
  // Any manual changes will be lost.
4
4
  import { apply } from "typestache";
5
- export const template = `if (toolCall.type === "function" &&
6
- toolCall.function.name === "{{{name:string}}}"
5
+ export const template = `if (
6
+ toolCall.name === "{{{name:string}}}"
7
7
  ) {
8
- const args = JSON.parse(toolCall.function.arguments);
8
+ const args = toolCall.arguments;
9
9
 
10
10
  toolCallStartTime = performance.now();
11
11
  const result = await {{{name}}}(args);
@@ -23,11 +23,10 @@ statelogClient.toolCall({
23
23
  });
24
24
 
25
25
  // Add function result to messages
26
- __messages.push({
27
- role: "tool",
28
- tool_call_id: toolCall.id,
29
- content: JSON.stringify(result),
30
- });
26
+ __messages.push(toolMessage(result, {
27
+ tool_call_id: toolCall.id,
28
+ name: toolCall.name,
29
+ }));
31
30
  }`;
32
31
  const render = (args) => {
33
32
  return apply(template, args);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agency-lang",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "The Agency language",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -41,6 +41,7 @@
41
41
  "homepage": "https://github.com/egonSchiele/agency-lang#readme",
42
42
  "dependencies": {
43
43
  "egonlog": "^0.0.2",
44
+ "smoltalk": "^0.0.11",
44
45
  "tarsec": "^0.1.1",
45
46
  "typestache": "^0.4.4",
46
47
  "zod": "^4.3.5"
@@ -49,7 +50,6 @@
49
50
  "@types/node": "^25.0.3",
50
51
  "nanoid": "^5.1.6",
51
52
  "piemachine": "^0.0.2",
52
- "smoltalk": "^0.0.11",
53
53
  "statelog-client": "^0.0.31",
54
54
  "tsc-alias": "^1.8.16",
55
55
  "typescript": "^5.9.3",