Skip to content
Open
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
1 change: 1 addition & 0 deletions packages/core/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export class Client {
return resolveFileUrl(source, this.requireApi().token, model, {
...opts,
identity: this.deps.identity,
timeout: this.deps.settings.timeout,
});
}

Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/files/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ async function getUploadPolicy(
return body.data;
}

const DEFAULT_UPLOAD_TIMEOUT = 120;

/**
* Step 2: Upload the file to OSS using the policy.
*/
async function uploadToOSS(
policy: UploadPolicy,
filePath: string,
signal?: AbortSignal,
timeoutSeconds: number = DEFAULT_UPLOAD_TIMEOUT,
): Promise<string> {
const fileName = basename(filePath);
const key = `${policy.upload_dir}/${fileName}`;
Expand All @@ -85,7 +88,7 @@ async function uploadToOSS(
form.append("success_action_status", "200");
form.append("file", new Blob([fileData]), fileName);

const uploadSignal = combineWithTimeout(120_000, signal);
const uploadSignal = combineWithTimeout(timeoutSeconds * 1000, signal);
const res = await fetch(policy.upload_host, {
method: "POST",
body: form,
Expand All @@ -109,6 +112,7 @@ export interface UploadOptions {
filePath: string;
identity: TrackingIdentity;
signal?: AbortSignal;
timeout?: number;
}

const IMAGE_MIME_TYPES: Readonly<Record<string, string>> = {
Expand Down Expand Up @@ -159,7 +163,7 @@ export function redactDataUri(input: string): string {
* The URL is valid for 48 hours.
*/
export async function uploadFile(opts: UploadOptions): Promise<string> {
const { apiKey, model, filePath, identity, signal } = opts;
const { apiKey, model, filePath, identity, signal, timeout } = opts;

if (!existsSync(filePath)) {
throw new BailianError(`File not found: ${filePath}`, ExitCode.USAGE);
Expand All @@ -171,7 +175,7 @@ export async function uploadFile(opts: UploadOptions): Promise<string> {
}

const policy = await getUploadPolicy(apiKey, model, identity, signal);
return uploadToOSS(policy, filePath, signal);
return uploadToOSS(policy, filePath, signal, timeout);
}

/**
Expand All @@ -192,7 +196,7 @@ export async function resolveFileUrl(
input: string,
apiKey: string,
model: string,
opts: { identity: TrackingIdentity; signal?: AbortSignal },
opts: { identity: TrackingIdentity; signal?: AbortSignal; timeout?: number },
): Promise<string> {
if (!isLocalFile(input)) return input;
return uploadFile({
Expand All @@ -201,6 +205,7 @@ export async function resolveFileUrl(
filePath: input,
identity: opts.identity,
signal: opts.signal,
timeout: opts.timeout,
});
}

Expand Down
Loading