@ouro.bot/cli 0.1.0-alpha.1145 → 0.1.0-alpha.1147
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/changelog.json +12 -0
- package/deploy/unraid/sanctuary.ouro/bundle-meta.json +1 -1
- package/deploy/unraid/sanctuary.xml +1 -1
- package/dist/heart/daemon/cli-defaults.js +16 -25
- package/dist/heart/shepherd/operations.js +1 -1
- package/dist/heart/shepherd-config.js +3 -13
- package/dist/heart/versioning/release-sources.js +15 -3
- package/dist/repertoire/guardrails.js +4 -1
- package/dist/repertoire/tools-app-sessions.js +82 -315
- package/dist/senses/app-sessions/app-interface.js +317 -0
- package/dist/senses/app-sessions/client.js +86 -250
- package/dist/senses/app-sessions/prompts.js +101 -0
- package/dist/senses/app-sessions/sense.js +13 -9
- package/dist/senses/app-sessions/size-watch.js +3 -2
- package/dist/senses/app-sessions/store.js +12 -2
- package/dist/senses/app-sessions/waiting-prompts.js +1 -5
- package/dist/senses/app-sessions/wake-runner.js +2 -1
- package/dist/senses/shepherd/host.js +0 -3
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/dist/senses/shepherd/copilot-app-holds.js +0 -138
- package/dist/senses/shepherd/copilot-app.js +0 -676
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.findSession = findSession;
|
|
37
|
+
exports.listSessionsAndChats = listSessionsAndChats;
|
|
38
|
+
exports.getSessionsStatus = getSessionsStatus;
|
|
39
|
+
exports.getSession = getSession;
|
|
40
|
+
exports.deliverMessage = deliverMessage;
|
|
41
|
+
exports.sendSessionMessage = sendSessionMessage;
|
|
42
|
+
exports.createSession = createSession;
|
|
43
|
+
exports.answerSessionInput = answerSessionInput;
|
|
44
|
+
exports.respondToSessionPlan = respondToSessionPlan;
|
|
45
|
+
exports.archiveSession = archiveSession;
|
|
46
|
+
exports.navigateTo = navigateTo;
|
|
47
|
+
exports.saveSessionAutomation = saveSessionAutomation;
|
|
48
|
+
exports.getSessionAutomation = getSessionAutomation;
|
|
49
|
+
exports.listWorkflows = listWorkflows;
|
|
50
|
+
exports.runWorkflow = runWorkflow;
|
|
51
|
+
const node_crypto_1 = require("node:crypto");
|
|
52
|
+
const os = __importStar(require("node:os"));
|
|
53
|
+
const runtime_1 = require("../../nerves/runtime");
|
|
54
|
+
const client_1 = require("./client");
|
|
55
|
+
const store_1 = require("./store");
|
|
56
|
+
/**
|
|
57
|
+
* The GitHub Copilot app's own interface, used the way a coordinating session uses its client tools: the same twelve
|
|
58
|
+
* operations, under the app's names, answering in the app's shapes. Each one is a request on the app's local WebSocket,
|
|
59
|
+
* the socket its own window speaks, and a reply read back from it. Nothing here reads the app's database or files.
|
|
60
|
+
*
|
|
61
|
+
* Three things a session gets from the app are not reachable from outside it. An outside client is anonymous: the app
|
|
62
|
+
* takes its messages for the person's, and ignores the creator and coordination fields on a create request, so a session
|
|
63
|
+
* made here has no coordinator and nothing reports back unless the sender watches it. A question or plan approval is
|
|
64
|
+
* announced once, to the clients connected then (see `prompts.ts`). `relay_session_input` and `publish_chat_update` act
|
|
65
|
+
* on the calling session's own conversation, which a client outside the app has none of.
|
|
66
|
+
*/
|
|
67
|
+
const REQUEST_WAIT_MS = 30_000;
|
|
68
|
+
const CREATE_WAIT_MS = 300_000;
|
|
69
|
+
const ARCHIVE_WAIT_MS = 60_000;
|
|
70
|
+
const ANSWER_WAIT_MS = 15_000;
|
|
71
|
+
const COORDINATION_NOTE = "an outside client cannot be a session's coordinator, so the app will not report back: replies come back to you through the app-sessions sense instead";
|
|
72
|
+
const isRecord = (value) => !!value && typeof value === "object" && !Array.isArray(value);
|
|
73
|
+
const str = (value) => (typeof value === "string" && value.length > 0 ? value : null);
|
|
74
|
+
const compact = (value) => Object.fromEntries(Object.entries(value).filter(([, item]) => item !== null && item !== undefined));
|
|
75
|
+
const appUrl = (id) => `ghapp://sessions/${id}`;
|
|
76
|
+
const reasonOf = (frame) => str(frame.error) ?? str(frame.message) ?? str(frame.reason) ?? str(frame.detail) ?? String(frame.type);
|
|
77
|
+
/** Whether a sender wants a session's reply back, and for how long: once (the default), always, or never. */
|
|
78
|
+
const notifyMode = (value) => (value === "never" ? null : value === "always" ? "always" : "once");
|
|
79
|
+
/** One request and its answer: the frame `settle` calls "ok", or the reason it was refused, or that the app did not answer in time. */
|
|
80
|
+
async function ask(frame, settle, waitMs, options) {
|
|
81
|
+
// An answer carries the id of the request it answers; every other request gets its own.
|
|
82
|
+
const requestId = str(frame.request_id) ?? (0, node_crypto_1.randomUUID)();
|
|
83
|
+
const verdict = (reply) => (reply.type === "socket_error" || reply.type === "error" ? "fail" : settle(reply, requestId));
|
|
84
|
+
const got = await (0, client_1.exchange)([{ ...frame, request_id: requestId }], { keep: reply => verdict(reply) !== "ignore", done: reply => /^(ok|fail)$/.test(verdict(reply)), waitMs }, options);
|
|
85
|
+
const last = got.at(-1);
|
|
86
|
+
if (last && verdict(last) === "ok")
|
|
87
|
+
return { ok: true, frame: last, error: null };
|
|
88
|
+
return { ok: false, frame: last ?? null, error: last && verdict(last) === "fail" ? reasonOf(last) : "the app did not answer in time" };
|
|
89
|
+
}
|
|
90
|
+
/** A session by its id, or a unique start of it (the first eight characters are enough). */
|
|
91
|
+
function findSession(rows, value) {
|
|
92
|
+
const wanted = str(value)?.trim() ?? "";
|
|
93
|
+
if (!wanted)
|
|
94
|
+
throw new Error("name a session: its id, or the start of it, from list_sessions_and_chats");
|
|
95
|
+
const exact = rows.find(row => row.id === wanted);
|
|
96
|
+
if (exact)
|
|
97
|
+
return exact;
|
|
98
|
+
const matches = rows.filter(row => row.id.startsWith(wanted));
|
|
99
|
+
if (matches.length === 1)
|
|
100
|
+
return matches[0];
|
|
101
|
+
throw new Error(matches.length ? `'${wanted}' matches ${matches.length} sessions; give more of the id` : `no session '${wanted}' in the Copilot app (list_sessions_and_chats shows them)`);
|
|
102
|
+
}
|
|
103
|
+
const resolve = async (value, options) => findSession(await (0, client_1.listAppSessions)(options), value);
|
|
104
|
+
function item(row) {
|
|
105
|
+
return compact({
|
|
106
|
+
type: row.kind === "chat" ? "chat" : "session", id: row.id, app_url: appUrl(row.id), name: row.name, project_id: row.projectId, project_name: row.project, project_repo: row.projectRepo,
|
|
107
|
+
branch: row.branch, path: row.path, created_at: row.createdAt, updated_at: row.updatedAt, creator_session_id: row.creatorSessionId,
|
|
108
|
+
coordinating_creator_session_id: row.coordinatingCreatorSessionId, parent_session_id: row.parentSessionId, is_running: row.isRunning, ...(row.remote ? { remote: true } : {}),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/** What an answer needs from a session blocked on something: the request's id and what it asks, or why the id is unknown. */
|
|
112
|
+
function awaitingInput(row, pending) {
|
|
113
|
+
const request = pending[row.id];
|
|
114
|
+
if (row.awaiting === "permission")
|
|
115
|
+
return { kind: "permission", request_id: row.prompt.requestId, what: row.prompt.what, note: "answer it in the app; the app's own tools do not answer permissions either" };
|
|
116
|
+
if (row.awaiting === "input" || row.awaiting === "plan") {
|
|
117
|
+
const kind = row.awaiting === "plan" ? "plan" : "user_input";
|
|
118
|
+
if (request && (request.kind === "plan") === (kind === "plan")) {
|
|
119
|
+
return compact({ kind, request_id: request.requestId, question: request.question, choices: request.choices, allow_freeform: request.allowFreeform, summary: request.summary, actions: request.actions });
|
|
120
|
+
}
|
|
121
|
+
return { kind, request_id: null, note: "it was asked while this agent was not connected to the app, which announces a question only once; the person can answer it in the app" };
|
|
122
|
+
}
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
function status(row, pending) {
|
|
126
|
+
const input = awaitingInput(row, pending);
|
|
127
|
+
return compact({
|
|
128
|
+
...item(row), was_interrupted: row.wasInterrupted || null,
|
|
129
|
+
activity: compact({ status: row.state === "working" ? "busy" : row.state, why: row.detail, last_activity: row.lastActivity }),
|
|
130
|
+
awaiting_user_input: row.awaiting === "input", awaiting_plan_approval: row.awaiting === "plan", awaiting_permission: row.awaiting === "permission",
|
|
131
|
+
awaiting_input: input,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
// ---------- reading ----------
|
|
135
|
+
async function listSessionsAndChats(options = {}) {
|
|
136
|
+
const rows = await (0, client_1.listAppSessions)(options);
|
|
137
|
+
return { total: rows.length, items: rows.map(item) };
|
|
138
|
+
}
|
|
139
|
+
async function getSessionsStatus(agentRoot, state, options = {}) {
|
|
140
|
+
const rows = await (0, client_1.listAppSessions)(options);
|
|
141
|
+
const pending = (0, store_1.readPending)(agentRoot);
|
|
142
|
+
const count = (keep) => rows.filter(keep).length;
|
|
143
|
+
return {
|
|
144
|
+
summary: {
|
|
145
|
+
total: rows.length, running: count(row => row.isRunning), busy: count(row => row.state === "working"), blocked: count(row => row.state === "waiting"), stuck: count(row => row.state === "stuck"),
|
|
146
|
+
awaiting_user_input: count(row => row.awaiting === "input"), awaiting_plan_approval: count(row => row.awaiting === "plan"),
|
|
147
|
+
},
|
|
148
|
+
sessions: rows.filter(row => !state || row.state === state || (state === "busy" && row.state === "working")).map(row => status(row, pending)),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
async function getSession(agentRoot, id, options = {}) {
|
|
152
|
+
return status(await resolve(id, options), (0, store_1.readPending)(agentRoot));
|
|
153
|
+
}
|
|
154
|
+
// ---------- writing ----------
|
|
155
|
+
/** Put a message into a session, as its message box does, and wait for the app to accept it (a session that is not loaded loads first). */
|
|
156
|
+
async function deliverMessage(id, message, mode, options = {}) {
|
|
157
|
+
(0, client_1.assertSessionId)(id);
|
|
158
|
+
let interim = false;
|
|
159
|
+
const sent = await ask({ type: "send_message", session_id: id, prompt: message, delivery_mode: mode }, (reply, requestId) => {
|
|
160
|
+
if (/^session_message_(sent|queued)$/.test(String(reply.type)) && reply.request_id === requestId)
|
|
161
|
+
return "ok";
|
|
162
|
+
if (reply.type === "session_dispatch_state" && reply.request_id === requestId) {
|
|
163
|
+
interim ||= reply.dispatch_may_have_started === true;
|
|
164
|
+
return "wait";
|
|
165
|
+
}
|
|
166
|
+
return reply.request_id === requestId || (reply.session_id === id && /^session_(message_delivery_unknown|resume_failed)$/.test(String(reply.type))) ? "fail" : "ignore";
|
|
167
|
+
}, options.sendWaitMs ?? client_1.SEND_WAIT_MS, options);
|
|
168
|
+
const unconfirmed = !sent.ok && interim && sent.frame?.type === "session_dispatch_state";
|
|
169
|
+
(0, runtime_1.emitNervesEvent)({ component: "senses", event: "senses.app_sessions_sent", message: "sent a message into a Copilot app session", meta: { session: id, chars: message.length, ok: sent.ok } });
|
|
170
|
+
return unconfirmed ? { ...sent, error: "the app began delivering the message but had not confirmed it in time; read the session before sending it again" } : sent;
|
|
171
|
+
}
|
|
172
|
+
/** `send_session_message`. `notify_on_idle` brings the session's finished reply back to the sender. */
|
|
173
|
+
async function sendSessionMessage(agentRoot, args, options = {}) {
|
|
174
|
+
const message = typeof args.message === "string" ? args.message : "";
|
|
175
|
+
if (!message.trim())
|
|
176
|
+
throw new Error("say what to send");
|
|
177
|
+
const row = await resolve(args.session_id, options);
|
|
178
|
+
const offset = (0, client_1.eventLogSize)(row.id, options);
|
|
179
|
+
const sent = await deliverMessage(row.id, message, args.delivery_mode === "enqueue" ? "enqueue" : "immediate", options);
|
|
180
|
+
const notify = notifyMode(args.notify_on_idle);
|
|
181
|
+
const watching = sent.ok && notify !== null;
|
|
182
|
+
if (watching)
|
|
183
|
+
(0, store_1.watchSession)(agentRoot, row.id, row.name, offset, Date.now(), notify === "once");
|
|
184
|
+
return compact({ ok: sent.ok, status: sent.ok ? (sent.frame?.type === "session_message_queued" ? "queued" : "accepted") : "failed", session_id: row.id, name: row.name, message_id: str(sent.frame?.message_id), watching, error: sent.error });
|
|
185
|
+
}
|
|
186
|
+
/** The project a create request names: its id, or its name or repository as the app lists it. */
|
|
187
|
+
async function projectId(value, options) {
|
|
188
|
+
const listed = await ask({ type: "list_projects" }, reply => (reply.type === "project_list" ? "ok" : "ignore"), REQUEST_WAIT_MS, options);
|
|
189
|
+
const projects = Array.isArray(listed.frame?.projects) ? listed.frame.projects.filter(isRecord) : [];
|
|
190
|
+
const found = projects.find(project => project.id === value) ?? projects.find(project => project.name === value);
|
|
191
|
+
if (!found)
|
|
192
|
+
throw new Error(`no project '${value}' in the Copilot app${projects.length ? ` (it has: ${projects.map(project => str(project.name) ?? "?").join(", ")})` : ""}`);
|
|
193
|
+
return found.id;
|
|
194
|
+
}
|
|
195
|
+
/** Start a session: a project session in its own worktree (or on the project's checkout), or a standalone chat. */
|
|
196
|
+
async function createSession(agentRoot, args, options = {}) {
|
|
197
|
+
const kickoff = isRecord(args.kickoff) ? args.kickoff : {};
|
|
198
|
+
const prompt = str(kickoff.prompt);
|
|
199
|
+
const name = str(args.name);
|
|
200
|
+
const project = str(args.project_id);
|
|
201
|
+
const chat = args.session_kind === "chat" || (!project && args.session_kind !== "project");
|
|
202
|
+
if (!chat && !project)
|
|
203
|
+
throw new Error("a project session needs project_id (a project's id or name; list_sessions_and_chats shows project_id on its sessions)");
|
|
204
|
+
let created;
|
|
205
|
+
if (chat) {
|
|
206
|
+
created = await ask({ type: "create_session", cwd: os.homedir(), session_type: "general_chat", ...(str(kickoff.model) ? { model: str(kickoff.model) } : {}), ...(str(kickoff.agent) ? { agent: str(kickoff.agent) } : {}) }, (reply, id) => (reply.request_id === id ? (reply.type === "session_created" ? "ok" : "fail") : "ignore"), REQUEST_WAIT_MS, options);
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
const worktree = args.workspace_type !== "branch";
|
|
210
|
+
created = await ask({
|
|
211
|
+
type: worktree ? "create_worktree_workspace" : "create_branch_workspace", project_id: await projectId(project, options),
|
|
212
|
+
...(worktree ? { branch_name: str(args.branch_name) ?? `ouro-${(0, node_crypto_1.randomUUID)().slice(0, 8)}`, checkout_existing_branch: false } : {}),
|
|
213
|
+
...(name ? { name } : {}), kickoff_permission_mode: str(kickoff.permission_mode) ?? "allow-all",
|
|
214
|
+
...(str(kickoff.model) ? { kickoff_model: str(kickoff.model) } : {}),
|
|
215
|
+
...(str(kickoff.agent) ? { kickoff_agent: str(kickoff.agent) } : {}), ...(str(kickoff.reasoning_effort) ? { kickoff_reasoning_effort: str(kickoff.reasoning_effort) } : {}),
|
|
216
|
+
}, (reply, id) => (reply.request_id === id ? (reply.type === "session_created" ? "ok" : reply.type === "workspace_created" || reply.type === "workspace_changed" ? "wait" : "fail") : reply.type === "session_creation_failed" ? "fail" : "ignore"), CREATE_WAIT_MS, options);
|
|
217
|
+
}
|
|
218
|
+
const id = str(created.frame?.session_id);
|
|
219
|
+
if (!created.ok || !id)
|
|
220
|
+
return { ok: false, created: false, error: created.error ?? "the app did not name the session it made" };
|
|
221
|
+
(0, client_1.assertSessionId)(id);
|
|
222
|
+
(0, store_1.addProbe)(agentRoot, id, name, Date.now());
|
|
223
|
+
// The app does not run a kickoff for a client that is not its window ("ready for first message"), so the first message and mode are set here.
|
|
224
|
+
let sent = null;
|
|
225
|
+
if (chat && name)
|
|
226
|
+
await ask({ type: "rename_session", session_id: id, title: name }, reply => (reply.type === "session_title_updated" && reply.session_id === id ? "ok" : "ignore"), REQUEST_WAIT_MS, options);
|
|
227
|
+
const mode = str(kickoff.mode);
|
|
228
|
+
if (mode && mode !== "interactive")
|
|
229
|
+
await ask({ type: "set_session_mode", session_id: id, mode }, reply => (reply.type === "session_mode_result" && reply.session_id === id ? "ok" : "ignore"), REQUEST_WAIT_MS, options);
|
|
230
|
+
if (prompt)
|
|
231
|
+
sent = await sendSessionMessage(agentRoot, { session_id: id, message: prompt, notify_on_idle: args.notify_on_idle }, options);
|
|
232
|
+
const error = sent && !sent.ok ? `created, but the first message was not accepted: ${String(sent.error)}` : null;
|
|
233
|
+
(0, runtime_1.emitNervesEvent)({ component: "senses", event: "senses.app_sessions_created", message: "created a Copilot app session", meta: { session: id, chat, prompted: !!prompt } });
|
|
234
|
+
return compact({ ok: error === null, created: true, session_id: id, app_url: appUrl(id), kind: chat ? "chat" : "session", workspace_id: str(created.frame?.workspace_id), watching: sent?.watching, coordination: COORDINATION_NOTE, error });
|
|
235
|
+
}
|
|
236
|
+
/** Answer a question a session asked with its ask_user tool. */
|
|
237
|
+
async function answerSessionInput(agentRoot, args, options = {}) {
|
|
238
|
+
const answer = typeof args.answer === "string" ? args.answer : "";
|
|
239
|
+
if (!answer)
|
|
240
|
+
throw new Error("give the answer");
|
|
241
|
+
const row = await resolve(args.session_id, options);
|
|
242
|
+
const request = (0, store_1.readPending)(agentRoot)[row.id];
|
|
243
|
+
if (row.awaiting !== "input" || request?.kind !== "input")
|
|
244
|
+
throw new Error(`session ${row.id} is not waiting on a question this agent heard asked (get_session shows what it waits on)`);
|
|
245
|
+
const answered = await ask({ type: "respond_to_user_input", session_id: row.id, request_id: request.requestId, answer, was_freeform: !(request.choices ?? []).includes(answer) }, reply => (reply.type === "user_input_resolved" && reply.request_id === request.requestId ? "ok" : "ignore"), ANSWER_WAIT_MS, options);
|
|
246
|
+
(0, runtime_1.emitNervesEvent)({ component: "senses", event: "senses.app_sessions_answered", message: "answered a Copilot app session's question", meta: { session: row.id, ok: answered.ok } });
|
|
247
|
+
return compact({ ok: answered.ok, session_id: row.id, error: answered.error });
|
|
248
|
+
}
|
|
249
|
+
/** Approve or reject a plan a session is paused on. */
|
|
250
|
+
async function respondToSessionPlan(agentRoot, args, options = {}) {
|
|
251
|
+
const row = await resolve(args.session_id, options);
|
|
252
|
+
const request = (0, store_1.readPending)(agentRoot)[row.id];
|
|
253
|
+
if (row.awaiting !== "plan" || request?.kind !== "plan")
|
|
254
|
+
throw new Error(`session ${row.id} is not waiting on a plan this agent heard presented (get_session shows what it waits on)`);
|
|
255
|
+
const approved = args.approved === true || args.approved === "true";
|
|
256
|
+
const responded = await ask({ type: "respond_to_exit_plan_mode", session_id: row.id, request_id: request.requestId, approved, ...(str(args.selected_action) ? { selected_action: str(args.selected_action) } : {}) }, reply => (reply.request_id === request.requestId ? (reply.type === "exit_plan_mode_resolved" ? "ok" : reply.type === "exit_plan_mode_response_failed" ? "fail" : "ignore") : "ignore"), ANSWER_WAIT_MS, options);
|
|
257
|
+
const feedback = str(args.feedback);
|
|
258
|
+
const told = responded.ok && feedback ? await sendSessionMessage(agentRoot, { session_id: row.id, message: feedback, notify_on_idle: "never" }, options) : null;
|
|
259
|
+
(0, runtime_1.emitNervesEvent)({ component: "senses", event: "senses.app_sessions_plan_answered", message: "answered a Copilot app session's plan", meta: { session: row.id, ok: responded.ok, approved } });
|
|
260
|
+
return compact({ ok: responded.ok, approved, session_id: row.id, feedback_sent: told ? told.ok : undefined, error: responded.error });
|
|
261
|
+
}
|
|
262
|
+
/** Archive a session this agent created: its workspace when it has one (and the worktree with it), else the chat. */
|
|
263
|
+
async function archiveSession(agentRoot, id, options = {}) {
|
|
264
|
+
const row = await resolve(id, options);
|
|
265
|
+
const made = (0, store_1.readProbes)(agentRoot);
|
|
266
|
+
if (!made[row.id])
|
|
267
|
+
throw new Error(`session ${row.id} was not created by this agent, so it will not archive it (yours: ${Object.keys(made).join(", ") || "none"})`);
|
|
268
|
+
const archived = row.workspaceId
|
|
269
|
+
? await ask({ type: "archive_workspace", workspace_id: row.workspaceId, delete_worktree: row.workspaceType === "worktree" }, (reply, requestId) => (reply.request_id === requestId ? (reply.type === "workspace_archived" ? "ok" : "fail") : "ignore"), ARCHIVE_WAIT_MS, options)
|
|
270
|
+
: await ask({ type: "archive_session", session_id: row.id }, (reply, requestId) => (reply.type === "general_chat_sessions_changed" ? "ok" : reply.request_id === requestId ? "fail" : "ignore"), ARCHIVE_WAIT_MS, options);
|
|
271
|
+
if (archived.ok)
|
|
272
|
+
(0, store_1.removeProbe)(agentRoot, row.id);
|
|
273
|
+
else
|
|
274
|
+
(0, runtime_1.emitNervesEvent)({ level: "warn", component: "senses", event: "senses.app_sessions_archive_refused", message: "the Copilot app did not archive a session", meta: { session: row.id, error: archived.error } });
|
|
275
|
+
(0, runtime_1.emitNervesEvent)({ component: "senses", event: "senses.app_sessions_archived", message: "asked the Copilot app to archive a session", meta: { session: row.id, ok: archived.ok } });
|
|
276
|
+
return compact({ ok: archived.ok, session_id: row.id, error: archived.error });
|
|
277
|
+
}
|
|
278
|
+
/** Show a session in the app's window, as following its link does. */
|
|
279
|
+
async function navigateTo(id, options = {}) {
|
|
280
|
+
const row = await resolve(id, options);
|
|
281
|
+
const shown = await ask({ type: "handle_deep_link", url: appUrl(row.id) }, reply => (reply.type === "deep_link_received" ? "ok" : "ignore"), REQUEST_WAIT_MS, options);
|
|
282
|
+
return compact({ ok: shown.ok, session_id: row.id, app_url: appUrl(row.id), error: shown.error });
|
|
283
|
+
}
|
|
284
|
+
// ---------- automations and workflows ----------
|
|
285
|
+
/** Create or replace the automation that wakes a session, or clear it. */
|
|
286
|
+
async function saveSessionAutomation(args, options = {}) {
|
|
287
|
+
const row = await resolve(args.session_id, options);
|
|
288
|
+
const settle = (reply, id) => (reply.request_id === id && /^session_automation_mutation_result$/.test(String(reply.type)) ? (reply.error ? "fail" : "ok") : "ignore");
|
|
289
|
+
if (args.clear === true) {
|
|
290
|
+
const owned = await ownedAutomations(row.id, options);
|
|
291
|
+
const first = Array.isArray(owned.frame?.automations) ? owned.frame.automations.filter(isRecord)[0] : undefined;
|
|
292
|
+
if (!str(first?.id))
|
|
293
|
+
return { ok: false, error: owned.error ?? "that session has no automation to clear" };
|
|
294
|
+
const cleared = await ask({ type: "cancel_session_automation", session_id: row.id, automation_id: first.id }, settle, REQUEST_WAIT_MS, options);
|
|
295
|
+
return compact({ ok: cleared.ok, session_id: row.id, cleared: cleared.ok, error: cleared.error });
|
|
296
|
+
}
|
|
297
|
+
const keys = ["prompt", "interval", "run_at", "every_minutes", "schedule_hour", "schedule_day", "schedule_minute", "cron_expression", "name"];
|
|
298
|
+
const saved = await ask({ type: "save_session_automation", session_id: row.id, ...Object.fromEntries(keys.filter(key => args[key] !== undefined).map(key => [key, args[key]])) }, settle, REQUEST_WAIT_MS, options);
|
|
299
|
+
return compact({ ok: saved.ok, session_id: row.id, automation: saved.frame?.automation, error: saved.error });
|
|
300
|
+
}
|
|
301
|
+
const ownedAutomations = (session, options) => ask({ type: "list_owned_session_automations", session_id: session }, (reply, id) => (reply.request_id === id && reply.type === "owned_session_automations_result" ? "ok" : "ignore"), REQUEST_WAIT_MS, options);
|
|
302
|
+
async function getSessionAutomation(id, options = {}) {
|
|
303
|
+
const row = await resolve(id, options);
|
|
304
|
+
const owned = await ownedAutomations(row.id, options);
|
|
305
|
+
return compact({ ok: owned.ok, session_id: row.id, automations: owned.frame?.automations, schedules: owned.frame?.legacy_schedules, error: owned.error });
|
|
306
|
+
}
|
|
307
|
+
async function listWorkflows(options = {}) {
|
|
308
|
+
const listed = await ask({ type: "list_workflows" }, (reply, id) => (reply.request_id === id && reply.type === "workflow_list" ? "ok" : "ignore"), REQUEST_WAIT_MS, options);
|
|
309
|
+
return compact({ ok: listed.ok, workflows: listed.frame?.tasks, error: listed.error });
|
|
310
|
+
}
|
|
311
|
+
async function runWorkflow(id, options = {}) {
|
|
312
|
+
const workflow = str(id);
|
|
313
|
+
if (!workflow)
|
|
314
|
+
throw new Error("name the workflow: its id, from list_workflows");
|
|
315
|
+
const started = await ask({ type: "run_workflow_now", id: workflow }, reply => (reply.type === "workflow_run_started" ? "ok" : reply.type === "workflow_run_rejected" ? "fail" : "ignore"), REQUEST_WAIT_MS, options);
|
|
316
|
+
return compact({ ok: started.ok, run: started.frame?.run, error: started.error });
|
|
317
|
+
}
|