-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from DashHub-ai/feature/internal-projects-sup…
…port Add support for attaching files to messages
- Loading branch information
Showing
65 changed files
with
896 additions
and
224 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
apps/backend/src/migrations/0025-add-internal-projects-fields.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Kysely } from 'kysely'; | ||
|
||
export async function up(db: Kysely<any>): Promise<void> { | ||
await db.schema | ||
.alterTable('projects_files') | ||
.addColumn('message_id', 'uuid', col => col.references('messages.id').onDelete('restrict')) | ||
.execute(); | ||
|
||
await db.schema | ||
.createIndex('projects_files_message_id_index') | ||
.on('projects_files') | ||
.column('message_id') | ||
.execute(); | ||
|
||
await db.schema | ||
.alterTable('projects') | ||
.addColumn('internal', 'boolean', col => col.notNull().defaultTo(false)) | ||
.execute(); | ||
} | ||
|
||
export async function down(db: Kysely<any>): Promise<void> { | ||
await db.schema | ||
.alterTable('projects') | ||
.dropColumn('internal') | ||
.execute(); | ||
|
||
await db.schema | ||
.alterTable('projects_files') | ||
.dropColumn('message_id') | ||
.execute(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Buffer } from 'node:buffer'; | ||
|
||
import { either as E, taskEither as TE } from 'fp-ts'; | ||
import { pipe } from 'fp-ts/lib/function'; | ||
import { z } from 'zod'; | ||
|
||
import { tryParseUsingZodSchema } from '@llm/commons'; | ||
import { SdkInvalidFileFormatError, SdkInvalidRequestError } from '@llm/sdk'; | ||
|
||
import type { ExtractedFile } from './try-extract-single-file'; | ||
|
||
export function tryExtractFiles<T extends z.ZodRawShape>( | ||
schema?: z.ZodObject<T>, | ||
): (body: Record<string, any>) => TE.TaskEither< | ||
SdkInvalidFileFormatError | SdkInvalidRequestError, | ||
z.infer<z.ZodObject<T>> & { files: readonly ExtractedFile[]; } | ||
> { | ||
const baseSchema = z.object({}); | ||
const finalSchema = schema | ||
? baseSchema.merge(schema) | ||
: baseSchema; | ||
|
||
return (body: Record<string, any>) => pipe( | ||
TE.fromEither( | ||
pipe( | ||
body, | ||
tryParseUsingZodSchema(finalSchema), | ||
E.mapLeft(error => new SdkInvalidRequestError(error.context)), | ||
), | ||
), | ||
TE.chainW(parsedPayload => pipe( | ||
extractAllFilesFromObject(body), | ||
TE.traverseArray(file => ( | ||
TE.tryCatch( | ||
async (): Promise<ExtractedFile> => ({ | ||
buffer: Buffer.from(await file.arrayBuffer()), | ||
mimeType: file.type, | ||
fileName: file.name, | ||
}), | ||
() => new SdkInvalidFileFormatError({ | ||
name: file.name, | ||
mimeType: file.type, | ||
}), | ||
)), | ||
), | ||
TE.map(extractedFiles => ({ | ||
...parsedPayload as z.infer<z.ZodObject<T>>, | ||
files: extractedFiles, | ||
})), | ||
)), | ||
); | ||
} | ||
|
||
function extractAllFilesFromObject(obj: Record<string, any>) { | ||
return Object | ||
.values(obj) | ||
.filter(value => value instanceof File); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.