Repository navigation
Expand file tree
/
Copy pathcodex.js
More file actions
307 lines (288 loc) · 12.8 KB
/
Copy pathcodex.js
File metadata and controls
307 lines (288 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* Codex(Astra) architecture bridge for the "Minecraft 架构师" agent preset.
*
* This module is mounted by `preset/minecraft-architect/` ONLY — never by the
* bundle patch — so the Minecraft expert preset keeps exactly the tool and
* skill catalog it shipped with. It contributes two things to that one preset:
*
* - the `mc_codex` tool, which writes the architecture brief to a visible file
* and runs the user's own Codex CLI on it as a normal, inspectable child
* process (command, output, exit code, changed files and session id all come
* back into the session);
* - the `minecraft-codex-architect` skill, whose body carries the workflow and
* the Coder rules (the Cherry Studio Architect/Coder split, with the
* `[TODO: Agent B]` marker as the handoff contract).
* @module minecraft-dev/codex
*/
import { mkdir, readFile, stat, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineTool } from '@deepseek-ai/dsh-tools'
import { BUNDLED_SKILL_RANK } from '@deepseek-ai/dsh-skill'
import Schema from '@deepseek-ai/schemastery'
import { runCodexAppServer } from './lib/codex-app-server.js'
import { architectFacts, renderArchitectBrief } from './lib/architect-brief.js'
import {
CODEX_SANDBOXES,
DEFAULT_CODEX_SANDBOX,
renderCodexResult,
runCodex,
} from './lib/codex-cli.js'
import { codexCallView, codexResultView } from './lib/present.js'
import { SUPPORTED_PLATFORMS } from './lib/versions.js'
/** Cordis plugin name. */
export const name = 'minecraft-codex'
/** Services this plugin consumes: the tool and skill registries. */
export const inject = ['tools', 'skills']
/** Deployment configuration. */
export const Config = Schema.object({
/** Default mc_codex timeout: framework-writing runs are long (default 30 min). */
codexTimeoutMs: Schema.number().default(1_800_000),
/** Characters mc_codex keeps from the merged Codex output (head + tail). */
outputTailChars: Schema.number().default(20_000),
})
/** The single skill this module contributes (preset-scoped, unlike skills.js). */
const SKILL_NAME = 'minecraft-codex-architect'
const SKILL_PROVIDER = 'minecraft-dev-codex'
const SKILL_DESCRIPTION = 'Use the user\'s own Codex (Astra) as a Minecraft ARCHITECT: write a '
+ 'visible architecture brief, run `codex exec` on it, and get a skeleton whose business-logic '
+ 'method bodies are `// [TODO: Agent B] <description>` markers plus a FILL-SPEC.md. Then fill '
+ 'exactly those markers yourself and verify with mc_gradle. Use only when the user explicitly '
+ 'asks for Codex/Astra or for an architect/coder split, and only when the mc_codex tool is '
+ 'available in this session.'
const ASSET_DIR = fileURLToPath(new URL('./assets/codex-architect/', import.meta.url))
const SKILL_LOCATOR = new URL('./assets/codex-architect/SKILL.md', import.meta.url)
const CODEX_DESCRIPTION = 'Use the user\'s own Codex CLI as an architect for one Minecraft project. '
+ 'Writes <projectDir>/.dsh/codex-architect.md (the exact prompt, readable and editable), then '
+ 'runs Codex on it through a visible command with the user\'s own Codex account and '
+ 'configuration. Returns the command, the full merged transcript, the exit code, the files the '
+ 'run changed, and the Codex thread id. Two modes: "app-server" (default) drives the official '
+ '`codex app-server --stdio` protocol, which creates a REAL Codex session that shows up in the '
+ 'Codex desktop app list and can be continued there; "exec" runs `codex exec` as a plain child '
+ 'process, which is simpler but its sessions stay hidden from the desktop app. Both send the '
+ 'prompt from the file, so nothing is hidden and the user can reproduce the run themselves. '
+ 'Expect a skeleton with `// [TODO: Agent B] <description>` markers plus a FILL-SPEC.md; that is '
+ 'the handoff contract for mc_codex uses.'
/** Transport modes for mc_codex; see the module description for the tradeoff. */
export const CODEX_MODES = ['app-server', 'exec']
/** Default mode: app-server, so every DSH-driven Codex session is app-visible. */
export const DEFAULT_CODEX_MODE = 'app-server'
/** Assert the target project directory exists; a missing one is a caller bug. */
async function assertProjectDir(projectDir) {
let info
try {
info = await stat(projectDir)
} catch {
throw new Error(`projectDir does not exist: ${projectDir} — create it (mc_scaffold or mkdir) before calling mc_codex`)
}
if (!info.isDirectory()) throw new Error(`projectDir is not a directory: ${projectDir}`)
}
/** The bundled skill provider, mirroring the skills.js provider pattern. */
function makeSkillProvider() {
const candidate = {
name: SKILL_NAME,
description: SKILL_DESCRIPTION,
invocation: { modelInvocable: true, userInvocable: true },
provider: SKILL_PROVIDER,
source: 'bundled',
resourceBase: { kind: 'directory', path: ASSET_DIR },
rank: BUNDLED_SKILL_RANK,
locator: SKILL_LOCATOR,
}
return {
name: SKILL_PROVIDER,
list: () => Promise.resolve([candidate]),
async get() {
return { ...candidate, content: await readFile(SKILL_LOCATOR, 'utf8') }
},
}
}
/** Register the mc_codex tool and the architect skill on `ctx`. */
export function apply(ctx, config = {}) {
const provider = makeSkillProvider()
ctx.skills.registerProvider(() => provider)
ctx.tools.register(defineTool({
name: 'mc_codex',
description: CODEX_DESCRIPTION,
parameters: {
projectDir: {
type: 'string',
required: true,
description: 'Absolute path of the existing project directory. Codex runs with this as its working directory (-C).',
},
goal: {
type: 'string',
required: true,
description: 'What the plugin should do — one paragraph of functional requirements for the architect.',
},
platform: {
type: 'string',
required: true,
enum: [...SUPPORTED_PLATFORMS],
description: `Project platform. Supported: ${SUPPORTED_PLATFORMS.join(', ')}.`,
},
minecraftVersion: {
type: 'string',
required: true,
description: 'Target Minecraft version, e.g. 1.21.8, 1.12.2, 26.3. Coordinates come from the shipped line matrix.',
},
javaVersion: {
type: 'integer',
enum: [8, 17, 21, 25],
description: 'Java language level for the brief. Omit for the era default.',
},
sandbox: {
type: 'string',
enum: [...CODEX_SANDBOXES],
description: `Codex sandbox (-s) for the run. Default ${DEFAULT_CODEX_SANDBOX}, which the architect needs to write files.`,
},
mode: {
type: 'string',
enum: [...CODEX_MODES],
description: `How to drive Codex. ${DEFAULT_CODEX_MODE} (default) speaks the official app-server protocol, so the session is a real Codex session that appears in the Codex desktop app and can be continued there. exec spawns \`codex exec\` directly: same prompt, but the session stays out of the desktop app list. Use exec only when the app-server path is unavailable or the user asks for it.`,
},
model: {
type: 'string',
description: 'Codex model (-m) for this run, e.g. deepseek-v4-pro. Omit to use the user\'s native Codex model.',
},
timeoutMs: {
type: 'number',
description: `Timeout in milliseconds; defaults to ${config.codexTimeoutMs ?? 1_800_000}. The whole process tree is killed on expiry.`,
},
description: {
type: 'string',
description: 'Short human description shown on the terminal card.',
},
},
output: {
schema: {
type: 'object',
additionalProperties: false,
properties: {
mode: { type: 'string', required: true },
command: { type: 'string', required: true },
executable: { type: 'string', required: true },
projectDir: { type: 'string', required: true },
promptFile: { type: 'string', required: true },
lastMessageFile: { type: 'string', required: true },
sandbox: { type: 'string', required: true },
model: { oneOf: [{ type: 'string' }, { type: 'null' }], required: true },
exitCode: { required: true, oneOf: [{ type: 'integer' }, { type: 'null' }] },
signal: { required: true, oneOf: [{ type: 'string' }, { type: 'null' }] },
timedOut: { type: 'boolean', required: true },
aborted: { type: 'boolean', required: true },
timeoutMs: { type: 'number', required: true },
durationMs: { type: 'number', required: true },
sessionId: { oneOf: [{ type: 'string' }, { type: 'null' }], required: true },
threadSource: { oneOf: [{ type: 'string' }, { type: 'null' }], required: true },
tokensUsed: { oneOf: [{ type: 'number' }, { type: 'null' }], required: true },
rolloutPath: { oneOf: [{ type: 'string' }, { type: 'null' }], required: true },
filesChanged: { type: 'array', required: true, items: { type: 'string' } },
briefBytes: { type: 'number', required: true },
target: {
type: 'object',
additionalProperties: false,
required: true,
properties: {
platform: { type: 'string', required: true },
minecraftVersion: { type: 'string', required: true },
javaVersion: { type: 'number', required: true },
apiCoord: { oneOf: [{ type: 'string' }, { type: 'null' }], required: true },
},
},
output: {
type: 'object',
additionalProperties: false,
required: true,
properties: {
text: { type: 'string', required: true },
truncated: { type: 'boolean', required: true },
},
},
},
},
render: (_args, value) => [{
type: 'text',
// The app-server envelope already carries its own assembled transcript
// (answer + observed items + thread identity); exec mode is rendered here.
text: value.mode === 'app-server'
? value.output.text
: renderCodexResult(value, {
projectDir: value.projectDir,
sandbox: value.sandbox,
model: value.model,
}),
}],
},
async execute(args, exec) {
const projectDir = args.projectDir
await assertProjectDir(projectDir)
const facts = architectFacts({
platform: args.platform,
minecraftVersion: args.minecraftVersion,
javaVersion: args.javaVersion,
})
const template = await readFile(new URL('./assets/codex-architect/references/architect-prompt.md', import.meta.url), 'utf8')
const brief = renderArchitectBrief({ template, facts, projectDir, goal: args.goal })
const dotDsh = join(projectDir, '.dsh')
await mkdir(dotDsh, { recursive: true })
const promptFile = join(dotDsh, 'codex-architect.md')
await writeFile(promptFile, brief, 'utf8')
const lastMessageFile = join(dotDsh, 'codex-last-message.md')
const sandbox = args.sandbox ?? DEFAULT_CODEX_SANDBOX
const mode = args.mode ?? DEFAULT_CODEX_MODE
const timeoutMs = args.timeoutMs ?? config.codexTimeoutMs ?? 1_800_000
const tailChars = config.outputTailChars ?? 20_000
const shared = {
projectDir,
promptFile,
sandbox,
model: args.model,
timeoutMs,
tailChars,
signal: exec.signal,
}
let result
if (mode === 'exec') {
result = await runCodex({ ...shared, lastMessageFile })
} else {
result = await runCodexAppServer(shared)
// Keep the same on-disk artifact as exec mode: the architect's answer.
if (typeof result.answer === 'string' && result.answer.length > 0) {
await writeFile(lastMessageFile, result.answer, 'utf8')
}
}
return {
mode,
command: result.command,
executable: result.executable,
projectDir,
promptFile,
lastMessageFile,
sandbox,
model: args.model ?? null,
exitCode: result.exitCode,
signal: result.signal,
timedOut: result.timedOut,
aborted: result.aborted,
timeoutMs: result.timeoutMs,
durationMs: result.durationMs,
sessionId: result.sessionId,
threadSource: result.threadSource ?? null,
tokensUsed: result.tokensUsed ?? null,
rolloutPath: result.rolloutPath,
filesChanged: result.filesChanged,
briefBytes: Buffer.byteLength(brief, 'utf8'),
target: {
platform: facts.platform,
minecraftVersion: facts.minecraftVersion,
javaVersion: facts.javaVersion,
apiCoord: facts.apiCoord,
},
output: result.output,
}
},
presentCall: codexCallView,
presentResult: codexResultView,
}))
}