@max10240/opencode-plugin-delayed-queue 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 +66 -2
- package/README.zh-CN.md +64 -0
- package/dist/index.js +2 -0
- package/dist/src/controller.js +591 -0
- package/dist/src/index.js +286 -0
- package/dist/src/options.js +65 -0
- package/dist/src/rpc.js +204 -0
- package/dist/src/tui.js +487 -0
- package/dist/tui.js +11 -0
- package/package.json +67 -4
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { Plugin } from "@opencode/plugin";
|
|
2
|
+
import { DelayedQueueController } from "./controller.js";
|
|
3
|
+
import { delayedQueueEnabled, parseDelayedQueueOptions } from "./options.js";
|
|
4
|
+
import { DelayedQueueRpc, toJobPayload, toStatusPayload } from "./rpc.js";
|
|
5
|
+
const STORAGE_PREFIX = "state:";
|
|
6
|
+
const MAX_SCAN_PAGES = 100;
|
|
7
|
+
let processBoot;
|
|
8
|
+
const boot = () => (processBoot ??= crypto.randomUUID());
|
|
9
|
+
function describe(error) {
|
|
10
|
+
return error instanceof Error ? error.message : String(error);
|
|
11
|
+
}
|
|
12
|
+
export async function setupDelayedQueue(context) {
|
|
13
|
+
let options;
|
|
14
|
+
try {
|
|
15
|
+
options = parseDelayedQueueOptions(context.options);
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
console.warn(`Delayed queue disabled: ${describe(error)}`);
|
|
19
|
+
return async () => { };
|
|
20
|
+
}
|
|
21
|
+
if (!options.enabled || !delayedQueueEnabled(context.options) || !context.rpc)
|
|
22
|
+
return async () => { };
|
|
23
|
+
if (typeof context.session?.hook !== "function") {
|
|
24
|
+
console.warn("Delayed queue disabled: the running OpenCode does not expose session hooks");
|
|
25
|
+
return async () => { };
|
|
26
|
+
}
|
|
27
|
+
let emitChanged = () => { };
|
|
28
|
+
const controller = new DelayedQueueController({
|
|
29
|
+
boot: boot(),
|
|
30
|
+
recheckMs: options.recheckMs,
|
|
31
|
+
waitKinds: options.waitKinds,
|
|
32
|
+
waitDescendants: options.waitDescendants,
|
|
33
|
+
pauseOnInterrupt: options.pauseOnInterrupt,
|
|
34
|
+
pauseOnFailure: options.pauseOnFailure,
|
|
35
|
+
now: () => Date.now(),
|
|
36
|
+
isBusy: async (sessionID) => isSessionBusy(await context.session.get({ sessionID })),
|
|
37
|
+
parentOf: async (sessionID) => (await context.session.get({ sessionID })).parentID,
|
|
38
|
+
deliver: async (sessionID, item) => {
|
|
39
|
+
await context.session.prompt({
|
|
40
|
+
sessionID,
|
|
41
|
+
id: item.id,
|
|
42
|
+
text: item.text,
|
|
43
|
+
metadata: { delayedQueue: true, queueID: item.id },
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
save: (sessionID, state) => context.storage.set(storageKey(sessionID), storageValue(state)),
|
|
47
|
+
onChange: (status) => emitChanged(status.sessionID, status),
|
|
48
|
+
onError: (error, sessionID) => console.warn(`Delayed queue failed for ${sessionID}: ${describe(error)}`),
|
|
49
|
+
});
|
|
50
|
+
const loaded = await loadStored(context, controller).catch((error) => {
|
|
51
|
+
console.warn(`Delayed queue could not load persisted messages: ${describe(error)}`);
|
|
52
|
+
return 0;
|
|
53
|
+
});
|
|
54
|
+
const idleHook = await context.session.hook("experimental.idle", (event) => {
|
|
55
|
+
const background = event.background.map(toBackground);
|
|
56
|
+
void controller.onIdle(event.sessionID, background);
|
|
57
|
+
});
|
|
58
|
+
const promptHook = await context.session.hook("prompt", (event) => {
|
|
59
|
+
const own = isRecord(event.metadata) && event.metadata.delayedQueue === true;
|
|
60
|
+
void controller.onPrompt(event.sessionID, own);
|
|
61
|
+
});
|
|
62
|
+
const registration = await context.rpc.register(DelayedQueueRpc, {
|
|
63
|
+
status: async (input) => {
|
|
64
|
+
const value = input;
|
|
65
|
+
return toStatusPayload(controller.status(value.sessionID));
|
|
66
|
+
},
|
|
67
|
+
enqueue: async (input) => {
|
|
68
|
+
const value = input;
|
|
69
|
+
return toStatusPayload(await controller.enqueue(value.sessionID, value.text, value.jobs?.map(toJobPayload)));
|
|
70
|
+
},
|
|
71
|
+
syncJobs: async (input) => {
|
|
72
|
+
const value = input;
|
|
73
|
+
return toStatusPayload(await controller.syncJobs(value.sessionID, value.jobs.map(toJobPayload)));
|
|
74
|
+
},
|
|
75
|
+
remove: async (input) => {
|
|
76
|
+
const value = input;
|
|
77
|
+
return toStatusPayload(await controller.remove(value.sessionID, value.ids));
|
|
78
|
+
},
|
|
79
|
+
clear: async (input) => {
|
|
80
|
+
const value = input;
|
|
81
|
+
return toStatusPayload(await controller.clear(value.sessionID));
|
|
82
|
+
},
|
|
83
|
+
pause: async (input) => {
|
|
84
|
+
const value = input;
|
|
85
|
+
return toStatusPayload(await controller.pause(value.sessionID, "manual"));
|
|
86
|
+
},
|
|
87
|
+
resume: async (input) => {
|
|
88
|
+
const value = input;
|
|
89
|
+
return toStatusPayload(await controller.resume(value.sessionID));
|
|
90
|
+
},
|
|
91
|
+
deliver: async (input) => {
|
|
92
|
+
const value = input;
|
|
93
|
+
return toStatusPayload(await controller.deliverNow(value.sessionID, value.id));
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
emitChanged = (sessionID, status) => {
|
|
97
|
+
void registration.events.emit("changed", { sessionID, state: toStatusPayload(status) }).catch((error) => {
|
|
98
|
+
console.warn(`Delayed queue could not publish a change: ${describe(error)}`);
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
const abort = new AbortController();
|
|
102
|
+
void watchEvents(context, controller, abort.signal);
|
|
103
|
+
if (loaded > 0)
|
|
104
|
+
console.info(`Delayed queue restored ${loaded} session(s)`);
|
|
105
|
+
return async () => {
|
|
106
|
+
abort.abort();
|
|
107
|
+
controller.dispose();
|
|
108
|
+
await idleHook.dispose();
|
|
109
|
+
await promptHook.dispose();
|
|
110
|
+
await registration.dispose();
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
export default Plugin.define({
|
|
114
|
+
id: "max10240.delayed-queue",
|
|
115
|
+
async setup(context) {
|
|
116
|
+
return setupDelayedQueue(context);
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
function isSessionBusy(info) {
|
|
120
|
+
const busy = info?.time?.busy;
|
|
121
|
+
if (busy === undefined)
|
|
122
|
+
return false;
|
|
123
|
+
const idle = info?.time?.idle;
|
|
124
|
+
if (idle === undefined)
|
|
125
|
+
return true;
|
|
126
|
+
return busy > idle;
|
|
127
|
+
}
|
|
128
|
+
function storageKey(sessionID) {
|
|
129
|
+
return STORAGE_PREFIX + sessionID;
|
|
130
|
+
}
|
|
131
|
+
function storageValue(state) {
|
|
132
|
+
return {
|
|
133
|
+
version: state.version,
|
|
134
|
+
boot: state.boot,
|
|
135
|
+
paused: state.paused,
|
|
136
|
+
...(state.pauseReason === undefined ? {} : { pauseReason: state.pauseReason }),
|
|
137
|
+
items: state.items.map((item) => ({ id: item.id, text: item.text, createdAt: item.createdAt })),
|
|
138
|
+
updatedAt: state.updatedAt,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
async function loadStored(context, controller) {
|
|
142
|
+
let after;
|
|
143
|
+
const seen = new Set();
|
|
144
|
+
let loaded = 0;
|
|
145
|
+
for (let pageIndex = 0; pageIndex < MAX_SCAN_PAGES; pageIndex++) {
|
|
146
|
+
const page = await context.storage.scan({
|
|
147
|
+
prefix: STORAGE_PREFIX,
|
|
148
|
+
limit: 100,
|
|
149
|
+
...(after === undefined ? {} : { after }),
|
|
150
|
+
});
|
|
151
|
+
for (const entry of page.entries) {
|
|
152
|
+
const state = decodeState(entry.value);
|
|
153
|
+
if (!state)
|
|
154
|
+
continue;
|
|
155
|
+
const sessionID = entry.key.slice(STORAGE_PREFIX.length);
|
|
156
|
+
if (!sessionID)
|
|
157
|
+
continue;
|
|
158
|
+
try {
|
|
159
|
+
await context.session.get({ sessionID: sessionID });
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
console.warn(`Delayed queue skipped a missing session ${sessionID}: ${describe(error)}`);
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
controller.hydrate(sessionID, state);
|
|
166
|
+
loaded++;
|
|
167
|
+
}
|
|
168
|
+
const next = page.next === undefined || page.next === "" ? undefined : page.next;
|
|
169
|
+
if (!next || next === after || seen.has(next) || page.entries.length === 0)
|
|
170
|
+
break;
|
|
171
|
+
seen.add(next);
|
|
172
|
+
after = next;
|
|
173
|
+
}
|
|
174
|
+
return loaded;
|
|
175
|
+
}
|
|
176
|
+
function decodeState(value) {
|
|
177
|
+
if (!isRecord(value) || value.version !== 1)
|
|
178
|
+
return undefined;
|
|
179
|
+
if (typeof value.boot !== "string" || !Array.isArray(value.items))
|
|
180
|
+
return undefined;
|
|
181
|
+
if (typeof value.updatedAt !== "number")
|
|
182
|
+
return undefined;
|
|
183
|
+
const items = value.items.flatMap((entry) => {
|
|
184
|
+
if (!isRecord(entry))
|
|
185
|
+
return [];
|
|
186
|
+
if (typeof entry.id !== "string" || typeof entry.text !== "string" || typeof entry.createdAt !== "number")
|
|
187
|
+
return [];
|
|
188
|
+
if (!entry.id.startsWith("msg_") || !entry.text.trim())
|
|
189
|
+
return [];
|
|
190
|
+
return [{ id: entry.id, text: entry.text, createdAt: entry.createdAt }];
|
|
191
|
+
});
|
|
192
|
+
return {
|
|
193
|
+
version: 1,
|
|
194
|
+
boot: value.boot,
|
|
195
|
+
paused: value.paused === true,
|
|
196
|
+
...(isPauseReason(value.pauseReason) ? { pauseReason: value.pauseReason } : {}),
|
|
197
|
+
items,
|
|
198
|
+
updatedAt: value.updatedAt,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function isPauseReason(value) {
|
|
202
|
+
return value === "interrupted" || value === "failed" || value === "restart" || value === "manual" || value === "delivery";
|
|
203
|
+
}
|
|
204
|
+
async function watchEvents(context, controller, signal) {
|
|
205
|
+
try {
|
|
206
|
+
for await (const event of context.event.subscribe({ signal })) {
|
|
207
|
+
const data = (isRecord(event.data) ? event.data : {});
|
|
208
|
+
const type = event.type;
|
|
209
|
+
const sessionID = typeof data.sessionID === "string" ? data.sessionID : undefined;
|
|
210
|
+
if (type === "experimental.job.updated") {
|
|
211
|
+
const info = isRecord(data.info) ? data.info : undefined;
|
|
212
|
+
const job = info ? toJob(info) : undefined;
|
|
213
|
+
if (job)
|
|
214
|
+
controller.onJob(job);
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (!sessionID)
|
|
218
|
+
continue;
|
|
219
|
+
if (type === "session.execution.started") {
|
|
220
|
+
controller.onExecutionStarted(sessionID);
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
if (type === "session.execution.failed") {
|
|
224
|
+
void controller.onExecutionFailed(sessionID);
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
if (type === "session.execution.interrupted") {
|
|
228
|
+
void controller.onExecutionInterrupted(sessionID, typeof data.reason === "string" ? data.reason : "");
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
if (type === "session.deleted") {
|
|
232
|
+
void controller.onSessionDeleted(sessionID).then(() => context.storage.remove(storageKey(sessionID)));
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
catch (error) {
|
|
237
|
+
if (!signal.aborted)
|
|
238
|
+
console.warn(`Delayed queue event stream failed: ${describe(error)}`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function toBackground(work) {
|
|
242
|
+
if (work.kind === "shell") {
|
|
243
|
+
return {
|
|
244
|
+
kind: work.kind,
|
|
245
|
+
sessionID: work.sessionID,
|
|
246
|
+
shellID: work.shellID ?? "",
|
|
247
|
+
command: work.command ?? "",
|
|
248
|
+
label: work.command ?? "",
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
if (work.kind === "subagent") {
|
|
252
|
+
return {
|
|
253
|
+
kind: work.kind,
|
|
254
|
+
sessionID: work.sessionID,
|
|
255
|
+
childSessionID: work.childSessionID ?? "",
|
|
256
|
+
agent: work.agent ?? "",
|
|
257
|
+
description: work.description ?? "",
|
|
258
|
+
label: work.description ?? "",
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
if (work.kind === "image") {
|
|
262
|
+
return { kind: work.kind, sessionID: work.sessionID, path: work.path ?? "", model: work.model ?? "", label: work.path };
|
|
263
|
+
}
|
|
264
|
+
if (work.kind === "video")
|
|
265
|
+
return { kind: work.kind, sessionID: work.sessionID, path: work.path ?? "", label: work.path };
|
|
266
|
+
return { kind: work.kind, sessionID: work.sessionID, jobID: work.jobID ?? "", label: "Code Mode script" };
|
|
267
|
+
}
|
|
268
|
+
function toJob(info) {
|
|
269
|
+
if (typeof info.id !== "string" || typeof info.kind !== "string" || typeof info.sessionID !== "string")
|
|
270
|
+
return undefined;
|
|
271
|
+
return {
|
|
272
|
+
id: info.id,
|
|
273
|
+
kind: info.kind,
|
|
274
|
+
sessionID: info.sessionID,
|
|
275
|
+
status: typeof info.status === "string" ? info.status : "running",
|
|
276
|
+
...(typeof info.childSessionID === "string" ? { childSessionID: info.childSessionID } : {}),
|
|
277
|
+
...(typeof info.shellID === "string" ? { shellID: info.shellID } : {}),
|
|
278
|
+
...(typeof info.command === "string" ? { command: info.command } : {}),
|
|
279
|
+
...(typeof info.description === "string" ? { description: info.description } : {}),
|
|
280
|
+
...(typeof info.title === "string" ? { title: info.title } : {}),
|
|
281
|
+
...(typeof info.path === "string" ? { path: info.path } : {}),
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
function isRecord(value) {
|
|
285
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
286
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export const DEFAULT_KEYBIND = "<leader>d";
|
|
2
|
+
export const DEFAULT_QUEUE_KEYBIND = "f9";
|
|
3
|
+
export const DEFAULT_RECHECK_MS = 5_000;
|
|
4
|
+
export const MIN_RECHECK_MS = 1_000;
|
|
5
|
+
export const MAX_RECHECK_MS = 10 * 60_000;
|
|
6
|
+
export const WAIT_KINDS = ["shell", "subagent", "image", "video", "codemode"];
|
|
7
|
+
export function delayedQueueEnabled(raw = {}) {
|
|
8
|
+
if (raw.enabled === false)
|
|
9
|
+
return false;
|
|
10
|
+
const features = raw.features;
|
|
11
|
+
return !(isRecord(features) && features.delayedQueue === false);
|
|
12
|
+
}
|
|
13
|
+
export function parseDelayedQueueOptions(raw = {}) {
|
|
14
|
+
return {
|
|
15
|
+
enabled: delayedQueueEnabled(raw),
|
|
16
|
+
keybind: readKeybind(raw.keybind),
|
|
17
|
+
queueKeybind: readKeybind(raw.queueKeybind, DEFAULT_QUEUE_KEYBIND),
|
|
18
|
+
recheckMs: readInteger(raw.recheckMs, "recheckMs", MIN_RECHECK_MS, MAX_RECHECK_MS, DEFAULT_RECHECK_MS),
|
|
19
|
+
waitKinds: readKinds(raw.waitKinds),
|
|
20
|
+
waitDescendants: readBoolean(raw.waitDescendants, "waitDescendants", true),
|
|
21
|
+
pauseOnInterrupt: readBoolean(raw.pauseOnInterrupt, "pauseOnInterrupt", true),
|
|
22
|
+
pauseOnFailure: readBoolean(raw.pauseOnFailure, "pauseOnFailure", true),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function readKeybind(raw, fallback = DEFAULT_KEYBIND) {
|
|
26
|
+
if (raw === undefined)
|
|
27
|
+
return fallback;
|
|
28
|
+
if (raw === false)
|
|
29
|
+
return false;
|
|
30
|
+
if (typeof raw !== "string" || !raw.trim())
|
|
31
|
+
throw new Error("keybind must be a non-empty string or false");
|
|
32
|
+
return raw.trim();
|
|
33
|
+
}
|
|
34
|
+
function readInteger(raw, name, min, max, fallback) {
|
|
35
|
+
if (raw === undefined)
|
|
36
|
+
return fallback;
|
|
37
|
+
if (typeof raw !== "number" || !Number.isFinite(raw) || raw < min || raw > max) {
|
|
38
|
+
throw new Error(`${name} must be a number between ${min} and ${max}`);
|
|
39
|
+
}
|
|
40
|
+
return Math.floor(raw);
|
|
41
|
+
}
|
|
42
|
+
function readBoolean(raw, name, fallback) {
|
|
43
|
+
if (raw === undefined)
|
|
44
|
+
return fallback;
|
|
45
|
+
if (typeof raw !== "boolean")
|
|
46
|
+
throw new Error(`${name} must be a boolean`);
|
|
47
|
+
return raw;
|
|
48
|
+
}
|
|
49
|
+
function readKinds(raw) {
|
|
50
|
+
if (raw === undefined)
|
|
51
|
+
return WAIT_KINDS;
|
|
52
|
+
if (!Array.isArray(raw))
|
|
53
|
+
throw new Error("waitKinds must be an array");
|
|
54
|
+
const seen = new Set();
|
|
55
|
+
for (const entry of raw) {
|
|
56
|
+
if (typeof entry !== "string" || !WAIT_KINDS.includes(entry)) {
|
|
57
|
+
throw new Error(`waitKinds contains an unknown kind: ${JSON.stringify(entry)}`);
|
|
58
|
+
}
|
|
59
|
+
seen.add(entry);
|
|
60
|
+
}
|
|
61
|
+
return [...seen];
|
|
62
|
+
}
|
|
63
|
+
function isRecord(value) {
|
|
64
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
65
|
+
}
|
package/dist/src/rpc.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { Rpc } from "@opencode/plugin/rpc";
|
|
2
|
+
const sessionInput = {
|
|
3
|
+
type: "object",
|
|
4
|
+
properties: { sessionID: { type: "string" } },
|
|
5
|
+
required: ["sessionID"],
|
|
6
|
+
additionalProperties: false,
|
|
7
|
+
};
|
|
8
|
+
const item = {
|
|
9
|
+
type: "object",
|
|
10
|
+
properties: {
|
|
11
|
+
id: { type: "string" },
|
|
12
|
+
text: { type: "string" },
|
|
13
|
+
createdAt: { type: "number" },
|
|
14
|
+
},
|
|
15
|
+
required: ["id", "text", "createdAt"],
|
|
16
|
+
additionalProperties: false,
|
|
17
|
+
};
|
|
18
|
+
const background = {
|
|
19
|
+
type: "object",
|
|
20
|
+
properties: {
|
|
21
|
+
kind: { type: "string" },
|
|
22
|
+
sessionID: { type: "string" },
|
|
23
|
+
label: { type: "string" },
|
|
24
|
+
jobID: { type: "string" },
|
|
25
|
+
shellID: { type: "string" },
|
|
26
|
+
command: { type: "string" },
|
|
27
|
+
childSessionID: { type: "string" },
|
|
28
|
+
agent: { type: "string" },
|
|
29
|
+
description: { type: "string" },
|
|
30
|
+
path: { type: "string" },
|
|
31
|
+
model: { type: "string" },
|
|
32
|
+
},
|
|
33
|
+
required: ["kind", "sessionID"],
|
|
34
|
+
additionalProperties: false,
|
|
35
|
+
};
|
|
36
|
+
const job = {
|
|
37
|
+
type: "object",
|
|
38
|
+
properties: {
|
|
39
|
+
id: { type: "string" },
|
|
40
|
+
kind: { type: "string" },
|
|
41
|
+
sessionID: { type: "string" },
|
|
42
|
+
status: { type: "string" },
|
|
43
|
+
childSessionID: { type: "string" },
|
|
44
|
+
shellID: { type: "string" },
|
|
45
|
+
command: { type: "string" },
|
|
46
|
+
description: { type: "string" },
|
|
47
|
+
title: { type: "string" },
|
|
48
|
+
path: { type: "string" },
|
|
49
|
+
},
|
|
50
|
+
required: ["id", "kind", "sessionID", "status"],
|
|
51
|
+
additionalProperties: false,
|
|
52
|
+
};
|
|
53
|
+
const status = {
|
|
54
|
+
type: "object",
|
|
55
|
+
properties: {
|
|
56
|
+
sessionID: { type: "string" },
|
|
57
|
+
paused: { type: "boolean" },
|
|
58
|
+
pauseReason: { type: "string", enum: ["interrupted", "failed", "restart", "manual", "delivery"] },
|
|
59
|
+
items: { type: "array", items: item },
|
|
60
|
+
waitingFor: { type: "array", items: background },
|
|
61
|
+
observed: { type: "boolean" },
|
|
62
|
+
delivering: { type: "boolean" },
|
|
63
|
+
lastError: { type: "string" },
|
|
64
|
+
updatedAt: { type: "number" },
|
|
65
|
+
},
|
|
66
|
+
required: ["sessionID", "paused", "items", "waitingFor", "observed", "delivering", "updatedAt"],
|
|
67
|
+
additionalProperties: false,
|
|
68
|
+
};
|
|
69
|
+
const invalid = { type: "object", additionalProperties: false };
|
|
70
|
+
export const DelayedQueueRpc = Rpc.define({
|
|
71
|
+
id: "max10240.delayed-queue",
|
|
72
|
+
events: {
|
|
73
|
+
changed: {
|
|
74
|
+
schema: {
|
|
75
|
+
type: "object",
|
|
76
|
+
properties: {
|
|
77
|
+
sessionID: { type: "string" },
|
|
78
|
+
state: status,
|
|
79
|
+
},
|
|
80
|
+
required: ["sessionID", "state"],
|
|
81
|
+
additionalProperties: false,
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
methods: {
|
|
86
|
+
status: {
|
|
87
|
+
input: sessionInput,
|
|
88
|
+
output: status,
|
|
89
|
+
errors: { invalid },
|
|
90
|
+
},
|
|
91
|
+
enqueue: {
|
|
92
|
+
input: {
|
|
93
|
+
type: "object",
|
|
94
|
+
properties: {
|
|
95
|
+
sessionID: { type: "string" },
|
|
96
|
+
text: { type: "string" },
|
|
97
|
+
jobs: { type: "array", items: job },
|
|
98
|
+
},
|
|
99
|
+
required: ["sessionID", "text"],
|
|
100
|
+
additionalProperties: false,
|
|
101
|
+
},
|
|
102
|
+
output: status,
|
|
103
|
+
errors: { invalid },
|
|
104
|
+
},
|
|
105
|
+
syncJobs: {
|
|
106
|
+
input: {
|
|
107
|
+
type: "object",
|
|
108
|
+
properties: {
|
|
109
|
+
sessionID: { type: "string" },
|
|
110
|
+
jobs: { type: "array", items: job },
|
|
111
|
+
},
|
|
112
|
+
required: ["sessionID", "jobs"],
|
|
113
|
+
additionalProperties: false,
|
|
114
|
+
},
|
|
115
|
+
output: status,
|
|
116
|
+
errors: { invalid },
|
|
117
|
+
},
|
|
118
|
+
remove: {
|
|
119
|
+
input: {
|
|
120
|
+
type: "object",
|
|
121
|
+
properties: {
|
|
122
|
+
sessionID: { type: "string" },
|
|
123
|
+
ids: { type: "array", items: { type: "string" } },
|
|
124
|
+
},
|
|
125
|
+
required: ["sessionID", "ids"],
|
|
126
|
+
additionalProperties: false,
|
|
127
|
+
},
|
|
128
|
+
output: status,
|
|
129
|
+
errors: { invalid },
|
|
130
|
+
},
|
|
131
|
+
clear: {
|
|
132
|
+
input: sessionInput,
|
|
133
|
+
output: status,
|
|
134
|
+
errors: { invalid },
|
|
135
|
+
},
|
|
136
|
+
pause: {
|
|
137
|
+
input: sessionInput,
|
|
138
|
+
output: status,
|
|
139
|
+
errors: { invalid },
|
|
140
|
+
},
|
|
141
|
+
resume: {
|
|
142
|
+
input: sessionInput,
|
|
143
|
+
output: status,
|
|
144
|
+
errors: { invalid },
|
|
145
|
+
},
|
|
146
|
+
deliver: {
|
|
147
|
+
input: {
|
|
148
|
+
type: "object",
|
|
149
|
+
properties: {
|
|
150
|
+
sessionID: { type: "string" },
|
|
151
|
+
id: { type: "string" },
|
|
152
|
+
},
|
|
153
|
+
required: ["sessionID", "id"],
|
|
154
|
+
additionalProperties: false,
|
|
155
|
+
},
|
|
156
|
+
output: status,
|
|
157
|
+
errors: { invalid },
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
/** Narrows controller values to the exact RPC payload shape (no extra keys). */
|
|
162
|
+
export function toStatusPayload(status) {
|
|
163
|
+
return {
|
|
164
|
+
sessionID: status.sessionID,
|
|
165
|
+
paused: status.paused,
|
|
166
|
+
...(status.pauseReason === undefined ? {} : { pauseReason: status.pauseReason }),
|
|
167
|
+
items: status.items.map((entry) => ({
|
|
168
|
+
id: entry.id,
|
|
169
|
+
text: entry.text,
|
|
170
|
+
createdAt: entry.createdAt,
|
|
171
|
+
})),
|
|
172
|
+
waitingFor: status.waitingFor.map((work) => ({
|
|
173
|
+
kind: work.kind,
|
|
174
|
+
sessionID: work.sessionID,
|
|
175
|
+
...(work.label === undefined ? {} : { label: work.label }),
|
|
176
|
+
...(work.jobID === undefined ? {} : { jobID: work.jobID }),
|
|
177
|
+
...(work.shellID === undefined ? {} : { shellID: work.shellID }),
|
|
178
|
+
...(work.command === undefined ? {} : { command: work.command }),
|
|
179
|
+
...(work.childSessionID === undefined ? {} : { childSessionID: work.childSessionID }),
|
|
180
|
+
...(work.agent === undefined ? {} : { agent: work.agent }),
|
|
181
|
+
...(work.description === undefined ? {} : { description: work.description }),
|
|
182
|
+
...(work.path === undefined ? {} : { path: work.path }),
|
|
183
|
+
...(work.model === undefined ? {} : { model: work.model }),
|
|
184
|
+
})),
|
|
185
|
+
observed: status.observed,
|
|
186
|
+
delivering: status.delivering,
|
|
187
|
+
...(status.lastError === undefined ? {} : { lastError: status.lastError }),
|
|
188
|
+
updatedAt: status.updatedAt,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
export function toJobPayload(job) {
|
|
192
|
+
return {
|
|
193
|
+
id: job.id,
|
|
194
|
+
kind: job.kind,
|
|
195
|
+
sessionID: job.sessionID,
|
|
196
|
+
status: job.status,
|
|
197
|
+
...(job.childSessionID === undefined ? {} : { childSessionID: job.childSessionID }),
|
|
198
|
+
...(job.shellID === undefined ? {} : { shellID: job.shellID }),
|
|
199
|
+
...(job.command === undefined ? {} : { command: job.command }),
|
|
200
|
+
...(job.description === undefined ? {} : { description: job.description }),
|
|
201
|
+
...(job.title === undefined ? {} : { title: job.title }),
|
|
202
|
+
...(job.path === undefined ? {} : { path: job.path }),
|
|
203
|
+
};
|
|
204
|
+
}
|