Skip to content
Open
2 changes: 2 additions & 0 deletions .scripts/source/grant_lakebase_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"agent_server": [
"responses",
"messages",
"conversation_aliases",
],
},
"openai": {
Expand All @@ -57,6 +58,7 @@
"agent_server": [
"responses",
"messages",
"conversation_aliases",
],
},
}
Expand Down
11 changes: 7 additions & 4 deletions agent-langgraph-advanced/agent_server/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from agent_server.prompts import SYSTEM_PROMPT
from agent_server.utils import (
_get_or_create_thread_id,
deduplicate_input,
get_user_workspace_client,
init_mcp_client,
process_agent_astream_events,
Expand Down Expand Up @@ -110,10 +111,7 @@ async def stream_handler(
if user_id:
config["configurable"]["user_id"] = user_id

input_state: dict[str, Any] = {
"messages": to_chat_completions_input([i.model_dump() for i in request.input]),
"custom_inputs": dict(request.custom_inputs or {}),
}
incoming_messages = to_chat_completions_input([i.model_dump() for i in request.input])

try:
async with lakebase_context(LAKEBASE_CONFIG) as (checkpointer, store):
Expand All @@ -123,6 +121,11 @@ async def stream_handler(
# For on-behalf-of user authentication, pass get_user_workspace_client() to init_agent.
agent = await init_agent(store=store, checkpointer=checkpointer)

input_state: dict[str, Any] = {
"messages": await deduplicate_input(agent, config, incoming_messages),
"custom_inputs": dict(request.custom_inputs or {}),
}

async for event in process_agent_astream_events(
agent.astream(input_state, config, stream_mode=["updates", "messages"])
):
Expand Down
21 changes: 21 additions & 0 deletions agent-langgraph-advanced/agent_server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ def _is_databricks_app_env() -> bool:
return bool(os.getenv("DATABRICKS_APP_NAME"))


async def deduplicate_input(
agent: Any, config: dict[str, Any], messages: list[dict[str, Any]]
) -> list[dict[str, Any]]:
"""Drop UI-echoed history when the checkpointer already holds the thread.

The chatbot UI replays the full conversation on each turn, but LangGraph's
checkpointer already has the prior messages keyed by ``thread_id``. Sending
them again duplicates everything in the agent's view. When we detect an
existing checkpoint for this thread, keep only the latest user message.
"""
if not messages:
return messages
try:
state = await agent.aget_state(config)
except Exception:
return messages
if state and state.values.get("messages"):
return messages[-1:]
return messages


def init_mcp_client(workspace_client: WorkspaceClient) -> DatabricksMultiServerMCPClient:
host_name = get_databricks_host_from_env()
return DatabricksMultiServerMCPClient(
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions agent-langgraph/scripts/grant_lakebase_permissions.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion agent-openai-advanced/agent_server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ async def deduplicate_input(request: ResponsesAgentRequest, session: AsyncDatabr
):
msg["content"] = [{"type": "output_text", "text": msg["content"], "annotations": []}]
session_items = await session.get_items()
if len(session_items) >= len(messages) - 1:
if session_items and len(messages) > 1:
return [messages[-1]]
return messages

Expand Down
2 changes: 2 additions & 0 deletions agent-openai-advanced/scripts/grant_lakebase_permissions.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions agent-openai-agents-sdk/scripts/grant_lakebase_permissions.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading