@axvaich/ai-lib 1.0.0
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 +24 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +107 -0
- package/index.js +3 -0
- package/package.json +31 -0
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @axvaich/ai-lib
|
|
2
|
+
|
|
3
|
+
## Using
|
|
4
|
+
|
|
5
|
+
code you see:
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
const command = await generate("multiple all numbers in array and return it");
|
|
9
|
+
console.log(command([2, 1, 2, 3]));
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
generated code:
|
|
13
|
+
|
|
14
|
+
```js
|
|
15
|
+
/**
|
|
16
|
+
* Перемножает все числа в массиве и возвращает результат.
|
|
17
|
+
*
|
|
18
|
+
* @param {number[]} numbers - Массив чисел для перемножения.
|
|
19
|
+
* @returns {number} Результат перемножения всех чисел в массиве.
|
|
20
|
+
*/
|
|
21
|
+
export default function multiplyNumbers(numbers) {
|
|
22
|
+
return numbers.reduce((acc, current) => acc * current, 1);
|
|
23
|
+
}
|
|
24
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
declare function compileTs(code: string): string;
|
|
2
|
+
declare function generate(prompt: string): Promise<any>;
|
|
3
|
+
|
|
4
|
+
declare class Storage {
|
|
5
|
+
funcs: never[];
|
|
6
|
+
private fullPath;
|
|
7
|
+
constructor();
|
|
8
|
+
addInMeta(): Promise<string>;
|
|
9
|
+
createFunction(funcName: string, codeInJS: string): Promise<string>;
|
|
10
|
+
}
|
|
11
|
+
declare const store: Storage;
|
|
12
|
+
|
|
13
|
+
interface FuncitonSpecs {
|
|
14
|
+
name: string;
|
|
15
|
+
description: string;
|
|
16
|
+
args?: string;
|
|
17
|
+
}
|
|
18
|
+
interface Generated {
|
|
19
|
+
name: string;
|
|
20
|
+
path: string;
|
|
21
|
+
model: string;
|
|
22
|
+
}
|
|
23
|
+
interface MetadataIndex {
|
|
24
|
+
[hash: string]: Generated;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { type FuncitonSpecs, type Generated, type MetadataIndex, compileTs, generate, store };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// src/generator.ts
|
|
2
|
+
import Groq from "groq-sdk";
|
|
3
|
+
import "dotenv/config";
|
|
4
|
+
|
|
5
|
+
// src/utils.ts
|
|
6
|
+
import crypto from "crypto";
|
|
7
|
+
import ts from "typescript";
|
|
8
|
+
function generateHash(input) {
|
|
9
|
+
return crypto.createHash("sha256").update(input).digest("hex");
|
|
10
|
+
}
|
|
11
|
+
function extractCodeBlock(text) {
|
|
12
|
+
const match = text.match(/```(?:typescript|ts)?\s*([\s\S]*?)```/);
|
|
13
|
+
if (!match) {
|
|
14
|
+
throw new Error("No code block found in LLM response");
|
|
15
|
+
}
|
|
16
|
+
return match[1].trim();
|
|
17
|
+
}
|
|
18
|
+
function getFunctionName(code) {
|
|
19
|
+
const source = ts.createSourceFile(
|
|
20
|
+
"temp.ts",
|
|
21
|
+
code,
|
|
22
|
+
ts.ScriptTarget.Latest,
|
|
23
|
+
true
|
|
24
|
+
);
|
|
25
|
+
let name = "";
|
|
26
|
+
source.forEachChild((node) => {
|
|
27
|
+
if (ts.isFunctionDeclaration(node) && node.name) {
|
|
28
|
+
name = node.name.text;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
if (!name) {
|
|
32
|
+
throw new Error("No named function found");
|
|
33
|
+
}
|
|
34
|
+
return name;
|
|
35
|
+
}
|
|
36
|
+
async function loadFunction(filePath) {
|
|
37
|
+
const module = await import(`file://${filePath}`);
|
|
38
|
+
return module.default;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/storage.ts
|
|
42
|
+
import fs from "fs-extra";
|
|
43
|
+
import path from "path";
|
|
44
|
+
var savingDir = "generated";
|
|
45
|
+
var Storage = class {
|
|
46
|
+
constructor() {
|
|
47
|
+
this.funcs = [];
|
|
48
|
+
this.fullPath = path.join(process.cwd(), savingDir);
|
|
49
|
+
}
|
|
50
|
+
async addInMeta() {
|
|
51
|
+
return generateHash("asd");
|
|
52
|
+
}
|
|
53
|
+
async createFunction(funcName, codeInJS) {
|
|
54
|
+
const fullPath = path.join(this.fullPath, funcName + ".js");
|
|
55
|
+
await fs.createFile(fullPath);
|
|
56
|
+
await fs.writeFile(fullPath, codeInJS);
|
|
57
|
+
return fullPath;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
var store = new Storage();
|
|
61
|
+
|
|
62
|
+
// src/generator.ts
|
|
63
|
+
import ts2 from "typescript";
|
|
64
|
+
var SYSTEM_PROMPT = `\u0422\u044B \u0433\u0435\u043D\u0435\u0440\u0430\u0442\u043E\u0440 TypeScript \u0444\u0443\u043D\u043A\u0446\u0438\u0439. \u0421\u0442\u0440\u043E\u0433\u0438\u0435 \u043F\u0440\u0430\u0432\u0438\u043B\u0430:
|
|
65
|
+
- \u041E\u0434\u043D\u0430 \u0444\u0443\u043D\u043A\u0446\u0438\u044F \u0441 export default
|
|
66
|
+
- \u0421\u0442\u0440\u043E\u0433\u0430\u044F \u0442\u0438\u043F\u0438\u0437\u0430\u0446\u0438\u044F \u0432\u0441\u0435\u0445 \u043F\u0430\u0440\u0430\u043C\u0435\u0442\u0440\u043E\u0432 \u0438 return type
|
|
67
|
+
- \u041E\u0431\u044F\u0437\u0430\u0442\u0435\u043B\u044C\u043D\u043E \u0432\u043E\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043D\u0430\u0447\u0435\u043D\u0438\u0435
|
|
68
|
+
- JSDoc \u043A\u043E\u043C\u043C\u0435\u043D\u0442\u0430\u0440\u0438\u0439 \u0441 \u043E\u043F\u0438\u0441\u0430\u043D\u0438\u0435\u043C
|
|
69
|
+
- Pure \u0444\u0443\u043D\u043A\u0446\u0438\u044F, \u0431\u0435\u0437 side-effects
|
|
70
|
+
- \u0422\u043E\u043B\u044C\u043A\u043E \u043A\u043E\u0434, \u0441 \u043D\u0435\u0431\u043E\u043B\u044C\u0448\u0438\u043C \u043E\u0431\u044A\u044F\u0441\u043D\u0435\u043D\u0438\u0435\u043C
|
|
71
|
+
- \u041D\u0438\u043A\u0430\u043A\u0438\u0445 \u0438\u043C\u043F\u043E\u0440\u0442\u043E\u0432 (\u0442\u043E\u043B\u044C\u043A\u043E \u0432\u0441\u0442\u0440\u043E\u0435\u043D\u043D\u044B\u0435 \u0442\u0438\u043F\u044B TS)
|
|
72
|
+
- \u0415\u0441\u043B\u0438 \u0432 \u043F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u0442\u0435\u043B\u044C\u0441\u043A\u043E\u043C \u0437\u0430\u043F\u0440\u043E\u0441\u0435 \u043D\u0435\u0442 \u044F\u0432\u043D\u044B\u0445 \u0430\u0440\u0433\u0443\u043C\u0435\u043D\u0442\u043E\u0432 \u2014 \u0444\u0443\u043D\u043A\u0446\u0438\u044F \u043D\u0435 \u0434\u043E\u043B\u0436\u043D\u0430 \u043F\u0440\u0438\u043D\u0438\u043C\u0430\u0442\u044C \u043F\u0430\u0440\u0430\u043C\u0435\u0442\u0440\u044B, \u043F\u0440\u043E\u0441\u0442\u043E \u0432\u043E\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043B\u044C\u0442\u0430\u0442
|
|
73
|
+
- \u0421\u043B\u0435\u0434\u0443\u0439 \u0441\u0442\u0440\u043E\u0433\u043E \u0437\u0430 \u0438\u043D\u0441\u0442\u0440\u0443\u043A\u0446\u0438\u044F\u043C\u0438 \u043F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u0442\u0435\u043B\u044F`;
|
|
74
|
+
var groq = new Groq({
|
|
75
|
+
apiKey: process.env.GROQ_API_KEY
|
|
76
|
+
});
|
|
77
|
+
function compileTs(code) {
|
|
78
|
+
const result = ts2.transpileModule(code, {
|
|
79
|
+
compilerOptions: {
|
|
80
|
+
module: ts2.ModuleKind.ESNext,
|
|
81
|
+
target: ts2.ScriptTarget.ES2020,
|
|
82
|
+
strict: true
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
return result.outputText;
|
|
86
|
+
}
|
|
87
|
+
async function generate(prompt) {
|
|
88
|
+
const result = await groq.chat.completions.create({
|
|
89
|
+
model: "llama-3.3-70b-versatile",
|
|
90
|
+
messages: [
|
|
91
|
+
{ role: "system", content: SYSTEM_PROMPT },
|
|
92
|
+
{ role: "user", content: prompt }
|
|
93
|
+
],
|
|
94
|
+
temperature: 0,
|
|
95
|
+
max_completion_tokens: 1024
|
|
96
|
+
});
|
|
97
|
+
const content = result.choices[0]?.message?.content || "";
|
|
98
|
+
const code = extractCodeBlock(content);
|
|
99
|
+
const fileName = getFunctionName(code);
|
|
100
|
+
const filePath = await store.createFunction(fileName, compileTs(code));
|
|
101
|
+
return await loadFunction(filePath);
|
|
102
|
+
}
|
|
103
|
+
export {
|
|
104
|
+
compileTs,
|
|
105
|
+
generate,
|
|
106
|
+
store
|
|
107
|
+
};
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@axvaich/ai-lib",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"main": "dist/index.cjs",
|
|
9
|
+
"module": "dist/index.js",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
14
|
+
"dev": "tsup src/index.ts --format esm --dts --watch --external typescript --external fs-extra",
|
|
15
|
+
"typecheck": "tsc --noEmit"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [],
|
|
18
|
+
"author": "",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/fs-extra": "^11.0.4",
|
|
22
|
+
"@types/node": "^25.2.3",
|
|
23
|
+
"tsup": "^8.5.1",
|
|
24
|
+
"typescript": "^5.9.3"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"dotenv": "^17.2.4",
|
|
28
|
+
"fs-extra": "^11.3.3",
|
|
29
|
+
"groq-sdk": "^0.37.0"
|
|
30
|
+
}
|
|
31
|
+
}
|