tune-sdk 0.1.2 → 0.1.4
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/bin/cli.js +121 -0
- package/dist/fsctx.js +1059 -0
- package/dist/tune.js +567 -289
- package/dist/tune.mjs +41 -3
- package/package.json +2 -1
package/bin/cli.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// Minimal inline argument parser
|
|
4
|
+
const args = {};
|
|
5
|
+
let currentKey = null;
|
|
6
|
+
|
|
7
|
+
process.argv.slice(2).forEach(arg => {
|
|
8
|
+
if (arg.startsWith('--')) {
|
|
9
|
+
const [key, value] = arg.substring(2).split('=');
|
|
10
|
+
if (typeof value !== 'undefined') {
|
|
11
|
+
args[key] = value;
|
|
12
|
+
currentKey = null;
|
|
13
|
+
} else {
|
|
14
|
+
currentKey = key;
|
|
15
|
+
args[currentKey] = true
|
|
16
|
+
}
|
|
17
|
+
} else if (currentKey) {
|
|
18
|
+
args[currentKey] = arg;
|
|
19
|
+
currentKey = null;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Example usage outputs (replace with your real logic)
|
|
24
|
+
/*
|
|
25
|
+
if (args.filename) {
|
|
26
|
+
console.log('filename:', args.filename);
|
|
27
|
+
}
|
|
28
|
+
if (args.system) {
|
|
29
|
+
console.log('system:', args.system);
|
|
30
|
+
}
|
|
31
|
+
if (args.user) {
|
|
32
|
+
console.log('user:', args.user);
|
|
33
|
+
}
|
|
34
|
+
if ('save' in args) {
|
|
35
|
+
console.log('save flag set');
|
|
36
|
+
}
|
|
37
|
+
if (args.stop) {
|
|
38
|
+
console.log('stop:', args.stop);
|
|
39
|
+
}
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
// Place your main script/logic here
|
|
43
|
+
const tune = require('../dist/tune.js');
|
|
44
|
+
const { fsctx } = require('../dist/fsctx.js');
|
|
45
|
+
const fs = require("fs");
|
|
46
|
+
const path = require("path");
|
|
47
|
+
|
|
48
|
+
async function main() {
|
|
49
|
+
let chat = ""
|
|
50
|
+
// where to search for inclusions
|
|
51
|
+
let dirs = [];
|
|
52
|
+
|
|
53
|
+
if (args.filename && fs.existsSync(args.filename)) {
|
|
54
|
+
chat = fs.readFileSync(args.filename, "utf8")
|
|
55
|
+
dirs.push(path.dirname(path.resolve(args.filename)))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!chat && args.system) {
|
|
59
|
+
chat = `system:\n${args.system}`
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (args.user) {
|
|
63
|
+
chat = `${chat}\nuser:\n${args.user}`
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if(process.env.TUNE_PATH) {
|
|
67
|
+
dirs = dirs.concat(process.env.TUNE_PATH.split(path.delimiter))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
dirs.push(process.cwd())
|
|
71
|
+
|
|
72
|
+
const ctx = tune.makeContext(process.env)
|
|
73
|
+
if (args.filename) {
|
|
74
|
+
ctx.stack.push({
|
|
75
|
+
filename: args.filename,
|
|
76
|
+
name: path.basename(args.filename),
|
|
77
|
+
dirname: path.dirname(path.resolve(args.filename))
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
ctx.use(fsctx(dirs))
|
|
81
|
+
|
|
82
|
+
for (const dir of dirs) {
|
|
83
|
+
let filename = ["default.ctx.js", "default.ctx.cjs", "default.ctx.mjs"].find(name => fs.existsSync(path.join(dir, name)));
|
|
84
|
+
if (!filename) continue;
|
|
85
|
+
filename = path.join(dir, filename);
|
|
86
|
+
let ext = path.extname(filename);
|
|
87
|
+
let module
|
|
88
|
+
if ((ext === ".js" || ext === ".cjs")) {
|
|
89
|
+
module = require(filename);
|
|
90
|
+
} else {
|
|
91
|
+
module = await import(filename);
|
|
92
|
+
module = module.default;
|
|
93
|
+
}
|
|
94
|
+
if (typeof module === "function") {
|
|
95
|
+
ctx.use(module);
|
|
96
|
+
} else if (Array.isArray(module)) {
|
|
97
|
+
for (const m of module) {
|
|
98
|
+
if (typeof m === "function") {
|
|
99
|
+
ctx.use(m)
|
|
100
|
+
} else {
|
|
101
|
+
throw(Error(`err: Context file export is not an array of functions or function ${filename}`))
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
} else {
|
|
105
|
+
throw(Error(`err: Context file export is not an array of functions or function ${filename}`))
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const longFormatRegex = /^(system|user|tool_call|tool_result|assistant|error):/;
|
|
110
|
+
let res = await tune.text2run(chat, ctx, args.step || "assistant")
|
|
111
|
+
res = tune.msg2text(res, longFormatRegex.test(chat))
|
|
112
|
+
if (args.filename && args.save) {
|
|
113
|
+
chat += "\n" + res
|
|
114
|
+
fs.writeFileSync(args.filename, chat)
|
|
115
|
+
}
|
|
116
|
+
if (!args.save) {
|
|
117
|
+
console.log(res)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
main()
|