@shliamb/dsh-telegram-bot 0.0.0-stage → 0.2.1
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 +310 -2
- package/README.ru.md +311 -0
- package/cordis.patch.yml +6 -0
- package/lib/client.js +275 -0
- package/lib/client.js.map +1 -0
- package/lib/index.d.ts +106 -0
- package/lib/index.js +2020 -0
- package/lib/logging/index.d.ts +28 -0
- package/lib/logging/index.js +106 -0
- package/lib/state/index.d.ts +69 -0
- package/lib/state/index.js +94 -0
- package/package.json +83 -4
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Persistent, redacted error log for the Telegram bot.
|
|
3
|
+
*
|
|
4
|
+
* The Host already receives every line through `ctx.logger`; this module also
|
|
5
|
+
* appends structured JSONL to `$DSH_HOME/logs/telegram-bot.log` so failures can
|
|
6
|
+
* be inspected later or downloaded from Telegram with `/logs`.
|
|
7
|
+
*/
|
|
8
|
+
export type LogLevel = 'info' | 'warn' | 'error';
|
|
9
|
+
export interface LogFields {
|
|
10
|
+
readonly [key: string]: string | number | boolean | undefined;
|
|
11
|
+
}
|
|
12
|
+
export interface PluginLogger {
|
|
13
|
+
readonly path: string;
|
|
14
|
+
info(event: string, message: string, fields?: LogFields): void;
|
|
15
|
+
warn(event: string, message: string, fields?: LogFields): void;
|
|
16
|
+
error(event: string, message: string, fields?: LogFields): void;
|
|
17
|
+
tail(lines: number): string;
|
|
18
|
+
close(): void;
|
|
19
|
+
}
|
|
20
|
+
interface LoggerHost {
|
|
21
|
+
readonly logger: {
|
|
22
|
+
info(message: string): void;
|
|
23
|
+
warn(message: string): void;
|
|
24
|
+
error(message: string): void;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export declare function createPluginLogger(host: LoggerHost): PluginLogger;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Persistent, redacted error log for the Telegram bot.
|
|
3
|
+
*
|
|
4
|
+
* The Host already receives every line through `ctx.logger`; this module also
|
|
5
|
+
* appends structured JSONL to `$DSH_HOME/logs/telegram-bot.log` so failures can
|
|
6
|
+
* be inspected later or downloaded from Telegram with `/logs`.
|
|
7
|
+
*/
|
|
8
|
+
import { appendFileSync, chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync, } from 'node:fs';
|
|
9
|
+
import { homedir } from 'node:os';
|
|
10
|
+
import { dirname, join } from 'node:path';
|
|
11
|
+
const MAX_BYTES = 2 * 1024 * 1024;
|
|
12
|
+
const KEEP = 3;
|
|
13
|
+
const TOKEN_RE = /\b\d{6,}:[A-Za-z0-9_-]{30,}\b/g;
|
|
14
|
+
function redact(value) {
|
|
15
|
+
return value.replace(TOKEN_RE, '<redacted-token>');
|
|
16
|
+
}
|
|
17
|
+
function logPath() {
|
|
18
|
+
const home = process.env.DSH_HOME ?? join(homedir(), '.dsh');
|
|
19
|
+
return join(home, 'logs', 'telegram-bot.log');
|
|
20
|
+
}
|
|
21
|
+
function rotate(path) {
|
|
22
|
+
for (let index = KEEP - 1; index >= 1; index -= 1) {
|
|
23
|
+
const from = `${path}.${index}`;
|
|
24
|
+
if (!existsSync(from))
|
|
25
|
+
continue;
|
|
26
|
+
const to = `${path}.${index + 1}`;
|
|
27
|
+
if (index === KEEP - 1)
|
|
28
|
+
rmSync(to, { force: true });
|
|
29
|
+
renameSync(from, to);
|
|
30
|
+
}
|
|
31
|
+
if (existsSync(path))
|
|
32
|
+
renameSync(path, `${path}.1`);
|
|
33
|
+
}
|
|
34
|
+
export function createPluginLogger(host) {
|
|
35
|
+
const path = logPath();
|
|
36
|
+
let bytes = 0;
|
|
37
|
+
let fileOk = true;
|
|
38
|
+
try {
|
|
39
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
40
|
+
if (!existsSync(path))
|
|
41
|
+
writeFileSync(path, '', { mode: 0o600 });
|
|
42
|
+
chmodSync(path, 0o600);
|
|
43
|
+
bytes = statSync(path).size;
|
|
44
|
+
if (bytes > MAX_BYTES) {
|
|
45
|
+
rotate(path);
|
|
46
|
+
bytes = 0;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
fileOk = false;
|
|
51
|
+
}
|
|
52
|
+
const write = (level, event, message, fields) => {
|
|
53
|
+
const text = redact(message);
|
|
54
|
+
const line = `${JSON.stringify({
|
|
55
|
+
time: new Date().toISOString(),
|
|
56
|
+
level,
|
|
57
|
+
scope: 'telegram-bot',
|
|
58
|
+
event,
|
|
59
|
+
message: text,
|
|
60
|
+
...fields,
|
|
61
|
+
})}\n`;
|
|
62
|
+
const mirror = `[telegram-bot] ${event}: ${text}`;
|
|
63
|
+
try {
|
|
64
|
+
if (level === 'error')
|
|
65
|
+
host.logger.error(mirror);
|
|
66
|
+
else if (level === 'warn')
|
|
67
|
+
host.logger.warn(mirror);
|
|
68
|
+
else
|
|
69
|
+
host.logger.info(mirror);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
// A broken Host logger must not break Telegram handling.
|
|
73
|
+
}
|
|
74
|
+
if (!fileOk)
|
|
75
|
+
return;
|
|
76
|
+
try {
|
|
77
|
+
if (bytes + Buffer.byteLength(line) > MAX_BYTES) {
|
|
78
|
+
rotate(path);
|
|
79
|
+
bytes = 0;
|
|
80
|
+
}
|
|
81
|
+
appendFileSync(path, line, { mode: 0o600 });
|
|
82
|
+
bytes += Buffer.byteLength(line);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
fileOk = false;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
return {
|
|
89
|
+
path,
|
|
90
|
+
info: (event, message, fields) => write('info', event, message, fields),
|
|
91
|
+
warn: (event, message, fields) => write('warn', event, message, fields),
|
|
92
|
+
error: (event, message, fields) => write('error', event, message, fields),
|
|
93
|
+
tail: (lines) => {
|
|
94
|
+
try {
|
|
95
|
+
if (!existsSync(path))
|
|
96
|
+
return '';
|
|
97
|
+
const data = readFileSync(path, 'utf8').trimEnd().split('\n');
|
|
98
|
+
return data.slice(-Math.max(1, lines)).join('\n');
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return '';
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
close: () => undefined,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable chat → workspace/session bindings.
|
|
3
|
+
*
|
|
4
|
+
* The preferred store is the Host's `telegram_bridge` domain (JSON backend).
|
|
5
|
+
* When the storage domain is unavailable the plugin keeps working with an
|
|
6
|
+
* in-memory store, so a storage outage never disables the bot.
|
|
7
|
+
*/
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
export interface ChatBinding {
|
|
10
|
+
readonly chatId: string;
|
|
11
|
+
readonly userId: number;
|
|
12
|
+
readonly activeWorkspaceId?: string;
|
|
13
|
+
readonly activeSessionId?: string;
|
|
14
|
+
readonly locale?: 'ru' | 'en';
|
|
15
|
+
readonly sound?: boolean;
|
|
16
|
+
readonly updatedAt: number;
|
|
17
|
+
}
|
|
18
|
+
export declare const ChatBindingSchema: z.ZodObject<{
|
|
19
|
+
chatId: z.ZodString;
|
|
20
|
+
userId: z.ZodNumber;
|
|
21
|
+
activeWorkspaceId: z.ZodOptional<z.ZodString>;
|
|
22
|
+
activeSessionId: z.ZodOptional<z.ZodString>;
|
|
23
|
+
locale: z.ZodOptional<z.ZodEnum<{
|
|
24
|
+
ru: "ru";
|
|
25
|
+
en: "en";
|
|
26
|
+
}>>;
|
|
27
|
+
sound: z.ZodOptional<z.ZodBoolean>;
|
|
28
|
+
updatedAt: z.ZodNumber;
|
|
29
|
+
}, z.core.$strip>;
|
|
30
|
+
interface KvTable {
|
|
31
|
+
get(key: string): ChatBinding | undefined;
|
|
32
|
+
put(key: string, value: ChatBinding): Promise<void>;
|
|
33
|
+
entries(): IterableIterator<[string, ChatBinding]>;
|
|
34
|
+
}
|
|
35
|
+
interface DomainHandle {
|
|
36
|
+
table(name: string): KvTable;
|
|
37
|
+
close(): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
interface DomainFacility {
|
|
40
|
+
open(spec: {
|
|
41
|
+
readonly name: string;
|
|
42
|
+
readonly version: number;
|
|
43
|
+
readonly invalidRecords?: 'backup-and-skip';
|
|
44
|
+
readonly tables: Record<string, {
|
|
45
|
+
readonly valueSchema: {
|
|
46
|
+
parse(value: unknown): unknown;
|
|
47
|
+
};
|
|
48
|
+
}>;
|
|
49
|
+
}): Promise<DomainHandle>;
|
|
50
|
+
}
|
|
51
|
+
interface Logger {
|
|
52
|
+
info(event: string, message: string, fields?: Record<string, unknown>): void;
|
|
53
|
+
warn(event: string, message: string, fields?: Record<string, unknown>): void;
|
|
54
|
+
error(event: string, message: string, fields?: Record<string, unknown>): void;
|
|
55
|
+
}
|
|
56
|
+
export declare class BindingStore {
|
|
57
|
+
private readonly logger;
|
|
58
|
+
private readonly memory;
|
|
59
|
+
private domain;
|
|
60
|
+
private table;
|
|
61
|
+
private constructor();
|
|
62
|
+
static open(facility: DomainFacility | undefined, logger: Logger): Promise<BindingStore>;
|
|
63
|
+
get(chatId: number | string): ChatBinding | undefined;
|
|
64
|
+
set(binding: ChatBinding): Promise<void>;
|
|
65
|
+
entries(): ChatBinding[];
|
|
66
|
+
sessionChat(sessionId: string): string | undefined;
|
|
67
|
+
close(): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
export {};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable chat → workspace/session bindings.
|
|
3
|
+
*
|
|
4
|
+
* The preferred store is the Host's `telegram_bridge` domain (JSON backend).
|
|
5
|
+
* When the storage domain is unavailable the plugin keeps working with an
|
|
6
|
+
* in-memory store, so a storage outage never disables the bot.
|
|
7
|
+
*/
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
export const ChatBindingSchema = z.object({
|
|
10
|
+
chatId: z.string().min(1),
|
|
11
|
+
userId: z.number().int().positive(),
|
|
12
|
+
activeWorkspaceId: z.string().min(1).optional(),
|
|
13
|
+
activeSessionId: z.string().min(1).optional(),
|
|
14
|
+
locale: z.enum(['ru', 'en']).optional(),
|
|
15
|
+
sound: z.boolean().optional(),
|
|
16
|
+
updatedAt: z.number().int().nonnegative(),
|
|
17
|
+
});
|
|
18
|
+
function messageOf(error) {
|
|
19
|
+
return error instanceof Error ? error.message : String(error);
|
|
20
|
+
}
|
|
21
|
+
export class BindingStore {
|
|
22
|
+
logger;
|
|
23
|
+
memory = new Map();
|
|
24
|
+
domain;
|
|
25
|
+
table;
|
|
26
|
+
constructor(logger) {
|
|
27
|
+
this.logger = logger;
|
|
28
|
+
}
|
|
29
|
+
static async open(facility, logger) {
|
|
30
|
+
const store = new BindingStore(logger);
|
|
31
|
+
if (facility === undefined) {
|
|
32
|
+
logger.warn('state-unavailable', 'storageDomain is unavailable; using memory-only bindings');
|
|
33
|
+
return store;
|
|
34
|
+
}
|
|
35
|
+
let lastError;
|
|
36
|
+
for (let attempt = 0; attempt < 30; attempt += 1) {
|
|
37
|
+
try {
|
|
38
|
+
const domain = await facility.open({
|
|
39
|
+
name: 'telegram_bridge',
|
|
40
|
+
version: 1,
|
|
41
|
+
invalidRecords: 'backup-and-skip',
|
|
42
|
+
tables: { chats: { valueSchema: ChatBindingSchema } },
|
|
43
|
+
});
|
|
44
|
+
store.domain = domain;
|
|
45
|
+
store.table = domain.table('chats');
|
|
46
|
+
for (const [key, value] of store.table.entries())
|
|
47
|
+
store.memory.set(key, value);
|
|
48
|
+
logger.info('state-ready', 'chat bindings loaded', { count: store.memory.size });
|
|
49
|
+
return store;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
lastError = error;
|
|
53
|
+
// HMR can mount the new runtime before the previous disposer closes the
|
|
54
|
+
// same domain; retry briefly so bindings survive a reload.
|
|
55
|
+
if (!messageOf(error).includes('already open') || attempt === 29)
|
|
56
|
+
break;
|
|
57
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
logger.warn('state-open-failed', `storage domain open failed: ${messageOf(lastError)}; using memory-only bindings`);
|
|
61
|
+
return store;
|
|
62
|
+
}
|
|
63
|
+
get(chatId) {
|
|
64
|
+
return this.memory.get(String(chatId));
|
|
65
|
+
}
|
|
66
|
+
async set(binding) {
|
|
67
|
+
this.memory.set(binding.chatId, binding);
|
|
68
|
+
if (this.table === undefined)
|
|
69
|
+
return;
|
|
70
|
+
try {
|
|
71
|
+
await this.table.put(binding.chatId, binding);
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
this.logger.error('state-write-failed', messageOf(error), { chatId: binding.chatId });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
entries() {
|
|
78
|
+
return [...this.memory.values()];
|
|
79
|
+
}
|
|
80
|
+
sessionChat(sessionId) {
|
|
81
|
+
for (const binding of this.memory.values()) {
|
|
82
|
+
if (binding.activeSessionId === sessionId)
|
|
83
|
+
return binding.chatId;
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
async close() {
|
|
88
|
+
const domain = this.domain;
|
|
89
|
+
this.domain = undefined;
|
|
90
|
+
this.table = undefined;
|
|
91
|
+
if (domain !== undefined)
|
|
92
|
+
await domain.close();
|
|
93
|
+
}
|
|
94
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,85 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shliamb/dsh-telegram-bot",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Telegram bridge for DeepSeek Harness",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"deepseek-harness",
|
|
7
|
+
"dsh",
|
|
8
|
+
"dsh-plugin",
|
|
9
|
+
"dsh-bundle",
|
|
10
|
+
"telegram",
|
|
11
|
+
"telegram-bot",
|
|
12
|
+
"bot",
|
|
13
|
+
"plugin",
|
|
14
|
+
"bundle"
|
|
15
|
+
],
|
|
16
|
+
"author": "shliamb",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/shliamb/HarnessBot.git"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/shliamb/HarnessBot#readme",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/shliamb/HarnessBot/issues"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"type": "module",
|
|
29
|
+
"main": "lib/index.js",
|
|
30
|
+
"types": "lib/index.d.ts",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./lib/index.d.ts",
|
|
34
|
+
"default": "./lib/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./client": {
|
|
37
|
+
"default": "./lib/client.js"
|
|
38
|
+
},
|
|
39
|
+
"./package.json": "./package.json"
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"lib/**/*.js",
|
|
43
|
+
"lib/*.js.map",
|
|
44
|
+
"lib/**/*.d.ts",
|
|
45
|
+
"cordis.patch.yml",
|
|
46
|
+
"README.md",
|
|
47
|
+
"README.ru.md",
|
|
48
|
+
"LICENSE"
|
|
49
|
+
],
|
|
50
|
+
"dsh": {
|
|
51
|
+
"bundle": {
|
|
52
|
+
"patch": "./cordis.patch.yml"
|
|
53
|
+
},
|
|
54
|
+
"client": {
|
|
55
|
+
"platform": "web",
|
|
56
|
+
"inject": [
|
|
57
|
+
"@deepseek-ai/dsh-api-remotes",
|
|
58
|
+
"@deepseek-ai/dsh-client-locale",
|
|
59
|
+
"@deepseek-ai/dsh-client-ui-plugin-manager",
|
|
60
|
+
"@deepseek-ai/dsh-client-ui-settings"
|
|
61
|
+
]
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"build": "tsc -p tsconfig.json && tsdown",
|
|
66
|
+
"check": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.client.json"
|
|
67
|
+
},
|
|
68
|
+
"dependencies": {
|
|
69
|
+
"@deepseek-ai/schemastery": "3.18.6-alpha.2",
|
|
70
|
+
"grammy": "^1.48.0",
|
|
71
|
+
"zod": "^4.6.7"
|
|
72
|
+
},
|
|
73
|
+
"devDependencies": {
|
|
74
|
+
"@types/node": "^24.19.4",
|
|
75
|
+
"@types/react": "~18.3.1",
|
|
76
|
+
"react": "^18.3.1",
|
|
77
|
+
"tsdown": "^0.22.2",
|
|
78
|
+
"typescript": "^6.0.3"
|
|
79
|
+
},
|
|
80
|
+
"engines": {
|
|
81
|
+
"node": ">=24.9.0",
|
|
82
|
+
"dsh": ">=0.2.2-alpha.3 <0.3.0"
|
|
83
|
+
},
|
|
84
|
+
"license": "MIT"
|
|
85
|
+
}
|