Skip to content

Instantly share code, notes, and snippets.

@shricodev
Last active November 21, 2025 18:23
Show Gist options
  • Select an option

  • Save shricodev/0733c30fb7f90a5cf15ebb3227ddeecf to your computer and use it in GitHub Desktop.

Select an option

Save shricodev/0733c30fb7f90a5cf15ebb3227ddeecf to your computer and use it in GitHub Desktop.
OpenAI GPT-5.1 Initial Agent Route Implementation - Blog Demo
import { NextRequest, NextResponse } from "next/server";
import {
AgentRequestPayload,
AgentResponsePayload,
ChatMessage,
ToolResponse,
} from "../../../lib/types";
function createMockToolResponse(
type: "createEvent" | "listEvents" | "deleteEvent",
): ToolResponse {
const now = new Date();
const start = new Date(now.getTime() + 60 * 60 * 1000);
const end = new Date(start.getTime() + 30 * 60 * 1000);
const toIso = (date: Date) => date.toISOString();
if (type === "listEvents") {
return {
toolType: "listEvents",
result: {
successful: true,
data: {
response_data: {
summary: "Daily standup",
htmlLink: "https://calendar.google.com/",
start: { dateTime: toIso(start), timeZone: "UTC" },
end: { dateTime: toIso(end), timeZone: "UTC" },
attendees: [
{
email: "you@example.com",
self: true,
responseStatus: "accepted",
},
],
organizer: { email: "you@example.com", self: true },
},
},
},
};
}
if (type === "deleteEvent") {
return {
toolType: "deleteEvent",
result: {
successful: true,
data: {
response_data: {
summary: "Cancelled meeting",
htmlLink: "https://calendar.google.com/",
start: { dateTime: toIso(start), timeZone: "UTC" },
end: { dateTime: toIso(end), timeZone: "UTC" },
attendees: [
{
email: "you@example.com",
self: true,
responseStatus: "declined",
},
],
organizer: { email: "you@example.com", self: true },
status: "cancelled",
},
},
},
};
}
// createEvent
return {
toolType: "createEvent",
result: {
successful: true,
data: {
response_data: {
summary: "New meeting",
htmlLink: "https://calendar.google.com/",
start: { dateTime: toIso(start), timeZone: "UTC" },
end: { dateTime: toIso(end), timeZone: "UTC" },
attendees: [
{
email: "you@example.com",
self: true,
responseStatus: "accepted",
},
],
organizer: { email: "you@example.com", self: true },
},
},
},
};
}
function determineIntent(message: string):
| "createEvent"
| "listEvents"
| "deleteEvent"
| "clarify" {
const text = message.toLowerCase();
if (text.includes("cancel") || text.includes("delete")) {
return "deleteEvent";
}
if (
text.includes("what does my day") ||
text.includes("show my meetings") ||
text.includes("agenda") ||
text.includes("list")
) {
return "listEvents";
}
if (
text.includes("create") ||
text.includes("schedule") ||
text.includes("book") ||
text.includes("meeting")
) {
return "createEvent";
}
return "clarify";
}
export async function POST(req: NextRequest) {
const body = (await req.json()) as AgentRequestPayload;
const { messages } = body;
const lastUser = [...messages]
.reverse()
.find((m) => m.role === "user" && m.content.trim().length > 0);
if (!lastUser) {
const response: AgentResponsePayload = {
messages: [
{
id: "assistant-initial",
role: "assistant",
content:
"I did not receive a message. Could you rephrase your request?",
createdAt: new Date().toISOString(),
},
],
};
return NextResponse.json(response);
}
const intent = determineIntent(lastUser.content);
if (intent === "clarify") {
const clarifyMessage: ChatMessage = {
id: `assistant-${Date.now()}`,
role: "assistant",
content:
"I can create, list, and cancel Google Calendar meetings. Could you specify what you would like me to do?",
createdAt: new Date().toISOString(),
};
const response: AgentResponsePayload = {
messages: [clarifyMessage],
};
return NextResponse.json(response);
}
const toolResponse = createMockToolResponse(intent);
let naturalLanguageSummary = "";
if (intent === "createEvent") {
naturalLanguageSummary =
"Got it. I created a new meeting in your calendar based on your request.";
} else if (intent === "listEvents") {
naturalLanguageSummary =
"Here is what your calendar looks like for the requested time window.";
} else if (intent === "deleteEvent") {
naturalLanguageSummary =
"Done. I cancelled the matching meeting in your calendar.";
}
const assistantMessage: ChatMessage = {
id: `assistant-${Date.now()}`,
role: "assistant",
content: naturalLanguageSummary,
createdAt: new Date().toISOString(),
toolResponses: [toolResponse],
};
const response: AgentResponsePayload = {
messages: [assistantMessage],
toolResponses: [toolResponse],
};
return NextResponse.json(response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment