fix(service-ai): stamp agent_id on auto-created chat conversations#2243
Merged
Conversation
`/api/v1/ai/agents/:agentName/chat` auto-created its conversation via
autoCreateConversation() with only userId + metadata, leaving
ai_conversations.agent_id NULL. That makes per-agent attribution (analytics,
and cloud's per-agent AI metering) impossible — every conversation looked
agent-less.
Thread the agent through: add ToolExecutionContext.agentId (spec), set it to
the path agentName in agent-routes, and forward ctx.agentId into
conversationService.create({ agentId }) in autoCreateConversation. Additive
and backward-compatible (undefined → null, unchanged for the general /ai/chat
route and system invocations).
Test: ai-service auto-creates the conversation with the context agentId.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
📓 Docs Drift CheckThis PR changes 2 package(s): 93 hand-written doc(s) reference the affected code and may need an implementation-accuracy re-verification:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
/api/v1/ai/agents/:agentName/chatauto-creates its conversation (when the client sends noconversationId) viaautoCreateConversation(), which only setuserId+metadata— leavingai_conversations.agent_idNULL. Every chat conversation looked agent-less, so per-agent attribution (analytics + cloud's per-agent AI metering) was impossible.Change
Thread the agent end-to-end (additive, backward-compatible):
ToolExecutionContext.agentId.agentId: agentNamein the request'stoolExecutionContext.ctx.agentIdintoconversationService.create({ agentId })inautoCreateConversation.undefined → null, so the general/api/v1/ai/chatroute and system invocations are unchanged.Why (downstream)
cloud's AI token guardrail meters build vs data-Q&A separately by joining
ai_messages → ai_conversations.agent_id. With agent_id null, everything classified asbuild(fail-safe but the data-Q&A meter stayed inert). This populates the field so the split actually works for new conversations.Test
ai-service› auto-creates the conversation with the agentId from the execution context. Fullservice-aisuite green (104 + 130).🤖 Generated with Claude Code