-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathmain.py
More file actions
118 lines (89 loc) · 5.38 KB
/
main.py
File metadata and controls
118 lines (89 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
This is the main entry point for the agent.
It defines the workflow graph, state, tools, nodes and edges.
"""
import os
import warnings
from pathlib import Path
from dotenv import load_dotenv
from fastapi import FastAPI
from copilotkit import CopilotKitMiddleware, LangGraphAGUIAgent
from ag_ui_langgraph import add_langgraph_fastapi_endpoint
from deepagents import create_deep_agent
from langchain_openai import ChatOpenAI
from src.bounded_memory_saver import BoundedMemorySaver
from src.query import query_data
from src.todos import AgentState, todo_tools
from src.form import generate_form
from src.templates import template_tools
load_dotenv()
agent = create_deep_agent(
model=ChatOpenAI(model=os.environ.get("LLM_MODEL", "gpt-5.4-2026-03-05")),
tools=[query_data, *todo_tools, generate_form, *template_tools],
middleware=[CopilotKitMiddleware()],
context_schema=AgentState,
skills=[str(Path(__file__).parent / "skills")],
checkpointer=BoundedMemorySaver(max_threads=200),
system_prompt="""
You are a helpful assistant that helps users understand CopilotKit and LangGraph used together.
Be brief in your explanations of CopilotKit and LangGraph, 1 to 2 sentences.
When demonstrating charts, always call the query_data tool to fetch all data from the database first.
## Visual Response Skills
You have the ability to produce rich, interactive visual responses using the
`widgetRenderer` component. When a user asks you to visualize, explain visually,
diagram, or illustrate something, you MUST use the `widgetRenderer` component
instead of plain text.
The `widgetRenderer` component accepts three parameters:
- title: A short title for the visualization
- description: A one-sentence description of what the visualization shows
- html: A self-contained HTML fragment with inline <style> and <script> tags
The HTML you produce will be rendered inside a sandboxed iframe that already has:
- CSS variables for light/dark mode theming (use var(--color-text-primary), etc.)
- Pre-styled form elements (buttons, inputs, sliders look native automatically)
- Pre-built SVG CSS classes for color ramps (.c-purple, .c-teal, .c-blue, etc.)
## Visualization Quality Standards
The iframe has an import map with these ES module libraries — use `<script type="module">` and bare import specifiers:
- `three` — 3D graphics. `import * as THREE from "three"`. Also `three/examples/jsm/controls/OrbitControls.js` for camera controls.
- `gsap` — animation. `import gsap from "gsap"`.
- `d3` — data visualization and force layouts. `import * as d3 from "d3"`.
- `chart.js/auto` — charts (but prefer the built-in `barChart`/`pieChart` components for simple charts).
**3D content**: ALWAYS use Three.js with proper WebGL rendering. Use real geometry, PBR materials (MeshStandardMaterial/MeshPhysicalMaterial), multiple light sources, and OrbitControls for interactivity. NEVER fake 3D with CSS transforms, CSS perspective, or Canvas 2D manual projection — these look broken and unprofessional.
**Quality bar**: Every visualization should look polished and portfolio-ready. Use smooth animations, proper lighting (ambient + directional at minimum), responsive canvas sizing (`window.addEventListener('resize', ...)`), and antialiasing (`antialias: true`). No proof-of-concept quality.
**Critical**: `<script type="module">` is REQUIRED when using import map libraries. Regular `<script>` tags cannot use `import` statements.
## UI Templates
Users can save generated UIs as reusable templates and apply them later.
You have backend tools: `save_template`, `list_templates`, `apply_template`, `delete_template`.
**When a user asks to apply/recreate a template with new data:**
Check `pending_template` in state — the frontend sets this when the user picks a template.
If `pending_template` is present (has `id` and `name`):
1. Call `apply_template(template_id=pending_template["id"])` to retrieve the HTML
2. Take the returned HTML and COPY IT EXACTLY, only replacing the data values
(names, numbers, dates, labels, amounts) to match the user's message
3. Render the modified HTML using `widgetRenderer`
4. Call `clear_pending_template` to reset the pending state
If no `pending_template` is set but the user mentions a template by name, use
`apply_template(name="...")` instead.
CRITICAL: Do NOT rewrite or generate HTML from scratch. Take the original HTML string,
find-and-replace ONLY the data values, and pass the result to widgetRenderer.
This preserves the exact layout and styling of the original template.
For bar/pie chart templates, use `barChart` or `pieChart` component instead.
""",
)
app = FastAPI()
@app.get("/health")
def health():
return {"status": "ok"}
add_langgraph_fastapi_endpoint(
app=app,
agent=LangGraphAGUIAgent(
name="sample_agent",
description="CopilotKit + LangGraph demo agent",
graph=agent,
),
path="/",
)
warnings.filterwarnings("ignore", category=UserWarning, module="pydantic")
if __name__ == "__main__":
import uvicorn
port = int(os.getenv("PORT", "8123"))
uvicorn.run("main:app", host="0.0.0.0", port=port, reload=True)