aimyapi 0.1.13 → 0.1.14
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/index.cjs +492 -0
- package/dist/index.d.cts +36 -0
- package/dist/index.d.mts +36 -0
- package/dist/index.d.ts +13 -11
- package/dist/index.js +480 -164
- package/dist/index.mjs +468 -0
- package/example/example1.ts +1 -1
- package/example/example2.ts +1 -1
- package/example/example3.ts +3 -3
- package/example/ordering_api_impl.ts +5 -5
- package/example/sales_api.md +2 -0
- package/example/sales_api.ts +1 -1
- package/example/sales_api_impl.ts +1 -1
- package/package.json +19 -7
- package/prompts/create-task-prompt.md +1 -1
- package/{lib → src}/index.ts +15 -17
- package/tsconfig.json +2 -2
- /package/{lib → src}/sandbox-wrappers.ts +0 -0
- /package/{lib → src}/sandbox.ts +0 -0
- /package/{lib → src}/utils.ts +0 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { OpenAI } from 'openai';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { getQuickJS } from 'quickjs-emscripten';
|
|
6
|
+
import * as ts from 'typescript';
|
|
7
|
+
|
|
8
|
+
var require$1 = (
|
|
9
|
+
true
|
|
10
|
+
? /* @__PURE__ */ createRequire(import.meta.url)
|
|
11
|
+
: require
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
let promiseId = 0;
|
|
15
|
+
let openPromises = {};
|
|
16
|
+
function wrapPromise(vm, promise, beginAsyncProcess, endAsyncProcess) {
|
|
17
|
+
promiseId += 1;
|
|
18
|
+
const vmPromise = vm.newPromise();
|
|
19
|
+
vmPromise._promiseId = promiseId;
|
|
20
|
+
beginAsyncProcess();
|
|
21
|
+
promise.then((...args) => {
|
|
22
|
+
endAsyncProcess();
|
|
23
|
+
const wrappedArgs = args.map((arg) => wrap(vm, arg, void 0, beginAsyncProcess, endAsyncProcess));
|
|
24
|
+
openPromises[vmPromise._promiseId] = null;
|
|
25
|
+
vmPromise.resolve(...wrappedArgs);
|
|
26
|
+
wrappedArgs.forEach((arg) => arg.dispose());
|
|
27
|
+
}).catch((...args) => {
|
|
28
|
+
const wrappedArgs = args.map((arg) => wrap(vm, arg, void 0, beginAsyncProcess, endAsyncProcess));
|
|
29
|
+
vmPromise.reject(...wrappedArgs);
|
|
30
|
+
wrappedArgs.forEach((arg) => arg.dispose());
|
|
31
|
+
openPromises[vmPromise._promiseId] = null;
|
|
32
|
+
});
|
|
33
|
+
vmPromise.settled.then(() => {
|
|
34
|
+
endAsyncProcess();
|
|
35
|
+
vm.runtime.executePendingJobs();
|
|
36
|
+
});
|
|
37
|
+
openPromises[promiseId] = vmPromise;
|
|
38
|
+
return vmPromise.handle;
|
|
39
|
+
}
|
|
40
|
+
function rejectOpenPromises(vm) {
|
|
41
|
+
for (const [id, promise] of Object.entries(openPromises)) {
|
|
42
|
+
if (promise !== null) {
|
|
43
|
+
promise.reject(vm.newError("Shutting down"));
|
|
44
|
+
openPromises[id] = null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
vm.runtime.executePendingJobs();
|
|
48
|
+
}
|
|
49
|
+
function wrap(vm, value, context, beginAsyncProcess, endAsyncProcess) {
|
|
50
|
+
if (value === null) {
|
|
51
|
+
return null;
|
|
52
|
+
} else if (typeof value === "object" && typeof value.then === "function") {
|
|
53
|
+
return wrapPromise(vm, value, beginAsyncProcess, endAsyncProcess);
|
|
54
|
+
} else if (typeof value === "function") {
|
|
55
|
+
return wrapGenericFunction(vm, value, context, beginAsyncProcess, endAsyncProcess);
|
|
56
|
+
} else if (Array.isArray(value)) {
|
|
57
|
+
return wrapArray(vm, value, beginAsyncProcess, endAsyncProcess);
|
|
58
|
+
} else if (value === null) {
|
|
59
|
+
return vm.null;
|
|
60
|
+
} else if (typeof value === "object") {
|
|
61
|
+
return wrapObject(vm, value, beginAsyncProcess, endAsyncProcess);
|
|
62
|
+
} else if (typeof value === "string") {
|
|
63
|
+
return vm.newString(value);
|
|
64
|
+
} else if (typeof value === "number") {
|
|
65
|
+
return vm.newNumber(value);
|
|
66
|
+
} else if (typeof value === "bigint") {
|
|
67
|
+
return vm.newBigInt(value);
|
|
68
|
+
} else if (typeof value === "boolean") {
|
|
69
|
+
return vm.evalCode(value ? "true" : "false").value;
|
|
70
|
+
} else if (typeof value === "undefined") {
|
|
71
|
+
return vm.undefined;
|
|
72
|
+
} else {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function wrapObject(vm, obj, beginAsyncProcess, endAsyncProcess) {
|
|
77
|
+
const vmObject = vm.newObject();
|
|
78
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
79
|
+
let wrappedValue = wrap(vm, value, obj, beginAsyncProcess, endAsyncProcess);
|
|
80
|
+
if (wrappedValue !== null) {
|
|
81
|
+
vm.setProp(vmObject, key, wrappedValue);
|
|
82
|
+
if (wrappedValue != vm.undefined && wrappedValue != vm.null && wrappedValue.dispose) {
|
|
83
|
+
wrappedValue.dispose();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return vmObject;
|
|
88
|
+
}
|
|
89
|
+
function wrapObjectWhitelist(vm, obj, whitelist, beginAsyncProcess, endAsyncProcess) {
|
|
90
|
+
const vmObject = vm.newObject();
|
|
91
|
+
whitelist.forEach((key) => {
|
|
92
|
+
const value = obj[key];
|
|
93
|
+
const wrappedValue = wrap(vm, value, obj, beginAsyncProcess, endAsyncProcess);
|
|
94
|
+
if (wrappedValue !== null) {
|
|
95
|
+
vm.setProp(vmObject, key, wrappedValue);
|
|
96
|
+
if (wrappedValue != vm.undefined && wrappedValue != vm.null && wrappedValue.dispose) {
|
|
97
|
+
wrappedValue.dispose();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
return vmObject;
|
|
102
|
+
}
|
|
103
|
+
function wrapArray(vm, arr, beginAsyncProcess, endAsyncProcess) {
|
|
104
|
+
const vmArray = vm.newArray();
|
|
105
|
+
for (let i = 0; i < arr.length; i++) {
|
|
106
|
+
let wrappedValue = wrap(vm, arr[i], arr, beginAsyncProcess, endAsyncProcess);
|
|
107
|
+
vm.setProp(vmArray, i, wrappedValue);
|
|
108
|
+
if (wrappedValue != vm.undefined && wrappedValue != vm.null) {
|
|
109
|
+
wrappedValue.dispose();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return vmArray;
|
|
113
|
+
}
|
|
114
|
+
function wrapGenericFunction(vm, fn, context = void 0, beginAsyncProcess, endAsyncProcess) {
|
|
115
|
+
const vmFn = vm.newFunction(fn.name, (...args) => {
|
|
116
|
+
const unwrappedArgs = args.map(vm.dump);
|
|
117
|
+
try {
|
|
118
|
+
const result = fn.call(context, ...unwrappedArgs);
|
|
119
|
+
const wrappedResult = wrap(vm, result, void 0, beginAsyncProcess, endAsyncProcess);
|
|
120
|
+
return wrappedResult;
|
|
121
|
+
} catch (e) {
|
|
122
|
+
console.log("Error", e);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
return vmFn;
|
|
126
|
+
}
|
|
127
|
+
function injectTimingFunctions(vm, cbAddedTimer, cbRemovedTimer, maxTimeout = 6e5) {
|
|
128
|
+
const timeoutFunctionHandles = {};
|
|
129
|
+
const _setTimeout = vm.newFunction("setTimeout", (vmFnHandle, timeoutHandle) => {
|
|
130
|
+
const vmFnHandleCopy = vmFnHandle.dup();
|
|
131
|
+
let timeout = vm.dump(timeoutHandle);
|
|
132
|
+
if (timeout > maxTimeout) {
|
|
133
|
+
timeout = maxTimeout;
|
|
134
|
+
}
|
|
135
|
+
cbAddedTimer();
|
|
136
|
+
const timeoutID = setTimeout(() => {
|
|
137
|
+
timeoutFunctionHandles[timeoutID.toString()] = null;
|
|
138
|
+
cbRemovedTimer();
|
|
139
|
+
vm.callFunction(vmFnHandleCopy, vm.undefined);
|
|
140
|
+
vmFnHandleCopy.dispose();
|
|
141
|
+
vm.runtime.executePendingJobs();
|
|
142
|
+
}, timeout);
|
|
143
|
+
timeoutFunctionHandles[timeoutID.toString()] = vmFnHandleCopy;
|
|
144
|
+
return vm.newNumber(timeoutID);
|
|
145
|
+
});
|
|
146
|
+
vm.setProp(vm.global, "setTimeout", _setTimeout);
|
|
147
|
+
_setTimeout.dispose();
|
|
148
|
+
const intervalFunctionHandles = {};
|
|
149
|
+
const _setInterval = vm.newFunction("setInterval", (vmFnHandle, timeoutHandle) => {
|
|
150
|
+
const vmFnHandleCopy = vmFnHandle.dup();
|
|
151
|
+
let timeout = vm.dump(timeoutHandle);
|
|
152
|
+
if (timeout > maxTimeout) {
|
|
153
|
+
timeout = maxTimeout;
|
|
154
|
+
}
|
|
155
|
+
const maxRepetitions = 99;
|
|
156
|
+
let repetitions = 0;
|
|
157
|
+
cbAddedTimer();
|
|
158
|
+
const intervalId = setInterval(() => {
|
|
159
|
+
repetitions += 1;
|
|
160
|
+
intervalFunctionHandles[intervalId.toString()] = null;
|
|
161
|
+
vm.callFunction(vmFnHandleCopy, vm.undefined);
|
|
162
|
+
vm.runtime.executePendingJobs();
|
|
163
|
+
if (repetitions > maxRepetitions) {
|
|
164
|
+
console.log("Sandbox interval exceeded max repetitions");
|
|
165
|
+
clearInterval(intervalId);
|
|
166
|
+
cbRemovedTimer();
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
}, timeout);
|
|
170
|
+
intervalFunctionHandles[intervalId.toString()] = vmFnHandleCopy;
|
|
171
|
+
return vm.newNumber(intervalId);
|
|
172
|
+
});
|
|
173
|
+
vm.setProp(vm.global, "setInterval", _setInterval);
|
|
174
|
+
_setInterval.dispose();
|
|
175
|
+
const _clearTimeout = vm.newFunction("clearTimeout", (timeoutIdHandle) => {
|
|
176
|
+
const timeoutId = vm.dump(timeoutIdHandle);
|
|
177
|
+
const timeoutHandle = timeoutFunctionHandles[timeoutId.toString()];
|
|
178
|
+
if (timeoutHandle) {
|
|
179
|
+
cbRemovedTimer();
|
|
180
|
+
timeoutHandle.dispose();
|
|
181
|
+
clearTimeout(timeoutId);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
vm.setProp(vm.global, "clearTimeout", _clearTimeout);
|
|
185
|
+
_clearTimeout.dispose();
|
|
186
|
+
const _clearInterval = vm.newFunction("clearInterval", (intervalIdHandle) => {
|
|
187
|
+
const intervalId = vm.dump(intervalIdHandle);
|
|
188
|
+
const intervalHandle = intervalFunctionHandles[intervalId.toString()];
|
|
189
|
+
if (intervalId) {
|
|
190
|
+
cbRemovedTimer();
|
|
191
|
+
intervalHandle.dispose();
|
|
192
|
+
clearInterval(intervalId);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
vm.setProp(vm.global, "clearInterval", _clearInterval);
|
|
196
|
+
_clearInterval.dispose();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function tsCompile(source, options = null) {
|
|
200
|
+
if (null === options) {
|
|
201
|
+
options = {
|
|
202
|
+
compilerOptions: {
|
|
203
|
+
target: ts.ScriptTarget.ES2020,
|
|
204
|
+
module: ts.ModuleKind.None
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
return ts.transpileModule(source, options).outputText;
|
|
209
|
+
}
|
|
210
|
+
async function createSandbox(QuickJS, requireLookup = {}, globals = {}, debug = false) {
|
|
211
|
+
const vm = QuickJS.newContext();
|
|
212
|
+
let errorState = false;
|
|
213
|
+
let lastError = "";
|
|
214
|
+
let asyncProcessesRunning = 0;
|
|
215
|
+
let beginAsyncProcess = () => {
|
|
216
|
+
asyncProcessesRunning++;
|
|
217
|
+
};
|
|
218
|
+
let endAsyncProcess = () => {
|
|
219
|
+
if (asyncProcessesRunning > 0) {
|
|
220
|
+
asyncProcessesRunning--;
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
let isAsyncProcessRunning = () => {
|
|
224
|
+
return asyncProcessesRunning > 0;
|
|
225
|
+
};
|
|
226
|
+
const logHandle = vm.newFunction("log", (...args) => {
|
|
227
|
+
const nativeArgs = args.map(vm.dump);
|
|
228
|
+
console.log("QuickJS Log:", ...nativeArgs);
|
|
229
|
+
});
|
|
230
|
+
const consoleHandle = vm.newObject();
|
|
231
|
+
const exportsHandle = vm.newObject();
|
|
232
|
+
const errorHandle = vm.newFunction("error", (...args) => {
|
|
233
|
+
try {
|
|
234
|
+
const nativeArgs = args.map(vm.dump);
|
|
235
|
+
console.log("QuickJS console.error:", ...nativeArgs);
|
|
236
|
+
lastError = JSON.stringify(nativeArgs);
|
|
237
|
+
errorState = true;
|
|
238
|
+
} catch (e) {
|
|
239
|
+
console.log("Error in error:", e);
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
const globalNames = Object.getOwnPropertyNames(globals);
|
|
243
|
+
for (let i = 0; i < globalNames.length; i++) {
|
|
244
|
+
const globalOption = globals[globalNames[i]];
|
|
245
|
+
const globalObj = globalOption.whitelist ? wrapObjectWhitelist(
|
|
246
|
+
vm,
|
|
247
|
+
globalOption.value,
|
|
248
|
+
globalOption.whitelist,
|
|
249
|
+
beginAsyncProcess,
|
|
250
|
+
endAsyncProcess
|
|
251
|
+
) : wrap(vm, globalOption.value, globalOption, beginAsyncProcess, endAsyncProcess);
|
|
252
|
+
vm.setProp(vm.global, globalNames[i], globalObj);
|
|
253
|
+
globalObj.dispose();
|
|
254
|
+
}
|
|
255
|
+
injectTimingFunctions(vm, beginAsyncProcess, endAsyncProcess);
|
|
256
|
+
const requireHandle = vm.newFunction("require", (...args) => {
|
|
257
|
+
const nativeArgs = args.map(vm.dump);
|
|
258
|
+
if (requireLookup[nativeArgs[0]]) {
|
|
259
|
+
const returnObj = wrapObject(vm, requireLookup[nativeArgs[0]], beginAsyncProcess, endAsyncProcess);
|
|
260
|
+
return returnObj;
|
|
261
|
+
} else {
|
|
262
|
+
return vm.undefined;
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
vm.setProp(consoleHandle, "log", logHandle);
|
|
266
|
+
vm.setProp(consoleHandle, "error", errorHandle);
|
|
267
|
+
vm.setProp(vm.global, "require", requireHandle);
|
|
268
|
+
vm.setProp(vm.global, "console", consoleHandle);
|
|
269
|
+
vm.setProp(vm.global, "exports", exportsHandle);
|
|
270
|
+
consoleHandle.dispose();
|
|
271
|
+
logHandle.dispose();
|
|
272
|
+
exportsHandle.dispose();
|
|
273
|
+
errorHandle.dispose();
|
|
274
|
+
requireHandle.dispose();
|
|
275
|
+
return {
|
|
276
|
+
vm,
|
|
277
|
+
getLastError: () => {
|
|
278
|
+
return lastError;
|
|
279
|
+
},
|
|
280
|
+
isAsyncProcessRunning,
|
|
281
|
+
runTask: (task) => {
|
|
282
|
+
try {
|
|
283
|
+
const compiled = tsCompile(task);
|
|
284
|
+
if (!compiled || compiled.length === 0) {
|
|
285
|
+
console.error("Could not compile", task);
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
errorState = false;
|
|
289
|
+
const result = vm.evalCode(compiled);
|
|
290
|
+
if (result.error) {
|
|
291
|
+
const lines = compiled.split("\n");
|
|
292
|
+
for (let i = 0; i < lines.length; i++) {
|
|
293
|
+
console.log(`${i + 1}: ${lines[i]}`);
|
|
294
|
+
}
|
|
295
|
+
console.log("Execution failed:", vm.dump(result.error));
|
|
296
|
+
result.error.dispose();
|
|
297
|
+
return false;
|
|
298
|
+
} else {
|
|
299
|
+
result.value.dispose();
|
|
300
|
+
if (errorState) {
|
|
301
|
+
const lines = compiled.split("\n");
|
|
302
|
+
for (let i = 0; i < lines.length; i++) {
|
|
303
|
+
console.log(`${i + 1}: ${lines[i]}`);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return !errorState;
|
|
307
|
+
}
|
|
308
|
+
} catch (e) {
|
|
309
|
+
console.log(e);
|
|
310
|
+
}
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
require$1("dotenv").config();
|
|
317
|
+
const openai = new OpenAI({
|
|
318
|
+
apiKey: process.env.OPENAI_API_KEY
|
|
319
|
+
// This is also the default, can be omitted
|
|
320
|
+
});
|
|
321
|
+
function createBasePrompt(apiFilePath, documentationPath) {
|
|
322
|
+
const apiText = fs.readFileSync(apiFilePath, "utf8");
|
|
323
|
+
const documentationText = documentationPath ? fs.readFileSync(documentationPath, "utf8") : "";
|
|
324
|
+
const createTaskPrompt = fs.readFileSync(path.join(__dirname, "../prompts/create-task-prompt.md"), "utf8").replace("{{DOCUMENTATION}}", documentationText).replace("{{API}}", apiText);
|
|
325
|
+
return createTaskPrompt;
|
|
326
|
+
}
|
|
327
|
+
const generateCode = async function(queryText, userChatHistory, createTaskPrompt, apiPath, debug = false, model = "gpt-4o") {
|
|
328
|
+
if (!queryText)
|
|
329
|
+
return { code: "", loggableCode: "" };
|
|
330
|
+
let generatedCode = "";
|
|
331
|
+
const prompt = createTaskPrompt.replace("{{QUERY_TEXT}}", queryText);
|
|
332
|
+
const messages = [
|
|
333
|
+
{
|
|
334
|
+
role: "system",
|
|
335
|
+
content: prompt
|
|
336
|
+
},
|
|
337
|
+
...userChatHistory,
|
|
338
|
+
{
|
|
339
|
+
role: "user",
|
|
340
|
+
content: `${queryText}.
|
|
341
|
+
Respond only with code enclosed in \`\`\`.`,
|
|
342
|
+
name: "user"
|
|
343
|
+
}
|
|
344
|
+
];
|
|
345
|
+
try {
|
|
346
|
+
const results = await openai.chat.completions.create(
|
|
347
|
+
{
|
|
348
|
+
model,
|
|
349
|
+
messages,
|
|
350
|
+
temperature: 0,
|
|
351
|
+
max_tokens: 700
|
|
352
|
+
}
|
|
353
|
+
);
|
|
354
|
+
if (debug)
|
|
355
|
+
console.log(results.usage);
|
|
356
|
+
const response = results.choices[0].message.content;
|
|
357
|
+
if (debug)
|
|
358
|
+
console.log(response);
|
|
359
|
+
const codeStart = response.indexOf("```");
|
|
360
|
+
const codeEnd = response.lastIndexOf("```");
|
|
361
|
+
if (codeStart === -1 || codeEnd === -1 || codeStart === codeEnd) {
|
|
362
|
+
return { code: "", loggableCode: "", rawResponse: response };
|
|
363
|
+
}
|
|
364
|
+
generatedCode = response.substring(codeStart + 3, codeEnd).replace("typescript", "");
|
|
365
|
+
const codeHeader = `import * as ApiDefs from '${apiPath}'
|
|
366
|
+
(async() {
|
|
367
|
+
try {`;
|
|
368
|
+
const codeFooter = `
|
|
369
|
+
} catch(err) {
|
|
370
|
+
console.error(err);
|
|
371
|
+
}
|
|
372
|
+
})();`;
|
|
373
|
+
return { code: codeHeader + generatedCode + codeFooter, loggableCode: generatedCode };
|
|
374
|
+
} catch (err) {
|
|
375
|
+
console.error(err);
|
|
376
|
+
return { code: "", loggableCode: "" };
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
let QuickJS = null;
|
|
380
|
+
async function createWithAPI(options) {
|
|
381
|
+
options = {
|
|
382
|
+
apiGlobalName: "api",
|
|
383
|
+
apiGlobals: {},
|
|
384
|
+
apiWhitelist: Object.getOwnPropertyNames(Object.getPrototypeOf(options.apiObject)).filter((f) => f !== "constructor" && !f.startsWith("_")),
|
|
385
|
+
debug: false,
|
|
386
|
+
model: "gpt-4o",
|
|
387
|
+
...options
|
|
388
|
+
};
|
|
389
|
+
if (!options.apiObject || !options.apiDefFilePath) {
|
|
390
|
+
throw new Error("apiObject and apiFilePath are required");
|
|
391
|
+
}
|
|
392
|
+
const { apiObject, apiWhitelist, apiGlobalName, apiExports, apiDefFilePath, apiDocsPath, debug, model } = options;
|
|
393
|
+
const createTaskPrompt = createBasePrompt(apiDefFilePath, apiDocsPath);
|
|
394
|
+
return {
|
|
395
|
+
options,
|
|
396
|
+
generateCode: async function(queryText, userChatHistory, currentContext = void 0) {
|
|
397
|
+
if (!queryText)
|
|
398
|
+
return { code: "", loggableCode: "" };
|
|
399
|
+
return await generateCode(queryText, userChatHistory, createTaskPrompt.replace("{{CONTEXT}}", currentContext ? "```" + JSON.stringify(currentContext, null, 2) + "```" : ""), apiDefFilePath, debug, model);
|
|
400
|
+
},
|
|
401
|
+
runCode: async function(generatedCode) {
|
|
402
|
+
if (!QuickJS) {
|
|
403
|
+
if (debug)
|
|
404
|
+
console.log("Creating quick.js");
|
|
405
|
+
QuickJS = await getQuickJS();
|
|
406
|
+
}
|
|
407
|
+
let apiGlobals = {};
|
|
408
|
+
for (const key in options.apiGlobals) {
|
|
409
|
+
apiGlobals[key] = {
|
|
410
|
+
value: options.apiGlobals[key]
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
const { vm, runTask, isAsyncProcessRunning } = await createSandbox(QuickJS, {
|
|
414
|
+
[apiDefFilePath.replaceAll("\\", "")]: apiExports
|
|
415
|
+
}, {
|
|
416
|
+
...apiGlobals,
|
|
417
|
+
[apiGlobalName]: {
|
|
418
|
+
value: apiObject,
|
|
419
|
+
whitelist: apiWhitelist
|
|
420
|
+
}
|
|
421
|
+
}, options.debug);
|
|
422
|
+
try {
|
|
423
|
+
if (debug) {
|
|
424
|
+
console.log("Running Code:\n", generatedCode);
|
|
425
|
+
}
|
|
426
|
+
const res = await runTask(generatedCode);
|
|
427
|
+
if (!res) {
|
|
428
|
+
console.error("Task unsuccessful");
|
|
429
|
+
}
|
|
430
|
+
while (isAsyncProcessRunning()) {
|
|
431
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
432
|
+
}
|
|
433
|
+
if (debug) {
|
|
434
|
+
console.log("Completed");
|
|
435
|
+
}
|
|
436
|
+
} catch (err) {
|
|
437
|
+
if (debug) {
|
|
438
|
+
console.error("Error running task:", err);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
442
|
+
rejectOpenPromises(vm);
|
|
443
|
+
vm.dispose();
|
|
444
|
+
},
|
|
445
|
+
processRequest: async function(userQuery, currentContext = void 0) {
|
|
446
|
+
if (debug) {
|
|
447
|
+
console.log("Query:", userQuery);
|
|
448
|
+
}
|
|
449
|
+
console.time("Generate Task");
|
|
450
|
+
const generatedCode = await generateCode(userQuery, [], createTaskPrompt.replace("{{CONTEXT}}", currentContext ? "```" + JSON.stringify(currentContext, null, 2) + "```" : ""), apiDefFilePath);
|
|
451
|
+
console.timeEnd("Generate Task");
|
|
452
|
+
console.time("Run Code");
|
|
453
|
+
await this.runCode(generatedCode.code);
|
|
454
|
+
console.timeEnd("Run Code");
|
|
455
|
+
return generatedCode;
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
const aimyapi = {
|
|
460
|
+
// Standard way of using the library
|
|
461
|
+
createWithAPI,
|
|
462
|
+
// functions for customized usage
|
|
463
|
+
createBasePrompt,
|
|
464
|
+
generateCode,
|
|
465
|
+
createSandbox
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
export { aimyapi, createBasePrompt, generateCode };
|
package/example/example1.ts
CHANGED
package/example/example2.ts
CHANGED
package/example/example3.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
|
|
2
|
-
import AIMyAPI from "../lib";
|
|
3
|
-
import { OrderingAPI } from "./ordering_api_impl";
|
|
4
|
-
import * as APIExports from "./ordering_api";
|
|
5
2
|
import path from "path";
|
|
3
|
+
import AIMyAPI from "../src";
|
|
4
|
+
import * as APIExports from "./ordering_api";
|
|
5
|
+
import { OrderingAPI } from "./ordering_api_impl";
|
|
6
6
|
|
|
7
7
|
import readline from 'readline';
|
|
8
8
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { ChatCompletionRequestMessage } from "openai";
|
|
2
|
-
import { delay } from "../
|
|
2
|
+
import { delay } from "../src/utils";
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
Customization,
|
|
5
|
+
Menu,
|
|
6
|
+
MenuItemBase,
|
|
7
|
+
OrderingAPIInterface,
|
|
8
8
|
} from "./ordering_api";
|
|
9
9
|
|
|
10
10
|
// TODO: make functions to print out an item, and print out the current order with the price totals.
|
package/example/sales_api.md
CHANGED
package/example/sales_api.ts
CHANGED
|
@@ -13,7 +13,7 @@ export interface APIInterface {
|
|
|
13
13
|
sendEmail(to: string, subject: string, body: string): Promise<void>;
|
|
14
14
|
|
|
15
15
|
getSalesData(): SalesData[];
|
|
16
|
-
// Returns an array of sales data.
|
|
16
|
+
// Returns an array of sales data.
|
|
17
17
|
|
|
18
18
|
print(text: string): void;
|
|
19
19
|
// Prints the specified text to the user.
|
package/package.json
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aimyapi",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
4
4
|
"description": "AI My API! Use LLMs to create small programs that safely* run and make calls against your API. Similar to ChatGPT's Code Interpreter.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"exports": {
|
|
9
|
+
"require": {
|
|
10
|
+
"types": "./dist/index.d.cts",
|
|
11
|
+
"default": "./dist/index.cjs"
|
|
12
|
+
},
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.mts",
|
|
15
|
+
"default": "./dist/index.mjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
7
18
|
"directories": {
|
|
8
19
|
"lib": "lib"
|
|
9
20
|
},
|
|
10
21
|
"scripts": {
|
|
11
|
-
"example1": "
|
|
12
|
-
"example2": "
|
|
13
|
-
"example3": "
|
|
14
|
-
"build": "
|
|
22
|
+
"example1": "tsx ./example/example1.ts",
|
|
23
|
+
"example2": "tsx ./example/example2.ts",
|
|
24
|
+
"example3": "tsx ./example/example3.ts",
|
|
25
|
+
"build": "pkgroll"
|
|
15
26
|
},
|
|
16
27
|
"repository": {
|
|
17
28
|
"type": "git",
|
|
@@ -24,7 +35,7 @@
|
|
|
24
35
|
],
|
|
25
36
|
"dependencies": {
|
|
26
37
|
"dotenv": "^16.0.3",
|
|
27
|
-
"openai": "^
|
|
38
|
+
"openai": "^4.61.0",
|
|
28
39
|
"quickjs-emscripten": "^0.22.0",
|
|
29
40
|
"typescript": "^5.0.3"
|
|
30
41
|
},
|
|
@@ -36,6 +47,7 @@
|
|
|
36
47
|
"homepage": "https://github.com/WhiteNoise/aimyapi#readme",
|
|
37
48
|
"devDependencies": {
|
|
38
49
|
"@types/node": "^18.16.2",
|
|
39
|
-
"
|
|
50
|
+
"pkgroll": "^2.5.0",
|
|
51
|
+
"tsx": "^4.19.1"
|
|
40
52
|
}
|
|
41
53
|
}
|