@max10240/opencode-plugin-infinite-loop 0.0.0-stage → 1.0.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -2
- package/README.zh-CN.md +76 -0
- package/dist/index.js +3 -0
- package/dist/src/controller.js +379 -0
- package/dist/src/duration.js +100 -0
- package/dist/src/index.js +277 -0
- package/dist/src/options.js +98 -0
- package/dist/src/rpc.js +118 -0
- package/dist/src/tracker.js +360 -0
- package/dist/src/tui.js +1320 -0
- package/dist/tui.js +9 -0
- package/package.json +67 -4
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { Plugin } from "@opencode/plugin";
|
|
2
|
+
import { InfiniteLoopController, isSessionBusy } from "./controller.js";
|
|
3
|
+
import { MAX_COOLDOWN_MS, infiniteLoopEnabled, parseInfiniteLoopOptions } from "./options.js";
|
|
4
|
+
import { InfiniteLoopRpc, toListItem, toStatusPayload } from "./rpc.js";
|
|
5
|
+
import { SessionTracker } from "./tracker.js";
|
|
6
|
+
// OpenCode can load the same plugin module once per location. Keep the runtime on
|
|
7
|
+
// globalThis so every location instance observes the same tasks and session tree.
|
|
8
|
+
const RUNTIME_KEY = Symbol.for("max10240.infinite-loop.runtime");
|
|
9
|
+
const registry = globalThis;
|
|
10
|
+
let shared = registry[RUNTIME_KEY];
|
|
11
|
+
function describe(error) {
|
|
12
|
+
return error instanceof Error ? error.message : String(error);
|
|
13
|
+
}
|
|
14
|
+
function withTimeout(promise, timeoutMs) {
|
|
15
|
+
let timer;
|
|
16
|
+
const timeout = new Promise((resolve) => {
|
|
17
|
+
timer = setTimeout(() => resolve(undefined), timeoutMs);
|
|
18
|
+
});
|
|
19
|
+
return Promise.race([promise, timeout]).finally(() => {
|
|
20
|
+
if (timer !== undefined)
|
|
21
|
+
clearTimeout(timer);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function createRuntime(context, options) {
|
|
25
|
+
const tracker = new SessionTracker();
|
|
26
|
+
const runtime = {};
|
|
27
|
+
const controller = new InfiniteLoopController({
|
|
28
|
+
delayMs: options.delayMs,
|
|
29
|
+
recheckMs: options.recheckMs,
|
|
30
|
+
cooldownMs: options.cooldownMs,
|
|
31
|
+
now: () => Date.now(),
|
|
32
|
+
timers: { setTimeout, clearTimeout },
|
|
33
|
+
isBusy: async (sessionID) => {
|
|
34
|
+
const known = tracker.statusOf(sessionID);
|
|
35
|
+
if (known !== undefined)
|
|
36
|
+
return known === "running";
|
|
37
|
+
try {
|
|
38
|
+
// The tracker normally answers from memory. The fallback is bounded so a
|
|
39
|
+
// stalled session read cannot leave a task without a timer.
|
|
40
|
+
const info = await withTimeout(runtime.context.session.get({ sessionID }), 5_000);
|
|
41
|
+
if (info === undefined)
|
|
42
|
+
return true;
|
|
43
|
+
tracker.remember(info);
|
|
44
|
+
return isSessionBusy(info);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
hasRunningWork: async (sessionID) => tracker.hasRunningWork(sessionID),
|
|
51
|
+
send: async (sessionID) => {
|
|
52
|
+
await runtime.context.session.prompt({
|
|
53
|
+
sessionID,
|
|
54
|
+
text: runtime.options.message,
|
|
55
|
+
metadata: { infiniteLoop: true },
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
onError: (error, sessionID) => {
|
|
59
|
+
console.warn(`Infinite loop nudge failed for ${sessionID}: ${describe(error)}`);
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
Object.assign(runtime, {
|
|
63
|
+
controller,
|
|
64
|
+
tracker,
|
|
65
|
+
refs: 0,
|
|
66
|
+
context,
|
|
67
|
+
contexts: [],
|
|
68
|
+
options,
|
|
69
|
+
});
|
|
70
|
+
shared = runtime;
|
|
71
|
+
registry[RUNTIME_KEY] = runtime;
|
|
72
|
+
return runtime;
|
|
73
|
+
}
|
|
74
|
+
export async function setupInfiniteLoop(context) {
|
|
75
|
+
let options;
|
|
76
|
+
try {
|
|
77
|
+
options = parseInfiniteLoopOptions(context.options);
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
console.warn(`Infinite loop disabled: ${describe(error)}`);
|
|
81
|
+
return async () => { };
|
|
82
|
+
}
|
|
83
|
+
if (!options.enabled || !infiniteLoopEnabled(context.options) || !context.rpc)
|
|
84
|
+
return async () => { };
|
|
85
|
+
if (typeof context.session?.hook !== "function") {
|
|
86
|
+
console.warn("Infinite loop disabled: the running OpenCode does not expose the experimental.idle session hook");
|
|
87
|
+
return async () => { };
|
|
88
|
+
}
|
|
89
|
+
const runtime = shared ?? registry[RUNTIME_KEY] ?? createRuntime(context, options);
|
|
90
|
+
shared = runtime;
|
|
91
|
+
registry[RUNTIME_KEY] = runtime;
|
|
92
|
+
runtime.context = context;
|
|
93
|
+
if (!runtime.contexts.includes(context))
|
|
94
|
+
runtime.contexts.push(context);
|
|
95
|
+
runtime.options = options;
|
|
96
|
+
runtime.refs += 1;
|
|
97
|
+
const tracker = runtime.tracker;
|
|
98
|
+
const controller = runtime.controller;
|
|
99
|
+
const idleHook = await context.session.hook("experimental.idle", (event) => {
|
|
100
|
+
try {
|
|
101
|
+
tracker.onIdle(event.sessionID, event.background);
|
|
102
|
+
controller.onIdle(event.sessionID, event.background);
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
console.warn(`Infinite loop idle handling failed for ${event.sessionID}: ${describe(error)}`);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
const promptHook = await context.session.hook("prompt", (event) => {
|
|
109
|
+
const own = isRecord(event.metadata) && event.metadata.infiniteLoop === true;
|
|
110
|
+
try {
|
|
111
|
+
tracker.onPrompt(event.sessionID, own);
|
|
112
|
+
controller.onPrompt(event.sessionID, own);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
console.warn(`Infinite loop prompt handling failed for ${event.sessionID}: ${describe(error)}`);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
const aborts = new AbortController();
|
|
119
|
+
if (typeof context.event?.subscribe === "function") {
|
|
120
|
+
void watchEvents(context, aborts.signal, controller, tracker);
|
|
121
|
+
}
|
|
122
|
+
// The seed runs once at server startup. Later attaches reuse the live directory.
|
|
123
|
+
void tracker.seed({ session: context.session });
|
|
124
|
+
let registration;
|
|
125
|
+
try {
|
|
126
|
+
registration = await context.rpc.register(InfiniteLoopRpc, {
|
|
127
|
+
status: async (raw) => {
|
|
128
|
+
const input = raw;
|
|
129
|
+
return toStatusPayload(controller.status(input.sessionID));
|
|
130
|
+
},
|
|
131
|
+
list: async () => {
|
|
132
|
+
const items = controller.activeStatuses().map(({ sessionID, status }) => toListItem(sessionID, status, tracker.info(sessionID)));
|
|
133
|
+
items.sort((left, right) => {
|
|
134
|
+
const directory = (left.directory ?? "").localeCompare(right.directory ?? "");
|
|
135
|
+
if (directory !== 0)
|
|
136
|
+
return directory;
|
|
137
|
+
return (left.title ?? left.sessionID).localeCompare(right.title ?? right.sessionID);
|
|
138
|
+
});
|
|
139
|
+
return items;
|
|
140
|
+
},
|
|
141
|
+
start: async (raw, request) => {
|
|
142
|
+
const input = raw;
|
|
143
|
+
if (typeof input.durationMs !== "number" || !Number.isFinite(input.durationMs) || input.durationMs <= 0) {
|
|
144
|
+
return request.error("invalid", "durationMs must be a positive number of milliseconds", {});
|
|
145
|
+
}
|
|
146
|
+
if (input.cooldownMs !== undefined &&
|
|
147
|
+
(typeof input.cooldownMs !== "number" || !Number.isFinite(input.cooldownMs) || input.cooldownMs < 0)) {
|
|
148
|
+
return request.error("invalid", "cooldownMs must be a non-negative number of milliseconds", {});
|
|
149
|
+
}
|
|
150
|
+
const cooldownMs = Math.min(Math.max(Math.round(input.cooldownMs ?? options.cooldownMs), 0), MAX_COOLDOWN_MS);
|
|
151
|
+
const durationMs = Math.min(Math.max(Math.round(input.durationMs), options.minDurationMs), options.maxDurationMs);
|
|
152
|
+
const known = tracker.statusOf(input.sessionID);
|
|
153
|
+
let alreadyIdle;
|
|
154
|
+
if (known !== undefined) {
|
|
155
|
+
alreadyIdle = known === "idle";
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
try {
|
|
159
|
+
const info = await withTimeout(context.session.get({ sessionID: input.sessionID }), 5_000);
|
|
160
|
+
if (info)
|
|
161
|
+
tracker.remember(info);
|
|
162
|
+
alreadyIdle = info !== undefined && !isSessionBusy(info);
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
alreadyIdle = false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return toStatusPayload(controller.start(input.sessionID, durationMs, alreadyIdle, cooldownMs));
|
|
169
|
+
},
|
|
170
|
+
stop: async (raw) => {
|
|
171
|
+
const input = raw;
|
|
172
|
+
return toStatusPayload(controller.stop(input.sessionID));
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
console.warn(`Infinite loop RPC unavailable: ${describe(error)}`);
|
|
178
|
+
}
|
|
179
|
+
return async () => {
|
|
180
|
+
aborts.abort();
|
|
181
|
+
await idleHook.dispose();
|
|
182
|
+
await promptHook.dispose();
|
|
183
|
+
if (registration)
|
|
184
|
+
await registration.dispose();
|
|
185
|
+
runtime.contexts = runtime.contexts.filter((item) => item !== context);
|
|
186
|
+
if (runtime.context === context)
|
|
187
|
+
runtime.context = runtime.contexts[0] ?? context;
|
|
188
|
+
runtime.refs -= 1;
|
|
189
|
+
if (runtime.refs <= 0) {
|
|
190
|
+
controller.dispose();
|
|
191
|
+
if (shared === runtime)
|
|
192
|
+
shared = undefined;
|
|
193
|
+
if (registry[RUNTIME_KEY] === runtime)
|
|
194
|
+
delete registry[RUNTIME_KEY];
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
async function watchEvents(context, signal, controller, tracker) {
|
|
199
|
+
while (!signal.aborted) {
|
|
200
|
+
try {
|
|
201
|
+
for await (const event of context.event.subscribe({ signal })) {
|
|
202
|
+
try {
|
|
203
|
+
handleEvent(event, controller, tracker);
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
console.warn(`Infinite loop event handling failed: ${describe(error)}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
// The stream ends with the server or a transport error; reconnect until the plugin unloads.
|
|
212
|
+
}
|
|
213
|
+
if (signal.aborted)
|
|
214
|
+
return;
|
|
215
|
+
await new Promise((resolve) => setTimeout(resolve, 1_000));
|
|
216
|
+
// Reconcile deletions and status changes missed during the stream gap.
|
|
217
|
+
void tracker.seed({ session: context.session }, 2_000, true);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
function handleEvent(event, controller, tracker) {
|
|
221
|
+
const type = event.type;
|
|
222
|
+
if (!type)
|
|
223
|
+
return;
|
|
224
|
+
tracker.handle(event);
|
|
225
|
+
const data = isRecord(event.data) ? event.data : {};
|
|
226
|
+
const sessionID = typeof data.sessionID === "string" ? data.sessionID : undefined;
|
|
227
|
+
if (!sessionID)
|
|
228
|
+
return;
|
|
229
|
+
if (type === "session.execution.failed") {
|
|
230
|
+
controller.onAborted(sessionID, { type: "failed" });
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (type === "session.execution.interrupted") {
|
|
234
|
+
const reason = typeof data.reason === "string" ? data.reason : "shutdown";
|
|
235
|
+
if (reason === "user") {
|
|
236
|
+
// Pause the task if the interrupted session is the task or one of its descendants.
|
|
237
|
+
let current = sessionID;
|
|
238
|
+
const seen = new Set();
|
|
239
|
+
while (current !== undefined && !seen.has(current)) {
|
|
240
|
+
seen.add(current);
|
|
241
|
+
if (controller.hasTask(current))
|
|
242
|
+
controller.onAborted(current, { type: "interrupted", reason: "user" });
|
|
243
|
+
current = tracker.parentOf(current);
|
|
244
|
+
}
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
controller.onAborted(sessionID, { type: "interrupted", reason });
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (type === "session.inbox.enqueued") {
|
|
251
|
+
const own = ownInbox(event.data);
|
|
252
|
+
tracker.onInboxEnqueued(sessionID, own);
|
|
253
|
+
controller.onInboxEnqueued(sessionID, own);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (type === "session.inbox.cancelled") {
|
|
257
|
+
tracker.onInboxCancelled(sessionID);
|
|
258
|
+
controller.onInputCancelled(sessionID);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function ownInbox(data) {
|
|
262
|
+
if (!isRecord(data))
|
|
263
|
+
return false;
|
|
264
|
+
const item = isRecord(data.item) ? data.item : undefined;
|
|
265
|
+
const payload = isRecord(item?.payload) ? item.payload : undefined;
|
|
266
|
+
const metadata = isRecord(payload?.metadata) ? payload.metadata : undefined;
|
|
267
|
+
return metadata?.infiniteLoop === true;
|
|
268
|
+
}
|
|
269
|
+
export default Plugin.define({
|
|
270
|
+
id: "max10240.infinite-loop",
|
|
271
|
+
async setup(context) {
|
|
272
|
+
return setupInfiniteLoop(context);
|
|
273
|
+
},
|
|
274
|
+
});
|
|
275
|
+
function isRecord(value) {
|
|
276
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
277
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { DAY_MS, MINUTE_MS, parsePresets } from "./duration.js";
|
|
2
|
+
export const DEFAULT_MESSAGE = "Keep working without asking any questions. Do not stop until you have finished everything you need to do.";
|
|
3
|
+
export const DEFAULT_DELAY_MS = 3_000;
|
|
4
|
+
export const DEFAULT_RECHECK_MS = 2_000;
|
|
5
|
+
export const DEFAULT_COOLDOWN_MS = 5 * MINUTE_MS;
|
|
6
|
+
export const DEFAULT_MIN_DURATION_MS = 5 * MINUTE_MS;
|
|
7
|
+
export const DEFAULT_MAX_DURATION_MS = 7 * DAY_MS;
|
|
8
|
+
export const DEFAULT_PRESETS = ["15m", "1h", "2h", "3h", "8h", "1d"];
|
|
9
|
+
export const DEFAULT_KEYBIND = "ctrl+shift+i";
|
|
10
|
+
export const DEFAULT_OVERVIEW_KEYBIND = "ctrl+shift+o";
|
|
11
|
+
export const DEFAULT_DURATION_MS = 60 * MINUTE_MS;
|
|
12
|
+
export const MIN_DELAY_MS = 100;
|
|
13
|
+
export const MAX_DELAY_MS = 10 * MINUTE_MS;
|
|
14
|
+
export const MIN_RECHECK_MS = 100;
|
|
15
|
+
export const MAX_RECHECK_MS = 10 * MINUTE_MS;
|
|
16
|
+
export const MIN_COOLDOWN_MS = 0;
|
|
17
|
+
export const MAX_COOLDOWN_MS = 30 * DAY_MS;
|
|
18
|
+
export const MIN_DURATION_FLOOR_MS = 1_000;
|
|
19
|
+
export const MAX_DURATION_CEILING_MS = 30 * DAY_MS;
|
|
20
|
+
export function infiniteLoopEnabled(raw) {
|
|
21
|
+
if (raw.enabled === false)
|
|
22
|
+
return false;
|
|
23
|
+
const features = raw.features;
|
|
24
|
+
return !(isRecord(features) && features.infiniteLoop === false);
|
|
25
|
+
}
|
|
26
|
+
export function parseInfiniteLoopOptions(raw = {}) {
|
|
27
|
+
const message = readMessage(raw.message);
|
|
28
|
+
const delayMs = readInteger(raw.delayMs, "delayMs", MIN_DELAY_MS, MAX_DELAY_MS, DEFAULT_DELAY_MS);
|
|
29
|
+
const recheckMs = readInteger(raw.recheckMs, "recheckMs", MIN_RECHECK_MS, MAX_RECHECK_MS, DEFAULT_RECHECK_MS);
|
|
30
|
+
const cooldownMs = readInteger(raw.cooldownMs, "cooldownMs", MIN_COOLDOWN_MS, MAX_COOLDOWN_MS, DEFAULT_COOLDOWN_MS);
|
|
31
|
+
const minDurationMs = readInteger(raw.minDurationMs, "minDurationMs", MIN_DURATION_FLOOR_MS, MAX_DURATION_CEILING_MS, DEFAULT_MIN_DURATION_MS);
|
|
32
|
+
const maxDurationMs = readInteger(raw.maxDurationMs, "maxDurationMs", minDurationMs, MAX_DURATION_CEILING_MS, DEFAULT_MAX_DURATION_MS);
|
|
33
|
+
const presets = readPresets(raw.presets, { minMs: minDurationMs, maxMs: maxDurationMs });
|
|
34
|
+
const defaultDurationMs = readInteger(raw.defaultDurationMs, "defaultDurationMs", minDurationMs, maxDurationMs, DEFAULT_DURATION_MS);
|
|
35
|
+
const keybind = readKeybind(raw.keybind, DEFAULT_KEYBIND, "keybind");
|
|
36
|
+
const overviewKeybind = readKeybind(raw.overviewKeybind, DEFAULT_OVERVIEW_KEYBIND, "overviewKeybind");
|
|
37
|
+
return {
|
|
38
|
+
enabled: infiniteLoopEnabled(raw),
|
|
39
|
+
message,
|
|
40
|
+
delayMs,
|
|
41
|
+
recheckMs,
|
|
42
|
+
cooldownMs,
|
|
43
|
+
minDurationMs,
|
|
44
|
+
maxDurationMs,
|
|
45
|
+
defaultDurationMs,
|
|
46
|
+
presets,
|
|
47
|
+
keybind,
|
|
48
|
+
overviewKeybind,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function readMessage(raw) {
|
|
52
|
+
if (raw === undefined)
|
|
53
|
+
return DEFAULT_MESSAGE;
|
|
54
|
+
if (typeof raw !== "string" || !raw.trim())
|
|
55
|
+
throw new Error("message must be a non-empty string");
|
|
56
|
+
return raw;
|
|
57
|
+
}
|
|
58
|
+
function readInteger(raw, name, min, max, fallback) {
|
|
59
|
+
if (raw === undefined)
|
|
60
|
+
return fallback;
|
|
61
|
+
if (typeof raw !== "number" || !Number.isSafeInteger(raw) || raw < min || raw > max) {
|
|
62
|
+
throw new Error(`${name} must be an integer between ${min} and ${max}`);
|
|
63
|
+
}
|
|
64
|
+
return raw;
|
|
65
|
+
}
|
|
66
|
+
function readPresets(raw, bounds) {
|
|
67
|
+
if (raw === undefined)
|
|
68
|
+
return DEFAULT_PRESETS;
|
|
69
|
+
if (!Array.isArray(raw))
|
|
70
|
+
throw new Error("presets must be an array of duration strings");
|
|
71
|
+
const seen = new Set();
|
|
72
|
+
const presets = [];
|
|
73
|
+
const invalid = [];
|
|
74
|
+
for (const entry of raw) {
|
|
75
|
+
const value = typeof entry === "string" ? entry.trim() : "";
|
|
76
|
+
if (!value || !parsePresets([value], bounds).length || seen.has(value.toLowerCase())) {
|
|
77
|
+
invalid.push(JSON.stringify(entry));
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
seen.add(value.toLowerCase());
|
|
81
|
+
presets.push(value);
|
|
82
|
+
}
|
|
83
|
+
if (invalid.length)
|
|
84
|
+
throw new Error(`presets contains invalid or duplicate durations: ${invalid.join(", ")}`);
|
|
85
|
+
return presets;
|
|
86
|
+
}
|
|
87
|
+
function readKeybind(raw, fallback, name) {
|
|
88
|
+
if (raw === undefined)
|
|
89
|
+
return fallback;
|
|
90
|
+
if (raw === false)
|
|
91
|
+
return false;
|
|
92
|
+
if (typeof raw !== "string" || !raw.trim())
|
|
93
|
+
throw new Error(`${name} must be a non-empty string or false`);
|
|
94
|
+
return raw.trim();
|
|
95
|
+
}
|
|
96
|
+
function isRecord(value) {
|
|
97
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
98
|
+
}
|
package/dist/src/rpc.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { Rpc } from "@opencode/plugin/rpc";
|
|
2
|
+
const state = {
|
|
3
|
+
type: "object",
|
|
4
|
+
properties: {
|
|
5
|
+
active: { type: "boolean" },
|
|
6
|
+
startedAt: { type: "number" },
|
|
7
|
+
deadline: { type: "number" },
|
|
8
|
+
sentCount: { type: "number" },
|
|
9
|
+
lastSentAt: { type: "number" },
|
|
10
|
+
lastInputAt: { type: "number" },
|
|
11
|
+
cooldownMs: { type: "number" },
|
|
12
|
+
cooldownUntil: { type: "number" },
|
|
13
|
+
nextNudgeAt: { type: "number" },
|
|
14
|
+
phase: { type: "string", enum: ["waiting", "quiet", "checking", "paused"] },
|
|
15
|
+
nudging: { type: "boolean" },
|
|
16
|
+
outcome: {
|
|
17
|
+
type: "object",
|
|
18
|
+
properties: {
|
|
19
|
+
reason: { type: "string", enum: ["expired", "stopped"] },
|
|
20
|
+
at: { type: "number" },
|
|
21
|
+
sentCount: { type: "number" },
|
|
22
|
+
lastSentAt: { type: "number" },
|
|
23
|
+
},
|
|
24
|
+
required: ["reason", "at"],
|
|
25
|
+
additionalProperties: false,
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
required: ["active"],
|
|
29
|
+
additionalProperties: false,
|
|
30
|
+
};
|
|
31
|
+
const listItem = {
|
|
32
|
+
type: "object",
|
|
33
|
+
properties: {
|
|
34
|
+
sessionID: { type: "string" },
|
|
35
|
+
title: { type: "string" },
|
|
36
|
+
directory: { type: "string" },
|
|
37
|
+
...state.properties,
|
|
38
|
+
},
|
|
39
|
+
required: ["sessionID", "active"],
|
|
40
|
+
additionalProperties: false,
|
|
41
|
+
};
|
|
42
|
+
const sessionInput = {
|
|
43
|
+
type: "object",
|
|
44
|
+
properties: { sessionID: { type: "string" } },
|
|
45
|
+
required: ["sessionID"],
|
|
46
|
+
additionalProperties: false,
|
|
47
|
+
};
|
|
48
|
+
const emptyInput = { type: "object", properties: {}, required: [], additionalProperties: false };
|
|
49
|
+
const invalid = { type: "object", additionalProperties: false };
|
|
50
|
+
export const InfiniteLoopRpc = Rpc.define({
|
|
51
|
+
id: "max10240.infinite-loop",
|
|
52
|
+
events: {},
|
|
53
|
+
methods: {
|
|
54
|
+
status: {
|
|
55
|
+
input: sessionInput,
|
|
56
|
+
output: state,
|
|
57
|
+
errors: { invalid },
|
|
58
|
+
},
|
|
59
|
+
list: {
|
|
60
|
+
input: emptyInput,
|
|
61
|
+
output: { type: "array", items: listItem },
|
|
62
|
+
errors: { invalid },
|
|
63
|
+
},
|
|
64
|
+
start: {
|
|
65
|
+
input: {
|
|
66
|
+
type: "object",
|
|
67
|
+
properties: {
|
|
68
|
+
sessionID: { type: "string" },
|
|
69
|
+
durationMs: { type: "number" },
|
|
70
|
+
cooldownMs: { type: "number" },
|
|
71
|
+
},
|
|
72
|
+
required: ["sessionID", "durationMs"],
|
|
73
|
+
additionalProperties: false,
|
|
74
|
+
},
|
|
75
|
+
output: state,
|
|
76
|
+
errors: { invalid },
|
|
77
|
+
},
|
|
78
|
+
stop: {
|
|
79
|
+
input: sessionInput,
|
|
80
|
+
output: state,
|
|
81
|
+
errors: { invalid },
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
/** Narrows a controller status to the exact RPC payload shape (no extra keys). */
|
|
86
|
+
export function toStatusPayload(status) {
|
|
87
|
+
return {
|
|
88
|
+
active: status.active,
|
|
89
|
+
...(status.startedAt === undefined ? {} : { startedAt: status.startedAt }),
|
|
90
|
+
...(status.deadline === undefined ? {} : { deadline: status.deadline }),
|
|
91
|
+
...(status.sentCount === undefined ? {} : { sentCount: status.sentCount }),
|
|
92
|
+
...(status.lastSentAt === undefined ? {} : { lastSentAt: status.lastSentAt }),
|
|
93
|
+
...(status.lastInputAt === undefined ? {} : { lastInputAt: status.lastInputAt }),
|
|
94
|
+
...(status.cooldownMs === undefined ? {} : { cooldownMs: status.cooldownMs }),
|
|
95
|
+
...(status.cooldownUntil === undefined ? {} : { cooldownUntil: status.cooldownUntil }),
|
|
96
|
+
...(status.nextNudgeAt === undefined ? {} : { nextNudgeAt: status.nextNudgeAt }),
|
|
97
|
+
...(status.phase === undefined ? {} : { phase: status.phase }),
|
|
98
|
+
...(status.nudging === undefined ? {} : { nudging: status.nudging }),
|
|
99
|
+
...(status.outcome === undefined
|
|
100
|
+
? {}
|
|
101
|
+
: {
|
|
102
|
+
outcome: {
|
|
103
|
+
reason: status.outcome.reason,
|
|
104
|
+
at: status.outcome.at,
|
|
105
|
+
...(status.outcome.sentCount === undefined ? {} : { sentCount: status.outcome.sentCount }),
|
|
106
|
+
...(status.outcome.lastSentAt === undefined ? {} : { lastSentAt: status.outcome.lastSentAt }),
|
|
107
|
+
},
|
|
108
|
+
}),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
export function toListItem(sessionID, status, info = {}) {
|
|
112
|
+
return {
|
|
113
|
+
sessionID,
|
|
114
|
+
...(info.title === undefined ? {} : { title: info.title }),
|
|
115
|
+
...(info.directory === undefined ? {} : { directory: info.directory }),
|
|
116
|
+
...toStatusPayload(status),
|
|
117
|
+
};
|
|
118
|
+
}
|