Blog
stripIndents utility in Chef codebase.

stripIndents utility in Chef codebase.

In this article, we review stripIndents utility in Chef codebase. We will look at:

  1. What is Chef?

  2. stripIndents utility.

I study patterns used in an open source project found on Github Trending. For this week, I reviewed some parts of Chef codebase and wrote this article.

What is Chef?

Chef is open-source, check out the GitHub repo here.

This is the only AI app builder that knows backend. By applying Convex primitives directly to your code generation, your apps are automatically equipped with optimal backend patterns and best practices. Your full-stack apps come with a built-in database, zero config auth, file uploads, real-time UIs, and background workflows. If you want to check out the secret sauce that powers Chef, you can view or download the system prompt here.

As mentioned above, Chef’s capabilities are enabled by being built on top of Convex, the open-source reactive database designed to make life easy for web app developers. The “magic” in Chef is just the fact that it’s using Convex’s APIs, which are an ideal fit for codegen.

This project is a fork of the stable branch of bolt.diy.

Learn more about Chef.

stripIndents utility

You will find the following code in secretsInstructions.ts

import { stripIndents } from '../utils/stripIndent.js';
import type { SystemPromptOptions } from '../types.js';

export function secretsInstructions(_options: SystemPromptOptions) {
  return stripIndents`
   <secrets_instructions>
      If you need to use a secret to call into an API, instruct the user to set up the secret as an
      environment variable in their Convex deployment.

      1. Tell the user to setup the secret as an environment variable, and tell them exactly what
         name to use (e.g. \`OPENAI_API_KEY\`).
      2. Give the user clear instructions for how to set the environment variable. They can do so
         by opening the "Database" tab, clicking on "Settings" (with the gear icon), clicking on
         "Environment variables", and then setting the variable.
      3. After the user confirms they've set the environment variable, you can use the secret in your
         code.
   </secrets_instructions>
`;
}

How does calling stripIndents without ( or ) invoke the function? This usage is called template literals in JavaScript. Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded expressions, and special constructs called tagged templates.

Learn more about Tempate Literals.

stripIndents definition

You will find the following code in stripindent.ts

export function stripIndents(value: string): string;
export function stripIndents(strings: TemplateStringsArray, ...values: any[]): string;
export function stripIndents(arg0: string | TemplateStringsArray, ...values: any[]) {
  if (typeof arg0 !== 'string') {
    const processedString = arg0.reduce((acc, curr, i) => {
      acc += curr + (values[i] ?? '');
      return acc;
    }, '');

    return _stripIndents(processedString);
  }

  return _stripIndents(arg0);
}

function _stripIndents(value: string) {
  let minIndent = Infinity;
  for (const line of value.split('\n')) {
    const trimmed = line.trimStart();
    if (trimmed.length === 0) {
      continue;
    }
    minIndent = Math.min(minIndent, line.length - trimmed.length);
  }
  if (minIndent === Infinity) {
    return value;
  }
  return value
    .split('\n')
    .map((line) => line.slice(minIndent).trimEnd())
    .filter((line) => line.length > 0)
    .join('\n')
    .replace(/[\r\n]$/, '');
}

This is function overloading in TypeScript. This function definition is straight forward, it simply removes the indents from the prompt. 

I played around with a prompt

and found that we can remove the indentation in front of the string by calling line.trimStart function.

About me:

Hey, my name is Ramu Narasinga. I study codebase architecture in large open-source projects.

Email: ramu.narasinga@gmail.com

I spent 200+ hours analyzing Supabase, shadcn/ui, LobeChat. Found the patterns that separate AI slop from production code. Stop refactoring AI slop. Start with proven patterns. Check out production-grade projects at thinkthroo.com

References:

  1. chef-agent/prompts/secretsInstructions.ts#L5

  2. https://github.com/get-convex/chef/blob/main/chef-agent/utils/stripIndent.ts#L1

  3. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals