-
Notifications
You must be signed in to change notification settings - Fork 475
feat(desktop): store humans as markdown files with YAML frontmatter #2791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2711d36
feat(desktop): store humans as markdown files with YAML frontmatter
devin-ai-integration[bot] d6bb4e7
fix: resolve TypeScript errors in human persister
devin-ai-integration[bot] 06e41ff
Merge remote-tracking branch 'origin/main' into devin/1767501906-huma…
devin-ai-integration[bot] 86c2996
refactor: use frontmatter crate via export plugin for human persister
devin-ai-integration[bot] 9e63339
chore: update pnpm-lock.yaml after removing yaml dependency
devin-ai-integration[bot] c44f5d9
style: fix dprint formatting issues
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
60 changes: 60 additions & 0 deletions
60
apps/desktop/src/store/tinybase/persister/human/collect.ts
This file contains hidden or 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,60 @@ | ||
| import type { MergeableStore, OptionalSchemas } from "tinybase/with-schemas"; | ||
|
|
||
| import type { FrontmatterInput, JsonValue } from "@hypr/plugin-export"; | ||
| import type { HumanStorage } from "@hypr/store"; | ||
|
|
||
| import type { CollectorResult, TablesContent } from "../utils"; | ||
| import { getHumanDir, getHumanFilePath } from "./utils"; | ||
|
|
||
| export interface HumanCollectorResult extends CollectorResult { | ||
| validHumanIds: Set<string>; | ||
| } | ||
|
|
||
| type HumansTable = Record<string, HumanStorage>; | ||
|
|
||
| export function collectHumanWriteOps<Schemas extends OptionalSchemas>( | ||
| _store: MergeableStore<Schemas>, | ||
| tables: TablesContent, | ||
| dataDir: string, | ||
| ): HumanCollectorResult { | ||
| const dirs = new Set<string>(); | ||
| const operations: CollectorResult["operations"] = []; | ||
| const validHumanIds = new Set<string>(); | ||
|
|
||
| const humansDir = getHumanDir(dataDir); | ||
| dirs.add(humansDir); | ||
|
|
||
| const humans = (tables as { humans?: HumansTable }).humans ?? {}; | ||
|
|
||
| const frontmatterItems: [FrontmatterInput, string][] = []; | ||
|
|
||
| for (const [humanId, human] of Object.entries(humans)) { | ||
| validHumanIds.add(humanId); | ||
|
|
||
| const { memo, ...frontmatterFields } = human; | ||
|
|
||
| const frontmatter: Record<string, JsonValue> = { | ||
| user_id: frontmatterFields.user_id ?? "", | ||
| created_at: frontmatterFields.created_at ?? "", | ||
| name: frontmatterFields.name ?? "", | ||
| email: frontmatterFields.email ?? "", | ||
| org_id: frontmatterFields.org_id ?? "", | ||
| job_title: frontmatterFields.job_title ?? "", | ||
| linkedin_username: frontmatterFields.linkedin_username ?? "", | ||
| }; | ||
|
|
||
| const body = memo ?? ""; | ||
| const filePath = getHumanFilePath(dataDir, humanId); | ||
|
|
||
| frontmatterItems.push([{ frontmatter, content: body }, filePath]); | ||
| } | ||
|
|
||
| if (frontmatterItems.length > 0) { | ||
| operations.push({ | ||
| type: "frontmatter-batch", | ||
| items: frontmatterItems, | ||
| }); | ||
| } | ||
|
|
||
| return { dirs, operations, validHumanIds }; | ||
| } |
This file contains hidden or 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,99 @@ | ||
| import { readDir, readTextFile, remove } from "@tauri-apps/plugin-fs"; | ||
|
|
||
| import type { HumanStorage } from "@hypr/store"; | ||
|
|
||
| import { isFileNotFoundError, isUUID } from "../utils"; | ||
| import { | ||
| getHumanDir, | ||
| getHumanFilePath, | ||
| parseMarkdownWithFrontmatter, | ||
| } from "./utils"; | ||
|
|
||
| export async function loadAllHumans( | ||
| dataDir: string, | ||
| ): Promise<Record<string, HumanStorage>> { | ||
| const result: Record<string, HumanStorage> = {}; | ||
| const humansDir = getHumanDir(dataDir); | ||
|
|
||
| let entries: { name: string; isDirectory: boolean }[]; | ||
| try { | ||
| entries = await readDir(humansDir); | ||
| } catch (error) { | ||
| if (isFileNotFoundError(error)) { | ||
| return result; | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| for (const entry of entries) { | ||
| if (entry.isDirectory) continue; | ||
| if (!entry.name.endsWith(".md")) continue; | ||
|
|
||
| const humanId = entry.name.replace(/\.md$/, ""); | ||
| if (!isUUID(humanId)) { | ||
| console.warn(`[HumanPersister] Skipping non-UUID file: ${entry.name}`); | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| const filePath = getHumanFilePath(dataDir, humanId); | ||
| const content = await readTextFile(filePath); | ||
| const { frontmatter, body } = await parseMarkdownWithFrontmatter(content); | ||
|
|
||
| result[humanId] = { | ||
| user_id: String(frontmatter.user_id ?? ""), | ||
| created_at: String(frontmatter.created_at ?? ""), | ||
| name: String(frontmatter.name ?? ""), | ||
| email: String(frontmatter.email ?? ""), | ||
| org_id: String(frontmatter.org_id ?? ""), | ||
| job_title: String(frontmatter.job_title ?? ""), | ||
| linkedin_username: String(frontmatter.linkedin_username ?? ""), | ||
| memo: body, | ||
| }; | ||
| } catch (error) { | ||
| console.error(`[HumanPersister] Failed to load human ${humanId}:`, error); | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| export async function cleanupOrphanHumanFiles( | ||
| dataDir: string, | ||
| validHumanIds: Set<string>, | ||
| ): Promise<void> { | ||
| const humansDir = getHumanDir(dataDir); | ||
|
|
||
| let entries: { name: string; isDirectory: boolean }[]; | ||
| try { | ||
| entries = await readDir(humansDir); | ||
| } catch (error) { | ||
| if (isFileNotFoundError(error)) { | ||
| return; | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| for (const entry of entries) { | ||
| if (entry.isDirectory) continue; | ||
| if (!entry.name.endsWith(".md")) continue; | ||
|
|
||
| const humanId = entry.name.replace(/\.md$/, ""); | ||
| if (!isUUID(humanId)) continue; | ||
|
|
||
| if (!validHumanIds.has(humanId)) { | ||
| try { | ||
| const filePath = getHumanFilePath(dataDir, humanId); | ||
| await remove(filePath); | ||
| } catch (error) { | ||
| if (!isFileNotFoundError(error)) { | ||
| console.error( | ||
| `[HumanPersister] Failed to remove orphan file ${entry.name}:`, | ||
| error, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
apps/desktop/src/store/tinybase/persister/human/migrate.ts
This file contains hidden or 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,76 @@ | ||
| import { sep } from "@tauri-apps/api/path"; | ||
| import { exists, mkdir, readTextFile, remove } from "@tauri-apps/plugin-fs"; | ||
|
|
||
| import { | ||
| commands as exportCommands, | ||
| type FrontmatterInput, | ||
| type JsonValue, | ||
| } from "@hypr/plugin-export"; | ||
| import type { HumanStorage } from "@hypr/store"; | ||
|
|
||
| import { isFileNotFoundError } from "../utils"; | ||
| import { getHumanDir, getHumanFilePath } from "./utils"; | ||
|
|
||
| export async function migrateHumansJsonIfNeeded( | ||
| dataDir: string, | ||
| ): Promise<void> { | ||
| const humansJsonPath = [dataDir, "humans.json"].join(sep()); | ||
| const humansDir = getHumanDir(dataDir); | ||
|
|
||
| const jsonExists = await exists(humansJsonPath); | ||
| if (!jsonExists) { | ||
| return; | ||
| } | ||
|
|
||
| const dirExists = await exists(humansDir); | ||
| if (dirExists) { | ||
| return; | ||
| } | ||
|
|
||
| console.log("[HumanPersister] Migrating from humans.json to humans/*.md"); | ||
|
|
||
| try { | ||
| const content = await readTextFile(humansJsonPath); | ||
| const humans = JSON.parse(content) as Record<string, HumanStorage>; | ||
|
|
||
| await mkdir(humansDir, { recursive: true }); | ||
|
|
||
| const batchItems: [FrontmatterInput, string][] = []; | ||
|
|
||
| for (const [humanId, human] of Object.entries(humans)) { | ||
| const { memo, ...frontmatterFields } = human; | ||
|
|
||
| const frontmatter: Record<string, JsonValue> = { | ||
| user_id: frontmatterFields.user_id ?? "", | ||
| created_at: frontmatterFields.created_at ?? "", | ||
| name: frontmatterFields.name ?? "", | ||
| email: frontmatterFields.email ?? "", | ||
| org_id: frontmatterFields.org_id ?? "", | ||
| job_title: frontmatterFields.job_title ?? "", | ||
| linkedin_username: frontmatterFields.linkedin_username ?? "", | ||
| }; | ||
|
|
||
| const body = memo ?? ""; | ||
| const filePath = getHumanFilePath(dataDir, humanId); | ||
|
|
||
| batchItems.push([{ frontmatter, content: body }, filePath]); | ||
| } | ||
|
|
||
| if (batchItems.length > 0) { | ||
| const result = await exportCommands.exportFrontmatterBatch(batchItems); | ||
| if (result.status === "error") { | ||
| throw new Error(`Failed to export migrated humans: ${result.error}`); | ||
| } | ||
| } | ||
|
|
||
| await remove(humansJsonPath); | ||
|
|
||
| console.log( | ||
| `[HumanPersister] Migration complete: ${Object.keys(humans).length} humans migrated`, | ||
| ); | ||
| } catch (error) { | ||
| if (!isFileNotFoundError(error)) { | ||
| console.error("[HumanPersister] Migration failed:", error); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Migration incompleteness bug: If both
humans.jsonand thehumans/directory exist, the migration exits early without deleting the old JSON file. This can occur if a previous migration was interrupted after creating the directory but before deleting the JSON file. On subsequent runs,humans.jsonwill persist indefinitely, potentially causing data inconsistency.Fix: Change the logic to attempt cleanup of
humans.jsonwhenever it exists and the directory exists:Spotted by Graphite Agent

Is this helpful? React 👍 or 👎 to let us know.