feat: implement more routes

This commit is contained in:
Travis Fischer
2023-11-11 03:29:43 -06:00
parent a7f95df342
commit 6a57938902
8 changed files with 114 additions and 58 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npm test
npm run precommit
+1
View File
@@ -16,6 +16,7 @@
"clean": "del dist",
"dev": "tsc --watch",
"prepare": "husky install",
"precommit": "lint-staged",
"release": "np",
"test": "run-p test:*",
"test:format": "prettier --check \"**/*.{js,ts,tsx}\"",
+2
View File
@@ -61,6 +61,8 @@ model AssistantFile {
object String @default("assistant.file")
assistant Assistant @relation(fields: [assistant_id], references: [id])
@@index([assistant_id, id])
}
model Thread {
+35 -22
View File
@@ -1,27 +1,25 @@
import { OpenAPIHono } from '@hono/zod-openapi'
import * as routes from './generated/oai-routes'
import * as utils from './utils'
import { prisma } from './db'
const app: OpenAPIHono = new OpenAPIHono()
app.openapi(routes.listAssistantFiles, async (c) => {
const { assistant_id } = c.req.valid('param')
const query = c.req.valid('query')
// TODO: there is a type issue with non-string query params not being recognized
// const { after, before, limit, order } = c.req.valid('query')
// const { after, before, limit, order } = c.req.query()
console.log('listAssistantFiles', { assistant_id })
// TODO
return c.jsonT({
data: [],
first_id: '',
last_id: '',
has_more: false,
object: 'list' as const
// items: {} // TODO: why does this exist on the response type?
const params = utils.getPrismaFindManyParams(query)
const res = await prisma.assistantFile.findMany({
...params,
where: {
...params?.where,
assistant_id
}
})
return c.jsonT(utils.getPaginatedObject(res, params))
})
app.openapi(routes.createAssistantFile, async (c) => {
@@ -29,20 +27,31 @@ app.openapi(routes.createAssistantFile, async (c) => {
const body = c.req.valid('json')
console.log('createAssistantFile', { assistant_id, body })
// TODO
// TODO: are file ids the same as assistant file ids?
const res = await prisma.assistantFile.create({
data: {
id: utils.convertOAIToPrisma(body).file_id,
assistant_id
}
})
return c.jsonT({} as any)
return c.jsonT(utils.convertPrismaToOAI(res))
})
app.openapi(routes.deleteAssistantFile, async (c) => {
const { assistant_id, file_id } = c.req.valid('param')
console.log('deleteAssistantFile', { assistant_id, file_id })
// TODO
const res = await prisma.assistantFile.delete({
where: {
id: file_id,
assistant_id
}
})
return c.jsonT({
deleted: true,
id: file_id,
id: res.id,
object: 'assistant.file.deleted' as const
})
})
@@ -51,11 +60,15 @@ app.openapi(routes.getAssistantFile, async (c) => {
const { assistant_id, file_id } = c.req.valid('param')
console.log('getAssistantFile', { assistant_id, file_id })
// TODO
const res = await prisma.assistantFile.findUnique({
where: {
id: file_id,
assistant_id
}
})
return c.jsonT({
object: 'file' as const
} as any)
if (!res) return c.notFound() as any
return c.jsonT(utils.convertPrismaToOAI(res))
})
export default app
+1 -7
View File
@@ -13,13 +13,7 @@ app.openapi(routes.listAssistants, async (c) => {
const params = utils.getPrismaFindManyParams(query)
const res = await prisma.assistant.findMany(params)
return c.jsonT({
data: res.map(utils.convertPrismaToOAI),
first_id: res[0]?.id,
last_id: res[res.length - 1]?.id,
has_more: res.length >= params.take,
object: 'list' as const
})
return c.jsonT(utils.getPaginatedObject(res, params))
})
app.openapi(routes.createAssistant, async (c) => {
+39 -12
View File
@@ -1,6 +1,8 @@
import { OpenAPIHono } from '@hono/zod-openapi'
import * as routes from './generated/oai-routes'
import * as utils from './utils'
import { prisma } from './db'
const app: OpenAPIHono = new OpenAPIHono()
@@ -8,10 +10,15 @@ app.openapi(routes.listFiles, async (c) => {
const { purpose } = c.req.valid('query')
console.log('listFiles', { purpose })
// TODO
const res = await prisma.file.findMany({
where: {
purpose
}
})
return c.jsonT({
data: [],
// TODO: this cast shouldn't be necessary
data: res.map(utils.convertPrismaToOAI) as any,
object: 'list' as const
})
})
@@ -20,20 +27,37 @@ app.openapi(routes.createFile, async (c) => {
const body = c.req.valid('form')
console.log('createFile', { body })
// TODO
const { file, purpose } = body
return c.jsonT({} as any)
// TODO: process file and upload to blob store
// TODO: extract file type and infer name
// TODO: correct byte length
const res = await prisma.file.create({
data: {
bytes: file.length,
filename: 'TODO',
status: 'uploaded',
purpose
}
})
return c.jsonT(utils.convertPrismaToOAI(res))
})
app.openapi(routes.deleteFile, async (c) => {
const { file_id } = c.req.valid('param')
console.log('deleteFile', { file_id })
// TODO
const res = await prisma.file.delete({
where: {
id: file_id
}
})
return c.jsonT({
deleted: true,
id: file_id,
id: res.id,
object: 'file' as const
})
})
@@ -42,11 +66,14 @@ app.openapi(routes.retrieveFile, async (c) => {
const { file_id } = c.req.valid('param')
console.log('retrieveFile', { file_id })
// TODO
const res = await prisma.file.findUnique({
where: {
id: file_id
}
})
return c.jsonT({
object: 'file' as const
} as any)
if (!res) return c.notFound() as any
return c.jsonT(utils.convertPrismaToOAI(res))
})
app.openapi(routes.downloadFile, async (c) => {
@@ -54,8 +81,8 @@ app.openapi(routes.downloadFile, async (c) => {
console.log('downloadFile', { file_id })
// TODO
return c.json({})
c.status(501)
return null as any
})
export default app
+23 -16
View File
@@ -1,36 +1,43 @@
import { OpenAPIHono } from '@hono/zod-openapi'
import * as routes from './generated/oai-routes'
import * as utils from './utils'
import { prisma } from './db'
const app: OpenAPIHono = new OpenAPIHono()
app.openapi(routes.listMessageFiles, async (c) => {
// TODO
const { thread_id, message_id } = c.req.valid('param')
const query = c.req.valid('query')
console.log('listMessageFiles', { thread_id, message_id, ...query })
// TODO: there is a type issue with non-string query params not being recognized
// const { after, before, order } = c.req.valid('query')
// const { after, before, limit, order } = c.req.query()
console.log('listMessageFiles', { thread_id, message_id })
const params = utils.getPrismaFindManyParams(query)
const res = await prisma.messageFile.findMany(params)
// TODO
return c.jsonT({
data: [],
first_id: '',
last_id: '',
has_more: false,
object: 'list' as const
})
return c.jsonT(utils.getPaginatedObject(res, params))
})
app.openapi(routes.getMessageFile, async (c) => {
const { thread_id, message_id, file_id } = c.req.valid('param')
console.log('getMessageFile', { thread_id, message_id, file_id })
// TODO
const message = await prisma.message.findUnique({
where: {
id: message_id
}
})
if (!message) return c.notFound() as any
if (message.thread_id !== thread_id) return c.notFound() as any
return c.jsonT({ object: 'thread.message.file' as const } as any)
const messageFile = await prisma.messageFile.findUnique({
where: {
id: file_id
}
})
if (!messageFile) return c.notFound() as any
if (messageFile.message_id !== message_id) return c.notFound() as any
return c.jsonT(utils.convertPrismaToOAI(messageFile))
})
export default app
+12
View File
@@ -167,3 +167,15 @@ export function getPrismaFindManyParams({
return params
}
export function getPaginatedObject<
T extends Record<string, unknown> & { id: string }
>(data: T[], params: any) {
return {
data: data.map(convertPrismaToOAI),
first_id: data[0]?.id,
last_id: data[data.length - 1]?.id,
has_more: data.length >= params.take,
object: 'list' as const
}
}