ui: move get_datetime tool to frontend

This commit is contained in:
Xuan Son Nguyen
2026-08-17 12:52:12 +02:00
parent d83f72d463
commit 35293d0bb0
10 changed files with 88 additions and 74 deletions
@@ -34,7 +34,7 @@ export const BUILTIN_TOOL_UI: Readonly<Record<BuiltInTool, BuiltinToolUiEntry>>
label: 'Search files',
source: ToolSource.BUILTIN
},
[BuiltInTool.GET_DATETIME]: { icon: Clock, label: 'Current time', source: ToolSource.BUILTIN },
[BuiltInTool.GET_DATETIME]: { icon: Clock, label: 'Current time', source: ToolSource.FRONTEND },
[BuiltInTool.GET_INFO]: { icon: Info, label: 'Runtime info', source: ToolSource.BUILTIN },
[BuiltInTool.GREP_SEARCH]: {
icon: SearchCode,
@@ -0,0 +1,20 @@
import { BuiltInTool, JsonSchemaType, ToolCallType } from '$lib/enums';
import type { OpenAIToolDefinition } from '$lib/types';
export const GET_DATETIME_TOOL_NAME = BuiltInTool.GET_DATETIME;
export function buildGetDatetimeToolDefinition(): OpenAIToolDefinition {
return {
function: {
description:
'Returns the current local date and time in ISO 8601 format, with the IANA time zone name',
name: GET_DATETIME_TOOL_NAME,
parameters: {
properties: {},
required: [],
type: JsonSchemaType.OBJECT
}
},
type: ToolCallType.FUNCTION
};
}
+1
View File
@@ -59,3 +59,4 @@ export * from './uri-template.constants';
export * from './url.constants';
export * from './working-directory.constants';
export * from './read-media';
export * from './get-datetime';
+21 -14
View File
@@ -56,7 +56,8 @@ import type {
AgenticSession,
McpServerOverride,
MCPToolCall,
SettingsConfigType
SettingsConfigType,
ToolExecutionResult
} from '$lib/types';
import type {
AgenticFlowCallbacks,
@@ -83,7 +84,7 @@ import type {
DatabaseMessageExtraAudioFile,
DatabaseMessageExtraImageFile
} from '$lib/types/database';
import { getAudioInputFormat, isAbortError } from '$lib/utils';
import { executeGetDatetimeTool, getAudioInputFormat, isAbortError } from '$lib/utils';
import { SvelteMap } from 'svelte/reactivity';
function createDefaultSession(): AgenticSession {
@@ -942,18 +943,24 @@ class AgenticStore {
if (executionResult.isError) toolSuccess = false;
} else if (toolSource === ToolSource.FRONTEND) {
const args = this.parseToolArguments(toolCall.function.arguments);
const executionResult =
toolName === BuiltInTool.READ_MEDIA
? await ReadMediaService.executeTool(
args,
{
audio: modelsStore.modelSupportsAudio(effectiveModel),
vision: modelsStore.modelSupportsVision(effectiveModel)
},
signal,
conversationsStore.activeConversation?.cwd
)
: await SandboxService.executeTool(toolName, args, signal);
let executionResult: ToolExecutionResult;
if (toolName === BuiltInTool.GET_DATETIME) {
executionResult = executeGetDatetimeTool();
} else if (toolName === BuiltInTool.READ_MEDIA) {
executionResult = await ReadMediaService.executeTool(
args,
{
audio: modelsStore.modelSupportsAudio(effectiveModel),
vision: modelsStore.modelSupportsVision(effectiveModel)
},
signal,
conversationsStore.activeConversation?.cwd
);
} else {
executionResult = await SandboxService.executeTool(toolName, args, signal);
}
result = executionResult.content;
+2 -1
View File
@@ -1,5 +1,6 @@
import { browser } from '$app/environment';
import {
buildGetDatetimeToolDefinition,
buildReadMediaToolDefinition,
DISABLED_TOOL_KEYS_LOCALSTORAGE_KEY,
HOME_TILDE,
@@ -175,7 +176,7 @@ class ToolsStore {
}
get frontendTools(): OpenAIToolDefinition[] {
const tools: OpenAIToolDefinition[] = [];
const tools: OpenAIToolDefinition[] = [buildGetDatetimeToolDefinition()];
if (settingsStore.config.jsSandboxEnabled) {
tools.push(buildSandboxToolDefinition(!!settingsStore.config.symbolicMathEnabled));
+38
View File
@@ -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
};
}
+3
View File
@@ -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';
// Cryptography utilities
export { uuid } from './uuid';