-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path+page.server.ts
More file actions
84 lines (73 loc) · 3.16 KB
/
+page.server.ts
File metadata and controls
84 lines (73 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { queryMessagesDb } from '$lib/iMessages'
import { getKhojReply } from '$lib/khoj'
import { getAnthropicReply } from '$lib/anthropic'
import { getGrokReply } from '$lib/grok'
import { logger } from '$lib/logger'
import { getOpenaiReply } from '$lib/openAi'
import { DEFAULT_PROVIDER } from '$lib/provider'
import { getAvailableProviders, hasMultipleProviders } from '$lib/providers/registry'
import { getAllSettings } from '$lib/config'
import type { Message, ToneType } from '$lib/types'
import { fail } from '@sveltejs/kit'
import type { Actions, PageServerLoad } from './$types'
const ONE_HOUR = 60 * 60 * 1000
export const load: PageServerLoad = async ({ url }) => {
const lookBack = Number.parseInt(url.searchParams.get('lookBackHours') || '1')
const end = new Date()
const start = new Date(end.getTime() - lookBack * ONE_HOUR)
const { messages } = await queryMessagesDb(start.toISOString(), end.toISOString())
const settings = await getAllSettings()
return {
messages,
multiProvider: hasMultipleProviders(),
defaultProvider: DEFAULT_PROVIDER,
availableProviders: getAvailableProviders(),
settings,
}
}
export const actions: Actions = {
generate: async ({ request }) => {
try {
const formData = await request.formData()
const messagesString = formData.get('messages') as string
const tone = formData.get('tone') as ToneType
const context = formData.get('context') as string
const provider = formData.get('provider') as string
if (!messagesString || !tone) {
return fail(400, { error: 'Invalid request format: Missing messages or tone.' })
}
const getReplies =
provider === 'khoj'
? getKhojReply
: provider === 'anthropic'
? getAnthropicReply
: provider === 'grok'
? getGrokReply
: getOpenaiReply
let messages: Message[]
try {
messages = JSON.parse(messagesString) as Message[]
} catch (err) {
logger.error({ err, messagesString }, 'Failed to parse messages')
return fail(400, {
error: 'Invalid messages format in FormData: Messages could not be parsed to an array.',
})
}
if (!Array.isArray(messages)) {
return fail(400, {
error: 'Invalid messages format: Expected an array of messages.',
})
}
const result = await getReplies(messages, tone, context || '')
logger.debug({ resultFromService: result }, 'Result received from AI service in action')
// Return the result directly - SvelteKit handles the JSON serialization
return result
} catch (err) {
logger.error({ err }, 'Error generating suggestions')
return fail(500, {
error: 'Failed to generate suggestions',
details: err instanceof Error ? err.message : String(err),
})
}
},
}