|
3 | 3 | import { useQuery, useQueryClient } from '@tanstack/react-query'
|
4 | 4 | import { FeatureExtractionPipelineOptions, pipeline } from '@xenova/transformers'
|
5 | 5 | import { generateId } from 'ai'
|
6 |
| -import { useChat } from 'ai/react' |
7 | 6 | import { Chart } from 'chart.js'
|
8 | 7 | import { codeBlock } from 'common-tags'
|
9 | 8 | import { Dispatch, SetStateAction, useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
10 |
| -import { useWorkspace } from '~/components/workspace' |
11 | 9 | import { useDatabaseUpdateMutation } from '~/data/databases/database-update-mutation'
|
12 | 10 | import { useTablesQuery } from '~/data/tables/tables-query'
|
13 |
| -import { Report } from '~/lib/schema' |
14 | 11 | import { getDb } from './db'
|
15 | 12 | import { loadFile, saveFile } from './files'
|
16 | 13 | import { SmoothScroller } from './smooth-scroller'
|
17 | 14 | import { OnToolCall } from './tools'
|
18 | 15 |
|
19 |
| -export type UseReportSuggestionsOptions = { |
20 |
| - enabled?: boolean |
21 |
| -} |
22 |
| - |
23 |
| -export function useReportSuggestions({ enabled = true }: UseReportSuggestionsOptions) { |
24 |
| - const { databaseId, appendMessage } = useWorkspace() |
25 |
| - const { data: tables } = useTablesQuery({ databaseId, schemas: ['public'] }) |
26 |
| - const [reports, setReports] = useState<Report[]>() |
27 |
| - |
28 |
| - const { setMessages } = useChat({ |
29 |
| - id: databaseId, |
30 |
| - api: '/api/chat', |
31 |
| - async onToolCall({ toolCall }) { |
32 |
| - switch (toolCall.toolName) { |
33 |
| - case 'brainstormReports': { |
34 |
| - const { reports } = toolCall.args as any |
35 |
| - setReports(reports) |
36 |
| - } |
37 |
| - } |
38 |
| - }, |
39 |
| - }) |
40 |
| - |
41 |
| - useEffect(() => { |
42 |
| - if (enabled && tables) { |
43 |
| - // Provide the LLM with the current schema before invoking the tool call |
44 |
| - setMessages([ |
45 |
| - { |
46 |
| - id: generateId(), |
47 |
| - role: 'assistant', |
48 |
| - content: '', |
49 |
| - toolInvocations: [ |
50 |
| - { |
51 |
| - state: 'result', |
52 |
| - toolCallId: generateId(), |
53 |
| - toolName: 'getDatabaseSchema', |
54 |
| - args: {}, |
55 |
| - result: tables, |
56 |
| - }, |
57 |
| - ], |
58 |
| - }, |
59 |
| - ]) |
60 |
| - |
61 |
| - appendMessage({ |
62 |
| - role: 'user', |
63 |
| - content: codeBlock` |
64 |
| - Brainstorm 5 interesting charts that can be generated based on tables and their columns in the database. |
65 |
| -
|
66 |
| - Keep descriptions short and concise. Don't say "eg.". Descriptions should mention charting or visualizing. |
67 |
| -
|
68 |
| - Titles should be 4 words or less. |
69 |
| - `, |
70 |
| - }) |
71 |
| - } |
72 |
| - // eslint-disable-next-line react-hooks/exhaustive-deps |
73 |
| - }, [enabled, tables]) |
74 |
| - |
75 |
| - return { reports } |
76 |
| -} |
77 |
| - |
78 | 16 | /**
|
79 | 17 | * Hook to load/store values from local storage with an API similar
|
80 | 18 | * to `useState()`.
|
|
0 commit comments