|
| 1 | +import { |
| 2 | + LocalRef, |
| 3 | + Ref, |
| 4 | + CrossAppEmbedding, |
| 5 | + InlineCrossAppContent, |
| 6 | + CrossAppBase, |
| 7 | + CrossAppNode, |
| 8 | +} from "../crossAppContracts"; |
| 9 | +import { LocalContentDataInput } from "../inputTypes"; |
| 10 | +import { Enums, CompositeTypes } from "../dbTypes"; |
| 11 | + |
| 12 | +type InlineEmbeddingInput = CompositeTypes<"inline_embedding_input">; |
| 13 | +type InlineAbstractBase = Partial<CrossAppBase>; |
| 14 | + |
| 15 | +const decodeLocalRef = <LocalVarName extends string>( |
| 16 | + ref: LocalRef | InlineAbstractBase | undefined, |
| 17 | + localVarName: LocalVarName, |
| 18 | +): Record<LocalVarName, string> | Record<string, never> => { |
| 19 | + if (ref === undefined) return {}; |
| 20 | + if ("localId" in ref) { |
| 21 | + return { |
| 22 | + [localVarName]: ref.localId, |
| 23 | + } as Record<LocalVarName, string>; |
| 24 | + } |
| 25 | + return {}; |
| 26 | +}; |
| 27 | + |
| 28 | +const decodeRef = <DbVarName extends string, LocalVarName extends string>( |
| 29 | + ref: Ref | undefined, |
| 30 | + dbVarName: DbVarName, |
| 31 | + localVarName: LocalVarName, |
| 32 | +): |
| 33 | + | Record<DbVarName, number> |
| 34 | + | Record<LocalVarName, string> |
| 35 | + | Record<string, never> => { |
| 36 | + if (ref === undefined) return {}; |
| 37 | + if ("dbId" in ref) |
| 38 | + return { [dbVarName]: ref.dbId } as Record<DbVarName, number>; |
| 39 | + return decodeLocalRef(ref, localVarName); |
| 40 | +}; |
| 41 | + |
| 42 | +const crossAppEmbeddingToDbEmbedding = ( |
| 43 | + embedding: CrossAppEmbedding | undefined, |
| 44 | +): InlineEmbeddingInput | undefined => |
| 45 | + embedding === undefined |
| 46 | + ? undefined |
| 47 | + : { |
| 48 | + vector: embedding.value, |
| 49 | + model: embedding.embedding || "openai_text_embedding_3_small_1536", |
| 50 | + }; |
| 51 | + |
| 52 | +const filterUndefined = <T extends Record<string, unknown>>(data: T): T => { |
| 53 | + return Object.fromEntries( |
| 54 | + Object.entries(data).filter(([, v]) => v !== undefined), |
| 55 | + ) as T; |
| 56 | +}; |
| 57 | + |
| 58 | +const inlineCrossAppContentToDbContent = ( |
| 59 | + content: InlineCrossAppContent | undefined, |
| 60 | + variant: Enums<"ContentVariant">, |
| 61 | +): LocalContentDataInput | undefined => { |
| 62 | + if (content === undefined) return undefined; |
| 63 | + return filterUndefined<LocalContentDataInput>({ |
| 64 | + ...decodeLocalRef(content, "source_local_id"), |
| 65 | + text: content.value, |
| 66 | + scale: content.scale || "document", |
| 67 | + content_type: content.contentType || "text/plain", |
| 68 | + variant, |
| 69 | + created: content.createdAt?.toISOString(), |
| 70 | + last_modified: content.modifiedAt?.toISOString(), |
| 71 | + ...decodeRef(content.author, "author_id", "author_local_id"), |
| 72 | + embedding_inline: crossAppEmbeddingToDbEmbedding(content.embedding), |
| 73 | + }); |
| 74 | +}; |
| 75 | + |
| 76 | +export const crossAppNodeToDbContent = ( |
| 77 | + node: CrossAppNode | undefined, |
| 78 | + variant: "full" | "direct", |
| 79 | +): LocalContentDataInput | undefined => { |
| 80 | + if (node === undefined) return undefined; |
| 81 | + const content = node.content[variant]; |
| 82 | + return inlineCrossAppContentToDbContent( |
| 83 | + { |
| 84 | + ...content, |
| 85 | + createdAt: content.createdAt || node.createdAt, |
| 86 | + modifiedAt: content.modifiedAt || node.modifiedAt, |
| 87 | + author: content.author || node.author, |
| 88 | + }, |
| 89 | + variant, |
| 90 | + ); |
| 91 | +}; |
0 commit comments