ai-sort 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/LICENSE +21 -0
- package/README.md +43 -0
- package/package.json +24 -0
- package/src/tests/basic.test.js +17 -0
- package/src/vibesort/index.js +80 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Mrinaal Arora
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# ai-sort
|
2
|
+
|
3
|
+
vibe-based array sorting powered by GPT.
|
4
|
+
because `array.sort()` is too mainstream.
|
5
|
+
|
6
|
+
## usage
|
7
|
+
|
8
|
+
install:
|
9
|
+
|
10
|
+
```bash
|
11
|
+
npm install ai-sort
|
12
|
+
```
|
13
|
+
|
14
|
+
set your OpenAI API key:
|
15
|
+
|
16
|
+
```bash
|
17
|
+
export OPENAI_API_KEY=your_key_here
|
18
|
+
```
|
19
|
+
|
20
|
+
then use it:
|
21
|
+
|
22
|
+
```js
|
23
|
+
import { vibesort } from 'ai-sort';
|
24
|
+
|
25
|
+
const result = await vibesort([5, 2, 8, 1, 9]);
|
26
|
+
console.log(result); // [1, 2, 5, 8, 9]
|
27
|
+
```
|
28
|
+
|
29
|
+
## test
|
30
|
+
|
31
|
+
```bash
|
32
|
+
npm run test
|
33
|
+
```
|
34
|
+
|
35
|
+
## dependencies
|
36
|
+
|
37
|
+
* openai
|
38
|
+
* zod
|
39
|
+
* dotenv (optional, for `.env` support)
|
40
|
+
|
41
|
+
⚠️ requires an OpenAI API key.
|
42
|
+
|
43
|
+
⚠️ experimental project — use for vibes, not for prod.
|
package/package.json
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
{
|
2
|
+
"name": "ai-sort",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "vibe-based array sorting powered by GPT",
|
5
|
+
"main": "src/vibesort/index.js",
|
6
|
+
"type": "module",
|
7
|
+
"scripts": {
|
8
|
+
"test": "node src/tests/basic.test.js"
|
9
|
+
},
|
10
|
+
"keywords": [
|
11
|
+
"sort",
|
12
|
+
"ai",
|
13
|
+
"vibesort",
|
14
|
+
"gpt",
|
15
|
+
"meme"
|
16
|
+
],
|
17
|
+
"author": "Mrinaal Arora",
|
18
|
+
"license": "MIT",
|
19
|
+
"dependencies": {
|
20
|
+
"dotenv": "^17.2.1",
|
21
|
+
"openai": "^5.12.2",
|
22
|
+
"zod": "^3.25.76"
|
23
|
+
}
|
24
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { vibesort } from '../vibesort/index.js';
|
2
|
+
import 'dotenv/config';
|
3
|
+
|
4
|
+
const input = [6, 2, 9, 1, 4];
|
5
|
+
|
6
|
+
console.log('[test] starting ai-sort with input:', input);
|
7
|
+
|
8
|
+
try {
|
9
|
+
const result = await vibesort(input);
|
10
|
+
|
11
|
+
console.log('[test] result:', result);
|
12
|
+
|
13
|
+
const isSorted = result.every((val, i, arr) => i === 0 || arr[i - 1] <= val);
|
14
|
+
console.log(`[test] is sorted correctly? ${isSorted ? 'yes' : 'no'}`);
|
15
|
+
} catch (err) {
|
16
|
+
console.error('[test] error:', err.message);
|
17
|
+
}
|
@@ -0,0 +1,80 @@
|
|
1
|
+
import OpenAI from "openai";
|
2
|
+
import { z } from "zod";
|
3
|
+
import { zodTextFormat } from "openai/helpers/zod";
|
4
|
+
|
5
|
+
const SortSchema = z.object({
|
6
|
+
sorted: z.array(z.number()),
|
7
|
+
});
|
8
|
+
|
9
|
+
function nowIso() {
|
10
|
+
return new Date().toISOString();
|
11
|
+
}
|
12
|
+
|
13
|
+
function logInfo(...parts) {
|
14
|
+
console.log(`[ai-sort] ${nowIso()} INFO:`, ...parts);
|
15
|
+
}
|
16
|
+
|
17
|
+
function logWarn(...parts) {
|
18
|
+
console.warn(`[ai-sort] ${nowIso()} WARN:`, ...parts);
|
19
|
+
}
|
20
|
+
|
21
|
+
function logError(...parts) {
|
22
|
+
console.error(`[ai-sort] ${nowIso()} ERROR:`, ...parts);
|
23
|
+
}
|
24
|
+
|
25
|
+
export async function vibesort(array) {
|
26
|
+
if (!Array.isArray(array)) throw new Error("input must be an array of numbers");
|
27
|
+
const allNumbers = array.every((value) => typeof value === "number" && Number.isFinite(value));
|
28
|
+
if (!allNumbers) {
|
29
|
+
throw new Error("input array must contain only finite numbers");
|
30
|
+
}
|
31
|
+
|
32
|
+
if (!process.env.OPENAI_API_KEY) {
|
33
|
+
const guidance = [
|
34
|
+
"OPENAI_API_KEY is not set.",
|
35
|
+
"Set it in your shell:",
|
36
|
+
' export OPENAI_API_KEY="sk-..."',
|
37
|
+
"Or add it to a .env file:",
|
38
|
+
" OPENAI_API_KEY=your_key",
|
39
|
+
"Then run your command again.",
|
40
|
+
"Forgot your key? Happens to the best of us. You can create one here: https://platform.openai.com/api-keys",
|
41
|
+
].join("\n");
|
42
|
+
logWarn(guidance);
|
43
|
+
throw new Error("missing OPENAI_API_KEY");
|
44
|
+
}
|
45
|
+
|
46
|
+
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
47
|
+
|
48
|
+
logInfo("sending array to GPT for sorting:", array);
|
49
|
+
|
50
|
+
try {
|
51
|
+
const response = await openai.responses.parse({
|
52
|
+
model: "gpt-4o-2024-08-06",
|
53
|
+
input: [
|
54
|
+
{
|
55
|
+
role: "user",
|
56
|
+
content: JSON.stringify({ array, order: "asc" }),
|
57
|
+
},
|
58
|
+
],
|
59
|
+
text: {
|
60
|
+
format: zodTextFormat(SortSchema, "vibesort"),
|
61
|
+
},
|
62
|
+
});
|
63
|
+
|
64
|
+
const result = response.output_parsed.sorted;
|
65
|
+
logInfo("sorted result from GPT:", result);
|
66
|
+
return result;
|
67
|
+
} catch (err) {
|
68
|
+
const status = err?.status || err?.code || "unknown";
|
69
|
+
const base = `sorting failed (status: ${status}).`;
|
70
|
+
const tips = [];
|
71
|
+
if (status === 401) tips.push("Your API key looks shy. Double-check OPENAI_API_KEY and try again.");
|
72
|
+
if (status === 429) tips.push("Too much vibe at once (rate-limited). Pause for a moment and retry.");
|
73
|
+
if (status === "unknown") tips.push("Network gremlins? Check your internet or try again.");
|
74
|
+
tips.push("If vibes keep failing, regenerate a fresh key: https://platform.openai.com/api-keys");
|
75
|
+
|
76
|
+
const message = [base, ...tips].join(" \n");
|
77
|
+
logError(message);
|
78
|
+
throw new Error(message);
|
79
|
+
}
|
80
|
+
}
|