a614acbbe490b484656c72dae3d93a600368f5c8
- Author
- TheEdgeOfRage <git@theedgeofrage.com>
- Committer
- TheEdgeOfRage <git@theedgeofrage.com>
- Date
Message
Diff
This diff is truncated to protect this page.
1diff --git a/src/opencode.ts b/src/opencode.ts
2index 855a5913961dd6d51b477aa2c4ef1a127c4f4b46..f3175f7bcb93f9d5d441dbe6b9bffb74747074c1 100644
3--- a/src/opencode.ts
4+++ b/src/opencode.ts
5@@ -57,7 +57,8 @@ export async function getOrCreateSession(
6 }
7
8 const result = await client.session.create();
9- if (result.error) throw new Error(`failed to create session: ${result.error}`);
10+ if (result.error)
11+ throw new Error(`failed to create session: ${result.error}`);
12
13 const sessionId = result.data.id;
14 chatSessions.set(chatId, sessionId);
15@@ -67,7 +68,8 @@ export async function getOrCreateSession(
16
17 export async function listAgents(): Promise<string[]> {
18 const result = await client.app.agents();
19- if (result.error) throw new Error(`list agents failed: ${JSON.stringify(result.error)}`);
20+ if (result.error)
21+ throw new Error(`list agents failed: ${JSON.stringify(result.error)}`);
22 if (!result.data) return [];
23 return result.data.map((a) => a.name);
24 }
25@@ -89,14 +91,16 @@ export async function sendPrompt(
26 path: { id: sessionId },
27 body: { parts: [{ type: "text", text }], ...(agent ? { agent } : {}) },
28 });
29- if (result.error) throw new Error(`prompt failed: ${JSON.stringify(result.error)}`);
30+ if (result.error)
31+ throw new Error(`prompt failed: ${JSON.stringify(result.error)}`);
32
33 return result.data.parts;
34 }
35
36 export async function createNewSession(chatId: number): Promise<string> {
37 const result = await client.session.create();
38- if (result.error) throw new Error(`failed to create session: ${result.error}`);
39+ if (result.error)
40+ throw new Error(`failed to create session: ${result.error}`);
41
42 const sessionId = result.data.id;
43 chatSessions.set(chatId, sessionId);
44@@ -127,28 +131,12 @@ export function switchSession(chatId: number, sessionId: string): void {
45 saveSessions();
46 }
47
48-export async function getSessionMessages(
49- sessionId: string,
50- limit = 10,
51-): Promise<Array<{ role: string; parts: Part[] }>> {
52- const result = await client.session.messages({
53- path: { id: sessionId },
54- query: { limit },
55- });
56- if (result.error) throw new Error(`list messages failed: ${JSON.stringify(result.error)}`);
57- if (!result.data) return [];
58-
59- return result.data.map((m) => ({
60- role: m.info.role,
61- parts: m.parts,
62- }));
63-}
64-
65 export async function abortSession(sessionId: string): Promise<void> {
66 const result = await client.session.abort({
67 path: { id: sessionId },
68 });
69- if (result.error) throw new Error(`abort failed: ${JSON.stringify(result.error)}`);
70+ if (result.error)
71+ throw new Error(`abort failed: ${JSON.stringify(result.error)}`);
72 }
73
74 export async function replyPermission(
75@@ -160,5 +148,6 @@ export async function replyPermission(
76 path: { id: sessionId, permissionID: permissionId },
77 body: { response },
78 });
79- if (result.error) throw new Error(`permission reply failed: ${JSON.stringify(result.error)}`);
80+ if (result.error)
81+ throw new Error(`permission reply failed: ${JSON.stringify(result.error)}`);
82 }
83diff --git a/src/telegram.ts b/src/telegram.ts
84index c0653568839d1a2da2a36bb84889d5fa0b9b7aa1..bfb3266c7b1e0d55dc01cca36c0bcaeea91f4133 100644
85--- a/src/telegram.ts
86+++ b/src/telegram.ts
87@@ -3,14 +3,18 @@ import type { Chat } from "grammy/types";
88 import type { Part } from "@opencode-ai/sdk";
89 import * as log from "./log.js";
90 import type { PermissionEvent } from "./events.js";
91-import { escapeMarkdownV2, formatParts, formatTextParts, splitMessage } from "./format.js";
92+import {
93+ escapeMarkdownV2,
94+ formatParts,
95+ formatTextParts,
96+ splitMessage,
97+} from "./format.js";
98 import {
99 getOrCreateSession,
100 createNewSession,
101 listSessions,
102 getSessionId,
103 switchSession,
104- getSessionMessages,
105 abortSession,
106 sendPrompt,
107 replyPermission,
108@@ -46,7 +50,10 @@ let channelChatId: number | null = null;
109 // Telegram callback data is limited to 64 bytes. Permission IDs are too long,
110 // so we store them in a map keyed by a short incrementing counter.
111 let permCounter = 0;
112-const pendingPerms = new Map<string, { sessionId: string; permissionId: string }>();
113+const pendingPerms = new Map<
114+ string,
115+ { sessionId: string; permissionId: string }
116+>();
117
118 function formatPartsPreview(parts: Part[]): string {
119 const text = formatParts(parts);
120@@ -108,9 +115,12 @@ export function createBot(token: string, allowedUsers: number[]): Bot {
121 try {
122 const sessionId = await createNewSession(chatId);
123 log.info(`[session] created session=${sessionId} chat=${chatId}`);
124- await ctx.reply(`New session created: \`${escapeMarkdownV2(sessionId)}\``, {
125- parse_mode: "MarkdownV2",
126- });
127+ await ctx.reply(
128+ `New session created: \`${escapeMarkdownV2(sessionId)}\``,
129+ {
130+ parse_mode: "MarkdownV2",
131+ },
132+ );
133 } catch (err) {
134 log.error(`[cmd] /new error:`, err);
135 await ctx.reply(`Failed to create session: ${String(err)}`);
136@@ -143,7 +153,8 @@ export function createBot(token: string, allowedUsers: number[]): Bot {
137
138 await ctx.reply(lines.join("\n"), {
139 parse_mode: "MarkdownV2",
140- reply_markup: keyboard.inline_keyboard.length > 0 ? keyboard : undefined,
141+ reply_markup:
142+ keyboard.inline_keyboard.length > 0 ? keyboard : undefined,
143 });
144 } catch (err) {
145 log.error(`[cmd] /sessions error:`, err);
146@@ -199,11 +210,17 @@ export function createBot(token: string, allowedUsers: number[]): Bot {
147 return;
148 }
149 pendingPerms.delete(key!);
150- const responseMap: Record<string, "once" | "always" | "reject"> = { a: "once", s: "always", d: "reject" };
151+ const responseMap: Record<string, "once" | "always" | "reject"> = {
152+ a: "once",
153+ s: "always",
154+ d: "reject",
155+ };
156 const permResponse = responseMap[action] ?? "reject";
157 try {
158 await replyPermission(perm.sessionId, perm.permissionId, permResponse);
159- log.info(`[permission] ${permResponse} session=${perm.sessionId} perm=${perm.permissionId}`);
160+ log.info(
161+ `[permission] ${permResponse} session=${perm.sessionId} perm=${perm.permissionId}`,
162+ );
163 await ctx.answerCallbackQuery({ text: `Permission: ${permResponse}` });
164 await ctx.deleteMessage();
165 } catch (err) {
166@@ -247,7 +264,9 @@ export function createBot(token: string, allowedUsers: number[]): Bot {
167 }
168 setAgent(chatId, match);
169 log.info(`[agent] set agent=${match} chat=${chatId}`);
170- await ctx.reply(`Agent set to \`${escapeMarkdownV2(match)}\`\\.`, { parse_mode: "MarkdownV2" });
171+ await ctx.reply(`Agent set to \`${escapeMarkdownV2(match)}\`\\.`, {
172+ parse_mode: "MarkdownV2",
173+ });
174 } catch (err) {
175 log.error(`[cmd] /agent error:`, err);
176 await ctx.reply(`Error: ${String(err)}`);
177@@ -261,17 +280,24 @@ export function createBot(token: string, allowedUsers: number[]): Bot {
178 try {
179 const { sessionId, fallback } = await getOrCreateSession(chatId);
180 if (fallback) {
181- log.info(`[session] previous session gone, created new session=${sessionId} chat=${chatId}`);
182+ log.info(
183+ `[session] previous session gone, created new session=${sessionId} chat=${chatId}`,
184+ );
185 await ctx.reply(
186- escapeMarkdownV2("Previous session no longer available. Started a new session."),