Skip to content

Commit 77301c2

Browse files
committed
feat(contracts): add generated mothership stream contract sync
1 parent b9926df commit 77301c2

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Generated Mothership Stream Protocol Artifacts
2+
3+
Files in this directory are generated from the local `copilot` repo contract
4+
source and should not be edited by hand.
5+
6+
Use:
7+
8+
- `bun run mothership:generate`
9+
- `bun run mothership:check`
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// AUTO-GENERATED FILE. DO NOT EDIT.
2+
// Source: copilot/copilot/contracts/mothership-stream-v1.contract.json
3+
4+
export const MOTHERSHIP_STREAM_V1_SCHEMA_VERSION = 1 as const
5+
export const MOTHERSHIP_STREAM_V1_CONTRACT_NAME = 'mothership-stream-v1' as const
6+
7+
export type MothershipStreamV1EventType =
8+
| 'session'
9+
| 'text'
10+
| 'tool'
11+
| 'span'
12+
| 'resource'
13+
| 'run'
14+
| 'error'
15+
| 'complete'
16+
export type MothershipStreamV1SessionKind = 'trace' | 'chat' | 'title' | 'start'
17+
export type MothershipStreamV1TextChannel = 'assistant' | 'thinking'
18+
export type MothershipStreamV1ToolExecutor = 'go' | 'sim' | 'client'
19+
export type MothershipStreamV1ToolMode = 'sync' | 'async'
20+
export type MothershipStreamV1ToolPhase = 'call' | 'result'
21+
export type MothershipStreamV1ToolOutcome =
22+
| 'success'
23+
| 'error'
24+
| 'cancelled'
25+
| 'skipped'
26+
| 'rejected'
27+
export type MothershipStreamV1AsyncToolRecordStatus =
28+
| 'pending'
29+
| 'running'
30+
| 'completed'
31+
| 'failed'
32+
| 'cancelled'
33+
| 'delivered'
34+
export type MothershipStreamV1RunKind =
35+
| 'checkpoint_pause'
36+
| 'resumed'
37+
| 'compaction_start'
38+
| 'compaction_done'
39+
export type MothershipStreamV1SpanKind = 'subagent'
40+
export type MothershipStreamV1ResourceOp = 'upsert' | 'remove'
41+
42+
export interface MothershipStreamV1Scope {
43+
lane: 'main' | 'subagent'
44+
agentId?: string
45+
parentToolCallId?: string
46+
}
47+
48+
export interface MothershipStreamV1EventEnvelope {
49+
v: typeof MOTHERSHIP_STREAM_V1_SCHEMA_VERSION
50+
type: MothershipStreamV1EventType
51+
seq: number
52+
ts: string
53+
stream: {
54+
streamId: string
55+
chatId?: string
56+
cursor?: string
57+
}
58+
trace?: { requestId: string; spanId?: string }
59+
scope?: MothershipStreamV1Scope
60+
payload: Record<string, unknown>
61+
}
62+
63+
export interface MothershipStreamV1ToolCallDescriptor {
64+
toolCallId: string
65+
toolName: string
66+
arguments: Record<string, unknown>
67+
executor: MothershipStreamV1ToolExecutor
68+
mode: MothershipStreamV1ToolMode
69+
phase: MothershipStreamV1ToolPhase
70+
requiresConfirmation?: boolean
71+
}
72+
73+
export interface MothershipStreamV1ToolResultPayload {
74+
success: boolean
75+
output?: unknown
76+
error?: string
77+
}
78+
79+
export interface MothershipStreamV1ResumeToolResult {
80+
toolCallId: string
81+
success: boolean
82+
output?: unknown
83+
error?: string
84+
}
85+
86+
export interface MothershipStreamV1ResumeRequest {
87+
checkpointId: string
88+
results: MothershipStreamV1ResumeToolResult[]
89+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
"lint:helm": "helm lint ./helm/sim --strict --values ./helm/sim/test/values-lint.yaml",
2222
"lint:all": "turbo run lint && bun run lint:helm",
2323
"check": "turbo run format:check",
24+
"mothership:generate": "bun run scripts/sync-mothership-stream-contract.ts",
25+
"mothership:check": "bun run scripts/sync-mothership-stream-contract.ts --check",
2426
"prepare": "bun husky",
2527
"type-check": "turbo run type-check",
2628
"release": "bun run scripts/create-single-release.ts"
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

Comments
 (0)