qwen-alpha 1.0.13 → 1.0.15

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qwen-alpha",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "Telegram bot for Qwen Code integration — AI-powered code review, bug detection, and code generation",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -55,6 +55,19 @@ async function messageHandler(ctx) {
55
55
  let contextMessages = [];
56
56
  let session = ctx.state.session;
57
57
 
58
+ // Если сессии нет в группе - создаём новую
59
+ if (!session && !isPrivate) {
60
+ session = sessionService.create({
61
+ userId,
62
+ chatId,
63
+ rootMessageId: ctx.message.message_id,
64
+ chatType: ctx.chat.type,
65
+ chatTitle: ctx.chat.title,
66
+ });
67
+ ctx.state.session = session;
68
+ statsService.incrementSessionCreated();
69
+ }
70
+
58
71
  if (session) {
59
72
  // Если это reply на сообщение в сессии
60
73
  const replyToMessageId = ctx.message?.reply_to_message?.message_id;
@@ -85,8 +98,8 @@ async function messageHandler(ctx) {
85
98
  // Форматирование ответа
86
99
  let responseText = result;
87
100
 
88
- // В группах добавляем упоминание если это первый ответ
89
- if (!isPrivate && ctx.message?.reply_to_message) {
101
+ // В группах добавляем упоминание только если отвечаем на сообщение пользователя (не бота)
102
+ if (!isPrivate && ctx.message?.reply_to_message && !ctx.message.reply_to_message.from.is_bot) {
90
103
  const originalUser = ctx.message.reply_to_message.from;
91
104
  if (originalUser?.username) {
92
105
  responseText = `@${originalUser.username} ${responseText}`;
@@ -124,9 +137,18 @@ async function messageHandler(ctx) {
124
137
  text: prompt,
125
138
  type: 'user_question',
126
139
  });
127
-
128
- // Добавляем ответ бота (будет добавлен после отправки)
129
- // sessionService.addMessage({...})
140
+
141
+ // Добавляем ответ бота
142
+ const botMessageId = ctx.message.message_id + 1; // Приблизительно
143
+ sessionService.addMessage({
144
+ sessionId: session.session_id,
145
+ chatId,
146
+ messageId: botMessageId,
147
+ userId: 'bot',
148
+ text: responseText,
149
+ type: 'bot_response',
150
+ parent_id: ctx.message.message_id,
151
+ });
130
152
  }
131
153
 
132
154
  // Обновление статистики
@@ -38,19 +38,29 @@ async function sessionMiddleware(ctx, next) {
38
38
  ctx.state.sessionKey = sessionKey;
39
39
  }
40
40
  } else {
41
- // Групповой чат - поиск сессии по reply
41
+ // Групповой чат - поиск сессии по reply или создание новой
42
42
  const replyToMessageId = ctx.message?.reply_to_message?.message_id;
43
-
43
+
44
44
  if (replyToMessageId) {
45
45
  // Поиск сессии по сообщению, на которое ответили
46
46
  const session = sessionService.findByMessage(chatId, replyToMessageId);
47
-
47
+
48
48
  if (session) {
49
49
  ctx.state.session = session;
50
50
  ctx.state.sessionKey = `chat:${chatId}`;
51
51
  ctx.state.replyToSession = true;
52
52
  }
53
53
  }
54
+
55
+ // Если сессии нет, ищем последнюю активную сессию чата
56
+ if (!ctx.state.session) {
57
+ const chatSessions = sessionService.getChatSessions(chatId);
58
+ const activeSession = chatSessions.find(s => s.status === 'active');
59
+ if (activeSession) {
60
+ ctx.state.session = activeSession;
61
+ ctx.state.sessionKey = `chat:${chatId}`;
62
+ }
63
+ }
54
64
  }
55
65
 
56
66
  ctx.state.isPrivate = isPrivate;
@@ -84,15 +84,18 @@ class QwenService {
84
84
  .map(msg => `${msg.role === 'assistant' ? 'Assistant' : 'User'}: ${msg.content}`)
85
85
  .join('\n');
86
86
 
87
- fullPrompt = `${contextText}\n\nUser: ${code}`;
87
+ // Явно указываем что это продолжение диалога
88
+ fullPrompt = `Продолжи диалог. Контекст:\n${contextText}\n\nUser: ${code}`;
88
89
  }
89
90
 
90
91
  // Создаём временный файл с промптом
91
92
  const tempFile = path.join(os.tmpdir(), `qwen-alpha-${Date.now()}-${Math.random().toString(36).substr(2, 9)}.txt`);
92
93
  fs.writeFileSync(tempFile, fullPrompt, 'utf-8');
93
94
 
94
- // Команда для Qwen с чтением из файла
95
- const command = `cat '${tempFile}' | qwen -p "Проанализируй код и дай рекомендации" -o json`;
95
+ // Команда для Qwen без системного промпта если есть контекст
96
+ const command = contextMessages.length > 0
97
+ ? `cat '${tempFile}' | qwen -o json`
98
+ : `cat '${tempFile}' | qwen -p "Проанализируй код и дай рекомендации" -o json`;
96
99
 
97
100
  logger.debug({ codeLength: code.length, contextLength: contextMessages.length, tempFile }, 'Running Qwen analysis');
98
101