mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-10-01 02:01:40 +00:00
Merge 6bdc6ae111 into cfa6c7cb17
This commit is contained in:
commit
71813c1850
3 changed files with 63 additions and 3 deletions
|
|
@ -6,6 +6,8 @@
|
|||
* diffing and append detection on the backend.
|
||||
*/
|
||||
|
||||
import { normalizeBaseUrl } from "./shared/context"
|
||||
|
||||
export interface ConversationMessage {
|
||||
role: "user" | "assistant" | "system" | "tool"
|
||||
content: string | ContentPart[]
|
||||
|
|
@ -52,6 +54,16 @@ export const toConversationImageUrl = (
|
|||
: `data:${mediaType};base64,${trimmed}`
|
||||
}
|
||||
|
||||
if (typeof value === "object" && value !== null && "url" in value) {
|
||||
const rawUrl = (value as { url: unknown }).url
|
||||
if (
|
||||
typeof rawUrl === "string" ||
|
||||
(typeof URL !== "undefined" && rawUrl instanceof URL)
|
||||
) {
|
||||
return toConversationImageUrl(rawUrl, mediaType)
|
||||
}
|
||||
}
|
||||
|
||||
const bytes =
|
||||
value instanceof Uint8Array
|
||||
? value
|
||||
|
|
@ -117,7 +129,7 @@ const CONVERSATION_REQUEST_TIMEOUT_MS = 30_000
|
|||
export async function addConversation(
|
||||
params: AddConversationParams,
|
||||
): Promise<AddConversationResponse> {
|
||||
const baseUrl = params.baseUrl || "https://api.supermemory.ai"
|
||||
const baseUrl = normalizeBaseUrl(params.baseUrl)
|
||||
const url = `${baseUrl}/v4/conversations`
|
||||
|
||||
const response = await fetch(url, {
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ import Supermemory from "supermemory"
|
|||
*/
|
||||
export const normalizeBaseUrl = (url?: string): string => {
|
||||
const defaultUrl = "https://api.supermemory.ai"
|
||||
if (!url) return defaultUrl
|
||||
return url.endsWith("/") ? url.slice(0, -1) : url
|
||||
const trimmed = url?.trim().replace(/\/+$/, "")
|
||||
return trimmed || defaultUrl
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
import { describe, expect, it } from "vitest"
|
||||
import { toConversationImageUrl } from "./conversations-client"
|
||||
import { normalizeBaseUrl } from "./shared/context"
|
||||
import {
|
||||
DEFAULT_VALUES,
|
||||
SEARCH_LIMIT_BOUNDS,
|
||||
|
|
@ -130,3 +132,49 @@ describe("deduplicateMemoriesForMode", () => {
|
|||
expect(deduplicated.searchResults).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe("normalizeBaseUrl", () => {
|
||||
it("returns default URL when input is missing or empty", () => {
|
||||
expect(normalizeBaseUrl()).toBe("https://api.supermemory.ai")
|
||||
expect(normalizeBaseUrl("")).toBe("https://api.supermemory.ai")
|
||||
expect(normalizeBaseUrl(" ")).toBe("https://api.supermemory.ai")
|
||||
})
|
||||
|
||||
it("collapses single and multiple trailing slashes", () => {
|
||||
expect(normalizeBaseUrl("http://localhost:6768/")).toBe(
|
||||
"http://localhost:6768",
|
||||
)
|
||||
expect(normalizeBaseUrl("http://localhost:6768///")).toBe(
|
||||
"http://localhost:6768",
|
||||
)
|
||||
expect(normalizeBaseUrl("https://api.supermemory.ai/")).toBe(
|
||||
"https://api.supermemory.ai",
|
||||
)
|
||||
})
|
||||
|
||||
it("trims whitespace around URLs", () => {
|
||||
expect(normalizeBaseUrl(" http://localhost:6768/ ")).toBe(
|
||||
"http://localhost:6768",
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("toConversationImageUrl", () => {
|
||||
it("handles string URLs and trims whitespace", () => {
|
||||
expect(toConversationImageUrl("https://example.com/image.png")).toBe(
|
||||
"https://example.com/image.png",
|
||||
)
|
||||
})
|
||||
|
||||
it("handles object representations containing url", () => {
|
||||
expect(
|
||||
toConversationImageUrl({ url: "https://example.com/avatar.jpg" }),
|
||||
).toBe("https://example.com/avatar.jpg")
|
||||
})
|
||||
|
||||
it("returns null for invalid or empty inputs", () => {
|
||||
expect(toConversationImageUrl("")).toBeNull()
|
||||
expect(toConversationImageUrl(null)).toBeNull()
|
||||
expect(toConversationImageUrl(undefined)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue