Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Add hook for uploading logfiles
Tomas Valenta6ec3ccff4364
diff --git a/hooks/useUploadLogs.ts b/hooks/useUploadLogs.tsnew file mode 100644index 000000000..4c781868c--- /dev/null+++ b/hooks/useUploadLogs.ts@@ -0,0 +1,30 @@+import { useCallback } from 'react'+import useSWRMutation from 'swr/mutation'++import { PostLogs, PostLogsResponse, RawFileLog } from 'utils/agentLogs'++async function handlePostLogs(url: string, { arg }: { arg: PostLogs }) {+ const response = await fetch(url, {+ method: 'POST',+ body: JSON.stringify(arg),+ headers: {+ 'Content-Type': 'application/json',+ },+ })+ return await response.json() as PostLogsResponse+}++export function useUploadLog(projectID: string) {+ const {+ trigger: upload,+ } = useSWRMutation('/api/logs', handlePostLogs)++ const uploadLog = useCallback(async (logFile: RawFileLog) => {+ await upload({+ logFiles: [logFile],+ projectID,+ })+ }, [projectID, upload])++ return uploadLog+}diff --git a/pages/api/logs/index.ts b/pages/api/logs/index.tsnew file mode 100644index 000000000..2242f630a--- /dev/null+++ b/pages/api/logs/index.ts@@ -0,0 +1,96 @@+import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs'+import { NextApiRequest, NextApiResponse } from 'next'++import { prisma } from 'db/prisma'+import { serverCreds } from 'db/credentials'+import { PostLogs } from 'utils/agentLogs'++async function postAgent(req: NextApiRequest, res: NextApiResponse) {+ const {+ logFiles,+ projectID,+ } = req.body as PostLogs++ try {+ const supabase = createServerSupabaseClient({ req, res }, serverCreds)+ const { data: { session } } = await supabase.auth.getSession()+ if (!session) {+ return res.status(401).json({+ error: 'not_authenticated',+ description: 'The user does not have an active session or is not authenticated',+ })+ }++ const user = await prisma.auth_users.findUnique({+ where: {+ id: session.user.id,+ },+ include: {+ users_teams: {+ include: {+ teams: {+ include: {+ projects: {+ select: {+ id: true,+ },+ },+ },+ },+ },+ },+ },+ })++ if (!user) {+ return res.status(401).json({+ error: 'invalid_user',+ })+ }++ const isInProject = user+ .users_teams+ .flatMap(t => t.teams.projects)+ .some(p => p.id === projectID)++ if (!isInProject) {+ return res.status(401).json({+ error: 'user_not_in_project',+ })+ }++ const logs = await prisma.logs.create({+ data: {+ data: logFiles as any,+ projects: {+ connect: {+ id: projectID,+ },+ },+ }+ })++ res.status(200).json({+ id: logs.id,+ })+ } catch (err: any) {+ console.error(err)+ res.status(500).json({ statusCode: 500, message: err.message })+ }+}++async function handler(+ req: NextApiRequest,+ res: NextApiResponse,+) {+ if (req.method === 'POST') {+ await postAgent(req, res)+ return+ }++ res.setHeader('Allow', 'POST')+ res.status(405).json({ statusCode: 405, message: 'Method Not Allowed' })+ return+}++export default handlerdiff --git a/utils/agentLogs.ts b/utils/agentLogs.tsindex 227ba609c..cb95d8f69 100644--- a/utils/agentLogs.ts+++ b/utils/agentLogs.ts@@ -37,3 +37,12 @@ export interface AssistantContext {arguments: { [argName: string]: string }}}++export interface PostLogs {+ logFiles: RawFileLog[]+ projectID: string+}++export interface PostLogsResponse {+ id: string+}