diff --git a/src/commands.ts b/src/commands.ts index 61b7b74..db238aa 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -24,8 +24,8 @@ export async function renderThinkingPicker( let variants: string[] = [] if (model) { try { - const list = await ctx.model.list() - const found = list.data.find( + const list = await ctx.model?.list?.() + const found = list?.data?.find( (m) => m.providerID === model.providerID && m.id === model.id, ) variants = (found?.variants ?? []).map((v) => v.id).slice(0, 12) @@ -57,8 +57,8 @@ export async function renderModelPicker( messageID?: number, current?: string, ): Promise { - const res = await ctx.model.list() - const models = res.data + const res = await ctx.model?.list?.() + const models = (res?.data ?? []) .filter((m) => m.status !== "deprecated") .map((m) => ({ label: `${m.providerID}/${m.id}`, key: `${m.providerID}|${m.id}` })) .filter((m) => m.key.length <= 56) diff --git a/src/index.ts b/src/index.ts index 041a4fb..1ca92f0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -105,10 +105,12 @@ export default Plugin.define({ } const loadLimits = async (): Promise => { try { - const list = await ctx.model.list() - for (const m of list.data) { - const limit = m.limit?.context - if (limit) modelLimits.set(`${m.providerID}/${m.id}`, limit) + const list = await ctx.model?.list?.() + if (list?.data) { + for (const m of list.data) { + const limit = m.limit?.context + if (limit) modelLimits.set(`${m.providerID}/${m.id}`, limit) + } } limitsLoaded = true } catch (err) { diff --git a/src/loop.ts b/src/loop.ts index 19f8f9c..64a32e1 100644 --- a/src/loop.ts +++ b/src/loop.ts @@ -1,4 +1,4 @@ -import { closeSync, mkdirSync, openSync, readFileSync, unlinkSync, writeSync, existsSync } from "node:fs" +import { closeSync, mkdirSync, openSync, readFileSync, unlinkSync, writeFileSync, writeSync, existsSync } from "node:fs" import { homedir } from "node:os" import { join } from "node:path" import { TelegramBot, type TelegramConfig, type TelegramMessage, type TelegramUpdate } from "./bot.js" @@ -41,7 +41,7 @@ const readLease = (): LeaseData | undefined => { } const writeLease = (data: LeaseData): void => { - writeSync(openSync(LEASE_FILE, "w"), JSON.stringify(data)) + writeFileSync(LEASE_FILE, JSON.stringify(data)) } export interface Lease { @@ -62,8 +62,11 @@ export function acquireLease(initialOffset = 0): Lease { for (let attempt = 0; attempt < 3; attempt++) { try { const fd = openSync(LEASE_FILE, "wx") - writeSync(fd, JSON.stringify({ pid: me, at: Date.now(), offset } satisfies LeaseData)) - closeSync(fd) + try { + writeSync(fd, JSON.stringify({ pid: me, at: Date.now(), offset } satisfies LeaseData)) + } finally { + closeSync(fd) + } return makeHeld() } catch (err) { if ((err as NodeJS.ErrnoException).code !== "EEXIST") throw err