Files
sim/scripts/sync-tool-catalog.ts
Siddharth Ganesan 0abcc6e813 improvement(mothership): restructured stream, tool structures, code typing, file write/patch/append tools, timing issues (#4090)
* fix build error

* improvement(mothership): new agent loop (#3920)

* feat(transport): replace shared chat transport with mothership-stream module

* improvement(contracts): regenerate contracts from go

* feat(tools): add tool catalog codegen from go tool contracts

* feat(tools): add tool-executor dispatch framework for sim side tool routing

* feat(orchestrator): rewrite tool dispatch with catalog-driven executor and simplified resume loop

* feat(orchestrator): checkpoint resume flow

* refactor(copilot): consolidate orchestrator into request/ layer

* refactor(mothership): reorganize lib/copilot into structured subdirectories

* refactor(mothership): canonical transcript layer, dead code cleanup, type consolidation

* refactor(mothership): rebase onto latest staging

* refactor(mothership): rename request continue to lifecycle

* feat(trace): add initial version of request traces

* improvement(stream): batch stream from redis

* fix(resume): fix the resume checkpoint

* fix(resume): fix resume client tool

* fix(subagents): subagent resume should join on existing subagent text block

* improvement(reconnect): harden reconnect logic

* fix(superagent): fix superagent integration tools

* improvement(stream): improve stream perf

* Rebase with origin dev

* fix(tests): fix failing test

* fix(build): fix type errors

* fix(build): fix build errors

* fix(build): fix type errors

* feat(mothership): add cli execution

* fix(mothership): fix function execute tests

* Force redeploy

* feat(motheship): add docx support

* feat(mothership): append

* Add deps

* improvement(mothership): docs

* File types

* Add client retry logic

* Fix stream reconnect

* Eager tool streaming

* Fix client side tools

* Security

* Fix shell var injection

* Remove auto injected tasks

* Fix 10mb tool response limit

* Fix trailing leak

* Remove dead tools

* file/folder tools

* Folder tools

* Hide function code inline

* Dont show internal tool result reads

* Fix spacing

* Auth vfs

* Empty folders should show in vfs

* Fix run workflow

* change to node runtime

* revert back to bun runtime

* Fix

* Appends

* Remove debug logs

* Patch

* Fix patch tool

* Temp

* Checkpoint

* File writes

* Fix

* Remove tool truncation limits

* Bad hook

* replace react markdown with streamdown

* Checkpoitn

* fix code block

* fix stream persistence

* temp

* Fix file tools

* tool joining

* cleanup subagent + streaming issues

* streamed text change

* Tool display intetns

* Fix dev

* Fix tests

* Fix dev

* Speed up dev ci

* Add req id

* Fix persistence

* Tool call names

* fix payload accesses

* Fix name

* fix snapshot crash bug

* fix

* Fix

* remove worker code

* Clickable resources

* Options ordering

* Folder vfs

* Restore and mass delete tools

* Fix

* lint

* Update request tracing and skills and handlers

* Fix editable

* fix type error

* Html code

* fix(chat): make inline code inherit parent font size in markdown headers

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* improved autolayout

* durable stream for files

* one more fix

* POSSIBLE BREAKAGE: SCROLLING

* Fixes

* Fixes

* Lint fix

* fix(resource): fix resource view disappearing on ats (#4103)

Co-authored-by: Theodore Li <theo@sim.ai>

* Fixes

* feat(mothership): add execution logs as a resource type

Adds `log` as a first-class mothership resource type so copilot can open
and display workflow execution logs as tabs alongside workflows, tables,
files, and knowledge bases.

- Add `log` to MothershipResourceType, all Zod enums, and VALID_RESOURCE_TYPES
- Register log in RESOURCE_REGISTRY (Library icon) and RESOURCE_INVALIDATORS
- Add EmbeddedLog and EmbeddedLogActions components in resource-content
- Export WorkflowOutputSection from log-details for reuse in EmbeddedLog
- Add log resolution branch in open_resource handler via new getLogById service
- Include log id in get_workflow_logs response and extract resources from output
- Exclude log from manual add-resource dropdown (enters via copilot tools only)
- Regenerate copilot contracts after adding log to open_resource Go enum

* Fix perf and message queueing

* Fix abort

* fix(ui): dont delete resource on clearing from context, set resource closed on new task (#4113)

Co-authored-by: Theodore Li <theo@sim.ai>

* improvement(mothership): structure sim side typing

* address comments

* reactive text editor tweaks

* Fix file read and tool call name persistence bug

* Fix code stream + create file opening resource

* fix use chat race + headless trace issues

* Fix type issue

* Fix mothership block req lifecycle

* Fix build

* Move copy reqid

* Fix

* fix(ui): fix resource tag transition from home to task (#4132)

Co-authored-by: Theodore Li <theo@sim.ai>

* Fix persistence

---------

Co-authored-by: Vikhyath Mondreti <vikhyath@simstudio.ai>
Co-authored-by: Waleed Latif <walif6@gmail.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Theodore Li <theo@sim.ai>
Co-authored-by: Theodore Li <theodoreqili@gmail.com>
2026-04-13 16:46:35 -07:00

230 lines
7.4 KiB
TypeScript

import { mkdir, readFile, writeFile } from 'node:fs/promises'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url))
const ROOT = resolve(SCRIPT_DIR, '..')
const DEFAULT_CATALOG_PATH = resolve(ROOT, '../copilot/copilot/contracts/tool-catalog-v1.json')
const OUTPUT_PATH = resolve(ROOT, 'apps/sim/lib/copilot/generated/tool-catalog-v1.ts')
const RUNTIME_SCHEMA_OUTPUT_PATH = resolve(
ROOT,
'apps/sim/lib/copilot/generated/tool-schemas-v1.ts'
)
function snakeToPascal(s: string): string {
return s
.split('_')
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
.join('')
}
function toCamelIdentifier(value: string): string {
const parts = value.split(/[^a-zA-Z0-9]+/).filter(Boolean)
if (parts.length === 0) return 'value'
const camel = parts
.map((part, index) => {
const lower = part.toLowerCase()
if (index === 0) return lower
return lower.charAt(0).toUpperCase() + lower.slice(1)
})
.join('')
return /^[0-9]/.test(camel) ? `v${camel}` : camel
}
function getTopLevelOperationEnum(tool: Record<string, unknown>): string[] | undefined {
const parameters =
typeof tool.parameters === 'object' && tool.parameters !== null
? (tool.parameters as Record<string, unknown>)
: null
const properties =
parameters && typeof parameters.properties === 'object' && parameters.properties !== null
? (parameters.properties as Record<string, unknown>)
: null
const operation =
properties && typeof properties.operation === 'object' && properties.operation !== null
? (properties.operation as Record<string, unknown>)
: null
const values = operation?.enum
if (!Array.isArray(values) || values.some((value) => typeof value !== 'string')) {
return undefined
}
return values as string[]
}
function inferTSType(values: unknown[]): string {
const unique = [...new Set(values.filter((v) => v !== undefined && v !== null))]
if (unique.length === 0) return 'string'
if (unique.every((v) => typeof v === 'string')) {
return unique
.map((v) => JSON.stringify(v))
.sort()
.join(' | ')
}
if (unique.every((v) => typeof v === 'boolean')) return 'boolean'
if (unique.every((v) => typeof v === 'number')) return 'number'
return 'unknown'
}
function renderRuntimeSchemaModule(catalog: { tools: Record<string, unknown>[] }): string {
const lines: string[] = [
'// AUTO-GENERATED FILE. DO NOT EDIT.',
'// Generated from copilot/contracts/tool-catalog-v1.json',
'//',
'',
'export type JsonSchema = unknown',
'',
'export interface ToolRuntimeSchemaEntry {',
' parameters?: JsonSchema;',
' resultSchema?: JsonSchema;',
'}',
'',
'export const TOOL_RUNTIME_SCHEMAS: Record<string, ToolRuntimeSchemaEntry> = {',
]
for (const tool of catalog.tools) {
const id = JSON.stringify(tool.id)
const parameters =
'parameters' in tool ? JSON.stringify(tool.parameters ?? null, null, 2) : 'undefined'
const resultSchema =
'resultSchema' in tool ? JSON.stringify(tool.resultSchema ?? null, null, 2) : 'undefined'
lines.push(` [${id}]: {`)
lines.push(
` parameters: ${parameters === 'null' ? 'undefined' : parameters.replace(/\n/g, '\n ')},`
)
lines.push(
` resultSchema: ${resultSchema === 'null' ? 'undefined' : resultSchema.replace(/\n/g, '\n ')},`
)
lines.push(' },')
}
lines.push('}')
lines.push('')
return lines.join('\n')
}
function generateInterface(tools: Record<string, unknown>[]): string {
if (tools.length === 0) return 'export interface ToolCatalogEntry {}\n'
const allKeys = new Set<string>()
for (const tool of tools) {
for (const key of Object.keys(tool)) {
allKeys.add(key)
}
}
const requiredKeys = new Set<string>()
for (const key of allKeys) {
if (tools.every((t) => key in t)) {
requiredKeys.add(key)
}
}
const lines: string[] = ['export interface ToolCatalogEntry {']
for (const key of [...allKeys].sort()) {
const values = tools.map((t) => t[key])
const tsType = inferTSType(values)
const optional = requiredKeys.has(key) ? '' : '?'
lines.push(` ${key}${optional}: ${tsType};`)
}
lines.push('}')
return lines.join('\n')
}
async function main() {
const checkOnly = process.argv.includes('--check')
const inputPathArg = process.argv.find((arg) => arg.startsWith('--input='))
const inputPath = inputPathArg
? resolve(ROOT, inputPathArg.slice('--input='.length))
: DEFAULT_CATALOG_PATH
const raw = await readFile(inputPath, 'utf8')
const catalog = JSON.parse(raw) as { version: string; tools: Record<string, unknown>[] }
const iface = generateInterface(catalog.tools)
const lines: string[] = [
'// AUTO-GENERATED FILE. DO NOT EDIT.',
'// Generated from copilot/contracts/tool-catalog-v1.json',
'//',
'',
iface,
'',
]
const constNames: string[] = []
for (const tool of catalog.tools) {
const constName = snakeToPascal(tool.id as string)
constNames.push(constName)
const fields: string[] = []
for (const [key, value] of Object.entries(tool)) {
fields.push(` ${key}: ${JSON.stringify(value)}`)
}
lines.push(`export const ${constName}: ToolCatalogEntry = {`)
lines.push(`${fields.join(',\n')},`)
lines.push('};')
lines.push('')
}
for (const tool of catalog.tools) {
const constName = snakeToPascal(tool.id as string)
const operationEnum = getTopLevelOperationEnum(tool)
if (!operationEnum || operationEnum.length === 0) continue
const operationConstName = `${constName}Operation`
const seenKeys = new Set<string>()
const members = operationEnum.map((value, index) => {
let key = toCamelIdentifier(value)
if (seenKeys.has(key)) key = `${key}${index + 1}`
seenKeys.add(key)
return { key, value }
})
lines.push(`export const ${operationConstName} = {`)
for (const member of members) {
lines.push(` ${member.key}: ${JSON.stringify(member.value)},`)
}
lines.push('} as const;')
lines.push('')
lines.push(
`export type ${operationConstName} = (typeof ${operationConstName})[keyof typeof ${operationConstName}];`
)
lines.push('')
lines.push(`export const ${operationConstName}Values = [`)
for (const member of members) {
lines.push(` ${operationConstName}.${member.key},`)
}
lines.push(`] as const;`)
lines.push('')
}
lines.push(`export const TOOL_CATALOG: Record<string, ToolCatalogEntry> = {`)
for (let i = 0; i < catalog.tools.length; i++) {
lines.push(` [${constNames[i]}.id]: ${constNames[i]},`)
}
lines.push('};')
lines.push('')
const rendered = lines.join('\n')
const runtimeSchemaRendered = renderRuntimeSchemaModule(catalog)
if (checkOnly) {
const existing = await readFile(OUTPUT_PATH, 'utf8').catch(() => null)
const existingRuntime = await readFile(RUNTIME_SCHEMA_OUTPUT_PATH, 'utf8').catch(() => null)
if (existing !== rendered || existingRuntime !== runtimeSchemaRendered) {
throw new Error(`Generated tool catalog is stale. Run: bun run mship-tools:generate`)
}
return
}
await mkdir(dirname(OUTPUT_PATH), { recursive: true })
await writeFile(OUTPUT_PATH, rendered, 'utf8')
await mkdir(dirname(RUNTIME_SCHEMA_OUTPUT_PATH), { recursive: true })
await writeFile(RUNTIME_SCHEMA_OUTPUT_PATH, runtimeSchemaRendered, 'utf8')
}
await main()