@dsh-mobile/mobile-remote 3.4.1 → 3.5.0-beta.2

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.
Files changed (42) hide show
  1. package/README.md +1 -1
  2. package/docs/PRIVACY-POLICY.en.md +10 -2
  3. package/docs/PRIVACY-POLICY.md +10 -2
  4. package/docs/TERMS-OF-SERVICE.en.md +3 -2
  5. package/docs/TERMS-OF-SERVICE.md +3 -2
  6. package/lib/backup.js +30 -0
  7. package/lib/config.js +10 -0
  8. package/lib/host-plugins.js +290 -0
  9. package/lib/host-schedule.js +273 -0
  10. package/lib/host-team.js +89 -0
  11. package/lib/index.js +208 -3292
  12. package/lib/live-activity.js +351 -0
  13. package/lib/memory/context.js +17 -3
  14. package/lib/memory/decay.js +68 -4
  15. package/lib/memory/promises.js +116 -0
  16. package/lib/memory/routes.js +113 -1
  17. package/lib/memory/store.js +212 -13
  18. package/lib/memory/turn-hook.js +109 -0
  19. package/lib/phone-actions.js +17 -5
  20. package/lib/phone-announce.js +6 -5
  21. package/lib/phone-push.js +310 -0
  22. package/lib/protocol.js +16 -0
  23. package/lib/relay/connector.js +13 -1
  24. package/lib/routes/agents.js +405 -0
  25. package/lib/routes/app-state.js +179 -0
  26. package/lib/routes/backup.js +144 -0
  27. package/lib/routes/diagnostics.js +227 -0
  28. package/lib/routes/files.js +326 -0
  29. package/lib/routes/git.js +53 -0
  30. package/lib/routes/history.js +313 -0
  31. package/lib/routes/host-services.js +51 -0
  32. package/lib/routes/models.js +462 -0
  33. package/lib/routes/notifications.js +183 -0
  34. package/lib/routes/pairing.js +170 -0
  35. package/lib/routes/route-groups.js +20 -0
  36. package/lib/routes/send.js +341 -0
  37. package/lib/routes/sessions.js +462 -0
  38. package/lib/routes/toolbox.js +14 -0
  39. package/lib/routes/usage.js +211 -0
  40. package/lib/timeline.js +5 -1
  41. package/lib/toolbox.js +355 -0
  42. package/package.json +1 -1
@@ -0,0 +1,273 @@
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
+ import { readBody, sendError, sendJson } from "./http-util.js";
32
+
33
+
34
+ export const SCHEDULES_FEATURE = "schedules";
35
+
36
+ const ID_RE = /^[A-Za-z0-9._:-]{1,128}$/;
37
+ const TITLE_MAX = 120;
38
+ const PROMPT_MAX = 32 * 1024;
39
+ const HISTORY_LIMIT = 20;
40
+ const PROMPT_VIEW_MAX = 16 * 1024;
41
+
42
+ const str = (v) => (typeof v === "string" ? v : undefined);
43
+ const clamp = (s, max) => (typeof s === "string" && s.length > max ? `${s.slice(0, max)}…` : s);
44
+
45
+
46
+ export function scheduleView(entry) {
47
+ if (!entry || typeof entry !== "object" || typeof entry.id !== "string") return null;
48
+ const target = entry.target && typeof entry.target === "object"
49
+ ? { kind: str(entry.target.kind) ?? "unknown", ...(str(entry.target.sessionId) ? { sessionId: entry.target.sessionId } : {}) }
50
+ : null;
51
+ const last = entry.lastDelivery && typeof entry.lastDelivery === "object"
52
+ ? { deliveredAt: str(entry.lastDelivery.deliveredAt) ?? null, targetSessionId: str(entry.lastDelivery.targetSessionId) ?? null }
53
+ : null;
54
+ const timing = { kind: str(entry.kind) ?? "unknown" };
55
+ for (const key of ["afterSeconds", "everySeconds"]) if (Number.isFinite(entry[key])) timing[key] = entry[key];
56
+ for (const key of ["time", "timeZone", "expression", "rrule"]) if (str(entry[key])) timing[key] = entry[key];
57
+ if (Array.isArray(entry.weekdays)) timing.weekdays = entry.weekdays.filter((d) => Number.isInteger(d));
58
+ return {
59
+ id: entry.id,
60
+ title: str(entry.title) ?? "",
61
+ prompt: clamp(str(entry.prompt) ?? "", PROMPT_VIEW_MAX),
62
+ status: ["active", "paused", "ended"].includes(entry.status) ? entry.status : "unknown",
63
+ timing,
64
+
65
+ nextAt: str(entry.scheduledAt) ?? null,
66
+ createdAt: str(entry.createdAt) ?? null,
67
+ updatedAt: str(entry.updatedAt) ?? null,
68
+ running: entry.executionActive === true,
69
+ target,
70
+ lastDelivery: last,
71
+ };
72
+ }
73
+
74
+ const receiptView = (r) => ({
75
+ scheduledAt: str(r?.scheduledAt) ?? null,
76
+ deliveredAt: str(r?.deliveredAt) ?? null,
77
+ targetSessionId: str(r?.targetSessionId) ?? null,
78
+ });
79
+
80
+ const hostZone = () => {
81
+ try { return Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC"; } catch { return "UTC"; }
82
+ };
83
+ const TIME_RE = /^\d{2}:\d{2}(:\d{2}(\.\d{1,3})?)?$/;
84
+ const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
85
+ const zoneOf = (v) => (typeof v === "string" && v.length > 0 && v.length <= 64 ? v : hostZone());
86
+ const secondsOf = (v) => (Number.isInteger(v) && v >= 1 && v <= 366 * 86400 ? v : undefined);
87
+
88
+
89
+ export function scheduleTimingOf(t) {
90
+ if (!t || typeof t !== "object" || Array.isArray(t)) return { error: "timing must be an object" };
91
+ switch (t.kind) {
92
+ case "after": {
93
+ const n = secondsOf(t.afterSeconds);
94
+ return n ? { timing: { kind: "after", after_seconds: n } } : { error: "afterSeconds must be a whole number of seconds" };
95
+ }
96
+ case "every": {
97
+ const n = secondsOf(t.everySeconds);
98
+ return n ? { timing: { kind: "every", every_seconds: n } } : { error: "everySeconds must be a whole number of seconds" };
99
+ }
100
+ case "at":
101
+ if (!DATE_RE.test(t.date ?? "") || !TIME_RE.test(t.time ?? "")) return { error: "at needs date (YYYY-MM-DD) and time (HH:mm)" };
102
+ return { timing: { kind: "at", at: { date: t.date, time: t.time.length === 5 ? `${t.time}:00` : t.time, time_zone: zoneOf(t.timeZone) } } };
103
+ case "daily":
104
+ if (!TIME_RE.test(t.time ?? "")) return { error: "daily needs time (HH:mm)" };
105
+ return { timing: { kind: "daily", daily: { time: t.time.length === 5 ? `${t.time}:00` : t.time, time_zone: zoneOf(t.timeZone) } } };
106
+ case "weekly": {
107
+ const days = Array.isArray(t.weekdays) ? [...new Set(t.weekdays)] : [];
108
+ if (!TIME_RE.test(t.time ?? "")) return { error: "weekly needs time (HH:mm)" };
109
+ if (days.length === 0 || days.some((d) => !Number.isInteger(d) || d < 1 || d > 7)) return { error: "weekdays must be 1 (Mon) to 7 (Sun)" };
110
+ return { timing: { kind: "weekly", weekly: { time: t.time.length === 5 ? `${t.time}:00` : t.time, time_zone: zoneOf(t.timeZone), weekdays: days.sort((a, b) => a - b) } } };
111
+ }
112
+ case "cron":
113
+ if (typeof t.expression !== "string" || !t.expression.trim() || t.expression.length > 200) return { error: "cron needs an expression" };
114
+ return { timing: { kind: "cron", cron: { expression: t.expression.trim(), time_zone: zoneOf(t.timeZone) } } };
115
+ default:
116
+ return { error: "timing.kind is not one the phone can set" };
117
+ }
118
+ }
119
+
120
+
121
+ export function scheduleTargetOf(t) {
122
+ if (!t || typeof t !== "object" || Array.isArray(t)) return { error: "target must be an object" };
123
+ if (t.kind === "session") {
124
+ if (typeof t.sessionId !== "string" || !ID_RE.test(t.sessionId)) return { error: "target.sessionId is not valid" };
125
+ return { target: { kind: "session", sessionId: t.sessionId } };
126
+ }
127
+ if (t.kind === "new_once" || t.kind === "new") {
128
+ const create = {};
129
+ if (typeof t.cwd === "string" && t.cwd && t.cwd.length <= 1024) create.cwd = t.cwd;
130
+ return { target: { kind: t.kind, ...(Object.keys(create).length ? { create } : {}) } };
131
+ }
132
+ return { error: "target.kind is not one the phone can set" };
133
+ }
134
+
135
+
136
+ export function schedulePatchOf(body) {
137
+ if (!body || typeof body !== "object" || Array.isArray(body)) return { error: "body must be an object" };
138
+ const patch = {};
139
+ if (body.title !== undefined) {
140
+ if (typeof body.title !== "string" || !body.title.trim()) return { error: "title must be a non-empty string" };
141
+ if (body.title.trim().length > TITLE_MAX) return { error: `title is at most ${TITLE_MAX} characters` };
142
+ patch.title = body.title.trim();
143
+ }
144
+ if (body.prompt !== undefined) {
145
+ if (typeof body.prompt !== "string" || !body.prompt.trim()) return { error: "prompt must be a non-empty string" };
146
+ if (body.prompt.length > PROMPT_MAX) return { error: "prompt is too long" };
147
+ patch.prompt = body.prompt;
148
+ }
149
+ if (body.active !== undefined) {
150
+ if (typeof body.active !== "boolean") return { error: "active must be true or false" };
151
+ patch.active = body.active;
152
+ }
153
+ if (body.timing !== undefined) {
154
+ const r = scheduleTimingOf(body.timing);
155
+ if (r.error) return { error: r.error };
156
+ patch.timing = r.timing;
157
+ }
158
+ if (body.target !== undefined) {
159
+ const r = scheduleTargetOf(body.target);
160
+ if (r.error) return { error: r.error };
161
+ patch.target = r.target;
162
+ }
163
+ if (Object.keys(patch).length === 0) return { error: "nothing to change" };
164
+ const expected = body.expectedUpdatedAt;
165
+ if (expected !== undefined && expected !== null && typeof expected !== "string") return { error: "expectedUpdatedAt must be a string or null" };
166
+ return { patch, expectedUpdatedAt: expected };
167
+ }
168
+
169
+
170
+ function answerMiss(res, result) {
171
+ if (result?.code === "schedule_not_found") { sendError(res, 404, "schedule-not-found"); return true; }
172
+ if (result?.code === "schedule_conflict") { sendError(res, 409, "schedule-conflict", "the task changed on the computer; reload it"); return true; }
173
+ if (result?.code === "schedule_execution_pending") { sendError(res, 409, "schedule-execution-pending", "this task is already being delivered"); return true; }
174
+ return false;
175
+ }
176
+ function answerThrown(res, err) {
177
+ const code = typeof err?.code === "string" ? err.code : undefined;
178
+
179
+ const inputCode = code && code !== "schedule/input-invalid" ? code : err?.details?.code;
180
+ if (inputCode && /^[a-z_]+$/.test(inputCode)) return sendError(res, 400, "schedule-invalid", `${inputCode}: ${String(err?.message ?? "")}`.slice(0, 300));
181
+ return sendError(res, 500, "schedule-failed", String(err?.message ?? err).slice(0, 300));
182
+ }
183
+
184
+
185
+
186
+
187
+ export function createScheduleRoutes({ service, logger }) {
188
+ const svc = () => {
189
+ try {
190
+ const s = service();
191
+ return s && typeof s.catalog === "function" && typeof s.edit === "function" ? s : undefined;
192
+ } catch {
193
+ return undefined;
194
+ }
195
+ };
196
+ const readJsonBody = async (req, res) => {
197
+ try {
198
+ return JSON.parse(await readBody(req)) ?? {};
199
+ } catch (err) {
200
+ sendError(res, err?.status === 413 ? 413 : 400, err?.status === 413 ? "payload-too-large" : "bad-request");
201
+ return undefined;
202
+ }
203
+ };
204
+
205
+ return async function handleSchedules(req, res, rest, method, url) {
206
+ if (rest !== "/schedules" && !rest.startsWith("/schedules/")) return false;
207
+ const s = svc();
208
+ if (!s) {
209
+ sendError(res, 503, "schedule-unavailable", "this computer's DSH does not offer scheduled tasks");
210
+ return true;
211
+ }
212
+ try {
213
+ if (rest === "/schedules") {
214
+ if (method !== "GET" && method !== "HEAD") { sendError(res, 405, "method-not-allowed"); return true; }
215
+ const entries = await s.catalog();
216
+ const items = (Array.isArray(entries) ? entries : []).map(scheduleView).filter(Boolean);
217
+ sendJson(res, 200, { ok: true, items, total: items.length });
218
+ return true;
219
+ }
220
+ const m = /^\/schedules\/([^/]+)(\/run)?$/.exec(rest);
221
+ if (!m) { sendError(res, 404, "not-found"); return true; }
222
+ let id;
223
+ try { id = decodeURIComponent(m[1]); } catch { id = ""; }
224
+ if (!ID_RE.test(id)) { sendError(res, 400, "bad-schedule-id"); return true; }
225
+ if (m[2]) {
226
+ if (method !== "POST") { sendError(res, 405, "method-not-allowed"); return true; }
227
+ const result = await s.run({ id });
228
+ if (answerMiss(res, result)) return true;
229
+ sendJson(res, 200, { ok: true, item: scheduleView(result) });
230
+ return true;
231
+ }
232
+ if (method === "GET" || method === "HEAD") {
233
+ const result = await s.view({ id });
234
+ if (answerMiss(res, result)) return true;
235
+ let history = [];
236
+ if (typeof s.history === "function") {
237
+ try {
238
+ const page = await s.history({ id, limit: HISTORY_LIMIT });
239
+ if (Array.isArray(page?.records)) history = page.records.map(receiptView);
240
+ } catch (err) {
241
+ logger?.debug?.(`mobile-remote: schedule history ${id}: ${err?.message ?? err}`);
242
+ }
243
+ }
244
+ sendJson(res, 200, { ok: true, item: scheduleView(result), history });
245
+ return true;
246
+ }
247
+ if (method === "PATCH") {
248
+ const body = await readJsonBody(req, res);
249
+ if (body === undefined) return true;
250
+ const parsed = schedulePatchOf(body);
251
+ if (parsed.error) { sendError(res, 400, "bad-request", parsed.error); return true; }
252
+ const result = await s.edit({ id, action: "update", patch: parsed.patch, ...(parsed.expectedUpdatedAt !== undefined ? { expectedUpdatedAt: parsed.expectedUpdatedAt } : {}) });
253
+ if (answerMiss(res, result)) return true;
254
+ sendJson(res, 200, { ok: true, item: scheduleView(result) });
255
+ return true;
256
+ }
257
+ if (method === "DELETE") {
258
+
259
+ if (url?.searchParams?.get("confirm") !== "1") { sendError(res, 400, "confirmation-required", "deleting a task needs confirm=1"); return true; }
260
+ const expected = url.searchParams.get("expectedUpdatedAt");
261
+ const result = await s.edit({ id, action: "delete", ...(expected ? { expectedUpdatedAt: expected } : {}) });
262
+ if (answerMiss(res, result)) return true;
263
+ sendJson(res, 200, { ok: true, deleted: result?.deleted === true, id });
264
+ return true;
265
+ }
266
+ sendError(res, 405, "method-not-allowed");
267
+ return true;
268
+ } catch (err) {
269
+ answerThrown(res, err);
270
+ return true;
271
+ }
272
+ };
273
+ }
@@ -0,0 +1,89 @@
1
+
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+ import { sendError, sendJson } from "./http-util.js";
22
+
23
+
24
+ export const AGENT_TEAM_FEATURE = "agent-team";
25
+
26
+ const MAX_TASKS = 256;
27
+ const TEXT_MAX = 4000;
28
+
29
+ const str = (v) => (typeof v === "string" ? v : undefined);
30
+ const clamp = (s, max) => (typeof s === "string" && s.length > max ? `${s.slice(0, max)}…` : s);
31
+
32
+
33
+ export function teamTaskView(t) {
34
+ if (!t || typeof t !== "object" || typeof t.id !== "string") return null;
35
+ return {
36
+ id: t.id,
37
+ subject: clamp(str(t.subject) ?? "", 500),
38
+ description: clamp(str(t.description) ?? "", TEXT_MAX),
39
+ status: ["pending", "in_progress", "completed"].includes(t.status) ? t.status : "unknown",
40
+ ready: t.ready === true,
41
+ blockedBy: Array.isArray(t.blockedBy) ? t.blockedBy.filter((x) => typeof x === "string").slice(0, 64) : [],
42
+ ownerId: str(t.ownerId) ?? null,
43
+ ownerName: str(t.ownerName) ?? null,
44
+ writeScopes: Array.isArray(t.writeScopes) ? t.writeScopes.filter((x) => typeof x === "string").slice(0, 32) : [],
45
+ warnings: Array.isArray(t.writeScopeWarnings) ? t.writeScopeWarnings.filter((x) => typeof x === "string").slice(0, 16) : [],
46
+ };
47
+ }
48
+
49
+
50
+
51
+
52
+
53
+ export function createTeamRoutes({ service, agents, logger }) {
54
+ const get = (fn) => {
55
+ try { return fn(); } catch { return undefined; }
56
+ };
57
+ return async function handleTeam(req, res, rest, method, url) {
58
+ if (rest !== "/team/tasks") return false;
59
+ if (method !== "GET" && method !== "HEAD") { sendError(res, 405, "method-not-allowed"); return true; }
60
+ const svc = get(service);
61
+ if (!svc || typeof svc.listTasks !== "function") {
62
+ sendError(res, 503, "team-unavailable", "this computer's DSH does not offer agent teams");
63
+ return true;
64
+ }
65
+ const sessionId = url?.searchParams?.get("sessionId") ?? "";
66
+ if (!sessionId) { sendError(res, 400, "bad-request", "missing sessionId"); return true; }
67
+ const agent = get(() => agents()?.get?.(sessionId));
68
+ if (!agent) {
69
+ sendJson(res, 200, { ok: true, sessionId, enabled: null, tasks: null });
70
+ return true;
71
+ }
72
+ let enabled = null;
73
+ try {
74
+ if (typeof svc.isEnabled === "function") enabled = svc.isEnabled(agent) === true;
75
+ } catch (err) {
76
+ logger?.debug?.(`mobile-remote: team isEnabled ${sessionId}: ${err?.message ?? err}`);
77
+ }
78
+ try {
79
+ const list = await svc.listTasks(agent);
80
+ const tasks = (Array.isArray(list) ? list : []).slice(0, MAX_TASKS).map(teamTaskView).filter(Boolean);
81
+ sendJson(res, 200, { ok: true, sessionId, enabled, tasks });
82
+ } catch (err) {
83
+
84
+ logger?.debug?.(`mobile-remote: team listTasks ${sessionId}: ${err?.message ?? err}`);
85
+ sendJson(res, 200, { ok: true, sessionId, enabled, tasks: null });
86
+ }
87
+ return true;
88
+ };
89
+ }