shadok-ai 0.20.61 → 0.20.62
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 +16 -8
- package/dist/guest-init.d.ts +25 -4
- package/dist/guest-init.js +17 -4
- package/dist/guest-init.js.map +1 -1
- package/dist/server.js +62 -30
- package/dist/server.js.map +1 -1
- package/package.json +1 -1
- package/public/index.html +3 -3
package/README.md
CHANGED
|
@@ -1151,13 +1151,14 @@ conversation, the same trust the site already has over what they type there.
|
|
|
1151
1151
|
Needs the multi-tenant deployment (External groups do), and HTTPS on the sites
|
|
1152
1152
|
that embed it.
|
|
1153
1153
|
|
|
1154
|
-
### First-run script (optional): prepare a guest's account before its agent
|
|
1154
|
+
### First-run script (optional): prepare a guest's account before its agent's first message
|
|
1155
1155
|
|
|
1156
1156
|
An **External** group can carry a shell script that runs once for each new
|
|
1157
|
-
conversation — a new WhatsApp number, Telegram user or site visitor —
|
|
1158
|
-
that person's agent
|
|
1159
|
-
place: a folder layout, a file fetched
|
|
1160
|
-
|
|
1157
|
+
conversation — a new WhatsApp number, Telegram user or site visitor — once
|
|
1158
|
+
that person's agent exists and before it is given their first message. Use it
|
|
1159
|
+
for whatever the agent should find in place: a folder layout, a file fetched
|
|
1160
|
+
from another service, an account reserved for this person, a daily schedule or
|
|
1161
|
+
a webhook of its own. The agent's first turn is then only its answer.
|
|
1161
1162
|
|
|
1162
1163
|
Write it in the External group's settings (*First-run script*). It runs with
|
|
1163
1164
|
`sh`, as that conversation's own Unix user, in its own folder, with the secrets
|
|
@@ -1169,11 +1170,18 @@ of the door's profile, and it is given:
|
|
|
1169
1170
|
| `SHADOK_GUEST_DOOR` | `whatsapp`, `telegram`, `widget`, or `agent` |
|
|
1170
1171
|
| `SHADOK_GUEST_DIR` | the conversation's folder (also the working directory) |
|
|
1171
1172
|
| `SHADOK_GROUP_ID` | the group |
|
|
1173
|
+
| `SHADOK_SESSION_ID`, `SHADOK_SESSION_KEY`, `SHADOK_PORT` | the agent being prepared: what the scheduler and webhook scripts read |
|
|
1174
|
+
|
|
1175
|
+
The key is that agent's own and reaches nothing else: the script can schedule
|
|
1176
|
+
a prompt or open a webhook **for its agent**, the way the agent would — with
|
|
1177
|
+
the scheduler skill's `schedule.mjs` / `webhook.mjs`, or `POST /crons` and
|
|
1178
|
+
`POST /webhooks` with the `x-shadok-session-key` header. It cannot store
|
|
1179
|
+
secrets for the conversation.
|
|
1172
1180
|
|
|
1173
1181
|
- The person's first message waits for it: 60 seconds at most.
|
|
1174
|
-
- **A non-zero exit or a timeout
|
|
1175
|
-
|
|
1176
|
-
their next message. The panel shows the last run and why it failed; the
|
|
1182
|
+
- **A non-zero exit or a timeout closes the agent.** Whatever the script
|
|
1183
|
+
scheduled or opened goes with it, the account is erased, the person is asked
|
|
1184
|
+
to write again, and the script runs again at their next message. The panel shows the last run and why it failed; the
|
|
1177
1185
|
server log has a `guests: first-run script FAILED` line.
|
|
1178
1186
|
- It applies to **new** conversations only. People already talking when you
|
|
1179
1187
|
add or change the script are not affected.
|
package/dist/guest-init.d.ts
CHANGED
|
@@ -2,7 +2,16 @@ import type { Group } from "./groups.js";
|
|
|
2
2
|
/**
|
|
3
3
|
* The FIRST-RUN SCRIPT of an external group: a shell script the operator
|
|
4
4
|
* writes once, run for every NEW conversation of that group before its agent
|
|
5
|
-
*
|
|
5
|
+
* is given the first message, as that conversation's own Unix user and in its
|
|
6
|
+
* own directory.
|
|
7
|
+
*
|
|
8
|
+
* It runs once the agent and its channel EXIST, on purpose: the first version
|
|
9
|
+
* ran before the spawn, and could prepare files but nothing that belongs to an
|
|
10
|
+
* agent — a morning schedule, a webhook — because a key is only honoured for a
|
|
11
|
+
* session the server knows. Those stayed on the agent's first turn, which is
|
|
12
|
+
* the very thing the script is there to keep free. So it now holds the agent's
|
|
13
|
+
* own key (`SHADOK_SESSION_KEY`), and `ready` — what makes a bridge deliver the
|
|
14
|
+
* person's message — waits for it.
|
|
6
15
|
*
|
|
7
16
|
* It exists so the account is ready before the first message is answered. The
|
|
8
17
|
* alternative is to describe the setup in the profile's prompt and let the
|
|
@@ -122,6 +131,17 @@ export declare function guestInitEnv(input: {
|
|
|
122
131
|
conversation: string;
|
|
123
132
|
door: GuestInitDoor;
|
|
124
133
|
dir: string;
|
|
134
|
+
/**
|
|
135
|
+
* The agent this script prepares the ground for — it exists by the time the
|
|
136
|
+
* script runs, so the script can do for it what it could do for itself:
|
|
137
|
+
* schedule a prompt, open a webhook. The key is that agent's own, and the
|
|
138
|
+
* server boxes it to that one session for as long as the script runs.
|
|
139
|
+
*/
|
|
140
|
+
session: {
|
|
141
|
+
id: string;
|
|
142
|
+
port: string;
|
|
143
|
+
key: string;
|
|
144
|
+
};
|
|
125
145
|
}): Record<string, string>;
|
|
126
146
|
/** `sh -c <script>`, wrapped in the same `setpriv` drop as the agent's spawn. */
|
|
127
147
|
export declare function guestInitCommand(user: string | null, script: string): {
|
|
@@ -157,9 +177,10 @@ export declare function initOutcome(r: {
|
|
|
157
177
|
* first message for as long as that call lasts, well past the ceiling. So the
|
|
158
178
|
* script gets a process group of its own, the WHOLE group is killed at the
|
|
159
179
|
* deadline, and the answer is taken when the shell exits, not when the pipes
|
|
160
|
-
* close. `killStragglers` is
|
|
161
|
-
* the group
|
|
162
|
-
*
|
|
180
|
+
* close. `killStragglers` is an optional second sweep for a process that left
|
|
181
|
+
* the group. The server passes none: the agent runs as the same Unix user by
|
|
182
|
+
* then, so "everything this user runs" is no longer just the script — and on a
|
|
183
|
+
* failure the whole account is erased anyway, processes first.
|
|
163
184
|
*/
|
|
164
185
|
export declare function runInitScript(input: {
|
|
165
186
|
cmd: string;
|
package/dist/guest-init.js
CHANGED
|
@@ -8,7 +8,16 @@ import { instanceKey } from "./paths.js";
|
|
|
8
8
|
/**
|
|
9
9
|
* The FIRST-RUN SCRIPT of an external group: a shell script the operator
|
|
10
10
|
* writes once, run for every NEW conversation of that group before its agent
|
|
11
|
-
*
|
|
11
|
+
* is given the first message, as that conversation's own Unix user and in its
|
|
12
|
+
* own directory.
|
|
13
|
+
*
|
|
14
|
+
* It runs once the agent and its channel EXIST, on purpose: the first version
|
|
15
|
+
* ran before the spawn, and could prepare files but nothing that belongs to an
|
|
16
|
+
* agent — a morning schedule, a webhook — because a key is only honoured for a
|
|
17
|
+
* session the server knows. Those stayed on the agent's first turn, which is
|
|
18
|
+
* the very thing the script is there to keep free. So it now holds the agent's
|
|
19
|
+
* own key (`SHADOK_SESSION_KEY`), and `ready` — what makes a bridge deliver the
|
|
20
|
+
* person's message — waits for it.
|
|
12
21
|
*
|
|
13
22
|
* It exists so the account is ready before the first message is answered. The
|
|
14
23
|
* alternative is to describe the setup in the profile's prompt and let the
|
|
@@ -161,6 +170,9 @@ export function guestInitEnv(input) {
|
|
|
161
170
|
env.SHADOK_GUEST_KEY = input.conversation;
|
|
162
171
|
env.SHADOK_GUEST_DOOR = input.door;
|
|
163
172
|
env.SHADOK_GUEST_DIR = input.dir;
|
|
173
|
+
env.SHADOK_SESSION_ID = input.session.id;
|
|
174
|
+
env.SHADOK_PORT = input.session.port;
|
|
175
|
+
env.SHADOK_SESSION_KEY = input.session.key;
|
|
164
176
|
Object.assign(env, dropEnv(input.user, input.home));
|
|
165
177
|
return env;
|
|
166
178
|
}
|
|
@@ -196,9 +208,10 @@ const STDERR_KEEP = 64 * 1024;
|
|
|
196
208
|
* first message for as long as that call lasts, well past the ceiling. So the
|
|
197
209
|
* script gets a process group of its own, the WHOLE group is killed at the
|
|
198
210
|
* deadline, and the answer is taken when the shell exits, not when the pipes
|
|
199
|
-
* close. `killStragglers` is
|
|
200
|
-
* the group
|
|
201
|
-
*
|
|
211
|
+
* close. `killStragglers` is an optional second sweep for a process that left
|
|
212
|
+
* the group. The server passes none: the agent runs as the same Unix user by
|
|
213
|
+
* then, so "everything this user runs" is no longer just the script — and on a
|
|
214
|
+
* failure the whole account is erased anyway, processes first.
|
|
202
215
|
*/
|
|
203
216
|
export function runInitScript(input) {
|
|
204
217
|
const timeoutMs = input.timeoutMs ?? GUEST_INIT_TIMEOUT_MS;
|
package/dist/guest-init.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guest-init.js","sourceRoot":"","sources":["../src/guest-init.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAElD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC
|
|
1
|
+
{"version":3,"file":"guest-init.js","sourceRoot":"","sources":["../src/guest-init.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAElD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,iFAAiF;AACjF,MAAM,CAAC,MAAM,oBAAoB,GAAG,EAAE,GAAG,IAAI,CAAC;AAE9C;;;;GAIG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC;AAE5C,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AA2BnD;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,CAAU;IACxC,IAAI,OAAO,CAAC,KAAK,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S;IAChB,OAAO,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,CAAC;AACtF,CAAC;AAED,MAAM,UAAU,cAAc;IAC5B,IAAI,CAAC;QACH,OAAO,mBAAmB,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,SAAS,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;IAC/E,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,IAA0B;IACvD,MAAM,CAAC,GAAG,SAAS,EAAE,CAAC;IACtB,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;IAChE,MAAM,GAAG,GAAG,GAAG,CAAC,IAAI,OAAO,CAAC,GAAG,MAAM,CAAC;IACtC,EAAE,CAAC,aAAa,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;IACtE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;AACxB,CAAC"}
|
package/dist/server.js
CHANGED
|
@@ -1470,10 +1470,13 @@ export function agentPrincipal(req) {
|
|
|
1470
1470
|
}
|
|
1471
1471
|
/** The principal of a KNOWN session id (already authenticated, or not a caller at all). */
|
|
1472
1472
|
function principalForSession(id) {
|
|
1473
|
-
|
|
1473
|
+
const p = resolveAgentPrincipal(id, loadChannels(), (name) => {
|
|
1474
1474
|
const row = withSalts(loadPeers(peerFileFor())).find((p) => p.name === name);
|
|
1475
1475
|
return row ? peerScope(row) : null;
|
|
1476
1476
|
}, sessions.get(id)?.profile, loadTgOwner());
|
|
1477
|
+
// An agent whose first-run script is running is a guest's whatever the
|
|
1478
|
+
// registry says yet (`preparingSessions`).
|
|
1479
|
+
return p.guest || !preparingSessions.has(id) ? p : { ...p, guest: `init:${id}` };
|
|
1477
1480
|
}
|
|
1478
1481
|
/**
|
|
1479
1482
|
* The restricted peer box a caller is in: the restricted peer itself
|
|
@@ -1532,7 +1535,7 @@ function requestAgent(req, chans) {
|
|
|
1532
1535
|
// A guest's agent is boxed to its own session (`guestConversationOf`), so it
|
|
1533
1536
|
// reaches no sibling and no group-level resource — a media subdirectory
|
|
1534
1537
|
// included, which is where the other guests' attachments live.
|
|
1535
|
-
return { id, group: ch?.group ?? null, boxed: !!guestConversationOf(ch, loadTgOwner()) };
|
|
1538
|
+
return { id, group: ch?.group ?? null, boxed: !!guestConversationOf(ch, loadTgOwner()) || preparingSessions.has(id) };
|
|
1536
1539
|
}
|
|
1537
1540
|
/** May this request act on (schedule for, read the files of) `targetId`? */
|
|
1538
1541
|
function mayActFor(req, targetId) {
|
|
@@ -4783,6 +4786,17 @@ async function ensureExternalUser(g, conversation) {
|
|
|
4783
4786
|
}
|
|
4784
4787
|
return { ok: true, user: r.user };
|
|
4785
4788
|
}
|
|
4789
|
+
/**
|
|
4790
|
+
* The agents whose first-run script is running right now. While it runs, the
|
|
4791
|
+
* script holds its agent's key — and a door's guest is not yet boxed by the
|
|
4792
|
+
* registry, because the binding that boxes it (`guestConversationOf`) is
|
|
4793
|
+
* written by its bridge on `ready`, which the script is what delays. Without
|
|
4794
|
+
* this, that key would speak for an ordinary member of the group for up to a
|
|
4795
|
+
* minute: able to reach the other strangers' agents. With it, the key acts on
|
|
4796
|
+
* its own session and nothing else, exactly as it will once the binding lands.
|
|
4797
|
+
* In memory on purpose: it describes a child process of this server.
|
|
4798
|
+
*/
|
|
4799
|
+
const preparingSessions = new Set();
|
|
4786
4800
|
/**
|
|
4787
4801
|
* Run the group's first-run script for a conversation that still owes one
|
|
4788
4802
|
* (`src/guest-init.ts`) — after its account and its directory exist, before
|
|
@@ -4818,6 +4832,7 @@ async function prepareConversation(g, conversation, run) {
|
|
|
4818
4832
|
conversation,
|
|
4819
4833
|
door: run.door,
|
|
4820
4834
|
dir: run.cwd,
|
|
4835
|
+
session: { id: run.sessionId, port: String(boundPort || START_PORT), key: sessionKeyFor(run.sessionId) },
|
|
4821
4836
|
});
|
|
4822
4837
|
recordSecretAudit({
|
|
4823
4838
|
action: "guest-init",
|
|
@@ -4830,17 +4845,9 @@ async function prepareConversation(g, conversation, run) {
|
|
|
4830
4845
|
});
|
|
4831
4846
|
const cmd = guestInitCommand(run.user, cfg.script);
|
|
4832
4847
|
const started = Date.now();
|
|
4833
|
-
|
|
4834
|
-
|
|
4835
|
-
|
|
4836
|
-
cwd: run.cwd,
|
|
4837
|
-
env,
|
|
4838
|
-
// Before its agent exists, everything this Unix user runs IS the script.
|
|
4839
|
-
killStragglers: () => {
|
|
4840
|
-
const uid = uidOf(run.user);
|
|
4841
|
-
return uid != null && uid !== 0 ? killProcessesOfUid(uid) : undefined;
|
|
4842
|
-
},
|
|
4843
|
-
});
|
|
4848
|
+
// Boxed for the length of the run (`preparingSessions`).
|
|
4849
|
+
preparingSessions.add(run.sessionId);
|
|
4850
|
+
const outcome = await runInitScript({ cmd: cmd.cmd, argv: cmd.argv, cwd: run.cwd, env }).finally(() => preparingSessions.delete(run.sessionId));
|
|
4844
4851
|
const ms = Date.now() - started;
|
|
4845
4852
|
saveGuestInits(withInitRun(loadGuestInits(), g.id, {
|
|
4846
4853
|
at: Date.now(),
|
|
@@ -4855,6 +4862,12 @@ async function prepareConversation(g, conversation, run) {
|
|
|
4855
4862
|
return { ok: true };
|
|
4856
4863
|
}
|
|
4857
4864
|
console.warn(`guests: first-run script FAILED for ${row.user} (${conversation}, group ${g.id.slice(0, 8)}) — ${outcome.detail}. No agent was started; the account is erased and the script runs again at this person's next message.`);
|
|
4865
|
+
// The agent first — its process runs as the account about to be erased, and
|
|
4866
|
+
// closing its channel is also what drops whatever the script scheduled or
|
|
4867
|
+
// opened for it. Then the account: `endChannel` erases it only for a channel
|
|
4868
|
+
// it can NAME, and a door's guest is named by a binding its bridge writes
|
|
4869
|
+
// after `ready`, which this conversation never reached.
|
|
4870
|
+
await run.closeAgent().catch(() => { });
|
|
4858
4871
|
await retireConversationUser(row);
|
|
4859
4872
|
return { ok: false, why: "this conversation could not be prepared" };
|
|
4860
4873
|
}
|
|
@@ -13402,21 +13415,6 @@ wss.on("connection", (ws, req) => {
|
|
|
13402
13415
|
const early = await connectRefusal(earlyUser);
|
|
13403
13416
|
if (early)
|
|
13404
13417
|
return fail(early.message, early.code, early.user);
|
|
13405
|
-
// The group's first-run script, for a conversation that owes one.
|
|
13406
|
-
// LAST of the refusals that cost nothing: a spawn that was going
|
|
13407
|
-
// to be refused anyway must not have run it, and the next thing
|
|
13408
|
-
// down is the agent itself.
|
|
13409
|
-
const initGroup = spawnConv && earlyUser ? loadGroups().find((x) => x.id === earlyIso.groupPatch.group) : undefined;
|
|
13410
|
-
if (spawnConv && earlyUser && initGroup?.nature === "external") {
|
|
13411
|
-
const prepared = await prepareConversation(initGroup, spawnConv, {
|
|
13412
|
-
user: earlyUser,
|
|
13413
|
-
cwd: effectiveCwd,
|
|
13414
|
-
door: isWidgetBridge ? "widget" : isWhatsAppBridge ? "whatsapp" : guestSetup ? "telegram" : "agent",
|
|
13415
|
-
profile: guestSetup ? guestSetup.profile : typeof msg.profile === "string" && msg.profile ? msg.profile : null,
|
|
13416
|
-
});
|
|
13417
|
-
if (!prepared.ok)
|
|
13418
|
-
return fail(prepared.why, GUEST_INIT_CODE);
|
|
13419
|
-
}
|
|
13420
13418
|
}
|
|
13421
13419
|
announceStep("workspace");
|
|
13422
13420
|
if (msg.worktree && !resumed && !spawnConv && isGitRepo(cwd)) {
|
|
@@ -13716,7 +13714,17 @@ wss.on("connection", (ws, req) => {
|
|
|
13716
13714
|
if (turns.length)
|
|
13717
13715
|
send({ type: "history", turns });
|
|
13718
13716
|
}
|
|
13719
|
-
|
|
13717
|
+
// The group's first-run script, for a conversation that still owes
|
|
13718
|
+
// one (`prepareConversation`). It runs once the agent AND its channel
|
|
13719
|
+
// row exist — that is what lets it act for the agent with the agent's
|
|
13720
|
+
// own key — and `ready` is held back until it has succeeded, because
|
|
13721
|
+
// `ready` is what makes a bridge deliver the person's first message.
|
|
13722
|
+
// Asked on a resume too: a server that stopped mid-script left the
|
|
13723
|
+
// debt on disk, and the next message must settle it, not skip it.
|
|
13724
|
+
const owedConv = spawnConv ?? guestConversationKey(storedRow);
|
|
13725
|
+
const owedGroup = owedConv && dropUser && agentGroupId ? loadGroups().find((x) => x.id === agentGroupId) : undefined;
|
|
13726
|
+
const initOwed = !!owedConv && owedGroup?.nature === "external" && externalUserRow(loadExternalUsers(), owedGroup.id, owedConv)?.init === "pending";
|
|
13727
|
+
const readyMsg = {
|
|
13720
13728
|
type: "ready",
|
|
13721
13729
|
instanceKey: instanceKey(),
|
|
13722
13730
|
sessionId: id,
|
|
@@ -13725,7 +13733,9 @@ wss.on("connection", (ws, req) => {
|
|
|
13725
13733
|
staleAuto: AUTO_RELOAD,
|
|
13726
13734
|
cwd: effectiveCwd,
|
|
13727
13735
|
branch: worktree?.branch ?? null,
|
|
13728
|
-
}
|
|
13736
|
+
};
|
|
13737
|
+
if (!initOwed)
|
|
13738
|
+
send(readyMsg);
|
|
13729
13739
|
// Mirroring drops an agent into the (admin-owned) Telegram board, so
|
|
13730
13740
|
// only an admin may enable it. A non-admin's `true` is DROPPED (not a
|
|
13731
13741
|
// spawn failure — same spirit as a refused parent link); an explicit
|
|
@@ -13808,6 +13818,28 @@ wss.on("connection", (ws, req) => {
|
|
|
13808
13818
|
...(agentConv ? { conversation: agentConv } : {}),
|
|
13809
13819
|
});
|
|
13810
13820
|
pendingCtx.delete(id);
|
|
13821
|
+
if (initOwed && owedGroup && owedConv && dropUser) {
|
|
13822
|
+
const live = session;
|
|
13823
|
+
const prepared = await prepareConversation(owedGroup, owedConv, {
|
|
13824
|
+
user: dropUser,
|
|
13825
|
+
cwd: effectiveCwd,
|
|
13826
|
+
door: isWidgetBridge ? "widget" : isWhatsAppBridge ? "whatsapp" : guestSetup ? "telegram" : "agent",
|
|
13827
|
+
profile,
|
|
13828
|
+
sessionId: id,
|
|
13829
|
+
// The agent goes first, and without telling THIS client it was
|
|
13830
|
+
// "stopped": to a bridge that word means someone closed the
|
|
13831
|
+
// conversation, and what it must hear is the refusal below.
|
|
13832
|
+
closeAgent: async () => {
|
|
13833
|
+
live.clients.delete(ws);
|
|
13834
|
+
await endChannel(id);
|
|
13835
|
+
},
|
|
13836
|
+
});
|
|
13837
|
+
if (!prepared.ok) {
|
|
13838
|
+
session = null;
|
|
13839
|
+
return fail(prepared.why, GUEST_INIT_CODE);
|
|
13840
|
+
}
|
|
13841
|
+
send(readyMsg);
|
|
13842
|
+
}
|
|
13811
13843
|
send({ type: "tokens", tokens: tokenTotals(session) });
|
|
13812
13844
|
send(profileMessage(session));
|
|
13813
13845
|
if (session.contextPct !== null)
|