sources only
This commit is contained in:
@@ -87,6 +87,8 @@ Rules:
|
|||||||
7. Use tools sparingly—one search usually suffices.
|
7. Use tools sparingly—one search usually suffices.
|
||||||
8. If you hit the search limit, end your reply with an italicized note: *Your question may be too broad. Try asking something more specific.* Do NOT mention "tools" or "tool limits"—the user doesn't know what those are."""
|
8. If you hit the search limit, end your reply with an italicized note: *Your question may be too broad. Try asking something more specific.* Do NOT mention "tools" or "tool limits"—the user doesn't know what those are."""
|
||||||
|
|
||||||
|
SOURCES_ONLY_INSTRUCTIONS = """SOURCES ONLY MODE: Give a 1-2 sentence summary maximum. Focus on listing sources in a bulleted list. No detailed explanations."""
|
||||||
|
|
||||||
|
|
||||||
def create_tool_call_limiter(max_calls: int = 3):
|
def create_tool_call_limiter(max_calls: int = 3):
|
||||||
"""Create a process_tool_call callback that limits tool calls."""
|
"""Create a process_tool_call callback that limits tool calls."""
|
||||||
@@ -110,7 +112,7 @@ def create_tool_call_limiter(max_calls: int = 3):
|
|||||||
return process_tool_call
|
return process_tool_call
|
||||||
|
|
||||||
|
|
||||||
def create_agent(user_roles: list[str] | None = None):
|
def create_agent(user_roles: list[str] | None = None, sources_only: bool = False):
|
||||||
"""Create an agent with MCP tools configured for the given user roles."""
|
"""Create an agent with MCP tools configured for the given user roles."""
|
||||||
toolsets = []
|
toolsets = []
|
||||||
|
|
||||||
@@ -140,10 +142,15 @@ def create_agent(user_roles: list[str] | None = None):
|
|||||||
else:
|
else:
|
||||||
logger.info("MCP server unavailable - running without MCP tools")
|
logger.info("MCP server unavailable - running without MCP tools")
|
||||||
|
|
||||||
|
# Build instructions based on mode
|
||||||
|
instructions = AGENT_INSTRUCTIONS
|
||||||
|
if sources_only:
|
||||||
|
instructions = f"{SOURCES_ONLY_INSTRUCTIONS}\n\n{AGENT_INSTRUCTIONS}"
|
||||||
|
|
||||||
return Agent(
|
return Agent(
|
||||||
model="anthropic:claude-sonnet-4-5",
|
model="anthropic:claude-sonnet-4-5",
|
||||||
toolsets=toolsets if toolsets else None,
|
toolsets=toolsets if toolsets else None,
|
||||||
instructions=AGENT_INSTRUCTIONS,
|
instructions=instructions,
|
||||||
history_processors=[limit_history],
|
history_processors=[limit_history],
|
||||||
model_settings=ModelSettings(max_tokens=4096),
|
model_settings=ModelSettings(max_tokens=4096),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -67,8 +67,13 @@ async def handle_agent_request(request: Request) -> Response:
|
|||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
logger.warning(f"Failed to parse x-user-roles header: {e}")
|
logger.warning(f"Failed to parse x-user-roles header: {e}")
|
||||||
|
|
||||||
# Create agent with the user's roles
|
# Extract sources-only mode from header
|
||||||
agent = create_agent(user_roles)
|
sources_only = request.headers.get("x-sources-only", "false") == "true"
|
||||||
|
if sources_only:
|
||||||
|
logger.info("Sources-only mode enabled")
|
||||||
|
|
||||||
|
# Create agent with the user's roles and mode
|
||||||
|
agent = create_agent(user_roles, sources_only=sources_only)
|
||||||
|
|
||||||
# Dispatch the request - tool limits handled by ToolCallLimiter in agent.py
|
# Dispatch the request - tool limits handled by ToolCallLimiter in agent.py
|
||||||
return await AGUIAdapter.dispatch_request(
|
return await AGUIAdapter.dispatch_request(
|
||||||
|
|||||||
@@ -15,13 +15,19 @@ export const POST = async (req: NextRequest) => {
|
|||||||
const session = await auth0.getSession();
|
const session = await auth0.getSession();
|
||||||
const userRoles = (session?.user?.roles as string[]) || [];
|
const userRoles = (session?.user?.roles as string[]) || [];
|
||||||
|
|
||||||
console.log("DEBUG: User roles from session:", userRoles);
|
// Get sources-only mode from query param
|
||||||
|
const url = new URL(req.url);
|
||||||
|
const sourcesOnly = url.searchParams.get("sourcesOnly") === "true";
|
||||||
|
|
||||||
// Create HttpAgent with user roles header
|
console.log("DEBUG: User roles from session:", userRoles);
|
||||||
|
console.log("DEBUG: Sources only mode:", sourcesOnly);
|
||||||
|
|
||||||
|
// Create HttpAgent with user roles and sources-only headers
|
||||||
const agent = new HttpAgent({
|
const agent = new HttpAgent({
|
||||||
url: process.env.AGENT_URL || "http://localhost:8000/",
|
url: process.env.AGENT_URL || "http://localhost:8000/",
|
||||||
headers: {
|
headers: {
|
||||||
"x-user-roles": JSON.stringify(userRoles),
|
"x-user-roles": JSON.stringify(userRoles),
|
||||||
|
"x-sources-only": sourcesOnly ? "true" : "false",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import type { Metadata } from "next";
|
import type { Metadata } from "next";
|
||||||
|
|
||||||
import { CopilotKit } from "@copilotkit/react-core";
|
|
||||||
import { Auth0Provider } from "@auth0/nextjs-auth0/client";
|
import { Auth0Provider } from "@auth0/nextjs-auth0/client";
|
||||||
import "./globals.css";
|
import "./globals.css";
|
||||||
import "@copilotkit/react-ui/styles.css";
|
import "@copilotkit/react-ui/styles.css";
|
||||||
@@ -19,9 +18,7 @@ export default function RootLayout({
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<body className={"antialiased"}>
|
<body className={"antialiased"}>
|
||||||
<Auth0Provider>
|
<Auth0Provider>
|
||||||
<CopilotKit runtimeUrl="/api/copilotkit" agent="vpi_1000">
|
{children}
|
||||||
{children}
|
|
||||||
</CopilotKit>
|
|
||||||
</Auth0Provider>
|
</Auth0Provider>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useCopilotAction, useCopilotChat } from "@copilotkit/react-core";
|
import { CopilotKit, useCopilotAction, useCopilotChat } from "@copilotkit/react-core";
|
||||||
import { CopilotKitCSSProperties, CopilotChat } from "@copilotkit/react-ui";
|
import { CopilotKitCSSProperties, CopilotChat } from "@copilotkit/react-ui";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { useUser } from "@auth0/nextjs-auth0/client";
|
import { useUser } from "@auth0/nextjs-auth0/client";
|
||||||
@@ -36,10 +36,8 @@ function LoadingOverlay() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function CopilotKitPage() {
|
// Chat content with CopilotKit hooks - must be inside CopilotKit provider
|
||||||
const [themeColor, setThemeColor] = useState("#6366f1");
|
function ChatContent({ themeColor, setThemeColor }: { themeColor: string; setThemeColor: (color: string) => void }) {
|
||||||
const { user, isLoading: authLoading } = useUser();
|
|
||||||
|
|
||||||
useCopilotAction({
|
useCopilotAction({
|
||||||
name: "setThemeColor",
|
name: "setThemeColor",
|
||||||
parameters: [{
|
parameters: [{
|
||||||
@@ -52,6 +50,35 @@ export default function CopilotKitPage() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className="flex-1 flex justify-center py-8 px-2 overflow-hidden relative"
|
||||||
|
style={{ "--copilot-kit-primary-color": themeColor } as CopilotKitCSSProperties}
|
||||||
|
>
|
||||||
|
<div className="h-full w-full max-w-5xl flex flex-col">
|
||||||
|
<CopilotChat
|
||||||
|
labels={{
|
||||||
|
title: "AI Cartwright",
|
||||||
|
initial: "Hello! I'm here to help with anything related to caving. Ask me about caves, techniques, safety, equipment, or anything else caving-related!",
|
||||||
|
}}
|
||||||
|
className="h-full w-full"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<LoadingOverlay />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function CopilotKitPage() {
|
||||||
|
const [themeColor, setThemeColor] = useState("#6366f1");
|
||||||
|
const [sourcesOnlyMode, setSourcesOnlyMode] = useState(false);
|
||||||
|
const { user, isLoading: authLoading } = useUser();
|
||||||
|
|
||||||
|
// Dynamic runtime URL based on sources-only mode
|
||||||
|
const runtimeUrl = sourcesOnlyMode
|
||||||
|
? "/api/copilotkit?sourcesOnly=true"
|
||||||
|
: "/api/copilotkit";
|
||||||
|
|
||||||
// Show loading state while checking authentication
|
// Show loading state while checking authentication
|
||||||
if (authLoading) {
|
if (authLoading) {
|
||||||
return (
|
return (
|
||||||
@@ -88,50 +115,51 @@ export default function CopilotKitPage() {
|
|||||||
|
|
||||||
// If authenticated, show the CopilotKit chat with user profile
|
// If authenticated, show the CopilotKit chat with user profile
|
||||||
return (
|
return (
|
||||||
<main
|
<CopilotKit
|
||||||
style={{ "--copilot-kit-primary-color": themeColor } as CopilotKitCSSProperties}
|
runtimeUrl={runtimeUrl}
|
||||||
className="h-screen w-screen flex flex-col bg-gray-50"
|
agent="vpi_1000"
|
||||||
|
key={sourcesOnlyMode ? "sources" : "normal"}
|
||||||
>
|
>
|
||||||
{/* Header with user profile and logout */}
|
<main className="h-screen w-screen flex flex-col bg-gray-50">
|
||||||
<div className="w-full bg-white shadow-sm border-b border-gray-200 px-4 py-3">
|
{/* Header with user profile and logout */}
|
||||||
<div className="max-w-7xl mx-auto flex justify-between items-center">
|
<div className="w-full bg-white shadow-sm border-b border-gray-200 px-4 py-3">
|
||||||
<div className="flex items-center gap-4">
|
<div className="max-w-7xl mx-auto flex justify-between items-center">
|
||||||
<h1 className="text-xl font-semibold text-gray-900">Cavepedia</h1>
|
<div className="flex items-center gap-4">
|
||||||
</div>
|
<h1 className="text-xl font-semibold text-gray-900">Cavepedia</h1>
|
||||||
<div className="flex items-center gap-4">
|
<label className="flex items-center gap-2 text-sm text-gray-600 cursor-pointer">
|
||||||
{user.picture && (
|
<input
|
||||||
<img
|
type="checkbox"
|
||||||
src={user.picture}
|
checked={sourcesOnlyMode}
|
||||||
alt={user.name || 'User'}
|
onChange={(e) => setSourcesOnlyMode(e.target.checked)}
|
||||||
className="w-8 h-8 rounded-full"
|
className="w-4 h-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500"
|
||||||
/>
|
/>
|
||||||
)}
|
Sources only
|
||||||
<div className="flex flex-col items-end">
|
</label>
|
||||||
<span className="text-sm text-gray-700">{user.name}</span>
|
</div>
|
||||||
{(user as any).roles && (user as any).roles.length > 0 && (
|
<div className="flex items-center gap-4">
|
||||||
<span className="text-xs text-gray-500">
|
{user.picture && (
|
||||||
{(user as any).roles.join(', ')}
|
<img
|
||||||
</span>
|
src={user.picture}
|
||||||
)}
|
alt={user.name || 'User'}
|
||||||
|
className="w-8 h-8 rounded-full"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<div className="flex flex-col items-end">
|
||||||
|
<span className="text-sm text-gray-700">{user.name}</span>
|
||||||
|
{(user as any).roles && (user as any).roles.length > 0 && (
|
||||||
|
<span className="text-xs text-gray-500">
|
||||||
|
{(user as any).roles.join(', ')}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<LogoutButton />
|
||||||
</div>
|
</div>
|
||||||
<LogoutButton />
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* CopilotKit Chat */}
|
{/* CopilotKit Chat */}
|
||||||
<div className="flex-1 flex justify-center py-8 px-2 overflow-hidden relative">
|
<ChatContent themeColor={themeColor} setThemeColor={setThemeColor} />
|
||||||
<div className="h-full w-full max-w-5xl flex flex-col">
|
</main>
|
||||||
<CopilotChat
|
</CopilotKit>
|
||||||
labels={{
|
|
||||||
title: "AI Cartwright",
|
|
||||||
initial: "Hello! I'm here to help with anything related to caving. Ask me about caves, techniques, safety, equipment, or anything else caving-related!",
|
|
||||||
}}
|
|
||||||
className="h-full w-full"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<LoadingOverlay />
|
|
||||||
</div>
|
|
||||||
</main>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user