-
Notifications
You must be signed in to change notification settings - Fork 820
feat: add session agent metadata #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
flamerged
wants to merge
5
commits into
rohitg00:main
Choose a base branch
from
flamerged:feat/session-agent-metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ed2bd51
feat: add session agent metadata
flamerged 8130764
feat: show session agent metadata in viewer
flamerged 27b49f3
feat: include session attribution in recall
flamerged 0f3e0c9
fix: address session attribution review
flamerged d760fd9
Merge origin/main into feat/session-agent-metadata
flamerged File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import type { Session, SessionAttribution } from "../types.js"; | ||
|
|
||
| function asNonEmptyString(value: unknown): string | undefined { | ||
| if (typeof value !== "string") return undefined; | ||
| const trimmed = value.trim(); | ||
| return trimmed.length > 0 ? trimmed : undefined; | ||
| } | ||
|
|
||
| function agentRecord(session: Session | null | undefined): Record<string, unknown> { | ||
| const agent = session?.agent; | ||
| return agent && typeof agent === "object" && !Array.isArray(agent) | ||
| ? (agent as Record<string, unknown>) | ||
| : {}; | ||
| } | ||
|
|
||
| export function sessionAgentType(session: Session | null | undefined): string | undefined { | ||
| const agent = agentRecord(session); | ||
| return ( | ||
| asNonEmptyString(agent["agentType"]) || | ||
| asNonEmptyString(agent["role"]) || | ||
| asNonEmptyString(agent["agent_type"]) | ||
| ); | ||
| } | ||
|
|
||
| export function sessionSource(session: Session | null | undefined): string | undefined { | ||
| const agent = agentRecord(session); | ||
| return ( | ||
| asNonEmptyString(agent["sessionSource"]) || | ||
| asNonEmptyString(agent["source"]) | ||
| ); | ||
| } | ||
|
|
||
| export function sessionModel(session: Session | null | undefined): string | undefined { | ||
| const agent = agentRecord(session); | ||
| return asNonEmptyString(agent["model"]) || asNonEmptyString(session?.model); | ||
| } | ||
|
|
||
| export function sessionAttributionLabel(session: Session | null | undefined): string { | ||
| if (!session) return ""; | ||
| const agent = agentRecord(session); | ||
| const parts: string[] = []; | ||
| const client = asNonEmptyString(agent["client"]); | ||
| const role = sessionAgentType(session); | ||
| const model = sessionModel(session); | ||
| const source = sessionSource(session); | ||
| if (client) parts.push(role ? `${client}/${role}` : client); | ||
| else if (role) parts.push(role); | ||
| if (model) parts.push(model); | ||
| if (source) parts.push(`source:${source}`); | ||
| if (session.startedAt) parts.push(session.startedAt); | ||
| return parts.join(" | "); | ||
| } | ||
|
|
||
| export function compactSessionAttribution( | ||
| sessionId: string, | ||
| session: Session | null | undefined, | ||
| ): SessionAttribution { | ||
| if (!session) return { id: sessionId }; | ||
| const model = sessionModel(session); | ||
| const role = sessionAgentType(session); | ||
| const source = sessionSource(session); | ||
| return { | ||
| id: session.id, | ||
| project: session.project, | ||
| startedAt: session.startedAt, | ||
| status: session.status, | ||
| ...(model ? { model } : {}), | ||
| ...(session.agent ? { agent: session.agent } : {}), | ||
| ...(role ? { agentType: role } : {}), | ||
| ...(source ? { sessionSource: source } : {}), | ||
| ...(session.metadata ? { metadata: session.metadata } : {}), | ||
| label: sessionAttributionLabel(session), | ||
| }; | ||
| } | ||
|
|
||
| export function formatSessionHeading(session: Session): string { | ||
| const attribution = sessionAttributionLabel(session); | ||
| return attribution | ||
| ? `Session ${session.id.slice(0, 8)} - ${attribution}` | ||
| : `Session ${session.id.slice(0, 8)} (${session.startedAt})`; | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import type { AgentIdentity, SessionMetadata } from "../types.js"; | ||
| import { stripPrivateData } from "./privacy.js"; | ||
|
|
||
| type SessionMetadataResult = { | ||
| model?: string; | ||
| agent?: AgentIdentity; | ||
| metadata?: SessionMetadata; | ||
| error?: string; | ||
| }; | ||
|
|
||
| function asNonEmptyString(value: unknown): string | undefined { | ||
| if (typeof value !== "string") return undefined; | ||
| const trimmed = value.trim(); | ||
| return trimmed.length > 0 ? trimmed : undefined; | ||
| } | ||
|
|
||
| function asRecord(value: unknown): Record<string, unknown> | undefined | null { | ||
| if (value === undefined) return undefined; | ||
| if (value === null) return null; | ||
| if (typeof value !== "object" || Array.isArray(value)) return null; | ||
| return value as Record<string, unknown>; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| function redactRecord(record: Record<string, unknown>): Record<string, unknown> { | ||
| try { | ||
| return JSON.parse(stripPrivateData(JSON.stringify(record))) as Record< | ||
| string, | ||
| unknown | ||
| >; | ||
| } catch { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| export function normalizeSessionMetadata( | ||
| body: Record<string, unknown>, | ||
| ): SessionMetadataResult { | ||
| const model = asNonEmptyString(body["model"]); | ||
|
|
||
| const rawMetadata = asRecord(body["metadata"]); | ||
| if (rawMetadata === null) { | ||
| return { error: "metadata must be an object when provided" }; | ||
| } | ||
|
|
||
| const rawAgent = asRecord(body["agent"]); | ||
| if (rawAgent === null) { | ||
| return { error: "agent must be an object when provided" }; | ||
| } | ||
|
|
||
| let agent: AgentIdentity | undefined; | ||
|
|
||
| if (rawAgent) { | ||
| const client = asNonEmptyString(rawAgent["client"]); | ||
| if (!client) { | ||
| return { error: "agent.client must be a non-empty string" }; | ||
| } | ||
|
|
||
| const agentModel = asNonEmptyString(rawAgent["model"]) || model; | ||
| const agentType = | ||
| asNonEmptyString(rawAgent["agentType"]) || | ||
| asNonEmptyString(rawAgent["role"]) || | ||
| asNonEmptyString(rawAgent["agent_type"]); | ||
| const sessionSource = | ||
| asNonEmptyString(rawAgent["sessionSource"]) || | ||
| asNonEmptyString(rawAgent["source"]); | ||
|
|
||
| agent = { | ||
| client, | ||
| ...(agentModel ? { model: agentModel } : {}), | ||
| ...(agentType ? { agentType } : {}), | ||
| ...(sessionSource ? { sessionSource } : {}), | ||
| }; | ||
| } else { | ||
| const client = asNonEmptyString(body["agentClient"]); | ||
| const agentType = | ||
| asNonEmptyString(body["agentType"]) || | ||
| asNonEmptyString(body["role"]) || | ||
| asNonEmptyString(body["agent_type"]); | ||
| const sessionSource = | ||
| asNonEmptyString(body["sessionSource"]) || | ||
| asNonEmptyString(body["source"]); | ||
|
|
||
| if (client || agentType || sessionSource) { | ||
| agent = { | ||
| client: client || "unknown", | ||
| ...(model ? { model } : {}), | ||
| ...(agentType ? { agentType } : {}), | ||
| ...(sessionSource ? { sessionSource } : {}), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| ...(model ? { model } : {}), | ||
| ...(agent ? { agent } : {}), | ||
| ...(rawMetadata ? { metadata: redactRecord(rawMetadata) } : {}), | ||
| }; | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.