-
Notifications
You must be signed in to change notification settings - Fork 99
feat(api): add search API schema and handlers #1816
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
Merged
willbakst
merged 2 commits into
v2
from
01-06-feat_api_add_search_api_schema_definitions
Jan 9, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,129 @@ | ||
| /** | ||
| * @fileoverview Search API handlers for ClickHouse-powered search. | ||
| * | ||
| * Provides handlers for searching spans, retrieving trace details, | ||
| * and getting analytics summaries from ClickHouse. | ||
| * | ||
| * ## Authentication | ||
| * | ||
| * All endpoints require API key authentication. The environment ID | ||
| * is extracted from the API key scope. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // Handler usage (internal) | ||
| * const result = yield* searchHandler({ startTime: "...", endTime: "..." }); | ||
| * ``` | ||
| */ | ||
|
|
||
| import { Effect } from "effect"; | ||
| import { Authentication } from "@/auth"; | ||
| import { | ||
| ClickHouseSearch, | ||
| type SpanSearchInput, | ||
| type TraceDetailInput, | ||
| type AnalyticsSummaryInput, | ||
| type AttributeFilter, | ||
| } from "@/clickhouse/search"; | ||
| import type { | ||
| SearchRequest, | ||
| AnalyticsSummaryRequest, | ||
| } from "@/api/search.schemas"; | ||
|
|
||
| export * from "@/api/search.schemas"; | ||
|
|
||
| // ============================================================================= | ||
| // Handlers | ||
| // ============================================================================= | ||
|
|
||
| /** | ||
| * Search spans with filters and pagination. | ||
| * | ||
| * Requires API key authentication. Uses the environment ID from the API key scope. | ||
| */ | ||
| export const searchHandler = (payload: SearchRequest) => | ||
| Effect.gen(function* () { | ||
| const { apiKeyInfo } = yield* Authentication.ApiKey; | ||
| const searchService = yield* ClickHouseSearch; | ||
|
|
||
| // Convert readonly arrays to mutable arrays | ||
| const model = | ||
| payload.model && payload.model.length > 0 | ||
| ? [...payload.model] | ||
| : undefined; | ||
| const provider = | ||
| payload.provider && payload.provider.length > 0 | ||
| ? [...payload.provider] | ||
| : undefined; | ||
| const attributeFilters: AttributeFilter[] | undefined = | ||
| payload.attributeFilters | ||
| ? payload.attributeFilters.map((f) => ({ | ||
| key: f.key, | ||
| operator: f.operator, | ||
| value: f.value, | ||
| })) | ||
| : undefined; | ||
|
|
||
| const input: SpanSearchInput = { | ||
| environmentId: apiKeyInfo.environmentId, | ||
| startTime: new Date(payload.startTime), | ||
| endTime: new Date(payload.endTime), | ||
| query: payload.query, | ||
| traceId: payload.traceId, | ||
| spanId: payload.spanId, | ||
| model, | ||
| provider, | ||
| functionId: payload.functionId, | ||
| functionName: payload.functionName, | ||
| hasError: payload.hasError, | ||
| minTokens: payload.minTokens, | ||
| maxTokens: payload.maxTokens, | ||
| minDuration: payload.minDuration, | ||
| maxDuration: payload.maxDuration, | ||
| attributeFilters, | ||
| limit: payload.limit, | ||
| offset: payload.offset, | ||
| sortBy: payload.sortBy, | ||
| sortOrder: payload.sortOrder, | ||
| }; | ||
|
|
||
| return yield* searchService.search(input); | ||
| }); | ||
|
|
||
| /** | ||
| * Get full trace detail with all spans. | ||
| * | ||
| * Requires API key authentication. Uses the environment ID from the API key scope. | ||
| */ | ||
| export const getTraceDetailHandler = (traceId: string) => | ||
| Effect.gen(function* () { | ||
| const { apiKeyInfo } = yield* Authentication.ApiKey; | ||
| const searchService = yield* ClickHouseSearch; | ||
|
|
||
| const input: TraceDetailInput = { | ||
| environmentId: apiKeyInfo.environmentId, | ||
| traceId, | ||
| }; | ||
|
|
||
| return yield* searchService.getTraceDetail(input); | ||
| }); | ||
|
|
||
| /** | ||
| * Get analytics summary for a time range. | ||
| * | ||
| * Requires API key authentication. Uses the environment ID from the API key scope. | ||
| */ | ||
| export const getAnalyticsSummaryHandler = (params: AnalyticsSummaryRequest) => | ||
| Effect.gen(function* () { | ||
| const { apiKeyInfo } = yield* Authentication.ApiKey; | ||
| const searchService = yield* ClickHouseSearch; | ||
|
|
||
| const input: AnalyticsSummaryInput = { | ||
| environmentId: apiKeyInfo.environmentId, | ||
| startTime: new Date(params.startTime), | ||
| endTime: new Date(params.endTime), | ||
| functionId: params.functionId, | ||
| }; | ||
|
|
||
| return yield* searchService.getAnalyticsSummary(input); | ||
| }); |
Oops, something went wrong.
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.