Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
60 changes: 60 additions & 0 deletions .coderabbit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
language: en-US
early_access: false
reviews:
profile: assertive
request_changes_workflow: true
high_level_summary: true
poem: false
review_status: true
collapse_walkthrough: true
path_filters:
- "!dist/**"
- "!node_modules/**"
- "!graphify-out/**"
- "!.planning/**"
- "!bun.lock"
- "!package-lock.json"
path_instructions:
- path: "src/**/*.ts"
instructions: |
Perform a deep efficiency and optimization review:
1. Identify redundant code, dead code, and over-engineered patterns
2. Suggest algorithmic improvements for performance-critical paths
3. Flag unnecessary abstractions that add complexity without value
4. Recommend modern ES2022+ syntax replacements for legacy patterns
5. Identify opportunities for lazy loading and tree-shaking
6. Review error handling for missing cases or swallowing
7. Check for memory leaks in intervals, timeouts, and event listeners
8. Validate that async/await usage is optimal vs Promise chains
9. Flag any duplicate logic that should be centralized
10. Recommend reducing LOC while preserving readability
- path: "tests/**/*.ts"
instructions: |
Review test suite for efficiency:
1. Identify slow or redundant tests
2. Check for proper isolation and mock cleanup
3. Flag tests that test implementation details instead of behavior
4. Suggest parallelization opportunities
auto_review:
enabled: true
ignore_title_keywords: []
drafts: true
base_branches: ["main"]
tools:
shellcheck:
enabled: false
ruff:
enabled: false
markdownlint:
enabled: true
github-checks:
enabled: true
timeout_ms: 90000
chat:
auto_reply: true
integrations:
jira:
usage: disabled
linear:
usage: disabled
102 changes: 50 additions & 52 deletions src/services/api-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CONFIG } from "../config.js";
import type { MemoryType } from "../types/index.js";
import { userPromptManager } from "./user-prompt/user-prompt-manager.js";
import { getAllUnresolvedConflicts, resolveConflict } from "./memory-conflicts.js";
import { safeToISOString, safeJSONParse } from "./utils/safe-transforms.js";

interface ApiResponse<T = any> {
success: boolean;
Expand Down Expand Up @@ -52,32 +53,6 @@ interface PaginatedResponse<T> {
totalPages: number;
}

function safeToISOString(timestamp: any): string {
try {
if (timestamp === null || timestamp === undefined) {
return new Date().toISOString();
}
const numValue = typeof timestamp === "bigint" ? Number(timestamp) : Number(timestamp);
if (isNaN(numValue) || numValue < 0) {
return new Date().toISOString();
}
return new Date(numValue).toISOString();
} catch {
return new Date().toISOString();
}
}

function safeJSONParse(jsonString: any): any {
if (!jsonString || typeof jsonString !== "string") {
return undefined;
}
try {
return JSON.parse(jsonString);
} catch {
return undefined;
}
}

function extractScopeFromTag(tag: string): { scope: "project"; hash: string } {
const parts = tag.split("_");
if (parts.length >= 3) {
Expand All @@ -88,8 +63,9 @@ function extractScopeFromTag(tag: string): { scope: "project"; hash: string } {
}

function getProjectPathFromTag(tag: string): string | undefined {
const projectShards = shardManager.getAllShards("project", "");
for (const shard of projectShards) {
const { scope, hash } = extractScopeFromTag(tag);
const shards = shardManager.getAllShards(scope, hash);
for (const shard of shards) {
const db = connectionManager.getConnection(shard.dbPath);
const tags = vectorSearch.getDistinctTags(db);
for (const t of tags) {
Expand Down Expand Up @@ -177,7 +153,7 @@ export async function handleListMemories(
createdAt: Number(r.created_at),
updatedAt: r.updated_at ? Number(r.updated_at) : undefined,
metadata,
linkedPromptId: metadata?.promptId,
linkedPromptId: (metadata as Record<string, unknown>)?.promptId,
displayName: r.display_name,
userName: r.user_name,
userEmail: r.user_email,
Expand Down Expand Up @@ -359,14 +335,19 @@ export async function handleDeleteMemory(
if (memory) {
if (cascade) {
const metadata = safeJSONParse(memory.metadata);
const linkedPromptId = metadata?.promptId;
const linkedPromptId = (metadata as Record<string, unknown>)?.promptId as
| string
| undefined;
if (linkedPromptId) userPromptManager.deletePrompt(linkedPromptId);
}
await vectorSearch.deleteVector(db, id, shard);
shardManager.decrementVectorCount(shard.id);
return {
success: true,
data: { deletedPrompt: cascade && !!safeJSONParse(memory.metadata)?.promptId },
data: {
deletedPrompt:
cascade && !!(safeJSONParse(memory.metadata) as Record<string, unknown>)?.promptId,
},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
};
}
}
Expand Down Expand Up @@ -569,7 +550,7 @@ export async function handleSearch(
projectName: r.projectName,
gitRepoUrl: r.gitRepoUrl,
isPinned: r.isPinned === 1,
linkedPromptId: r.metadata?.promptId,
linkedPromptId: (r.metadata as Record<string, unknown>)?.promptId as string | undefined,
}));

const combinedResults = [...formattedMemories, ...formattedPrompts].sort(
Expand Down Expand Up @@ -630,15 +611,17 @@ export async function handleSearch(
createdAt: safeToISOString(m.created_at),
updatedAt: m.updated_at ? safeToISOString(m.updated_at) : undefined,
similarity: 0,
metadata: safeJSONParse(m.metadata),
metadata: safeJSONParse(m.metadata) as Record<string, unknown>,
displayName: m.display_name,
userName: m.user_name,
userEmail: m.user_email,
projectPath: m.project_path,
projectName: m.project_name,
gitRepoUrl: m.git_repo_url,
isPinned: m.is_pinned === 1,
linkedPromptId: safeJSONParse(m.metadata)?.promptId,
linkedPromptId: (safeJSONParse(m.metadata) as Record<string, unknown>)?.promptId as
| string
| undefined,
isContext: true,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
});
}
Expand Down Expand Up @@ -963,26 +946,41 @@ export async function handleDetectTagMigration(): Promise<
}
}

interface MigrationProgress {
processed: number;
total: number;
currentBatch: number;
totalBatches: number;
isComplete: boolean;
errors: string[];
class MigrationProgressTracker {
processed = 0;
total = 0;
currentBatch = 0;
totalBatches = 0;
isComplete = true;
errors: string[] = [];

reset(): void {
this.processed = 0;
this.total = 0;
this.currentBatch = 0;
this.totalBatches = 0;
this.isComplete = true;
this.errors = [];
}

toJSON() {
return {
processed: this.processed,
total: this.total,
currentBatch: this.currentBatch,
totalBatches: this.totalBatches,
isComplete: this.isComplete,
errors: this.errors,
};
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

let migrationProgress: MigrationProgress = {
processed: 0,
total: 0,
currentBatch: 0,
totalBatches: 0,
isComplete: true,
errors: [],
};

export async function handleGetTagMigrationProgress(): Promise<ApiResponse<MigrationProgress>> {
return { success: true, data: migrationProgress };
const migrationProgress = new MigrationProgressTracker();

export async function handleGetTagMigrationProgress(): Promise<
ApiResponse<ReturnType<MigrationProgressTracker["toJSON"]>>
> {
return { success: true, data: migrationProgress.toJSON() };
}

export async function handleRunTagMigrationBatch(
Expand Down
29 changes: 1 addition & 28 deletions src/services/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,10 @@ import type { MemoryRecord } from "./sqlite/types.js";
import { calculateAllScores } from "./memory-scoring.js";
import { classifyMemory } from "./memory-lifecycle.js";
import { detectConflicts } from "./memory-conflicts.js";
import { safeToISOString, safeJSONParse } from "./utils/safe-transforms.js";

export type MemoryScope = "project" | "all-projects";

function safeToISOString(timestamp: any): string {
try {
if (timestamp === null || timestamp === undefined) {
return new Date().toISOString();
}
const numValue = typeof timestamp === "bigint" ? Number(timestamp) : Number(timestamp);

if (isNaN(numValue) || numValue < 0) {
return new Date().toISOString();
}

return new Date(numValue).toISOString();
} catch {
return new Date().toISOString();
}
}

function safeJSONParse(jsonString: any): any {
if (!jsonString || typeof jsonString !== "string") {
return undefined;
}
try {
return JSON.parse(jsonString);
} catch {
return undefined;
}
}

function extractScopeFromContainerTag(containerTag: string): {
scope: "user" | "project";
hash: string;
Expand Down
32 changes: 32 additions & 0 deletions src/services/utils/safe-transforms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Shared utility functions for safe data transformations.
* Centralized to eliminate duplication across client.ts and api-handlers.ts.
*/

export function safeToISOString(timestamp: unknown): string {
try {
if (timestamp === null || timestamp === undefined) {
return new Date().toISOString();
}
const numValue = typeof timestamp === "bigint" ? Number(timestamp) : Number(timestamp);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

if (isNaN(numValue) || numValue < 0) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
return new Date().toISOString();
}

return new Date(numValue).toISOString();
} catch {
return new Date().toISOString();
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

export function safeJSONParse(jsonString: unknown): unknown {
if (!jsonString || typeof jsonString !== "string") {
return undefined;
}
try {
return JSON.parse(jsonString);
} catch {
return undefined;
}
}
Loading