shadok-ai 0.20.60 → 0.20.61
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 +31 -0
- package/dist/external-users.d.ts +15 -0
- package/dist/external-users.js +17 -0
- package/dist/external-users.js.map +1 -1
- package/dist/guest-admit.d.ts +8 -0
- package/dist/guest-admit.js +8 -0
- package/dist/guest-admit.js.map +1 -1
- package/dist/guest-init.d.ts +185 -0
- package/dist/guest-init.js +280 -0
- package/dist/guest-init.js.map +1 -0
- package/dist/secret-audit.d.ts +1 -1
- package/dist/secret-audit.js +1 -1
- package/dist/secret-audit.js.map +1 -1
- package/dist/server.js +135 -2
- package/dist/server.js.map +1 -1
- package/dist/telegram.js +14 -1
- package/dist/telegram.js.map +1 -1
- package/dist/wa-bridge.js +6 -1
- package/dist/wa-bridge.js.map +1 -1
- package/package.json +1 -1
- package/public/index.html +81 -5
package/README.md
CHANGED
|
@@ -1151,6 +1151,37 @@ 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 starts
|
|
1155
|
+
|
|
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 — before
|
|
1158
|
+
that person's agent is started. Use it for whatever the agent should find in
|
|
1159
|
+
place: a folder layout, a file fetched from another service, an account
|
|
1160
|
+
reserved for this person. The agent's first turn is then only its answer.
|
|
1161
|
+
|
|
1162
|
+
Write it in the External group's settings (*First-run script*). It runs with
|
|
1163
|
+
`sh`, as that conversation's own Unix user, in its own folder, with the secrets
|
|
1164
|
+
of the door's profile, and it is given:
|
|
1165
|
+
|
|
1166
|
+
| Variable | Value |
|
|
1167
|
+
|---|---|
|
|
1168
|
+
| `SHADOK_GUEST_KEY` | the conversation: `wa-<number>`, `tg-<id>`, `web-<visitor>` |
|
|
1169
|
+
| `SHADOK_GUEST_DOOR` | `whatsapp`, `telegram`, `widget`, or `agent` |
|
|
1170
|
+
| `SHADOK_GUEST_DIR` | the conversation's folder (also the working directory) |
|
|
1171
|
+
| `SHADOK_GROUP_ID` | the group |
|
|
1172
|
+
|
|
1173
|
+
- The person's first message waits for it: 60 seconds at most.
|
|
1174
|
+
- **A non-zero exit or a timeout means no agent is started.** The account is
|
|
1175
|
+
erased, the person is asked to write again, and the script runs again at
|
|
1176
|
+
their next message. The panel shows the last run and why it failed; the
|
|
1177
|
+
server log has a `guests: first-run script FAILED` line.
|
|
1178
|
+
- It applies to **new** conversations only. People already talking when you
|
|
1179
|
+
add or change the script are not affected.
|
|
1180
|
+
- An agent you start yourself in the group runs it too (door `agent`) — the
|
|
1181
|
+
way to try a script without a phone.
|
|
1182
|
+
- What the script reserves in another service is not released when the
|
|
1183
|
+
conversation is erased.
|
|
1184
|
+
|
|
1154
1185
|
### Running in Docker
|
|
1155
1186
|
|
|
1156
1187
|
> Putting one instance online for someone else, on its own machine, behind a
|
package/dist/external-users.d.ts
CHANGED
|
@@ -37,6 +37,17 @@ export interface ExternalUserRow {
|
|
|
37
37
|
* operator asked to erase.
|
|
38
38
|
*/
|
|
39
39
|
retiring?: number;
|
|
40
|
+
/**
|
|
41
|
+
* `"pending"` while the group's first-run script has not SUCCEEDED for this
|
|
42
|
+
* conversation (`src/guest-init.ts`) — absent otherwise, which is also what
|
|
43
|
+
* every row written before the field existed reads as.
|
|
44
|
+
*
|
|
45
|
+
* Written with the row, at the mint, and dropped on success. It is on disk
|
|
46
|
+
* rather than in memory because the gap it covers is a server that stopped
|
|
47
|
+
* mid-script: without it the next message would find an account, conclude
|
|
48
|
+
* the conversation is an old one, and start an agent in a half-prepared home.
|
|
49
|
+
*/
|
|
50
|
+
init?: "pending";
|
|
40
51
|
}
|
|
41
52
|
/**
|
|
42
53
|
* What provisioning this conversation's user amounts to, against the registry.
|
|
@@ -69,6 +80,10 @@ export declare function withExternalUser(rows: readonly ExternalUserRow[], row:
|
|
|
69
80
|
export declare function externalUsersOfGroup(rows: readonly ExternalUserRow[], groupId: string): ExternalUserRow[];
|
|
70
81
|
/** The account minted for this conversation, or null. */
|
|
71
82
|
export declare function externalUserRow(rows: readonly ExternalUserRow[], groupId: string, conversation: string): ExternalUserRow | null;
|
|
83
|
+
/** Mark a conversation's first-run script as owed (new array). */
|
|
84
|
+
export declare function withInitPending(rows: readonly ExternalUserRow[], user: string): ExternalUserRow[];
|
|
85
|
+
/** The first-run script succeeded — or there is none left to run (new array). */
|
|
86
|
+
export declare function withInitDone(rows: readonly ExternalUserRow[], user: string): ExternalUserRow[];
|
|
72
87
|
/** Drop rows (new array) — a torn-down conversation, or a deleted group's lot. */
|
|
73
88
|
export declare function withoutExternalUsers(rows: readonly ExternalUserRow[], users: readonly string[]): ExternalUserRow[];
|
|
74
89
|
/**
|
package/dist/external-users.js
CHANGED
|
@@ -51,6 +51,19 @@ export function externalUserRow(rows, groupId, conversation) {
|
|
|
51
51
|
const unit = conversationUnit(groupId, conversation);
|
|
52
52
|
return rows.find((r) => r.unit === unit) ?? null;
|
|
53
53
|
}
|
|
54
|
+
/** Mark a conversation's first-run script as owed (new array). */
|
|
55
|
+
export function withInitPending(rows, user) {
|
|
56
|
+
return rows.map((r) => (r.user === user ? { ...r, init: "pending" } : r));
|
|
57
|
+
}
|
|
58
|
+
/** The first-run script succeeded — or there is none left to run (new array). */
|
|
59
|
+
export function withInitDone(rows, user) {
|
|
60
|
+
return rows.map((r) => {
|
|
61
|
+
if (r.user !== user || r.init === undefined)
|
|
62
|
+
return r;
|
|
63
|
+
const { init: _init, ...rest } = r;
|
|
64
|
+
return rest;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
54
67
|
/** Drop rows (new array) — a torn-down conversation, or a deleted group's lot. */
|
|
55
68
|
export function withoutExternalUsers(rows, users) {
|
|
56
69
|
const gone = new Set(users);
|
|
@@ -240,6 +253,10 @@ export function normalizeExternalUsers(raw) {
|
|
|
240
253
|
// read as live because its timestamp was mangled.
|
|
241
254
|
if (row.retiring != null)
|
|
242
255
|
kept.retiring = typeof row.retiring === "number" && Number.isFinite(row.retiring) ? row.retiring : 0;
|
|
256
|
+
// Same direction for the first-run script: any trace of the mark means it
|
|
257
|
+
// is still owed, never that it ran.
|
|
258
|
+
if (row.init != null)
|
|
259
|
+
kept.init = "pending";
|
|
243
260
|
out.push(kept);
|
|
244
261
|
}
|
|
245
262
|
return out;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"external-users.js","sourceRoot":"","sources":["../src/external-users.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"external-users.js","sourceRoot":"","sources":["../src/external-users.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAsDpF;;;;;;;;;;GAUG;AACH,MAAM,UAAU,mBAAmB,CACjC,IAAgC,EAChC,IAAoC,EACpC,GAAW;IAKX,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,CAAC,CAAC;IACtD,IAAI,MAAM,EAAE,CAAC;QACX,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI;YAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC;QACr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2E;QAC3E,uEAAuE;QACvE,0EAA0E;QAC1E,0EAA0E;QAC1E,oDAAoD;QACpD,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,IAAgC;IAChE,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IACtD,EAAE,CAAC,aAAa,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;IACvE,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,2CAA2C;AACxE,CAAC"}
|
package/dist/guest-admit.d.ts
CHANGED
|
@@ -45,6 +45,14 @@ export declare const GUEST_RATE_WINDOW_MS = 3600000;
|
|
|
45
45
|
/** The refusal's `code` on the wire, so a bridge classifies it without
|
|
46
46
|
* matching on the message text (the `busy` convention). */
|
|
47
47
|
export declare const GUEST_FULL_CODE = "guest-full";
|
|
48
|
+
/**
|
|
49
|
+
* The `error` code of a spawn refused because the group's first-run script
|
|
50
|
+
* failed (`src/guest-init.ts`). It lives here, beside the other code a bridge
|
|
51
|
+
* must recognise, so a bridge does not import the script runner to read it.
|
|
52
|
+
* Unlike the ceiling, trying again IS the answer: the account was erased and
|
|
53
|
+
* the next message starts over.
|
|
54
|
+
*/
|
|
55
|
+
export declare const GUEST_INIT_CODE = "guest-init";
|
|
48
56
|
/**
|
|
49
57
|
* This door's limits: what the operator stored, else the defaults.
|
|
50
58
|
*
|
package/dist/guest-admit.js
CHANGED
|
@@ -15,6 +15,14 @@ export const GUEST_RATE_WINDOW_MS = 3_600_000;
|
|
|
15
15
|
/** The refusal's `code` on the wire, so a bridge classifies it without
|
|
16
16
|
* matching on the message text (the `busy` convention). */
|
|
17
17
|
export const GUEST_FULL_CODE = "guest-full";
|
|
18
|
+
/**
|
|
19
|
+
* The `error` code of a spawn refused because the group's first-run script
|
|
20
|
+
* failed (`src/guest-init.ts`). It lives here, beside the other code a bridge
|
|
21
|
+
* must recognise, so a bridge does not import the script runner to read it.
|
|
22
|
+
* Unlike the ceiling, trying again IS the answer: the account was erased and
|
|
23
|
+
* the next message starts over.
|
|
24
|
+
*/
|
|
25
|
+
export const GUEST_INIT_CODE = "guest-init";
|
|
18
26
|
/**
|
|
19
27
|
* This door's limits: what the operator stored, else the defaults.
|
|
20
28
|
*
|
package/dist/guest-admit.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guest-admit.js","sourceRoot":"","sources":["../src/guest-admit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACnE,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAqC1D;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,YAAY,GAAgB,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;AAEpE,4CAA4C;AAC5C,MAAM,CAAC,MAAM,oBAAoB,GAAG,SAAS,CAAC;AAE9C;4DAC4D;AAC5D,MAAM,CAAC,MAAM,eAAe,GAAG,YAAY,CAAC;AAE5C;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,aAAa,CAAC,KAAiC;IAC7D,MAAM,GAAG,GAAG,CAAC,CAAU,EAAE,QAAgB,EAAE,EAAE,CAC3C,OAAO,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;IAC3E,OAAO;QACL,IAAI,E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,GAAG,EAAE,MAAM;YACX,OAAO,EAAE,mDAAmD,MAAM,CAAC,IAAI,sBAAsB;SAC9F,CAAC;IACJ,MAAM,MAAM,GAAG,KAAK,CAAC,QAAQ,IAAI,oBAAoB,CAAC;IACtD,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,GAAG,MAAM,CAAC;IACjC,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC;IAC3D,IAAI,KAAK,IAAI,MAAM,CAAC,OAAO;QACzB,OAAO;YACL,EAAE,EAAE,KAAK;YACT,GAAG,EAAE,MAAM;YACX,OAAO,EAAE,6CAA6C,MAAM,CAAC,OAAO,2CAA2C;SAChH,CAAC;IACJ,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;AACxC,CAAC"}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type { Group } from "./groups.js";
|
|
2
|
+
/**
|
|
3
|
+
* The FIRST-RUN SCRIPT of an external group: a shell script the operator
|
|
4
|
+
* writes once, run for every NEW conversation of that group before its agent
|
|
5
|
+
* exists, as that conversation's own Unix user and in its own directory.
|
|
6
|
+
*
|
|
7
|
+
* It exists so the account is ready before the first message is answered. The
|
|
8
|
+
* alternative is to describe the setup in the profile's prompt and let the
|
|
9
|
+
* agent do it on its first turn: slower (the person waits on a model following
|
|
10
|
+
* instructions), and it hands a stranger's very first message to an agent that
|
|
11
|
+
* is busy with something else. A script is deterministic, costs no quota, and
|
|
12
|
+
* the agent's first turn is only the answer.
|
|
13
|
+
*
|
|
14
|
+
* Three rules shape everything below.
|
|
15
|
+
*
|
|
16
|
+
* - **It runs as the conversation, never as the server.** The server is root in
|
|
17
|
+
* the only deployment where an external group exists, so "run it as the
|
|
18
|
+
* server" would make this field a root shell (the lesson of the cron guard,
|
|
19
|
+
* invariant 60). It gets the same `setpriv` drop, the same allow-listed
|
|
20
|
+
* environment and the same profile secrets as that conversation's agent — it
|
|
21
|
+
* is strictly no more powerful than the agent it prepares the ground for.
|
|
22
|
+
* - **A conversation whose script did not succeed has no agent.** A half
|
|
23
|
+
* prepared account is the one outcome nobody chose: the admission is refused,
|
|
24
|
+
* the account is erased, and the person's next message starts over.
|
|
25
|
+
* - **Only the operator writes it.** It is stored beside the other door
|
|
26
|
+
* settings, root-owned, and reachable through an admin, browser-only route.
|
|
27
|
+
*
|
|
28
|
+
* Everything here is pure except the store at the bottom, so the rules are
|
|
29
|
+
* tested with no root, no account and no shell.
|
|
30
|
+
*/
|
|
31
|
+
/** What a script may weigh. Far past any setup script; a ceiling, not a need. */
|
|
32
|
+
export declare const GUEST_INIT_MAX_BYTES: number;
|
|
33
|
+
/**
|
|
34
|
+
* How long one run may take. The person's first message waits on it, so it is
|
|
35
|
+
* short on purpose: a script that needs longer belongs in the background of
|
|
36
|
+
* the script itself, not in front of someone waiting for an answer.
|
|
37
|
+
*/
|
|
38
|
+
export declare const GUEST_INIT_TIMEOUT_MS = 60000;
|
|
39
|
+
export { GUEST_INIT_CODE } from "./guest-admit.js";
|
|
40
|
+
/** Through which door the conversation came — `agent` for one a member opened
|
|
41
|
+
* in the group themselves, which is also how a script is tried without a phone. */
|
|
42
|
+
export type GuestInitDoor = "telegram" | "whatsapp" | "widget" | "agent";
|
|
43
|
+
/** The outcome of the latest run, kept for the operator's panel. */
|
|
44
|
+
export interface GuestInitRun {
|
|
45
|
+
at: number;
|
|
46
|
+
ok: boolean;
|
|
47
|
+
/** The conversation it ran for (`wa-…`, `tg-…`, `web-…`). */
|
|
48
|
+
conversation: string;
|
|
49
|
+
ms: number;
|
|
50
|
+
/** Why it failed — first line of stderr, or the timeout. Absent on success. */
|
|
51
|
+
detail?: string;
|
|
52
|
+
}
|
|
53
|
+
export interface GuestInit {
|
|
54
|
+
/** The external group this script belongs to. */
|
|
55
|
+
group: string;
|
|
56
|
+
script: string;
|
|
57
|
+
updatedAt: number;
|
|
58
|
+
/** The admin account that last wrote it. */
|
|
59
|
+
by: string;
|
|
60
|
+
last?: GuestInitRun;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* A script as typed in the panel → what is stored. Line endings are unified
|
|
64
|
+
* (a textarea posts CRLF, and `sh` reads the CR as part of the last word of
|
|
65
|
+
* every line); nothing else is touched, because it is a program.
|
|
66
|
+
*/
|
|
67
|
+
export declare function cleanInitScript(v: unknown): {
|
|
68
|
+
ok: true;
|
|
69
|
+
script: string;
|
|
70
|
+
} | {
|
|
71
|
+
ok: false;
|
|
72
|
+
error: string;
|
|
73
|
+
};
|
|
74
|
+
/** The store as read from disk: malformed rows are dropped, one row per group. */
|
|
75
|
+
export declare function normalizeGuestInits(raw: unknown): GuestInit[];
|
|
76
|
+
export declare function guestInitOf(list: readonly GuestInit[], group: string): GuestInit | null;
|
|
77
|
+
/**
|
|
78
|
+
* Write (or, with an empty script, remove) a group's first-run script.
|
|
79
|
+
*
|
|
80
|
+
* Refused outside an EXTERNAL group: that is the only nature with one Unix
|
|
81
|
+
* account per conversation, and a script run once per stranger as the group's
|
|
82
|
+
* shared user would be writing into every other stranger's files.
|
|
83
|
+
*/
|
|
84
|
+
export declare function guestInitWriteFlow(input: {
|
|
85
|
+
list: readonly GuestInit[];
|
|
86
|
+
groups: readonly Pick<Group, "id" | "nature">[];
|
|
87
|
+
/** Why the instance cannot isolate conversations at all, or null. */
|
|
88
|
+
unavailable: string | null;
|
|
89
|
+
group: string;
|
|
90
|
+
script: unknown;
|
|
91
|
+
by: string;
|
|
92
|
+
now: number;
|
|
93
|
+
}): {
|
|
94
|
+
ok: true;
|
|
95
|
+
list: GuestInit[];
|
|
96
|
+
init: GuestInit | null;
|
|
97
|
+
} | {
|
|
98
|
+
ok: false;
|
|
99
|
+
status: number;
|
|
100
|
+
error: string;
|
|
101
|
+
};
|
|
102
|
+
/** Record a run's outcome on its group's row (new array). */
|
|
103
|
+
export declare function withInitRun(list: readonly GuestInit[], group: string, run: GuestInitRun): GuestInit[];
|
|
104
|
+
/** Drop the rows of groups that no longer exist (new array). */
|
|
105
|
+
export declare function withoutStaleInits(list: readonly GuestInit[], groups: readonly Pick<Group, "id">[]): GuestInit[];
|
|
106
|
+
/**
|
|
107
|
+
* The environment a run gets, in the order `cronCheckEnv` applies — and for
|
|
108
|
+
* the same reasons: an allow-list of the server's own variables (never a
|
|
109
|
+
* subtraction), then the profile's secrets, then the facts of THIS run, then
|
|
110
|
+
* the drop user's identity last so nothing above can keep `HOME=/root`.
|
|
111
|
+
*
|
|
112
|
+
* The conversation is handed over as DATA, in variables: a phone number or a
|
|
113
|
+
* visitor id is chosen by a stranger and must never be spliced into the text
|
|
114
|
+
* the shell parses.
|
|
115
|
+
*/
|
|
116
|
+
export declare function guestInitEnv(input: {
|
|
117
|
+
base: Record<string, string | undefined>;
|
|
118
|
+
secrets: Record<string, string>;
|
|
119
|
+
user: string | null;
|
|
120
|
+
home: string | null;
|
|
121
|
+
group: string;
|
|
122
|
+
conversation: string;
|
|
123
|
+
door: GuestInitDoor;
|
|
124
|
+
dir: string;
|
|
125
|
+
}): Record<string, string>;
|
|
126
|
+
/** `sh -c <script>`, wrapped in the same `setpriv` drop as the agent's spawn. */
|
|
127
|
+
export declare function guestInitCommand(user: string | null, script: string): {
|
|
128
|
+
cmd: string;
|
|
129
|
+
argv: string[];
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Did the run succeed? The EXIT CODE decides, which is the opposite of a cron
|
|
133
|
+
* guard (invariant 16) and on purpose: a guard exits 1 to say "nothing to
|
|
134
|
+
* report", a setup script exits non-zero to say it did not set things up.
|
|
135
|
+
*/
|
|
136
|
+
export declare function initOutcome(r: {
|
|
137
|
+
/** The exit code, or null when it was killed or never started. */
|
|
138
|
+
code: number | null;
|
|
139
|
+
timedOut: boolean;
|
|
140
|
+
/** Why the shell could not be started at all. */
|
|
141
|
+
spawnError?: string;
|
|
142
|
+
stderr: string;
|
|
143
|
+
timeoutMs: number;
|
|
144
|
+
}): {
|
|
145
|
+
ok: true;
|
|
146
|
+
} | {
|
|
147
|
+
ok: false;
|
|
148
|
+
detail: string;
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Run the script and say how it ended.
|
|
152
|
+
*
|
|
153
|
+
* Not `execFile` with a `timeout`, for one reason: that kills the shell and
|
|
154
|
+
* then waits for its output pipes to close — and a child the script left
|
|
155
|
+
* running (`something &`, or just the command the shell was in the middle of)
|
|
156
|
+
* keeps them open. A script stuck on a slow network call would hold a person's
|
|
157
|
+
* first message for as long as that call lasts, well past the ceiling. So the
|
|
158
|
+
* script gets a process group of its own, the WHOLE group is killed at the
|
|
159
|
+
* deadline, and the answer is taken when the shell exits, not when the pipes
|
|
160
|
+
* close. `killStragglers` is the caller's second sweep, for a process that left
|
|
161
|
+
* the group (the server passes "everything this Unix user runs", which before
|
|
162
|
+
* its agent exists is exactly this script).
|
|
163
|
+
*/
|
|
164
|
+
export declare function runInitScript(input: {
|
|
165
|
+
cmd: string;
|
|
166
|
+
argv: string[];
|
|
167
|
+
cwd: string;
|
|
168
|
+
env: Record<string, string>;
|
|
169
|
+
timeoutMs?: number;
|
|
170
|
+
killStragglers?: () => void | Promise<unknown>;
|
|
171
|
+
}): Promise<{
|
|
172
|
+
ok: true;
|
|
173
|
+
} | {
|
|
174
|
+
ok: false;
|
|
175
|
+
detail: string;
|
|
176
|
+
}>;
|
|
177
|
+
/** What the panel is shown. The script itself is the operator's own text. */
|
|
178
|
+
export declare function publicGuestInit(init: GuestInit | null): {
|
|
179
|
+
script: string;
|
|
180
|
+
updatedAt: number | null;
|
|
181
|
+
by: string | null;
|
|
182
|
+
last: GuestInitRun | null;
|
|
183
|
+
};
|
|
184
|
+
export declare function loadGuestInits(): GuestInit[];
|
|
185
|
+
export declare function saveGuestInits(list: readonly GuestInit[]): void;
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { CHECK_ENV_ALLOW } from "./group-exec.js";
|
|
6
|
+
import { dropArgv, dropEnv } from "./osusers.js";
|
|
7
|
+
import { instanceKey } from "./paths.js";
|
|
8
|
+
/**
|
|
9
|
+
* The FIRST-RUN SCRIPT of an external group: a shell script the operator
|
|
10
|
+
* writes once, run for every NEW conversation of that group before its agent
|
|
11
|
+
* exists, as that conversation's own Unix user and in its own directory.
|
|
12
|
+
*
|
|
13
|
+
* It exists so the account is ready before the first message is answered. The
|
|
14
|
+
* alternative is to describe the setup in the profile's prompt and let the
|
|
15
|
+
* agent do it on its first turn: slower (the person waits on a model following
|
|
16
|
+
* instructions), and it hands a stranger's very first message to an agent that
|
|
17
|
+
* is busy with something else. A script is deterministic, costs no quota, and
|
|
18
|
+
* the agent's first turn is only the answer.
|
|
19
|
+
*
|
|
20
|
+
* Three rules shape everything below.
|
|
21
|
+
*
|
|
22
|
+
* - **It runs as the conversation, never as the server.** The server is root in
|
|
23
|
+
* the only deployment where an external group exists, so "run it as the
|
|
24
|
+
* server" would make this field a root shell (the lesson of the cron guard,
|
|
25
|
+
* invariant 60). It gets the same `setpriv` drop, the same allow-listed
|
|
26
|
+
* environment and the same profile secrets as that conversation's agent — it
|
|
27
|
+
* is strictly no more powerful than the agent it prepares the ground for.
|
|
28
|
+
* - **A conversation whose script did not succeed has no agent.** A half
|
|
29
|
+
* prepared account is the one outcome nobody chose: the admission is refused,
|
|
30
|
+
* the account is erased, and the person's next message starts over.
|
|
31
|
+
* - **Only the operator writes it.** It is stored beside the other door
|
|
32
|
+
* settings, root-owned, and reachable through an admin, browser-only route.
|
|
33
|
+
*
|
|
34
|
+
* Everything here is pure except the store at the bottom, so the rules are
|
|
35
|
+
* tested with no root, no account and no shell.
|
|
36
|
+
*/
|
|
37
|
+
/** What a script may weigh. Far past any setup script; a ceiling, not a need. */
|
|
38
|
+
export const GUEST_INIT_MAX_BYTES = 16 * 1024;
|
|
39
|
+
/**
|
|
40
|
+
* How long one run may take. The person's first message waits on it, so it is
|
|
41
|
+
* short on purpose: a script that needs longer belongs in the background of
|
|
42
|
+
* the script itself, not in front of someone waiting for an answer.
|
|
43
|
+
*/
|
|
44
|
+
export const GUEST_INIT_TIMEOUT_MS = 60_000;
|
|
45
|
+
export { GUEST_INIT_CODE } from "./guest-admit.js";
|
|
46
|
+
/**
|
|
47
|
+
* A script as typed in the panel → what is stored. Line endings are unified
|
|
48
|
+
* (a textarea posts CRLF, and `sh` reads the CR as part of the last word of
|
|
49
|
+
* every line); nothing else is touched, because it is a program.
|
|
50
|
+
*/
|
|
51
|
+
export function cleanInitScript(v) {
|
|
52
|
+
if (typeof v !== "string")
|
|
53
|
+
return { ok: false, error: "the script must be text" };
|
|
54
|
+
const script = v.replace(/\r\n?/g, "\n");
|
|
55
|
+
if (script.includes("\0"))
|
|
56
|
+
return { ok: false, error: "the script contains a NUL byte" };
|
|
57
|
+
if (Buffer.byteLength(script, "utf8") > GUEST_INIT_MAX_BYTES)
|
|
58
|
+
return { ok: false, error: `the script is over ${GUEST_INIT_MAX_BYTES} bytes` };
|
|
59
|
+
return { ok: true, script: script.trim() ? script : "" };
|
|
60
|
+
}
|
|
61
|
+
function cleanRun(v) {
|
|
62
|
+
const r = (v ?? {});
|
|
63
|
+
if (typeof r.at !== "number" || typeof r.ok !== "boolean" || typeof r.conversation !== "string")
|
|
64
|
+
return undefined;
|
|
65
|
+
return {
|
|
66
|
+
at: r.at,
|
|
67
|
+
ok: r.ok,
|
|
68
|
+
conversation: r.conversation,
|
|
69
|
+
ms: typeof r.ms === "number" ? r.ms : 0,
|
|
70
|
+
...(typeof r.detail === "string" && r.detail ? { detail: r.detail } : {}),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/** The store as read from disk: malformed rows are dropped, one row per group. */
|
|
74
|
+
export function normalizeGuestInits(raw) {
|
|
75
|
+
if (!Array.isArray(raw))
|
|
76
|
+
return [];
|
|
77
|
+
const out = [];
|
|
78
|
+
const seen = new Set();
|
|
79
|
+
for (const x of raw) {
|
|
80
|
+
const r = (x ?? {});
|
|
81
|
+
if (typeof r.group !== "string" || !r.group || seen.has(r.group))
|
|
82
|
+
continue;
|
|
83
|
+
const s = cleanInitScript(r.script);
|
|
84
|
+
if (!s.ok || !s.script)
|
|
85
|
+
continue;
|
|
86
|
+
seen.add(r.group);
|
|
87
|
+
const last = cleanRun(r.last);
|
|
88
|
+
out.push({
|
|
89
|
+
group: r.group,
|
|
90
|
+
script: s.script,
|
|
91
|
+
updatedAt: typeof r.updatedAt === "number" ? r.updatedAt : 0,
|
|
92
|
+
by: typeof r.by === "string" ? r.by : "",
|
|
93
|
+
...(last ? { last } : {}),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
export function guestInitOf(list, group) {
|
|
99
|
+
return list.find((x) => x.group === group) ?? null;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Write (or, with an empty script, remove) a group's first-run script.
|
|
103
|
+
*
|
|
104
|
+
* Refused outside an EXTERNAL group: that is the only nature with one Unix
|
|
105
|
+
* account per conversation, and a script run once per stranger as the group's
|
|
106
|
+
* shared user would be writing into every other stranger's files.
|
|
107
|
+
*/
|
|
108
|
+
export function guestInitWriteFlow(input) {
|
|
109
|
+
if (input.unavailable)
|
|
110
|
+
return { ok: false, status: 409, error: input.unavailable };
|
|
111
|
+
const g = input.groups.find((x) => x.id === input.group);
|
|
112
|
+
if (!g)
|
|
113
|
+
return { ok: false, status: 404, error: "no such group" };
|
|
114
|
+
if (g.nature !== "external")
|
|
115
|
+
return { ok: false, status: 400, error: "a first-run script belongs to an external group" };
|
|
116
|
+
const s = cleanInitScript(input.script);
|
|
117
|
+
if (!s.ok)
|
|
118
|
+
return { ok: false, status: 400, error: s.error };
|
|
119
|
+
const rest = input.list.filter((x) => x.group !== input.group);
|
|
120
|
+
if (!s.script)
|
|
121
|
+
return { ok: true, list: rest, init: null };
|
|
122
|
+
const prev = guestInitOf(input.list, input.group);
|
|
123
|
+
// The last run described the PREVIOUS text: showing "failed" under a script
|
|
124
|
+
// that was just corrected would send the operator after a fixed fault.
|
|
125
|
+
const init = {
|
|
126
|
+
group: input.group,
|
|
127
|
+
script: s.script,
|
|
128
|
+
updatedAt: input.now,
|
|
129
|
+
by: input.by,
|
|
130
|
+
...(prev?.last && prev.script === s.script ? { last: prev.last } : {}),
|
|
131
|
+
};
|
|
132
|
+
return { ok: true, list: [...rest, init], init };
|
|
133
|
+
}
|
|
134
|
+
/** Record a run's outcome on its group's row (new array). */
|
|
135
|
+
export function withInitRun(list, group, run) {
|
|
136
|
+
return list.map((x) => (x.group === group ? { ...x, last: run } : x));
|
|
137
|
+
}
|
|
138
|
+
/** Drop the rows of groups that no longer exist (new array). */
|
|
139
|
+
export function withoutStaleInits(list, groups) {
|
|
140
|
+
const live = new Set(groups.map((g) => g.id));
|
|
141
|
+
return list.filter((x) => live.has(x.group));
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* The environment a run gets, in the order `cronCheckEnv` applies — and for
|
|
145
|
+
* the same reasons: an allow-list of the server's own variables (never a
|
|
146
|
+
* subtraction), then the profile's secrets, then the facts of THIS run, then
|
|
147
|
+
* the drop user's identity last so nothing above can keep `HOME=/root`.
|
|
148
|
+
*
|
|
149
|
+
* The conversation is handed over as DATA, in variables: a phone number or a
|
|
150
|
+
* visitor id is chosen by a stranger and must never be spliced into the text
|
|
151
|
+
* the shell parses.
|
|
152
|
+
*/
|
|
153
|
+
export function guestInitEnv(input) {
|
|
154
|
+
const env = {};
|
|
155
|
+
for (const [k, v] of Object.entries(input.base)) {
|
|
156
|
+
if (v !== undefined && CHECK_ENV_ALLOW.test(k))
|
|
157
|
+
env[k] = v;
|
|
158
|
+
}
|
|
159
|
+
Object.assign(env, input.secrets);
|
|
160
|
+
env.SHADOK_GROUP_ID = input.group;
|
|
161
|
+
env.SHADOK_GUEST_KEY = input.conversation;
|
|
162
|
+
env.SHADOK_GUEST_DOOR = input.door;
|
|
163
|
+
env.SHADOK_GUEST_DIR = input.dir;
|
|
164
|
+
Object.assign(env, dropEnv(input.user, input.home));
|
|
165
|
+
return env;
|
|
166
|
+
}
|
|
167
|
+
/** `sh -c <script>`, wrapped in the same `setpriv` drop as the agent's spawn. */
|
|
168
|
+
export function guestInitCommand(user, script) {
|
|
169
|
+
return user ? dropArgv(user, "sh", ["-c", script]) : { cmd: "sh", argv: ["-c", script] };
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Did the run succeed? The EXIT CODE decides, which is the opposite of a cron
|
|
173
|
+
* guard (invariant 16) and on purpose: a guard exits 1 to say "nothing to
|
|
174
|
+
* report", a setup script exits non-zero to say it did not set things up.
|
|
175
|
+
*/
|
|
176
|
+
export function initOutcome(r) {
|
|
177
|
+
if (r.timedOut)
|
|
178
|
+
return { ok: false, detail: `timed out after ${Math.round(r.timeoutMs / 1000)}s` };
|
|
179
|
+
if (r.spawnError)
|
|
180
|
+
return { ok: false, detail: r.spawnError.split("\n")[0].slice(0, 200) };
|
|
181
|
+
if (r.code === 0)
|
|
182
|
+
return { ok: true };
|
|
183
|
+
const first = (r.stderr || "").trim().split("\n")[0].slice(0, 200);
|
|
184
|
+
const what = r.code === null ? "killed" : `exit ${r.code}`;
|
|
185
|
+
return { ok: false, detail: `${what}${first ? `: ${first}` : ""}` };
|
|
186
|
+
}
|
|
187
|
+
/** How much of stderr is kept: the first line is all the panel shows. */
|
|
188
|
+
const STDERR_KEEP = 64 * 1024;
|
|
189
|
+
/**
|
|
190
|
+
* Run the script and say how it ended.
|
|
191
|
+
*
|
|
192
|
+
* Not `execFile` with a `timeout`, for one reason: that kills the shell and
|
|
193
|
+
* then waits for its output pipes to close — and a child the script left
|
|
194
|
+
* running (`something &`, or just the command the shell was in the middle of)
|
|
195
|
+
* keeps them open. A script stuck on a slow network call would hold a person's
|
|
196
|
+
* first message for as long as that call lasts, well past the ceiling. So the
|
|
197
|
+
* script gets a process group of its own, the WHOLE group is killed at the
|
|
198
|
+
* deadline, and the answer is taken when the shell exits, not when the pipes
|
|
199
|
+
* close. `killStragglers` is the caller's second sweep, for a process that left
|
|
200
|
+
* the group (the server passes "everything this Unix user runs", which before
|
|
201
|
+
* its agent exists is exactly this script).
|
|
202
|
+
*/
|
|
203
|
+
export function runInitScript(input) {
|
|
204
|
+
const timeoutMs = input.timeoutMs ?? GUEST_INIT_TIMEOUT_MS;
|
|
205
|
+
return new Promise((resolve) => {
|
|
206
|
+
let stderr = "";
|
|
207
|
+
let timedOut = false;
|
|
208
|
+
let settled = false;
|
|
209
|
+
let timer = null;
|
|
210
|
+
const settle = (code, spawnError) => {
|
|
211
|
+
if (settled)
|
|
212
|
+
return;
|
|
213
|
+
settled = true;
|
|
214
|
+
if (timer)
|
|
215
|
+
clearTimeout(timer);
|
|
216
|
+
resolve(initOutcome({ code, timedOut, spawnError, stderr, timeoutMs }));
|
|
217
|
+
};
|
|
218
|
+
let child;
|
|
219
|
+
try {
|
|
220
|
+
child = spawn(input.cmd, input.argv, {
|
|
221
|
+
cwd: input.cwd,
|
|
222
|
+
env: input.env,
|
|
223
|
+
detached: true,
|
|
224
|
+
stdio: ["ignore", "ignore", "pipe"],
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
catch (e) {
|
|
228
|
+
return settle(null, e instanceof Error ? e.message : String(e));
|
|
229
|
+
}
|
|
230
|
+
child.stderr?.on("data", (d) => {
|
|
231
|
+
if (stderr.length < STDERR_KEEP)
|
|
232
|
+
stderr += d.toString("utf8");
|
|
233
|
+
});
|
|
234
|
+
child.on("error", (e) => settle(null, e.message));
|
|
235
|
+
child.on("exit", (code) => settle(code));
|
|
236
|
+
timer = setTimeout(() => {
|
|
237
|
+
timedOut = true;
|
|
238
|
+
try {
|
|
239
|
+
if (child.pid)
|
|
240
|
+
process.kill(-child.pid, "SIGKILL");
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
/* the group is already gone */
|
|
244
|
+
}
|
|
245
|
+
void Promise.resolve(input.killStragglers?.()).catch(() => { });
|
|
246
|
+
// The shell's own exit normally settles this; if the kill could not
|
|
247
|
+
// reach it, do not leave the admission hanging on a process we lost.
|
|
248
|
+
setTimeout(() => settle(null), 2000).unref();
|
|
249
|
+
}, timeoutMs);
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
/** What the panel is shown. The script itself is the operator's own text. */
|
|
253
|
+
export function publicGuestInit(init) {
|
|
254
|
+
return {
|
|
255
|
+
script: init?.script ?? "",
|
|
256
|
+
updatedAt: init?.updatedAt ?? null,
|
|
257
|
+
by: init?.by || null,
|
|
258
|
+
last: init?.last ?? null,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
// ── Store ─────────────────────────────────────────────────────────────────
|
|
262
|
+
function storeFile() {
|
|
263
|
+
return path.join(os.homedir(), ".shadok-ai", "guest-init", instanceKey() + ".json");
|
|
264
|
+
}
|
|
265
|
+
export function loadGuestInits() {
|
|
266
|
+
try {
|
|
267
|
+
return normalizeGuestInits(JSON.parse(fs.readFileSync(storeFile(), "utf8")));
|
|
268
|
+
}
|
|
269
|
+
catch {
|
|
270
|
+
return [];
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
export function saveGuestInits(list) {
|
|
274
|
+
const f = storeFile();
|
|
275
|
+
fs.mkdirSync(path.dirname(f), { recursive: true, mode: 0o700 });
|
|
276
|
+
const tmp = `${f}.${process.pid}.tmp`;
|
|
277
|
+
fs.writeFileSync(tmp, JSON.stringify(list, null, 2), { mode: 0o600 });
|
|
278
|
+
fs.renameSync(tmp, f);
|
|
279
|
+
}
|
|
280
|
+
//# sourceMappingURL=guest-init.js.map
|