Skip to content

Commit e9ab9ed

Browse files
committed
[tools] use calender list tools and add date to prompt
1 parent 62c249f commit e9ab9ed

File tree

9 files changed

+112
-44
lines changed

9 files changed

+112
-44
lines changed

py-langchain/backend/app/agents/assistant0.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,23 @@
55
from app.agents.tools.google_calendar import list_upcoming_events
66
from app.agents.tools.user_info import get_user_info
77
from app.agents.tools.context_docs import get_context_docs
8+
from datetime import date
89

910

1011
tools = [get_user_info, list_upcoming_events, shop_online, get_context_docs]
1112

1213
llm = ChatOpenAI(model="gpt-4.1-mini")
1314

15+
def get_prompt():
16+
today_str = date.today().strftime('%Y-%m-%d')
17+
return (
18+
f"You are a personal assistant named Assistant0. You are a helpful assistant that can answer questions and help with tasks. "
19+
f"Today's date is {today_str}. You have access to a set of tools, use the tools as needed to answer the user's question. "
20+
f"Render the email body as a markdown block, do not wrap it in code blocks."
21+
)
22+
1423
agent = create_react_agent(
1524
llm,
1625
tools=ToolNode(tools, handle_tool_errors=False),
17-
prompt="You are a personal assistant named Assistant0. You are a helpful assistant that can answer questions and help with tasks. You have access to a set of tools, use the tools as needed to answer the user's question. Render the email body as a markdown block, do not wrap it in code blocks.",
26+
prompt=get_prompt(),
1827
)

ts-langchain/src/lib/agent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getAccessToken, withGoogleConnection, withAsyncAuthorization } from './
1010
import { getUserInfoTool } from './tools/user-info';
1111
import { shopOnlineTool } from './tools/shop-online';
1212
import { getContextDocumentsTool } from './tools/context-docs';
13-
import { checkUsersCalendarTool } from './tools/google-calender';
13+
import { getCalendarEventsTool } from './tools/google-calender';
1414

1515
const date = new Date().toISOString();
1616

@@ -40,7 +40,7 @@ const tools = [
4040
withGoogleConnection(new GmailCreateDraft(gmailParams)),
4141
withGoogleConnection(new GoogleCalendarCreateTool(googleCalendarParams)),
4242
withGoogleConnection(new GoogleCalendarViewTool(googleCalendarParams)),
43-
withGoogleConnection(checkUsersCalendarTool),
43+
withGoogleConnection(getCalendarEventsTool),
4444
getUserInfoTool,
4545
withAsyncAuthorization(shopOnlineTool),
4646
getContextDocumentsTool,

ts-langchain/src/lib/auth0-ai.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export const withGoogleConnection = auth0AI.withTokenForConnection({
1313
'https://www.googleapis.com/auth/gmail.readonly',
1414
'https://www.googleapis.com/auth/gmail.compose',
1515
'https://www.googleapis.com/auth/calendar.events',
16-
'https://www.googleapis.com/auth/calendar.freebusy',
1716
],
1817
});
1918

ts-langchain/src/lib/tools/google-calender.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { tool } from '@langchain/core/tools';
2-
import { addHours, formatISO } from 'date-fns';
2+
import { endOfDay, formatISO, startOfDay } from 'date-fns';
33
import { GaxiosError } from 'gaxios';
44
import { google } from 'googleapis';
55
import { z } from 'zod';
66
import { FederatedConnectionError } from '@auth0/ai/interrupts';
77

88
import { getAccessToken } from '../auth0-ai';
99

10-
export const checkUsersCalendarTool = tool(
10+
export const getCalendarEventsTool = tool(
1111
async ({ date }) => {
1212
// Get the access token from Auth0 AI
1313
const accessToken = await getAccessToken();
@@ -21,18 +21,38 @@ export const checkUsersCalendarTool = tool(
2121
access_token: accessToken,
2222
});
2323

24-
const response = await calendar.freebusy.query({
24+
// Get events for the entire day
25+
const response = await calendar.events.list({
2526
auth,
26-
requestBody: {
27-
timeMin: formatISO(date),
28-
timeMax: addHours(date, 1).toISOString(),
29-
timeZone: 'UTC',
30-
items: [{ id: 'primary' }],
31-
},
27+
calendarId: 'primary',
28+
timeMin: formatISO(startOfDay(date)),
29+
timeMax: formatISO(endOfDay(date)),
30+
singleEvents: true,
31+
orderBy: 'startTime',
32+
maxResults: 50,
3233
});
3334

35+
const events = response.data.items || [];
36+
3437
return {
35-
available: response.data?.calendars?.primary?.busy?.length === 0,
38+
date: formatISO(date, { representation: 'date' }),
39+
eventsCount: events.length,
40+
events: events.map((event) => ({
41+
id: event.id,
42+
summary: event.summary || 'No title',
43+
description: event.description,
44+
startTime: event.start?.dateTime || event.start?.date,
45+
endTime: event.end?.dateTime || event.end?.date,
46+
location: event.location,
47+
attendees:
48+
event.attendees?.map((attendee) => ({
49+
email: attendee.email,
50+
name: attendee.displayName,
51+
responseStatus: attendee.responseStatus,
52+
})) || [],
53+
status: event.status,
54+
htmlLink: event.htmlLink,
55+
})),
3656
};
3757
} catch (error) {
3858
if (error instanceof GaxiosError) {
@@ -45,8 +65,8 @@ export const checkUsersCalendarTool = tool(
4565
}
4666
},
4767
{
48-
name: 'check_users_calendar_availability',
49-
description: 'Check user availability on a given date time on their calendar',
68+
name: 'get_calendar_events',
69+
description: `Get calendar events for a given date from the user's Google Calendar`,
5070
schema: z.object({
5171
date: z.coerce.date(),
5272
}),

ts-llamaindex/src/app/api/chat/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { errorSerializer } from '@auth0/ai-vercel/interrupts';
99
import { serpApiTool } from '@/lib/tools/serpapi';
1010
import { getUserInfoTool } from '@/lib/tools/user-info';
1111
import { gmailDraftTool, gmailSearchTool } from '@/lib/tools/gmail';
12-
import { checkUsersCalendarTool } from '@/lib/tools/google-calender';
12+
import { getCalendarEventsTool } from '@/lib/tools/google-calender';
1313
import { shopOnlineTool } from '@/lib/tools/shop-online';
1414
import { getContextDocumentsTool } from '@/lib/tools/context-docs';
1515

@@ -29,7 +29,7 @@ async function initializeAgent(sanitizedMessages: Message[] = []) {
2929
getUserInfoTool,
3030
gmailSearchTool,
3131
gmailDraftTool,
32-
checkUsersCalendarTool,
32+
getCalendarEventsTool,
3333
shopOnlineTool,
3434
getContextDocumentsTool,
3535
];

ts-llamaindex/src/lib/auth0-ai.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const withGoogleConnection = auth0AI.withTokenForConnection({
1414
scopes: [
1515
'https://www.googleapis.com/auth/gmail.readonly',
1616
'https://www.googleapis.com/auth/gmail.compose',
17-
'https://www.googleapis.com/auth/calendar.freebusy',
17+
'https://www.googleapis.com/auth/calendar.events',
1818
],
1919
refreshToken: getRefreshToken,
2020
credentialsContext: 'tool-call',

ts-llamaindex/src/lib/tools/google-calender.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { tool } from 'llamaindex';
2-
import { addHours, formatISO } from 'date-fns';
2+
import { endOfDay, formatISO, startOfDay } from 'date-fns';
33
import { GaxiosError } from 'gaxios';
44
import { google } from 'googleapis';
55
import { z } from 'zod';
66
import { FederatedConnectionError } from '@auth0/ai/interrupts';
77

88
import { getAccessToken, withGoogleConnection } from '../auth0-ai';
99

10-
export const checkUsersCalendarTool = withGoogleConnection(
10+
export const getCalendarEventsTool = withGoogleConnection(
1111
tool({
12-
name: 'checkUsersCalendar',
13-
description: 'Check user availability on a given date time on their calendar',
12+
name: 'getCalendarEvents',
13+
description: `Get calendar events for a given date from the user's Google Calendar`,
1414
parameters: z.object({
1515
date: z.coerce.date(),
1616
}),
@@ -27,18 +27,38 @@ export const checkUsersCalendarTool = withGoogleConnection(
2727
access_token: accessToken,
2828
});
2929

30-
const response = await calendar.freebusy.query({
30+
// Get events for the entire day
31+
const response = await calendar.events.list({
3132
auth,
32-
requestBody: {
33-
timeMin: formatISO(date),
34-
timeMax: addHours(date, 1).toISOString(),
35-
timeZone: 'UTC',
36-
items: [{ id: 'primary' }],
37-
},
33+
calendarId: 'primary',
34+
timeMin: formatISO(startOfDay(date)),
35+
timeMax: formatISO(endOfDay(date)),
36+
singleEvents: true,
37+
orderBy: 'startTime',
38+
maxResults: 50,
3839
});
3940

41+
const events = response.data.items || [];
42+
4043
return {
41-
available: response.data?.calendars?.primary?.busy?.length === 0,
44+
date: formatISO(date, { representation: 'date' }),
45+
eventsCount: events.length,
46+
events: events.map((event) => ({
47+
id: event.id || '',
48+
summary: event.summary || 'No title',
49+
description: event.description || '',
50+
startTime: event.start?.dateTime || event.start?.date || '',
51+
endTime: event.end?.dateTime || event.end?.date || '',
52+
location: event.location || '',
53+
attendees:
54+
event.attendees?.map((attendee) => ({
55+
email: attendee.email || '',
56+
name: attendee.displayName || '',
57+
responseStatus: attendee.responseStatus || '',
58+
})) || [],
59+
status: event.status || '',
60+
htmlLink: event.htmlLink || '',
61+
})),
4262
};
4363
} catch (error) {
4464
if (error instanceof GaxiosError) {

ts-vercel-ai/src/app/api/chat/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { errorSerializer, withInterruptions } from '@auth0/ai-vercel/interrupts'
77
import { serpApiTool } from '@/lib/tools/serpapi';
88
import { getUserInfoTool } from '@/lib/tools/user-info';
99
import { gmailDraftTool, gmailSearchTool } from '@/lib/tools/gmail';
10-
import { checkUsersCalendarTool } from '@/lib/tools/google-calender';
10+
import { getCalendarEventsTool } from '@/lib/tools/google-calender';
1111
import { shopOnlineTool } from '@/lib/tools/shop-online';
1212
import { getContextDocumentsTool } from '@/lib/tools/context-docs';
1313

@@ -30,7 +30,7 @@ export async function POST(req: NextRequest) {
3030
getUserInfoTool,
3131
gmailSearchTool,
3232
gmailDraftTool,
33-
checkUsersCalendarTool,
33+
getCalendarEventsTool,
3434
shopOnlineTool,
3535
getContextDocumentsTool,
3636
};

ts-vercel-ai/src/lib/tools/google-calender.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { tool } from 'ai';
2-
import { addHours, formatISO } from 'date-fns';
2+
import { endOfDay, formatISO, startOfDay } from 'date-fns';
33
import { GaxiosError } from 'gaxios';
44
import { google } from 'googleapis';
55
import { z } from 'zod';
66
import { FederatedConnectionError } from '@auth0/ai/interrupts';
77

88
import { getAccessToken, withGoogleConnection } from '../auth0-ai';
99

10-
export const checkUsersCalendarTool = withGoogleConnection(
10+
export const getCalendarEventsTool = withGoogleConnection(
1111
tool({
12-
description: 'Check user availability on a given date time on their calendar',
12+
description: `Get calendar events for a given date from the user's Google Calendar`,
1313
parameters: z.object({
1414
date: z.coerce.date(),
1515
}),
@@ -26,18 +26,38 @@ export const checkUsersCalendarTool = withGoogleConnection(
2626
access_token: accessToken,
2727
});
2828

29-
const response = await calendar.freebusy.query({
29+
// Get events for the entire day
30+
const response = await calendar.events.list({
3031
auth,
31-
requestBody: {
32-
timeMin: formatISO(date),
33-
timeMax: addHours(date, 1).toISOString(),
34-
timeZone: 'UTC',
35-
items: [{ id: 'primary' }],
36-
},
32+
calendarId: 'primary',
33+
timeMin: formatISO(startOfDay(date)),
34+
timeMax: formatISO(endOfDay(date)),
35+
singleEvents: true,
36+
orderBy: 'startTime',
37+
maxResults: 50,
3738
});
3839

40+
const events = response.data.items || [];
41+
3942
return {
40-
available: response.data?.calendars?.primary?.busy?.length === 0,
43+
date: formatISO(date, { representation: 'date' }),
44+
eventsCount: events.length,
45+
events: events.map((event) => ({
46+
id: event.id,
47+
summary: event.summary || 'No title',
48+
description: event.description,
49+
startTime: event.start?.dateTime || event.start?.date,
50+
endTime: event.end?.dateTime || event.end?.date,
51+
location: event.location,
52+
attendees:
53+
event.attendees?.map((attendee) => ({
54+
email: attendee.email,
55+
name: attendee.displayName,
56+
responseStatus: attendee.responseStatus,
57+
})) || [],
58+
status: event.status,
59+
htmlLink: event.htmlLink,
60+
})),
4161
};
4262
} catch (error) {
4363
if (error instanceof GaxiosError) {

0 commit comments

Comments
 (0)