fix: better timezones and error handling in ai tools (#11085)
* current time in user timezone, let ai handle 401 * update agent.ts * simple onboarding for non cal.com users * reverting this line - unecessary * v1.0.1
This commit is contained in:
1 parent
c12abd75a8
commit
dfccf4604f
8 files changed
+25
-19
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@calcom/ai",
|
||||
"version": "0.1.0",
|
||||
"version": "1.0.1",
|
||||
"private": true,
|
||||
"author": "Cal.com Inc.",
|
||||
"dependencies": {
|
||||
|
||||
@@ -45,6 +45,7 @@ export const POST = async (request: NextRequest) => {
|
||||
select: {
|
||||
email: true,
|
||||
id: true,
|
||||
timeZone: true,
|
||||
credentials: {
|
||||
select: {
|
||||
appId: true,
|
||||
@@ -55,15 +56,17 @@ export const POST = async (request: NextRequest) => {
|
||||
where: { email: envelope.from },
|
||||
});
|
||||
|
||||
if (!signature || !user?.email || !user?.id) {
|
||||
// User is not a cal.com user or is using an unverified email.
|
||||
if (!signature || !user) {
|
||||
await sendEmail({
|
||||
html: `Thanks for your interest in Cal AI! To get started, Make sure you have a <a href="https://cal.com/signup" target="_blank">cal.com</a> account with this email address.`,
|
||||
subject: `Re: ${body.subject}`,
|
||||
text: "Sorry, you are not authorized to use this service. Please verify your email address and try again.",
|
||||
to: user?.email || "",
|
||||
text: `Thanks for your interest in Cal AI! To get started, Make sure you have a cal.com account with this email address. You can sign up for an account at: https://cal.com/signup`,
|
||||
to: envelope.from,
|
||||
from: aiEmail,
|
||||
});
|
||||
|
||||
return new NextResponse();
|
||||
return new NextResponse("ok");
|
||||
}
|
||||
|
||||
const credential = user.credentials.find((c) => c.appId === env.APP_ID)?.key;
|
||||
@@ -93,8 +96,8 @@ export const POST = async (request: NextRequest) => {
|
||||
fetchAvailability({
|
||||
apiKey,
|
||||
userId: user.id,
|
||||
dateFrom: now,
|
||||
dateTo: now,
|
||||
dateFrom: now(user.timeZone),
|
||||
dateTo: now(user.timeZone),
|
||||
}),
|
||||
]);
|
||||
|
||||
@@ -120,7 +123,7 @@ export const POST = async (request: NextRequest) => {
|
||||
return new NextResponse("Error fetching event types. Please try again.", { status: 400 });
|
||||
}
|
||||
|
||||
const { timeZone, workingHours } = availability;
|
||||
const { workingHours } = availability;
|
||||
|
||||
const appHost = getHostFromHeaders(request.headers);
|
||||
|
||||
@@ -135,7 +138,7 @@ export const POST = async (request: NextRequest) => {
|
||||
user: {
|
||||
email: user.email,
|
||||
eventTypes,
|
||||
timeZone,
|
||||
timeZone: user.timeZone,
|
||||
workingHours,
|
||||
},
|
||||
}),
|
||||
|
||||
@@ -52,9 +52,8 @@ const createBooking = async ({
|
||||
method: "POST",
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
throw new Error("Unauthorized");
|
||||
}
|
||||
// Let GPT handle this. This will happen when wrong event type id is used.
|
||||
// if (response.status === 401) throw new Error("Unauthorized");
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
|
||||
@@ -31,7 +31,8 @@ const cancelBooking = async ({
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
if (response.status === 401) throw new Error("Unauthorized");
|
||||
// Let GPT handle this. This will happen when wrong booking id is used.
|
||||
// if (response.status === 401) throw new Error("Unauthorized");
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
|
||||
@@ -35,9 +35,7 @@ export const fetchAvailability = async ({
|
||||
|
||||
const response = await fetch(url);
|
||||
|
||||
if (response.status === 401) {
|
||||
throw new Error("Unauthorized");
|
||||
}
|
||||
if (response.status === 401) throw new Error("Unauthorized");
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
|
||||
@@ -41,7 +41,8 @@ const editBooking = async ({
|
||||
method: "PATCH",
|
||||
});
|
||||
|
||||
if (response.status === 401) throw new Error("Unauthorized");
|
||||
// Let GPT handle this. This will happen when wrong booking id is used.
|
||||
// if (response.status === 401) throw new Error("Unauthorized");
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ const agent = async (input: string, user: User, apiKey: string, userId: number)
|
||||
Sometimes, tools return errors. In this case, try to handle the error intelligently or ask the user for more information.
|
||||
Tools will always handle times in UTC, but times sent to the user should be formatted per that user's timezone.
|
||||
|
||||
Current UTC time is: ${now}
|
||||
The current time in the user's timezone is: ${now(user.timeZone)}
|
||||
The user's time zone is: ${user.timeZone}
|
||||
The user's event types are: ${user.eventTypes
|
||||
.map((e: EventType) => `ID: ${e.id}, Title: ${e.title}, Length: ${e.length}`)
|
||||
|
||||
@@ -1 +1,5 @@
|
||||
export default new Date().toISOString();
|
||||
export default function now(timeZone: string) {
|
||||
return new Date().toLocaleString("en-US", {
|
||||
timeZone,
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user