Skip to content

Commit

Permalink
hack: assemblyai merge streaming + realtime (#673)
Browse files Browse the repository at this point in the history
Co-authored-by: Deep Singhvi <[email protected]>
  • Loading branch information
abvthecity and dsinghvi authored Apr 19, 2024
1 parent bb6981c commit 41ec16a
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 18 deletions.
2 changes: 2 additions & 0 deletions packages/commons/fdr-utils/src/__test__/primer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe("getNavigationRoot", () => {

const urls = getNavigationRoot(
"docs/api/introduction/getting-started".split("/"),
fixture.baseUrl.domain,
fixture.baseUrl.basePath,
fixture.definition.config.navigation,
fixture.definition.apis,
Expand All @@ -38,6 +39,7 @@ describe("getNavigationRoot", () => {

const urls = getNavigationRoot(
"docs/api/v2.1/api-reference/client-session-api/retrieve-client-side-token".split("/"),
fixture.baseUrl.domain,
fixture.baseUrl.basePath,
fixture.definition.config.navigation,
fixture.definition.apis,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function testGetNavigationRoot(fixtureName: string, slug: string): void {

const urls = getNavigationRoot(
slug.split("/"),
fixture.baseUrl.domain,
fixture.baseUrl.basePath,
fixture.definition.config.navigation,
fixture.definition.apis,
Expand Down
30 changes: 28 additions & 2 deletions packages/commons/fdr-utils/src/flattenApiDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,14 @@ export function flattenApiDefinition(
apiDefinition: APIV1Read.ApiDefinition,
parentSlugs: readonly string[],
navigation: DocsV1Read.ApiNavigationConfigRoot | undefined,
domain: string,
): FlattenedApiDefinition {
const package_ = flattenPackage(
apiDefinition.rootPackage,
apiDefinition.subpackages,
parentSlugs,
navigation ?? toConfigRoot(apiDefinition.navigation),
domain,
);

return {
Expand All @@ -171,6 +173,7 @@ function flattenPackage(
subpackagesMap: Record<string, APIV1Read.ApiDefinitionSubpackage>,
parentSlugs: readonly string[],
order: DocsV1Read.ApiNavigationConfigRoot | undefined,
domain: string,
): FlattenedApiDefinitionPackage {
let currentPackage: APIV1Read.ApiDefinitionPackage | undefined = apiDefinitionPackage;
while (currentPackage?.pointsTo != null) {
Expand Down Expand Up @@ -251,7 +254,7 @@ function flattenPackage(
(item): item is DocsV1Read.ApiNavigationConfigItem.Subpackage => item.type === "subpackage",
);

const subpackages = currentPackage.subpackages
let subpackages = currentPackage.subpackages
.map((subpackageId): FlattenedSubpackage | undefined => {
const subpackage = subpackagesMap[subpackageId];
if (subpackage == null) {
Expand All @@ -264,11 +267,13 @@ function flattenPackage(
subpackageId: subpackage.subpackageId,
name: subpackage.displayName ?? titleCase(subpackage.name),
description: subpackage.description,
...flattenPackage(subpackage, subpackagesMap, subpackageSlugs, subpackageOrder),
...flattenPackage(subpackage, subpackagesMap, subpackageSlugs, subpackageOrder, domain),
};
})
.filter(isNonNullish);

subpackages = maybeMergeSubpackages(subpackages, domain);

const pages =
order?.items
?.filter((item): item is DocsV1Read.ApiNavigationConfigItem.Page => item.type === "page")
Expand Down Expand Up @@ -355,3 +360,24 @@ function toConfigRoot(
): DocsV1Read.ApiNavigationConfigRoot | undefined {
return root;
}

function maybeMergeSubpackages(subpackages: FlattenedSubpackage[], domain: string): FlattenedSubpackage[] {
if (domain.includes("assemblyai")) {
const realtimeIdx = subpackages.findIndex((subpackage) => subpackage.subpackageId === "subpackage_realtime");
const realtime = subpackages[realtimeIdx];
if (realtime != null) {
const subpackageIdx = subpackages.findIndex(
(subpackage) => subpackage.subpackageId === "subpackage_streaming",
);
if (subpackageIdx !== -1) {
const subpackage = subpackages[subpackageIdx];
subpackages.splice(subpackageIdx, 1, {
...subpackage,
items: [...realtime.items, ...subpackage.items],
});
subpackages.splice(realtimeIdx, 1);
}
}
}
return subpackages;
}
2 changes: 1 addition & 1 deletion packages/commons/fdr-utils/src/getAllUrlsFromDocsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function getAllUrlsFromDocsConfig(
apis: Record<string, APIV1Read.ApiDefinition>,
): string[] {
const basePathSlug = basePath != null ? basePath.split("/").filter((t) => t.length > 0) : [];
const root = resolveSidebarNodesRoot(nav, apis, {}, basePathSlug);
const root = resolveSidebarNodesRoot(nav, apis, {}, basePathSlug, host);
const visitedSlugs: string[] = [];

visitSidebarNodeRaw(root, (node) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/commons/fdr-utils/src/getNavigationRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ const PRIORITY_LIST: Record<SidebarNodeRaw.VisitableNode["type"], number> = {

export function getNavigationRoot(
slugArray: string[],
domain: string,
basePath: string | undefined,
nav: DocsV1Read.NavigationConfig,
apis: Record<FdrAPI.ApiId, APIV1Read.ApiDefinition>,
pages: Record<string, DocsV1Read.PageContent>,
): Found | Redirect | undefined {
const basePathSlug = basePath != null ? basePath.split("/").filter((t) => t.length > 0) : [];

const root = resolveSidebarNodesRoot(nav, apis, pages, basePathSlug);
const root = resolveSidebarNodesRoot(nav, apis, pages, basePathSlug, domain);
const hits: { node: SidebarNodeRaw.VisitableNode; parents: SidebarNodeRaw.ParentNode[] }[] = [];

visitSidebarNodeRaw(root, (node, parents) => {
Expand Down
20 changes: 12 additions & 8 deletions packages/commons/fdr-utils/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ export function resolveSidebarNodesRoot(
apis: Record<FdrAPI.ApiId, APIV1Read.ApiDefinition>,
pages: Record<string, DocsV1Read.PageContent>,
basePathSlug: string[],
domain: string,
): SidebarNodeRaw.Root {
return {
type: "root",
slug: basePathSlug,
items: resolveSidebarNodesRootItems(nav, apis, pages, basePathSlug),
items: resolveSidebarNodesRootItems(nav, apis, pages, basePathSlug, domain),
};
}

Expand All @@ -142,6 +143,7 @@ function resolveSidebarNodesRootItems(
apis: Record<FdrAPI.ApiId, APIV1Read.ApiDefinition>,
pages: Record<string, DocsV1Read.PageContent>,
parentSlugs: readonly string[],
domain: string,
): SidebarNodeRaw.Root["items"] {
if (isVersionedNavigationConfig(nav)) {
const toRet: SidebarNodeRaw.VersionGroup[] = [];
Expand All @@ -154,7 +156,7 @@ function resolveSidebarNodesRootItems(
slug: parentSlugs,
index,
availability: version.availability ?? null,
items: resolveSidebarNodesVersionItems(version.config, apis, pages, parentSlugs),
items: resolveSidebarNodesVersionItems(version.config, apis, pages, parentSlugs, domain),
});
}

Expand All @@ -165,21 +167,22 @@ function resolveSidebarNodesRootItems(
slug: versionSlug,
index,
availability: version.availability ?? null,
items: resolveSidebarNodesVersionItems(version.config, apis, pages, versionSlug),
items: resolveSidebarNodesVersionItems(version.config, apis, pages, versionSlug, domain),
});
});

return toRet;
}

return resolveSidebarNodesVersionItems(nav, apis, pages, parentSlugs);
return resolveSidebarNodesVersionItems(nav, apis, pages, parentSlugs, domain);
}

function resolveSidebarNodesVersionItems(
nav: DocsV1Read.UnversionedNavigationConfig,
apis: Record<FdrAPI.ApiId, APIV1Read.ApiDefinition>,
pages: Record<string, DocsV1Read.PageContent>,
parentSlugs: readonly string[],
domain: string,
): SidebarNodeRaw.VersionGroup["items"] {
if (isUnversionedTabbedNavigationConfig(nav)) {
return nav.tabs.map((tab, index): SidebarNodeRaw.TabGroup => {
Expand All @@ -190,12 +193,12 @@ function resolveSidebarNodesVersionItems(
icon: tab.icon,
slug: tabSlug,
index,
items: resolveSidebarNodes(tab.items, apis, pages, tabSlug, parentSlugs),
items: resolveSidebarNodes(tab.items, apis, pages, tabSlug, parentSlugs, domain),
};
});
}

return resolveSidebarNodes(nav.items, apis, pages, parentSlugs, parentSlugs);
return resolveSidebarNodes(nav.items, apis, pages, parentSlugs, parentSlugs, domain);
}

export function resolveSidebarNodes(
Expand All @@ -204,6 +207,7 @@ export function resolveSidebarNodes(
pages: Record<string, DocsV1Read.PageContent>,
parentSlugs: readonly string[], // parent slugs that are inherited from the parent node
fixedSlugs: readonly string[], // basepath and version slugs
domain: string,
): SidebarNodeRaw[] {
const sidebarNodes: SidebarNodeRaw[] = [];

Expand Down Expand Up @@ -245,7 +249,7 @@ export function resolveSidebarNodes(
: api.skipUrlSlug
? parentSlugs
: [...parentSlugs, ...api.urlSlug.split("/")];
const flattened = flattenApiDefinition(definition, definitionSlug, api.navigation);
const flattened = flattenApiDefinition(definition, definitionSlug, api.navigation, domain);
const resolved = resolveSidebarNodeRawApiSection(
api.api,
api.api,
Expand Down Expand Up @@ -317,7 +321,7 @@ export function resolveSidebarNodes(
title: section.title,
slug: sectionSlug,
// if section.fullSlug is defined, the child slugs will be built from that, rather than from inherited parentSlugs
items: resolveSidebarNodes(section.items, apis, pages, sectionSlug, fixedSlugs),
items: resolveSidebarNodes(section.items, apis, pages, sectionSlug, fixedSlugs, domain),
icon: section.icon,
hidden: section.hidden ?? false,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("resolveApiDefinition", () => {
const content = fs.readFileSync(fixturePath, "utf-8");

const fixture = JSON.parse(content) as APIV1Read.ApiDefinition;
const flattened = flattenApiDefinition(fixture, [], undefined);
const flattened = flattenApiDefinition(fixture, [], undefined, "docs.buildwithfern.com");
const resolved = await ApiDefinitionResolver.resolve("API Reference", flattened, {}, undefined);
expect(resolved).toMatchSnapshot();
});
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/app/src/util/convertNavigatableToResolvedPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ export async function convertNavigatableToResolvedPath({
apis,
pages,
mdxOptions,
domain,
}: {
rawSidebarNodes: readonly SidebarNodeRaw[];
sidebarNodes: SidebarNode[];
currentNode: SidebarNodeRaw.VisitableNode;
apis: Record<string, APIV1Read.ApiDefinition>;
pages: Record<string, DocsV1Read.PageContent>;
mdxOptions?: FernSerializeMdxOptions;
domain: string;
}): Promise<ResolvedPath | undefined> {
const traverseState = traverseSidebarNodes(sidebarNodes, currentNode);

Expand Down Expand Up @@ -165,7 +167,7 @@ export async function convertNavigatableToResolvedPath({
resolvedApis = Object.fromEntries(
await Promise.all(
Object.entries(apis).map(async ([apiName, api]) => {
const flattenedApiDefinition = flattenApiDefinition(api, ["dummy"], undefined);
const flattenedApiDefinition = flattenApiDefinition(api, ["dummy"], undefined, domain);
return [
apiName,
await ApiDefinitionResolver.resolve(title, flattenedApiDefinition, pages, mdxOptions),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const resolveApiHandler: NextApiHandler = async (req, res: NextApiResponse<Resol

const navigation = getNavigationRoot(
pathname.slice(1).split("/"),
docs.body.baseUrl.domain,
basePath,
docsDefinition.config.navigation,
docsDefinition.apis,
Expand All @@ -74,7 +75,7 @@ const resolveApiHandler: NextApiHandler = async (req, res: NextApiResponse<Resol
res.status(200).json(
await ApiDefinitionResolver.resolve(
apiSection?.title ?? "",
flattenApiDefinition(apiDefinition, apiSection?.slug ?? [], undefined),
flattenApiDefinition(apiDefinition, apiSection?.slug ?? [], undefined, docs.body.baseUrl.domain),
pages,
undefined,
),
Expand Down
11 changes: 9 additions & 2 deletions packages/ui/docs-bundle/src/utils/getDocsPageProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ async function convertDocsToDocsPageProps({
xFernHost: string;
}): Promise<DocsPageResult<DocsPage.Props>> {
const docsDefinition = docs.definition;
const basePath = docs.baseUrl.basePath;
const docsConfig = docsDefinition.config;
const pages = docs.definition.pages;

Expand All @@ -161,7 +160,14 @@ async function convertDocsToDocsPageProps({
};
}

const navigation = getNavigationRoot(slug, basePath, docsConfig.navigation, docs.definition.apis, pages);
const navigation = getNavigationRoot(
slug,
docs.baseUrl.domain,
docs.baseUrl.basePath,
docsConfig.navigation,
docs.definition.apis,
pages,
);

if (navigation == null) {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -189,6 +195,7 @@ async function convertDocsToDocsPageProps({
sidebarNodes,
apis: docsDefinition.apis,
pages: docsDefinition.pages,
domain: docs.baseUrl.domain,
});

if (resolvedPath == null) {
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/local-preview-bundle/src/pages/[[...slug]].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ async function getDocsPageProps(

const navigation = getNavigationRoot(
slug,
basePath,
docs.baseUrl.domain,
docs.baseUrl.basePath,
docsConfig.navigation,
docs.definition.apis,
docs.definition.pages,
Expand Down Expand Up @@ -128,6 +129,7 @@ async function getDocsPageProps(
mdxOptions: {
showError: true,
},
domain: docs.baseUrl.domain,
});

if (resolvedPath == null) {
Expand Down

0 comments on commit 41ec16a

Please sign in to comment.