|
| 1 | +import { mkdir, readFile, writeFile } from 'node:fs/promises' |
| 2 | +import { dirname, resolve } from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | + |
| 5 | +type ContractJson = { |
| 6 | + $schemaVersion: number |
| 7 | + name: string |
| 8 | + description: string |
| 9 | + eventTypes: string[] |
| 10 | + sessionKinds: string[] |
| 11 | + textChannels: string[] |
| 12 | + toolExecutors: string[] |
| 13 | + toolModes: string[] |
| 14 | + toolPhases: string[] |
| 15 | + toolOutcomes: string[] |
| 16 | + asyncToolRecordStatuses: string[] |
| 17 | + runKinds: string[] |
| 18 | + spanKinds: string[] |
| 19 | + resourceOps: string[] |
| 20 | +} |
| 21 | + |
| 22 | +const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url)) |
| 23 | +const ROOT = resolve(SCRIPT_DIR, '..') |
| 24 | +const DEFAULT_CONTRACT_PATH = resolve( |
| 25 | + ROOT, |
| 26 | + '../copilot/copilot/contracts/mothership-stream-v1.contract.json' |
| 27 | +) |
| 28 | +const OUTPUT_PATH = resolve(ROOT, 'apps/sim/lib/copilot/generated/mothership-stream-v1.ts') |
| 29 | + |
| 30 | +function toUnion(values: string[]) { |
| 31 | + return values.map((value) => `'${value}'`).join(' | ') |
| 32 | +} |
| 33 | + |
| 34 | +function renderContract(contract: ContractJson) { |
| 35 | + return `// AUTO-GENERATED FILE. DO NOT EDIT. |
| 36 | +// Source: copilot/copilot/contracts/mothership-stream-v1.contract.json |
| 37 | +
|
| 38 | +export const MOTHERSHIP_STREAM_V1_SCHEMA_VERSION = ${contract.$schemaVersion} as const |
| 39 | +export const MOTHERSHIP_STREAM_V1_CONTRACT_NAME = '${contract.name}' as const |
| 40 | +
|
| 41 | +export type MothershipStreamV1EventType = ${toUnion(contract.eventTypes)} |
| 42 | +export type MothershipStreamV1SessionKind = ${toUnion(contract.sessionKinds)} |
| 43 | +export type MothershipStreamV1TextChannel = ${toUnion(contract.textChannels)} |
| 44 | +export type MothershipStreamV1ToolExecutor = ${toUnion(contract.toolExecutors)} |
| 45 | +export type MothershipStreamV1ToolMode = ${toUnion(contract.toolModes)} |
| 46 | +export type MothershipStreamV1ToolPhase = ${toUnion(contract.toolPhases)} |
| 47 | +export type MothershipStreamV1ToolOutcome = ${toUnion(contract.toolOutcomes)} |
| 48 | +export type MothershipStreamV1AsyncToolRecordStatus = ${toUnion(contract.asyncToolRecordStatuses)} |
| 49 | +export type MothershipStreamV1RunKind = ${toUnion(contract.runKinds)} |
| 50 | +export type MothershipStreamV1SpanKind = ${toUnion(contract.spanKinds)} |
| 51 | +export type MothershipStreamV1ResourceOp = ${toUnion(contract.resourceOps)} |
| 52 | +
|
| 53 | +export interface MothershipStreamV1Scope { |
| 54 | + lane: 'main' | 'subagent' |
| 55 | + agentId?: string |
| 56 | + parentToolCallId?: string |
| 57 | +} |
| 58 | +
|
| 59 | +export interface MothershipStreamV1EventEnvelope { |
| 60 | + v: typeof MOTHERSHIP_STREAM_V1_SCHEMA_VERSION |
| 61 | + type: MothershipStreamV1EventType |
| 62 | + seq: number |
| 63 | + ts: string |
| 64 | + stream: { |
| 65 | + streamId: string |
| 66 | + chatId?: string |
| 67 | + cursor?: string |
| 68 | + } |
| 69 | + trace?: { requestId: string; spanId?: string } |
| 70 | + scope?: MothershipStreamV1Scope |
| 71 | + payload: Record<string, unknown> |
| 72 | +} |
| 73 | +
|
| 74 | +export interface MothershipStreamV1ToolCallDescriptor { |
| 75 | + toolCallId: string |
| 76 | + toolName: string |
| 77 | + arguments: Record<string, unknown> |
| 78 | + executor: MothershipStreamV1ToolExecutor |
| 79 | + mode: MothershipStreamV1ToolMode |
| 80 | + phase: MothershipStreamV1ToolPhase |
| 81 | + requiresConfirmation?: boolean |
| 82 | +} |
| 83 | +
|
| 84 | +export interface MothershipStreamV1ToolResultPayload { |
| 85 | + success: boolean |
| 86 | + output?: unknown |
| 87 | + error?: string |
| 88 | +} |
| 89 | +
|
| 90 | +export interface MothershipStreamV1ResumeToolResult { |
| 91 | + toolCallId: string |
| 92 | + success: boolean |
| 93 | + output?: unknown |
| 94 | + error?: string |
| 95 | +} |
| 96 | +
|
| 97 | +export interface MothershipStreamV1ResumeRequest { |
| 98 | + checkpointId: string |
| 99 | + results: MothershipStreamV1ResumeToolResult[] |
| 100 | +} |
| 101 | +` |
| 102 | +} |
| 103 | + |
| 104 | +async function main() { |
| 105 | + const checkOnly = process.argv.includes('--check') |
| 106 | + const inputPathArg = process.argv.find((arg) => arg.startsWith('--input=')) |
| 107 | + const inputPath = inputPathArg ? resolve(ROOT, inputPathArg.slice('--input='.length)) : DEFAULT_CONTRACT_PATH |
| 108 | + |
| 109 | + const raw = await readFile(inputPath, 'utf8') |
| 110 | + const contract = JSON.parse(raw) as ContractJson |
| 111 | + const rendered = renderContract(contract) |
| 112 | + |
| 113 | + if (checkOnly) { |
| 114 | + const existing = await readFile(OUTPUT_PATH, 'utf8').catch(() => null) |
| 115 | + if (existing !== rendered) { |
| 116 | + throw new Error( |
| 117 | + `Generated mothership stream contract is stale. Run: bun run mothership:generate` |
| 118 | + ) |
| 119 | + } |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + await mkdir(dirname(OUTPUT_PATH), { recursive: true }) |
| 124 | + await writeFile(OUTPUT_PATH, rendered, 'utf8') |
| 125 | +} |
| 126 | + |
| 127 | +await main() |
0 commit comments