Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -57,8 +57,8 @@ export async function renderModelPicker(
messageID?: number,
current?: string,
): Promise<void> {
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)
Expand Down
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ export default Plugin.define({
}
const loadLimits = async (): Promise<void> => {
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) {
Expand Down
11 changes: 7 additions & 4 deletions src/loop.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down