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
68 changes: 49 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,25 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
if (isFirstMessage) {
injectedSessions.add(input.sessionID);

const [profile, userMemories, projectMemoriesList] = await Promise.all([
const [profileResult, userMemoriesResult, projectMemoriesListResult] = await Promise.all([
supermemoryClient.getProfile(tags.user, userMessage),
supermemoryClient.searchMemories(userMessage, tags.user),
supermemoryClient.listMemories(tags.project, CONFIG.maxProjectMemories),
]);

const profile = profileResult.success ? profileResult : null;
const userMemories = userMemoriesResult.success ? userMemoriesResult : { results: [] };
const projectMemoriesList = projectMemoriesListResult.success ? projectMemoriesListResult : { memories: [] };

const projectMemories = {
results: (projectMemoriesList?.memories || []).map((m: any) => ({
results: (projectMemoriesList.memories || []).map((m: any) => ({
id: m.id,
memory: m.summary,
similarity: 1,
title: m.title,
metadata: m.metadata,
})),
total: projectMemoriesList?.memories?.length || 0,
total: projectMemoriesList.memories?.length || 0,
timing: 0,
};

Expand Down Expand Up @@ -264,10 +268,10 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
{ type: args.type }
);

if (!result) {
if (!result.success) {
return JSON.stringify({
success: false,
error: "Failed to add memory",
error: result.error || "Failed to add memory",
});
}

Expand All @@ -291,32 +295,51 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
const scope = args.scope;

if (scope === "user") {
const results = await supermemoryClient.searchMemories(
const result = await supermemoryClient.searchMemories(
args.query,
tags.user
);
return formatSearchResults(args.query, scope, results, args.limit);
if (!result.success) {
return JSON.stringify({
success: false,
error: result.error || "Failed to search memories",
});
}
return formatSearchResults(args.query, scope, result, args.limit);
}

if (scope === "project") {
const results = await supermemoryClient.searchMemories(
const result = await supermemoryClient.searchMemories(
args.query,
tags.project
);
return formatSearchResults(args.query, scope, results, args.limit);
if (!result.success) {
return JSON.stringify({
success: false,
error: result.error || "Failed to search memories",
});
}
return formatSearchResults(args.query, scope, result, args.limit);
}

const [userResults, projectResults] = await Promise.all([
const [userResult, projectResult] = await Promise.all([
supermemoryClient.searchMemories(args.query, tags.user),
supermemoryClient.searchMemories(args.query, tags.project),
]);

if (!userResult.success || !projectResult.success) {
return JSON.stringify({
success: false,
error: userResult.error || projectResult.error || "Failed to search memories",
});
}

const combined = [
...(userResults.results || []).map((r) => ({
...(userResult.results || []).map((r) => ({
...r,
scope: "user" as const,
})),
...(projectResults.results || []).map((r) => ({
...(projectResult.results || []).map((r) => ({
...r,
scope: "project" as const,
})),
Expand All @@ -336,23 +359,23 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
}

case "profile": {
const profile = await supermemoryClient.getProfile(
const result = await supermemoryClient.getProfile(
tags.user,
args.query
);

if (!profile) {
if (!result.success) {
return JSON.stringify({
success: false,
error: "Failed to fetch profile",
error: result.error || "Failed to fetch profile",
});
}

return JSON.stringify({
success: true,
profile: {
static: profile.profile?.static || [],
dynamic: profile.profile?.dynamic || [],
static: result.profile?.static || [],
dynamic: result.profile?.dynamic || [],
},
});
}
Expand All @@ -368,6 +391,13 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
limit
);

if (!result.success) {
return JSON.stringify({
success: false,
error: result.error || "Failed to list memories",
});
}

const memories = result.memories || [];
return JSON.stringify({
success: true,
Expand Down Expand Up @@ -396,10 +426,10 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
args.memoryId
);

if (!result) {
if (!result.success) {
return JSON.stringify({
success: false,
error: "Failed to delete memory",
error: result.error || "Failed to delete memory",
});
}

Expand Down
53 changes: 30 additions & 23 deletions src/services/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export class SupermemoryClient {
TIMEOUT_MS
);
log("searchMemories: success", { count: result.results?.length || 0 });
return result;
return { success: true as const, ...result };
} catch (error) {
log("searchMemories: error", { error: String(error) });
console.error("Supermemory: search failed", error);
return { results: [], total: 0, timing: 0 };
const errorMessage = error instanceof Error ? error.message : String(error);
log("searchMemories: error", { error: errorMessage });
return { success: false as const, error: errorMessage, results: [], total: 0, timing: 0 };
}
}

Expand All @@ -70,11 +70,11 @@ export class SupermemoryClient {
TIMEOUT_MS
);
log("getProfile: success", { hasProfile: !!result?.profile });
return result;
return { success: true as const, ...result };
} catch (error) {
log("getProfile: error", { error: String(error) });
console.error("Supermemory: profile fetch failed", error);
return null;
const errorMessage = error instanceof Error ? error.message : String(error);
log("getProfile: error", { error: errorMessage });
return { success: false as const, error: errorMessage, profile: null };
}
}

Expand All @@ -83,31 +83,38 @@ export class SupermemoryClient {
containerTag: string,
metadata?: { type?: MemoryType; tool?: string; [key: string]: unknown }
) {
log("addMemory: start", { containerTag, contentLength: content.length });
try {
return await withTimeout(
const result = await withTimeout(
this.getClient().memories.add({
content,
containerTag,
metadata: metadata as Record<string, string | number | boolean | string[]>,
}),
TIMEOUT_MS
);
log("addMemory: success", { id: result.id });
return { success: true as const, ...result };
} catch (error) {
console.error("Supermemory: add memory failed", error);
return null;
const errorMessage = error instanceof Error ? error.message : String(error);
log("addMemory: error", { error: errorMessage });
return { success: false as const, error: errorMessage };
}
}

async deleteMemory(memoryId: string) {
log("deleteMemory: start", { memoryId });
try {
await withTimeout(
this.getClient().memories.delete(memoryId),
TIMEOUT_MS
);
log("deleteMemory: success", { memoryId });
return { success: true };
} catch (error) {
console.error("Supermemory: delete memory failed", error);
return null;
const errorMessage = error instanceof Error ? error.message : String(error);
log("deleteMemory: error", { memoryId, error: errorMessage });
return { success: false, error: errorMessage };
}
}

Expand All @@ -124,11 +131,11 @@ export class SupermemoryClient {
TIMEOUT_MS
);
log("listMemories: success", { count: result.memories?.length || 0 });
return result;
return { success: true as const, ...result };
} catch (error) {
log("listMemories: error", { error: String(error) });
console.error("Supermemory: list memories failed", error);
return { memories: [], pagination: { currentPage: 1, totalItems: 0, totalPages: 0 } };
const errorMessage = error instanceof Error ? error.message : String(error);
log("listMemories: error", { error: errorMessage });
return { success: false as const, error: errorMessage, memories: [], pagination: { currentPage: 1, totalItems: 0, totalPages: 0 } };
}
}

Expand All @@ -137,7 +144,7 @@ export class SupermemoryClient {
messages: ConversationMessage[],
containerTags: string[],
metadata?: Record<string, string | number | boolean>
): Promise<ConversationIngestResponse | null> {
) {
log("ingestConversation: start", { conversationId, messageCount: messages.length });
try {
const response = await withTimeout(
Expand All @@ -160,16 +167,16 @@ export class SupermemoryClient {
if (!response.ok) {
const errorText = await response.text();
log("ingestConversation: error response", { status: response.status, error: errorText });
return null;
return { success: false as const, error: `HTTP ${response.status}: ${errorText}` };
}

const result = await response.json() as ConversationIngestResponse;
log("ingestConversation: success", { conversationId, status: result.status });
return result;
return { success: true as const, ...result };
} catch (error) {
log("ingestConversation: error", { error: String(error) });
console.error("Supermemory: ingest conversation failed", error);
return null;
const errorMessage = error instanceof Error ? error.message : String(error);
log("ingestConversation: error", { error: errorMessage });
return { success: false as const, error: errorMessage };
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/services/compaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,10 @@ export function createCompactionHook(
{ type: "conversation" }
);

if (result) {
if (result.success) {
log("[compaction] summary saved as memory", { sessionID, memoryId: result.id });
} else {
log("[compaction] failed to save summary", { error: result.error });
}
} catch (err) {
log("[compaction] failed to save summary", { error: String(err) });
Expand Down