Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

3.2 KB
Raw Blame
import { Predicate } from "effect";
import { Children, cloneElement, isValidElement, type ReactNode } from "react";
import type { ServerProviderSkill } from "@akeru/contracts";
import {
  formatProviderSkillDisplayName,
  resolveProviderSkillTextIcon,
} from "@akeru/client-runtime/providerSkills";

import {
  CHAT_INLINE_CHIP_LABEL_CLASS_NAME,
  CHAT_INLINE_SKILL_CHIP_CLASS_NAME,
  INLINE_SKILL_CHIP_ICON_CLASS_NAME,
  SKILL_CHIP_ICON_SVG,
} from "../composerInlineChip";

const SKILL_TOKEN_REGEX = /(^|\s)\$([a-zA-Z0-9][a-zA-Z0-9:_-]*)(?=\s|$)/g;

type InlineSkill = Pick<ServerProviderSkill, "name" | "displayName" | "icon">;

export function SkillInlineText(props: { text: string; skills: ReadonlyArray<InlineSkill> }) {
  const nodes: ReactNode[] = [];
  let cursor = 0;

  for (const match of props.text.matchAll(SKILL_TOKEN_REGEX)) {
    const prefix = match[1] ?? "";
    const name = match[2] ?? "";
    const start = (match.index ?? 0) + prefix.length;
    const rawText = `$${name}`;
    const skill = props.skills.find((candidate) => candidate.name === name);

    if (!skill) {
      continue;
    }

    if (start > cursor) {
      nodes.push(props.text.slice(cursor, start));
    }

    nodes.push(<SkillChip key={`${start}:${name}`} skill={skill} rawText={rawText} />);
    cursor = start + rawText.length;
  }

  if (cursor === 0) {
    return <>{props.text}</>;
  }

  if (cursor < props.text.length) {
    nodes.push(props.text.slice(cursor));
  }

  return <>{nodes}</>;
}

export function renderSkillInlineMarkdownChildren(
  children: ReactNode,
  skills: ReadonlyArray<InlineSkill>,
): ReactNode {
  return Children.map(children, (child) => {
    if (Predicate.isString(child)) {
      return <SkillInlineText text={child} skills={skills} />;
    }

    if (!isValidElement<{ children?: ReactNode; node?: { tagName?: string } }>(child)) {
      return child;
    }

    // Custom react-markdown components replace the intrinsic type, so also
    // check the hast node they carry.
    const markdownTagName = Predicate.isString(child.type) ? child.type : child.props.node?.tagName;

    if (markdownTagName === "code" || markdownTagName === "a") {
      return child;
    }

    if (!("children" in child.props)) {
      return child;
    }

    return cloneElement(
      child,
      undefined,
      renderSkillInlineMarkdownChildren(child.props.children, skills),
    );
  });
}

function SkillChip(props: { skill: InlineSkill; rawText: string }) {
  const textIcon = resolveProviderSkillTextIcon(props.skill);

  return (
    <span className="inline-flex align-middle leading-none" data-markdown-copy={props.rawText}>
      <span className={CHAT_INLINE_SKILL_CHIP_CLASS_NAME}>
        {textIcon ? (
          <span aria-hidden="true" className={INLINE_SKILL_CHIP_ICON_CLASS_NAME}>
            {textIcon}
          </span>
        ) : (
          <span
            aria-hidden="true"
            className={INLINE_SKILL_CHIP_ICON_CLASS_NAME}
            dangerouslySetInnerHTML={{ __html: SKILL_CHIP_ICON_SVG }}
          />
        )}
        <span className={CHAT_INLINE_CHIP_LABEL_CLASS_NAME}>
          {formatProviderSkillDisplayName(props.skill)}
        </span>
      </span>
    </span>
  );
}