teleton 0.5.2 → 0.7.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.
Files changed (40) hide show
  1. package/README.md +243 -105
  2. package/dist/chunk-FNV5FF35.js +331 -0
  3. package/dist/chunk-LRCPA7SC.js +149 -0
  4. package/dist/{chunk-WUTMT6DW.js → chunk-N3F7E7DR.js} +114 -566
  5. package/dist/chunk-ND2X5FWB.js +368 -0
  6. package/dist/chunk-NERLQY2H.js +421 -0
  7. package/dist/{chunk-YBA6IBGT.js → chunk-OCLG5GKI.js} +24 -24
  8. package/dist/{chunk-WOXBZOQX.js → chunk-OGIG552S.js} +2152 -4488
  9. package/dist/chunk-RCMD3U65.js +141 -0
  10. package/dist/{chunk-O4R7V5Y2.js → chunk-RO62LO6Z.js} +11 -1
  11. package/dist/chunk-TCD4NZDA.js +3226 -0
  12. package/dist/chunk-UCN6TI25.js +143 -0
  13. package/dist/{chunk-WL2Q3VRD.js → chunk-UDD7FYOU.js} +12 -4
  14. package/dist/chunk-VAUJSSD3.js +20 -0
  15. package/dist/chunk-XBE4JB7C.js +8 -0
  16. package/dist/chunk-XBKSS6DM.js +58 -0
  17. package/dist/cli/index.js +1179 -412
  18. package/dist/client-3VWE7NC4.js +29 -0
  19. package/dist/{get-my-gifts-KVULMBJ3.js → get-my-gifts-RI7FAXAL.js} +3 -1
  20. package/dist/index.js +17 -8
  21. package/dist/{memory-Y5J7CXAR.js → memory-RD7ZSTRV.js} +16 -10
  22. package/dist/{migrate-UEQCDWL2.js → migrate-GO4NOBT7.js} +14 -6
  23. package/dist/{server-BQY7CM2N.js → server-OWVEZTR3.js} +869 -93
  24. package/dist/setup-server-C7ZTPHD5.js +934 -0
  25. package/dist/{task-dependency-resolver-TRPILAHM.js → task-dependency-resolver-WKZWJLLM.js} +19 -15
  26. package/dist/{task-executor-N7XNVK5N.js → task-executor-PD3H4MLO.js} +5 -2
  27. package/dist/tool-adapter-Y3TCEQOC.js +145 -0
  28. package/dist/tool-index-MIVK3D7H.js +250 -0
  29. package/dist/{transcript-7V4UNID4.js → transcript-UDJZP6NK.js} +2 -1
  30. package/dist/web/assets/complete-fZLnb5Ot.js +1 -0
  31. package/dist/web/assets/index-B_FcaX5D.css +1 -0
  32. package/dist/web/assets/index-CbeAP4_n.js +67 -0
  33. package/dist/web/assets/index.es-oXiZF7Hc.js +11 -0
  34. package/dist/web/assets/login-telegram-BP7CJDmx.js +1 -0
  35. package/dist/web/assets/run-DOrDowjK.js +1 -0
  36. package/dist/web/index.html +2 -2
  37. package/package.json +21 -15
  38. package/dist/chunk-5WWR4CU3.js +0 -124
  39. package/dist/web/assets/index-CDMbujHf.css +0 -1
  40. package/dist/web/assets/index-DDX8oQ2z.js +0 -67
@@ -0,0 +1,143 @@
1
+ import {
2
+ TELETON_ROOT
3
+ } from "./chunk-EYWNOHMJ.js";
4
+ import {
5
+ createLogger
6
+ } from "./chunk-RCMD3U65.js";
7
+
8
+ // src/utils/module-db.ts
9
+ import Database from "better-sqlite3";
10
+ import { existsSync, mkdirSync, chmodSync } from "fs";
11
+ import { dirname, join } from "path";
12
+ var log = createLogger("Utils");
13
+ var JOURNAL_SCHEMA = `
14
+ CREATE TABLE IF NOT EXISTS journal (
15
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
16
+ timestamp INTEGER NOT NULL DEFAULT (unixepoch()),
17
+ type TEXT NOT NULL CHECK(type IN ('trade', 'gift', 'middleman', 'kol')),
18
+ action TEXT NOT NULL,
19
+ asset_from TEXT,
20
+ asset_to TEXT,
21
+ amount_from REAL,
22
+ amount_to REAL,
23
+ price_ton REAL,
24
+ counterparty TEXT,
25
+ platform TEXT,
26
+ reasoning TEXT,
27
+ outcome TEXT CHECK(outcome IN ('pending', 'profit', 'loss', 'neutral', 'cancelled')),
28
+ pnl_ton REAL,
29
+ pnl_pct REAL,
30
+ tx_hash TEXT,
31
+ tool_used TEXT,
32
+ chat_id TEXT,
33
+ user_id INTEGER,
34
+ closed_at INTEGER,
35
+ created_at INTEGER NOT NULL DEFAULT (unixepoch())
36
+ );
37
+
38
+ CREATE INDEX IF NOT EXISTS idx_journal_type ON journal(type);
39
+ CREATE INDEX IF NOT EXISTS idx_journal_timestamp ON journal(timestamp DESC);
40
+ CREATE INDEX IF NOT EXISTS idx_journal_asset_from ON journal(asset_from);
41
+ CREATE INDEX IF NOT EXISTS idx_journal_outcome ON journal(outcome);
42
+ CREATE INDEX IF NOT EXISTS idx_journal_type_timestamp ON journal(type, timestamp DESC);
43
+ `;
44
+ var USED_TRANSACTIONS_SCHEMA = `
45
+ CREATE TABLE IF NOT EXISTS used_transactions (
46
+ tx_hash TEXT PRIMARY KEY,
47
+ user_id TEXT NOT NULL,
48
+ amount REAL NOT NULL,
49
+ game_type TEXT NOT NULL,
50
+ used_at INTEGER NOT NULL DEFAULT (unixepoch())
51
+ );
52
+
53
+ CREATE INDEX IF NOT EXISTS idx_used_tx_user ON used_transactions(user_id);
54
+ CREATE INDEX IF NOT EXISTS idx_used_tx_used_at ON used_transactions(used_at);
55
+ `;
56
+ function openModuleDb(path) {
57
+ const dir = dirname(path);
58
+ if (!existsSync(dir)) {
59
+ mkdirSync(dir, { recursive: true });
60
+ }
61
+ const db = new Database(path);
62
+ try {
63
+ chmodSync(path, 384);
64
+ } catch {
65
+ }
66
+ db.pragma("journal_mode = WAL");
67
+ return db;
68
+ }
69
+ function createDbWrapper(getDb, moduleName) {
70
+ return function withDb(executor) {
71
+ return (params, context) => {
72
+ const moduleDb = getDb();
73
+ if (!moduleDb) {
74
+ return Promise.resolve({
75
+ success: false,
76
+ error: `${moduleName} module not started`
77
+ });
78
+ }
79
+ return executor(params, { ...context, db: moduleDb });
80
+ };
81
+ };
82
+ }
83
+ var MAIN_DB_PATH = join(TELETON_ROOT, "memory.db");
84
+ function migrateFromMainDb(moduleDb, tables) {
85
+ let totalMigrated = 0;
86
+ for (const table of tables) {
87
+ if (!/^[a-z_]+$/.test(table)) {
88
+ throw new Error(`Invalid table name for migration: "${table}"`);
89
+ }
90
+ }
91
+ for (const table of tables) {
92
+ try {
93
+ const row = moduleDb.prepare(`SELECT COUNT(*) as c FROM ${table}`).get();
94
+ if (row.c > 0) return 0;
95
+ } catch {
96
+ continue;
97
+ }
98
+ }
99
+ if (!existsSync(MAIN_DB_PATH)) return 0;
100
+ try {
101
+ moduleDb.exec(`ATTACH DATABASE '${MAIN_DB_PATH}' AS main_db`);
102
+ for (const table of tables) {
103
+ try {
104
+ const exists = moduleDb.prepare(`SELECT name FROM main_db.sqlite_master WHERE type='table' AND name=?`).get(table);
105
+ if (!exists) continue;
106
+ const src = moduleDb.prepare(`SELECT COUNT(*) as c FROM main_db.${table}`).get();
107
+ if (src.c === 0) continue;
108
+ const dstCols = moduleDb.prepare(`PRAGMA table_info(${table})`).all().map((r) => r.name);
109
+ const srcCols = moduleDb.prepare(`PRAGMA main_db.table_info(${table})`).all().map((r) => r.name);
110
+ const shared = dstCols.filter((c) => srcCols.includes(c));
111
+ if (shared.length === 0) continue;
112
+ const cols = shared.join(", ");
113
+ moduleDb.exec(
114
+ `INSERT OR IGNORE INTO ${table} (${cols}) SELECT ${cols} FROM main_db.${table}`
115
+ );
116
+ totalMigrated += src.c;
117
+ try {
118
+ moduleDb.exec(`DROP TABLE main_db.${table}`);
119
+ } catch {
120
+ }
121
+ log.info(`Migrated ${src.c} rows from memory.db \u2192 ${table} (source dropped)`);
122
+ } catch (e) {
123
+ log.warn({ err: e }, `Could not migrate table ${table}`);
124
+ }
125
+ }
126
+ moduleDb.exec(`DETACH DATABASE main_db`);
127
+ } catch (e) {
128
+ log.warn({ err: e }, `Migration from memory.db failed`);
129
+ try {
130
+ moduleDb.exec(`DETACH DATABASE main_db`);
131
+ } catch {
132
+ }
133
+ }
134
+ return totalMigrated;
135
+ }
136
+
137
+ export {
138
+ JOURNAL_SCHEMA,
139
+ USED_TRANSACTIONS_SCHEMA,
140
+ openModuleDb,
141
+ createDbWrapper,
142
+ migrateFromMainDb
143
+ };
@@ -1,6 +1,14 @@
1
+ import {
2
+ getErrorMessage
3
+ } from "./chunk-XBE4JB7C.js";
4
+ import {
5
+ createLogger
6
+ } from "./chunk-RCMD3U65.js";
7
+
1
8
  // src/agent/tools/telegram/gifts/get-my-gifts.ts
2
9
  import { Type } from "@sinclair/typebox";
3
10
  import { Api } from "telegram";
11
+ var log = createLogger("Tools");
4
12
  var giftCatalogCache = null;
5
13
  var CATALOG_CACHE_TTL_MS = 5 * 60 * 1e3;
6
14
  function extractEmoji(sticker) {
@@ -179,8 +187,8 @@ var telegramGetMyGiftsExecutor = async (params, context) => {
179
187
  const unlimited = gifts.filter((g) => !g.isLimited);
180
188
  const collectibles = gifts.filter((g) => g.isCollectible);
181
189
  const viewingLabel = viewSender ? `sender (${context.senderId})` : userId || "self";
182
- console.log(
183
- `\u{1F4E6} get_my_gifts: viewing ${viewingLabel}, found ${gifts.length} gifts (${collectibles.length} collectibles)`
190
+ log.info(
191
+ `get_my_gifts: viewing ${viewingLabel}, found ${gifts.length} gifts (${collectibles.length} collectibles)`
184
192
  );
185
193
  return {
186
194
  success: true,
@@ -198,10 +206,10 @@ var telegramGetMyGiftsExecutor = async (params, context) => {
198
206
  }
199
207
  };
200
208
  } catch (error) {
201
- console.error("Error getting gifts:", error);
209
+ log.error({ err: error }, "Error getting gifts");
202
210
  return {
203
211
  success: false,
204
- error: error instanceof Error ? error.message : String(error)
212
+ error: getErrorMessage(error)
205
213
  };
206
214
  }
207
215
  };
@@ -0,0 +1,20 @@
1
+ import {
2
+ DEFAULT_FETCH_TIMEOUT_MS
3
+ } from "./chunk-4DU3C27M.js";
4
+
5
+ // src/utils/fetch.ts
6
+ var DEFAULT_TIMEOUT_MS = DEFAULT_FETCH_TIMEOUT_MS;
7
+ function fetchWithTimeout(url, init) {
8
+ const { timeoutMs = DEFAULT_TIMEOUT_MS, ...fetchInit } = init ?? {};
9
+ if (fetchInit.signal) {
10
+ return fetch(url, fetchInit);
11
+ }
12
+ return fetch(url, {
13
+ ...fetchInit,
14
+ signal: AbortSignal.timeout(timeoutMs)
15
+ });
16
+ }
17
+
18
+ export {
19
+ fetchWithTimeout
20
+ };
@@ -0,0 +1,8 @@
1
+ // src/utils/errors.ts
2
+ function getErrorMessage(error) {
3
+ return error instanceof Error ? error.message : String(error);
4
+ }
5
+
6
+ export {
7
+ getErrorMessage
8
+ };
@@ -0,0 +1,58 @@
1
+ import {
2
+ fetchWithTimeout
3
+ } from "./chunk-VAUJSSD3.js";
4
+
5
+ // src/constants/api-endpoints.ts
6
+ var TONAPI_BASE_URL = "https://tonapi.io/v2";
7
+ var _tonapiKey;
8
+ function setTonapiKey(key) {
9
+ _tonapiKey = key;
10
+ }
11
+ function tonapiHeaders() {
12
+ const headers = { Accept: "application/json" };
13
+ if (_tonapiKey) {
14
+ headers["Authorization"] = `Bearer ${_tonapiKey}`;
15
+ }
16
+ return headers;
17
+ }
18
+ var TONAPI_MAX_RPS = 5;
19
+ var _tonapiTimestamps = [];
20
+ async function waitForTonapiSlot() {
21
+ const clean = () => {
22
+ const cutoff = Date.now() - 1e3;
23
+ while (_tonapiTimestamps.length > 0 && _tonapiTimestamps[0] <= cutoff) {
24
+ _tonapiTimestamps.shift();
25
+ }
26
+ };
27
+ clean();
28
+ if (_tonapiTimestamps.length >= TONAPI_MAX_RPS) {
29
+ const waitMs = _tonapiTimestamps[0] + 1e3 - Date.now() + 50;
30
+ if (waitMs > 0) await new Promise((r) => setTimeout(r, waitMs));
31
+ clean();
32
+ }
33
+ _tonapiTimestamps.push(Date.now());
34
+ }
35
+ async function tonapiFetch(path, init) {
36
+ await waitForTonapiSlot();
37
+ return fetchWithTimeout(`${TONAPI_BASE_URL}${path}`, {
38
+ ...init,
39
+ headers: { ...tonapiHeaders(), ...init?.headers }
40
+ });
41
+ }
42
+ var STONFI_API_BASE_URL = "https://api.ston.fi/v1";
43
+ var GECKOTERMINAL_API_URL = "https://api.geckoterminal.com/api/v2";
44
+ var COINGECKO_API_URL = "https://api.coingecko.com/api/v3";
45
+ var OPENAI_TTS_URL = "https://api.openai.com/v1/audio/speech";
46
+ var ELEVENLABS_TTS_URL = "https://api.elevenlabs.io/v1/text-to-speech";
47
+ var VOYAGE_API_URL = "https://api.voyageai.com/v1";
48
+
49
+ export {
50
+ setTonapiKey,
51
+ tonapiFetch,
52
+ STONFI_API_BASE_URL,
53
+ GECKOTERMINAL_API_URL,
54
+ COINGECKO_API_URL,
55
+ OPENAI_TTS_URL,
56
+ ELEVENLABS_TTS_URL,
57
+ VOYAGE_API_URL
58
+ };