Skip to content
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
8 changes: 8 additions & 0 deletions app/api/morph-chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ export async function POST(req: Request) {
currentFragment: FragmentSchema
} = await req.json()

// Validate that currentFragment exists (required for morph edits)
if (!currentFragment?.file_path || !currentFragment?.code) {
return new Response(
JSON.stringify({ error: 'currentFragment with file_path and code is required for morph edits' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
)
}

// Rate limiting (same as chat route)
const limit = !config.apiKey
? await ratelimit(
Expand Down
15 changes: 7 additions & 8 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ export default function Home() {
: { [selectedTemplate]: templates[selectedTemplate] }
const lastMessage = messages[messages.length - 1]

// Determine which API to use based on morph toggle and existing fragment
const shouldUseMorph =
useMorphApply && fragment && fragment.code && fragment.file_path
const apiEndpoint = shouldUseMorph ? '/api/morph-chat' : '/api/chat'
// Determine which API to use based on morph toggle AND whether we have a fragment to edit
const apiEndpoint = useMorphApply && fragment?.code ? '/api/morph-chat' : '/api/chat'

const { object, submit, isLoading, stop, error } = useObject({
api: apiEndpoint,
Expand Down Expand Up @@ -128,7 +126,8 @@ export default function Home() {
})

useEffect(() => {
if (object) {
// Only update messages during active streaming, not when toggling settings
if (object && isLoading) {
setFragment(object)
const content: Message['content'] = [
{ type: 'text', text: object.commentary || '' },
Expand All @@ -150,7 +149,7 @@ export default function Home() {
})
}
}
}, [object])
}, [object, isLoading])

useEffect(() => {
if (error) stop()
Expand Down Expand Up @@ -200,7 +199,7 @@ export default function Home() {
template: currentTemplate,
model: currentModel,
config: languageModel,
...(shouldUseMorph && fragment ? { currentFragment: fragment } : {}),
...(useMorphApply && fragment?.code ? { currentFragment: fragment } : {}),
})

setChatInput('')
Expand All @@ -221,7 +220,7 @@ export default function Home() {
template: currentTemplate,
model: currentModel,
config: languageModel,
...(shouldUseMorph && fragment ? { currentFragment: fragment } : {}),
...(useMorphApply && fragment?.code ? { currentFragment: fragment } : {}),
})
}

Expand Down