Files
supabase/apps/studio/lib/ai/model.utils.ts
Jordi Enric 2aa1b52234 feat(studio): add feature to rewrite queries DEBUG-145 (#47266)
## Problem

Moving the Logs Explorer to ClickHouse means users' saved BigQuery
queries no longer run.
<img width="2430" height="1010" alt="CleanShot 2026-06-29 at 11 36
04@2x"
src="https://github.com/user-attachments/assets/ae0ab155-7d3d-4ae9-81c3-22bf3a88cf8c"
/>

## Fix

Rewrite the query with AI instead of a SQL transpiler. AI handles the
long tail of nested fields and dialect differences far better than a
rule-based rewriter, and it needs no extra runtime dependency.

- `rewriteLogsSqlWithAI` posts the current query to
`/api/ai/code/complete` with `dialect: 'clickhouse'`. The endpoint skips
the Postgres schema and best-practices for that dialect and uses
logs-specific instructions and model so the output is ClickHouse logs
SQL (FROM `logs` + `source` filter, no `unnest` joins, nested fields
read from `log_attributes['...']`).
- The query's `source` is detected and its real `log_attributes` keys
are fetched and passed to the model, so it maps to exact paths instead
of guessing.
- The rewrite runs in the background and is proposed as a side-by-side
accept/discard diff in the editor. The AI Assistant panel is not opened.
- Entry points: a banner shown only for legacy-looking queries
(dismissal persisted), and a "Fix Query" button next to Field Reference.
- The Field Reference drawers discover `log_attributes` keys from real
data so the listed fields match what the source actually emits.

## Dependencies

Built on top of #47265 (Logs Explorer -> OTEL endpoint) — that is the
base branch of this PR. Merge #47265 first. Behind `otelLegacyLogs` (off
by default).

Part of DEBUG-145 (split from #47087).

## How to test

- Open the Logs Explorer with a BigQuery logs query (the templates have
some), click "Fix Query", and confirm the diff shows valid ClickHouse
SQL. Accept it and confirm the applied query runs.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added an OTEL legacy logs workflow (behind a feature flag) with an
interactive banner and a “Fix Query” ClickHouse rewrite action,
including an accept/discard diff review overlay.
* Introduced OTEL-aware field reference rendering with dynamic discovery
of `log_attributes` keys and updated OTEL source insertion behavior.
* Enabled dialect-aware SQL completion for ClickHouse logs, using
logs-specific instructions and output constraints.
* **Bug Fixes**
* Improved rewrite flow validation and handling, including log source
detection and cleanup of AI-generated SQL formatting.
* **Tests**
* Added Vitest coverage for rewrite prompt generation,
detection/classification utilities, SQL fence stripping, OTEL field
mapping, and OTEL log attribute key discovery.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-06-29 14:31:18 +02:00

173 lines
5.4 KiB
TypeScript

export type ProviderName = 'bedrock' | 'openai'
export type BedrockModel = 'anthropic.claude-3-7-sonnet-20250219-v1:0' | 'openai.gpt-oss-120b-1:0'
export type OpenAIModelId = 'gpt-5.4-nano' | 'gpt-5.3-codex'
// Source: https://developers.openai.com/api/docs/guides/reasoning + per-model pages
export type ReasoningEffort = 'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh'
// Per-model reasoning effort compatibility.
// Sources: https://developers.openai.com/api/docs/models/gpt-5.4-nano
// https://developers.openai.com/api/docs/models/gpt-5.3-codex
type ModelReasoningSupport = {
'gpt-5.4-nano': 'none' | 'low' | 'medium' | 'high' | 'xhigh'
'gpt-5.3-codex': 'low' | 'medium' | 'high' | 'xhigh'
}
type ReasoningEffortFor<ModelId extends OpenAIModelId> = ModelId extends keyof ModelReasoningSupport
? ModelReasoningSupport[ModelId]
: never
/** Type-safe factory for configuring OpenAI models with compatible reasoning efforts. */
export function openaiModelEntry<
ModelId extends OpenAIModelId,
RequiresAdvance extends boolean = false,
>(config: {
id: ModelId
/** When true, the model requires the `assistant.advance_model` entitlement (paid plans). Defaults to false. */
requiresAdvanceModelEntitlement?: RequiresAdvance
/**
* When omitted, OpenAI applies its own default reasoning effort for the model,
* which may not be zero. Use an explicit level to control cost and latency.
*/
reasoningEffort?: ReasoningEffortFor<ModelId>
}): {
id: ModelId
requiresAdvanceModelEntitlement: RequiresAdvance
reasoningEffort?: ReasoningEffortFor<ModelId>
} {
return {
requiresAdvanceModelEntitlement: false as RequiresAdvance,
...config,
}
}
export type OpenAIModelEntry = ReturnType<typeof openaiModelEntry>
/** Default model entry for simple completion endpoints where latency is more important than reasoning. */
export const DEFAULT_COMPLETION_MODEL = openaiModelEntry({
id: 'gpt-5.4-nano',
reasoningEffort: 'none',
})
export const LOGS_REWRITE_MODEL = openaiModelEntry({
id: 'gpt-5.4-nano',
reasoningEffort: 'low',
})
// Single source of truth for all Assistant chat model variants and their reasoning levels.
// Models with requiresAdvanceModelEntitlement false are available to all users; true requires the assistant.advance_model entitlement.
export const ASSISTANT_MODELS = [
openaiModelEntry({
id: 'gpt-5.4-nano',
requiresAdvanceModelEntitlement: false,
reasoningEffort: 'low',
}),
openaiModelEntry({
id: 'gpt-5.3-codex',
requiresAdvanceModelEntitlement: true,
reasoningEffort: 'low',
}),
] as const
export type AssistantBaseModelId = Extract<
(typeof ASSISTANT_MODELS)[number],
{ requiresAdvanceModelEntitlement: false }
>['id']
export type AssistantModelId = (typeof ASSISTANT_MODELS)[number]['id']
const ASSISTANT_MODELS_MAP = Object.fromEntries(ASSISTANT_MODELS.map((m) => [m.id, m])) as Record<
AssistantModelId,
(typeof ASSISTANT_MODELS)[number]
>
export const DEFAULT_ASSISTANT_BASE_MODEL_ID = 'gpt-5.4-nano' satisfies AssistantBaseModelId
export const DEFAULT_ASSISTANT_ADVANCE_MODEL_ID = 'gpt-5.3-codex' satisfies AssistantModelId
export function defaultAssistantModelId(hasAccessToAdvanceModel: boolean): AssistantModelId {
return hasAccessToAdvanceModel
? DEFAULT_ASSISTANT_ADVANCE_MODEL_ID
: DEFAULT_ASSISTANT_BASE_MODEL_ID
}
export function isKnownAssistantModelId(id: string): id is AssistantModelId {
return Object.hasOwn(ASSISTANT_MODELS_MAP, id)
}
export function isAssistantBaseModelId(id: string): id is AssistantBaseModelId {
return (
id in ASSISTANT_MODELS_MAP &&
!ASSISTANT_MODELS_MAP[id as AssistantModelId].requiresAdvanceModelEntitlement
)
}
export function isAdvanceOnlyModelId(id: string): boolean {
return (
id in ASSISTANT_MODELS_MAP &&
ASSISTANT_MODELS_MAP[id as AssistantModelId].requiresAdvanceModelEntitlement
)
}
export function getAssistantModelEntry(id: AssistantModelId): (typeof ASSISTANT_MODELS)[number] {
return ASSISTANT_MODELS_MAP[id]
}
export type Model = BedrockModel | OpenAIModelId
export type ProviderModelConfig = {
/** Optional providerOptions to attach to the system message for this model */
systemProviderOptions?: Record<string, any>
/** The default model for this provider (used when limited or no preferred specified) */
default: boolean
}
export type ProviderRegistry = {
bedrock: {
models: Record<BedrockModel, ProviderModelConfig>
providerOptions?: Record<string, any>
}
openai: {
models: Record<OpenAIModelId, ProviderModelConfig>
providerOptions?: Record<string, any>
}
}
export const PROVIDERS: ProviderRegistry = {
bedrock: {
models: {
'anthropic.claude-3-7-sonnet-20250219-v1:0': {
systemProviderOptions: {
bedrock: {
// Always cache the system prompt (must not contain dynamic content)
cachePoint: { type: 'default' },
},
},
default: false,
},
'openai.gpt-oss-120b-1:0': {
default: true,
},
},
},
openai: {
models: {
'gpt-5.3-codex': { default: false },
'gpt-5.4-nano': { default: true },
},
providerOptions: {
openai: {
store: false,
},
},
},
}
export function getDefaultModelForProvider(provider: ProviderName): Model | undefined {
const models = PROVIDERS[provider]?.models as Record<Model, ProviderModelConfig>
if (!models) return undefined
return Object.keys(models).find((id) => models[id as Model]?.default) as Model | undefined
}