companionbot 0.8.0 → 0.8.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.
|
@@ -49,41 +49,53 @@ async function sendStreamingResponse(ctx, messages, systemPrompt, modelId) {
|
|
|
49
49
|
let lastUpdate = Date.now();
|
|
50
50
|
const UPDATE_INTERVAL = 500; // 0.5초마다 업데이트 (Telegram rate limit 고려)
|
|
51
51
|
let lastText = "";
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
52
|
+
try {
|
|
53
|
+
const result = await chatSmart(messages, systemPrompt, modelId, async (_chunk, accumulated) => {
|
|
54
|
+
const now = Date.now();
|
|
55
|
+
// 0.5초마다 또는 충분히 변경되었을 때 업데이트
|
|
56
|
+
if (now - lastUpdate > UPDATE_INTERVAL && accumulated !== lastText) {
|
|
57
|
+
try {
|
|
58
|
+
await ctx.api.editMessageText(chatId, messageId, accumulated + " ▌");
|
|
59
|
+
lastUpdate = now;
|
|
60
|
+
lastText = accumulated;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// rate limit 등 무시
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
// 도구를 사용한 경우 스트리밍이 안됐으므로 새 응답 전송
|
|
68
|
+
if (result.usedTools) {
|
|
69
|
+
// placeholder 메시지를 최종 결과로 교체
|
|
56
70
|
try {
|
|
57
|
-
await ctx.api.editMessageText(chatId, messageId,
|
|
58
|
-
lastUpdate = now;
|
|
59
|
-
lastText = accumulated;
|
|
71
|
+
await ctx.api.editMessageText(chatId, messageId, result.text);
|
|
60
72
|
}
|
|
61
73
|
catch {
|
|
62
|
-
//
|
|
74
|
+
// 실패시 새 메시지로 전송
|
|
75
|
+
await ctx.api.deleteMessage(chatId, messageId);
|
|
76
|
+
await ctx.reply(result.text);
|
|
63
77
|
}
|
|
78
|
+
return result.text;
|
|
64
79
|
}
|
|
65
|
-
|
|
66
|
-
// 도구를 사용한 경우 스트리밍이 안됐으므로 새 응답 전송
|
|
67
|
-
if (result.usedTools) {
|
|
68
|
-
// placeholder 메시지를 최종 결과로 교체
|
|
80
|
+
// 최종 메시지 업데이트 (커서 제거)
|
|
69
81
|
try {
|
|
70
82
|
await ctx.api.editMessageText(chatId, messageId, result.text);
|
|
71
83
|
}
|
|
72
84
|
catch {
|
|
73
|
-
//
|
|
74
|
-
await ctx.api.deleteMessage(chatId, messageId);
|
|
75
|
-
await ctx.reply(result.text);
|
|
85
|
+
// 이미 동일 텍스트면 에러 발생 가능 - 무시
|
|
76
86
|
}
|
|
77
87
|
return result.text;
|
|
78
88
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
89
|
+
catch (error) {
|
|
90
|
+
// 에러 발생 시 placeholder 삭제
|
|
91
|
+
try {
|
|
92
|
+
await ctx.api.deleteMessage(chatId, messageId);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
// 삭제 실패해도 계속 진행
|
|
96
|
+
}
|
|
97
|
+
throw error; // 에러 재전파
|
|
85
98
|
}
|
|
86
|
-
return result.text;
|
|
87
99
|
}
|
|
88
100
|
/**
|
|
89
101
|
* 메시지 핸들러들을 봇에 등록합니다.
|
|
@@ -223,11 +235,11 @@ export function registerMessageHandlers(bot) {
|
|
|
223
235
|
else if (errorMsg.includes("timeout") || errorMsg.includes("ETIMEDOUT")) {
|
|
224
236
|
await ctx.reply("응답이 너무 오래 걸려서 중단됐어. 다시 시도해줄래?");
|
|
225
237
|
}
|
|
226
|
-
else if (errorMsg.includes("
|
|
238
|
+
else if (errorMsg.includes("context_length") || errorMsg.includes("too many tokens") || errorMsg.includes("maximum context")) {
|
|
227
239
|
await ctx.reply("대화가 너무 길어졌어. /compact 로 정리하고 다시 시도해줘!");
|
|
228
240
|
}
|
|
229
241
|
else {
|
|
230
|
-
await ctx.reply(
|
|
242
|
+
await ctx.reply(`문제가 생겼어: ${errorMsg.slice(0, 100)}`);
|
|
231
243
|
}
|
|
232
244
|
}
|
|
233
245
|
});
|