Project Files
src / tools / current-date-tool.ts
/**
* Current Date tool factory.
*/
import { tool, type Tool, type ToolsProviderController } from "@lmstudio/sdk"
import { z } from "zod"
import { resolveConfig } from "../config/resolve-config"
import { formatToolError } from "../errors"
import { formatInTimezone, resolveHostTimezone, validateTimezone } from "../timezone"
/**
* Build the Current Date tool: returns the current instant rendered in a caller-selected or
* plugin-configured IANA timezone.
*
* @param ctl - Tools provider controller supplied by the LM Studio SDK.
* @returns The configured Current Date tool.
*/
export function createCurrentDateTool(ctl: ToolsProviderController): Tool {
return tool({
name: "Current Date",
description:
"Get the current date and time. Call this tool first when the user makes queries containing phrases like 'current,' 'right now,' 'today,' 'recently,' 'this year,' etc. Since your knowledge cutoff lags the current date significantly, it is STRONGLY recommended that you use this tool when responding to time-sensitive queries and when researching current events (e.g. before performing a web search) to ensure that you reference the current date correctly.",
parameters: {
timezone: z.string().optional().describe("Optional IANA timezone name to render the current instant in."),
},
/**
* Resolve the requested timezone, validate it, and return both ISO and local renderings of the
* current instant.
*
* @param arguments_ - Validated tool parameters.
* @param context - Runtime tool context supplied by the SDK.
* @returns The structured current-date record or a user-facing error string.
*/
implementation: (arguments_, context) => {
const { timezone: requestedTimezone } = arguments_
context.status("Resolving current date...")
try {
const { defaultTimezone } = resolveConfig(ctl)
const timezone = requestedTimezone ?? defaultTimezone ?? resolveHostTimezone()
validateTimezone(timezone)
const now = new Date()
return {
iso: now.toISOString(),
local: formatInTimezone(now, timezone),
timezone,
unix: Math.floor(now.getTime() / 1000),
}
} catch (error) {
return formatToolError(error, context, "current-date")
}
},
})
}