Boots this OffiCraft member, then runs `ocagent listen --deliver-mod` and submits each event into its main conversation as a prompt.

Craft your own AI office. OffiCraft 是一間跑在你自己 Mac 上的 AI 工作室:你僱幾位常駐的 AI 成員,把事情整件交給他們,在一個網頁控制台裡看他們做到哪、在他們需要你點頭時回一句。跑的就是你機器上那個 Claude Code 或 Codex——你在它上面串好的設定照舊生效(Claude Code 的 skill / plugin / MCP 原封不動全都在)。
curl -fsSL https://github.com/pkyosx/OffiCraft/releases/latest/download/install.sh | bash
裝完會印出一行一次性設定連結(http://127.0.0.1:7755/?code=…),打開它設個 owner 密碼就進控制台了。完整前置需求、升級與移除見 安裝、升級與移除。
需要
tmux,以及已登入的claude(Claude Code CLI)或codex(Codex CLI)至少一種——每位成員底下就是一個跑在 tmux 裡的 Claude Code 或 Codex session。
server 只綁 127.0.0.1,預設不對外。要從手機或外面連,開一條你自己的 tunnel(如 cloudflared,會給你一個公開 HTTPS 網址)或走 VPN,設一組夠強的密碼即可。完整步驟(含加到手機主畫面)見 在手機上用控制台。
完整的使用者文件在 docs/guide/,控制台裡的「使用說明」分頁讀的也是同一份:
hooks/register.ts 211 lines1import type { EngineInterface, Register } from 'claude-code'
2
3// Written beside this mod by the warden on every spawn (cli/ocwarden/notifymod.go
4// notifyModConfigFile); every path and name below comes from it.
5const CONFIG_FILE = 'officraft.json'
6
7// A plugin submit resolves only when its turn STARTS, minutes later while the
8// member is busy, far past the listener's 30 s ack wait; a refusal comes at once.
9const ACCEPT_GRACE_MS = 1500
10// ⚠️ While the member is busy each submit spends the whole grace, one after the
11// other: a batch of more than ~20 payloads (30 s ack wait / 1.5 s) outlasts the
12// listener's ack wait and brings the reprint loop back.
13
14type Config = {
15 boot_prompt: string
16 started_marker: string
17 loaded_marker: string
18 disabled_marker: string
19 booted_marker: string
20 ack_file: string
21 ready_prefixes: string[]
22 listener: { argv: string[]; cwd: string; env: Record<string, string> }
23}
24
25type Frame = { submit: string } | { batch: string }
26
27export const register: Register = on => {
28 on('session.start', async ($, e, next) => {
29 // Without a config the mod stays silent: the warden then finds no load marker
30 // and fails the start.
31 const config = await readConfig($)
32 // Diagnostics only, first thing and before every check: when the mod does not
33 // load, the warden logs its presence and mtime, which tell a late
34 // session.start from a mod that never ran.
35 if (config !== undefined) await markStarted($, config)
36 const started = await next(e)
37 if (config === undefined) return started
38 // 🔴 The warden gave up on this session and is tearing it down: a boot or a
39 // listener now would mark messages read in a member about to be killed.
40 if (await $.fs.exists(config.disabled_marker)) {
41 $.ui.log('session.start found the warden already gave up on this session; not booting', { to: 'debug' })
42 return started
43 }
44 void boot($, config)
45 return started
46 })
47}
48
49// Detached from session.start, which the engine awaits before the first prompt:
50// the boot prompt is waited on here instead.
51async function boot($: EngineInterface, config: Config): Promise<void> {
52 // 🔴 The boot prompt goes in BEFORE the listener exists: a backlog the listener
53 // prints on connect queues behind it instead of becoming the member's first
54 // turn (and being marked read before the member ever booted).
55 if (!(await accepted($, config.boot_prompt, true))) {
56 $.ui.log('the boot prompt was refused; not listening', { to: 'debug' })
57 return
58 }
59 // Tells the warden this attempt booted the member, so it is not restarted.
60 await $.fs.write(config.booted_marker, 'booted\n')
61 await listen($, config)
62}
63
64// A failed write only loses the diagnosis; the session goes on.
65async function markStarted($: EngineInterface, config: Config): Promise<void> {
66 try {
67 await $.fs.write(config.started_marker, `${new Date(await $.clock.now()).toISOString()}\n`)
68 } catch (err) {
69 $.ui.log(`cannot write ${config.started_marker} (${String(err)})`, { to: 'debug' })
70 }
71}
72
73async function readConfig($: EngineInterface): Promise<Config | undefined> {
74 const path = `${$.plugin.root}/${CONFIG_FILE}`
75 try {
76 const value: unknown = JSON.parse(await $.fs.read(path))
77 if (isConfig(value)) return value
78 $.ui.log(`${path} is not a notification config; not listening`, { to: 'debug' })
79 } catch (err) {
80 $.ui.log(`cannot read ${path} (${String(err)}); not listening`, { to: 'debug' })
81 }
82 return undefined
83}
84
85function isConfig(value: unknown): value is Config {
86 if (typeof value !== 'object' || value === null) return false
87 const c = value as Record<string, unknown>
88 const listener = c.listener as Record<string, unknown> | undefined
89 return (
90 typeof c.boot_prompt === 'string' &&
91 typeof c.started_marker === 'string' &&
92 typeof c.loaded_marker === 'string' &&
93 typeof c.disabled_marker === 'string' &&
94 typeof c.booted_marker === 'string' &&
95 typeof c.ack_file === 'string' &&
96 Array.isArray(c.ready_prefixes) &&
97 c.ready_prefixes.every(p => typeof p === 'string') &&
98 typeof listener === 'object' &&
99 listener !== null &&
100 Array.isArray(listener.argv) &&
101 listener.argv.every(a => typeof a === 'string') &&
102 typeof listener.cwd === 'string' &&
103 typeof listener.env === 'object' &&
104 listener.env !== null &&
105 Object.values(listener.env).every(v => typeof v === 'string')
106 )
107}
108
109// The child lives as long as this loop: leaving it, or the module unloading,
110// kills it. A listener that exits is not restarted: the connection it held
111// disappearing is what makes the station recycle this member.
112//
113// The load marker waits for the listener's first frame or transport line: one
114// that refused to start prints neither, and the missing marker is what fails
115// the warden's start.
116async function listen($: EngineInterface, config: Config): Promise<void> {
117 const child = $.process.spawn({
118 argv: config.listener.argv,
119 cwd: config.listener.cwd,
120 env: config.listener.env,
121 })
122 let pending = ''
123 let pendingErr = ''
124 let ready = false
125 let batchDelivered = true
126 // false: the warden gave up meanwhile, so this listener must go.
127 const markReady = async (): Promise<boolean> => {
128 if (ready) return true
129 if (await $.fs.exists(config.disabled_marker)) {
130 $.ui.log('the warden gave up on this session; stopping this listener', { to: 'debug' })
131 return false
132 }
133 await $.fs.write(config.loaded_marker, 'loaded\n')
134 ready = true
135 return true
136 }
137 try {
138 for await (const { stream, text } of child) {
139 if (stream === 'stderr') {
140 $.ui.log(text, { to: 'debug' })
141 pendingErr += text
142 let isTransport = false
143 for (let nl = pendingErr.indexOf('\n'); nl >= 0; nl = pendingErr.indexOf('\n')) {
144 const line = pendingErr.slice(0, nl)
145 pendingErr = pendingErr.slice(nl + 1)
146 if (config.ready_prefixes.some(prefix => line.startsWith(prefix))) isTransport = true
147 }
148 if (isTransport && !(await markReady())) return
149 continue
150 }
151 pending += text
152 for (let nl = pending.indexOf('\n'); nl >= 0; nl = pending.indexOf('\n')) {
153 const line = pending.slice(0, nl)
154 pending = pending.slice(nl + 1)
155 const frame = parseFrame(line)
156 if (frame !== undefined && !(await markReady())) return
157 if (frame === undefined) {
158 if (line.trim() !== '') $.ui.log(line, { to: 'debug' })
159 } else if ('submit' in frame) {
160 // asUser, or Claude Code wraps every payload in its own plugin notice.
161 // ⚠️ The member then reads chat and webhook text as the user's own words.
162 batchDelivered = (await accepted($, frame.submit, true)) && batchDelivered
163 } else {
164 // Answered only after every submit before the marker settled: the
165 // listener marks the batch read on the station on `ack`.
166 await $.fs.write(config.ack_file, `${batchDelivered ? 'ack' : 'nack'} ${frame.batch}\n`)
167 batchDelivered = true
168 }
169 }
170 }
171 const { code, signal } = await child.result
172 $.ui.log(`ocagent listen exited (code ${code}, signal ${signal})`, { to: 'debug' })
173 } catch (err) {
174 $.ui.log(`ocagent listen stopped: ${String(err)}`, { to: 'debug' })
175 }
176}
177
178function parseFrame(line: string): Frame | undefined {
179 try {
180 const value: unknown = JSON.parse(line)
181 if (typeof value !== 'object' || value === null) return undefined
182 const fields = value as Record<string, unknown>
183 if (typeof fields.submit === 'string') return { submit: fields.submit }
184 if (typeof fields.batch === 'string') return { batch: fields.batch }
185 } catch {
186 return undefined
187 }
188 return undefined
189}
190
191type SubmitOutcome = 'entered' | 'pending' | string
192
193// True once the engine has the prompt queued: refused or thrown within the grace
194// is a failure, anything still pending then counts as accepted. Each call returns
195// before the next submit is issued, so prompts are queued in order.
196async function accepted($: EngineInterface, text: string, asUser = false): Promise<boolean> {
197 // The async wrapper turns a synchronous throw into a rejection, a failed submit.
198 const outcome: Promise<SubmitOutcome> = (async () => $.prompt.submit(asUser ? { text, asUser: true } : { text }))().then(
199 result => (result.drop === undefined ? 'entered' : `dropped (${result.drop})`),
200 err => `threw (${String(err)})`,
201 )
202 const early = await Promise.race([outcome, $.clock.sleep(ACCEPT_GRACE_MS).then((): SubmitOutcome => 'pending')])
203 if (early !== 'pending') return early === 'entered'
204 // ⚠️ Acked as queued, so the listener has already marked it read: a refusal
205 // arriving now loses the message, and this log line is the only trace.
206 void outcome.then(late => {
207 if (late !== 'entered') $.ui.log(`a queued prompt was ${late} after it was acked: ${text.slice(0, 120)}`, { to: 'debug' })
208 })
209 return true
210}
211