Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

9.3 KB
Raw Blame
import { err, ok } from 'neverthrow';
// Usage:
// bun run scripts/generate-dcs-tools.ts
//
// This script generates AI tool schemas by running the local Rust binary
// and generates TS types for use in the chat block and service package.

import { mkdir, readFile } from 'node:fs/promises';
import * as path from 'node:path';
import { $ } from 'bun';
import $RefParser from '@apidevtools/json-schema-ref-parser';
import { compile } from 'json-schema-to-typescript';
import { jsonSchemaToZod } from 'json-schema-to-zod';
import { z } from 'zod';
import { documentCognitionBase } from './services';

const rustWorkspaceDir = path.resolve(import.meta.dirname, '../../..');
const aiToolsDir = path.join(rustWorkspaceDir, 'crates', 'ai_tools');
const schemasJsonPath = path.join(aiToolsDir, 'schemas', 'tools.json');

const serviceDir = path.resolve(
  import.meta.dirname,
  documentCognitionBase.output
);
const outputDir = path.join(serviceDir, 'generated', 'tools');
const schemasFile = path.join(outputDir, 'schemas.ts');
const typesFile = path.join(outputDir, 'types.ts');
const toolFile = path.join(outputDir, 'tool.ts');

await $`rm ${schemasFile} ${toolFile} ${typesFile}|| true`;

const warning = `/* Generated by scripts/generate-dcs-tools.ts
 * *DO NOT EDIT MANUALLY*
 */`;

await mkdir(outputDir, { recursive: true });

type JsonObject = Record<string, unknown>;

const JsonObjectValidator: z.ZodType<JsonObject> = z.record(
  z.string(),
  z.unknown()
);

// The frontend typegen schema (ai_toolset FrontendSchemas) has shared $defs
// and a tools array
const FrontendSchemasValidator = z.object({
  $defs: z.record(z.string(), JsonObjectValidator),
  tools: z.array(
    z.object({
      name: z.string(),
      input: z.string(),
      output: z.string(),
    })
  ),
});

type FrontendSchemas = z.infer<typeof FrontendSchemasValidator>;

async function buildAndRunSchemaGenerator(): Promise<void> {
  console.log('Building gen_tool_schemas binary...');
  // No `--package`: like the OpenAPI bins and apps/docs' generator, resolve
  // features across the workspace, so one build serves every caller instead
  // of `--package ai_tools` compiling a second feature set of its deps.
  await $`cd ${rustWorkspaceDir} && SQLX_OFFLINE=true cargo build --bin gen_tool_schemas`;
  console.log('Build complete.');

  await $`rm -rf ${path.join(aiToolsDir, 'schemas')}`.quiet();

  console.log('Running gen_tool_schemas...');
  const binaryPath = path.join(
    rustWorkspaceDir,
    'target',
    'debug',
    'gen_tool_schemas'
  );
  await $`cd ${aiToolsDir} && ${binaryPath}`;
  console.log('Schema generation complete.');
}

async function loadFrontendSchemas(): Promise<FrontendSchemas> {
  await buildAndRunSchemaGenerator();
  const data = await readFile(schemasJsonPath, 'utf-8');
  const parsed = FrontendSchemasValidator.parse(JSON.parse(data));
  console.log(
    `Loaded ${parsed.tools.length} tools, ${Object.keys(parsed.$defs).length} definitions`
  );
  return parsed;
}

// json-schema-to-zod drops both `.optional()` and `.default()` for an optional
// property whose schema is `anyOf: [<multi-literal oneOf>, null]` with a
// `default` (e.g. an `Option<Enum>` field). The result is a required key the
// model legitimately omits, so `deserializeToolCall` fails and the whole tool
// card is hidden. The `default` is meaningless for parse-to-render, so strip it
// from every optional (non-required) property; the field then renders as
// `.optional()`.
function stripOptionalDefaults(node: unknown, seen = new WeakSet<object>()): void {
  if (!node || typeof node !== 'object' || seen.has(node)) return;
  seen.add(node);
  if (Array.isArray(node)) {
    for (const child of node) stripOptionalDefaults(child, seen);
    return;
  }
  const obj = node as Record<string, unknown>;
  const properties = obj.properties;
  if (properties && typeof properties === 'object') {
    const required = new Set(Array.isArray(obj.required) ? obj.required : []);
    for (const [key, propSchema] of Object.entries(properties)) {
      if (
        !required.has(key) &&
        propSchema &&
        typeof propSchema === 'object' &&
        'default' in propSchema
      ) {
        delete (propSchema as Record<string, unknown>).default;
      }
    }
  }
  for (const value of Object.values(obj)) stripOptionalDefaults(value, seen);
}

async function generateSchemasFile(schema: FrontendSchemas) {
  const resolved = await $RefParser.dereference(structuredClone(schema));
  stripOptionalDefaults(resolved);
  const defs = (resolved as { $defs: Record<string, JsonObject> }).$defs;
  const seen = new Set<string>();
  const content: string[] = [];

  for (const tool of schema.tools) {
    for (const defName of [tool.input, tool.output]) {
      if (seen.has(defName)) continue;
      seen.add(defName);

      const resolvedDef = defs[defName];
      const schemaWithTitle = { ...resolvedDef, title: defName };

      content.push(
        jsonSchemaToZod(schemaWithTitle, {
          module: 'esm',
          name: defName,
          withoutDescribes: true,
          noImport: true,
        })
      );
    }
  }

  await Bun.write(
    schemasFile,
    `${warning}\nimport { z } from 'zod/v3';\n\n${content.join('\n\n')}\n`
  );
}

async function generateToolTypesFile(schema: FrontendSchemas) {
  const properties: Record<string, unknown> = {};
  for (const key of Object.keys(schema.$defs)) {
    properties[key] = { $ref: `#/$defs/${key}` };
  }

  const code = await compile(
    {
      $defs: schema.$defs,
      type: 'object',
      properties,
      additionalProperties: false,
    },
    'AllToolTypes',
    {
      additionalProperties: false,
      style: { singleQuote: true },
    }
  );

  // Strip the root wrapper interface — we only want the definition types
  const cleaned = code
    .replace(/export interface AllToolTypes\s*\{[\s\S]*?\n\}\n*/, '')
    // Empty Rust structs compile to `export interface X {}`, which biome's
    // `noEmptyInterface` rule rejects. Rewrite to a type alias.
    .replace(/export interface (\w+) \{\}/g, 'export type $1 = {};');

  await Bun.write(typesFile, `${warning}\n${cleaned}`);
}

async function generateToolsFile(schema: FrontendSchemas) {
  const entries = schema.tools
    .map((t) => ({
      name: t.name,
      inputTitle: t.input,
      outputTitle: t.output,
    }))
    .sort((a, b) => a.name.localeCompare(b.name));

  const contents = `${warning}


import type { ResultError } from '@core/util/result';
import { err, ok, type Result } from 'neverthrow';
import { bookingLinkHistory } from '../../booking-link-history';
import * as schemas from './schemas';
import type * as types from './types';

type ToolParserMap = {
${entries
  .map(
    (t) =>
      `${t.name}: { call: types.${t.inputTitle}, response: types.${t.outputTitle} }`
  )
  .join('\n')}
};

const toolParserMap = {
${entries
  .map(
    (t) =>
      `${t.name}: { call: schemas.${t.inputTitle}, response: schemas.${t.outputTitle} }`
  )
  .join(',\n')}
};

export type ToolName = keyof ToolParserMap;

type NamedRawTool = {
  id: string;
  name: string;
  json: unknown;
};

type ToolDataMap = {
${entries
  .map(
    (t) =>
      `${t.name}: { call: types.${t.inputTitle}, response: types.${t.outputTitle} }`
  )
  .join(';\n')};
};

export type NamedTool<
  TName extends ToolName = ToolName,
  TDirection extends 'call' | 'response' = 'call' | 'response',
> = {
  id: string;
  name: TName;
  data: ToolDataMap[TName][TDirection];
};

function deserializeTool<T extends NamedTool>(
  tool: NamedRawTool,
  direction: 'call' | 'response'
): Result<T, ResultError<'parse_error' | 'not_found'>[]> {
  if (!(tool.name in toolParserMap)) {
    return err([{ code: 'not_found', message: \`tool name not found \${tool.name}\` }]);
  }
  const parser = toolParserMap[tool.name as ToolName];
  const maybeToolCall = parser[direction].safeParse(bookingLinkHistory(tool.name, direction, tool.json));
  if (maybeToolCall.success) {
    return ok({
      id: tool.id,
      name: tool.name as ToolName,
      data: maybeToolCall.data,
    } as T);
  }
  return err([{ code: 'parse_error', message: 'tool parsing failed' }]);
}

export function deserializeToolCall(
  tool: NamedRawTool
): Result<NamedTool<ToolName, 'call'>, ResultError<'parse_error' | 'not_found'>[]> {
  return deserializeTool(tool, 'call');
}

export function deserializeToolResponse(
  tool: NamedRawTool
): Result<NamedTool<ToolName, 'response'>, ResultError<'parse_error' | 'not_found'>[]> {
  return deserializeTool(tool, 'response');
}
`;
  await Bun.write(toolFile, contents);
}

const schema = await loadFrontendSchemas();

await generateSchemasFile(schema);
await generateToolsFile(schema);
await generateToolTypesFile(schema);

// On NixOS, the npm-installed biome binary doesn't work due to dynamic linking issues.
const isNixOS =
  process.env.NIX_PATH !== undefined ||
  ((await Bun.file('/etc/os-release').exists()) &&
    (await Bun.file('/etc/os-release').text()).includes('NixOS'));
if (isNixOS) {
  const systemBiomePath =
    await $`bash -c 'PATH=$(echo "$PATH" | tr ":" "\n" | grep -v node_modules | tr "\n" ":") which biome'`.text();
  await $`${systemBiomePath.trim()} format --write --no-errors-on-unmatched ${toolFile} ${schemasFile} ${typesFile}`;
} else {
  await $`biome format --write --no-errors-on-unmatched ${toolFile} ${schemasFile} ${typesFile}`;
}

console.log(`Generated AI tools types at: ${outputDir}`);
console.log('✅ AI tools types generation completed successfully');