mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-10-10 03:28:14 +00:00
Add 30s request timeouts to SupermemoryClient outbound calls
- Pass explicit timeout: 30_000 to the Supermemory SDK constructor so all
SDK-backed calls (search, add, forget, profile) are bounded to 30 seconds
- Add AbortSignal.timeout(FETCH_TIMEOUT_MS) to the manual fetch calls in
getProjects() and getDocuments() which had no timeout before
- Accept an optional { signal?: AbortSignal } argument on both methods so
callers can thread a request-scoped signal when one is available
- Catch AbortError / TimeoutError in handleError() and surface a friendly
'Request to Supermemory API timed out' message instead of an opaque crash
- Follow existing codebase convention (FETCH_TIMEOUT_MS constant, same value
used in apps/api/src/services/extraction/extractors/image.ts)
Co-authored-by: Dhravya Shah <dhravya@supermemory.com>
This commit is contained in:
parent
cf47d73126
commit
f9369d5eb9
1 changed files with 16 additions and 1 deletions
|
|
@ -2,6 +2,7 @@ import Supermemory from "supermemory"
|
||||||
|
|
||||||
const MAX_CHARS = 200000 // ~50k tokens (character-based limit)
|
const MAX_CHARS = 200000 // ~50k tokens (character-based limit)
|
||||||
const DEFAULT_PROJECT_ID = "sm_project_default"
|
const DEFAULT_PROJECT_ID = "sm_project_default"
|
||||||
|
const FETCH_TIMEOUT_MS = 30_000
|
||||||
|
|
||||||
interface MemoryRichFields {
|
interface MemoryRichFields {
|
||||||
metadata?: Record<string, unknown> | null
|
metadata?: Record<string, unknown> | null
|
||||||
|
|
@ -142,6 +143,7 @@ export class SupermemoryClient {
|
||||||
this.client = new Supermemory({
|
this.client = new Supermemory({
|
||||||
apiKey: bearerToken,
|
apiKey: bearerToken,
|
||||||
baseURL: apiUrl,
|
baseURL: apiUrl,
|
||||||
|
timeout: FETCH_TIMEOUT_MS,
|
||||||
})
|
})
|
||||||
this.containerTag = containerTag || DEFAULT_PROJECT_ID
|
this.containerTag = containerTag || DEFAULT_PROJECT_ID
|
||||||
}
|
}
|
||||||
|
|
@ -328,14 +330,16 @@ export class SupermemoryClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get projects list
|
// Get projects list
|
||||||
async getProjects(): Promise<string[]> {
|
async getProjects(options?: { signal?: AbortSignal }): Promise<string[]> {
|
||||||
try {
|
try {
|
||||||
|
const signal = options?.signal ?? AbortSignal.timeout(FETCH_TIMEOUT_MS)
|
||||||
const response = await fetch(`${this.apiUrl}/v3/projects`, {
|
const response = await fetch(`${this.apiUrl}/v3/projects`, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${this.bearerToken}`,
|
Authorization: `Bearer ${this.bearerToken}`,
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
|
signal,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|
@ -359,8 +363,10 @@ export class SupermemoryClient {
|
||||||
containerTags?: string[],
|
containerTags?: string[],
|
||||||
page = 1,
|
page = 1,
|
||||||
limit = 10,
|
limit = 10,
|
||||||
|
options?: { signal?: AbortSignal },
|
||||||
): Promise<DocumentsApiResponse> {
|
): Promise<DocumentsApiResponse> {
|
||||||
try {
|
try {
|
||||||
|
const signal = options?.signal ?? AbortSignal.timeout(FETCH_TIMEOUT_MS)
|
||||||
const response = await fetch(`${this.apiUrl}/v3/documents/documents`, {
|
const response = await fetch(`${this.apiUrl}/v3/documents/documents`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
|
|
@ -374,6 +380,7 @@ export class SupermemoryClient {
|
||||||
order: "desc",
|
order: "desc",
|
||||||
containerTags,
|
containerTags,
|
||||||
}),
|
}),
|
||||||
|
signal,
|
||||||
})
|
})
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw Object.assign(new Error("Failed to fetch documents"), {
|
throw Object.assign(new Error("Failed to fetch documents"), {
|
||||||
|
|
@ -387,6 +394,14 @@ export class SupermemoryClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleError(error: unknown): never {
|
private handleError(error: unknown): never {
|
||||||
|
// Handle request timeout (AbortSignal.timeout or explicit abort)
|
||||||
|
if (
|
||||||
|
error instanceof Error &&
|
||||||
|
(error.name === "AbortError" || error.name === "TimeoutError")
|
||||||
|
) {
|
||||||
|
throw new Error("Request to Supermemory API timed out")
|
||||||
|
}
|
||||||
|
|
||||||
// Handle network/fetch errors
|
// Handle network/fetch errors
|
||||||
if (error instanceof TypeError) {
|
if (error instanceof TypeError) {
|
||||||
if (
|
if (
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue