Merge pull request #5595 from sebdanielsson/dev

feat: Add more variables to prompts
This commit is contained in:
Timothy Jaeryang Baek 2024-09-22 16:47:51 +02:00 committed by GitHub
commit 2cfe6830df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 92 additions and 6 deletions

View File

@ -1,6 +1,14 @@
<script lang="ts">
import { prompts } from '$lib/stores';
import { findWordIndices } from '$lib/utils';
import {
findWordIndices,
getUserPosition,
getFormattedDate,
getFormattedTime,
getCurrentDateTime,
getUserTimezone,
getWeekday
} from '$lib/utils';
import { tick, getContext } from 'svelte';
import { toast } from 'svelte-sonner';
@ -39,8 +47,6 @@
return '{{CLIPBOARD}}';
});
console.log(clipboardText);
const clipboardItems = await navigator.clipboard.read();
let imageUrl = null;
@ -50,7 +56,6 @@
if (type.startsWith('image/')) {
const blob = await item.getType(type);
imageUrl = URL.createObjectURL(blob);
console.log(`Image URL (${type}): ${imageUrl}`);
}
}
}
@ -65,7 +70,42 @@
];
}
text = command.content.replaceAll('{{CLIPBOARD}}', clipboardText);
text = text.replaceAll('{{CLIPBOARD}}', clipboardText);
}
if (command.content.includes('{{USER_LOCATION}}')) {
const location = await getUserPosition();
text = text.replaceAll('{{USER_LOCATION}}', String(location));
}
if (command.content.includes('{{USER_LANGUAGE}}')) {
const language = localStorage.getItem('locale') || 'en-US';
text = text.replaceAll('{{USER_LANGUAGE}}', language);
}
if (command.content.includes('{{CURRENT_DATE}}')) {
const date = getFormattedDate();
text = text.replaceAll('{{CURRENT_DATE}}', date);
}
if (command.content.includes('{{CURRENT_TIME}}')) {
const time = getFormattedTime();
text = text.replaceAll('{{CURRENT_TIME}}', time);
}
if (command.content.includes('{{CURRENT_DATETIME}}')) {
const dateTime = getCurrentDateTime();
text = text.replaceAll('{{CURRENT_DATETIME}}', dateTime);
}
if (command.content.includes('{{CURRENT_TIMEZONE}}')) {
const timezone = getUserTimezone();
text = text.replaceAll('{{CURRENT_TIMEZONE}}', timezone);
}
if (command.content.includes('{{CURRENT_WEEKDAY}}')) {
const weekday = getWeekday();
text = text.replaceAll('{{CURRENT_WEEKDAY}}', weekday);
}
prompt = text;

View File

@ -664,6 +664,15 @@ export const promptTemplate = (
hour12: true
});
// Get the current weekday
const currentWeekday = getWeekday();
// Get the user's timezone
const currentTimezone = getUserTimezone();
// Get the user's language
const userLanguage = localStorage.getItem('locale') || 'en-US';
// Replace {{CURRENT_DATETIME}} in the template with the formatted datetime
template = template.replace('{{CURRENT_DATETIME}}', `${formattedDate} ${currentTime}`);
@ -673,6 +682,15 @@ export const promptTemplate = (
// Replace {{CURRENT_TIME}} in the template with the formatted time
template = template.replace('{{CURRENT_TIME}}', currentTime);
// Replace {{CURRENT_WEEKDAY}} in the template with the current weekday
template = template.replace('{{CURRENT_WEEKDAY}}', currentWeekday);
// Replace {{CURRENT_TIMEZONE}} in the template with the user's timezone
template = template.replace('{{CURRENT_TIMEZONE}}', currentTimezone);
// Replace {{USER_LANGUAGE}} in the template with the user's language
template = template.replace('{{USER_LANGUAGE}}', userLanguage);
if (user_name) {
// Replace {{USER_NAME}} in the template with the user's name
template = template.replace('{{USER_NAME}}', user_name);
@ -829,6 +847,34 @@ export const bestMatchingLanguage = (supportedLanguages, preferredLanguages, def
.map((prefLang) => languages.find((lang) => lang.startsWith(prefLang)))
.find(Boolean);
console.log(languages, preferredLanguages, match, defaultLocale);
return match || defaultLocale;
};
// Get the date in the format YYYY-MM-DD
export const getFormattedDate = () => {
const date = new Date();
return date.toISOString().split('T')[0];
};
// Get the time in the format HH:MM:SS
export const getFormattedTime = () => {
const date = new Date();
return date.toTimeString().split(' ')[0];
};
// Get the current date and time in the format YYYY-MM-DD HH:MM:SS
export const getCurrentDateTime = () => {
return `${getFormattedDate()} ${getFormattedTime()}`;
};
// Get the user's timezone
export const getUserTimezone = () => {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
};
// Get the weekday
export const getWeekday = () => {
const date = new Date();
const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
return weekdays[date.getDay()];
};