ui: move get_datetime tool to frontend (#27255)
* ui: move get_datetime tool to frontend * clarify docs * server: drop the now unused ctime include strftime() and gmtime_r() were the only users, both went away with the get_datetime tool. Also make the renderer's catch inert: the browser executor always emits JSON, so a non-JSON result is no longer a date to display. --------- Co-authored-by: Pascal <admin@serveurperso.com>
This commit is contained in:
co-authored by
Pascal
parent
805984d676
commit
666f8898a2
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Frontend executor for the `get_datetime` tool. It runs in the browser, so it
|
||||
* reports the user's own clock and time zone instead of the server's UTC time -
|
||||
* a chat about "tomorrow" means the user's tomorrow, not the host's.
|
||||
*
|
||||
* @see buildGetDatetimeToolDefinition in constants/get-datetime.ts - tool schema sent to the LLM
|
||||
*/
|
||||
|
||||
import type { ToolExecutionResult } from '$lib/types';
|
||||
|
||||
function pad(value: number): string {
|
||||
return String(value).padStart(2, '0');
|
||||
}
|
||||
|
||||
/** ISO 8601 in local time, e.g. `2026-08-17T14:05:09+02:00` */
|
||||
function localIsoString(date: Date): string {
|
||||
// getTimezoneOffset() counts minutes behind UTC, ISO 8601 counts them ahead
|
||||
const offset = -date.getTimezoneOffset();
|
||||
const sign = offset < 0 ? '-' : '+';
|
||||
const absOffset = Math.abs(offset);
|
||||
const day = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
|
||||
const time = `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
|
||||
|
||||
return `${day}T${time}${sign}${pad(Math.floor(absOffset / 60))}:${pad(absOffset % 60)}`;
|
||||
}
|
||||
|
||||
/** The `result` field keeps the shape the `get_datetime` renderer already reads. */
|
||||
export function executeGetDatetimeTool(): ToolExecutionResult {
|
||||
const now = new Date();
|
||||
|
||||
return {
|
||||
content: JSON.stringify({
|
||||
result: localIsoString(now),
|
||||
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
|
||||
}),
|
||||
isError: false
|
||||
};
|
||||
}
|
||||
@@ -331,6 +331,9 @@ export { getChatCommands } from './chat-commands';
|
||||
// SANDBOX_TOOL_DEFINITION is deprecated; kept for backward compatibility.
|
||||
export { buildSandboxToolDefinition, SANDBOX_TOOL_DEFINITION } from './sandbox-tool';
|
||||
|
||||
// Frontend `get_datetime` executor (the browser clock, not the server's)
|
||||
export { executeGetDatetimeTool } from './get-datetime';
|
||||
|
||||
// Browser fallback for the server's get_info tool
|
||||
export { executeBrowserInfoTool } from './browser-info';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user