- Notifications
You must be signed in to change notification settings - Fork1.1k
feat:<RequestLogsRow /> cached tokens output#20974
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Changes fromall commits
553824dbe38c10362b29ee6f320f6dc7f70374a899File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -21,6 +21,55 @@ type RequestLogsRowProps = { | ||
| interception: AIBridgeInterception; | ||
| }; | ||
| /** | ||
| * This function merges multiple objects with the same keys into a single object. | ||
| * It's super unconventional, but it's only a temporary workaround until we | ||
| * structure our metadata field for rendering in the UI. | ||
| * @param objects - The objects to merge. | ||
| * @returns The merged object. | ||
| */ | ||
| function tokenUsageMetadataMerge( | ||
| ...objects: Array<AIBridgeInterception["token_usages"][number]["metadata"]> | ||
| ): unknown { | ||
| // Filter out empty objects | ||
| const nonEmptyObjects = objects.filter((obj) => Object.keys(obj).length > 0); | ||
| // If all objects were empty, return null | ||
| if (nonEmptyObjects.length === 0) return null; | ||
| // Check if all objects have the same keys | ||
| const keySets = nonEmptyObjects.map((obj) => | ||
| Object.keys(obj).sort().join(","), | ||
| ); | ||
| // If the keys are different, just instantly return the objects | ||
| if (new Set(keySets).size > 1) return nonEmptyObjects; | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. We can still merge the fields which are common, no? | ||
| // Group the objects by key | ||
| const grouped = Object.fromEntries( | ||
| Object.keys(nonEmptyObjects[0]).map((key) => [ | ||
| key, | ||
| nonEmptyObjects.map((obj) => obj[key]), | ||
| ]), | ||
| ); | ||
| // Map the grouped values to a new object | ||
| const result = Object.fromEntries( | ||
| Object.entries(grouped).map(([key, values]: [string, unknown[]]) => { | ||
| const allNumeric = values.every((v: unknown) => typeof v === "number"); | ||
| const allSame = new Set(values).size === 1; | ||
| if (allNumeric) | ||
| return [key, values.reduce((acc, v) => acc + (v as number), 0)]; | ||
| if (allSame) return [key, values[0]]; | ||
| return [key, null]; // Mark conflict | ||
| }), | ||
| ); | ||
| return Object.values(result).some((v: unknown) => v === null) | ||
| ? nonEmptyObjects | ||
| : result; | ||
| } | ||
| export const RequestLogsRow: FC<RequestLogsRowProps> = ({ interception }) => { | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| @@ -34,6 +83,11 @@ export const RequestLogsRow: FC<RequestLogsRowProps> = ({ interception }) => { | ||
| (acc, tokenUsage) => acc + tokenUsage.output_tokens, | ||
| 0, | ||
| ); | ||
| const tokenUsagesMetadata = tokenUsageMetadataMerge( | ||
| ...interception.token_usages.map((tokenUsage) => tokenUsage.metadata), | ||
| ); | ||
| const toolCalls = interception.tool_usages.length; | ||
| const duration = | ||
| interception.ended_at && | ||
| @@ -208,6 +262,15 @@ export const RequestLogsRow: FC<RequestLogsRowProps> = ({ interception }) => { | ||
| </div> | ||
| </div> | ||
| )} | ||
| {tokenUsagesMetadata !== null && ( | ||
| <div className="flex flex-col gap-2"> | ||
| <div>Token Usage Metadata</div> | ||
| <div className="bg-surface-secondary rounded-md p-4"> | ||
| <pre>{JSON.stringify(tokenUsagesMetadata, null, 2)}</pre> | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Not really what we discussed, but probably ok for now. | ||
| </div> | ||
| </div> | ||
| )} | ||
| </div> | ||
| </TableCell> | ||
| </TableRow> | ||
Uh oh!
There was an error while loading.Please reload this page.