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
13 changes: 8 additions & 5 deletions src/gguf/utils/resolveBinarySplitGgufPartUrls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ export function resolveBinarySplitGgufPartUrls(ggufUrl: string) {
)
return ggufUrl;

const ggufIndex = parsedGgufUrl.pathname.indexOf(".gguf");
const pathnameWithoutPart = parsedGgufUrl.pathname.slice(0, ggufIndex + ".gguf".length);
// The directory can contain ".gguf" too. Cut at this match, not the first one.
if (binaryPartsMatch.index == null)
return ggufUrl;

const pathnameWithoutPart = parsedGgufUrl.pathname.slice(0, binaryPartsMatch.index + ".gguf".length);

const res: string[] = [];
for (let i = 1; i <= parts; i++) {
Expand All @@ -38,9 +41,9 @@ export function getFilenameForBinarySplitGgufPartUrls(urls: string[]) {

const firstParsedUrl = new URL(urls[0]!);

if (binarySplitGgufPartsRegex.test(firstParsedUrl.pathname)) {
const ggufIndex = firstParsedUrl.pathname.toLowerCase().indexOf(".gguf");
const urlWithoutPart = firstParsedUrl.pathname.slice(0, ggufIndex + ".gguf".length);
const binaryPartsMatch = firstParsedUrl.pathname.match(binarySplitGgufPartsRegex);
if (binaryPartsMatch?.index != null) {
const urlWithoutPart = firstParsedUrl.pathname.slice(0, binaryPartsMatch.index + ".gguf".length);

const filename = decodeURIComponent(urlWithoutPart.split("/").pop()!);
return filenamify(filename);
Expand Down
19 changes: 18 additions & 1 deletion test/standalone/utils/modelDownloader.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {describe, expect, test} from "vitest";
import {getFilenameForBinarySplitGgufPartUrls} from "../../../src/gguf/utils/resolveBinarySplitGgufPartUrls.js";
import {getFilenameForBinarySplitGgufPartUrls, resolveBinarySplitGgufPartUrls} from "../../../src/gguf/utils/resolveBinarySplitGgufPartUrls.js";


describe("utils", () => {
Expand All @@ -19,5 +19,22 @@ describe("utils", () => {
]);
expect(res).to.eql("model.Q6_K.gguf");
});

test("keeps an earlier .gguf directory when expanding part urls", async () => {
const res = resolveBinarySplitGgufPartUrls("https://example.com/models/old.gguf/new.gguf.part1of2?hi=true");

expect(res).to.eql([
"https://example.com/models/old.gguf/new.gguf.part1of2?hi=true",
"https://example.com/models/old.gguf/new.gguf.part2of2?hi=true"
]);
});

test("filename ignores an earlier .gguf directory", async () => {
const res = getFilenameForBinarySplitGgufPartUrls([
"https://example.com/models/old.gguf/new.gguf.part1of2"
]);

expect(res).to.eql("new.gguf");
});
});
});
Loading