Skip to content
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

feat: remove initial tool call messages #73

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions apps/postgres-new/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,6 @@ import ChatMessage from './chat-message'
import SignInButton from './sign-in-button'
import { useWorkspace } from './workspace'

export function getInitialMessages(tables: TablesData): Message[] {
return [
// An artificial tool call containing the DB schema
// as if it was already called by the LLM
{
id: generateId(),
role: 'assistant',
content: '',
toolInvocations: [
{
state: 'result',
toolCallId: generateId(),
toolName: 'getDatabaseSchema',
args: {},
result: tables,
},
],
},
]
}

export default function Chat() {
const { user, isLoadingUser, focusRef, setIsSignInDialogOpen } = useApp()
const [inputFocusState, setInputFocusState] = useState(false)
Expand Down
12 changes: 4 additions & 8 deletions apps/postgres-new/components/workspace.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use client'

import { CreateMessage, Message, useChat, UseChatHelpers } from 'ai/react'
import { createContext, useCallback, useContext, useMemo } from 'react'
import { createContext, useCallback, useContext } from 'react'
import { useMessageCreateMutation } from '~/data/messages/message-create-mutation'
import { useMessagesQuery } from '~/data/messages/messages-query'
import { useTablesQuery } from '~/data/tables/tables-query'
import { useOnToolCall } from '~/lib/hooks'
import { useBreakpoint } from '~/lib/use-breakpoint'
import { ensureMessageId, ensureToolResult } from '~/lib/util'
import Chat, { getInitialMessages } from './chat'
import Chat from './chat'
import IDE from './ide'

// TODO: support public/private DBs that live in the cloud
Expand Down Expand Up @@ -61,16 +61,13 @@ export default function Workspace({
})
const { data: existingMessages, isLoading: isLoadingMessages } = useMessagesQuery(databaseId)

const initialMessages = useMemo(() => (tables ? getInitialMessages(tables) : undefined), [tables])

const { messages, setMessages, append, stop } = useChat({
id: databaseId,
api: '/api/chat',
maxToolRoundtrips: 10,
keepLastMessageOnError: true,
onToolCall: onToolCall as any, // our `OnToolCall` type is more specific than `ai` SDK's
initialMessages:
existingMessages && existingMessages.length > 0 ? existingMessages : initialMessages,
initialMessages: existingMessages && existingMessages.length > 0 ? existingMessages : [],
async onFinish(message) {
// Order is important here
await onReply?.(message, append)
Expand Down Expand Up @@ -99,8 +96,7 @@ export default function Workspace({
onCancelReply?.(append)
}, [onCancelReply, stop, append])

const isConversationStarted =
initialMessages !== undefined && messages.length > initialMessages.length
const isConversationStarted = messages.length > 0

return (
<WorkspaceContext.Provider
Expand Down