Skip to content

Commit b03c7f7

Browse files
committed
Share time filter tool code
1 parent c6a07ef commit b03c7f7

File tree

1 file changed

+43
-68
lines changed

1 file changed

+43
-68
lines changed

apps/webapp/app/v3/services/aiQueryService.server.ts

Lines changed: 43 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,47 @@ export class AIQueryService {
5858
private readonly model: LanguageModelV1 = openai("gpt-4o-mini")
5959
) {}
6060

61+
/**
62+
* Build the setTimeFilter tool definition
63+
* Used by both streamQuery() and call() to keep behavior consistent
64+
*/
65+
private buildSetTimeFilterTool() {
66+
return tool({
67+
description:
68+
"Set the time filter for the query page UI instead of adding triggered_at conditions to the query. ALWAYS use this tool when the user wants to filter by time (e.g., 'last 7 days', 'past hour', 'yesterday'). The UI will apply this filter automatically. Do NOT add triggered_at to the WHERE clause - use this tool instead.",
69+
parameters: z.object({
70+
period: z
71+
.string()
72+
.optional()
73+
.describe(
74+
"Relative time period like '1m', '5m', '30m', '1h', '6h', '12h', '1d', '3d', '7d', '14d', '30d', '90d'. Use this for 'last X days/hours/minutes' requests."
75+
),
76+
from: z
77+
.string()
78+
.optional()
79+
.describe(
80+
"ISO 8601 timestamp for the start of an absolute date range. Use with 'to' for specific date ranges."
81+
),
82+
to: z
83+
.string()
84+
.optional()
85+
.describe(
86+
"ISO 8601 timestamp for the end of an absolute date range. Use with 'from' for specific date ranges."
87+
),
88+
}),
89+
execute: async ({ period, from, to }) => {
90+
// Store the time filter so we can include it in the result
91+
this.pendingTimeFilter = { period, from, to };
92+
return {
93+
success: true,
94+
message: period
95+
? `Time filter set to: last ${period}`
96+
: `Time filter set to: ${from ?? "start"} - ${to ?? "now"}`,
97+
};
98+
},
99+
});
100+
}
101+
61102
/**
62103
* Generate a TSQL query from natural language, streaming the result
63104
*/
@@ -104,40 +145,7 @@ export class AIQueryService {
104145
return this.getSchemaInfo(tableName);
105146
},
106147
}),
107-
setTimeFilter: tool({
108-
description:
109-
"Set the time filter for the query page UI instead of adding triggered_at conditions to the query. ALWAYS use this tool when the user wants to filter by time (e.g., 'last 7 days', 'past hour', 'yesterday'). The UI will apply this filter automatically. Do NOT add triggered_at to the WHERE clause - use this tool instead.",
110-
parameters: z.object({
111-
period: z
112-
.string()
113-
.optional()
114-
.describe(
115-
"Relative time period like '1m', '5m', '30m', '1h', '6h', '12h', '1d', '3d', '7d', '14d', '30d', '90d'. Use this for 'last X days/hours/minutes' requests."
116-
),
117-
from: z
118-
.string()
119-
.optional()
120-
.describe(
121-
"ISO 8601 timestamp for the start of an absolute date range. Use with 'to' for specific date ranges."
122-
),
123-
to: z
124-
.string()
125-
.optional()
126-
.describe(
127-
"ISO 8601 timestamp for the end of an absolute date range. Use with 'from' for specific date ranges."
128-
),
129-
}),
130-
execute: async ({ period, from, to }) => {
131-
// Store the time filter so we can include it in the result
132-
this.pendingTimeFilter = { period, from, to };
133-
return {
134-
success: true,
135-
message: period
136-
? `Time filter set to: last ${period}`
137-
: `Time filter set to: ${from ?? "start"} - ${to ?? "now"}`,
138-
};
139-
},
140-
}),
148+
setTimeFilter: this.buildSetTimeFilterTool(),
141149
},
142150
maxSteps: 5,
143151
experimental_telemetry: {
@@ -203,40 +211,7 @@ export class AIQueryService {
203211
return this.getSchemaInfo(tableName);
204212
},
205213
}),
206-
setTimeFilter: tool({
207-
description:
208-
"Set the time filter for the query page UI instead of adding triggered_at conditions to the query. ALWAYS use this tool when the user wants to filter by time (e.g., 'last 7 days', 'past hour', 'yesterday'). The UI will apply this filter automatically. Do NOT add triggered_at to the WHERE clause - use this tool instead.",
209-
parameters: z.object({
210-
period: z
211-
.string()
212-
.optional()
213-
.describe(
214-
"Relative time period like '1m', '5m', '30m', '1h', '6h', '12h', '1d', '3d', '7d', '14d', '30d', '90d'. Use this for 'last X days/hours/minutes' requests."
215-
),
216-
from: z
217-
.string()
218-
.optional()
219-
.describe(
220-
"ISO 8601 timestamp for the start of an absolute date range. Use with 'to' for specific date ranges."
221-
),
222-
to: z
223-
.string()
224-
.optional()
225-
.describe(
226-
"ISO 8601 timestamp for the end of an absolute date range. Use with 'from' for specific date ranges."
227-
),
228-
}),
229-
execute: async ({ period, from, to }) => {
230-
// Store the time filter so we can include it in the result
231-
this.pendingTimeFilter = { period, from, to };
232-
return {
233-
success: true,
234-
message: period
235-
? `Time filter set to: last ${period}`
236-
: `Time filter set to: ${from ?? "start"} - ${to ?? "now"}`,
237-
};
238-
},
239-
}),
214+
setTimeFilter: this.buildSetTimeFilterTool(),
240215
},
241216
maxSteps: 5,
242217
experimental_telemetry: {

0 commit comments

Comments
 (0)