aimyapi 1.0.7 → 1.0.9
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 +38 -4
- package/dist/index.d.cts +2 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +38 -4
- package/dist/index.mjs +38 -4
- package/package.json +1 -1
- package/prompts/create-task-prompt.md +14 -7
- package/src/index.ts +45 -6
- package/src/sandbox-wrappers.ts +2 -0
package/dist/index.cjs
CHANGED
|
@@ -80,6 +80,8 @@ function wrap(vm, value, context, beginAsyncProcess, endAsyncProcess) {
|
|
|
80
80
|
return wrapArray(vm, value, beginAsyncProcess, endAsyncProcess);
|
|
81
81
|
} else if (value === null) {
|
|
82
82
|
return vm.null;
|
|
83
|
+
} else if (value instanceof Date) {
|
|
84
|
+
return vm.evalCode(`new Date(${value.getTime()})`).value;
|
|
83
85
|
} else if (typeof value === "object") {
|
|
84
86
|
return wrapObject(vm, value, beginAsyncProcess, endAsyncProcess);
|
|
85
87
|
} else if (typeof value === "string") {
|
|
@@ -438,11 +440,12 @@ const checkCodeSchema = zod.z.object({
|
|
|
438
440
|
const submitCodeSchema = zod.z.object({
|
|
439
441
|
code: zod.z.string()
|
|
440
442
|
});
|
|
441
|
-
const generateCode = async function(instance, queryText, userChatHistory, createTaskPrompt, debug = false, hideLogsFromAgent = false, model = "gpt-5-mini", additionalModelOptions) {
|
|
443
|
+
const generateCode = async function(instance, queryText, userChatHistory, createTaskPrompt, debug = false, hideLogsFromAgent = false, model = "gpt-5-mini", additionalModelOptions, attempt = 0) {
|
|
442
444
|
let finalSubmittedCode = "";
|
|
443
445
|
if (debug) {
|
|
444
446
|
console.log("Generating code for query:", queryText);
|
|
445
447
|
}
|
|
448
|
+
const newHistoryItems = [];
|
|
446
449
|
const codingTools = [
|
|
447
450
|
{
|
|
448
451
|
type: "function",
|
|
@@ -487,6 +490,14 @@ const generateCode = async function(instance, queryText, userChatHistory, create
|
|
|
487
490
|
name: "submitCode",
|
|
488
491
|
// @ts-ignore
|
|
489
492
|
function: async ({ code }) => {
|
|
493
|
+
const checkRes = await instance.checkCode(code);
|
|
494
|
+
if (!checkRes.success) {
|
|
495
|
+
return {
|
|
496
|
+
success: false,
|
|
497
|
+
reason: "Code check failed",
|
|
498
|
+
logs: checkRes.logs
|
|
499
|
+
};
|
|
500
|
+
}
|
|
490
501
|
finalSubmittedCode = code;
|
|
491
502
|
return {
|
|
492
503
|
success: true
|
|
@@ -509,7 +520,7 @@ const generateCode = async function(instance, queryText, userChatHistory, create
|
|
|
509
520
|
...userChatHistory,
|
|
510
521
|
{
|
|
511
522
|
role: "user",
|
|
512
|
-
content: createTaskPrompt.replace("{{USER_QUERY}}", queryText)
|
|
523
|
+
content: attempt == 0 ? createTaskPrompt.replace("{{USER_QUERY}}", queryText) : queryText
|
|
513
524
|
}
|
|
514
525
|
],
|
|
515
526
|
tools: codingTools,
|
|
@@ -518,6 +529,7 @@ const generateCode = async function(instance, queryText, userChatHistory, create
|
|
|
518
529
|
if (debug) {
|
|
519
530
|
console.log("Message:", JSON.stringify(message, null, 2));
|
|
520
531
|
}
|
|
532
|
+
newHistoryItems.push(message);
|
|
521
533
|
}).on("functionToolCall", (functionCall) => {
|
|
522
534
|
if (debug) {
|
|
523
535
|
console.log("Function Call:", JSON.stringify(functionCall, null, 2));
|
|
@@ -529,15 +541,37 @@ const generateCode = async function(instance, queryText, userChatHistory, create
|
|
|
529
541
|
});
|
|
530
542
|
const finalContent = await runner.finalContent();
|
|
531
543
|
if (debug) {
|
|
532
|
-
console.log("
|
|
544
|
+
console.log("Generate code final response:", finalContent);
|
|
545
|
+
}
|
|
546
|
+
if (!finalSubmittedCode) {
|
|
547
|
+
console.error("No code was submitted by the agent.");
|
|
548
|
+
if (attempt < 3) {
|
|
549
|
+
return generateCode(instance, `You must call submitCode with valid code before proceeding. Attempt ${attempt + 1} of 3.`, [
|
|
550
|
+
...userChatHistory,
|
|
551
|
+
{
|
|
552
|
+
role: "user",
|
|
553
|
+
content: attempt == 0 ? createTaskPrompt.replace("{{USER_QUERY}}", queryText) : queryText
|
|
554
|
+
},
|
|
555
|
+
...newHistoryItems
|
|
556
|
+
], createTaskPrompt, debug, hideLogsFromAgent, model, additionalModelOptions, attempt + 1);
|
|
557
|
+
} else {
|
|
558
|
+
return {
|
|
559
|
+
success: false,
|
|
560
|
+
comments: "No code was submitted by the agent after multiple attempts. Final comment: " + finalContent
|
|
561
|
+
};
|
|
562
|
+
}
|
|
533
563
|
}
|
|
534
564
|
return {
|
|
565
|
+
success: true,
|
|
535
566
|
code: finalSubmittedCode,
|
|
536
567
|
comments: finalContent || void 0
|
|
537
568
|
};
|
|
538
569
|
} catch (err) {
|
|
539
570
|
console.error(err);
|
|
540
|
-
return
|
|
571
|
+
return {
|
|
572
|
+
success: false,
|
|
573
|
+
comments: `Error generating code: ${err}`
|
|
574
|
+
};
|
|
541
575
|
}
|
|
542
576
|
};
|
|
543
577
|
async function createWithAPI(options) {
|
package/dist/index.d.cts
CHANGED
|
@@ -40,12 +40,13 @@ interface AIMyAPIInstance {
|
|
|
40
40
|
processRequest: (userQuery: string, context?: object) => Promise<GenerateCodeResult | null>;
|
|
41
41
|
}
|
|
42
42
|
interface GenerateCodeResult {
|
|
43
|
+
success: boolean;
|
|
43
44
|
code?: string;
|
|
44
45
|
comments?: string;
|
|
45
46
|
}
|
|
46
47
|
declare function zodParseJSON<T>(schema: ZodSchema<T>): (input: string) => T;
|
|
47
48
|
declare function createBasePrompt(apiFilePath: string, apiGlobalName: string, documentationPath?: string): string;
|
|
48
|
-
declare const generateCode: (instance: AIMyAPIInstance, queryText: string, userChatHistory: ChatCompletionMessageParam[], createTaskPrompt: string, debug?: boolean, hideLogsFromAgent?: boolean, model?: string, additionalModelOptions?: object) => Promise<GenerateCodeResult | null>;
|
|
49
|
+
declare const generateCode: (instance: AIMyAPIInstance, queryText: string, userChatHistory: ChatCompletionMessageParam[], createTaskPrompt: string, debug?: boolean, hideLogsFromAgent?: boolean, model?: string, additionalModelOptions?: object, attempt?: number) => Promise<GenerateCodeResult | null>;
|
|
49
50
|
interface AIMyAPIModuleExports {
|
|
50
51
|
createWithAPI: (options: AIMyAPIOptions) => Promise<AIMyAPIInstance>;
|
|
51
52
|
createBasePrompt: (apiFilePath: string, documentationPath: string) => string;
|
package/dist/index.d.mts
CHANGED
|
@@ -40,12 +40,13 @@ interface AIMyAPIInstance {
|
|
|
40
40
|
processRequest: (userQuery: string, context?: object) => Promise<GenerateCodeResult | null>;
|
|
41
41
|
}
|
|
42
42
|
interface GenerateCodeResult {
|
|
43
|
+
success: boolean;
|
|
43
44
|
code?: string;
|
|
44
45
|
comments?: string;
|
|
45
46
|
}
|
|
46
47
|
declare function zodParseJSON<T>(schema: ZodSchema<T>): (input: string) => T;
|
|
47
48
|
declare function createBasePrompt(apiFilePath: string, apiGlobalName: string, documentationPath?: string): string;
|
|
48
|
-
declare const generateCode: (instance: AIMyAPIInstance, queryText: string, userChatHistory: ChatCompletionMessageParam[], createTaskPrompt: string, debug?: boolean, hideLogsFromAgent?: boolean, model?: string, additionalModelOptions?: object) => Promise<GenerateCodeResult | null>;
|
|
49
|
+
declare const generateCode: (instance: AIMyAPIInstance, queryText: string, userChatHistory: ChatCompletionMessageParam[], createTaskPrompt: string, debug?: boolean, hideLogsFromAgent?: boolean, model?: string, additionalModelOptions?: object, attempt?: number) => Promise<GenerateCodeResult | null>;
|
|
49
50
|
interface AIMyAPIModuleExports {
|
|
50
51
|
createWithAPI: (options: AIMyAPIOptions) => Promise<AIMyAPIInstance>;
|
|
51
52
|
createBasePrompt: (apiFilePath: string, documentationPath: string) => string;
|
package/dist/index.d.ts
CHANGED
|
@@ -40,12 +40,13 @@ interface AIMyAPIInstance {
|
|
|
40
40
|
processRequest: (userQuery: string, context?: object) => Promise<GenerateCodeResult | null>;
|
|
41
41
|
}
|
|
42
42
|
interface GenerateCodeResult {
|
|
43
|
+
success: boolean;
|
|
43
44
|
code?: string;
|
|
44
45
|
comments?: string;
|
|
45
46
|
}
|
|
46
47
|
declare function zodParseJSON<T>(schema: ZodSchema<T>): (input: string) => T;
|
|
47
48
|
declare function createBasePrompt(apiFilePath: string, apiGlobalName: string, documentationPath?: string): string;
|
|
48
|
-
declare const generateCode: (instance: AIMyAPIInstance, queryText: string, userChatHistory: ChatCompletionMessageParam[], createTaskPrompt: string, debug?: boolean, hideLogsFromAgent?: boolean, model?: string, additionalModelOptions?: object) => Promise<GenerateCodeResult | null>;
|
|
49
|
+
declare const generateCode: (instance: AIMyAPIInstance, queryText: string, userChatHistory: ChatCompletionMessageParam[], createTaskPrompt: string, debug?: boolean, hideLogsFromAgent?: boolean, model?: string, additionalModelOptions?: object, attempt?: number) => Promise<GenerateCodeResult | null>;
|
|
49
50
|
interface AIMyAPIModuleExports {
|
|
50
51
|
createWithAPI: (options: AIMyAPIOptions) => Promise<AIMyAPIInstance>;
|
|
51
52
|
createBasePrompt: (apiFilePath: string, documentationPath: string) => string;
|
package/dist/index.js
CHANGED
|
@@ -80,6 +80,8 @@ function wrap(vm, value, context, beginAsyncProcess, endAsyncProcess) {
|
|
|
80
80
|
return wrapArray(vm, value, beginAsyncProcess, endAsyncProcess);
|
|
81
81
|
} else if (value === null) {
|
|
82
82
|
return vm.null;
|
|
83
|
+
} else if (value instanceof Date) {
|
|
84
|
+
return vm.evalCode(`new Date(${value.getTime()})`).value;
|
|
83
85
|
} else if (typeof value === "object") {
|
|
84
86
|
return wrapObject(vm, value, beginAsyncProcess, endAsyncProcess);
|
|
85
87
|
} else if (typeof value === "string") {
|
|
@@ -438,11 +440,12 @@ const checkCodeSchema = zod.z.object({
|
|
|
438
440
|
const submitCodeSchema = zod.z.object({
|
|
439
441
|
code: zod.z.string()
|
|
440
442
|
});
|
|
441
|
-
const generateCode = async function(instance, queryText, userChatHistory, createTaskPrompt, debug = false, hideLogsFromAgent = false, model = "gpt-5-mini", additionalModelOptions) {
|
|
443
|
+
const generateCode = async function(instance, queryText, userChatHistory, createTaskPrompt, debug = false, hideLogsFromAgent = false, model = "gpt-5-mini", additionalModelOptions, attempt = 0) {
|
|
442
444
|
let finalSubmittedCode = "";
|
|
443
445
|
if (debug) {
|
|
444
446
|
console.log("Generating code for query:", queryText);
|
|
445
447
|
}
|
|
448
|
+
const newHistoryItems = [];
|
|
446
449
|
const codingTools = [
|
|
447
450
|
{
|
|
448
451
|
type: "function",
|
|
@@ -487,6 +490,14 @@ const generateCode = async function(instance, queryText, userChatHistory, create
|
|
|
487
490
|
name: "submitCode",
|
|
488
491
|
// @ts-ignore
|
|
489
492
|
function: async ({ code }) => {
|
|
493
|
+
const checkRes = await instance.checkCode(code);
|
|
494
|
+
if (!checkRes.success) {
|
|
495
|
+
return {
|
|
496
|
+
success: false,
|
|
497
|
+
reason: "Code check failed",
|
|
498
|
+
logs: checkRes.logs
|
|
499
|
+
};
|
|
500
|
+
}
|
|
490
501
|
finalSubmittedCode = code;
|
|
491
502
|
return {
|
|
492
503
|
success: true
|
|
@@ -509,7 +520,7 @@ const generateCode = async function(instance, queryText, userChatHistory, create
|
|
|
509
520
|
...userChatHistory,
|
|
510
521
|
{
|
|
511
522
|
role: "user",
|
|
512
|
-
content: createTaskPrompt.replace("{{USER_QUERY}}", queryText)
|
|
523
|
+
content: attempt == 0 ? createTaskPrompt.replace("{{USER_QUERY}}", queryText) : queryText
|
|
513
524
|
}
|
|
514
525
|
],
|
|
515
526
|
tools: codingTools,
|
|
@@ -518,6 +529,7 @@ const generateCode = async function(instance, queryText, userChatHistory, create
|
|
|
518
529
|
if (debug) {
|
|
519
530
|
console.log("Message:", JSON.stringify(message, null, 2));
|
|
520
531
|
}
|
|
532
|
+
newHistoryItems.push(message);
|
|
521
533
|
}).on("functionToolCall", (functionCall) => {
|
|
522
534
|
if (debug) {
|
|
523
535
|
console.log("Function Call:", JSON.stringify(functionCall, null, 2));
|
|
@@ -529,15 +541,37 @@ const generateCode = async function(instance, queryText, userChatHistory, create
|
|
|
529
541
|
});
|
|
530
542
|
const finalContent = await runner.finalContent();
|
|
531
543
|
if (debug) {
|
|
532
|
-
console.log("
|
|
544
|
+
console.log("Generate code final response:", finalContent);
|
|
545
|
+
}
|
|
546
|
+
if (!finalSubmittedCode) {
|
|
547
|
+
console.error("No code was submitted by the agent.");
|
|
548
|
+
if (attempt < 3) {
|
|
549
|
+
return generateCode(instance, `You must call submitCode with valid code before proceeding. Attempt ${attempt + 1} of 3.`, [
|
|
550
|
+
...userChatHistory,
|
|
551
|
+
{
|
|
552
|
+
role: "user",
|
|
553
|
+
content: attempt == 0 ? createTaskPrompt.replace("{{USER_QUERY}}", queryText) : queryText
|
|
554
|
+
},
|
|
555
|
+
...newHistoryItems
|
|
556
|
+
], createTaskPrompt, debug, hideLogsFromAgent, model, additionalModelOptions, attempt + 1);
|
|
557
|
+
} else {
|
|
558
|
+
return {
|
|
559
|
+
success: false,
|
|
560
|
+
comments: "No code was submitted by the agent after multiple attempts. Final comment: " + finalContent
|
|
561
|
+
};
|
|
562
|
+
}
|
|
533
563
|
}
|
|
534
564
|
return {
|
|
565
|
+
success: true,
|
|
535
566
|
code: finalSubmittedCode,
|
|
536
567
|
comments: finalContent || void 0
|
|
537
568
|
};
|
|
538
569
|
} catch (err) {
|
|
539
570
|
console.error(err);
|
|
540
|
-
return
|
|
571
|
+
return {
|
|
572
|
+
success: false,
|
|
573
|
+
comments: `Error generating code: ${err}`
|
|
574
|
+
};
|
|
541
575
|
}
|
|
542
576
|
};
|
|
543
577
|
async function createWithAPI(options) {
|
package/dist/index.mjs
CHANGED
|
@@ -58,6 +58,8 @@ function wrap(vm, value, context, beginAsyncProcess, endAsyncProcess) {
|
|
|
58
58
|
return wrapArray(vm, value, beginAsyncProcess, endAsyncProcess);
|
|
59
59
|
} else if (value === null) {
|
|
60
60
|
return vm.null;
|
|
61
|
+
} else if (value instanceof Date) {
|
|
62
|
+
return vm.evalCode(`new Date(${value.getTime()})`).value;
|
|
61
63
|
} else if (typeof value === "object") {
|
|
62
64
|
return wrapObject(vm, value, beginAsyncProcess, endAsyncProcess);
|
|
63
65
|
} else if (typeof value === "string") {
|
|
@@ -416,11 +418,12 @@ const checkCodeSchema = z.object({
|
|
|
416
418
|
const submitCodeSchema = z.object({
|
|
417
419
|
code: z.string()
|
|
418
420
|
});
|
|
419
|
-
const generateCode = async function(instance, queryText, userChatHistory, createTaskPrompt, debug = false, hideLogsFromAgent = false, model = "gpt-5-mini", additionalModelOptions) {
|
|
421
|
+
const generateCode = async function(instance, queryText, userChatHistory, createTaskPrompt, debug = false, hideLogsFromAgent = false, model = "gpt-5-mini", additionalModelOptions, attempt = 0) {
|
|
420
422
|
let finalSubmittedCode = "";
|
|
421
423
|
if (debug) {
|
|
422
424
|
console.log("Generating code for query:", queryText);
|
|
423
425
|
}
|
|
426
|
+
const newHistoryItems = [];
|
|
424
427
|
const codingTools = [
|
|
425
428
|
{
|
|
426
429
|
type: "function",
|
|
@@ -465,6 +468,14 @@ const generateCode = async function(instance, queryText, userChatHistory, create
|
|
|
465
468
|
name: "submitCode",
|
|
466
469
|
// @ts-ignore
|
|
467
470
|
function: async ({ code }) => {
|
|
471
|
+
const checkRes = await instance.checkCode(code);
|
|
472
|
+
if (!checkRes.success) {
|
|
473
|
+
return {
|
|
474
|
+
success: false,
|
|
475
|
+
reason: "Code check failed",
|
|
476
|
+
logs: checkRes.logs
|
|
477
|
+
};
|
|
478
|
+
}
|
|
468
479
|
finalSubmittedCode = code;
|
|
469
480
|
return {
|
|
470
481
|
success: true
|
|
@@ -487,7 +498,7 @@ const generateCode = async function(instance, queryText, userChatHistory, create
|
|
|
487
498
|
...userChatHistory,
|
|
488
499
|
{
|
|
489
500
|
role: "user",
|
|
490
|
-
content: createTaskPrompt.replace("{{USER_QUERY}}", queryText)
|
|
501
|
+
content: attempt == 0 ? createTaskPrompt.replace("{{USER_QUERY}}", queryText) : queryText
|
|
491
502
|
}
|
|
492
503
|
],
|
|
493
504
|
tools: codingTools,
|
|
@@ -496,6 +507,7 @@ const generateCode = async function(instance, queryText, userChatHistory, create
|
|
|
496
507
|
if (debug) {
|
|
497
508
|
console.log("Message:", JSON.stringify(message, null, 2));
|
|
498
509
|
}
|
|
510
|
+
newHistoryItems.push(message);
|
|
499
511
|
}).on("functionToolCall", (functionCall) => {
|
|
500
512
|
if (debug) {
|
|
501
513
|
console.log("Function Call:", JSON.stringify(functionCall, null, 2));
|
|
@@ -507,15 +519,37 @@ const generateCode = async function(instance, queryText, userChatHistory, create
|
|
|
507
519
|
});
|
|
508
520
|
const finalContent = await runner.finalContent();
|
|
509
521
|
if (debug) {
|
|
510
|
-
console.log("
|
|
522
|
+
console.log("Generate code final response:", finalContent);
|
|
523
|
+
}
|
|
524
|
+
if (!finalSubmittedCode) {
|
|
525
|
+
console.error("No code was submitted by the agent.");
|
|
526
|
+
if (attempt < 3) {
|
|
527
|
+
return generateCode(instance, `You must call submitCode with valid code before proceeding. Attempt ${attempt + 1} of 3.`, [
|
|
528
|
+
...userChatHistory,
|
|
529
|
+
{
|
|
530
|
+
role: "user",
|
|
531
|
+
content: attempt == 0 ? createTaskPrompt.replace("{{USER_QUERY}}", queryText) : queryText
|
|
532
|
+
},
|
|
533
|
+
...newHistoryItems
|
|
534
|
+
], createTaskPrompt, debug, hideLogsFromAgent, model, additionalModelOptions, attempt + 1);
|
|
535
|
+
} else {
|
|
536
|
+
return {
|
|
537
|
+
success: false,
|
|
538
|
+
comments: "No code was submitted by the agent after multiple attempts. Final comment: " + finalContent
|
|
539
|
+
};
|
|
540
|
+
}
|
|
511
541
|
}
|
|
512
542
|
return {
|
|
543
|
+
success: true,
|
|
513
544
|
code: finalSubmittedCode,
|
|
514
545
|
comments: finalContent || void 0
|
|
515
546
|
};
|
|
516
547
|
} catch (err) {
|
|
517
548
|
console.error(err);
|
|
518
|
-
return
|
|
549
|
+
return {
|
|
550
|
+
success: false,
|
|
551
|
+
comments: `Error generating code: ${err}`
|
|
552
|
+
};
|
|
519
553
|
}
|
|
520
554
|
};
|
|
521
555
|
async function createWithAPI(options) {
|
package/package.json
CHANGED
|
@@ -4,23 +4,30 @@ Most recent user query:
|
|
|
4
4
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
-
Based on the
|
|
7
|
+
Based on the prior conversation, write a typescript script that performs the operation requested by the user and call the `submitCode` tool with the script.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
## API documentation
|
|
10
|
+
|
|
11
|
+
You can make use of this global API object:
|
|
10
12
|
|
|
11
13
|
`api.ts`:
|
|
12
14
|
```typescript
|
|
13
15
|
{{API}}
|
|
14
16
|
```
|
|
15
17
|
|
|
18
|
+
## Instructions
|
|
19
|
+
|
|
16
20
|
Use the global api object to make calls to the API.
|
|
21
|
+
|
|
17
22
|
You may use top level `await`.
|
|
18
23
|
|
|
19
|
-
If needed,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
If needed, you may use `runCode` to run any non-destructive API calls to gather data for your script. Nothing will be visible to you unless you `console.log` it.
|
|
25
|
+
|
|
26
|
+
You may use the `checkCode` tool to confirm that your code passes typescript compilation and doesn't have errors before submitting.
|
|
27
|
+
|
|
28
|
+
In your code, `console.log` and `console.error` can be used to understand the execution or indicate that an error happened, but debug logs should be removed in the final version (errors can stay).
|
|
29
|
+
|
|
30
|
+
When finished, you must call `submitCode` to submit the final version of the code, then respond with "Successfully submitted code" on success. On failure, correct any errors and submit again.
|
|
24
31
|
|
|
25
32
|
---
|
|
26
33
|
|
package/src/index.ts
CHANGED
|
@@ -11,6 +11,7 @@ import { QuickJSWASMModule, getQuickJS } from "quickjs-emscripten";
|
|
|
11
11
|
import { AutoParseableTool } from 'openai/lib/parser.js';
|
|
12
12
|
import { ZodSchema, z } from 'zod';
|
|
13
13
|
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
14
|
+
import { check } from 'zod/v4';
|
|
14
15
|
|
|
15
16
|
const openai = new OpenAI( { baseURL: process.env.OPENAI_API_BASE_URL || undefined, apiKey: process.env.OPENAI_API_KEY || undefined } );
|
|
16
17
|
|
|
@@ -40,6 +41,7 @@ export interface AIMyAPIInstance {
|
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
export interface GenerateCodeResult {
|
|
44
|
+
success: boolean;
|
|
43
45
|
code?: string;
|
|
44
46
|
comments?: string;
|
|
45
47
|
}
|
|
@@ -89,12 +91,15 @@ const submitCodeSchema = z.object({
|
|
|
89
91
|
code: z.string(),
|
|
90
92
|
});
|
|
91
93
|
|
|
92
|
-
|
|
94
|
+
// Basic harness for writing code for the sandbox.
|
|
95
|
+
export const generateCode = async function(instance:AIMyAPIInstance, queryText:string, userChatHistory:ChatCompletionMessageParam[], createTaskPrompt:string, debug:boolean = false, hideLogsFromAgent:boolean = false, model="gpt-5-mini", additionalModelOptions?: object, attempt:number = 0): Promise<GenerateCodeResult | null> {
|
|
93
96
|
let finalSubmittedCode = "";
|
|
94
97
|
|
|
95
98
|
if(debug) {
|
|
96
99
|
console.log("Generating code for query:", queryText);
|
|
97
100
|
}
|
|
101
|
+
|
|
102
|
+
const newHistoryItems = [];
|
|
98
103
|
// checkcode
|
|
99
104
|
const codingTools: AutoParseableTool<any, true>[] = [
|
|
100
105
|
{
|
|
@@ -143,7 +148,16 @@ export const generateCode = async function(instance:AIMyAPIInstance, queryText:s
|
|
|
143
148
|
function: {
|
|
144
149
|
name: "submitCode",
|
|
145
150
|
// @ts-ignore
|
|
146
|
-
function: async ({code}: {code: string}) => {
|
|
151
|
+
function: async ({code}: {code: string}) => {
|
|
152
|
+
const checkRes = await instance.checkCode(code);
|
|
153
|
+
if(!checkRes.success) {
|
|
154
|
+
return {
|
|
155
|
+
success: false,
|
|
156
|
+
reason: "Code check failed",
|
|
157
|
+
logs: checkRes.logs,
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
147
161
|
finalSubmittedCode = code;
|
|
148
162
|
return {
|
|
149
163
|
success: true,
|
|
@@ -169,7 +183,7 @@ export const generateCode = async function(instance:AIMyAPIInstance, queryText:s
|
|
|
169
183
|
...userChatHistory,
|
|
170
184
|
{
|
|
171
185
|
role: 'user',
|
|
172
|
-
content: createTaskPrompt.replace("{{USER_QUERY}}", queryText)
|
|
186
|
+
content: attempt == 0 ? createTaskPrompt.replace("{{USER_QUERY}}", queryText) : queryText,
|
|
173
187
|
},
|
|
174
188
|
|
|
175
189
|
],
|
|
@@ -180,8 +194,9 @@ export const generateCode = async function(instance:AIMyAPIInstance, queryText:s
|
|
|
180
194
|
.on('message', async (message: ChatCompletionMessageParam) => {
|
|
181
195
|
if(debug) {
|
|
182
196
|
console.log("Message:", JSON.stringify(message, null, 2));
|
|
183
|
-
|
|
184
197
|
}
|
|
198
|
+
|
|
199
|
+
newHistoryItems.push(message);
|
|
185
200
|
})
|
|
186
201
|
.on('functionToolCall', (functionCall: OpenAI.Chat.Completions.ChatCompletionMessageFunctionToolCall.Function) => {
|
|
187
202
|
if(debug) {
|
|
@@ -199,17 +214,41 @@ export const generateCode = async function(instance:AIMyAPIInstance, queryText:s
|
|
|
199
214
|
const finalContent = await runner.finalContent();
|
|
200
215
|
|
|
201
216
|
if(debug) {
|
|
202
|
-
console.log("
|
|
217
|
+
console.log("Generate code final response:", finalContent);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if(!finalSubmittedCode) {
|
|
221
|
+
console.error("No code was submitted by the agent.");
|
|
222
|
+
if(attempt < 3) {
|
|
223
|
+
return generateCode(instance, `You must call submitCode with valid code before proceeding. Attempt ${attempt + 1} of 3.`, [
|
|
224
|
+
...userChatHistory,
|
|
225
|
+
{
|
|
226
|
+
role: 'user',
|
|
227
|
+
content: attempt == 0 ? createTaskPrompt.replace("{{USER_QUERY}}", queryText) : queryText,
|
|
228
|
+
},
|
|
229
|
+
...newHistoryItems
|
|
230
|
+
], createTaskPrompt, debug, hideLogsFromAgent, model, additionalModelOptions, attempt + 1);
|
|
231
|
+
} else {
|
|
232
|
+
return {
|
|
233
|
+
success: false,
|
|
234
|
+
comments: "No code was submitted by the agent after multiple attempts. Final comment: " + finalContent,
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
203
238
|
}
|
|
204
239
|
|
|
205
240
|
return {
|
|
241
|
+
success: true,
|
|
206
242
|
code: finalSubmittedCode,
|
|
207
243
|
comments: finalContent || undefined,
|
|
208
244
|
}
|
|
209
245
|
|
|
210
246
|
} catch(err) {
|
|
211
247
|
console.error(err);
|
|
212
|
-
return
|
|
248
|
+
return {
|
|
249
|
+
success: false,
|
|
250
|
+
comments: `Error generating code: ${err}`,
|
|
251
|
+
};
|
|
213
252
|
}
|
|
214
253
|
};
|
|
215
254
|
|
package/src/sandbox-wrappers.ts
CHANGED
|
@@ -61,6 +61,8 @@ export function wrap(vm, value: any, context: object, beginAsyncProcess: () =>
|
|
|
61
61
|
} else if (value === null) {
|
|
62
62
|
//console.log("Wrapped null")
|
|
63
63
|
return vm.null;
|
|
64
|
+
} else if (value instanceof Date) {
|
|
65
|
+
return vm.evalCode(`new Date(${value.getTime()})`).value;
|
|
64
66
|
} else if (typeof value === "object") {
|
|
65
67
|
//console.log("Wrapped object")
|
|
66
68
|
return wrapObject(vm, value, beginAsyncProcess, endAsyncProcess);
|