@clagentic/console 1.11.0 → 1.12.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.
- package/README.md +1 -0
- package/bin/cli.js +14 -2
- package/lib/build-changes.js +8 -2
- package/lib/build-sha.json +2 -2
- package/lib/build-update-offer.js +31 -7
- package/lib/claude-settings-env.js +142 -0
- package/lib/cli/arg-validation.js +1 -1
- package/lib/cli/duration.js +27 -0
- package/lib/cli/ipc-subcommands.js +26 -0
- package/lib/cli/schedule-input-subcommands.js +1 -22
- package/lib/cli/session-subcommands.js +38 -7
- package/lib/daemon.js +26 -0
- package/lib/notification-prefs.js +257 -0
- package/lib/notify/core.js +175 -0
- package/lib/notify/index.js +31 -0
- package/lib/notify/live-prompts.js +36 -0
- package/lib/notify/payload.js +63 -0
- package/lib/notify/target.js +5 -0
- package/lib/notify/type-registry.js +64 -0
- package/lib/notify/types.js +20 -0
- package/lib/open-sockets.js +27 -0
- package/lib/outbound-access.js +74 -0
- package/lib/outbound-entry.js +109 -0
- package/lib/outbound-ledger.js +306 -0
- package/lib/outbound-query.js +153 -0
- package/lib/pending-prompts.js +31 -16
- package/lib/project-connection.js +40 -1
- package/lib/project-filesystem.js +20 -5
- package/lib/project-http.js +9 -1
- package/lib/project-loop.js +25 -13
- package/lib/project-notifications.js +206 -42
- package/lib/project-prompt-responses.js +1 -1
- package/lib/project-sessions.js +30 -1
- package/lib/project.js +15 -1
- package/lib/prompt-registry.js +92 -24
- package/lib/prompt-reminders.js +282 -0
- package/lib/prompt-source.js +43 -8
- package/lib/public/app.js +1 -0
- package/lib/public/css/diagnostics.css +28 -0
- package/lib/public/css/home-hub.css +12 -0
- package/lib/public/css/icon-strip.css +9 -0
- package/lib/public/css/input.css +93 -0
- package/lib/public/css/menus.css +22 -0
- package/lib/public/css/mobile-nav.css +11 -0
- package/lib/public/css/server-settings.css +24 -0
- package/lib/public/css/sidebar.css +24 -0
- package/lib/public/index.html +6 -0
- package/lib/public/modules/activity-state.js +20 -4
- package/lib/public/modules/app-connection.js +22 -5
- package/lib/public/modules/app-header.js +2 -0
- package/lib/public/modules/app-messages.js +6 -2
- package/lib/public/modules/app-notifications.js +65 -40
- package/lib/public/modules/app-projects.js +3 -1
- package/lib/public/modules/app-rate-limit.js +12 -6
- package/lib/public/modules/app-rendering.js +26 -3
- package/lib/public/modules/build-update-sessions.js +81 -0
- package/lib/public/modules/collapsible-text.js +77 -0
- package/lib/public/modules/diagnostics.js +9 -2
- package/lib/public/modules/env-shadow-notice.js +34 -0
- package/lib/public/modules/keep-alive.js +66 -0
- package/lib/public/modules/notification-prefs.js +151 -0
- package/lib/public/modules/notify-client.js +153 -0
- package/lib/public/modules/project-settings.js +5 -0
- package/lib/public/modules/prompt-reminders-setting.js +13 -0
- package/lib/public/modules/prompt-state.js +3 -3
- package/lib/public/modules/prompts.js +1 -1
- package/lib/public/modules/server-settings.js +3 -0
- package/lib/public/modules/sidebar-mobile.js +8 -0
- package/lib/public/modules/sidebar-sessions.js +27 -0
- package/lib/public/modules/terminal-links.js +175 -0
- package/lib/public/modules/terminal.js +19 -103
- package/lib/public/notify-sw-logic.js +300 -0
- package/lib/public/notify-target.js +97 -0
- package/lib/public/sw.js +15 -62
- package/lib/push-audience.js +84 -0
- package/lib/push.js +87 -20
- package/lib/runtime-env.js +1 -0
- package/lib/sdk-bridge.js +83 -36
- package/lib/sdk-message-processor.js +148 -49
- package/lib/sender-ref.js +3 -0
- package/lib/server.js +80 -4
- package/lib/session-directory.js +6 -4
- package/lib/session-input-ipc.js +11 -1
- package/lib/session-input.js +95 -14
- package/lib/session-liveness.js +53 -0
- package/lib/session-reclaim.js +37 -2
- package/lib/sessions.js +58 -4
- package/lib/usage-store.js +403 -0
- package/lib/vendor-auth.js +33 -2
- package/lib/ws-schema.js +13 -2
- package/lib/yoke/adapters/claude-vendor.js +11 -1
- package/lib/yoke/adapters/claude.js +17 -0
- package/lib/yoke/adapters/codex.js +5 -1
- package/lib/yoke/auth-checks.js +17 -2
- package/lib/yoke/index.js +21 -3
- package/lib/yoke/provider-credentials.js +24 -0
- package/lib/yoke/registry.js +8 -1
- package/package.json +2 -2
- package/scripts/check-test-count.js +14 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Per-user notification preferences, kept on the server so the phone and the
|
|
4
|
+
// desktop of one user agree and so push can be filtered where it is sent.
|
|
5
|
+
//
|
|
6
|
+
// A preference is a boolean for each (surface, category): the surfaces are the
|
|
7
|
+
// in-app banner, the sound and push; the categories group the notification
|
|
8
|
+
// types that exist. Muting a category never hides the item: the notifications
|
|
9
|
+
// panel and the session's prompt card still show it. Only the alert, the sound
|
|
10
|
+
// and the push are suppressed (the client reads this for the first two, the
|
|
11
|
+
// push module for the third).
|
|
12
|
+
//
|
|
13
|
+
// Store: <CONFIG_DIR>/notification-prefs.json, i.e. ~/.clagentic/console/
|
|
14
|
+
// notification-prefs.json, written atomically with mode 0600:
|
|
15
|
+
// { "version": 1, "users": { "<userId or _default>": { "banner": {...}, "sound": {...}, "push": {...} } } }
|
|
16
|
+
// The key "_default" is the one user of a daemon that has no accounts.
|
|
17
|
+
|
|
18
|
+
var fs = require("fs");
|
|
19
|
+
var path = require("path");
|
|
20
|
+
var config = require("./config");
|
|
21
|
+
var store = require("./store");
|
|
22
|
+
|
|
23
|
+
var FILE_NAME = "notification-prefs.json";
|
|
24
|
+
var DEFAULT_USER = "_default";
|
|
25
|
+
|
|
26
|
+
var SURFACES = ["banner", "sound", "push"];
|
|
27
|
+
var CATEGORIES = ["turn_done", "approval", "approval_reminder", "loop", "messages", "system"];
|
|
28
|
+
|
|
29
|
+
// Display names for the settings panel; the client renders these.
|
|
30
|
+
var CATEGORY_LABELS = {
|
|
31
|
+
turn_done: "Turn finished",
|
|
32
|
+
approval: "Approval prompts and questions",
|
|
33
|
+
approval_reminder: "Approval reminders",
|
|
34
|
+
loop: "Loop and schedule results",
|
|
35
|
+
messages: "Messages and mentions",
|
|
36
|
+
system: "Account and system notices",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// Notification type (project-notifications.js) -> category.
|
|
40
|
+
var NOTIFICATION_CATEGORY = {
|
|
41
|
+
response_done: "turn_done",
|
|
42
|
+
permission_request: "approval",
|
|
43
|
+
permission_reminder: "approval_reminder",
|
|
44
|
+
loop_complete: "loop",
|
|
45
|
+
loop_error: "loop",
|
|
46
|
+
mate_dm: "messages",
|
|
47
|
+
mention_response: "messages",
|
|
48
|
+
user_mention: "messages",
|
|
49
|
+
auth_required: "system",
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// Push payload type -> category. A type absent here (the silent "test"
|
|
53
|
+
// validation push) is never filtered.
|
|
54
|
+
var PUSH_CATEGORY = {
|
|
55
|
+
permission_request: "approval",
|
|
56
|
+
elicitation: "approval",
|
|
57
|
+
ask_user: "approval",
|
|
58
|
+
permission_reminder: "approval_reminder",
|
|
59
|
+
dm: "messages",
|
|
60
|
+
user_mention: "messages",
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// Today's behaviour: every banner and push is on; the sound plays for a
|
|
64
|
+
// finished turn only.
|
|
65
|
+
function defaultPrefs() {
|
|
66
|
+
var out = {};
|
|
67
|
+
SURFACES.forEach(function (surface) {
|
|
68
|
+
out[surface] = {};
|
|
69
|
+
CATEGORIES.forEach(function (category) {
|
|
70
|
+
out[surface][category] = surface === "sound" ? category === "turn_done" : true;
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// The categories each preset switches on, on every surface. "system" is left
|
|
77
|
+
// as the user has it: an expired login or a failed update needs the operator
|
|
78
|
+
// whatever else is muted.
|
|
79
|
+
var PRESETS = {
|
|
80
|
+
all: { label: "Everything (default)", enabled: null },
|
|
81
|
+
approval_prompts: { label: "Approval prompts only", enabled: ["approval", "approval_reminder"] },
|
|
82
|
+
approval_reminders: { label: "Approval reminders only", enabled: ["approval_reminder"] },
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
function applyPreset(prefs, presetName) {
|
|
86
|
+
// presetName comes from a client: only a preset this module defines counts.
|
|
87
|
+
if (!Object.prototype.hasOwnProperty.call(PRESETS, presetName)) return null;
|
|
88
|
+
var preset = PRESETS[presetName];
|
|
89
|
+
var base = defaultPrefs();
|
|
90
|
+
var next = {};
|
|
91
|
+
SURFACES.forEach(function (surface) {
|
|
92
|
+
next[surface] = {};
|
|
93
|
+
CATEGORIES.forEach(function (category) {
|
|
94
|
+
if (category === "system") next[surface][category] = prefs[surface][category];
|
|
95
|
+
else if (preset.enabled === null) next[surface][category] = base[surface][category];
|
|
96
|
+
else next[surface][category] = preset.enabled.indexOf(category) !== -1;
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
return next;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Which preset the preferences equal, or null for a custom mix.
|
|
103
|
+
function presetOf(prefs) {
|
|
104
|
+
var names = Object.keys(PRESETS);
|
|
105
|
+
for (var i = 0; i < names.length; i++) {
|
|
106
|
+
var applied = applyPreset(prefs, names[i]);
|
|
107
|
+
if (JSON.stringify(applied) === JSON.stringify(prefs)) return names[i];
|
|
108
|
+
}
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Stored data is whatever a file or a client held; only known booleans count.
|
|
113
|
+
function normalize(raw) {
|
|
114
|
+
var out = defaultPrefs();
|
|
115
|
+
if (!raw || typeof raw !== "object") return out;
|
|
116
|
+
SURFACES.forEach(function (surface) {
|
|
117
|
+
var row = raw[surface];
|
|
118
|
+
if (!row || typeof row !== "object") return;
|
|
119
|
+
CATEGORIES.forEach(function (category) {
|
|
120
|
+
if (typeof row[category] === "boolean") out[surface][category] = row[category];
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function categoryOfNotification(type) {
|
|
127
|
+
return Object.prototype.hasOwnProperty.call(NOTIFICATION_CATEGORY, type) ? NOTIFICATION_CATEGORY[type] : null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function categoryOfPush(payload) {
|
|
131
|
+
if (!payload || typeof payload.type !== "string") return null;
|
|
132
|
+
if (payload.type === "done") return payload.tag === "ralph-loop-done" ? "loop" : "turn_done";
|
|
133
|
+
// A close push follows the preference of the notification it closes.
|
|
134
|
+
if (payload.type === "notify_close") return categoryOfPush({ type: payload.closes });
|
|
135
|
+
return Object.prototype.hasOwnProperty.call(PUSH_CATEGORY, payload.type) ? PUSH_CATEGORY[payload.type] : null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function userKey(userId) {
|
|
139
|
+
return typeof userId === "string" && userId ? userId : DEFAULT_USER;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @param {object} [o]
|
|
144
|
+
* @param {string} [o.file] absolute path of the store file (tests); default is
|
|
145
|
+
* the console config directory's notification-prefs.json.
|
|
146
|
+
* @param {function(object)} [o.persist] writes the whole document; default is
|
|
147
|
+
* store.js, which queues and writes atomically.
|
|
148
|
+
*/
|
|
149
|
+
function createPrefsStore(o) {
|
|
150
|
+
o = o || {};
|
|
151
|
+
var file = o.file || path.join(config.CONFIG_DIR, FILE_NAME);
|
|
152
|
+
var persist = o.persist || function (doc) {
|
|
153
|
+
return store.writeJsonAt(file, doc).catch(function (err) {
|
|
154
|
+
process.stderr.write("[notification-prefs] save failed: " + (err && err.message ? err.message : err) + "\n");
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
var users = Object.create(null);
|
|
158
|
+
|
|
159
|
+
try {
|
|
160
|
+
var saved = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
161
|
+
if (saved && saved.users && typeof saved.users === "object") {
|
|
162
|
+
Object.keys(saved.users).forEach(function (id) { users[id] = normalize(saved.users[id]); });
|
|
163
|
+
}
|
|
164
|
+
} catch (e) {
|
|
165
|
+
if (e.code !== "ENOENT") process.stderr.write("[notification-prefs] ignoring unreadable " + file + ": " + e.message + "\n");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function save() {
|
|
169
|
+
return persist({ version: 1, users: users });
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function get(userId) {
|
|
173
|
+
var key = userKey(userId);
|
|
174
|
+
return users[key] ? normalize(users[key]) : defaultPrefs();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function view(userId) {
|
|
178
|
+
var prefs = get(userId);
|
|
179
|
+
return {
|
|
180
|
+
prefs: prefs,
|
|
181
|
+
preset: presetOf(prefs),
|
|
182
|
+
known: Object.prototype.hasOwnProperty.call(users, userKey(userId)),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Change a user's preferences by preset name or by per-cell patch
|
|
188
|
+
* ({surface: {category: boolean}}). Unknown names and non-booleans are
|
|
189
|
+
* ignored. Returns the user's resulting view.
|
|
190
|
+
*/
|
|
191
|
+
function set(userId, change) {
|
|
192
|
+
var key = userKey(userId);
|
|
193
|
+
var prefs = get(userId);
|
|
194
|
+
if (change && typeof change.preset === "string") {
|
|
195
|
+
var applied = applyPreset(prefs, change.preset);
|
|
196
|
+
if (applied) prefs = applied;
|
|
197
|
+
}
|
|
198
|
+
if (change && change.surfaces && typeof change.surfaces === "object") {
|
|
199
|
+
prefs = normalize(Object.assign({}, prefs, mergeSurfaces(prefs, change.surfaces)));
|
|
200
|
+
}
|
|
201
|
+
users[key] = prefs;
|
|
202
|
+
save();
|
|
203
|
+
return view(userId);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function mergeSurfaces(prefs, patch) {
|
|
207
|
+
var out = {};
|
|
208
|
+
SURFACES.forEach(function (surface) {
|
|
209
|
+
out[surface] = Object.assign({}, prefs[surface], patch[surface] && typeof patch[surface] === "object" ? patch[surface] : {});
|
|
210
|
+
});
|
|
211
|
+
return out;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Seed a user's preferences from what the browser held before they lived on
|
|
216
|
+
* the server, once: only when the user has none stored. Today's globals map
|
|
217
|
+
* as far as they mean anything per user: the sound switch is the turn-done
|
|
218
|
+
* sound. The browser-alert and push switches describe this browser's
|
|
219
|
+
* permission and subscription, not a choice about types, and stay with it.
|
|
220
|
+
*/
|
|
221
|
+
function migrateLegacy(userId, legacy) {
|
|
222
|
+
if (Object.prototype.hasOwnProperty.call(users, userKey(userId))) return view(userId);
|
|
223
|
+
var prefs = defaultPrefs();
|
|
224
|
+
if (legacy && legacy.sound === false) prefs.sound.turn_done = false;
|
|
225
|
+
users[userKey(userId)] = prefs;
|
|
226
|
+
save();
|
|
227
|
+
return view(userId);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function allows(userId, surface, category) {
|
|
231
|
+
if (!category) return true;
|
|
232
|
+
var row = get(userId)[surface];
|
|
233
|
+
return !row || row[category] !== false;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return {
|
|
237
|
+
get: get,
|
|
238
|
+
view: view,
|
|
239
|
+
set: set,
|
|
240
|
+
migrateLegacy: migrateLegacy,
|
|
241
|
+
allows: allows,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
module.exports = {
|
|
246
|
+
createPrefsStore: createPrefsStore,
|
|
247
|
+
categoryOfNotification: categoryOfNotification,
|
|
248
|
+
categoryOfPush: categoryOfPush,
|
|
249
|
+
defaultPrefs: defaultPrefs,
|
|
250
|
+
userKey: userKey,
|
|
251
|
+
SURFACES: SURFACES,
|
|
252
|
+
CATEGORIES: CATEGORIES,
|
|
253
|
+
CATEGORY_LABELS: CATEGORY_LABELS,
|
|
254
|
+
PRESETS: PRESETS,
|
|
255
|
+
DEFAULT_USER: DEFAULT_USER,
|
|
256
|
+
FILE_NAME: FILE_NAME,
|
|
257
|
+
};
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// The notification core: the one place that turns a target and a message into a
|
|
4
|
+
// push, collapses repeats, and closes a push when its prompt resolves.
|
|
5
|
+
// Emitters call notify() and resolve() and keep no tag, route or dedupe logic.
|
|
6
|
+
// A notification reaches the surfaces its type registers: push, and the in-app
|
|
7
|
+
// banner (lib/project-notifications.js is the banner store the core delivers
|
|
8
|
+
// to; it is not a second core).
|
|
9
|
+
//
|
|
10
|
+
// The core keeps no memory of what it showed. Every tag is derived from the
|
|
11
|
+
// type's collapse rule and the target alone (payload.tagFor), so resolve() can
|
|
12
|
+
// name the tags to close from the target it is given, in any process: a close
|
|
13
|
+
// still works after a daemon restart.
|
|
14
|
+
|
|
15
|
+
var target = require("./target");
|
|
16
|
+
var payloads = require("./payload");
|
|
17
|
+
var { pushForSession, audienceFor } = require("../push-audience");
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {object} deps
|
|
21
|
+
* @param {object} deps.registry a type registry (type-registry.js).
|
|
22
|
+
* @param {?object} deps.push the push module (sendPush, sendPushToUser).
|
|
23
|
+
* @param {?{show: function, dismissKeys: function}} [deps.inApp] the in-app
|
|
24
|
+
* banner store: show(event, data, meta) creates a banner, replacing the one
|
|
25
|
+
* under the same meta.key; dismissKeys(keys) removes the banners under them.
|
|
26
|
+
*/
|
|
27
|
+
function createNotifyCore(deps) {
|
|
28
|
+
var registry = deps.registry;
|
|
29
|
+
var push = deps.push || null;
|
|
30
|
+
var inApp = deps.inApp || null;
|
|
31
|
+
|
|
32
|
+
function audienceOf(audience) {
|
|
33
|
+
return {
|
|
34
|
+
ownerId: audience && audience.ownerId || null,
|
|
35
|
+
isMultiUser: audience && audience.isMultiUser,
|
|
36
|
+
listAdmins: audience && audience.listAdmins,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function perSurface(content) {
|
|
41
|
+
var c = content || {};
|
|
42
|
+
if (c.push || c.banner) return { push: c.push || null, banner: c.banner || null };
|
|
43
|
+
return { push: c, banner: null };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function deliver(audience, payload) {
|
|
47
|
+
return pushForSession(push, audience.ownerId, payload, audience.isMultiUser, audience.listAdmins);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// The banner follows the push audience: the owner, or the admins for an
|
|
51
|
+
// ownerless session in multi-user mode, so no one else reads its title.
|
|
52
|
+
function bannerData(type, norm, raw, audience, data) {
|
|
53
|
+
var aud = audienceFor(audience.ownerId, audience.isMultiUser, audience.listAdmins);
|
|
54
|
+
// The audience and the target are applied last: emitter content cannot
|
|
55
|
+
// widen or redirect who reads the banner, or where it points.
|
|
56
|
+
return Object.assign({}, data, {
|
|
57
|
+
ownerId: audience.ownerId,
|
|
58
|
+
targetUserId: aud.kind === "owner" ? aud.userIds[0] : null,
|
|
59
|
+
targetUserIds: aud.kind === "admins" ? aud.userIds : null,
|
|
60
|
+
slug: norm.slug,
|
|
61
|
+
sessionId: raw.sessionId,
|
|
62
|
+
requestId: norm.requestId,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Shows (or replaces) the notification for a target, on every surface the
|
|
68
|
+
* type registers.
|
|
69
|
+
* @param {string} typeName a registered type.
|
|
70
|
+
* @param {{slug: ?string, sessionId: ?(number|string), requestId: ?string}} t
|
|
71
|
+
* @param {{push: ?object, banner: ?object}|object} content what each surface
|
|
72
|
+
* shows. push: the push text ({title, body}, or what the type's format
|
|
73
|
+
* reads); banner: the in-app store's own content (the target and audience
|
|
74
|
+
* fields are added here). A surface without content is not delivered to;
|
|
75
|
+
* the core picks the surfaces from the type's registration, never the
|
|
76
|
+
* emitter. A content naming neither key is the push content itself.
|
|
77
|
+
* @param {{ownerId: ?string, isMultiUser: ?function, listAdmins: ?function}} [audience]
|
|
78
|
+
* who the session belongs to; the push audience rules (lib/push-audience.js)
|
|
79
|
+
* decide the recipients from it, for the push and the banner alike. The
|
|
80
|
+
* mode and admin readers are optional.
|
|
81
|
+
* @returns {?object} the payload built, or null when nothing was delivered.
|
|
82
|
+
*/
|
|
83
|
+
function notify(typeName, t, content, audience) {
|
|
84
|
+
var type = registry.get(typeName);
|
|
85
|
+
if (!type) throw new Error("unregistered notification type: " + typeName);
|
|
86
|
+
var norm = target.makeTarget(t);
|
|
87
|
+
if (!norm) throw new Error("invalid notification target for " + typeName);
|
|
88
|
+
if (type.collapse === "request" && !norm.requestId) {
|
|
89
|
+
throw new Error("notification type " + typeName + " needs a requestId");
|
|
90
|
+
}
|
|
91
|
+
var bySurface = perSurface(content);
|
|
92
|
+
var aud = audienceOf(audience);
|
|
93
|
+
var payload = payloads.buildPayload(type, norm, bySurface.push || bySurface.banner);
|
|
94
|
+
var delivered = false;
|
|
95
|
+
if (push && bySurface.push) {
|
|
96
|
+
deliver(aud, payload);
|
|
97
|
+
delivered = true;
|
|
98
|
+
}
|
|
99
|
+
if (inApp && type.banner && bySurface.banner) {
|
|
100
|
+
inApp.show(typeName, bannerData(type, norm, t, aud, bySurface.banner), {
|
|
101
|
+
key: payload.tag,
|
|
102
|
+
collapse: type.collapse,
|
|
103
|
+
persistent: type.persistent,
|
|
104
|
+
liveInState: type.liveInState,
|
|
105
|
+
});
|
|
106
|
+
delivered = true;
|
|
107
|
+
}
|
|
108
|
+
return delivered ? payload : null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// The close-on-resolve types the targets' resolve reaches, grouped by
|
|
112
|
+
// notification category: a close push follows the preference of the
|
|
113
|
+
// notification it closes, and types of one category share a preference, so
|
|
114
|
+
// one push per category carries all its tags.
|
|
115
|
+
function closeGroups(norms) {
|
|
116
|
+
var groups = Object.create(null);
|
|
117
|
+
var order = [];
|
|
118
|
+
var bannerKeys = [];
|
|
119
|
+
norms.forEach(function (norm) {
|
|
120
|
+
registry.names().forEach(function (name) {
|
|
121
|
+
var type = registry.get(name);
|
|
122
|
+
if (!type.closeOnResolve) return;
|
|
123
|
+
// With a request named, that prompt's request-level notifications;
|
|
124
|
+
// without one, the session-level ones.
|
|
125
|
+
if ((type.collapse === "request") !== !!norm.requestId) return;
|
|
126
|
+
var key = type.category || name;
|
|
127
|
+
if (!groups[key]) {
|
|
128
|
+
groups[key] = { type: type, tags: [] };
|
|
129
|
+
order.push(key);
|
|
130
|
+
}
|
|
131
|
+
var tag = payloads.tagFor(type, norm);
|
|
132
|
+
if (groups[key].tags.indexOf(tag) === -1) groups[key].tags.push(tag);
|
|
133
|
+
if (type.banner && bannerKeys.indexOf(tag) === -1) bannerKeys.push(tag);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
return { groups: order.map(function (key) { return groups[key]; }), bannerKeys: bannerKeys };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Closes what is shown for a target, on every surface and every device in
|
|
141
|
+
* its audience: the push on each device, and the banner in every page.
|
|
142
|
+
* @param {object|object[]} what a target { slug, sessionId, requestId }, or a
|
|
143
|
+
* list of them (a prompt known by more than one id). With a requestId it
|
|
144
|
+
* closes that prompt's request-level notifications; without one (a session
|
|
145
|
+
* whose prompts all settled) it closes the session-level ones. A target
|
|
146
|
+
* without a session id closes no push (a request-only target retires the
|
|
147
|
+
* banners of that request).
|
|
148
|
+
* @param {{ownerId: ?string, isMultiUser: ?function, listAdmins: ?function}} [audience]
|
|
149
|
+
* the same audience the notification was sent to.
|
|
150
|
+
* @returns {number} how many close pushes were sent.
|
|
151
|
+
*/
|
|
152
|
+
function resolve(what, audience) {
|
|
153
|
+
var all = [].concat(what).map(target.makeTarget).filter(Boolean);
|
|
154
|
+
var norms = all.filter(function (n) { return n.sessionId; });
|
|
155
|
+
var closing = closeGroups(norms);
|
|
156
|
+
if (inApp && closing.bannerKeys.length) inApp.dismissKeys(closing.bannerKeys);
|
|
157
|
+
// An answer that arrives for a prompt no session claims (a stale one) names
|
|
158
|
+
// only its request: the banners of that request are retired, in its project.
|
|
159
|
+
if (inApp && typeof inApp.dismissRequest === "function") {
|
|
160
|
+
all.filter(function (n) { return !n.sessionId && n.requestId; }).forEach(function (n) {
|
|
161
|
+
inApp.dismissRequest(n.slug, n.requestId);
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
if (!push) return 0;
|
|
165
|
+
var aud = audienceOf(audience);
|
|
166
|
+
closing.groups.forEach(function (g) {
|
|
167
|
+
deliver(aud, payloads.buildClose(g.type, g.tags));
|
|
168
|
+
});
|
|
169
|
+
return closing.groups.length;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return { notify: notify, resolve: resolve };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
module.exports = { createNotifyCore: createNotifyCore };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// The notification core's public surface. notifyFor(push, inApp) returns the
|
|
4
|
+
// core bound to a push module and the in-app banner store.
|
|
5
|
+
|
|
6
|
+
var { createTypeRegistry } = require("./type-registry");
|
|
7
|
+
var { registerBuiltins } = require("./types");
|
|
8
|
+
var { createNotifyCore } = require("./core");
|
|
9
|
+
var target = require("./target");
|
|
10
|
+
|
|
11
|
+
var registry = registerBuiltins(createTypeRegistry());
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The core is stateless (every tag derives from the target), so a notification
|
|
15
|
+
* raised through one instance is closed through another.
|
|
16
|
+
* @param {?object} push the push module; null for no push.
|
|
17
|
+
* @param {?object} [inApp] the in-app banner store (lib/project-notifications.js).
|
|
18
|
+
*/
|
|
19
|
+
function notifyFor(push, inApp) {
|
|
20
|
+
return createNotifyCore({ registry: registry, push: push || null, inApp: inApp || null });
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = {
|
|
24
|
+
notifyFor: notifyFor,
|
|
25
|
+
registerType: registry.register,
|
|
26
|
+
getType: registry.get,
|
|
27
|
+
targetKey: target.targetKey,
|
|
28
|
+
makeTarget: target.makeTarget,
|
|
29
|
+
createNotifyCore: createNotifyCore,
|
|
30
|
+
createTypeRegistry: createTypeRegistry,
|
|
31
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// The record the page checks a shown prompt notification against: which
|
|
4
|
+
// prompts of a project await the operator right now, as seen by one viewer.
|
|
5
|
+
// Ids only, never prompt content. It is what makes every persistent
|
|
6
|
+
// close-on-resolve type verifiable on the page, including those with no banner
|
|
7
|
+
// (ask_user, elicitation).
|
|
8
|
+
|
|
9
|
+
var { audienceFor } = require("../push-audience");
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {object} deps
|
|
13
|
+
* @param {string} deps.slug the project the sessions belong to.
|
|
14
|
+
* @param {{forEach: function}} deps.sessions the project's sessions.
|
|
15
|
+
* @param {{pendingInfo: function}} deps.prompts the project's prompt registry.
|
|
16
|
+
* @param {?string} deps.viewerId the connecting user, if any.
|
|
17
|
+
* @param {function(): boolean} [deps.isMultiUser] see push-audience.audienceFor.
|
|
18
|
+
* @param {function(): string[]} [deps.listAdmins] see push-audience.audienceFor.
|
|
19
|
+
* @returns {{slug: string, at: number, items: Array<{sessionId: string, requestId: string, toolUseId: ?string}>}}
|
|
20
|
+
* at is the server time of the snapshot: a notification sent after it is not
|
|
21
|
+
* judged by it.
|
|
22
|
+
*/
|
|
23
|
+
function snapshotLivePrompts(deps) {
|
|
24
|
+
var items = [];
|
|
25
|
+
deps.sessions.forEach(function (session) {
|
|
26
|
+
// Only prompts the viewer would have been pushed.
|
|
27
|
+
var audience = audienceFor(session.ownerId || null, deps.isMultiUser, deps.listAdmins);
|
|
28
|
+
if (audience.kind !== "broadcast" && audience.userIds.indexOf(deps.viewerId) === -1) return;
|
|
29
|
+
deps.prompts.pendingInfo(session).forEach(function (info) {
|
|
30
|
+
items.push({ sessionId: String(session.localId), requestId: info.requestId, toolUseId: info.toolUseId || null });
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
return { slug: deps.slug, at: Date.now(), items: items };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = { snapshotLivePrompts: snapshotLivePrompts };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Builds the wire payloads. The tag is derived here and nowhere else: from the
|
|
4
|
+
// type's collapse rule and the target key, so two notifications for the same
|
|
5
|
+
// target share a tag (the platform replaces, never stacks) and different
|
|
6
|
+
// targets never do.
|
|
7
|
+
|
|
8
|
+
var target = require("./target");
|
|
9
|
+
|
|
10
|
+
// Marks a payload the generic service worker handles from its own fields. A
|
|
11
|
+
// payload without it is a legacy one, shown as before.
|
|
12
|
+
var CORE_MARK = 1;
|
|
13
|
+
var CLOSE_TYPE = "notify_close";
|
|
14
|
+
|
|
15
|
+
function tagFor(type, t) {
|
|
16
|
+
var key = type.collapse === "request" ? target.targetKey(t) : target.sessionKey(t);
|
|
17
|
+
return "n:" + type.name + ":" + key;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {object} type a registered type.
|
|
22
|
+
* @param {object} t a normalised target.
|
|
23
|
+
* @param {{title: string, body: string}} content
|
|
24
|
+
*/
|
|
25
|
+
function buildPayload(type, t, content) {
|
|
26
|
+
var text = type.format(content || {});
|
|
27
|
+
return {
|
|
28
|
+
core: CORE_MARK,
|
|
29
|
+
type: type.name,
|
|
30
|
+
title: text.title,
|
|
31
|
+
body: text.body,
|
|
32
|
+
target: t,
|
|
33
|
+
tag: tagFor(type, t),
|
|
34
|
+
collapse: type.collapse,
|
|
35
|
+
url: target.urlForTarget(t),
|
|
36
|
+
requireInteraction: type.persistent,
|
|
37
|
+
renotify: type.renotify,
|
|
38
|
+
liveInState: type.liveInState,
|
|
39
|
+
// Server time of the send: the page's reconcile only judges a notification
|
|
40
|
+
// sent before the state snapshot it checks against.
|
|
41
|
+
sentAt: Date.now(),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The push that closes a shown notification. It names the type it closes so
|
|
47
|
+
* the push filter applies that type's preference: a device never told of the
|
|
48
|
+
* notification is not told of its closing.
|
|
49
|
+
* @param {object} type a type of the category being closed.
|
|
50
|
+
* @param {string|string[]} tags the tag or tags to close.
|
|
51
|
+
*/
|
|
52
|
+
function buildClose(type, tags) {
|
|
53
|
+
return {
|
|
54
|
+
core: CORE_MARK,
|
|
55
|
+
type: CLOSE_TYPE,
|
|
56
|
+
closes: type.name,
|
|
57
|
+
tags: [].concat(tags),
|
|
58
|
+
title: "Clagentic: Console",
|
|
59
|
+
body: "No longer waiting for input",
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = { tagFor: tagFor, buildPayload: buildPayload, buildClose: buildClose, CLOSE_TYPE: CLOSE_TYPE };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// The notification types the core knows. A new type is one register() call:
|
|
4
|
+
// the core derives its tag, payload and close behaviour from what is declared
|
|
5
|
+
// here, so no emitter carries any of it.
|
|
6
|
+
|
|
7
|
+
var COLLAPSE = ["request", "session"];
|
|
8
|
+
|
|
9
|
+
function defaultFormat(content) {
|
|
10
|
+
return { title: content.title, body: content.body };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function createTypeRegistry() {
|
|
14
|
+
var types = Object.create(null);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {string} name the push payload type.
|
|
18
|
+
* @param {object} def
|
|
19
|
+
* @param {string} def.category the notification-prefs category (documentation
|
|
20
|
+
* of what lib/notification-prefs.js filters the type under).
|
|
21
|
+
* @param {"request"|"session"} def.collapse "request": one notification per
|
|
22
|
+
* prompt; "session": one per session, a repeat replaces it.
|
|
23
|
+
* @param {boolean} def.persistent stays on screen until acted on.
|
|
24
|
+
* @param {boolean} [def.renotify] a replacement alerts again.
|
|
25
|
+
* @param {boolean} [def.closeOnResolve] the notification is closed when its
|
|
26
|
+
* target resolves (default true).
|
|
27
|
+
* @param {boolean} [def.banner] the type also shows as an in-app banner,
|
|
28
|
+
* closed with the notification (default false).
|
|
29
|
+
* @param {boolean} [def.liveInState] the page can verify the notification
|
|
30
|
+
* against notifications_state (default false).
|
|
31
|
+
* @param {function(object): {title: string, body: string}} [def.format]
|
|
32
|
+
*/
|
|
33
|
+
function register(name, def) {
|
|
34
|
+
if (typeof name !== "string" || !name) throw new Error("notification type needs a name");
|
|
35
|
+
if (types[name]) throw new Error("notification type already registered: " + name);
|
|
36
|
+
if (!def || COLLAPSE.indexOf(def.collapse) === -1) {
|
|
37
|
+
throw new Error("notification type " + name + " needs collapse: request|session");
|
|
38
|
+
}
|
|
39
|
+
types[name] = Object.freeze({
|
|
40
|
+
name: name,
|
|
41
|
+
category: def.category || null,
|
|
42
|
+
collapse: def.collapse,
|
|
43
|
+
persistent: def.persistent === true,
|
|
44
|
+
renotify: def.renotify === true,
|
|
45
|
+
closeOnResolve: def.closeOnResolve !== false,
|
|
46
|
+
banner: def.banner === true,
|
|
47
|
+
liveInState: def.liveInState === true,
|
|
48
|
+
format: typeof def.format === "function" ? def.format : defaultFormat,
|
|
49
|
+
});
|
|
50
|
+
return types[name];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function get(name) {
|
|
54
|
+
return typeof name === "string" && Object.prototype.hasOwnProperty.call(types, name) ? types[name] : null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function names() {
|
|
58
|
+
return Object.keys(types);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return { register: register, get: get, names: names };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = { createTypeRegistry: createTypeRegistry, COLLAPSE: COLLAPSE };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// The notification types registered with the core. Adding a type is adding an
|
|
4
|
+
// entry here; emitters then call notify()/resolve() with its name.
|
|
5
|
+
|
|
6
|
+
var PROMPT_TYPES = {
|
|
7
|
+
permission_request: { category: "approval", collapse: "request", persistent: true, banner: true, liveInState: true },
|
|
8
|
+
permission_reminder: { category: "approval_reminder", collapse: "session", persistent: true, renotify: true, banner: true, liveInState: true },
|
|
9
|
+
ask_user: { category: "approval", collapse: "request", persistent: true, liveInState: true },
|
|
10
|
+
elicitation: { category: "approval", collapse: "request", persistent: true, liveInState: true },
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
function registerBuiltins(registry) {
|
|
14
|
+
Object.keys(PROMPT_TYPES).forEach(function (name) {
|
|
15
|
+
registry.register(name, PROMPT_TYPES[name]);
|
|
16
|
+
});
|
|
17
|
+
return registry;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = { registerBuiltins: registerBuiltins, PROMPT_TYPES: PROMPT_TYPES };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Open browser sockets per project, for the activity diagnostics. A tab holds
|
|
4
|
+
// one socket, so a project showing more sockets than tabs is the sign of an
|
|
5
|
+
// orphaned connection. The count is the daemon's own and holds no connection
|
|
6
|
+
// detail: only the project slug and a number.
|
|
7
|
+
|
|
8
|
+
var counts = new Map();
|
|
9
|
+
|
|
10
|
+
function opened(slug) {
|
|
11
|
+
counts.set(slug, (counts.get(slug) || 0) + 1);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function closed(slug) {
|
|
15
|
+
var n = (counts.get(slug) || 0) - 1;
|
|
16
|
+
if (n > 0) counts.set(slug, n);
|
|
17
|
+
else counts.delete(slug);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// { slug: openSocketCount } for every project with at least one open socket.
|
|
21
|
+
function snapshot() {
|
|
22
|
+
var out = {};
|
|
23
|
+
counts.forEach(function (n, slug) { out[slug] = n; });
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = { opened: opened, closed: closed, snapshot: snapshot };
|