-
Notifications
You must be signed in to change notification settings - Fork 3
/
code-optimizer.ts
80 lines (72 loc) · 1.79 KB
/
code-optimizer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { inspect } from "node:util";
import { Agent } from "@hyv/core";
import { createInstructionTemplate, GPTModelAdapter } from "@hyv/openai";
import { extractCode, minify } from "@hyv/utils";
inspect.defaultOptions.depth = null;
const developer = new Agent(
new GPTModelAdapter({
maxTokens: 2048,
model: "gpt-4",
systemInstruction: createInstructionTemplate(
"expert JavaScript Developer, expert Canvas2D Developer, **performance expert**",
minify`
Achieve the {{goal}}.
Use the {{boilerplate}}.
`,
{
thoughts: "elaborative thoughts",
code: "valid JavaScript",
}
),
}),
{ verbosity: 1 }
);
const optimizer = new Agent(
new GPTModelAdapter({
maxTokens: 2048,
model: "gpt-4",
systemInstruction: createInstructionTemplate(
"expert JavaScript Developer, expert Canvas2D Developer, **performance expert**",
minify`
Review the {{code}}.
Look for potential errors and fix them.
Optimize the {{code}} as needed.
`,
{
review: "elaborative review and critique",
code: "valid JavaScript (original or optimized)",
}
),
}),
{
verbosity: 1,
sideEffects: [
{
prop: "code",
async run(value: string) {
const { code } = extractCode(value);
console.log("SIDE EFFECT");
console.log(code);
},
},
],
}
);
try {
const raw = await developer.assign({
goal: "The matrix code",
boilerplate: minify`
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
function setCanvasSize() {
canvas.width = window.innerWidth * devicePixelRatio;
canvas.height = window.innerHeight * devicePixelRatio;
}
setCanvasSize();
window.addEventListener("resize", setCanvasSize, {passive: true});
`,
});
await optimizer.do(raw.id);
} catch (error) {
console.error("Error:", error);
}