Skip to content

Commit 62c1be6

Browse files
Merge pull request #62 from auth0/fix/tools
[a4aa-docs] fix calender tool and date issues
2 parents dfb87d6 + b978fab commit 62c1be6

File tree

5 files changed

+51
-30
lines changed

5 files changed

+51
-30
lines changed

auth4genai/snippets/get-started/langchain-fastapi-py/async-auth.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,11 @@ tools = [shop_online]
253253

254254
llm = ChatOpenAI(model="gpt-4.1-mini")
255255

256+
# ... existing code
256257
agent = create_react_agent(
257258
llm,
258259
tools=ToolNode(tools, handle_tool_errors=False),
259-
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.",
260+
prompt=get_prompt(),
260261
)
261262
```
262263

auth4genai/snippets/get-started/langchain-fastapi-py/auth-for-rag.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,11 @@ tools = [get_context_docs]
169169

170170
llm = ChatOpenAI(model="gpt-4.1-mini")
171171

172+
# ... existing code
172173
agent = create_react_agent(
173174
llm,
174175
tools=ToolNode(tools, handle_tool_errors=False),
175-
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.",
176+
prompt=get_prompt(),
176177
)
177178
```
178179

auth4genai/snippets/get-started/langchain-fastapi-py/call-others-api.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,11 @@ tools = [list_upcoming_events]
198198

199199
llm = ChatOpenAI(model="gpt-4.1-mini")
200200

201+
# ... existing code
201202
agent = create_react_agent(
202203
llm,
203204
tools=ToolNode(tools, handle_tool_errors=False),
204-
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.",
205+
prompt=get_prompt(),
205206
)
206207
```
207208

auth4genai/snippets/get-started/langchain-fastapi-py/call-your-api.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,11 @@ tools = [get_user_info]
151151

152152
llm = ChatOpenAI(model="gpt-4.1-mini")
153153

154+
# ... existing code
154155
agent = create_react_agent(
155156
llm,
156157
tools=ToolNode(tools, handle_tool_errors=False),
157-
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.",
158+
prompt=get_prompt(),
158159
)
159160
```
160161

auth4genai/snippets/get-started/vercel-ai-next-js/call-others-api.mdx

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,21 @@ export const getRefreshToken = async () => {
8888

8989
Once the user is authenticated, you can fetch an access token from the Token Vault using the Auth0 AI SDK. In this example, we fetch an access token for a Google social connection. Once you've obtained the access token for a social connection, you can use it with an AI agent to fetch data during a tool call and provide contextual data in its response.
9090

91-
In our example, we create a file at `src/lib/tools/google-calendar.ts`. In the file, we define a tool, `checkUsersCalendarTool`, that uses the access token with the Google Calendar API to query for calendar events in a specified date range:
91+
In our example, we create a file at `src/lib/tools/google-calendar.ts`. In the file, we define a tool, `getCalendarEventsTool`, that uses the access token with the Google Calendar API to query for calendar events in a specified date:
9292

9393
```ts src/lib/tools/google-calendar.ts wrap lines
94-
import { tool } from "ai";
95-
import { addHours, formatISO } from "date-fns";
96-
import { GaxiosError } from "gaxios";
97-
import { google } from "googleapis";
98-
import { z } from "zod";
99-
import { FederatedConnectionError } from "@auth0/ai/interrupts";
94+
import { tool } from 'ai';
95+
import { endOfDay, formatISO, startOfDay } from 'date-fns';
96+
import { GaxiosError } from 'gaxios';
97+
import { google } from 'googleapis';
98+
import { z } from 'zod';
99+
import { FederatedConnectionError } from '@auth0/ai/interrupts';
100100

101-
import { getAccessToken, withGoogleConnection } from "../auth0-ai";
101+
import { getAccessToken, withGoogleConnection } from '../auth0-ai';
102102

103-
export const checkUsersCalendarTool = withGoogleConnection(
103+
export const getCalendarEventsTool = withGoogleConnection(
104104
tool({
105-
description:
106-
"Check user availability on a given date time on their calendar",
105+
description: `Get calendar events for a given date from the user's Google Calendar`,
107106
parameters: z.object({
108107
date: z.coerce.date(),
109108
}),
@@ -113,39 +112,57 @@ export const checkUsersCalendarTool = withGoogleConnection(
113112

114113
// Google SDK
115114
try {
116-
const calendar = google.calendar("v3");
115+
const calendar = google.calendar('v3');
117116
const auth = new google.auth.OAuth2();
118117

119118
auth.setCredentials({
120119
access_token: accessToken,
121120
});
122121

123-
const response = await calendar.freebusy.query({
122+
// Get events for the entire day
123+
const response = await calendar.events.list({
124124
auth,
125-
requestBody: {
126-
timeMin: formatISO(date),
127-
timeMax: addHours(date, 1).toISOString(),
128-
timeZone: "UTC",
129-
items: [{ id: "primary" }],
130-
},
125+
calendarId: 'primary',
126+
timeMin: formatISO(startOfDay(date)),
127+
timeMax: formatISO(endOfDay(date)),
128+
singleEvents: true,
129+
orderBy: 'startTime',
130+
maxResults: 50,
131131
});
132132

133+
const events = response.data.items || [];
134+
133135
return {
134-
available: response.data?.calendars?.primary?.busy?.length === 0,
136+
date: formatISO(date, { representation: 'date' }),
137+
eventsCount: events.length,
138+
events: events.map((event) => ({
139+
id: event.id,
140+
summary: event.summary || 'No title',
141+
description: event.description,
142+
startTime: event.start?.dateTime || event.start?.date,
143+
endTime: event.end?.dateTime || event.end?.date,
144+
location: event.location,
145+
attendees:
146+
event.attendees?.map((attendee) => ({
147+
email: attendee.email,
148+
name: attendee.displayName,
149+
responseStatus: attendee.responseStatus,
150+
})) || [],
151+
status: event.status,
152+
htmlLink: event.htmlLink,
153+
})),
135154
};
136155
} catch (error) {
137156
if (error instanceof GaxiosError) {
138157
if (error.status === 401) {
139-
throw new FederatedConnectionError(
140-
`Authorization required to access the Federated Connection`
141-
);
158+
throw new FederatedConnectionError(`Authorization required to access the Federated Connection`);
142159
}
143160
}
144161

145162
throw error;
146163
}
147164
},
148-
})
165+
}),
149166
);
150167
```
151168

@@ -266,7 +283,7 @@ import {
266283
errorSerializer,
267284
withInterruptions,
268285
} from "@auth0/ai-vercel/interrupts";
269-
import { checkUsersCalendarTool } from "@/lib/tools/google-calendar";
286+
import { getCalendarEventsTool } from "@/lib/tools/google-calendar";
270287
//... existing code
271288

272289
export async function POST(req: NextRequest) {
@@ -276,7 +293,7 @@ export async function POST(req: NextRequest) {
276293
setAIContext({ threadID: request.id });
277294

278295
const tools = {
279-
checkUsersCalendarTool,
296+
getCalendarEventsTool,
280297
};
281298

282299
return createDataStreamResponse({

0 commit comments

Comments
 (0)