aimyapi 1.0.4 → 1.0.6

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/README.md CHANGED
@@ -6,9 +6,9 @@ Requires an OpenAI compatible API
6
6
 
7
7
  AIMyAPI will handle async operations and timers in your API object.
8
8
 
9
- ## About version 1.0 rewrite and motivation
9
+ ## About version 1.0 update and motivation
10
10
 
11
- AIMyAPI was originally written at a time (circa ChatGPT 3.5 / GPT-4) before tool calling existed, before Claude code and codex and just before OpenAI released their code interpreter. LLM models have also gotten drastically better since. Some of the problems it originally aimed to solve are better solved today with simple tool calling. However, there is still a need to safely run small programs in a sandboxed way, reliable way. Tools are good if you have a set interface but there may be times that an agent needs to do some sorting or filtering, or an algorithm or analysis, or some repeatable workflow which isn't implemented by the available tool functions. Writing code allows the agent to do things it might not otherwise be able to do with tools alone. Having the ability to write code allows the tools API surface to be smaller; it could simply be a set of CRUD operations.
11
+ AIMyAPI was originally written at a time (circa ChatGPT 3.5 / GPT-4) before tool calling existed, before Claude code and codex and just before OpenAI released their code interpreter. LLM models have also gotten drastically better since. Some of the problems it originally aimed to solve are better solved today with simple tool calling. However, there is still a need to safely run small programs in a sandboxed way, reliable way. Tools are good if you have a set interface but there may be times that an agent needs to do some sorting or filtering, or an algorithm or analysis, or some repeatable workflow which isn't implemented by the available tool functions. Writing code allows the agent to do things it might not otherwise be able to do with tools alone. All the AI models are thoroughly trained to write code do solve all kinds of problems. Having the ability to write code allows the tools API surface to be smaller; it could simply be a set of CRUD operations.
12
12
 
13
13
  The other advantage of writing code is privacy and security. When an agent executes code in a sandbox, it doesn't see any of the results*, API keys or other things you may want to keep hidden. When an agent uses tools, it sees all the parameters and results of the tool calls. Code can be audited for security and correctness before running it. Finally, code can encompass a series of operations, whereas tool calls need to be approved one at a time or just trusted, often without knowing what will come next.
14
14
 
@@ -89,8 +89,8 @@ const api = new API();
89
89
  const aimyapi = await AIMyAPI.createWithAPI({
90
90
  apiObject: api,
91
91
  apiExports: APIExports,
92
- apiDefFilePath: path.join(__dirname, "./sales_api.ts"),
93
- apiDocsPath: path.join(__dirname, "./sales_api.md"),
92
+ apiDefFilePath: "./sales_api.ts",
93
+ apiDocsPath: "./sales_api.md",
94
94
  })
95
95
  ```
96
96
 
package/dist/index.cjs CHANGED
@@ -3,7 +3,6 @@
3
3
  var module$1 = require('module');
4
4
  var fs = require('fs');
5
5
  var openai$1 = require('openai');
6
- var path = require('path');
7
6
  var quickjsEmscripten = require('quickjs-emscripten');
8
7
  var ts = require('typescript');
9
8
  var zod = require('zod');
@@ -224,7 +223,7 @@ function tsCompile(source, options) {
224
223
  if (!options) {
225
224
  options = {
226
225
  compilerOptions: {
227
- target: ts__namespace.ScriptTarget.ES2020,
226
+ target: ts__namespace.ScriptTarget.ES2022,
228
227
  module: ts__namespace.ModuleKind.None
229
228
  }
230
229
  };
@@ -380,7 +379,7 @@ async function createSandbox(QuickJS, requireLookup = {}, globals = {}, debug =
380
379
  result.value.dispose();
381
380
  if (errorState) {
382
381
  const lines = compiled.split("\n").map((line, index) => `${index + 1}: ${line}`);
383
- logger.error(["An error was reported during execution. Compiled code with line numbers:", lines.join("\n")], "runner");
382
+ logger.error(["An error was reported during execution. Compiled code with line numbers:\n", lines.join("\n")], "runner");
384
383
  }
385
384
  return {
386
385
  success: !errorState,
@@ -427,7 +426,7 @@ function zodParseJSON(schema) {
427
426
  function createBasePrompt(apiFilePath, apiGlobalName, documentationPath) {
428
427
  const apiText = fs.readFileSync(apiFilePath, "utf8");
429
428
  const documentationText = documentationPath ? fs.readFileSync(documentationPath, "utf8") : "";
430
- const createTaskPrompt = fs.readFileSync(path.join(__dirname, "../prompts/create-task-prompt.md"), "utf8").replace("{{DOCUMENTATION}}", documentationText).replace("{{API}}", apiText).replace("{{API_GLOBAL_NAME}}", apiGlobalName || "");
429
+ const createTaskPrompt = fs.readFileSync("./prompts/create-task-prompt.md", "utf8").replace("{{DOCUMENTATION}}", documentationText).replace("{{API}}", apiText).replace("{{API_GLOBAL_NAME}}", apiGlobalName || "");
431
430
  return createTaskPrompt;
432
431
  }
433
432
  const runCodeSchema = zod.z.object({
@@ -505,7 +504,7 @@ const generateCode = async function(instance, queryText, userChatHistory, create
505
504
  messages: [
506
505
  {
507
506
  role: "system",
508
- content: "You will assist with writing code to perform the user's request."
507
+ content: "You will assist with writing code to perform the user's request. You can call `runCode` to run code in a sandboxed environment that has access to the API object. You can call `checkCode` to check code for errors without running it. Call `submitCode` when you are ready to submit the final code to run."
509
508
  },
510
509
  ...userChatHistory,
511
510
  {
package/dist/index.js CHANGED
@@ -3,7 +3,6 @@
3
3
  var module$1 = require('module');
4
4
  var fs = require('fs');
5
5
  var openai$1 = require('openai');
6
- var path = require('path');
7
6
  var quickjsEmscripten = require('quickjs-emscripten');
8
7
  var ts = require('typescript');
9
8
  var zod = require('zod');
@@ -224,7 +223,7 @@ function tsCompile(source, options) {
224
223
  if (!options) {
225
224
  options = {
226
225
  compilerOptions: {
227
- target: ts__namespace.ScriptTarget.ES2020,
226
+ target: ts__namespace.ScriptTarget.ES2022,
228
227
  module: ts__namespace.ModuleKind.None
229
228
  }
230
229
  };
@@ -380,7 +379,7 @@ async function createSandbox(QuickJS, requireLookup = {}, globals = {}, debug =
380
379
  result.value.dispose();
381
380
  if (errorState) {
382
381
  const lines = compiled.split("\n").map((line, index) => `${index + 1}: ${line}`);
383
- logger.error(["An error was reported during execution. Compiled code with line numbers:", lines.join("\n")], "runner");
382
+ logger.error(["An error was reported during execution. Compiled code with line numbers:\n", lines.join("\n")], "runner");
384
383
  }
385
384
  return {
386
385
  success: !errorState,
@@ -427,7 +426,7 @@ function zodParseJSON(schema) {
427
426
  function createBasePrompt(apiFilePath, apiGlobalName, documentationPath) {
428
427
  const apiText = fs.readFileSync(apiFilePath, "utf8");
429
428
  const documentationText = documentationPath ? fs.readFileSync(documentationPath, "utf8") : "";
430
- const createTaskPrompt = fs.readFileSync(path.join(__dirname, "../prompts/create-task-prompt.md"), "utf8").replace("{{DOCUMENTATION}}", documentationText).replace("{{API}}", apiText).replace("{{API_GLOBAL_NAME}}", apiGlobalName || "");
429
+ const createTaskPrompt = fs.readFileSync("./prompts/create-task-prompt.md", "utf8").replace("{{DOCUMENTATION}}", documentationText).replace("{{API}}", apiText).replace("{{API_GLOBAL_NAME}}", apiGlobalName || "");
431
430
  return createTaskPrompt;
432
431
  }
433
432
  const runCodeSchema = zod.z.object({
package/dist/index.mjs CHANGED
@@ -1,7 +1,6 @@
1
1
  import { createRequire } from 'module';
2
2
  import fs from 'fs';
3
3
  import { OpenAI } from 'openai';
4
- import path from 'path';
5
4
  import { getQuickJS } from 'quickjs-emscripten';
6
5
  import * as ts from 'typescript';
7
6
  import { z } from 'zod';
@@ -202,7 +201,7 @@ function tsCompile(source, options) {
202
201
  if (!options) {
203
202
  options = {
204
203
  compilerOptions: {
205
- target: ts.ScriptTarget.ES2020,
204
+ target: ts.ScriptTarget.ES2022,
206
205
  module: ts.ModuleKind.None
207
206
  }
208
207
  };
@@ -358,7 +357,7 @@ async function createSandbox(QuickJS, requireLookup = {}, globals = {}, debug =
358
357
  result.value.dispose();
359
358
  if (errorState) {
360
359
  const lines = compiled.split("\n").map((line, index) => `${index + 1}: ${line}`);
361
- logger.error(["An error was reported during execution. Compiled code with line numbers:", lines.join("\n")], "runner");
360
+ logger.error(["An error was reported during execution. Compiled code with line numbers:\n", lines.join("\n")], "runner");
362
361
  }
363
362
  return {
364
363
  success: !errorState,
@@ -405,7 +404,7 @@ function zodParseJSON(schema) {
405
404
  function createBasePrompt(apiFilePath, apiGlobalName, documentationPath) {
406
405
  const apiText = fs.readFileSync(apiFilePath, "utf8");
407
406
  const documentationText = documentationPath ? fs.readFileSync(documentationPath, "utf8") : "";
408
- const createTaskPrompt = fs.readFileSync(path.join(__dirname, "../prompts/create-task-prompt.md"), "utf8").replace("{{DOCUMENTATION}}", documentationText).replace("{{API}}", apiText).replace("{{API_GLOBAL_NAME}}", apiGlobalName || "");
407
+ const createTaskPrompt = fs.readFileSync("./prompts/create-task-prompt.md", "utf8").replace("{{DOCUMENTATION}}", documentationText).replace("{{API}}", apiText).replace("{{API_GLOBAL_NAME}}", apiGlobalName || "");
409
408
  return createTaskPrompt;
410
409
  }
411
410
  const runCodeSchema = z.object({
@@ -1,4 +1,3 @@
1
- import path from "path";
2
1
  import { AIMyAPI } from "../src";
3
2
  import { API } from "./sales_api_impl";
4
3
 
@@ -10,8 +9,8 @@ import { API } from "./sales_api_impl";
10
9
  const aimyapi = await AIMyAPI.createWithAPI({
11
10
  apiObject: api,
12
11
  apiGlobalName: "salesApi", // Should match whatever you declared as your global in your api.
13
- apiDefFilePath: path.join(__dirname, "./sales_api.ts"),
14
- apiDocsPath: path.join(__dirname, "./sales_api.md"),
12
+ apiDefFilePath: "./example/sales_api.ts",
13
+ apiDocsPath: "./example/sales_api.md",
15
14
  debug: false,
16
15
  })
17
16
 
@@ -1,6 +1,5 @@
1
1
  // Note: This example is quite stupid and slow and you'd be better off using Tool calling for something like this, but I'm leaving it for demonstration purposes
2
2
 
3
- import path from "path";
4
3
  import { AIMyAPI } from "../src";
5
4
  import { Menu } from "./ordering_api";
6
5
  import { OrderingAPI } from "./ordering_api_impl";
@@ -17,8 +16,8 @@ import { OrderingAPI } from "./ordering_api_impl";
17
16
  apiGlobals: {
18
17
  Menu: Menu,
19
18
  },
20
- apiDefFilePath: path.join(__dirname, "./ordering_api.ts"),
21
- apiDocsPath: path.join(__dirname, "./ordering_api.md"),
19
+ apiDefFilePath: "./example/ordering_api.ts",
20
+ apiDocsPath: "./example/ordering_api.md",
22
21
  debug: false,
23
22
  };
24
23
  const aimyapi = await AIMyAPI.createWithAPI(options)
@@ -1,5 +1,4 @@
1
1
 
2
- import path from "path";
3
2
  import { AIMyAPI } from "../src";
4
3
  import { Menu } from "./ordering_api";
5
4
  import { OrderingAPI } from "./ordering_api_impl";
@@ -30,8 +29,8 @@ function getInput(query: string): Promise<string> {
30
29
  apiGlobals: {
31
30
  Menu: Menu,
32
31
  },
33
- apiDefFilePath: path.join(__dirname, "./ordering_api.ts"),
34
- apiDocsPath: path.join(__dirname, "./ordering_api.md"),
32
+ apiDefFilePath: "./example/ordering_api.ts",
33
+ apiDocsPath: "./example/ordering_api.md",
35
34
  debug: false,
36
35
  };
37
36
  const aimyapi = await AIMyAPI.createWithAPI(options)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aimyapi",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "AI My API! Typescript sandbox for AI agents to interact with your API in a safe way.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  require('dotenv').config()
2
2
  import fs from 'fs';
3
3
  import { OpenAI } from "openai";
4
- import path from 'path';
5
4
 
6
5
  import createSandbox, { CodeRunResult } from "./sandbox";
7
6
  import { rejectOpenPromises } from './sandbox-wrappers';
@@ -71,7 +70,7 @@ export function createBasePrompt(apiFilePath:string, apiGlobalName: string, docu
71
70
  const documentationText: string = documentationPath ? fs.readFileSync(documentationPath, 'utf8') : '';
72
71
 
73
72
  //console.time("Loading templates");
74
- const createTaskPrompt: string = fs.readFileSync( path.join(__dirname, '../prompts/create-task-prompt.md'), 'utf8').replace("{{DOCUMENTATION}}", documentationText).replace("{{API}}", apiText).replace("{{API_GLOBAL_NAME}}", apiGlobalName || "");
73
+ const createTaskPrompt: string = fs.readFileSync( './prompts/create-task-prompt.md', 'utf8').replace("{{DOCUMENTATION}}", documentationText).replace("{{API}}", apiText).replace("{{API_GLOBAL_NAME}}", apiGlobalName || "");
75
74
  //console.timeEnd("Loading templates")
76
75
 
77
76
  return createTaskPrompt;
package/src/sandbox.ts CHANGED
@@ -18,7 +18,7 @@ function tsCompile(
18
18
  if (!options) {
19
19
  options = {
20
20
  compilerOptions: {
21
- target: ts.ScriptTarget.ES2020,
21
+ target: ts.ScriptTarget.ES2022,
22
22
  module: ts.ModuleKind.None,
23
23
  },
24
24
  };
@@ -250,7 +250,7 @@ export default async function createSandbox(QuickJS: QuickJSWASMModule, requireL
250
250
 
251
251
  if (errorState) {
252
252
  const lines = compiled.split("\n").map((line, index) => `${index + 1}: ${line}`);
253
- logger.error(["An error was reported during execution. Compiled code with line numbers:", lines.join("\n")], "runner");
253
+ logger.error(["An error was reported during execution. Compiled code with line numbers:\n", lines.join("\n")], "runner");
254
254
  }
255
255
 
256
256
  return {