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
3 changes: 2 additions & 1 deletion src/lib/server/textGeneration/mcp/runMcpFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ export async function* runMcpFlow({
imageProcessor,
mmEnabled
);
const toolPreprompt = buildToolPreprompt(oaTools);
const userTimezone = (locals as unknown as { timezone?: string })?.timezone;
const toolPreprompt = buildToolPreprompt(oaTools, userTimezone);
const prepromptPieces: string[] = [];
if (toolPreprompt.trim().length > 0) {
prepromptPieces.push(toolPreprompt);
Expand Down
22 changes: 17 additions & 5 deletions src/lib/server/textGeneration/utils/toolPrompt.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import type { OpenAiTool } from "$lib/server/mcp/tools";

export function buildToolPreprompt(tools: OpenAiTool[]): string {
export function buildToolPreprompt(tools: OpenAiTool[], timezone?: string): string {
if (!Array.isArray(tools) || tools.length === 0) return "";
const names = tools
.map((t) => (t?.function?.name ? String(t.function.name) : ""))
.filter((s) => s.length > 0);
if (names.length === 0) return "";
const currentDate = new Date().toLocaleDateString("en-US", {

const now = new Date();
const dateTimeOptions: Intl.DateTimeFormatOptions = {
year: "numeric",
month: "long",
day: "numeric",
});
weekday: "long",
hour: "2-digit",
minute: "2-digit",
...(timezone ? { timeZone: timezone } : {}),
};
const currentDateTime = now.toLocaleString("en-US", dateTimeOptions);

// Build a user-location hint from the IANA timezone (e.g. "America/New_York" → "America/New_York")
const locationLine = timezone ? ` User's timezone: ${timezone}.` : "";

return [
`You can use the following tools if helpful: ${names.join(", ")}.`,
`Today's date: ${currentDate}.`,
`You have access to these tools: ${names.join(", ")}.`,
`Current date and time: ${currentDateTime}.${locationLine}`,
`Only use a tool if you cannot answer without it. For simple tasks like writing, editing text, or answering from your knowledge, respond directly without tools.`,
`If a tool generates an image, you can inline it directly: ![alt text](image_url).`,
`If a tool needs an image, set its image field ("input_image", "image", or "image_url") to a reference like "image_1", "image_2", etc. (ordered by when the user uploaded them).`,
`Default to image references; only use a full http(s) URL when the tool description explicitly asks for one, or reuse a URL a previous tool returned.`,
Expand Down
3 changes: 3 additions & 0 deletions src/lib/utils/messageUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type MessageUpdateRequestOptions = {
selectedMcpServerNames?: string[];
// Optional: pass selected MCP server configs (for custom client-defined servers)
selectedMcpServers?: Array<{ name: string; url: string; headers?: KeyValuePair[] }>;
// User's IANA timezone (e.g. "America/New_York")
timezone?: string;
};
export async function fetchMessageUpdates(
conversationId: string,
Expand All @@ -43,6 +45,7 @@ export async function fetchMessageUpdates(
// Will be ignored server-side if unsupported
selectedMcpServerNames: opts.selectedMcpServerNames,
selectedMcpServers: opts.selectedMcpServers,
timezone: opts.timezone,
});

opts.files?.forEach((file) => {
Expand Down
1 change: 1 addition & 0 deletions src/routes/conversation/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
url: s.url,
headers: s.headers,
})),
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
},
messageUpdatesAbortController.signal
).catch((err) => {
Expand Down
7 changes: 7 additions & 0 deletions src/routes/conversation/[id]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export async function POST({ request, locals, params, getClientAddress }) {
is_retry: isRetry,
selectedMcpServerNames,
selectedMcpServers,
timezone,
} = z
.object({
id: z.string().uuid().refine(isMessageId).optional(), // parent message id to append to for a normal message, or the message id for a retry/continue
Expand All @@ -152,6 +153,7 @@ export async function POST({ request, locals, params, getClientAddress }) {
)
)
.default([]),
timezone: z.optional(z.string()),
files: z.optional(
z.array(
z.object({
Expand Down Expand Up @@ -182,6 +184,11 @@ export async function POST({ request, locals, params, getClientAddress }) {
// ignore attachment errors, pipeline will just use env servers
}

// Attach user timezone so the tool prompt can include localized time
if (timezone) {
(locals as unknown as Record<string, unknown>).timezone = timezone;
}

const inputFiles = await Promise.all(
form
.getAll("files")
Expand Down