mirror of
https://github.com/supabase/supabase.git
synced 2026-09-06 09:59:03 +08:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## What kind of change does this PR introduce? Feature and bug fix. ## What is the current behavior? The assistant can call `query_logs`, but it is not given the ClickHouse schema and query-writing guidance it needs. It also lacks a current UTC reference for producing the absolute timestamps required by the tool, which can lead to valid queries being run against the wrong time range and reported as returning zero rows. ## What is the new behavior? - Adds a dedicated `logs` knowledge topic backed by the shared ClickHouse schema and query guidance. - Requires the assistant to load that knowledge before using `query_logs`. - Includes the current UTC time in project context so relative requests can be converted to correct absolute tool parameters. - Covers the new knowledge flow and context with focused tests and updates the assistant eval expectation. ## How to test 1. Check out this PR and run Studio against a project that has recent logs. Generate some project activity first, such as an API request, if needed. 2. Open the AI Assistant and ask: `Show log counts by minute for the last 15 minutes and summarize any spikes.` 3. Expand the assistant's tool activity and verify it loads the `logs` knowledge topic before calling `query_logs`. 4. Inspect the `query_logs` input and verify: - `iso_timestamp_start` and `iso_timestamp_end` are absolute UTC timestamps ending in `Z`. - The timestamps cover approximately the requested 15-minute window. - The SQL uses ClickHouse syntax, includes a `LIMIT`, and does not put the time range in the SQL `WHERE` clause. 5. Verify the assistant's summary reflects the rows returned by `query_logs` instead of reporting zero rows when results are present. ## Additional context This is the bottom PR in stack #49294. The front-end visualization is added separately in #49293. Verified with 59 focused tests across assistant context, Studio/MCP tools, query display, and logs result parsing. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added AI-assisted project log querying through the `query_logs` tool. - Added logs knowledge guidance for time ranges, schema discovery, query limits, and concise result summaries. - Project context now includes the current UTC timestamp to improve relative time-range interpretation. - Improved notebook assistance with safer table verification and appropriate handling of log queries. - **Bug Fixes** - Prevented incorrect SQL timestamp filtering and enabled cross-service searches without requiring a source filter. - Added validation for supported knowledge topics. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
72 lines
2.5 KiB
TypeScript
72 lines
2.5 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import { buildAssistantContextMessages, NO_SCHEMA_ACCESS_MESSAGE } from '@/lib/ai/assistant-context'
|
|
|
|
const SCHEMAS = 'The available database schema names are: ["public"]'
|
|
|
|
describe('buildAssistantContextMessages', () => {
|
|
it('describes the project when there is project context', () => {
|
|
const messages = buildAssistantContextMessages({
|
|
projectRef: 'abcdefghijklmnopqrst',
|
|
chatName: 'Slow queries',
|
|
schemasString: SCHEMAS,
|
|
now: new Date('2026-08-20T06:53:00.000Z'),
|
|
})
|
|
|
|
expect(messages).toHaveLength(1)
|
|
expect(messages[0].role).toBe('assistant')
|
|
expect(messages[0].content).toContain('abcdefghijklmnopqrst')
|
|
expect(messages[0].content).toContain(SCHEMAS)
|
|
expect(messages[0].content).toContain('Slow queries')
|
|
expect(messages[0].content).toContain('2026-08-20T06:53:00.000Z')
|
|
expect(messages[0].content).toContain('iso_timestamp_start')
|
|
})
|
|
|
|
it('omits the project message when there is nothing to say', () => {
|
|
const messages = buildAssistantContextMessages({
|
|
schemasString: NO_SCHEMA_ACCESS_MESSAGE,
|
|
})
|
|
|
|
expect(messages).toEqual([])
|
|
})
|
|
|
|
it('adds the support instructions in support mode', () => {
|
|
const messages = buildAssistantContextMessages({
|
|
schemasString: NO_SCHEMA_ACCESS_MESSAGE,
|
|
supportMode: true,
|
|
})
|
|
|
|
expect(messages).toHaveLength(1)
|
|
expect(messages[0].content).toContain('escalate_to_human')
|
|
})
|
|
|
|
it('adds ClickHouse instructions when the conversation attached a logs query', () => {
|
|
const messages = buildAssistantContextMessages({
|
|
projectRef: 'abcdefghijklmnopqrst',
|
|
schemasString: SCHEMAS,
|
|
includesLogsSnippets: true,
|
|
})
|
|
|
|
expect(messages).toHaveLength(2)
|
|
const logsContext = messages[1].content
|
|
// The dialect rules, so it doesn't answer in Postgres...
|
|
expect(logsContext).toContain('ClickHouse')
|
|
// ...and the table reference, so it doesn't invent BigQuery-style unnests.
|
|
expect(logsContext).toContain('log_attributes')
|
|
expect(logsContext).toContain("where source = 'edge_logs'")
|
|
expect(logsContext).toContain('query_logs')
|
|
expect(logsContext).not.toContain('cannot run')
|
|
})
|
|
|
|
it('adds nothing extra for a database-only conversation', () => {
|
|
const messages = buildAssistantContextMessages({
|
|
projectRef: 'abcdefghijklmnopqrst',
|
|
schemasString: SCHEMAS,
|
|
includesLogsSnippets: false,
|
|
})
|
|
|
|
expect(messages).toHaveLength(1)
|
|
expect(messages[0].content).not.toContain('ClickHouse')
|
|
})
|
|
})
|