tg-agent 0.1.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/.env.example +50 -0
- package/README.md +152 -0
- package/dist/auth.js +71 -0
- package/dist/cli.js +2 -0
- package/dist/codexAuth.js +93 -0
- package/dist/config.js +59 -0
- package/dist/customTools.js +386 -0
- package/dist/index.js +954 -0
- package/dist/mcp.js +427 -0
- package/dist/piAgentRunner.js +407 -0
- package/dist/piAiRunner.js +99 -0
- package/dist/proxy.js +19 -0
- package/dist/sessionStore.js +138 -0
- package/dist/types.js +1 -0
- package/dist/utils.js +91 -0
- package/package.json +41 -0
package/dist/utils.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
export function nowMs() {
|
|
6
|
+
return Date.now();
|
|
7
|
+
}
|
|
8
|
+
export function expandHome(inputPath) {
|
|
9
|
+
if (!inputPath.startsWith("~")) {
|
|
10
|
+
return inputPath;
|
|
11
|
+
}
|
|
12
|
+
return path.join(os.homedir(), inputPath.slice(1));
|
|
13
|
+
}
|
|
14
|
+
export async function ensureDir(dirPath) {
|
|
15
|
+
await fs.mkdir(dirPath, { recursive: true });
|
|
16
|
+
}
|
|
17
|
+
export async function atomicWriteJson(filePath, data) {
|
|
18
|
+
const dir = path.dirname(filePath);
|
|
19
|
+
const tmpName = `${path.basename(filePath)}.${randomUUID()}.tmp`;
|
|
20
|
+
const tmpPath = path.join(dir, tmpName);
|
|
21
|
+
const json = JSON.stringify(data, null, 2);
|
|
22
|
+
await fs.writeFile(tmpPath, json, "utf8");
|
|
23
|
+
await fs.rename(tmpPath, filePath);
|
|
24
|
+
}
|
|
25
|
+
export function shortId() {
|
|
26
|
+
return randomUUID().split("-")[0];
|
|
27
|
+
}
|
|
28
|
+
export function chunkText(text, maxLen) {
|
|
29
|
+
if (text.length <= maxLen) {
|
|
30
|
+
return [text];
|
|
31
|
+
}
|
|
32
|
+
const chunks = [];
|
|
33
|
+
let remaining = text;
|
|
34
|
+
while (remaining.length > maxLen) {
|
|
35
|
+
let slice = remaining.slice(0, maxLen);
|
|
36
|
+
const lastNewline = slice.lastIndexOf("\n");
|
|
37
|
+
if (lastNewline > maxLen * 0.6) {
|
|
38
|
+
slice = slice.slice(0, lastNewline);
|
|
39
|
+
}
|
|
40
|
+
chunks.push(slice);
|
|
41
|
+
remaining = remaining.slice(slice.length);
|
|
42
|
+
}
|
|
43
|
+
if (remaining.length > 0) {
|
|
44
|
+
chunks.push(remaining);
|
|
45
|
+
}
|
|
46
|
+
return chunks;
|
|
47
|
+
}
|
|
48
|
+
export function createQueueMap() {
|
|
49
|
+
const queues = new Map();
|
|
50
|
+
return async function enqueue(key, task) {
|
|
51
|
+
const prev = queues.get(key) ?? Promise.resolve();
|
|
52
|
+
const next = prev.then(task, task);
|
|
53
|
+
queues.set(key, next);
|
|
54
|
+
try {
|
|
55
|
+
return await next;
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
if (queues.get(key) === next) {
|
|
59
|
+
queues.delete(key);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export function createSemaphore(limit) {
|
|
65
|
+
let active = 0;
|
|
66
|
+
const queue = [];
|
|
67
|
+
const next = () => {
|
|
68
|
+
if (active >= limit) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const resolve = queue.shift();
|
|
72
|
+
if (!resolve) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
active += 1;
|
|
76
|
+
resolve();
|
|
77
|
+
};
|
|
78
|
+
return async function withSemaphore(task) {
|
|
79
|
+
await new Promise((resolve) => {
|
|
80
|
+
queue.push(resolve);
|
|
81
|
+
next();
|
|
82
|
+
});
|
|
83
|
+
try {
|
|
84
|
+
return await task();
|
|
85
|
+
}
|
|
86
|
+
finally {
|
|
87
|
+
active = Math.max(0, active - 1);
|
|
88
|
+
next();
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tg-agent",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Telegram Codex agent",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"tg-agent": "dist/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
".env.example",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"dev": "tsx src/index.ts",
|
|
17
|
+
"build": "tsc -p tsconfig.json && node scripts/add-shebang.mjs",
|
|
18
|
+
"prepublishOnly": "npm run build",
|
|
19
|
+
"start": "node dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@mariozechner/pi-ai": "^0.46.0",
|
|
23
|
+
"@mariozechner/pi-coding-agent": "^0.46.0",
|
|
24
|
+
"@sinclair/typebox": "^0.32.35",
|
|
25
|
+
"dotenv": "^16.4.5",
|
|
26
|
+
"https-proxy-agent": "^7.0.5",
|
|
27
|
+
"node-telegram-bot-api": "^0.64.0",
|
|
28
|
+
"openai": "^4.61.0",
|
|
29
|
+
"socks-proxy-agent": "^8.0.3",
|
|
30
|
+
"undici": "^7.18.2"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/node": "^20.14.10",
|
|
34
|
+
"@types/node-telegram-bot-api": "^0.64.7",
|
|
35
|
+
"tsx": "^4.19.1",
|
|
36
|
+
"typescript": "^5.6.3"
|
|
37
|
+
},
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=18"
|
|
40
|
+
}
|
|
41
|
+
}
|