Merge branch 'main' into fix/poll-loop-prescripts-on-followups
This commit is contained in:
11
CLAUDE.md
11
CLAUDE.md
@@ -157,6 +157,17 @@ Four types of skills. See [CONTRIBUTING.md](CONTRIBUTING.md) for the full taxono
|
|||||||
|
|
||||||
Before creating a PR, adding a skill, or preparing any contribution, you MUST read [CONTRIBUTING.md](CONTRIBUTING.md). It covers accepted change types, the four skill types and their guidelines, `SKILL.md` format rules, and the pre-submission checklist.
|
Before creating a PR, adding a skill, or preparing any contribution, you MUST read [CONTRIBUTING.md](CONTRIBUTING.md). It covers accepted change types, the four skill types and their guidelines, `SKILL.md` format rules, and the pre-submission checklist.
|
||||||
|
|
||||||
|
## PR Hygiene
|
||||||
|
|
||||||
|
Before creating a PR, run these checks:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git diff upstream/main --stat HEAD
|
||||||
|
git log upstream/main..HEAD --oneline
|
||||||
|
```
|
||||||
|
|
||||||
|
Show the output and wait for approval. Installation-specific files (group files, .claude/settings.json, local configs) should not be included.
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
Run commands directly — don't tell the user to run them.
|
Run commands directly — don't tell the user to run them.
|
||||||
|
|||||||
@@ -123,7 +123,8 @@ Test your contribution on a fresh clone before submitting. For skills, run the s
|
|||||||
|
|
||||||
1. **Link related issues.** If your PR resolves an open issue, include `Closes #123` in the description so it's auto-closed on merge.
|
1. **Link related issues.** If your PR resolves an open issue, include `Closes #123` in the description so it's auto-closed on merge.
|
||||||
2. **Test thoroughly.** Run the feature yourself. For skills, test on a fresh clone.
|
2. **Test thoroughly.** Run the feature yourself. For skills, test on a fresh clone.
|
||||||
3. **Check the right box** in the PR template. Labels are auto-applied based on your selection:
|
3. **Check for installation-specific files.** Before creating a PR, verify no installation-specific files are in your diff (see PR Hygiene in CLAUDE.md).
|
||||||
|
4. **Check the right box** in the PR template. Labels are auto-applied based on your selection:
|
||||||
|
|
||||||
| Checkbox | Label |
|
| Checkbox | Label |
|
||||||
|----------|-------|
|
|----------|-------|
|
||||||
|
|||||||
30
assets/setup-splash.txt
Normal file
30
assets/setup-splash.txt
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
|
||||||
|
[38;2;43;183;206m⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠰⣄⠘⣦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀[39m
|
||||||
|
[38;2;43;183;206m⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⡆⢸⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀[39m [2m[38;2;43;183;206m°[39m[22m
|
||||||
|
[38;2;43;183;206m⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀[39m
|
||||||
|
[38;2;43;183;206m⠀⠀⠀⠀⠀⢀⣠⣴⠾⠟⠛⠛⠿⢶⣦⣾⠇⣾⠁⠀⠀⠀⢀⣤⣤⠀⢀⣄⠀[39m
|
||||||
|
[38;2;43;183;206m⠀⠀⠀⠀⣴⡿⡋⠀⠀⠀⠀⠀⢤⣾⣿⢛⢿⣏⠀⠀⠀⢰⣟⣽⡏⠀⣸⡿⣧[39m
|
||||||
|
[2m[38;2;43;183;206mo[39m[22m [38;2;43;183;206m⠀⠀⢀⣾⠋⠀⠀⠀⠀⠀⠀⠀⠀⠘⠈⣧⣀⣿⣧⠀⠀⣿⣼⣿⣇⣾⠋⢠⣿[39m
|
||||||
|
[38;2;43;183;206m⠀⠀⣾⢃⠀⢲⣷⡋⣰⡀⢀⣀⣀⡀⠠⣿⣿⣠⣿⣇⠀⣿⢻⣉⠉⠙⠠⣼⠇[39m
|
||||||
|
[38;2;43;183;206m⠀⣼⡏⠃⠀⢸⣿⣿⡿⠃⣾⣷⣻⣿⡏⢹⠿⠿⣿⣿⢀⣿⣐⠙⣷⣦⡾⠋⠀[39m [2m[38;2;43;183;206mo[39m[22m
|
||||||
|
[38;2;43;183;206m⢠⣿⡃⠀⠀⠀⠀⠀⠈⠀⠀⠉⠙⠁⠀⠀⠀⠐⣿⣿⣟⠁⣿⣿⠟⠋⠀⠀⠀[39m
|
||||||
|
[2m[38;2;43;183;206m°[39m[22m [38;2;43;183;206m⢸⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣨⣿⣿⣿⣿⣿⠟⠁⠀⠀⠀⠀⠀[39m
|
||||||
|
[38;2;43;183;206m⢸⣿⣿⣷⣤⣤⠀⣀⢀⠀⢀⣀⣠⣴⣶⣿⣿⣿⣿⡿⠛⠁⠀⠀⠀⠀⠀⠀⠀[39m
|
||||||
|
[38;2;43;183;206m⣿⢋⠿⣿⣿⣿⣿⡿⣿⣿⣿⣿⣿⣿⠿⠿⠿⣿⣅⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀[39m [38;2;43;183;206mO[39m
|
||||||
|
[38;2;43;183;206m⣿⣿⠙⢾⣽⣟⣿⣿⣼⣿⣿⣿⣩⣶⣶⣦⠀⠀⠩⢻⣆⠀⠀⠀⠀⠀⠀⠀⠀[39m
|
||||||
|
[38;2;43;183;206m⠘⣿⣶⣤⣿⣿⣿⣿⣵⢖⡀⠉⠹⡛⢷⣝⡿⠁⠀⠀⣿⡆⠀⠀⠀⠀⠀⠀⠀[39m
|
||||||
|
[38;2;43;183;206m⠀⢹⣯⣽⣟⣛⣻⣿⣿⣾⣽⢶⣽⣿⣿⣿⣏⠀⠠⣤⣿⡇⠀⠀⠀⠀⠀⠀⠀[39m
|
||||||
|
[38;2;43;183;206m⠀⠀⠻⣿⣶⣾⣿⢿⣻⣿⣿⣿⣿⣿⣿⣏⣛⣧⣦⣿⣿⣧⣄⠀⠀⠀⠀⠀⠀[39m
|
||||||
|
[38;2;43;183;206mo[39m [38;2;43;183;206m⠀⠀⠀⠈⠻⣿⣶⣥⣼⣿⣿⣽⣿⣿⣿⣷⣶⣾⣿⣿⣯⣘⣿⣧⠀⠀⠀⠀⠀[39m
|
||||||
|
[38;2;43;183;206m⠀⠀⠀⠀⠤⣤⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠿⠿⠋⠀⠀⠀⠀⠀[39m
|
||||||
|
|
||||||
|
[1m _ _ [22m[38;2;43;183;206m[1m ___ _ [22m[39m
|
||||||
|
[1m| \| |__ _ _ _ ___ [22m[38;2;43;183;206m[1m / __| |__ ___ __ __[22m[39m
|
||||||
|
[1m| .` / _` | ' \/ _ \[22m[38;2;43;183;206m[1m| (__| / _` \ V V /[22m[39m
|
||||||
|
[1m|_|\_\__,_|_||_\___/[22m[38;2;43;183;206m[1m \___|_\__,_|\_/\_/ [22m[39m
|
||||||
|
|
||||||
|
[2mSmall.[22m
|
||||||
|
[2mRuns on your machine.[22m
|
||||||
|
[2mYours to modify.[22m
|
||||||
|
|
||||||
|
[38;2;5;62;165m════════════════════════════════════════[39m
|
||||||
@@ -89,6 +89,9 @@ export const scheduleTask: McpToolDefinition = {
|
|||||||
script,
|
script,
|
||||||
processAfter,
|
processAfter,
|
||||||
recurrence,
|
recurrence,
|
||||||
|
platformId: r.platform_id,
|
||||||
|
channelType: r.channel_type,
|
||||||
|
threadId: r.thread_id,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -226,8 +226,12 @@ function createPreCompactHook(assistantName?: string): HookCallback {
|
|||||||
/**
|
/**
|
||||||
* Claude Code auto-compacts context at this window (tokens). Kept here so
|
* Claude Code auto-compacts context at this window (tokens). Kept here so
|
||||||
* the generic bootstrap doesn't need to know about Claude-specific env vars.
|
* the generic bootstrap doesn't need to know about Claude-specific env vars.
|
||||||
|
*
|
||||||
|
* Operator override: set CLAUDE_CODE_AUTO_COMPACT_WINDOW in the host env to
|
||||||
|
* raise or lower the threshold without editing source — useful when running
|
||||||
|
* with a 1M-context model variant or when emergency-tuning a deployment.
|
||||||
*/
|
*/
|
||||||
const CLAUDE_CODE_AUTO_COMPACT_WINDOW = '165000';
|
const CLAUDE_CODE_AUTO_COMPACT_WINDOW = process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW || '165000';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stale-session detection. Matches Claude Code's error text when a
|
* Stale-session detection. Matches Claude Code's error text when a
|
||||||
|
|||||||
47
nanoclaw.sh
47
nanoclaw.sh
@@ -129,10 +129,46 @@ rm -f "$PROGRESS_LOG"
|
|||||||
mkdir -p "$STEPS_DIR" "$LOGS_DIR"
|
mkdir -p "$STEPS_DIR" "$LOGS_DIR"
|
||||||
write_header
|
write_header
|
||||||
|
|
||||||
# NanoClaw wordmark — clack's intro carries the "let's get you set up" framing,
|
# NanoClaw splash — under-the-sea lobster mascot in truecolor braille,
|
||||||
# so we don't print a subtitle here. setup:auto sees NANOCLAW_BOOTSTRAPPED=1 and
|
# with the figlet wordmark and taglines below. Pre-rendered into
|
||||||
# skips re-printing the wordmark, keeping the flow visually continuous.
|
# assets/setup-splash.txt (built from assets/nanoclaw-icon.png via chafa +
|
||||||
printf '\n %s%s\n\n' "$(bold 'Nano')" "$(brand_bold 'Claw')"
|
# figlet); the bash script just streams the literal frame. clack's intro
|
||||||
|
# then carries the "let's get you set up" framing — setup:auto sees
|
||||||
|
# NANOCLAW_BOOTSTRAPPED=1 and skips re-printing the wordmark.
|
||||||
|
cat "$PROJECT_ROOT/assets/setup-splash.txt"
|
||||||
|
|
||||||
|
# ─── pre-flight: root user warning (Linux) ────────────────────────────
|
||||||
|
if [ "$(uname -s)" = "Linux" ] && [ "$(id -u)" -eq 0 ]; then
|
||||||
|
printf ' %s\n' \
|
||||||
|
"$(red 'Warning: you are running as root.')"
|
||||||
|
printf ' %s\n' \
|
||||||
|
"$(dim "Running NanoClaw as root is not recommended. It can cause permission")"
|
||||||
|
printf ' %s\n\n' \
|
||||||
|
"$(dim "issues with containers, services, and file ownership.")"
|
||||||
|
printf ' %s\n' "$(bold '1)') $(dim 'Show me instructions for creating a new Linux user')"
|
||||||
|
printf ' %s\n\n' "$(bold '2)') $(dim 'Continue setting up NanoClaw as root user (not recommended)')"
|
||||||
|
read -r -p " $(bold 'Choose [1/2]: ')" ROOT_ANS </dev/tty
|
||||||
|
|
||||||
|
case "${ROOT_ANS:-1}" in
|
||||||
|
2)
|
||||||
|
ph_event setup_root_continued
|
||||||
|
printf '\n'
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
ph_event setup_root_aborted
|
||||||
|
printf '\n %s\n' "$(bold 'To set up a regular user (via SSH):')"
|
||||||
|
printf ' %s\n\n' "$(dim 'Not using SSH? Refer to your hosting provider docs or ask your coding agent to help you set up SSH access.')"
|
||||||
|
printf ' %s\n' "$(dim '1. Create a new user: adduser nanoclaw')"
|
||||||
|
printf ' %s\n' "$(dim '2. Add to sudo group: usermod -aG sudo nanoclaw')"
|
||||||
|
printf ' %s\n' "$(dim '3. Enable passwordless sudo: echo "nanoclaw ALL=(ALL) NOPASSWD:ALL" | tee /etc/sudoers.d/nanoclaw')"
|
||||||
|
printf ' %s\n' "$(dim '4. Log out: exit')"
|
||||||
|
printf ' %s\n' "$(dim '5. Log back in as the new user: ssh nanoclaw@your-server')"
|
||||||
|
printf ' %s\n' "$(dim '6. Clone the repo: git clone https://github.com/qwibitai/nanoclaw.git && cd nanoclaw')"
|
||||||
|
printf ' %s\n\n' "$(dim '7. Re-run setup: bash nanoclaw.sh')"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
# ─── pre-flight: Homebrew on macOS ─────────────────────────────────────
|
# ─── pre-flight: Homebrew on macOS ─────────────────────────────────────
|
||||||
# setup/install-node.sh and setup/install-docker.sh both require `brew` on
|
# setup/install-node.sh and setup/install-docker.sh both require `brew` on
|
||||||
@@ -188,9 +224,6 @@ BOOTSTRAP_RAW="${STEPS_DIR}/01-bootstrap.log"
|
|||||||
BOOTSTRAP_LABEL="Installing the basics"
|
BOOTSTRAP_LABEL="Installing the basics"
|
||||||
BOOTSTRAP_START=$(date +%s)
|
BOOTSTRAP_START=$(date +%s)
|
||||||
|
|
||||||
# One-line "why" that teaches a differentiator while the user waits.
|
|
||||||
printf '%s %s\n' "$(gray '│')" \
|
|
||||||
"$(dim "Small. Runs on your machine. Yours to modify.")"
|
|
||||||
spinner_start "$BOOTSTRAP_LABEL"
|
spinner_start "$BOOTSTRAP_LABEL"
|
||||||
|
|
||||||
# Run in the background so we can tick elapsed time. Capture exit code via
|
# Run in the background so we can tick elapsed time. Capture exit code via
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nanoclaw",
|
"name": "nanoclaw",
|
||||||
"version": "2.0.15",
|
"version": "2.0.21",
|
||||||
"description": "Personal Claude assistant. Lightweight, secure, customizable.",
|
"description": "Personal Claude assistant. Lightweight, secure, customizable.",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"packageManager": "pnpm@10.33.0",
|
"packageManager": "pnpm@10.33.0",
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="90" height="20" role="img" aria-label="133k tokens, 67% of context window">
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="90" height="20" role="img" aria-label="138k tokens, 69% of context window">
|
||||||
<title>133k tokens, 67% of context window</title>
|
<title>138k tokens, 69% of context window</title>
|
||||||
<linearGradient id="s" x2="0" y2="100%">
|
<linearGradient id="s" x2="0" y2="100%">
|
||||||
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
|
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
|
||||||
<stop offset="1" stop-opacity=".1"/>
|
<stop offset="1" stop-opacity=".1"/>
|
||||||
@@ -15,8 +15,8 @@
|
|||||||
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
|
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
|
||||||
<text aria-hidden="true" x="26" y="15" fill="#010101" fill-opacity=".3">tokens</text>
|
<text aria-hidden="true" x="26" y="15" fill="#010101" fill-opacity=".3">tokens</text>
|
||||||
<text x="26" y="14">tokens</text>
|
<text x="26" y="14">tokens</text>
|
||||||
<text aria-hidden="true" x="71" y="15" fill="#010101" fill-opacity=".3">133k</text>
|
<text aria-hidden="true" x="71" y="15" fill="#010101" fill-opacity=".3">138k</text>
|
||||||
<text x="71" y="14">133k</text>
|
<text x="71" y="14">138k</text>
|
||||||
</g>
|
</g>
|
||||||
</g>
|
</g>
|
||||||
</a>
|
</a>
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
75
scripts/delete-cli-agent.ts
Normal file
75
scripts/delete-cli-agent.ts
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
/**
|
||||||
|
* Delete the scratch CLI agent created during setup's ping-pong test.
|
||||||
|
*
|
||||||
|
* Dynamically finds and removes all rows referencing the agent group
|
||||||
|
* (any table with an agent_group_id column), deletes the agent group
|
||||||
|
* itself, and removes the groups/<folder>/ directory. Leaves the CLI
|
||||||
|
* messaging group intact so it can be reused for a new agent.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* pnpm exec tsx scripts/delete-cli-agent.ts --folder <folder-name>
|
||||||
|
*/
|
||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
import { DATA_DIR } from '../src/config.js';
|
||||||
|
import { getAgentGroupByFolder, deleteAgentGroup } from '../src/db/agent-groups.js';
|
||||||
|
import { initDb } from '../src/db/connection.js';
|
||||||
|
import { runMigrations } from '../src/db/migrations/index.js';
|
||||||
|
|
||||||
|
interface Args {
|
||||||
|
folder: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseArgs(): Args {
|
||||||
|
const argv = process.argv.slice(2);
|
||||||
|
let folder = '';
|
||||||
|
for (let i = 0; i < argv.length; i++) {
|
||||||
|
if (argv[i] === '--folder' && argv[i + 1]) folder = argv[++i];
|
||||||
|
}
|
||||||
|
if (!folder) {
|
||||||
|
console.error('usage: pnpm exec tsx scripts/delete-cli-agent.ts --folder <folder-name>');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
return { folder };
|
||||||
|
}
|
||||||
|
|
||||||
|
const args = parseArgs();
|
||||||
|
|
||||||
|
const db = initDb(path.join(DATA_DIR, 'v2.db'));
|
||||||
|
runMigrations(db);
|
||||||
|
|
||||||
|
const ag = getAgentGroupByFolder(args.folder);
|
||||||
|
if (!ag) {
|
||||||
|
console.log(`No agent group with folder "${args.folder}" — nothing to delete.`);
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
const cleanup = db.transaction(() => {
|
||||||
|
const tables = db
|
||||||
|
.prepare(
|
||||||
|
`SELECT DISTINCT m.name FROM sqlite_master m
|
||||||
|
JOIN pragma_table_info(m.name) p ON p.name = 'agent_group_id'
|
||||||
|
WHERE m.type = 'table' AND m.name != 'agent_groups'`,
|
||||||
|
)
|
||||||
|
.all() as { name: string }[];
|
||||||
|
for (const { name } of tables) {
|
||||||
|
db.prepare(`DELETE FROM ${name} WHERE agent_group_id = ?`).run(ag.id);
|
||||||
|
}
|
||||||
|
deleteAgentGroup(ag.id);
|
||||||
|
});
|
||||||
|
cleanup();
|
||||||
|
|
||||||
|
// Remove the groups/<folder>/ directory.
|
||||||
|
const groupDir = path.join(process.cwd(), 'groups', args.folder);
|
||||||
|
if (fs.existsSync(groupDir)) {
|
||||||
|
fs.rmSync(groupDir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove session data on disk.
|
||||||
|
const sessionsDir = path.join(DATA_DIR, 'v2-sessions', ag.id);
|
||||||
|
if (fs.existsSync(sessionsDir)) {
|
||||||
|
fs.rmSync(sessionsDir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Deleted agent group ${ag.id} (${args.folder}).`);
|
||||||
@@ -41,11 +41,13 @@ const CLI_SYNTHETIC_USER_ID = `${CLI_CHANNEL}:${CLI_PLATFORM_ID}`;
|
|||||||
interface Args {
|
interface Args {
|
||||||
displayName: string;
|
displayName: string;
|
||||||
agentName: string;
|
agentName: string;
|
||||||
|
folder?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseArgs(argv: string[]): Args {
|
function parseArgs(argv: string[]): Args {
|
||||||
let displayName: string | undefined;
|
let displayName: string | undefined;
|
||||||
let agentName: string | undefined;
|
let agentName: string | undefined;
|
||||||
|
let folder: string | undefined;
|
||||||
for (let i = 0; i < argv.length; i++) {
|
for (let i = 0; i < argv.length; i++) {
|
||||||
const key = argv[i];
|
const key = argv[i];
|
||||||
const val = argv[i + 1];
|
const val = argv[i + 1];
|
||||||
@@ -55,6 +57,9 @@ function parseArgs(argv: string[]): Args {
|
|||||||
} else if (key === '--agent-name') {
|
} else if (key === '--agent-name') {
|
||||||
agentName = val;
|
agentName = val;
|
||||||
i++;
|
i++;
|
||||||
|
} else if (key === '--folder') {
|
||||||
|
folder = val;
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,6 +72,7 @@ function parseArgs(argv: string[]): Args {
|
|||||||
return {
|
return {
|
||||||
displayName,
|
displayName,
|
||||||
agentName: agentName?.trim() || displayName,
|
agentName: agentName?.trim() || displayName,
|
||||||
|
folder,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +101,7 @@ async function main(): Promise<void> {
|
|||||||
const promotedToOwner = false;
|
const promotedToOwner = false;
|
||||||
|
|
||||||
// 2. Agent group + filesystem.
|
// 2. Agent group + filesystem.
|
||||||
const folder = `cli-with-${normalizeName(args.displayName)}`;
|
const folder = args.folder || `cli-with-${normalizeName(args.displayName)}`;
|
||||||
let ag: AgentGroup | undefined = getAgentGroupByFolder(folder);
|
let ag: AgentGroup | undefined = getAgentGroupByFolder(folder);
|
||||||
if (!ag) {
|
if (!ag) {
|
||||||
const agId = generateId('ag');
|
const agId = generateId('ag');
|
||||||
|
|||||||
171
setup/auto.ts
171
setup/auto.ts
@@ -51,9 +51,9 @@ import { pollHealth } from './onecli.js';
|
|||||||
import { getLaunchdLabel, getSystemdUnit } from '../src/install-slug.js';
|
import { getLaunchdLabel, getSystemdUnit } from '../src/install-slug.js';
|
||||||
import { claudeCliAvailable, resolveTimezoneViaClaude } from './lib/tz-from-claude.js';
|
import { claudeCliAvailable, resolveTimezoneViaClaude } from './lib/tz-from-claude.js';
|
||||||
import * as setupLog from './logs.js';
|
import * as setupLog from './logs.js';
|
||||||
import { ensureAnswer, fail, runQuietChild, runQuietStep } from './lib/runner.js';
|
import { ensureAnswer, fail, runQuietChild, runQuietStep, spawnQuiet } from './lib/runner.js';
|
||||||
import { emit as phEmit } from './lib/diagnostics.js';
|
import { emit as phEmit } from './lib/diagnostics.js';
|
||||||
import { accentGreen, brandBody, brandBold, brandChip, dimWrap, fitToWidth, note, wrapForGutter } from './lib/theme.js';
|
import { accentGreen, brandBody, brandBold, brandChip, dimWrap, fitToWidth, fmtDuration, note, wrapForGutter } from './lib/theme.js';
|
||||||
import { isValidTimezone } from '../src/timezone.js';
|
import { isValidTimezone } from '../src/timezone.js';
|
||||||
|
|
||||||
const CLI_AGENT_NAME = 'Terminal Agent';
|
const CLI_AGENT_NAME = 'Terminal Agent';
|
||||||
@@ -85,17 +85,21 @@ async function main(): Promise<void> {
|
|||||||
|
|
||||||
// Welcome menu — default path or open advanced overrides before any setup
|
// Welcome menu — default path or open advanced overrides before any setup
|
||||||
// work begins. Default lands on standard so Enter is the happy path.
|
// work begins. Default lands on standard so Enter is the happy path.
|
||||||
const startChoice = ensureAnswer(
|
// On sg re-exec, the user already chose — skip straight to standard.
|
||||||
await brightSelect<'default' | 'advanced'>({
|
let startChoice: 'default' | 'advanced' = 'default';
|
||||||
message: 'How would you like to begin?',
|
if (process.env.NANOCLAW_REEXEC_SG !== '1') {
|
||||||
options: [
|
startChoice = ensureAnswer(
|
||||||
{ value: 'default', label: 'Standard setup' },
|
await brightSelect<'default' | 'advanced'>({
|
||||||
{ value: 'advanced', label: 'Advanced', hint: 'override defaults' },
|
message: 'How would you like to begin?',
|
||||||
],
|
options: [
|
||||||
initialValue: 'default',
|
{ value: 'default', label: 'Standard setup' },
|
||||||
}),
|
{ value: 'advanced', label: 'Advanced', hint: 'override defaults' },
|
||||||
) as 'default' | 'advanced';
|
],
|
||||||
setupLog.userInput('start_choice', startChoice);
|
initialValue: 'default',
|
||||||
|
}),
|
||||||
|
) as 'default' | 'advanced';
|
||||||
|
setupLog.userInput('start_choice', startChoice);
|
||||||
|
}
|
||||||
if (startChoice === 'advanced') {
|
if (startChoice === 'advanced') {
|
||||||
configValues = await runAdvancedScreen(configValues);
|
configValues = await runAdvancedScreen(configValues);
|
||||||
applyToEnv(configValues);
|
applyToEnv(configValues);
|
||||||
@@ -122,39 +126,6 @@ async function main(): Promise<void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detect existing .env and offer to reuse it so the user doesn't have to
|
|
||||||
// paste credentials again on a re-run.
|
|
||||||
const existingEnv = detectExistingEnv();
|
|
||||||
if (existingEnv) {
|
|
||||||
const lines = Object.values(existingEnv.groups).map(
|
|
||||||
(g) => ` ${k.green('✓')} ${g.label}`,
|
|
||||||
);
|
|
||||||
note(lines.join('\n'), 'Found existing configuration');
|
|
||||||
|
|
||||||
const reuseChoice = ensureAnswer(
|
|
||||||
await brightSelect({
|
|
||||||
message: 'Use this existing environment?',
|
|
||||||
options: [
|
|
||||||
{ value: 'reuse', label: 'Yes, use what I already have', hint: 'recommended' },
|
|
||||||
{ value: 'fresh', label: 'No, start fresh' },
|
|
||||||
],
|
|
||||||
initialValue: 'reuse',
|
|
||||||
}),
|
|
||||||
) as 'reuse' | 'fresh';
|
|
||||||
setupLog.userInput('existing_env_choice', reuseChoice);
|
|
||||||
|
|
||||||
if (reuseChoice === 'reuse') {
|
|
||||||
for (const [key, value] of Object.entries(existingEnv.raw)) {
|
|
||||||
if (!process.env[key]) process.env[key] = value;
|
|
||||||
}
|
|
||||||
if (existingEnv.groups.onecli) skip.add('onecli');
|
|
||||||
if (detectRegisteredGroups(process.cwd())) {
|
|
||||||
skip.add('cli-agent');
|
|
||||||
skip.add('first-chat');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!skip.has('container')) {
|
if (!skip.has('container')) {
|
||||||
p.log.message(brandBody(dimWrap('Your assistant lives in its own sandbox. It can only see what you explicitly share.', 4)));
|
p.log.message(brandBody(dimWrap('Your assistant lives in its own sandbox. It can only see what you explicitly share.', 4)));
|
||||||
p.log.message(
|
p.log.message(
|
||||||
@@ -344,6 +315,11 @@ async function main(): Promise<void> {
|
|||||||
return displayName;
|
return displayName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!skip.has('cli-agent') && detectRegisteredGroups(process.cwd())) {
|
||||||
|
skip.add('cli-agent');
|
||||||
|
skip.add('first-chat');
|
||||||
|
}
|
||||||
|
|
||||||
if (!skip.has('cli-agent')) {
|
if (!skip.has('cli-agent')) {
|
||||||
await resolveDisplayName();
|
await resolveDisplayName();
|
||||||
const res = await runQuietStep(
|
const res = await runQuietStep(
|
||||||
@@ -352,7 +328,7 @@ async function main(): Promise<void> {
|
|||||||
running: 'Bringing your assistant online…',
|
running: 'Bringing your assistant online…',
|
||||||
done: 'Assistant wired up.',
|
done: 'Assistant wired up.',
|
||||||
},
|
},
|
||||||
['--display-name', displayName!, '--agent-name', CLI_AGENT_NAME],
|
['--display-name', displayName!, '--agent-name', CLI_AGENT_NAME, '--folder', '_ping-test'],
|
||||||
);
|
);
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
await fail(
|
await fail(
|
||||||
@@ -373,6 +349,27 @@ async function main(): Promise<void> {
|
|||||||
const ping = await confirmAssistantResponds();
|
const ping = await confirmAssistantResponds();
|
||||||
if (ping === 'ok') {
|
if (ping === 'ok') {
|
||||||
phEmit('first_chat_ready');
|
phEmit('first_chat_ready');
|
||||||
|
const cleanupRawLog = setupLog.stepRawLog('cleanup-cli-agent');
|
||||||
|
const cleanupStart = Date.now();
|
||||||
|
const cleanup = await spawnQuiet(
|
||||||
|
'pnpm',
|
||||||
|
['exec', 'tsx', 'scripts/delete-cli-agent.ts', '--folder', '_ping-test'],
|
||||||
|
cleanupRawLog,
|
||||||
|
);
|
||||||
|
setupLog.step(
|
||||||
|
'cleanup-cli-agent',
|
||||||
|
cleanup.ok ? 'success' : 'failed',
|
||||||
|
Date.now() - cleanupStart,
|
||||||
|
{ exit_code: cleanup.exitCode },
|
||||||
|
cleanupRawLog,
|
||||||
|
);
|
||||||
|
if (!cleanup.ok) {
|
||||||
|
p.log.warn(
|
||||||
|
brandBody(
|
||||||
|
`Couldn't clean up the test agent — it may still appear in your agent list. See ${cleanupRawLog} for details.`,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
const next = ensureAnswer(
|
const next = ensureAnswer(
|
||||||
await brightSelect<'continue' | 'chat'>({
|
await brightSelect<'continue' | 'chat'>({
|
||||||
message: 'What next?',
|
message: 'What next?',
|
||||||
@@ -390,7 +387,23 @@ async function main(): Promise<void> {
|
|||||||
}),
|
}),
|
||||||
) as 'continue' | 'chat';
|
) as 'continue' | 'chat';
|
||||||
setupLog.userInput('first_chat_choice', next);
|
setupLog.userInput('first_chat_choice', next);
|
||||||
if (next === 'chat') await runFirstChat();
|
if (next === 'chat') {
|
||||||
|
const terminalAgentName = `${displayName!}'s Terminal`;
|
||||||
|
const createRes = await runQuietChild(
|
||||||
|
'create-terminal-agent',
|
||||||
|
'pnpm',
|
||||||
|
['exec', 'tsx', 'scripts/init-cli-agent.ts', '--display-name', displayName!, '--agent-name', terminalAgentName],
|
||||||
|
{ running: `Creating ${terminalAgentName}…`, done: `${terminalAgentName} is ready.` },
|
||||||
|
);
|
||||||
|
if (!createRes.ok) {
|
||||||
|
await fail(
|
||||||
|
'create-terminal-agent',
|
||||||
|
`Couldn't create ${terminalAgentName}.`,
|
||||||
|
'You can retry later with `pnpm exec tsx scripts/init-cli-agent.ts`.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
await runFirstChat();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
phEmit('first_chat_failed', { reason: ping });
|
phEmit('first_chat_failed', { reason: ping });
|
||||||
renderPingFailureNote(ping);
|
renderPingFailureNote(ping);
|
||||||
@@ -579,18 +592,16 @@ async function confirmAssistantResponds(): Promise<PingResult> {
|
|||||||
const s = p.spinner();
|
const s = p.spinner();
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
const label = 'Waking your assistant…';
|
const label = 'Waking your assistant…';
|
||||||
s.start(fitToWidth(label, ' (999s)'));
|
s.start(fitToWidth(label, ' (99m 59s)'));
|
||||||
const tick = setInterval(() => {
|
const tick = setInterval(() => {
|
||||||
const elapsed = Math.round((Date.now() - start) / 1000);
|
const suffix = ` (${fmtDuration(Date.now() - start)})`;
|
||||||
const suffix = ` (${elapsed}s)`;
|
|
||||||
s.message(`${fitToWidth(label, suffix)}${k.dim(suffix)}`);
|
s.message(`${fitToWidth(label, suffix)}${k.dim(suffix)}`);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
||||||
const result = await pingCliAgent();
|
const result = await pingCliAgent();
|
||||||
|
|
||||||
clearInterval(tick);
|
clearInterval(tick);
|
||||||
const elapsed = Math.round((Date.now() - start) / 1000);
|
const suffix = ` (${fmtDuration(Date.now() - start)})`;
|
||||||
const suffix = ` (${elapsed}s)`;
|
|
||||||
if (result === 'ok') {
|
if (result === 'ok') {
|
||||||
s.stop(`${k.bold(fitToWidth('Your assistant is ready.', suffix))}${k.dim(suffix)}`);
|
s.stop(`${k.bold(fitToWidth('Your assistant is ready.', suffix))}${k.dim(suffix)}`);
|
||||||
} else {
|
} else {
|
||||||
@@ -1063,56 +1074,6 @@ async function askChannelChoice(): Promise<ChannelChoice> {
|
|||||||
|
|
||||||
// ─── interactive / env helpers ─────────────────────────────────────────
|
// ─── interactive / env helpers ─────────────────────────────────────────
|
||||||
|
|
||||||
interface ExistingEnvGroup {
|
|
||||||
label: string;
|
|
||||||
keys: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
const ENV_KEY_GROUPS: Record<string, { label: string; keys: string[] }> = {
|
|
||||||
onecli: { label: 'OneCLI', keys: ['ONECLI_URL'] },
|
|
||||||
telegram: { label: 'Telegram', keys: ['TELEGRAM_BOT_TOKEN'] },
|
|
||||||
discord: { label: 'Discord', keys: ['DISCORD_BOT_TOKEN', 'DISCORD_APPLICATION_ID', 'DISCORD_PUBLIC_KEY'] },
|
|
||||||
slack: { label: 'Slack', keys: ['SLACK_BOT_TOKEN', 'SLACK_SIGNING_SECRET'] },
|
|
||||||
signal: { label: 'Signal', keys: ['SIGNAL_ACCOUNT'] },
|
|
||||||
teams: { label: 'Teams', keys: ['TEAMS_APP_ID', 'TEAMS_APP_PASSWORD', 'TEAMS_APP_TENANT_ID', 'TEAMS_APP_TYPE'] },
|
|
||||||
whatsapp: { label: 'WhatsApp', keys: ['ASSISTANT_HAS_OWN_NUMBER'] },
|
|
||||||
imessage: { label: 'iMessage', keys: ['IMESSAGE_LOCAL', 'IMESSAGE_ENABLED', 'IMESSAGE_SERVER_URL', 'IMESSAGE_API_KEY'] },
|
|
||||||
};
|
|
||||||
|
|
||||||
function detectExistingEnv(): { groups: Record<string, ExistingEnvGroup>; raw: Record<string, string> } | null {
|
|
||||||
const envPath = path.join(process.cwd(), '.env');
|
|
||||||
if (!fs.existsSync(envPath)) return null;
|
|
||||||
|
|
||||||
let content: string;
|
|
||||||
try {
|
|
||||||
content = fs.readFileSync(envPath, 'utf-8');
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const raw: Record<string, string> = {};
|
|
||||||
for (const line of content.split('\n')) {
|
|
||||||
const trimmed = line.trim();
|
|
||||||
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
||||||
const eq = trimmed.indexOf('=');
|
|
||||||
if (eq < 1) continue;
|
|
||||||
raw[trimmed.slice(0, eq)] = trimmed.slice(eq + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Object.keys(raw).length === 0) return null;
|
|
||||||
|
|
||||||
const groups: Record<string, ExistingEnvGroup> = {};
|
|
||||||
for (const [id, def] of Object.entries(ENV_KEY_GROUPS)) {
|
|
||||||
const found = def.keys.filter((key) => raw[key] !== undefined);
|
|
||||||
if (found.length > 0) {
|
|
||||||
groups[id] = { label: def.label, keys: found };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Object.keys(groups).length === 0) return null;
|
|
||||||
return { groups, raw };
|
|
||||||
}
|
|
||||||
|
|
||||||
function anthropicSecretExists(): boolean {
|
function anthropicSecretExists(): boolean {
|
||||||
try {
|
try {
|
||||||
const res = spawnSync('onecli', ['secrets', 'list'], {
|
const res = spawnSync('onecli', ['secrets', 'list'], {
|
||||||
@@ -1190,9 +1151,11 @@ function maybeReexecUnderSg(): void {
|
|||||||
if (spawnSync('which', ['sg'], { stdio: 'ignore' }).status !== 0) return;
|
if (spawnSync('which', ['sg'], { stdio: 'ignore' }).status !== 0) return;
|
||||||
|
|
||||||
p.log.warn(brandBody('Docker socket not accessible in current group. Re-executing under `sg docker`.'));
|
p.log.warn(brandBody('Docker socket not accessible in current group. Re-executing under `sg docker`.'));
|
||||||
|
const existingSkip = (process.env.NANOCLAW_SKIP ?? '').split(',').map((s) => s.trim()).filter(Boolean);
|
||||||
|
const skipList = [...new Set([...existingSkip, ...setupLog.completedStepNames()])].join(',');
|
||||||
const res = spawnSync('sg', ['docker', '-c', 'pnpm run setup:auto'], {
|
const res = spawnSync('sg', ['docker', '-c', 'pnpm run setup:auto'], {
|
||||||
stdio: 'inherit',
|
stdio: 'inherit',
|
||||||
env: { ...process.env, NANOCLAW_REEXEC_SG: '1' },
|
env: { ...process.env, NANOCLAW_REEXEC_SG: '1', ...(skipList ? { NANOCLAW_SKIP: skipList } : {}) },
|
||||||
});
|
});
|
||||||
process.exit(res.status ?? 1);
|
process.exit(res.status ?? 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,10 +28,11 @@ import k from 'kleur';
|
|||||||
|
|
||||||
import * as setupLog from '../logs.js';
|
import * as setupLog from '../logs.js';
|
||||||
import { brightSelect } from '../lib/bright-select.js';
|
import { brightSelect } from '../lib/bright-select.js';
|
||||||
import { confirmThenOpen } from '../lib/browser.js';
|
import { confirmThenOpen, formatNoteLink } from '../lib/browser.js';
|
||||||
import { askOperatorRole } from '../lib/role-prompt.js';
|
import { askOperatorRole } from '../lib/role-prompt.js';
|
||||||
import { ensureAnswer, fail, runQuietChild } from '../lib/runner.js';
|
import { ensureAnswer, fail, runQuietChild } from '../lib/runner.js';
|
||||||
import { accentGreen, brandBody, note } from '../lib/theme.js';
|
import { readEnvKey } from '../environment.js';
|
||||||
|
import { accentGreen, brandBody, fmtDuration, note } from '../lib/theme.js';
|
||||||
|
|
||||||
const DEFAULT_AGENT_NAME = 'Nano';
|
const DEFAULT_AGENT_NAME = 'Nano';
|
||||||
const DISCORD_API = 'https://discord.com/api/v10';
|
const DISCORD_API = 'https://discord.com/api/v10';
|
||||||
@@ -164,9 +165,8 @@ async function walkThroughBotCreation(): Promise<void> {
|
|||||||
' 2. In the "Bot" tab, click "Reset Token" and copy the token',
|
' 2. In the "Bot" tab, click "Reset Token" and copy the token',
|
||||||
' 3. On the same tab, enable "Message Content Intent"',
|
' 3. On the same tab, enable "Message Content Intent"',
|
||||||
' (under Privileged Gateway Intents)',
|
' (under Privileged Gateway Intents)',
|
||||||
'',
|
formatNoteLink(url),
|
||||||
k.dim(url),
|
].filter((line): line is string => line !== null).join('\n'),
|
||||||
].join('\n'),
|
|
||||||
'Create a Discord bot',
|
'Create a Discord bot',
|
||||||
);
|
);
|
||||||
await confirmThenOpen(url, 'Press Enter to open the Developer Portal');
|
await confirmThenOpen(url, 'Press Enter to open the Developer Portal');
|
||||||
@@ -224,9 +224,8 @@ async function walkThroughServerCreation(): Promise<void> {
|
|||||||
' 1. In Discord, click the "+" at the bottom of the server list',
|
' 1. In Discord, click the "+" at the bottom of the server list',
|
||||||
' 2. Choose "Create My Own" → "For me and my friends"',
|
' 2. Choose "Create My Own" → "For me and my friends"',
|
||||||
' 3. Give it any name (e.g. "NanoClaw")',
|
' 3. Give it any name (e.g. "NanoClaw")',
|
||||||
'',
|
formatNoteLink(url),
|
||||||
k.dim(url),
|
].filter((line): line is string => line !== null).join('\n'),
|
||||||
].join('\n'),
|
|
||||||
'Create a Discord server',
|
'Create a Discord server',
|
||||||
);
|
);
|
||||||
await confirmThenOpen(url, 'Press Enter to open Discord');
|
await confirmThenOpen(url, 'Press Enter to open Discord');
|
||||||
@@ -240,7 +239,7 @@ async function walkThroughServerCreation(): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function collectDiscordToken(): Promise<string> {
|
async function collectDiscordToken(): Promise<string> {
|
||||||
const existing = process.env.DISCORD_BOT_TOKEN?.trim();
|
const existing = readEnvKey('DISCORD_BOT_TOKEN');
|
||||||
if (existing && /^[A-Za-z0-9._-]{50,}$/.test(existing)) {
|
if (existing && /^[A-Za-z0-9._-]{50,}$/.test(existing)) {
|
||||||
const reuse = ensureAnswer(await p.confirm({
|
const reuse = ensureAnswer(await p.confirm({
|
||||||
message: `Found an existing Discord bot token (${existing.slice(0, 10)}…). Use it?`,
|
message: `Found an existing Discord bot token (${existing.slice(0, 10)}…). Use it?`,
|
||||||
@@ -289,9 +288,8 @@ async function validateDiscordToken(token: string): Promise<string> {
|
|||||||
username?: string;
|
username?: string;
|
||||||
message?: string;
|
message?: string;
|
||||||
};
|
};
|
||||||
const elapsedS = Math.round((Date.now() - start) / 1000);
|
|
||||||
if (res.ok && data.username) {
|
if (res.ok && data.username) {
|
||||||
s.stop(`Found your bot: @${data.username}. ${k.dim(`(${elapsedS}s)`)}`);
|
s.stop(`Found your bot: @${data.username}. ${k.dim(`(${fmtDuration(Date.now() - start)})`)}`);
|
||||||
setupLog.step('discord-validate', 'success', Date.now() - start, {
|
setupLog.step('discord-validate', 'success', Date.now() - start, {
|
||||||
BOT_USERNAME: data.username,
|
BOT_USERNAME: data.username,
|
||||||
BOT_ID: data.id ?? '',
|
BOT_ID: data.id ?? '',
|
||||||
@@ -309,8 +307,7 @@ async function validateDiscordToken(token: string): Promise<string> {
|
|||||||
'Copy the token again from the Developer Portal and retry setup.',
|
'Copy the token again from the Developer Portal and retry setup.',
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const elapsedS = Math.round((Date.now() - start) / 1000);
|
s.stop(`Couldn't reach Discord. ${k.dim(`(${fmtDuration(Date.now() - start)})`)}`, 1);
|
||||||
s.stop(`Couldn't reach Discord. ${k.dim(`(${elapsedS}s)`)}`, 1);
|
|
||||||
const message = err instanceof Error ? err.message : String(err);
|
const message = err instanceof Error ? err.message : String(err);
|
||||||
setupLog.step('discord-validate', 'failed', Date.now() - start, {
|
setupLog.step('discord-validate', 'failed', Date.now() - start, {
|
||||||
ERROR: message,
|
ERROR: message,
|
||||||
@@ -338,7 +335,6 @@ async function fetchApplicationInfo(token: string): Promise<AppInfo> {
|
|||||||
team?: unknown;
|
team?: unknown;
|
||||||
message?: string;
|
message?: string;
|
||||||
};
|
};
|
||||||
const elapsedS = Math.round((Date.now() - start) / 1000);
|
|
||||||
if (!res.ok || !data.id || !data.verify_key) {
|
if (!res.ok || !data.id || !data.verify_key) {
|
||||||
const reason = data.message ?? `HTTP ${res.status}`;
|
const reason = data.message ?? `HTTP ${res.status}`;
|
||||||
s.stop(`Couldn't read application info: ${reason}`, 1);
|
s.stop(`Couldn't read application info: ${reason}`, 1);
|
||||||
@@ -351,7 +347,7 @@ async function fetchApplicationInfo(token: string): Promise<AppInfo> {
|
|||||||
'Re-run setup. If it keeps failing, check the bot token has the right scopes.',
|
'Re-run setup. If it keeps failing, check the bot token has the right scopes.',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
s.stop(`Got your application details. ${k.dim(`(${elapsedS}s)`)}`);
|
s.stop(`Got your application details. ${k.dim(`(${fmtDuration(Date.now() - start)})`)}`);
|
||||||
// owner is populated for solo applications; team-owned apps return a
|
// owner is populated for solo applications; team-owned apps return a
|
||||||
// team object instead and we'll fall back to a manual user-id prompt.
|
// team object instead and we'll fall back to a manual user-id prompt.
|
||||||
const owner =
|
const owner =
|
||||||
@@ -369,8 +365,7 @@ async function fetchApplicationInfo(token: string): Promise<AppInfo> {
|
|||||||
owner,
|
owner,
|
||||||
};
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const elapsedS = Math.round((Date.now() - start) / 1000);
|
s.stop(`Couldn't reach Discord. ${k.dim(`(${fmtDuration(Date.now() - start)})`)}`, 1);
|
||||||
s.stop(`Couldn't reach Discord. ${k.dim(`(${elapsedS}s)`)}`, 1);
|
|
||||||
const message = err instanceof Error ? err.message : String(err);
|
const message = err instanceof Error ? err.message : String(err);
|
||||||
setupLog.step('discord-app-info', 'failed', Date.now() - start, {
|
setupLog.step('discord-app-info', 'failed', Date.now() - start, {
|
||||||
ERROR: message,
|
ERROR: message,
|
||||||
@@ -450,9 +445,8 @@ async function promptInviteBot(
|
|||||||
'',
|
'',
|
||||||
' 1. Pick any server you\'re in (a personal one is fine)',
|
' 1. Pick any server you\'re in (a personal one is fine)',
|
||||||
' 2. Click "Authorize"',
|
' 2. Click "Authorize"',
|
||||||
'',
|
formatNoteLink(url),
|
||||||
k.dim(url),
|
].filter((line): line is string => line !== null).join('\n'),
|
||||||
].join('\n'),
|
|
||||||
'Add bot to a server',
|
'Add bot to a server',
|
||||||
);
|
);
|
||||||
await confirmThenOpen(url, 'Press Enter to open the invite page');
|
await confirmThenOpen(url, 'Press Enter to open the invite page');
|
||||||
@@ -479,7 +473,6 @@ async function openDmChannel(token: string, userId: string): Promise<string> {
|
|||||||
body: JSON.stringify({ recipient_id: userId }),
|
body: JSON.stringify({ recipient_id: userId }),
|
||||||
});
|
});
|
||||||
const data = (await res.json()) as { id?: string; message?: string };
|
const data = (await res.json()) as { id?: string; message?: string };
|
||||||
const elapsedS = Math.round((Date.now() - start) / 1000);
|
|
||||||
if (!res.ok || !data.id) {
|
if (!res.ok || !data.id) {
|
||||||
const reason = data.message ?? `HTTP ${res.status}`;
|
const reason = data.message ?? `HTTP ${res.status}`;
|
||||||
s.stop(`Couldn't open a DM channel: ${reason}`, 1);
|
s.stop(`Couldn't open a DM channel: ${reason}`, 1);
|
||||||
@@ -492,14 +485,13 @@ async function openDmChannel(token: string, userId: string): Promise<string> {
|
|||||||
'Make sure the bot is in a server you\'re also in, then retry setup.',
|
'Make sure the bot is in a server you\'re also in, then retry setup.',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
s.stop(`DM channel ready. ${k.dim(`(${elapsedS}s)`)}`);
|
s.stop(`DM channel ready. ${k.dim(`(${fmtDuration(Date.now() - start)})`)}`);
|
||||||
setupLog.step('discord-open-dm', 'success', Date.now() - start, {
|
setupLog.step('discord-open-dm', 'success', Date.now() - start, {
|
||||||
DM_CHANNEL_ID: data.id,
|
DM_CHANNEL_ID: data.id,
|
||||||
});
|
});
|
||||||
return data.id;
|
return data.id;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const elapsedS = Math.round((Date.now() - start) / 1000);
|
s.stop(`Couldn't reach Discord. ${k.dim(`(${fmtDuration(Date.now() - start)})`)}`, 1);
|
||||||
s.stop(`Couldn't reach Discord. ${k.dim(`(${elapsedS}s)`)}`, 1);
|
|
||||||
const message = err instanceof Error ? err.message : String(err);
|
const message = err instanceof Error ? err.message : String(err);
|
||||||
setupLog.step('discord-open-dm', 'failed', Date.now() - start, {
|
setupLog.step('discord-open-dm', 'failed', Date.now() - start, {
|
||||||
ERROR: message,
|
ERROR: message,
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ import { brightSelect } from '../lib/bright-select.js';
|
|||||||
import { askOperatorRole } from '../lib/role-prompt.js';
|
import { askOperatorRole } from '../lib/role-prompt.js';
|
||||||
import { ensureAnswer, fail, runQuietChild } from '../lib/runner.js';
|
import { ensureAnswer, fail, runQuietChild } from '../lib/runner.js';
|
||||||
import { accentGreen, note, wrapForGutter } from '../lib/theme.js';
|
import { accentGreen, note, wrapForGutter } from '../lib/theme.js';
|
||||||
|
import { readEnvKey } from '../environment.js';
|
||||||
|
|
||||||
const DEFAULT_AGENT_NAME = 'Nano';
|
const DEFAULT_AGENT_NAME = 'Nano';
|
||||||
|
|
||||||
@@ -222,8 +223,8 @@ async function walkThroughFullDiskAccess(): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function collectRemoteCreds(): Promise<RemoteCreds> {
|
async function collectRemoteCreds(): Promise<RemoteCreds> {
|
||||||
const existingUrl = process.env.IMESSAGE_SERVER_URL?.trim();
|
const existingUrl = readEnvKey('IMESSAGE_SERVER_URL');
|
||||||
const existingKey = process.env.IMESSAGE_API_KEY?.trim();
|
const existingKey = readEnvKey('IMESSAGE_API_KEY');
|
||||||
if (existingUrl && existingKey && /^https?:\/\//i.test(existingUrl)) {
|
if (existingUrl && existingKey && /^https?:\/\//i.test(existingUrl)) {
|
||||||
const reuse = ensureAnswer(await p.confirm({
|
const reuse = ensureAnswer(await p.confirm({
|
||||||
message: `Found existing Photon credentials (${existingUrl}). Use them?`,
|
message: `Found existing Photon credentials (${existingUrl}). Use them?`,
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ import {
|
|||||||
writeStepEntry,
|
writeStepEntry,
|
||||||
} from '../lib/runner.js';
|
} from '../lib/runner.js';
|
||||||
import { askOperatorRole } from '../lib/role-prompt.js';
|
import { askOperatorRole } from '../lib/role-prompt.js';
|
||||||
import { accentGreen, note } from '../lib/theme.js';
|
import { accentGreen, fmtDuration, note } from '../lib/theme.js';
|
||||||
|
|
||||||
const DEFAULT_AGENT_NAME = 'Nano';
|
const DEFAULT_AGENT_NAME = 'Nano';
|
||||||
|
|
||||||
@@ -324,8 +324,7 @@ async function restartService(): Promise<void> {
|
|||||||
// Give the adapter a moment to connect to signal-cli before
|
// Give the adapter a moment to connect to signal-cli before
|
||||||
// init-first-agent's welcome DM hits the delivery path.
|
// init-first-agent's welcome DM hits the delivery path.
|
||||||
await new Promise((r) => setTimeout(r, 5000));
|
await new Promise((r) => setTimeout(r, 5000));
|
||||||
const elapsed = Math.round((Date.now() - start) / 1000);
|
s.stop(`NanoClaw restarted. ${k.dim(`(${fmtDuration(Date.now() - start)})`)}`);
|
||||||
s.stop(`NanoClaw restarted. ${k.dim(`(${elapsed}s)`)}`);
|
|
||||||
setupLog.step('signal-restart', 'success', Date.now() - start, {
|
setupLog.step('signal-restart', 'success', Date.now() - start, {
|
||||||
PLATFORM: platform,
|
PLATFORM: platform,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -25,10 +25,11 @@ import * as p from '@clack/prompts';
|
|||||||
import k from 'kleur';
|
import k from 'kleur';
|
||||||
|
|
||||||
import * as setupLog from '../logs.js';
|
import * as setupLog from '../logs.js';
|
||||||
import { confirmThenOpen } from '../lib/browser.js';
|
import { confirmThenOpen, formatNoteLink } from '../lib/browser.js';
|
||||||
import { askOperatorRole } from '../lib/role-prompt.js';
|
import { askOperatorRole } from '../lib/role-prompt.js';
|
||||||
import { ensureAnswer, fail, runQuietChild } from '../lib/runner.js';
|
import { ensureAnswer, fail, runQuietChild } from '../lib/runner.js';
|
||||||
import { accentGreen, note, wrapForGutter } from '../lib/theme.js';
|
import { readEnvKey } from '../environment.js';
|
||||||
|
import { accentGreen, fmtDuration, note, wrapForGutter } from '../lib/theme.js';
|
||||||
|
|
||||||
const SLACK_API = 'https://slack.com/api';
|
const SLACK_API = 'https://slack.com/api';
|
||||||
const SLACK_APPS_URL = 'https://api.slack.com/apps';
|
const SLACK_APPS_URL = 'https://api.slack.com/apps';
|
||||||
@@ -135,9 +136,8 @@ async function walkThroughAppCreation(): Promise<void> {
|
|||||||
' slash commands and messages from the messages tab"',
|
' slash commands and messages from the messages tab"',
|
||||||
' 4. Basic Information → copy the "Signing Secret"',
|
' 4. Basic Information → copy the "Signing Secret"',
|
||||||
' 5. Install to Workspace → copy the "Bot User OAuth Token" (xoxb-…)',
|
' 5. Install to Workspace → copy the "Bot User OAuth Token" (xoxb-…)',
|
||||||
'',
|
formatNoteLink(SLACK_APPS_URL),
|
||||||
k.dim(SLACK_APPS_URL),
|
].filter((line): line is string => line !== null).join('\n'),
|
||||||
].join('\n'),
|
|
||||||
'Create a Slack app',
|
'Create a Slack app',
|
||||||
);
|
);
|
||||||
await confirmThenOpen(SLACK_APPS_URL, 'Press Enter to open Slack app settings');
|
await confirmThenOpen(SLACK_APPS_URL, 'Press Enter to open Slack app settings');
|
||||||
@@ -151,7 +151,7 @@ async function walkThroughAppCreation(): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function collectBotToken(): Promise<string> {
|
async function collectBotToken(): Promise<string> {
|
||||||
const existing = process.env.SLACK_BOT_TOKEN?.trim();
|
const existing = readEnvKey('SLACK_BOT_TOKEN');
|
||||||
if (existing && existing.startsWith('xoxb-') && existing.length >= 24) {
|
if (existing && existing.startsWith('xoxb-') && existing.length >= 24) {
|
||||||
const reuse = ensureAnswer(await p.confirm({
|
const reuse = ensureAnswer(await p.confirm({
|
||||||
message: `Found an existing Slack bot token (${existing.slice(0, 10)}…). Use it?`,
|
message: `Found an existing Slack bot token (${existing.slice(0, 10)}…). Use it?`,
|
||||||
@@ -185,7 +185,7 @@ async function collectBotToken(): Promise<string> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function collectSigningSecret(): Promise<string> {
|
async function collectSigningSecret(): Promise<string> {
|
||||||
const existing = process.env.SLACK_SIGNING_SECRET?.trim();
|
const existing = readEnvKey('SLACK_SIGNING_SECRET');
|
||||||
if (existing && /^[a-f0-9]{16,}$/i.test(existing)) {
|
if (existing && /^[a-f0-9]{16,}$/i.test(existing)) {
|
||||||
const reuse = ensureAnswer(await p.confirm({
|
const reuse = ensureAnswer(await p.confirm({
|
||||||
message: 'Found an existing Slack signing secret. Use it?',
|
message: 'Found an existing Slack signing secret. Use it?',
|
||||||
@@ -241,10 +241,9 @@ async function validateSlackToken(token: string): Promise<WorkspaceInfo> {
|
|||||||
user_id?: string;
|
user_id?: string;
|
||||||
error?: string;
|
error?: string;
|
||||||
};
|
};
|
||||||
const elapsedS = Math.round((Date.now() - start) / 1000);
|
|
||||||
if (data.ok && data.team && data.user) {
|
if (data.ok && data.team && data.user) {
|
||||||
s.stop(
|
s.stop(
|
||||||
`Connected to ${data.team} as @${data.user}. ${k.dim(`(${elapsedS}s)`)}`,
|
`Connected to ${data.team} as @${data.user}. ${k.dim(`(${fmtDuration(Date.now() - start)})`)}`,
|
||||||
);
|
);
|
||||||
const info: WorkspaceInfo = {
|
const info: WorkspaceInfo = {
|
||||||
teamName: data.team,
|
teamName: data.team,
|
||||||
@@ -273,8 +272,7 @@ async function validateSlackToken(token: string): Promise<WorkspaceInfo> {
|
|||||||
: `Slack said "${reason}". Check the token scopes and workspace install, then retry.`,
|
: `Slack said "${reason}". Check the token scopes and workspace install, then retry.`,
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const elapsedS = Math.round((Date.now() - start) / 1000);
|
s.stop(`Couldn't reach Slack. ${k.dim(`(${fmtDuration(Date.now() - start)})`)}`, 1);
|
||||||
s.stop(`Couldn't reach Slack. ${k.dim(`(${elapsedS}s)`)}`, 1);
|
|
||||||
const message = err instanceof Error ? err.message : String(err);
|
const message = err instanceof Error ? err.message : String(err);
|
||||||
setupLog.step('slack-validate', 'failed', Date.now() - start, {
|
setupLog.step('slack-validate', 'failed', Date.now() - start, {
|
||||||
ERROR: message,
|
ERROR: message,
|
||||||
@@ -334,9 +332,8 @@ async function openDmChannel(token: string, userId: string): Promise<string> {
|
|||||||
channel?: { id?: string };
|
channel?: { id?: string };
|
||||||
error?: string;
|
error?: string;
|
||||||
};
|
};
|
||||||
const elapsedS = Math.round((Date.now() - start) / 1000);
|
|
||||||
if (data.ok && data.channel?.id) {
|
if (data.ok && data.channel?.id) {
|
||||||
s.stop(`DM channel ready. ${k.dim(`(${elapsedS}s)`)}`);
|
s.stop(`DM channel ready. ${k.dim(`(${fmtDuration(Date.now() - start)})`)}`);
|
||||||
setupLog.step('slack-open-dm', 'success', Date.now() - start, {
|
setupLog.step('slack-open-dm', 'success', Date.now() - start, {
|
||||||
DM_CHANNEL_ID: data.channel.id,
|
DM_CHANNEL_ID: data.channel.id,
|
||||||
});
|
});
|
||||||
@@ -360,8 +357,7 @@ async function openDmChannel(token: string, userId: string): Promise<string> {
|
|||||||
`Slack said "${reason}". Check the member ID and app permissions, then retry.`,
|
`Slack said "${reason}". Check the member ID and app permissions, then retry.`,
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const elapsedS = Math.round((Date.now() - start) / 1000);
|
s.stop(`Couldn't reach Slack. ${k.dim(`(${fmtDuration(Date.now() - start)})`)}`, 1);
|
||||||
s.stop(`Couldn't reach Slack. ${k.dim(`(${elapsedS}s)`)}`, 1);
|
|
||||||
const message = err instanceof Error ? err.message : String(err);
|
const message = err instanceof Error ? err.message : String(err);
|
||||||
setupLog.step('slack-open-dm', 'failed', Date.now() - start, {
|
setupLog.step('slack-open-dm', 'failed', Date.now() - start, {
|
||||||
ERROR: message,
|
ERROR: message,
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ import { ensureAnswer, fail, runQuietChild } from '../lib/runner.js';
|
|||||||
import { buildTeamsAppPackage } from '../lib/teams-manifest.js';
|
import { buildTeamsAppPackage } from '../lib/teams-manifest.js';
|
||||||
import { note } from '../lib/theme.js';
|
import { note } from '../lib/theme.js';
|
||||||
import * as setupLog from '../logs.js';
|
import * as setupLog from '../logs.js';
|
||||||
|
import { readEnvKey } from '../environment.js';
|
||||||
|
|
||||||
const CHANNEL = 'teams';
|
const CHANNEL = 'teams';
|
||||||
const MANIFEST_DIR = path.join(process.cwd(), 'data', 'teams');
|
const MANIFEST_DIR = path.join(process.cwd(), 'data', 'teams');
|
||||||
@@ -60,8 +61,8 @@ export async function runTeamsChannel(_displayName: string): Promise<void> {
|
|||||||
const collected: Collected = {};
|
const collected: Collected = {};
|
||||||
const completed: string[] = [];
|
const completed: string[] = [];
|
||||||
|
|
||||||
const existingAppId = process.env.TEAMS_APP_ID?.trim();
|
const existingAppId = readEnvKey('TEAMS_APP_ID');
|
||||||
const existingPassword = process.env.TEAMS_APP_PASSWORD?.trim();
|
const existingPassword = readEnvKey('TEAMS_APP_PASSWORD');
|
||||||
if (existingAppId && existingPassword) {
|
if (existingAppId && existingPassword) {
|
||||||
const reuse = ensureAnswer(await p.confirm({
|
const reuse = ensureAnswer(await p.confirm({
|
||||||
message: `Found existing Teams credentials (App ID: ${existingAppId.slice(0, 8)}…). Use them?`,
|
message: `Found existing Teams credentials (App ID: ${existingAppId.slice(0, 8)}…). Use them?`,
|
||||||
@@ -70,9 +71,9 @@ export async function runTeamsChannel(_displayName: string): Promise<void> {
|
|||||||
if (reuse) {
|
if (reuse) {
|
||||||
collected.appId = existingAppId;
|
collected.appId = existingAppId;
|
||||||
collected.appPassword = existingPassword;
|
collected.appPassword = existingPassword;
|
||||||
collected.appType = (process.env.TEAMS_APP_TYPE?.trim() as 'SingleTenant' | 'MultiTenant') || 'MultiTenant';
|
collected.appType = (readEnvKey('TEAMS_APP_TYPE') as 'SingleTenant' | 'MultiTenant') || 'MultiTenant';
|
||||||
if (collected.appType === 'SingleTenant') {
|
if (collected.appType === 'SingleTenant') {
|
||||||
collected.tenantId = process.env.TEAMS_APP_TENANT_ID?.trim();
|
collected.tenantId = readEnvKey('TEAMS_APP_TENANT_ID') ?? undefined;
|
||||||
}
|
}
|
||||||
setupLog.userInput('teams_credentials', 'reused-existing');
|
setupLog.userInput('teams_credentials', 'reused-existing');
|
||||||
await installAdapter(collected);
|
await installAdapter(collected);
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import * as p from '@clack/prompts';
|
|||||||
import k from 'kleur';
|
import k from 'kleur';
|
||||||
|
|
||||||
import * as setupLog from '../logs.js';
|
import * as setupLog from '../logs.js';
|
||||||
import { confirmThenOpen } from '../lib/browser.js';
|
import { confirmThenOpen, formatNoteLink } from '../lib/browser.js';
|
||||||
import { askOperatorRole } from '../lib/role-prompt.js';
|
import { askOperatorRole } from '../lib/role-prompt.js';
|
||||||
import {
|
import {
|
||||||
type Block,
|
type Block,
|
||||||
@@ -33,7 +33,8 @@ import {
|
|||||||
spawnStep,
|
spawnStep,
|
||||||
writeStepEntry,
|
writeStepEntry,
|
||||||
} from '../lib/runner.js';
|
} from '../lib/runner.js';
|
||||||
import { accentGreen, brandBold, note } from '../lib/theme.js';
|
import { readEnvKey } from '../environment.js';
|
||||||
|
import { accentGreen, brandBold, fitToWidth, fmtDuration, note } from '../lib/theme.js';
|
||||||
|
|
||||||
const DEFAULT_AGENT_NAME = 'Nano';
|
const DEFAULT_AGENT_NAME = 'Nano';
|
||||||
|
|
||||||
@@ -50,9 +51,8 @@ export async function runTelegramChannel(displayName: string): Promise<void> {
|
|||||||
note(
|
note(
|
||||||
[
|
[
|
||||||
`Opening @${botUsername} in Telegram so it's ready when the pairing code shows up.`,
|
`Opening @${botUsername} in Telegram so it's ready when the pairing code shows up.`,
|
||||||
'',
|
formatNoteLink(botUrl),
|
||||||
k.dim(botUrl),
|
].filter((line): line is string => line !== null).join('\n'),
|
||||||
].join('\n'),
|
|
||||||
'Open Telegram',
|
'Open Telegram',
|
||||||
);
|
);
|
||||||
await confirmThenOpen(botUrl, 'Press Enter to open Telegram');
|
await confirmThenOpen(botUrl, 'Press Enter to open Telegram');
|
||||||
@@ -132,7 +132,7 @@ export async function runTelegramChannel(displayName: string): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function collectTelegramToken(): Promise<string> {
|
async function collectTelegramToken(): Promise<string> {
|
||||||
const existing = process.env.TELEGRAM_BOT_TOKEN?.trim();
|
const existing = readEnvKey('TELEGRAM_BOT_TOKEN');
|
||||||
if (existing && /^[0-9]+:[A-Za-z0-9_-]{35,}$/.test(existing)) {
|
if (existing && /^[0-9]+:[A-Za-z0-9_-]{35,}$/.test(existing)) {
|
||||||
const reuse = ensureAnswer(await p.confirm({
|
const reuse = ensureAnswer(await p.confirm({
|
||||||
message: `Found an existing Telegram bot token (${existing.slice(0, 8)}…). Use it?`,
|
message: `Found an existing Telegram bot token (${existing.slice(0, 8)}…). Use it?`,
|
||||||
@@ -191,10 +191,9 @@ async function validateTelegramToken(token: string): Promise<string> {
|
|||||||
result?: { username?: string; id?: number };
|
result?: { username?: string; id?: number };
|
||||||
description?: string;
|
description?: string;
|
||||||
};
|
};
|
||||||
const elapsedS = Math.round((Date.now() - start) / 1000);
|
|
||||||
if (data.ok && data.result?.username) {
|
if (data.ok && data.result?.username) {
|
||||||
const username = data.result.username;
|
const username = data.result.username;
|
||||||
s.stop(`Found your bot: @${username}. ${k.dim(`(${elapsedS}s)`)}`);
|
s.stop(`Found your bot: @${username}. ${k.dim(`(${fmtDuration(Date.now() - start)})`)}`);
|
||||||
setupLog.step('telegram-validate', 'success', Date.now() - start, {
|
setupLog.step('telegram-validate', 'success', Date.now() - start, {
|
||||||
BOT_USERNAME: username,
|
BOT_USERNAME: username,
|
||||||
BOT_ID: data.result.id ?? '',
|
BOT_ID: data.result.id ?? '',
|
||||||
@@ -212,8 +211,7 @@ async function validateTelegramToken(token: string): Promise<string> {
|
|||||||
'Copy the token again from @BotFather and try setup once more.',
|
'Copy the token again from @BotFather and try setup once more.',
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const elapsedS = Math.round((Date.now() - start) / 1000);
|
s.stop(`Couldn't reach Telegram. ${k.dim(`(${fmtDuration(Date.now() - start)})`)}`, 1);
|
||||||
s.stop(`Couldn't reach Telegram. ${k.dim(`(${elapsedS}s)`)}`, 1);
|
|
||||||
const message = err instanceof Error ? err.message : String(err);
|
const message = err instanceof Error ? err.message : String(err);
|
||||||
setupLog.step('telegram-validate', 'failed', Date.now() - start, {
|
setupLog.step('telegram-validate', 'failed', Date.now() - start, {
|
||||||
ERROR: message,
|
ERROR: message,
|
||||||
@@ -254,11 +252,11 @@ async function runPairTelegram(): Promise<
|
|||||||
stopSpinner("Old code expired. Here's a fresh one.");
|
stopSpinner("Old code expired. Here's a fresh one.");
|
||||||
}
|
}
|
||||||
note(formatCodeCard(block.fields.CODE ?? '????'), 'Secret code');
|
note(formatCodeCard(block.fields.CODE ?? '????'), 'Secret code');
|
||||||
s.start('Waiting for you to send the code from Telegram…');
|
s.start(fitToWidth('Waiting for you to send the code from Telegram…', ''));
|
||||||
spinnerActive = true;
|
spinnerActive = true;
|
||||||
} else if (block.type === 'PAIR_TELEGRAM_ATTEMPT') {
|
} else if (block.type === 'PAIR_TELEGRAM_ATTEMPT') {
|
||||||
stopSpinner(`Got "${block.fields.CANDIDATE ?? '?'}", not a match.`);
|
stopSpinner(`Got "${block.fields.CANDIDATE ?? '?'}", not a match.`);
|
||||||
s.start('Waiting for the correct code…');
|
s.start(fitToWidth('Waiting for the correct code…', ''));
|
||||||
spinnerActive = true;
|
spinnerActive = true;
|
||||||
} else if (block.type === 'PAIR_TELEGRAM') {
|
} else if (block.type === 'PAIR_TELEGRAM') {
|
||||||
if (block.fields.STATUS === 'success') {
|
if (block.fields.STATUS === 'success') {
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ import {
|
|||||||
writeStepEntry,
|
writeStepEntry,
|
||||||
} from '../lib/runner.js';
|
} from '../lib/runner.js';
|
||||||
import { askOperatorRole } from '../lib/role-prompt.js';
|
import { askOperatorRole } from '../lib/role-prompt.js';
|
||||||
import { accentGreen, brandBody, brandBold, note } from '../lib/theme.js';
|
import { accentGreen, brandBody, brandBold, fmtDuration, note } from '../lib/theme.js';
|
||||||
|
|
||||||
const DEFAULT_AGENT_NAME = 'Nano';
|
const DEFAULT_AGENT_NAME = 'Nano';
|
||||||
const AUTH_CREDS_PATH = path.join(process.cwd(), 'store', 'auth', 'creds.json');
|
const AUTH_CREDS_PATH = path.join(process.cwd(), 'store', 'auth', 'creds.json');
|
||||||
@@ -379,8 +379,7 @@ async function restartService(): Promise<void> {
|
|||||||
// Give the adapter a moment to reconnect before init-first-agent's
|
// Give the adapter a moment to reconnect before init-first-agent's
|
||||||
// welcome DM hits the delivery path.
|
// welcome DM hits the delivery path.
|
||||||
await new Promise((r) => setTimeout(r, 5000));
|
await new Promise((r) => setTimeout(r, 5000));
|
||||||
const elapsed = Math.round((Date.now() - start) / 1000);
|
s.stop(`NanoClaw restarted. ${k.dim(`(${fmtDuration(Date.now() - start)})`)}`);
|
||||||
s.stop(`NanoClaw restarted. ${k.dim(`(${elapsed}s)`)}`);
|
|
||||||
setupLog.step('whatsapp-restart', 'success', Date.now() - start, {
|
setupLog.step('whatsapp-restart', 'success', Date.now() - start, {
|
||||||
PLATFORM: platform,
|
PLATFORM: platform,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
* Args:
|
* Args:
|
||||||
* --display-name <name> (required) operator's display name
|
* --display-name <name> (required) operator's display name
|
||||||
* --agent-name <name> (optional) agent persona name, defaults to display-name
|
* --agent-name <name> (optional) agent persona name, defaults to display-name
|
||||||
|
* --folder <name> (optional) explicit folder name, defaults to cli-with-<normalized-display-name>
|
||||||
*/
|
*/
|
||||||
import { execFileSync } from 'child_process';
|
import { execFileSync } from 'child_process';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
@@ -18,9 +19,11 @@ import { emitStatus } from './status.js';
|
|||||||
function parseArgs(args: string[]): {
|
function parseArgs(args: string[]): {
|
||||||
displayName: string;
|
displayName: string;
|
||||||
agentName?: string;
|
agentName?: string;
|
||||||
|
folder?: string;
|
||||||
} {
|
} {
|
||||||
let displayName: string | undefined;
|
let displayName: string | undefined;
|
||||||
let agentName: string | undefined;
|
let agentName: string | undefined;
|
||||||
|
let folder: string | undefined;
|
||||||
|
|
||||||
for (let i = 0; i < args.length; i++) {
|
for (let i = 0; i < args.length; i++) {
|
||||||
const key = args[i];
|
const key = args[i];
|
||||||
@@ -34,6 +37,10 @@ function parseArgs(args: string[]): {
|
|||||||
agentName = val;
|
agentName = val;
|
||||||
i++;
|
i++;
|
||||||
break;
|
break;
|
||||||
|
case '--folder':
|
||||||
|
folder = val;
|
||||||
|
i++;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,17 +53,18 @@ function parseArgs(args: string[]): {
|
|||||||
process.exit(2);
|
process.exit(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { displayName, agentName };
|
return { displayName, agentName, folder };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function run(args: string[]): Promise<void> {
|
export async function run(args: string[]): Promise<void> {
|
||||||
const { displayName, agentName } = parseArgs(args);
|
const { displayName, agentName, folder } = parseArgs(args);
|
||||||
|
|
||||||
const projectRoot = process.cwd();
|
const projectRoot = process.cwd();
|
||||||
const script = path.join(projectRoot, 'scripts', 'init-cli-agent.ts');
|
const script = path.join(projectRoot, 'scripts', 'init-cli-agent.ts');
|
||||||
|
|
||||||
const scriptArgs = ['exec', 'tsx', script, '--display-name', displayName];
|
const scriptArgs = ['exec', 'tsx', script, '--display-name', displayName];
|
||||||
if (agentName) scriptArgs.push('--agent-name', agentName);
|
if (agentName) scriptArgs.push('--agent-name', agentName);
|
||||||
|
if (folder) scriptArgs.push('--folder', folder);
|
||||||
|
|
||||||
log.info('Invoking init-cli-agent', { displayName, agentName });
|
log.info('Invoking init-cli-agent', { displayName, agentName });
|
||||||
|
|
||||||
|
|||||||
@@ -127,11 +127,22 @@ export async function run(args: string[]): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Socket is unreachable due to group perms — current shell's supplementary
|
// Socket is unreachable due to group perms — current shell's supplementary
|
||||||
// groups are fixed at login, so `usermod -aG docker` (via install-docker.sh
|
// groups are fixed at login, so `usermod -aG docker` doesn't affect us
|
||||||
// or a prior install) doesn't affect us until next login. Re-exec this
|
// until next login. Ensure the user is in the docker group (install-docker.sh
|
||||||
// step under `sg docker` so the child picks up docker as its primary
|
// does this on fresh installs, but skips when Docker is already present),
|
||||||
// group and can talk to /var/run/docker.sock without a logout.
|
// then re-exec under `sg docker` so the child picks up docker as its
|
||||||
|
// primary group and can talk to /var/run/docker.sock without a logout.
|
||||||
if (status === 'no-permission' && getPlatform() === 'linux' && commandExists('sg')) {
|
if (status === 'no-permission' && getPlatform() === 'linux' && commandExists('sg')) {
|
||||||
|
// Ensure the current user is in the docker group — without this,
|
||||||
|
// sg will ask for the (typically unset) group password and fail.
|
||||||
|
const inGroup = spawnSync('id', ['-nG'], { encoding: 'utf-8' });
|
||||||
|
if (!(inGroup.stdout ?? '').split(/\s+/).includes('docker')) {
|
||||||
|
log.info('Adding current user to docker group');
|
||||||
|
spawnSync('sudo', ['usermod', '-aG', 'docker', process.env.USER ?? ''], {
|
||||||
|
stdio: 'inherit',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
log.info('Re-executing container step under `sg docker`');
|
log.info('Re-executing container step under `sg docker`');
|
||||||
const res = spawnSync(
|
const res = spawnSync(
|
||||||
'sg',
|
'sg',
|
||||||
|
|||||||
@@ -11,6 +11,30 @@ import { log } from '../src/log.js';
|
|||||||
import { commandExists, getPlatform, isHeadless, isWSL } from './platform.js';
|
import { commandExists, getPlatform, isHeadless, isWSL } from './platform.js';
|
||||||
import { emitStatus } from './status.js';
|
import { emitStatus } from './status.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a single key from `.env` on disk (not process.env).
|
||||||
|
* Returns the trimmed value or null if the key isn't set / file doesn't exist.
|
||||||
|
*/
|
||||||
|
export function readEnvKey(key: string, projectRoot?: string): string | null {
|
||||||
|
const envPath = path.join(projectRoot ?? process.cwd(), '.env');
|
||||||
|
let content: string;
|
||||||
|
try {
|
||||||
|
content = fs.readFileSync(envPath, 'utf-8');
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
for (const line of content.split('\n')) {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
if (!trimmed || trimmed.startsWith('#')) continue;
|
||||||
|
const eq = trimmed.indexOf('=');
|
||||||
|
if (eq < 1) continue;
|
||||||
|
if (trimmed.slice(0, eq) === key) {
|
||||||
|
return trimmed.slice(eq + 1).trim() || null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
export function detectExistingDisplayName(projectRoot: string): string | null {
|
export function detectExistingDisplayName(projectRoot: string): string | null {
|
||||||
const dbPath = path.join(projectRoot, 'data', 'v2.db');
|
const dbPath = path.join(projectRoot, 'data', 'v2.db');
|
||||||
if (!fs.existsSync(dbPath)) return null;
|
if (!fs.existsSync(dbPath)) return null;
|
||||||
|
|||||||
@@ -9,12 +9,19 @@
|
|||||||
* `confirmThenOpen` pauses for the operator before triggering the open —
|
* `confirmThenOpen` pauses for the operator before triggering the open —
|
||||||
* the browser tends to steal focus when it pops, and a split-second
|
* the browser tends to steal focus when it pops, and a split-second
|
||||||
* "wait what just happened" moment is worse than letting the user hit
|
* "wait what just happened" moment is worse than letting the user hit
|
||||||
* Enter when they're ready.
|
* Enter when they're ready. On headless devices (no graphical session
|
||||||
|
* available) it skips both the prompt and the open: there's no browser
|
||||||
|
* to launch, the surrounding `note(...)` already shows the URL for
|
||||||
|
* copy-paste on another device, and the next prompt in the channel
|
||||||
|
* flow ("Got your bot token?" etc.) provides the natural completion
|
||||||
|
* confirmation.
|
||||||
*/
|
*/
|
||||||
import { spawn } from 'child_process';
|
import { spawn } from 'child_process';
|
||||||
|
|
||||||
import * as p from '@clack/prompts';
|
import * as p from '@clack/prompts';
|
||||||
|
import k from 'kleur';
|
||||||
|
|
||||||
|
import { isHeadless } from '../platform.js';
|
||||||
import { ensureAnswer } from './runner.js';
|
import { ensureAnswer } from './runner.js';
|
||||||
|
|
||||||
/** Best-effort open of a URL in the user's default browser. Silent on failure. */
|
/** Best-effort open of a URL in the user's default browser. Silent on failure. */
|
||||||
@@ -32,18 +39,43 @@ export function openUrl(url: string): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format a URL for inclusion in a setup `note(...)` card. On
|
||||||
|
* headless devices we surface the URL inside the card with a
|
||||||
|
* "Get started:" label at full strength — copy-pasting onto
|
||||||
|
* another device is the actual action, not an incidental
|
||||||
|
* reference. The leading `\n` acts as a visual separator from
|
||||||
|
* the body steps above; callers `.filter(line => line !== null)`
|
||||||
|
* before joining, so on GUI we drop the line entirely (and the
|
||||||
|
* URL ends up below the next-step confirm prompt as a "if
|
||||||
|
* browser does not appear, please visit" fallback — see
|
||||||
|
* `confirmThenOpen`).
|
||||||
|
*/
|
||||||
|
export function formatNoteLink(url: string): string | null {
|
||||||
|
if (isHeadless()) return `\nGet started: ${url}`;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gate a browser-open on a confirm so the user is ready for their browser
|
* Gate a browser-open on a confirm so the user is ready for their browser
|
||||||
* to take focus. Proceeds on cancel as well — the user can always copy the
|
* to take focus. Proceeds on cancel as well. On headless devices both the
|
||||||
* URL from the note that precedes the prompt.
|
* prompt and the open are skipped — the URL is already surfaced inside
|
||||||
|
* the surrounding note (via `formatNoteLink`).
|
||||||
|
*
|
||||||
|
* On GUI devices the confirm message includes the fallback URL on the
|
||||||
|
* lines below the action ("If browser does not appear, please visit:
|
||||||
|
* <url>" in dim) so the user has a copy-paste path right next to the
|
||||||
|
* action button without needing to scroll back up to the card.
|
||||||
*/
|
*/
|
||||||
export async function confirmThenOpen(
|
export async function confirmThenOpen(
|
||||||
url: string,
|
url: string,
|
||||||
message = 'Press Enter to open your browser',
|
message = 'Press Enter to open your browser',
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
if (isHeadless()) return;
|
||||||
|
const fallback = `\n${k.dim(`If browser does not appear, please visit: ${url}`)}`;
|
||||||
ensureAnswer(
|
ensureAnswer(
|
||||||
await p.confirm({
|
await p.confirm({
|
||||||
message,
|
message: `${message}${fallback}`,
|
||||||
initialValue: true,
|
initialValue: true,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import * as p from '@clack/prompts';
|
|||||||
import k from 'kleur';
|
import k from 'kleur';
|
||||||
|
|
||||||
import { ensureAnswer } from './runner.js';
|
import { ensureAnswer } from './runner.js';
|
||||||
import { brandBody, fitToWidth, note } from './theme.js';
|
import { brandBody, fitToWidth, fmtDuration, note } from './theme.js';
|
||||||
|
|
||||||
export interface AssistContext {
|
export interface AssistContext {
|
||||||
stepName: string;
|
stepName: string;
|
||||||
@@ -295,9 +295,8 @@ async function queryClaudeUnderSpinner(
|
|||||||
// Move cursor back to the start of the block (WINDOW_SIZE + 1 = header + window).
|
// Move cursor back to the start of the block (WINDOW_SIZE + 1 = header + window).
|
||||||
out.write(`\x1b[${WINDOW_SIZE + 1}A`);
|
out.write(`\x1b[${WINDOW_SIZE + 1}A`);
|
||||||
|
|
||||||
const elapsed = Math.round((Date.now() - start) / 1000);
|
|
||||||
const icon = SPINNER_FRAMES[frameIdx % SPINNER_FRAMES.length];
|
const icon = SPINNER_FRAMES[frameIdx % SPINNER_FRAMES.length];
|
||||||
const suffix = ` (${elapsed}s)`;
|
const suffix = ` (${fmtDuration(Date.now() - start)})`;
|
||||||
const header = fitToWidth('Asking Claude to diagnose…', suffix);
|
const header = fitToWidth('Asking Claude to diagnose…', suffix);
|
||||||
out.write(`\x1b[2K${k.cyan(icon)} ${header}${k.dim(suffix)}\n`);
|
out.write(`\x1b[2K${k.cyan(icon)} ${header}${k.dim(suffix)}\n`);
|
||||||
|
|
||||||
@@ -355,8 +354,7 @@ async function queryClaudeUnderSpinner(
|
|||||||
clearBlock();
|
clearBlock();
|
||||||
out.write(SHOW_CURSOR);
|
out.write(SHOW_CURSOR);
|
||||||
process.off('exit', restoreCursorOnExit);
|
process.off('exit', restoreCursorOnExit);
|
||||||
const elapsed = Math.round((Date.now() - start) / 1000);
|
const suffix = ` (${fmtDuration(Date.now() - start)})`;
|
||||||
const suffix = ` (${elapsed}s)`;
|
|
||||||
if (kind === 'ok') {
|
if (kind === 'ok') {
|
||||||
p.log.success(`${brandBody(fitToWidth('Claude replied.', suffix))}${k.dim(suffix)}`);
|
p.log.success(`${brandBody(fitToWidth('Claude replied.', suffix))}${k.dim(suffix)}`);
|
||||||
resolve(payload);
|
resolve(payload);
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import k from 'kleur';
|
|||||||
import * as setupLog from '../logs.js';
|
import * as setupLog from '../logs.js';
|
||||||
import { offerClaudeAssist } from './claude-assist.js';
|
import { offerClaudeAssist } from './claude-assist.js';
|
||||||
import { emit as phEmit } from './diagnostics.js';
|
import { emit as phEmit } from './diagnostics.js';
|
||||||
import { brandBody, fitToWidth } from './theme.js';
|
import { brandBody, fitToWidth, fmtDuration } from './theme.js';
|
||||||
|
|
||||||
export type Fields = Record<string, string>;
|
export type Fields = Record<string, string>;
|
||||||
export type Block = { type: string; fields: Fields };
|
export type Block = { type: string; fields: Fields };
|
||||||
@@ -307,18 +307,16 @@ async function runUnderSpinner<
|
|||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
const s = p.spinner();
|
const s = p.spinner();
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
s.start(fitToWidth(labels.running, ' (999s)'));
|
s.start(fitToWidth(labels.running, ' (99m 59s)'));
|
||||||
const tick = setInterval(() => {
|
const tick = setInterval(() => {
|
||||||
const elapsed = Math.round((Date.now() - start) / 1000);
|
const suffix = ` (${fmtDuration(Date.now() - start)})`;
|
||||||
const suffix = ` (${elapsed}s)`;
|
|
||||||
s.message(`${fitToWidth(labels.running, suffix)}${k.dim(suffix)}`);
|
s.message(`${fitToWidth(labels.running, suffix)}${k.dim(suffix)}`);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
||||||
const result = await work();
|
const result = await work();
|
||||||
|
|
||||||
clearInterval(tick);
|
clearInterval(tick);
|
||||||
const elapsed = Math.round((Date.now() - start) / 1000);
|
const suffix = ` (${fmtDuration(Date.now() - start)})`;
|
||||||
const suffix = ` (${elapsed}s)`;
|
|
||||||
if (result.ok) {
|
if (result.ok) {
|
||||||
const isSkipped = result.terminal?.fields.STATUS === 'skipped';
|
const isSkipped = result.terminal?.fields.STATUS === 'skipped';
|
||||||
const msg = isSkipped && labels.skipped ? labels.skipped : labels.done;
|
const msg = isSkipped && labels.skipped ? labels.skipped : labels.done;
|
||||||
|
|||||||
@@ -51,6 +51,22 @@ export function accentGreen(s: string): string {
|
|||||||
return k.green(s);
|
return k.green(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format an elapsed-time duration (in milliseconds) for the spinner
|
||||||
|
* suffixes setup writes everywhere. Sub-minute durations stay in plain
|
||||||
|
* seconds (`47s`); once the timer crosses 60 seconds we switch to the
|
||||||
|
* `Xm Ys` form (`2m 34s`) so a long step doesn't read as `247s` or
|
||||||
|
* similar. The format is consistent above 60s — `4m 0s` over `4m` —
|
||||||
|
* so live spinner output doesn't change shape at every whole minute.
|
||||||
|
*/
|
||||||
|
export function fmtDuration(ms: number): string {
|
||||||
|
const totalSec = Math.round(ms / 1000);
|
||||||
|
if (totalSec < 60) return `${totalSec}s`;
|
||||||
|
const m = Math.floor(totalSec / 60);
|
||||||
|
const s = totalSec % 60;
|
||||||
|
return `${m}m ${s}s`;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Brand body color for setup-flow prose. Used for card bodies (via the
|
* Brand body color for setup-flow prose. Used for card bodies (via the
|
||||||
* `note()` formatter) and `p.log.*` body arguments — anywhere the
|
* `note()` formatter) and `p.log.*` body arguments — anywhere the
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import * as p from '@clack/prompts';
|
|||||||
import k from 'kleur';
|
import k from 'kleur';
|
||||||
|
|
||||||
import { isValidTimezone } from '../../src/timezone.js';
|
import { isValidTimezone } from '../../src/timezone.js';
|
||||||
import { fitToWidth } from './theme.js';
|
import { fitToWidth, fmtDuration } from './theme.js';
|
||||||
|
|
||||||
export function claudeCliAvailable(): boolean {
|
export function claudeCliAvailable(): boolean {
|
||||||
try {
|
try {
|
||||||
@@ -44,18 +44,16 @@ export async function resolveTimezoneViaClaude(
|
|||||||
const s = p.spinner();
|
const s = p.spinner();
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
const label = 'Looking up that timezone…';
|
const label = 'Looking up that timezone…';
|
||||||
s.start(fitToWidth(label, ' (999s)'));
|
s.start(fitToWidth(label, ' (99m 59s)'));
|
||||||
const tick = setInterval(() => {
|
const tick = setInterval(() => {
|
||||||
const elapsed = Math.round((Date.now() - start) / 1000);
|
const suffix = ` (${fmtDuration(Date.now() - start)})`;
|
||||||
const suffix = ` (${elapsed}s)`;
|
|
||||||
s.message(`${fitToWidth(label, suffix)}${k.dim(suffix)}`);
|
s.message(`${fitToWidth(label, suffix)}${k.dim(suffix)}`);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
||||||
const reply = await queryClaude(prompt);
|
const reply = await queryClaude(prompt);
|
||||||
|
|
||||||
clearInterval(tick);
|
clearInterval(tick);
|
||||||
const elapsed = Math.round((Date.now() - start) / 1000);
|
const suffix = ` (${fmtDuration(Date.now() - start)})`;
|
||||||
const suffix = ` (${elapsed}s)`;
|
|
||||||
|
|
||||||
const resolved = reply ? extractTimezone(reply) : null;
|
const resolved = reply ? extractTimezone(reply) : null;
|
||||||
if (resolved) {
|
if (resolved) {
|
||||||
|
|||||||
71
src/attachment-naming.test.ts
Normal file
71
src/attachment-naming.test.ts
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
|
||||||
|
import { deriveAttachmentName, extForMime } from './attachment-naming.js';
|
||||||
|
|
||||||
|
describe('extForMime', () => {
|
||||||
|
it('returns empty for undefined / non-string / empty', () => {
|
||||||
|
expect(extForMime(undefined)).toBe('');
|
||||||
|
expect(extForMime('')).toBe('');
|
||||||
|
expect(extForMime({})).toBe('');
|
||||||
|
expect(extForMime(null)).toBe('');
|
||||||
|
expect(extForMime(42)).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('maps common MIME types to canonical extensions', () => {
|
||||||
|
expect(extForMime('image/jpeg')).toBe('jpg');
|
||||||
|
expect(extForMime('application/pdf')).toBe('pdf');
|
||||||
|
expect(extForMime('audio/ogg')).toBe('ogg');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('strips parameters and is case-insensitive', () => {
|
||||||
|
expect(extForMime('image/JPEG; foo=bar')).toBe('jpg');
|
||||||
|
expect(extForMime(' Application/PDF ')).toBe('pdf');
|
||||||
|
expect(extForMime('text/plain; charset=utf-8')).toBe('txt');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns empty for unknown MIMEs', () => {
|
||||||
|
expect(extForMime('application/octet-stream')).toBe('');
|
||||||
|
expect(extForMime('application/x-totally-made-up')).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('deriveAttachmentName', () => {
|
||||||
|
it('returns explicit name when set, no derivation', () => {
|
||||||
|
expect(deriveAttachmentName({ name: 'photo.jpg', mimeType: 'application/pdf' })).toBe('photo.jpg');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('ignores empty / non-string explicit name and falls through to derivation', () => {
|
||||||
|
const out = deriveAttachmentName({ name: '', mimeType: 'application/pdf' });
|
||||||
|
expect(out).toMatch(/^attachment-\d+\.pdf$/);
|
||||||
|
|
||||||
|
const out2 = deriveAttachmentName({ name: 42, mimeType: 'application/pdf' });
|
||||||
|
expect(out2).toMatch(/^attachment-\d+\.pdf$/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('derives extension from mimeType when no name', () => {
|
||||||
|
expect(deriveAttachmentName({ mimeType: 'application/pdf' })).toMatch(/^attachment-\d+\.pdf$/);
|
||||||
|
expect(deriveAttachmentName({ mimeType: 'image/jpeg' })).toMatch(/^attachment-\d+\.jpg$/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to att.type when mimeType is missing (Telegram photos/stickers)', () => {
|
||||||
|
expect(deriveAttachmentName({ type: 'photo' })).toMatch(/^attachment-\d+\.jpg$/);
|
||||||
|
expect(deriveAttachmentName({ type: 'sticker' })).toMatch(/^attachment-\d+\.webp$/);
|
||||||
|
expect(deriveAttachmentName({ type: 'voice' })).toMatch(/^attachment-\d+\.ogg$/);
|
||||||
|
expect(deriveAttachmentName({ type: 'animation' })).toMatch(/^attachment-\d+\.mp4$/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('case-insensitive att.type lookup', () => {
|
||||||
|
expect(deriveAttachmentName({ type: 'PHOTO' })).toMatch(/^attachment-\d+\.jpg$/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns bare timestamp when nothing matches', () => {
|
||||||
|
expect(deriveAttachmentName({})).toMatch(/^attachment-\d+$/);
|
||||||
|
expect(deriveAttachmentName({ mimeType: 'application/octet-stream' })).toMatch(/^attachment-\d+$/);
|
||||||
|
expect(deriveAttachmentName({ type: 'mystery-class' })).toMatch(/^attachment-\d+$/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not crash on non-string mimeType (defensive against buggy bridges)', () => {
|
||||||
|
expect(() => deriveAttachmentName({ mimeType: { foo: 'bar' } })).not.toThrow();
|
||||||
|
expect(deriveAttachmentName({ mimeType: { foo: 'bar' } })).toMatch(/^attachment-\d+$/);
|
||||||
|
});
|
||||||
|
});
|
||||||
69
src/attachment-naming.ts
Normal file
69
src/attachment-naming.ts
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
/**
|
||||||
|
* Derive a safe, extensioned filename for inbound attachments when the
|
||||||
|
* channel bridge passes data without an explicit `name`.
|
||||||
|
*
|
||||||
|
* Two-step lookup:
|
||||||
|
* 1. `mimeType` → extension (Discord/Slack documents, Telegram document
|
||||||
|
* uploads — channels that set the MIME but not a filename).
|
||||||
|
* 2. `att.type` → extension (Telegram photos/stickers/voice/animations —
|
||||||
|
* coarse media-class set by the chat-sdk bridge with no MIME).
|
||||||
|
*
|
||||||
|
* Output is still passed through `isSafeAttachmentName` at the call site.
|
||||||
|
* The maps emit static values, so no derivation path can construct a
|
||||||
|
* traversal payload — only an attacker-controlled `att.name` can, and that
|
||||||
|
* goes through the safety guard unchanged.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Map common MIME types to canonical file extensions. Without an extension,
|
||||||
|
// agents (and humans) can't tell what kind of file landed in the inbox, and
|
||||||
|
// tools keyed on extension (image viewers, exiftool, etc.) misbehave.
|
||||||
|
const MIME_TO_EXT: Record<string, string> = {
|
||||||
|
'image/jpeg': 'jpg',
|
||||||
|
'image/png': 'png',
|
||||||
|
'image/webp': 'webp',
|
||||||
|
'image/gif': 'gif',
|
||||||
|
'image/heic': 'heic',
|
||||||
|
'audio/ogg': 'ogg',
|
||||||
|
'audio/mpeg': 'mp3',
|
||||||
|
'audio/wav': 'wav',
|
||||||
|
'audio/mp4': 'm4a',
|
||||||
|
'video/mp4': 'mp4',
|
||||||
|
'video/webm': 'webm',
|
||||||
|
'video/quicktime': 'mov',
|
||||||
|
'application/pdf': 'pdf',
|
||||||
|
'text/plain': 'txt',
|
||||||
|
'application/json': 'json',
|
||||||
|
'application/zip': 'zip',
|
||||||
|
};
|
||||||
|
|
||||||
|
// Fallback when `mimeType` is missing — Telegram photos and stickers arrive
|
||||||
|
// without an explicit MIME on the attachment object. The channel bridge sets
|
||||||
|
// `att.type` to a coarse media-class (`photo` / `sticker` / `voice` / etc.)
|
||||||
|
// which is reliable enough to derive a canonical extension. Telegram's GIFs
|
||||||
|
// are actually MP4, hence `animation: 'mp4'`.
|
||||||
|
const TYPE_TO_EXT: Record<string, string> = {
|
||||||
|
image: 'jpg',
|
||||||
|
photo: 'jpg',
|
||||||
|
sticker: 'webp',
|
||||||
|
voice: 'ogg',
|
||||||
|
audio: 'mp3',
|
||||||
|
video: 'mp4',
|
||||||
|
animation: 'mp4',
|
||||||
|
};
|
||||||
|
|
||||||
|
export function extForMime(mime: unknown): string {
|
||||||
|
if (typeof mime !== 'string' || !mime) return '';
|
||||||
|
const clean = mime.split(';')[0].trim().toLowerCase();
|
||||||
|
return MIME_TO_EXT[clean] ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
export function deriveAttachmentName(att: Record<string, unknown>): string {
|
||||||
|
const explicit = att.name;
|
||||||
|
if (typeof explicit === 'string' && explicit) return explicit;
|
||||||
|
let ext = extForMime(att.mimeType);
|
||||||
|
if (!ext && typeof att.type === 'string') {
|
||||||
|
ext = TYPE_TO_EXT[att.type.toLowerCase()] ?? '';
|
||||||
|
}
|
||||||
|
const ts = Date.now();
|
||||||
|
return ext ? `attachment-${ts}.${ext}` : `attachment-${ts}`;
|
||||||
|
}
|
||||||
@@ -135,6 +135,7 @@ export interface ChannelAdapter {
|
|||||||
// Optional
|
// Optional
|
||||||
setTyping?(platformId: string, threadId: string | null): Promise<void>;
|
setTyping?(platformId: string, threadId: string | null): Promise<void>;
|
||||||
syncConversations?(): Promise<ConversationInfo[]>;
|
syncConversations?(): Promise<ConversationInfo[]>;
|
||||||
|
resolveChannelName?(platformId: string): Promise<string | null>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subscribe the bot to a thread so follow-up messages route via the
|
* Subscribe the bot to a thread so follow-up messages route via the
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ const activeContainers = new Map<string, { process: ChildProcess; containerName:
|
|||||||
* a duplicate container against the same session directory, producing
|
* a duplicate container against the same session directory, producing
|
||||||
* racy double-replies.
|
* racy double-replies.
|
||||||
*/
|
*/
|
||||||
const wakePromises = new Map<string, Promise<void>>();
|
const wakePromises = new Map<string, Promise<boolean>>();
|
||||||
|
|
||||||
export function getActiveContainerCount(): number {
|
export function getActiveContainerCount(): number {
|
||||||
return activeContainers.size;
|
return activeContainers.size;
|
||||||
@@ -73,20 +73,32 @@ export function isContainerRunning(sessionId: string): boolean {
|
|||||||
* (the in-flight wake promise is reused).
|
* (the in-flight wake promise is reused).
|
||||||
*
|
*
|
||||||
* The container runs the v2 agent-runner which polls the session DB.
|
* The container runs the v2 agent-runner which polls the session DB.
|
||||||
|
*
|
||||||
|
* Contract: never throws. Returns `true` on successful spawn, `false` on
|
||||||
|
* transient spawn failure (e.g. OneCLI gateway unreachable). Callers don't
|
||||||
|
* need to wrap — the inbound row stays pending and host-sweep retries on
|
||||||
|
* its next tick. Callers that care (e.g. the router's typing indicator)
|
||||||
|
* can branch on the boolean.
|
||||||
*/
|
*/
|
||||||
export function wakeContainer(session: Session): Promise<void> {
|
export function wakeContainer(session: Session): Promise<boolean> {
|
||||||
if (activeContainers.has(session.id)) {
|
if (activeContainers.has(session.id)) {
|
||||||
log.debug('Container already running', { sessionId: session.id });
|
log.debug('Container already running', { sessionId: session.id });
|
||||||
return Promise.resolve();
|
return Promise.resolve(true);
|
||||||
}
|
}
|
||||||
const existing = wakePromises.get(session.id);
|
const existing = wakePromises.get(session.id);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
log.debug('Container wake already in-flight — joining existing promise', { sessionId: session.id });
|
log.debug('Container wake already in-flight — joining existing promise', { sessionId: session.id });
|
||||||
return existing;
|
return existing;
|
||||||
}
|
}
|
||||||
const promise = spawnContainer(session).finally(() => {
|
const promise = spawnContainer(session)
|
||||||
wakePromises.delete(session.id);
|
.then(() => true)
|
||||||
});
|
.catch((err) => {
|
||||||
|
log.warn('wakeContainer failed — host-sweep will retry', { sessionId: session.id, err });
|
||||||
|
return false;
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
wakePromises.delete(session.id);
|
||||||
|
});
|
||||||
wakePromises.set(session.id, promise);
|
wakePromises.set(session.id, promise);
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
@@ -435,20 +447,18 @@ async function buildContainerArgs(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// OneCLI gateway — injects HTTPS_PROXY + certs so container API calls
|
// OneCLI gateway — injects HTTPS_PROXY + certs so container API calls
|
||||||
// are routed through the agent vault for credential injection.
|
// are routed through the agent vault for credential injection. Treated as
|
||||||
try {
|
// a transient hard failure: if we can't wire the gateway, we don't spawn.
|
||||||
if (agentIdentifier) {
|
// The caller (router or host-sweep) catches the throw, leaves the inbound
|
||||||
await onecli.ensureAgent({ name: agentGroup.name, identifier: agentIdentifier });
|
// message pending, and the next sweep tick retries.
|
||||||
}
|
if (agentIdentifier) {
|
||||||
const onecliApplied = await onecli.applyContainerConfig(args, { addHostMapping: false, agent: agentIdentifier });
|
await onecli.ensureAgent({ name: agentGroup.name, identifier: agentIdentifier });
|
||||||
if (onecliApplied) {
|
|
||||||
log.info('OneCLI gateway applied', { containerName });
|
|
||||||
} else {
|
|
||||||
log.warn('OneCLI gateway not applied — container will have no credentials', { containerName });
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
log.warn('OneCLI gateway error — container will have no credentials', { containerName, err });
|
|
||||||
}
|
}
|
||||||
|
const onecliApplied = await onecli.applyContainerConfig(args, { addHostMapping: false, agent: agentIdentifier });
|
||||||
|
if (!onecliApplied) {
|
||||||
|
throw new Error('OneCLI gateway not applied — refusing to spawn container without credentials');
|
||||||
|
}
|
||||||
|
log.info('OneCLI gateway applied', { containerName });
|
||||||
|
|
||||||
// Host gateway
|
// Host gateway
|
||||||
args.push(...hostGatewayArgs());
|
args.push(...hostGatewayArgs());
|
||||||
|
|||||||
@@ -168,6 +168,8 @@ async function sweepSession(session: Session): Promise<void> {
|
|||||||
const dueCount = countDueMessages(inDb);
|
const dueCount = countDueMessages(inDb);
|
||||||
if (dueCount > 0 && !isContainerRunning(session.id)) {
|
if (dueCount > 0 && !isContainerRunning(session.id)) {
|
||||||
log.info('Waking container for due messages', { sessionId: session.id, count: dueCount });
|
log.info('Waking container for due messages', { sessionId: session.id, count: dueCount });
|
||||||
|
// wakeContainer never throws — transient spawn failures (OneCLI down,
|
||||||
|
// etc.) return false and leave messages pending for the next tick.
|
||||||
await wakeContainer(session);
|
await wakeContainer(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -153,8 +153,10 @@ describe('unknown-channel registration flow', () => {
|
|||||||
expect(kind).toBe('chat-sdk');
|
expect(kind).toBe('chat-sdk');
|
||||||
const payload = JSON.parse(content as string);
|
const payload = JSON.parse(content as string);
|
||||||
expect(payload.type).toBe('ask_question');
|
expect(payload.type).toBe('ask_question');
|
||||||
// Card names the target agent so the owner knows what they're wiring to.
|
// Single-agent card offers a direct "Connect to <name>" button.
|
||||||
expect(payload.question).toContain('Andy');
|
const connectOption = payload.options.find((o: { value: string }) => o.value.startsWith('connect:'));
|
||||||
|
expect(connectOption).toBeDefined();
|
||||||
|
expect(connectOption.label).toContain('Andy');
|
||||||
|
|
||||||
const { getDb } = await import('../../db/connection.js');
|
const { getDb } = await import('../../db/connection.js');
|
||||||
const rows = getDb().prepare('SELECT * FROM pending_channel_approvals').all() as Array<{
|
const rows = getDb().prepare('SELECT * FROM pending_channel_approvals').all() as Array<{
|
||||||
@@ -202,11 +204,11 @@ describe('unknown-channel registration flow', () => {
|
|||||||
};
|
};
|
||||||
expect(pending).toBeDefined();
|
expect(pending).toBeDefined();
|
||||||
|
|
||||||
// Owner clicks approve.
|
// Owner clicks "Connect to Andy" (single-agent card).
|
||||||
for (const handler of getResponseHandlers()) {
|
for (const handler of getResponseHandlers()) {
|
||||||
const claimed = await handler({
|
const claimed = await handler({
|
||||||
questionId: pending.messaging_group_id,
|
questionId: pending.messaging_group_id,
|
||||||
value: 'approve',
|
value: 'connect:ag-1',
|
||||||
userId: 'owner', // raw platform id — handler namespaces it
|
userId: 'owner', // raw platform id — handler namespaces it
|
||||||
channelType: 'telegram',
|
channelType: 'telegram',
|
||||||
platformId: 'dm-owner',
|
platformId: 'dm-owner',
|
||||||
@@ -215,7 +217,7 @@ describe('unknown-channel registration flow', () => {
|
|||||||
if (claimed) break;
|
if (claimed) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wiring created with MVP defaults.
|
// Wiring created with defaults.
|
||||||
const mga = getDb()
|
const mga = getDb()
|
||||||
.prepare('SELECT * FROM messaging_group_agents WHERE messaging_group_id = ?')
|
.prepare('SELECT * FROM messaging_group_agents WHERE messaging_group_id = ?')
|
||||||
.get(pending.messaging_group_id) as {
|
.get(pending.messaging_group_id) as {
|
||||||
@@ -261,7 +263,7 @@ describe('unknown-channel registration flow', () => {
|
|||||||
for (const handler of getResponseHandlers()) {
|
for (const handler of getResponseHandlers()) {
|
||||||
const claimed = await handler({
|
const claimed = await handler({
|
||||||
questionId: pending.messaging_group_id,
|
questionId: pending.messaging_group_id,
|
||||||
value: 'approve',
|
value: 'connect:ag-1',
|
||||||
userId: 'owner',
|
userId: 'owner',
|
||||||
channelType: 'telegram',
|
channelType: 'telegram',
|
||||||
platformId: 'dm-owner',
|
platformId: 'dm-owner',
|
||||||
|
|||||||
@@ -5,24 +5,32 @@
|
|||||||
* addressed to the bot (SDK-confirmed mention or DM), it calls
|
* addressed to the bot (SDK-confirmed mention or DM), it calls
|
||||||
* `requestChannelApproval` instead of silently dropping. The flow:
|
* `requestChannelApproval` instead of silently dropping. The flow:
|
||||||
*
|
*
|
||||||
* 1. Pick the target agent group we'd wire to (MVP: first by name).
|
* 1. Gather all existing agent groups.
|
||||||
* Multi-agent picker is a follow-up — see ACTION-ITEMS.
|
|
||||||
* 2. Pick an eligible approver (owner / admin) and a reachable DM for
|
* 2. Pick an eligible approver (owner / admin) and a reachable DM for
|
||||||
* them, reusing the same primitives the sender-approval flow uses.
|
* them, reusing the same primitives the sender-approval flow uses.
|
||||||
* 3. Deliver an Approve / Ignore card that names the target agent
|
* 3. Deliver a card with three action families:
|
||||||
* explicitly so the owner knows what they're wiring to.
|
* a. Connect to [agent] — one button per existing agent group.
|
||||||
|
* Single-agent installs get a one-click connect.
|
||||||
|
* b. Connect new agent — prompts for a free-text name, creates
|
||||||
|
* the agent immediately on reply.
|
||||||
|
* c. Reject — deny the channel.
|
||||||
* 4. Record a `pending_channel_approvals` row holding the original event
|
* 4. Record a `pending_channel_approvals` row holding the original event
|
||||||
* so it can be re-routed on approve.
|
* so it can be re-routed on connect/create.
|
||||||
*
|
*
|
||||||
* On approve (handler in index.ts):
|
* On connect (handler in index.ts):
|
||||||
* - Create `messaging_group_agents` with MVP defaults
|
* - Create `messaging_group_agents` with defaults
|
||||||
* (mention-sticky for groups / pattern='.' for DMs,
|
* (mention-sticky for groups / pattern='.' for DMs,
|
||||||
* sender_scope='known', ignored_message_policy='accumulate')
|
* sender_scope='known', ignored_message_policy='accumulate')
|
||||||
* - Add the triggering sender to `agent_group_members` so sender_scope
|
* - Add the triggering sender to `agent_group_members` so sender_scope
|
||||||
* doesn't bounce the replayed message into a sender-approval cascade
|
* doesn't bounce the replayed message into a sender-approval cascade
|
||||||
* - Delete the pending row, replay the original event
|
* - Delete the pending row, replay the original event
|
||||||
*
|
*
|
||||||
* On ignore:
|
* On connect new agent (handler in index.ts):
|
||||||
|
* - Prompt for a free-text agent name via DM
|
||||||
|
* - On reply: create the agent group + filesystem, then wire
|
||||||
|
* and replay as above
|
||||||
|
*
|
||||||
|
* On reject:
|
||||||
* - Set `messaging_groups.denied_at = now()` so the router stops
|
* - Set `messaging_groups.denied_at = now()` so the router stops
|
||||||
* escalating on this channel until an admin explicitly re-wires
|
* escalating on this channel until an admin explicitly re-wires
|
||||||
* - Delete the pending row
|
* - Delete the pending row
|
||||||
@@ -36,19 +44,81 @@
|
|||||||
* - Approver has no reachable DM.
|
* - Approver has no reachable DM.
|
||||||
* - Delivery adapter missing.
|
* - Delivery adapter missing.
|
||||||
*/
|
*/
|
||||||
import { normalizeOptions, type RawOption } from '../../channels/ask-question.js';
|
import { normalizeOptions, type NormalizedOption, type RawOption } from '../../channels/ask-question.js';
|
||||||
import { getAllAgentGroups } from '../../db/agent-groups.js';
|
import { createAgentGroup, getAgentGroup, getAgentGroupByFolder, getAllAgentGroups } from '../../db/agent-groups.js';
|
||||||
import { getMessagingGroup } from '../../db/messaging-groups.js';
|
import { getChannelAdapter } from '../../channels/channel-registry.js';
|
||||||
|
import { getMessagingGroup, updateMessagingGroup } from '../../db/messaging-groups.js';
|
||||||
import { getDeliveryAdapter } from '../../delivery.js';
|
import { getDeliveryAdapter } from '../../delivery.js';
|
||||||
|
import { initGroupFilesystem } from '../../group-init.js';
|
||||||
import { log } from '../../log.js';
|
import { log } from '../../log.js';
|
||||||
import type { InboundEvent } from '../../channels/adapter.js';
|
import type { InboundEvent } from '../../channels/adapter.js';
|
||||||
|
import type { AgentGroup } from '../../types.js';
|
||||||
import { pickApprovalDelivery, pickApprover } from '../approvals/primitive.js';
|
import { pickApprovalDelivery, pickApprover } from '../approvals/primitive.js';
|
||||||
import { createPendingChannelApproval, hasInFlightChannelApproval } from './db/pending-channel-approvals.js';
|
import { createPendingChannelApproval, hasInFlightChannelApproval } from './db/pending-channel-approvals.js';
|
||||||
|
|
||||||
const APPROVAL_OPTIONS: RawOption[] = [
|
// ── Value constants (response handler in index.ts parses these) ──
|
||||||
{ label: 'Approve', selectedLabel: '✅ Wired', value: 'approve' },
|
|
||||||
{ label: 'Ignore', selectedLabel: '🙅 Ignored', value: 'reject' },
|
export const CONNECT_PREFIX = 'connect:';
|
||||||
];
|
export const NEW_AGENT_VALUE = 'new_agent';
|
||||||
|
export const CHOOSE_EXISTING_VALUE = 'choose_existing';
|
||||||
|
export const REJECT_VALUE = 'reject';
|
||||||
|
|
||||||
|
// ── Utilities ──
|
||||||
|
|
||||||
|
function toFolder(name: string): string {
|
||||||
|
return (
|
||||||
|
name
|
||||||
|
.toLowerCase()
|
||||||
|
.replace(/[^a-z0-9]+/g, '-')
|
||||||
|
.replace(/^-+|-+$/g, '') || 'unnamed'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Card builders ──
|
||||||
|
|
||||||
|
function buildApprovalOptions(agentGroups: AgentGroup[]): RawOption[] {
|
||||||
|
const options: RawOption[] = [];
|
||||||
|
if (agentGroups.length === 1) {
|
||||||
|
options.push({
|
||||||
|
label: `Connect to ${agentGroups[0].name}`,
|
||||||
|
selectedLabel: `✅ Connected to ${agentGroups[0].name}`,
|
||||||
|
value: `${CONNECT_PREFIX}${agentGroups[0].id}`,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
options.push({
|
||||||
|
label: 'Choose existing agent',
|
||||||
|
selectedLabel: '📋 Choosing…',
|
||||||
|
value: CHOOSE_EXISTING_VALUE,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
options.push({
|
||||||
|
label: 'Connect new agent',
|
||||||
|
selectedLabel: '🆕 Connecting new agent…',
|
||||||
|
value: NEW_AGENT_VALUE,
|
||||||
|
});
|
||||||
|
options.push({
|
||||||
|
label: 'Reject',
|
||||||
|
selectedLabel: '🙅 Rejected',
|
||||||
|
value: REJECT_VALUE,
|
||||||
|
});
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildQuestionText(
|
||||||
|
isGroup: boolean,
|
||||||
|
senderName: string | undefined,
|
||||||
|
channelName: string | null,
|
||||||
|
channelType: string,
|
||||||
|
): string {
|
||||||
|
const who = senderName ?? 'Someone';
|
||||||
|
if (isGroup) {
|
||||||
|
const where = channelName ? `${channelName} on ${channelType}` : `a ${channelType} channel`;
|
||||||
|
return `${who} mentioned your bot in ${where}. How would you like to handle this channel?`;
|
||||||
|
}
|
||||||
|
return `${who} sent your bot a DM on ${channelType}. How would you like to handle it?`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Main flow ──
|
||||||
|
|
||||||
export interface RequestChannelApprovalInput {
|
export interface RequestChannelApprovalInput {
|
||||||
messagingGroupId: string;
|
messagingGroupId: string;
|
||||||
@@ -58,17 +128,11 @@ export interface RequestChannelApprovalInput {
|
|||||||
export async function requestChannelApproval(input: RequestChannelApprovalInput): Promise<void> {
|
export async function requestChannelApproval(input: RequestChannelApprovalInput): Promise<void> {
|
||||||
const { messagingGroupId, event } = input;
|
const { messagingGroupId, event } = input;
|
||||||
|
|
||||||
// In-flight dedup: don't spam the owner if the same unwired channel
|
|
||||||
// gets more mentions / DMs while a card is already pending.
|
|
||||||
if (hasInFlightChannelApproval(messagingGroupId)) {
|
if (hasInFlightChannelApproval(messagingGroupId)) {
|
||||||
log.debug('Channel registration already in flight — dropping retry', {
|
log.debug('Channel registration already in flight — dropping retry', { messagingGroupId });
|
||||||
messagingGroupId,
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// MVP: pick the first agent group by name. Multi-agent systems will get
|
|
||||||
// a richer card later (user picks the target from a list).
|
|
||||||
const agentGroups = getAllAgentGroups();
|
const agentGroups = getAllAgentGroups();
|
||||||
if (agentGroups.length === 0) {
|
if (agentGroups.length === 0) {
|
||||||
log.warn('Channel registration skipped — no agent groups configured. Run /init-first-agent.', {
|
log.warn('Channel registration skipped — no agent groups configured. Run /init-first-agent.', {
|
||||||
@@ -76,55 +140,65 @@ export async function requestChannelApproval(input: RequestChannelApprovalInput)
|
|||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const target = agentGroups[0];
|
// Use first agent group for approver resolution — owners and global admins
|
||||||
|
// are returned regardless of which group we pass.
|
||||||
|
const referenceGroup = agentGroups[0];
|
||||||
|
|
||||||
// pickApprover takes the target agent group's id — gets scoped admins +
|
const approvers = pickApprover(referenceGroup.id);
|
||||||
// global admins + owners. For fresh installs with only an owner, the
|
|
||||||
// owner is returned.
|
|
||||||
const approvers = pickApprover(target.id);
|
|
||||||
if (approvers.length === 0) {
|
if (approvers.length === 0) {
|
||||||
log.warn('Channel registration skipped — no owner or admin configured', {
|
log.warn('Channel registration skipped — no owner or admin configured', {
|
||||||
messagingGroupId,
|
messagingGroupId,
|
||||||
targetAgentGroupId: target.id,
|
targetAgentGroupId: referenceGroup.id,
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const originMg = getMessagingGroup(messagingGroupId);
|
const originMg = getMessagingGroup(messagingGroupId);
|
||||||
const originChannelType = originMg?.channel_type ?? '';
|
const originChannelType = originMg?.channel_type ?? '';
|
||||||
|
|
||||||
|
// Resolve channel name if not yet persisted.
|
||||||
|
if (originMg && !originMg.name) {
|
||||||
|
const channelAdapter = getChannelAdapter(originChannelType);
|
||||||
|
if (channelAdapter?.resolveChannelName) {
|
||||||
|
try {
|
||||||
|
const name = await channelAdapter.resolveChannelName(originMg.platform_id);
|
||||||
|
if (name) {
|
||||||
|
updateMessagingGroup(originMg.id, { name });
|
||||||
|
originMg.name = name;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
/* non-critical */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const delivery = await pickApprovalDelivery(approvers, originChannelType);
|
const delivery = await pickApprovalDelivery(approvers, originChannelType);
|
||||||
if (!delivery) {
|
if (!delivery) {
|
||||||
log.warn('Channel registration skipped — no DM channel for any approver', {
|
log.warn('Channel registration skipped — no DM channel for any approver', {
|
||||||
messagingGroupId,
|
messagingGroupId,
|
||||||
targetAgentGroupId: target.id,
|
targetAgentGroupId: referenceGroup.id,
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isGroup = event.message?.isGroup ?? originMg?.is_group === 1;
|
const isGroup = event.message?.isGroup ?? originMg?.is_group === 1;
|
||||||
|
|
||||||
// Extract sender name from the event content for a human-readable card.
|
|
||||||
let senderName: string | undefined;
|
let senderName: string | undefined;
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(event.message.content) as Record<string, unknown>;
|
const parsed = JSON.parse(event.message.content) as Record<string, unknown>;
|
||||||
senderName = (parsed.senderName ?? parsed.sender) as string | undefined;
|
senderName = (parsed.senderName ?? parsed.sender) as string | undefined;
|
||||||
} catch {
|
} catch {
|
||||||
// non-critical — fall through to generic wording
|
// non-critical
|
||||||
}
|
}
|
||||||
|
|
||||||
const title = isGroup ? '📣 Bot mentioned in new chat' : '💬 New direct message';
|
const channelName = originMg?.name ?? null;
|
||||||
const question = isGroup
|
const title = isGroup ? '📣 Bot mentioned in new channel' : '💬 New direct message';
|
||||||
? senderName
|
const question = buildQuestionText(isGroup, senderName, channelName, originChannelType);
|
||||||
? `${senderName} mentioned your agent in a ${originChannelType} channel. Wire it to ${target.name} and let it engage?`
|
const options = normalizeOptions(buildApprovalOptions(agentGroups));
|
||||||
: `Your agent was mentioned in a ${originChannelType} channel. Wire it to ${target.name} and let it engage?`
|
|
||||||
: senderName
|
|
||||||
? `${senderName} DM'd your agent on ${originChannelType}. Wire it to ${target.name} and let it respond?`
|
|
||||||
: `Someone DM'd your agent on ${originChannelType}. Wire it to ${target.name} and let it respond?`;
|
|
||||||
const options = normalizeOptions(APPROVAL_OPTIONS);
|
|
||||||
|
|
||||||
createPendingChannelApproval({
|
createPendingChannelApproval({
|
||||||
messaging_group_id: messagingGroupId,
|
messaging_group_id: messagingGroupId,
|
||||||
agent_group_id: target.id,
|
agent_group_id: referenceGroup.id,
|
||||||
original_message: JSON.stringify(event),
|
original_message: JSON.stringify(event),
|
||||||
approver_user_id: delivery.userId,
|
approver_user_id: delivery.userId,
|
||||||
created_at: new Date().toISOString(),
|
created_at: new Date().toISOString(),
|
||||||
@@ -134,9 +208,7 @@ export async function requestChannelApproval(input: RequestChannelApprovalInput)
|
|||||||
|
|
||||||
const adapter = getDeliveryAdapter();
|
const adapter = getDeliveryAdapter();
|
||||||
if (!adapter) {
|
if (!adapter) {
|
||||||
log.error('Channel registration row created but no delivery adapter is wired', {
|
log.error('Channel registration row created but no delivery adapter is wired', { messagingGroupId });
|
||||||
messagingGroupId,
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,9 +220,6 @@ export async function requestChannelApproval(input: RequestChannelApprovalInput)
|
|||||||
'chat-sdk',
|
'chat-sdk',
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
type: 'ask_question',
|
type: 'ask_question',
|
||||||
// Use messaging_group_id as the questionId — it's unique per card
|
|
||||||
// (PK on pending table dedups) and lets the response handler look
|
|
||||||
// up the pending row directly without another index.
|
|
||||||
questionId: messagingGroupId,
|
questionId: messagingGroupId,
|
||||||
title,
|
title,
|
||||||
question,
|
question,
|
||||||
@@ -159,16 +228,56 @@ export async function requestChannelApproval(input: RequestChannelApprovalInput)
|
|||||||
);
|
);
|
||||||
log.info('Channel registration card delivered', {
|
log.info('Channel registration card delivered', {
|
||||||
messagingGroupId,
|
messagingGroupId,
|
||||||
targetAgentGroupId: target.id,
|
agentGroupCount: agentGroups.length,
|
||||||
approver: delivery.userId,
|
approver: delivery.userId,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.error('Channel registration card delivery failed', {
|
log.error('Channel registration card delivery failed', { messagingGroupId, err });
|
||||||
messagingGroupId,
|
|
||||||
err,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const APPROVE_VALUE = 'approve';
|
// ── Helpers for the response handler (index.ts) ──
|
||||||
export const REJECT_VALUE = 'reject';
|
|
||||||
|
/**
|
||||||
|
* Build normalized options for the agent-selection follow-up card.
|
||||||
|
*/
|
||||||
|
export function buildAgentSelectionOptions(agentGroups: AgentGroup[]): NormalizedOption[] {
|
||||||
|
const options: RawOption[] = agentGroups.map((ag) => ({
|
||||||
|
label: ag.name,
|
||||||
|
selectedLabel: `✅ Connected to ${ag.name}`,
|
||||||
|
value: `${CONNECT_PREFIX}${ag.id}`,
|
||||||
|
}));
|
||||||
|
options.push({
|
||||||
|
label: 'Cancel',
|
||||||
|
selectedLabel: '🙅 Cancelled',
|
||||||
|
value: REJECT_VALUE,
|
||||||
|
});
|
||||||
|
return normalizeOptions(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new agent group and initialize its filesystem. Handles
|
||||||
|
* folder-name collisions with numeric suffixes.
|
||||||
|
*/
|
||||||
|
export function createNewAgentGroup(name: string): AgentGroup {
|
||||||
|
let folder = toFolder(name);
|
||||||
|
const baseFolder = folder;
|
||||||
|
let suffix = 2;
|
||||||
|
while (getAgentGroupByFolder(folder)) {
|
||||||
|
folder = `${baseFolder}-${suffix}`;
|
||||||
|
suffix++;
|
||||||
|
}
|
||||||
|
|
||||||
|
const agId = `ag-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
||||||
|
createAgentGroup({
|
||||||
|
id: agId,
|
||||||
|
name,
|
||||||
|
folder,
|
||||||
|
agent_provider: null,
|
||||||
|
created_at: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const ag = getAgentGroup(agId)!;
|
||||||
|
initGroupFilesystem(ag);
|
||||||
|
return ag;
|
||||||
|
}
|
||||||
|
|||||||
@@ -51,6 +51,12 @@ export function hasInFlightChannelApproval(messagingGroupId: string): boolean {
|
|||||||
return row !== undefined;
|
return row !== undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function updatePendingChannelApprovalCard(messagingGroupId: string, title: string, optionsJson: string): void {
|
||||||
|
getDb()
|
||||||
|
.prepare('UPDATE pending_channel_approvals SET title = ?, options_json = ? WHERE messaging_group_id = ?')
|
||||||
|
.run(title, optionsJson, messagingGroupId);
|
||||||
|
}
|
||||||
|
|
||||||
export function deletePendingChannelApproval(messagingGroupId: string): void {
|
export function deletePendingChannelApproval(messagingGroupId: string): void {
|
||||||
getDb().prepare('DELETE FROM pending_channel_approvals WHERE messaging_group_id = ?').run(messagingGroupId);
|
getDb().prepare('DELETE FROM pending_channel_approvals WHERE messaging_group_id = ?').run(messagingGroupId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,27 +16,53 @@
|
|||||||
* access gate is not registered and core defaults to allow-all.
|
* access gate is not registered and core defaults to allow-all.
|
||||||
*/
|
*/
|
||||||
import { recordDroppedMessage } from '../../db/dropped-messages.js';
|
import { recordDroppedMessage } from '../../db/dropped-messages.js';
|
||||||
|
import { getAgentGroup, getAllAgentGroups } from '../../db/agent-groups.js';
|
||||||
import { createMessagingGroupAgent, setMessagingGroupDeniedAt } from '../../db/messaging-groups.js';
|
import { createMessagingGroupAgent, setMessagingGroupDeniedAt } from '../../db/messaging-groups.js';
|
||||||
import {
|
import {
|
||||||
routeInbound,
|
routeInbound,
|
||||||
setAccessGate,
|
setAccessGate,
|
||||||
setChannelRequestGate,
|
setChannelRequestGate,
|
||||||
|
setMessageInterceptor,
|
||||||
setSenderResolver,
|
setSenderResolver,
|
||||||
setSenderScopeGate,
|
setSenderScopeGate,
|
||||||
type AccessGateResult,
|
type AccessGateResult,
|
||||||
} from '../../router.js';
|
} from '../../router.js';
|
||||||
import type { InboundEvent } from '../../channels/adapter.js';
|
import type { InboundEvent } from '../../channels/adapter.js';
|
||||||
import { registerResponseHandler, type ResponsePayload } from '../../response-registry.js';
|
import { registerResponseHandler, type ResponsePayload } from '../../response-registry.js';
|
||||||
|
import { getDeliveryAdapter } from '../../delivery.js';
|
||||||
import { log } from '../../log.js';
|
import { log } from '../../log.js';
|
||||||
import type { MessagingGroup, MessagingGroupAgent } from '../../types.js';
|
import type { MessagingGroup, MessagingGroupAgent } from '../../types.js';
|
||||||
import { canAccessAgentGroup } from './access.js';
|
import { canAccessAgentGroup } from './access.js';
|
||||||
import { requestChannelApproval } from './channel-approval.js';
|
import {
|
||||||
|
buildAgentSelectionOptions,
|
||||||
|
CHOOSE_EXISTING_VALUE,
|
||||||
|
CONNECT_PREFIX,
|
||||||
|
createNewAgentGroup,
|
||||||
|
NEW_AGENT_VALUE,
|
||||||
|
REJECT_VALUE,
|
||||||
|
requestChannelApproval,
|
||||||
|
} from './channel-approval.js';
|
||||||
import { addMember } from './db/agent-group-members.js';
|
import { addMember } from './db/agent-group-members.js';
|
||||||
import { deletePendingChannelApproval, getPendingChannelApproval } from './db/pending-channel-approvals.js';
|
import {
|
||||||
|
deletePendingChannelApproval,
|
||||||
|
getPendingChannelApproval,
|
||||||
|
updatePendingChannelApprovalCard,
|
||||||
|
} from './db/pending-channel-approvals.js';
|
||||||
import { deletePendingSenderApproval, getPendingSenderApproval } from './db/pending-sender-approvals.js';
|
import { deletePendingSenderApproval, getPendingSenderApproval } from './db/pending-sender-approvals.js';
|
||||||
import { hasAdminPrivilege } from './db/user-roles.js';
|
import { hasAdminPrivilege } from './db/user-roles.js';
|
||||||
import { getUser, upsertUser } from './db/users.js';
|
import { getUser, upsertUser } from './db/users.js';
|
||||||
import { requestSenderApproval } from './sender-approval.js';
|
import { requestSenderApproval } from './sender-approval.js';
|
||||||
|
import { ensureUserDm } from './user-dm.js';
|
||||||
|
|
||||||
|
// ── Free-text name input state ──
|
||||||
|
// Tracks approvers waiting for a text reply with the agent name. Keyed by
|
||||||
|
// namespaced userId (e.g. "slack:U0ABC"). Cleared on receipt or restart.
|
||||||
|
interface PendingNameInput {
|
||||||
|
channelMgId: string;
|
||||||
|
dmChannelType: string;
|
||||||
|
dmPlatformId: string;
|
||||||
|
}
|
||||||
|
const awaitingNameInput = new Map<string, PendingNameInput>();
|
||||||
|
|
||||||
function extractAndUpsertUser(event: InboundEvent): string | null {
|
function extractAndUpsertUser(event: InboundEvent): string | null {
|
||||||
let content: Record<string, unknown>;
|
let content: Record<string, unknown>;
|
||||||
@@ -271,22 +297,17 @@ setChannelRequestGate(async (mg, event) => {
|
|||||||
* by messaging_group_id). If no such row, return false so downstream
|
* by messaging_group_id). If no such row, return false so downstream
|
||||||
* handlers get a shot.
|
* handlers get a shot.
|
||||||
*
|
*
|
||||||
* Approve: create the wiring with MVP defaults (mention-sticky for
|
* Value dispatch:
|
||||||
* groups / pattern='.' for DMs; sender_scope='known';
|
* connect:<id> — wire to an existing agent group, replay the message
|
||||||
* ignored_message_policy='accumulate'), add the triggering sender as a
|
* choose_existing — send a follow-up card listing all agents
|
||||||
* member so sender_scope doesn't immediately bounce them into a
|
* new_agent — prompt for a free-text agent name (interceptor
|
||||||
* sender-approval card, then replay the original event.
|
* captures the reply and creates immediately)
|
||||||
*
|
* reject — set denied_at, delete pending row
|
||||||
* Deny: set `messaging_groups.denied_at = now()` so future mentions on
|
|
||||||
* this channel drop silently until an admin explicitly wires it.
|
|
||||||
*/
|
*/
|
||||||
async function handleChannelApprovalResponse(payload: ResponsePayload): Promise<boolean> {
|
async function handleChannelApprovalResponse(payload: ResponsePayload): Promise<boolean> {
|
||||||
const row = getPendingChannelApproval(payload.questionId);
|
const row = getPendingChannelApproval(payload.questionId);
|
||||||
if (!row) return false;
|
if (!row) return false;
|
||||||
|
|
||||||
// Click-auth: same pattern as sender-approval (see commit 68058cb).
|
|
||||||
// Raw platform userId → namespace with channelType → must match the
|
|
||||||
// designated approver OR have admin privilege over the target agent.
|
|
||||||
const clickerId = payload.userId ? `${payload.channelType}:${payload.userId}` : null;
|
const clickerId = payload.userId ? `${payload.channelType}:${payload.userId}` : null;
|
||||||
const isAuthorized =
|
const isAuthorized =
|
||||||
clickerId !== null && (clickerId === row.approver_user_id || hasAdminPrivilege(clickerId, row.agent_group_id));
|
clickerId !== null && (clickerId === row.approver_user_id || hasAdminPrivilege(clickerId, row.agent_group_id));
|
||||||
@@ -296,25 +317,129 @@ async function handleChannelApprovalResponse(payload: ResponsePayload): Promise<
|
|||||||
clickerId,
|
clickerId,
|
||||||
expectedApprover: row.approver_user_id,
|
expectedApprover: row.approver_user_id,
|
||||||
});
|
});
|
||||||
return true; // claim but take no action
|
return true;
|
||||||
}
|
}
|
||||||
const approverId = clickerId;
|
const approverId = clickerId;
|
||||||
const approved = payload.value === 'approve';
|
|
||||||
|
|
||||||
if (!approved) {
|
// ── Reject / Cancel ──
|
||||||
|
if (payload.value === REJECT_VALUE) {
|
||||||
setMessagingGroupDeniedAt(row.messaging_group_id, new Date().toISOString());
|
setMessagingGroupDeniedAt(row.messaging_group_id, new Date().toISOString());
|
||||||
deletePendingChannelApproval(row.messaging_group_id);
|
deletePendingChannelApproval(row.messaging_group_id);
|
||||||
log.info('Channel registration denied', {
|
log.info('Channel registration denied', {
|
||||||
messagingGroupId: row.messaging_group_id,
|
messagingGroupId: row.messaging_group_id,
|
||||||
agentGroupId: row.agent_group_id,
|
|
||||||
approverId,
|
approverId,
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rehydrate the original event to know (a) whether it was a DM or group
|
// ── Choose existing agent — send agent-selection follow-up card ──
|
||||||
// (chooses engage_mode default), and (b) who the triggering sender was
|
if (payload.value === CHOOSE_EXISTING_VALUE) {
|
||||||
// (auto-member-add so sender_scope='known' doesn't bounce the replay).
|
const approverDm = await ensureUserDm(row.approver_user_id);
|
||||||
|
if (!approverDm) {
|
||||||
|
log.error('Channel registration: no DM channel for approver', {
|
||||||
|
messagingGroupId: row.messaging_group_id,
|
||||||
|
approverUserId: row.approver_user_id,
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const adapter = getDeliveryAdapter();
|
||||||
|
if (!adapter) return true;
|
||||||
|
|
||||||
|
const agentGroups = getAllAgentGroups();
|
||||||
|
const options = buildAgentSelectionOptions(agentGroups);
|
||||||
|
const title = '📋 Choose an agent';
|
||||||
|
updatePendingChannelApprovalCard(row.messaging_group_id, title, JSON.stringify(options));
|
||||||
|
|
||||||
|
try {
|
||||||
|
await adapter.deliver(
|
||||||
|
approverDm.channel_type,
|
||||||
|
approverDm.platform_id,
|
||||||
|
null,
|
||||||
|
'chat-sdk',
|
||||||
|
JSON.stringify({
|
||||||
|
type: 'ask_question',
|
||||||
|
questionId: row.messaging_group_id,
|
||||||
|
title,
|
||||||
|
question: 'Which agent should handle this channel?',
|
||||||
|
options,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
log.error('Channel registration: agent-selection card delivery failed', {
|
||||||
|
messagingGroupId: row.messaging_group_id,
|
||||||
|
err,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Create new agent — prompt for free-text name ──
|
||||||
|
if (payload.value === NEW_AGENT_VALUE) {
|
||||||
|
const approverDm = await ensureUserDm(row.approver_user_id);
|
||||||
|
if (!approverDm) {
|
||||||
|
log.error('Channel registration: no DM channel for approver', {
|
||||||
|
messagingGroupId: row.messaging_group_id,
|
||||||
|
approverUserId: row.approver_user_id,
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const adapter = getDeliveryAdapter();
|
||||||
|
if (!adapter) {
|
||||||
|
log.error('Channel registration: no delivery adapter for name prompt', {
|
||||||
|
messagingGroupId: row.messaging_group_id,
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
awaitingNameInput.set(row.approver_user_id, {
|
||||||
|
channelMgId: row.messaging_group_id,
|
||||||
|
dmChannelType: approverDm.channel_type,
|
||||||
|
dmPlatformId: approverDm.platform_id,
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
await adapter.deliver(
|
||||||
|
approverDm.channel_type,
|
||||||
|
approverDm.platform_id,
|
||||||
|
null,
|
||||||
|
'chat-sdk',
|
||||||
|
JSON.stringify({ text: 'Reply with the name for your new agent:' }),
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
log.error('Channel registration: name prompt delivery failed', {
|
||||||
|
messagingGroupId: row.messaging_group_id,
|
||||||
|
err,
|
||||||
|
});
|
||||||
|
awaitingNameInput.delete(row.approver_user_id);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Resolve target agent group (connect to existing or create new) ──
|
||||||
|
let targetAgentGroupId: string;
|
||||||
|
|
||||||
|
if (payload.value.startsWith(CONNECT_PREFIX)) {
|
||||||
|
targetAgentGroupId = payload.value.slice(CONNECT_PREFIX.length);
|
||||||
|
const ag = getAgentGroup(targetAgentGroupId);
|
||||||
|
if (!ag) {
|
||||||
|
log.error('Channel registration: target agent group no longer exists', {
|
||||||
|
messagingGroupId: row.messaging_group_id,
|
||||||
|
targetAgentGroupId,
|
||||||
|
});
|
||||||
|
deletePendingChannelApproval(row.messaging_group_id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.warn('Channel registration: unknown response value', {
|
||||||
|
messagingGroupId: row.messaging_group_id,
|
||||||
|
value: payload.value,
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Wire + replay (shared path for connect and create) ──
|
||||||
let event: InboundEvent;
|
let event: InboundEvent;
|
||||||
try {
|
try {
|
||||||
event = JSON.parse(row.original_message) as InboundEvent;
|
event = JSON.parse(row.original_message) as InboundEvent;
|
||||||
@@ -327,15 +452,6 @@ async function handleChannelApprovalResponse(payload: ResponsePayload): Promise<
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decide engage_mode from the original event. DMs (`isMention=true` &
|
|
||||||
// not in a group) get `pattern='.'` (always respond). Group mentions
|
|
||||||
// get `mention-sticky` (respond now + follow the thread).
|
|
||||||
//
|
|
||||||
// We can't read `mg.is_group` reliably here because we only auto-create
|
|
||||||
// the mg with `is_group=0` on first sight — the adapter hasn't told us
|
|
||||||
// yet whether it's actually a group. Fall back to the InboundEvent's
|
|
||||||
// `threadId`: a non-null threadId implies a threaded platform (Slack
|
|
||||||
// channel thread, Discord thread), which we treat as a group.
|
|
||||||
const isGroup = event.threadId !== null;
|
const isGroup = event.threadId !== null;
|
||||||
const engageMode: MessagingGroupAgent['engage_mode'] = isGroup ? 'mention-sticky' : 'pattern';
|
const engageMode: MessagingGroupAgent['engage_mode'] = isGroup ? 'mention-sticky' : 'pattern';
|
||||||
const engagePattern = isGroup ? null : '.';
|
const engagePattern = isGroup ? null : '.';
|
||||||
@@ -344,7 +460,7 @@ async function handleChannelApprovalResponse(payload: ResponsePayload): Promise<
|
|||||||
createMessagingGroupAgent({
|
createMessagingGroupAgent({
|
||||||
id: mgaId,
|
id: mgaId,
|
||||||
messaging_group_id: row.messaging_group_id,
|
messaging_group_id: row.messaging_group_id,
|
||||||
agent_group_id: row.agent_group_id,
|
agent_group_id: targetAgentGroupId,
|
||||||
engage_mode: engageMode,
|
engage_mode: engageMode,
|
||||||
engage_pattern: engagePattern,
|
engage_pattern: engagePattern,
|
||||||
sender_scope: 'known',
|
sender_scope: 'known',
|
||||||
@@ -355,28 +471,22 @@ async function handleChannelApprovalResponse(payload: ResponsePayload): Promise<
|
|||||||
});
|
});
|
||||||
log.info('Channel registration approved — wiring created', {
|
log.info('Channel registration approved — wiring created', {
|
||||||
messagingGroupId: row.messaging_group_id,
|
messagingGroupId: row.messaging_group_id,
|
||||||
agentGroupId: row.agent_group_id,
|
agentGroupId: targetAgentGroupId,
|
||||||
mgaId,
|
mgaId,
|
||||||
engageMode,
|
engageMode,
|
||||||
approverId,
|
approverId,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Auto-admit the triggering sender. Without this, the replay below
|
|
||||||
// would bounce through sender-approval (sender_scope='known' +
|
|
||||||
// sender-is-not-a-member).
|
|
||||||
const senderUserId = extractAndUpsertUser(event);
|
const senderUserId = extractAndUpsertUser(event);
|
||||||
if (senderUserId) {
|
if (senderUserId) {
|
||||||
addMember({
|
addMember({
|
||||||
user_id: senderUserId,
|
user_id: senderUserId,
|
||||||
agent_group_id: row.agent_group_id,
|
agent_group_id: targetAgentGroupId,
|
||||||
added_by: approverId,
|
added_by: approverId,
|
||||||
added_at: new Date().toISOString(),
|
added_at: new Date().toISOString(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear the pending row BEFORE replay so the gate check on the second
|
|
||||||
// attempt sees a wired channel (agentCount > 0) and takes the fan-out
|
|
||||||
// path normally.
|
|
||||||
deletePendingChannelApproval(row.messaging_group_id);
|
deletePendingChannelApproval(row.messaging_group_id);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -391,3 +501,117 @@ async function handleChannelApprovalResponse(payload: ResponsePayload): Promise<
|
|||||||
}
|
}
|
||||||
|
|
||||||
registerResponseHandler(handleChannelApprovalResponse);
|
registerResponseHandler(handleChannelApprovalResponse);
|
||||||
|
|
||||||
|
// ── Free-text name interceptor ──
|
||||||
|
// Captures the next DM from an approver who clicked "Create new agent",
|
||||||
|
// creates the agent immediately, wires the channel, and replays.
|
||||||
|
|
||||||
|
setMessageInterceptor(async (event: InboundEvent): Promise<boolean> => {
|
||||||
|
const userId = extractAndUpsertUser(event);
|
||||||
|
if (!userId) return false;
|
||||||
|
|
||||||
|
const pending = awaitingNameInput.get(userId);
|
||||||
|
if (!pending) return false;
|
||||||
|
if (event.channelType !== pending.dmChannelType || event.platformId !== pending.dmPlatformId) return false;
|
||||||
|
|
||||||
|
awaitingNameInput.delete(userId);
|
||||||
|
|
||||||
|
let text: string | undefined;
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(event.message.content) as Record<string, unknown>;
|
||||||
|
text = (typeof parsed.text === 'string' ? parsed.text : undefined)?.trim();
|
||||||
|
} catch {
|
||||||
|
/* fall through */
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!text) {
|
||||||
|
log.warn('Channel registration: empty name reply, ignoring', { userId });
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const row = getPendingChannelApproval(pending.channelMgId);
|
||||||
|
if (!row) return true;
|
||||||
|
|
||||||
|
const ag = createNewAgentGroup(text);
|
||||||
|
log.info('Channel registration: new agent group created', {
|
||||||
|
messagingGroupId: row.messaging_group_id,
|
||||||
|
agentGroupId: ag.id,
|
||||||
|
agentName: ag.name,
|
||||||
|
folder: ag.folder,
|
||||||
|
});
|
||||||
|
|
||||||
|
let originalEvent: InboundEvent;
|
||||||
|
try {
|
||||||
|
originalEvent = JSON.parse(row.original_message) as InboundEvent;
|
||||||
|
} catch (err) {
|
||||||
|
log.error('Channel registration: failed to parse stored event', {
|
||||||
|
messagingGroupId: row.messaging_group_id,
|
||||||
|
err,
|
||||||
|
});
|
||||||
|
deletePendingChannelApproval(row.messaging_group_id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const isGroup = originalEvent.threadId !== null;
|
||||||
|
const engageMode: MessagingGroupAgent['engage_mode'] = isGroup ? 'mention-sticky' : 'pattern';
|
||||||
|
const engagePattern = isGroup ? null : '.';
|
||||||
|
|
||||||
|
const mgaId = `mga-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
||||||
|
createMessagingGroupAgent({
|
||||||
|
id: mgaId,
|
||||||
|
messaging_group_id: row.messaging_group_id,
|
||||||
|
agent_group_id: ag.id,
|
||||||
|
engage_mode: engageMode,
|
||||||
|
engage_pattern: engagePattern,
|
||||||
|
sender_scope: 'known',
|
||||||
|
ignored_message_policy: 'accumulate',
|
||||||
|
session_mode: 'shared',
|
||||||
|
priority: 0,
|
||||||
|
created_at: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
log.info('Channel registration approved — wiring created', {
|
||||||
|
messagingGroupId: row.messaging_group_id,
|
||||||
|
agentGroupId: ag.id,
|
||||||
|
mgaId,
|
||||||
|
engageMode,
|
||||||
|
approverId: userId,
|
||||||
|
});
|
||||||
|
|
||||||
|
const senderUserId = extractAndUpsertUser(originalEvent);
|
||||||
|
if (senderUserId) {
|
||||||
|
addMember({
|
||||||
|
user_id: senderUserId,
|
||||||
|
agent_group_id: ag.id,
|
||||||
|
added_by: userId,
|
||||||
|
added_at: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
deletePendingChannelApproval(row.messaging_group_id);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await routeInbound(originalEvent);
|
||||||
|
} catch (err) {
|
||||||
|
log.error('Failed to replay message after channel approval', {
|
||||||
|
messagingGroupId: row.messaging_group_id,
|
||||||
|
err,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const adapter = getDeliveryAdapter();
|
||||||
|
if (adapter) {
|
||||||
|
const dm = await ensureUserDm(row.approver_user_id);
|
||||||
|
if (dm) {
|
||||||
|
adapter
|
||||||
|
.deliver(
|
||||||
|
dm.channel_type,
|
||||||
|
dm.platform_id,
|
||||||
|
null,
|
||||||
|
'chat-sdk',
|
||||||
|
JSON.stringify({ text: `✅ Agent "${ag.name}" created and connected.` }),
|
||||||
|
)
|
||||||
|
.catch(() => {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ import {
|
|||||||
getMessagingGroupWithAgentCount,
|
getMessagingGroupWithAgentCount,
|
||||||
} from './db/messaging-groups.js';
|
} from './db/messaging-groups.js';
|
||||||
import { findSessionForAgent } from './db/sessions.js';
|
import { findSessionForAgent } from './db/sessions.js';
|
||||||
import { startTypingRefresh } from './modules/typing/index.js';
|
import { startTypingRefresh, stopTypingRefresh } from './modules/typing/index.js';
|
||||||
import { log } from './log.js';
|
import { log } from './log.js';
|
||||||
import { resolveSession, writeSessionMessage, writeOutboundDirect } from './session-manager.js';
|
import { resolveSession, writeSessionMessage, writeOutboundDirect } from './session-manager.js';
|
||||||
import { wakeContainer } from './container-runner.js';
|
import { wakeContainer } from './container-runner.js';
|
||||||
@@ -108,6 +108,20 @@ export function setSenderScopeGate(fn: SenderScopeGateFn): void {
|
|||||||
senderScopeGate = fn;
|
senderScopeGate = fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Message-interceptor hook. Runs at the very top of routeInbound, before
|
||||||
|
* messaging-group resolution. When the interceptor returns true the message
|
||||||
|
* is consumed and routing stops. Used by the permissions module to capture
|
||||||
|
* free-text replies during multi-step approval flows (e.g. agent naming).
|
||||||
|
*/
|
||||||
|
export type MessageInterceptorFn = (event: InboundEvent) => Promise<boolean>;
|
||||||
|
|
||||||
|
let messageInterceptor: MessageInterceptorFn | null = null;
|
||||||
|
|
||||||
|
export function setMessageInterceptor(fn: MessageInterceptorFn): void {
|
||||||
|
messageInterceptor = fn;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Channel-registration hook. Runs when the router sees a mention/DM on a
|
* Channel-registration hook. Runs when the router sees a mention/DM on a
|
||||||
* messaging group that has no wirings AND hasn't been denied. The hook is
|
* messaging group that has no wirings AND hasn't been denied. The hook is
|
||||||
@@ -142,6 +156,10 @@ function safeParseContent(raw: string): { text?: string; sender?: string; sender
|
|||||||
* Creates messaging group + session if they don't exist yet.
|
* Creates messaging group + session if they don't exist yet.
|
||||||
*/
|
*/
|
||||||
export async function routeInbound(event: InboundEvent): Promise<void> {
|
export async function routeInbound(event: InboundEvent): Promise<void> {
|
||||||
|
// Pre-route interceptor — lets modules consume messages before any routing
|
||||||
|
// (e.g. free-text replies during multi-step approval flows).
|
||||||
|
if (messageInterceptor && (await messageInterceptor(event))) return;
|
||||||
|
|
||||||
// 0. Apply the adapter's thread policy. Non-threaded adapters (Telegram,
|
// 0. Apply the adapter's thread policy. Non-threaded adapters (Telegram,
|
||||||
// WhatsApp, iMessage, email) collapse threads to the channel.
|
// WhatsApp, iMessage, email) collapse threads to the channel.
|
||||||
const adapter = getChannelAdapter(event.channelType);
|
const adapter = getChannelAdapter(event.channelType);
|
||||||
@@ -457,7 +475,11 @@ async function deliverToAgent(
|
|||||||
startTypingRefresh(session.id, session.agent_group_id, event.channelType, event.platformId, event.threadId);
|
startTypingRefresh(session.id, session.agent_group_id, event.channelType, event.platformId, event.threadId);
|
||||||
const freshSession = getSession(session.id);
|
const freshSession = getSession(session.id);
|
||||||
if (freshSession) {
|
if (freshSession) {
|
||||||
await wakeContainer(freshSession);
|
const woke = await wakeContainer(freshSession);
|
||||||
|
// wakeContainer never throws — it returns false on transient spawn
|
||||||
|
// failure (host-sweep retries). Stop the typing indicator we just
|
||||||
|
// started so it doesn't leak; the inbound row stays pending.
|
||||||
|
if (!woke) stopTypingRefresh(freshSession.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import type Database from 'better-sqlite3';
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
|
import { deriveAttachmentName } from './attachment-naming.js';
|
||||||
import { isSafeAttachmentName } from './attachment-safety.js';
|
import { isSafeAttachmentName } from './attachment-safety.js';
|
||||||
import type { OutboundFile } from './channels/adapter.js';
|
import type { OutboundFile } from './channels/adapter.js';
|
||||||
import { DATA_DIR } from './config.js';
|
import { DATA_DIR } from './config.js';
|
||||||
@@ -259,7 +260,7 @@ function extractAttachmentFiles(
|
|||||||
// this guard, `path.join(inboxDir, '../../...')` writes anywhere the
|
// this guard, `path.join(inboxDir, '../../...')` writes anywhere the
|
||||||
// host process has fs permission — see Signal Desktop's Nov 2025
|
// host process has fs permission — see Signal Desktop's Nov 2025
|
||||||
// attachment-fileName advisory for the same archetype.
|
// attachment-fileName advisory for the same archetype.
|
||||||
const rawName = (att.name as string | undefined) ?? `attachment-${Date.now()}`;
|
const rawName = deriveAttachmentName(att);
|
||||||
const filename = isSafeAttachmentName(rawName) ? rawName : `attachment-${Date.now()}`;
|
const filename = isSafeAttachmentName(rawName) ? rawName : `attachment-${Date.now()}`;
|
||||||
if (filename !== rawName) {
|
if (filename !== rawName) {
|
||||||
log.warn('Refused unsafe attachment filename — would escape inbox', {
|
log.warn('Refused unsafe attachment filename — would escape inbox', {
|
||||||
@@ -372,6 +373,11 @@ export function readOutboxFiles(
|
|||||||
if (!fs.existsSync(outboxDir)) return undefined;
|
if (!fs.existsSync(outboxDir)) return undefined;
|
||||||
const files: OutboundFile[] = [];
|
const files: OutboundFile[] = [];
|
||||||
for (const filename of filenames) {
|
for (const filename of filenames) {
|
||||||
|
// Reject any name that isn't a bare basename before touching the filesystem.
|
||||||
|
if (!isSafeAttachmentName(filename)) {
|
||||||
|
log.warn('Refused unsafe outbox filename — would escape outbox', { messageId, filename });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
const filePath = path.join(outboxDir, filename);
|
const filePath = path.join(outboxDir, filename);
|
||||||
if (fs.existsSync(filePath)) {
|
if (fs.existsSync(filePath)) {
|
||||||
files.push({ filename, data: fs.readFileSync(filePath) });
|
files.push({ filename, data: fs.readFileSync(filePath) });
|
||||||
|
|||||||
Reference in New Issue
Block a user