Files
wehub-resource-sync 6ede33ccdb
Build and Push Docker Images / create_manifest (web, surfsense-web, , cpu) (push) Has been cancelled
Build and Push Docker Images / finalize_release (push) Has been cancelled
Obsidian Plugin Lint / lint (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-24.04-arm, linux/arm64, arm64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-latest, linux/amd64, amd64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / compute_version (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / verify_digests (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, , cpu) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda, cuda) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda126, cuda126) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:33:44 +08:00

100 lines
3.2 KiB
TypeScript

import {
type AnonChatRequest,
type AnonModel,
type AnonQuotaResponse,
anonChatRequest,
anonQuotaResponse,
getAnonModelResponse,
getAnonModelsResponse,
} from "@/contracts/types/anonymous-chat.types";
import { buildBackendUrl } from "../env-config";
import { ValidationError } from "../error";
const BASE = "/api/v1/public/anon-chat";
export type AnonUploadResult =
| { ok: true; data: { filename: string; size_bytes: number } }
| { ok: false; reason: "quota_exceeded" };
class AnonymousChatApiService {
private fullUrl(path: string): string {
return buildBackendUrl(`${BASE}${path}`);
}
getModels = async (): Promise<AnonModel[]> => {
const res = await fetch(this.fullUrl("/models"), { credentials: "include" });
if (!res.ok) throw new Error(`Failed to fetch models: ${res.status}`);
const data = await res.json();
const parsed = getAnonModelsResponse.safeParse(data);
if (!parsed.success) console.error("Invalid anon models response:", parsed.error);
return data;
};
getModel = async (slug: string): Promise<AnonModel> => {
const res = await fetch(this.fullUrl(`/models/${encodeURIComponent(slug)}`), {
credentials: "include",
});
if (!res.ok) {
if (res.status === 404) throw new Error("Model not found");
throw new Error(`Failed to fetch model: ${res.status}`);
}
const data = await res.json();
const parsed = getAnonModelResponse.safeParse(data);
if (!parsed.success) console.error("Invalid anon model response:", parsed.error);
return data;
};
getQuota = async (): Promise<AnonQuotaResponse> => {
const res = await fetch(this.fullUrl("/quota"), { credentials: "include" });
if (!res.ok) throw new Error(`Failed to fetch quota: ${res.status}`);
const data = await res.json();
const parsed = anonQuotaResponse.safeParse(data);
if (!parsed.success) console.error("Invalid anon quota response:", parsed.error);
return data;
};
streamChat = async (request: AnonChatRequest): Promise<Response> => {
const validated = anonChatRequest.safeParse(request);
if (!validated.success) {
throw new ValidationError(
`Invalid request: ${validated.error.issues.map((i) => i.message).join(", ")}`
);
}
return fetch(this.fullUrl("/stream"), {
method: "POST",
headers: { "Content-Type": "application/json" },
credentials: "include",
body: JSON.stringify(validated.data),
});
};
uploadDocument = async (file: File): Promise<AnonUploadResult> => {
const formData = new FormData();
formData.append("file", file);
const res = await fetch(this.fullUrl("/upload"), {
method: "POST",
credentials: "include",
body: formData,
});
if (res.status === 409) {
return { ok: false, reason: "quota_exceeded" };
}
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.detail || `Upload failed: ${res.status}`);
}
const data = await res.json();
return { ok: true, data };
};
getDocument = async (): Promise<{ filename: string; size_bytes: number } | null> => {
const res = await fetch(this.fullUrl("/document"), { credentials: "include" });
if (res.status === 404) return null;
if (!res.ok) throw new Error(`Failed to fetch document: ${res.status}`);
return res.json();
};
}
export const anonymousChatApiService = new AnonymousChatApiService();