Skip to content

Commit

Permalink
Merge branch 'main' into rohin/required-global-headers
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava authored Feb 5, 2025
2 parents 5e4c334 + f25bdbe commit a25c4da
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "@fern-docs/search-server/turbopuffer";
import { COOKIE_FERN_TOKEN, withoutStaging } from "@fern-docs/utils";
import { embed, EmbeddingModel, streamText, tool } from "ai";
import { initLogger, traced, wrapAISDKModel } from "braintrust";
import { initLogger, wrapAISDKModel } from "braintrust";
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
Expand Down Expand Up @@ -86,54 +86,51 @@ export async function POST(req: NextRequest) {
date: new Date().toDateString(),
documents,
});
// eslint-disable-next-line @typescript-eslint/await-thenable
const result = await traced(() =>
streamText({
model: languageModel,
system,
messages,
maxSteps: 10,
maxRetries: 3,
tools: {
search: tool({
description:
"Search the knowledge base for the user's query. Semantic search is enabled.",
parameters: z.object({
query: z.string(),
}),
async execute({ query }) {
const response = await runQueryTurbopuffer(query, {
embeddingModel,
namespace,
authed: user != null,
roles: user?.roles ?? [],
});
return response.map((hit) => {
const { domain, pathname, hash } = hit.attributes;
const url = `https://${domain}${pathname}${hash ?? ""}`;
return { url, ...hit.attributes };
});
},
const result = streamText({
model: languageModel,
system,
messages,
maxSteps: 10,
maxRetries: 3,
tools: {
search: tool({
description:
"Search the knowledge base for the user's query. Semantic search is enabled.",
parameters: z.object({
query: z.string(),
}),
},
onFinish: async (e) => {
const end = Date.now();
await track("ask_ai", {
languageModel: languageModel.modelId,
embeddingModel: embeddingModel.modelId,
durationMs: end - start,
domain,
namespace,
numToolCalls: e.toolCalls.length,
finishReason: e.finishReason,
...e.usage,
});
e.warnings?.forEach((warning) => {
console.warn(warning);
});
},
})
);
async execute({ query }) {
const response = await runQueryTurbopuffer(query, {
embeddingModel,
namespace,
authed: user != null,
roles: user?.roles ?? [],
});
return response.map((hit) => {
const { domain, pathname, hash } = hit.attributes;
const url = `https://${domain}${pathname}${hash ?? ""}`;
return { url, ...hit.attributes };
});
},
}),
},
onFinish: async (e) => {
const end = Date.now();
await track("ask_ai", {
languageModel: languageModel.modelId,
embeddingModel: embeddingModel.modelId,
durationMs: end - start,
domain,
namespace,
numToolCalls: e.toolCalls.length,
finishReason: e.finishReason,
...e.usage,
});
e.warnings?.forEach((warning) => {
console.warn(warning);
});
},
});

const response = result.toDataStreamResponse();
response.headers.set("Access-Control-Allow-Origin", "*");
Expand Down
15 changes: 7 additions & 8 deletions packages/fern-docs/bundle/src/server/withInitialProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,13 @@ export async function withInitialProps({
definition: docs.definition,
edgeFlags,
scope: {
props: {
authed: authState.authed,
user: authState.authed ? authState.user : undefined,
// frontmatter is already available under `{frontmatter}`, so this adds a new scope variable {props}
// note: do NOT override `props.components`
version: found?.currentVersion?.versionId,
tab: found?.currentTab?.title,
},
authed: authState.authed,
user: authState.authed ? authState.user : undefined,
// frontmatter is already available under `{frontmatter}`, so this adds a new scope variable {props}
// note: do NOT override `props.components`
version: found?.currentVersion?.versionId,
tab: found?.currentTab?.title,
slug: slug,
},
replaceSrc: resolveFileSrc,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,10 @@ export async function getDocsPageProps(
files: docs.definition.jsFiles,
scope: {
env: "development",
props: {
authed: false,
user: undefined,
version: node.currentVersion?.versionId,
tab: node.currentTab?.title,
},
authed: false,
user: undefined,
version: node.currentVersion?.versionId,
tab: node.currentTab?.title,
},

// inject the file url and dimensions for images and other embeddable files
Expand Down
1 change: 1 addition & 0 deletions packages/fern-docs/mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@types/unist": "^3.0.3",
"collapse-white-space": "^2.1.0",
"es-toolkit": "^1.27.0",
"estree-util-is-identifier-name": "^3.0.0",
"estree-util-value-to-estree": "^3.2.1",
"estree-walker": "^3.0.3",
"github-slugger": "^2.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/fern-docs/mdx/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./rehype-acorn-error-boundary";
export * from "./rehype-mdx-class-style";
export * from "./rehype-squeeze-paragraphs";
export * from "./remark-inject-esm";
export * from "./remark-sanitize-acorn";
export * from "./remark-squeeze-paragraphs";
62 changes: 62 additions & 0 deletions packages/fern-docs/mdx/src/plugins/remark-inject-esm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* A remark plugin to inject an ESM export into the MDX file.
*
* Inspired by https://github.com/remcohaszing/remark-mdx-frontmatter/blob/main/src/remark-mdx-frontmatter.ts
*/

import type { Statement } from "estree";
import { name as isIdentifierName } from "estree-util-is-identifier-name";
import { valueToEstree } from "estree-util-value-to-estree";
import type { Root } from "mdast";
import type { Plugin } from "unified";

export interface RemarkInjectEsmOptions {
scope: Record<string, unknown>;
}

/**
* A remark plugin to inject scope of variables into the MDX file.
*
* @param options Optional options to configure the output.
* @returns A unified transformer.
*/
export const remarkInjectEsm: Plugin<[RemarkInjectEsmOptions?], Root> = (
{ scope } = { scope: {} }
) => {
const keys = Object.keys(scope);
if (keys.some((key) => !isIdentifierName(key))) {
throw new Error(
`Scope keys should be valid identifiers, got: ${JSON.stringify(keys)}`
);
}

return (ast) => {
if (Object.keys(scope).length === 0) {
return;
}

ast.children.unshift({
type: "mdxjsEsm",
value: "",
data: {
estree: {
type: "Program",
sourceType: "module",
body: Object.entries(scope).map(
([key, value]): Statement => ({
type: "VariableDeclaration",
kind: "const",
declarations: [
{
type: "VariableDeclarator",
id: { type: "Identifier", name: key },
init: valueToEstree(value, { preserveReferences: true }),
},
],
})
),
},
},
});
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,17 @@ import { createMdxComponents } from "../components";

export const MdxBundlerComponent = ({
code,
scope,
frontmatter,
jsxRefs,
}: Exclude<FernDocs.MarkdownText, string>): ReactElement => {
const Component = useMemo(
() =>
getMDXComponent(code, {
// Note: do not override `props` from scope
...scope,

// allows us to use MDXProvider to pass components to children
MdxJsReact: {
useMDXComponents,
},

// allows us to use frontmatter in the MDX
frontmatter,
}),
[code, scope, frontmatter]
[code]
);
return (
<MDXProvider components={createMdxComponents(jsxRefs ?? [])}>
Expand Down
7 changes: 5 additions & 2 deletions packages/fern-docs/ui/src/mdx/bundlers/mdx-bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
rehypeAcornErrorBoundary,
rehypeMdxClassStyle,
rehypeSqueezeParagraphs,
remarkInjectEsm,
remarkSanitizeAcorn,
remarkSqueezeParagraphs,
} from "@fern-docs/mdx/plugins";
Expand Down Expand Up @@ -117,13 +118,15 @@ async function serializeMdxImpl(
const remarkPlugins: PluggableList = [
[remarkExtractTitle, { frontmatter }],
remarkSqueezeParagraphs,
remarkSanitizeAcorn,
[remarkInjectEsm, { scope }],
[remarkSanitizeAcorn],
remarkGfm,
remarkSmartypants,
remarkMath,
remarkGemoji,
];

// inject scope variables
if (options?.remarkPlugins != null) {
remarkPlugins.push(...options.remarkPlugins);
}
Expand Down Expand Up @@ -185,7 +188,7 @@ async function serializeMdxImpl(
engine: "mdx-bundler",
code: bundled.code,
frontmatter: bundled.frontmatter,
scope,
scope: {},
jsxRefs: jsxElements,
};
}
Expand Down
23 changes: 8 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a25c4da

Please sign in to comment.