Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
241 changes: 219 additions & 22 deletions apps/obsidian/src/utils/conceptConversion.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type { TFile } from "obsidian";
import type { DiscourseNode } from "~/types";
import type {
DiscourseNode,
DiscourseRelation,
DiscourseRelationType,
RelationInstance,
} from "~/types";
import type { SupabaseContext } from "./supabaseContext";
import type { DiscourseNodeInVault } from "./getDiscourseNodes";
import type { LocalConceptDataInput } from "@repo/database/inputTypes";
import type { ObsidianDiscourseNodeData } from "./syncDgNodesToSupabase";
import type { Json } from "@repo/database/dbTypes";
import DiscourseGraphPlugin from "..";

/**
* Get extra data (author, timestamps) from file metadata
Expand All @@ -14,6 +18,7 @@ const getNodeExtraData = (
file: TFile,
accountLocalId: string,
): {
/* eslint-disable @typescript-eslint/naming-convention */
author_local_id: string;
created: string;
last_modified: string;
Expand All @@ -23,6 +28,7 @@ const getNodeExtraData = (
created: new Date(file.stat.ctime).toISOString(),
last_modified: new Date(file.stat.mtime).toISOString(),
};
/* eslint-enable @typescript-eslint/naming-convention */
};

export const discourseNodeSchemaToLocalConcept = ({
Expand All @@ -34,8 +40,23 @@ export const discourseNodeSchemaToLocalConcept = ({
node: DiscourseNode;
accountLocalId: string;
}): LocalConceptDataInput => {
const { description, template, id, name, created, modified, ...otherData } =
node;
const {
description,
template,
id,
name,
created,
modified,
importedFromRid,
...otherData
} = node;
/* eslint-disable @typescript-eslint/naming-convention */
const literal_content: Record<string, Json> = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking maybe we should have a stronger type system for Concepts.literal_content and Content.metadata. We already use metadata for Wikilink import (we store the full path of the file there). with different types that we upload here (Node, Relation, schemas), it might get messy really soon if we don't enforce the type and still read data from these json columns. It's not blocking for now, we can make this a separate ticket and create the corresponding literal_content type for each Node/Relation Instance-schema separately. or if you prefer to just add the Type in this PR for relation instance and schema that works too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one I want to address later, it requires thought.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes non blocking for now

label: name,
source_data: otherData,
};
if (template) literal_content.template = template;
if (importedFromRid) literal_content.importedFromRid = importedFromRid;
return {
space_id: context.spaceId,
name,
Expand All @@ -45,11 +66,106 @@ export const discourseNodeSchemaToLocalConcept = ({
created: new Date(created).toISOString(),
last_modified: new Date(modified).toISOString(),
description: description,
literal_content: {
label: name,
template: template,
source_data: otherData,
literal_content,
/* eslint-enable @typescript-eslint/naming-convention */
};
};

const STANDARD_ROLES = ["source", "destination"];

export const discourseRelationTypeToLocalConcept = ({
context,
relationType,
accountLocalId,
}: {
context: SupabaseContext;
relationType: DiscourseRelationType;
accountLocalId: string;
}): LocalConceptDataInput => {
const {
id,
label,
complement,
created,
modified,
importedFromRid,
...otherData
} = relationType;
// eslint-disable-next-line @typescript-eslint/naming-convention
const literal_content: Record<string, Json> = {
roles: STANDARD_ROLES,
label,
complement,
// eslint-disable-next-line @typescript-eslint/naming-convention
source_data: otherData,
};
if (importedFromRid) literal_content.importedFromRid = importedFromRid;

return {
/* eslint-disable @typescript-eslint/naming-convention */
space_id: context.spaceId,
name: label,
source_local_id: id,
is_schema: true,
author_local_id: accountLocalId,
created: new Date(created).toISOString(),
last_modified: new Date(modified).toISOString(),
literal_content,
/* eslint-enable @typescript-eslint/naming-convention */
};
};

export const discourseRelationTripleSchemaToLocalConcept = ({
context,
relation,
accountLocalId,
nodeTypesById,
relationTypesById,
}: {
context: SupabaseContext;
relation: DiscourseRelation;
accountLocalId: string;
nodeTypesById: Record<string, DiscourseNode>;
relationTypesById: Record<string, DiscourseRelationType>;
}): LocalConceptDataInput | null => {
const {
id,
relationshipTypeId,
sourceId,
destinationId,
created,
modified,
importedFromRid,
} = relation;
const sourceName = nodeTypesById[sourceId]?.name ?? sourceId;
const destinationName = nodeTypesById[destinationId]?.name ?? destinationId;
const relationType = relationTypesById[relationshipTypeId];
if (!relationType) return null;
const { label, complement } = relationType;
// eslint-disable-next-line @typescript-eslint/naming-convention
const literal_content: Record<string, Json> = {
roles: STANDARD_ROLES,
label,
complement,
};
if (importedFromRid) literal_content.importedFromRid = importedFromRid;

return {
/* eslint-disable @typescript-eslint/naming-convention */
space_id: context.spaceId,
name: `${sourceName} -${label}-> ${destinationName}`,
source_local_id: id,
is_schema: true,
author_local_id: accountLocalId,
created: new Date(created).toISOString(),
last_modified: new Date(modified).toISOString(),
literal_content,
local_reference_content: {
relation_type: relationshipTypeId,
source: sourceId,
destination: destinationId,
},
/* eslint-enable @typescript-eslint/naming-convention */
};
};

Expand All @@ -66,21 +182,83 @@ export const discourseNodeInstanceToLocalConcept = ({
accountLocalId: string;
}): LocalConceptDataInput => {
const extraData = getNodeExtraData(nodeData.file, accountLocalId);
const { nodeInstanceId, nodeTypeId, ...otherData } = nodeData.frontmatter;
const { nodeInstanceId, nodeTypeId, importedFromRid, ...otherData } =
nodeData.frontmatter;
// eslint-disable-next-line @typescript-eslint/naming-convention
const literal_content: Record<string, Json> = {
label: nodeData.file.basename,
// eslint-disable-next-line @typescript-eslint/naming-convention
source_data: otherData as unknown as Json,
};
if (importedFromRid && typeof importedFromRid === "string")
literal_content.importedFromRid = importedFromRid;
return {
/* eslint-disable @typescript-eslint/naming-convention */
space_id: context.spaceId,
name: nodeData.file.path,
source_local_id: nodeInstanceId as string,
schema_represented_by_local_id: nodeTypeId as string,
is_schema: false,
literal_content: {
label: nodeData.file.basename,
source_data: otherData as unknown as Json,
},
literal_content,
/* eslint-enable @typescript-eslint/naming-convention */
...extraData,
};
};

export const relationInstanceToLocalConcept = ({
context,
relationTypesById,
allNodesById,
relationInstanceData,
}: {
context: SupabaseContext;
relationTypesById: Record<string, DiscourseRelationType>;
allNodesById: Record<string, DiscourseNodeInVault>;
relationInstanceData: RelationInstance;
}): LocalConceptDataInput | null => {
const { type, created, lastModified, source, destination, importedFromRid } =
relationInstanceData;
const relationType = relationTypesById[type];

if (!relationType) {
console.error("Missing relationType id " + type);
return null;
}
const sourceNode = allNodesById[source];
const destinationNode = allNodesById[destination];
if (sourceNode === undefined || destinationNode === undefined) {
console.error("Cannot find the nodes");
return null;
}

if (
sourceNode.frontmatter.importedFromRid ||
destinationNode.frontmatter.importedFromRid
)
return null; // punt relation to imported nodes for now.
// otherwise put the importedFromRid in source, dest.

/* eslint-disable @typescript-eslint/naming-convention */
const literal_content: Record<string, Json> = {};
if (importedFromRid) literal_content.importedFromRid = importedFromRid;
return {
space_id: context.spaceId,
name: `[[${sourceNode.file.basename}]] -${relationType.label}-> [[${destinationNode.file.basename}]]`,
source_local_id: relationInstanceData.id,
author_local_id: relationInstanceData.author,
schema_represented_by_local_id: type,
is_schema: false,
created: new Date(created).toISOString(),
last_modified: new Date(lastModified ?? created).toISOString(),
literal_content,
local_reference_content: {
source,
destination,
},
/* eslint-enable @typescript-eslint/naming-convention */
};
};

export const relatedConcepts = (concept: LocalConceptDataInput): string[] => {
const relations = Object.values(
concept.local_reference_content || {},
Expand All @@ -99,27 +277,40 @@ export const relatedConcepts = (concept: LocalConceptDataInput): string[] => {
* schema_represented_by_local_id or local_reference_content — so that id
* must equal some concept's source_local_id or it is reported as "missing".
*/
const orderConceptsRec = (
ordered: LocalConceptDataInput[],
concept: LocalConceptDataInput,
remainder: { [key: string]: LocalConceptDataInput },
): Set<string> => {
const orderConceptsRec = ({
ordered,
concept,
remainder,
processed,
}: {
ordered: LocalConceptDataInput[];
concept: LocalConceptDataInput;
remainder: { [key: string]: LocalConceptDataInput };
processed: Set<string>;
}): Set<string> => {
const relatedConceptIds = relatedConcepts(concept);
let missing: Set<string> = new Set();
while (relatedConceptIds.length > 0) {
const relatedConceptId = relatedConceptIds.shift()!;
if (processed.has(relatedConceptId)) continue;
const relatedConcept = remainder[relatedConceptId];
if (relatedConcept === undefined) {
missing.add(relatedConceptId);
} else {
missing = new Set([
...missing,
...orderConceptsRec(ordered, relatedConcept, remainder),
...orderConceptsRec({
ordered,
concept: relatedConcept,
remainder,
processed,
}),
]);
delete remainder[relatedConceptId];
}
}
ordered.push(concept);
processed.add(concept.source_local_id!);
delete remainder[concept.source_local_id!];
return missing;
};
Expand All @@ -143,14 +334,20 @@ export const orderConceptsByDependency = (
);
const ordered: LocalConceptDataInput[] = [];
let missing: Set<string> = new Set();
const processed: Set<string> = new Set();
while (Object.keys(conceptById).length > 0) {
const first = Object.values(conceptById)[0];
if (!first) break;
missing = new Set([
...missing,
...orderConceptsRec(ordered, first, conceptById),
...orderConceptsRec({
ordered,
concept: first,
remainder: conceptById,
processed,
}),
]);
if (missing.size > 0) console.error(`missing: ${[...missing]}`);
if (missing.size > 0) console.error(`missing: ${[...missing].join(", ")}`);
}
return { ordered, missing: Array.from(missing) };
};
2 changes: 1 addition & 1 deletion apps/obsidian/src/utils/relationsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type DiscourseGraphPlugin from "~/index";
import { ensureNodeInstanceId } from "~/utils/nodeInstanceId";
import { checkAndCreateFolder } from "~/utils/file";
import { getVaultId } from "./supabaseContext";
import { RelationInstance } from "../types";
import type { RelationInstance } from "~/types";

const RELATIONS_FILE_NAME = "relations.json";
const RELATIONS_FILE_VERSION = 1;
Expand Down
Loading