agency-lang 0.0.34 → 0.0.35

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.
@@ -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);\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;\nconst head = <T>(arr: T[]): T | undefined => arr[0];\nconst tail = <T>(arr: T[]): T[] => arr.slice(1);\nconst empty = <T>(arr: T[]): boolean => arr.length === 0;";
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;\nconst head = <T>(arr: T[]): T | undefined => arr[0];\nconst tail = <T>(arr: T[]): T[] => arr.slice(1);\nconst empty = <T>(arr: T[]): boolean => arr.length === 0;\n\n// interrupts\n\ntype Interrupt<T> = {\n type: \"interrupt\";\n data: T;\n};\n\nfunction interrupt<T>(data: T): Interrupt<T> {\n return {\n type: \"interrupt\",\n data,\n };\n}\n\nfunction isInterrupt<T>(obj: any): obj is Interrupt<T> {\n return obj && obj.type === \"interrupt\";\n}\n\nfunction printJSON(obj: any) {\n console.log(JSON.stringify(obj, null, 2));\n}";
2
2
  export type TemplateType = {
3
3
  nodes: string;
4
4
  };
@@ -70,7 +70,29 @@ const and = (a: any, b: any): boolean => a && b;
70
70
  const or = (a: any, b: any): boolean => a || b;
71
71
  const head = <T>(arr: T[]): T | undefined => arr[0];
72
72
  const tail = <T>(arr: T[]): T[] => arr.slice(1);
73
- const empty = <T>(arr: T[]): boolean => arr.length === 0;`;
73
+ const empty = <T>(arr: T[]): boolean => arr.length === 0;
74
+
75
+ // interrupts
76
+
77
+ type Interrupt<T> = {
78
+ type: "interrupt";
79
+ data: T;
80
+ };
81
+
82
+ function interrupt<T>(data: T): Interrupt<T> {
83
+ return {
84
+ type: "interrupt",
85
+ data,
86
+ };
87
+ }
88
+
89
+ function isInterrupt<T>(obj: any): obj is Interrupt<T> {
90
+ return obj && obj.type === "interrupt";
91
+ }
92
+
93
+ function printJSON(obj: any) {
94
+ console.log(JSON.stringify(obj, null, 2));
95
+ }`;
74
96
  const render = (args) => {
75
97
  return apply(template, args);
76
98
  };
@@ -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 __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 const __client = getClientWithConfig({{{clientConfig:string}}});\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";
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 const __client = getClientWithConfig({{{clientConfig:string}}});\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 let haltExecution = false;\n\n // Process each tool call\n for (const toolCall of responseMessage.toolCalls) {\n {{{functionCalls:string}}}\n }\n\n if (haltExecution) {\n statelogClient.debug(`Tool call interrupted execution.`, {\n messages: __messages,\n model: __client.getModel(),\n });\n try {\n const obj = JSON.parse(__messages.at(-1).content);\n obj.__messages = __messages;\n return obj;\n } catch (e) {\n return __messages.at(-1).content;\n }\n //return __messages;\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;
@@ -48,11 +48,27 @@ async function _{{{variableName:string}}}({{{argsStr:string}}}): Promise<{{{type
48
48
  // Add assistant's response with tool calls to message history
49
49
  __messages.push(assistantMessage(responseMessage.output, { toolCalls: responseMessage.toolCalls }));
50
50
  let toolCallStartTime, toolCallEndTime;
51
+ let haltExecution = false;
51
52
 
52
53
  // Process each tool call
53
54
  for (const toolCall of responseMessage.toolCalls) {
54
55
  {{{functionCalls:string}}}
55
56
  }
57
+
58
+ if (haltExecution) {
59
+ statelogClient.debug(\`Tool call interrupted execution.\`, {
60
+ messages: __messages,
61
+ model: __client.getModel(),
62
+ });
63
+ try {
64
+ const obj = JSON.parse(__messages.at(-1).content);
65
+ obj.__messages = __messages;
66
+ return obj;
67
+ } catch (e) {
68
+ return __messages.at(-1).content;
69
+ }
70
+ //return __messages;
71
+ }
56
72
 
57
73
  const nextStartTime = performance.now();
58
74
  let __completion = await __client.text({
@@ -1,4 +1,4 @@
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}";
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\n if (isInterrupt(result)) {\n haltExecution = true;\n break;\n }\n}";
2
2
  export type TemplateType = {
3
3
  name: string;
4
4
  };
@@ -27,6 +27,11 @@ statelogClient.toolCall({
27
27
  tool_call_id: toolCall.id,
28
28
  name: toolCall.name,
29
29
  }));
30
+
31
+ if (isInterrupt(result)) {
32
+ haltExecution = true;
33
+ break;
34
+ }
30
35
  }`;
31
36
  const render = (args) => {
32
37
  return apply(template, args);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agency-lang",
3
- "version": "0.0.34",
3
+ "version": "0.0.35",
4
4
  "description": "The Agency language",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {