Skip to content

Commit 6c066cc

Browse files
committed
A couple of perf tweaks for enriching events
1 parent d6ae7dd commit 6c066cc

File tree

1 file changed

+22
-39
lines changed

1 file changed

+22
-39
lines changed

apps/webapp/app/v3/utils/enrichCreatableEvents.server.ts

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,5 @@
11
import type { CreatableEvent } from "../eventRepository.server";
22

3-
type StyleEnricher = {
4-
name: string;
5-
condition: (event: CreatableEvent) => boolean;
6-
enrich: (event: CreatableEvent) => Record<string, string> | undefined;
7-
};
8-
9-
// Define our style enrichers
10-
const styleEnrichers: StyleEnricher[] = [
11-
{
12-
name: "GenAI System",
13-
condition: (event) => typeof event.properties["gen_ai.system"] === "string",
14-
enrich: (event) => ({
15-
icon: `tabler-brand-${event.properties["gen_ai.system"]}`,
16-
}),
17-
},
18-
{
19-
name: "Agent workflow",
20-
condition: (event) =>
21-
typeof event.properties["name"] === "string" &&
22-
event.properties["name"].includes("Agent workflow"),
23-
enrich: () => ({
24-
icon: "tabler-brain",
25-
}),
26-
},
27-
];
28-
293
export function enrichCreatableEvents(events: CreatableEvent[]) {
304
return events.map((event) => {
315
return enrichCreatableEvent(event);
@@ -42,23 +16,22 @@ function enrichCreatableEvent(event: CreatableEvent): CreatableEvent {
4216
}
4317

4418
function enrichStyle(event: CreatableEvent) {
45-
// Keep existing style properties as base
4619
const baseStyle = event.style ?? {};
20+
const props = event.properties;
4721

48-
// Find the first matching enricher
49-
for (const enricher of styleEnrichers) {
50-
if (enricher.condition(event)) {
51-
const enrichedStyle = enricher.enrich(event);
52-
if (enrichedStyle) {
53-
return {
54-
...baseStyle,
55-
...enrichedStyle,
56-
};
57-
}
58-
}
22+
// Direct property access and early returns
23+
// GenAI System check
24+
const system = props["gen_ai.system"];
25+
if (typeof system === "string") {
26+
return { ...baseStyle, icon: `tabler-brand-${system}` };
27+
}
28+
29+
// Agent workflow check
30+
const name = props["name"];
31+
if (typeof name === "string" && name.includes("Agent workflow")) {
32+
return { ...baseStyle, icon: "tabler-brain" };
5933
}
6034

61-
// Return original style if no enricher matched
6235
return baseStyle;
6336
}
6437

@@ -70,6 +43,16 @@ function repr(value: any): string {
7043
}
7144

7245
function formatPythonStyle(template: string, values: Record<string, any>): string {
46+
// Early return if template is too long
47+
if (template.length >= 256) {
48+
return template;
49+
}
50+
51+
// Early return if no template variables present
52+
if (!template.includes("{")) {
53+
return template;
54+
}
55+
7356
return template.replace(/\{([^}]+?)(?:!r)?\}/g, (match, key) => {
7457
const hasRepr = match.endsWith("!r}");
7558
const actualKey = hasRepr ? key : key;

0 commit comments

Comments
 (0)