返回 CodeWhale
README.md
根目录 / npm / runtime-sdk / README.md
1 # @codewhale/runtime-sdk
2
3 Small JavaScript helpers and TypeScript declarations for Codewhale's local
4 Runtime API. The package is intentionally transport-only: it never bypasses the
5 Rust runtime, sandbox, approvals, provider configuration, or Runtime execution
6 ledger.
7
8 ```js
9 import { createRuntimeClient } from "@codewhale/runtime-sdk";
10
11 const client = createRuntimeClient({
12 baseUrl: "http://127.0.0.1:7878",
13 token: process.env.CODEWHALE_RUNTIME_TOKEN,
14 });
15
16 const created = await client.createFleetRun({
17 target: "this_computer",
18 roles: [{ name: "reviewer" }, { name: "verifier" }],
19 workflow: {
20 id: "release-check",
21 kind: "parallel",
22 tasks: [
23 { id: "review", name: "Review", instructions: "Review locally.", worker: { role: "reviewer" } },
24 { id: "verify", name: "Verify", instructions: "Verify locally.", worker: { role: "verifier" } },
25 ],
26 },
27 });
28
29 // Creation is durable but does not launch work. Launch remains explicit.
30 await client.startFleetRun(created.run.id);
31
32 let cursor;
33 for await (const event of client.fleetEvents(created.run.id, { after: cursor })) {
34 if (event.cursor) cursor = event.cursor; // persist durable cursors only
35 if (event.event === "fleet.replay.cursor_unavailable") {
36 // Reload getFleetRun(created.run.id), then reconnect without the old cursor.
37 }
38 }
39 ```
40
41 ## Fleet Helpers
42
43 - `listFleetRuns()`
44 - `getFleetRun(runId)`
45 - `listFleetWorkers(runId)`
46 - `getFleetWorker(workerId)`
47 - `interruptWorker(workerId)`
48 - `stopWorker(workerId)`
49 - `restartWorker(workerId)`
50 - `stopFleetRun(runId)`
51 - `startFleetRun(runId)`
52 - `replayFleetEvents(runId, { after, limit })`
53 - `fleetEvents(runId, { after, limit })`
54 - `createFleetRun(spec)`
55
56 The v0.9.11 Runtime implements the complete local managed-Fleet path. Fleet
57 names the roster and selected member; the Runtime owns launch authority,
58 durable run/worker/event state, replay, and execution. A creation request must
59 name its roles, define a `parallel` Workflow, and select the explicit
60 `this_computer` target. `another_computer` and `cloud` are contract values but
61 fail closed until those targets are implemented. Event cursors are opaque and
62 durable across Runtime restarts; if Runtime ledger compaction removes an old
63 cursor, replay returns a conflict so the client can reload the run projection.
64 Local worker IDs are generated per run; managed creation does not yet accept
65 caller-assigned `worker_specs` because worker controls address IDs globally.
66
67 Older runtimes that do not expose one of these endpoints produce a
68 `RuntimeCapabilityError` with a stable capability string instead of a generic
69 fetch failure.
70
71 ## Read a thread journal
72
73 `threadEvents(threadId, { sinceSeq, replayLimit, signal })` reads the existing
74 `GET /v1/threads/{id}/events` SSE endpoint. It never creates a thread or starts
75 a turn. Pass an `AbortSignal` to close the subscription. Redirects are refused,
76 and incomplete or oversized frames fail instead of producing partial records.
77
78 The returned `seq` and `previous_seq` belong to Runtime. Keep the last accepted
79 `seq` for reconnects; sequence numbers need not be consecutive. Consumers should
80 validate the selected thread and predecessor cursor before advancing their own
81 read position. Authentication uses the client constructor's existing `token`
82 option and stays in the Authorization header.
83
84
85 Consumers that present **current** activity can pass `includeProgress: true`.
86 The same endpoint adds `progress=true` and advertises support with
87 `x-codewhale-event-progress: 1`. A Runtime without that capability fails
88 explicitly; a historical event is not a readiness signal.
89
90 Opt-in streams include `{ event: "stream.progress", thread_id, seq, state }`,
91 where `state` is `replaying` or `live`. These are transport frames at the existing
92 journal cursor, not journal events or new sequence numbers. Initial replay and
93 broadcast-lag recovery are `replaying`. The stream becomes `live` only after
94 both durable history and the already queued live tail have been drained. A
95 request answered during replay therefore settles before readiness is reported.
96 Later lag can return the same connection to `replaying`. Consumers should stop
97 extending activity while replaying or disconnected and validate the thread and
98 cursor. Default streams retain the original event-only contract.
99
99 lines MARKDOWN