@agimon-ai/po-wake 0.0.1-alpha.4 → 0.0.1-alpha.5
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 +7 -6
- package/dist/extensions/server.cjs +350 -152
- package/dist/extensions/server.cjs.map +1 -1
- package/dist/extensions/server.mjs +350 -152
- package/dist/extensions/server.mjs.map +1 -1
- package/dist/index.cjs +6 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.mts +10 -2
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +7 -1
- package/dist/index.mjs.map +1 -0
- package/dist/po-runtime-identity.json +1 -1
- package/dist/wakeRecords-CfkvJoto.cjs.map +1 -1
- package/dist/{wakeRecords-BKby8VQs.d.mts → wakeRecords-DCV0XXgo.d.mts} +4 -2
- package/dist/wakeRecords-DCV0XXgo.d.mts.map +1 -0
- package/dist/wakeRecords-mOiBrclj.mjs.map +1 -1
- package/dist/wakeRecords.d.mts +1 -1
- package/llms.txt +1 -1
- package/package.json +6 -5
- package/src/prompts/help-how-to-wake/SKILL.md +3 -3
- package/dist/wakeRecords-BKby8VQs.d.mts.map +0 -1
package/README.md
CHANGED
|
@@ -10,14 +10,15 @@ Add `@agimon-ai/po-wake` to the appropriate selected package list in `.po/preset
|
|
|
10
10
|
|
|
11
11
|
## How it works
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
po-wake owns the durable timer for bot sessions. It works without Calendar or Task. Deadlines are stored in the host's shared Po database (`po/db`). A dormant or evicted bot can wake without anyone reopening it. A missed check-in runs once after the host returns.
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
- A record that is **due** (Task: a routine under its daily limit) schedules a routine wake through a provider that can schedule (Calendar). It fires after 15 quiet minutes; a new turn withdraws it. After one fires, no new routine wake is scheduled until a routine makes progress or the day rolls over, and the routines stop covering, so the reminder applies again.
|
|
17
|
-
- With nothing covering, the bot gets the reminder, or a delayed check-in through the provider when it was just reminded.
|
|
18
|
-
- A provider that cannot answer makes the outcome unavailable, never empty.
|
|
15
|
+
Providers can register standing work through `po/wake-records`:
|
|
19
16
|
|
|
20
|
-
|
|
17
|
+
- A record that **covers** suppresses the reminder, not the next check.
|
|
18
|
+
- A record that is **due** can wake the bot after 15 quiet minutes. A new turn withdraws that wake. A routine does not wake the bot again until a record makes progress or the day rolls over.
|
|
19
|
+
- A provider that cannot answer makes its coverage unknown. po-wake retries rather than treating unknown coverage as empty.
|
|
20
|
+
|
|
21
|
+
Only sessions marked as bots are woken. The default check-in does not assume any optional plugin is installed.
|
|
21
22
|
|
|
22
23
|
```text
|
|
23
24
|
/wake
|
|
@@ -5,9 +5,63 @@ Object.defineProperties(exports, {
|
|
|
5
5
|
const require_wakeRecords = require("../wakeRecords-CfkvJoto.cjs");
|
|
6
6
|
let _agimon_ai_po_core_serverPlugin = require("@agimon-ai/po-core/serverPlugin");
|
|
7
7
|
let _agimon_ai_po_core_extensionFile = require("@agimon-ai/po-core/extensionFile");
|
|
8
|
+
let _agimon_ai_po_db = require("@agimon-ai/po-db");
|
|
8
9
|
let _agimon_ai_po_profile = require("@agimon-ai/po-profile");
|
|
9
10
|
let _agimon_ai_po_core_headless = require("@agimon-ai/po-core/headless");
|
|
10
11
|
let _agimon_ai_po_core_help = require("@agimon-ai/po-core/help");
|
|
12
|
+
//#region src/services/wakeAlarm/index.ts
|
|
13
|
+
async function withWakeAlarm(_context, start) {
|
|
14
|
+
let timer;
|
|
15
|
+
let handle;
|
|
16
|
+
return start({
|
|
17
|
+
async arm(at) {
|
|
18
|
+
clearTimeout(timer);
|
|
19
|
+
if (at !== void 0) {
|
|
20
|
+
timer = setTimeout(() => void handle?.(), Math.max(0, Math.min(at - Date.now(), 2147483647)));
|
|
21
|
+
timer.unref();
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
onAlarm(handler) {
|
|
25
|
+
handle = handler;
|
|
26
|
+
return () => {
|
|
27
|
+
handle = void 0;
|
|
28
|
+
clearTimeout(timer);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/services/wakeDeadlines/index.ts
|
|
35
|
+
const SELECT = "SELECT workspace_id AS workspaceId, session_id AS sessionId, slot, at, request_id AS requestId FROM po_wake_deadlines";
|
|
36
|
+
const KEY = "workspace_id = ? AND session_id = ? AND slot = ?";
|
|
37
|
+
const key = (owner, slot) => [
|
|
38
|
+
owner.workspaceId,
|
|
39
|
+
owner.sessionId,
|
|
40
|
+
slot
|
|
41
|
+
];
|
|
42
|
+
const requestId = () => `wake-${crypto.randomUUID()}`;
|
|
43
|
+
async function openWakeDeadlines(db) {
|
|
44
|
+
await db.migrate("po_wake", [{
|
|
45
|
+
version: 1,
|
|
46
|
+
statements: [`CREATE TABLE po_wake_deadlines (
|
|
47
|
+
workspace_id TEXT NOT NULL, session_id TEXT NOT NULL, slot TEXT NOT NULL CHECK (slot IN ('reminder', 'routine')),
|
|
48
|
+
at INTEGER NOT NULL, request_id TEXT NOT NULL, PRIMARY KEY (workspace_id, session_id, slot)
|
|
49
|
+
)`, "CREATE INDEX po_wake_deadlines_at ON po_wake_deadlines (at)"]
|
|
50
|
+
}]);
|
|
51
|
+
const { database } = db;
|
|
52
|
+
return {
|
|
53
|
+
async schedule(owner, slot, at, preserve = false) {
|
|
54
|
+
await database.run(`INSERT INTO po_wake_deadlines (workspace_id, session_id, slot, at, request_id) VALUES (?, ?, ?, ?, ?)
|
|
55
|
+
ON CONFLICT (workspace_id, session_id, slot) ${preserve ? "DO NOTHING" : "DO UPDATE SET at = excluded.at, request_id = excluded.request_id"}`, ...key(owner, slot), at, requestId());
|
|
56
|
+
},
|
|
57
|
+
cancel: (owner, slot) => database.run(`DELETE FROM po_wake_deadlines WHERE ${KEY}`, ...key(owner, slot)),
|
|
58
|
+
due: (at) => database.all(`${SELECT} WHERE at <= ? ORDER BY at, slot`, at),
|
|
59
|
+
next: async () => (await database.get(`${SELECT} ORDER BY at LIMIT 1`))?.at,
|
|
60
|
+
current: async (row) => !!await database.get(`${SELECT} WHERE ${KEY} AND request_id = ?`, ...key(row, row.slot), row.requestId),
|
|
61
|
+
complete: (row, at, retry = false) => at === void 0 ? database.run(`DELETE FROM po_wake_deadlines WHERE ${KEY} AND request_id = ?`, ...key(row, row.slot), row.requestId) : database.run(`UPDATE po_wake_deadlines SET at = ?, request_id = ? WHERE ${KEY} AND request_id = ?`, at, retry ? row.requestId : requestId(), ...key(row, row.slot), row.requestId)
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
11
65
|
//#region src/services/wakeRoot/type.ts
|
|
12
66
|
const WAKE_ROOT_SERVICE = (0, _agimon_ai_po_core_serverPlugin.defineServiceContract)("po/wake-root", { scope: "host" });
|
|
13
67
|
function readWakeRoot(context) {
|
|
@@ -15,33 +69,149 @@ function readWakeRoot(context) {
|
|
|
15
69
|
}
|
|
16
70
|
//#endregion
|
|
17
71
|
//#region src/services/wakeRoot/index.ts
|
|
18
|
-
const HOST_ONLY = "Bot wake requires the
|
|
19
|
-
|
|
20
|
-
function createWakeRootValue(scopes, onError) {
|
|
72
|
+
const HOST_ONLY = "Bot wake requires the host session directory.";
|
|
73
|
+
const RETRY_MS = 6e4;
|
|
74
|
+
function createWakeRootValue(scopes, onError, timer) {
|
|
21
75
|
const attached = /* @__PURE__ */ new Map();
|
|
76
|
+
let disposed = false;
|
|
77
|
+
let draining;
|
|
78
|
+
let arming = Promise.resolve();
|
|
79
|
+
const rearm = () => {
|
|
80
|
+
if (!timer || disposed) return Promise.resolve();
|
|
81
|
+
const next = arming.catch(() => void 0).then(async () => {
|
|
82
|
+
const at = await timer.store.next();
|
|
83
|
+
if (!disposed) await timer.alarm.arm(at);
|
|
84
|
+
});
|
|
85
|
+
arming = next;
|
|
86
|
+
return next;
|
|
87
|
+
};
|
|
88
|
+
const deliver = async (row) => {
|
|
89
|
+
if (!timer) return;
|
|
90
|
+
const { sessions, store } = timer;
|
|
91
|
+
const current = async () => !disposed && await store.current(row) && !disposed;
|
|
92
|
+
if (!await current()) return;
|
|
93
|
+
const live = () => scopes().find((entry) => entry.sessionId === row.sessionId && entry.workspaceId === row.workspaceId);
|
|
94
|
+
let revived = false;
|
|
95
|
+
if (!live()) {
|
|
96
|
+
if (scopes().some((entry) => entry.sessionId === row.sessionId)) return store.complete(row);
|
|
97
|
+
const saved = sessions.dormant?.().find((entry) => entry.sessionId === row.sessionId && entry.workspaceId === row.workspaceId);
|
|
98
|
+
if (!saved || !sessions.revive) {
|
|
99
|
+
if (!saved && sessions.directoryReady?.(row.workspaceId)) return store.complete(row);
|
|
100
|
+
return store.complete(row, Date.now() + RETRY_MS, true);
|
|
101
|
+
}
|
|
102
|
+
if (!await current()) return;
|
|
103
|
+
revived = await sessions.revive(row.sessionId, { automatic: true }) === "revived" || !!live();
|
|
104
|
+
}
|
|
105
|
+
const release = async () => {
|
|
106
|
+
if (revived) await sessions.release?.(row.sessionId, { onlyIfIdle: true });
|
|
107
|
+
};
|
|
108
|
+
let settled;
|
|
109
|
+
try {
|
|
110
|
+
const target = live();
|
|
111
|
+
if (target && !target.bot) return await store.complete(row);
|
|
112
|
+
const due = attached.get(row.sessionId)?.due;
|
|
113
|
+
const state = target && await sessions.readExecutionState?.(row.sessionId);
|
|
114
|
+
if (!target || !due || state && (!state.isIdle || state.hasPendingMessages)) return await store.complete(row, Date.now() + RETRY_MS, true);
|
|
115
|
+
if (!await current()) return;
|
|
116
|
+
const result = await due(row, current);
|
|
117
|
+
settled = result.settled;
|
|
118
|
+
await store.complete(row, result.retry ? Date.now() + RETRY_MS : result.nextAt, result.retry);
|
|
119
|
+
} finally {
|
|
120
|
+
if (settled) settled.catch(onError).then(release).catch(onError);
|
|
121
|
+
else await release();
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
const drain = () => {
|
|
125
|
+
if (!timer || disposed) return Promise.resolve();
|
|
126
|
+
const { store, alarm } = timer;
|
|
127
|
+
draining ??= (async () => {
|
|
128
|
+
try {
|
|
129
|
+
for (const row of await store.due(Date.now())) {
|
|
130
|
+
if (disposed) break;
|
|
131
|
+
try {
|
|
132
|
+
await deliver(row);
|
|
133
|
+
} catch (error) {
|
|
134
|
+
onError(error);
|
|
135
|
+
await store.complete(row, Date.now() + RETRY_MS, true);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
await rearm();
|
|
139
|
+
} catch (error) {
|
|
140
|
+
onError(error);
|
|
141
|
+
if (!disposed) await alarm.arm(Date.now() + RETRY_MS).catch(onError);
|
|
142
|
+
}
|
|
143
|
+
})().finally(() => {
|
|
144
|
+
draining = void 0;
|
|
145
|
+
});
|
|
146
|
+
return draining;
|
|
147
|
+
};
|
|
22
148
|
return {
|
|
23
|
-
botSession: ({ sessionId, workspaceId }) =>
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
149
|
+
botSession: ({ sessionId, workspaceId }) => scopes().find((entry) => entry.sessionId === sessionId && entry.workspaceId === workspaceId)?.bot,
|
|
150
|
+
attach(sessionId, recover, due) {
|
|
151
|
+
const callbacks = {
|
|
152
|
+
recover,
|
|
153
|
+
due
|
|
154
|
+
};
|
|
155
|
+
attached.set(sessionId, callbacks);
|
|
29
156
|
return () => {
|
|
30
|
-
if (attached.get(sessionId) ===
|
|
157
|
+
if (attached.get(sessionId) === callbacks) attached.delete(sessionId);
|
|
31
158
|
};
|
|
32
159
|
},
|
|
33
160
|
sessionAdded({ sessionId, workspaceId }) {
|
|
34
161
|
if (!scopes().some((entry) => entry.sessionId === sessionId && entry.workspaceId === workspaceId && entry.bot)) return;
|
|
35
|
-
attached.get(sessionId)?.().catch(onError);
|
|
162
|
+
attached.get(sessionId)?.recover().then(rearm).catch(onError);
|
|
163
|
+
},
|
|
164
|
+
async schedule(owner, slot, at, preserve) {
|
|
165
|
+
if (disposed) return;
|
|
166
|
+
await timer?.store.schedule(owner, slot, at, preserve);
|
|
167
|
+
await rearm();
|
|
168
|
+
},
|
|
169
|
+
async cancel(owner, slot) {
|
|
170
|
+
if (disposed) return;
|
|
171
|
+
await timer?.store.cancel(owner, slot);
|
|
172
|
+
await rearm();
|
|
173
|
+
},
|
|
174
|
+
async start() {
|
|
175
|
+
const unsubscribe = timer?.alarm.onAlarm(drain);
|
|
176
|
+
const stop = () => {
|
|
177
|
+
disposed = true;
|
|
178
|
+
unsubscribe?.();
|
|
179
|
+
};
|
|
180
|
+
try {
|
|
181
|
+
await rearm();
|
|
182
|
+
return stop;
|
|
183
|
+
} catch (error) {
|
|
184
|
+
stop();
|
|
185
|
+
throw error;
|
|
186
|
+
}
|
|
36
187
|
}
|
|
37
188
|
};
|
|
38
189
|
}
|
|
39
|
-
function wakeRootForHost(host) {
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
190
|
+
async function wakeRootForHost(host, db, alarm) {
|
|
191
|
+
const sessions = host.sessionService;
|
|
192
|
+
const scopes = sessions?.scopes?.bind(sessions);
|
|
193
|
+
if (host.scope !== "host" || !sessions || !scopes) throw new Error(HOST_ONLY);
|
|
194
|
+
return createWakeRootValue(scopes, (error) => host.onNotice(`Wake: ${error instanceof Error ? error.message : String(error)}`), {
|
|
195
|
+
store: await openWakeDeadlines(db),
|
|
196
|
+
sessions,
|
|
197
|
+
alarm
|
|
198
|
+
});
|
|
43
199
|
}
|
|
44
200
|
function createWakeRoot(plugin, scope) {
|
|
201
|
+
const activateRoot = async (host, db, alarm, publish) => {
|
|
202
|
+
const root = await wakeRootForHost(host, db, alarm);
|
|
203
|
+
const withdraw = await publish(root);
|
|
204
|
+
try {
|
|
205
|
+
const stop = await root.start();
|
|
206
|
+
return async () => {
|
|
207
|
+
stop();
|
|
208
|
+
await withdraw();
|
|
209
|
+
};
|
|
210
|
+
} catch (error) {
|
|
211
|
+
await withdraw();
|
|
212
|
+
throw error;
|
|
213
|
+
}
|
|
214
|
+
};
|
|
45
215
|
return {
|
|
46
216
|
value: void 0,
|
|
47
217
|
async onStart() {
|
|
@@ -49,13 +219,19 @@ function createWakeRoot(plugin, scope) {
|
|
|
49
219
|
if ((0, _agimon_ai_po_core_serverPlugin.readPoHostServices)(plugin.context)) await plugin.context.inject([_agimon_ai_po_core_serverPlugin.PO_HOST_SERVICES_SERVICE], (context) => (0, _agimon_ai_po_core_serverPlugin.readPoHostServices)(context)?.register(plugin.host, {
|
|
50
220
|
packageName: "@agimon-ai/po-wake",
|
|
51
221
|
async activate(activation) {
|
|
52
|
-
|
|
222
|
+
const fiber = activation.context.inject([_agimon_ai_po_db.PO_DB_SERVICE], async (bound) => {
|
|
223
|
+
const db = (0, _agimon_ai_po_db.requirePoDb)(bound);
|
|
224
|
+
return withWakeAlarm(bound, (alarm) => activateRoot(activation.host.context, db, alarm, (root) => activation.provide(WAKE_ROOT_SERVICE, root)));
|
|
225
|
+
});
|
|
226
|
+
await fiber;
|
|
227
|
+
return () => fiber.dispose();
|
|
53
228
|
}
|
|
54
229
|
}));
|
|
55
|
-
else if (scope === "host") {
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
230
|
+
else if (scope === "host") await plugin.context.inject([_agimon_ai_po_core_serverPlugin.PO_SERVER_SCOPE_SERVICE, _agimon_ai_po_db.PO_DB_SERVICE], async (bound) => {
|
|
231
|
+
const db = (0, _agimon_ai_po_db.requirePoDb)(bound);
|
|
232
|
+
const serverScope = (0, _agimon_ai_po_core_serverPlugin.requirePoServerScope)(bound);
|
|
233
|
+
return withWakeAlarm(bound, (alarm) => activateRoot(plugin.host.context, db, alarm, (root) => serverScope.provide(WAKE_ROOT_SERVICE, root)));
|
|
234
|
+
});
|
|
59
235
|
}
|
|
60
236
|
};
|
|
61
237
|
}
|
|
@@ -66,14 +242,17 @@ var root_server_default$2 = (0, _agimon_ai_po_core_extensionFile.defineRoot)((co
|
|
|
66
242
|
//#region src/extensions/workspaces/(backend)/root.server.ts
|
|
67
243
|
var root_server_default$1 = (0, _agimon_ai_po_core_extensionFile.defineRoot)((context) => createWakeRoot(context, "workspace"));
|
|
68
244
|
//#endregion
|
|
245
|
+
//#region src/constants/wake.ts
|
|
246
|
+
const COMMAND_DESCRIPTION = "List the standing work that decides whether this bot is woken";
|
|
247
|
+
const WAKE_NUDGE_TYPE = "wake-nudge";
|
|
248
|
+
const WAKE_INTENT_TYPE = "po.wake-intent";
|
|
249
|
+
const WAKE_RESULT_TYPE = "po.wake-result";
|
|
250
|
+
const LEGACY_WAKE_INTENT_TYPE = "po.calendar-wake-intent";
|
|
251
|
+
const LEGACY_WAKE_RESULT_TYPE = "po.calendar-wake-result";
|
|
252
|
+
//#endregion
|
|
69
253
|
//#region src/services/wake/index.ts
|
|
70
|
-
const WAKE_NUDGE_TYPE = "calendar-wake-nudge";
|
|
71
|
-
const WAKE_INTENT_TYPE = "po.calendar-wake-intent";
|
|
72
|
-
const WAKE_RESULT_TYPE = "po.calendar-wake-result";
|
|
73
|
-
/** The latest routine wake: `{ key, at }` while one is scheduled or has fired, `{}` once withdrawn. */
|
|
74
254
|
const WAKE_ROUTINE_TYPE = "po.wake-routine";
|
|
75
|
-
|
|
76
|
-
const PASSIVE_TYPES = /* @__PURE__ */ new Set(["calendar-outcome", WAKE_NUDGE_TYPE]);
|
|
255
|
+
const isNudge = (type) => type === "wake-nudge" || type === "calendar-wake-nudge";
|
|
77
256
|
const DEFAULT_WAKE_HORIZON_MS = 864e5;
|
|
78
257
|
/** A routine wake fires only after this long with no run. */
|
|
79
258
|
const ROUTINE_QUIET_MS = 9e5;
|
|
@@ -81,38 +260,24 @@ const MAX_RECORDS = 200;
|
|
|
81
260
|
const MAX_SUMMARY_LINES = 8;
|
|
82
261
|
const MAX_LINE = 120;
|
|
83
262
|
const MAX_ENTRY_SCAN = 400;
|
|
84
|
-
const BUILT_IN_REMINDER = "
|
|
263
|
+
const BUILT_IN_REMINDER = "Review your standing work and choose the next useful action. Respect existing commitments and approval requirements.";
|
|
85
264
|
const GUIDANCE = [
|
|
86
|
-
"Keep untimed routines such as social checks and prospecting on your task list as routine tasks, not on recurring events.",
|
|
87
|
-
"Put content briefs on a recurring prompt or an approved workflow.",
|
|
88
|
-
"Give a specific client follow-up a one-off event.",
|
|
89
265
|
"Do not duplicate or retime commitments listed here.",
|
|
90
|
-
"
|
|
91
|
-
"Use your profile playbook
|
|
92
|
-
"Reconcile saved
|
|
93
|
-
"If a plugin requests approval, keep working on unrelated tasks.",
|
|
94
|
-
"Save finished outcomes to profile memory before deleting completed tasks; do not clear the journal.",
|
|
95
|
-
"Schedule your own next check-in, including a follow-up for any workflow results you need to review."
|
|
266
|
+
"A wake does not authorize sending, publishing or other actions that need approval.",
|
|
267
|
+
"Use your profile playbook to choose the most important unblocked work.",
|
|
268
|
+
"Reconcile saved outcomes and approval receipts before retrying. Do not repeat an action with an uncertain outcome."
|
|
96
269
|
];
|
|
97
|
-
const ROUTINE_PROMPT = [
|
|
98
|
-
"Your routines are due:",
|
|
99
|
-
"",
|
|
100
|
-
"{lines}",
|
|
101
|
-
"",
|
|
102
|
-
"Pick the next one by judgement and your playbook. Mark it in_progress, do the work, then mark it completed.",
|
|
103
|
-
"Add any playbook routine missing from your task list."
|
|
104
|
-
].join("\n");
|
|
105
270
|
const record = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
106
271
|
const lineOf = (entry) => (entry.line ?? `- ${entry.title}`).slice(0, MAX_LINE);
|
|
107
|
-
/**
|
|
272
|
+
/** One failed provider makes the whole read unavailable. */
|
|
108
273
|
async function readWakeRecords(providers, at, horizonMs) {
|
|
109
274
|
try {
|
|
110
275
|
const lists = await Promise.all(providers.map((provider) => provider.records(at, horizonMs)));
|
|
111
|
-
const
|
|
276
|
+
const guidance = providers.flatMap((provider, index) => lists[index]?.length ? (provider.guidance ?? []).slice(0, 8) : []);
|
|
112
277
|
return {
|
|
113
278
|
status: "ok",
|
|
114
279
|
records: lists.flat(),
|
|
115
|
-
|
|
280
|
+
guidance: guidance.slice(0, 8).map((line) => line.slice(0, 240))
|
|
116
281
|
};
|
|
117
282
|
} catch (error) {
|
|
118
283
|
return {
|
|
@@ -122,9 +287,10 @@ async function readWakeRecords(providers, at, horizonMs) {
|
|
|
122
287
|
}
|
|
123
288
|
}
|
|
124
289
|
function createWake(options) {
|
|
125
|
-
const { signal } = options;
|
|
290
|
+
const { signal, timer } = options;
|
|
126
291
|
const now = options.now ?? Date.now;
|
|
127
292
|
const inflight = /* @__PURE__ */ new Set();
|
|
293
|
+
let routineAdmission = false;
|
|
128
294
|
const fenceOf = (profile) => `${profile?.generation ?? ""}|${JSON.stringify(profile?.settings?.wake ?? null)}`;
|
|
129
295
|
/** The inputs the run consumed, read by the settle record's entry ids; assistant and tool output is ignored. */
|
|
130
296
|
const consumed = async (session, ids) => {
|
|
@@ -152,40 +318,94 @@ function createWake(options) {
|
|
|
152
318
|
const current = options.bot();
|
|
153
319
|
return !signal.aborted && !options.profile()?.signal?.aborted && fenceOf(options.profile()) === fence && !!current && current.wakePeriodMs === bot.wakePeriodMs;
|
|
154
320
|
};
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
321
|
+
const snapshot = async (session, canonical, legacy) => {
|
|
322
|
+
const saved = await session.customSnapshot(canonical);
|
|
323
|
+
return saved.available && !saved.entry ? await session.customSnapshot(legacy) : saved;
|
|
324
|
+
};
|
|
325
|
+
const dueRecords = (read) => read.records.filter((entry) => entry.due);
|
|
326
|
+
const routineKey = (due) => JSON.stringify(due.map((entry) => [
|
|
327
|
+
entry.source,
|
|
328
|
+
entry.title,
|
|
329
|
+
entry.progress ?? ""
|
|
330
|
+
]));
|
|
331
|
+
const reminder = (read) => {
|
|
332
|
+
const lines = read.records.slice(0, MAX_SUMMARY_LINES).map(lineOf);
|
|
333
|
+
return [
|
|
334
|
+
options.profile()?.settings?.wake?.prompt ?? BUILT_IN_REMINDER,
|
|
335
|
+
"",
|
|
336
|
+
read.records.length ? `Your standing work (private, ${read.records.length} active):` : "You have no active standing work.",
|
|
337
|
+
...lines,
|
|
338
|
+
...read.records.length > lines.length ? [`(${read.records.length - lines.length} more not shown.)`] : [],
|
|
339
|
+
"",
|
|
340
|
+
...GUIDANCE,
|
|
341
|
+
...read.guidance
|
|
342
|
+
].join("\n");
|
|
343
|
+
};
|
|
344
|
+
const routineWake = async (session, read, live, preserve) => {
|
|
163
345
|
const last = await session.customSnapshot(WAKE_ROUTINE_TYPE);
|
|
164
|
-
if (!last.available) return false;
|
|
346
|
+
if (!last.available || !live()) return false;
|
|
165
347
|
const saved = record(last.entry?.["data"]);
|
|
166
|
-
const
|
|
348
|
+
const due = dueRecords(read);
|
|
167
349
|
if (!due.length) {
|
|
168
|
-
|
|
169
|
-
await scheduler.cancel("routine");
|
|
170
|
-
await session.appendCustomEntry(WAKE_ROUTINE_TYPE, {});
|
|
171
|
-
}
|
|
350
|
+
await timer.cancel("routine");
|
|
172
351
|
return false;
|
|
173
352
|
}
|
|
174
|
-
const key =
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
if (saved?.["key"] === key && typeof saved["at"] === "number" && saved["at"] <= at) return false;
|
|
180
|
-
const fireAt = at + ROUTINE_QUIET_MS;
|
|
181
|
-
const text = ROUTINE_PROMPT.replace("{lines}", due.slice(0, MAX_SUMMARY_LINES).map(lineOf).join("\n"));
|
|
182
|
-
if (!live() || !await scheduler.schedule("routine", fireAt, text, live)) return false;
|
|
183
|
-
await session.appendCustomEntry(WAKE_ROUTINE_TYPE, {
|
|
353
|
+
const key = routineKey(due);
|
|
354
|
+
if (saved?.["key"] === key && (saved["fired"] === true || saved["fired"] === void 0 && typeof saved["at"] === "number" && saved["at"] <= now())) return false;
|
|
355
|
+
const at = now() + ROUTINE_QUIET_MS;
|
|
356
|
+
await timer.schedule("routine", at, preserve);
|
|
357
|
+
if (live() && !preserve) await session.appendCustomEntry(WAKE_ROUTINE_TYPE, {
|
|
184
358
|
key,
|
|
185
|
-
at
|
|
359
|
+
at,
|
|
360
|
+
fired: false
|
|
186
361
|
});
|
|
187
362
|
return true;
|
|
188
363
|
};
|
|
364
|
+
const due = async (row, current) => {
|
|
365
|
+
const bot = options.bot();
|
|
366
|
+
if (!bot || signal.aborted) return { retry: true };
|
|
367
|
+
const session = options.session();
|
|
368
|
+
if (!session?.admitMessage || !session.customSnapshot) return { retry: true };
|
|
369
|
+
const live = liveFor(bot, fenceOf(options.profile()));
|
|
370
|
+
const horizon = horizonOf(bot);
|
|
371
|
+
const read = await readWakeRecords(options.providers(), now(), horizon);
|
|
372
|
+
if (!live() || read.status !== "ok" || read.records.length > MAX_RECORDS || !await current()) return { retry: true };
|
|
373
|
+
const routines = dueRecords(read);
|
|
374
|
+
if (row.slot === "routine" && !routines.length) return {};
|
|
375
|
+
if (row.slot === "reminder" && read.records.some((entry) => entry.covers)) return { nextAt: now() + horizon };
|
|
376
|
+
const text = row.slot === "routine" ? [
|
|
377
|
+
"Your routines are due:",
|
|
378
|
+
...routines.slice(0, MAX_SUMMARY_LINES).map(lineOf),
|
|
379
|
+
"",
|
|
380
|
+
"Choose the next useful routine within your permissions.",
|
|
381
|
+
...read.guidance
|
|
382
|
+
].join("\n") : reminder(read);
|
|
383
|
+
if (!live() || !await current() || !live()) return { retry: true };
|
|
384
|
+
routineAdmission = row.slot === "routine";
|
|
385
|
+
let receipt;
|
|
386
|
+
try {
|
|
387
|
+
receipt = await session.admitMessage({
|
|
388
|
+
type: WAKE_NUDGE_TYPE,
|
|
389
|
+
text,
|
|
390
|
+
display: true
|
|
391
|
+
}, "followUp", row.requestId);
|
|
392
|
+
if (!receipt) return { retry: true };
|
|
393
|
+
if (row.slot === "routine") {
|
|
394
|
+
await session.appendCustomEntry(WAKE_ROUTINE_TYPE, {
|
|
395
|
+
key: routineKey(routines),
|
|
396
|
+
at: now(),
|
|
397
|
+
fired: true
|
|
398
|
+
});
|
|
399
|
+
return { settled: receipt.settled };
|
|
400
|
+
}
|
|
401
|
+
} finally {
|
|
402
|
+
routineAdmission = false;
|
|
403
|
+
}
|
|
404
|
+
return {
|
|
405
|
+
nextAt: now() + horizon,
|
|
406
|
+
settled: receipt.settled
|
|
407
|
+
};
|
|
408
|
+
};
|
|
189
409
|
const run = async (runId) => {
|
|
190
410
|
if (signal.aborted) return "fenced";
|
|
191
411
|
const bot = options.bot();
|
|
@@ -198,103 +418,77 @@ function createWake(options) {
|
|
|
198
418
|
const id = settledId || "calendar-bootstrap";
|
|
199
419
|
const ids = saved?.["entryIds"];
|
|
200
420
|
if (!settled.available) return "unavailable";
|
|
201
|
-
const profile = options.profile();
|
|
202
|
-
const fence = fenceOf(profile);
|
|
203
|
-
const horizon = horizonOf(bot);
|
|
204
|
-
const read = await readWakeRecords(options.providers(), now(), horizon);
|
|
205
|
-
if (read.status !== "ok") return "unavailable";
|
|
206
|
-
if (read.records.length > MAX_RECORDS) return "too-many";
|
|
207
|
-
const live = liveFor(bot, fence);
|
|
208
|
-
const routinesCover = await routineWake(session, read, live);
|
|
209
421
|
if (runId !== void 0 && (runId !== settledId || !Array.isArray(ids) || !ids.length)) return "no-record";
|
|
210
422
|
if (settledId && (!Array.isArray(ids) || !ids.every((entryId) => typeof entryId === "string"))) return "no-record";
|
|
423
|
+
const live = liveFor(bot, fenceOf(options.profile()));
|
|
424
|
+
const horizon = horizonOf(bot);
|
|
425
|
+
if (!live()) return "fenced";
|
|
426
|
+
await timer.schedule("reminder", now() + horizon, true);
|
|
211
427
|
if (inflight.has(id)) return "duplicate";
|
|
212
428
|
inflight.add(id);
|
|
213
429
|
try {
|
|
214
|
-
const result = await session
|
|
430
|
+
const result = await snapshot(session, WAKE_RESULT_TYPE, LEGACY_WAKE_RESULT_TYPE);
|
|
215
431
|
if (!result.available) return "unavailable";
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
const intent = await session.customSnapshot(WAKE_INTENT_TYPE);
|
|
432
|
+
if (record(result.entry?.["data"])?.["runId"] === id) return "duplicate";
|
|
433
|
+
const intent = await snapshot(session, WAKE_INTENT_TYPE, LEGACY_WAKE_INTENT_TYPE);
|
|
219
434
|
if (!intent.available) return "unavailable";
|
|
220
435
|
const pending = record(intent.entry?.["data"]);
|
|
221
436
|
const requestId = pending?.["runId"] === id ? pending["requestId"] : void 0;
|
|
222
437
|
if (pending?.["runId"] === id && (typeof requestId !== "string" || !requestId)) return "unavailable";
|
|
223
|
-
|
|
224
|
-
|
|
438
|
+
const inputs = await consumed(session, new Set(ids ?? []));
|
|
439
|
+
if (settledId && !inputs.length) return "no-input";
|
|
440
|
+
const nudgeOnly = inputs.length > 0 && inputs.every(isNudge);
|
|
441
|
+
const passiveOnly = inputs.length > 0 && inputs.every((type) => isNudge(type) || type === "calendar-outcome");
|
|
442
|
+
if (runId !== void 0 && !passiveOnly && live()) await timer.schedule("reminder", now() + horizon);
|
|
443
|
+
const read = await readWakeRecords(options.providers(), now(), horizon);
|
|
444
|
+
if (read.status !== "ok") return "unavailable";
|
|
445
|
+
if (read.records.length > MAX_RECORDS) return "too-many";
|
|
446
|
+
const routinesCover = await routineWake(session, read, live, runId === void 0);
|
|
447
|
+
if (nudgeOnly) return "nudge-only";
|
|
448
|
+
if (passiveOnly) return "passive-only";
|
|
449
|
+
if (routinesCover || read.records.some((entry) => entry.covers)) return "covered";
|
|
450
|
+
if (runId === void 0 && settledId && requestId === void 0) return "no-record";
|
|
451
|
+
if (!live()) return "fenced";
|
|
452
|
+
const latest = await session.customSnapshot(_agimon_ai_po_core_headless.AGENT_SETTLED_ENTRY_TYPE);
|
|
453
|
+
if (!latest.available || record(latest.entry?.["data"])?.["runId"] !== settledId || !live()) return "fenced";
|
|
454
|
+
const key = typeof requestId === "string" ? requestId : `wake-${crypto.randomUUID()}`;
|
|
455
|
+
if (!requestId) await session.appendCustomEntry(WAKE_INTENT_TYPE, {
|
|
456
|
+
runId: id,
|
|
457
|
+
requestId: key,
|
|
458
|
+
createdAt: now()
|
|
459
|
+
});
|
|
460
|
+
if (!live()) return "fenced";
|
|
461
|
+
const receipt = await session.admitMessage({
|
|
462
|
+
type: WAKE_NUDGE_TYPE,
|
|
463
|
+
text: reminder(read),
|
|
464
|
+
display: true,
|
|
465
|
+
details: { runId: id }
|
|
466
|
+
}, "followUp", key);
|
|
467
|
+
await session.appendCustomEntry(WAKE_RESULT_TYPE, {
|
|
225
468
|
runId: id,
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
duplicate,
|
|
229
|
-
pendingRequestId: typeof requestId === "string" && !duplicate ? requestId : void 0,
|
|
230
|
-
read,
|
|
231
|
-
covered: routinesCover || read.records.some((entry) => entry.covers),
|
|
232
|
-
horizon,
|
|
233
|
-
live
|
|
469
|
+
requestId: key,
|
|
470
|
+
status: receipt ? "admitted" : "untracked"
|
|
234
471
|
});
|
|
472
|
+
return receipt ? "nudged" : "untracked";
|
|
235
473
|
} finally {
|
|
236
474
|
inflight.delete(id);
|
|
237
475
|
}
|
|
238
476
|
};
|
|
239
|
-
const evaluate = async (session, input) => {
|
|
240
|
-
const { runId, settledId, duplicate, pendingRequestId, read, live } = input;
|
|
241
|
-
const inputs = await consumed(session, new Set(input.ids));
|
|
242
|
-
if (settledId && !inputs.length) return "no-input";
|
|
243
|
-
const nudgeOnly = inputs.length > 0 && inputs.every((type) => type === "calendar-wake-nudge");
|
|
244
|
-
if (nudgeOnly && !read.scheduler?.schedule) return "nudge-only";
|
|
245
|
-
if (!nudgeOnly && inputs.length > 0 && inputs.every((type) => type !== void 0 && PASSIVE_TYPES.has(type))) return "passive-only";
|
|
246
|
-
if (input.covered) return "covered";
|
|
247
|
-
if (!live()) return "fenced";
|
|
248
|
-
const at = now();
|
|
249
|
-
const lines = read.records.slice(0, MAX_SUMMARY_LINES).map(lineOf);
|
|
250
|
-
const text = [
|
|
251
|
-
options.profile()?.settings?.wake?.prompt ?? BUILT_IN_REMINDER,
|
|
252
|
-
"",
|
|
253
|
-
read.records.length ? `Your standing work (private, ${read.records.length} active):` : "You have no active standing events or routines.",
|
|
254
|
-
...lines,
|
|
255
|
-
...read.records.length > lines.length ? [`(${read.records.length - lines.length} more not shown.)`] : [],
|
|
256
|
-
"",
|
|
257
|
-
...GUIDANCE
|
|
258
|
-
].join("\n");
|
|
259
|
-
const requestId = pendingRequestId ?? `calendar-wake-${crypto.randomUUID()}`;
|
|
260
|
-
const latest = await session.customSnapshot(_agimon_ai_po_core_headless.AGENT_SETTLED_ENTRY_TYPE);
|
|
261
|
-
if (!latest.available || record(latest.entry?.["data"])?.["runId"] !== settledId || !live()) return "fenced";
|
|
262
|
-
if (nudgeOnly || duplicate) {
|
|
263
|
-
if (!read.scheduler?.schedule) return "duplicate";
|
|
264
|
-
return await read.scheduler.schedule("reminder", at + input.horizon, text, live) ? "scheduled" : "fenced";
|
|
265
|
-
}
|
|
266
|
-
if (!pendingRequestId) await session.appendCustomEntry(WAKE_INTENT_TYPE, {
|
|
267
|
-
runId,
|
|
268
|
-
requestId,
|
|
269
|
-
createdAt: at
|
|
270
|
-
});
|
|
271
|
-
if (!live()) return "fenced";
|
|
272
|
-
const receipt = await session.admitMessage({
|
|
273
|
-
type: WAKE_NUDGE_TYPE,
|
|
274
|
-
text,
|
|
275
|
-
display: true,
|
|
276
|
-
details: { runId }
|
|
277
|
-
}, "followUp", requestId);
|
|
278
|
-
await session.appendCustomEntry(WAKE_RESULT_TYPE, {
|
|
279
|
-
runId,
|
|
280
|
-
requestId,
|
|
281
|
-
status: receipt ? "admitted" : "untracked"
|
|
282
|
-
});
|
|
283
|
-
return receipt ? "nudged" : "untracked";
|
|
284
|
-
};
|
|
285
477
|
return {
|
|
478
|
+
due,
|
|
286
479
|
settled: (event) => typeof event.runId === "string" && event.runId && event.aborted !== true ? run(event.runId) : Promise.resolve("no-record"),
|
|
287
480
|
recover: () => run(),
|
|
288
481
|
started: async () => {
|
|
289
482
|
const session = options.session();
|
|
290
483
|
if (signal.aborted || !options.bot() || !session?.customSnapshot || !session.appendCustomEntry) return;
|
|
291
484
|
const last = await session.customSnapshot(WAKE_ROUTINE_TYPE);
|
|
292
|
-
const
|
|
293
|
-
if (!last.available ||
|
|
294
|
-
const
|
|
295
|
-
if (
|
|
296
|
-
|
|
297
|
-
await
|
|
485
|
+
const saved = record(last.entry?.["data"]);
|
|
486
|
+
if (!last.available || saved?.["fired"] === true) return;
|
|
487
|
+
const at = saved?.["at"];
|
|
488
|
+
if (saved?.["fired"] !== false && typeof at === "number" && at <= now()) return;
|
|
489
|
+
if (routineAdmission) return;
|
|
490
|
+
await timer.cancel("routine");
|
|
491
|
+
if (typeof at === "number") await session.appendCustomEntry(WAKE_ROUTINE_TYPE, {});
|
|
298
492
|
}
|
|
299
493
|
};
|
|
300
494
|
}
|
|
@@ -322,7 +516,11 @@ function createWakeSessionState(agent, root, owner) {
|
|
|
322
516
|
providers: () => providers,
|
|
323
517
|
session: () => agent?.context.session,
|
|
324
518
|
profile: () => profileRuntime?.readSelection(),
|
|
325
|
-
signal: lifetime.signal
|
|
519
|
+
signal: lifetime.signal,
|
|
520
|
+
timer: {
|
|
521
|
+
schedule: (slot, at, preserve) => root.schedule(owner, slot, at, preserve),
|
|
522
|
+
cancel: (slot) => root.cancel(owner, slot)
|
|
523
|
+
}
|
|
326
524
|
});
|
|
327
525
|
const check = async (operation) => {
|
|
328
526
|
if (lifetime.signal.aborted || !root.botSession(owner)) return;
|
|
@@ -371,7 +569,10 @@ function createWakeSessionState(agent, root, owner) {
|
|
|
371
569
|
level: "warning"
|
|
372
570
|
}))
|
|
373
571
|
});
|
|
374
|
-
const detach = root.attach(owner.sessionId, () => check(() => wake.recover()))
|
|
572
|
+
const detach = root.attach(owner.sessionId, () => check(() => wake.recover()), async (row, current) => {
|
|
573
|
+
await ready();
|
|
574
|
+
return wake.due(row, current);
|
|
575
|
+
});
|
|
375
576
|
return () => {
|
|
376
577
|
detach();
|
|
377
578
|
started.dispose();
|
|
@@ -425,9 +626,6 @@ var wake_lifecycle_server_default = (0, _agimon_ai_po_core_extensionFile.defineC
|
|
|
425
626
|
}) };
|
|
426
627
|
}, { context: true });
|
|
427
628
|
//#endregion
|
|
428
|
-
//#region src/constants/wake.ts
|
|
429
|
-
const COMMAND_DESCRIPTION = "List the standing work that decides whether this bot is woken";
|
|
430
|
-
//#endregion
|
|
431
629
|
//#region src/extensions/workspaces/sessions/(backend)/command/wake.server.ts
|
|
432
630
|
/** `/wake`: the standing work every plugin reports for this session, so a person can see why a bot sleeps. */
|
|
433
631
|
var wake_server_default = (0, _agimon_ai_po_core_extensionFile.defineCommand)((context) => ({
|