Skip to content

Commit 01f007f

Browse files
committed
chore(deps): update ai sdk to latest version and related deps
1 parent 7e19b65 commit 01f007f

11 files changed

Lines changed: 598 additions & 584 deletions

File tree

app/api/chat/route.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { toPrompt } from '@/lib/prompt'
55
import ratelimit from '@/lib/ratelimit'
66
import { fragmentSchema as schema } from '@/lib/schema'
77
import { Templates } from '@/lib/templates'
8-
import { streamObject, LanguageModel, CoreMessage } from 'ai'
8+
import { streamText, Output, LanguageModel, type ModelMessage } from 'ai'
99

1010
export const maxDuration = 300
1111

@@ -25,7 +25,7 @@ export async function POST(req: Request) {
2525
model,
2626
config,
2727
}: {
28-
messages: CoreMessage[]
28+
messages: ModelMessage[]
2929
userID: string | undefined
3030
teamID: string | undefined
3131
template: Templates
@@ -54,18 +54,29 @@ export async function POST(req: Request) {
5454
const { model: modelNameString, apiKey: modelApiKey, ...modelParams } = config
5555
const modelClient = getModelClient(model, config)
5656

57-
try {
58-
const stream = await streamObject({
59-
model: modelClient as LanguageModel,
60-
schema,
61-
system: toPrompt(template),
62-
messages,
63-
maxRetries: 0, // do not retry on errors
64-
...modelParams,
65-
})
57+
let apiError: any = null
58+
59+
const result = streamText({
60+
model: modelClient as LanguageModel,
61+
output: Output.object({ schema }),
62+
system: toPrompt(template),
63+
messages,
64+
maxRetries: 0,
65+
onError: ({ error }) => {
66+
apiError = error
67+
},
68+
...modelParams,
69+
})
6670

67-
return stream.toTextStreamResponse()
68-
} catch (error: any) {
69-
return handleAPIError(error, { hasOwnApiKey: !!config.apiKey })
71+
// Check if API call succeeds by awaiting first chunk
72+
try {
73+
await result.response
74+
} catch {
75+
// apiError is set by onError callback with the actual API error
76+
if (apiError) {
77+
return handleAPIError(apiError, { hasOwnApiKey: !!config.apiKey })
78+
}
7079
}
80+
81+
return result.toTextStreamResponse()
7182
}

app/api/morph-chat/route.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { Duration } from '@/lib/duration'
33
import { getModelClient, LLMModel, LLMModelConfig } from '@/lib/models'
44
import { applyPatch } from '@/lib/morph'
55
import ratelimit from '@/lib/ratelimit'
6-
import { FragmentSchema, morphEditSchema, MorphEditSchema } from '@/lib/schema'
7-
import { generateObject, LanguageModel, CoreMessage } from 'ai'
6+
import { FragmentSchema, morphEditSchema } from '@/lib/schema'
7+
import { generateText, Output, LanguageModel, type ModelMessage } from 'ai'
88

99
export const maxDuration = 300
1010

@@ -24,7 +24,7 @@ export async function POST(req: Request) {
2424
config,
2525
currentFragment,
2626
}: {
27-
messages: CoreMessage[]
27+
messages: ModelMessage[]
2828
model: LLMModel
2929
config: LLMModelConfig
3030
currentFragment: FragmentSchema
@@ -64,16 +64,16 @@ ${currentFragment.code}
6464
6565
`
6666

67-
const result = await generateObject({
67+
const result = await generateText({
6868
model: modelClient as LanguageModel,
6969
system: contextualSystemPrompt,
7070
messages,
71-
schema: morphEditSchema,
71+
output: Output.object({ schema: morphEditSchema }),
7272
maxRetries: 0,
7373
...modelParams,
7474
})
7575

76-
const editInstructions = result.object
76+
const editInstructions = result.output!
7777

7878
// Apply edits using Morph
7979
const morphResult = await applyPatch({
@@ -105,7 +105,7 @@ ${currentFragment.code}
105105
'Content-Type': 'text/plain; charset=utf-8',
106106
},
107107
})
108-
} catch (error: any) {
108+
} catch (error: unknown) {
109109
return handleAPIError(error, { hasOwnApiKey: !!config.apiKey })
110110
}
111111
}

app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { supabase } from '@/lib/supabase'
1717
import templates from '@/lib/templates'
1818
import { ExecutionResult } from '@/lib/types'
1919
import { DeepPartial } from 'ai'
20-
import { experimental_useObject as useObject } from 'ai/react'
20+
import { experimental_useObject as useObject } from '@ai-sdk/react'
2121
import { usePostHog } from 'posthog-js/react'
2222
import { SetStateAction, useEffect, useState } from 'react'
2323
import { useLocalStorage } from 'usehooks-ts'

app/providers.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use client'
22

3-
import { ThemeProvider as NextThemesProvider } from 'next-themes'
4-
import { type ThemeProviderProps } from 'next-themes/dist/types'
3+
import { ThemeProvider as NextThemesProvider, type ThemeProviderProps } from 'next-themes'
54
import posthog from 'posthog-js'
65
import { PostHogProvider as PostHogProviderJS } from 'posthog-js/react'
76

components/auth.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ function Auth({
559559
magicLink = false,
560560
onSignUpValidate,
561561
metadata,
562-
}: AuthProps): JSX.Element | null {
562+
}: AuthProps): React.ReactNode {
563563
const [authView, setAuthView] = useState<ViewType>(view)
564564
const {
565565
loading,

components/chat-settings.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,15 @@ export function ChatSettings({
124124
</span>
125125
<Input
126126
type="number"
127-
defaultValue={languageModel.maxTokens}
127+
defaultValue={languageModel.maxOutputTokens}
128128
min={50}
129129
max={10000}
130130
step={1}
131131
className="h-6 rounded-sm w-[84px] text-xs text-center tabular-nums"
132132
placeholder="Auto"
133133
onChange={(e) =>
134134
onLanguageModelChange({
135-
maxTokens: parseFloat(e.target.value) || undefined,
135+
maxOutputTokens: parseFloat(e.target.value) || undefined,
136136
})
137137
}
138138
/>

lib/api-errors.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,41 @@ export interface APIError {
77
message: string
88
}
99

10+
/**
11+
* Gets the status code from an error
12+
*/
13+
function getStatusCode(error: any): number | undefined {
14+
return error?.statusCode ?? error?.status
15+
}
16+
1017
/**
1118
* Checks if an error is a rate limit error
1219
*/
1320
export function isRateLimitError(error: any): boolean {
21+
const status = getStatusCode(error)
1422
return (
1523
error &&
16-
(error.statusCode === 429 ||
17-
error.message.toLowerCase().includes('limit') ||
18-
error.message.toLowerCase().includes('billing'))
24+
(status === 429 ||
25+
error.message?.toLowerCase().includes('limit') ||
26+
error.message?.toLowerCase().includes('billing'))
1927
)
2028
}
2129

2230
/**
2331
* Checks if an error is an overloaded/unavailable error
2432
*/
2533
export function isOverloadedError(error: any): boolean {
26-
return error && (error.statusCode === 529 || error.statusCode === 503)
34+
const status = getStatusCode(error)
35+
return error && (status === 529 || status === 503)
2736
}
2837

2938
/**
3039
* Checks if an error is an access denied/unauthorized error
3140
*/
3241
export function isAccessDeniedError(error: any): boolean {
33-
return error && (error.statusCode === 403 || error.statusCode === 401)
42+
const status = getStatusCode(error)
43+
const message = error?.message?.toLowerCase() || ''
44+
return error && (status === 403 || status === 401 || message.includes('api key') || message.includes('x-api-key') || message.includes('unauthorized'))
3445
}
3546

3647
/**
@@ -40,8 +51,7 @@ export function handleAPIError(
4051
error: any,
4152
context?: { hasOwnApiKey?: boolean },
4253
): Response {
43-
// Log the error for debugging
44-
console.error('API Error:', error)
54+
console.error('API Error:', error?.message || error)
4555

4656
if (isRateLimitError(error)) {
4757
const message = context?.hasOwnApiKey

lib/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type LLMModelConfig = {
2222
topK?: number
2323
frequencyPenalty?: number
2424
presencePenalty?: number
25-
maxTokens?: number
25+
maxOutputTokens?: number
2626
}
2727

2828
export function getModelClient(model: LLMModel, config: LLMModelConfig) {

lib/morph.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createOpenAI } from '@ai-sdk/openai'
1+
import { createOpenAICompatible } from '@ai-sdk/openai-compatible'
22
import { generateText, LanguageModel } from 'ai'
33

44
export async function applyPatch({
@@ -23,17 +23,29 @@ export async function applyPatch({
2323
)
2424
}
2525

26-
const openai = createOpenAI({
26+
const morph = createOpenAICompatible({
27+
name: 'morph',
2728
apiKey: morphApiKey,
2829
baseURL: 'https://api.morphllm.com/v1',
2930
})
3031

3132
try {
33+
console.log('[Morph] Starting applyPatch', {
34+
targetFile,
35+
instructionsLength: instructions.length,
36+
initialCodeLength: initialCode.length,
37+
codeEditLength: codeEdit.length,
38+
hasApiKey: !!morphApiKey
39+
})
40+
3241
const { text: mergedCode } = await generateText({
33-
model: openai('morph-v3-large') as LanguageModel,
42+
model: morph('morph-v3-large') as LanguageModel,
3443
prompt: `<instruction>${instructions}</instruction>\n<code>${initialCode}</code>\n<update>${codeEdit}</update>`,
44+
maxRetries: 0,
3545
})
3646

47+
console.log('[Morph] Success', { mergedCodeLength: mergedCode?.length })
48+
3749
if (!mergedCode) {
3850
throw new Error('Morph Apply returned empty content')
3951
}
@@ -43,6 +55,13 @@ export async function applyPatch({
4355
code: mergedCode,
4456
}
4557
} catch (error: any) {
58+
console.error('[Morph] Error', {
59+
message: error.message,
60+
status: error.status,
61+
cause: error.cause,
62+
name: error.name,
63+
stack: error.stack?.substring(0, 500),
64+
})
4665
if (error.message.includes('Invalid API key') || error.status === 401) {
4766
throw new Error('Invalid Morph API key. Please check your settings.')
4867
}

0 commit comments

Comments
 (0)