src / toolsProvider.ts
import { tool, type ToolsProviderController } from "@lmstudio/sdk";
export async function toolsProvider(ctl: ToolsProviderController) {
const resolvedOptions = Intl.DateTimeFormat().resolvedOptions();
const timezone = resolvedOptions.timeZone;
function getTimezoneOffset(): string {
const tz = resolvedOptions.timeZone;
const date = new Date();
const utcDate = new Date(date.toLocaleString("en-US", { timeZone: "UTC" }));
const tzDate = new Date(date.toLocaleString("en-US", { timeZone: tz }));
const diffMs = tzDate.getTime() - utcDate.getTime();
const diffHours = Math.floor(Math.abs(diffMs) / 3600000);
const diffMins = Math.floor((Math.abs(diffMs) % 3600000) / 60000);
const sign = diffMs >= 0 ? "+" : "-";
return `${sign}${diffHours.toString().padStart(2, "0")}:${diffMins.toString().padStart(2, "0")}`;
}
const getDatetimeTool = tool({
name: "get_current_datetime",
description:
"Get the current system date and time. Use this tool whenever the user asks about the current date, time, day of the week, or any time-sensitive information.",
parameters: {},
implementation: async () => {
const currentDate = new Date();
const locale = undefined as unknown as string;
return {
datetime: currentDate.toISOString(),
local_datetime: new Intl.DateTimeFormat(locale, {
year: "numeric",
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
}).format(currentDate),
local_date: new Intl.DateTimeFormat(locale, {
year: "numeric",
month: "numeric",
day: "numeric",
}).format(currentDate),
local_time: new Intl.DateTimeFormat(locale, {
hour: "numeric",
minute: "numeric",
second: "numeric",
}).format(currentDate),
day_of_week: new Intl.DateTimeFormat(locale, { weekday: "long" }).format(currentDate),
timezone: timezone,
timezone_offset: getTimezoneOffset(),
unix_timestamp: Math.floor(currentDate.getTime() / 1000),
};
},
});
return [getDatetimeTool];
}