Skip to content

Conversation

maparent
Copy link
Collaborator

@maparent maparent commented Aug 30, 2025

https://linear.app/discourse-graphs/issue/ENG-810/repair-supabase-typing

Restore type imports in roam.
A few subsequent type fixes.
Mark remaining requires appropriately; none are likely to happen soon, but we want to indicate why.

Summary by CodeRabbit

  • Refactor
    • Improved compatibility in the Roam app by standardizing module loading.
    • Tightened ID handling (using numeric IDs and allowing nulls where appropriate) for safer data processing.
    • Adopted shared backend type definitions to increase type safety without changing behavior.
  • Style
    • Minor formatting updates with no functional impact.
  • Documentation
    • Clarified comments around module format expectations.
  • Chores
    • Cleaned up local type aliases in favor of shared types.

No user-facing behavior changes.

Copy link

linear bot commented Aug 30, 2025

Copy link

supabase bot commented Aug 30, 2025

This pull request has been ignored for the connected project zytfjzqyijgagqxrzbmz because there are no changes detected in packages/database/supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@maparent
Copy link
Collaborator Author

@CodeRabbit review

Copy link
Contributor

coderabbitai bot commented Aug 30, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

coderabbitai bot commented Aug 30, 2025

📝 Walkthrough

Walkthrough

Refactors Roam utilities to use shared database types and adjust ID handling (string→number, nullable source_local_id), while switching Roam’s Octokit imports to CommonJS require. Obsidian publish script comment updated. No functional control-flow changes; edits are type/import shifts and formatting.

Changes

Cohort / File(s) Summary of changes
Roam utils: shared DB types and ID typing
apps/roam/src/utils/cleanupOrphanedNodes.ts, apps/roam/src/utils/syncDgNodesToSupabase.ts, apps/roam/src/utils/upsertNodesAsContentWithEmbeddings.ts
Replace local aliases with imported types: DGSupabaseClient (from @repo/database/lib/client), Json and CompositeTypes (from @repo/database/dbTypes). Tighten IDs to numbers; allow source_local_id to be `string
Roam publish script: Octokit require
apps/roam/scripts/publish.ts
Swap ES module imports for Octokit/auth-app to CommonJS require; add comment explaining Roam’s module config; logic unchanged.
Obsidian publish script: comment/format
apps/obsidian/scripts/publish.ts
Update TODO comment to conditional ESM note; whitespace/format tweaks only; no codepath changes.

Sequence Diagram(s)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
apps/roam/scripts/publish.ts (1)

43-71: Don’t require GITHUB_TOKEN for read-only git commands; make redaction optional.

execGitCommand throws if GITHUB_TOKEN is unset even though commands like git rev-parse HEAD don’t need it. Make the token optional and only use it for redaction.

Apply:

-  const token = getRequiredEnvVar("GITHUB_TOKEN");
+  const token = process.env.GITHUB_TOKEN;
@@
-  } catch (error) {
-    const sanitizedError = new Error(
-      (error as Error).message.replace(token, "***"),
-    );
+  } catch (error) {
+    const msg = (error as Error).message;
+    const sanitizedError = new Error(
+      token ? msg.replace(token, "***") : msg,
+    );
🧹 Nitpick comments (5)
apps/roam/scripts/publish.ts (2)

86-93: Avoid hard-coding installationId.

Move installationId to an env var (e.g., APP_INSTALLATION_ID) to keep this script portable across installations and environments.

-      installationId: 59416220,
+      installationId: parseInt(getRequiredEnvVar("APP_INSTALLATION_ID"), 10),

88-91: Private key newline handling (optional).

If APP_PRIVATE_KEY is provided with \n escapes, normalize before passing to Octokit to avoid auth failures.

Example:

privateKey: getRequiredEnvVar("APP_PRIVATE_KEY").replace(/\\n/g, "\n")
apps/roam/src/utils/cleanupOrphanedNodes.ts (1)

24-31: Null-safe mappings on joined content look solid; add one minor guard.

Supabase typically returns [], but being defensive on schemas avoids surprises.

-    const schemaIds = schemas.map((s: { id: number }) => s.id);
+    const schemaIds = (schemas ?? []).map((s: { id: number }) => s.id);

Also applies to: 52-56, 75-77, 114-121

apps/roam/src/utils/upsertNodesAsContentWithEmbeddings.ts (1)

55-59: Implement bounded retries with backoff for transient embedding API errors.

You already noted throttling; add lightweight retries to de-flake batch uploads.

-    const response = await fetch(nextApiRoot() + "/embeddings/openai/small", {
-      method: "POST",
-      headers: { "Content-Type": "application/json" },
-      body: JSON.stringify({ input: batch }),
-    });
+    const maxAttempts = 3;
+    let response: Response | undefined;
+    for (let attempt = 1; attempt <= maxAttempts; attempt++) {
+      response = await fetch(nextApiRoot() + "/embeddings/openai/small", {
+        method: "POST",
+        headers: { "Content-Type": "application/json" },
+        body: JSON.stringify({ input: batch }),
+      });
+      if (response.ok) break;
+      const retriable = [429, 500, 502, 503, 504].includes(response.status);
+      if (!retriable || attempt === maxAttempts) break;
+      await new Promise((r) => setTimeout(r, 1000 * attempt));
+    }
apps/roam/src/utils/syncDgNodesToSupabase.ts (1)

52-57: Use the SYNC_FUNCTION constant for consistency.

Minor consistency fix; avoids magic strings.

-      s_function: "embedding",
+      s_function: SYNC_FUNCTION,
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 3e17556 and c0313ef.

📒 Files selected for processing (5)
  • apps/obsidian/scripts/publish.ts (2 hunks)
  • apps/roam/scripts/publish.ts (1 hunks)
  • apps/roam/src/utils/cleanupOrphanedNodes.ts (11 hunks)
  • apps/roam/src/utils/syncDgNodesToSupabase.ts (3 hunks)
  • apps/roam/src/utils/upsertNodesAsContentWithEmbeddings.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (6)
📚 Learning: 2025-07-19T22:34:23.619Z
Learnt from: CR
PR: DiscourseGraphs/discourse-graph#0
File: .cursor/rules/roam.mdc:0-0
Timestamp: 2025-07-19T22:34:23.619Z
Learning: Applies to apps/roam/**/*.{js,jsx,ts,tsx} : Use the roamAlphaApi documentation from https://roamresearch.com/#/app/developer-documentation/page/tIaOPdXCj when working with the Roam API

Applied to files:

  • apps/roam/scripts/publish.ts
📚 Learning: 2025-07-19T22:34:23.619Z
Learnt from: CR
PR: DiscourseGraphs/discourse-graph#0
File: .cursor/rules/roam.mdc:0-0
Timestamp: 2025-07-19T22:34:23.619Z
Learning: Applies to apps/roam/**/*.{js,jsx,ts,tsx} : Use Roam Depot/Extension API documentation from https://roamresearch.com/#/app/developer-documentation/page/y31lhjIqU when working with the Roam Extension API

Applied to files:

  • apps/roam/scripts/publish.ts
📚 Learning: 2025-05-30T14:49:24.016Z
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#182
File: apps/website/app/utils/supabase/dbUtils.ts:22-28
Timestamp: 2025-05-30T14:49:24.016Z
Learning: In apps/website/app/utils/supabase/dbUtils.ts, expanding the KNOWN_EMBEDDINGS and DEFAULT_DIMENSIONS mappings to support additional embedding models requires corresponding database model changes (creating new embedding tables), which should be scoped as separate work from API route implementations.

Applied to files:

  • apps/roam/src/utils/upsertNodesAsContentWithEmbeddings.ts
📚 Learning: 2025-06-22T10:40:52.752Z
Learnt from: sid597
PR: DiscourseGraphs/discourse-graph#232
File: apps/roam/src/utils/getAllDiscourseNodesSince.ts:18-31
Timestamp: 2025-06-22T10:40:52.752Z
Learning: In apps/roam/src/utils/getAllDiscourseNodesSince.ts, the user confirmed that querying for `?title` with `:node/title` and mapping it to the `text` field in the DiscourseGraphContent type is the correct implementation for retrieving discourse node content from Roam Research, despite it appearing to query page titles rather than block text content.

Applied to files:

  • apps/roam/src/utils/upsertNodesAsContentWithEmbeddings.ts
📚 Learning: 2025-07-21T14:22:20.752Z
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#291
File: packages/database/supabase/functions/create-space/index.ts:0-0
Timestamp: 2025-07-21T14:22:20.752Z
Learning: In the discourse-graph codebase, types.gen.ts is not accessible from Supabase edge functions, requiring duplication of types and utilities when needed in the edge function environment at packages/database/supabase/functions/.

Applied to files:

  • apps/roam/src/utils/cleanupOrphanedNodes.ts
📚 Learning: 2025-07-13T16:47:14.352Z
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#0
File: :0-0
Timestamp: 2025-07-13T16:47:14.352Z
Learning: In the discourse-graph codebase, types.gen.ts contains automatically generated database function type definitions that may have reordered signatures between versions. This reordering is expected behavior from the code generation process and should not be flagged as an issue requiring fixes.

Applied to files:

  • apps/roam/src/utils/syncDgNodesToSupabase.ts
🔇 Additional comments (11)
apps/roam/scripts/publish.ts (1)

6-9: Correct: switch to CommonJS for Octokit in Roam runtime.

Roam’s module env does require require(...) here. LGTM.

apps/obsidian/scripts/publish.ts (1)

7-10: Clarified TODO about ESM/require—good.

Comment aligns with the Roam script approach and keeps behavior clear until ESM migration.

apps/roam/src/utils/cleanupOrphanedNodes.ts (4)

1-2: Type-only imports: good step toward cleaner build output.


165-172: Order of deletes is correct (concepts referencing content first), with targeted filters.

No further action.

Also applies to: 177-187


221-241: Good: numeric IDs and explicit type guards prevent accidental null propagation.

This will reduce runtime errors during cascaded deletes.

Also applies to: 265-270, 286-288


334-352: Document cleanup keyed by space_id is a nice touch.

Prevents cross-space deletions for identical local IDs.

apps/roam/src/utils/upsertNodesAsContentWithEmbeddings.ts (2)

5-9: Strong typing via CompositeTypes is the right direction.

This improves RPC payload safety.


93-100: Upsert batching and inline embeddings shape match the DB contract.

RPC call site casting to Json is acceptable given composite typing upstream.

Also applies to: 102-121

apps/roam/src/utils/syncDgNodesToSupabase.ts (3)

22-27: Good: centralizing DB types via imports.

Reduces drift across modules.


147-162: Query formatting change only—OK.

Keeps query readable; no semantic shift.


314-320: Roam user fetch query tidy-up—OK.

Mapping to AccountLocalInput looks correct.

@maparent
Copy link
Collaborator Author

@CodeRabbit:
I think retries should be handled downstream in the vercel function.
APP_INSTALLATION_ID is a good idea but out of scope.
I'm not worried about an empty schemas.
Good catch on embeddings.

Copy link
Contributor

coderabbitai bot commented Aug 30, 2025

Tip

For best results, initiate chat on the files or code changes.

@maparent Thank you for the feedback and clarifications!

I understand your approach:

  • Retries: Makes sense to handle these downstream in the Vercel function for better separation of concerns
  • APP_INSTALLATION_ID: Good to keep this PR focused - that can be addressed separately
  • Empty schemas: Noted that this isn't a concern for your use case
  • Embeddings: Glad that catch was helpful!

Your scope management approach keeps this PR focused on the core typing improvements, which is solid practice.

🐰✨ ears perked up with understanding


✏️ Learnings added
Learnt from: maparent
URL: https://github.com/DiscourseGraphs/discourse-graph/pull/397

Timestamp: 2025-08-30T17:03:26.888Z
Learning: For maparent: Prefers to handle retries downstream in Vercel functions rather than at the source level, following separation of concerns principle.

Learnt from: maparent
URL: https://github.com/DiscourseGraphs/discourse-graph/pull/397

Timestamp: 2025-08-30T17:03:26.888Z
Learning: For maparent: Takes a focused approach to PR scope - acknowledges good suggestions that are out of scope for current PR rather than expanding scope.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

@maparent maparent requested a review from mdroidian August 30, 2025 17:04
@maparent
Copy link
Collaborator Author

Note: I was tempted to introduce a Nullable type to handle all the T | null. I did not do it because it is out of scope, and does not seem to follow this repository's code practice, but I like using some of this kind of syntax.

@maparent maparent force-pushed the eng-810-repair-supabase-typing branch from c0313ef to 0e75ac7 Compare August 30, 2025 17:19
@mdroidian mdroidian merged commit 6d837dd into main Aug 31, 2025
5 checks passed
@mdroidian mdroidian deleted the eng-810-repair-supabase-typing branch August 31, 2025 00:02
@github-project-automation github-project-automation bot moved this to Done in General Aug 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants