Compare commits
134 Commits
claude/pha
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 62a4414968 | |||
| f2841bdd74 | |||
| 95d786ef2c | |||
|
|
f11be8735c | ||
|
|
679b90a667 | ||
|
|
ea7892baa3 | ||
|
|
467d9b0dc4 | ||
|
|
5168518759 | ||
|
|
8a98bfe79f | ||
|
|
de95cf1b91 | ||
|
|
9a753e9adb | ||
|
|
95061c648f | ||
|
|
78827e0a5b | ||
|
|
fe80edbf52 | ||
|
|
996af168ef | ||
|
|
35b7291660 | ||
|
|
75423579b7 | ||
|
|
ac0384e49c | ||
|
|
000745592a | ||
|
|
d1dd8ffcd9 | ||
|
|
263e3a31f8 | ||
|
|
bb7cf3f55e | ||
|
|
66dbf0046e | ||
|
|
ac9fcce0fb | ||
|
|
f820c75632 | ||
|
|
be657f6e3c | ||
|
|
f6053dd2a8 | ||
|
|
1611fe5fa2 | ||
| cd770b9a1d | |||
|
|
a27cdf733c | ||
|
|
a0bdf6ddc8 | ||
|
|
9bc3187a4a | ||
|
|
7452b8c6d2 | ||
|
|
f136451e22 | ||
|
|
d962e5e7f4 | ||
|
|
a36b45b791 | ||
|
|
ff7e95a081 | ||
|
|
0b3ce11df8 | ||
|
|
ca869561ac | ||
|
|
2073b50c94 | ||
|
|
20421e6d58 | ||
|
|
557f8486cd | ||
|
|
f14b112cdd | ||
|
|
22fa2e1606 | ||
|
|
eb777d63ee | ||
|
|
d6835fe0b0 | ||
|
|
42eb420545 | ||
|
|
f3b47cb7cd | ||
|
|
4cd9468b6f | ||
|
|
4585d3b0ec | ||
|
|
3469165c92 | ||
|
|
797906dde2 | ||
|
|
81c4bfd7e3 | ||
|
|
a746783c48 | ||
|
|
634b3f823b | ||
|
|
6ace14c3c8 | ||
|
|
1c8d13a837 | ||
|
|
5cf9d679b4 | ||
|
|
dd177f7ef5 | ||
|
|
83d2106633 | ||
|
|
f720683496 | ||
|
|
4b814c9a17 | ||
|
|
45d3f1ae11 | ||
|
|
8ae3764b05 | ||
|
|
6c0fba8cc1 | ||
|
|
b1b76a846e | ||
|
|
f07d17e927 | ||
|
|
81468063ff | ||
|
|
b8781a0081 | ||
|
|
a2c7182ed6 | ||
|
|
e4b995ec14 | ||
|
|
97787a0272 | ||
|
|
0e1036b2ff | ||
|
|
ba89574c83 | ||
|
|
f8b4ce4b6d | ||
|
|
50e56a2658 | ||
|
|
629fcf6b41 | ||
|
|
28cdb33e57 | ||
|
|
58823d7187 | ||
|
|
5a406b16c3 | ||
|
|
de26578778 | ||
|
|
f8a5ff8e43 | ||
|
|
c50f003744 | ||
|
|
eeec01cd0e | ||
|
|
6fc734b0ae | ||
|
|
a55ed44394 | ||
|
|
b78eff1f7b | ||
|
|
9062717eee | ||
|
|
eb9c8847a4 | ||
|
|
459d7c99e4 | ||
|
|
b2fb517286 | ||
|
|
0122fef89e | ||
|
|
3b2c46dd36 | ||
|
|
3291a32d37 | ||
|
|
23d70971f8 | ||
|
|
e55207f5d2 | ||
|
|
568d37c48c | ||
|
|
a6e611736b | ||
|
|
4ab0d25224 | ||
|
|
c98eb6e843 | ||
|
|
738811dbe7 | ||
|
|
f3e438444a | ||
|
|
044669edfc | ||
|
|
404a1d1782 | ||
|
|
3a5ab6ef44 | ||
|
|
9930d295f2 | ||
|
|
354c59f9e3 | ||
|
|
d7e4cb91d6 | ||
|
|
478a92f1a5 | ||
|
|
b3f20967db | ||
|
|
2f8d7175f6 | ||
|
|
f3fb07e41f | ||
|
|
ca57ebda85 | ||
|
|
54658ee689 | ||
|
|
c69f00e891 | ||
|
|
460eede226 | ||
|
|
63578bb645 | ||
|
|
6fd136e944 | ||
|
|
55df4a6439 | ||
|
|
5f3aa8f3fe | ||
|
|
fb5165bb26 | ||
|
|
7db3a56fe8 | ||
|
|
47988602e2 | ||
|
|
9f4991abe0 | ||
|
|
dbf37f86a8 | ||
|
|
d6dd2bef5e | ||
|
|
ad054a7586 | ||
|
|
f7b0cfb341 | ||
|
|
68b1e63c46 | ||
|
|
e928600c98 | ||
|
|
f395ed9a23 | ||
|
|
dda03bff9e | ||
|
|
77559a6284 | ||
|
|
6135bd8d63 |
18
.env.example
18
.env.example
@ -25,9 +25,9 @@ PUBLIC_POSTHOG_KEY=
|
|||||||
PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
|
PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
|
||||||
SIGNUPS_ENABLED=true
|
SIGNUPS_ENABLED=true
|
||||||
SIGNUP_ALLOWLIST=
|
SIGNUP_ALLOWLIST=
|
||||||
# First-run discovery. Leave unset and the onboarding scan never touches the host filesystem.
|
# First-run discovery. Leave unset and onboarding never looks at the host filesystem.
|
||||||
# Set a home-style directory (its Projects, Documents, src, ... subfolders are scanned) to let the
|
# Set a folder (for example a home folder) and the deployment owner's onboarding offers to list the
|
||||||
# deployment owner's own onboarding discover local projects. Other accounts never scan the host.
|
# git projects in it, reading file and folder names only. Other accounts never see the step.
|
||||||
# ONBOARDING_SCAN_HOME=
|
# ONBOARDING_SCAN_HOME=
|
||||||
ENCRYPTION_KEY=replace-with-64-random-hex-characters
|
ENCRYPTION_KEY=replace-with-64-random-hex-characters
|
||||||
DATA_DIR=./data
|
DATA_DIR=./data
|
||||||
@ -99,8 +99,12 @@ PI_DEFAULT_PROVIDER=openrouter
|
|||||||
# Computer-use tests can override this with COMPUTER_E2E_MODEL.
|
# Computer-use tests can override this with COMPUTER_E2E_MODEL.
|
||||||
PI_DEFAULT_MODEL=openai/gpt-5.6-luna
|
PI_DEFAULT_MODEL=openai/gpt-5.6-luna
|
||||||
# Optional local OpenAI-compatible models. Leave blank to disable the provider.
|
# Optional local OpenAI-compatible models. Leave blank to disable the provider.
|
||||||
|
# With PI_DEFAULT_PROVIDER=local they are the deployment's model and nobody has to connect one.
|
||||||
|
# An entry may name its own server: id=http://host:port/v1.
|
||||||
RAKAZO_LOCAL_MODELS=
|
RAKAZO_LOCAL_MODELS=
|
||||||
RAKAZO_LOCAL_MODELS_URL=http://127.0.0.1:11434/v1
|
RAKAZO_LOCAL_MODELS_URL=http://127.0.0.1:11434/v1
|
||||||
|
# Optional tiers the agent can delegate to, for every space: fast=id,standard=id,code=id.
|
||||||
|
RAKAZO_LOCAL_TIERS=
|
||||||
RAKAZO_LOCAL_CONTEXT_WINDOW=32768
|
RAKAZO_LOCAL_CONTEXT_WINDOW=32768
|
||||||
RAKAZO_LOCAL_MAX_TOKENS=4096
|
RAKAZO_LOCAL_MAX_TOKENS=4096
|
||||||
# Allow user-connected OpenAI-compatible endpoints on public hostnames (default: private/loopback only).
|
# Allow user-connected OpenAI-compatible endpoints on public hostnames (default: private/loopback only).
|
||||||
@ -166,8 +170,12 @@ SMTP_URL=
|
|||||||
EMAIL_FROM=
|
EMAIL_FROM=
|
||||||
# Local development only: capture password-reset email and print its link to the API console.
|
# Local development only: capture password-reset email and print its link to the API console.
|
||||||
EMAIL_EMULATOR=
|
EMAIL_EMULATOR=
|
||||||
|
# Optional web push keys (set both). Unset: generated once and stored encrypted in the database.
|
||||||
|
# Browsers only allow push on https (or localhost).
|
||||||
VAPID_PUBLIC_KEY=
|
VAPID_PUBLIC_KEY=
|
||||||
VAPID_PRIVATE_KEY=
|
VAPID_PRIVATE_KEY=
|
||||||
|
# Contact for push services: mailto: or https: URL. Defaults to WEB_ORIGIN when it is https.
|
||||||
|
VAPID_SUBJECT=
|
||||||
# Structured logs to stdout (json in production, pretty in development unless overridden).
|
# Structured logs to stdout (json in production, pretty in development unless overridden).
|
||||||
LOG_LEVEL=info
|
LOG_LEVEL=info
|
||||||
LOG_FORMAT=
|
LOG_FORMAT=
|
||||||
@ -198,10 +206,10 @@ RAKAZO_COMPOSE_FILE=
|
|||||||
# RAKAZO_UPDATE_SERVICES=supervisor
|
# RAKAZO_UPDATE_SERVICES=supervisor
|
||||||
RAKAZO_UPDATE_SERVICES=
|
RAKAZO_UPDATE_SERVICES=
|
||||||
# Published images (defaults match this repository). Tag `local` is built from the checkout.
|
# Published images (defaults match this repository). Tag `local` is built from the checkout.
|
||||||
RAKAZO_IMAGE=ghcr.io/elie222/rakazo/app
|
RAKAZO_IMAGE=ghcr.io/cynarai/evoo-local/app
|
||||||
RAKAZO_IMAGE_TAG=local
|
RAKAZO_IMAGE_TAG=local
|
||||||
RAKAZO_IMAGE_TAG_PREVIOUS=
|
RAKAZO_IMAGE_TAG_PREVIOUS=
|
||||||
RAKAZO_UPDATER_IMAGE=ghcr.io/elie222/rakazo/updater
|
RAKAZO_UPDATER_IMAGE=ghcr.io/cynarai/evoo-local/updater
|
||||||
RAKAZO_UPDATER_IMAGE_TAG=local
|
RAKAZO_UPDATER_IMAGE_TAG=local
|
||||||
# Required only when starting the opt-in `updater` Compose profile; the sidecar refuses to start
|
# Required only when starting the opt-in `updater` Compose profile; the sidecar refuses to start
|
||||||
# without it. Use a dedicated random value, not BETTER_AUTH_SECRET,
|
# without it. Use a dedicated random value, not BETTER_AUTH_SECRET,
|
||||||
|
|||||||
6
.github/ISSUE_TEMPLATE/bug.yml
vendored
6
.github/ISSUE_TEMPLATE/bug.yml
vendored
@ -1,5 +1,5 @@
|
|||||||
name: Bug report
|
name: Bug report
|
||||||
description: Something is broken in Rakazo
|
description: Something is broken in evOo
|
||||||
title: "[Bug]: "
|
title: "[Bug]: "
|
||||||
labels:
|
labels:
|
||||||
- bug
|
- bug
|
||||||
@ -7,7 +7,7 @@ body:
|
|||||||
- type: markdown
|
- type: markdown
|
||||||
attributes:
|
attributes:
|
||||||
value: |
|
value: |
|
||||||
For **security vulnerabilities**, email [security@rakazo.com](mailto:security@rakazo.com) — do not use this form. See [SECURITY.md](https://github.com/elie222/rakazo/blob/main/SECURITY.md).
|
For **security vulnerabilities**, open a [private security advisory](https://github.com/cynarAI/evOo-local/security/advisories/new) do not use this form. See [SECURITY.md](https://github.com/cynarAI/evOo-local/blob/main/SECURITY.md).
|
||||||
|
|
||||||
- type: checkboxes
|
- type: checkboxes
|
||||||
id: not-security
|
id: not-security
|
||||||
@ -55,7 +55,7 @@ body:
|
|||||||
- type: input
|
- type: input
|
||||||
id: version
|
id: version
|
||||||
attributes:
|
attributes:
|
||||||
label: Rakazo version or commit
|
label: evOo version or commit
|
||||||
placeholder: v0.1.0 or abc1234
|
placeholder: v0.1.0 or abc1234
|
||||||
validations:
|
validations:
|
||||||
required: true
|
required: true
|
||||||
|
|||||||
10
.github/ISSUE_TEMPLATE/config.yml
vendored
10
.github/ISSUE_TEMPLATE/config.yml
vendored
@ -1,11 +1,5 @@
|
|||||||
blank_issues_enabled: false
|
blank_issues_enabled: false
|
||||||
contact_links:
|
contact_links:
|
||||||
- name: Security vulnerabilities
|
- name: Security vulnerabilities
|
||||||
url: https://github.com/elie222/rakazo/blob/main/SECURITY.md
|
url: https://github.com/cynarAI/evOo-local/security/advisories/new
|
||||||
about: Report security issues to security@rakazo.com — do not open a public issue.
|
about: Report security issues privately. Do not open a public issue.
|
||||||
- name: User support
|
|
||||||
url: mailto:support@rakazo.com
|
|
||||||
about: Questions about using Rakazo or self-hosting help.
|
|
||||||
- name: Maintainer
|
|
||||||
url: mailto:elie@rakazo.com
|
|
||||||
about: Reach the maintainer directly.
|
|
||||||
|
|||||||
8
.github/ISSUE_TEMPLATE/self-host.yml
vendored
8
.github/ISSUE_TEMPLATE/self-host.yml
vendored
@ -1,5 +1,5 @@
|
|||||||
name: Self-host setup
|
name: Self-host setup
|
||||||
description: Cannot start Rakazo, run compose, migrate, or build the sandbox
|
description: Cannot start evOo, run compose, migrate, or build the sandbox
|
||||||
title: "[Self-host]: "
|
title: "[Self-host]: "
|
||||||
labels:
|
labels:
|
||||||
- self-host
|
- self-host
|
||||||
@ -7,9 +7,9 @@ body:
|
|||||||
- type: markdown
|
- type: markdown
|
||||||
attributes:
|
attributes:
|
||||||
value: |
|
value: |
|
||||||
For **security vulnerabilities**, email [security@rakazo.com](mailto:security@rakazo.com). See [SECURITY.md](https://github.com/elie222/rakazo/blob/main/SECURITY.md).
|
For **security vulnerabilities**, open a [private security advisory](https://github.com/cynarAI/evOo-local/security/advisories/new). See [SECURITY.md](https://github.com/cynarAI/evOo-local/blob/main/SECURITY.md).
|
||||||
|
|
||||||
Setup steps are in [README.md](https://github.com/elie222/rakazo/blob/main/README.md). Do not paste `.env` contents or API keys.
|
Setup steps are in [README.md](https://github.com/cynarAI/evOo-local/blob/main/README.md). Do not paste `.env` contents or API keys.
|
||||||
|
|
||||||
- type: dropdown
|
- type: dropdown
|
||||||
id: step
|
id: step
|
||||||
@ -46,7 +46,7 @@ body:
|
|||||||
- type: input
|
- type: input
|
||||||
id: version
|
id: version
|
||||||
attributes:
|
attributes:
|
||||||
label: Rakazo version or commit
|
label: evOo version or commit
|
||||||
placeholder: main @ abc1234
|
placeholder: main @ abc1234
|
||||||
validations:
|
validations:
|
||||||
required: true
|
required: true
|
||||||
|
|||||||
2
.github/MERGE_GATES.md
vendored
2
.github/MERGE_GATES.md
vendored
@ -24,7 +24,7 @@ These workflows may fail without blocking product delivery. Do **not** add them
|
|||||||
| Check / workflow | Why infra |
|
| Check / workflow | Why infra |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| `macOS setup screenshot (infra)` (`desktop-macos-screenshot.yml`) | Native screenshot capture; skips when the runner has no display |
|
| `macOS setup screenshot (infra)` (`desktop-macos-screenshot.yml`) | Native screenshot capture; skips when the runner has no display |
|
||||||
| Playwright / desktop artifact uploads | Soft-fail (`continue-on-error`) when GitHub artifact quota is exhausted |
|
| Playwright report handoff | Soft-fail (`continue-on-error`); the report stays on the evo runner host for `publish Playwright report` |
|
||||||
| `publish-server-image.yml` (PR paths) | Docker image validation for infra changes |
|
| `publish-server-image.yml` (PR paths) | Docker image validation for infra changes |
|
||||||
| `mobile Android screenshots` | Manual `workflow_dispatch` only |
|
| `mobile Android screenshots` | Manual `workflow_dispatch` only |
|
||||||
| `nightly verification` | Scheduled visual regression |
|
| `nightly verification` | Scheduled visual regression |
|
||||||
|
|||||||
5
.github/workflows/autopilot-auto-merge.yml
vendored
5
.github/workflows/autopilot-auto-merge.yml
vendored
@ -71,7 +71,10 @@ jobs:
|
|||||||
HEAD_SHA: ${{ steps.pr.outputs.head_sha }}
|
HEAD_SHA: ${{ steps.pr.outputs.head_sha }}
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
mapfile -t conclusions < <(
|
conclusions=()
|
||||||
|
while IFS= read -r conclusion; do
|
||||||
|
[[ -n "$conclusion" ]] && conclusions+=("$conclusion")
|
||||||
|
done < <(
|
||||||
gh api "repos/${GITHUB_REPOSITORY}/commits/${HEAD_SHA}/check-runs" --paginate \
|
gh api "repos/${GITHUB_REPOSITORY}/commits/${HEAD_SHA}/check-runs" --paginate \
|
||||||
--jq '.check_runs[] | select(.name == "Product CI") | .conclusion'
|
--jq '.check_runs[] | select(.name == "Product CI") | .conclusion'
|
||||||
)
|
)
|
||||||
|
|||||||
171
.github/workflows/ci.yml
vendored
171
.github/workflows/ci.yml
vendored
@ -5,19 +5,87 @@ name: ci
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [main]
|
branches: [main]
|
||||||
|
paths:
|
||||||
|
- "apps/**"
|
||||||
|
- "packages/**"
|
||||||
|
- "infra/**"
|
||||||
|
- "scripts/**"
|
||||||
|
- "ops/**"
|
||||||
|
- ".github/**"
|
||||||
|
- "package.json"
|
||||||
|
- "pnpm-lock.yaml"
|
||||||
|
- "pnpm-workspace.yaml"
|
||||||
|
- "turbo.json"
|
||||||
|
- "biome.json"
|
||||||
|
- "vitest.config.ts"
|
||||||
|
- "tsconfig*.json"
|
||||||
pull_request:
|
pull_request:
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
pull-requests: read
|
||||||
|
|
||||||
concurrency:
|
concurrency:
|
||||||
group: ci-${{ github.event.pull_request.number || github.ref }}
|
group: ci-${{ github.event.pull_request.number || github.ref }}
|
||||||
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
||||||
|
|
||||||
|
# Jobs that only need Node/pnpm/bash use [self-hosted, evoo-ci] so macOS runners (air, mini)
|
||||||
|
# can share load. Linux-only work keeps the Linux label (Docker, xvfb, local Postgres :5433,
|
||||||
|
# Homebrew ffmpeg with libass/subtitles filter for media unit tests).
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
changes:
|
||||||
|
name: Detect product changes
|
||||||
|
runs-on: [self-hosted, evoo-ci]
|
||||||
|
timeout-minutes: 10
|
||||||
|
outputs:
|
||||||
|
product: ${{ steps.product.outputs.product }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v5
|
||||||
|
with:
|
||||||
|
persist-credentials: false
|
||||||
|
fetch-depth: 0
|
||||||
|
- uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
|
||||||
|
id: filter
|
||||||
|
continue-on-error: true
|
||||||
|
with:
|
||||||
|
filters: |
|
||||||
|
product:
|
||||||
|
- 'apps/**'
|
||||||
|
- 'packages/**'
|
||||||
|
- 'infra/**'
|
||||||
|
- 'scripts/**'
|
||||||
|
- 'ops/**'
|
||||||
|
- '.github/**'
|
||||||
|
- 'package.json'
|
||||||
|
- 'pnpm-lock.yaml'
|
||||||
|
- 'pnpm-workspace.yaml'
|
||||||
|
- 'turbo.json'
|
||||||
|
- 'biome.json'
|
||||||
|
- 'vitest.config.ts'
|
||||||
|
- 'tsconfig*.json'
|
||||||
|
- name: Product path decision
|
||||||
|
id: product
|
||||||
|
if: always()
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
if [[ "${{ steps.filter.outcome }}" == "success" ]]; then
|
||||||
|
if [[ "${{ steps.filter.outputs.product }}" == "true" ]]; then
|
||||||
|
echo "product=true" >> "$GITHUB_OUTPUT"
|
||||||
|
else
|
||||||
|
echo "product=false" >> "$GITHUB_OUTPUT"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "Change detection failed; running full Product CI." >&2
|
||||||
|
echo "product=true" >> "$GITHUB_OUTPUT"
|
||||||
|
fi
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
name: Lint
|
name: Lint
|
||||||
runs-on: [self-hosted, evoo-ci, Linux]
|
needs: changes
|
||||||
|
if: ${{ (needs.changes.result == 'success' && needs.changes.outputs.product == 'true') || needs.changes.result != 'success' }}
|
||||||
|
runs-on: [self-hosted, evoo-ci]
|
||||||
|
timeout-minutes: 20
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v5
|
||||||
with:
|
with:
|
||||||
@ -45,13 +113,15 @@ jobs:
|
|||||||
- uses: actions/setup-node@v4
|
- uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 22
|
node-version: 22
|
||||||
cache: pnpm
|
|
||||||
- run: pnpm install --frozen-lockfile
|
- run: pnpm install --frozen-lockfile
|
||||||
- run: pnpm lint
|
- run: pnpm lint
|
||||||
|
|
||||||
check:
|
check:
|
||||||
name: Typecheck
|
name: Typecheck
|
||||||
runs-on: [self-hosted, evoo-ci, Linux]
|
needs: changes
|
||||||
|
if: ${{ (needs.changes.result == 'success' && needs.changes.outputs.product == 'true') || needs.changes.result != 'success' }}
|
||||||
|
runs-on: [self-hosted, evoo-ci]
|
||||||
|
timeout-minutes: 20
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v5
|
||||||
with:
|
with:
|
||||||
@ -79,7 +149,6 @@ jobs:
|
|||||||
- uses: actions/setup-node@v4
|
- uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 22
|
node-version: 22
|
||||||
cache: pnpm
|
|
||||||
- run: pnpm install --frozen-lockfile
|
- run: pnpm install --frozen-lockfile
|
||||||
- run: pnpm db:generate
|
- run: pnpm db:generate
|
||||||
env:
|
env:
|
||||||
@ -88,6 +157,8 @@ jobs:
|
|||||||
|
|
||||||
build:
|
build:
|
||||||
name: Production builds
|
name: Production builds
|
||||||
|
needs: changes
|
||||||
|
if: ${{ (needs.changes.result == 'success' && needs.changes.outputs.product == 'true') || needs.changes.result != 'success' }}
|
||||||
runs-on: [self-hosted, evoo-ci, Linux]
|
runs-on: [self-hosted, evoo-ci, Linux]
|
||||||
timeout-minutes: 10
|
timeout-minutes: 10
|
||||||
steps:
|
steps:
|
||||||
@ -117,7 +188,6 @@ jobs:
|
|||||||
- uses: actions/setup-node@v4
|
- uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 22
|
node-version: 22
|
||||||
cache: pnpm
|
|
||||||
- run: pnpm install --frozen-lockfile
|
- run: pnpm install --frozen-lockfile
|
||||||
- run: pnpm build
|
- run: pnpm build
|
||||||
env:
|
env:
|
||||||
@ -126,18 +196,13 @@ jobs:
|
|||||||
ENCRYPTION_KEY: ci-build-encryption-key-at-least-32-characters
|
ENCRYPTION_KEY: ci-build-encryption-key-at-least-32-characters
|
||||||
- name: Electron smoke
|
- name: Electron smoke
|
||||||
run: xvfb-run --auto-servernum pnpm --filter @rakazo/desktop exec playwright test --config e2e/playwright.config.ts
|
run: xvfb-run --auto-servernum pnpm --filter @rakazo/desktop exec playwright test --config e2e/playwright.config.ts
|
||||||
- name: Upload desktop setup screenshots
|
|
||||||
if: always()
|
|
||||||
continue-on-error: true
|
|
||||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
|
||||||
with:
|
|
||||||
name: desktop-e2e-screenshots
|
|
||||||
path: apps/desktop/e2e/screenshots
|
|
||||||
if-no-files-found: ignore
|
|
||||||
|
|
||||||
test:
|
test:
|
||||||
name: Unit tests
|
name: Unit tests
|
||||||
|
needs: changes
|
||||||
|
if: ${{ (needs.changes.result == 'success' && needs.changes.outputs.product == 'true') || needs.changes.result != 'success' }}
|
||||||
runs-on: [self-hosted, evoo-ci, Linux]
|
runs-on: [self-hosted, evoo-ci, Linux]
|
||||||
|
timeout-minutes: 45
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v5
|
||||||
with:
|
with:
|
||||||
@ -165,7 +230,6 @@ jobs:
|
|||||||
- uses: actions/setup-node@v4
|
- uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 22
|
node-version: 22
|
||||||
cache: pnpm
|
|
||||||
- run: pnpm install --frozen-lockfile
|
- run: pnpm install --frozen-lockfile
|
||||||
- run: pnpm db:generate
|
- run: pnpm db:generate
|
||||||
env:
|
env:
|
||||||
@ -174,6 +238,8 @@ jobs:
|
|||||||
|
|
||||||
test-integration:
|
test-integration:
|
||||||
name: Postgres journeys
|
name: Postgres journeys
|
||||||
|
needs: changes
|
||||||
|
if: ${{ (needs.changes.result == 'success' && needs.changes.outputs.product == 'true') || needs.changes.result != 'success' }}
|
||||||
runs-on: [self-hosted, evoo-ci, Linux]
|
runs-on: [self-hosted, evoo-ci, Linux]
|
||||||
timeout-minutes: 10
|
timeout-minutes: 10
|
||||||
steps:
|
steps:
|
||||||
@ -203,24 +269,28 @@ jobs:
|
|||||||
- uses: actions/setup-node@v4
|
- uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 22
|
node-version: 22
|
||||||
cache: pnpm
|
|
||||||
- run: pnpm install --frozen-lockfile
|
- run: pnpm install --frozen-lockfile
|
||||||
- run: pnpm test:integration
|
- run: pnpm test:integration
|
||||||
|
|
||||||
test-e2e:
|
test-e2e:
|
||||||
name: Web E2E
|
name: Web E2E
|
||||||
|
needs: changes
|
||||||
|
if: ${{ (needs.changes.result == 'success' && needs.changes.outputs.product == 'true') || needs.changes.result != 'success' }}
|
||||||
uses: ./.github/workflows/playwright.yml
|
uses: ./.github/workflows/playwright.yml
|
||||||
with:
|
with:
|
||||||
upload_artifacts: true
|
hand_off_report: true
|
||||||
|
|
||||||
product-gate:
|
product-gate:
|
||||||
name: Product CI
|
name: Product CI
|
||||||
if: always()
|
if: always()
|
||||||
needs: [lint, check, build, test, test-integration, test-e2e]
|
needs: [changes, lint, check, build, test, test-integration, test-e2e]
|
||||||
runs-on: [self-hosted, evoo-ci, Linux]
|
runs-on: [self-hosted, evoo-ci]
|
||||||
|
timeout-minutes: 5
|
||||||
steps:
|
steps:
|
||||||
- name: Verify product checks
|
- name: Verify product checks
|
||||||
env:
|
env:
|
||||||
|
CHANGES: ${{ needs.changes.result }}
|
||||||
|
PRODUCT: ${{ needs.changes.outputs.product }}
|
||||||
LINT: ${{ needs.lint.result }}
|
LINT: ${{ needs.lint.result }}
|
||||||
CHECK: ${{ needs.check.result }}
|
CHECK: ${{ needs.check.result }}
|
||||||
BUILD: ${{ needs.build.result }}
|
BUILD: ${{ needs.build.result }}
|
||||||
@ -229,11 +299,23 @@ jobs:
|
|||||||
TEST_E2E: ${{ needs.test-e2e.result }}
|
TEST_E2E: ${{ needs.test-e2e.result }}
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
product="$PRODUCT"
|
||||||
|
if [[ "$CHANGES" != "success" ]]; then
|
||||||
|
echo "Change detection did not finish cleanly; requiring downstream product checks." >&2
|
||||||
|
product=true
|
||||||
|
fi
|
||||||
|
if [[ "$product" != "true" ]]; then
|
||||||
|
echo "No product-impacting paths changed; Product CI satisfied."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
failed=0
|
failed=0
|
||||||
for name in LINT CHECK BUILD TEST TEST_INTEGRATION TEST_E2E; do
|
for name in LINT CHECK BUILD TEST TEST_INTEGRATION TEST_E2E; do
|
||||||
result="${!name}"
|
result="${!name}"
|
||||||
printf '%s=%s\n' "$name" "$result"
|
printf '%s=%s\n' "$name" "$result"
|
||||||
if [[ "$result" != "success" ]]; then
|
if [[ "$result" == "skipped" ]]; then
|
||||||
|
echo "$name was skipped although product paths changed." >&2
|
||||||
|
failed=1
|
||||||
|
elif [[ "$result" != "success" ]]; then
|
||||||
failed=1
|
failed=1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
@ -245,9 +327,9 @@ jobs:
|
|||||||
|
|
||||||
publish-mobile-update:
|
publish-mobile-update:
|
||||||
name: Publish compatible mobile update
|
name: Publish compatible mobile update
|
||||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main' && needs.changes.outputs.product == 'true'
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
needs: [lint, check, build, test, test-integration, test-e2e]
|
needs: [changes, lint, check, build, test, test-integration, test-e2e]
|
||||||
runs-on: [self-hosted, evoo-ci, Linux]
|
runs-on: [self-hosted, evoo-ci, Linux]
|
||||||
timeout-minutes: 20
|
timeout-minutes: 20
|
||||||
concurrency:
|
concurrency:
|
||||||
@ -388,7 +470,6 @@ jobs:
|
|||||||
if: steps.ota.outputs.publish == 'true' && steps.expo_token.outputs.ready == 'true'
|
if: steps.ota.outputs.publish == 'true' && steps.expo_token.outputs.ready == 'true'
|
||||||
with:
|
with:
|
||||||
node-version: 24
|
node-version: 24
|
||||||
cache: pnpm
|
|
||||||
- name: Set up Expo
|
- name: Set up Expo
|
||||||
if: steps.ota.outputs.publish == 'true' && steps.expo_token.outputs.ready == 'true'
|
if: steps.ota.outputs.publish == 'true' && steps.expo_token.outputs.ready == 'true'
|
||||||
uses: expo/expo-github-action@eab7a230208c952974db8c3245cfd78402c7b385 # v9
|
uses: expo/expo-github-action@eab7a230208c952974db8c3245cfd78402c7b385 # v9
|
||||||
@ -396,6 +477,9 @@ jobs:
|
|||||||
eas-version: latest
|
eas-version: latest
|
||||||
packager: pnpm
|
packager: pnpm
|
||||||
token: ${{ secrets.EXPO_TOKEN }}
|
token: ${{ secrets.EXPO_TOKEN }}
|
||||||
|
# Its default caches go to the GitHub cache service; keep them on the runner.
|
||||||
|
eas-cache: false
|
||||||
|
expo-cache: false
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
if: steps.ota.outputs.publish == 'true' && steps.expo_token.outputs.ready == 'true'
|
if: steps.ota.outputs.publish == 'true' && steps.expo_token.outputs.ready == 'true'
|
||||||
run: pnpm install --frozen-lockfile
|
run: pnpm install --frozen-lockfile
|
||||||
@ -422,46 +506,3 @@ jobs:
|
|||||||
fi
|
fi
|
||||||
eas update --platform all --channel production --environment production --message "$GITHUB_SHA" --non-interactive
|
eas update --platform all --channel production --environment production --message "$GITHUB_SHA" --non-interactive
|
||||||
|
|
||||||
deploy-production:
|
|
||||||
if: >-
|
|
||||||
github.event_name == 'push' &&
|
|
||||||
github.ref == 'refs/heads/main' &&
|
|
||||||
vars.PRODUCTION_DEPLOY_ENABLED == 'true'
|
|
||||||
needs: [lint, check, build, test, test-integration, test-e2e]
|
|
||||||
runs-on: [self-hosted, evoo-ci, Linux]
|
|
||||||
timeout-minutes: 30
|
|
||||||
concurrency:
|
|
||||||
group: production
|
|
||||||
cancel-in-progress: false
|
|
||||||
environment:
|
|
||||||
name: production
|
|
||||||
url: https://app.rakazo.com
|
|
||||||
steps:
|
|
||||||
- name: Configure production SSH identity
|
|
||||||
env:
|
|
||||||
SSH_PRIVATE_KEY: ${{ secrets.PRODUCTION_SSH_PRIVATE_KEY }}
|
|
||||||
SSH_KNOWN_HOSTS: ${{ secrets.PRODUCTION_SSH_KNOWN_HOSTS }}
|
|
||||||
run: |
|
|
||||||
test -n "$SSH_PRIVATE_KEY"
|
|
||||||
test -n "$SSH_KNOWN_HOSTS"
|
|
||||||
install -d -m 700 "$HOME/.ssh"
|
|
||||||
printf '%s\n' "$SSH_PRIVATE_KEY" > "$HOME/.ssh/rakazo-production"
|
|
||||||
printf '%s\n' "$SSH_KNOWN_HOSTS" > "$HOME/.ssh/known_hosts"
|
|
||||||
chmod 600 "$HOME/.ssh/rakazo-production" "$HOME/.ssh/known_hosts"
|
|
||||||
ssh-keygen -y -f "$HOME/.ssh/rakazo-production" >/dev/null
|
|
||||||
|
|
||||||
- name: Deploy successful main revision
|
|
||||||
env:
|
|
||||||
SSH_HOST: ${{ secrets.PRODUCTION_SSH_HOST }}
|
|
||||||
SSH_USER: ${{ secrets.PRODUCTION_SSH_USER }}
|
|
||||||
run: |
|
|
||||||
test -n "$SSH_HOST"
|
|
||||||
test -n "$SSH_USER"
|
|
||||||
ssh -F /dev/null \
|
|
||||||
-i "$HOME/.ssh/rakazo-production" \
|
|
||||||
-o BatchMode=yes \
|
|
||||||
-o ConnectTimeout=15 \
|
|
||||||
-o IdentitiesOnly=yes \
|
|
||||||
-o StrictHostKeyChecking=yes \
|
|
||||||
-o UserKnownHostsFile="$HOME/.ssh/known_hosts" \
|
|
||||||
"$SSH_USER@$SSH_HOST" deploy-main
|
|
||||||
|
|||||||
82
.github/workflows/cleanup-actions-artifacts.yml
vendored
Normal file
82
.github/workflows/cleanup-actions-artifacts.yml
vendored
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
name: cleanup-actions-artifacts
|
||||||
|
|
||||||
|
# Manual reclaim of Actions artifact storage for this repository.
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
dry_run:
|
||||||
|
description: List artifacts only; do not delete
|
||||||
|
type: boolean
|
||||||
|
default: false
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
paths:
|
||||||
|
- .github/workflows/cleanup-actions-artifacts.yml
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
actions: write
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
cleanup:
|
||||||
|
name: Delete stale Actions artifacts
|
||||||
|
runs-on: [self-hosted, evoo-ci, Linux]
|
||||||
|
timeout-minutes: 30
|
||||||
|
steps:
|
||||||
|
- name: Remove old artifacts
|
||||||
|
env:
|
||||||
|
GH_TOKEN: ${{ github.token }}
|
||||||
|
DRY_RUN: ${{ github.event_name == 'workflow_dispatch' && inputs.dry_run || 'false' }}
|
||||||
|
REPOSITORY: ${{ github.repository }}
|
||||||
|
run: |
|
||||||
|
python3 <<'PY'
|
||||||
|
import json, os, subprocess, sys
|
||||||
|
|
||||||
|
repo = os.environ["REPOSITORY"]
|
||||||
|
dry_run = os.environ.get("DRY_RUN", "false").lower() == "true"
|
||||||
|
|
||||||
|
def gh(*args):
|
||||||
|
return subprocess.check_output(["gh", "api", *args], text=True)
|
||||||
|
|
||||||
|
def should_delete(name: str) -> bool:
|
||||||
|
if name.startswith("playwright-artifacts-"):
|
||||||
|
return True
|
||||||
|
if name.startswith("digest-"):
|
||||||
|
return True
|
||||||
|
if name == "desktop-e2e-screenshots":
|
||||||
|
return True
|
||||||
|
if name.endswith(".dockerbuild"):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
artifacts = []
|
||||||
|
page = 1
|
||||||
|
while True:
|
||||||
|
data = json.loads(
|
||||||
|
gh(f"repos/{repo}/actions/artifacts?per_page=100&page={page}")
|
||||||
|
)
|
||||||
|
artifacts.extend(data.get("artifacts", []))
|
||||||
|
if len(data.get("artifacts", [])) < 100:
|
||||||
|
break
|
||||||
|
page += 1
|
||||||
|
|
||||||
|
deleted = 0
|
||||||
|
freed = 0
|
||||||
|
for art in artifacts:
|
||||||
|
name = art["name"]
|
||||||
|
if not should_delete(name):
|
||||||
|
print(f"skip id={art['id']} name={name} size={art['size_in_bytes']}")
|
||||||
|
continue
|
||||||
|
if dry_run:
|
||||||
|
print(f"would delete id={art['id']} name={name} size={art['size_in_bytes']}")
|
||||||
|
continue
|
||||||
|
subprocess.check_call(
|
||||||
|
["gh", "api", "--method", "DELETE", f"repos/{repo}/actions/artifacts/{art['id']}"]
|
||||||
|
)
|
||||||
|
print(f"deleted id={art['id']} name={name} size={art['size_in_bytes']}")
|
||||||
|
deleted += 1
|
||||||
|
freed += art["size_in_bytes"]
|
||||||
|
|
||||||
|
print(f"deleted_count={deleted}")
|
||||||
|
print(f"freed_bytes={freed}")
|
||||||
|
PY
|
||||||
12
.github/workflows/desktop-macos-screenshot.yml
vendored
12
.github/workflows/desktop-macos-screenshot.yml
vendored
@ -50,7 +50,6 @@ jobs:
|
|||||||
if: steps.display.outputs.available == 'true'
|
if: steps.display.outputs.available == 'true'
|
||||||
with:
|
with:
|
||||||
node-version: 22
|
node-version: 22
|
||||||
cache: pnpm
|
|
||||||
- run: pnpm install --frozen-lockfile
|
- run: pnpm install --frozen-lockfile
|
||||||
if: steps.display.outputs.available == 'true'
|
if: steps.display.outputs.available == 'true'
|
||||||
- run: pnpm --filter @rakazo/desktop build
|
- run: pnpm --filter @rakazo/desktop build
|
||||||
@ -77,14 +76,3 @@ jobs:
|
|||||||
pnpm --filter @rakazo/desktop exec playwright test
|
pnpm --filter @rakazo/desktop exec playwright test
|
||||||
--config e2e/playwright.config.ts setup.spec.ts
|
--config e2e/playwright.config.ts setup.spec.ts
|
||||||
--grep "first run asks whether to use a local or existing instance"
|
--grep "first run asks whether to use a local or existing instance"
|
||||||
- name: Upload native macOS setup screenshot
|
|
||||||
if: always() && steps.display.outputs.available == 'true'
|
|
||||||
continue-on-error: true
|
|
||||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
|
||||||
with:
|
|
||||||
name: desktop-e2e-macos-screenshot
|
|
||||||
path: |
|
|
||||||
apps/desktop/e2e/screenshots/08-setup-macos-native-controls.png
|
|
||||||
apps/desktop/e2e/screenshots/09-macos-dock.png
|
|
||||||
if-no-files-found: warn
|
|
||||||
retention-days: 1
|
|
||||||
|
|||||||
55
.github/workflows/mobile-android-screenshots.yml
vendored
55
.github/workflows/mobile-android-screenshots.yml
vendored
@ -39,41 +39,39 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
|
|
||||||
|
# Runner-host caches only: never GitHub's cache or artifact storage.
|
||||||
- name: Restore screenshot APK
|
- name: Restore screenshot APK
|
||||||
id: screenshot-apk
|
id: screenshot-apk
|
||||||
uses: actions/cache/restore@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
|
env:
|
||||||
with:
|
APK_KEY: ${{ hashFiles('pnpm-lock.yaml', 'package.json', 'pnpm-workspace.yaml', 'turbo.json', 'tsconfig*.json', '.github/workflows/mobile-android-screenshots.yml', 'apps/mobile/package.json', 'apps/mobile/app.json', 'apps/mobile/app.config.ts', 'apps/mobile/metro.config.js', 'apps/mobile/metro-resolver.js', 'apps/mobile/assets/**', 'apps/mobile/app/**', 'apps/mobile/components/**', 'apps/mobile/lib/**', 'apps/mobile/modules/**', 'apps/mobile/plugins/**', 'packages/chat-ui/**', 'packages/contracts/**', 'packages/core/**', 'packages/ui-tokens/**') }}
|
||||||
path: apps/mobile/android/app/build/outputs/apk/release/app-release.apk
|
run: |
|
||||||
key: mobile-android-apk-v2-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml', 'package.json', 'pnpm-workspace.yaml', 'turbo.json', 'tsconfig*.json', '.github/workflows/mobile-android-screenshots.yml', 'apps/mobile/package.json', 'apps/mobile/app.json', 'apps/mobile/app.config.ts', 'apps/mobile/metro.config.js', 'apps/mobile/metro-resolver.js', 'apps/mobile/assets/**', 'apps/mobile/app/**', 'apps/mobile/components/**', 'apps/mobile/lib/**', 'apps/mobile/modules/**', 'apps/mobile/plugins/**', 'packages/chat-ui/**', 'packages/contracts/**', 'packages/core/**', 'packages/ui-tokens/**') }}
|
set -euo pipefail
|
||||||
|
cache="$HOME/.cache/evoo-ci/mobile-android-apk"
|
||||||
|
echo "path=$cache/v2-$APK_KEY.apk" >> "$GITHUB_OUTPUT"
|
||||||
|
if [[ -f "$cache/v2-$APK_KEY.apk" ]]; then
|
||||||
|
mkdir -p apps/mobile/android/app/build/outputs/apk/release
|
||||||
|
cp "$cache/v2-$APK_KEY.apk" apps/mobile/android/app/build/outputs/apk/release/app-release.apk
|
||||||
|
echo "cache-hit=true" >> "$GITHUB_OUTPUT"
|
||||||
|
fi
|
||||||
|
|
||||||
- uses: pnpm/action-setup@f40ffcd9367d9f12939873eb1018b921a783ffaa # v4
|
- uses: pnpm/action-setup@f40ffcd9367d9f12939873eb1018b921a783ffaa # v4
|
||||||
- uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6
|
- uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6
|
||||||
with:
|
with:
|
||||||
node-version: 22
|
node-version: 22
|
||||||
cache: pnpm
|
|
||||||
- uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1
|
- uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1
|
||||||
with:
|
with:
|
||||||
distribution: temurin
|
distribution: temurin
|
||||||
java-version: 17
|
java-version: 17
|
||||||
- uses: gradle/actions/setup-gradle@48b5f213c81028ace310571dc5ec0fbbca0b2947 # v4.4.3
|
|
||||||
if: steps.screenshot-apk.outputs.cache-hit != 'true'
|
|
||||||
with:
|
|
||||||
cache-read-only: false
|
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: pnpm install --frozen-lockfile
|
run: pnpm install --frozen-lockfile
|
||||||
|
|
||||||
- name: Restore Maestro CLI
|
|
||||||
id: maestro-cache
|
|
||||||
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
|
|
||||||
with:
|
|
||||||
path: ~/.maestro
|
|
||||||
key: maestro-${{ runner.os }}-2.6.1-3440825f
|
|
||||||
|
|
||||||
- name: Install pinned Maestro CLI
|
- name: Install pinned Maestro CLI
|
||||||
if: steps.maestro-cache.outputs.cache-hit != 'true'
|
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
if [[ -f "$HOME/.maestro/.installed-2.6.1-3440825f" ]]; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
maestro_tmp=$(mktemp -d)
|
maestro_tmp=$(mktemp -d)
|
||||||
trap 'rm -rf "$maestro_tmp"' EXIT
|
trap 'rm -rf "$maestro_tmp"' EXIT
|
||||||
base_url=https://github.com/mobile-dev-inc/maestro/releases/download/cli-2.6.1
|
base_url=https://github.com/mobile-dev-inc/maestro/releases/download/cli-2.6.1
|
||||||
@ -88,8 +86,10 @@ jobs:
|
|||||||
(cd "$maestro_tmp" && sha256sum --check --strict -)
|
(cd "$maestro_tmp" && sha256sum --check --strict -)
|
||||||
(cd "$maestro_tmp" && sha256sum --check --strict checksums_sha256.txt)
|
(cd "$maestro_tmp" && sha256sum --check --strict checksums_sha256.txt)
|
||||||
unzip -q "$maestro_tmp/maestro.zip" -d "$maestro_tmp"
|
unzip -q "$maestro_tmp/maestro.zip" -d "$maestro_tmp"
|
||||||
|
rm -rf "$HOME/.maestro"
|
||||||
mkdir -p "$HOME/.maestro"
|
mkdir -p "$HOME/.maestro"
|
||||||
cp -R "$maestro_tmp/maestro/." "$HOME/.maestro/"
|
cp -R "$maestro_tmp/maestro/." "$HOME/.maestro/"
|
||||||
|
touch "$HOME/.maestro/.installed-2.6.1-3440825f"
|
||||||
|
|
||||||
- name: Configure Maestro CLI
|
- name: Configure Maestro CLI
|
||||||
run: |
|
run: |
|
||||||
@ -112,10 +112,14 @@ jobs:
|
|||||||
|
|
||||||
- name: Save screenshot APK
|
- name: Save screenshot APK
|
||||||
if: steps.screenshot-apk.outputs.cache-hit != 'true'
|
if: steps.screenshot-apk.outputs.cache-hit != 'true'
|
||||||
uses: actions/cache/save@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
|
env:
|
||||||
with:
|
APK_CACHE: ${{ steps.screenshot-apk.outputs.path }}
|
||||||
path: apps/mobile/android/app/build/outputs/apk/release/app-release.apk
|
run: |
|
||||||
key: ${{ steps.screenshot-apk.outputs.cache-primary-key }}
|
set -euo pipefail
|
||||||
|
mkdir -p "$(dirname "$APK_CACHE")"
|
||||||
|
find "$(dirname "$APK_CACHE")" -name '*.apk' -mtime +14 -delete
|
||||||
|
cp apps/mobile/android/app/build/outputs/apk/release/app-release.apk "$APK_CACHE.partial"
|
||||||
|
mv "$APK_CACHE.partial" "$APK_CACHE"
|
||||||
|
|
||||||
- name: Enable KVM
|
- name: Enable KVM
|
||||||
run: |
|
run: |
|
||||||
@ -151,12 +155,3 @@ jobs:
|
|||||||
MOBILE_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
MOBILE_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||||
MOBILE_SHA: ${{ github.sha }}
|
MOBILE_SHA: ${{ github.sha }}
|
||||||
run: bash scripts/publish-mobile-screenshot-gallery.sh
|
run: bash scripts/publish-mobile-screenshot-gallery.sh
|
||||||
|
|
||||||
- name: Upload screenshots and diagnostics
|
|
||||||
if: always()
|
|
||||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
|
||||||
with:
|
|
||||||
name: mobile-android-screenshots-${{ github.run_id }}-${{ github.run_attempt }}
|
|
||||||
path: test-report/mobile-screenshots
|
|
||||||
if-no-files-found: error
|
|
||||||
retention-days: 7
|
|
||||||
|
|||||||
47
.github/workflows/playwright.yml
vendored
47
.github/workflows/playwright.yml
vendored
@ -25,8 +25,8 @@ on:
|
|||||||
required: false
|
required: false
|
||||||
type: boolean
|
type: boolean
|
||||||
default: false
|
default: false
|
||||||
upload_artifacts:
|
hand_off_report:
|
||||||
description: Retain the report and diagnostics as GitHub artifacts
|
description: Leave the report on the runner host for the trusted publish workflow
|
||||||
required: false
|
required: false
|
||||||
type: boolean
|
type: boolean
|
||||||
default: false
|
default: false
|
||||||
@ -54,7 +54,7 @@ jobs:
|
|||||||
playwright:
|
playwright:
|
||||||
name: Web E2E
|
name: Web E2E
|
||||||
runs-on: [self-hosted, evoo-ci, Linux]
|
runs-on: [self-hosted, evoo-ci, Linux]
|
||||||
timeout-minutes: 30
|
timeout-minutes: 60
|
||||||
concurrency:
|
concurrency:
|
||||||
group: ${{ inputs.publish_report && 'playwright-publication' || format('playwright-{0}', github.run_id) }}
|
group: ${{ inputs.publish_report && 'playwright-publication' || format('playwright-{0}', github.run_id) }}
|
||||||
cancel-in-progress: false
|
cancel-in-progress: false
|
||||||
@ -83,16 +83,12 @@ jobs:
|
|||||||
- uses: actions/setup-node@v4
|
- uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 22
|
node-version: 22
|
||||||
cache: pnpm
|
|
||||||
- run: pnpm install --frozen-lockfile
|
- run: pnpm install --frozen-lockfile
|
||||||
- name: Clear stale Vite dependency cache
|
- name: Clear stale Vite dependency cache
|
||||||
run: rm -rf apps/web/node_modules/.vite node_modules/.vite
|
run: rm -rf apps/web/node_modules/.vite node_modules/.vite
|
||||||
- uses: actions/cache@v4
|
|
||||||
with:
|
|
||||||
path: ~/.cache/ms-playwright
|
|
||||||
key: ${{ runner.os }}-playwright-${{ hashFiles('pnpm-lock.yaml') }}
|
|
||||||
- name: Install Chromium
|
- name: Install Chromium
|
||||||
timeout-minutes: 5
|
timeout-minutes: 5
|
||||||
|
# Browsers persist in ~/.cache/ms-playwright on the runner host; no GitHub cache.
|
||||||
# Skip --with-deps: evo Linux runners keep Chromium's OS libraries installed,
|
# Skip --with-deps: evo Linux runners keep Chromium's OS libraries installed,
|
||||||
# and apt-get can stall until the job timeout on persistent self-hosted machines.
|
# and apt-get can stall until the job timeout on persistent self-hosted machines.
|
||||||
run: pnpm --filter @rakazo/web exec playwright install chromium
|
run: pnpm --filter @rakazo/web exec playwright install chromium
|
||||||
@ -149,7 +145,7 @@ jobs:
|
|||||||
if: always() && (steps.playwright_tests.outcome == 'success' || steps.playwright_tests.outcome == 'failure')
|
if: always() && (steps.playwright_tests.outcome == 'success' || steps.playwright_tests.outcome == 'failure')
|
||||||
run: pnpm --filter @rakazo/www e2e
|
run: pnpm --filter @rakazo/www e2e
|
||||||
- name: Record Playwright result
|
- name: Record Playwright result
|
||||||
if: always() && inputs.upload_artifacts
|
if: always() && inputs.hand_off_report
|
||||||
env:
|
env:
|
||||||
PLAYWRIGHT_RESULT: ${{ steps.playwright_tests.outcome }}
|
PLAYWRIGHT_RESULT: ${{ steps.playwright_tests.outcome }}
|
||||||
WWW_PLAYWRIGHT_RESULT: ${{ steps.www_playwright_tests.outcome }}
|
WWW_PLAYWRIGHT_RESULT: ${{ steps.www_playwright_tests.outcome }}
|
||||||
@ -162,19 +158,28 @@ jobs:
|
|||||||
else
|
else
|
||||||
printf 'failure\n' > test-report/e2e/outcome.txt
|
printf 'failure\n' > test-report/e2e/outcome.txt
|
||||||
fi
|
fi
|
||||||
- name: Upload Playwright artifacts
|
- name: Hand off Playwright report on the runner host
|
||||||
if: always() && inputs.upload_artifacts
|
if: always() && inputs.hand_off_report
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
uses: actions/upload-artifact@v4
|
# Never GitHub artifact storage: the publish workflow runs on the same evo host
|
||||||
with:
|
# and reads this archive as untrusted input.
|
||||||
name: playwright-artifacts-${{ github.run_id }}-${{ github.run_attempt }}
|
env:
|
||||||
path: |
|
HANDOFF_ARCHIVE: ${{ github.run_id }}-${{ github.run_attempt }}.zip
|
||||||
playwright-report
|
run: |
|
||||||
playwright-report-www
|
set -euo pipefail
|
||||||
apps/web/test-results
|
handoff="$HOME/.cache/evoo-ci/playwright-handoff"
|
||||||
test-report/e2e
|
mkdir -p "$handoff"
|
||||||
retention-days: 7
|
python3 - "$handoff/$HANDOFF_ARCHIVE.partial" playwright-report playwright-report-www test-report/e2e <<'PY'
|
||||||
if-no-files-found: warn
|
import os, sys, zipfile
|
||||||
|
with zipfile.ZipFile(sys.argv[1], "w", zipfile.ZIP_DEFLATED) as archive:
|
||||||
|
for root in sys.argv[2:]:
|
||||||
|
for directory, _, files in os.walk(root):
|
||||||
|
for name in files:
|
||||||
|
file = os.path.join(directory, name)
|
||||||
|
if os.path.isfile(file) and not os.path.islink(file):
|
||||||
|
archive.write(file)
|
||||||
|
PY
|
||||||
|
mv "$handoff/$HANDOFF_ARCHIVE.partial" "$handoff/$HANDOFF_ARCHIVE"
|
||||||
- name: Publish Playwright visual report
|
- name: Publish Playwright visual report
|
||||||
if: always() && inputs.publish_report
|
if: always() && inputs.publish_report
|
||||||
env:
|
env:
|
||||||
|
|||||||
36
.github/workflows/publish-playwright-report.yml
vendored
36
.github/workflows/publish-playwright-report.yml
vendored
@ -37,16 +37,17 @@ jobs:
|
|||||||
- uses: actions/setup-node@v4
|
- uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 22
|
node-version: 22
|
||||||
cache: pnpm
|
|
||||||
- run: pnpm install --frozen-lockfile
|
- run: pnpm install --frozen-lockfile
|
||||||
- name: Read Playwright job result
|
- name: Read Playwright job result
|
||||||
id: playwright
|
id: playwright
|
||||||
env:
|
env:
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
RUN_ID: ${{ github.event.workflow_run.id }}
|
RUN_ID: ${{ github.event.workflow_run.id }}
|
||||||
|
RUN_ATTEMPT: ${{ github.event.workflow_run.run_attempt }}
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
result="$(gh api "repos/${GITHUB_REPOSITORY}/actions/runs/${RUN_ID}/jobs?per_page=100" \
|
# Read the attempt that finished: a re-run already in progress has no conclusion yet.
|
||||||
|
result="$(gh api "repos/${GITHUB_REPOSITORY}/actions/runs/${RUN_ID}/attempts/${RUN_ATTEMPT}/jobs?per_page=100" \
|
||||||
--jq '[.jobs[] | select(.name == "Web E2E / Web E2E") | .conclusion] | unique | if length == 1 then .[0] else error("expected exactly one Playwright job") end')"
|
--jq '[.jobs[] | select(.name == "Web E2E / Web E2E") | .conclusion] | unique | if length == 1 then .[0] else error("expected exactly one Playwright job") end')"
|
||||||
case "$result" in
|
case "$result" in
|
||||||
success|failure) ;;
|
success|failure) ;;
|
||||||
@ -75,38 +76,31 @@ jobs:
|
|||||||
echo "::warning::Could not resolve a pull request for ${HEAD_OWNER}:${HEAD_BRANCH} at ${HEAD_SHA}."
|
echo "::warning::Could not resolve a pull request for ${HEAD_OWNER}:${HEAD_BRANCH} at ${HEAD_SHA}."
|
||||||
fi
|
fi
|
||||||
echo "number=$number" >> "$GITHUB_OUTPUT"
|
echo "number=$number" >> "$GITHUB_OUTPUT"
|
||||||
- name: Check for Playwright artifact
|
- name: Take over Playwright report from the runner host
|
||||||
id: artifact
|
id: artifact
|
||||||
env:
|
env:
|
||||||
ARTIFACT_NAME: playwright-artifacts-${{ github.event.workflow_run.id }}-${{ github.event.workflow_run.run_attempt }}
|
HANDOFF_ARCHIVE: ${{ github.event.workflow_run.id }}-${{ github.event.workflow_run.run_attempt }}.zip
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
PLAYWRIGHT_JOB_RESULT: ${{ steps.playwright.outputs.job_result }}
|
PLAYWRIGHT_JOB_RESULT: ${{ steps.playwright.outputs.job_result }}
|
||||||
RUN_ID: ${{ github.event.workflow_run.id }}
|
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
artifacts="$(gh api --method GET \
|
# The Web E2E job left this archive on the same evo host; GitHub artifact storage is never used.
|
||||||
"repos/${GITHUB_REPOSITORY}/actions/runs/${RUN_ID}/artifacts?name=${ARTIFACT_NAME}")"
|
handoff="$HOME/.cache/evoo-ci/playwright-handoff"
|
||||||
count="$(jq --arg name "$ARTIFACT_NAME" \
|
mkdir -p "$handoff"
|
||||||
'[.artifacts[] | select(.name == $name and .expired == false)] | length' \
|
find "$handoff" -type f -mtime +1 -delete
|
||||||
<<<"$artifacts")"
|
if [[ -f "$handoff/$HANDOFF_ARCHIVE" ]]; then
|
||||||
if [[ "$count" == "1" ]]; then
|
mv "$handoff/$HANDOFF_ARCHIVE" "$RUNNER_TEMP/playwright-artifacts.zip"
|
||||||
echo "exists=true" >> "$GITHUB_OUTPUT"
|
echo "exists=true" >> "$GITHUB_OUTPUT"
|
||||||
echo "id=$(jq -r --arg name "$ARTIFACT_NAME" '.artifacts[] | select(.name == $name and .expired == false) | .id' <<<"$artifacts")" >> "$GITHUB_OUTPUT"
|
elif [[ "$PLAYWRIGHT_JOB_RESULT" == "timed_out" ]]; then
|
||||||
elif [[ "$count" == "0" && "$PLAYWRIGHT_JOB_RESULT" == "timed_out" ]]; then
|
echo "::warning::The timed-out Playwright job ended before it handed off its report. Publishing an empty gallery."
|
||||||
echo "::warning::The timed-out Playwright job ended before it uploaded an artifact. Publishing an empty gallery."
|
|
||||||
echo "exists=false" >> "$GITHUB_OUTPUT"
|
echo "exists=false" >> "$GITHUB_OUTPUT"
|
||||||
else
|
else
|
||||||
echo "Expected exactly one unexpired Playwright artifact; found $count." >&2
|
echo "Missing Playwright report $handoff/$HANDOFF_ARCHIVE on this runner host." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
- name: Download Playwright artifacts
|
- name: Extract Playwright report
|
||||||
if: steps.artifact.outputs.exists == 'true'
|
if: steps.artifact.outputs.exists == 'true'
|
||||||
env:
|
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
ARTIFACT_ID: ${{ steps.artifact.outputs.id }}
|
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
gh api "repos/${GITHUB_REPOSITORY}/actions/artifacts/${ARTIFACT_ID}/zip" > "$RUNNER_TEMP/playwright-artifacts.zip"
|
|
||||||
# Keep untrusted files outside the checkout, dependencies, configuration,
|
# Keep untrusted files outside the checkout, dependencies, configuration,
|
||||||
# and generated dashboard consumed by the comment step.
|
# and generated dashboard consumed by the comment step.
|
||||||
python3 scripts/extract-playwright-artifacts.py \
|
python3 scripts/extract-playwright-artifacts.py \
|
||||||
|
|||||||
176
.github/workflows/publish-server-image.yml
vendored
176
.github/workflows/publish-server-image.yml
vendored
@ -3,9 +3,26 @@ name: publish-server-image
|
|||||||
# Release tags publish the source-addressed image used by in-app updates plus the human-readable
|
# Release tags publish the source-addressed image used by in-app updates plus the human-readable
|
||||||
# semver tags. Main publishes `edge`. Pull requests build with a read-only token and an isolated
|
# semver tags. Main publishes `edge`. Pull requests build with a read-only token and an isolated
|
||||||
# cache, so untrusted code can validate the images without receiving package-publish authority.
|
# cache, so untrusted code can validate the images without receiving package-publish authority.
|
||||||
|
# Self-hosted evo runners on one host share a Docker daemon and often ~/.docker; parallel matrix
|
||||||
|
# jobs must use DOCKER_CONFIG under RUNNER_TEMP plus a per-job buildx builder name wired into
|
||||||
|
# build-push-action, or setup-buildx --use races and one job's post cleanup stops another's BuildKit.
|
||||||
|
# `ci-<run>-<image>-<arch>` handoff tags are extra aliases on the per-arch digest; GHCR does not
|
||||||
|
# expose a safe tag-only delete API (DELETE /v2/.../manifests/<tag> is unsupported or digest-scoped),
|
||||||
|
# so they are left in place and do not affect edge/sha-* manifests assembled in publish.
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [main]
|
branches: [main]
|
||||||
|
paths:
|
||||||
|
- ".dockerignore"
|
||||||
|
- ".github/workflows/publish-server-image.yml"
|
||||||
|
- "apps/**"
|
||||||
|
- "infra/compose/**"
|
||||||
|
- "infra/sandboxes/computer/**"
|
||||||
|
- "infra/updater/**"
|
||||||
|
- "packages/**"
|
||||||
|
- "package.json"
|
||||||
|
- "pnpm-lock.yaml"
|
||||||
|
- "pnpm-workspace.yaml"
|
||||||
tags: ["v*"]
|
tags: ["v*"]
|
||||||
pull_request:
|
pull_request:
|
||||||
paths:
|
paths:
|
||||||
@ -27,12 +44,55 @@ permissions:
|
|||||||
env:
|
env:
|
||||||
# docker/metadata-action otherwise truncates commit tags to seven collision-prone characters.
|
# docker/metadata-action otherwise truncates commit tags to seven collision-prone characters.
|
||||||
DOCKER_METADATA_SHORT_SHA_LENGTH: 40
|
DOCKER_METADATA_SHORT_SHA_LENGTH: 40
|
||||||
|
# build-push-action v6 uploads large *.dockerbuild records by default; publish reads digests from GHCR handoff tags.
|
||||||
|
DOCKER_BUILD_RECORD_UPLOAD: false
|
||||||
|
|
||||||
concurrency:
|
concurrency:
|
||||||
group: publish-server-image-${{ github.ref }}
|
group: publish-server-image-${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || github.ref }}
|
||||||
cancel-in-progress: false
|
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
changes:
|
||||||
|
name: Detect image publish inputs
|
||||||
|
runs-on: [self-hosted, evoo-ci]
|
||||||
|
outputs:
|
||||||
|
publish: ${{ steps.decide.outputs.publish }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5.1.0
|
||||||
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||||
|
with:
|
||||||
|
persist-credentials: false
|
||||||
|
fetch-depth: 0
|
||||||
|
- uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
|
||||||
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||||
|
id: filter
|
||||||
|
with:
|
||||||
|
filters: |
|
||||||
|
publish:
|
||||||
|
- '.dockerignore'
|
||||||
|
- '.github/workflows/publish-server-image.yml'
|
||||||
|
- 'apps/**'
|
||||||
|
- 'infra/compose/**'
|
||||||
|
- 'infra/sandboxes/computer/**'
|
||||||
|
- 'infra/updater/**'
|
||||||
|
- 'packages/**'
|
||||||
|
- 'package.json'
|
||||||
|
- 'pnpm-lock.yaml'
|
||||||
|
- 'pnpm-workspace.yaml'
|
||||||
|
- name: Decide whether release jobs should run
|
||||||
|
id: decide
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]] || [[ "${{ github.ref }}" == refs/tags/v* ]]; then
|
||||||
|
echo "publish=true" >> "$GITHUB_OUTPUT"
|
||||||
|
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
|
||||||
|
echo "publish=true" >> "$GITHUB_OUTPUT"
|
||||||
|
elif [[ "${{ steps.filter.outputs.publish }}" == "true" ]]; then
|
||||||
|
echo "publish=true" >> "$GITHUB_OUTPUT"
|
||||||
|
else
|
||||||
|
echo "publish=false" >> "$GITHUB_OUTPUT"
|
||||||
|
fi
|
||||||
|
|
||||||
installer-smoke:
|
installer-smoke:
|
||||||
name: Compose installer smokes
|
name: Compose installer smokes
|
||||||
if: github.event_name == 'pull_request'
|
if: github.event_name == 'pull_request'
|
||||||
@ -70,7 +130,16 @@ jobs:
|
|||||||
- uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5.1.0
|
- uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5.1.0
|
||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
|
- name: Prepare isolated Docker config directory
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
cfg="${RUNNER_TEMP}/docker-config"
|
||||||
|
mkdir -p "$cfg"
|
||||||
|
echo "DOCKER_CONFIG=${cfg}" >> "$GITHUB_ENV"
|
||||||
|
- id: buildx
|
||||||
|
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
|
||||||
|
with:
|
||||||
|
name: ci-pr-${{ github.event.pull_request.number }}-${{ matrix.name }}
|
||||||
- name: Resolve the lowercase image namespace
|
- name: Resolve the lowercase image namespace
|
||||||
id: ns
|
id: ns
|
||||||
# GHCR rejects repository names with capitals; the GitHub repository slug has them.
|
# GHCR rejects repository names with capitals; the GitHub repository slug has them.
|
||||||
@ -80,28 +149,37 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
images: ghcr.io/${{ steps.ns.outputs.repo }}/${{ matrix.name }}
|
images: ghcr.io/${{ steps.ns.outputs.repo }}/${{ matrix.name }}
|
||||||
tags: type=sha,prefix=sha-
|
tags: type=sha,prefix=sha-
|
||||||
|
- name: Locate the main build cache on this runner host
|
||||||
|
id: cache
|
||||||
|
# Build caches stay on the evo host; GitHub's cache service is never used.
|
||||||
|
run: echo "dir=$HOME/.cache/evoo-ci/buildx/${{ matrix.name }}-amd64" >> "$GITHUB_OUTPUT"
|
||||||
- uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2
|
- uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2
|
||||||
with:
|
with:
|
||||||
|
builder: ${{ steps.buildx.outputs.name }}
|
||||||
context: ${{ matrix.context }}
|
context: ${{ matrix.context }}
|
||||||
file: ${{ matrix.dockerfile }}
|
file: ${{ matrix.dockerfile }}
|
||||||
push: false
|
push: false
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
build-args: ${{ matrix.name == 'app' && format('GIT_SHA={0}', github.sha) || '' }}
|
build-args: ${{ matrix.name == 'app' && format('GIT_SHA={0}', github.sha) || '' }}
|
||||||
cache-from: type=gha,scope=${{ matrix.name }}
|
# Pull requests only read the cache main writes.
|
||||||
cache-to: type=gha,mode=max,scope=pr-${{ github.event.pull_request.number }}-${{ matrix.name }}
|
cache-from: type=local,src=${{ steps.cache.outputs.dir }}
|
||||||
provenance: false
|
provenance: false
|
||||||
sbom: false
|
sbom: false
|
||||||
|
|
||||||
build:
|
build:
|
||||||
name: Build ${{ matrix.name }} ${{ matrix.arch }}
|
name: Build ${{ matrix.name }} ${{ matrix.arch }}
|
||||||
if: github.event_name != 'pull_request'
|
needs: changes
|
||||||
|
if: github.event_name != 'pull_request' && needs.changes.outputs.publish == 'true'
|
||||||
# Each architecture builds natively on its own runner and pushes by digest only.
|
# Each architecture builds natively on its own runner and pushes by digest only.
|
||||||
# amd64 on evo Linux runners. arm64 is paused: the macOS runners have no Docker buildx,
|
# amd64 on evo Linux runners. arm64 is paused: the macOS runners have no Docker buildx,
|
||||||
# and QEMU emulation took hours for the desktop-sized computer image.
|
# and QEMU emulation took hours for the desktop-sized computer image.
|
||||||
# See docs/self-host.md (Published images and tags).
|
# See docs/self-host.md (Published images and tags).
|
||||||
runs-on: ${{ matrix.runner }}
|
runs-on: ${{ matrix.runner }}
|
||||||
timeout-minutes: 90
|
timeout-minutes: 90
|
||||||
|
concurrency:
|
||||||
|
group: publish-server-image-build-${{ github.ref }}-${{ matrix.name }}-${{ matrix.arch }}
|
||||||
|
cancel-in-progress: ${{ github.ref == 'refs/heads/main' }}
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
packages: write
|
packages: write
|
||||||
@ -128,7 +206,16 @@ jobs:
|
|||||||
- uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5.1.0
|
- uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5.1.0
|
||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
|
- name: Prepare isolated Docker config directory
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
cfg="${RUNNER_TEMP}/docker-config"
|
||||||
|
mkdir -p "$cfg"
|
||||||
|
echo "DOCKER_CONFIG=${cfg}" >> "$GITHUB_ENV"
|
||||||
|
- id: buildx
|
||||||
|
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
|
||||||
|
with:
|
||||||
|
name: ci-${{ github.run_id }}-${{ github.run_attempt }}-${{ matrix.name }}-${{ matrix.arch }}
|
||||||
- name: Log in to GHCR
|
- name: Log in to GHCR
|
||||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0
|
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0
|
||||||
with:
|
with:
|
||||||
@ -145,9 +232,14 @@ jobs:
|
|||||||
# Labels only; the tagged manifest is assembled in the publish job.
|
# Labels only; the tagged manifest is assembled in the publish job.
|
||||||
images: ghcr.io/${{ steps.ns.outputs.repo }}/${{ matrix.name }}
|
images: ghcr.io/${{ steps.ns.outputs.repo }}/${{ matrix.name }}
|
||||||
tags: type=sha,prefix=sha-
|
tags: type=sha,prefix=sha-
|
||||||
|
- name: Locate the build cache on this runner host
|
||||||
|
id: cache
|
||||||
|
# Build caches stay on the evo host; GitHub's cache service is never used.
|
||||||
|
run: echo "dir=$HOME/.cache/evoo-ci/buildx/${{ matrix.name }}-${{ matrix.arch }}" >> "$GITHUB_OUTPUT"
|
||||||
- id: build
|
- id: build
|
||||||
uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2
|
uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2
|
||||||
with:
|
with:
|
||||||
|
builder: ${{ steps.buildx.outputs.name }}
|
||||||
context: ${{ matrix.context }}
|
context: ${{ matrix.context }}
|
||||||
file: ${{ matrix.dockerfile }}
|
file: ${{ matrix.dockerfile }}
|
||||||
platforms: linux/${{ matrix.arch }}
|
platforms: linux/${{ matrix.arch }}
|
||||||
@ -156,33 +248,37 @@ jobs:
|
|||||||
outputs: type=image,name=ghcr.io/${{ steps.ns.outputs.repo }}/${{ matrix.name }},push-by-digest=true,name-canonical=true,push=true
|
outputs: type=image,name=ghcr.io/${{ steps.ns.outputs.repo }}/${{ matrix.name }},push-by-digest=true,name-canonical=true,push=true
|
||||||
# GET /health can report the exact source commit without a deployment-supplied override.
|
# GET /health can report the exact source commit without a deployment-supplied override.
|
||||||
build-args: ${{ matrix.name == 'app' && format('GIT_SHA={0}', github.sha) || '' }}
|
build-args: ${{ matrix.name == 'app' && format('GIT_SHA={0}', github.sha) || '' }}
|
||||||
cache-from: type=gha,scope=${{ matrix.name }}-${{ matrix.arch }}
|
cache-from: type=local,src=${{ steps.cache.outputs.dir }}
|
||||||
cache-to: type=gha,mode=max,scope=${{ matrix.name }}-${{ matrix.arch }}
|
cache-to: type=local,mode=max,dest=${{ steps.cache.outputs.dir }}-new
|
||||||
provenance: mode=max
|
provenance: mode=max
|
||||||
sbom: true
|
sbom: true
|
||||||
- name: Record the architecture digest
|
- name: Replace the build cache
|
||||||
id: digest
|
# A local cache only grows; swapping in the fresh export drops layers this build no longer uses.
|
||||||
env:
|
env:
|
||||||
DIGEST: ${{ steps.build.outputs.digest }}
|
CACHE_DIR: ${{ steps.cache.outputs.dir }}
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
digest_dir="${RUNNER_TEMP}/digest"
|
rm -rf "$CACHE_DIR"
|
||||||
mkdir -p "$digest_dir"
|
mv "$CACHE_DIR-new" "$CACHE_DIR"
|
||||||
touch "${digest_dir}/${DIGEST#sha256:}"
|
- name: Tag digest for publish job handoff
|
||||||
echo "dir=${digest_dir}" >> "$GITHUB_OUTPUT"
|
env:
|
||||||
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
DIGEST: ${{ steps.build.outputs.digest }}
|
||||||
with:
|
IMAGE: ghcr.io/${{ steps.ns.outputs.repo }}/${{ matrix.name }}
|
||||||
name: digest-${{ matrix.name }}-${{ matrix.arch }}
|
run: |
|
||||||
path: ${{ steps.digest.outputs.dir }}/*
|
set -euo pipefail
|
||||||
if-no-files-found: error
|
docker buildx imagetools create \
|
||||||
retention-days: 1
|
-t "${IMAGE}:ci-${GITHUB_RUN_ID}-${{ matrix.name }}-${{ matrix.arch }}" \
|
||||||
|
"${IMAGE}@${DIGEST}"
|
||||||
|
|
||||||
publish:
|
publish:
|
||||||
name: Publish ${{ matrix.name }} image
|
name: Publish ${{ matrix.name }} image
|
||||||
if: github.event_name != 'pull_request'
|
if: github.event_name != 'pull_request' && needs.changes.outputs.publish == 'true'
|
||||||
needs: build
|
needs: [changes, build]
|
||||||
runs-on: [self-hosted, evoo-ci, Linux]
|
runs-on: [self-hosted, evoo-ci, Linux]
|
||||||
timeout-minutes: 15
|
timeout-minutes: 15
|
||||||
|
concurrency:
|
||||||
|
group: publish-server-image-publish-${{ github.ref == 'refs/heads/main' && 'main' || github.ref }}-${{ matrix.name }}
|
||||||
|
cancel-in-progress: false
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
packages: write
|
packages: write
|
||||||
@ -196,15 +292,16 @@ jobs:
|
|||||||
- name: updater
|
- name: updater
|
||||||
- name: computer
|
- name: computer
|
||||||
steps:
|
steps:
|
||||||
- name: Prepare an isolated digest directory
|
- name: Prepare isolated Docker config directory
|
||||||
id: digest_dir
|
run: |
|
||||||
run: echo "path=${RUNNER_TEMP}/digests" >> "$GITHUB_OUTPUT"
|
set -euo pipefail
|
||||||
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
|
cfg="${RUNNER_TEMP}/docker-config"
|
||||||
|
mkdir -p "$cfg"
|
||||||
|
echo "DOCKER_CONFIG=${cfg}" >> "$GITHUB_ENV"
|
||||||
|
- id: buildx
|
||||||
|
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
|
||||||
with:
|
with:
|
||||||
pattern: digest-${{ matrix.name }}-*
|
name: ci-${{ github.run_id }}-${{ github.run_attempt }}-publish-${{ matrix.name }}
|
||||||
path: ${{ steps.digest_dir.outputs.path }}
|
|
||||||
merge-multiple: true
|
|
||||||
- uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
|
|
||||||
- name: Log in to GHCR
|
- name: Log in to GHCR
|
||||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0
|
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0
|
||||||
with:
|
with:
|
||||||
@ -215,6 +312,21 @@ jobs:
|
|||||||
id: ns
|
id: ns
|
||||||
# GHCR rejects repository names with capitals; the GitHub repository slug has them.
|
# GHCR rejects repository names with capitals; the GitHub repository slug has them.
|
||||||
run: echo "repo=${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT"
|
run: echo "repo=${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT"
|
||||||
|
- name: Collect architecture digests from registry handoff tags
|
||||||
|
id: digest_dir
|
||||||
|
env:
|
||||||
|
IMAGE: ghcr.io/${{ steps.ns.outputs.repo }}/${{ matrix.name }}
|
||||||
|
IMAGE_NAME: ${{ matrix.name }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
digest_dir="${RUNNER_TEMP}/digests"
|
||||||
|
mkdir -p "$digest_dir"
|
||||||
|
for arch in amd64; do
|
||||||
|
handoff="${IMAGE}:ci-${GITHUB_RUN_ID}-${IMAGE_NAME}-${arch}"
|
||||||
|
digest="$(docker buildx imagetools inspect "$handoff" --format '{{.Manifest.Digest}}')"
|
||||||
|
touch "${digest_dir}/${digest#sha256:}"
|
||||||
|
done
|
||||||
|
echo "path=${digest_dir}" >> "$GITHUB_OUTPUT"
|
||||||
- id: meta
|
- id: meta
|
||||||
uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0
|
uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0
|
||||||
with:
|
with:
|
||||||
|
|||||||
88
.github/workflows/release-desktop.yml
vendored
88
.github/workflows/release-desktop.yml
vendored
@ -18,6 +18,8 @@ jobs:
|
|||||||
validate:
|
validate:
|
||||||
name: Validate stable release
|
name: Validate stable release
|
||||||
runs-on: [self-hosted, evoo-ci, Linux]
|
runs-on: [self-hosted, evoo-ci, Linux]
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
outputs:
|
outputs:
|
||||||
tag: ${{ steps.release.outputs.tag }}
|
tag: ${{ steps.release.outputs.tag }}
|
||||||
version: ${{ steps.release.outputs.version }}
|
version: ${{ steps.release.outputs.version }}
|
||||||
@ -95,17 +97,32 @@ jobs:
|
|||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
include='[{"runner":["self-hosted","evoo-ci","macOS"],"artifact":"macos"},{"runner":["self-hosted","evoo-ci","Linux"],"artifact":"linux"}]'
|
include='[{"runner":["self-hosted","evoo-ci","macOS"],"artifact":"macos"},{"runner":["self-hosted","evoo-ci","Linux"],"artifact":"linux"}]'
|
||||||
if [[ "$WINDOWS_SIGNING" == "true" ]]; then
|
if [[ "$WINDOWS_SIGNING" == "true" ]]; then
|
||||||
# No self-hosted Windows runner; keep GitHub-hosted only when signing credentials exist.
|
# Internal runners only: Windows waits for a self-hosted Windows runner, never a GitHub-hosted one.
|
||||||
include="$(jq -c '. + [{"runner":"windows-2022","artifact":"windows"}]' <<<"$include")"
|
include="$(jq -c '. + [{"runner":["self-hosted","evoo-ci","Windows"],"artifact":"windows"}]' <<<"$include")"
|
||||||
fi
|
fi
|
||||||
echo "include=$include" >> "$GITHUB_OUTPUT"
|
echo "include=$include" >> "$GITHUB_OUTPUT"
|
||||||
echo "windows=$WINDOWS_SIGNING" >> "$GITHUB_OUTPUT"
|
echo "windows=$WINDOWS_SIGNING" >> "$GITHUB_OUTPUT"
|
||||||
|
- name: Open the draft release the platform builds fill
|
||||||
|
env:
|
||||||
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
RELEASE_TAG: ${{ steps.release.outputs.tag }}
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
# Builds hand their files to this draft, never to GitHub Actions artifact storage.
|
||||||
|
if [[ "$(gh api "repos/${GITHUB_REPOSITORY}/releases?per_page=100" \
|
||||||
|
--jq "[.[] | select(.draft and .tag_name == \"${RELEASE_TAG}\")] | length")" != "0" ]]; then
|
||||||
|
gh release delete "$RELEASE_TAG" --yes
|
||||||
|
fi
|
||||||
|
gh release create "$RELEASE_TAG" --draft --generate-notes --title "Rakazo $RELEASE_TAG" --verify-tag
|
||||||
|
|
||||||
build:
|
build:
|
||||||
name: Build signed ${{ matrix.artifact }} artifacts
|
name: Build signed ${{ matrix.artifact }} artifacts
|
||||||
needs: validate
|
needs: validate
|
||||||
runs-on: ${{ matrix.runner }}
|
runs-on: ${{ matrix.runner }}
|
||||||
timeout-minutes: 60
|
timeout-minutes: 60
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
@ -172,8 +189,8 @@ jobs:
|
|||||||
xcrun stapler validate "$app"
|
xcrun stapler validate "$app"
|
||||||
spctl --assess --type execute --verbose=2 "$app"
|
spctl --assess --type execute --verbose=2 "$app"
|
||||||
grep -Fqx "provider: github" "$app/Contents/Resources/app-update.yml"
|
grep -Fqx "provider: github" "$app/Contents/Resources/app-update.yml"
|
||||||
grep -Fqx "owner: elie222" "$app/Contents/Resources/app-update.yml"
|
grep -Fqx "owner: cynarAI" "$app/Contents/Resources/app-update.yml"
|
||||||
grep -Fqx "repo: rakazo" "$app/Contents/Resources/app-update.yml"
|
grep -Fqx "repo: evOo-local" "$app/Contents/Resources/app-update.yml"
|
||||||
|
|
||||||
- name: Require Windows signing credentials
|
- name: Require Windows signing credentials
|
||||||
if: runner.os == 'Windows'
|
if: runner.os == 'Windows'
|
||||||
@ -214,7 +231,7 @@ jobs:
|
|||||||
throw "Windows update config is not bound to the signing publisher."
|
throw "Windows update config is not bound to the signing publisher."
|
||||||
}
|
}
|
||||||
$feed = Get-Content -Raw $config
|
$feed = Get-Content -Raw $config
|
||||||
foreach ($expected in @("provider: github", "owner: elie222", "repo: rakazo")) {
|
foreach ($expected in @("provider: github", "owner: cynarAI", "repo: evOo-local")) {
|
||||||
if ($feed -notmatch "(?m)^$([regex]::Escape($expected))\r?$") {
|
if ($feed -notmatch "(?m)^$([regex]::Escape($expected))\r?$") {
|
||||||
throw "Windows update config missing '$expected'."
|
throw "Windows update config missing '$expected'."
|
||||||
}
|
}
|
||||||
@ -239,25 +256,32 @@ jobs:
|
|||||||
test -n "$config"
|
test -n "$config"
|
||||||
test -f "$config"
|
test -f "$config"
|
||||||
grep -Fqx "provider: github" "$config"
|
grep -Fqx "provider: github" "$config"
|
||||||
grep -Fqx "owner: elie222" "$config"
|
grep -Fqx "owner: cynarAI" "$config"
|
||||||
grep -Fqx "repo: rakazo" "$config"
|
grep -Fqx "repo: evOo-local" "$config"
|
||||||
test -f apps/desktop/out/latest-linux.yml
|
test -f apps/desktop/out/latest-linux.yml
|
||||||
grep -Fqx "version: $RELEASE_VERSION" apps/desktop/out/latest-linux.yml
|
grep -Fqx "version: $RELEASE_VERSION" apps/desktop/out/latest-linux.yml
|
||||||
|
|
||||||
- name: Retain installers and updater metadata
|
- name: Hand installers and updater metadata to the draft release
|
||||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
shell: bash
|
||||||
with:
|
env:
|
||||||
name: desktop-${{ matrix.artifact }}-${{ needs.validate.outputs.sha }}
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
path: |
|
RELEASE_TAG: ${{ needs.validate.outputs.tag }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
shopt -s nullglob
|
||||||
|
files=(
|
||||||
apps/desktop/out/*.AppImage
|
apps/desktop/out/*.AppImage
|
||||||
apps/desktop/out/*.blockmap
|
apps/desktop/out/*.blockmap
|
||||||
apps/desktop/out/*.dmg
|
apps/desktop/out/*.dmg
|
||||||
apps/desktop/out/*.exe
|
apps/desktop/out/*.exe
|
||||||
apps/desktop/out/latest*.yml
|
apps/desktop/out/latest*.yml
|
||||||
apps/desktop/out/*.zip
|
apps/desktop/out/*.zip
|
||||||
if-no-files-found: error
|
)
|
||||||
compression-level: 0
|
if [[ ${#files[@]} -eq 0 ]]; then
|
||||||
retention-days: 7
|
echo "The build produced no release files." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
bash scripts/upload-release-assets.sh "${files[@]}"
|
||||||
|
|
||||||
publish:
|
publish:
|
||||||
name: Attest and publish complete release
|
name: Attest and publish complete release
|
||||||
@ -275,11 +299,11 @@ jobs:
|
|||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
ref: ${{ needs.validate.outputs.sha }}
|
ref: ${{ needs.validate.outputs.sha }}
|
||||||
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
|
- name: Collect every platform from the draft release
|
||||||
with:
|
env:
|
||||||
pattern: desktop-*-${{ needs.validate.outputs.sha }}
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
path: release-artifacts
|
RELEASE_TAG: ${{ needs.validate.outputs.tag }}
|
||||||
merge-multiple: true
|
run: gh release download "$RELEASE_TAG" --dir release-artifacts
|
||||||
- name: Verify the complete stable update feed
|
- name: Verify the complete stable update feed
|
||||||
env:
|
env:
|
||||||
RELEASE_SHA: ${{ needs.validate.outputs.sha }}
|
RELEASE_SHA: ${{ needs.validate.outputs.sha }}
|
||||||
@ -323,26 +347,34 @@ jobs:
|
|||||||
uses: actions/attest-build-provenance@977bb373ede98d70efdf65b84cb5f73e068dcc2a # v3
|
uses: actions/attest-build-provenance@977bb373ede98d70efdf65b84cb5f73e068dcc2a # v3
|
||||||
with:
|
with:
|
||||||
subject-path: release-artifacts/*
|
subject-path: release-artifacts/*
|
||||||
- name: Create draft and upload every platform
|
- name: Add checksums to the draft
|
||||||
id: create_release
|
|
||||||
env:
|
env:
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
RELEASE_TAG: ${{ needs.validate.outputs.tag }}
|
RELEASE_TAG: ${{ needs.validate.outputs.tag }}
|
||||||
run: >-
|
run: gh release upload "$RELEASE_TAG" release-artifacts/SHA256SUMS --clobber
|
||||||
gh release create "$RELEASE_TAG" release-artifacts/*
|
|
||||||
--draft --generate-notes --title "Rakazo $RELEASE_TAG" --verify-tag
|
|
||||||
- name: Publish the completed release
|
- name: Publish the completed release
|
||||||
env:
|
env:
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
RELEASE_TAG: ${{ needs.validate.outputs.tag }}
|
RELEASE_TAG: ${{ needs.validate.outputs.tag }}
|
||||||
run: gh release edit "$RELEASE_TAG" --draft=false --latest
|
run: gh release edit "$RELEASE_TAG" --draft=false --latest
|
||||||
- name: Remove an incomplete draft
|
|
||||||
if: failure() && steps.create_release.outcome != 'skipped'
|
discard:
|
||||||
|
name: Remove an incomplete draft
|
||||||
|
needs: [validate, build, publish]
|
||||||
|
if: failure() && needs.validate.result == 'success'
|
||||||
|
runs-on: [self-hosted, evoo-ci, Linux]
|
||||||
|
timeout-minutes: 5
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
steps:
|
||||||
|
- name: Delete the draft release
|
||||||
env:
|
env:
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
GH_REPO: ${{ github.repository }}
|
||||||
RELEASE_TAG: ${{ needs.validate.outputs.tag }}
|
RELEASE_TAG: ${{ needs.validate.outputs.tag }}
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
if [[ "$(gh api "repos/${GITHUB_REPOSITORY}/releases/tags/${RELEASE_TAG}" --jq .draft 2>/dev/null)" == "true" ]]; then
|
if [[ "$(gh api "repos/${GITHUB_REPOSITORY}/releases?per_page=100" \
|
||||||
|
--jq "[.[] | select(.draft and .tag_name == \"${RELEASE_TAG}\")] | length")" != "0" ]]; then
|
||||||
gh release delete "$RELEASE_TAG" --yes
|
gh release delete "$RELEASE_TAG" --yes
|
||||||
fi
|
fi
|
||||||
|
|||||||
1
.github/workflows/topology-verification.yml
vendored
1
.github/workflows/topology-verification.yml
vendored
@ -19,7 +19,6 @@ jobs:
|
|||||||
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
||||||
with:
|
with:
|
||||||
node-version: 22
|
node-version: 22
|
||||||
cache: pnpm
|
|
||||||
- run: pnpm install --frozen-lockfile
|
- run: pnpm install --frozen-lockfile
|
||||||
- run: pnpm test:topology
|
- run: pnpm test:topology
|
||||||
- name: Replay computer tools against real Chromium without inference
|
- name: Replay computer tools against real Chromium without inference
|
||||||
|
|||||||
@ -11,6 +11,7 @@
|
|||||||
- Treat auth, secret handling, sandbox boundaries, host commands, and integrations as security-sensitive. Keep tests deterministic and offline by default.
|
- Treat auth, secret handling, sandbox boundaries, host commands, and integrations as security-sensitive. Keep tests deterministic and offline by default.
|
||||||
- The desktop Playwright suite (`pnpm --filter @rakazo/desktop test:e2e`) opens real Electron windows and steals focus on macOS. Do not run it on a maintainer's machine as routine verification; run the unit tests locally and let CI's virtual-display job run the e2e on push.
|
- The desktop Playwright suite (`pnpm --filter @rakazo/desktop test:e2e`) opens real Electron windows and steals focus on macOS. Do not run it on a maintainer's machine as routine verification; run the unit tests locally and let CI's virtual-display job run the e2e on push.
|
||||||
- Work autonomously. Make routine calls yourself, such as opening or reopening a pull request, linking CI screenshots, or fixing gaps you found that belong to the task, then verify and report the outcome. Ask the maintainer only before irreversible actions or for product decisions without a sensible default.
|
- Work autonomously. Make routine calls yourself, such as opening or reopening a pull request, linking CI screenshots, or fixing gaps you found that belong to the task, then verify and report the outcome. Ask the maintainer only before irreversible actions or for product decisions without a sensible default.
|
||||||
|
- CI runs only on the self-hosted `evoo-ci` runners and never consumes GitHub Actions resources: no GitHub-hosted runners, no Actions artifact storage (`upload-artifact`, `download-artifact`), and no GitHub cache service (`actions/cache`, setup-node `cache:`, buildx `type=gha`). Keep caches and job handoffs on the runner host; `infra/updater/src/github-actions-budget.test.ts` enforces this.
|
||||||
- After creating a pull request, stay with it until CI and automated review bots have finished; passing checks alone do not mean the review is complete. Follow `.agents/skills/pr-watch/SKILL.md`: run its `pr-digest --watch` helper as a single backgrounded call that blocks until the checks on the current head commit are terminal, then act on the verdict it prints. Do not poll in the foreground on a timer, which mostly buys repeated snapshots of an unchanged PR and dies at the tool timeout. Address every actionable issue, push the fixes, and repeat until no actionable feedback remains. Do not merge while review bots are still pending or review issues remain unresolved.
|
- After creating a pull request, stay with it until CI and automated review bots have finished; passing checks alone do not mean the review is complete. Follow `.agents/skills/pr-watch/SKILL.md`: run its `pr-digest --watch` helper as a single backgrounded call that blocks until the checks on the current head commit are terminal, then act on the verdict it prints. Do not poll in the foreground on a timer, which mostly buys repeated snapshots of an unchanged PR and dies at the tool timeout. Address every actionable issue, push the fixes, and repeat until no actionable feedback remains. Do not merge while review bots are still pending or review issues remain unresolved.
|
||||||
- PR descriptions say why the change exists, what changed, and how it was tested. Write Why as the product reason (what was wrong and what this changes); never "Elie wants" or a third-person briefing of a named person; the maintainer is the author.
|
- PR descriptions say why the change exists, what changed, and how it was tested. Write Why as the product reason (what was wrong and what this changes); never "Elie wants" or a third-person briefing of a named person; the maintainer is the author.
|
||||||
- Commit messages, PR descriptions, issues, and review replies are public too. Describe test results in words instead of pasting tool output, and never include anything that identifies a person, machine, or account: local paths, usernames, hostnames, signing identities, legal entity names, Apple Team IDs, API key IDs, issuer or tenant IDs, account emails, or the accounts, machines, and files that secret values came from. Naming the secrets a workflow reads is fine; use placeholders when a value must be shown.
|
- Commit messages, PR descriptions, issues, and review replies are public too. Describe test results in words instead of pasting tool output, and never include anything that identifies a person, machine, or account: local paths, usernames, hostnames, signing identities, legal entity names, Apple Team IDs, API key IDs, issuer or tenant IDs, account emails, or the accounts, machines, and files that secret values came from. Naming the secrets a workflow reads is fine; use placeholders when a value must be shown.
|
||||||
|
|||||||
114
PROJECT.md
Normal file
114
PROJECT.md
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
# Project: Dual-Surface-View in evOo (`apps/web`)
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
evOo (`apps/web`) organizes the workspace into two primary surfaces:
|
||||||
|
1. **Mission Stream**: The chat, agent transcript (`data-testid="transcript"`), run steps (`data-testid="run-steps"`), and composer inside `<main>`.
|
||||||
|
2. **Living Canvas**: The computer surface (`data-testid="computer-preview"`), workspace files explorer (`WorkspaceFilesView`), code inspector, in-app browser, and artifact viewer inside `<aside data-testid="side-panel">`.
|
||||||
|
|
||||||
|
In desktop viewports (>= 768px `md`), these two surfaces operate as equal, first-class split surfaces within `Shell.tsx`:
|
||||||
|
- Positioned side-by-side without modal dialog occlusion.
|
||||||
|
- Separated by an interactive `DualSurfaceDivider` supporting fluid pointer drag-resizing, double-click snap to 50/50 balance, and keyboard arrow adjustments.
|
||||||
|
- Controlled via quick-switch header preset controls: Focus Stream (100/0), Dual-Surface Split (50/50), Focus Canvas (0/100), and a side-swapping toggle (Stream-first vs. Canvas-first).
|
||||||
|
- Configured by persisted preferences (`localStorage` via `dual-surface-pref.ts`) with safe defaults and bounded numeric clamping.
|
||||||
|
- Responsive degradation (< 768px `md`): Automatically transitions to stacked/drawer/single-surface mode without mutating desktop split ratio preferences.
|
||||||
|
- Monochrome design system adhering to `@rakazo/ui-tokens` with zero hardcoded hex or arbitrary CSS values.
|
||||||
|
- Internationalized using LinguiJS with 100% German (`de`) translation coverage.
|
||||||
|
|
||||||
|
```
|
||||||
|
+-----------------------------------------------------------------------------------+
|
||||||
|
| Top Navigation Bar: [Bots Toggle] ... [Preset: 100/0 | 50/50 | 0/100] [Swap] ... |
|
||||||
|
+----------------------------------------------------+---+--------------------------+
|
||||||
|
| Surface A (e.g. Mission Stream / Chat) | D | Surface B (Living Canvas)|
|
||||||
|
| | I | |
|
||||||
|
| - Transcript (`data-testid="transcript"`) | V | - Computer Preview |
|
||||||
|
| - Run steps (`data-testid="run-steps"`) | I | - Workspace Explorer |
|
||||||
|
| - Composer | D | - Code Inspector |
|
||||||
|
| | E | (`data-testid="side-panel")|
|
||||||
|
| | R | |
|
||||||
|
+----------------------------------------------------+---+--------------------------+
|
||||||
|
```
|
||||||
|
|
||||||
|
## Feature Inventory
|
||||||
|
| # | Feature | Description | Milestone | Source |
|
||||||
|
|---|---------|-------------|-----------|--------|
|
||||||
|
| 1 | Preferences Storage & Schema | `dual-surface-pref.ts` managing `splitRatio` (0.20-0.80, default 0.50), `layoutMode` (`split`, `stream`, `canvas`), `orientation` (`stream-first`, `canvas-first`) with localStorage read/write and bounds clamping | M1 | ORIGINAL_REQUEST §R3 |
|
||||||
|
| 2 | Layout Hook & State Coordination | `useDualSurfaceLayout.ts` React hook handling layout state, preset toggles, orientation flip, and media query synchronization | M1 | ORIGINAL_REQUEST §R1, R3 |
|
||||||
|
| 3 | Fluid Drag-Resize & Snap Divider | `DualSurfaceDivider.tsx` + `useDualSurfaceResize.ts` with pointer capture, hover/active semantic tokens, bounds clamping, double-click 50/50 snap, and ARIA separator attributes | M2 | ORIGINAL_REQUEST §R2, R4 |
|
||||||
|
| 4 | Header Preset Controls & Orientation Swap | `DualSurfaceHeaderControls.tsx` with quick-switch presets (100/0, 50/50, 0/100) and Swap Orientation toggle, styled with semantic tokens and full accessibility | M2 | ORIGINAL_REQUEST §R1, R2 |
|
||||||
|
| 5 | Shell Flex Integration & Surface Swap | Coordinated flex container in `Shell.tsx` hosting `<main>` and `<aside data-testid="side-panel">`, supporting dynamic order swapping while preserving existing test IDs | M3 | ORIGINAL_REQUEST §R1, R2, R4 |
|
||||||
|
| 6 | Responsive Degradation (< md) | Seamless fallback on viewports < 768px to stacked/single-surface navigation without corrupting persisted desktop preferences | M3 | ORIGINAL_REQUEST §R3 |
|
||||||
|
| 7 | Design System & Token Compliance | Exclusively semantic tokens from `@rakazo/ui-tokens` (`bg-border`, `hover:bg-accent`, `active:bg-primary`, `ring-ring`), monochrome styling, no hardcoded hex/arbitrary css | M3 | ORIGINAL_REQUEST §R4 |
|
||||||
|
| 8 | Localization (German Translations) | Lingui message extraction, full German translations in `messages.po`, clean `intl:extract` and `intl:compile` | M3 | ORIGINAL_REQUEST §Acceptance Criteria |
|
||||||
|
| 9 | E2E Testing & Adversarial Hardening | Comprehensive test suite (Tiers 1-4) and adversarial hardening (Tier 5), 100% green tests (78/78 suites baseline + new tests) and 0 TS errors | M4 | ORIGINAL_REQUEST §Acceptance Criteria |
|
||||||
|
|
||||||
|
## Milestones
|
||||||
|
| # | Name | Scope | Dependencies | Status |
|
||||||
|
|---|------|-------|-------------|--------|
|
||||||
|
| M1 | Preferences, Layout Hook & State Management | `dual-surface-pref.ts`, `useDualSurfaceLayout.ts`, unit tests | none | DONE |
|
||||||
|
| M2 | Interactive Divider & Header Preset Controls | `DualSurfaceDivider.tsx`, `useDualSurfaceResize.ts`, `dual-surface-header-controls.tsx`, unit/DOM tests | M1 | DONE |
|
||||||
|
| M3 | Shell Integration, Responsive Degradation & Localization | `Shell.tsx` layout refactoring, responsive fallback, German translations in `messages.po`, preserve test IDs | M2 | DONE |
|
||||||
|
| M4 | E2E Test Suite (Tiers 1-4) & Adversarial Hardening (Tier 5) | Comprehensive E2E test suite execution, 100% pass, forensic integrity audit | M3, E2E Track | IN_PROGRESS |
|
||||||
|
|
||||||
|
## Interface Contracts
|
||||||
|
### `dual-surface-pref.ts`
|
||||||
|
```typescript
|
||||||
|
export type DualSurfaceLayoutMode = "split" | "stream" | "canvas";
|
||||||
|
export type DualSurfaceOrientation = "stream-first" | "canvas-first";
|
||||||
|
|
||||||
|
export interface DualSurfacePreferences {
|
||||||
|
splitRatio: number; // 0.20 to 0.80, default 0.50
|
||||||
|
layoutMode: DualSurfaceLayoutMode;
|
||||||
|
orientation: DualSurfaceOrientation;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function readDualSurfacePreferences(): DualSurfacePreferences;
|
||||||
|
export function writeDualSurfacePreferences(prefs: Partial<DualSurfacePreferences>): void;
|
||||||
|
export function clampSplitRatio(ratio: number): number;
|
||||||
|
```
|
||||||
|
|
||||||
|
### `useDualSurfaceLayout.ts`
|
||||||
|
```typescript
|
||||||
|
export interface DualSurfaceLayoutState {
|
||||||
|
splitRatio: number;
|
||||||
|
layoutMode: DualSurfaceLayoutMode;
|
||||||
|
orientation: DualSurfaceOrientation;
|
||||||
|
isMobile: boolean; // < 768px (md)
|
||||||
|
setSplitRatio: (ratio: number) => void;
|
||||||
|
setLayoutMode: (mode: DualSurfaceLayoutMode) => void;
|
||||||
|
setOrientation: (orientation: DualSurfaceOrientation) => void;
|
||||||
|
toggleOrientation: () => void;
|
||||||
|
resetSplit: () => void; // Snaps to 0.50
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### `DualSurfaceDivider.tsx`
|
||||||
|
```typescript
|
||||||
|
export interface DualSurfaceDividerProps {
|
||||||
|
splitRatio: number;
|
||||||
|
onResize: (newRatio: number) => void;
|
||||||
|
onReset: () => void; // Double-click snap
|
||||||
|
disabled?: boolean;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### `DualSurfaceHeaderControls.tsx`
|
||||||
|
```typescript
|
||||||
|
export interface DualSurfaceHeaderControlsProps {
|
||||||
|
layoutMode: DualSurfaceLayoutMode;
|
||||||
|
orientation: DualSurfaceOrientation;
|
||||||
|
onSelectMode: (mode: DualSurfaceLayoutMode) => void;
|
||||||
|
onToggleOrientation: () => void;
|
||||||
|
disabled?: boolean;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Code Layout
|
||||||
|
- `apps/web/src/lib/dual-surface-pref.ts`: Preference reading, writing, and boundary clamping.
|
||||||
|
- `apps/web/src/lib/dual-surface-pref.test.ts`: Unit tests for storage and mathematical boundaries.
|
||||||
|
- `apps/web/src/hooks/useDualSurfaceLayout.ts`: Layout state and responsive lifecycle management.
|
||||||
|
- `apps/web/src/hooks/useDualSurfaceResize.ts`: Pointer capture and keyboard navigation hook for divider.
|
||||||
|
- `apps/web/src/components/DualSurfaceDivider.tsx`: Accessible interactive divider component.
|
||||||
|
- `apps/web/src/pages/shell/dual-surface-header-controls.tsx`: Quick-switch header presets and swap buttons.
|
||||||
|
- `apps/web/src/pages/Shell.tsx`: Integration into application root layout.
|
||||||
|
- `apps/web/src/locales/de/messages.po`: German translation catalog entries.
|
||||||
|
- `apps/web/src/tests/dual-surface-view.test.tsx`: Component integration and DOM interaction tests.
|
||||||
@ -49,7 +49,7 @@ Full local notes: **[docs/LOCAL-SETUP.md](./docs/LOCAL-SETUP.md)**
|
|||||||
- TypeScript monorepo (pnpm + Turborepo)
|
- TypeScript monorepo (pnpm + Turborepo)
|
||||||
- React 19 / Vite / Tailwind (web), Electron (desktop), Expo (mobile)
|
- React 19 / Vite / Tailwind (web), Electron (desktop), Expo (mobile)
|
||||||
- Hono + oRPC, PostgreSQL + Prisma, Better Auth, Graphile Worker
|
- Hono + oRPC, PostgreSQL + Prisma, Better Auth, Graphile Worker
|
||||||
- Pi model routing, Docker sandboxes, Voice (Kokoro local / BYO keys)
|
- Pi model routing, Docker sandboxes, Voice (Kokoro, local)
|
||||||
|
|
||||||
## Antigravity tools kept on this branch
|
## Antigravity tools kept on this branch
|
||||||
|
|
||||||
@ -68,7 +68,7 @@ docker run -p 8880:8880 ghcr.io/remsky/kokoro-fastapi-cpu:latest
|
|||||||
export KOKORO_BASE_URL=http://127.0.0.1:8880
|
export KOKORO_BASE_URL=http://127.0.0.1:8880
|
||||||
```
|
```
|
||||||
|
|
||||||
Details: [docs/kokoro-voice.md](./docs/kokoro-voice.md)
|
German voices come from a local Piper server (`PIPER_BASE_URL`). Details: [docs/kokoro-voice.md](./docs/kokoro-voice.md)
|
||||||
|
|
||||||
## Tech debt (brand / packages)
|
## Tech debt (brand / packages)
|
||||||
|
|
||||||
|
|||||||
135
TEST_INFRA.md
Normal file
135
TEST_INFRA.md
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
# E2E Test Infrastructure: evOo Voice-First Autonomous Agent OS (Dual Track)
|
||||||
|
|
||||||
|
## 1. Test Philosophy & Principles
|
||||||
|
- **Opaque-Box & Requirement-Driven**: Derived strictly from user specifications in `ORIGINAL_REQUEST.md` (§R1, §R2, §R3) and architecture specifications in `orchestrator_2/PROJECT.md` (§F1–§F12).
|
||||||
|
- **Interface Contract Verification**: Validates concrete contracts for `StreamingVoiceFrame`, `StreamingVoiceClient`, `ActionQueueStep`, `DynamicActionQueue`, and `RoutineNotificationPolicy`.
|
||||||
|
- **Offline & Deterministic Execution**: Zero external cloud or hosted vendor dependencies; uses simulated Web Audio, deterministic PCM frame generators, and pure React 19 testing harnesses.
|
||||||
|
- **Strict Linting & Design System Compliance**: Conforms strictly to `@rakazo/ui-tokens` monochrome tokens, zero arbitrary hex colors, top-level `import type`, and clean Biome formatting.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Test Architecture & Runner Commands
|
||||||
|
|
||||||
|
### Execution Commands
|
||||||
|
1. **Run All E2E OS Test Suites (139 tests)**:
|
||||||
|
```bash
|
||||||
|
pnpm vitest run apps/web/src/tests/e2e-voice-first-foundation.test.ts apps/web/src/tests/e2e-autonomous-agency-queues.test.ts apps/web/src/tests/e2e-minimalist-tactile-ux.test.tsx apps/web/src/tests/e2e-cross-feature-workflows.test.ts
|
||||||
|
```
|
||||||
|
2. **Run Individual Feature Suites**:
|
||||||
|
- Voice Foundation (F1–F4):
|
||||||
|
```bash
|
||||||
|
pnpm vitest run apps/web/src/tests/e2e-voice-first-foundation.test.ts
|
||||||
|
```
|
||||||
|
- Autonomous Agency & Queues (F5–F8):
|
||||||
|
```bash
|
||||||
|
pnpm vitest run apps/web/src/tests/e2e-autonomous-agency-queues.test.ts
|
||||||
|
```
|
||||||
|
- Minimalist Tactile UX & Onboarding (F9–F12):
|
||||||
|
```bash
|
||||||
|
pnpm vitest run apps/web/src/tests/e2e-minimalist-tactile-ux.test.tsx
|
||||||
|
```
|
||||||
|
- Cross-Feature & Real-World Workflows (T3 & T4):
|
||||||
|
```bash
|
||||||
|
pnpm vitest run apps/web/src/tests/e2e-cross-feature-workflows.test.ts
|
||||||
|
```
|
||||||
|
3. **TypeScript Compilation Check**:
|
||||||
|
```bash
|
||||||
|
pnpm --filter @rakazo/web check
|
||||||
|
```
|
||||||
|
4. **Code Quality & Linter**:
|
||||||
|
```bash
|
||||||
|
pnpm biome check apps/web/src/tests/e2e-*.ts* apps/web/src/tests/fixtures/*.ts
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Feature Inventory & Test Coverage Matrix
|
||||||
|
|
||||||
|
| # | Feature | Scope & Requirement | Test File | T1 | T2 | T3 | T4 | Total Tests |
|
||||||
|
|---|---------|---------------------|-----------|:--:|:--:|:--:|:--:|:-----------:|
|
||||||
|
| **F1** | Streaming Audio Wire & Low-Latency VAD | ORIGINAL_REQUEST §R1 (<120ms framing, PCM16, VAD) | `e2e-voice-first-foundation.test.ts` | 5 | 5 | ✓ | ✓ | 10+ |
|
||||||
|
| **F2** | Full-Duplex Audio & Barge-In Interruption | ORIGINAL_REQUEST §R1 (Continuous AEC, barge-in mute) | `e2e-voice-first-foundation.test.ts` | 5 | 5 | ✓ | ✓ | 10+ |
|
||||||
|
| **F3** | Incremental Utterance Streamer & Invite Strip | ORIGINAL_REQUEST §R1 (Clause pipelining, no invite delays) | `e2e-voice-first-foundation.test.ts` | 5 | 5 | ✓ | ✓ | 10+ |
|
||||||
|
| **F4** | Cross-Platform Earcons & Mobile Audio | ORIGINAL_REQUEST §R1 (Tactile Web Audio & mobile cues) | `e2e-voice-first-foundation.test.ts` | 5 | 5 | ✓ | ✓ | 10+ |
|
||||||
|
| **F5** | Optimistic Input Queue & Zero UI Freeze | ORIGINAL_REQUEST §R2 (Non-blocking composer, optimistic FIFO) | `e2e-autonomous-agency-queues.test.ts` | 5 | 5 | ✓ | ✓ | 10+ |
|
||||||
|
| **F6** | Mid-Step Tool Cancellation & Active Steering | ORIGINAL_REQUEST §R2 (Mid-execution abort, active steering) | `e2e-autonomous-agency-queues.test.ts` | 5 | 5 | ✓ | ✓ | 10+ |
|
||||||
|
| **F7** | Outcome-Driven Action Queue State Machine | ORIGINAL_REQUEST §R2 (Dynamic action queue, outcomes) | `e2e-autonomous-agency-queues.test.ts` | 5 | 5 | ✓ | ✓ | 10+ |
|
||||||
|
| **F8** | Quiet Background Routines & Delta Evaluation | ORIGINAL_REQUEST §R2 (on_change delta hash gating, no nag) | `e2e-autonomous-agency-queues.test.ts` | 5 | 5 | ✓ | ✓ | 10+ |
|
||||||
|
| **F9** | Tactile Minimalist Step Execution | ORIGINAL_REQUEST §R3 (Strip chrome bloat, folding step) | `e2e-minimalist-tactile-ux.test.tsx` | 5 | 5 | ✓ | ✓ | 10+ |
|
||||||
|
| **F10** | 30-Second Rapid Onboarding | ORIGINAL_REQUEST §R3 (1-click bootstrap, 3 action sparks) | `e2e-minimalist-tactile-ux.test.tsx` | 5 | 5 | ✓ | ✓ | 10+ |
|
||||||
|
| **F11** | Monochrome Semantic Token Hygiene | ORIGINAL_REQUEST §R3 (@rakazo/ui-tokens, zero rogue hex) | `e2e-minimalist-tactile-ux.test.tsx` | 5 | 5 | ✓ | ✓ | 10+ |
|
||||||
|
| **F12** | Living Canvas Progressive Disclosure | ORIGINAL_REQUEST §R3 (Dynamic canvas, preserve test IDs) | `e2e-minimalist-tactile-ux.test.tsx` | 5 | 5 | ✓ | ✓ | 10+ |
|
||||||
|
| **T3** | Cross-Feature Interactions | Pairwise combinations across R1, R2, R3 | `e2e-cross-feature-workflows.test.ts` | - | - | 12 | - | 12 |
|
||||||
|
| **T4** | Real-World Application Workflows | Realistic end-to-end operational scenarios | `e2e-cross-feature-workflows.test.ts` | - | - | - | 7 | 7 |
|
||||||
|
| **Total** | **All Features F1–F12 across T1–T4** | **100% Comprehensive Coverage** | **4 Suites + 3 Fixtures** | **60** | **60** | **12** | **7** | **139** |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Test Tiers Breakdown
|
||||||
|
|
||||||
|
### Tier 1: Feature Coverage (60 Tests: 5 Tests × 12 Features)
|
||||||
|
- **F1**: 80ms PCM16 frame chunking (1280 samples at 16kHz); VAD normalized RMS energy; <120ms cutoff threshold eliminating 850ms silence floor; monotonic timestamp increment; streaming client transcript & playback chunk callbacks.
|
||||||
|
- **F2**: Continuous microphone capture with acoustic echo cancellation (`echoCancellation: true`); user speech energy detection while bot speaking triggers barge-in; immediate playback audio mute; backend abort propagation (`abort('barge_in')`); state transition from speaking to listening.
|
||||||
|
- **F3**: Incremental clause segmentation in 4–6 word chunks; time-to-first-sound pipelining (<300ms TTFS target); stripping robotic invite prompts ("What would you like to do next?"); code fence description transformation per `speakable()`; natural punctuation boundary clause splits.
|
||||||
|
- **F4**: Core tactile earcons (`listening-start`, `listening-stop`, `speaking-start`, `speaking-done`); Web Audio procedural oscillator profiles; master audio bus ducking (0.28 ducking ratio); cross-platform mobile earcon mapping; state machine audio cue triggers (`interact`, `attention`).
|
||||||
|
- **F5**: Submitting message while another in-flight enqueues without UI freeze; temporary optimistic ID assignment (`temp-...`) and timestamp; composer text clearing immediately; Stop button unblocked and clickable; sequential FIFO drainage.
|
||||||
|
- **F6**: Ingesting steering during active tool call dispatches mid-step cancellation signal; tool abort controller receives `abort('steering_redirection')`; aborted tool output discarded from history; agent pivots immediately to steering intent; steering message marked claimed.
|
||||||
|
- **F7**: `DynamicActionQueue` initialization; multi-step prompt synthesis into `ActionQueueStep` items; queue advancement starts pending step with `startedAt`; completing step sets `completedAt` and advances; `cancelActive()` marks step cancelled.
|
||||||
|
- **F8**: `RoutineNotificationPolicy` with `"on_change"` evaluates output delta hash; identical output hash suppresses notification (quiet run); distinct output hash triggers notification; policy `"always"` notifies unconditionally; policy `"never"` suppresses all notifications.
|
||||||
|
- **F9**: Minimalist folding step button renders with `data-testid="run-steps"`; displays clean `# step` counter without progress track bloat; clicking fold button toggles `aria-expanded`; step tool name and summary rendered when expanded; keyboard Enter/Space toggles fold.
|
||||||
|
- **F10**: 1-click bootstrap creates initial agent workspace without 11-step questionnaire; auto-detects system timezone via `Intl.DateTimeFormat`; auto-detects system language via `navigator.language`; surfaces 3 tactile starter sparks; selecting spark starts session (<30s value).
|
||||||
|
- **F11**: Theme tokens definition from `@rakazo/ui-tokens`; zero hardcoded product hex colors; zero arbitrary Tailwind brackets `[var(--...)]`; accessible focus rings (`focus-visible:ring-ring`); status indicators use semantic tokens (`text-destructive`, `text-muted-foreground`).
|
||||||
|
- **F12**: Living Canvas pane renders with `data-testid="side-panel"`; Mission Stream renders with `data-testid="mission-stream-pane"` and `data-testid="transcript"`; preserves critical test IDs (`computer-preview`, `dual-surface-divider`, `dual-surface-header-controls`); progressive disclosure on artifact creation; tab switching between Files, Sandbox, Browser.
|
||||||
|
|
||||||
|
### Tier 2: Boundary & Corner Cases (60 Tests: 5 Tests × 12 Features)
|
||||||
|
- **F1**: Empty audio buffer (`Int16Array(0)`) handled with RMS 0 and no NaN errors; pure silence buffer maintains energy 0 and triggers silence cutoff without hang; clipping values clamped to Int16 limits (-32768 to 32767); out-of-order timestamp detection and jitter buffer tolerance; network drop triggers client reconnect and state preservation.
|
||||||
|
- **F2**: Microphone permission denied handled with graceful error state; interruption when audio playback is already idle is safe no-op; micro-burst transients (<50ms noise pop) filtered by VAD debounce; rapid alternating speaker turns (ping-pong barge-in); concurrent interrupt and incoming playback chunk race condition resolves in favor of user interrupt.
|
||||||
|
- **F3**: Single-word answers streamed without minimum character delay stalling; code-only block output sanitized without synthetic audio artifacts; emoji-only message stream strips emojis and does not stall TTS engine; micro-fragment token arrivals buffered into complete words; unexpected LLM stream termination cleanly finalizes active clause.
|
||||||
|
- **F4**: Rapid consecutive sound cue triggers (<10ms) queue smoothly without audio glitches; muted/silent mode suppresses oscillator output while executing state transition; AudioContext suspended state auto-resumes on first user gesture; unknown sound cue name falls back gracefully without throwing exceptions; cross-platform volume normalization preserves peak headroom.
|
||||||
|
- **F5**: Rapid typing burst (10 rapid inputs within 100ms) enqueues all 10 without dropping; bounded queue size enforces max pending limit with graceful error; network failure while message in flight triggers failed state with retry option without losing text; empty/whitespace-only messages rejected before enqueueing; duplicate messages submitted in rapid succession deduplicated if flagged.
|
||||||
|
- **F6**: Race condition: tool finishes at the exact moment abort signal fires resolves cleanly; tool execution timeout triggers automated mid-step cancellation with `timeout` reason; multiple rapid contradictory steering messages resolves to most recent intent; tool that throws exception upon abort caught gracefully without crashing runtime; external system side-effect fence prevents state pollution on aborted tools.
|
||||||
|
- **F7**: Synthesizing empty or meaningless input results in clean no-op; `advance()` called on an empty queue or completed queue is safe no-op; failed step status prevents automatic advance unless retry policy allows; step status state machine strictly validates allowable transitions; queue serialization and deserialization preserves complete step fidelity.
|
||||||
|
- **F8**: First execution (`prevOutputHash === ''`) with `"on_change"` triggers initial notification; policy `"on_failure"` triggers notification only when routine status is `"failed"`; whitespace/formatting variations evaluated cleanly against normalized hash; high frequency routine executions do not spam notifications if output remains constant; routine execution failure self-heals without crashing scheduler.
|
||||||
|
- **F9**: Empty steps list renders cleanly without error or broken layout; extreme step count (50+ tool calls) remains folded by default without layout overflow; failed step renders clean status badge without noisy error stacks unless expanded; absence of chrome bloat (removal of animated tracks, live timers `· 1:24`, uppercase callouts `AUFGABE`); rapid folding/unfolding toggles do not oscillate or corrupt DOM state.
|
||||||
|
- **F10**: Missing or invalid timezone fallback safely resolves to UTC without throwing error; missing or invalid locale fallback safely resolves to `en`; re-entering onboarding while workspace already exists redirects to active space; starter spark with special characters sanitized safely; offline onboarding gracefully saves initial draft locally without blocking UI.
|
||||||
|
- **F11**: Semantic contrast in monochrome mode preserves readable text tokens; disabled component states apply semantic opacity `disabled:opacity-50` rather than arbitrary grey; high contrast mode support: borders preserve visibility via semantic `border-border`; dynamic theme switching updates token class mappings deterministically; interactive hover states use semantic hover tokens (`hover:bg-accent`).
|
||||||
|
- **F12**: Closing Living Canvas collapses aside pane without unmounting workspace state; rapid switching between canvas tabs maintains individual surface state; resizing viewport below `md` breakpoint gracefully transitions Dual-Surface to responsive layout; expanding viewport back above `md` restores Dual-Surface split without reset; concurrent artifact creation from multiple sub-agents surfaces items progressively without flicker.
|
||||||
|
|
||||||
|
### Tier 3: Cross-Feature Interactions (12 Tests: Pairwise Coverage)
|
||||||
|
- **Pair 1**: Voice barge-in interrupt while action queue is actively running step (aborts active step, pauses execution, pivots queue).
|
||||||
|
- **Pair 2**: Optimistic queue burst while streaming audio TTS is playing (synthesizes follow-ups without audio dropout).
|
||||||
|
- **Pair 3**: Mid-step tool cancellation triggered via voice interruption event (voice audio energy spike propagates to in-flight tool abort controller).
|
||||||
|
- **Pair 4**: Quiet background routine fires during active voice session (audio ducking remains unaffected; output unchanged suppresses chat/audio notification).
|
||||||
|
- **Pair 5**: 30-second rapid onboarding directly initiates full-duplex voice stage (first-time user goes from 1-click bootstrap straight to hands-free voice dialogue).
|
||||||
|
- **Pair 6**: Action queue synthesis dynamically surfaces Living Canvas for artifact inspection (multi-step queue opens canvas on artifact creation).
|
||||||
|
- **Pair 7**: Tactile folding step reflects mid-step tool cancellation without UI glitch (shows `[cancelled]` step folded cleanly).
|
||||||
|
- **Pair 8**: Voice streaming client reconnects after temporary network drop while action queue advances to next step.
|
||||||
|
- **Pair 9**: Audio earcon triggers on optimistic queue drain and background delta detection (`interact` on drain, `attention` on delta).
|
||||||
|
- **Pair 10**: Living Canvas orientation swap while action queue runs background steps (DOM flex order reverses while steps advance uninterrupted).
|
||||||
|
- **Pair 11**: Voice interrupt resets incremental utterance clause pipelining (discards queued clauses immediately).
|
||||||
|
- **Pair 12**: Delta routine evaluation suppresses notification while user is typing in optimistic composer (zero disruption to active composer focus).
|
||||||
|
|
||||||
|
### Tier 4: Real-World Application Scenarios (7 Comprehensive Workflows)
|
||||||
|
- **Scenario 1: Ambient Hands-Free Meeting & Live Action Queue**: User dictates three multi-step action items in voice mode -> VAD frames audio -> parsed into Dynamic Action Queue -> steps advance deterministically -> earcon signals completion.
|
||||||
|
- **Scenario 2: Conversational Barge-In & Mid-Step Tool Cancellation**: Agent executing long command -> user speaks barge-in redirect -> playback instantly muted -> in-flight tool aborted -> queue pivoted to new intent.
|
||||||
|
- **Scenario 3: High-Frequency Multi-Prompt Burst & Optimistic Ordering**: User types 3 follow-up messages rapidly during elevated latency -> optimistic queue renders all 3 immediately with temporary IDs -> requests drain in order -> zero UI freeze.
|
||||||
|
- **Scenario 4: Quiet Background Routine with Delta Detection & No Nagging**: Scheduled routine runs periodic cycles -> hash unchanged -> zero notifications -> on cycle with detected anomaly, delta triggers notification -> user alerted only when critical.
|
||||||
|
- **Scenario 5: Rapid 30-Second Zero-Questionnaire Onboarding to Live Execution**: New user lands -> system auto-detects timezone and locale -> user clicks starter spark "Research competitor architecture" -> workspace boots instantly -> agent begins execution within 30 seconds.
|
||||||
|
- **Scenario 6: Tactile Dual-Surface Progressive Disclosure & Step Folding Workflow**: User asks agent to inspect repo -> agent executes 12 tool steps -> steps fold into clean `# steps` button -> agent writes file -> Living Canvas smoothly unfolds without modal occlusion.
|
||||||
|
- **Scenario 7: Network Loss & Reconnection Resilience during Full-Duplex Voice Session**: Voice stream active -> network packet loss occurs -> streaming client detects connection drop -> buffers outgoing frames -> reconnects -> flushes buffer and resumes conversation without data loss.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Artifact Index & Directory Layout
|
||||||
|
|
||||||
|
```
|
||||||
|
apps/web/src/tests/
|
||||||
|
├── e2e-voice-first-foundation.test.ts # 40 tests (Features F1-F4, Tiers 1-2)
|
||||||
|
├── e2e-autonomous-agency-queues.test.ts # 40 tests (Features F5-F8, Tiers 1-2)
|
||||||
|
├── e2e-minimalist-tactile-ux.test.tsx # 40 tests (Features F9-F12, Tiers 1-2)
|
||||||
|
├── e2e-cross-feature-workflows.test.ts # 19 tests (Tier 3 Pairwise & Tier 4 Scenarios)
|
||||||
|
└── fixtures/
|
||||||
|
├── voice-contracts.ts # F1-F4 contracts & reference VAD/framing models
|
||||||
|
├── agency-contracts.ts # F5-F8 contracts, Action Queue & Routine policy models
|
||||||
|
└── ux-contracts.ts # F9-F12 contracts, environment detection & starter sparks
|
||||||
|
```
|
||||||
123
TEST_READY.md
Normal file
123
TEST_READY.md
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
# TEST_READY: evOo Voice-First Autonomous Agent Operating System
|
||||||
|
|
||||||
|
## Status
|
||||||
|
- **Test Suite Status**: READY
|
||||||
|
- **Author**: `test_writer_r2_1` (Dual-Track E2E Testing Specialist)
|
||||||
|
- **Timestamp**: 2026-10-12T13:05:00Z
|
||||||
|
- **Target Milestones**: M1, M2, M3, M4, M5 (Features F1–F12 across Requirements R1, R2, R3)
|
||||||
|
- **Test Framework**: Vitest 4.1.11 with `// @vitest-environment jsdom` & Pure React 19 harnesses
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Authored Test Suites Summary
|
||||||
|
|
||||||
|
| # | Test File Path | Scope & Feature Focus | Tiers Covered | Total Test Cases | Passing Baseline |
|
||||||
|
|---|----------------|-----------------------|:-------------:|:----------------:|:----------------:|
|
||||||
|
| 1 | `apps/web/src/tests/e2e-voice-first-foundation.test.ts` | Streaming Audio Wire (<120ms framing, PCM16, VAD), Full-Duplex Audio & Barge-In Interruption, Incremental Utterance Streamer (<300ms TTFS, invite strip), Tactile Monochrome Earcons | Tier 1, Tier 2 (F1–F4) | 40 | ✅ 40 / 40 Passed |
|
||||||
|
| 2 | `apps/web/src/tests/e2e-autonomous-agency-queues.test.ts` | Optimistic Input Queue (Zero UI Freeze, unblocked stop), Mid-Step Tool Cancellation & Active Steering, Dynamic Action Queue State Machine, Quiet Background Routines & Delta Evaluation (`on_change`) | Tier 1, Tier 2 (F5–F8) | 40 | ✅ 40 / 40 Passed |
|
||||||
|
| 3 | `apps/web/src/tests/e2e-minimalist-tactile-ux.test.tsx` | Tactile Minimalist Step Execution (strip JobProgressCard bloat, folding `# step`), 30-Second Rapid Onboarding (zero questionnaires, 3 action sparks), Monochrome Token Hygiene (@rakazo/ui-tokens), Living Canvas Progressive Disclosure | Tier 1, Tier 2 (F9–F12) | 40 | ✅ 40 / 40 Passed |
|
||||||
|
| 4 | `apps/web/src/tests/e2e-cross-feature-workflows.test.ts` | 12 Pairwise Cross-Feature Interactions & 7 Realistic End-to-End Workflows (Hands-free meeting, voice barge-in tool abort, high-frequency prompt burst, quiet background routines, 30s onboarding, progressive disclosure, voice network resilience) | Tier 3, Tier 4 | 19 | ✅ 19 / 19 Passed |
|
||||||
|
| **Total** | **4 Comprehensive Test Suites + 3 Fixtures** | **Full Feature Inventory (F1 to F12) Coverage** | **Tiers 1–4** | **139 tests** | **✅ 139 / 139 (100% Pass)** |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Requirement & Feature Coverage Matrix
|
||||||
|
|
||||||
|
| Feature | Source Requirement | Primary Test File | Tiers | Pass / Fail |
|
||||||
|
|---------|-------------------|-------------------|:-----:|:-----------:|
|
||||||
|
| **F1: Streaming Audio Wire & Low-Latency VAD** | ORIGINAL_REQUEST §R1 | `e2e-voice-first-foundation.test.ts` | T1, T2, T3, T4 | ✅ PASS (10/10) |
|
||||||
|
| **F2: Full-Duplex Audio & Barge-In Interruption** | ORIGINAL_REQUEST §R1 | `e2e-voice-first-foundation.test.ts` | T1, T2, T3, T4 | ✅ PASS (10/10) |
|
||||||
|
| **F3: Incremental Utterance Streamer & Invite Strip** | ORIGINAL_REQUEST §R1 | `e2e-voice-first-foundation.test.ts` | T1, T2, T3, T4 | ✅ PASS (10/10) |
|
||||||
|
| **F4: Cross-Platform Earcons & Mobile Audio** | ORIGINAL_REQUEST §R1 | `e2e-voice-first-foundation.test.ts` | T1, T2, T3, T4 | ✅ PASS (10/10) |
|
||||||
|
| **F5: Optimistic Input Queue & Zero UI Freeze** | ORIGINAL_REQUEST §R2 | `e2e-autonomous-agency-queues.test.ts` | T1, T2, T3, T4 | ✅ PASS (10/10) |
|
||||||
|
| **F6: Mid-Step Tool Cancellation & Active Steering** | ORIGINAL_REQUEST §R2 | `e2e-autonomous-agency-queues.test.ts` | T1, T2, T3, T4 | ✅ PASS (10/10) |
|
||||||
|
| **F7: Outcome-Driven Action Queue State Machine** | ORIGINAL_REQUEST §R2 | `e2e-autonomous-agency-queues.test.ts` | T1, T2, T3, T4 | ✅ PASS (10/10) |
|
||||||
|
| **F8: Quiet Background Routines & Delta Evaluation** | ORIGINAL_REQUEST §R2 | `e2e-autonomous-agency-queues.test.ts` | T1, T2, T3, T4 | ✅ PASS (10/10) |
|
||||||
|
| **F9: Tactile Minimalist Step Execution** | ORIGINAL_REQUEST §R3 | `e2e-minimalist-tactile-ux.test.tsx` | T1, T2, T3, T4 | ✅ PASS (10/10) |
|
||||||
|
| **F10: 30-Second Rapid Onboarding** | ORIGINAL_REQUEST §R3 | `e2e-minimalist-tactile-ux.test.tsx` | T1, T2, T3, T4 | ✅ PASS (10/10) |
|
||||||
|
| **F11: Monochrome Semantic Token Hygiene** | ORIGINAL_REQUEST §R3 | `e2e-minimalist-tactile-ux.test.tsx` | T1, T2, T3, T4 | ✅ PASS (10/10) |
|
||||||
|
| **F12: Living Canvas Progressive Disclosure** | ORIGINAL_REQUEST §R3 | `e2e-minimalist-tactile-ux.test.tsx` | T1, T2, T3, T4 | ✅ PASS (10/10) |
|
||||||
|
| **T3: Pairwise Cross-Feature Interactions** | Cross-Module Interactions | `e2e-cross-feature-workflows.test.ts` | Tier 3 | ✅ PASS (12/12) |
|
||||||
|
| **T4: Real-World Application Scenarios** | Operational Workflows | `e2e-cross-feature-workflows.test.ts` | Tier 4 | ✅ PASS (7/7) |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Tier Breakdown & Test Cases
|
||||||
|
|
||||||
|
### Tier 1: Feature Coverage (60 Tests)
|
||||||
|
- **F1**: 80ms PCM16 frame chunking (1280 samples at 16kHz); VAD normalized RMS energy; <120ms cutoff threshold eliminating 850ms silence floor; monotonic timestamp increment; streaming client transcript & playback chunk callbacks.
|
||||||
|
- **F2**: Continuous microphone capture with acoustic echo cancellation (`echoCancellation: true`); user speech energy detection while bot speaking triggers barge-in; immediate playback audio mute; backend abort propagation (`abort('barge_in')`); state transition from speaking to listening.
|
||||||
|
- **F3**: Incremental clause segmentation in 4–6 word chunks; time-to-first-sound pipelining (<300ms TTFS target); stripping robotic invite prompts ("What would you like to do next?"); code fence description transformation per `speakable()`; natural punctuation boundary clause splits.
|
||||||
|
- **F4**: Core tactile earcons (`listening-start`, `listening-stop`, `speaking-start`, `speaking-done`); Web Audio procedural oscillator profiles; master audio bus ducking (0.28 ducking ratio); cross-platform mobile earcon mapping; state machine audio cue triggers (`interact`, `attention`).
|
||||||
|
- **F5**: Submitting message while another in-flight enqueues without UI freeze; temporary optimistic ID assignment (`temp-...`) and timestamp; composer text clearing immediately; Stop button unblocked and clickable; sequential FIFO drainage.
|
||||||
|
- **F6**: Ingesting steering during active tool call dispatches mid-step cancellation signal; tool abort controller receives `abort('steering_redirection')`; aborted tool output discarded from history; agent pivots immediately to steering intent; steering message marked claimed.
|
||||||
|
- **F7**: `DynamicActionQueue` initialization; multi-step prompt synthesis into `ActionQueueStep` items; queue advancement starts pending step with `startedAt`; completing step sets `completedAt` and advances; `cancelActive()` marks step cancelled.
|
||||||
|
- **F8**: `RoutineNotificationPolicy` with `"on_change"` evaluates output delta hash; identical output hash suppresses notification (quiet run); distinct output hash triggers notification; policy `"always"` notifies unconditionally; policy `"never"` suppresses all notifications.
|
||||||
|
- **F9**: Minimalist folding step button renders with `data-testid="run-steps"`; displays clean `# step` counter without progress track bloat; clicking fold button toggles `aria-expanded`; step tool name and summary rendered when expanded; keyboard Enter/Space toggles fold.
|
||||||
|
- **F10**: 1-click bootstrap creates initial agent workspace without 11-step questionnaire; auto-detects system timezone via `Intl.DateTimeFormat`; auto-detects system language via `navigator.language`; surfaces 3 tactile starter sparks; selecting spark starts session (<30s value).
|
||||||
|
- **F11**: Theme tokens definition from `@rakazo/ui-tokens`; zero hardcoded product hex colors; zero arbitrary Tailwind brackets `[var(--...)]`; accessible focus rings (`focus-visible:ring-ring`); status indicators use semantic tokens (`text-destructive`, `text-muted-foreground`).
|
||||||
|
- **F12**: Living Canvas pane renders with `data-testid="side-panel"`; Mission Stream renders with `data-testid="mission-stream-pane"` and `data-testid="transcript"`; preserves critical test IDs (`computer-preview`, `dual-surface-divider`, `dual-surface-header-controls`); progressive disclosure on artifact creation; tab switching between Files, Sandbox, Browser.
|
||||||
|
|
||||||
|
### Tier 2: Boundary & Corner Cases (60 Tests)
|
||||||
|
- **F1**: Empty audio buffer (`Int16Array(0)`) handled with RMS 0 and no NaN errors; pure silence buffer maintains energy 0 and triggers silence cutoff without hang; clipping values clamped to Int16 limits (-32768 to 32767); out-of-order timestamp detection and jitter buffer tolerance; network drop triggers client reconnect and state preservation.
|
||||||
|
- **F2**: Microphone permission denied handled with graceful error state; interruption when audio playback is already idle is safe no-op; micro-burst transients (<50ms noise pop) filtered by VAD debounce; rapid alternating speaker turns (ping-pong barge-in); concurrent interrupt and incoming playback chunk race condition resolves in favor of user interrupt.
|
||||||
|
- **F3**: Single-word answers streamed without minimum character delay stalling; code-only block output sanitized without synthetic audio artifacts; emoji-only message stream strips emojis and does not stall TTS engine; micro-fragment token arrivals buffered into complete words; unexpected LLM stream termination cleanly finalizes active clause.
|
||||||
|
- **F4**: Rapid consecutive sound cue triggers (<10ms) queue smoothly without audio glitches; muted/silent mode suppresses oscillator output while executing state transition; AudioContext suspended state auto-resumes on first user gesture; unknown sound cue name falls back gracefully without throwing exceptions; cross-platform volume normalization preserves peak headroom.
|
||||||
|
- **F5**: Rapid typing burst (10 rapid inputs within 100ms) enqueues all 10 without dropping; bounded queue size enforces max pending limit with graceful error; network failure while message in flight triggers failed state with retry option without losing text; empty/whitespace-only messages rejected before enqueueing; duplicate messages submitted in rapid succession deduplicated if flagged.
|
||||||
|
- **F6**: Race condition: tool finishes at the exact moment abort signal fires resolves cleanly; tool execution timeout triggers automated mid-step cancellation with `timeout` reason; multiple rapid contradictory steering messages resolves to most recent intent; tool that throws exception upon abort caught gracefully without crashing runtime; external system side-effect fence prevents state pollution on aborted tools.
|
||||||
|
- **F7**: Synthesizing empty or meaningless input results in clean no-op; `advance()` called on an empty queue or completed queue is safe no-op; failed step status prevents automatic advance unless retry policy allows; step status state machine strictly validates allowable transitions; queue serialization and deserialization preserves complete step fidelity.
|
||||||
|
- **F8**: First execution (`prevOutputHash === ''`) with `"on_change"` triggers initial notification; policy `"on_failure"` triggers notification only when routine status is `"failed"`; whitespace/formatting variations evaluated cleanly against normalized hash; high frequency routine executions do not spam notifications if output remains constant; routine execution failure self-heals without crashing scheduler.
|
||||||
|
- **F9**: Empty steps list renders cleanly without error or broken layout; extreme step count (50+ tool calls) remains folded by default without layout overflow; failed step renders clean status badge without noisy error stacks unless expanded; absence of chrome bloat (removal of animated tracks, live timers `· 1:24`, uppercase callouts `AUFGABE`); rapid folding/unfolding toggles do not oscillate or corrupt DOM state.
|
||||||
|
- **F10**: Missing or invalid timezone fallback safely resolves to UTC without throwing error; missing or invalid locale fallback safely resolves to `en`; re-entering onboarding while workspace already exists redirects to active space; starter spark with special characters sanitized safely; offline onboarding gracefully saves initial draft locally without blocking UI.
|
||||||
|
- **F11**: Semantic contrast in monochrome mode preserves readable text tokens; disabled component states apply semantic opacity `disabled:opacity-50` rather than arbitrary grey; high contrast mode support: borders preserve visibility via semantic `border-border`; dynamic theme switching updates token class mappings deterministically; interactive hover states use semantic hover tokens (`hover:bg-accent`).
|
||||||
|
- **F12**: Closing Living Canvas collapses aside pane without unmounting workspace state; rapid switching between canvas tabs maintains individual surface state; resizing viewport below `md` breakpoint gracefully transitions Dual-Surface to responsive layout; expanding viewport back above `md` restores Dual-Surface split without reset; concurrent artifact creation from multiple sub-agents surfaces items progressively without flicker.
|
||||||
|
|
||||||
|
### Tier 3: Cross-Feature Interactions (12 Tests)
|
||||||
|
- Pair 1: Voice barge-in interrupt while action queue is actively running step.
|
||||||
|
- Pair 2: Optimistic queue burst while streaming audio TTS is playing.
|
||||||
|
- Pair 3: Mid-step tool cancellation triggered via voice interruption event.
|
||||||
|
- Pair 4: Quiet background routine fires during active voice session.
|
||||||
|
- Pair 5: 30-second rapid onboarding directly initiates full-duplex voice stage.
|
||||||
|
- Pair 6: Action queue synthesis dynamically surfaces Living Canvas for artifact inspection.
|
||||||
|
- Pair 7: Tactile folding step reflects mid-step tool cancellation without UI glitch.
|
||||||
|
- Pair 8: Voice streaming client reconnects after temporary network drop while action queue advances.
|
||||||
|
- Pair 9: Audio earcon triggers on optimistic queue drain and background delta detection.
|
||||||
|
- Pair 10: Living Canvas orientation swap while action queue runs background steps.
|
||||||
|
- Pair 11: Voice interrupt resets incremental utterance clause pipelining.
|
||||||
|
- Pair 12: Delta routine evaluation suppresses notification while user is typing in optimistic composer.
|
||||||
|
|
||||||
|
### Tier 4: Real-World Application Scenarios (7 Comprehensive Workflows)
|
||||||
|
- Scenario 1: Ambient Hands-Free Meeting & Live Action Queue.
|
||||||
|
- Scenario 2: Conversational Barge-In & Mid-Step Tool Cancellation.
|
||||||
|
- Scenario 3: High-Frequency Multi-Prompt Burst & Optimistic Ordering.
|
||||||
|
- Scenario 4: Quiet Background Routine with Delta Detection & No Nagging.
|
||||||
|
- Scenario 5: Rapid 30-Second Zero-Questionnaire Onboarding to Live Execution.
|
||||||
|
- Scenario 6: Tactile Dual-Surface Progressive Disclosure & Step Folding Workflow.
|
||||||
|
- Scenario 7: Network Loss & Reconnection Resilience during Full-Duplex Voice Session.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Verification Method
|
||||||
|
|
||||||
|
Run the following command in the repository root to execute the complete E2E test suite:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm vitest run apps/web/src/tests/e2e-voice-first-foundation.test.ts apps/web/src/tests/e2e-autonomous-agency-queues.test.ts apps/web/src/tests/e2e-minimalist-tactile-ux.test.tsx apps/web/src/tests/e2e-cross-feature-workflows.test.ts
|
||||||
|
```
|
||||||
|
|
||||||
|
Output:
|
||||||
|
```
|
||||||
|
Test Files 4 passed (4)
|
||||||
|
Tests 139 passed (139)
|
||||||
|
Duration ~3.0s
|
||||||
|
```
|
||||||
|
|
||||||
|
TypeScript Check:
|
||||||
|
```bash
|
||||||
|
pnpm --filter @rakazo/web check
|
||||||
|
```
|
||||||
|
|
||||||
|
Biome Lint & Format Check:
|
||||||
|
```bash
|
||||||
|
pnpm biome check apps/web/src/tests/e2e-*.ts* apps/web/src/tests/fixtures/*.ts
|
||||||
|
```
|
||||||
@ -60,6 +60,8 @@ One bot has one continuous visible thread because continuity is part of its iden
|
|||||||
Team Computers share a persisted browser identity and isolate simultaneous live desktops when supported because shared login state and safe concurrency are both required.
|
Team Computers share a persisted browser identity and isolate simultaneous live desktops when supported because shared login state and safe concurrency are both required.
|
||||||
Private Computers isolate the entire working home because some trust boundaries must be stronger than collaboration convenience.
|
Private Computers isolate the entire working home because some trust boundaries must be stronger than collaboration convenience.
|
||||||
Connections belong to the user or space rather than requiring repetitive per-bot setup because bots differ through role and state, not account plumbing.
|
Connections belong to the user or space rather than requiring repetitive per-bot setup because bots differ through role and state, not account plumbing.
|
||||||
|
Bots answer with cards from a fixed catalog of sections rather than generated interface because the backend must validate every card and apply every tap, and a small catalog stays calm and consistent on every surface.
|
||||||
|
An autopilot works on an order with no time or cost limit, but pauses when runs stop reporting progress and always shows when it started and ended, because long unattended work must stay visible and must never spin silently.
|
||||||
|
|
||||||
## Scope
|
## Scope
|
||||||
|
|
||||||
|
|||||||
@ -26,9 +26,13 @@ function savedSkill(name: string, source = "user") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setup(rows: ReturnType<typeof savedSkill>[] = []) {
|
function setup(rows: ReturnType<typeof savedSkill>[] = []) {
|
||||||
type Where = { id?: string; spaceId: string; userId: string; source?: string };
|
type Where = { id?: string; spaceId?: string; userId: string; source?: string; name?: unknown };
|
||||||
const matches = (row: ReturnType<typeof savedSkill>, where: Where) =>
|
const matches = (row: ReturnType<typeof savedSkill>, where: Where) =>
|
||||||
Object.entries(where).every(([key, value]) => row[key as keyof typeof row] === value);
|
Object.entries(where).every(([key, value]) =>
|
||||||
|
key === "name" && typeof value === "object" && value && "equals" in value
|
||||||
|
? row.name.toLowerCase() === String(value.equals).toLowerCase()
|
||||||
|
: key === "NOT" || row[key as keyof typeof row] === value,
|
||||||
|
);
|
||||||
const agentSkill = {
|
const agentSkill = {
|
||||||
findMany: vi.fn(async ({ where }: { where: Where }) =>
|
findMany: vi.fn(async ({ where }: { where: Where }) =>
|
||||||
rows.filter((row) => matches(row, where)),
|
rows.filter((row) => matches(row, where)),
|
||||||
@ -55,15 +59,19 @@ function setup(rows: ReturnType<typeof savedSkill>[] = []) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Only the entries competing for the name "Interrogate"; other builtins are irrelevant here. */
|
||||||
|
const interrogate = <T extends { name: string }>(skills: T[]) =>
|
||||||
|
skills.filter((skill) => skill.name.trim().toLowerCase() === "interrogate");
|
||||||
|
|
||||||
describe("built-in skill precedence in the API", () => {
|
describe("built-in skill precedence in the API", () => {
|
||||||
it.each(["interrogate", " Interrogate "])(
|
it.each(["interrogate", " Interrogate "])(
|
||||||
"keeps %j listed, readable and mutable",
|
"keeps %j listed, readable and mutable",
|
||||||
async (name) => {
|
async (name) => {
|
||||||
const { service } = setup([savedSkill(name)]);
|
const { service } = setup([savedSkill(name)]);
|
||||||
await expect(service.list(actor)).resolves.toEqual([
|
expect(interrogate(await service.list(actor))).toEqual([
|
||||||
expect.objectContaining({ id: "saved-1", name, readOnly: false }),
|
expect.objectContaining({ id: "saved-1", name, readOnly: false }),
|
||||||
]);
|
]);
|
||||||
await expect(service.listWithContent(actor)).resolves.toEqual([
|
expect(interrogate(await service.listWithContent(actor))).toEqual([
|
||||||
expect.objectContaining({ id: "saved-1", content: expect.stringContaining("Saved steps") }),
|
expect.objectContaining({ id: "saved-1", content: expect.stringContaining("Saved steps") }),
|
||||||
]);
|
]);
|
||||||
await expect(service.get(actor, { name: " INTERROGATE " })).resolves.toMatchObject({
|
await expect(service.get(actor, { name: " INTERROGATE " })).resolves.toMatchObject({
|
||||||
@ -97,7 +105,23 @@ describe("built-in skill precedence in the API", () => {
|
|||||||
expect(agentSkill.deleteMany).not.toHaveBeenCalled();
|
expect(agentSkill.deleteMany).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it.each([{ spaceId: "other-space" }, { userId: "other-user" }])(
|
it("lists the user's skills from other spaces too, the current space winning a clash", async () => {
|
||||||
|
const { service } = setup([
|
||||||
|
{ ...savedSkill("Launch"), id: "home-1", spaceId: "home" },
|
||||||
|
{ ...savedSkill("launch"), id: "here-1" },
|
||||||
|
{ ...savedSkill("Weekly review"), id: "home-2", spaceId: "home" },
|
||||||
|
]);
|
||||||
|
const own = (await service.list(actor)).filter((skill) => skill.source === "user");
|
||||||
|
expect(own.map((skill) => skill.id).sort()).toEqual(["here-1", "home-2"]);
|
||||||
|
await expect(service.get(actor, { name: "Weekly review" })).resolves.toMatchObject({
|
||||||
|
id: "home-2",
|
||||||
|
});
|
||||||
|
await expect(
|
||||||
|
service.create(actor, { name: "weekly REVIEW", description: "x" }),
|
||||||
|
).rejects.toThrow("already exists");
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([{ userId: "other-user" }])(
|
||||||
"does not let foreign skills shadow the builtin: %j",
|
"does not let foreign skills shadow the builtin: %j",
|
||||||
async (foreignOwner) => {
|
async (foreignOwner) => {
|
||||||
const { service } = setup([{ ...savedSkill(" Interrogate "), ...foreignOwner }]);
|
const { service } = setup([{ ...savedSkill(" Interrogate "), ...foreignOwner }]);
|
||||||
@ -105,7 +129,7 @@ describe("built-in skill precedence in the API", () => {
|
|||||||
id: "builtin:Interrogate",
|
id: "builtin:Interrogate",
|
||||||
readOnly: true,
|
readOnly: true,
|
||||||
});
|
});
|
||||||
await expect(service.list(actor)).resolves.toEqual([
|
expect(interrogate(await service.list(actor))).toEqual([
|
||||||
expect.objectContaining({ id: "builtin:Interrogate" }),
|
expect.objectContaining({ id: "builtin:Interrogate" }),
|
||||||
]);
|
]);
|
||||||
await expect(service.get(actor, { skillId: "saved-1" })).rejects.toThrow();
|
await expect(service.get(actor, { skillId: "saved-1" })).rejects.toThrow();
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import {
|
|||||||
isSkillReadOnly,
|
isSkillReadOnly,
|
||||||
mergeBuiltinSkills,
|
mergeBuiltinSkills,
|
||||||
parseSkillMd,
|
parseSkillMd,
|
||||||
|
preferSpaceSkills,
|
||||||
type SkillSource,
|
type SkillSource,
|
||||||
} from "@rakazo/core";
|
} from "@rakazo/core";
|
||||||
import { IsolationError, type PrismaClient } from "@rakazo/db";
|
import { IsolationError, type PrismaClient } from "@rakazo/db";
|
||||||
@ -121,11 +122,7 @@ export function resolveSkillContent(input: {
|
|||||||
export function createAgentSkillsService(prisma: PrismaClient) {
|
export function createAgentSkillsService(prisma: PrismaClient) {
|
||||||
async function owned(actor: Actor, skillId: string) {
|
async function owned(actor: Actor, skillId: string) {
|
||||||
const row = await prisma.agentSkill.findFirst({
|
const row = await prisma.agentSkill.findFirst({
|
||||||
where: {
|
where: { id: skillId, userId: actor.userId },
|
||||||
id: skillId,
|
|
||||||
spaceId: actor.spaceId,
|
|
||||||
userId: actor.userId,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
if (!row) throw new IsolationError();
|
if (!row) throw new IsolationError();
|
||||||
return row;
|
return row;
|
||||||
@ -134,20 +131,24 @@ export function createAgentSkillsService(prisma: PrismaClient) {
|
|||||||
return {
|
return {
|
||||||
async list(actor: Actor): Promise<Omit<AgentSkill, "content">[]> {
|
async list(actor: Actor): Promise<Omit<AgentSkill, "content">[]> {
|
||||||
const rows = await prisma.agentSkill.findMany({
|
const rows = await prisma.agentSkill.findMany({
|
||||||
where: { spaceId: actor.spaceId, userId: actor.userId },
|
where: { userId: actor.userId },
|
||||||
orderBy: [{ name: "asc" }, { id: "asc" }],
|
orderBy: [{ name: "asc" }, { id: "asc" }],
|
||||||
});
|
});
|
||||||
return mergeBuiltinSkills(builtinCatalog(), rows.map(mapAgentSkill)).map(
|
return mergeBuiltinSkills(
|
||||||
({ content: _content, ...entry }) => entry,
|
builtinCatalog(),
|
||||||
);
|
preferSpaceSkills(rows, actor.spaceId).map(mapAgentSkill),
|
||||||
|
).map(({ content: _content, ...entry }) => entry);
|
||||||
},
|
},
|
||||||
|
|
||||||
async listWithContent(actor: Actor): Promise<AgentSkill[]> {
|
async listWithContent(actor: Actor): Promise<AgentSkill[]> {
|
||||||
const rows = await prisma.agentSkill.findMany({
|
const rows = await prisma.agentSkill.findMany({
|
||||||
where: { spaceId: actor.spaceId, userId: actor.userId },
|
where: { userId: actor.userId },
|
||||||
orderBy: [{ name: "asc" }, { id: "asc" }],
|
orderBy: [{ name: "asc" }, { id: "asc" }],
|
||||||
});
|
});
|
||||||
return mergeBuiltinSkills(builtinCatalog(), rows.map(mapAgentSkill));
|
return mergeBuiltinSkills(
|
||||||
|
builtinCatalog(),
|
||||||
|
preferSpaceSkills(rows, actor.spaceId).map(mapAgentSkill),
|
||||||
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
async get(actor: Actor, input: { skillId?: string; name?: string }): Promise<AgentSkill> {
|
async get(actor: Actor, input: { skillId?: string; name?: string }): Promise<AgentSkill> {
|
||||||
@ -161,13 +162,10 @@ export function createAgentSkillsService(prisma: PrismaClient) {
|
|||||||
}
|
}
|
||||||
const name = input.name?.trim() ?? "";
|
const name = input.name?.trim() ?? "";
|
||||||
const rows = await prisma.agentSkill.findMany({
|
const rows = await prisma.agentSkill.findMany({
|
||||||
where: {
|
where: { userId: actor.userId },
|
||||||
spaceId: actor.spaceId,
|
|
||||||
userId: actor.userId,
|
|
||||||
},
|
|
||||||
orderBy: [{ name: "asc" }, { id: "asc" }],
|
orderBy: [{ name: "asc" }, { id: "asc" }],
|
||||||
});
|
});
|
||||||
const row = findSkillByName(rows, name);
|
const row = findSkillByName(preferSpaceSkills(rows, actor.spaceId), name);
|
||||||
if (row) return mapAgentSkill(row);
|
if (row) return mapAgentSkill(row);
|
||||||
const builtin = findSkillByName(builtinCatalog(), name);
|
const builtin = findSkillByName(builtinCatalog(), name);
|
||||||
if (!builtin) throw new IsolationError();
|
if (!builtin) throw new IsolationError();
|
||||||
@ -181,7 +179,6 @@ export function createAgentSkillsService(prisma: PrismaClient) {
|
|||||||
const resolved = resolveSkillContent(input);
|
const resolved = resolveSkillContent(input);
|
||||||
const clash = await prisma.agentSkill.findFirst({
|
const clash = await prisma.agentSkill.findFirst({
|
||||||
where: {
|
where: {
|
||||||
spaceId: actor.spaceId,
|
|
||||||
userId: actor.userId,
|
userId: actor.userId,
|
||||||
name: { equals: resolved.name, mode: "insensitive" },
|
name: { equals: resolved.name, mode: "insensitive" },
|
||||||
},
|
},
|
||||||
@ -234,7 +231,6 @@ export function createAgentSkillsService(prisma: PrismaClient) {
|
|||||||
if (!findSkillByName([existing], resolved.name)) {
|
if (!findSkillByName([existing], resolved.name)) {
|
||||||
const clash = await prisma.agentSkill.findFirst({
|
const clash = await prisma.agentSkill.findFirst({
|
||||||
where: {
|
where: {
|
||||||
spaceId: actor.spaceId,
|
|
||||||
userId: actor.userId,
|
userId: actor.userId,
|
||||||
name: { equals: resolved.name, mode: "insensitive" },
|
name: { equals: resolved.name, mode: "insensitive" },
|
||||||
NOT: { id: existing.id },
|
NOT: { id: existing.id },
|
||||||
@ -250,12 +246,7 @@ export function createAgentSkillsService(prisma: PrismaClient) {
|
|||||||
// Mutate only owner-scoped user rows (never builtin/plugin), even if source was tampered.
|
// Mutate only owner-scoped user rows (never builtin/plugin), even if source was tampered.
|
||||||
try {
|
try {
|
||||||
const updated = await prisma.agentSkill.updateMany({
|
const updated = await prisma.agentSkill.updateMany({
|
||||||
where: {
|
where: { id: existing.id, userId: actor.userId, source: "user" },
|
||||||
id: existing.id,
|
|
||||||
spaceId: actor.spaceId,
|
|
||||||
userId: actor.userId,
|
|
||||||
source: "user",
|
|
||||||
},
|
|
||||||
data: {
|
data: {
|
||||||
name: resolved.name,
|
name: resolved.name,
|
||||||
description: resolved.description,
|
description: resolved.description,
|
||||||
@ -275,11 +266,7 @@ export function createAgentSkillsService(prisma: PrismaClient) {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
const row = await prisma.agentSkill.findFirst({
|
const row = await prisma.agentSkill.findFirst({
|
||||||
where: {
|
where: { id: existing.id, userId: actor.userId },
|
||||||
id: existing.id,
|
|
||||||
spaceId: actor.spaceId,
|
|
||||||
userId: actor.userId,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
if (!row) throw new IsolationError();
|
if (!row) throw new IsolationError();
|
||||||
return mapAgentSkill(row);
|
return mapAgentSkill(row);
|
||||||
@ -291,12 +278,7 @@ export function createAgentSkillsService(prisma: PrismaClient) {
|
|||||||
throw new ORPCError("BAD_REQUEST", { message: "Builtin and plugin skills are read-only." });
|
throw new ORPCError("BAD_REQUEST", { message: "Builtin and plugin skills are read-only." });
|
||||||
}
|
}
|
||||||
const deleted = await prisma.agentSkill.deleteMany({
|
const deleted = await prisma.agentSkill.deleteMany({
|
||||||
where: {
|
where: { id: existing.id, userId: actor.userId, source: "user" },
|
||||||
id: existing.id,
|
|
||||||
spaceId: actor.spaceId,
|
|
||||||
userId: actor.userId,
|
|
||||||
source: "user",
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
if (deleted.count !== 1) throw new IsolationError();
|
if (deleted.count !== 1) throw new IsolationError();
|
||||||
return { ok: true };
|
return { ok: true };
|
||||||
|
|||||||
@ -20,8 +20,8 @@ function setup() {
|
|||||||
secret: { findMany: vi.fn(async () => []) },
|
secret: { findMany: vi.fn(async () => []) },
|
||||||
bot: { findMany: vi.fn(async () => []) },
|
bot: { findMany: vi.fn(async () => []) },
|
||||||
spaceVoicePreference: {
|
spaceVoicePreference: {
|
||||||
findFirst: vi.fn(async () => ({ credential: { provider: "openai" }, voiceId: "alloy" })),
|
findFirst: vi.fn(async () => ({ credential: { provider: "kokoro" }, voiceId: "af_heart" })),
|
||||||
findMany: vi.fn(async () => [{ credential: { provider: "openai" } }]),
|
findMany: vi.fn(async () => [{ credential: { provider: "kokoro" } }]),
|
||||||
},
|
},
|
||||||
spaceMemoryConfig: {
|
spaceMemoryConfig: {
|
||||||
findUnique: vi.fn(async () => null),
|
findUnique: vi.fn(async () => null),
|
||||||
@ -29,6 +29,7 @@ function setup() {
|
|||||||
},
|
},
|
||||||
spaceMember: { findUnique: vi.fn(async () => ({ role: "owner" })) },
|
spaceMember: { findUnique: vi.fn(async () => ({ role: "owner" })) },
|
||||||
deploymentSettings: { findUnique: vi.fn(async () => null) },
|
deploymentSettings: { findUnique: vi.fn(async () => null) },
|
||||||
|
integrationProviderConfig: { findUnique: vi.fn(async () => null) },
|
||||||
aiDataConsent: { findMany: vi.fn(async () => []), upsert },
|
aiDataConsent: { findMany: vi.fn(async () => []), upsert },
|
||||||
$transaction: vi.fn(async (queries) => Promise.all(queries)),
|
$transaction: vi.fn(async (queries) => Promise.all(queries)),
|
||||||
},
|
},
|
||||||
@ -103,12 +104,12 @@ describe("consent grants", () => {
|
|||||||
it("discloses only the selected voice provider before a voice action", async () => {
|
it("discloses only the selected voice provider before a voice action", async () => {
|
||||||
const { deps, actor } = setup();
|
const { deps, actor } = setup();
|
||||||
vi.mocked(deps.prisma.spaceVoicePreference.findMany).mockResolvedValue([
|
vi.mocked(deps.prisma.spaceVoicePreference.findMany).mockResolvedValue([
|
||||||
{ credential: { provider: "openai" } },
|
{ credential: { provider: "kokoro" } },
|
||||||
{ credential: { provider: "elevenlabs" } },
|
{ credential: { provider: "scripted" } },
|
||||||
] as never);
|
] as never);
|
||||||
const status = await aiConsentStatus(deps, actor, { uses: ["voice"] });
|
const status = await aiConsentStatus(deps, actor, { uses: ["voice"] });
|
||||||
expect(status.recipients).toHaveLength(1);
|
expect(status.recipients).toHaveLength(1);
|
||||||
expect(status.recipients[0]?.name).toBe("OpenAI");
|
expect(status.recipients[0]?.name).toBe("Local voice");
|
||||||
expect(deps.prisma.spaceVoicePreference.findMany).not.toHaveBeenCalled();
|
expect(deps.prisma.spaceVoicePreference.findMany).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
it("discloses memory provider details to space owners", async () => {
|
it("discloses memory provider details to space owners", async () => {
|
||||||
@ -134,6 +135,54 @@ describe("consent grants", () => {
|
|||||||
expect(JSON.stringify(status.recipients)).not.toContain("supermemory");
|
expect(JSON.stringify(status.recipients)).not.toContain("supermemory");
|
||||||
expect(deps.prisma.spaceMemoryConfig.findUnique).not.toHaveBeenCalled();
|
expect(deps.prisma.spaceMemoryConfig.findUnique).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
it("discloses the deployment's media servers before model use, keyed by server", async () => {
|
||||||
|
const { deps, actor } = setup();
|
||||||
|
const settings = {
|
||||||
|
comfyui: { baseUrl: "http://127.0.0.1:8188", apiKey: "comfy-key", imageModel: "sdxl" },
|
||||||
|
transcription: { baseUrl: "http://127.0.0.1:8080/v1", model: "whisper-1" },
|
||||||
|
};
|
||||||
|
vi.mocked(deps.prisma.integrationProviderConfig.findUnique).mockResolvedValue({
|
||||||
|
id: "media",
|
||||||
|
ciphertext: "fake-media-ciphertext",
|
||||||
|
} as never);
|
||||||
|
deps.secrets = {
|
||||||
|
load: vi.fn(() => JSON.stringify(settings)),
|
||||||
|
} as unknown as RouterDeps["secrets"];
|
||||||
|
const status = await aiConsentStatus(deps, actor, { botId: "bot", uses: ["model"] });
|
||||||
|
expect(status.recipients.map((recipient) => recipient.name)).toEqual([
|
||||||
|
"ComfyUI (http://127.0.0.1:8188)",
|
||||||
|
"Transcription (http://127.0.0.1:8080)",
|
||||||
|
]);
|
||||||
|
expect(status.recipients[0]?.detail).toBe("Model: sdxl.");
|
||||||
|
expect(JSON.stringify(status)).not.toContain("comfy-key");
|
||||||
|
// The local voice cannot transcribe, so dictation also goes to the transcription server.
|
||||||
|
expect(
|
||||||
|
(await aiConsentStatus(deps, actor, { uses: ["voice"] })).recipients.map(
|
||||||
|
(recipient) => recipient.name,
|
||||||
|
),
|
||||||
|
).toEqual(["Local voice", "Transcription (http://127.0.0.1:8080)"]);
|
||||||
|
});
|
||||||
|
it("discloses the transcription server for dictation when the voice provider cannot transcribe", async () => {
|
||||||
|
const { deps, actor } = setup();
|
||||||
|
vi.mocked(deps.prisma.integrationProviderConfig.findUnique).mockResolvedValue({
|
||||||
|
id: "media",
|
||||||
|
ciphertext: "fake-media-ciphertext-voice",
|
||||||
|
} as never);
|
||||||
|
deps.secrets = {
|
||||||
|
load: vi.fn(() =>
|
||||||
|
JSON.stringify({ comfyui: null, transcription: { baseUrl: "http://127.0.0.1:8080/v1" } }),
|
||||||
|
),
|
||||||
|
} as unknown as RouterDeps["secrets"];
|
||||||
|
vi.mocked(deps.prisma.spaceVoicePreference.findFirst).mockResolvedValue({
|
||||||
|
credential: { provider: "kokoro" },
|
||||||
|
voiceId: "af_heart",
|
||||||
|
} as never);
|
||||||
|
const status = await aiConsentStatus(deps, actor, { uses: ["voice"] });
|
||||||
|
expect(status.recipients.map((recipient) => [recipient.name, recipient.use])).toEqual([
|
||||||
|
["Local voice", "voice"],
|
||||||
|
["Transcription (http://127.0.0.1:8080)", "voice"],
|
||||||
|
]);
|
||||||
|
});
|
||||||
it("returns an operator policy without requiring a hosted provider", async () => {
|
it("returns an operator policy without requiring a hosted provider", async () => {
|
||||||
const { deps, actor } = setup();
|
const { deps, actor } = setup();
|
||||||
deps.env.privacyPolicyUrl = "https://example.com/privacy";
|
deps.env.privacyPolicyUrl = "https://example.com/privacy";
|
||||||
|
|||||||
@ -3,9 +3,13 @@ import { ORPCError } from "@orpc/server";
|
|||||||
import {
|
import {
|
||||||
aiRecipient,
|
aiRecipient,
|
||||||
cloudAgentsEnabled,
|
cloudAgentsEnabled,
|
||||||
|
loadMediaSettings,
|
||||||
|
MEDIA_COMFYUI_PROVIDER_ID,
|
||||||
|
MEDIA_TRANSCRIPTION_PROVIDER_ID,
|
||||||
parseModelSecret,
|
parseModelSecret,
|
||||||
selectConfiguredModel,
|
selectConfiguredModel,
|
||||||
toStringRecord,
|
toStringRecord,
|
||||||
|
voiceCatalogEntry,
|
||||||
} from "@rakazo/adapters";
|
} from "@rakazo/adapters";
|
||||||
import type { Actor, AiConsentQuery, AiConsentStatus, AiRecipient } from "@rakazo/contracts";
|
import type { Actor, AiConsentQuery, AiConsentStatus, AiRecipient } from "@rakazo/contracts";
|
||||||
import { AI_DATA_DISCLOSURES, AI_DISCLOSURE_VERSION, AI_PRIVACY_URL } from "@rakazo/contracts";
|
import { AI_DATA_DISCLOSURES, AI_DISCLOSURE_VERSION, AI_PRIVACY_URL } from "@rakazo/contracts";
|
||||||
@ -183,6 +187,21 @@ export async function aiConsentStatus(
|
|||||||
}
|
}
|
||||||
for (const voice of voices)
|
for (const voice of voices)
|
||||||
add(aiRecipient({ provider: voice.credential.provider, use: "voice" }));
|
add(aiRecipient({ provider: voice.credential.provider, use: "voice" }));
|
||||||
|
if (uses.includes("voice")) {
|
||||||
|
// Dictation goes to the owner's transcription server when the voice provider cannot transcribe.
|
||||||
|
const transcription = (await loadMediaSettings(deps)).transcription;
|
||||||
|
const credential = transcription ? await findDefaultVoiceCredential(deps.prisma, actor) : null;
|
||||||
|
if (transcription && !(credential && voiceCatalogEntry(credential.provider)?.transcribe)) {
|
||||||
|
add(
|
||||||
|
aiRecipient({
|
||||||
|
provider: MEDIA_TRANSCRIPTION_PROVIDER_ID,
|
||||||
|
use: "voice",
|
||||||
|
baseUrl: transcription.baseUrl,
|
||||||
|
modelId: transcription.model,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (includesMemory) {
|
if (includesMemory) {
|
||||||
if (memoryOwner) {
|
if (memoryOwner) {
|
||||||
const memory = await deps.prisma.spaceMemoryConfig.findUnique({
|
const memory = await deps.prisma.spaceMemoryConfig.findUnique({
|
||||||
@ -210,6 +229,30 @@ export async function aiConsentStatus(
|
|||||||
) {
|
) {
|
||||||
add(aiRecipient({ provider: deps.cloudAgent.provider.describe().id, use: "model" }));
|
add(aiRecipient({ provider: deps.cloudAgent.provider.describe().id, use: "model" }));
|
||||||
}
|
}
|
||||||
|
if (uses.includes("model")) {
|
||||||
|
// Media tools and card actions send prompts, images, video and audio to the owner's servers.
|
||||||
|
const media = await loadMediaSettings(deps);
|
||||||
|
if (media.comfyui) {
|
||||||
|
add(
|
||||||
|
aiRecipient({
|
||||||
|
provider: MEDIA_COMFYUI_PROVIDER_ID,
|
||||||
|
use: "model",
|
||||||
|
baseUrl: media.comfyui.baseUrl,
|
||||||
|
modelId: media.comfyui.imageModel,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (media.transcription) {
|
||||||
|
add(
|
||||||
|
aiRecipient({
|
||||||
|
provider: MEDIA_TRANSCRIPTION_PROVIDER_ID,
|
||||||
|
use: "model",
|
||||||
|
baseUrl: media.transcription.baseUrl,
|
||||||
|
modelId: media.transcription.model,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
scope: createHash("sha256")
|
scope: createHash("sha256")
|
||||||
.update(JSON.stringify([actor.userId, actor.spaceId]))
|
.update(JSON.stringify([actor.userId, actor.spaceId]))
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { rm } from "node:fs/promises";
|
import { ORPCError, onError, ValidationError } from "@orpc/server";
|
||||||
import { ORPCError, onError } from "@orpc/server";
|
|
||||||
import { RPCHandler } from "@orpc/server/fetch";
|
import { RPCHandler } from "@orpc/server/fetch";
|
||||||
import type {
|
import type {
|
||||||
AgentRuntime,
|
AgentRuntime,
|
||||||
@ -15,6 +14,7 @@ import type {
|
|||||||
ComposioProvider,
|
ComposioProvider,
|
||||||
ConnectorRegistry,
|
ConnectorRegistry,
|
||||||
DestinationEmulator,
|
DestinationEmulator,
|
||||||
|
MediaOperationRegistration,
|
||||||
RemoteConnectorDependencies,
|
RemoteConnectorDependencies,
|
||||||
} from "@rakazo/adapters";
|
} from "@rakazo/adapters";
|
||||||
import {
|
import {
|
||||||
@ -25,28 +25,34 @@ import {
|
|||||||
createCloudAgentConnection,
|
createCloudAgentConnection,
|
||||||
createConnectorStack,
|
createConnectorStack,
|
||||||
createJobReconciler,
|
createJobReconciler,
|
||||||
|
createMediaOperations,
|
||||||
|
createMediaRuntime,
|
||||||
createMessagingContextLoader,
|
createMessagingContextLoader,
|
||||||
createMessagingTeamChatSender,
|
createMessagingTeamChatSender,
|
||||||
|
createPostgresReconciliationLeadership,
|
||||||
createProfileConsolidationScheduler,
|
createProfileConsolidationScheduler,
|
||||||
createRunExecutor,
|
createRunExecutor,
|
||||||
createRunSandbox,
|
createRunSandbox,
|
||||||
createRunSecretWriter,
|
createRunSecretWriter,
|
||||||
createWebProvider,
|
createWebProvider,
|
||||||
destroyBot,
|
destroyBot,
|
||||||
|
destroySpaceSandboxes,
|
||||||
EmailEmulator,
|
EmailEmulator,
|
||||||
EncryptedSecretStore,
|
EncryptedSecretStore,
|
||||||
ExpoPushProvider,
|
ExpoPushSender,
|
||||||
GraphileJobPublisher,
|
GraphileJobPublisher,
|
||||||
GraphileJobWorkerHost,
|
GraphileJobWorkerHost,
|
||||||
InMemoryJobQueue,
|
InMemoryJobQueue,
|
||||||
InMemoryRealtimeFanout,
|
InMemoryRealtimeFanout,
|
||||||
InstalledConnectorProvider,
|
InstalledConnectorProvider,
|
||||||
IntegrationProviderSettings,
|
IntegrationProviderSettings,
|
||||||
|
importLegacyPushTokens,
|
||||||
isComposioEnabled,
|
isComposioEnabled,
|
||||||
isMessagingSurfaceEnabled,
|
isMessagingSurfaceEnabled,
|
||||||
isPipedreamEnabled,
|
isPipedreamEnabled,
|
||||||
LocalAgentHomeStore,
|
LocalAgentHomeStore,
|
||||||
LocalArtifactStore,
|
LocalArtifactStore,
|
||||||
|
loadMediaSettings,
|
||||||
McpConnector,
|
McpConnector,
|
||||||
McpOAuthBroker,
|
McpOAuthBroker,
|
||||||
messagingPlatformsFromEnv,
|
messagingPlatformsFromEnv,
|
||||||
@ -54,9 +60,9 @@ import {
|
|||||||
PiOAuthLogins,
|
PiOAuthLogins,
|
||||||
PipedreamConnector,
|
PipedreamConnector,
|
||||||
PostgresRealtimeFanout,
|
PostgresRealtimeFanout,
|
||||||
|
PushNotificationProvider,
|
||||||
pipedreamConfigFromEnv,
|
pipedreamConfigFromEnv,
|
||||||
piSessionsRoot,
|
piSessionsRoot,
|
||||||
pushTokenPath,
|
|
||||||
reconcileCloudAgents,
|
reconcileCloudAgents,
|
||||||
reconcileComputerUpdates,
|
reconcileComputerUpdates,
|
||||||
reconcileDelegatedTasks,
|
reconcileDelegatedTasks,
|
||||||
@ -65,6 +71,9 @@ import {
|
|||||||
SmtpEmailProvider,
|
SmtpEmailProvider,
|
||||||
SpaceMemoryProviderResolver,
|
SpaceMemoryProviderResolver,
|
||||||
toTeamChatInbound,
|
toTeamChatInbound,
|
||||||
|
VapidKeys,
|
||||||
|
vapidEnvFromProcess,
|
||||||
|
WebPushSender,
|
||||||
} from "@rakazo/adapters";
|
} from "@rakazo/adapters";
|
||||||
import { blockedAuthPaths, createAuth } from "@rakazo/auth";
|
import { blockedAuthPaths, createAuth } from "@rakazo/auth";
|
||||||
import { signupPolicyFromEnv } from "@rakazo/core";
|
import { signupPolicyFromEnv } from "@rakazo/core";
|
||||||
@ -76,6 +85,7 @@ import {
|
|||||||
parsePositiveInteger,
|
parsePositiveInteger,
|
||||||
provisionMessagingIdentity,
|
provisionMessagingIdentity,
|
||||||
requireMembership,
|
requireMembership,
|
||||||
|
spaceIdsForSandboxTeardownAfterAccountDelete,
|
||||||
} from "@rakazo/db";
|
} from "@rakazo/db";
|
||||||
import type { Logger } from "@rakazo/logging";
|
import type { Logger } from "@rakazo/logging";
|
||||||
import {
|
import {
|
||||||
@ -89,6 +99,7 @@ import { requestLogging } from "@rakazo/logging/hono";
|
|||||||
import { MarkdownMemoryStore } from "@rakazo/memory";
|
import { MarkdownMemoryStore } from "@rakazo/memory";
|
||||||
import { Hono } from "hono";
|
import { Hono } from "hono";
|
||||||
import { cors } from "hono/cors";
|
import { cors } from "hono/cors";
|
||||||
|
import { mountArtifactHttpRoutes } from "./artifact-http.js";
|
||||||
import type { AppEnv } from "./env.js";
|
import type { AppEnv } from "./env.js";
|
||||||
import { loadEnv } from "./env.js";
|
import { loadEnv } from "./env.js";
|
||||||
import { mountLocalSettings } from "./local-settings.js";
|
import { mountLocalSettings } from "./local-settings.js";
|
||||||
@ -110,6 +121,7 @@ import {
|
|||||||
TEAM_CHAT_STARTUP_SHUTDOWN_MS,
|
TEAM_CHAT_STARTUP_SHUTDOWN_MS,
|
||||||
} from "./team-chat-startup.js";
|
} from "./team-chat-startup.js";
|
||||||
import { mountVoiceHttpRoutes } from "./voice.js";
|
import { mountVoiceHttpRoutes } from "./voice.js";
|
||||||
|
import { isLoopbackHost, isPrivateOrLocalHost, requestWebOrigin } from "./web-origin.js";
|
||||||
import { mountWebhookHttpRoutes } from "./webhook.js";
|
import { mountWebhookHttpRoutes } from "./webhook.js";
|
||||||
|
|
||||||
export interface AppHandles {
|
export interface AppHandles {
|
||||||
@ -138,6 +150,8 @@ export async function createApp(
|
|||||||
email?: TransactionalEmailProvider;
|
email?: TransactionalEmailProvider;
|
||||||
remoteConnectors?: RemoteConnectorDependencies;
|
remoteConnectors?: RemoteConnectorDependencies;
|
||||||
logger?: Logger;
|
logger?: Logger;
|
||||||
|
/** Test seam: media operations instead of the configured ones. */
|
||||||
|
mediaOperations?: MediaOperationRegistration[];
|
||||||
} = {},
|
} = {},
|
||||||
): Promise<AppHandles> {
|
): Promise<AppHandles> {
|
||||||
const {
|
const {
|
||||||
@ -150,6 +164,7 @@ export async function createApp(
|
|||||||
email: emailOverride,
|
email: emailOverride,
|
||||||
remoteConnectors,
|
remoteConnectors,
|
||||||
logger: loggerOverride,
|
logger: loggerOverride,
|
||||||
|
mediaOperations,
|
||||||
...envOverrides
|
...envOverrides
|
||||||
} = overrides;
|
} = overrides;
|
||||||
const env = { ...loadEnv(process.env), ...envOverrides };
|
const env = { ...loadEnv(process.env), ...envOverrides };
|
||||||
@ -308,7 +323,14 @@ export async function createApp(
|
|||||||
: new PiAgentRuntime({
|
: new PiAgentRuntime({
|
||||||
sessionRoot: env.piSessionRecording ? piSessionsRoot(env.dataDir) : undefined,
|
sessionRoot: env.piSessionRecording ? piSessionsRoot(env.dataDir) : undefined,
|
||||||
});
|
});
|
||||||
const notifications = new ExpoPushProvider(env.dataDir);
|
const vapid = new VapidKeys(prisma, secrets, vapidEnvFromProcess(process.env));
|
||||||
|
const notifications = new PushNotificationProvider(prisma, {
|
||||||
|
webpush: new WebPushSender(vapid),
|
||||||
|
expo: new ExpoPushSender(),
|
||||||
|
});
|
||||||
|
void importLegacyPushTokens(prisma, env.dataDir).catch((error) =>
|
||||||
|
getLogger().error("legacy push token import failed", error),
|
||||||
|
);
|
||||||
const auth = createAuth(prisma, {
|
const auth = createAuth(prisma, {
|
||||||
secret: env.authSecret,
|
secret: env.authSecret,
|
||||||
baseURL: env.authUrl,
|
baseURL: env.authUrl,
|
||||||
@ -339,32 +361,46 @@ export async function createApp(
|
|||||||
"https://172.*:*",
|
"https://172.*:*",
|
||||||
"http://*.local:*",
|
"http://*.local:*",
|
||||||
"https://*.local:*",
|
"https://*.local:*",
|
||||||
...(process.env.EXTRA_TRUSTED_ORIGINS?.split(",").map((s) => s.trim()) ?? []),
|
...(env.extraTrustedOrigins ?? []),
|
||||||
],
|
],
|
||||||
beforeDeleteUser: async (userId) => {
|
beforeDeleteUser: async (userId) => {
|
||||||
const bots = await prisma.bot.findMany({
|
const bots = await prisma.bot.findMany({
|
||||||
where: { userId },
|
where: { userId },
|
||||||
select: { id: true, userId: true, spaceId: true, name: true, archivedAt: true },
|
select: { id: true, userId: true, spaceId: true, name: true, archivedAt: true },
|
||||||
});
|
});
|
||||||
|
const lifecycleDeps = {
|
||||||
|
prisma,
|
||||||
|
sandbox,
|
||||||
|
home,
|
||||||
|
jobs,
|
||||||
|
artifacts,
|
||||||
|
dataDir: env.dataDir,
|
||||||
|
};
|
||||||
|
const deleteContext = {
|
||||||
|
operationId: `account-delete:${userId}`,
|
||||||
|
traceId: `account-delete:${userId}`,
|
||||||
|
userId,
|
||||||
|
signal: new AbortController().signal,
|
||||||
|
};
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
bots.map((bot) =>
|
bots.map((bot) =>
|
||||||
destroyBot(
|
destroyBot(
|
||||||
{ prisma, sandbox, home, jobs, artifacts, dataDir: env.dataDir },
|
lifecycleDeps,
|
||||||
bot,
|
bot,
|
||||||
{
|
{ ...deleteContext, spaceId: bot.spaceId, botId: bot.id },
|
||||||
operationId: `account-delete:${userId}`,
|
|
||||||
traceId: `account-delete:${userId}`,
|
|
||||||
spaceId: bot.spaceId,
|
|
||||||
userId,
|
|
||||||
botId: bot.id,
|
|
||||||
signal: new AbortController().signal,
|
|
||||||
},
|
|
||||||
{ deleteMemories: true },
|
{ deleteMemories: true },
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
const candidateSpaceIds = [...new Set(bots.map((bot) => bot.spaceId))];
|
||||||
|
const spaceIds = await spaceIdsForSandboxTeardownAfterAccountDelete(prisma, {
|
||||||
|
userId,
|
||||||
|
spaceIds: candidateSpaceIds,
|
||||||
|
});
|
||||||
|
for (const spaceId of spaceIds) {
|
||||||
|
await destroySpaceSandboxes(lifecycleDeps, { spaceId }, { ...deleteContext, spaceId });
|
||||||
|
}
|
||||||
await removePiUserSessions(env.dataDir, userId);
|
await removePiUserSessions(env.dataDir, userId);
|
||||||
await rm(pushTokenPath(env.dataDir, userId), { force: true }).catch(() => undefined);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
// One provider instance so emulator launches and polls share the same Map.
|
// One provider instance so emulator launches and polls share the same Map.
|
||||||
@ -374,6 +410,15 @@ export async function createApp(
|
|||||||
CLOUD_AGENT_SPACE_ID: env.cloudAgentSpaceId,
|
CLOUD_AGENT_SPACE_ID: env.cloudAgentSpaceId,
|
||||||
});
|
});
|
||||||
const shutdown = new AbortController();
|
const shutdown = new AbortController();
|
||||||
|
const media = createMediaRuntime({
|
||||||
|
prisma,
|
||||||
|
events,
|
||||||
|
jobs,
|
||||||
|
artifacts,
|
||||||
|
registrations:
|
||||||
|
mediaOperations ??
|
||||||
|
createMediaOperations({ loadSettings: () => loadMediaSettings({ prisma, secrets }) }),
|
||||||
|
});
|
||||||
const executor = createRunExecutor({
|
const executor = createRunExecutor({
|
||||||
prisma,
|
prisma,
|
||||||
runtime,
|
runtime,
|
||||||
@ -410,6 +455,7 @@ export async function createApp(
|
|||||||
messaging: messaging ? createMessagingContextLoader(prisma) : undefined,
|
messaging: messaging ? createMessagingContextLoader(prisma) : undefined,
|
||||||
web: createWebProvider(),
|
web: createWebProvider(),
|
||||||
cloudAgent,
|
cloudAgent,
|
||||||
|
media,
|
||||||
shutdownSignal: shutdown.signal,
|
shutdownSignal: shutdown.signal,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -427,30 +473,37 @@ export async function createApp(
|
|||||||
deploymentModelKey: env.deploymentModelKey,
|
deploymentModelKey: env.deploymentModelKey,
|
||||||
messaging,
|
messaging,
|
||||||
cloudAgent,
|
cloudAgent,
|
||||||
|
media,
|
||||||
});
|
});
|
||||||
|
let graphileWorkerHost: GraphileJobWorkerHost | undefined;
|
||||||
if (inMemoryJobs) {
|
if (inMemoryJobs) {
|
||||||
await inMemoryJobs.start(jobHandlers);
|
await inMemoryJobs.start(jobHandlers);
|
||||||
} else if (jobPool) {
|
} else if (jobPool) {
|
||||||
const graphileWorkerHost = new GraphileJobWorkerHost(jobPool, {
|
graphileWorkerHost = new GraphileJobWorkerHost(jobPool, {
|
||||||
concurrency: 4,
|
concurrency: parsePositiveInteger(process.env.GRAPHILE_WORKER_CONCURRENCY, 4),
|
||||||
pollInterval: 500,
|
pollInterval: 500,
|
||||||
});
|
});
|
||||||
await graphileWorkerHost.start(jobHandlers);
|
await graphileWorkerHost.start(jobHandlers);
|
||||||
}
|
}
|
||||||
const reconciler = inMemoryJobs
|
const reconciler =
|
||||||
? createJobReconciler({
|
inMemoryJobs || !prismaOverride
|
||||||
prisma,
|
? createJobReconciler({
|
||||||
jobs,
|
prisma,
|
||||||
reconcileCloudAgents: () => reconcileCloudAgents({ prisma, jobs, cloudAgent }),
|
jobs,
|
||||||
reconcileComputerUpdates: () => reconcileComputerUpdates({ prisma, jobs }),
|
events,
|
||||||
reconcileProfileConsolidation: createProfileConsolidationScheduler({ prisma, jobs }),
|
leadership: jobPool ? createPostgresReconciliationLeadership(jobPool) : undefined,
|
||||||
reconcileDelegatedTasks: () => reconcileDelegatedTasks({ prisma, jobs, events }),
|
reconcileCloudAgents: () => reconcileCloudAgents({ prisma, jobs, cloudAgent }),
|
||||||
})
|
reconcileComputerUpdates: () => reconcileComputerUpdates({ prisma, jobs }),
|
||||||
: undefined;
|
reconcileProfileConsolidation: createProfileConsolidationScheduler({ prisma, jobs }),
|
||||||
|
reconcileDelegatedTasks: () => reconcileDelegatedTasks({ prisma, jobs, events }),
|
||||||
|
reconcileMediaJobs: () => media.reconcile(),
|
||||||
|
})
|
||||||
|
: undefined;
|
||||||
reconciler?.start();
|
reconciler?.start();
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
cloudAgent,
|
cloudAgent,
|
||||||
|
media,
|
||||||
prisma,
|
prisma,
|
||||||
events,
|
events,
|
||||||
auth,
|
auth,
|
||||||
@ -462,6 +515,7 @@ export async function createApp(
|
|||||||
secrets,
|
secrets,
|
||||||
oauthLogins,
|
oauthLogins,
|
||||||
integrationSettings,
|
integrationSettings,
|
||||||
|
vapid,
|
||||||
mcpOAuth,
|
mcpOAuth,
|
||||||
composio: stack.composio,
|
composio: stack.composio,
|
||||||
connectors: stack.connector,
|
connectors: stack.connector,
|
||||||
@ -512,7 +566,7 @@ export async function createApp(
|
|||||||
await next();
|
await next();
|
||||||
c.res.headers.set(
|
c.res.headers.set(
|
||||||
"Content-Security-Policy",
|
"Content-Security-Policy",
|
||||||
"default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self'; connect-src 'self' ws:; frame-ancestors 'none'; base-uri 'self'; form-action 'self'",
|
"default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; media-src 'self' blob:; font-src 'self'; connect-src 'self' ws:; frame-ancestors 'none'; base-uri 'self'; form-action 'self'",
|
||||||
);
|
);
|
||||||
c.res.headers.set("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
|
c.res.headers.set("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
|
||||||
c.res.headers.set("X-Frame-Options", "DENY");
|
c.res.headers.set("X-Frame-Options", "DENY");
|
||||||
@ -559,7 +613,12 @@ export async function createApp(
|
|||||||
}
|
}
|
||||||
const { matched, response } = await rpc.handle(c.req.raw, {
|
const { matched, response } = await rpc.handle(c.req.raw, {
|
||||||
prefix: "/rpc",
|
prefix: "/rpc",
|
||||||
context: { actor, signal: c.req.raw.signal },
|
context: {
|
||||||
|
actor,
|
||||||
|
signal: c.req.raw.signal,
|
||||||
|
userAgent: c.req.header("user-agent"),
|
||||||
|
webOrigin: requestWebOrigin(c.req.header("origin"), env.webOrigin, env.extraTrustedOrigins),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
if (matched) return c.newResponse(response.body, response);
|
if (matched) return c.newResponse(response.body, response);
|
||||||
await next();
|
await next();
|
||||||
@ -575,6 +634,13 @@ export async function createApp(
|
|||||||
if (actor) enrichLogContext({ "user.id": actor.userId, "space.id": actor.spaceId });
|
if (actor) enrichLogContext({ "user.id": actor.userId, "space.id": actor.spaceId });
|
||||||
return actor;
|
return actor;
|
||||||
});
|
});
|
||||||
|
mountArtifactHttpRoutes(app, {
|
||||||
|
prisma,
|
||||||
|
artifacts,
|
||||||
|
sessionUserId: async (request) =>
|
||||||
|
(await auth.api.getSession({ headers: sessionHeaders(request) }))?.user?.id ?? null,
|
||||||
|
trustedOrigin: (origin) => isTrustedOrigin(origin, env),
|
||||||
|
});
|
||||||
mountWebhookHttpRoutes(app, { prisma, secrets, events, jobs });
|
mountWebhookHttpRoutes(app, { prisma, secrets, events, jobs });
|
||||||
// Shared with stop so a shutdown during retry delays does not restart polling.
|
// Shared with stop so a shutdown during retry delays does not restart polling.
|
||||||
let messagingStopped = false;
|
let messagingStopped = false;
|
||||||
@ -945,6 +1011,7 @@ export async function createApp(
|
|||||||
await teamChatBridge?.stop();
|
await teamChatBridge?.stop();
|
||||||
await email?.drain?.();
|
await email?.drain?.();
|
||||||
await reconciler?.stop();
|
await reconciler?.stop();
|
||||||
|
await graphileWorkerHost?.stop();
|
||||||
await jobs.close();
|
await jobs.close();
|
||||||
await realtime.close();
|
await realtime.close();
|
||||||
await connector.stop();
|
await connector.stop();
|
||||||
@ -960,6 +1027,7 @@ export async function createApp(
|
|||||||
function isTrustedOrigin(origin: string, env: AppEnv) {
|
function isTrustedOrigin(origin: string, env: AppEnv) {
|
||||||
if (!origin) return true;
|
if (!origin) return true;
|
||||||
if (origin === env.webOrigin || origin === env.apiUrl || origin === env.authUrl) return true;
|
if (origin === env.webOrigin || origin === env.apiUrl || origin === env.authUrl) return true;
|
||||||
|
if (env.extraTrustedOrigins?.includes(origin)) return true;
|
||||||
if (origin.startsWith("rakazo://") || origin.startsWith("exp://")) return true;
|
if (origin.startsWith("rakazo://") || origin.startsWith("exp://")) return true;
|
||||||
try {
|
try {
|
||||||
const host = new URL(origin).hostname;
|
const host = new URL(origin).hostname;
|
||||||
@ -969,20 +1037,6 @@ function isTrustedOrigin(origin: string, env: AppEnv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isPrivateOrLocalHost(host: string): boolean {
|
|
||||||
if (isLoopbackHost(host)) return true;
|
|
||||||
if (host.endsWith(".local")) return true;
|
|
||||||
if (/^192\.168\.\d{1,3}\.\d{1,3}$/.test(host)) return true;
|
|
||||||
if (/^10\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(host)) return true;
|
|
||||||
if (/^172\.(1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}$/.test(host)) return true;
|
|
||||||
if (/^100\.(6[4-9]|[7-9]\d|1[01]\d|12[0-7])\.\d{1,3}\.\d{1,3}$/.test(host)) return true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isLoopbackHost(host: string): boolean {
|
|
||||||
return host === "localhost" || host === "127.0.0.1" || host === "::1" || host === "[::1]";
|
|
||||||
}
|
|
||||||
|
|
||||||
function sessionHeaders(request: Request) {
|
function sessionHeaders(request: Request) {
|
||||||
const headers = new Headers(request.headers);
|
const headers = new Headers(request.headers);
|
||||||
const authz = headers.get("authorization");
|
const authz = headers.get("authorization");
|
||||||
@ -1001,7 +1055,18 @@ function sessionHeaders(request: Request) {
|
|||||||
* "fetch failed" and keep the host and errno one level down.
|
* "fetch failed" and keep the host and errno one level down.
|
||||||
*/
|
*/
|
||||||
export function logUnexpectedRpcError(error: unknown, path: readonly string[]): void {
|
export function logUnexpectedRpcError(error: unknown, path: readonly string[]): void {
|
||||||
if (error instanceof ORPCError) return;
|
|
||||||
const where = `rpc ${path.join("/")} failed`;
|
const where = `rpc ${path.join("/")} failed`;
|
||||||
|
if (error instanceof ORPCError) {
|
||||||
|
// A handler result that breaks its output contract is a server bug, not a chosen error.
|
||||||
|
if (error.code !== "INTERNAL_SERVER_ERROR" || !(error.cause instanceof ValidationError)) return;
|
||||||
|
const issues = error.cause.issues.map((issue) => ({
|
||||||
|
path: (issue.path ?? [])
|
||||||
|
.map((segment) => (typeof segment === "object" ? segment.key : segment))
|
||||||
|
.join("."),
|
||||||
|
problem: issue.message,
|
||||||
|
}));
|
||||||
|
getLogger().error(where, error, { issues });
|
||||||
|
return;
|
||||||
|
}
|
||||||
getLogger().error(where, error);
|
getLogger().error(where, error);
|
||||||
}
|
}
|
||||||
|
|||||||
275
apps/api/src/artifact-http.test.ts
Normal file
275
apps/api/src/artifact-http.test.ts
Normal file
@ -0,0 +1,275 @@
|
|||||||
|
import { mkdtemp, readdir, rm } from "node:fs/promises";
|
||||||
|
import { createServer } from "node:http";
|
||||||
|
import { tmpdir } from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
import type { ArtifactStore } from "@rakazo/adapter-kit";
|
||||||
|
import { LocalArtifactStore } from "@rakazo/adapters";
|
||||||
|
import type { PrismaClient } from "@rakazo/db";
|
||||||
|
import { Hono } from "hono";
|
||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import {
|
||||||
|
API_SERVER_OPTIONS,
|
||||||
|
contentDisposition,
|
||||||
|
mountArtifactHttpRoutes,
|
||||||
|
parseByteRange,
|
||||||
|
stallGuardedChunks,
|
||||||
|
UploadStalledError,
|
||||||
|
} from "./artifact-http.js";
|
||||||
|
|
||||||
|
describe("parseByteRange", () => {
|
||||||
|
it("ignores an absent header or another unit", () => {
|
||||||
|
expect(parseByteRange(undefined, 100)).toBeNull();
|
||||||
|
expect(parseByteRange("", 100)).toBeNull();
|
||||||
|
expect(parseByteRange("items=0-1", 100)).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("parses closed, open and suffix ranges and clamps the end to the size", () => {
|
||||||
|
expect(parseByteRange("bytes=0-9", 100)).toEqual({ start: 0, end: 9 });
|
||||||
|
expect(parseByteRange("bytes=90-", 100)).toEqual({ start: 90, end: 99 });
|
||||||
|
expect(parseByteRange("bytes=-10", 100)).toEqual({ start: 90, end: 99 });
|
||||||
|
expect(parseByteRange("bytes=-500", 100)).toEqual({ start: 0, end: 99 });
|
||||||
|
expect(parseByteRange("bytes=50-1000", 100)).toEqual({ start: 50, end: 99 });
|
||||||
|
expect(parseByteRange("Bytes= 0-0 ", 100)).toEqual({ start: 0, end: 0 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects multiple, inverted, out-of-file and malformed ranges", () => {
|
||||||
|
for (const header of [
|
||||||
|
"bytes=0-1,5-6",
|
||||||
|
"bytes=5-4",
|
||||||
|
"bytes=100-",
|
||||||
|
"bytes=100-200",
|
||||||
|
"bytes=-0",
|
||||||
|
"bytes=-",
|
||||||
|
"bytes=a-b",
|
||||||
|
"bytes=1.5-2",
|
||||||
|
"bytes= -1-2",
|
||||||
|
"bytes=0x10-20",
|
||||||
|
`bytes=${"9".repeat(16)}-`,
|
||||||
|
]) {
|
||||||
|
expect(parseByteRange(header, 100), header).toBe("unsatisfiable");
|
||||||
|
}
|
||||||
|
expect(parseByteRange("bytes=0-0", 0)).toBe("unsatisfiable");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("contentDisposition", () => {
|
||||||
|
it("keeps plain names and marks media inline", () => {
|
||||||
|
expect(contentDisposition("clip.mp4", true)).toBe(
|
||||||
|
`inline; filename="clip.mp4"; filename*=UTF-8''clip.mp4`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("neutralizes quotes, separators, control characters and header injection", () => {
|
||||||
|
const header = contentDisposition('../evil"\r\nSet-Cookie: x=1;.txt', false);
|
||||||
|
expect(header.startsWith("attachment; ")).toBe(true);
|
||||||
|
expect(header).not.toMatch(/[\r\n]/);
|
||||||
|
expect(header).toContain(`filename=".._evil___Set-Cookie: x=1_.txt"`);
|
||||||
|
expect(header).not.toContain("/");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("encodes non-ASCII names with an ASCII fallback", () => {
|
||||||
|
expect(contentDisposition("Grüße (1).png", true)).toBe(
|
||||||
|
`inline; filename="Gr__e (1).png"; filename*=UTF-8''Gr%C3%BC%C3%9Fe%20%281%29.png`,
|
||||||
|
);
|
||||||
|
expect(contentDisposition(" ", false)).toBe(
|
||||||
|
`attachment; filename="download"; filename*=UTF-8''download`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("artifact HTTP routes", () => {
|
||||||
|
function app(sessionUserId: () => Promise<string | null>) {
|
||||||
|
const prisma = {
|
||||||
|
artifact: { findUnique: vi.fn(), create: vi.fn() },
|
||||||
|
spaceMember: { findFirst: vi.fn() },
|
||||||
|
};
|
||||||
|
const artifacts = { putStream: vi.fn(), stat: vi.fn(), createReadStream: vi.fn() };
|
||||||
|
const hono = new Hono();
|
||||||
|
mountArtifactHttpRoutes(hono, {
|
||||||
|
prisma: prisma as unknown as PrismaClient,
|
||||||
|
artifacts: artifacts as unknown as ArtifactStore,
|
||||||
|
sessionUserId,
|
||||||
|
trustedOrigin: (origin) => origin === "http://127.0.0.1:5173",
|
||||||
|
});
|
||||||
|
return { hono, prisma, artifacts };
|
||||||
|
}
|
||||||
|
|
||||||
|
it("rejects signed-out reads and uploads before touching storage", async () => {
|
||||||
|
const { hono, prisma, artifacts } = app(async () => null);
|
||||||
|
const read = await hono.request("/api/artifacts/art-1");
|
||||||
|
const upload = await hono.request("/api/artifacts?botId=bot-1&name=a.mp4", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "content-type": "video/mp4" },
|
||||||
|
body: "data",
|
||||||
|
});
|
||||||
|
expect(read.status).toBe(401);
|
||||||
|
expect(upload.status).toBe(401);
|
||||||
|
expect(prisma.artifact.findUnique).not.toHaveBeenCalled();
|
||||||
|
expect(artifacts.putStream).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("refuses uploads from untrusted origins before reading the session", async () => {
|
||||||
|
const sessionUserId = vi.fn(async () => "user-1");
|
||||||
|
const { hono, artifacts } = app(sessionUserId);
|
||||||
|
const response = await hono.request("/api/artifacts?botId=bot-1&name=a.txt", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "content-type": "text/plain", origin: "https://evil.example" },
|
||||||
|
body: "data",
|
||||||
|
});
|
||||||
|
expect(response.status).toBe(403);
|
||||||
|
expect(sessionUserId).not.toHaveBeenCalled();
|
||||||
|
expect(artifacts.putStream).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("answers ids that are not artifact ids with 404 without a lookup", async () => {
|
||||||
|
const { hono, prisma } = app(async () => "user-1");
|
||||||
|
const response = await hono.request(`/api/artifacts/${encodeURIComponent("../etc/passwd")}`);
|
||||||
|
expect(response.status).toBe(404);
|
||||||
|
expect(prisma.artifact.findUnique).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects declared oversized and disallowed uploads without reading the body", async () => {
|
||||||
|
const { hono, prisma, artifacts } = app(async () => "user-1");
|
||||||
|
prisma.spaceMember.findFirst.mockResolvedValue({
|
||||||
|
userId: "user-1",
|
||||||
|
spaceId: "space-1",
|
||||||
|
member: { user: { email: "user@example.test" } },
|
||||||
|
});
|
||||||
|
(prisma as unknown as { deploymentSettings: unknown }).deploymentSettings = {
|
||||||
|
findUnique: vi.fn(async () => null),
|
||||||
|
};
|
||||||
|
const cancel = vi.fn(() => new Promise<void>(() => undefined));
|
||||||
|
const oversized = await hono.request(
|
||||||
|
new Request("http://localhost/api/artifacts?botId=bot-1&name=a.mp4", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "content-type": "video/mp4", "content-length": String(501 * 1024 * 1024) },
|
||||||
|
body: new ReadableStream({ cancel }),
|
||||||
|
duplex: "half",
|
||||||
|
} as RequestInit & { duplex: "half" }),
|
||||||
|
);
|
||||||
|
expect(oversized.status).toBe(413);
|
||||||
|
expect(cancel).toHaveBeenCalled();
|
||||||
|
|
||||||
|
const html = await hono.request("/api/artifacts?botId=bot-1&name=a.html", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "content-type": "text/html" },
|
||||||
|
body: "<script></script>",
|
||||||
|
});
|
||||||
|
expect(html.status).toBe(415);
|
||||||
|
|
||||||
|
const noTarget = await hono.request("/api/artifacts?name=a.mp4", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "content-type": "video/mp4" },
|
||||||
|
body: "data",
|
||||||
|
});
|
||||||
|
expect(noTarget.status).toBe(400);
|
||||||
|
expect(artifacts.putStream).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("upload timeouts", () => {
|
||||||
|
const dirs: string[] = [];
|
||||||
|
afterEach(async () => {
|
||||||
|
await Promise.all(dirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("gives the server time for large uploads but keeps the header timeout", () => {
|
||||||
|
const server = createServer(API_SERVER_OPTIONS);
|
||||||
|
expect(server.requestTimeout).toBe(2 * 60 * 60 * 1000);
|
||||||
|
expect(server.headersTimeout).toBe(createServer().headersTimeout);
|
||||||
|
});
|
||||||
|
|
||||||
|
/** A body that sends `chunks` at `gapMs` intervals, then stays open forever if `hang`. */
|
||||||
|
function drip(chunks: string[], gapMs: number, hang: boolean) {
|
||||||
|
const cancel = vi.fn(() => new Promise<void>(() => undefined));
|
||||||
|
let sent = 0;
|
||||||
|
const body = new ReadableStream<Uint8Array>({
|
||||||
|
async pull(controller) {
|
||||||
|
if (sent === chunks.length) {
|
||||||
|
if (!hang) controller.close();
|
||||||
|
else await new Promise(() => undefined);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (sent > 0) await new Promise((resolve) => setTimeout(resolve, gapMs));
|
||||||
|
controller.enqueue(new TextEncoder().encode(chunks[sent++]));
|
||||||
|
},
|
||||||
|
cancel,
|
||||||
|
});
|
||||||
|
return { body, cancel };
|
||||||
|
}
|
||||||
|
|
||||||
|
it("throws once the body stops sending and cancels it without waiting", async () => {
|
||||||
|
const { body, cancel } = drip(["ab", "cd"], 5, true);
|
||||||
|
const seen: string[] = [];
|
||||||
|
await expect(
|
||||||
|
(async () => {
|
||||||
|
for await (const chunk of stallGuardedChunks(body, 50)) {
|
||||||
|
seen.push(new TextDecoder().decode(chunk));
|
||||||
|
}
|
||||||
|
})(),
|
||||||
|
).rejects.toBeInstanceOf(UploadStalledError);
|
||||||
|
expect(seen).toEqual(["ab", "cd"]);
|
||||||
|
expect(cancel).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
async function postDrip(stallMs: number, body: ReadableStream<Uint8Array>) {
|
||||||
|
const root = await mkdtemp(path.join(tmpdir(), "rakazo-artifact-upload-"));
|
||||||
|
dirs.push(root);
|
||||||
|
const create = vi.fn(async ({ data }: { data: Record<string, unknown> }) => ({
|
||||||
|
id: "art-1",
|
||||||
|
createdAt: new Date(),
|
||||||
|
...data,
|
||||||
|
}));
|
||||||
|
const prisma = {
|
||||||
|
artifact: { create },
|
||||||
|
spaceMember: {
|
||||||
|
findFirst: vi.fn(async () => ({
|
||||||
|
userId: "user-1",
|
||||||
|
spaceId: "space-1",
|
||||||
|
member: { user: { email: "user@example.test" } },
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
deploymentSettings: { findUnique: vi.fn(async () => null) },
|
||||||
|
bot: { findFirst: vi.fn(async () => ({ id: "bot-1" })) },
|
||||||
|
};
|
||||||
|
const hono = new Hono();
|
||||||
|
mountArtifactHttpRoutes(hono, {
|
||||||
|
prisma: prisma as unknown as PrismaClient,
|
||||||
|
artifacts: new LocalArtifactStore(root),
|
||||||
|
sessionUserId: async () => "user-1",
|
||||||
|
trustedOrigin: () => true,
|
||||||
|
uploadStallTimeoutMs: stallMs,
|
||||||
|
});
|
||||||
|
const response = await hono.request(
|
||||||
|
new Request("http://localhost/api/artifacts?botId=bot-1&name=clip.mp4", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "content-type": "video/mp4" },
|
||||||
|
body,
|
||||||
|
duplex: "half",
|
||||||
|
} as RequestInit & { duplex: "half" }),
|
||||||
|
);
|
||||||
|
// The store creates the space directory before it reads the body, so it must exist here.
|
||||||
|
const stored = await readdir(path.join(root, "artifacts", "space-1"));
|
||||||
|
return { response, create, stored };
|
||||||
|
}
|
||||||
|
|
||||||
|
it("aborts a stalled upload with 408 and deletes the partial file", async () => {
|
||||||
|
const { body, cancel } = drip(["partial video bytes"], 0, true);
|
||||||
|
const { response, create, stored } = await postDrip(50, body);
|
||||||
|
expect(response.status).toBe(408);
|
||||||
|
expect(await response.json()).toEqual({ error: "Upload stalled." });
|
||||||
|
expect(cancel).toHaveBeenCalled();
|
||||||
|
expect(create).not.toHaveBeenCalled();
|
||||||
|
expect(stored).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps a slow but steady upload going past the stall timeout", async () => {
|
||||||
|
const { body } = drip(["a", "b", "c", "d", "e", "f"], 30, false);
|
||||||
|
const { response, create, stored } = await postDrip(100, body);
|
||||||
|
expect(response.status).toBe(201);
|
||||||
|
expect(create).toHaveBeenCalledWith({
|
||||||
|
data: expect.objectContaining({ size: 6, mimeType: "video/mp4", botId: "bot-1" }),
|
||||||
|
});
|
||||||
|
expect(stored).toHaveLength(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
336
apps/api/src/artifact-http.ts
Normal file
336
apps/api/src/artifact-http.ts
Normal file
@ -0,0 +1,336 @@
|
|||||||
|
import type { AdapterContext, ArtifactByteRange, ArtifactStore } from "@rakazo/adapter-kit";
|
||||||
|
import { ArtifactTooLargeError } from "@rakazo/adapter-kit";
|
||||||
|
import { storeArtifactStream } from "@rakazo/adapters";
|
||||||
|
import type { Actor } from "@rakazo/contracts";
|
||||||
|
import { artifactMaxBytes, attachmentKindOf, isAllowedAttachmentMimeType } from "@rakazo/contracts";
|
||||||
|
import type { PrismaClient } from "@rakazo/db";
|
||||||
|
import { createGroupRepos, createRepos, IsolationError, requireMembership } from "@rakazo/db";
|
||||||
|
import type { Context, Hono } from "hono";
|
||||||
|
import { cancelBody } from "./http-body.js";
|
||||||
|
|
||||||
|
export const ARTIFACT_HTTP_PATH = "/api/artifacts";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Node's default 5-minute requestTimeout cuts off a large upload on a slow uplink; 2 hours fits a
|
||||||
|
* 5 GiB repository ZIP at about 6 Mbit/s. headersTimeout keeps its default, and the per-upload
|
||||||
|
* stall timeout stops a slow-drip client from holding a connection that long.
|
||||||
|
*/
|
||||||
|
export const API_SERVER_OPTIONS = { requestTimeout: 2 * 60 * 60 * 1000 };
|
||||||
|
/** An upload that sends no bytes for this long is aborted and its partial file deleted. */
|
||||||
|
export const ARTIFACT_UPLOAD_STALL_TIMEOUT_MS = 60_000;
|
||||||
|
|
||||||
|
export class UploadStalledError extends Error {
|
||||||
|
constructor() {
|
||||||
|
super("Upload stalled");
|
||||||
|
this.name = "UploadStalledError";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Yields the body's chunks and throws UploadStalledError when the next chunk takes longer than
|
||||||
|
* stallMs. Only time spent waiting for the client counts, not time spent writing to disk.
|
||||||
|
*/
|
||||||
|
export async function* stallGuardedChunks(
|
||||||
|
body: ReadableStream<Uint8Array>,
|
||||||
|
stallMs: number,
|
||||||
|
): AsyncGenerator<Uint8Array> {
|
||||||
|
const reader = body.getReader();
|
||||||
|
try {
|
||||||
|
for (;;) {
|
||||||
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||||
|
const stalled = new Promise<never>((_, reject) => {
|
||||||
|
timer = setTimeout(() => reject(new UploadStalledError()), stallMs);
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
const { done, value } = await Promise.race([reader.read(), stalled]);
|
||||||
|
if (done) return;
|
||||||
|
yield value;
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
// Never wait on the client here: a stalled or hostile body may never confirm the cancel.
|
||||||
|
void reader.cancel().catch(() => undefined);
|
||||||
|
reader.releaseLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ArtifactHttpDeps = {
|
||||||
|
prisma: PrismaClient;
|
||||||
|
artifacts: ArtifactStore;
|
||||||
|
/** The signed-in user (cookie or bearer session), or null. */
|
||||||
|
sessionUserId: (request: Request) => Promise<string | null>;
|
||||||
|
/** Same rule as CORS. Raw uploads can be CORS "simple" requests, so the origin is checked. */
|
||||||
|
trustedOrigin: (origin: string) => boolean;
|
||||||
|
/** Overrides ARTIFACT_UPLOAD_STALL_TIMEOUT_MS (tests). */
|
||||||
|
uploadStallTimeoutMs?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Generated ids only; anything else is not an artifact. */
|
||||||
|
const ARTIFACT_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
||||||
|
const MAX_RANGE_DIGITS = 15;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a single `bytes=` range against the stored size. Returns null when the header should
|
||||||
|
* be ignored (absent or another unit) and "unsatisfiable" for multiple ranges or offsets
|
||||||
|
* outside the file, which the caller answers with 416.
|
||||||
|
*/
|
||||||
|
export function parseByteRange(
|
||||||
|
header: string | undefined,
|
||||||
|
size: number,
|
||||||
|
): ArtifactByteRange | "unsatisfiable" | null {
|
||||||
|
if (!header) return null;
|
||||||
|
const value = header.trim();
|
||||||
|
if (!value.toLowerCase().startsWith("bytes=")) return null;
|
||||||
|
const spec = value.slice("bytes=".length).trim();
|
||||||
|
const match = new RegExp(`^(\\d{0,${MAX_RANGE_DIGITS}})-(\\d{0,${MAX_RANGE_DIGITS}})$`).exec(
|
||||||
|
spec,
|
||||||
|
);
|
||||||
|
if (!match || size <= 0) return "unsatisfiable";
|
||||||
|
const [, first = "", last = ""] = match;
|
||||||
|
if (first === "" && last === "") return "unsatisfiable";
|
||||||
|
if (first === "") {
|
||||||
|
const suffix = Number(last);
|
||||||
|
if (suffix <= 0) return "unsatisfiable";
|
||||||
|
return { start: Math.max(0, size - suffix), end: size - 1 };
|
||||||
|
}
|
||||||
|
const start = Number(first);
|
||||||
|
const end = last === "" ? size - 1 : Number(last);
|
||||||
|
if (start >= size || end < start) return "unsatisfiable";
|
||||||
|
return { start, end: Math.min(end, size - 1) };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** `inline` or `attachment` with an ASCII fallback and the exact UTF-8 name (RFC 6266). */
|
||||||
|
export function contentDisposition(name: string, inline: boolean): string {
|
||||||
|
const cleaned =
|
||||||
|
Array.from(name, (char) => {
|
||||||
|
const code = char.codePointAt(0) ?? 0;
|
||||||
|
return code < 0x20 || code === 0x7f || char === "/" || char === "\\" ? "_" : char;
|
||||||
|
})
|
||||||
|
.join("")
|
||||||
|
.trim() || "download";
|
||||||
|
const ascii = cleaned.replace(/[^ -~]/g, "_").replace(/["%;]/g, "_");
|
||||||
|
const encoded = encodeURIComponent(cleaned).replace(
|
||||||
|
/['()*]/g,
|
||||||
|
(char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`,
|
||||||
|
);
|
||||||
|
return `${inline ? "inline" : "attachment"}; filename="${ascii}"; filename*=UTF-8''${encoded}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function adapterContext(actor: Actor, operationId: string, signal: AbortSignal): AdapterContext {
|
||||||
|
return {
|
||||||
|
operationId,
|
||||||
|
traceId: operationId,
|
||||||
|
spaceId: actor.spaceId,
|
||||||
|
userId: actor.userId,
|
||||||
|
signal,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function notFound(c: Context) {
|
||||||
|
return c.json({ error: "Not found" }, 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
function artifactDto(row: {
|
||||||
|
id: string;
|
||||||
|
botId: string | null;
|
||||||
|
groupId: string | null;
|
||||||
|
runId: string | null;
|
||||||
|
name: string;
|
||||||
|
mimeType: string;
|
||||||
|
size: number;
|
||||||
|
createdAt: Date;
|
||||||
|
}) {
|
||||||
|
return {
|
||||||
|
id: row.id,
|
||||||
|
botId: row.botId,
|
||||||
|
groupId: row.groupId,
|
||||||
|
runId: row.runId,
|
||||||
|
name: row.name,
|
||||||
|
mimeType: row.mimeType,
|
||||||
|
size: row.size,
|
||||||
|
createdAt: row.createdAt.toISOString(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Streams artifacts over HTTP so media can play with seeking and large files never travel as
|
||||||
|
* base64 RPC payloads. Authorization matches `artifacts.get` and `artifacts.create`.
|
||||||
|
*/
|
||||||
|
export function mountArtifactHttpRoutes(app: Hono, deps: ArtifactHttpDeps) {
|
||||||
|
const repos = createRepos(deps.prisma);
|
||||||
|
const groupRepos = createGroupRepos(deps.prisma);
|
||||||
|
|
||||||
|
/** Same rule as artifacts.get: the owner's artifact of an active bot thread or group. */
|
||||||
|
async function readableArtifact(
|
||||||
|
userId: string,
|
||||||
|
requestedSpaceId: string | undefined,
|
||||||
|
id: string,
|
||||||
|
) {
|
||||||
|
if (!ARTIFACT_ID.test(id)) return null;
|
||||||
|
const row = await deps.prisma.artifact.findUnique({ where: { id } });
|
||||||
|
if (!row) return null;
|
||||||
|
// <video> and <img> cannot send the space header, so the artifact's space is the default;
|
||||||
|
// membership is still required and an explicit header must name the same space.
|
||||||
|
const actor = await requireMembership(
|
||||||
|
deps.prisma,
|
||||||
|
userId,
|
||||||
|
requestedSpaceId ?? row.spaceId,
|
||||||
|
).catch(() => null);
|
||||||
|
if (!actor || actor.spaceId !== row.spaceId || actor.userId !== row.userId) return null;
|
||||||
|
try {
|
||||||
|
if (row.groupId) await groupRepos.getGroupTarget(actor, row.groupId);
|
||||||
|
else if (row.botId) await repos.getBot(actor, row.botId);
|
||||||
|
else return null;
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof IsolationError) return null;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
return { row, actor };
|
||||||
|
}
|
||||||
|
|
||||||
|
app.get(`${ARTIFACT_HTTP_PATH}/:id`, async (c) => {
|
||||||
|
const userId = await deps.sessionUserId(c.req.raw);
|
||||||
|
if (!userId) return c.json({ error: "Unauthorized" }, 401);
|
||||||
|
const found = await readableArtifact(
|
||||||
|
userId,
|
||||||
|
c.req.header("x-rakazo-space-id"),
|
||||||
|
c.req.param("id"),
|
||||||
|
);
|
||||||
|
if (!found) return notFound(c);
|
||||||
|
const { row, actor } = found;
|
||||||
|
const context = adapterContext(actor, `artifact-http-get:${row.id}`, c.req.raw.signal);
|
||||||
|
let size: number;
|
||||||
|
try {
|
||||||
|
({ size } = await deps.artifacts.stat(row.storageKey, context));
|
||||||
|
} catch {
|
||||||
|
return notFound(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
const mimeType = isAllowedAttachmentMimeType(row.mimeType)
|
||||||
|
? row.mimeType
|
||||||
|
: "application/octet-stream";
|
||||||
|
const inline = attachmentKindOf(mimeType) !== "file" && c.req.query("download") === undefined;
|
||||||
|
const etag = /^[A-Za-z0-9]{16,128}$/.test(row.hash) ? `"${row.hash}"` : undefined;
|
||||||
|
const headers = new Headers({
|
||||||
|
"accept-ranges": "bytes",
|
||||||
|
// Revalidate every time so a revoked thread stops serving from the browser cache.
|
||||||
|
"cache-control": "private, no-cache",
|
||||||
|
"content-disposition": contentDisposition(row.name, inline),
|
||||||
|
"content-type": mimeType,
|
||||||
|
"cross-origin-resource-policy": "same-origin",
|
||||||
|
"x-content-type-options": "nosniff",
|
||||||
|
});
|
||||||
|
if (etag) headers.set("etag", etag);
|
||||||
|
if (etag && c.req.header("if-none-match") === etag) {
|
||||||
|
return new Response(null, { status: 304, headers });
|
||||||
|
}
|
||||||
|
|
||||||
|
const ifRange = c.req.header("if-range");
|
||||||
|
const range =
|
||||||
|
ifRange === undefined || ifRange === etag
|
||||||
|
? parseByteRange(c.req.header("range"), size)
|
||||||
|
: null;
|
||||||
|
if (range === "unsatisfiable") {
|
||||||
|
headers.set("content-range", `bytes */${size}`);
|
||||||
|
return new Response(null, { status: 416, headers });
|
||||||
|
}
|
||||||
|
const status = range ? 206 : 200;
|
||||||
|
headers.set("content-length", String(range ? range.end - range.start + 1 : size));
|
||||||
|
if (range) headers.set("content-range", `bytes ${range.start}-${range.end}/${size}`);
|
||||||
|
// Hono answers HEAD from this handler and drops the body; never open a stream for it.
|
||||||
|
if (c.req.method === "HEAD") return new Response(null, { status, headers });
|
||||||
|
|
||||||
|
let body: ReadableStream<Uint8Array>;
|
||||||
|
try {
|
||||||
|
body = await deps.artifacts.createReadStream(row.storageKey, context, range ?? undefined);
|
||||||
|
} catch {
|
||||||
|
return notFound(c);
|
||||||
|
}
|
||||||
|
return new Response(body, { status, headers });
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post(ARTIFACT_HTTP_PATH, async (c) => {
|
||||||
|
const request = c.req.raw;
|
||||||
|
const reject = (status: 400 | 401 | 403 | 404 | 408 | 413 | 415, error: string) => {
|
||||||
|
cancelBody(request.body);
|
||||||
|
return c.json({ error }, status);
|
||||||
|
};
|
||||||
|
const origin = c.req.header("origin");
|
||||||
|
if (origin && !deps.trustedOrigin(origin)) return reject(403, "Forbidden");
|
||||||
|
const userId = await deps.sessionUserId(request);
|
||||||
|
if (!userId) return reject(401, "Unauthorized");
|
||||||
|
const actor = await requireMembership(
|
||||||
|
deps.prisma,
|
||||||
|
userId,
|
||||||
|
c.req.header("x-rakazo-space-id"),
|
||||||
|
).catch(() => null);
|
||||||
|
if (!actor) return reject(401, "Unauthorized");
|
||||||
|
|
||||||
|
const botId = c.req.query("botId") || undefined;
|
||||||
|
const groupId = c.req.query("groupId") || undefined;
|
||||||
|
const name = c.req.query("name") ?? "";
|
||||||
|
if (Boolean(botId) === Boolean(groupId)) {
|
||||||
|
return reject(400, "Provide exactly one of botId or groupId");
|
||||||
|
}
|
||||||
|
if (name.length < 1 || name.length > 255) return reject(400, "Invalid file name");
|
||||||
|
const mimeType = (c.req.header("content-type") ?? "").split(";", 1)[0]!.trim().toLowerCase();
|
||||||
|
if (!isAllowedAttachmentMimeType(mimeType)) {
|
||||||
|
return reject(415, "Unsupported attachment type");
|
||||||
|
}
|
||||||
|
const maxBytes = artifactMaxBytes(mimeType);
|
||||||
|
const declared = c.req.header("content-length");
|
||||||
|
if (declared !== undefined) {
|
||||||
|
if (!/^\d{1,16}$/.test(declared)) return reject(400, "Invalid content length");
|
||||||
|
if (Number(declared) > maxBytes) return reject(413, "File is too large.");
|
||||||
|
if (Number(declared) === 0) return reject(400, "File is empty.");
|
||||||
|
}
|
||||||
|
if (!request.body) return reject(400, "File is empty.");
|
||||||
|
|
||||||
|
// Same targets as artifacts.create: an own active bot, or an active group of own bots.
|
||||||
|
let ownerBotId: string | undefined;
|
||||||
|
try {
|
||||||
|
ownerBotId = botId
|
||||||
|
? (await repos.getBot(actor, botId)).id
|
||||||
|
: (await groupRepos.getGroupTarget(actor, groupId!)).members[0]?.bot.id;
|
||||||
|
} catch (error) {
|
||||||
|
if (!(error instanceof IsolationError)) throw error;
|
||||||
|
}
|
||||||
|
if (!ownerBotId) return reject(404, "Not found");
|
||||||
|
|
||||||
|
let row: Awaited<ReturnType<typeof storeArtifactStream>>;
|
||||||
|
try {
|
||||||
|
row = await storeArtifactStream(deps, {
|
||||||
|
spaceId: actor.spaceId,
|
||||||
|
userId: actor.userId,
|
||||||
|
botId: ownerBotId,
|
||||||
|
groupId: groupId ?? null,
|
||||||
|
runId: null,
|
||||||
|
name,
|
||||||
|
mimeType,
|
||||||
|
stream: stallGuardedChunks(
|
||||||
|
request.body,
|
||||||
|
deps.uploadStallTimeoutMs ?? ARTIFACT_UPLOAD_STALL_TIMEOUT_MS,
|
||||||
|
),
|
||||||
|
maxBytes,
|
||||||
|
signal: request.signal,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof ArtifactTooLargeError) return reject(413, "File is too large.");
|
||||||
|
if (error instanceof UploadStalledError) return reject(408, "Upload stalled.");
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
if (row.size === 0) {
|
||||||
|
await deps.prisma.artifact.delete({ where: { id: row.id } });
|
||||||
|
await deps.artifacts
|
||||||
|
.remove(
|
||||||
|
row.storageKey,
|
||||||
|
adapterContext(actor, `artifact-http-post:${row.id}`, request.signal),
|
||||||
|
)
|
||||||
|
.catch(() => undefined);
|
||||||
|
return c.json({ error: "File is empty." }, 400);
|
||||||
|
}
|
||||||
|
return c.json(artifactDto(row), 201);
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -11,6 +11,12 @@ import {
|
|||||||
} from "@rakazo/core";
|
} from "@rakazo/core";
|
||||||
import { IsolationError, type PrismaClient } from "@rakazo/db";
|
import { IsolationError, type PrismaClient } from "@rakazo/db";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base64 reads load the whole file into memory; larger media is served by
|
||||||
|
* GET /api/artifacts/:id instead.
|
||||||
|
*/
|
||||||
|
export const ARTIFACT_INLINE_READ_MAX_BYTES = 25 * 1024 * 1024;
|
||||||
|
|
||||||
function adapterContext(actor: Actor, botId: string, operationId: string) {
|
function adapterContext(actor: Actor, botId: string, operationId: string) {
|
||||||
return {
|
return {
|
||||||
operationId,
|
operationId,
|
||||||
@ -131,6 +137,9 @@ async function readArtifact(
|
|||||||
},
|
},
|
||||||
contextBotId: string,
|
contextBotId: string,
|
||||||
) {
|
) {
|
||||||
|
if (row.size > ARTIFACT_INLINE_READ_MAX_BYTES) {
|
||||||
|
throw new AttachmentValidationError("This file is too large to load inline.");
|
||||||
|
}
|
||||||
const bytes = await artifacts.get(
|
const bytes = await artifacts.get(
|
||||||
row.storageKey,
|
row.storageKey,
|
||||||
adapterContext(actor, contextBotId, `artifact-get:${row.id}`),
|
adapterContext(actor, contextBotId, `artifact-get:${row.id}`),
|
||||||
|
|||||||
106
apps/api/src/autopilot.ts
Normal file
106
apps/api/src/autopilot.ts
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
import { ORPCError } from "@orpc/server";
|
||||||
|
import type { JobPublisher, SandboxProvider } from "@rakazo/adapter-kit";
|
||||||
|
import {
|
||||||
|
autopilotView,
|
||||||
|
endAutopilot,
|
||||||
|
findOpenAutopilot,
|
||||||
|
kickAutopilot,
|
||||||
|
resumeAutopilot,
|
||||||
|
startAutopilot,
|
||||||
|
} from "@rakazo/adapters";
|
||||||
|
import type { Actor, Autopilot, AutopilotRecent } from "@rakazo/contracts";
|
||||||
|
import { AUTOPILOT_RECENT_MS } from "@rakazo/contracts";
|
||||||
|
import type { PrismaClient, ThreadEvents } from "@rakazo/db";
|
||||||
|
import { IsolationError } from "@rakazo/db";
|
||||||
|
import type { ThreadTarget } from "./thread-target.js";
|
||||||
|
import { groupDefaultBotId, stopThreadRuns } from "./thread-target.js";
|
||||||
|
|
||||||
|
type AutopilotDeps = {
|
||||||
|
prisma: PrismaClient;
|
||||||
|
events: ThreadEvents;
|
||||||
|
jobs: JobPublisher;
|
||||||
|
sandbox: SandboxProvider;
|
||||||
|
};
|
||||||
|
|
||||||
|
function owner(actor: Actor, target: ThreadTarget) {
|
||||||
|
const botId = target.kind === "bot" ? target.botId : groupDefaultBotId(target);
|
||||||
|
if (!botId) throw new IsolationError();
|
||||||
|
return { spaceId: actor.spaceId, userId: actor.userId, botId, threadId: target.threadId };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getThreadAutopilot(
|
||||||
|
deps: Pick<AutopilotDeps, "prisma">,
|
||||||
|
actor: Actor,
|
||||||
|
target: ThreadTarget,
|
||||||
|
): Promise<Autopilot | null> {
|
||||||
|
const open = await findOpenAutopilot(deps.prisma, owner(actor, target));
|
||||||
|
return open ? autopilotView(open) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Start from the composer: the first run begins at once. */
|
||||||
|
export async function startThreadAutopilot(
|
||||||
|
deps: AutopilotDeps,
|
||||||
|
actor: Actor,
|
||||||
|
target: ThreadTarget,
|
||||||
|
input: { goal: string; focus?: string; until?: string },
|
||||||
|
): Promise<Autopilot> {
|
||||||
|
const started = await startAutopilot(deps, owner(actor, target), {
|
||||||
|
goal: input.goal,
|
||||||
|
focus: input.focus,
|
||||||
|
until: input.until ? new Date(input.until) : undefined,
|
||||||
|
});
|
||||||
|
if ("error" in started) throw new ORPCError("CONFLICT", { message: started.error });
|
||||||
|
await kickAutopilot(deps, started.autopilot.id);
|
||||||
|
return autopilotView(started.autopilot);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Stop at once: end the autopilot first so no run starts another, then stop what runs. */
|
||||||
|
export async function stopThreadAutopilot(
|
||||||
|
deps: AutopilotDeps,
|
||||||
|
actor: Actor,
|
||||||
|
target: ThreadTarget,
|
||||||
|
): Promise<{ ok: true }> {
|
||||||
|
const open = await findOpenAutopilot(deps.prisma, owner(actor, target));
|
||||||
|
if (open && (await endAutopilot(deps, open.id, { status: "stopped" }))) {
|
||||||
|
await stopThreadRuns(deps, actor, target);
|
||||||
|
}
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function resumeThreadAutopilot(
|
||||||
|
deps: AutopilotDeps,
|
||||||
|
actor: Actor,
|
||||||
|
target: ThreadTarget,
|
||||||
|
): Promise<{ ok: true }> {
|
||||||
|
const open = await findOpenAutopilot(deps.prisma, owner(actor, target));
|
||||||
|
if (open?.status !== "paused" || !(await resumeAutopilot(deps, open.id))) {
|
||||||
|
throw new ORPCError("CONFLICT", { message: "This autopilot is not paused." });
|
||||||
|
}
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Today's view: the user's open autopilots in this space and those that ended within a day. */
|
||||||
|
export async function listRecentAutopilots(
|
||||||
|
deps: Pick<AutopilotDeps, "prisma">,
|
||||||
|
actor: Actor,
|
||||||
|
now = new Date(),
|
||||||
|
): Promise<AutopilotRecent[]> {
|
||||||
|
const rows = await deps.prisma.autopilot.findMany({
|
||||||
|
where: {
|
||||||
|
spaceId: actor.spaceId,
|
||||||
|
userId: actor.userId,
|
||||||
|
OR: [
|
||||||
|
{ status: { in: ["active", "paused"] } },
|
||||||
|
{ endedAt: { gte: new Date(now.getTime() - AUTOPILOT_RECENT_MS) } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
include: { thread: { select: { groupId: true } } },
|
||||||
|
orderBy: { startedAt: "desc" },
|
||||||
|
take: 10,
|
||||||
|
});
|
||||||
|
return rows.map((row) => ({
|
||||||
|
...autopilotView(row),
|
||||||
|
spaceId: row.spaceId,
|
||||||
|
groupId: row.thread.groupId,
|
||||||
|
}));
|
||||||
|
}
|
||||||
74
apps/api/src/bot-import.test.ts
Normal file
74
apps/api/src/bot-import.test.ts
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { importableRoutines, relativeImportedFiles, safeImportPath } from "./bot-import.js";
|
||||||
|
|
||||||
|
describe("safeImportPath", () => {
|
||||||
|
it("normalizes separators and dot segments", () => {
|
||||||
|
expect(safeImportPath("notes\\./result.txt")).toBe("notes/result.txt");
|
||||||
|
expect(safeImportPath("MEMORY.md")).toBe("MEMORY.md");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects paths that could leave the bot home", () => {
|
||||||
|
expect(safeImportPath("../secrets")).toBeNull();
|
||||||
|
expect(safeImportPath("notes/../../x")).toBeNull();
|
||||||
|
expect(safeImportPath("/etc/passwd")).toBeNull();
|
||||||
|
expect(safeImportPath("C:\\Windows")).toBeNull();
|
||||||
|
expect(safeImportPath("./")).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("relativeImportedFiles", () => {
|
||||||
|
it("drops the old Team Computer workspace prefix", () => {
|
||||||
|
expect(
|
||||||
|
relativeImportedFiles([
|
||||||
|
{ path: "bots/old-id/notes/result.txt", content: "a" },
|
||||||
|
{ path: "bots/old-id/README.md", content: "b" },
|
||||||
|
]),
|
||||||
|
).toEqual([
|
||||||
|
{ path: "notes/result.txt", content: "a" },
|
||||||
|
{ path: "README.md", content: "b" },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps Private Computer paths as they are", () => {
|
||||||
|
const files = [
|
||||||
|
{ path: "notes/result.txt", content: "a" },
|
||||||
|
{ path: "bots/other/file.txt", content: "b" },
|
||||||
|
];
|
||||||
|
expect(relativeImportedFiles(files)).toEqual(files);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps paths when files span several workspaces", () => {
|
||||||
|
const files = [
|
||||||
|
{ path: "bots/a/x.txt", content: "a" },
|
||||||
|
{ path: "bots/b/y.txt", content: "b" },
|
||||||
|
];
|
||||||
|
expect(relativeImportedFiles(files)).toEqual(files);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("refuses the whole set when one path is unsafe", () => {
|
||||||
|
expect(
|
||||||
|
relativeImportedFiles([
|
||||||
|
{ path: "ok.txt", content: "a" },
|
||||||
|
{ path: "../escape.txt", content: "b" },
|
||||||
|
]),
|
||||||
|
).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("accepts an export without files", () => {
|
||||||
|
expect(relativeImportedFiles([])).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("importableRoutines", () => {
|
||||||
|
it("leaves out one-shot schedules", () => {
|
||||||
|
const routines = [
|
||||||
|
{ name: "daily", crons: ["0 9 * * *"] },
|
||||||
|
{ name: "once", crons: ["@once"] },
|
||||||
|
{ name: "webhook", crons: [] },
|
||||||
|
];
|
||||||
|
expect(importableRoutines(routines).map((routine) => routine.name)).toEqual([
|
||||||
|
"daily",
|
||||||
|
"webhook",
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
34
apps/api/src/bot-import.ts
Normal file
34
apps/api/src/bot-import.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import { isOneShotRoutineCrons } from "@rakazo/core";
|
||||||
|
|
||||||
|
/** A relative, forward-slash path without `.`/`..` segments, or null when it could escape. */
|
||||||
|
export function safeImportPath(raw: string): string | null {
|
||||||
|
const normalized = raw.replace(/\\/g, "/");
|
||||||
|
if (normalized.startsWith("/") || /^[a-z]:/i.test(normalized)) return null;
|
||||||
|
const segments = normalized.split("/").filter((segment) => segment !== "" && segment !== ".");
|
||||||
|
if (segments.length === 0 || segments.includes("..")) return null;
|
||||||
|
return segments.join("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exports of bots on a Team Computer carry their workspace prefix (`bots/<old id>/…`).
|
||||||
|
* The prefix names the old bot, so it is dropped and the importer writes under the new one.
|
||||||
|
*/
|
||||||
|
export function relativeImportedFiles<T extends { path: string }>(
|
||||||
|
files: T[],
|
||||||
|
): Array<T & { path: string }> | null {
|
||||||
|
const safe: Array<T & { path: string }> = [];
|
||||||
|
for (const file of files) {
|
||||||
|
const path = safeImportPath(file.path);
|
||||||
|
if (!path) return null;
|
||||||
|
safe.push({ ...file, path });
|
||||||
|
}
|
||||||
|
const first = safe[0]?.path.split("/");
|
||||||
|
const workspace = first && first.length > 2 && first[0] === "bots" ? `bots/${first[1]}/` : null;
|
||||||
|
if (!workspace || !safe.every((file) => file.path.startsWith(workspace))) return safe;
|
||||||
|
return safe.map((file) => ({ ...file, path: file.path.slice(workspace.length) }));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** One-shot schedules belong to the chat that set them, so they are not carried over. */
|
||||||
|
export function importableRoutines<T extends { crons: string[] }>(routines: T[]): T[] {
|
||||||
|
return routines.filter((routine) => !isOneShotRoutineCrons(routine.crons));
|
||||||
|
}
|
||||||
96
apps/api/src/coach.ts
Normal file
96
apps/api/src/coach.ts
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
import { ORPCError } from "@orpc/server";
|
||||||
|
import type { JobPublisher } from "@rakazo/adapter-kit";
|
||||||
|
import { getCoachRhythm, setCoachRhythm } from "@rakazo/adapters";
|
||||||
|
import type {
|
||||||
|
Actor,
|
||||||
|
CheckIn,
|
||||||
|
CoachRhythm,
|
||||||
|
CoachToday,
|
||||||
|
Goal,
|
||||||
|
Habit,
|
||||||
|
LogCheckIn,
|
||||||
|
} from "@rakazo/contracts";
|
||||||
|
import type { CheckInFilter, GoalPatch, HabitPatch, PrismaClient } from "@rakazo/db";
|
||||||
|
import {
|
||||||
|
CoachNotFoundError,
|
||||||
|
createGoal,
|
||||||
|
createHabit,
|
||||||
|
InvalidCoachInputError,
|
||||||
|
listCheckIns,
|
||||||
|
listGoals,
|
||||||
|
listHabits,
|
||||||
|
loadCoachToday,
|
||||||
|
logCheckIn,
|
||||||
|
removeGoal,
|
||||||
|
removeHabit,
|
||||||
|
updateGoal,
|
||||||
|
updateHabit,
|
||||||
|
} from "@rakazo/db";
|
||||||
|
|
||||||
|
export interface CoachDeps {
|
||||||
|
prisma: PrismaClient;
|
||||||
|
jobs: JobPublisher;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Another user's ids read as missing, so a caller cannot probe them. */
|
||||||
|
async function mapErrors<T>(work: () => Promise<T>): Promise<T> {
|
||||||
|
try {
|
||||||
|
return await work();
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof CoachNotFoundError) {
|
||||||
|
throw new ORPCError("NOT_FOUND", { message: error.message });
|
||||||
|
}
|
||||||
|
if (error instanceof InvalidCoachInputError) {
|
||||||
|
throw new ORPCError("BAD_REQUEST", { message: error.message });
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Coach procedures for the signed-in user only; nothing here is space-scoped. */
|
||||||
|
export function createCoachService(deps: CoachDeps) {
|
||||||
|
const { prisma } = deps;
|
||||||
|
return {
|
||||||
|
today: (actor: Actor): Promise<CoachToday> => loadCoachToday(prisma, actor.userId),
|
||||||
|
|
||||||
|
listGoals: (actor: Actor, input: { status?: Goal["status"] }) =>
|
||||||
|
listGoals(prisma, actor.userId, input),
|
||||||
|
createGoal: (
|
||||||
|
actor: Actor,
|
||||||
|
input: {
|
||||||
|
title: string;
|
||||||
|
detail?: string;
|
||||||
|
targetDate?: string | null;
|
||||||
|
spaceId?: string | null;
|
||||||
|
},
|
||||||
|
) => mapErrors(() => createGoal(prisma, actor.userId, { ...input, source: "user" })),
|
||||||
|
updateGoal: (actor: Actor, { goalId, ...patch }: GoalPatch & { goalId: string }) =>
|
||||||
|
mapErrors(() => updateGoal(prisma, actor.userId, goalId, patch)),
|
||||||
|
removeGoal: async (actor: Actor, input: { goalId: string }) => {
|
||||||
|
await mapErrors(() => removeGoal(prisma, actor.userId, input.goalId));
|
||||||
|
return { ok: true as const };
|
||||||
|
},
|
||||||
|
|
||||||
|
listHabits: (actor: Actor, input: { includeInactive?: boolean }): Promise<Habit[]> =>
|
||||||
|
listHabits(prisma, actor.userId, input),
|
||||||
|
createHabit: (
|
||||||
|
actor: Actor,
|
||||||
|
input: { title: string; cadence: Habit["cadence"]; goalId?: string | null },
|
||||||
|
) => mapErrors(() => createHabit(prisma, actor.userId, { ...input, source: "user" })),
|
||||||
|
updateHabit: (actor: Actor, { habitId, ...patch }: HabitPatch & { habitId: string }) =>
|
||||||
|
mapErrors(() => updateHabit(prisma, actor.userId, habitId, patch)),
|
||||||
|
removeHabit: async (actor: Actor, input: { habitId: string }) => {
|
||||||
|
await mapErrors(() => removeHabit(prisma, actor.userId, input.habitId));
|
||||||
|
return { ok: true as const };
|
||||||
|
},
|
||||||
|
|
||||||
|
logCheckIn: (actor: Actor, input: LogCheckIn): Promise<CheckIn> =>
|
||||||
|
mapErrors(() => logCheckIn(prisma, actor.userId, input)),
|
||||||
|
listCheckIns: (actor: Actor, input: CheckInFilter) =>
|
||||||
|
mapErrors(() => listCheckIns(prisma, actor.userId, input)),
|
||||||
|
|
||||||
|
getRhythm: (actor: Actor): Promise<CoachRhythm> => getCoachRhythm(prisma, actor.userId),
|
||||||
|
setRhythm: (actor: Actor, rhythm: CoachRhythm): Promise<CoachRhythm> =>
|
||||||
|
setCoachRhythm(deps, { userId: actor.userId, rhythm }),
|
||||||
|
};
|
||||||
|
}
|
||||||
50
apps/api/src/connected-app-facts.ts
Normal file
50
apps/api/src/connected-app-facts.ts
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import { FEATURED_CONNECTOR_LABELS, matchFeaturedConnectorId } from "@rakazo/core";
|
||||||
|
import type { PrismaClient } from "@rakazo/db";
|
||||||
|
import { updateUserProfile } from "@rakazo/db";
|
||||||
|
import { getLogger } from "@rakazo/logging";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The app's own name: a featured app's label, otherwise the label of the user's first account for
|
||||||
|
* it (later accounts may be named "Gmail 2" or renamed).
|
||||||
|
*/
|
||||||
|
async function connectedAppName(
|
||||||
|
prisma: PrismaClient,
|
||||||
|
connection: { userId: string; connectorId: string; provider: string },
|
||||||
|
): Promise<string | null> {
|
||||||
|
const featured = matchFeaturedConnectorId(connection.provider);
|
||||||
|
if (featured) return FEATURED_CONNECTOR_LABELS[featured];
|
||||||
|
const first = await prisma.connection.findFirst({
|
||||||
|
where: {
|
||||||
|
userId: connection.userId,
|
||||||
|
connectorId: connection.connectorId,
|
||||||
|
provider: connection.provider,
|
||||||
|
},
|
||||||
|
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
|
||||||
|
select: { displayName: true },
|
||||||
|
});
|
||||||
|
const name = first?.displayName.trim().slice(0, 80);
|
||||||
|
return name || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remembers "Uses <App>" in the user's profile when one of their connections becomes connected.
|
||||||
|
* Revoking the connection keeps the fact; the user removes it in About you. Best effort: the
|
||||||
|
* connection itself has already succeeded, so a full profile or a database error only logs.
|
||||||
|
*/
|
||||||
|
export async function recordConnectedApp(
|
||||||
|
prisma: PrismaClient,
|
||||||
|
connection: { userId: string; connectorId: string; provider: string },
|
||||||
|
): Promise<void> {
|
||||||
|
try {
|
||||||
|
const name = await connectedAppName(prisma, connection);
|
||||||
|
if (!name) return;
|
||||||
|
await updateUserProfile(prisma, connection.userId, {
|
||||||
|
addFacts: [{ category: "tools", value: `Uses ${name}`, source: "connector" }],
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
getLogger().error("connected app fact was not recorded", error, {
|
||||||
|
connectorId: connection.connectorId,
|
||||||
|
provider: connection.provider,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -20,6 +20,7 @@ export interface AppEnv {
|
|||||||
authSecret: string;
|
authSecret: string;
|
||||||
authUrl: string;
|
authUrl: string;
|
||||||
webOrigin: string;
|
webOrigin: string;
|
||||||
|
extraTrustedOrigins: string[];
|
||||||
privacyPolicyUrl?: string;
|
privacyPolicyUrl?: string;
|
||||||
apiUrl: string;
|
apiUrl: string;
|
||||||
apiHost: string;
|
apiHost: string;
|
||||||
@ -109,6 +110,10 @@ export function loadEnv(source: NodeJS.ProcessEnv = process.env): AppEnv {
|
|||||||
authSecret,
|
authSecret,
|
||||||
authUrl: source.BETTER_AUTH_URL ?? source.WEB_ORIGIN ?? "http://127.0.0.1:5173",
|
authUrl: source.BETTER_AUTH_URL ?? source.WEB_ORIGIN ?? "http://127.0.0.1:5173",
|
||||||
webOrigin: source.WEB_ORIGIN ?? "http://127.0.0.1:5173",
|
webOrigin: source.WEB_ORIGIN ?? "http://127.0.0.1:5173",
|
||||||
|
extraTrustedOrigins: (source.EXTRA_TRUSTED_ORIGINS ?? "")
|
||||||
|
.split(",")
|
||||||
|
.map((s) => s.trim())
|
||||||
|
.filter(Boolean),
|
||||||
privacyPolicyUrl: optional(source.PRIVACY_POLICY_URL),
|
privacyPolicyUrl: optional(source.PRIVACY_POLICY_URL),
|
||||||
apiUrl: source.API_URL ?? "http://127.0.0.1:3100",
|
apiUrl: source.API_URL ?? "http://127.0.0.1:3100",
|
||||||
apiHost: source.API_HOST ?? "127.0.0.1",
|
apiHost: source.API_HOST ?? "127.0.0.1",
|
||||||
|
|||||||
@ -1,8 +1,10 @@
|
|||||||
|
import { buildSuggestionCard, withSuggestionStatus } from "@rakazo/core";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import {
|
import {
|
||||||
buildInboxItems,
|
buildInboxItems,
|
||||||
type InboxMessageRow,
|
type InboxMessageRow,
|
||||||
type InboxRunRow,
|
type InboxRunRow,
|
||||||
|
type InboxSuggestionRow,
|
||||||
type InboxUnreadThreadRow,
|
type InboxUnreadThreadRow,
|
||||||
} from "./inbox.js";
|
} from "./inbox.js";
|
||||||
|
|
||||||
@ -18,9 +20,11 @@ function run(
|
|||||||
status: "waiting_input" | "waiting_takeover",
|
status: "waiting_input" | "waiting_takeover",
|
||||||
thread: Partial<InboxRunRow["thread"]> = {},
|
thread: Partial<InboxRunRow["thread"]> = {},
|
||||||
bot = scout,
|
bot = scout,
|
||||||
|
spaceId = "space-home",
|
||||||
): InboxRunRow {
|
): InboxRunRow {
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
|
spaceId,
|
||||||
status,
|
status,
|
||||||
updatedAt: at(0),
|
updatedAt: at(0),
|
||||||
bot,
|
bot,
|
||||||
@ -48,8 +52,10 @@ const approval = {
|
|||||||
function unreadThread(
|
function unreadThread(
|
||||||
bot: InboxUnreadThreadRow["bot"],
|
bot: InboxUnreadThreadRow["bot"],
|
||||||
latest?: { id: string; text: string; minute: number },
|
latest?: { id: string; text: string; minute: number },
|
||||||
|
spaceId = "space-home",
|
||||||
): InboxUnreadThreadRow {
|
): InboxUnreadThreadRow {
|
||||||
return {
|
return {
|
||||||
|
spaceId,
|
||||||
createdAt: at(0),
|
createdAt: at(0),
|
||||||
bot,
|
bot,
|
||||||
messages: latest
|
messages: latest
|
||||||
@ -227,4 +233,105 @@ describe("buildInboxItems", () => {
|
|||||||
});
|
});
|
||||||
expect(items[2]).toMatchObject({ messageId: null, text: "", createdAt: at(0).toISOString() });
|
expect(items[2]).toMatchObject({ messageId: null, text: "", createdAt: at(0).toISOString() });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("lists asks from another space's main conversation, but not the open space's own", () => {
|
||||||
|
const project = { id: "space-launch", name: "Launch" };
|
||||||
|
const main = (groupId: string) => ({
|
||||||
|
botId: null,
|
||||||
|
groupId,
|
||||||
|
group: { kind: "main", archivedAt: null },
|
||||||
|
});
|
||||||
|
const items = buildInboxItems({
|
||||||
|
runs: [
|
||||||
|
run("home-main", "waiting_input", main("group-main-home")),
|
||||||
|
run("project-main", "waiting_input", main("group-main-launch"), writer, project.id),
|
||||||
|
],
|
||||||
|
messages: [
|
||||||
|
message("ask-home", "home-main", [question], 1),
|
||||||
|
message("ask-project", "project-main", [question], 2),
|
||||||
|
],
|
||||||
|
unreadThreads: [],
|
||||||
|
otherSpaces: new Map([[project.id, project]]),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(items).toHaveLength(1);
|
||||||
|
expect(items[0]).toMatchObject({
|
||||||
|
runId: "project-main",
|
||||||
|
space: project,
|
||||||
|
target: { groupId: "group-main-launch" },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("names the space of items from the user's other spaces", () => {
|
||||||
|
const side = { id: "space-side", name: "Side project" };
|
||||||
|
const items = buildInboxItems({
|
||||||
|
runs: [
|
||||||
|
run("run-home", "waiting_input"),
|
||||||
|
run("run-side", "waiting_input", {}, writer, side.id),
|
||||||
|
run("takeover-side", "waiting_takeover", {}, writer, side.id),
|
||||||
|
],
|
||||||
|
messages: [
|
||||||
|
message("ask-home", "run-home", [question], 1),
|
||||||
|
message("ask-side", "run-side", [question], 2),
|
||||||
|
],
|
||||||
|
unreadThreads: [
|
||||||
|
unreadThread(
|
||||||
|
{ id: "bot-quiet", name: "Quiet", color: "#3EC5A8" },
|
||||||
|
{ id: "quiet-latest", text: "Done", minute: 3 },
|
||||||
|
side.id,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
otherSpaces: new Map([[side.id, side]]),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(items.map((item) => [item.kind, item.space?.name ?? null])).toEqual([
|
||||||
|
["takeover", "Side project"],
|
||||||
|
["ask", null],
|
||||||
|
["ask", "Side project"],
|
||||||
|
["unread", "Side project"],
|
||||||
|
]);
|
||||||
|
expect(items[1]).not.toHaveProperty("space");
|
||||||
|
expect(items[2]).toMatchObject({ space: side, target: { botId: writer.id } });
|
||||||
|
});
|
||||||
|
it("lists an open suggestion with its card, and drops it once settled", () => {
|
||||||
|
const card = buildSuggestionCard({
|
||||||
|
id: "effect-9",
|
||||||
|
effectId: "effect-9",
|
||||||
|
tool: "schedule_create",
|
||||||
|
args: { name: "Morning walk", prompt: "Take a walk." },
|
||||||
|
});
|
||||||
|
const row = (blocks: unknown[]): InboxSuggestionRow => ({
|
||||||
|
spaceId: "space-home",
|
||||||
|
createdAt: at(3),
|
||||||
|
bot: scout,
|
||||||
|
thread: { botId: scout.id, groupId: null, group: null },
|
||||||
|
messageId: "msg-card",
|
||||||
|
cardId: "effect-9",
|
||||||
|
blocks,
|
||||||
|
});
|
||||||
|
const items = buildInboxItems({
|
||||||
|
runs: [],
|
||||||
|
messages: [],
|
||||||
|
unreadThreads: [unreadThread(scout, { id: "msg-card", text: "", minute: 3 })],
|
||||||
|
suggestions: [row([card])],
|
||||||
|
});
|
||||||
|
expect(items).toEqual([
|
||||||
|
{
|
||||||
|
kind: "suggestion",
|
||||||
|
bot: scout,
|
||||||
|
target: { botId: scout.id },
|
||||||
|
messageId: "msg-card",
|
||||||
|
card,
|
||||||
|
createdAt: at(3).toISOString(),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
expect(
|
||||||
|
buildInboxItems({
|
||||||
|
runs: [],
|
||||||
|
messages: [],
|
||||||
|
unreadThreads: [],
|
||||||
|
suggestions: [row([withSuggestionStatus(card, "dismissed")])],
|
||||||
|
}),
|
||||||
|
).toEqual([]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,20 +1,23 @@
|
|||||||
import type { Actor, InboxItem, InboxThreadTarget } from "@rakazo/contracts";
|
import type { Actor, InboxItem, InboxThreadTarget } from "@rakazo/contracts";
|
||||||
import { MessageBlock } from "@rakazo/contracts";
|
import { MessageBlock, SUGGESTION_TTL_MS } from "@rakazo/contracts";
|
||||||
import { isApprovalAskBlock } from "@rakazo/core";
|
import { isApprovalAskBlock } from "@rakazo/core";
|
||||||
import type { PrismaClient } from "@rakazo/db";
|
import type { PrismaClient } from "@rakazo/db";
|
||||||
import { promptSnippet } from "./runs.js";
|
import { promptSnippet } from "./runs.js";
|
||||||
|
|
||||||
/** Caps per query; a space rarely holds more waiting runs or unread threads than this. */
|
/** Caps per query; the user's spaces rarely hold more waiting runs or unread threads than this. */
|
||||||
const INBOX_RUN_LIMIT = 50;
|
const INBOX_RUN_LIMIT = 50;
|
||||||
const INBOX_UNREAD_LIMIT = 50;
|
const INBOX_UNREAD_LIMIT = 50;
|
||||||
const INBOX_PREVIEW_LENGTH = 280;
|
const INBOX_PREVIEW_LENGTH = 280;
|
||||||
|
|
||||||
type AskBlock = Extract<MessageBlock, { kind: "ask" }>;
|
type AskBlock = Extract<MessageBlock, { kind: "ask" }>;
|
||||||
|
type CardBlock = Extract<MessageBlock, { kind: "card" }>;
|
||||||
type ComputerBlock = Extract<MessageBlock, { kind: "computer" }>;
|
type ComputerBlock = Extract<MessageBlock, { kind: "computer" }>;
|
||||||
type InboxBot = { id: string; name: string; color: string };
|
type InboxBot = { id: string; name: string; color: string };
|
||||||
|
type InboxSpace = NonNullable<InboxItem["space"]>;
|
||||||
|
|
||||||
export interface InboxRunRow {
|
export interface InboxRunRow {
|
||||||
id: string;
|
id: string;
|
||||||
|
spaceId: string;
|
||||||
status: string;
|
status: string;
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
bot: InboxBot;
|
bot: InboxBot;
|
||||||
@ -33,12 +36,25 @@ export interface InboxMessageRow {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface InboxUnreadThreadRow {
|
export interface InboxUnreadThreadRow {
|
||||||
|
spaceId: string;
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
bot: InboxBot | null;
|
bot: InboxBot | null;
|
||||||
/** The newest bot message, if any. */
|
/** The newest bot message, if any. */
|
||||||
messages: Array<Omit<InboxMessageRow, "runId">>;
|
messages: Array<Omit<InboxMessageRow, "runId">>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** An open suggestion: the card holding it and the thread it was posted in. */
|
||||||
|
export interface InboxSuggestionRow {
|
||||||
|
spaceId: string;
|
||||||
|
createdAt: Date;
|
||||||
|
bot: InboxBot;
|
||||||
|
thread: InboxRunRow["thread"];
|
||||||
|
messageId: string;
|
||||||
|
cardId: string;
|
||||||
|
/** The card message's blocks. */
|
||||||
|
blocks: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
function parseBlocks(blocks: unknown): MessageBlock[] {
|
function parseBlocks(blocks: unknown): MessageBlock[] {
|
||||||
const parsed = MessageBlock.array().safeParse(blocks);
|
const parsed = MessageBlock.array().safeParse(blocks);
|
||||||
return parsed.success ? parsed.data : [];
|
return parsed.success ? parsed.data : [];
|
||||||
@ -73,12 +89,14 @@ function previewText(blocks: unknown): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Where an ask can be answered from. The main conversation already shows its own asks, and an
|
* Where an ask can be answered from. The open space's main conversation already shows its own
|
||||||
* archived group or an external conversation has no thread target to answer through.
|
* asks (a project's main conversation does not), and an archived group or an external
|
||||||
|
* conversation has no thread target to answer through.
|
||||||
*/
|
*/
|
||||||
function askTarget(thread: InboxRunRow["thread"]): InboxThreadTarget | null {
|
function askTarget(thread: InboxRunRow["thread"], currentSpace: boolean): InboxThreadTarget | null {
|
||||||
if (thread.groupId) {
|
if (thread.groupId) {
|
||||||
if (!thread.group || thread.group.kind === "main" || thread.group.archivedAt) return null;
|
if (!thread.group || thread.group.archivedAt) return null;
|
||||||
|
if (thread.group.kind === "main" && currentSpace) return null;
|
||||||
return { groupId: thread.groupId };
|
return { groupId: thread.groupId };
|
||||||
}
|
}
|
||||||
return thread.botId ? { botId: thread.botId } : null;
|
return thread.botId ? { botId: thread.botId } : null;
|
||||||
@ -93,16 +111,25 @@ function byCreatedAt(left: InboxItem, right: InboxItem): number {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turns waiting runs, their ask and takeover messages, and unread threads into inbox items:
|
* Turns waiting runs, their ask and takeover messages, open suggestions and unread threads into
|
||||||
|
* inbox items:
|
||||||
* everything that waits on the user first, then unread threads not already covered, each
|
* everything that waits on the user first, then unread threads not already covered, each
|
||||||
* oldest first.
|
* oldest first. Items from another space name it.
|
||||||
*/
|
*/
|
||||||
export function buildInboxItems(input: {
|
export function buildInboxItems(input: {
|
||||||
runs: InboxRunRow[];
|
runs: InboxRunRow[];
|
||||||
/** Ask and takeover messages of those runs, newest first. */
|
/** Ask and takeover messages of those runs, newest first. */
|
||||||
messages: InboxMessageRow[];
|
messages: InboxMessageRow[];
|
||||||
unreadThreads: InboxUnreadThreadRow[];
|
unreadThreads: InboxUnreadThreadRow[];
|
||||||
|
/** Open suggestions, wherever their card was posted. */
|
||||||
|
suggestions?: InboxSuggestionRow[];
|
||||||
|
/** The user's other spaces by id; rows of any space not listed belong to the current one. */
|
||||||
|
otherSpaces?: ReadonlyMap<string, InboxSpace>;
|
||||||
}): InboxItem[] {
|
}): InboxItem[] {
|
||||||
|
const spaceOf = (spaceId: string) => {
|
||||||
|
const space = input.otherSpaces?.get(spaceId);
|
||||||
|
return space ? { space } : {};
|
||||||
|
};
|
||||||
const messagesByRun = new Map<string, InboxMessageRow[]>();
|
const messagesByRun = new Map<string, InboxMessageRow[]>();
|
||||||
for (const message of input.messages) {
|
for (const message of input.messages) {
|
||||||
if (!message.runId) continue;
|
if (!message.runId) continue;
|
||||||
@ -115,13 +142,14 @@ export function buildInboxItems(input: {
|
|||||||
for (const run of input.runs) {
|
for (const run of input.runs) {
|
||||||
const messages = messagesByRun.get(run.id) ?? [];
|
const messages = messagesByRun.get(run.id) ?? [];
|
||||||
if (run.status === "waiting_input") {
|
if (run.status === "waiting_input") {
|
||||||
const target = askTarget(run.thread);
|
const target = askTarget(run.thread, !input.otherSpaces?.has(run.spaceId));
|
||||||
const pending = target ? newest(messages, pendingAsk) : undefined;
|
const pending = target ? newest(messages, pendingAsk) : undefined;
|
||||||
if (!target || !pending) continue;
|
if (!target || !pending) continue;
|
||||||
waiting.push({
|
waiting.push({
|
||||||
kind: isApprovalAskBlock(pending.block) ? "approval" : "ask",
|
kind: isApprovalAskBlock(pending.block) ? "approval" : "ask",
|
||||||
bot: run.bot,
|
bot: run.bot,
|
||||||
target,
|
target,
|
||||||
|
...spaceOf(run.spaceId),
|
||||||
runId: run.id,
|
runId: run.id,
|
||||||
messageId: pending.message.id,
|
messageId: pending.message.id,
|
||||||
ask: pending.block,
|
ask: pending.block,
|
||||||
@ -135,6 +163,7 @@ export function buildInboxItems(input: {
|
|||||||
kind: "takeover",
|
kind: "takeover",
|
||||||
bot: run.bot,
|
bot: run.bot,
|
||||||
target: { botId: run.bot.id },
|
target: { botId: run.bot.id },
|
||||||
|
...spaceOf(run.spaceId),
|
||||||
runId: run.id,
|
runId: run.id,
|
||||||
messageId: request?.message.id ?? null,
|
messageId: request?.message.id ?? null,
|
||||||
text: request?.block.text ?? "",
|
text: request?.block.text ?? "",
|
||||||
@ -143,6 +172,23 @@ export function buildInboxItems(input: {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const suggestion of input.suggestions ?? []) {
|
||||||
|
const target = askTarget(suggestion.thread, !input.otherSpaces?.has(suggestion.spaceId));
|
||||||
|
const card = parseBlocks(suggestion.blocks).find(
|
||||||
|
(block): block is CardBlock => block.kind === "card" && block.id === suggestion.cardId,
|
||||||
|
);
|
||||||
|
if (!target || card?.suggestion?.status !== "proposed") continue;
|
||||||
|
waiting.push({
|
||||||
|
kind: "suggestion",
|
||||||
|
bot: suggestion.bot,
|
||||||
|
target,
|
||||||
|
...spaceOf(suggestion.spaceId),
|
||||||
|
messageId: suggestion.messageId,
|
||||||
|
card,
|
||||||
|
createdAt: suggestion.createdAt.toISOString(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const covered = new Set(waiting.map((item) => targetKey(item.target)));
|
const covered = new Set(waiting.map((item) => targetKey(item.target)));
|
||||||
const unread: InboxItem[] = [];
|
const unread: InboxItem[] = [];
|
||||||
for (const thread of input.unreadThreads) {
|
for (const thread of input.unreadThreads) {
|
||||||
@ -154,6 +200,7 @@ export function buildInboxItems(input: {
|
|||||||
kind: "unread",
|
kind: "unread",
|
||||||
bot: thread.bot,
|
bot: thread.bot,
|
||||||
target,
|
target,
|
||||||
|
...spaceOf(thread.spaceId),
|
||||||
messageId: latest?.id ?? null,
|
messageId: latest?.id ?? null,
|
||||||
text: previewText(latest?.blocks),
|
text: previewText(latest?.blocks),
|
||||||
createdAt: (latest?.createdAt ?? thread.createdAt).toISOString(),
|
createdAt: (latest?.createdAt ?? thread.createdAt).toISOString(),
|
||||||
@ -165,13 +212,45 @@ export function buildInboxItems(input: {
|
|||||||
|
|
||||||
const botSelect = { id: true, name: true, color: true } as const;
|
const botSelect = { id: true, name: true, color: true } as const;
|
||||||
|
|
||||||
/** The current space's inbox: only the actor's own bots, runs and threads. */
|
/**
|
||||||
|
* The other spaces whose items the inbox shows. The home space's main conversation collects every
|
||||||
|
* space the user belongs to, except spaces being deleted; a project's main conversation keeps to
|
||||||
|
* its own project.
|
||||||
|
*/
|
||||||
|
async function otherInboxSpaces(
|
||||||
|
prisma: PrismaClient,
|
||||||
|
actor: Actor,
|
||||||
|
): Promise<Map<string, InboxSpace>> {
|
||||||
|
const current = await prisma.space.findUnique({
|
||||||
|
where: { id: actor.spaceId },
|
||||||
|
select: { isDefault: true },
|
||||||
|
});
|
||||||
|
if (!current?.isDefault) return new Map();
|
||||||
|
const memberships = await prisma.spaceMember.findMany({
|
||||||
|
where: { userId: actor.userId, spaceId: { not: actor.spaceId }, space: { deletingAt: null } },
|
||||||
|
select: { space: { select: { id: true, name: true } } },
|
||||||
|
});
|
||||||
|
return new Map(memberships.map(({ space }) => [space.id, space]));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The inbox across the user's spaces: only the actor's own bots, runs and threads. Items are
|
||||||
|
* answered and opened in their own space, where the thread procedures check membership and
|
||||||
|
* ownership again.
|
||||||
|
*/
|
||||||
export async function listInbox(prisma: PrismaClient, actor: Actor): Promise<InboxItem[]> {
|
export async function listInbox(prisma: PrismaClient, actor: Actor): Promise<InboxItem[]> {
|
||||||
|
const otherSpaces = await otherInboxSpaces(prisma, actor);
|
||||||
|
const spaceIds = [actor.spaceId, ...otherSpaces.keys()];
|
||||||
const ownBot = { userId: actor.userId, archivedAt: null };
|
const ownBot = { userId: actor.userId, archivedAt: null };
|
||||||
const [runs, unreadThreads] = await Promise.all([
|
const threadSelect = {
|
||||||
|
botId: true,
|
||||||
|
groupId: true,
|
||||||
|
group: { select: { kind: true, archivedAt: true } },
|
||||||
|
} as const;
|
||||||
|
const [runs, unreadThreads, suggestionEffects] = await Promise.all([
|
||||||
prisma.run.findMany({
|
prisma.run.findMany({
|
||||||
where: {
|
where: {
|
||||||
spaceId: actor.spaceId,
|
spaceId: { in: spaceIds },
|
||||||
userId: actor.userId,
|
userId: actor.userId,
|
||||||
status: { in: ["waiting_input", "waiting_takeover"] },
|
status: { in: ["waiting_input", "waiting_takeover"] },
|
||||||
bot: ownBot,
|
bot: ownBot,
|
||||||
@ -179,17 +258,12 @@ export async function listInbox(prisma: PrismaClient, actor: Actor): Promise<Inb
|
|||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
|
spaceId: true,
|
||||||
status: true,
|
status: true,
|
||||||
threadId: true,
|
threadId: true,
|
||||||
updatedAt: true,
|
updatedAt: true,
|
||||||
bot: { select: botSelect },
|
bot: { select: botSelect },
|
||||||
thread: {
|
thread: { select: threadSelect },
|
||||||
select: {
|
|
||||||
botId: true,
|
|
||||||
groupId: true,
|
|
||||||
group: { select: { kind: true, archivedAt: true } },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
orderBy: [{ updatedAt: "asc" }, { id: "asc" }],
|
orderBy: [{ updatedAt: "asc" }, { id: "asc" }],
|
||||||
take: INBOX_RUN_LIMIT,
|
take: INBOX_RUN_LIMIT,
|
||||||
@ -197,12 +271,13 @@ export async function listInbox(prisma: PrismaClient, actor: Actor): Promise<Inb
|
|||||||
// Only specialists' own threads; group threads, the main conversation included, stay out.
|
// Only specialists' own threads; group threads, the main conversation included, stay out.
|
||||||
prisma.thread.findMany({
|
prisma.thread.findMany({
|
||||||
where: {
|
where: {
|
||||||
spaceId: actor.spaceId,
|
spaceId: { in: spaceIds },
|
||||||
userId: actor.userId,
|
userId: actor.userId,
|
||||||
unread: true,
|
unread: true,
|
||||||
bot: { ...ownBot, kind: "specialist" },
|
bot: { ...ownBot, kind: "specialist" },
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
|
spaceId: true,
|
||||||
createdAt: true,
|
createdAt: true,
|
||||||
bot: { select: botSelect },
|
bot: { select: botSelect },
|
||||||
messages: {
|
messages: {
|
||||||
@ -215,7 +290,54 @@ export async function listInbox(prisma: PrismaClient, actor: Actor): Promise<Inb
|
|||||||
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
|
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
|
||||||
take: INBOX_UNREAD_LIMIT,
|
take: INBOX_UNREAD_LIMIT,
|
||||||
}),
|
}),
|
||||||
|
prisma.externalEffect.findMany({
|
||||||
|
where: {
|
||||||
|
spaceId: { in: spaceIds },
|
||||||
|
kind: "suggestion",
|
||||||
|
status: "proposed",
|
||||||
|
createdAt: { gt: new Date(Date.now() - SUGGESTION_TTL_MS) },
|
||||||
|
run: { userId: actor.userId, bot: ownBot, thread: { userId: actor.userId } },
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
spaceId: true,
|
||||||
|
createdAt: true,
|
||||||
|
request: true,
|
||||||
|
run: { select: { bot: { select: botSelect }, thread: { select: threadSelect } } },
|
||||||
|
},
|
||||||
|
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
|
||||||
|
take: INBOX_RUN_LIMIT,
|
||||||
|
}),
|
||||||
]);
|
]);
|
||||||
|
const cardMessageIds = new Map<string, string>();
|
||||||
|
for (const effect of suggestionEffects) {
|
||||||
|
const messageId = (effect.request as { card?: { messageId?: unknown } } | null)?.card
|
||||||
|
?.messageId;
|
||||||
|
if (typeof messageId === "string") cardMessageIds.set(effect.id, messageId);
|
||||||
|
}
|
||||||
|
const cardMessages =
|
||||||
|
cardMessageIds.size === 0
|
||||||
|
? []
|
||||||
|
: await prisma.message.findMany({
|
||||||
|
where: { id: { in: [...cardMessageIds.values()] }, role: "bot" },
|
||||||
|
select: { id: true, blocks: true },
|
||||||
|
});
|
||||||
|
const blocksById = new Map(cardMessages.map((message) => [message.id, message.blocks]));
|
||||||
|
const suggestions: InboxSuggestionRow[] = suggestionEffects.flatMap((effect) => {
|
||||||
|
const messageId = cardMessageIds.get(effect.id);
|
||||||
|
if (!messageId || !blocksById.has(messageId)) return [];
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
spaceId: effect.spaceId,
|
||||||
|
createdAt: effect.createdAt,
|
||||||
|
bot: effect.run.bot,
|
||||||
|
thread: effect.run.thread,
|
||||||
|
messageId,
|
||||||
|
cardId: effect.id,
|
||||||
|
blocks: blocksById.get(messageId),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
});
|
||||||
const messages =
|
const messages =
|
||||||
runs.length === 0
|
runs.length === 0
|
||||||
? []
|
? []
|
||||||
@ -232,5 +354,5 @@ export async function listInbox(prisma: PrismaClient, actor: Actor): Promise<Inb
|
|||||||
orderBy: { seq: "desc" },
|
orderBy: { seq: "desc" },
|
||||||
select: { id: true, runId: true, blocks: true, createdAt: true },
|
select: { id: true, runId: true, blocks: true, createdAt: true },
|
||||||
});
|
});
|
||||||
return buildInboxItems({ runs, messages, unreadThreads });
|
return buildInboxItems({ runs, messages, unreadThreads, suggestions, otherSpaces });
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import { serve } from "@hono/node-server";
|
|||||||
import { SERVICE_NAMES } from "@rakazo/logging";
|
import { SERVICE_NAMES } from "@rakazo/logging";
|
||||||
import { createRootLogger } from "@rakazo/logging/axiom";
|
import { createRootLogger } from "@rakazo/logging/axiom";
|
||||||
import { createApp } from "./app.js";
|
import { createApp } from "./app.js";
|
||||||
|
import { API_SERVER_OPTIONS } from "./artifact-http.js";
|
||||||
import { loadEnv } from "./env.js";
|
import { loadEnv } from "./env.js";
|
||||||
|
|
||||||
const logger = createRootLogger(SERVICE_NAMES.api);
|
const logger = createRootLogger(SERVICE_NAMES.api);
|
||||||
@ -14,9 +15,17 @@ const logger = createRootLogger(SERVICE_NAMES.api);
|
|||||||
try {
|
try {
|
||||||
const env = loadEnv();
|
const env = loadEnv();
|
||||||
const { app, stop } = await createApp({ ...env, logger });
|
const { app, stop } = await createApp({ ...env, logger });
|
||||||
const server = serve({ fetch: app.fetch, port: env.port, hostname: env.apiHost }, () => {
|
const server = serve(
|
||||||
logger.info("api listening", { "http.host": env.apiHost, "http.port": env.port });
|
{
|
||||||
});
|
fetch: app.fetch,
|
||||||
|
port: env.port,
|
||||||
|
hostname: env.apiHost,
|
||||||
|
serverOptions: API_SERVER_OPTIONS,
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
logger.info("api listening", { "http.host": env.apiHost, "http.port": env.port });
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// Long-lived connections (threads.subscribe SSE streams) never end on their
|
// Long-lived connections (threads.subscribe SSE streams) never end on their
|
||||||
// own, so server.close() alone waits forever for them. Track sockets and
|
// own, so server.close() alone waits forever for them. Track sockets and
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { ORPCError } from "@orpc/server";
|
import { ORPCError, ValidationError } from "@orpc/server";
|
||||||
import { createLogger, createTestSink, installLogger } from "@rakazo/logging";
|
import { createLogger, createTestSink, installLogger } from "@rakazo/logging";
|
||||||
import { afterEach, describe, expect, it } from "vitest";
|
import { afterEach, describe, expect, it } from "vitest";
|
||||||
import { logUnexpectedRpcError } from "./app.js";
|
import { logUnexpectedRpcError } from "./app.js";
|
||||||
@ -20,6 +20,28 @@ describe("logUnexpectedRpcError", () => {
|
|||||||
expect(sink.events).toEqual([]);
|
expect(sink.events).toEqual([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("names the procedure and the fields when a result breaks its output contract", () => {
|
||||||
|
const sink = createTestSink();
|
||||||
|
installLogger(createLogger({ service: "rakazo-api", sinks: [sink] }));
|
||||||
|
const error = new ORPCError("INTERNAL_SERVER_ERROR", {
|
||||||
|
message: "Output validation failed",
|
||||||
|
cause: new ValidationError({
|
||||||
|
message: "Output validation failed",
|
||||||
|
issues: [{ message: "Expected string", path: ["items", 0, { key: "title" }] }],
|
||||||
|
data: { items: [{ title: null, body: "private" }] },
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
logUnexpectedRpcError(error, ["inbox", "list"]);
|
||||||
|
|
||||||
|
expect(sink.events).toHaveLength(1);
|
||||||
|
expect(sink.events[0]).toMatchObject({
|
||||||
|
message: "rpc inbox/list failed",
|
||||||
|
issues: [{ path: "items.0.title", problem: "Expected string" }],
|
||||||
|
});
|
||||||
|
expect(JSON.stringify(sink.events[0])).not.toContain("private");
|
||||||
|
});
|
||||||
|
|
||||||
it("names the procedure and every cause behind an opaque failure", () => {
|
it("names the procedure and every cause behind an opaque failure", () => {
|
||||||
const sink = createTestSink();
|
const sink = createTestSink();
|
||||||
installLogger(createLogger({ service: "rakazo-api", sinks: [sink] }));
|
installLogger(createLogger({ service: "rakazo-api", sinks: [sink] }));
|
||||||
|
|||||||
35
apps/api/src/media-capabilities.test.ts
Normal file
35
apps/api/src/media-capabilities.test.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import type { MediaCapabilities } from "@rakazo/adapter-kit";
|
||||||
|
import type { Actor } from "@rakazo/contracts";
|
||||||
|
import { MediaOperationSchema } from "@rakazo/contracts";
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import type { MediaRouteDeps } from "./media-jobs.js";
|
||||||
|
import { getMediaCapabilities } from "./media-jobs.js";
|
||||||
|
|
||||||
|
const actor: Actor = {
|
||||||
|
userId: "user-1",
|
||||||
|
spaceId: "space-1",
|
||||||
|
email: "user@rakazo.test",
|
||||||
|
isDeploymentOwner: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
describe("media capabilities", () => {
|
||||||
|
it("answers for the actor's space", async () => {
|
||||||
|
const asked: string[] = [];
|
||||||
|
const deps = {
|
||||||
|
media: {
|
||||||
|
capabilities: async (spaceId: string) => {
|
||||||
|
asked.push(spaceId);
|
||||||
|
return { transcribe: true, export_video: false } as MediaCapabilities;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as unknown as MediaRouteDeps;
|
||||||
|
await expect(getMediaCapabilities(deps, actor)).resolves.toMatchObject({ transcribe: true });
|
||||||
|
expect(asked).toEqual(["space-1"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reports nothing available without a media runtime", async () => {
|
||||||
|
const capabilities = await getMediaCapabilities({} as MediaRouteDeps, actor);
|
||||||
|
expect(Object.keys(capabilities)).toEqual(MediaOperationSchema.options);
|
||||||
|
expect(Object.values(capabilities).every((value) => value === false)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
165
apps/api/src/media-jobs.ts
Normal file
165
apps/api/src/media-jobs.ts
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
import { ORPCError } from "@orpc/server";
|
||||||
|
import type { MediaCapabilities, MediaRuntime } from "@rakazo/adapter-kit";
|
||||||
|
import type { EncryptedSecretStore } from "@rakazo/adapters";
|
||||||
|
import {
|
||||||
|
loadMediaSettings,
|
||||||
|
MediaJobRejectedError,
|
||||||
|
MediaSettingsInvalidError,
|
||||||
|
mediaSettingsView,
|
||||||
|
saveMediaSettings,
|
||||||
|
} from "@rakazo/adapters";
|
||||||
|
import type { Actor, MediaJobStartInput, MediaSettingsUpdate } from "@rakazo/contracts";
|
||||||
|
import { MediaOperationSchema } from "@rakazo/contracts";
|
||||||
|
import type { PrismaClient } from "@rakazo/db";
|
||||||
|
import { IsolationError, requireMembership } from "@rakazo/db";
|
||||||
|
import { resolveThreadTarget } from "./thread-target.js";
|
||||||
|
|
||||||
|
export interface MediaRouteDeps {
|
||||||
|
prisma: PrismaClient;
|
||||||
|
secrets: EncryptedSecretStore;
|
||||||
|
media?: MediaRuntime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Media backends run on the deployment owner's own server, so only they configure them. */
|
||||||
|
function requireDeploymentOwner(actor: Actor) {
|
||||||
|
if (!actor.isDeploymentOwner) throw new ORPCError("FORBIDDEN");
|
||||||
|
}
|
||||||
|
|
||||||
|
function requireMedia(deps: MediaRouteDeps): MediaRuntime {
|
||||||
|
if (!deps.media) throw new ORPCError("NOT_IMPLEMENTED");
|
||||||
|
return deps.media;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getMediaSettings(deps: MediaRouteDeps, actor: Actor) {
|
||||||
|
requireDeploymentOwner(actor);
|
||||||
|
return mediaSettingsView(await loadMediaSettings(deps));
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateMediaSettings(
|
||||||
|
deps: MediaRouteDeps,
|
||||||
|
actor: Actor,
|
||||||
|
input: MediaSettingsUpdate,
|
||||||
|
) {
|
||||||
|
requireDeploymentOwner(actor);
|
||||||
|
try {
|
||||||
|
return await saveMediaSettings(deps, input);
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof MediaSettingsInvalidError) {
|
||||||
|
throw new ORPCError("BAD_REQUEST", { message: error.message });
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The actor in the space a card lives in. A card is acted on where it is shown, which need not be
|
||||||
|
* the space the client has selected (the main conversation lives in the home space), so the
|
||||||
|
* space comes from the card's own record: it must belong to the actor, and the actor must still
|
||||||
|
* be a member of that space. Anything else reads as not found.
|
||||||
|
*/
|
||||||
|
async function actorInSpace(deps: MediaRouteDeps, actor: Actor, spaceId: string): Promise<Actor> {
|
||||||
|
if (spaceId === actor.spaceId) return actor;
|
||||||
|
try {
|
||||||
|
return await requireMembership(deps.prisma, actor.userId, spaceId);
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof IsolationError) throw new ORPCError("NOT_FOUND");
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts an operation a user picked on a card (export, transcribe, edit) in the card's
|
||||||
|
* conversation. The conversation is authorized like every thread procedure, in the card's space;
|
||||||
|
* the runtime checks that the operation's source artifacts belong to the actor there, and the
|
||||||
|
* job can only ever read the actor's artifacts of that space.
|
||||||
|
*/
|
||||||
|
export async function startMediaJob(deps: MediaRouteDeps, actor: Actor, input: MediaJobStartInput) {
|
||||||
|
const media = requireMedia(deps);
|
||||||
|
const source = await deps.prisma.message.findFirst({
|
||||||
|
where: {
|
||||||
|
id: input.messageId,
|
||||||
|
thread: {
|
||||||
|
userId: actor.userId,
|
||||||
|
...(input.botId ? { botId: input.botId } : { groupId: input.groupId }),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
select: { botId: true, thread: { select: { spaceId: true } } },
|
||||||
|
});
|
||||||
|
if (!source) throw new ORPCError("NOT_FOUND");
|
||||||
|
const scoped = await actorInSpace(deps, actor, source.thread.spaceId);
|
||||||
|
const target = await resolveThreadTarget(deps.prisma, scoped, input).catch((error) => {
|
||||||
|
if (error instanceof IsolationError) throw new ORPCError("NOT_FOUND");
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
|
const bots =
|
||||||
|
target.kind === "bot"
|
||||||
|
? [target.botId]
|
||||||
|
: [
|
||||||
|
...(target.leadBotId && target.memberBotIds.includes(target.leadBotId)
|
||||||
|
? [target.leadBotId]
|
||||||
|
: []),
|
||||||
|
...target.memberBotIds,
|
||||||
|
];
|
||||||
|
const botId = source.botId && bots.includes(source.botId) ? source.botId : bots[0];
|
||||||
|
if (!botId) throw new ORPCError("NOT_FOUND");
|
||||||
|
try {
|
||||||
|
return await media.start({
|
||||||
|
operation: input.operation,
|
||||||
|
input: input.input,
|
||||||
|
spaceId: scoped.spaceId,
|
||||||
|
userId: scoped.userId,
|
||||||
|
botId,
|
||||||
|
groupId: target.kind === "group" ? target.groupId : null,
|
||||||
|
threadId: target.threadId,
|
||||||
|
runId: null,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof MediaJobRejectedError) {
|
||||||
|
throw new ORPCError("BAD_REQUEST", { message: error.message });
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Booleans only: which operations a space can run, never how they are configured. With a job,
|
||||||
|
* the space is the job's (a card's follow-ups); otherwise the actor's current space.
|
||||||
|
*/
|
||||||
|
export async function getMediaCapabilities(
|
||||||
|
deps: MediaRouteDeps,
|
||||||
|
actor: Actor,
|
||||||
|
input: { jobId?: string } = {},
|
||||||
|
): Promise<MediaCapabilities> {
|
||||||
|
const spaceId = input.jobId ? (await ownedJob(deps, actor, input.jobId)).spaceId : actor.spaceId;
|
||||||
|
if (deps.media) return deps.media.capabilities(spaceId);
|
||||||
|
return Object.fromEntries(
|
||||||
|
MediaOperationSchema.options.map((operation) => [operation, false]),
|
||||||
|
) as MediaCapabilities;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The actor's job, in whichever of the actor's spaces it runs. */
|
||||||
|
async function ownedJob(deps: MediaRouteDeps, actor: Actor, jobId: string) {
|
||||||
|
const job = await deps.prisma.mediaJob.findFirst({
|
||||||
|
where: { id: jobId, userId: actor.userId },
|
||||||
|
select: { id: true, spaceId: true },
|
||||||
|
});
|
||||||
|
if (!job) throw new ORPCError("NOT_FOUND");
|
||||||
|
await actorInSpace(deps, actor, job.spaceId);
|
||||||
|
return job;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getMediaJob(deps: MediaRouteDeps, actor: Actor, jobId: string) {
|
||||||
|
const media = requireMedia(deps);
|
||||||
|
const snapshot = await media.get((await ownedJob(deps, actor, jobId)).id);
|
||||||
|
if (!snapshot) throw new ORPCError("NOT_FOUND");
|
||||||
|
return snapshot;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function cancelMediaJob(deps: MediaRouteDeps, actor: Actor, jobId: string) {
|
||||||
|
const media = requireMedia(deps);
|
||||||
|
const { id } = await ownedJob(deps, actor, jobId);
|
||||||
|
await media.cancel(id);
|
||||||
|
const snapshot = await media.get(id);
|
||||||
|
if (!snapshot) throw new ORPCError("NOT_FOUND");
|
||||||
|
return snapshot;
|
||||||
|
}
|
||||||
82
apps/api/src/media-library.ts
Normal file
82
apps/api/src/media-library.ts
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
import path from "node:path";
|
||||||
|
import { ORPCError } from "@orpc/server";
|
||||||
|
import type { JobPublisher } from "@rakazo/adapter-kit";
|
||||||
|
import { mediaLibrarySyncJob } from "@rakazo/adapter-kit";
|
||||||
|
import { checkMediaLibraryFolder, mediaLibrarySourceView } from "@rakazo/adapters";
|
||||||
|
import type { Actor, MediaLibrarySourceAddInput } from "@rakazo/contracts";
|
||||||
|
import type { PrismaClient } from "@rakazo/db";
|
||||||
|
|
||||||
|
export interface MediaLibraryRouteDeps {
|
||||||
|
prisma: PrismaClient;
|
||||||
|
jobs: JobPublisher;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Library folders are paths on the server, so only its owner adds them, like host folders. */
|
||||||
|
function requireDeploymentOwner(actor: Actor) {
|
||||||
|
if (!actor.isDeploymentOwner) throw new ORPCError("FORBIDDEN");
|
||||||
|
}
|
||||||
|
|
||||||
|
async function ownSource(deps: MediaLibraryRouteDeps, actor: Actor, sourceId: string) {
|
||||||
|
const source = await deps.prisma.mediaLibrarySource.findFirst({
|
||||||
|
where: { id: sourceId, userId: actor.userId },
|
||||||
|
});
|
||||||
|
if (!source) throw new ORPCError("NOT_FOUND");
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function listMediaLibrarySources(deps: MediaLibraryRouteDeps, actor: Actor) {
|
||||||
|
requireDeploymentOwner(actor);
|
||||||
|
const rows = await deps.prisma.mediaLibrarySource.findMany({
|
||||||
|
where: { userId: actor.userId },
|
||||||
|
orderBy: { createdAt: "asc" },
|
||||||
|
});
|
||||||
|
return rows.map(mediaLibrarySourceView);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function addMediaLibrarySource(
|
||||||
|
deps: MediaLibraryRouteDeps,
|
||||||
|
actor: Actor,
|
||||||
|
input: MediaLibrarySourceAddInput,
|
||||||
|
) {
|
||||||
|
requireDeploymentOwner(actor);
|
||||||
|
const problem = await checkMediaLibraryFolder(input.path);
|
||||||
|
if (problem) throw new ORPCError("BAD_REQUEST", { message: problem });
|
||||||
|
const folder = path.resolve(input.path);
|
||||||
|
const existing = await deps.prisma.mediaLibrarySource.findUnique({
|
||||||
|
where: { userId_path: { userId: actor.userId, path: folder } },
|
||||||
|
});
|
||||||
|
if (existing) return mediaLibrarySourceView(existing);
|
||||||
|
const source = await deps.prisma.mediaLibrarySource.create({
|
||||||
|
data: { userId: actor.userId, path: folder, status: "syncing" },
|
||||||
|
});
|
||||||
|
await deps.jobs.enqueue(mediaLibrarySyncJob(source.id));
|
||||||
|
return mediaLibrarySourceView(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function removeMediaLibrarySource(
|
||||||
|
deps: MediaLibraryRouteDeps,
|
||||||
|
actor: Actor,
|
||||||
|
sourceId: string,
|
||||||
|
) {
|
||||||
|
requireDeploymentOwner(actor);
|
||||||
|
await ownSource(deps, actor, sourceId);
|
||||||
|
await deps.prisma.mediaLibrarySource.deleteMany({
|
||||||
|
where: { id: sourceId, userId: actor.userId },
|
||||||
|
});
|
||||||
|
return { ok: true as const };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function syncMediaLibrarySourceNow(
|
||||||
|
deps: MediaLibraryRouteDeps,
|
||||||
|
actor: Actor,
|
||||||
|
sourceId: string,
|
||||||
|
) {
|
||||||
|
requireDeploymentOwner(actor);
|
||||||
|
await ownSource(deps, actor, sourceId);
|
||||||
|
const source = await deps.prisma.mediaLibrarySource.update({
|
||||||
|
where: { id: sourceId },
|
||||||
|
data: { status: "syncing", error: null },
|
||||||
|
});
|
||||||
|
await deps.jobs.enqueue(mediaLibrarySyncJob(sourceId));
|
||||||
|
return mediaLibrarySourceView(source);
|
||||||
|
}
|
||||||
116
apps/api/src/onboarding-intake.test.ts
Normal file
116
apps/api/src/onboarding-intake.test.ts
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import {
|
||||||
|
closingNote,
|
||||||
|
focusCard,
|
||||||
|
focusFactValue,
|
||||||
|
intakeContext,
|
||||||
|
intakeStep,
|
||||||
|
nextIntakeStep,
|
||||||
|
readIntakeAnswer,
|
||||||
|
secondaryOption,
|
||||||
|
} from "./onboarding-intake.js";
|
||||||
|
|
||||||
|
// Intl may separate the time from AM/PM with a narrow no-break space.
|
||||||
|
const plain = (text: string) => text.replace(/\s/g, " ");
|
||||||
|
const off = { morning: null, evening: null, weekly: null };
|
||||||
|
|
||||||
|
describe("onboarding steps", () => {
|
||||||
|
it("writes focus facts in the user's locale", () => {
|
||||||
|
expect(focusFactValue("day", "en-US")).toBe("Wants help with day-to-day work first");
|
||||||
|
expect(focusFactValue("day", "de-DE")).toBe(
|
||||||
|
"Möchte zuerst bei der täglichen Arbeit unterstützt werden",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("asks in order and offers notifications only to a device that can still turn push on", () => {
|
||||||
|
expect(nextIntakeStep("focus", { canEnablePush: false })?.id).toBe("goal");
|
||||||
|
expect(nextIntakeStep("goal", { canEnablePush: false })?.id).toBe("rhythm");
|
||||||
|
expect(nextIntakeStep("rhythm", { canEnablePush: false })?.id).toBe("quiet_hours");
|
||||||
|
expect(nextIntakeStep("quiet_hours", { canEnablePush: true })?.id).toBe("notifications");
|
||||||
|
expect(nextIntakeStep("quiet_hours", { canEnablePush: false })).toBeNull();
|
||||||
|
expect(nextIntakeStep("notifications", { canEnablePush: true })).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("gives every onboarding card a quiet way out, and another bot's focus card none", () => {
|
||||||
|
const context = intakeContext(null, new Date());
|
||||||
|
for (const id of ["focus", "goal", "rhythm", "quiet_hours", "notifications"] as const) {
|
||||||
|
const card = intakeStep(id).card(context);
|
||||||
|
expect(card.step).toBe(id);
|
||||||
|
expect(secondaryOption(card)).toBeDefined();
|
||||||
|
}
|
||||||
|
expect(focusCard().step).toBeUndefined();
|
||||||
|
expect(secondaryOption(focusCard())).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("writes quiet hours in the user's locale", () => {
|
||||||
|
const labels = (locale: string | null) =>
|
||||||
|
intakeStep("quiet_hours")
|
||||||
|
.card(intakeContext({ locale, timeZone: null }, new Date()))
|
||||||
|
.options.map((option) => plain(option.label));
|
||||||
|
expect(labels("de-AT")).toEqual(["22:00 – 7:00", "23:00 – 8:00", "Skip"]);
|
||||||
|
expect(labels("en-US")).toEqual(["10:00 PM – 7:00 AM", "11:00 PM – 8:00 AM", "Skip"]);
|
||||||
|
expect(labels("not a tag")).toEqual(labels("en"));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("readIntakeAnswer", () => {
|
||||||
|
const goal = intakeStep("goal").card(intakeContext(null, new Date()));
|
||||||
|
const rhythm = intakeStep("rhythm").card(intakeContext(null, new Date()));
|
||||||
|
|
||||||
|
it("accepts the card's own options and words only where the card takes them", () => {
|
||||||
|
expect(readIntakeAnswer(rhythm, "weekly", undefined)).toEqual({
|
||||||
|
option: { id: "weekly", letter: "C", label: "Once a week" },
|
||||||
|
});
|
||||||
|
expect(readIntakeAnswer(rhythm, "skip", undefined)).toBeNull();
|
||||||
|
expect(readIntakeAnswer(rhythm, "_text", "Mornings")).toBeNull();
|
||||||
|
expect(readIntakeAnswer(goal, "_text", " Run a 10K ")).toEqual({ text: "Run a 10K" });
|
||||||
|
expect(readIntakeAnswer(goal, "_text", " ")).toBeNull();
|
||||||
|
expect(readIntakeAnswer(goal, "skip", undefined)?.option?.label).toBe("Skip");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("closingNote", () => {
|
||||||
|
const vienna = (iso: string, locale = "de-AT") =>
|
||||||
|
intakeContext({ locale, timeZone: "Europe/Vienna" }, new Date(iso));
|
||||||
|
const note = (rhythm: Parameters<typeof closingNote>[0], context: ReturnType<typeof vienna>) => {
|
||||||
|
const { nextCheckInAt, text } = closingNote(rhythm, context);
|
||||||
|
return { nextCheckInAt, text: plain(text) };
|
||||||
|
};
|
||||||
|
|
||||||
|
it("names the first check-in in the user's time zone", () => {
|
||||||
|
const morning = { ...off, morning: "08:00" };
|
||||||
|
expect(note(morning, vienna("2026-10-07T04:00:00Z"))).toEqual({
|
||||||
|
nextCheckInAt: "2026-10-07T06:00:00.000Z",
|
||||||
|
text: "All set. I’ll check in today at 8:00.",
|
||||||
|
});
|
||||||
|
expect(note(morning, vienna("2026-10-07T09:00:00Z"))).toEqual({
|
||||||
|
nextCheckInAt: "2026-10-08T06:00:00.000Z",
|
||||||
|
text: "All set. I’ll check in tomorrow at 8:00.",
|
||||||
|
});
|
||||||
|
expect(note({ ...morning, evening: "18:00" }, vienna("2026-10-07T09:00:00Z")).text).toBe(
|
||||||
|
"All set. I’ll check in today at 18:00.",
|
||||||
|
);
|
||||||
|
// Wednesday in Vienna; the weekly review is on Sunday.
|
||||||
|
const weekly = { ...off, weekly: { day: 0, time: "18:00" } };
|
||||||
|
expect(note(weekly, vienna("2026-10-07T09:00:00Z", "en-US"))).toEqual({
|
||||||
|
nextCheckInAt: "2026-10-11T16:00:00.000Z",
|
||||||
|
text: "All set. I’ll check in on Sunday at 6:00 PM.",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("says the user can just write when no check-in is set", () => {
|
||||||
|
expect(closingNote(off, vienna("2026-10-07T09:00:00Z"))).toEqual({
|
||||||
|
kind: "intake_note",
|
||||||
|
note: "done",
|
||||||
|
nextCheckInAt: null,
|
||||||
|
text: "All set. I’m here whenever you need me.",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("falls back to English and UTC for an unusable profile", () => {
|
||||||
|
expect(intakeContext({ locale: "??", timeZone: "Mars/Olympus" }, new Date(0))).toMatchObject({
|
||||||
|
locale: "en",
|
||||||
|
timeZone: "UTC",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
326
apps/api/src/onboarding-intake.ts
Normal file
326
apps/api/src/onboarding-intake.ts
Normal file
@ -0,0 +1,326 @@
|
|||||||
|
import type { JobPublisher } from "@rakazo/adapter-kit";
|
||||||
|
import { COACH_KEYS, coachCron, setCoachRhythm } from "@rakazo/adapters";
|
||||||
|
import type { CoachRhythm, MessageBlock, OnboardingStep, QuietHours } from "@rakazo/contracts";
|
||||||
|
import { CHOICE_TEXT_ANSWER_ID } from "@rakazo/contracts";
|
||||||
|
import { knownTimeZone, nextCronDateAcross } from "@rakazo/core";
|
||||||
|
import type { Prisma, PrismaClient } from "@rakazo/db";
|
||||||
|
import { createGoal, updateNotificationSettings, updateUserProfile } from "@rakazo/db";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The questions the main bot asks a new user in the main conversation, one card at a time.
|
||||||
|
* Each step is a card plus what its answer changes; the order of `INTAKE_STEPS` is the order
|
||||||
|
* the user sees. Everything here is deterministic, so no model tokens are spent.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export type ChoiceBlock = Extract<MessageBlock, { kind: "choice" }>;
|
||||||
|
type ChoiceOption = ChoiceBlock["options"][number];
|
||||||
|
|
||||||
|
/** A validated answer: an option of the card, or the user's own words. */
|
||||||
|
export type IntakeAnswer =
|
||||||
|
| { option: ChoiceOption; text?: undefined }
|
||||||
|
| { option?: undefined; text: string };
|
||||||
|
|
||||||
|
/** What a card may adapt to: times are written in the user's locale. */
|
||||||
|
export interface IntakeContext {
|
||||||
|
locale: string;
|
||||||
|
timeZone: string;
|
||||||
|
now: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IntakeEffects {
|
||||||
|
prisma: PrismaClient;
|
||||||
|
jobs: JobPublisher;
|
||||||
|
tx: Prisma.TransactionClient;
|
||||||
|
userId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IntakeStep {
|
||||||
|
id: OnboardingStep;
|
||||||
|
/** Asked only when this holds; otherwise onboarding moves past the step. */
|
||||||
|
asks?: (device: { canEnablePush: boolean }) => boolean;
|
||||||
|
card: (context: IntakeContext) => ChoiceBlock;
|
||||||
|
/** Records a validated answer. Runs while the main conversation is locked. */
|
||||||
|
save: (effects: IntakeEffects, answer: IntakeAnswer) => Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type FocusOptionId = "day" | "inbox" | "research" | "crew";
|
||||||
|
|
||||||
|
export interface FocusOption {
|
||||||
|
id: FocusOptionId;
|
||||||
|
letter: string;
|
||||||
|
label: string;
|
||||||
|
apps: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const FOCUS_FACTS = {
|
||||||
|
en: {
|
||||||
|
day: "Wants help with day-to-day work first",
|
||||||
|
inbox: "Wants help with inbox and email first",
|
||||||
|
research: "Wants help with research and writing first",
|
||||||
|
crew: "Wants a coordinated crew of specialist bots",
|
||||||
|
},
|
||||||
|
de: {
|
||||||
|
day: "Möchte zuerst bei der täglichen Arbeit unterstützt werden",
|
||||||
|
inbox: "Möchte zuerst bei Posteingang und E-Mail unterstützt werden",
|
||||||
|
research: "Möchte zuerst bei Recherche und Schreiben unterstützt werden",
|
||||||
|
crew: "Möchte eine koordinierte Crew aus Spezialisten-Bots",
|
||||||
|
},
|
||||||
|
} as const satisfies Record<string, Record<FocusOptionId, string>>;
|
||||||
|
|
||||||
|
/** Profile fact text for a focus answer, in the user's locale (defaults to English). */
|
||||||
|
function isFocusOptionId(id: string): id is FocusOptionId {
|
||||||
|
return FOCUS_OPTIONS.some((option) => option.id === id);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function focusFactValue(focusId: string, locale: string): string {
|
||||||
|
if (!isFocusOptionId(focusId)) return focusId;
|
||||||
|
const key: keyof typeof FOCUS_FACTS = locale.toLowerCase().startsWith("de") ? "de" : "en";
|
||||||
|
return FOCUS_FACTS[key][focusId];
|
||||||
|
}
|
||||||
|
|
||||||
|
export const FOCUS_OPTIONS: FocusOption[] = [
|
||||||
|
{
|
||||||
|
id: "day",
|
||||||
|
letter: "A",
|
||||||
|
label: "Day-to-day work",
|
||||||
|
apps: ["slack", "gmail", "googlecalendar"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "inbox",
|
||||||
|
letter: "B",
|
||||||
|
label: "Inbox & email",
|
||||||
|
apps: ["gmail", "googlecalendar", "slack"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "research",
|
||||||
|
letter: "C",
|
||||||
|
label: "Research & writing",
|
||||||
|
apps: ["hackernews", "notion", "googledocs"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "crew",
|
||||||
|
letter: "D",
|
||||||
|
label: "Specialist crew",
|
||||||
|
apps: ["slack", "gmail", "googlecalendar"],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const SKIP: ChoiceOption = { id: "skip", letter: "", label: "Skip" };
|
||||||
|
|
||||||
|
/** The focus card. Onboarding adds a skip; another bot's card is dismissed instead. */
|
||||||
|
export function focusCard(step?: "focus"): ChoiceBlock {
|
||||||
|
return {
|
||||||
|
kind: "choice",
|
||||||
|
question: "What do you want me on first?",
|
||||||
|
options: [
|
||||||
|
...FOCUS_OPTIONS.map(({ id, letter, label }) => ({ id, letter, label })),
|
||||||
|
...(step ? [SKIP] : []),
|
||||||
|
],
|
||||||
|
...(step ? { step } : {}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const RHYTHMS: Record<string, CoachRhythm> = {
|
||||||
|
morning: { morning: "08:00", evening: null, weekly: null },
|
||||||
|
morning_evening: { morning: "08:00", evening: "18:00", weekly: null },
|
||||||
|
weekly: { morning: null, evening: null, weekly: { day: 0, time: "18:00" } },
|
||||||
|
none: { morning: null, evening: null, weekly: null },
|
||||||
|
};
|
||||||
|
|
||||||
|
const QUIET_HOURS: Record<string, QuietHours> = {
|
||||||
|
"22-07": { start: "22:00", end: "07:00" },
|
||||||
|
"23-08": { start: "23:00", end: "08:00" },
|
||||||
|
};
|
||||||
|
|
||||||
|
export const INTAKE_STEPS: IntakeStep[] = [
|
||||||
|
{
|
||||||
|
id: "focus",
|
||||||
|
card: () => focusCard("focus"),
|
||||||
|
save: async ({ prisma, tx, userId }, answer) => {
|
||||||
|
const option = FOCUS_OPTIONS.find((entry) => entry.id === answer.option?.id);
|
||||||
|
if (!option) return;
|
||||||
|
const profile = await tx.userProfile.findUnique({
|
||||||
|
where: { userId },
|
||||||
|
select: { locale: true, timeZone: true },
|
||||||
|
});
|
||||||
|
const locale = intakeContext(profile, new Date()).locale;
|
||||||
|
await tx.userOnboarding.update({ where: { userId }, data: { focus: option.id } });
|
||||||
|
await updateUserProfile(prisma, userId, {
|
||||||
|
addFacts: [
|
||||||
|
{ category: "goals", value: focusFactValue(option.id, locale), source: "onboarding" },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "goal",
|
||||||
|
card: () => ({
|
||||||
|
kind: "choice",
|
||||||
|
step: "goal",
|
||||||
|
question: "What’s one thing you want to get done in the next few weeks?",
|
||||||
|
textInput: { placeholder: "Your goal" },
|
||||||
|
options: [SKIP],
|
||||||
|
}),
|
||||||
|
save: async ({ prisma, userId }, answer) => {
|
||||||
|
if (answer.text)
|
||||||
|
await createGoal(prisma, userId, { title: answer.text, source: "onboarding" });
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "rhythm",
|
||||||
|
card: () => ({
|
||||||
|
kind: "choice",
|
||||||
|
step: "rhythm",
|
||||||
|
question: "When should I check in with you?",
|
||||||
|
options: [
|
||||||
|
{ id: "morning", letter: "A", label: "Every morning" },
|
||||||
|
{ id: "morning_evening", letter: "B", label: "Morning and evening" },
|
||||||
|
{ id: "weekly", letter: "C", label: "Once a week" },
|
||||||
|
{ id: "none", letter: "", label: "Not now" },
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
save: async ({ prisma, jobs, userId }, answer) => {
|
||||||
|
const rhythm = answer.option && RHYTHMS[answer.option.id];
|
||||||
|
if (rhythm) await setCoachRhythm({ prisma, jobs }, { userId, rhythm });
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "quiet_hours",
|
||||||
|
card: (context) => ({
|
||||||
|
kind: "choice",
|
||||||
|
step: "quiet_hours",
|
||||||
|
question: "When should I stay quiet?",
|
||||||
|
options: [
|
||||||
|
...Object.entries(QUIET_HOURS).map(([id, range], index) => ({
|
||||||
|
id,
|
||||||
|
letter: String.fromCharCode(65 + index),
|
||||||
|
label: `${formatClock(range.start, context.locale)} – ${formatClock(range.end, context.locale)}`,
|
||||||
|
})),
|
||||||
|
SKIP,
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
save: async ({ prisma, userId }, answer) => {
|
||||||
|
const quietHours = answer.option && QUIET_HOURS[answer.option.id];
|
||||||
|
if (quietHours) await updateNotificationSettings(prisma, userId, { quietHours });
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// The browser subscribes when the user taps "Turn on"; the answer only moves on.
|
||||||
|
id: "notifications",
|
||||||
|
asks: ({ canEnablePush }) => canEnablePush,
|
||||||
|
card: () => ({
|
||||||
|
kind: "choice",
|
||||||
|
step: "notifications",
|
||||||
|
question: "Can I send you notifications?",
|
||||||
|
options: [
|
||||||
|
{ id: "on", letter: "A", label: "Turn on" },
|
||||||
|
{ id: "later", letter: "", label: "Not now" },
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
save: async () => {},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export function intakeStep(id: OnboardingStep): IntakeStep {
|
||||||
|
const step = INTAKE_STEPS.find((entry) => entry.id === id);
|
||||||
|
if (!step) throw new Error(`Unknown onboarding step: ${id}`);
|
||||||
|
return step;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The step after `id` that applies to this device, or null when onboarding is done. */
|
||||||
|
export function nextIntakeStep(
|
||||||
|
id: OnboardingStep,
|
||||||
|
device: { canEnablePush: boolean },
|
||||||
|
): IntakeStep | null {
|
||||||
|
const index = INTAKE_STEPS.findIndex((entry) => entry.id === id);
|
||||||
|
return INTAKE_STEPS.slice(index + 1).find((entry) => entry.asks?.(device) ?? true) ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Checks an answer against the card it answers; null when the card does not offer it. */
|
||||||
|
export function readIntakeAnswer(
|
||||||
|
card: ChoiceBlock,
|
||||||
|
optionId: string,
|
||||||
|
text: string | undefined,
|
||||||
|
): IntakeAnswer | null {
|
||||||
|
if (optionId === CHOICE_TEXT_ANSWER_ID) {
|
||||||
|
const words = text?.trim();
|
||||||
|
return card.textInput && words ? { text: words } : null;
|
||||||
|
}
|
||||||
|
const option = card.options.find((entry) => entry.id === optionId);
|
||||||
|
return option ? { option } : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The secondary answer ("Skip", "Not now") a dismissed card counts as. */
|
||||||
|
export function secondaryOption(card: ChoiceBlock): ChoiceOption | undefined {
|
||||||
|
return card.options.find((option) => !option.letter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Card times and the closing line follow the user's profile, falling back to English and UTC. */
|
||||||
|
export function intakeContext(
|
||||||
|
profile: { locale: string | null; timeZone: string | null } | null,
|
||||||
|
now: Date,
|
||||||
|
): IntakeContext {
|
||||||
|
let locale = "en";
|
||||||
|
try {
|
||||||
|
if (profile?.locale) locale = Intl.getCanonicalLocales(profile.locale)[0] ?? "en";
|
||||||
|
} catch {
|
||||||
|
// Not a language tag; keep English.
|
||||||
|
}
|
||||||
|
return { locale, timeZone: knownTimeZone(profile?.timeZone) ?? "UTC", now };
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatClock(time: string, locale: string): string {
|
||||||
|
const [hour = 0, minute = 0] = time.split(":").map(Number);
|
||||||
|
return new Intl.DateTimeFormat(locale, {
|
||||||
|
hour: "numeric",
|
||||||
|
minute: "2-digit",
|
||||||
|
timeZone: "UTC",
|
||||||
|
}).format(new Date(Date.UTC(2000, 0, 1, hour, minute)));
|
||||||
|
}
|
||||||
|
|
||||||
|
function localDay(date: Date, timeZone: string): number {
|
||||||
|
const [year, month, day] = new Intl.DateTimeFormat("en-CA", {
|
||||||
|
timeZone,
|
||||||
|
year: "numeric",
|
||||||
|
month: "2-digit",
|
||||||
|
day: "2-digit",
|
||||||
|
})
|
||||||
|
.format(date)
|
||||||
|
.split("-")
|
||||||
|
.map(Number);
|
||||||
|
return Date.UTC(year ?? 1970, (month ?? 1) - 1, day ?? 1) / 86_400_000;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type IntakeNote = Extract<MessageBlock, { kind: "intake_note" }>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The closing note: what happens next, i.e. the first check-in when there is one. The web says
|
||||||
|
* it in the user's language from `nextCheckInAt`; `text` is the English line for everything else.
|
||||||
|
*/
|
||||||
|
export function closingNote(rhythm: CoachRhythm, context: IntakeContext): IntakeNote {
|
||||||
|
const crons = COACH_KEYS.flatMap((key) => coachCron(key, rhythm) ?? []);
|
||||||
|
const next = nextCronDateAcross(crons, context.now, context.timeZone);
|
||||||
|
return {
|
||||||
|
kind: "intake_note",
|
||||||
|
note: "done",
|
||||||
|
nextCheckInAt: next?.toISOString() ?? null,
|
||||||
|
text: closingText(next, context),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function closingText(next: Date | null, context: IntakeContext): string {
|
||||||
|
if (!next) return "All set. I’m here whenever you need me.";
|
||||||
|
const days = localDay(next, context.timeZone) - localDay(context.now, context.timeZone);
|
||||||
|
const day =
|
||||||
|
days === 0
|
||||||
|
? "today"
|
||||||
|
: days === 1
|
||||||
|
? "tomorrow"
|
||||||
|
: `on ${new Intl.DateTimeFormat("en-US", { timeZone: context.timeZone, weekday: "long" }).format(next)}`;
|
||||||
|
const time = new Intl.DateTimeFormat(context.locale, {
|
||||||
|
hour: "numeric",
|
||||||
|
minute: "2-digit",
|
||||||
|
timeZone: context.timeZone,
|
||||||
|
}).format(next);
|
||||||
|
return `All set. I’ll check in ${day} at ${time}.`;
|
||||||
|
}
|
||||||
@ -1,166 +1,192 @@
|
|||||||
import { mkdir, mkdtemp, realpath, writeFile } from "node:fs/promises";
|
import { mkdir, mkdtemp, realpath, rm, symlink, writeFile } from "node:fs/promises";
|
||||||
import { tmpdir } from "node:os";
|
import { tmpdir } from "node:os";
|
||||||
import { join } from "node:path";
|
import path from "node:path";
|
||||||
import type * as db from "@rakazo/db";
|
import type { PrismaClient } from "@rakazo/db";
|
||||||
import { SpaceLimitError } from "@rakazo/db";
|
import { afterAll, describe, expect, it, vi } from "vitest";
|
||||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
import {
|
||||||
import { getScanStatus, startOnboardingScan } from "./onboarding-scan.js";
|
applyOnboardingScan,
|
||||||
|
discoverOnboardingScan,
|
||||||
const createSpaceForMember = vi.hoisted(() => vi.fn());
|
onboardingScanFolder,
|
||||||
const ensureMainConversation = vi.hoisted(() => vi.fn());
|
onboardingScanStatus,
|
||||||
vi.mock("@rakazo/db", async (original) => ({
|
startOnboardingScan,
|
||||||
...(await original<typeof db>()),
|
} from "./onboarding-scan.js";
|
||||||
createSpaceForMember,
|
|
||||||
ensureMainConversation,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const owner = { userId: "user-owner", spaceId: "space-owner", isDeploymentOwner: true };
|
const owner = { userId: "user-owner", spaceId: "space-owner", isDeploymentOwner: true };
|
||||||
const member = { userId: "user-member", spaceId: "space-member", isDeploymentOwner: false };
|
const member = { userId: "user-member", spaceId: "space-member", isDeploymentOwner: false };
|
||||||
|
const roots: string[] = [];
|
||||||
|
|
||||||
type SpaceUpdate = { where: { id: string }; data: { workspacePath: string } };
|
/** Fails the test if the scan writes anything before its checks pass. */
|
||||||
|
const untouchedPrisma = new Proxy(
|
||||||
/** The spaces the scan reads and links; new ones come from `createSpaceForMember`. */
|
{},
|
||||||
function fakeDatabase({ spaceLimit = Number.POSITIVE_INFINITY } = {}) {
|
{
|
||||||
const spaces: Array<{ id: string; name: string; workspacePath: string | null }> = [];
|
get() {
|
||||||
createSpaceForMember.mockImplementation(async (_prisma: unknown, input: { name: string }) => {
|
throw new Error("the database must not be touched");
|
||||||
if (spaces.length >= spaceLimit) throw new SpaceLimitError();
|
|
||||||
const space = { id: `space-${spaces.length + 1}`, name: input.name, workspacePath: null };
|
|
||||||
spaces.push(space);
|
|
||||||
return { id: space.id, name: space.name };
|
|
||||||
});
|
|
||||||
const prisma = {
|
|
||||||
space: {
|
|
||||||
findMany: async () => spaces.filter((space) => space.workspacePath),
|
|
||||||
update: async ({ where, data }: SpaceUpdate) => {
|
|
||||||
const space = spaces.find((candidate) => candidate.id === where.id);
|
|
||||||
if (space) space.workspacePath = data.workspacePath;
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
} as unknown as db.PrismaClient;
|
},
|
||||||
return { prisma, spaces };
|
) as PrismaClient;
|
||||||
|
|
||||||
|
async function tempDir(): Promise<string> {
|
||||||
|
const dir = await realpath(await mkdtemp(path.join(tmpdir(), "evoo-scan-")));
|
||||||
|
roots.push(dir);
|
||||||
|
return dir;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function scanHomeWithOneFile(): Promise<string> {
|
async function files(root: string, entries: string[]): Promise<void> {
|
||||||
const home = await mkdtemp(join(tmpdir(), "evoo-scan-"));
|
for (const entry of entries) {
|
||||||
await mkdir(join(home, "Projects"));
|
const file = path.join(root, entry);
|
||||||
await writeFile(join(home, "Projects", "notes.ts"), "export const x = 1;\n");
|
await mkdir(path.dirname(file), { recursive: true });
|
||||||
return home;
|
await writeFile(file, "x\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function scanHomeWithRepos(...repos: string[]): Promise<string> {
|
async function project(root: string, relative: string, entries: string[]): Promise<void> {
|
||||||
const home = await scanHomeWithOneFile();
|
await mkdir(path.join(root, relative, ".git"), { recursive: true });
|
||||||
for (const repo of repos) await mkdir(join(home, repo, ".git"), { recursive: true });
|
await files(path.join(root, relative), entries);
|
||||||
return home;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function completed(scanId: string, userId: string) {
|
async function completed(scanId: string, userId: string) {
|
||||||
return await vi.waitFor(
|
return await vi.waitFor(
|
||||||
async () => {
|
() => {
|
||||||
const status = await getScanStatus(scanId, userId);
|
const status = onboardingScanStatus(scanId, userId);
|
||||||
if (status.status !== "complete" && status.status !== "failed") throw new Error("pending");
|
if (status.status === "scanning") throw new Error("pending");
|
||||||
return status;
|
return status;
|
||||||
},
|
},
|
||||||
{ timeout: 10_000, interval: 50 },
|
{ timeout: 10_000, interval: 20 },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The emulated runtime that the shared test setup pins keeps the scan simulated. */
|
afterAll(async () => {
|
||||||
function allowHostScan() {
|
await Promise.all(roots.map((root) => rm(root, { recursive: true, force: true })));
|
||||||
vi.stubEnv("AGENT_RUNTIME", "");
|
});
|
||||||
vi.stubEnv("SANDBOX_PROVIDER", "");
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("onboarding scan", () => {
|
describe("onboarding scan", () => {
|
||||||
afterEach(() => {
|
it("lists git projects with their languages and tools and never follows a symlink", async () => {
|
||||||
vi.unstubAllEnvs();
|
const home = await tempDir();
|
||||||
createSpaceForMember.mockReset();
|
const outside = await tempDir();
|
||||||
ensureMainConversation.mockReset();
|
// A home folder tracked by git is not itself a project.
|
||||||
|
await mkdir(path.join(home, ".git"));
|
||||||
|
await project(home, "code/atlas", [
|
||||||
|
"src/a.ts",
|
||||||
|
"src/b.ts",
|
||||||
|
"src/c.ts",
|
||||||
|
"src/d.tsx",
|
||||||
|
"scripts/build.js",
|
||||||
|
"Dockerfile",
|
||||||
|
".github/workflows/ci.yml",
|
||||||
|
"infra/main.tf",
|
||||||
|
"src/odd.constructor",
|
||||||
|
"node_modules/dep/index.js",
|
||||||
|
"node_modules/dep/more.js",
|
||||||
|
]);
|
||||||
|
await project(home, "code/garden", ["main.py"]);
|
||||||
|
await project(home, "deep/a/b/edge", ["tool.py"]);
|
||||||
|
await project(home, "deep/a/b/c/too-deep", ["main.go"]);
|
||||||
|
await project(home, ".hidden/secret", ["main.go"]);
|
||||||
|
await project(home, "notes/node_modules/vendored", ["main.go"]);
|
||||||
|
await files(home, ["notes/todo.md"]);
|
||||||
|
await project(outside, "escaped", ["main.rs"]);
|
||||||
|
await symlink(path.join(outside, "escaped"), path.join(home, "linked"));
|
||||||
|
await symlink(outside, path.join(home, "code/atlas/src/outside"));
|
||||||
|
|
||||||
|
const findings = await discoverOnboardingScan(home);
|
||||||
|
|
||||||
|
expect(findings.projects).toEqual([
|
||||||
|
{
|
||||||
|
id: "project-0",
|
||||||
|
name: "atlas",
|
||||||
|
path: "code/atlas",
|
||||||
|
languages: ["TypeScript", "JavaScript"],
|
||||||
|
tools: ["Docker", "GitHub Actions", "Terraform"],
|
||||||
|
},
|
||||||
|
{ id: "project-1", name: "garden", path: "code/garden", languages: ["Python"], tools: [] },
|
||||||
|
{ id: "project-2", name: "edge", path: "deep/a/b/edge", languages: ["Python"], tools: [] },
|
||||||
|
]);
|
||||||
|
expect(findings.facts).toEqual([
|
||||||
|
{ id: "language:Python", label: "Python", category: "work", value: "Codes in Python" },
|
||||||
|
{
|
||||||
|
id: "language:JavaScript",
|
||||||
|
label: "JavaScript",
|
||||||
|
category: "work",
|
||||||
|
value: "Codes in JavaScript",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "language:TypeScript",
|
||||||
|
label: "TypeScript",
|
||||||
|
category: "work",
|
||||||
|
value: "Codes in TypeScript",
|
||||||
|
},
|
||||||
|
{ id: "tool:Docker", label: "Docker", category: "tools", value: "Uses Docker" },
|
||||||
|
{
|
||||||
|
id: "tool:GitHub Actions",
|
||||||
|
label: "GitHub Actions",
|
||||||
|
category: "tools",
|
||||||
|
value: "Uses GitHub Actions",
|
||||||
|
},
|
||||||
|
{ id: "tool:Terraform", label: "Terraform", category: "tools", value: "Uses Terraform" },
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("hides a scan from every account except the one that started it", async () => {
|
it("offers a scan folder only to the deployment owner and only when it exists", async () => {
|
||||||
const { prisma } = fakeDatabase();
|
const home = await tempDir();
|
||||||
const { scanId } = await startOnboardingScan({ actor: member }, prisma);
|
await files(home, ["file.txt"]);
|
||||||
await expect(getScanStatus(scanId, owner.userId)).rejects.toThrow(/Scan not found/);
|
await expect(onboardingScanFolder(member, home)).resolves.toBeNull();
|
||||||
await expect(getScanStatus(scanId, member.userId)).resolves.toMatchObject({
|
await expect(onboardingScanFolder(owner, undefined)).resolves.toBeNull();
|
||||||
status: expect.any(String),
|
await expect(onboardingScanFolder(owner, path.join(home, "missing"))).resolves.toBeNull();
|
||||||
});
|
await expect(onboardingScanFolder(owner, path.join(home, "file.txt"))).resolves.toBeNull();
|
||||||
await expect(getScanStatus("scan_missing", member.userId)).rejects.toThrow(/Scan not found/);
|
await expect(onboardingScanFolder(owner, home)).resolves.toBe(home);
|
||||||
|
await expect(startOnboardingScan(member, home)).rejects.toMatchObject({ code: "FORBIDDEN" });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("never reads the host without a configured scan home", async () => {
|
it("keeps a scan with the account that started it and returns only relative paths", async () => {
|
||||||
const { prisma } = fakeDatabase();
|
const home = await tempDir();
|
||||||
const { scanId } = await startOnboardingScan({ actor: owner }, prisma);
|
await project(home, "code/atlas", ["index.ts"]);
|
||||||
|
const { scanId } = await startOnboardingScan(owner, home);
|
||||||
|
|
||||||
const status = await completed(scanId, owner.userId);
|
const status = await completed(scanId, owner.userId);
|
||||||
expect(status.status).toBe("complete");
|
expect(status).toEqual({
|
||||||
expect(status.result).toMatchObject({ localRepos: [], fileSummary: { totalFiles: 0 } });
|
status: "complete",
|
||||||
});
|
projects: [{ id: "project-0", name: "atlas", path: "code/atlas" }],
|
||||||
|
facts: [{ id: "language:TypeScript", label: "TypeScript" }],
|
||||||
it("lets only the deployment owner discover the configured scan home", async () => {
|
|
||||||
allowHostScan();
|
|
||||||
const scanHome = await scanHomeWithRepos("Projects/app");
|
|
||||||
const { prisma } = fakeDatabase();
|
|
||||||
|
|
||||||
const asMember = await startOnboardingScan({ actor: member, scanHome }, prisma);
|
|
||||||
const memberStatus = await completed(asMember.scanId, member.userId);
|
|
||||||
expect(memberStatus.result).toMatchObject({ fileSummary: { totalFiles: 0 } });
|
|
||||||
expect(createSpaceForMember).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
const asOwner = await startOnboardingScan({ actor: owner, scanHome }, prisma);
|
|
||||||
const ownerStatus = await completed(asOwner.scanId, owner.userId);
|
|
||||||
expect(ownerStatus.status).toBe("complete");
|
|
||||||
expect(ownerStatus.result).toMatchObject({ fileSummary: { totalFiles: 1 } });
|
|
||||||
});
|
|
||||||
|
|
||||||
it("links each repository of the deployment owner to one project of its own", async () => {
|
|
||||||
allowHostScan();
|
|
||||||
const scanHome = await scanHomeWithRepos("Projects/app", ".config/nvim");
|
|
||||||
const app = await realpath(join(scanHome, "Projects", "app"));
|
|
||||||
const { prisma, spaces } = fakeDatabase();
|
|
||||||
|
|
||||||
const first = await startOnboardingScan({ actor: owner, scanHome }, prisma);
|
|
||||||
const firstStatus = await completed(first.scanId, owner.userId);
|
|
||||||
expect(firstStatus.status).toBe("complete");
|
|
||||||
expect(firstStatus.result).toMatchObject({
|
|
||||||
localRepos: [{ name: "app" }],
|
|
||||||
createdProjects: [{ spaceId: "space-1", name: "app", path: app }],
|
|
||||||
});
|
});
|
||||||
expect(createSpaceForMember).toHaveBeenCalledWith(prisma, {
|
expect(JSON.stringify(status)).not.toContain(home);
|
||||||
currentSpaceId: owner.spaceId,
|
expect(() => onboardingScanStatus(scanId, member.userId)).toThrow(
|
||||||
userId: owner.userId,
|
expect.objectContaining({ code: "NOT_FOUND" }),
|
||||||
name: "app",
|
);
|
||||||
|
expect(() => onboardingScanStatus("missing-scan", owner.userId)).toThrow(
|
||||||
|
expect.objectContaining({ code: "NOT_FOUND" }),
|
||||||
|
);
|
||||||
|
await expect(
|
||||||
|
applyOnboardingScan(untouchedPrisma, member, home, {
|
||||||
|
scanId,
|
||||||
|
projectIds: ["project-0"],
|
||||||
|
factIds: [],
|
||||||
|
}),
|
||||||
|
).rejects.toMatchObject({ code: "NOT_FOUND" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("checks ownership, the folder and every chosen project again before writing", async () => {
|
||||||
|
const home = await tempDir();
|
||||||
|
const outside = await tempDir();
|
||||||
|
await project(home, "code/atlas", ["index.ts"]);
|
||||||
|
await project(home, "code/garden", ["main.py"]);
|
||||||
|
await project(outside, "elsewhere", ["main.rs"]);
|
||||||
|
const { scanId } = await startOnboardingScan(owner, home);
|
||||||
|
await completed(scanId, owner.userId);
|
||||||
|
const choice = { scanId, projectIds: ["project-1"], factIds: ["language:Python"] };
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
applyOnboardingScan(untouchedPrisma, { ...owner, isDeploymentOwner: false }, home, choice),
|
||||||
|
).rejects.toMatchObject({ code: "FORBIDDEN" });
|
||||||
|
await expect(
|
||||||
|
applyOnboardingScan(untouchedPrisma, owner, outside, choice),
|
||||||
|
).rejects.toMatchObject({ code: "FORBIDDEN" });
|
||||||
|
|
||||||
|
// The chosen project became a symlink out of the scan folder after the scan.
|
||||||
|
await rm(path.join(home, "code/garden"), { recursive: true });
|
||||||
|
await symlink(path.join(outside, "elsewhere"), path.join(home, "code/garden"));
|
||||||
|
await expect(applyOnboardingScan(untouchedPrisma, owner, home, choice)).rejects.toMatchObject({
|
||||||
|
code: "BAD_REQUEST",
|
||||||
|
message: "That folder does not exist or is not readable.",
|
||||||
});
|
});
|
||||||
expect(spaces).toEqual([{ id: "space-1", name: "app", workspacePath: app }]);
|
|
||||||
// Each new project starts with its own main agent.
|
|
||||||
expect(ensureMainConversation).toHaveBeenCalledWith(prisma, owner.userId, "space-1");
|
|
||||||
|
|
||||||
// Scanning again finds the linked project instead of creating a second one.
|
|
||||||
const again = await startOnboardingScan({ actor: owner, scanHome }, prisma);
|
|
||||||
const againStatus = await completed(again.scanId, owner.userId);
|
|
||||||
expect(againStatus.result).toMatchObject({ createdProjects: [] });
|
|
||||||
expect(createSpaceForMember).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("joins a running scan instead of linking its repositories twice", async () => {
|
|
||||||
allowHostScan();
|
|
||||||
const scanHome = await scanHomeWithRepos("Projects/app");
|
|
||||||
const { prisma, spaces } = fakeDatabase();
|
|
||||||
|
|
||||||
const first = await startOnboardingScan({ actor: owner, scanHome }, prisma);
|
|
||||||
const second = await startOnboardingScan({ actor: owner, scanHome }, prisma);
|
|
||||||
expect(second.scanId).toBe(first.scanId);
|
|
||||||
await completed(first.scanId, owner.userId);
|
|
||||||
expect(spaces).toHaveLength(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("stops creating projects at the space limit and still completes", async () => {
|
|
||||||
allowHostScan();
|
|
||||||
const scanHome = await scanHomeWithRepos("Projects/a", "Projects/b", "Projects/c");
|
|
||||||
const { prisma } = fakeDatabase({ spaceLimit: 1 });
|
|
||||||
|
|
||||||
const { scanId } = await startOnboardingScan({ actor: owner, scanHome }, prisma);
|
|
||||||
const status = await completed(scanId, owner.userId);
|
|
||||||
expect(status.status).toBe("complete");
|
|
||||||
expect(status.result).toMatchObject({ createdProjects: [{ spaceId: "space-1" }] });
|
|
||||||
expect(createSpaceForMember).toHaveBeenCalledTimes(2);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,7 @@
|
|||||||
import type * as db from "@rakazo/db";
|
import type * as db from "@rakazo/db";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import { chooseFocus, markAppConnected } from "./onboarding.js";
|
import { chooseFocus, markAppConnected } from "./onboarding.js";
|
||||||
|
import { focusCard } from "./onboarding-intake.js";
|
||||||
|
|
||||||
const posted = vi.hoisted(() => [] as Array<{ blocks: unknown[] }>);
|
const posted = vi.hoisted(() => [] as Array<{ blocks: unknown[] }>);
|
||||||
vi.mock("@rakazo/db", async (original) => ({
|
vi.mock("@rakazo/db", async (original) => ({
|
||||||
@ -13,16 +14,16 @@ vi.mock("@rakazo/db", async (original) => ({
|
|||||||
}));
|
}));
|
||||||
function fixture(catalog: unknown[]) {
|
function fixture(catalog: unknown[]) {
|
||||||
posted.length = 0;
|
posted.length = 0;
|
||||||
|
// A specialist's focus card, waiting for an answer.
|
||||||
|
const pending = vi.fn(async () => [{ id: "choice", blocks: [focusCard()] }]);
|
||||||
const tx = {
|
const tx = {
|
||||||
$executeRaw: vi.fn(),
|
$executeRaw: vi.fn(),
|
||||||
message: {
|
message: { findMany: pending, update: vi.fn() },
|
||||||
findMany: vi.fn(async () => [{ id: "choice", blocks: [{ kind: "choice", answerId: null }] }]),
|
|
||||||
update: vi.fn(),
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
const deps = {
|
const deps = {
|
||||||
prisma: {
|
prisma: {
|
||||||
bot: { findFirst: vi.fn(async () => ({ id: "bot", thread: { id: "thread" } })) },
|
bot: { findFirst: vi.fn(async () => ({ id: "bot", thread: { id: "thread" } })) },
|
||||||
|
message: { findMany: pending },
|
||||||
$transaction: vi.fn(async (fn: (tx: unknown) => unknown) => fn(tx)),
|
$transaction: vi.fn(async (fn: (tx: unknown) => unknown) => fn(tx)),
|
||||||
},
|
},
|
||||||
events: { notify: vi.fn() },
|
events: { notify: vi.fn() },
|
||||||
@ -39,17 +40,16 @@ function fixture(catalog: unknown[]) {
|
|||||||
describe("onboarding connection suggestions", () => {
|
describe("onboarding connection suggestions", () => {
|
||||||
it("does not invent authorization cards when no connector has an app catalog", async () => {
|
it("does not invent authorization cards when no connector has an app catalog", async () => {
|
||||||
const { deps, actor } = fixture([]);
|
const { deps, actor } = fixture([]);
|
||||||
await chooseFocus(deps, actor, "bot", "day");
|
await chooseFocus(deps, actor, "bot", { optionId: "day" });
|
||||||
expect(posted.flatMap((message) => message.blocks)).not.toContainEqual(
|
// The answered card is all that changes; nothing is said around it.
|
||||||
expect.objectContaining({ kind: "app_connect" }),
|
expect(posted).toEqual([]);
|
||||||
);
|
expect(deps.events.notify).toHaveBeenCalled();
|
||||||
expect(posted.length).toBeGreaterThan(0);
|
|
||||||
});
|
});
|
||||||
it("uses the available connector and omits unavailable apps", async () => {
|
it("uses the available connector and omits unavailable apps", async () => {
|
||||||
const { deps, actor } = fixture([
|
const { deps, actor } = fixture([
|
||||||
{ connectorId: "pipedream", slug: "slack", name: "Slack", connected: false, logo: null },
|
{ connectorId: "pipedream", slug: "slack", name: "Slack", connected: false, logo: null },
|
||||||
]);
|
]);
|
||||||
await chooseFocus(deps, actor, "bot", "day");
|
await chooseFocus(deps, actor, "bot", { optionId: "day" });
|
||||||
expect(
|
expect(
|
||||||
posted
|
posted
|
||||||
.flatMap((message) => message.blocks)
|
.flatMap((message) => message.blocks)
|
||||||
@ -57,6 +57,8 @@ describe("onboarding connection suggestions", () => {
|
|||||||
).toEqual([
|
).toEqual([
|
||||||
expect.objectContaining({ connectorId: "pipedream", provider: "slack", name: "Slack" }),
|
expect.objectContaining({ connectorId: "pipedream", provider: "slack", name: "Slack" }),
|
||||||
]);
|
]);
|
||||||
|
// The cards speak for themselves.
|
||||||
|
expect(posted).toHaveLength(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -1,67 +1,47 @@
|
|||||||
|
import { ORPCError } from "@orpc/server";
|
||||||
|
import type { JobPublisher } from "@rakazo/adapter-kit";
|
||||||
import type { ConnectorRegistry } from "@rakazo/adapters";
|
import type { ConnectorRegistry } from "@rakazo/adapters";
|
||||||
import type { Actor, MessageBlock } from "@rakazo/contracts";
|
import { getCoachRhythm } from "@rakazo/adapters";
|
||||||
|
import type { Actor, MessageBlock, OnboardingStep } from "@rakazo/contracts";
|
||||||
|
import { CHOICE_TEXT_ANSWER_ID } from "@rakazo/contracts";
|
||||||
import { featuredConnectorProvidersMatch } from "@rakazo/core";
|
import { featuredConnectorProvidersMatch } from "@rakazo/core";
|
||||||
import {
|
import {
|
||||||
appendEventInTransaction,
|
appendEventInTransaction,
|
||||||
createThreadMessageInTransaction,
|
createThreadMessageInTransaction,
|
||||||
ensureMainConversation,
|
ensureMainConversation,
|
||||||
IsolationError,
|
IsolationError,
|
||||||
|
isPersonalMainBot,
|
||||||
type Prisma,
|
type Prisma,
|
||||||
type PrismaClient,
|
type PrismaClient,
|
||||||
type ThreadEvents,
|
type ThreadEvents,
|
||||||
} from "@rakazo/db";
|
} from "@rakazo/db";
|
||||||
|
import type { ChoiceBlock, FocusOption, IntakeAnswer, IntakeContext } from "./onboarding-intake.js";
|
||||||
|
import {
|
||||||
|
closingNote,
|
||||||
|
FOCUS_OPTIONS,
|
||||||
|
focusCard,
|
||||||
|
intakeContext,
|
||||||
|
intakeStep,
|
||||||
|
nextIntakeStep,
|
||||||
|
readIntakeAnswer,
|
||||||
|
secondaryOption,
|
||||||
|
} from "./onboarding-intake.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* First-run conversational onboarding, seeded deterministically into the bot's
|
* Conversational onboarding, posted deterministically into a bot's thread; no model tokens are
|
||||||
* thread: greeting, a focus choice, and available app cards the user authorizes
|
* spent. The main bot asks the onboarding questions (`onboarding-intake.ts`) in the main
|
||||||
* inline. Focus must not rename the bot. No model tokens are spent. The main bot
|
* conversation, one card after each answer. Any other bot gets one focus card that suggests
|
||||||
* talks in the main conversation, where onboarding lands.
|
* apps the user authorizes inline. Focus never renames a bot.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type OnboardingDeps = {
|
type OnboardingDeps = {
|
||||||
prisma: PrismaClient;
|
prisma: PrismaClient;
|
||||||
events: ThreadEvents;
|
events: ThreadEvents;
|
||||||
connectors: ConnectorRegistry;
|
connectors: ConnectorRegistry;
|
||||||
|
jobs: JobPublisher;
|
||||||
};
|
};
|
||||||
|
|
||||||
type FocusOption = {
|
type Target = { spaceId: string; botId: string; threadId: string };
|
||||||
id: string;
|
|
||||||
letter: string;
|
|
||||||
label: string;
|
|
||||||
summary: string;
|
|
||||||
apps: string[];
|
|
||||||
};
|
|
||||||
|
|
||||||
const FOCUS_OPTIONS: FocusOption[] = [
|
|
||||||
{
|
|
||||||
id: "day",
|
|
||||||
letter: "A",
|
|
||||||
label: "Day-to-day work",
|
|
||||||
summary: "Slack, calendar, and email",
|
|
||||||
apps: ["slack", "gmail", "googlecalendar"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "inbox",
|
|
||||||
letter: "B",
|
|
||||||
label: "Inbox & email",
|
|
||||||
summary: "email and calendar",
|
|
||||||
apps: ["gmail", "googlecalendar", "slack"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "research",
|
|
||||||
letter: "C",
|
|
||||||
label: "Research & writing",
|
|
||||||
summary: "the web, notes, and docs",
|
|
||||||
apps: ["hackernews", "notion", "googledocs"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "crew",
|
|
||||||
letter: "D",
|
|
||||||
label: "Specialist crew",
|
|
||||||
summary: "a coordinated team of focused Oos",
|
|
||||||
apps: ["slack", "gmail", "googlecalendar"],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const APP_DESCRIPTIONS: Record<string, string> = {
|
const APP_DESCRIPTIONS: Record<string, string> = {
|
||||||
slack: "Search, read, and send messages.",
|
slack: "Search, read, and send messages.",
|
||||||
@ -85,74 +65,102 @@ async function requireBotThread(deps: OnboardingDeps, actor: Actor, botId: strin
|
|||||||
return { bot, thread: bot.thread };
|
return { bot, thread: bot.thread };
|
||||||
}
|
}
|
||||||
|
|
||||||
async function post(
|
/**
|
||||||
deps: OnboardingDeps,
|
* Serializes card writes on a thread. NO KEY UPDATE still lets an answer's effects insert rows
|
||||||
target: { spaceId: string; botId: string; threadId: string },
|
* that reference the thread from another connection (the coach routines do).
|
||||||
|
*/
|
||||||
|
async function lockThread(tx: Prisma.TransactionClient, threadId: string): Promise<void> {
|
||||||
|
await tx.$executeRaw`SELECT id FROM threads WHERE id = ${threadId} FOR NO KEY UPDATE`;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function postInTransaction(
|
||||||
|
tx: Prisma.TransactionClient,
|
||||||
|
target: Target,
|
||||||
blocks: MessageBlock[],
|
blocks: MessageBlock[],
|
||||||
): Promise<string> {
|
): Promise<{ seq: number }> {
|
||||||
const committed = await deps.prisma.$transaction(async (tx: Prisma.TransactionClient) => {
|
const message = await createThreadMessageInTransaction(tx, {
|
||||||
const message = await createThreadMessageInTransaction(tx, {
|
threadId: target.threadId,
|
||||||
threadId: target.threadId,
|
role: "bot",
|
||||||
role: "bot",
|
blocks,
|
||||||
blocks,
|
botId: target.botId,
|
||||||
botId: target.botId,
|
});
|
||||||
});
|
return appendEventInTransaction(tx, {
|
||||||
const event = await appendEventInTransaction(tx, {
|
spaceId: target.spaceId,
|
||||||
spaceId: target.spaceId,
|
threadId: target.threadId,
|
||||||
threadId: target.threadId,
|
botId: target.botId,
|
||||||
botId: target.botId,
|
type: "thread.message.created",
|
||||||
type: "thread.message.created",
|
payload: { messageId: message.id, role: "bot", blocks },
|
||||||
payload: { messageId: message.id, role: "bot", blocks },
|
});
|
||||||
});
|
}
|
||||||
return { message, event };
|
|
||||||
|
async function updateBlocksInTransaction(
|
||||||
|
tx: Prisma.TransactionClient,
|
||||||
|
target: Target,
|
||||||
|
messageId: string,
|
||||||
|
blocks: MessageBlock[],
|
||||||
|
): Promise<{ seq: number }> {
|
||||||
|
await tx.message.update({ where: { id: messageId }, data: { blocks } });
|
||||||
|
return appendEventInTransaction(tx, {
|
||||||
|
spaceId: target.spaceId,
|
||||||
|
threadId: target.threadId,
|
||||||
|
botId: target.botId,
|
||||||
|
type: "thread.message.updated",
|
||||||
|
payload: { messageId, role: "bot", blocks },
|
||||||
});
|
});
|
||||||
await deps.events.notify(target.threadId, committed.event.seq);
|
|
||||||
return committed.message.id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateBlocks(
|
async function updateBlocks(
|
||||||
deps: OnboardingDeps,
|
deps: OnboardingDeps,
|
||||||
target: { spaceId: string; botId: string; threadId: string },
|
target: Target,
|
||||||
messageId: string,
|
messageId: string,
|
||||||
blocks: MessageBlock[],
|
blocks: MessageBlock[],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const event = await deps.prisma.$transaction(async (tx: Prisma.TransactionClient) => {
|
const event = await deps.prisma.$transaction((tx: Prisma.TransactionClient) =>
|
||||||
await tx.message.update({ where: { id: messageId }, data: { blocks } });
|
updateBlocksInTransaction(tx, target, messageId, blocks),
|
||||||
return appendEventInTransaction(tx, {
|
);
|
||||||
spaceId: target.spaceId,
|
|
||||||
threadId: target.threadId,
|
|
||||||
botId: target.botId,
|
|
||||||
type: "thread.message.updated",
|
|
||||||
payload: { messageId, role: "bot", blocks },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
await deps.events.notify(target.threadId, event.seq);
|
await deps.events.notify(target.threadId, event.seq);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sentinel answerId for a focus card the user dismissed without choosing. */
|
/** Sentinel answerId for a focus card the user dismissed without choosing. */
|
||||||
export const FOCUS_DISMISSED_ANSWER_ID = "_dismissed";
|
export const FOCUS_DISMISSED_ANSWER_ID = "_dismissed";
|
||||||
|
|
||||||
|
/** Checks that the caller may onboard this bot's thread; fresh threads start empty. */
|
||||||
export async function startOnboarding(
|
export async function startOnboarding(
|
||||||
deps: OnboardingDeps,
|
deps: OnboardingDeps,
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
botId: string,
|
botId: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { thread } = await requireBotThread(deps, actor, botId);
|
await requireBotThread(deps, actor, botId);
|
||||||
const existing = await deps.prisma.message.count({ where: { threadId: thread.id } });
|
|
||||||
if (existing > 0) return;
|
|
||||||
// Fresh chats start empty (same as web). The focus card is posted later via
|
|
||||||
// promptFocus so non-first bots can wait ~10s for free typing, or skip if the
|
|
||||||
// user already engaged.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function messageHasChoice(blocks: MessageBlock[]): boolean {
|
function isChoice(block: MessageBlock): block is ChoiceBlock {
|
||||||
return blocks.some((block) => block.kind === "choice");
|
return block.kind === "choice";
|
||||||
}
|
}
|
||||||
|
|
||||||
function messageHasPendingChoice(blocks: MessageBlock[]): boolean {
|
function pendingChoice(blocks: MessageBlock[]): ChoiceBlock | undefined {
|
||||||
return blocks.some((block) => block.kind === "choice" && !block.answerId);
|
return blocks.find((block): block is ChoiceBlock => isChoice(block) && !block.answerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The newest card in the thread that still waits for an answer, however long ago it came. */
|
||||||
|
async function findPendingCard(client: PrismaClient | Prisma.TransactionClient, threadId: string) {
|
||||||
|
const messages = await client.message.findMany({
|
||||||
|
where: { threadId, role: "bot", blocks: { array_contains: [{ kind: "choice" }] } },
|
||||||
|
select: { id: true, blocks: true },
|
||||||
|
orderBy: { seq: "desc" },
|
||||||
|
take: 20,
|
||||||
|
});
|
||||||
|
for (const message of messages) {
|
||||||
|
const blocks = message.blocks as MessageBlock[];
|
||||||
|
const card = pendingChoice(blocks);
|
||||||
|
if (card) return { messageId: message.id, blocks, card };
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Posts the first card once. The main bot starts the onboarding questions, one set per user;
|
||||||
|
* any other bot posts its focus card only while its thread is still idle.
|
||||||
|
*/
|
||||||
export async function promptFocus(
|
export async function promptFocus(
|
||||||
deps: OnboardingDeps,
|
deps: OnboardingDeps,
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
@ -160,44 +168,32 @@ export async function promptFocus(
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { bot, thread } = await requireBotThread(deps, actor, botId);
|
const { bot, thread } = await requireBotThread(deps, actor, botId);
|
||||||
const target = { spaceId: actor.spaceId, botId: bot.id, threadId: thread.id };
|
const target = { spaceId: actor.spaceId, botId: bot.id, threadId: thread.id };
|
||||||
const blocks: MessageBlock[] = [
|
// The intake runs with the personal main agent; a project's main bot starts like any other.
|
||||||
{
|
const main = await isPersonalMainBot(deps.prisma, bot);
|
||||||
kind: "choice",
|
// Check + insert + event in one transaction so concurrent callers cannot duplicate cards,
|
||||||
question: "What do you want me on first?",
|
// and a concurrent user send cannot publish first.
|
||||||
options: FOCUS_OPTIONS.map(({ id, letter, label }) => ({ id, letter, label })),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
// Check + insert + event in one transaction so concurrent promptFocus calls
|
|
||||||
// cannot duplicate cards, and a concurrent user send cannot publish first.
|
|
||||||
const committed = await deps.prisma.$transaction(async (tx: Prisma.TransactionClient) => {
|
const committed = await deps.prisma.$transaction(async (tx: Prisma.TransactionClient) => {
|
||||||
// Serialize concurrent promptFocus callers on this thread before the gate check.
|
await lockThread(tx, thread.id);
|
||||||
await tx.$executeRaw`SELECT id FROM threads WHERE id = ${thread.id} FOR UPDATE`;
|
if (main && (await tx.userOnboarding.findUnique({ where: { userId: actor.userId } }))) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
const recent = await tx.message.findMany({
|
const recent = await tx.message.findMany({
|
||||||
where: { threadId: thread.id },
|
where: { threadId: thread.id },
|
||||||
select: { role: true, blocks: true },
|
select: { role: true, blocks: true },
|
||||||
orderBy: { createdAt: "asc" },
|
|
||||||
});
|
});
|
||||||
if (recent.some((message) => message.role === "user")) return null;
|
if (!main && recent.some((message) => message.role === "user")) return null;
|
||||||
if (recent.some((message) => messageHasChoice(message.blocks as MessageBlock[]))) return null;
|
if (recent.some((message) => (message.blocks as MessageBlock[]).some(isChoice))) return null;
|
||||||
const message = await createThreadMessageInTransaction(tx, {
|
if (!main) return postInTransaction(tx, target, [focusCard()]);
|
||||||
threadId: target.threadId,
|
const first = intakeStep("focus");
|
||||||
role: "bot",
|
await tx.userOnboarding.create({ data: { userId: actor.userId, step: first.id } });
|
||||||
blocks,
|
return postInTransaction(tx, target, [
|
||||||
botId: target.botId,
|
first.card(await loadIntakeContext(tx, actor.userId, new Date())),
|
||||||
});
|
]);
|
||||||
const event = await appendEventInTransaction(tx, {
|
|
||||||
spaceId: target.spaceId,
|
|
||||||
threadId: target.threadId,
|
|
||||||
botId: target.botId,
|
|
||||||
type: "thread.message.created",
|
|
||||||
payload: { messageId: message.id, role: "bot", blocks },
|
|
||||||
});
|
|
||||||
return { message, event };
|
|
||||||
});
|
});
|
||||||
if (!committed) return;
|
if (committed) await deps.events.notify(target.threadId, committed.seq);
|
||||||
await deps.events.notify(target.threadId, committed.event.seq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Dismisses the pending card. An onboarding question counts as skipped, so the next follows. */
|
||||||
export async function dismissFocus(
|
export async function dismissFocus(
|
||||||
deps: OnboardingDeps,
|
deps: OnboardingDeps,
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
@ -205,81 +201,128 @@ export async function dismissFocus(
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { bot, thread } = await requireBotThread(deps, actor, botId);
|
const { bot, thread } = await requireBotThread(deps, actor, botId);
|
||||||
const target = { spaceId: actor.spaceId, botId: bot.id, threadId: thread.id };
|
const target = { spaceId: actor.spaceId, botId: bot.id, threadId: thread.id };
|
||||||
const claimed = await deps.prisma.$transaction(async (tx: Prisma.TransactionClient) => {
|
const pending = await findPendingCard(deps.prisma, thread.id);
|
||||||
await tx.$executeRaw`SELECT id FROM threads WHERE id = ${thread.id} FOR UPDATE`;
|
const skip = pending?.card.step ? secondaryOption(pending.card) : undefined;
|
||||||
const recent = await tx.message.findMany({
|
if (pending?.card.step && skip) {
|
||||||
where: { threadId: thread.id },
|
await chooseFocus(deps, actor, botId, { step: pending.card.step, optionId: skip.id });
|
||||||
orderBy: { createdAt: "asc" },
|
return;
|
||||||
});
|
}
|
||||||
const pending = recent.find((message) =>
|
const event = await deps.prisma.$transaction(async (tx: Prisma.TransactionClient) => {
|
||||||
messageHasPendingChoice(message.blocks as MessageBlock[]),
|
await lockThread(tx, thread.id);
|
||||||
|
const current = await findPendingCard(tx, thread.id);
|
||||||
|
if (!current || current.card.step) return null;
|
||||||
|
const blocks = current.blocks.map((block) =>
|
||||||
|
block === current.card ? { ...current.card, answerId: FOCUS_DISMISSED_ANSWER_ID } : block,
|
||||||
);
|
);
|
||||||
if (!pending) return null;
|
return updateBlocksInTransaction(tx, target, current.messageId, blocks);
|
||||||
const blocks = (pending.blocks as MessageBlock[]).map((block) =>
|
|
||||||
block.kind === "choice" && !block.answerId
|
|
||||||
? { ...block, answerId: FOCUS_DISMISSED_ANSWER_ID }
|
|
||||||
: block,
|
|
||||||
);
|
|
||||||
await tx.message.update({ where: { id: pending.id }, data: { blocks } });
|
|
||||||
const event = await appendEventInTransaction(tx, {
|
|
||||||
spaceId: target.spaceId,
|
|
||||||
threadId: target.threadId,
|
|
||||||
botId: target.botId,
|
|
||||||
type: "thread.message.updated",
|
|
||||||
payload: { messageId: pending.id, role: "bot", blocks },
|
|
||||||
});
|
|
||||||
return { messageId: pending.id, blocks, event };
|
|
||||||
});
|
});
|
||||||
if (!claimed) return;
|
if (event) await deps.events.notify(target.threadId, event.seq);
|
||||||
await deps.events.notify(target.threadId, claimed.event.seq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ChooseInput {
|
||||||
|
step?: OnboardingStep;
|
||||||
|
optionId: string;
|
||||||
|
text?: string;
|
||||||
|
canEnablePush?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Answers the pending card. The answer is checked against that card, a stale answer (another
|
||||||
|
* card is pending by now) is ignored, and everything that follows is committed together with
|
||||||
|
* it: the focus card's app suggestions, then the next onboarding question or the closing line.
|
||||||
|
*/
|
||||||
export async function chooseFocus(
|
export async function chooseFocus(
|
||||||
deps: OnboardingDeps,
|
deps: OnboardingDeps,
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
botId: string,
|
botId: string,
|
||||||
optionId: string,
|
input: ChooseInput,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const option = FOCUS_OPTIONS.find((entry) => entry.id === optionId);
|
|
||||||
if (!option) throw new IsolationError();
|
|
||||||
const { bot, thread } = await requireBotThread(deps, actor, botId);
|
const { bot, thread } = await requireBotThread(deps, actor, botId);
|
||||||
const target = { spaceId: actor.spaceId, botId: bot.id, threadId: thread.id };
|
const target = { spaceId: actor.spaceId, botId: bot.id, threadId: thread.id };
|
||||||
|
const pending = await findPendingCard(deps.prisma, thread.id);
|
||||||
|
if (!pending || pending.card.step !== input.step) return;
|
||||||
|
const answer = readIntakeAnswer(pending.card, input.optionId, input.text);
|
||||||
|
if (!answer) throw new ORPCError("BAD_REQUEST", { message: "This card has no such answer" });
|
||||||
|
const focus =
|
||||||
|
!pending.card.step || pending.card.step === "focus"
|
||||||
|
? FOCUS_OPTIONS.find((option) => option.id === answer.option?.id)
|
||||||
|
: undefined;
|
||||||
|
// Fetched before the transaction: the connector catalog can take seconds.
|
||||||
|
const followUp = focus ? await focusFollowUp(deps, actor, bot.id, focus) : [];
|
||||||
|
|
||||||
const claimed = await deps.prisma.$transaction(async (tx: Prisma.TransactionClient) => {
|
const event = await deps.prisma.$transaction(async (tx: Prisma.TransactionClient) => {
|
||||||
await tx.$executeRaw`SELECT id FROM threads WHERE id = ${thread.id} FOR UPDATE`;
|
await lockThread(tx, thread.id);
|
||||||
const recent = await tx.message.findMany({
|
const current = await findPendingCard(tx, thread.id);
|
||||||
where: { threadId: thread.id },
|
if (current?.messageId !== pending.messageId || current.card.step !== input.step) return null;
|
||||||
orderBy: { createdAt: "asc" },
|
const onboarding = current.card.step
|
||||||
});
|
? await tx.userOnboarding.findUnique({ where: { userId: actor.userId } })
|
||||||
const pending = recent.find((message) =>
|
: null;
|
||||||
messageHasPendingChoice(message.blocks as MessageBlock[]),
|
if (current.card.step) {
|
||||||
);
|
if (onboarding?.step !== current.card.step) return null;
|
||||||
if (!pending) return null;
|
await intakeStep(current.card.step).save(
|
||||||
const blocks = (pending.blocks as MessageBlock[]).map((block) =>
|
{ prisma: deps.prisma, jobs: deps.jobs, tx, userId: actor.userId },
|
||||||
block.kind === "choice" ? { ...block, answerId: option.id } : block,
|
answer,
|
||||||
);
|
);
|
||||||
await tx.message.update({ where: { id: pending.id }, data: { blocks } });
|
}
|
||||||
const event = await appendEventInTransaction(tx, {
|
const answered = answeredCard(current.card, answer);
|
||||||
spaceId: target.spaceId,
|
const blocks = current.blocks.map((block) => (block === current.card ? answered : block));
|
||||||
threadId: target.threadId,
|
let last = await updateBlocksInTransaction(tx, target, current.messageId, blocks);
|
||||||
botId: target.botId,
|
for (const message of followUp) last = await postInTransaction(tx, target, message);
|
||||||
type: "thread.message.updated",
|
if (current.card.step) {
|
||||||
payload: { messageId: pending.id, role: "bot", blocks },
|
last = await askNext(deps, tx, target, actor.userId, current.card.step, input.canEnablePush);
|
||||||
});
|
}
|
||||||
return { messageId: pending.id, blocks, event };
|
return last;
|
||||||
});
|
});
|
||||||
if (!claimed) return;
|
if (event) await deps.events.notify(target.threadId, event.seq);
|
||||||
await deps.events.notify(target.threadId, claimed.event.seq);
|
}
|
||||||
|
|
||||||
// Keep the name and title the user chose when creating the bot; the focus
|
function answeredCard(card: ChoiceBlock, answer: IntakeAnswer): ChoiceBlock {
|
||||||
// step only suggests apps, it must not rename the bot.
|
return answer.option
|
||||||
await post(deps, target, [
|
? { ...card, answerId: answer.option.id }
|
||||||
{
|
: { ...card, answerId: CHOICE_TEXT_ANSWER_ID, answerText: answer.text };
|
||||||
kind: "text",
|
}
|
||||||
text: `Got it. ${capitalize(option.summary)}. I’ll see what’s already connected so I don’t make you set something up twice.`,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
|
async function loadIntakeContext(
|
||||||
|
tx: Prisma.TransactionClient,
|
||||||
|
userId: string,
|
||||||
|
now: Date,
|
||||||
|
): Promise<IntakeContext> {
|
||||||
|
const profile = await tx.userProfile.findUnique({
|
||||||
|
where: { userId },
|
||||||
|
select: { locale: true, timeZone: true },
|
||||||
|
});
|
||||||
|
return intakeContext(profile, now);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Posts the next onboarding question, or the closing line once every step is answered. */
|
||||||
|
async function askNext(
|
||||||
|
deps: OnboardingDeps,
|
||||||
|
tx: Prisma.TransactionClient,
|
||||||
|
target: Target,
|
||||||
|
userId: string,
|
||||||
|
answeredStep: OnboardingStep,
|
||||||
|
canEnablePush = false,
|
||||||
|
): Promise<{ seq: number }> {
|
||||||
|
const now = new Date();
|
||||||
|
const context = await loadIntakeContext(tx, userId, now);
|
||||||
|
const next = nextIntakeStep(answeredStep, { canEnablePush });
|
||||||
|
if (next) {
|
||||||
|
await tx.userOnboarding.update({ where: { userId }, data: { step: next.id } });
|
||||||
|
return postInTransaction(tx, target, [next.card(context)]);
|
||||||
|
}
|
||||||
|
// The rhythm step saved its answer outside this transaction, so the routines are readable.
|
||||||
|
const rhythm = await getCoachRhythm(deps.prisma, userId);
|
||||||
|
await tx.userOnboarding.update({ where: { userId }, data: { step: null, completedAt: now } });
|
||||||
|
return postInTransaction(tx, target, [closingNote(rhythm, context)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The apps worth connecting for a focus, as cards; they need no words around them. */
|
||||||
|
async function focusFollowUp(
|
||||||
|
deps: OnboardingDeps,
|
||||||
|
actor: Actor,
|
||||||
|
botId: string,
|
||||||
|
option: FocusOption,
|
||||||
|
): Promise<MessageBlock[][]> {
|
||||||
const providers = deps.connectors.managedProviders();
|
const providers = deps.connectors.managedProviders();
|
||||||
const catalog = (
|
const catalog = (
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
@ -290,14 +333,14 @@ export async function chooseFocus(
|
|||||||
traceId: "onboarding.choose",
|
traceId: "onboarding.choose",
|
||||||
spaceId: actor.spaceId,
|
spaceId: actor.spaceId,
|
||||||
userId: actor.userId,
|
userId: actor.userId,
|
||||||
botId: bot.id,
|
botId,
|
||||||
signal: AbortSignal.timeout(15_000),
|
signal: AbortSignal.timeout(15_000),
|
||||||
})
|
})
|
||||||
.catch(() => []),
|
.catch(() => []),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
).flat();
|
).flat();
|
||||||
const cards: MessageBlock[] = option.apps.flatMap((slug) => {
|
const cards = option.apps.flatMap((slug) => {
|
||||||
const entry = catalog.find(
|
const entry = catalog.find(
|
||||||
(item) =>
|
(item) =>
|
||||||
item.slug.toLowerCase() === slug.toLowerCase() ||
|
item.slug.toLowerCase() === slug.toLowerCase() ||
|
||||||
@ -316,27 +359,7 @@ export async function chooseFocus(
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
if (!cards.length) {
|
return cards.length > 0 ? [cards] : [];
|
||||||
await post(deps, target, [{ kind: "text", text: "What would you like to work on first?" }]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const cardNames = cards
|
|
||||||
.map((card) => (card.kind === "app_connect" ? card.name : ""))
|
|
||||||
.filter(Boolean);
|
|
||||||
const named = `${cardNames.slice(0, -1).join(", ")}${cardNames.length > 1 ? ", and " : ""}${cardNames.at(-1)}`;
|
|
||||||
await post(deps, target, [
|
|
||||||
{
|
|
||||||
kind: "text",
|
|
||||||
text: `${named} are a good place to start. Connect them here and I’ll use what you already have.`,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
await post(deps, target, cards);
|
|
||||||
await post(deps, target, [
|
|
||||||
{
|
|
||||||
kind: "text",
|
|
||||||
text: `Hit those ${cards.length === 1 ? "one" : cards.length === 2 ? "two" : "three"} and I’ll start pulling the picture.`,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function markAppConnected(
|
export async function markAppConnected(
|
||||||
@ -376,7 +399,3 @@ export async function markAppConnected(
|
|||||||
await updateBlocks(deps, target, message.id, next);
|
await updateBlocks(deps, target, message.id, next);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function capitalize(value: string): string {
|
|
||||||
return value.length > 0 ? (value[0] ?? "").toUpperCase() + value.slice(1) : value;
|
|
||||||
}
|
|
||||||
|
|||||||
43
apps/api/src/project-directories.test.ts
Normal file
43
apps/api/src/project-directories.test.ts
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import fs from "node:fs/promises";
|
||||||
|
import os from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { listHostDirectories } from "./project-directories.js";
|
||||||
|
|
||||||
|
describe("listHostDirectories", () => {
|
||||||
|
it("lists directories in the user home or projects directory by default", async () => {
|
||||||
|
const listing = await listHostDirectories();
|
||||||
|
expect(listing.currentPath).toBeTruthy();
|
||||||
|
expect(Array.isArray(listing.directories)).toBe(true);
|
||||||
|
expect(Array.isArray(listing.commonPaths)).toBe(true);
|
||||||
|
expect(listing.commonPaths.some((p) => p.name.includes("Home") || p.name === "Projects")).toBe(
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("lists specific existing directory and excludes hidden folders and node_modules", async () => {
|
||||||
|
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "evoo-test-dirs-"));
|
||||||
|
try {
|
||||||
|
await fs.mkdir(path.join(tmpDir, "visible-folder-a"));
|
||||||
|
await fs.mkdir(path.join(tmpDir, "visible-folder-b"));
|
||||||
|
await fs.mkdir(path.join(tmpDir, ".hidden-folder"));
|
||||||
|
await fs.mkdir(path.join(tmpDir, "node_modules"));
|
||||||
|
await fs.writeFile(path.join(tmpDir, "some-file.txt"), "hello");
|
||||||
|
|
||||||
|
const listing = await listHostDirectories(tmpDir);
|
||||||
|
expect(listing.currentPath).toBe(await fs.realpath(tmpDir));
|
||||||
|
expect(listing.directories.map((d) => d.name)).toEqual([
|
||||||
|
"visible-folder-a",
|
||||||
|
"visible-folder-b",
|
||||||
|
]);
|
||||||
|
} finally {
|
||||||
|
await fs.rm(tmpDir, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("falls back safely when given an invalid path", async () => {
|
||||||
|
const listing = await listHostDirectories("/nonexistent-path-12345/abcdef");
|
||||||
|
expect(listing.currentPath).toBe(os.homedir());
|
||||||
|
expect(Array.isArray(listing.directories)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
95
apps/api/src/project-directories.ts
Normal file
95
apps/api/src/project-directories.ts
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
import fs from "node:fs/promises";
|
||||||
|
import os from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
export interface HostDirectoryListing {
|
||||||
|
currentPath: string;
|
||||||
|
parentPath: string | null;
|
||||||
|
commonPaths: Array<{ name: string; path: string }>;
|
||||||
|
directories: Array<{ name: string; path: string }>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function listHostDirectories(targetPath?: string): Promise<HostDirectoryListing> {
|
||||||
|
const home = os.homedir();
|
||||||
|
const raw = targetPath?.trim();
|
||||||
|
let requestedPath = home;
|
||||||
|
|
||||||
|
if (raw) {
|
||||||
|
if (raw === "~") {
|
||||||
|
requestedPath = home;
|
||||||
|
} else if (raw.startsWith("~/")) {
|
||||||
|
requestedPath = path.join(home, raw.slice(2));
|
||||||
|
} else {
|
||||||
|
requestedPath = path.resolve(raw);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Default: prioritize ~/Projects if it exists, otherwise home directory
|
||||||
|
const projectsCandidate = path.join(home, "Projects");
|
||||||
|
try {
|
||||||
|
const stat = await fs.stat(projectsCandidate);
|
||||||
|
if (stat.isDirectory()) {
|
||||||
|
requestedPath = projectsCandidate;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
requestedPath = home;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let currentPath: string;
|
||||||
|
try {
|
||||||
|
currentPath = await fs.realpath(requestedPath);
|
||||||
|
const stat = await fs.stat(currentPath);
|
||||||
|
if (!stat.isDirectory()) {
|
||||||
|
currentPath = home;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
currentPath = home;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsed = path.parse(currentPath);
|
||||||
|
const parentPath = currentPath === parsed.root ? null : path.dirname(currentPath);
|
||||||
|
|
||||||
|
const commonCandidates = [
|
||||||
|
{ name: "Projects", path: path.join(home, "Projects") },
|
||||||
|
{ name: "Home (~)", path: home },
|
||||||
|
{ name: "Desktop", path: path.join(home, "Desktop") },
|
||||||
|
{ name: "Documents", path: path.join(home, "Documents") },
|
||||||
|
{ name: "Downloads", path: path.join(home, "Downloads") },
|
||||||
|
];
|
||||||
|
|
||||||
|
const commonPaths: Array<{ name: string; path: string }> = [];
|
||||||
|
for (const candidate of commonCandidates) {
|
||||||
|
try {
|
||||||
|
const s = await fs.stat(candidate.path);
|
||||||
|
if (s.isDirectory()) {
|
||||||
|
commonPaths.push(candidate);
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// not present on host
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const directories: Array<{ name: string; path: string }> = [];
|
||||||
|
try {
|
||||||
|
const entries = await fs.readdir(currentPath, { withFileTypes: true });
|
||||||
|
for (const entry of entries) {
|
||||||
|
if (!entry.isDirectory()) continue;
|
||||||
|
// Skip hidden folders (.git, .Trash, etc.) and node_modules
|
||||||
|
if (entry.name.startsWith(".") || entry.name === "node_modules") continue;
|
||||||
|
directories.push({
|
||||||
|
name: entry.name,
|
||||||
|
path: path.join(currentPath, entry.name),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
directories.sort((a, b) => a.name.localeCompare(b.name, undefined, { sensitivity: "base" }));
|
||||||
|
} catch {
|
||||||
|
// Unreadable directory returns empty list
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
currentPath,
|
||||||
|
parentPath,
|
||||||
|
commonPaths,
|
||||||
|
directories,
|
||||||
|
};
|
||||||
|
}
|
||||||
114
apps/api/src/project-github.test.ts
Normal file
114
apps/api/src/project-github.test.ts
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
import { listUserGithubRepos } from "./project-github.js";
|
||||||
|
|
||||||
|
vi.mock("node:child_process", () => ({
|
||||||
|
execFile: vi.fn((_cmd, _args, callback) => {
|
||||||
|
callback(new Error("gh not installed"));
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe("listUserGithubRepos", () => {
|
||||||
|
it("queries repositories and filters by query", async () => {
|
||||||
|
const mockPrisma = {
|
||||||
|
capabilityInstall: {
|
||||||
|
findMany: vi.fn().mockResolvedValue([
|
||||||
|
{
|
||||||
|
id: "cap-1",
|
||||||
|
kind: "mcp",
|
||||||
|
config: { preset: "github" },
|
||||||
|
secretId: "sec-1",
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
secret: {
|
||||||
|
findUnique: vi.fn().mockResolvedValue({
|
||||||
|
id: "sec-1",
|
||||||
|
ciphertext: "fake-ciphertext",
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockSecrets = {
|
||||||
|
load: vi.fn().mockReturnValue("fake-token"),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Mock fetch for GitHub API
|
||||||
|
const originalFetch = globalThis.fetch;
|
||||||
|
globalThis.fetch = vi.fn().mockImplementation(async (url: string) => {
|
||||||
|
if (url.includes("/user/repos")) {
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
json: async () => [
|
||||||
|
{
|
||||||
|
name: "evOo-local",
|
||||||
|
full_name: "testorg/evOo-local",
|
||||||
|
html_url: "https://github.com/testorg/evOo-local",
|
||||||
|
description: "AI Operating System",
|
||||||
|
private: false,
|
||||||
|
updated_at: "2026-10-12T00:00:00Z",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "secret-vault",
|
||||||
|
full_name: "testorg/secret-vault",
|
||||||
|
html_url: "https://github.com/testorg/secret-vault",
|
||||||
|
description: "Private keys",
|
||||||
|
private: true,
|
||||||
|
updated_at: "2026-10-11T00:00:00Z",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
} as unknown as Response;
|
||||||
|
}
|
||||||
|
if (url.includes("/user")) {
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
json: async () => ({ login: "testuser" }),
|
||||||
|
} as unknown as Response;
|
||||||
|
}
|
||||||
|
return { ok: false } as unknown as Response;
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const all = await listUserGithubRepos(mockPrisma as any, undefined, mockSecrets);
|
||||||
|
expect(all.connected).toBe(true);
|
||||||
|
expect(all.username).toBe("testuser");
|
||||||
|
expect(all.repos).toHaveLength(2);
|
||||||
|
expect(all.repos[0].name).toBe("evOo-local");
|
||||||
|
|
||||||
|
const filtered = await listUserGithubRepos(
|
||||||
|
mockPrisma as any,
|
||||||
|
{ query: "vault" },
|
||||||
|
mockSecrets,
|
||||||
|
);
|
||||||
|
expect(filtered.repos).toHaveLength(1);
|
||||||
|
expect(filtered.repos[0].name).toBe("secret-vault");
|
||||||
|
expect(filtered.repos[0].isPrivate).toBe(true);
|
||||||
|
} finally {
|
||||||
|
globalThis.fetch = originalFetch;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles unauthenticated state gracefully", async () => {
|
||||||
|
const mockPrisma = {
|
||||||
|
capabilityInstall: {
|
||||||
|
findMany: vi.fn().mockResolvedValue([]),
|
||||||
|
},
|
||||||
|
secret: {
|
||||||
|
findUnique: vi.fn().mockResolvedValue(null),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const origToken = process.env.GITHUB_TOKEN;
|
||||||
|
const origGhToken = process.env.GH_TOKEN;
|
||||||
|
delete process.env.GITHUB_TOKEN;
|
||||||
|
delete process.env.GH_TOKEN;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await listUserGithubRepos(mockPrisma as any, { query: "test" });
|
||||||
|
expect(res.connected !== undefined).toBe(true);
|
||||||
|
expect(Array.isArray(res.repos)).toBe(true);
|
||||||
|
} finally {
|
||||||
|
if (origToken) process.env.GITHUB_TOKEN = origToken;
|
||||||
|
if (origGhToken) process.env.GH_TOKEN = origGhToken;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
200
apps/api/src/project-github.ts
Normal file
200
apps/api/src/project-github.ts
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
import { execFile } from "node:child_process";
|
||||||
|
import { promisify } from "node:util";
|
||||||
|
import type { EncryptedSecretStore } from "@rakazo/adapters";
|
||||||
|
import type { PrismaClient } from "@rakazo/db";
|
||||||
|
|
||||||
|
const execFileAsync = promisify(execFile);
|
||||||
|
|
||||||
|
export interface GithubRepoItem {
|
||||||
|
name: string;
|
||||||
|
fullName: string;
|
||||||
|
url: string;
|
||||||
|
description?: string | null;
|
||||||
|
isPrivate: boolean;
|
||||||
|
updatedAt?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ListGithubReposOutput {
|
||||||
|
connected: boolean;
|
||||||
|
username?: string;
|
||||||
|
repos: GithubRepoItem[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function listUserGithubRepos(
|
||||||
|
prisma: Pick<PrismaClient, "capabilityInstall" | "secret">,
|
||||||
|
input?: { query?: string; limit?: number },
|
||||||
|
secrets?: Pick<EncryptedSecretStore, "load">,
|
||||||
|
): Promise<ListGithubReposOutput> {
|
||||||
|
let token: string | null = null;
|
||||||
|
|
||||||
|
// 1. Check CapabilityInstall in database if secret store is provided
|
||||||
|
if (secrets) {
|
||||||
|
try {
|
||||||
|
const installs = await prisma.capabilityInstall.findMany({
|
||||||
|
where: { kind: "mcp" },
|
||||||
|
});
|
||||||
|
for (const inst of installs) {
|
||||||
|
const cfg = inst.config as Record<string, unknown> | null;
|
||||||
|
if (cfg?.preset === "github" && inst.secretId) {
|
||||||
|
const secret = await prisma.secret.findUnique({
|
||||||
|
where: { id: inst.secretId },
|
||||||
|
select: { id: true, ciphertext: true },
|
||||||
|
});
|
||||||
|
if (secret) {
|
||||||
|
try {
|
||||||
|
const loaded = secrets.load(secret.ciphertext, secret.id);
|
||||||
|
if (loaded?.trim()) {
|
||||||
|
token = loaded.trim();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// decryption error fallback
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Database lookup error fallback
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Check environment
|
||||||
|
if (!token) {
|
||||||
|
token = process.env.GITHUB_TOKEN?.trim() || process.env.GH_TOKEN?.trim() || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Check local gh CLI auth token
|
||||||
|
if (!token) {
|
||||||
|
try {
|
||||||
|
const { stdout } = await execFileAsync("gh", ["auth", "token"], { timeout: 3000 });
|
||||||
|
if (stdout.trim()) {
|
||||||
|
token = stdout.trim();
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// gh CLI not available or not logged in
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If token is available, query GitHub REST API
|
||||||
|
if (token) {
|
||||||
|
try {
|
||||||
|
let username: string | undefined;
|
||||||
|
const userRes = await fetch("https://api.github.com/user", {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
"User-Agent": "evOo-App",
|
||||||
|
Accept: "application/vnd.github+json",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (userRes.ok) {
|
||||||
|
const userData = (await userRes.json()) as { login?: string };
|
||||||
|
username = userData.login;
|
||||||
|
}
|
||||||
|
|
||||||
|
const reposRes = await fetch(
|
||||||
|
"https://api.github.com/user/repos?sort=pushed&per_page=100&type=all",
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
"User-Agent": "evOo-App",
|
||||||
|
Accept: "application/vnd.github+json",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (reposRes.ok) {
|
||||||
|
const rawRepos = (await reposRes.json()) as Array<{
|
||||||
|
name: string;
|
||||||
|
full_name: string;
|
||||||
|
html_url: string;
|
||||||
|
description?: string | null;
|
||||||
|
private?: boolean;
|
||||||
|
pushed_at?: string;
|
||||||
|
updated_at?: string;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
let items: GithubRepoItem[] = rawRepos.map((r) => ({
|
||||||
|
name: r.name,
|
||||||
|
fullName: r.full_name,
|
||||||
|
url: r.html_url,
|
||||||
|
description: r.description ?? null,
|
||||||
|
isPrivate: Boolean(r.private),
|
||||||
|
updatedAt: r.pushed_at || r.updated_at,
|
||||||
|
}));
|
||||||
|
|
||||||
|
if (input?.query?.trim()) {
|
||||||
|
const q = input.query.trim().toLowerCase();
|
||||||
|
items = items.filter(
|
||||||
|
(r) =>
|
||||||
|
r.name.toLowerCase().includes(q) ||
|
||||||
|
r.fullName.toLowerCase().includes(q) ||
|
||||||
|
(r.description && r.description.toLowerCase().includes(q)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const limit = input?.limit ?? 50;
|
||||||
|
return {
|
||||||
|
connected: true,
|
||||||
|
username,
|
||||||
|
repos: items.slice(0, limit),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Fall through to gh repo list fallback
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: try gh repo list directly
|
||||||
|
try {
|
||||||
|
const { stdout } = await execFileAsync(
|
||||||
|
"gh",
|
||||||
|
[
|
||||||
|
"repo",
|
||||||
|
"list",
|
||||||
|
"--limit",
|
||||||
|
"100",
|
||||||
|
"--json",
|
||||||
|
"nameWithOwner,name,url,description,isPrivate,pushedAt",
|
||||||
|
],
|
||||||
|
{ timeout: 5000 },
|
||||||
|
);
|
||||||
|
const parsed = JSON.parse(stdout) as Array<{
|
||||||
|
nameWithOwner: string;
|
||||||
|
name: string;
|
||||||
|
url: string;
|
||||||
|
description?: string;
|
||||||
|
isPrivate?: boolean;
|
||||||
|
pushedAt?: string;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
let items: GithubRepoItem[] = parsed.map((r) => ({
|
||||||
|
name: r.name,
|
||||||
|
fullName: r.nameWithOwner,
|
||||||
|
url: r.url,
|
||||||
|
description: r.description || null,
|
||||||
|
isPrivate: Boolean(r.isPrivate),
|
||||||
|
updatedAt: r.pushedAt,
|
||||||
|
}));
|
||||||
|
|
||||||
|
if (input?.query?.trim()) {
|
||||||
|
const q = input.query.trim().toLowerCase();
|
||||||
|
items = items.filter(
|
||||||
|
(r) =>
|
||||||
|
r.name.toLowerCase().includes(q) ||
|
||||||
|
r.fullName.toLowerCase().includes(q) ||
|
||||||
|
(r.description && r.description.toLowerCase().includes(q)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const limit = input?.limit ?? 50;
|
||||||
|
return {
|
||||||
|
connected: true,
|
||||||
|
repos: items.slice(0, limit),
|
||||||
|
};
|
||||||
|
} catch {
|
||||||
|
return {
|
||||||
|
connected: false,
|
||||||
|
repos: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -55,7 +55,11 @@ export function requestBodyLimit(maxSize: number): MiddlewareHandler {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Install limits only on framework-parsed JSON surfaces with known payload contracts. */
|
/**
|
||||||
|
* Install limits only on framework-parsed JSON surfaces with known payload contracts. Streaming
|
||||||
|
* artifact uploads (`POST /api/artifacts`) are not buffered: the route enforces the per-kind cap
|
||||||
|
* from Content-Length and again while writing.
|
||||||
|
*/
|
||||||
export function mountApiRequestBodyLimits(app: Hono): void {
|
export function mountApiRequestBodyLimits(app: Hono): void {
|
||||||
app.use("/api/auth/*", requestBodyLimit(MAX_AUTH_REQUEST_BYTES));
|
app.use("/api/auth/*", requestBodyLimit(MAX_AUTH_REQUEST_BYTES));
|
||||||
app.use(`${LOCAL_SETTINGS_RPC}/*`, requestBodyLimit(MAX_RPC_REQUEST_BYTES));
|
app.use(`${LOCAL_SETTINGS_RPC}/*`, requestBodyLimit(MAX_RPC_REQUEST_BYTES));
|
||||||
|
|||||||
@ -224,7 +224,12 @@ describe("model setup gate", () => {
|
|||||||
|
|
||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
await expect(response.json()).resolves.toEqual({
|
await expect(response.json()).resolves.toEqual({
|
||||||
json: expect.objectContaining({ needsModel: false }),
|
json: expect.objectContaining({
|
||||||
|
needsModel: false,
|
||||||
|
modelsManaged: true,
|
||||||
|
defaultProvider: "openrouter",
|
||||||
|
defaultModel: "test-model",
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -238,7 +243,13 @@ describe("model setup gate", () => {
|
|||||||
|
|
||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
await expect(response.json()).resolves.toEqual({
|
await expect(response.json()).resolves.toEqual({
|
||||||
json: expect.objectContaining({ needsModel: true }),
|
// The connect flow opens on the recommendation, not the keyless deployment default.
|
||||||
|
json: expect.objectContaining({
|
||||||
|
needsModel: true,
|
||||||
|
modelsManaged: false,
|
||||||
|
defaultProvider: "anthropic",
|
||||||
|
defaultModel: "claude-opus-5-5",
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -627,7 +638,11 @@ describe("computer screen url", () => {
|
|||||||
controlRunId: null,
|
controlRunId: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const callScreenUrl = async (connectScreen: () => Promise<unknown>, updateMany = vi.fn()) => {
|
const callScreenUrl = async (
|
||||||
|
connectScreen: () => Promise<unknown>,
|
||||||
|
updateMany = vi.fn(),
|
||||||
|
webOrigin?: string,
|
||||||
|
) => {
|
||||||
const prisma = {
|
const prisma = {
|
||||||
bot: {
|
bot: {
|
||||||
findFirst: vi.fn().mockResolvedValue({
|
findFirst: vi.fn().mockResolvedValue({
|
||||||
@ -660,7 +675,7 @@ describe("computer screen url", () => {
|
|||||||
headers: { "content-type": "application/json" },
|
headers: { "content-type": "application/json" },
|
||||||
body: JSON.stringify({ json: { botId: "bot-1" } }),
|
body: JSON.stringify({ json: { botId: "bot-1" } }),
|
||||||
}),
|
}),
|
||||||
{ prefix: "/rpc", context: { actor } },
|
{ prefix: "/rpc", context: { actor, webOrigin } },
|
||||||
);
|
);
|
||||||
return { response, updateMany };
|
return { response, updateMany };
|
||||||
};
|
};
|
||||||
@ -685,6 +700,16 @@ describe("computer screen url", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("points the screen at the browser's own LAN origin", async () => {
|
||||||
|
const { response } = await callScreenUrl(
|
||||||
|
async () => ({ url: "https://screen.example/vnc.html?token=fake-token" }),
|
||||||
|
vi.fn(),
|
||||||
|
"http://192.168.0.20:5173",
|
||||||
|
);
|
||||||
|
const { json } = await response.json();
|
||||||
|
expect(new URL(json.url).origin).toBe("http://192.168.0.20:5173");
|
||||||
|
});
|
||||||
|
|
||||||
it("returns desktop provider screen URLs without sealing them", async () => {
|
it("returns desktop provider screen URLs without sealing them", async () => {
|
||||||
const { response } = await callScreenUrl(async () => ({
|
const { response } = await callScreenUrl(async () => ({
|
||||||
url: "desktop://screen/computer-1",
|
url: "desktop://screen/computer-1",
|
||||||
@ -977,3 +1002,52 @@ describe("interrupted computer reservation release", () => {
|
|||||||
expect(order).toEqual(["operation", "computer"]);
|
expect(order).toEqual(["operation", "computer"]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("mcp oauth redirect", () => {
|
||||||
|
const begin = async (redirectUri: string, webOrigin?: string) => {
|
||||||
|
const start = vi.fn().mockResolvedValue({ status: "already_connected" });
|
||||||
|
const deps = {
|
||||||
|
prisma: {},
|
||||||
|
env: { webOrigin: "http://127.0.0.1:5173" },
|
||||||
|
mcpOAuth: { begin: start },
|
||||||
|
} as unknown as RouterDeps;
|
||||||
|
const { response } = await new RPCHandler(createRouter(deps)).handle(
|
||||||
|
new Request("http://127.0.0.1/rpc/mcp/oauth/begin", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
body: JSON.stringify({ json: { serverId: "server-1", redirectUri } }),
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
prefix: "/rpc",
|
||||||
|
context: {
|
||||||
|
actor: {
|
||||||
|
userId: "user",
|
||||||
|
spaceId: "space",
|
||||||
|
email: "user@rakazo.test",
|
||||||
|
isDeploymentOwner: true,
|
||||||
|
},
|
||||||
|
webOrigin,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
return { status: response.status, start };
|
||||||
|
};
|
||||||
|
|
||||||
|
it("returns to the LAN origin the browser opened", async () => {
|
||||||
|
const { status, start } = await begin(
|
||||||
|
"http://192.168.0.20:5173/mcp/oauth/callback",
|
||||||
|
"http://192.168.0.20:5173",
|
||||||
|
);
|
||||||
|
expect(status).toBe(200);
|
||||||
|
expect(start).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects a redirect to any other origin", async () => {
|
||||||
|
const { status, start } = await begin(
|
||||||
|
"https://attacker.example/mcp/oauth/callback",
|
||||||
|
"http://192.168.0.20:5173",
|
||||||
|
);
|
||||||
|
expect(status).toBe(400);
|
||||||
|
expect(start).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -20,6 +20,16 @@ describe("run activity copy", () => {
|
|||||||
).toBe("Maya asked: Please check the release workflow.");
|
).toBe("Maya asked: Please check the release workflow.");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("names an autopilot step by the user's order", () => {
|
||||||
|
expect(
|
||||||
|
activityPromptSnippet({
|
||||||
|
trigger: "autopilot",
|
||||||
|
prompt:
|
||||||
|
"Autopilot: continue the user's order on your own.\nOrder: Tidy the tickets\nUntil: done",
|
||||||
|
}),
|
||||||
|
).toBe("Tidy the tickets");
|
||||||
|
});
|
||||||
|
|
||||||
it("names a delegated task by its title instead of its wake prompt", () => {
|
it("names a delegated task by its title instead of its wake prompt", () => {
|
||||||
expect(
|
expect(
|
||||||
activityPromptSnippet({
|
activityPromptSnippet({
|
||||||
|
|||||||
@ -26,6 +26,10 @@ export function activityPromptSnippet(
|
|||||||
const task = parsed.success ? taskBlockOf(parsed.data) : undefined;
|
const task = parsed.success ? taskBlockOf(parsed.data) : undefined;
|
||||||
return promptSnippet(task?.title ?? input.prompt, max);
|
return promptSnippet(task?.title ?? input.prompt, max);
|
||||||
}
|
}
|
||||||
|
if (input.trigger === "autopilot") {
|
||||||
|
// The continuation prompt is for the model; people know the run by its order.
|
||||||
|
return promptSnippet(/^Order: (.*)$/m.exec(input.prompt)?.[1] ?? input.prompt, max);
|
||||||
|
}
|
||||||
if (input.trigger !== "bot_message") return promptSnippet(input.prompt, max);
|
if (input.trigger !== "bot_message") return promptSnippet(input.prompt, max);
|
||||||
const parsed = MessageBlock.array().safeParse(input.sourceBlocks);
|
const parsed = MessageBlock.array().safeParse(input.sourceBlocks);
|
||||||
const message = parsed.success ? botMessageContext(parsed.data) : undefined;
|
const message = parsed.success ? botMessageContext(parsed.data) : undefined;
|
||||||
@ -45,15 +49,19 @@ export function activityNotificationsEnabled(
|
|||||||
return groupId !== null || notifyOnFinish;
|
return groupId !== null || notifyOnFinish;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function listSpaceRuns(
|
/**
|
||||||
|
* The user's runs across every space they belong to, so a chat waiting on them in another
|
||||||
|
* project still shows up wherever they look.
|
||||||
|
*/
|
||||||
|
export async function listUserRuns(
|
||||||
prisma: PrismaClient,
|
prisma: PrismaClient,
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
filter: "active" | "recent",
|
filter: "active" | "recent",
|
||||||
): Promise<RunActivityRow[]> {
|
): Promise<RunActivityRow[]> {
|
||||||
const rows = await prisma.run.findMany({
|
const rows = await prisma.run.findMany({
|
||||||
where: {
|
where: {
|
||||||
spaceId: actor.spaceId,
|
|
||||||
userId: actor.userId,
|
userId: actor.userId,
|
||||||
|
space: { memberships: { some: { userId: actor.userId } } },
|
||||||
bot: { archivedAt: null },
|
bot: { archivedAt: null },
|
||||||
...(filter === "active"
|
...(filter === "active"
|
||||||
? { status: { in: [...ACTIVE_RUN_STATUSES] } }
|
? { status: { in: [...ACTIVE_RUN_STATUSES] } }
|
||||||
@ -84,6 +92,7 @@ export async function listSpaceRuns(
|
|||||||
groupId: row.thread.groupId,
|
groupId: row.thread.groupId,
|
||||||
groupName: row.thread.group?.name ?? null,
|
groupName: row.thread.group?.name ?? null,
|
||||||
threadId: row.threadId,
|
threadId: row.threadId,
|
||||||
|
spaceId: row.spaceId,
|
||||||
status: row.status as RunActivityRow["status"],
|
status: row.status as RunActivityRow["status"],
|
||||||
trigger: row.trigger as RunActivityRow["trigger"],
|
trigger: row.trigger as RunActivityRow["trigger"],
|
||||||
notificationsEnabled: activityNotificationsEnabled(row.thread.groupId, row.bot.notifyOnFinish),
|
notificationsEnabled: activityNotificationsEnabled(row.thread.groupId, row.bot.notifyOnFinish),
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import type { Actor, MessageBlock, SearchHit } from "@rakazo/contracts";
|
import type { Actor, MessageBlock, SearchHit } from "@rakazo/contracts";
|
||||||
import { extractLinksFromText, matchesSearchQuery, snippetAroundMatch } from "@rakazo/core";
|
import { extractLinksFromText, matchesSearchQuery, snippetAroundMatch } from "@rakazo/core";
|
||||||
import { Prisma, type PrismaClient } from "@rakazo/db";
|
import type { PrismaClient } from "@rakazo/db";
|
||||||
|
import { Prisma } from "@rakazo/db";
|
||||||
|
|
||||||
const SEARCH_LIMIT = 25;
|
const SEARCH_LIMIT = 25;
|
||||||
/** Cap name matches so content hits (messages/files/links/routines) keep most of the budget. */
|
/** Cap name matches so content hits (messages/files/links/routines) keep most of the budget. */
|
||||||
@ -64,19 +65,65 @@ export async function querySpaceSearch(
|
|||||||
bucket.push(hit);
|
bucket.push(hit);
|
||||||
}
|
}
|
||||||
|
|
||||||
const bots = await prisma.bot.findMany({
|
const [bots, groups, artifacts, groupArtifacts, routines] = await Promise.all([
|
||||||
where: {
|
prisma.bot.findMany({
|
||||||
spaceId: actor.spaceId,
|
where: {
|
||||||
userId: actor.userId,
|
spaceId: actor.spaceId,
|
||||||
archivedAt: null,
|
userId: actor.userId,
|
||||||
OR: [
|
archivedAt: null,
|
||||||
{ name: { contains: query, mode: "insensitive" } },
|
OR: [
|
||||||
{ title: { contains: query, mode: "insensitive" } },
|
{ name: { contains: query, mode: "insensitive" } },
|
||||||
{ description: { contains: query, mode: "insensitive" } },
|
{ title: { contains: query, mode: "insensitive" } },
|
||||||
],
|
{ description: { contains: query, mode: "insensitive" } },
|
||||||
},
|
],
|
||||||
take: CONVERSATION_HIT_LIMIT,
|
},
|
||||||
});
|
take: CONVERSATION_HIT_LIMIT,
|
||||||
|
}),
|
||||||
|
prisma.chatGroup.findMany({
|
||||||
|
where: {
|
||||||
|
spaceId: actor.spaceId,
|
||||||
|
userId: actor.userId,
|
||||||
|
name: { contains: query, mode: "insensitive" },
|
||||||
|
},
|
||||||
|
take: CONVERSATION_HIT_LIMIT,
|
||||||
|
}),
|
||||||
|
prisma.artifact.findMany({
|
||||||
|
where: {
|
||||||
|
spaceId: actor.spaceId,
|
||||||
|
userId: actor.userId,
|
||||||
|
groupId: null,
|
||||||
|
botId: { not: null },
|
||||||
|
name: { contains: query, mode: "insensitive" },
|
||||||
|
bot: { archivedAt: null },
|
||||||
|
},
|
||||||
|
include: { bot: { select: { name: true } } },
|
||||||
|
take: SEARCH_LIMIT,
|
||||||
|
}),
|
||||||
|
prisma.artifact.findMany({
|
||||||
|
where: {
|
||||||
|
spaceId: actor.spaceId,
|
||||||
|
userId: actor.userId,
|
||||||
|
groupId: { not: null },
|
||||||
|
name: { contains: query, mode: "insensitive" },
|
||||||
|
},
|
||||||
|
include: { group: { select: { name: true } } },
|
||||||
|
take: SEARCH_LIMIT,
|
||||||
|
}),
|
||||||
|
prisma.routine.findMany({
|
||||||
|
where: {
|
||||||
|
spaceId: actor.spaceId,
|
||||||
|
userId: actor.userId,
|
||||||
|
OR: [
|
||||||
|
{ name: { contains: query, mode: "insensitive" } },
|
||||||
|
{ prompt: { contains: query, mode: "insensitive" } },
|
||||||
|
],
|
||||||
|
bot: { archivedAt: null },
|
||||||
|
},
|
||||||
|
include: { bot: { select: { name: true } } },
|
||||||
|
take: SEARCH_LIMIT,
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
for (const bot of bots) {
|
for (const bot of bots) {
|
||||||
pushInto(
|
pushInto(
|
||||||
conversationHits,
|
conversationHits,
|
||||||
@ -91,14 +138,6 @@ export async function querySpaceSearch(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const groups = await prisma.chatGroup.findMany({
|
|
||||||
where: {
|
|
||||||
spaceId: actor.spaceId,
|
|
||||||
userId: actor.userId,
|
|
||||||
name: { contains: query, mode: "insensitive" },
|
|
||||||
},
|
|
||||||
take: CONVERSATION_HIT_LIMIT,
|
|
||||||
});
|
|
||||||
for (const group of groups) {
|
for (const group of groups) {
|
||||||
pushInto(
|
pushInto(
|
||||||
conversationHits,
|
conversationHits,
|
||||||
@ -114,27 +153,72 @@ export async function querySpaceSearch(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const contentBudget = Math.max(0, SEARCH_LIMIT - conversationHits.length);
|
const contentBudget = Math.max(0, SEARCH_LIMIT - conversationHits.length);
|
||||||
|
const pattern = `%${query}%`;
|
||||||
|
|
||||||
|
const [botArtifactMessages, groupArtifactMessages, messageRows, groupMessageRows] =
|
||||||
|
await Promise.all([
|
||||||
|
findArtifactMessages(
|
||||||
|
prisma,
|
||||||
|
actor,
|
||||||
|
artifacts.flatMap((artifact) =>
|
||||||
|
artifact.botId && artifact.bot ? [{ id: artifact.id, targetId: artifact.botId }] : [],
|
||||||
|
),
|
||||||
|
"bot",
|
||||||
|
),
|
||||||
|
findArtifactMessages(
|
||||||
|
prisma,
|
||||||
|
actor,
|
||||||
|
groupArtifacts.flatMap((artifact) =>
|
||||||
|
artifact.groupId && artifact.group
|
||||||
|
? [{ id: artifact.id, targetId: artifact.groupId }]
|
||||||
|
: [],
|
||||||
|
),
|
||||||
|
"group",
|
||||||
|
),
|
||||||
|
prisma.$queryRaw<
|
||||||
|
Array<{
|
||||||
|
id: string;
|
||||||
|
threadId: string;
|
||||||
|
seq: number;
|
||||||
|
blocks: Prisma.JsonValue;
|
||||||
|
botId: string;
|
||||||
|
botName: string;
|
||||||
|
}>
|
||||||
|
>`
|
||||||
|
SELECT m.id, m."threadId", m.seq, m.blocks, b.id AS "botId", b.name AS "botName"
|
||||||
|
FROM messages m
|
||||||
|
INNER JOIN threads t ON t.id = m."threadId"
|
||||||
|
INNER JOIN bots b ON b.id = t."botId"
|
||||||
|
WHERE t."spaceId" = ${actor.spaceId}
|
||||||
|
AND t."userId" = ${actor.userId}
|
||||||
|
AND b."archivedAt" IS NULL
|
||||||
|
AND m.blocks::text ILIKE ${pattern}
|
||||||
|
ORDER BY m."createdAt" DESC
|
||||||
|
LIMIT ${SEARCH_LIMIT}
|
||||||
|
`,
|
||||||
|
prisma.$queryRaw<
|
||||||
|
Array<{
|
||||||
|
id: string;
|
||||||
|
threadId: string;
|
||||||
|
seq: number;
|
||||||
|
blocks: Prisma.JsonValue;
|
||||||
|
groupId: string;
|
||||||
|
groupName: string;
|
||||||
|
}>
|
||||||
|
>`
|
||||||
|
SELECT m.id, m."threadId", m.seq, m.blocks, g.id AS "groupId", g.name AS "groupName"
|
||||||
|
FROM messages m
|
||||||
|
INNER JOIN threads t ON t.id = m."threadId"
|
||||||
|
INNER JOIN chat_groups g ON g.id = t."groupId"
|
||||||
|
WHERE t."spaceId" = ${actor.spaceId}
|
||||||
|
AND t."userId" = ${actor.userId}
|
||||||
|
AND t."groupId" IS NOT NULL
|
||||||
|
AND m.blocks::text ILIKE ${pattern}
|
||||||
|
ORDER BY m."createdAt" DESC
|
||||||
|
LIMIT ${SEARCH_LIMIT}
|
||||||
|
`,
|
||||||
|
]);
|
||||||
|
|
||||||
const artifacts = await prisma.artifact.findMany({
|
|
||||||
where: {
|
|
||||||
spaceId: actor.spaceId,
|
|
||||||
userId: actor.userId,
|
|
||||||
groupId: null,
|
|
||||||
botId: { not: null },
|
|
||||||
name: { contains: query, mode: "insensitive" },
|
|
||||||
bot: { archivedAt: null },
|
|
||||||
},
|
|
||||||
include: { bot: { select: { name: true } } },
|
|
||||||
take: SEARCH_LIMIT,
|
|
||||||
});
|
|
||||||
const botArtifactMessages = await findArtifactMessages(
|
|
||||||
prisma,
|
|
||||||
actor,
|
|
||||||
artifacts.flatMap((artifact) =>
|
|
||||||
artifact.botId && artifact.bot ? [{ id: artifact.id, targetId: artifact.botId }] : [],
|
|
||||||
),
|
|
||||||
"bot",
|
|
||||||
);
|
|
||||||
for (const artifact of artifacts) {
|
for (const artifact of artifacts) {
|
||||||
if (!artifact.botId || !artifact.bot) continue;
|
if (!artifact.botId || !artifact.bot) continue;
|
||||||
const message = botArtifactMessages.get(artifact.id);
|
const message = botArtifactMessages.get(artifact.id);
|
||||||
@ -155,24 +239,6 @@ export async function querySpaceSearch(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const groupArtifacts = await prisma.artifact.findMany({
|
|
||||||
where: {
|
|
||||||
spaceId: actor.spaceId,
|
|
||||||
userId: actor.userId,
|
|
||||||
groupId: { not: null },
|
|
||||||
name: { contains: query, mode: "insensitive" },
|
|
||||||
},
|
|
||||||
include: { group: { select: { name: true } } },
|
|
||||||
take: SEARCH_LIMIT,
|
|
||||||
});
|
|
||||||
const groupArtifactMessages = await findArtifactMessages(
|
|
||||||
prisma,
|
|
||||||
actor,
|
|
||||||
groupArtifacts.flatMap((artifact) =>
|
|
||||||
artifact.groupId && artifact.group ? [{ id: artifact.id, targetId: artifact.groupId }] : [],
|
|
||||||
),
|
|
||||||
"group",
|
|
||||||
);
|
|
||||||
for (const artifact of groupArtifacts) {
|
for (const artifact of groupArtifacts) {
|
||||||
if (!artifact.groupId || !artifact.group) continue;
|
if (!artifact.groupId || !artifact.group) continue;
|
||||||
const message = groupArtifactMessages.get(artifact.id);
|
const message = groupArtifactMessages.get(artifact.id);
|
||||||
@ -193,19 +259,6 @@ export async function querySpaceSearch(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const routines = await prisma.routine.findMany({
|
|
||||||
where: {
|
|
||||||
spaceId: actor.spaceId,
|
|
||||||
userId: actor.userId,
|
|
||||||
OR: [
|
|
||||||
{ name: { contains: query, mode: "insensitive" } },
|
|
||||||
{ prompt: { contains: query, mode: "insensitive" } },
|
|
||||||
],
|
|
||||||
bot: { archivedAt: null },
|
|
||||||
},
|
|
||||||
include: { bot: { select: { name: true } } },
|
|
||||||
take: SEARCH_LIMIT,
|
|
||||||
});
|
|
||||||
for (const routine of routines) {
|
for (const routine of routines) {
|
||||||
pushInto(
|
pushInto(
|
||||||
contentHits,
|
contentHits,
|
||||||
@ -221,29 +274,6 @@ export async function querySpaceSearch(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const pattern = `%${query}%`;
|
|
||||||
const messageRows = await prisma.$queryRaw<
|
|
||||||
Array<{
|
|
||||||
id: string;
|
|
||||||
threadId: string;
|
|
||||||
seq: number;
|
|
||||||
blocks: Prisma.JsonValue;
|
|
||||||
botId: string;
|
|
||||||
botName: string;
|
|
||||||
}>
|
|
||||||
>`
|
|
||||||
SELECT m.id, m."threadId", m.seq, m.blocks, b.id AS "botId", b.name AS "botName"
|
|
||||||
FROM messages m
|
|
||||||
INNER JOIN threads t ON t.id = m."threadId"
|
|
||||||
INNER JOIN bots b ON b.id = t."botId"
|
|
||||||
WHERE t."spaceId" = ${actor.spaceId}
|
|
||||||
AND t."userId" = ${actor.userId}
|
|
||||||
AND b."archivedAt" IS NULL
|
|
||||||
AND m.blocks::text ILIKE ${pattern}
|
|
||||||
ORDER BY m."createdAt" DESC
|
|
||||||
LIMIT ${SEARCH_LIMIT}
|
|
||||||
`;
|
|
||||||
|
|
||||||
for (const row of messageRows) {
|
for (const row of messageRows) {
|
||||||
pushMessageHits(row.blocks as MessageBlock[], {
|
pushMessageHits(row.blocks as MessageBlock[], {
|
||||||
botId: row.botId,
|
botId: row.botId,
|
||||||
@ -255,28 +285,6 @@ export async function querySpaceSearch(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const groupMessageRows = await prisma.$queryRaw<
|
|
||||||
Array<{
|
|
||||||
id: string;
|
|
||||||
threadId: string;
|
|
||||||
seq: number;
|
|
||||||
blocks: Prisma.JsonValue;
|
|
||||||
groupId: string;
|
|
||||||
groupName: string;
|
|
||||||
}>
|
|
||||||
>`
|
|
||||||
SELECT m.id, m."threadId", m.seq, m.blocks, g.id AS "groupId", g.name AS "groupName"
|
|
||||||
FROM messages m
|
|
||||||
INNER JOIN threads t ON t.id = m."threadId"
|
|
||||||
INNER JOIN chat_groups g ON g.id = t."groupId"
|
|
||||||
WHERE t."spaceId" = ${actor.spaceId}
|
|
||||||
AND t."userId" = ${actor.userId}
|
|
||||||
AND t."groupId" IS NOT NULL
|
|
||||||
AND m.blocks::text ILIKE ${pattern}
|
|
||||||
ORDER BY m."createdAt" DESC
|
|
||||||
LIMIT ${SEARCH_LIMIT}
|
|
||||||
`;
|
|
||||||
|
|
||||||
for (const row of groupMessageRows) {
|
for (const row of groupMessageRows) {
|
||||||
pushMessageHits(row.blocks as MessageBlock[], {
|
pushMessageHits(row.blocks as MessageBlock[], {
|
||||||
groupId: row.groupId,
|
groupId: row.groupId,
|
||||||
|
|||||||
@ -90,7 +90,7 @@ describe("server update install kind", () => {
|
|||||||
if (href.endsWith("/state")) {
|
if (href.endsWith("/state")) {
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
image: "ghcr.io/elie222/rakazo/app",
|
image: "ghcr.io/cynarai/evoo-local/app",
|
||||||
currentTag: "sha-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
currentTag: "sha-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||||
previousTag: "sha-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
previousTag: "sha-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
||||||
running: false,
|
running: false,
|
||||||
@ -98,7 +98,7 @@ describe("server update install kind", () => {
|
|||||||
present: true,
|
present: true,
|
||||||
commit: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
commit: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||||
branch: "main",
|
branch: "main",
|
||||||
remoteUrl: "https://github.com/elie222/rakazo",
|
remoteUrl: "https://github.com/cynarAI/evOo-local",
|
||||||
dirty: false,
|
dirty: false,
|
||||||
dirtyPaths: [],
|
dirtyPaths: [],
|
||||||
},
|
},
|
||||||
@ -134,7 +134,7 @@ describe("server update install kind", () => {
|
|||||||
fromTag: "sha-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
fromTag: "sha-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||||
toTag: "sha-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
toTag: "sha-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
||||||
strategy: "pull",
|
strategy: "pull",
|
||||||
repoUrl: "https://github.com/elie222/rakazo",
|
repoUrl: "https://github.com/cynarAI/evOo-local",
|
||||||
branch: "main",
|
branch: "main",
|
||||||
restart: "not-required",
|
restart: "not-required",
|
||||||
restartAdvice: "Recreate failed; prior image restored, env pin not restored.",
|
restartAdvice: "Recreate failed; prior image restored, env pin not restored.",
|
||||||
@ -149,7 +149,7 @@ describe("server update install kind", () => {
|
|||||||
if (href.endsWith("/state")) {
|
if (href.endsWith("/state")) {
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
image: "ghcr.io/elie222/rakazo/app",
|
image: "ghcr.io/cynarai/evoo-local/app",
|
||||||
currentTag: "sha-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
currentTag: "sha-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
||||||
previousTag: "sha-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
previousTag: "sha-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||||
running: false,
|
running: false,
|
||||||
@ -158,7 +158,7 @@ describe("server update install kind", () => {
|
|||||||
present: true,
|
present: true,
|
||||||
commit: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
commit: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||||
branch: "main",
|
branch: "main",
|
||||||
remoteUrl: "https://github.com/elie222/rakazo",
|
remoteUrl: "https://github.com/cynarAI/evOo-local",
|
||||||
dirty: false,
|
dirty: false,
|
||||||
dirtyPaths: [],
|
dirtyPaths: [],
|
||||||
},
|
},
|
||||||
|
|||||||
@ -713,7 +713,7 @@ export class TeamChatBridge {
|
|||||||
const blocks = Array.isArray(response?.blocks) ? (response.blocks as MessageBlock[]) : [];
|
const blocks = Array.isArray(response?.blocks) ? (response.blocks as MessageBlock[]) : [];
|
||||||
const content =
|
const content =
|
||||||
run.status === "failed"
|
run.status === "failed"
|
||||||
? `${target.name} could not complete the delegated request. Open Rakazo for details.`
|
? `${target.name} could not complete the delegated request. Open evOo for details.`
|
||||||
: teamChatResponseText(blocks, target.name);
|
: teamChatResponseText(blocks, target.name);
|
||||||
if (!content) continue;
|
if (!content) continue;
|
||||||
await this.deps.send({
|
await this.deps.send({
|
||||||
@ -1160,7 +1160,7 @@ export class TeamChatBridge {
|
|||||||
await this.sendOnce(message.id, {
|
await this.sendOnce(message.id, {
|
||||||
conversationId: message.externalConversation.conversationId,
|
conversationId: message.externalConversation.conversationId,
|
||||||
replyThreadId: message.replyThreadId,
|
replyThreadId: message.replyThreadId,
|
||||||
content: `${this.target?.name ?? "The agent"} could not complete that request. Open Rakazo for details.`,
|
content: `${this.target?.name ?? "The agent"} could not complete that request. Open evOo for details.`,
|
||||||
idempotencyKey: `external-message:${message.id}:failure`,
|
idempotencyKey: `external-message:${message.id}:failure`,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -88,4 +88,77 @@ describe("model team chat engagement judge", () => {
|
|||||||
expect(instructions).toContain("(Europe/Berlin, the user's time zone)");
|
expect(instructions).toContain("(Europe/Berlin, the user's time zone)");
|
||||||
expect(instructions).toContain("low-cost engagement judge");
|
expect(instructions).toContain("low-cost engagement judge");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("prefers the space fast model tier when mapped and no explicit judge override is set", async () => {
|
||||||
|
let selectedModel: { provider: string; id: string; apiKey?: string } | undefined;
|
||||||
|
const runtime = {
|
||||||
|
run: vi.fn((request: { model: { provider: string; id: string; apiKey?: string } }) => {
|
||||||
|
selectedModel = request.model;
|
||||||
|
return (async function* () {
|
||||||
|
yield { type: "done" as const, text: '{"act":true,"reason":"Direct mention."}' };
|
||||||
|
})();
|
||||||
|
}),
|
||||||
|
} as unknown as AgentRuntime;
|
||||||
|
const prisma = {
|
||||||
|
deploymentSettings: { findUnique: vi.fn(async () => null) },
|
||||||
|
spaceModelPreference: { findFirst: vi.fn(async () => null) },
|
||||||
|
spaceModelTier: {
|
||||||
|
findUnique: vi.fn(async () => ({
|
||||||
|
tier: "fast",
|
||||||
|
credentialId: "cred-fast",
|
||||||
|
modelId: "gemma4:latest",
|
||||||
|
credential: {
|
||||||
|
id: "cred-fast",
|
||||||
|
userId: "user-1",
|
||||||
|
provider: "openai-compatible",
|
||||||
|
secretId: "sec-fast",
|
||||||
|
preferences: [{ isDefault: false, modelId: null }],
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
secret: {
|
||||||
|
findFirst: vi.fn(async () => ({ id: "sec-fast", ciphertext: "cipher-fast" })),
|
||||||
|
},
|
||||||
|
userProfile: { findUnique: vi.fn(async () => ({ timeZone: "UTC" })) },
|
||||||
|
} as unknown as PrismaClient;
|
||||||
|
const secrets = {
|
||||||
|
load: vi.fn(() =>
|
||||||
|
JSON.stringify({
|
||||||
|
kind: "openai_compatible",
|
||||||
|
apiKey: "local-key",
|
||||||
|
baseUrl: "http://127.0.0.1:11434/v1",
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
} as unknown as EncryptedSecretStore;
|
||||||
|
|
||||||
|
const judge = new ModelTeamChatEngagementJudge({
|
||||||
|
prisma,
|
||||||
|
runtime,
|
||||||
|
secrets,
|
||||||
|
deploymentProvider: "openrouter",
|
||||||
|
deploymentModel: "expensive-default",
|
||||||
|
});
|
||||||
|
|
||||||
|
const decision = await judge.decide({
|
||||||
|
bot: {
|
||||||
|
id: "bot-1",
|
||||||
|
spaceId: "space-1",
|
||||||
|
userId: "user-1",
|
||||||
|
name: "Arthur",
|
||||||
|
modelProvider: "anthropic",
|
||||||
|
modelId: "claude-opus",
|
||||||
|
},
|
||||||
|
channelId: "C1",
|
||||||
|
rules: "",
|
||||||
|
messages: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(decision).toEqual({ act: true, reason: "Direct mention.", askedByEventId: undefined });
|
||||||
|
expect(selectedModel).toMatchObject({
|
||||||
|
provider: "openai-compatible",
|
||||||
|
id: "gemma4:latest",
|
||||||
|
apiKey: "local-key",
|
||||||
|
baseUrl: "http://127.0.0.1:11434/v1",
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,14 +1,15 @@
|
|||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import type { AgentModelOAuthCredential, AgentRuntime } from "@rakazo/adapter-kit";
|
import type { AgentModelOAuthCredential, AgentRuntime } from "@rakazo/adapter-kit";
|
||||||
|
import type { EncryptedSecretStore } from "@rakazo/adapters";
|
||||||
import {
|
import {
|
||||||
type EncryptedSecretStore,
|
|
||||||
formatCurrentTimeInstruction,
|
formatCurrentTimeInstruction,
|
||||||
loadUserTimeZone,
|
loadUserTimeZone,
|
||||||
resolveModelAuth,
|
resolveModelAuth,
|
||||||
serializeModelSecret,
|
serializeModelSecret,
|
||||||
toOAuthCredential,
|
toOAuthCredential,
|
||||||
} from "@rakazo/adapters";
|
} from "@rakazo/adapters";
|
||||||
import { findDefaultModelCredential, findModelCredential, type PrismaClient } from "@rakazo/db";
|
import type { PrismaClient } from "@rakazo/db";
|
||||||
|
import { findDefaultModelCredential, findModelCredential, findSpaceModelTier } from "@rakazo/db";
|
||||||
import { getLogger } from "@rakazo/logging";
|
import { getLogger } from "@rakazo/logging";
|
||||||
|
|
||||||
const MAX_RULES_CHARS = 4_000;
|
const MAX_RULES_CHARS = 4_000;
|
||||||
@ -191,27 +192,39 @@ export class ModelTeamChatEngagementJudge implements TeamChatEngagementJudge {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
} | null> {
|
} | null> {
|
||||||
const settings = await this.deps.prisma.deploymentSettings.findUnique({
|
|
||||||
where: { id: "default" },
|
|
||||||
});
|
|
||||||
const scope = { userId: bot.userId, spaceId: bot.spaceId };
|
const scope = { userId: bot.userId, spaceId: bot.spaceId };
|
||||||
const defaultCredential = await findDefaultModelCredential(this.deps.prisma, scope);
|
const explicitJudgeOverride = Boolean(this.deps.providerOverride && this.deps.modelOverride);
|
||||||
|
const fastTier =
|
||||||
|
!explicitJudgeOverride && this.deps.prisma.spaceModelTier
|
||||||
|
? await findSpaceModelTier(this.deps.prisma, scope, "fast")
|
||||||
|
: null;
|
||||||
|
const defaultCredential = fastTier
|
||||||
|
? null
|
||||||
|
: await findDefaultModelCredential(this.deps.prisma, scope);
|
||||||
const requestedProvider = this.deps.providerOverride ?? bot.modelProvider;
|
const requestedProvider = this.deps.providerOverride ?? bot.modelProvider;
|
||||||
const requestedModel = this.deps.modelOverride ?? bot.modelId;
|
const requestedModel = this.deps.modelOverride ?? bot.modelId;
|
||||||
const overrideCredential = requestedProvider
|
const overrideCredential =
|
||||||
? await findModelCredential(this.deps.prisma, scope, requestedProvider)
|
!fastTier && requestedProvider
|
||||||
: null;
|
? await findModelCredential(this.deps.prisma, scope, requestedProvider)
|
||||||
const explicitJudgeOverride = Boolean(this.deps.providerOverride && this.deps.modelOverride);
|
: null;
|
||||||
const useOverride = Boolean(
|
const useOverride = Boolean(
|
||||||
requestedProvider && requestedModel && (explicitJudgeOverride || overrideCredential),
|
requestedProvider && requestedModel && (explicitJudgeOverride || overrideCredential),
|
||||||
);
|
);
|
||||||
const credential = useOverride ? overrideCredential : defaultCredential;
|
const credential =
|
||||||
|
fastTier?.credential ?? (useOverride ? overrideCredential : defaultCredential);
|
||||||
|
const settings = fastTier
|
||||||
|
? null
|
||||||
|
: await this.deps.prisma.deploymentSettings.findUnique({
|
||||||
|
where: { id: "default" },
|
||||||
|
});
|
||||||
const provider =
|
const provider =
|
||||||
|
fastTier?.credential.provider ??
|
||||||
(useOverride ? requestedProvider : null) ??
|
(useOverride ? requestedProvider : null) ??
|
||||||
credential?.provider ??
|
credential?.provider ??
|
||||||
settings?.defaultModelProvider ??
|
settings?.defaultModelProvider ??
|
||||||
this.deps.deploymentProvider;
|
this.deps.deploymentProvider;
|
||||||
const modelId =
|
const modelId =
|
||||||
|
fastTier?.modelId ??
|
||||||
(useOverride ? requestedModel : null) ??
|
(useOverride ? requestedModel : null) ??
|
||||||
credential?.defaultModel ??
|
credential?.defaultModel ??
|
||||||
settings?.defaultModelId ??
|
settings?.defaultModelId ??
|
||||||
|
|||||||
275
apps/api/src/thread-cards.postgres.test.ts
Normal file
275
apps/api/src/thread-cards.postgres.test.ts
Normal file
@ -0,0 +1,275 @@
|
|||||||
|
import type { JobPublisher } from "@rakazo/adapter-kit";
|
||||||
|
import type { Actor, CardBlock, MessageBlock } from "@rakazo/contracts";
|
||||||
|
import { buildCardFromTool, buildSuggestionCard } from "@rakazo/core";
|
||||||
|
import { approvalEffectKey } from "@rakazo/core/node/approval-effect-key";
|
||||||
|
import type { PrismaClient, ThreadEvents } from "@rakazo/db";
|
||||||
|
import { createDb, provisionMessagingIdentity } from "@rakazo/db";
|
||||||
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||||||
|
import { actOnThreadCard } from "./thread-cards.js";
|
||||||
|
import type { ThreadTarget } from "./thread-target.js";
|
||||||
|
import { resolveThreadTarget } from "./thread-target.js";
|
||||||
|
|
||||||
|
const databaseUrl = process.env.DATABASE_URL;
|
||||||
|
const describePostgres =
|
||||||
|
process.env.VERIFY_DATABASE && databaseUrl ? describe.sequential : describe.skip;
|
||||||
|
|
||||||
|
describePostgres("actOnThreadCard (PostgreSQL)", () => {
|
||||||
|
let prisma: PrismaClient;
|
||||||
|
let close: () => Promise<void>;
|
||||||
|
let identity: { userId: string; spaceId: string; botId: string; threadId: string };
|
||||||
|
let actor: Actor;
|
||||||
|
let target: ThreadTarget;
|
||||||
|
const notify = vi.fn(async () => undefined);
|
||||||
|
const enqueue = vi.fn(async () => undefined);
|
||||||
|
let deps: { prisma: PrismaClient; events: ThreadEvents; jobs: JobPublisher };
|
||||||
|
let seq = 0;
|
||||||
|
let nonce = 0;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const db = createDb(databaseUrl!);
|
||||||
|
prisma = db.prisma;
|
||||||
|
close = async () => {
|
||||||
|
await db.prisma.$disconnect();
|
||||||
|
await db.pool.end();
|
||||||
|
};
|
||||||
|
identity = await provisionMessagingIdentity(
|
||||||
|
prisma,
|
||||||
|
{ provider: "sendblue", address: `+1556${Date.now().toString().slice(-7)}` },
|
||||||
|
{ signupsEnabled: undefined, signupAllowlist: undefined },
|
||||||
|
);
|
||||||
|
actor = {
|
||||||
|
userId: identity.userId,
|
||||||
|
spaceId: identity.spaceId,
|
||||||
|
email: "card@example.com",
|
||||||
|
isDeploymentOwner: false,
|
||||||
|
};
|
||||||
|
deps = {
|
||||||
|
prisma,
|
||||||
|
events: { notify } as unknown as ThreadEvents,
|
||||||
|
jobs: {
|
||||||
|
enqueue,
|
||||||
|
cancel: vi.fn(async () => undefined),
|
||||||
|
close: vi.fn(async () => undefined),
|
||||||
|
} as unknown as JobPublisher,
|
||||||
|
};
|
||||||
|
target = await resolveThreadTarget(prisma, actor, { botId: identity.botId });
|
||||||
|
seq =
|
||||||
|
(
|
||||||
|
await prisma.message.aggregate({
|
||||||
|
where: { threadId: identity.threadId },
|
||||||
|
_max: { seq: true },
|
||||||
|
})
|
||||||
|
)._max.seq ?? 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
if (identity) {
|
||||||
|
await prisma.messagingIdentity.deleteMany({ where: { userId: identity.userId } });
|
||||||
|
await prisma.organization.deleteMany({ where: { id: identity.spaceId } });
|
||||||
|
await prisma.user.deleteMany({ where: { id: identity.userId } });
|
||||||
|
}
|
||||||
|
await close?.();
|
||||||
|
});
|
||||||
|
|
||||||
|
async function postCard(args: Parameters<typeof buildCardFromTool>[0]) {
|
||||||
|
const built = buildCardFromTool(args, `card${++seq}`);
|
||||||
|
if (!("ok" in built)) throw new Error(built.error);
|
||||||
|
const message = await prisma.message.create({
|
||||||
|
data: {
|
||||||
|
threadId: identity.threadId,
|
||||||
|
seq,
|
||||||
|
role: "bot",
|
||||||
|
botId: identity.botId,
|
||||||
|
blocks: [built.block],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return { messageId: message.id, cardId: built.block.id! };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function card(messageId: string) {
|
||||||
|
const row = await prisma.message.findUniqueOrThrow({ where: { id: messageId } });
|
||||||
|
return (row.blocks as MessageBlock[])[0] as CardBlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
const act = (input: Omit<Parameters<typeof actOnThreadCard>[3], "clientNonce">, again?: string) =>
|
||||||
|
actOnThreadCard(deps, actor, target, { ...input, clientNonce: again ?? `nonce-${++nonce}` });
|
||||||
|
|
||||||
|
it("ticks a checklist item without starting a run", async () => {
|
||||||
|
const posted = await postCard({ title: "Shopping", checklist: ["Milk", "Eggs"] });
|
||||||
|
await act({
|
||||||
|
...posted,
|
||||||
|
act: { type: "toggle", sectionId: "checklist", itemId: "i2", checked: true },
|
||||||
|
});
|
||||||
|
const updated = await card(posted.messageId);
|
||||||
|
expect(updated.sections?.[1]).toMatchObject({
|
||||||
|
items: [{ id: "i1" }, { id: "i2", checked: true }],
|
||||||
|
});
|
||||||
|
expect(await prisma.message.count({ where: { replyToMessageId: posted.messageId } })).toBe(0);
|
||||||
|
expect(
|
||||||
|
await prisma.event.count({
|
||||||
|
where: {
|
||||||
|
threadId: identity.threadId,
|
||||||
|
type: "thread.message.updated",
|
||||||
|
payload: { path: ["messageId"], equals: posted.messageId },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("answers a choice once and sends it to the bot as the user's reply", async () => {
|
||||||
|
const posted = await postCard({ title: "Dinner", options: ["Pasta", "Curry"] });
|
||||||
|
const pick = {
|
||||||
|
...posted,
|
||||||
|
act: { type: "select" as const, sectionId: "select", optionId: "o2" },
|
||||||
|
};
|
||||||
|
await act(pick, "pick-curry");
|
||||||
|
// A retried tap is a no-op rather than an error.
|
||||||
|
await act(pick, "pick-curry");
|
||||||
|
expect((await card(posted.messageId)).answer).toEqual({ text: "Curry" });
|
||||||
|
const replies = await prisma.message.findMany({
|
||||||
|
where: { replyToMessageId: posted.messageId },
|
||||||
|
select: { role: true, blocks: true, runId: true },
|
||||||
|
});
|
||||||
|
expect(replies).toHaveLength(1);
|
||||||
|
expect(replies[0]).toMatchObject({ role: "user", blocks: [{ kind: "text", text: "Curry" }] });
|
||||||
|
expect(replies[0]?.runId).toBeTruthy();
|
||||||
|
await expect(
|
||||||
|
act({ ...posted, act: { type: "select", sectionId: "select", optionId: "o1" } }),
|
||||||
|
).rejects.toThrow("already answered");
|
||||||
|
});
|
||||||
|
|
||||||
|
async function postSuggestion(createdAt?: Date) {
|
||||||
|
const task = await prisma.task.create({
|
||||||
|
data: {
|
||||||
|
spaceId: identity.spaceId,
|
||||||
|
botId: identity.botId,
|
||||||
|
threadId: identity.threadId,
|
||||||
|
userId: identity.userId,
|
||||||
|
prompt: "I should move more",
|
||||||
|
status: "completed",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const run = await prisma.run.create({
|
||||||
|
data: {
|
||||||
|
spaceId: identity.spaceId,
|
||||||
|
botId: identity.botId,
|
||||||
|
threadId: identity.threadId,
|
||||||
|
taskId: task.id,
|
||||||
|
userId: identity.userId,
|
||||||
|
status: "completed",
|
||||||
|
trigger: "user",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const call = {
|
||||||
|
tool: "schedule_create",
|
||||||
|
args: { name: "Morning walk", prompt: "Take a walk.", cron: "0 8 * * *" },
|
||||||
|
};
|
||||||
|
const effect = await prisma.externalEffect.create({
|
||||||
|
data: {
|
||||||
|
spaceId: identity.spaceId,
|
||||||
|
runId: run.id,
|
||||||
|
kind: "suggestion",
|
||||||
|
idempotencyKey: `suggestion:${run.id}:test`,
|
||||||
|
status: "proposed",
|
||||||
|
request: { ...call, botId: identity.botId, threadId: identity.threadId, subject: "x" },
|
||||||
|
...(createdAt ? { createdAt } : {}),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const block = buildSuggestionCard({ id: effect.id, effectId: effect.id, ...call });
|
||||||
|
const message = await prisma.message.create({
|
||||||
|
data: {
|
||||||
|
threadId: identity.threadId,
|
||||||
|
seq: ++seq,
|
||||||
|
role: "bot",
|
||||||
|
botId: identity.botId,
|
||||||
|
blocks: [block],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
await prisma.externalEffect.update({
|
||||||
|
where: { id: effect.id },
|
||||||
|
data: {
|
||||||
|
request: {
|
||||||
|
...call,
|
||||||
|
botId: identity.botId,
|
||||||
|
threadId: identity.threadId,
|
||||||
|
subject: "x",
|
||||||
|
card: { threadId: identity.threadId, messageId: message.id },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return { messageId: message.id, cardId: effect.id, effectId: effect.id, ...call };
|
||||||
|
}
|
||||||
|
|
||||||
|
it("runs an accepted suggestion as an approved run, once", async () => {
|
||||||
|
const posted = await postSuggestion();
|
||||||
|
enqueue.mockClear();
|
||||||
|
const accept = {
|
||||||
|
messageId: posted.messageId,
|
||||||
|
cardId: posted.cardId,
|
||||||
|
act: { type: "action" as const, actionId: "accept" },
|
||||||
|
};
|
||||||
|
await act(accept, "accept-walk");
|
||||||
|
await act(accept, "accept-walk");
|
||||||
|
expect((await card(posted.messageId)).suggestion?.status).toBe("running");
|
||||||
|
const effect = await prisma.externalEffect.findUniqueOrThrow({
|
||||||
|
where: { id: posted.effectId },
|
||||||
|
});
|
||||||
|
expect(effect.status).toBe("accepted");
|
||||||
|
const runId = (effect.result as { runId: string }).runId;
|
||||||
|
const run = await prisma.run.findUniqueOrThrow({ where: { id: runId } });
|
||||||
|
expect(run).toMatchObject({ trigger: "suggestion", botId: identity.botId, status: "queued" });
|
||||||
|
const approved = await prisma.externalEffect.findMany({ where: { runId } });
|
||||||
|
expect(approved).toEqual([
|
||||||
|
expect.objectContaining({
|
||||||
|
kind: "schedule_create",
|
||||||
|
status: "approved",
|
||||||
|
idempotencyKey: approvalEffectKey(runId, "schedule_create", posted.args),
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
expect(enqueue).toHaveBeenCalledTimes(1);
|
||||||
|
// No reply goes to the bot; the card itself carries the outcome.
|
||||||
|
expect(await prisma.message.count({ where: { replyToMessageId: posted.messageId } })).toBe(0);
|
||||||
|
await expect(act({ ...accept, act: { type: "action", actionId: "dismiss" } })).rejects.toThrow(
|
||||||
|
"already settled",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("dismisses a suggestion without running anything", async () => {
|
||||||
|
const posted = await postSuggestion();
|
||||||
|
await act({
|
||||||
|
messageId: posted.messageId,
|
||||||
|
cardId: posted.cardId,
|
||||||
|
act: { type: "action", actionId: "dismiss" },
|
||||||
|
});
|
||||||
|
expect((await card(posted.messageId)).suggestion?.status).toBe("dismissed");
|
||||||
|
const effect = await prisma.externalEffect.findUniqueOrThrow({
|
||||||
|
where: { id: posted.effectId },
|
||||||
|
});
|
||||||
|
expect(effect).toMatchObject({ status: "dismissed" });
|
||||||
|
expect((effect.result as { runId: unknown }).runId).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("lets an old suggestion expire instead of running it", async () => {
|
||||||
|
const posted = await postSuggestion(new Date(Date.now() - 8 * 24 * 60 * 60 * 1000));
|
||||||
|
await act({
|
||||||
|
messageId: posted.messageId,
|
||||||
|
cardId: posted.cardId,
|
||||||
|
act: { type: "action", actionId: "accept" },
|
||||||
|
});
|
||||||
|
expect((await card(posted.messageId)).suggestion?.status).toBe("expired");
|
||||||
|
expect(
|
||||||
|
await prisma.externalEffect.findUniqueOrThrow({ where: { id: posted.effectId } }),
|
||||||
|
).toMatchObject({ status: "expired", result: null });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps other threads' cards out of reach", async () => {
|
||||||
|
const posted = await postCard({ title: "Elsewhere", checklist: ["One"] });
|
||||||
|
await expect(
|
||||||
|
act({
|
||||||
|
...posted,
|
||||||
|
cardId: "not-this-card",
|
||||||
|
act: { type: "toggle", sectionId: "checklist", itemId: "i1", checked: true },
|
||||||
|
}),
|
||||||
|
).rejects.toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
242
apps/api/src/thread-cards.ts
Normal file
242
apps/api/src/thread-cards.ts
Normal file
@ -0,0 +1,242 @@
|
|||||||
|
import { ORPCError } from "@orpc/server";
|
||||||
|
import type { JobPublisher } from "@rakazo/adapter-kit";
|
||||||
|
import { runContinueJob } from "@rakazo/adapter-kit";
|
||||||
|
import type { Actor, CardAct, CardBlock, MessageBlock } from "@rakazo/contracts";
|
||||||
|
import { SUGGESTION_TTL_MS } from "@rakazo/contracts";
|
||||||
|
import type { CardActResult } from "@rakazo/core";
|
||||||
|
import { applyCardAct, withSuggestionStatus } from "@rakazo/core";
|
||||||
|
import { approvalEffectKey } from "@rakazo/core/node/approval-effect-key";
|
||||||
|
import type { Prisma, PrismaClient, ThreadEvents } from "@rakazo/db";
|
||||||
|
import { IsolationError, updateCardInTransaction } from "@rakazo/db";
|
||||||
|
import { getLogger } from "@rakazo/logging";
|
||||||
|
import type { ThreadTarget } from "./thread-target.js";
|
||||||
|
import { sendThreadMessage } from "./thread-target.js";
|
||||||
|
|
||||||
|
type CardDeps = { prisma: PrismaClient; events: ThreadEvents; jobs: JobPublisher };
|
||||||
|
|
||||||
|
/** What an accepted suggestion runs: the stored call, by the bot it was meant for. */
|
||||||
|
type StoredSuggestion = {
|
||||||
|
tool: string;
|
||||||
|
args: Record<string, unknown>;
|
||||||
|
botId: string;
|
||||||
|
threadId: string;
|
||||||
|
card?: { threadId: string; messageId: string };
|
||||||
|
};
|
||||||
|
|
||||||
|
function storedSuggestion(value: unknown): StoredSuggestion | null {
|
||||||
|
if (!value || typeof value !== "object") return null;
|
||||||
|
const request = value as Partial<StoredSuggestion>;
|
||||||
|
return typeof request.tool === "string" &&
|
||||||
|
typeof request.botId === "string" &&
|
||||||
|
typeof request.threadId === "string" &&
|
||||||
|
request.args &&
|
||||||
|
typeof request.args === "object"
|
||||||
|
? (request as StoredSuggestion)
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function resultObject(value: unknown): Record<string, unknown> {
|
||||||
|
return value && typeof value === "object" && !Array.isArray(value)
|
||||||
|
? (value as Record<string, unknown>)
|
||||||
|
: {};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadCard(prisma: PrismaClient, threadId: string, messageId: string, cardId: string) {
|
||||||
|
const message = await prisma.message.findFirst({
|
||||||
|
where: { id: messageId, threadId, role: "bot" },
|
||||||
|
select: { blocks: true },
|
||||||
|
});
|
||||||
|
const blocks = (Array.isArray(message?.blocks) ? message.blocks : []) as MessageBlock[];
|
||||||
|
const card = blocks.find((block) => block.kind === "card" && block.id === cardId);
|
||||||
|
return card?.kind === "card" ? card : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply one tap on a bot's card. The card changes in place for everyone watching the thread; a
|
||||||
|
* choice or an action then goes to the card's bot as the user's reply, through the normal send.
|
||||||
|
* Accept or Dismiss on a suggestion runs or drops the stored call instead.
|
||||||
|
*/
|
||||||
|
export async function actOnThreadCard(
|
||||||
|
deps: CardDeps,
|
||||||
|
actor: Actor,
|
||||||
|
target: ThreadTarget,
|
||||||
|
input: { messageId: string; cardId: string; act: CardAct; clientNonce: string },
|
||||||
|
options: { requireModel?: () => Promise<void> } = {},
|
||||||
|
): Promise<{ ok: true }> {
|
||||||
|
// A retried answer already landed as the user's reply; the card was answered with it.
|
||||||
|
const replayed = await deps.prisma.message.findUnique({
|
||||||
|
where: { threadId_clientNonce: { threadId: target.threadId, clientNonce: input.clientNonce } },
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
if (replayed) return { ok: true };
|
||||||
|
|
||||||
|
const card = await loadCard(deps.prisma, target.threadId, input.messageId, input.cardId);
|
||||||
|
if (!card) throw new IsolationError();
|
||||||
|
if (card.suggestion && input.act.type === "action") {
|
||||||
|
return settleSuggestion(deps, actor, target, input, card.suggestion.effectId);
|
||||||
|
}
|
||||||
|
if (input.act.type !== "toggle") await options.requireModel?.();
|
||||||
|
|
||||||
|
let outcome: CardActResult | undefined;
|
||||||
|
let botId: string | null = null;
|
||||||
|
const updated = await deps.prisma.$transaction(async (tx) => {
|
||||||
|
const message = await tx.message.findFirst({
|
||||||
|
where: { id: input.messageId, threadId: target.threadId },
|
||||||
|
select: { botId: true },
|
||||||
|
});
|
||||||
|
botId = message?.botId ?? null;
|
||||||
|
return updateCardInTransaction(
|
||||||
|
tx,
|
||||||
|
{ threadId: target.threadId, messageId: input.messageId, cardId: input.cardId },
|
||||||
|
(current) => {
|
||||||
|
outcome = applyCardAct(current, input.act);
|
||||||
|
return outcome.ok && outcome.changed ? outcome.block : null;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
if (!outcome) throw new IsolationError();
|
||||||
|
if (!outcome.ok) throw new ORPCError("CONFLICT", { message: outcome.error });
|
||||||
|
if (updated) await notify(deps.events, target.threadId, updated.seq);
|
||||||
|
if (outcome.reply) {
|
||||||
|
await sendThreadMessage(deps, actor, target, {
|
||||||
|
text: outcome.reply,
|
||||||
|
replyToMessageId: input.messageId,
|
||||||
|
clientNonce: input.clientNonce,
|
||||||
|
// In a group the answer goes to the bot that asked, not to whoever leads.
|
||||||
|
...(target.kind === "group" && botId
|
||||||
|
? { mentions: [{ kind: "bot" as const, id: botId }] }
|
||||||
|
: {}),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function notify(events: ThreadEvents, threadId: string, seq: number) {
|
||||||
|
await events.notify(threadId, seq).catch((error) => {
|
||||||
|
getLogger().error("thread card realtime notification", error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accept runs the stored call as its own run of the bot it was meant for, already approved:
|
||||||
|
* accepting is the approval. Dismiss only closes the card. Neither needs a model.
|
||||||
|
*/
|
||||||
|
async function settleSuggestion(
|
||||||
|
deps: CardDeps,
|
||||||
|
actor: Actor,
|
||||||
|
target: ThreadTarget,
|
||||||
|
input: { messageId: string; cardId: string; act: CardAct; clientNonce: string },
|
||||||
|
effectId: string,
|
||||||
|
): Promise<{ ok: true }> {
|
||||||
|
const settled = await deps.prisma.$transaction(async (tx) => {
|
||||||
|
let outcome: CardActResult | undefined;
|
||||||
|
const effect = await tx.externalEffect.findUnique({ where: { id: effectId } });
|
||||||
|
const request = storedSuggestion(effect?.request);
|
||||||
|
if (
|
||||||
|
!effect ||
|
||||||
|
!request ||
|
||||||
|
effect.kind !== "suggestion" ||
|
||||||
|
effect.spaceId !== actor.spaceId ||
|
||||||
|
request.card?.messageId !== input.messageId
|
||||||
|
) {
|
||||||
|
throw new IsolationError();
|
||||||
|
}
|
||||||
|
// A retried tap that already went through.
|
||||||
|
if (resultObject(effect.result).clientNonce === input.clientNonce) return null;
|
||||||
|
const expired = Date.now() - effect.createdAt.getTime() > SUGGESTION_TTL_MS;
|
||||||
|
const updated = await updateCardInTransaction(
|
||||||
|
tx,
|
||||||
|
{ threadId: target.threadId, messageId: input.messageId, cardId: input.cardId },
|
||||||
|
(current: CardBlock) => {
|
||||||
|
if (expired && current.suggestion?.status === "proposed") {
|
||||||
|
return withSuggestionStatus(current, "expired");
|
||||||
|
}
|
||||||
|
outcome = applyCardAct(current, input.act);
|
||||||
|
return outcome.ok && outcome.changed ? outcome.block : null;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (expired) {
|
||||||
|
await tx.externalEffect.updateMany({
|
||||||
|
where: { id: effect.id, status: "proposed" },
|
||||||
|
data: { status: "expired" },
|
||||||
|
});
|
||||||
|
return { seq: updated?.seq ?? null, runId: null };
|
||||||
|
}
|
||||||
|
if (!outcome) throw new IsolationError();
|
||||||
|
if (!outcome.ok) throw new ORPCError("CONFLICT", { message: outcome.error });
|
||||||
|
if (effect.status !== "proposed") {
|
||||||
|
throw new ORPCError("CONFLICT", { message: "This suggestion was already settled." });
|
||||||
|
}
|
||||||
|
const runId =
|
||||||
|
outcome.suggestion === "accept"
|
||||||
|
? await startAcceptedRun(tx, actor, effect.spaceId, request)
|
||||||
|
: null;
|
||||||
|
await tx.externalEffect.update({
|
||||||
|
where: { id: effect.id },
|
||||||
|
data: {
|
||||||
|
status: runId ? "accepted" : "dismissed",
|
||||||
|
result: { ...resultObject(effect.result), clientNonce: input.clientNonce, runId },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return { seq: updated?.seq ?? null, runId };
|
||||||
|
});
|
||||||
|
if (settled?.seq) await notify(deps.events, target.threadId, settled.seq);
|
||||||
|
if (settled?.runId) {
|
||||||
|
await deps.jobs.enqueue(runContinueJob(settled.runId)).catch((error) => {
|
||||||
|
getLogger().error("accepted suggestion enqueue", error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function startAcceptedRun(
|
||||||
|
tx: Prisma.TransactionClient,
|
||||||
|
actor: Actor,
|
||||||
|
spaceId: string,
|
||||||
|
request: StoredSuggestion,
|
||||||
|
): Promise<string> {
|
||||||
|
const thread = await tx.thread.findFirst({
|
||||||
|
where: { id: request.threadId, spaceId },
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
const bot = await tx.bot.findFirst({
|
||||||
|
where: { id: request.botId, spaceId },
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
if (!thread || !bot) {
|
||||||
|
throw new ORPCError("CONFLICT", { message: "The bot for this suggestion is gone." });
|
||||||
|
}
|
||||||
|
const task = await tx.task.create({
|
||||||
|
data: {
|
||||||
|
spaceId,
|
||||||
|
botId: bot.id,
|
||||||
|
threadId: thread.id,
|
||||||
|
userId: actor.userId,
|
||||||
|
prompt: `Accepted suggestion: ${request.tool}`,
|
||||||
|
status: "queued",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const run = await tx.run.create({
|
||||||
|
data: {
|
||||||
|
spaceId,
|
||||||
|
botId: bot.id,
|
||||||
|
threadId: thread.id,
|
||||||
|
taskId: task.id,
|
||||||
|
userId: actor.userId,
|
||||||
|
status: "queued",
|
||||||
|
trigger: "suggestion",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
// The run replays this exact call; the approval gate sees it as already approved.
|
||||||
|
await tx.externalEffect.create({
|
||||||
|
data: {
|
||||||
|
spaceId,
|
||||||
|
runId: run.id,
|
||||||
|
kind: request.tool,
|
||||||
|
idempotencyKey: approvalEffectKey(run.id, request.tool, request.args),
|
||||||
|
status: "approved",
|
||||||
|
request: request.args as never,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return run.id;
|
||||||
|
}
|
||||||
@ -2,15 +2,17 @@ import type { SandboxProvider } from "@rakazo/adapter-kit";
|
|||||||
import type { Actor } from "@rakazo/contracts";
|
import type { Actor } from "@rakazo/contracts";
|
||||||
import type { PrismaClient } from "@rakazo/db";
|
import type { PrismaClient } from "@rakazo/db";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
import type { ThreadTarget } from "./thread-target.js";
|
||||||
import {
|
import {
|
||||||
cancelSupersededQueuedRuns,
|
cancelSupersededQueuedRuns,
|
||||||
followUpGroupThreadMessage,
|
followUpGroupThreadMessage,
|
||||||
groupDefaultBotId,
|
groupDefaultBotId,
|
||||||
reactToThreadMessage,
|
reactToThreadMessage,
|
||||||
resolveGroupFollowUpBotId,
|
resolveGroupFollowUpBotId,
|
||||||
|
resumeThreadRuns,
|
||||||
|
sendQueuedThreadMessagesNow,
|
||||||
sendThreadMessage,
|
sendThreadMessage,
|
||||||
stopThreadRuns,
|
stopThreadRuns,
|
||||||
type ThreadTarget,
|
|
||||||
threadHead,
|
threadHead,
|
||||||
threadSnapshot,
|
threadSnapshot,
|
||||||
} from "./thread-target.js";
|
} from "./thread-target.js";
|
||||||
@ -211,7 +213,13 @@ describe("threadSnapshot", () => {
|
|||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
where: expect.objectContaining({
|
where: expect.objectContaining({
|
||||||
type: {
|
type: {
|
||||||
in: ["thread.progress", "thread.subagent", "agent.tool.called", "agent.tool.completed"],
|
in: [
|
||||||
|
"thread.message.created",
|
||||||
|
"thread.progress",
|
||||||
|
"thread.subagent",
|
||||||
|
"agent.tool.called",
|
||||||
|
"agent.tool.completed",
|
||||||
|
],
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
@ -230,6 +238,69 @@ describe("threadSnapshot", () => {
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("does not reload live narration that was already posted as a message", async () => {
|
||||||
|
const run = {
|
||||||
|
id: "run-1",
|
||||||
|
botId: "bot-1",
|
||||||
|
threadId: "thread-1",
|
||||||
|
taskId: "task-1",
|
||||||
|
status: "running",
|
||||||
|
trigger: "user",
|
||||||
|
modelProvider: null,
|
||||||
|
modelId: null,
|
||||||
|
error: null,
|
||||||
|
startedAt: null,
|
||||||
|
completedAt: null,
|
||||||
|
createdAt: new Date("2026-08-23T00:00:00.000Z"),
|
||||||
|
};
|
||||||
|
const at = new Date("2026-08-23T00:00:00.000Z");
|
||||||
|
const event = (seq: number, type: string, payload: Record<string, unknown>) => ({
|
||||||
|
id: `event-${seq}`,
|
||||||
|
threadId: "thread-1",
|
||||||
|
botId: "bot-1",
|
||||||
|
seq,
|
||||||
|
type,
|
||||||
|
runId: "run-1",
|
||||||
|
payload,
|
||||||
|
createdAt: at,
|
||||||
|
});
|
||||||
|
const tx = {
|
||||||
|
$queryRaw: threadQueries(),
|
||||||
|
message: { findMany: vi.fn().mockResolvedValue([]) },
|
||||||
|
event: {
|
||||||
|
findFirst: vi.fn().mockResolvedValue({ seq: 6 }),
|
||||||
|
findMany: vi.fn().mockResolvedValue([
|
||||||
|
event(4, "thread.progress", { text: "Opening the page." }),
|
||||||
|
event(5, "thread.message.created", {
|
||||||
|
messageId: "message-1",
|
||||||
|
role: "bot",
|
||||||
|
blocks: [{ kind: "text", text: "Opening the page." }],
|
||||||
|
}),
|
||||||
|
event(6, "thread.progress", { text: "Writing the summary." }),
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
run: { findFirst: botRunFindFirst([run]) },
|
||||||
|
};
|
||||||
|
const prisma = {
|
||||||
|
$transaction: vi.fn(async (callback: (client: typeof tx) => unknown) => callback(tx)),
|
||||||
|
} as unknown as PrismaClient;
|
||||||
|
const target = {
|
||||||
|
kind: "bot",
|
||||||
|
botId: "bot-1",
|
||||||
|
threadId: "thread-1",
|
||||||
|
bot: { computer: null },
|
||||||
|
} as ThreadTarget;
|
||||||
|
|
||||||
|
const snapshot = await threadSnapshot({ prisma }, target);
|
||||||
|
|
||||||
|
expect(snapshot.messages).toEqual([
|
||||||
|
expect.objectContaining({
|
||||||
|
id: "progress:run-1",
|
||||||
|
blocks: [{ kind: "progress", text: "Writing the summary." }],
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
it("returns the latest failed run so the client can show its error", async () => {
|
it("returns the latest failed run so the client can show its error", async () => {
|
||||||
const run = {
|
const run = {
|
||||||
id: "run-failed",
|
id: "run-failed",
|
||||||
@ -961,10 +1032,13 @@ function groupTarget() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("sendThreadMessage", () => {
|
describe("sendThreadMessage", () => {
|
||||||
it("rejects a new bot message while a run is waiting on input", async () => {
|
it("steers a waiting-input run instead of refusing the message", async () => {
|
||||||
const tx = {
|
const tx = {
|
||||||
thread: {
|
thread: {
|
||||||
update: vi.fn().mockResolvedValue({ nextMessageSeq: 2 }),
|
update: vi
|
||||||
|
.fn()
|
||||||
|
.mockResolvedValueOnce({ nextMessageSeq: 2 })
|
||||||
|
.mockResolvedValueOnce({ nextEventSeq: 3 }),
|
||||||
},
|
},
|
||||||
message: {
|
message: {
|
||||||
create: vi.fn().mockResolvedValue({
|
create: vi.fn().mockResolvedValue({
|
||||||
@ -972,7 +1046,7 @@ describe("sendThreadMessage", () => {
|
|||||||
threadId: "thread-1",
|
threadId: "thread-1",
|
||||||
seq: 1,
|
seq: 1,
|
||||||
role: "user",
|
role: "user",
|
||||||
blocks: [{ kind: "text", text: "hi" }],
|
blocks: [{ kind: "text", text: "more context" }],
|
||||||
botId: null,
|
botId: null,
|
||||||
replyToMessageId: null,
|
replyToMessageId: null,
|
||||||
runId: null,
|
runId: null,
|
||||||
@ -984,9 +1058,13 @@ describe("sendThreadMessage", () => {
|
|||||||
findMany: vi
|
findMany: vi
|
||||||
.fn()
|
.fn()
|
||||||
.mockResolvedValue([{ id: "run-waiting", taskId: "task-1", status: "waiting_input" }]),
|
.mockResolvedValue([{ id: "run-waiting", taskId: "task-1", status: "waiting_input" }]),
|
||||||
|
findUnique: vi.fn().mockResolvedValue({
|
||||||
|
status: "waiting_input",
|
||||||
|
startedAt: new Date(),
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
steeringMessage: { create: vi.fn() },
|
steeringMessage: { create: vi.fn() },
|
||||||
event: { create: vi.fn() },
|
event: { create: vi.fn().mockResolvedValue({ seq: 2 }) },
|
||||||
task: { create: vi.fn() },
|
task: { create: vi.fn() },
|
||||||
};
|
};
|
||||||
const prisma = {
|
const prisma = {
|
||||||
@ -1000,26 +1078,37 @@ describe("sendThreadMessage", () => {
|
|||||||
threadId: "thread-1",
|
threadId: "thread-1",
|
||||||
bot: { computer: null },
|
bot: { computer: null },
|
||||||
} as ThreadTarget;
|
} as ThreadTarget;
|
||||||
|
const enqueue = vi.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
sendThreadMessage(
|
sendThreadMessage(
|
||||||
{
|
{
|
||||||
prisma,
|
prisma,
|
||||||
events: { notify: vi.fn() } as never,
|
events: { notify: vi.fn().mockResolvedValue(undefined) } as never,
|
||||||
jobs: { enqueue: vi.fn() } as never,
|
jobs: { enqueue } as never,
|
||||||
},
|
},
|
||||||
actor,
|
actor,
|
||||||
target,
|
target,
|
||||||
{
|
{
|
||||||
text: "hi",
|
text: "more context",
|
||||||
clientNonce: "nonce-1",
|
clientNonce: "nonce-1",
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
).rejects.toMatchObject({
|
).resolves.toMatchObject({
|
||||||
code: "CONFLICT",
|
runId: "run-waiting",
|
||||||
message: "Answer the pending ask first.",
|
taskId: "task-1",
|
||||||
|
seq: 1,
|
||||||
|
runIds: ["run-waiting"],
|
||||||
});
|
});
|
||||||
expect(tx.steeringMessage.create).not.toHaveBeenCalled();
|
expect(tx.steeringMessage.create).toHaveBeenCalledWith({
|
||||||
|
data: {
|
||||||
|
messageId: "msg-1",
|
||||||
|
botId: "bot-1",
|
||||||
|
userId: "user-1",
|
||||||
|
runId: "run-waiting",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(enqueue).toHaveBeenCalledWith(expect.objectContaining({ name: "run.continue" }));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("steers a waiting-takeover run instead of refusing the message", async () => {
|
it("steers a waiting-takeover run instead of refusing the message", async () => {
|
||||||
@ -2031,4 +2120,192 @@ describe("stopThreadRuns", () => {
|
|||||||
expect.objectContaining({ runId: "run-b" }),
|
expect.objectContaining({ runId: "run-b" }),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("sendQueuedThreadMessagesNow cancels the active run, preserves pending steering, and enqueues an immediate continuation run", async () => {
|
||||||
|
const queuedCreatedAt = new Date("2026-01-01T10:00:05.000Z");
|
||||||
|
const transaction = {
|
||||||
|
$queryRaw: vi.fn(),
|
||||||
|
steeringMessage: {
|
||||||
|
findMany: vi.fn().mockResolvedValue([
|
||||||
|
{
|
||||||
|
id: "steer-1",
|
||||||
|
botId: "bot-a",
|
||||||
|
userId: "user-1",
|
||||||
|
messageId: "msg-queued-1",
|
||||||
|
claimedAt: null,
|
||||||
|
message: {
|
||||||
|
id: "msg-queued-1",
|
||||||
|
blocks: [{ kind: "text", text: "Send this right now" }],
|
||||||
|
seq: 2,
|
||||||
|
createdAt: queuedCreatedAt,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
deleteMany: vi.fn().mockResolvedValue({ count: 0 }),
|
||||||
|
updateMany: vi.fn().mockResolvedValue({ count: 1 }),
|
||||||
|
},
|
||||||
|
run: {
|
||||||
|
updateManyAndReturn: vi.fn().mockResolvedValue([{ id: "run-old", taskId: "task-old" }]),
|
||||||
|
create: vi.fn().mockResolvedValue({ id: "run-new", status: "queued" }),
|
||||||
|
},
|
||||||
|
task: {
|
||||||
|
updateMany: vi.fn().mockResolvedValue({ count: 1 }),
|
||||||
|
create: vi.fn().mockResolvedValue({ id: "task-new" }),
|
||||||
|
},
|
||||||
|
message: {
|
||||||
|
updateMany: vi.fn().mockResolvedValue({ count: 1 }),
|
||||||
|
},
|
||||||
|
computerExecutionLease: {
|
||||||
|
findMany: vi.fn().mockResolvedValue([]),
|
||||||
|
},
|
||||||
|
computer: {
|
||||||
|
findMany: vi.fn().mockResolvedValue([]),
|
||||||
|
},
|
||||||
|
event: {
|
||||||
|
findFirst: vi.fn().mockResolvedValue({ seq: 42 }),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const prisma = {
|
||||||
|
$transaction: vi.fn(async (callback: (client: typeof transaction) => unknown) =>
|
||||||
|
callback(transaction),
|
||||||
|
),
|
||||||
|
computerExecutionLease: {
|
||||||
|
updateMany: vi.fn().mockResolvedValue({ count: 0 }),
|
||||||
|
},
|
||||||
|
computer: {
|
||||||
|
updateMany: vi.fn().mockResolvedValue({ count: 0 }),
|
||||||
|
},
|
||||||
|
event: {
|
||||||
|
deleteMany: vi.fn().mockResolvedValue({ count: 0 }),
|
||||||
|
},
|
||||||
|
} as unknown as PrismaClient;
|
||||||
|
const jobs = {
|
||||||
|
enqueue: vi.fn().mockResolvedValue(undefined),
|
||||||
|
cancel: vi.fn().mockResolvedValue(undefined),
|
||||||
|
};
|
||||||
|
const events = {
|
||||||
|
notify: vi.fn().mockResolvedValue(undefined),
|
||||||
|
};
|
||||||
|
const actor = { spaceId: "workspace-1", userId: "user-1" } as Actor;
|
||||||
|
const target = {
|
||||||
|
kind: "bot",
|
||||||
|
botId: "bot-a",
|
||||||
|
threadId: "thread-1",
|
||||||
|
bot: {} as never,
|
||||||
|
} satisfies ThreadTarget;
|
||||||
|
|
||||||
|
const result = await sendQueuedThreadMessagesNow(
|
||||||
|
{
|
||||||
|
prisma,
|
||||||
|
sandbox: {} as SandboxProvider,
|
||||||
|
events: events as never,
|
||||||
|
jobs: jobs as never,
|
||||||
|
},
|
||||||
|
actor,
|
||||||
|
target,
|
||||||
|
{ messageId: "msg-queued-1" },
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toEqual({ ok: true, runIds: ["run-new"] });
|
||||||
|
expect(transaction.run.updateManyAndReturn).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
where: expect.objectContaining({
|
||||||
|
threadId: "thread-1",
|
||||||
|
botId: { in: ["bot-a"] },
|
||||||
|
}),
|
||||||
|
data: expect.objectContaining({ status: "cancelled" }),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
expect(transaction.steeringMessage.updateMany).toHaveBeenCalledWith({
|
||||||
|
where: { id: { in: ["steer-1"] } },
|
||||||
|
data: { runId: "run-new", claimedAt: null },
|
||||||
|
});
|
||||||
|
expect(jobs.cancel).toHaveBeenCalledWith("run:run-old");
|
||||||
|
expect(jobs.enqueue).toHaveBeenCalledWith({
|
||||||
|
name: "run.continue",
|
||||||
|
payload: { runId: "run-new" },
|
||||||
|
replaceKey: "run:run-new",
|
||||||
|
});
|
||||||
|
expect(events.notify).toHaveBeenCalledWith("thread-1", 42);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("resumeThreadRuns", () => {
|
||||||
|
it("re-queues the latest cancelled or failed run without creating a duplicate message", async () => {
|
||||||
|
const transaction = {
|
||||||
|
$queryRaw: vi.fn().mockResolvedValue([{ id: "thread-1" }]),
|
||||||
|
run: {
|
||||||
|
findMany: vi
|
||||||
|
.fn()
|
||||||
|
.mockResolvedValueOnce([])
|
||||||
|
.mockResolvedValueOnce([
|
||||||
|
{
|
||||||
|
id: "run-cancelled",
|
||||||
|
taskId: "task-cancelled",
|
||||||
|
botId: "bot-a",
|
||||||
|
status: "cancelled",
|
||||||
|
createdAt: new Date("2026-04-20T10:00:00.000Z"),
|
||||||
|
completedAt: new Date("2026-04-20T10:02:00.000Z"),
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
update: vi.fn().mockResolvedValue({}),
|
||||||
|
},
|
||||||
|
task: {
|
||||||
|
updateMany: vi.fn().mockResolvedValue({ count: 1 }),
|
||||||
|
},
|
||||||
|
event: {
|
||||||
|
findFirst: vi.fn().mockResolvedValue({ seq: 19 }),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const prisma = {
|
||||||
|
$transaction: vi.fn(async (fn: (tx: typeof transaction) => Promise<unknown>) =>
|
||||||
|
fn(transaction),
|
||||||
|
),
|
||||||
|
} as unknown as PrismaClient;
|
||||||
|
const jobs = {
|
||||||
|
enqueue: vi.fn().mockResolvedValue(undefined),
|
||||||
|
};
|
||||||
|
const events = {
|
||||||
|
notify: vi.fn().mockResolvedValue(undefined),
|
||||||
|
};
|
||||||
|
const actor = { spaceId: "workspace-1", userId: "user-1" } as Actor;
|
||||||
|
const target = {
|
||||||
|
kind: "bot",
|
||||||
|
botId: "bot-a",
|
||||||
|
threadId: "thread-1",
|
||||||
|
bot: {} as never,
|
||||||
|
} satisfies ThreadTarget;
|
||||||
|
|
||||||
|
const result = await resumeThreadRuns(
|
||||||
|
{
|
||||||
|
prisma,
|
||||||
|
events: events as never,
|
||||||
|
jobs: jobs as never,
|
||||||
|
},
|
||||||
|
actor,
|
||||||
|
target,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toEqual({ ok: true, runIds: ["run-cancelled"] });
|
||||||
|
expect(transaction.task.updateMany).toHaveBeenCalledWith({
|
||||||
|
where: { id: "task-cancelled" },
|
||||||
|
data: { status: "queued" },
|
||||||
|
});
|
||||||
|
expect(transaction.run.update).toHaveBeenCalledWith({
|
||||||
|
where: { id: "run-cancelled" },
|
||||||
|
data: {
|
||||||
|
status: "queued",
|
||||||
|
error: null,
|
||||||
|
completedAt: null,
|
||||||
|
leaseOwner: null,
|
||||||
|
leaseExpiresAt: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(jobs.enqueue).toHaveBeenCalledWith({
|
||||||
|
name: "run.continue",
|
||||||
|
payload: { runId: "run-cancelled" },
|
||||||
|
replaceKey: "run:run-cancelled",
|
||||||
|
});
|
||||||
|
expect(events.notify).toHaveBeenCalledWith("thread-1", 19);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,5 +1,10 @@
|
|||||||
import { ORPCError } from "@orpc/server";
|
import { ORPCError } from "@orpc/server";
|
||||||
import { type JobPublisher, runContinueJob, type SandboxProvider } from "@rakazo/adapter-kit";
|
import {
|
||||||
|
type JobPublisher,
|
||||||
|
runContinueJob,
|
||||||
|
runJobKey,
|
||||||
|
type SandboxProvider,
|
||||||
|
} from "@rakazo/adapter-kit";
|
||||||
import { cancelComputerRunWork, screenLeaseIdForRun, toComputerRef } from "@rakazo/adapters";
|
import { cancelComputerRunWork, screenLeaseIdForRun, toComputerRef } from "@rakazo/adapters";
|
||||||
import {
|
import {
|
||||||
type Actor,
|
type Actor,
|
||||||
@ -160,9 +165,15 @@ function quoteAppearsInBlocks(quote: string, blocks: MessageBlock[]): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const THREAD_MESSAGE_PAGE_SIZE = 100;
|
const THREAD_MESSAGE_PAGE_SIZE = 100;
|
||||||
const RUNS_NEEDING_CONTINUE = new Set(["queued", "waiting_takeover"]);
|
const RUNS_NEEDING_CONTINUE = new Set(["queued", "waiting_input", "waiting_takeover"]);
|
||||||
|
|
||||||
const STEERABLE_RUN_STATUSES = new Set(["queued", "leased", "running", "waiting_takeover"]);
|
const STEERABLE_RUN_STATUSES = new Set([
|
||||||
|
"queued",
|
||||||
|
"leased",
|
||||||
|
"running",
|
||||||
|
"waiting_input",
|
||||||
|
"waiting_takeover",
|
||||||
|
]);
|
||||||
|
|
||||||
type MentionTargetInput = string | { kind: "bot" | "group" | "routine" | "connector"; id: string };
|
type MentionTargetInput = string | { kind: "bot" | "group" | "routine" | "connector"; id: string };
|
||||||
|
|
||||||
@ -491,6 +502,7 @@ export async function threadSnapshot(
|
|||||||
runId: currentRun.id,
|
runId: currentRun.id,
|
||||||
type: {
|
type: {
|
||||||
in: [
|
in: [
|
||||||
|
"thread.message.created",
|
||||||
"thread.progress",
|
"thread.progress",
|
||||||
"thread.subagent",
|
"thread.subagent",
|
||||||
"agent.tool.called",
|
"agent.tool.called",
|
||||||
@ -556,6 +568,7 @@ export async function threadSnapshot(
|
|||||||
runId: { in: activeRuns.map((run) => run.id) },
|
runId: { in: activeRuns.map((run) => run.id) },
|
||||||
type: {
|
type: {
|
||||||
in: [
|
in: [
|
||||||
|
"thread.message.created",
|
||||||
"thread.progress",
|
"thread.progress",
|
||||||
"thread.subagent",
|
"thread.subagent",
|
||||||
"agent.tool.called",
|
"agent.tool.called",
|
||||||
@ -631,8 +644,12 @@ function messagesWithLiveEvents(
|
|||||||
persisted: ThreadSnapshot["messages"],
|
persisted: ThreadSnapshot["messages"],
|
||||||
liveEvents: Parameters<typeof projectMessages>[0],
|
liveEvents: Parameters<typeof projectMessages>[0],
|
||||||
) {
|
) {
|
||||||
|
// Message events only clear the live text they promoted; the persisted rows already carry them.
|
||||||
const live = projectMessages(liveEvents).filter((message) => {
|
const live = projectMessages(liveEvents).filter((message) => {
|
||||||
if (message.blocks.some((block) => block.kind === "progress" || block.kind === "steps")) {
|
if (
|
||||||
|
message.id.startsWith("progress:") &&
|
||||||
|
message.blocks.some((block) => block.kind === "progress" || block.kind === "steps")
|
||||||
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!message.id.startsWith("subagent:")) return false;
|
if (!message.id.startsWith("subagent:")) return false;
|
||||||
@ -1139,63 +1156,50 @@ export async function reactToThreadMessage(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function stopThreadRuns(
|
async function snapshotCancelledRunTeardown(tx: Prisma.TransactionClient, runIds: string[]) {
|
||||||
|
// Snapshot teardown coordinates before commit. Once cancellation becomes
|
||||||
|
// visible, a worker can release its lease / execution columns immediately; a
|
||||||
|
// later lookup would then miss the sandbox work this request must stop.
|
||||||
|
// Team ownership lives on ComputerExecutionLease; Computer.executionRunId is
|
||||||
|
// only a legacy secondary path and is not written by current acquisition.
|
||||||
|
const leases = runIds.length
|
||||||
|
? await tx.computerExecutionLease.findMany({
|
||||||
|
where: { runId: { in: runIds } },
|
||||||
|
select: { computerId: true, botId: true, runId: true, fence: true },
|
||||||
|
})
|
||||||
|
: [];
|
||||||
|
const leaseComputerIds = [...new Set(leases.map((lease) => lease.computerId))];
|
||||||
|
const computers = runIds.length
|
||||||
|
? await tx.computer.findMany({
|
||||||
|
where:
|
||||||
|
leaseComputerIds.length > 0
|
||||||
|
? {
|
||||||
|
OR: [{ id: { in: leaseComputerIds } }, { executionRunId: { in: runIds } }],
|
||||||
|
}
|
||||||
|
: { executionRunId: { in: runIds } },
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
homeKey: true,
|
||||||
|
kind: true,
|
||||||
|
providerRef: true,
|
||||||
|
executionBotId: true,
|
||||||
|
executionRunId: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
: [];
|
||||||
|
return { runIds, computers, leases };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function teardownCancelledRuns(
|
||||||
deps: {
|
deps: {
|
||||||
prisma: PrismaClient;
|
prisma: PrismaClient;
|
||||||
sandbox: SandboxProvider;
|
sandbox: SandboxProvider;
|
||||||
},
|
},
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
target: ThreadTarget,
|
snapshot: Awaited<ReturnType<typeof snapshotCancelledRunTeardown>>,
|
||||||
) {
|
) {
|
||||||
const { runIds, computers, leases } = await deps.prisma.$transaction(async (tx) => {
|
const { runIds, computers, leases } = snapshot;
|
||||||
await tx.$queryRaw`SELECT id FROM threads WHERE id = ${target.threadId} FOR UPDATE`;
|
if (runIds.length === 0) return;
|
||||||
const cancelled = await tx.run.updateManyAndReturn({
|
|
||||||
where: {
|
|
||||||
threadId: target.threadId,
|
|
||||||
status: { in: [...ACTIVE_RUN_STATUSES] },
|
|
||||||
},
|
|
||||||
data: { status: "cancelled", completedAt: new Date() },
|
|
||||||
select: { id: true },
|
|
||||||
});
|
|
||||||
const ids = cancelled.map((run) => run.id);
|
|
||||||
await tx.steeringMessage.deleteMany({
|
|
||||||
where: {
|
|
||||||
botId: { in: target.kind === "bot" ? [target.botId] : target.memberBotIds },
|
|
||||||
message: { threadId: target.threadId },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
// Snapshot teardown coordinates before commit. Once cancellation becomes
|
|
||||||
// visible, a worker can release its lease / execution columns immediately; a
|
|
||||||
// later lookup would then miss the sandbox work this request must stop.
|
|
||||||
// Team ownership lives on ComputerExecutionLease; Computer.executionRunId is
|
|
||||||
// only a legacy secondary path and is not written by current acquisition.
|
|
||||||
const leases = ids.length
|
|
||||||
? await tx.computerExecutionLease.findMany({
|
|
||||||
where: { runId: { in: ids } },
|
|
||||||
select: { computerId: true, botId: true, runId: true, fence: true },
|
|
||||||
})
|
|
||||||
: [];
|
|
||||||
const leaseComputerIds = [...new Set(leases.map((lease) => lease.computerId))];
|
|
||||||
const computers = ids.length
|
|
||||||
? await tx.computer.findMany({
|
|
||||||
where:
|
|
||||||
leaseComputerIds.length > 0
|
|
||||||
? {
|
|
||||||
OR: [{ id: { in: leaseComputerIds } }, { executionRunId: { in: ids } }],
|
|
||||||
}
|
|
||||||
: { executionRunId: { in: ids } },
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
homeKey: true,
|
|
||||||
kind: true,
|
|
||||||
providerRef: true,
|
|
||||||
executionBotId: true,
|
|
||||||
executionRunId: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
: [];
|
|
||||||
return { runIds: ids, computers, leases };
|
|
||||||
});
|
|
||||||
// Keep the DB lease until after teardown so a replacement run cannot claim the
|
// Keep the DB lease until after teardown so a replacement run cannot claim the
|
||||||
// screen while we still need the cancelled run's screenLeaseId to release it.
|
// screen while we still need the cancelled run's screenLeaseId to release it.
|
||||||
const computerById = new Map(computers.map((computer) => [computer.id, computer]));
|
const computerById = new Map(computers.map((computer) => [computer.id, computer]));
|
||||||
@ -1273,6 +1277,381 @@ export async function stopThreadRuns(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function stopThreadRuns(
|
||||||
|
deps: {
|
||||||
|
prisma: PrismaClient;
|
||||||
|
sandbox: SandboxProvider;
|
||||||
|
},
|
||||||
|
actor: Actor,
|
||||||
|
target: ThreadTarget,
|
||||||
|
) {
|
||||||
|
const snapshot = await deps.prisma.$transaction(async (tx) => {
|
||||||
|
await tx.$queryRaw`SELECT id FROM threads WHERE id = ${target.threadId} FOR UPDATE`;
|
||||||
|
const cancelled = await tx.run.updateManyAndReturn({
|
||||||
|
where: {
|
||||||
|
threadId: target.threadId,
|
||||||
|
status: { in: [...ACTIVE_RUN_STATUSES] },
|
||||||
|
},
|
||||||
|
data: { status: "cancelled", completedAt: new Date() },
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
const ids = cancelled.map((run) => run.id);
|
||||||
|
await tx.steeringMessage.deleteMany({
|
||||||
|
where: {
|
||||||
|
botId: { in: target.kind === "bot" ? [target.botId] : target.memberBotIds },
|
||||||
|
message: { threadId: target.threadId },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return snapshotCancelledRunTeardown(tx, ids);
|
||||||
|
});
|
||||||
|
await teardownCancelledRuns(deps, actor, snapshot);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resume the latest stopped (`cancelled`) or interrupted (`failed`) run in the thread
|
||||||
|
* without creating a duplicate user message row.
|
||||||
|
*/
|
||||||
|
export async function resumeThreadRuns(
|
||||||
|
deps: {
|
||||||
|
prisma: PrismaClient;
|
||||||
|
events: ThreadEvents;
|
||||||
|
jobs: JobPublisher;
|
||||||
|
},
|
||||||
|
actor: Actor,
|
||||||
|
target: ThreadTarget,
|
||||||
|
) {
|
||||||
|
const botIds = target.kind === "bot" ? [target.botId] : target.memberBotIds;
|
||||||
|
const committed = await deps.prisma.$transaction(async (tx) => {
|
||||||
|
await tx.$queryRaw`SELECT id FROM threads WHERE id = ${target.threadId} FOR UPDATE`;
|
||||||
|
const activeRuns = await tx.run.findMany({
|
||||||
|
where: {
|
||||||
|
threadId: target.threadId,
|
||||||
|
botId: { in: botIds },
|
||||||
|
status: { in: [...ACTIVE_RUN_STATUSES] },
|
||||||
|
},
|
||||||
|
select: { id: true, status: true },
|
||||||
|
});
|
||||||
|
if (activeRuns.length > 0) {
|
||||||
|
return {
|
||||||
|
runs: activeRuns,
|
||||||
|
eventSeq: null as number | null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const recentTerminals = await tx.run.findMany({
|
||||||
|
where: {
|
||||||
|
threadId: target.threadId,
|
||||||
|
botId: { in: botIds },
|
||||||
|
trigger: { not: "bot_message" },
|
||||||
|
status: { in: ["failed", "completed", "cancelled"] },
|
||||||
|
},
|
||||||
|
orderBy: [{ updatedAt: "desc" }, { id: "desc" }],
|
||||||
|
take: 20,
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
botId: true,
|
||||||
|
taskId: true,
|
||||||
|
status: true,
|
||||||
|
createdAt: true,
|
||||||
|
completedAt: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const latestTerminal = pickLatestTerminalRun(recentTerminals);
|
||||||
|
if (
|
||||||
|
!latestTerminal ||
|
||||||
|
(latestTerminal.status !== "cancelled" && latestTerminal.status !== "failed")
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
runs: [] as Array<{ id: string; status: string }>,
|
||||||
|
eventSeq: null as number | null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
await tx.task.updateMany({
|
||||||
|
where: { id: latestTerminal.taskId },
|
||||||
|
data: { status: "queued" },
|
||||||
|
});
|
||||||
|
await tx.run.update({
|
||||||
|
where: { id: latestTerminal.id },
|
||||||
|
data: {
|
||||||
|
status: "queued",
|
||||||
|
error: null,
|
||||||
|
completedAt: null,
|
||||||
|
leaseOwner: null,
|
||||||
|
leaseExpiresAt: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (target.kind === "group") {
|
||||||
|
await touchGroupUpdatedAt(tx, target.groupId);
|
||||||
|
}
|
||||||
|
const latestEvent = await tx.event.findFirst({
|
||||||
|
where: { threadId: target.threadId },
|
||||||
|
orderBy: { seq: "desc" },
|
||||||
|
select: { seq: true },
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
runs: [{ id: latestTerminal.id, status: "queued" }],
|
||||||
|
eventSeq: latestEvent?.seq ?? null,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
if (committed.eventSeq !== null) {
|
||||||
|
await deps.events.notify(target.threadId, committed.eventSeq).catch((error) => {
|
||||||
|
getLogger().error("thread resume realtime notification", error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
await enqueueRunsNeedingContinue(deps.jobs, committed.runs);
|
||||||
|
return {
|
||||||
|
ok: true as const,
|
||||||
|
runIds: committed.runs.map((run) => run.id),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Promote queued follow-up messages immediately: cancel any currently active runs
|
||||||
|
* holding up the queue without discarding unclaimed steering messages, create a
|
||||||
|
* new continuation run for the pending steering messages, and enqueue it right away.
|
||||||
|
*/
|
||||||
|
export async function sendQueuedThreadMessagesNow(
|
||||||
|
deps: {
|
||||||
|
prisma: PrismaClient;
|
||||||
|
sandbox: SandboxProvider;
|
||||||
|
events: ThreadEvents;
|
||||||
|
jobs: JobPublisher;
|
||||||
|
},
|
||||||
|
actor: Actor,
|
||||||
|
target: ThreadTarget,
|
||||||
|
input?: { messageId?: string },
|
||||||
|
) {
|
||||||
|
const botIds = target.kind === "bot" ? [target.botId] : target.memberBotIds;
|
||||||
|
const committed = await deps.prisma.$transaction(async (tx) => {
|
||||||
|
await tx.$queryRaw`SELECT id FROM threads WHERE id = ${target.threadId} FOR UPDATE`;
|
||||||
|
const pendingSteering = await tx.steeringMessage.findMany({
|
||||||
|
where: {
|
||||||
|
botId: { in: botIds },
|
||||||
|
claimedAt: null,
|
||||||
|
message: { threadId: target.threadId },
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
message: { select: { id: true, blocks: true, seq: true, createdAt: true } },
|
||||||
|
},
|
||||||
|
orderBy: [{ message: { seq: "asc" } }, { id: "asc" }],
|
||||||
|
});
|
||||||
|
|
||||||
|
const pendingByBotId = new Map<string, typeof pendingSteering>();
|
||||||
|
for (const item of pendingSteering) {
|
||||||
|
const list = pendingByBotId.get(item.botId) ?? [];
|
||||||
|
list.push(item);
|
||||||
|
pendingByBotId.set(item.botId, list);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pendingByBotId.size === 0 && input?.messageId) {
|
||||||
|
const targetMsg = await tx.message.findFirst({
|
||||||
|
where: { id: input.messageId, threadId: target.threadId, role: "user" },
|
||||||
|
select: { id: true, blocks: true, seq: true, createdAt: true },
|
||||||
|
});
|
||||||
|
if (targetMsg) {
|
||||||
|
const activeRuns = await tx.run.findMany({
|
||||||
|
where: {
|
||||||
|
threadId: target.threadId,
|
||||||
|
botId: { in: botIds },
|
||||||
|
status: { in: [...ACTIVE_RUN_STATUSES] },
|
||||||
|
createdAt: { lt: targetMsg.createdAt },
|
||||||
|
},
|
||||||
|
select: { id: true, botId: true },
|
||||||
|
});
|
||||||
|
const fallbackBotId =
|
||||||
|
activeRuns[0]?.botId ??
|
||||||
|
(target.kind === "bot" ? target.botId : groupDefaultBotId(target));
|
||||||
|
if (fallbackBotId && activeRuns.length > 0) {
|
||||||
|
const steering = await tx.steeringMessage.create({
|
||||||
|
data: {
|
||||||
|
messageId: targetMsg.id,
|
||||||
|
botId: fallbackBotId,
|
||||||
|
userId: actor.userId,
|
||||||
|
runId: activeRuns[0]?.id ?? null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
pendingByBotId.set(fallbackBotId, [{ ...steering, message: targetMsg }]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pendingByBotId.size === 0) {
|
||||||
|
return {
|
||||||
|
teardown: { runIds: [] as string[], computers: [], leases: [] },
|
||||||
|
runs: [] as Array<{ id: string; status: string }>,
|
||||||
|
eventSeq: null as number | null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const affectedBotIds = [...pendingByBotId.keys()];
|
||||||
|
const now = new Date();
|
||||||
|
const cancelled = await tx.run.updateManyAndReturn({
|
||||||
|
where: {
|
||||||
|
threadId: target.threadId,
|
||||||
|
botId: { in: affectedBotIds },
|
||||||
|
status: { in: [...ACTIVE_RUN_STATUSES] },
|
||||||
|
},
|
||||||
|
data: { status: "cancelled", completedAt: now },
|
||||||
|
select: { id: true, taskId: true },
|
||||||
|
});
|
||||||
|
const cancelledRunIds = cancelled.map((run) => run.id);
|
||||||
|
if (cancelled.length > 0) {
|
||||||
|
await tx.task.updateMany({
|
||||||
|
where: { id: { in: cancelled.map((run) => run.taskId) } },
|
||||||
|
data: { status: "cancelled" },
|
||||||
|
});
|
||||||
|
await tx.steeringMessage.deleteMany({
|
||||||
|
where: {
|
||||||
|
runId: { in: cancelledRunIds },
|
||||||
|
claimedAt: { not: null },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const teardown = await snapshotCancelledRunTeardown(tx, cancelledRunIds);
|
||||||
|
|
||||||
|
const createdRuns: Array<{ id: string; status: string }> = [];
|
||||||
|
for (const [botId, items] of pendingByBotId) {
|
||||||
|
const last = items.at(-1)!;
|
||||||
|
const task = await tx.task.create({
|
||||||
|
data: {
|
||||||
|
spaceId: actor.spaceId,
|
||||||
|
botId,
|
||||||
|
threadId: target.threadId,
|
||||||
|
userId: items[0]!.userId,
|
||||||
|
prompt: "Respond to the user's steering context.",
|
||||||
|
status: "queued",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const runCreatedAt = new Date(Math.max(Date.now(), last.message.createdAt.getTime() + 1));
|
||||||
|
const run = await tx.run.create({
|
||||||
|
data: {
|
||||||
|
spaceId: actor.spaceId,
|
||||||
|
botId,
|
||||||
|
threadId: target.threadId,
|
||||||
|
taskId: task.id,
|
||||||
|
userId: items[0]!.userId,
|
||||||
|
status: "queued",
|
||||||
|
trigger: "follow_up",
|
||||||
|
sourceMessageId: last.message.id,
|
||||||
|
createdAt: runCreatedAt,
|
||||||
|
},
|
||||||
|
select: { id: true, status: true },
|
||||||
|
});
|
||||||
|
await tx.steeringMessage.updateMany({
|
||||||
|
where: { id: { in: items.map((item) => item.id) } },
|
||||||
|
data: { runId: run.id, claimedAt: null },
|
||||||
|
});
|
||||||
|
await tx.message.updateMany({
|
||||||
|
where: { id: { in: items.map((item) => item.message.id) } },
|
||||||
|
data: { runId: run.id },
|
||||||
|
});
|
||||||
|
createdRuns.push(run);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (target.kind === "group") {
|
||||||
|
await touchGroupUpdatedAt(tx, target.groupId);
|
||||||
|
}
|
||||||
|
const latestEvent = await tx.event.findFirst({
|
||||||
|
where: { threadId: target.threadId },
|
||||||
|
orderBy: { seq: "desc" },
|
||||||
|
select: { seq: true },
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
teardown,
|
||||||
|
runs: createdRuns,
|
||||||
|
eventSeq: latestEvent?.seq ?? null,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
if (committed.teardown.runIds.length > 0) {
|
||||||
|
await Promise.all([
|
||||||
|
teardownCancelledRuns(deps, actor, committed.teardown),
|
||||||
|
...committed.teardown.runIds.map((runId) =>
|
||||||
|
deps.jobs.cancel(runJobKey(runId)).catch(() => undefined),
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
if (committed.runs.length > 0) {
|
||||||
|
await enqueueRunsNeedingContinue(deps.jobs, committed.runs);
|
||||||
|
if (committed.eventSeq != null) {
|
||||||
|
await deps.events.notify(target.threadId, committed.eventSeq).catch((error) => {
|
||||||
|
getLogger().error("thread sendNow realtime notification", error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
ok: true as const,
|
||||||
|
runIds: committed.runs.map((run) => run.id),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function removeQueuedMessage(
|
||||||
|
deps: {
|
||||||
|
prisma: PrismaClient;
|
||||||
|
events: ThreadEvents;
|
||||||
|
},
|
||||||
|
actor: Actor,
|
||||||
|
target: ThreadTarget,
|
||||||
|
messageId: string,
|
||||||
|
) {
|
||||||
|
const botIds = target.kind === "bot" ? [target.botId] : target.memberBotIds;
|
||||||
|
const result = await deps.prisma.$transaction(async (tx) => {
|
||||||
|
// Verify the message exists and belongs to this thread
|
||||||
|
const message = await tx.message.findFirst({
|
||||||
|
where: {
|
||||||
|
id: messageId,
|
||||||
|
threadId: target.threadId,
|
||||||
|
role: "user",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!message) {
|
||||||
|
return { removed: false as const };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if this message is queued (has an unclaimed steering message)
|
||||||
|
const steering = await tx.steeringMessage.findFirst({
|
||||||
|
where: {
|
||||||
|
messageId: messageId,
|
||||||
|
botId: { in: botIds },
|
||||||
|
claimedAt: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!steering) {
|
||||||
|
// No queued steering entry — nothing to remove
|
||||||
|
return { removed: false as const };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete the steering message (unclaims it from the queue)
|
||||||
|
await tx.steeringMessage.delete({
|
||||||
|
where: { id: steering.id },
|
||||||
|
});
|
||||||
|
|
||||||
|
// Also clean up the user message if it's exclusively queued
|
||||||
|
// (no run attached to it)
|
||||||
|
if (!message.runId) {
|
||||||
|
await tx.message.delete({
|
||||||
|
where: { id: messageId },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return { removed: true as const };
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.removed) {
|
||||||
|
// Notify subscribers so the UI updates
|
||||||
|
const latest = await deps.prisma.event.findFirst({
|
||||||
|
where: { threadId: target.threadId },
|
||||||
|
orderBy: { seq: "desc" },
|
||||||
|
select: { seq: true },
|
||||||
|
});
|
||||||
|
await deps.events.notify(target.threadId, latest?.seq ?? 0).catch(() => undefined);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ok: true as const, removed: result.removed };
|
||||||
|
}
|
||||||
export async function setThreadUnreadState(
|
export async function setThreadUnreadState(
|
||||||
prisma: PrismaClient,
|
prisma: PrismaClient,
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
import { ORPCError } from "@orpc/server";
|
import { ORPCError } from "@orpc/server";
|
||||||
|
import type { CoachRhythmDeps } from "@rakazo/adapters";
|
||||||
|
import { syncCoachTimeZone } from "@rakazo/adapters";
|
||||||
import type {
|
import type {
|
||||||
Actor,
|
Actor,
|
||||||
ForgetUserFacts,
|
ForgetUserFacts,
|
||||||
@ -29,32 +31,32 @@ export function getProfile(prisma: PrismaClient, actor: Actor): Promise<UserProf
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function updateProfile(
|
export async function updateProfile(
|
||||||
prisma: PrismaClient,
|
deps: CoachRhythmDeps,
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
input: UpdateUserProfile,
|
input: UpdateUserProfile,
|
||||||
): Promise<UserProfileState> {
|
): Promise<UserProfileState> {
|
||||||
try {
|
try {
|
||||||
await updateUserProfile(prisma, actor.userId, input);
|
await updateUserProfile(deps.prisma, actor.userId, input);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw toRpcError(error);
|
throw toRpcError(error);
|
||||||
}
|
}
|
||||||
return getUserProfileState(prisma, actor.userId);
|
if (input.timeZone !== undefined) await syncCoachTimeZone(deps, actor.userId);
|
||||||
|
return getUserProfileState(deps.prisma, actor.userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Unknown ids are ignored, so a caller cannot probe another user's facts. */
|
/** Unknown ids are ignored, so a caller cannot probe another user's facts. */
|
||||||
export async function forgetProfile(
|
export async function forgetProfile(
|
||||||
prisma: PrismaClient,
|
deps: CoachRhythmDeps,
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
input: ForgetUserFacts,
|
input: ForgetUserFacts,
|
||||||
): Promise<UserProfileState> {
|
): Promise<UserProfileState> {
|
||||||
await forgetUserFacts(
|
const target = input.factIds
|
||||||
prisma,
|
? { factIds: input.factIds }
|
||||||
actor.userId,
|
: input.category
|
||||||
input.factIds
|
? { category: input.category }
|
||||||
? { factIds: input.factIds }
|
: { all: true as const };
|
||||||
: input.category
|
await forgetUserFacts(deps.prisma, actor.userId, target);
|
||||||
? { category: input.category }
|
// Forgetting everything clears the time zone too.
|
||||||
: { all: true },
|
if ("all" in target) await syncCoachTimeZone(deps, actor.userId);
|
||||||
);
|
return getUserProfileState(deps.prisma, actor.userId);
|
||||||
return getUserProfileState(prisma, actor.userId);
|
|
||||||
}
|
}
|
||||||
|
|||||||
91
apps/api/src/voice-transcribe.test.ts
Normal file
91
apps/api/src/voice-transcribe.test.ts
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
import type { Server } from "node:http";
|
||||||
|
import { createServer } from "node:http";
|
||||||
|
import type { AddressInfo } from "node:net";
|
||||||
|
import { NoVoiceConfigured } from "@rakazo/adapters";
|
||||||
|
import type { Actor } from "@rakazo/contracts";
|
||||||
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||||
|
import type { VoiceDeps } from "./voice.js";
|
||||||
|
import { toVoiceStatus, transcribeVoice } from "./voice.js";
|
||||||
|
|
||||||
|
const actor: Actor = {
|
||||||
|
userId: "user-1",
|
||||||
|
spaceId: "space-1",
|
||||||
|
email: "user@rakazo.test",
|
||||||
|
isDeploymentOwner: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
/** A user without a voice credential, on a deployment whose media settings are `settings`. */
|
||||||
|
function deps(settings: object | null, ciphertext: string): VoiceDeps {
|
||||||
|
return {
|
||||||
|
prisma: {
|
||||||
|
spaceVoicePreference: { findFirst: async () => null },
|
||||||
|
integrationProviderConfig: {
|
||||||
|
findUnique: async () => (settings ? { id: "media", ciphertext } : null),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
secrets: { load: () => JSON.stringify(settings) },
|
||||||
|
} as unknown as VoiceDeps;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("dictation through the self-hosted transcription server", () => {
|
||||||
|
let server: Server;
|
||||||
|
let baseUrl = "";
|
||||||
|
const uploads: string[] = [];
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
server = createServer((request, response) => {
|
||||||
|
const chunks: Buffer[] = [];
|
||||||
|
request.on("data", (chunk: Buffer) => chunks.push(chunk));
|
||||||
|
request.on("end", () => {
|
||||||
|
uploads.push(`${request.url} ${Buffer.concat(chunks).toString("latin1")}`);
|
||||||
|
response.setHeader("content-type", "application/json");
|
||||||
|
response.end(JSON.stringify({ text: " Remind me to call Ada. " }));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||||
|
baseUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}/v1`;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await new Promise((resolve) => server.close(resolve));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("transcribes when no voice provider can", async () => {
|
||||||
|
const result = await transcribeVoice(
|
||||||
|
deps({ comfyui: null, transcription: { baseUrl } }, "media-a"),
|
||||||
|
actor,
|
||||||
|
{ audio: new Uint8Array([1, 2, 3]), mimeType: "audio/webm;codecs=opus" },
|
||||||
|
);
|
||||||
|
expect(result).toEqual({ text: "Remind me to call Ada." });
|
||||||
|
expect(uploads[0]).toContain("/v1/audio/transcriptions");
|
||||||
|
expect(uploads[0]).toContain('filename="audio.webm"');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps asking for a voice key when no server is set up either", async () => {
|
||||||
|
await expect(
|
||||||
|
transcribeVoice(deps(null, "none"), actor, {
|
||||||
|
audio: new Uint8Array([1]),
|
||||||
|
mimeType: "audio/webm",
|
||||||
|
}),
|
||||||
|
).rejects.toBeInstanceOf(NoVoiceConfigured);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("refuses an empty recording before uploading it", async () => {
|
||||||
|
const before = uploads.length;
|
||||||
|
await expect(
|
||||||
|
transcribeVoice(deps({ comfyui: null, transcription: { baseUrl } }, "media-b"), actor, {
|
||||||
|
audio: new Uint8Array(),
|
||||||
|
mimeType: "audio/webm",
|
||||||
|
}),
|
||||||
|
).rejects.toThrow("That recording is empty or too large.");
|
||||||
|
expect(uploads).toHaveLength(before);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reports server dictation in the voice status", () => {
|
||||||
|
expect(toVoiceStatus(null).transcribe).toBe(false);
|
||||||
|
expect(toVoiceStatus(null, { mediaTranscription: true })).toMatchObject({
|
||||||
|
configured: false,
|
||||||
|
transcribe: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -13,18 +13,18 @@ import {
|
|||||||
|
|
||||||
describe("toVoiceStatus", () => {
|
describe("toVoiceStatus", () => {
|
||||||
it("treats a saved key without a voice as configured but not ready", () => {
|
it("treats a saved key without a voice as configured but not ready", () => {
|
||||||
expect(toVoiceStatus({ provider: "elevenlabs", voiceId: "" })).toEqual({
|
expect(toVoiceStatus({ provider: "kokoro", voiceId: "" })).toEqual({
|
||||||
configured: true,
|
configured: true,
|
||||||
ready: false,
|
ready: false,
|
||||||
transcribe: true,
|
transcribe: false,
|
||||||
provider: "elevenlabs",
|
provider: "kokoro",
|
||||||
voiceId: "",
|
voiceId: "",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("is ready once a voice is chosen", () => {
|
it("is ready once a voice is chosen", () => {
|
||||||
expect(toVoiceStatus({ provider: "cartesia", voiceId: "katie" }).ready).toBe(true);
|
expect(toVoiceStatus({ provider: "kokoro", voiceId: "df_eva" }).ready).toBe(true);
|
||||||
expect(toVoiceStatus({ provider: "cartesia", voiceId: "katie" }).transcribe).toBe(false);
|
expect(toVoiceStatus({ provider: "kokoro", voiceId: "df_eva" }).transcribe).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("is off when nothing is connected", () => {
|
it("is off when nothing is connected", () => {
|
||||||
@ -39,7 +39,7 @@ describe("toVoiceStatus", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("voice HTTP routes", () => {
|
describe("voice HTTP routes", () => {
|
||||||
it("rejects unauthenticated speak and transcribe", async () => {
|
it("rejects unauthenticated speak, transcribe, and transcribe-stream", async () => {
|
||||||
const app = new Hono();
|
const app = new Hono();
|
||||||
mountVoiceHttpRoutes(app, {} as VoiceDeps, async () => null);
|
mountVoiceHttpRoutes(app, {} as VoiceDeps, async () => null);
|
||||||
const speak = await app.request("/api/voice/speak", {
|
const speak = await app.request("/api/voice/speak", {
|
||||||
@ -52,13 +52,20 @@ describe("voice HTTP routes", () => {
|
|||||||
headers: { "content-type": "application/json" },
|
headers: { "content-type": "application/json" },
|
||||||
body: JSON.stringify({ audioBase64: "AAAA", mimeType: "audio/webm" }),
|
body: JSON.stringify({ audioBase64: "AAAA", mimeType: "audio/webm" }),
|
||||||
});
|
});
|
||||||
|
const transcribeStream = await app.request("/api/voice/transcribe-stream", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
body: JSON.stringify({ audioBase64: "AAAA", mimeType: "audio/webm" }),
|
||||||
|
});
|
||||||
expect(speak.status).toBe(401);
|
expect(speak.status).toBe(401);
|
||||||
expect(transcribe.status).toBe(401);
|
expect(transcribe.status).toBe(401);
|
||||||
|
expect(transcribeStream.status).toBe(401);
|
||||||
});
|
});
|
||||||
|
|
||||||
it.each([
|
it.each([
|
||||||
["/api/voice/speak", MAX_SPEAK_REQUEST_BYTES],
|
["/api/voice/speak", MAX_SPEAK_REQUEST_BYTES],
|
||||||
["/api/voice/transcribe", MAX_TRANSCRIBE_REQUEST_BYTES],
|
["/api/voice/transcribe", MAX_TRANSCRIBE_REQUEST_BYTES],
|
||||||
|
["/api/voice/transcribe-stream", MAX_TRANSCRIBE_REQUEST_BYTES],
|
||||||
])(
|
])(
|
||||||
"rejects a declared oversized body on %s without waiting for cancellation",
|
"rejects a declared oversized body on %s without waiting for cancellation",
|
||||||
async (path, max) => {
|
async (path, max) => {
|
||||||
@ -112,6 +119,22 @@ describe("voice HTTP routes", () => {
|
|||||||
expect(response.status).toBe(413);
|
expect(response.status).toBe(413);
|
||||||
expect(cancel).toHaveBeenCalledOnce();
|
expect(cancel).toHaveBeenCalledOnce();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("rejects empty recording on transcribe-stream", async () => {
|
||||||
|
const app = new Hono();
|
||||||
|
mountVoiceHttpRoutes(
|
||||||
|
app,
|
||||||
|
{} as VoiceDeps,
|
||||||
|
async () => ({ userId: "user", spaceId: "space" }) as Actor,
|
||||||
|
);
|
||||||
|
const response = await app.request("/api/voice/transcribe-stream", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
body: JSON.stringify({ audioBase64: "", mimeType: "audio/webm" }),
|
||||||
|
});
|
||||||
|
expect(response.status).toBe(400);
|
||||||
|
await expect(response.json()).resolves.toEqual({ error: "Recording is empty." });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const actor = { userId: "user-1", spaceId: "space-1" } as Actor;
|
const actor = { userId: "user-1", spaceId: "space-1" } as Actor;
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
import { ORPCError } from "@orpc/server";
|
import { ORPCError } from "@orpc/server";
|
||||||
import type { AdapterContext } from "@rakazo/adapter-kit";
|
import type { AdapterContext } from "@rakazo/adapter-kit";
|
||||||
|
import type { EncryptedSecretStore } from "@rakazo/adapters";
|
||||||
import {
|
import {
|
||||||
|
createOpenAiCompatibleTranscriptionProvider,
|
||||||
createVoiceProvider,
|
createVoiceProvider,
|
||||||
type EncryptedSecretStore,
|
|
||||||
isVoiceProviderId,
|
isVoiceProviderId,
|
||||||
listVoiceCatalog,
|
listVoiceCatalog,
|
||||||
|
loadMediaSettings,
|
||||||
MAX_SPEAK_CHARS,
|
MAX_SPEAK_CHARS,
|
||||||
MAX_TRANSCRIBE_BYTES,
|
MAX_TRANSCRIBE_BYTES,
|
||||||
NoVoiceConfigured,
|
NoVoiceConfigured,
|
||||||
@ -23,7 +25,7 @@ import {
|
|||||||
selectSpaceVoicePreference,
|
selectSpaceVoicePreference,
|
||||||
} from "@rakazo/db";
|
} from "@rakazo/db";
|
||||||
import type { Context, Hono } from "hono";
|
import type { Context, Hono } from "hono";
|
||||||
import { readBoundedBody } from "./http-body.js";
|
import { cancelBody, readBoundedBody } from "./http-body.js";
|
||||||
import { withSerializableRetry } from "./serializable-retry.js";
|
import { withSerializableRetry } from "./serializable-retry.js";
|
||||||
|
|
||||||
export interface VoiceDeps {
|
export interface VoiceDeps {
|
||||||
@ -51,12 +53,20 @@ export function catalogEntry(provider: string) {
|
|||||||
return voiceCatalogEntry(provider);
|
return voiceCatalogEntry(provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toVoiceStatus(cred: { provider: string; voiceId: string } | null): VoiceStatus {
|
/** The deployment's self-hosted transcription server, when the owner set one up. */
|
||||||
|
export async function mediaTranscriptionSettings(deps: VoiceDeps) {
|
||||||
|
return (await loadMediaSettings(deps).catch(() => null))?.transcription ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function toVoiceStatus(
|
||||||
|
cred: { provider: string; voiceId: string } | null,
|
||||||
|
options: { mediaTranscription?: boolean } = {},
|
||||||
|
): VoiceStatus {
|
||||||
const entry = cred ? catalogEntry(cred.provider) : undefined;
|
const entry = cred ? catalogEntry(cred.provider) : undefined;
|
||||||
return {
|
return {
|
||||||
configured: Boolean(cred),
|
configured: Boolean(cred),
|
||||||
ready: Boolean(cred?.voiceId),
|
ready: Boolean(cred?.voiceId),
|
||||||
transcribe: Boolean(entry?.transcribe && cred),
|
transcribe: Boolean(entry?.transcribe && cred) || Boolean(options.mediaTranscription),
|
||||||
provider: cred?.provider ?? null,
|
provider: cred?.provider ?? null,
|
||||||
voiceId: cred?.voiceId ?? "",
|
voiceId: cred?.voiceId ?? "",
|
||||||
};
|
};
|
||||||
@ -86,7 +96,8 @@ export async function loadVoiceCredential(deps: VoiceDeps, actor: Actor, provide
|
|||||||
const cred = provider
|
const cred = provider
|
||||||
? await findVoiceCredential(deps.prisma, actor, provider)
|
? await findVoiceCredential(deps.prisma, actor, provider)
|
||||||
: await findDefaultVoiceCredential(deps.prisma, actor);
|
: await findDefaultVoiceCredential(deps.prisma, actor);
|
||||||
if (!cred) return null;
|
// Credentials of providers that were removed from the catalog no longer speak.
|
||||||
|
if (!cred || !isVoiceProviderId(cred.provider)) return null;
|
||||||
const secret = await deps.prisma.secret.findFirst({
|
const secret = await deps.prisma.secret.findFirst({
|
||||||
where: { id: cred.secretId, userId: actor.userId, spaceId: null },
|
where: { id: cred.secretId, userId: actor.userId, spaceId: null },
|
||||||
});
|
});
|
||||||
@ -136,7 +147,11 @@ export async function persistVoiceCredential(
|
|||||||
let voiceId = input.voiceId?.trim() ?? "";
|
let voiceId = input.voiceId?.trim() ?? "";
|
||||||
if (!voiceId) {
|
if (!voiceId) {
|
||||||
const voices = await provider.listVoices(input.plaintext, voiceContext(actor, input.signal));
|
const voices = await provider.listVoices(input.plaintext, voiceContext(actor, input.signal));
|
||||||
voiceId = voices[0]?.id ?? "";
|
const germanVoice =
|
||||||
|
input.provider === "kokoro"
|
||||||
|
? voices.find((v) => v.id === "df_eva" || /^d[fm]_/i.test(v.id))
|
||||||
|
: undefined;
|
||||||
|
voiceId = germanVoice?.id ?? voices[0]?.id ?? "";
|
||||||
}
|
}
|
||||||
const stored = await deps.secrets.put(input.plaintext, voiceContext(actor, input.signal));
|
const stored = await deps.secrets.put(input.plaintext, voiceContext(actor, input.signal));
|
||||||
const cred = await withSerializableRetry(() =>
|
const cred = await withSerializableRetry(() =>
|
||||||
@ -277,9 +292,22 @@ export async function transcribeVoice(
|
|||||||
input: { audio: Uint8Array; mimeType: string; signal?: AbortSignal },
|
input: { audio: Uint8Array; mimeType: string; signal?: AbortSignal },
|
||||||
) {
|
) {
|
||||||
const loaded = await loadDefaultVoiceCredential(deps, actor);
|
const loaded = await loadDefaultVoiceCredential(deps, actor);
|
||||||
if (!loaded) throw new NoVoiceConfigured("key");
|
const provider = loaded ? createVoiceProvider(loaded.cred.provider) : null;
|
||||||
const provider = createVoiceProvider(loaded.cred.provider);
|
if (!loaded || !provider?.transcribe) {
|
||||||
if (!provider.transcribe) {
|
// Without a voice provider that transcribes, the self-hosted media server can.
|
||||||
|
const server = await mediaTranscriptionSettings(deps);
|
||||||
|
if (server) {
|
||||||
|
if (input.audio.byteLength === 0 || input.audio.byteLength > MAX_TRANSCRIBE_BYTES) {
|
||||||
|
throw new ORPCError("BAD_REQUEST", { message: "That recording is empty or too large." });
|
||||||
|
}
|
||||||
|
const transcript = await createOpenAiCompatibleTranscriptionProvider(server).transcribe({
|
||||||
|
audio: input.audio,
|
||||||
|
mimeType: input.mimeType || "audio/webm",
|
||||||
|
signal: input.signal,
|
||||||
|
});
|
||||||
|
return { text: transcript.text };
|
||||||
|
}
|
||||||
|
if (!loaded) throw new NoVoiceConfigured("key");
|
||||||
throw new ORPCError("BAD_REQUEST", {
|
throw new ORPCError("BAD_REQUEST", {
|
||||||
message: "This voice provider does not transcribe audio. Use on-device dictation instead.",
|
message: "This voice provider does not transcribe audio. Use on-device dictation instead.",
|
||||||
});
|
});
|
||||||
@ -352,6 +380,113 @@ export function mountVoiceHttpRoutes(
|
|||||||
return voiceHttpError(c, error);
|
return voiceHttpError(c, error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.post("/api/voice/transcribe-stream", async (c) => {
|
||||||
|
const actor = await authenticate(c);
|
||||||
|
if (!actor) return c.json({ error: "Unauthorized" }, 401);
|
||||||
|
const contentType = c.req.header("content-type") ?? "";
|
||||||
|
const isJson =
|
||||||
|
contentType.includes("application/json") ||
|
||||||
|
(!contentType.startsWith("audio/") && !contentType.includes("octet-stream"));
|
||||||
|
|
||||||
|
let audio: Uint8Array;
|
||||||
|
let mimeType = "audio/webm";
|
||||||
|
|
||||||
|
if (isJson) {
|
||||||
|
const raw = await readBoundedBody(c.req.raw, MAX_TRANSCRIBE_REQUEST_BYTES);
|
||||||
|
if (raw === null) return c.json({ error: "Request body is too large." }, 413);
|
||||||
|
const body = parseVoiceRequestBody(raw);
|
||||||
|
const audioBase64 = String((body as { audioBase64?: unknown }).audioBase64 ?? "");
|
||||||
|
try {
|
||||||
|
audio = decodeAudioBase64(audioBase64);
|
||||||
|
mimeType = String((body as { mimeType?: unknown }).mimeType ?? "audio/webm");
|
||||||
|
} catch (error) {
|
||||||
|
return voiceHttpError(c, error);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const rawBytes = await readBoundedBytes(c.req.raw, MAX_TRANSCRIBE_REQUEST_BYTES);
|
||||||
|
if (rawBytes === null) return c.json({ error: "Request body is too large." }, 413);
|
||||||
|
if (rawBytes.byteLength === 0) {
|
||||||
|
return voiceHttpError(c, new ORPCError("BAD_REQUEST", { message: "Recording is empty." }));
|
||||||
|
}
|
||||||
|
audio = rawBytes;
|
||||||
|
mimeType = contentType.split(";")[0]?.trim() || "audio/webm";
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await transcribeVoice(deps, actor, {
|
||||||
|
audio,
|
||||||
|
mimeType,
|
||||||
|
signal: c.req.raw.signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
const accept = c.req.header("accept") ?? "";
|
||||||
|
if (accept.includes("application/json") && !accept.includes("text/event-stream")) {
|
||||||
|
return c.json({ text: result.text, isFinal: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
const encoder = new TextEncoder();
|
||||||
|
const stream = new ReadableStream({
|
||||||
|
start(controller) {
|
||||||
|
controller.enqueue(
|
||||||
|
encoder.encode(
|
||||||
|
`data: ${JSON.stringify({ text: result.text, delta: result.text, isFinal: true })}\n\n`,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
controller.enqueue(encoder.encode(`data: [DONE]\n\n`));
|
||||||
|
controller.close();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return new Response(stream, {
|
||||||
|
headers: {
|
||||||
|
"content-type": "text/event-stream; charset=utf-8",
|
||||||
|
"cache-control": "no-cache, no-transform",
|
||||||
|
connection: "keep-alive",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
return voiceHttpError(c, error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readBoundedBytes(request: Request, maxBytes: number): Promise<Uint8Array | null> {
|
||||||
|
const contentLengthHeader = request.headers.get("content-length");
|
||||||
|
if (contentLengthHeader !== null) {
|
||||||
|
const contentLength = Number(contentLengthHeader);
|
||||||
|
if (!Number.isFinite(contentLength) || contentLength < 0 || contentLength > maxBytes) {
|
||||||
|
cancelBody(request.body);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!request.body) return new Uint8Array(0);
|
||||||
|
|
||||||
|
const reader = request.body.getReader();
|
||||||
|
const chunks: Uint8Array[] = [];
|
||||||
|
let bytes = 0;
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) break;
|
||||||
|
bytes += value.byteLength;
|
||||||
|
if (bytes > maxBytes) {
|
||||||
|
cancelBody(reader);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
chunks.push(value);
|
||||||
|
}
|
||||||
|
const combined = new Uint8Array(bytes);
|
||||||
|
let offset = 0;
|
||||||
|
for (const chunk of chunks) {
|
||||||
|
combined.set(chunk, offset);
|
||||||
|
offset += chunk.byteLength;
|
||||||
|
}
|
||||||
|
return combined;
|
||||||
|
} finally {
|
||||||
|
reader.releaseLock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseVoiceRequestBody(raw: string): Record<string, unknown> {
|
function parseVoiceRequestBody(raw: string): Record<string, unknown> {
|
||||||
|
|||||||
36
apps/api/src/web-origin.test.ts
Normal file
36
apps/api/src/web-origin.test.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { requestWebOrigin } from "./web-origin.js";
|
||||||
|
|
||||||
|
const configured = "http://127.0.0.1:5173";
|
||||||
|
|
||||||
|
describe("requestWebOrigin", () => {
|
||||||
|
it("follows a browser on loopback or a private network", () => {
|
||||||
|
expect(requestWebOrigin("http://192.168.0.20:5173", configured)).toBe(
|
||||||
|
"http://192.168.0.20:5173",
|
||||||
|
);
|
||||||
|
expect(requestWebOrigin("http://10.0.0.5:5173", configured)).toBe("http://10.0.0.5:5173");
|
||||||
|
expect(requestWebOrigin("http://mac.local:5173", configured)).toBe("http://mac.local:5173");
|
||||||
|
expect(requestWebOrigin("http://localhost:5173", configured)).toBe("http://localhost:5173");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps WEB_ORIGIN for public hosts, other schemes, and missing origins", () => {
|
||||||
|
expect(requestWebOrigin("https://attacker.example", configured)).toBe(configured);
|
||||||
|
expect(requestWebOrigin("rakazo://app", configured)).toBe(configured);
|
||||||
|
expect(requestWebOrigin("null", configured)).toBe(configured);
|
||||||
|
expect(requestWebOrigin(undefined, configured)).toBe(configured);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("accepts the configured origin even when it is public", () => {
|
||||||
|
expect(requestWebOrigin("https://evoo.example", "https://evoo.example")).toBe(
|
||||||
|
"https://evoo.example",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("accepts origins listed in extraTrustedOrigins", () => {
|
||||||
|
expect(
|
||||||
|
requestWebOrigin("https://host.example.ts.net:8443", configured, [
|
||||||
|
"https://host.example.ts.net:8443",
|
||||||
|
]),
|
||||||
|
).toBe("https://host.example.ts.net:8443");
|
||||||
|
});
|
||||||
|
});
|
||||||
35
apps/api/src/web-origin.ts
Normal file
35
apps/api/src/web-origin.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/** Hosts the API already trusts as browser origins: loopback and private networks. */
|
||||||
|
export function isPrivateOrLocalHost(host: string): boolean {
|
||||||
|
if (isLoopbackHost(host)) return true;
|
||||||
|
if (host.endsWith(".local")) return true;
|
||||||
|
if (/^192\.168\.\d{1,3}\.\d{1,3}$/.test(host)) return true;
|
||||||
|
if (/^10\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(host)) return true;
|
||||||
|
if (/^172\.(1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}$/.test(host)) return true;
|
||||||
|
if (/^100\.(6[4-9]|[7-9]\d|1[01]\d|12[0-7])\.\d{1,3}\.\d{1,3}$/.test(host)) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isLoopbackHost(host: string): boolean {
|
||||||
|
return host === "localhost" || host === "127.0.0.1" || host === "::1" || host === "[::1]";
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The web origin that browser-facing URLs (OAuth returns, screen links) should
|
||||||
|
* point at for this request. A browser opened over a loopback, private-network,
|
||||||
|
* or explicitly trusted origin gets its own origin back so a phone on the LAN
|
||||||
|
* or Tailscale is not sent to 127.0.0.1; any other caller gets WEB_ORIGIN. */
|
||||||
|
export function requestWebOrigin(
|
||||||
|
origin: string | undefined,
|
||||||
|
webOrigin: string,
|
||||||
|
extraTrustedOrigins: readonly string[] = [],
|
||||||
|
): string {
|
||||||
|
if (!origin) return webOrigin;
|
||||||
|
try {
|
||||||
|
const url = new URL(origin);
|
||||||
|
if (url.protocol !== "http:" && url.protocol !== "https:") return webOrigin;
|
||||||
|
if (url.origin === new URL(webOrigin).origin) return url.origin;
|
||||||
|
if (extraTrustedOrigins.includes(url.origin)) return url.origin;
|
||||||
|
return isPrivateOrLocalHost(url.hostname) ? url.origin : webOrigin;
|
||||||
|
} catch {
|
||||||
|
return webOrigin;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -177,7 +177,7 @@ test("This computer installs and starts the stack, then opens the app", async ()
|
|||||||
|
|
||||||
const appWindowPromise = app.waitForEvent("window");
|
const appWindowPromise = app.waitForEvent("window");
|
||||||
await setup.getByRole("button", { name: "Continue" }).click();
|
await setup.getByRole("button", { name: "Continue" }).click();
|
||||||
await expect(setup.locator("#stack-phase")).toHaveText("Downloading Rakazo…");
|
await expect(setup.locator("#stack-phase")).toHaveText("Downloading evOo…");
|
||||||
await expect(setup.getByRole("button", { name: "Continue" })).toBeDisabled();
|
await expect(setup.getByRole("button", { name: "Continue" })).toBeDisabled();
|
||||||
// Docker output stays behind the details toggle; the phase, the bar, and the size show by default.
|
// Docker output stays behind the details toggle; the phase, the bar, and the size show by default.
|
||||||
await expect(setup.locator("#stack-detail")).toHaveText("412 MB downloaded");
|
await expect(setup.locator("#stack-detail")).toHaveText("412 MB downloaded");
|
||||||
@ -249,7 +249,7 @@ test("switching to Existing instance while the stack starts keeps that choice",
|
|||||||
const setup = await app.firstWindow();
|
const setup = await app.firstWindow();
|
||||||
|
|
||||||
await setup.getByRole("button", { name: "Continue" }).click();
|
await setup.getByRole("button", { name: "Continue" }).click();
|
||||||
await expect(setup.locator("#stack-phase")).toHaveText("Downloading Rakazo…");
|
await expect(setup.locator("#stack-phase")).toHaveText("Downloading evOo…");
|
||||||
|
|
||||||
// Fake docker sleeps during pull; leave This computer before ready so followStack must not save.
|
// Fake docker sleeps during pull; leave This computer before ready so followStack must not save.
|
||||||
await setup.getByRole("radio", { name: /Existing instance/ }).check();
|
await setup.getByRole("radio", { name: /Existing instance/ }).check();
|
||||||
|
|||||||
@ -83,7 +83,7 @@ test("first run asks whether to use a local or existing instance", async () => {
|
|||||||
}
|
}
|
||||||
const setup = await app.firstWindow();
|
const setup = await app.firstWindow();
|
||||||
|
|
||||||
await expect(setup.getByRole("heading", { name: "Welcome to Rakazo" })).toBeVisible();
|
await expect(setup.getByRole("heading", { name: "Welcome to evOo" })).toBeVisible();
|
||||||
await expect(setup.getByText("Choose which server this app should use.")).toBeVisible();
|
await expect(setup.getByText("Choose which server this app should use.")).toBeVisible();
|
||||||
await expect(setup.getByText("This computer")).toBeVisible();
|
await expect(setup.getByText("This computer")).toBeVisible();
|
||||||
await expect(setup.getByText("Existing instance")).toBeVisible();
|
await expect(setup.getByText("Existing instance")).toBeVisible();
|
||||||
@ -139,7 +139,7 @@ test("connecting to an existing instance verifies, saves, and opens it", async (
|
|||||||
|
|
||||||
await setup.locator("#server-url").fill(serverUrl);
|
await setup.locator("#server-url").fill(serverUrl);
|
||||||
await setup.getByRole("button", { name: "Check connection" }).click();
|
await setup.getByRole("button", { name: "Check connection" }).click();
|
||||||
await expect(setup.locator("#status")).toHaveText(`Rakazo answered at ${serverUrl}.`);
|
await expect(setup.locator("#status")).toHaveText(`evOo answered at ${serverUrl}.`);
|
||||||
await expect(setup.locator("#status")).toHaveAttribute("data-tone", "ok");
|
await expect(setup.locator("#status")).toHaveAttribute("data-tone", "ok");
|
||||||
|
|
||||||
await setup.screenshot({
|
await setup.screenshot({
|
||||||
@ -437,7 +437,7 @@ test("a malformed address is rejected before anything is written", async () => {
|
|||||||
}).toPass();
|
}).toPass();
|
||||||
});
|
});
|
||||||
|
|
||||||
test("a generic web page is not accepted as a Rakazo server", async () => {
|
test("a generic web page is not accepted as an evOo server", async () => {
|
||||||
const plain = createServer((_request, response) => {
|
const plain = createServer((_request, response) => {
|
||||||
response.writeHead(200, { "content-type": "text/html; charset=utf-8" });
|
response.writeHead(200, { "content-type": "text/html; charset=utf-8" });
|
||||||
response.end("<!doctype html><p>not Rakazo</p>");
|
response.end("<!doctype html><p>not Rakazo</p>");
|
||||||
@ -454,7 +454,7 @@ test("a generic web page is not accepted as a Rakazo server", async () => {
|
|||||||
await setup.getByRole("button", { name: "Continue" }).click();
|
await setup.getByRole("button", { name: "Continue" }).click();
|
||||||
|
|
||||||
await expect(setup.locator("#status")).toHaveText(
|
await expect(setup.locator("#status")).toHaveText(
|
||||||
"That address did not respond like a Rakazo server.",
|
"That address did not respond like an evOo server.",
|
||||||
);
|
);
|
||||||
await expect(async () => {
|
await expect(async () => {
|
||||||
await expect(readFile(path.join(userData, "setup.json"), "utf8")).rejects.toThrow();
|
await expect(readFile(path.join(userData, "setup.json"), "utf8")).rejects.toThrow();
|
||||||
@ -507,7 +507,7 @@ test("an unreachable saved server falls back to setup with a recovery message",
|
|||||||
app = await launch();
|
app = await launch();
|
||||||
const setup = await app.firstWindow();
|
const setup = await app.firstWindow();
|
||||||
|
|
||||||
await expect(setup.getByRole("heading", { name: "Welcome to Rakazo" })).toBeVisible();
|
await expect(setup.getByRole("heading", { name: "Welcome to evOo" })).toBeVisible();
|
||||||
await expect(setup.getByRole("radio", { name: /Existing instance/ })).toBeChecked();
|
await expect(setup.getByRole("radio", { name: /Existing instance/ })).toBeChecked();
|
||||||
await expect(setup.locator("#server-url")).toHaveValue(closedUrl);
|
await expect(setup.locator("#server-url")).toHaveValue(closedUrl);
|
||||||
await expect(setup.locator("#status")).toContainText("Could not reconnect to the saved server.");
|
await expect(setup.locator("#status")).toContainText("Could not reconnect to the saved server.");
|
||||||
@ -529,7 +529,7 @@ test("the native application menu can reopen setup without exposing setup IPC to
|
|||||||
});
|
});
|
||||||
const setup = await setupPromise;
|
const setup = await setupPromise;
|
||||||
|
|
||||||
await expect(setup.getByRole("heading", { name: "Welcome to Rakazo" })).toBeVisible();
|
await expect(setup.getByRole("heading", { name: "Welcome to evOo" })).toBeVisible();
|
||||||
await expect(setup.locator("#status")).toBeEmpty();
|
await expect(setup.locator("#status")).toBeEmpty();
|
||||||
|
|
||||||
// Closing setup without saving restores the connected instance.
|
// Closing setup without saving restores the connected instance.
|
||||||
|
|||||||
@ -80,8 +80,8 @@
|
|||||||
"publish": [
|
"publish": [
|
||||||
{
|
{
|
||||||
"provider": "github",
|
"provider": "github",
|
||||||
"owner": "elie222",
|
"owner": "cynarAI",
|
||||||
"repo": "rakazo"
|
"repo": "evOo-local"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -79,7 +79,7 @@ describe("renderStackEnv", () => {
|
|||||||
expect(lines.some((line) => line.startsWith("RAKAZO_IMAGE_TAG="))).toBe(false);
|
expect(lines.some((line) => line.startsWith("RAKAZO_IMAGE_TAG="))).toBe(false);
|
||||||
expect(lines.some((line) => line.startsWith("RAKAZO_COMPUTER_IMAGE_TAG="))).toBe(false);
|
expect(lines.some((line) => line.startsWith("RAKAZO_COMPUTER_IMAGE_TAG="))).toBe(false);
|
||||||
// Everything else, including the image names and empty optional keys, stays verbatim.
|
// Everything else, including the image names and empty optional keys, stays verbatim.
|
||||||
expect(lines).toContain("RAKAZO_IMAGE=ghcr.io/elie222/rakazo/app");
|
expect(lines).toContain("RAKAZO_IMAGE=ghcr.io/cynarai/evoo-local/app");
|
||||||
expect(lines).toContain("SANDBOX_PROVIDER=docker");
|
expect(lines).toContain("SANDBOX_PROVIDER=docker");
|
||||||
expect(lines).toContain("OPENROUTER_API_KEY=");
|
expect(lines).toContain("OPENROUTER_API_KEY=");
|
||||||
expect(rendered.endsWith("\n")).toBe(template.endsWith("\n"));
|
expect(rendered.endsWith("\n")).toBe(template.endsWith("\n"));
|
||||||
|
|||||||
@ -711,7 +711,7 @@ function installApplicationMenu() {
|
|||||||
};
|
};
|
||||||
const changeServer: Electron.MenuItemConstructorOptions = {
|
const changeServer: Electron.MenuItemConstructorOptions = {
|
||||||
id: "change-rakazo-server",
|
id: "change-rakazo-server",
|
||||||
label: "Change Rakazo Server…",
|
label: "Change evOo Server…",
|
||||||
accelerator: "CmdOrCtrl+Shift+K",
|
accelerator: "CmdOrCtrl+Shift+K",
|
||||||
click: () => showSetupWindow(),
|
click: () => showSetupWindow(),
|
||||||
};
|
};
|
||||||
@ -789,7 +789,7 @@ async function probeServer(rawUrl: string, signal?: AbortSignal): Promise<Deskto
|
|||||||
ok: false,
|
ok: false,
|
||||||
status: response.status,
|
status: response.status,
|
||||||
url,
|
url,
|
||||||
error: "That address redirects elsewhere. Enter the final Rakazo server address.",
|
error: "That address redirects elsewhere. Enter the final evOo server address.",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@ -806,7 +806,7 @@ async function probeServer(rawUrl: string, signal?: AbortSignal): Promise<Deskto
|
|||||||
ok: false,
|
ok: false,
|
||||||
status: response.status,
|
status: response.status,
|
||||||
url,
|
url,
|
||||||
error: "That address did not respond like a Rakazo server.",
|
error: "That address did not respond like an evOo server.",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
@ -1249,7 +1249,7 @@ app.whenReady().then(async () => {
|
|||||||
if (managedUrl === null || !(await localStack.matchesDesiredStack())) {
|
if (managedUrl === null || !(await localStack.matchesDesiredStack())) {
|
||||||
return {
|
return {
|
||||||
ok: false,
|
ok: false,
|
||||||
error: "The app-managed Rakazo services are not ready. Retry setup.",
|
error: "The app-managed evOo services are not ready. Retry setup.",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
openSetup = { mode: "new", serverUrl: managedUrl };
|
openSetup = { mode: "new", serverUrl: managedUrl };
|
||||||
|
|||||||
@ -66,9 +66,9 @@ describe("desktop release workflow", () => {
|
|||||||
`include='[{"runner":["self-hosted","evoo-ci","macOS"],"artifact":"macos"},{"runner":["self-hosted","evoo-ci","Linux"],"artifact":"linux"}]'`,
|
`include='[{"runner":["self-hosted","evoo-ci","macOS"],"artifact":"macos"},{"runner":["self-hosted","evoo-ci","Linux"],"artifact":"linux"}]'`,
|
||||||
);
|
);
|
||||||
expect(workflow).toMatch(
|
expect(workflow).toMatch(
|
||||||
/if \[\[ "\$WINDOWS_SIGNING" == "true" \]\]; then\n(?:\s*#[^\n]*\n)*\s+include=.*\{"runner":"windows-2022","artifact":"windows"\}/,
|
/if \[\[ "\$WINDOWS_SIGNING" == "true" \]\]; then\n(?:\s*#[^\n]*\n)*\s+include=.*\{"runner":\["self-hosted","evoo-ci","Windows"\],"artifact":"windows"\}/,
|
||||||
);
|
);
|
||||||
expect(workflow.split("windows-2022")).toHaveLength(2);
|
expect(workflow).not.toMatch(/windows-\d|windows-latest/);
|
||||||
expect(workflow).toContain(`WINDOWS_BUILT: ${expression("needs.validate.outputs.windows")}`);
|
expect(workflow).toContain(`WINDOWS_BUILT: ${expression("needs.validate.outputs.windows")}`);
|
||||||
expect(workflow).toContain('if [[ "$WINDOWS_BUILT" == "true" ]]; then');
|
expect(workflow).toContain('if [[ "$WINDOWS_BUILT" == "true" ]]; then');
|
||||||
expect(workflow).toContain("feeds+=(release-artifacts/latest.yml)");
|
expect(workflow).toContain("feeds+=(release-artifacts/latest.yml)");
|
||||||
@ -76,8 +76,8 @@ describe("desktop release workflow", () => {
|
|||||||
|
|
||||||
it("pins every platform update feed to the official GitHub owner and repo", () => {
|
it("pins every platform update feed to the official GitHub owner and repo", () => {
|
||||||
expect(workflow).toContain('grep -Fqx "provider: github"');
|
expect(workflow).toContain('grep -Fqx "provider: github"');
|
||||||
expect(workflow).toContain('grep -Fqx "owner: elie222"');
|
expect(workflow).toContain('grep -Fqx "owner: cynarAI"');
|
||||||
expect(workflow).toContain('grep -Fqx "repo: rakazo"');
|
expect(workflow).toContain('grep -Fqx "repo: evOo-local"');
|
||||||
expect(workflow).toContain("Verify Linux update feed is pinned to the official GitHub channel");
|
expect(workflow).toContain("Verify Linux update feed is pinned to the official GitHub channel");
|
||||||
expect(workflow).toContain("Windows update config missing");
|
expect(workflow).toContain("Windows update config missing");
|
||||||
expect(workflow).toContain("RELEASE_VERSION:");
|
expect(workflow).toContain("RELEASE_VERSION:");
|
||||||
@ -94,4 +94,11 @@ describe("desktop release workflow", () => {
|
|||||||
expect(workflow).toContain("--draft --generate-notes");
|
expect(workflow).toContain("--draft --generate-notes");
|
||||||
expect(workflow).toContain("--draft=false --latest");
|
expect(workflow).toContain("--draft=false --latest");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("hands platform builds to the draft release, never to Actions artifact storage", () => {
|
||||||
|
expect(workflow).not.toMatch(/actions\/(?:upload|download)-artifact@/);
|
||||||
|
expect(workflow).toContain("bash scripts/upload-release-assets.sh");
|
||||||
|
expect(workflow).toContain('gh release download "$RELEASE_TAG" --dir release-artifacts');
|
||||||
|
expect(workflow).toContain("needs: [validate, build, publish]");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -6,18 +6,18 @@
|
|||||||
http-equiv="Content-Security-Policy"
|
http-equiv="Content-Security-Policy"
|
||||||
content="default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; form-action 'none'; base-uri 'none'"
|
content="default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; form-action 'none'; base-uri 'none'"
|
||||||
/>
|
/>
|
||||||
<title>Set up Rakazo</title>
|
<title>Set up evOo</title>
|
||||||
<link rel="stylesheet" href="tokens.css" />
|
<link rel="stylesheet" href="tokens.css" />
|
||||||
<link rel="stylesheet" href="setup.css" />
|
<link rel="stylesheet" href="setup.css" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<header class="titlebar">
|
<header class="titlebar">
|
||||||
<span class="titlebar-name">Rakazo</span>
|
<span class="titlebar-name">evOo</span>
|
||||||
<button id="quit" class="titlebar-quit" type="button" title="Quit Rakazo">Quit</button>
|
<button id="quit" class="titlebar-quit" type="button" title="Quit evOo">Quit</button>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<main class="shell">
|
<main class="shell">
|
||||||
<h1 class="heading">Welcome to Rakazo</h1>
|
<h1 class="heading">Welcome to evOo</h1>
|
||||||
<p class="subheading">Choose which server this app should use.</p>
|
<p class="subheading">Choose which server this app should use.</p>
|
||||||
|
|
||||||
<form id="setup" novalidate>
|
<form id="setup" novalidate>
|
||||||
|
|||||||
@ -23,10 +23,10 @@
|
|||||||
const PHASE_LABELS = {
|
const PHASE_LABELS = {
|
||||||
"checking-docker": "Getting ready…",
|
"checking-docker": "Getting ready…",
|
||||||
preparing: "Getting ready…",
|
preparing: "Getting ready…",
|
||||||
pulling: "Downloading Rakazo…",
|
pulling: "Downloading evOo…",
|
||||||
starting: "Starting Rakazo…",
|
starting: "Starting evOo…",
|
||||||
"waiting-healthy": "Almost ready…",
|
"waiting-healthy": "Almost ready…",
|
||||||
ready: "Rakazo is ready.",
|
ready: "evOo is ready.",
|
||||||
};
|
};
|
||||||
const TERMINAL_PHASES = new Set([
|
const TERMINAL_PHASES = new Set([
|
||||||
"idle",
|
"idle",
|
||||||
@ -266,7 +266,7 @@
|
|||||||
const result = await bridge.test(value);
|
const result = await bridge.test(value);
|
||||||
if (result.ok) {
|
if (result.ok) {
|
||||||
serverUrl.value = result.url;
|
serverUrl.value = result.url;
|
||||||
setStatus(`Rakazo answered at ${result.url}.`, "ok");
|
setStatus(`evOo answered at ${result.url}.`, "ok");
|
||||||
} else {
|
} else {
|
||||||
setStatus(result.error ?? "Could not reach that address.", "error");
|
setStatus(result.error ?? "Could not reach that address.", "error");
|
||||||
}
|
}
|
||||||
@ -362,7 +362,7 @@
|
|||||||
continueButton.focus();
|
continueButton.focus();
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
setStatus("Setup could not start. Quit Rakazo and try again.", "error");
|
setStatus("Setup could not start. Quit evOo and try again.", "error");
|
||||||
setBusy(true);
|
setBusy(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@ name: Android live working status demonstration
|
|||||||
notifications: allow
|
notifications: allow
|
||||||
# A cold API 34 emulator can show a launcher ANR over the healthy app.
|
# A cold API 34 emulator can show a launcher ANR over the healthy app.
|
||||||
- extendedWaitUntil:
|
- extendedWaitUntil:
|
||||||
visible: "Sign up for Rakazo|Pixel Launcher isn't responding"
|
visible: "Sign up for evOo|Pixel Launcher isn't responding"
|
||||||
timeout: 30000
|
timeout: 30000
|
||||||
- runFlow:
|
- runFlow:
|
||||||
when:
|
when:
|
||||||
@ -15,7 +15,7 @@ name: Android live working status demonstration
|
|||||||
commands:
|
commands:
|
||||||
- tapOn: "Close app"
|
- tapOn: "Close app"
|
||||||
- extendedWaitUntil:
|
- extendedWaitUntil:
|
||||||
visible: "Sign up for Rakazo"
|
visible: "Sign up for evOo"
|
||||||
timeout: 30000
|
timeout: 30000
|
||||||
- tapOn: "Sign in"
|
- tapOn: "Sign in"
|
||||||
- tapOn: "Email"
|
- tapOn: "Email"
|
||||||
|
|||||||
@ -10,7 +10,7 @@ tags:
|
|||||||
|
|
||||||
# A cold API 34 emulator can show a launcher ANR over the healthy app.
|
# A cold API 34 emulator can show a launcher ANR over the healthy app.
|
||||||
- extendedWaitUntil:
|
- extendedWaitUntil:
|
||||||
visible: "Sign up for Rakazo|Pixel Launcher isn't responding"
|
visible: "Sign up for evOo|Pixel Launcher isn't responding"
|
||||||
timeout: 30000
|
timeout: 30000
|
||||||
- runFlow:
|
- runFlow:
|
||||||
when:
|
when:
|
||||||
@ -18,11 +18,11 @@ tags:
|
|||||||
commands:
|
commands:
|
||||||
- tapOn: "Close app"
|
- tapOn: "Close app"
|
||||||
- extendedWaitUntil:
|
- extendedWaitUntil:
|
||||||
visible: "Sign up for Rakazo"
|
visible: "Sign up for evOo"
|
||||||
timeout: 30000
|
timeout: 30000
|
||||||
- takeScreenshot: "02-sign-up"
|
- takeScreenshot: "02-sign-up"
|
||||||
- tapOn: "Sign in"
|
- tapOn: "Sign in"
|
||||||
- assertVisible: "Sign in to Rakazo"
|
- assertVisible: "Sign in to evOo"
|
||||||
- takeScreenshot: "01-sign-in"
|
- takeScreenshot: "01-sign-in"
|
||||||
|
|
||||||
- tapOn: "Use a custom server"
|
- tapOn: "Use a custom server"
|
||||||
|
|||||||
@ -4,18 +4,18 @@ name: Rakazo mobile smoke
|
|||||||
- launchApp:
|
- launchApp:
|
||||||
clearState: true
|
clearState: true
|
||||||
|
|
||||||
- assertVisible: "Sign in to Rakazo"
|
- assertVisible: "Sign in to evOo"
|
||||||
- assertVisible: "Sign up"
|
- assertVisible: "Sign up"
|
||||||
- tapOn: "Sign up"
|
- tapOn: "Sign up"
|
||||||
- assertVisible: "Sign up for Rakazo"
|
- assertVisible: "Sign up for evOo"
|
||||||
- assertVisible: "Name"
|
- assertVisible: "Name"
|
||||||
- tapOn: "Sign in"
|
- tapOn: "Sign in"
|
||||||
- assertVisible: "Sign in to Rakazo"
|
- assertVisible: "Sign in to evOo"
|
||||||
- tapOn: "Email"
|
- tapOn: "Email"
|
||||||
- inputText: ${RAKAZO_E2E_EMAIL}
|
- inputText: ${RAKAZO_E2E_EMAIL}
|
||||||
- tapOn: "Password"
|
- tapOn: "Password"
|
||||||
- inputText: ${RAKAZO_E2E_PASSWORD}
|
- inputText: ${RAKAZO_E2E_PASSWORD}
|
||||||
- tapOn: "Sign in to Rakazo"
|
- tapOn: "Sign in to evOo"
|
||||||
- tapOn: "Sign in"
|
- tapOn: "Sign in"
|
||||||
|
|
||||||
- extendedWaitUntil:
|
- extendedWaitUntil:
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"expo": {
|
"expo": {
|
||||||
"name": "Rakazo",
|
"name": "evOo",
|
||||||
"slug": "rakazo",
|
"slug": "rakazo",
|
||||||
"owner": "inbox-zero",
|
"owner": "inbox-zero",
|
||||||
"scheme": "rakazo",
|
"scheme": "rakazo",
|
||||||
@ -33,8 +33,8 @@
|
|||||||
[
|
[
|
||||||
"expo-image-picker",
|
"expo-image-picker",
|
||||||
{
|
{
|
||||||
"photosPermission": "Rakazo uses your photo library so you can attach images to a bot message.",
|
"photosPermission": "evOo uses your photo library so you can attach images to a bot message.",
|
||||||
"cameraPermission": "Rakazo uses the camera so you can attach photos to a bot message.",
|
"cameraPermission": "evOo uses the camera so you can attach photos to a bot message.",
|
||||||
"microphonePermission": false
|
"microphonePermission": false
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -51,9 +51,9 @@
|
|||||||
"usesNonExemptEncryption": false
|
"usesNonExemptEncryption": false
|
||||||
},
|
},
|
||||||
"infoPlist": {
|
"infoPlist": {
|
||||||
"NSCameraUsageDescription": "Rakazo uses the camera so you can attach photos to a bot message.",
|
"NSCameraUsageDescription": "evOo uses the camera so you can attach photos to a bot message.",
|
||||||
"NSPhotoLibraryUsageDescription": "Rakazo uses your photo library so you can attach images to a bot message.",
|
"NSPhotoLibraryUsageDescription": "evOo uses your photo library so you can attach images to a bot message.",
|
||||||
"NSLocalNetworkUsageDescription": "Rakazo connects to a self-hosted Rakazo server on your local network.",
|
"NSLocalNetworkUsageDescription": "evOo connects to a self-hosted evOo server on your local network.",
|
||||||
"NSAppTransportSecurity": {
|
"NSAppTransportSecurity": {
|
||||||
"NSAllowsLocalNetworking": true
|
"NSAllowsLocalNetworking": true
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import { useEffect, useMemo, useState } from "react";
|
|||||||
import { View } from "react-native";
|
import { View } from "react-native";
|
||||||
import { GestureHandlerRootView } from "react-native-gesture-handler";
|
import { GestureHandlerRootView } from "react-native-gesture-handler";
|
||||||
import { KeyboardProvider } from "react-native-keyboard-controller";
|
import { KeyboardProvider } from "react-native-keyboard-controller";
|
||||||
import { AvatarStyleProvider } from "../components/avatar-style";
|
|
||||||
import { ComputerUpdateProgress } from "../components/computer-update-progress";
|
import { ComputerUpdateProgress } from "../components/computer-update-progress";
|
||||||
import { currentApiBase, loadApiBase, loadSessionToken, selectedSpaceId } from "../lib/api";
|
import { currentApiBase, loadApiBase, loadSessionToken, selectedSpaceId } from "../lib/api";
|
||||||
import { loadAppearancePreference, mobileTokens } from "../lib/appearance";
|
import { loadAppearancePreference, mobileTokens } from "../lib/appearance";
|
||||||
@ -57,74 +56,72 @@ export default function Layout() {
|
|||||||
<GestureHandlerRootView style={{ flex: 1 }}>
|
<GestureHandlerRootView style={{ flex: 1 }}>
|
||||||
<KeyboardProvider>
|
<KeyboardProvider>
|
||||||
{ready ? (
|
{ready ? (
|
||||||
<AvatarStyleProvider>
|
<ThemeProvider value={navigationTheme}>
|
||||||
<ThemeProvider value={navigationTheme}>
|
<StatusBar style={resolved === "light" ? "dark" : "light"} />
|
||||||
<StatusBar style={resolved === "light" ? "dark" : "light"} />
|
<Stack
|
||||||
<Stack
|
screenOptions={{
|
||||||
screenOptions={{
|
headerStyle: { backgroundColor: navigationTheme.colors.background },
|
||||||
headerStyle: { backgroundColor: navigationTheme.colors.background },
|
headerTintColor: navigationTheme.colors.text,
|
||||||
headerTintColor: navigationTheme.colors.text,
|
headerShadowVisible: false,
|
||||||
headerShadowVisible: false,
|
headerBackButtonDisplayMode: "minimal",
|
||||||
headerBackButtonDisplayMode: "minimal",
|
contentStyle: { backgroundColor: String(native.page) },
|
||||||
contentStyle: { backgroundColor: String(native.page) },
|
}}
|
||||||
|
>
|
||||||
|
<Stack.Screen name="index" options={{ headerShown: false, title: "evOo" }} />
|
||||||
|
<Stack.Screen name="sign-in" options={{ headerShown: false }} />
|
||||||
|
<Stack.Screen
|
||||||
|
name="integration-setup"
|
||||||
|
options={{ title: t("Server integrations") }}
|
||||||
|
/>
|
||||||
|
<Stack.Screen name="ai-data-sharing" options={{ title: "AI data sharing" }} />
|
||||||
|
<Stack.Screen name="account" options={{ title: t("Account") }} />
|
||||||
|
<Stack.Screen
|
||||||
|
name="change-password"
|
||||||
|
options={{
|
||||||
|
title: t("Change password"),
|
||||||
|
presentation: "formSheet",
|
||||||
|
sheetAllowedDetents: [0.6, 1],
|
||||||
|
sheetGrabberVisible: true,
|
||||||
}}
|
}}
|
||||||
>
|
/>
|
||||||
<Stack.Screen name="index" options={{ headerShown: false, title: "Rakazo" }} />
|
<Stack.Screen name="models" options={{ title: t("Models") }} />
|
||||||
<Stack.Screen name="sign-in" options={{ headerShown: false }} />
|
<Stack.Screen name="voice" options={{ title: t("Voice") }} />
|
||||||
<Stack.Screen
|
<Stack.Screen name="integrations" options={{ title: t("Integrations") }} />
|
||||||
name="integration-setup"
|
<Stack.Screen
|
||||||
options={{ title: t("Server integrations") }}
|
name="new"
|
||||||
/>
|
options={{
|
||||||
<Stack.Screen name="ai-data-sharing" options={{ title: "AI data sharing" }} />
|
title: t("New bot"),
|
||||||
<Stack.Screen name="account" options={{ title: t("Account") }} />
|
presentation: "modal",
|
||||||
<Stack.Screen
|
gestureEnabled: true,
|
||||||
name="change-password"
|
headerBackVisible: false,
|
||||||
options={{
|
}}
|
||||||
title: t("Change password"),
|
/>
|
||||||
presentation: "formSheet",
|
<Stack.Screen
|
||||||
sheetAllowedDetents: [0.6, 1],
|
name="new-group"
|
||||||
sheetGrabberVisible: true,
|
options={{
|
||||||
}}
|
title: t("New group"),
|
||||||
/>
|
presentation: "modal",
|
||||||
<Stack.Screen name="models" options={{ title: t("Models") }} />
|
gestureEnabled: true,
|
||||||
<Stack.Screen name="voice" options={{ title: t("Voice") }} />
|
}}
|
||||||
<Stack.Screen name="integrations" options={{ title: t("Integrations") }} />
|
/>
|
||||||
<Stack.Screen
|
<Stack.Screen
|
||||||
name="new"
|
name="new-space"
|
||||||
options={{
|
options={{
|
||||||
title: t("New bot"),
|
title: t("New space"),
|
||||||
presentation: "modal",
|
presentation: "modal",
|
||||||
gestureEnabled: true,
|
gestureEnabled: true,
|
||||||
headerBackVisible: false,
|
headerBackVisible: false,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<Stack.Screen
|
<Stack.Screen name="group-thread" options={{ title: t("Group") }} />
|
||||||
name="new-group"
|
<Stack.Screen name="group-settings" options={{ title: t("Group settings") }} />
|
||||||
options={{
|
<Stack.Screen name="bot-settings" options={{ title: t("Chat settings") }} />
|
||||||
title: t("New group"),
|
<Stack.Screen name="thread" options={{ title: t("Thread") }} />
|
||||||
presentation: "modal",
|
<Stack.Screen name="routine" options={{ title: t("Routine") }} />
|
||||||
gestureEnabled: true,
|
<Stack.Screen name="computer" options={{ title: t("Computer") }} />
|
||||||
}}
|
</Stack>
|
||||||
/>
|
<ComputerUpdateProgress />
|
||||||
<Stack.Screen
|
</ThemeProvider>
|
||||||
name="new-space"
|
|
||||||
options={{
|
|
||||||
title: t("New space"),
|
|
||||||
presentation: "modal",
|
|
||||||
gestureEnabled: true,
|
|
||||||
headerBackVisible: false,
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<Stack.Screen name="group-thread" options={{ title: t("Group") }} />
|
|
||||||
<Stack.Screen name="group-settings" options={{ title: t("Group settings") }} />
|
|
||||||
<Stack.Screen name="bot-settings" options={{ title: t("Chat settings") }} />
|
|
||||||
<Stack.Screen name="thread" options={{ title: t("Thread") }} />
|
|
||||||
<Stack.Screen name="routine" options={{ title: t("Routine") }} />
|
|
||||||
<Stack.Screen name="computer" options={{ title: t("Computer") }} />
|
|
||||||
</Stack>
|
|
||||||
<ComputerUpdateProgress />
|
|
||||||
</ThemeProvider>
|
|
||||||
</AvatarStyleProvider>
|
|
||||||
) : (
|
) : (
|
||||||
<View style={{ flex: 1, backgroundColor: String(native.page) }} />
|
<View style={{ flex: 1, backgroundColor: String(native.page) }} />
|
||||||
)}
|
)}
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import type { AvatarStyle } from "@rakazo/contracts";
|
|
||||||
import { useLocalSearchParams, useRouter } from "expo-router";
|
import { useLocalSearchParams, useRouter } from "expo-router";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import {
|
import {
|
||||||
@ -15,8 +14,6 @@ import {
|
|||||||
View,
|
View,
|
||||||
} from "react-native";
|
} from "react-native";
|
||||||
import { SafeAreaView } from "react-native-safe-area-context";
|
import { SafeAreaView } from "react-native-safe-area-context";
|
||||||
import { useAvatarStyle } from "../components/avatar-style";
|
|
||||||
import { BotAvatar } from "../components/bot-avatar";
|
|
||||||
import type { MobileBot, MobileMe } from "../lib/api";
|
import type { MobileBot, MobileMe } from "../lib/api";
|
||||||
import {
|
import {
|
||||||
currentApiBase,
|
currentApiBase,
|
||||||
@ -49,6 +46,7 @@ import { native, useResolvedAppearance, useThemedStyles } from "../lib/native";
|
|||||||
import { registerPushToken } from "../lib/push";
|
import { registerPushToken } from "../lib/push";
|
||||||
import type { AccountUiLocale } from "../lib/ui-locale";
|
import type { AccountUiLocale } from "../lib/ui-locale";
|
||||||
import { ACCOUNT_UI_LOCALES, UI_LOCALE_LABELS } from "../lib/ui-locale";
|
import { ACCOUNT_UI_LOCALES, UI_LOCALE_LABELS } from "../lib/ui-locale";
|
||||||
|
import { alignVoiceWithLocale } from "../lib/voice";
|
||||||
|
|
||||||
/** Render account settings, including the entry point for voice configuration. */
|
/** Render account settings, including the entry point for voice configuration. */
|
||||||
export default function Account() {
|
export default function Account() {
|
||||||
@ -61,8 +59,6 @@ export default function Account() {
|
|||||||
const [localeSaving, setLocaleSaving] = useState(false);
|
const [localeSaving, setLocaleSaving] = useState(false);
|
||||||
const [localeError, setLocaleError] = useState<string | null>(null);
|
const [localeError, setLocaleError] = useState<string | null>(null);
|
||||||
const [pending, setPending] = useState(false);
|
const [pending, setPending] = useState(false);
|
||||||
const [avatarPending, setAvatarPending] = useState(false);
|
|
||||||
const [avatarError, setAvatarError] = useState<string | null>(null);
|
|
||||||
const [notifications, setNotifications] = useState<LiveNotificationSettings>(
|
const [notifications, setNotifications] = useState<LiveNotificationSettings>(
|
||||||
DEFAULT_LIVE_NOTIFICATION_SETTINGS,
|
DEFAULT_LIVE_NOTIFICATION_SETTINGS,
|
||||||
);
|
);
|
||||||
@ -76,7 +72,6 @@ export default function Account() {
|
|||||||
inputTokens: number;
|
inputTokens: number;
|
||||||
outputTokens: number;
|
outputTokens: number;
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
const { avatarStyle, updateAvatarStyle } = useAvatarStyle();
|
|
||||||
const appearance = getCachedAppearancePreference();
|
const appearance = getCachedAppearancePreference();
|
||||||
const styles = useThemedStyles(createAccountStyles);
|
const styles = useThemedStyles(createAccountStyles);
|
||||||
const versionInfo = getAppVersionInfo();
|
const versionInfo = getAppVersionInfo();
|
||||||
@ -127,19 +122,6 @@ export default function Account() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function selectAvatarStyle(next: AvatarStyle) {
|
|
||||||
if (next === avatarStyle) return;
|
|
||||||
setAvatarPending(true);
|
|
||||||
setAvatarError(null);
|
|
||||||
try {
|
|
||||||
await updateAvatarStyle(next);
|
|
||||||
} catch {
|
|
||||||
setAvatarError(t("Couldn't update avatars"));
|
|
||||||
} finally {
|
|
||||||
setAvatarPending(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleSignOut() {
|
async function handleSignOut() {
|
||||||
setPending(true);
|
setPending(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
@ -202,6 +184,7 @@ export default function Account() {
|
|||||||
setLocaleSaving(true);
|
setLocaleSaving(true);
|
||||||
setLocaleError(null);
|
setLocaleError(null);
|
||||||
void setUiLocale(code)
|
void setUiLocale(code)
|
||||||
|
.then((activated) => alignVoiceWithLocale(activated).catch(() => undefined))
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
setLocaleError(t("Could not change language"));
|
setLocaleError(t("Could not change language"));
|
||||||
})
|
})
|
||||||
@ -260,7 +243,7 @@ export default function Account() {
|
|||||||
<Text style={styles.chevron}>›</Text>
|
<Text style={styles.chevron}>›</Text>
|
||||||
</Pressable>
|
</Pressable>
|
||||||
|
|
||||||
<View accessibilityLabel={t("Appearance")} style={styles.avatarSection}>
|
<View accessibilityLabel={t("Appearance")} style={styles.appearanceSection}>
|
||||||
<Text style={styles.settingsTitle}>{t("Appearance")}</Text>
|
<Text style={styles.settingsTitle}>{t("Appearance")}</Text>
|
||||||
<View style={styles.appearanceOptions}>
|
<View style={styles.appearanceOptions}>
|
||||||
{(
|
{(
|
||||||
@ -292,40 +275,6 @@ export default function Account() {
|
|||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
<View accessibilityLabel={t("Avatar style")} style={styles.avatarSection}>
|
|
||||||
<Text style={styles.settingsTitle}>{t("Avatars")}</Text>
|
|
||||||
<View style={styles.avatarOptions}>
|
|
||||||
{(["robot", "organic"] as const).map((style) => {
|
|
||||||
const selected = avatarStyle === style;
|
|
||||||
const styleLabel = style === "robot" ? t("Robot") : t("Organic");
|
|
||||||
return (
|
|
||||||
<Pressable
|
|
||||||
key={style}
|
|
||||||
accessibilityLabel={t("{style} avatars", { style: styleLabel })}
|
|
||||||
accessibilityRole="button"
|
|
||||||
accessibilityState={{ selected, disabled: avatarPending }}
|
|
||||||
disabled={avatarPending}
|
|
||||||
onPress={() => void selectAvatarStyle(style)}
|
|
||||||
style={({ pressed }) => [
|
|
||||||
styles.avatarOption,
|
|
||||||
selected && styles.avatarOptionSelected,
|
|
||||||
pressed && styles.pressed,
|
|
||||||
]}
|
|
||||||
>
|
|
||||||
<BotAvatar
|
|
||||||
color={style === "robot" ? "#8B5CF6" : "#D62F8B"}
|
|
||||||
identity="avatar-preview"
|
|
||||||
size={42}
|
|
||||||
variant={style}
|
|
||||||
/>
|
|
||||||
<Text style={styles.avatarLabel}>{styleLabel}</Text>
|
|
||||||
</Pressable>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</View>
|
|
||||||
{avatarError ? <Text style={styles.error}>{avatarError}</Text> : null}
|
|
||||||
</View>
|
|
||||||
|
|
||||||
<Pressable
|
<Pressable
|
||||||
accessibilityRole="button"
|
accessibilityRole="button"
|
||||||
accessibilityLabel={t("Language")}
|
accessibilityLabel={t("Language")}
|
||||||
@ -404,15 +353,17 @@ export default function Account() {
|
|||||||
</View>
|
</View>
|
||||||
) : null}
|
) : null}
|
||||||
|
|
||||||
<Pressable
|
{me?.modelsManaged ? null : (
|
||||||
accessibilityRole="button"
|
<Pressable
|
||||||
disabled={pending}
|
accessibilityRole="button"
|
||||||
onPress={() => router.push("/models")}
|
disabled={pending}
|
||||||
style={({ pressed }) => [styles.settingsButton, pressed && styles.pressed]}
|
onPress={() => router.push("/models")}
|
||||||
>
|
style={({ pressed }) => [styles.settingsButton, pressed && styles.pressed]}
|
||||||
<Text style={styles.settingsTitle}>{t("Models")}</Text>
|
>
|
||||||
<Text style={styles.chevron}>›</Text>
|
<Text style={styles.settingsTitle}>{t("Models")}</Text>
|
||||||
</Pressable>
|
<Text style={styles.chevron}>›</Text>
|
||||||
|
</Pressable>
|
||||||
|
)}
|
||||||
|
|
||||||
<Pressable
|
<Pressable
|
||||||
accessibilityRole="button"
|
accessibilityRole="button"
|
||||||
@ -649,7 +600,7 @@ function createAccountStyles() {
|
|||||||
alignItems: "center",
|
alignItems: "center",
|
||||||
justifyContent: "space-between",
|
justifyContent: "space-between",
|
||||||
},
|
},
|
||||||
avatarSection: {
|
appearanceSection: {
|
||||||
borderRadius: 16,
|
borderRadius: 16,
|
||||||
backgroundColor: native.fill,
|
backgroundColor: native.fill,
|
||||||
padding: 18,
|
padding: 18,
|
||||||
@ -677,29 +628,6 @@ function createAccountStyles() {
|
|||||||
fontSize: 14,
|
fontSize: 14,
|
||||||
fontWeight: "600",
|
fontWeight: "600",
|
||||||
},
|
},
|
||||||
avatarOptions: {
|
|
||||||
flexDirection: "row",
|
|
||||||
gap: 12,
|
|
||||||
},
|
|
||||||
avatarOption: {
|
|
||||||
flex: 1,
|
|
||||||
minHeight: 86,
|
|
||||||
borderRadius: 14,
|
|
||||||
borderWidth: StyleSheet.hairlineWidth,
|
|
||||||
borderColor: native.tertiaryLabel,
|
|
||||||
alignItems: "center",
|
|
||||||
justifyContent: "center",
|
|
||||||
gap: 8,
|
|
||||||
},
|
|
||||||
avatarOptionSelected: {
|
|
||||||
borderColor: native.label,
|
|
||||||
backgroundColor: native.fillPressed,
|
|
||||||
},
|
|
||||||
avatarLabel: {
|
|
||||||
color: native.label,
|
|
||||||
fontSize: 14,
|
|
||||||
fontWeight: "600",
|
|
||||||
},
|
|
||||||
settingsTitle: {
|
settingsTitle: {
|
||||||
color: native.label,
|
color: native.label,
|
||||||
fontSize: 17,
|
fontSize: 17,
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import { useEffect, useMemo, useState } from "react";
|
|||||||
import { Pressable, ScrollView, Text, TextInput, View } from "react-native";
|
import { Pressable, ScrollView, Text, TextInput, View } from "react-native";
|
||||||
import { BotAvatar } from "../components/bot-avatar";
|
import { BotAvatar } from "../components/bot-avatar";
|
||||||
import { ComputerModePicker } from "../components/computer-mode-picker";
|
import { ComputerModePicker } from "../components/computer-mode-picker";
|
||||||
|
import { OoAtelier } from "../components/oo-atelier";
|
||||||
import {
|
import {
|
||||||
type MobileBot,
|
type MobileBot,
|
||||||
type MobileMe,
|
type MobileMe,
|
||||||
@ -348,53 +349,31 @@ export default function BotSettingsScreen() {
|
|||||||
textAlignVertical: "top",
|
textAlignVertical: "top",
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<Text style={{ color: tokens.mutedForeground, marginTop: 16, fontSize: 14 }}>
|
<View style={{ marginTop: 4 }}>
|
||||||
{t("Color")}
|
<OoAtelier value={color} identity={bot?.id ?? ""} onChange={setColor} />
|
||||||
</Text>
|
</View>
|
||||||
<ScrollView
|
|
||||||
horizontal
|
|
||||||
showsHorizontalScrollIndicator={false}
|
|
||||||
contentContainerStyle={{ gap: 10, marginTop: 8 }}
|
|
||||||
accessibilityRole="radiogroup"
|
|
||||||
>
|
|
||||||
{BOT_COLORS.map((option, index) => (
|
|
||||||
<Pressable
|
|
||||||
key={option}
|
|
||||||
accessibilityRole="radio"
|
|
||||||
accessibilityLabel={t("Color {number}", { number: index + 1 })}
|
|
||||||
accessibilityState={{ checked: color === option }}
|
|
||||||
onPress={() => setColor(option)}
|
|
||||||
style={{
|
|
||||||
width: 36,
|
|
||||||
height: 36,
|
|
||||||
borderRadius: 18,
|
|
||||||
backgroundColor: option,
|
|
||||||
borderWidth: 3,
|
|
||||||
borderColor: color === option ? tokens.foreground : "transparent",
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
</ScrollView>
|
|
||||||
<ComputerModePicker value={computerMode} onChange={setComputerMode} />
|
<ComputerModePicker value={computerMode} onChange={setComputerMode} />
|
||||||
<Pressable
|
{me?.modelsManaged ? null : (
|
||||||
accessibilityRole="button"
|
<Pressable
|
||||||
accessibilityLabel={t("Advanced")}
|
accessibilityRole="button"
|
||||||
accessibilityState={{ expanded: advancedOpen }}
|
accessibilityLabel={t("Advanced")}
|
||||||
onPress={() => setAdvancedOpen((open) => !open)}
|
accessibilityState={{ expanded: advancedOpen }}
|
||||||
style={{
|
onPress={() => setAdvancedOpen((open) => !open)}
|
||||||
marginTop: 20,
|
style={{
|
||||||
minHeight: 44,
|
marginTop: 20,
|
||||||
flexDirection: "row",
|
minHeight: 44,
|
||||||
alignItems: "center",
|
flexDirection: "row",
|
||||||
justifyContent: "space-between",
|
alignItems: "center",
|
||||||
}}
|
justifyContent: "space-between",
|
||||||
>
|
}}
|
||||||
<Text style={{ color: tokens.mutedForeground, fontSize: 14 }}>{t("Advanced")}</Text>
|
>
|
||||||
<Text style={{ color: tokens.mutedForeground, fontSize: 18 }}>
|
<Text style={{ color: tokens.mutedForeground, fontSize: 14 }}>{t("Advanced")}</Text>
|
||||||
{advancedOpen ? "⌃" : "⌄"}
|
<Text style={{ color: tokens.mutedForeground, fontSize: 18 }}>
|
||||||
</Text>
|
{advancedOpen ? "⌃" : "⌄"}
|
||||||
</Pressable>
|
</Text>
|
||||||
{advancedOpen ? (
|
</Pressable>
|
||||||
|
)}
|
||||||
|
{advancedOpen && !me?.modelsManaged ? (
|
||||||
<View>
|
<View>
|
||||||
<Text
|
<Text
|
||||||
style={{
|
style={{
|
||||||
|
|||||||
@ -3,8 +3,15 @@ import { Stack, useLocalSearchParams, useRouter } from "expo-router";
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { Alert, Pressable, ScrollView, Text, TextInput } from "react-native";
|
import { Alert, Pressable, ScrollView, Text, TextInput } from "react-native";
|
||||||
import { BotMemberPicker } from "../components/bot-member-picker";
|
import { BotMemberPicker } from "../components/bot-member-picker";
|
||||||
import { type MobileBot, type MobileGroup, rpc } from "../lib/api";
|
import {
|
||||||
|
type MobileBot,
|
||||||
|
type MobileGroup,
|
||||||
|
type MobileSpace,
|
||||||
|
type MobileSpaceNavigation,
|
||||||
|
rpc,
|
||||||
|
} from "../lib/api";
|
||||||
import { useI18n } from "../lib/i18n";
|
import { useI18n } from "../lib/i18n";
|
||||||
|
import { canDeleteInboxSpace, removeInboxSpace } from "../lib/inbox-spaces";
|
||||||
import { useMobileTokens } from "../lib/native";
|
import { useMobileTokens } from "../lib/native";
|
||||||
|
|
||||||
export default function GroupSettingsScreen() {
|
export default function GroupSettingsScreen() {
|
||||||
@ -13,12 +20,16 @@ export default function GroupSettingsScreen() {
|
|||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { groupId } = useLocalSearchParams<{ groupId: string }>();
|
const { groupId } = useLocalSearchParams<{ groupId: string }>();
|
||||||
const [group, setGroup] = useState<MobileGroup | null>(null);
|
const [group, setGroup] = useState<MobileGroup | null>(null);
|
||||||
|
const [projectSpace, setProjectSpace] = useState<MobileSpace | null>(null);
|
||||||
const [bots, setBots] = useState<MobileBot[]>([]);
|
const [bots, setBots] = useState<MobileBot[]>([]);
|
||||||
const [name, setName] = useState("");
|
const [name, setName] = useState("");
|
||||||
const [selected, setSelected] = useState<string[]>([]);
|
const [selected, setSelected] = useState<string[]>([]);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [pending, setPending] = useState(false);
|
const [pending, setPending] = useState(false);
|
||||||
|
|
||||||
|
const isMainConversation = group?.kind === "main";
|
||||||
|
const isProjectMain = isMainConversation && projectSpace !== null && !projectSpace.isDefault;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!groupId) return;
|
if (!groupId) return;
|
||||||
void Promise.all([
|
void Promise.all([
|
||||||
@ -26,11 +37,19 @@ export default function GroupSettingsScreen() {
|
|||||||
(groups) => groups.find((row) => row.id === groupId) ?? null,
|
(groups) => groups.find((row) => row.id === groupId) ?? null,
|
||||||
),
|
),
|
||||||
rpc<MobileBot[]>("bots/list"),
|
rpc<MobileBot[]>("bots/list"),
|
||||||
|
rpc<MobileSpaceNavigation>("spaces/list").catch(() => null),
|
||||||
])
|
])
|
||||||
.then(([nextGroup, nextBots]) => {
|
.then(([nextGroup, nextBots, navigation]) => {
|
||||||
if (!nextGroup) throw new Error(t("Group not found"));
|
if (!nextGroup) throw new Error(t("Group not found"));
|
||||||
setGroup(nextGroup);
|
setGroup(nextGroup);
|
||||||
setName(nextGroup.name);
|
const space =
|
||||||
|
navigation?.spaces.find(
|
||||||
|
(item) => item.id === nextGroup.spaceId || item.id === navigation.current.id,
|
||||||
|
) ?? null;
|
||||||
|
setProjectSpace(space);
|
||||||
|
setName(
|
||||||
|
nextGroup.kind === "main" && space && !space.isDefault ? space.name : nextGroup.name,
|
||||||
|
);
|
||||||
setSelected(nextGroup.members.map((member) => member.botId));
|
setSelected(nextGroup.members.map((member) => member.botId));
|
||||||
setBots(nextBots.filter((bot) => !bot.archivedAt));
|
setBots(nextBots.filter((bot) => !bot.archivedAt));
|
||||||
})
|
})
|
||||||
@ -42,10 +61,20 @@ export default function GroupSettingsScreen() {
|
|||||||
setPending(true);
|
setPending(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
|
const trimmed = name.trim();
|
||||||
|
if (isProjectMain && projectSpace) {
|
||||||
|
if (trimmed && trimmed !== projectSpace.name) {
|
||||||
|
await rpc("spaces/update", { spaceId: projectSpace.id, name: trimmed });
|
||||||
|
}
|
||||||
|
router.back();
|
||||||
|
return;
|
||||||
|
}
|
||||||
const input: { groupId: string; name?: string; botIds?: string[] } = { groupId };
|
const input: { groupId: string; name?: string; botIds?: string[] } = { groupId };
|
||||||
if (name.trim() !== group.name) input.name = name.trim();
|
if (trimmed !== group.name) input.name = trimmed;
|
||||||
const memberIds = group.members.map((member) => member.botId).join(",");
|
if (!isMainConversation) {
|
||||||
if (selected.join(",") !== memberIds) input.botIds = selected;
|
const memberIds = group.members.map((member) => member.botId).join(",");
|
||||||
|
if (selected.join(",") !== memberIds) input.botIds = selected;
|
||||||
|
}
|
||||||
if (input.name || input.botIds) await rpc("groups/update", input);
|
if (input.name || input.botIds) await rpc("groups/update", input);
|
||||||
router.back();
|
router.back();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -57,6 +86,29 @@ export default function GroupSettingsScreen() {
|
|||||||
|
|
||||||
function remove() {
|
function remove() {
|
||||||
if (!groupId || !group) return;
|
if (!groupId || !group) return;
|
||||||
|
if (isProjectMain && projectSpace) {
|
||||||
|
Alert.alert(
|
||||||
|
t("Delete {name}?", { name: projectSpace.name }),
|
||||||
|
t("This removes the empty space for everyone."),
|
||||||
|
[
|
||||||
|
{ text: t("Cancel"), style: "cancel" },
|
||||||
|
{
|
||||||
|
text: t("Delete"),
|
||||||
|
style: "destructive",
|
||||||
|
onPress: () =>
|
||||||
|
void removeInboxSpace(projectSpace.id, async () => {})
|
||||||
|
.then(() => router.replace("/"))
|
||||||
|
.catch((err) =>
|
||||||
|
Alert.alert(
|
||||||
|
t("Could not delete space"),
|
||||||
|
err instanceof Error ? err.message : t("Try again."),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
Alert.alert(group.name, t("Delete this group? Bots and their solo threads are kept."), [
|
Alert.alert(group.name, t("Delete this group? Bots and their solo threads are kept."), [
|
||||||
{ text: t("Cancel"), style: "cancel" },
|
{ text: t("Cancel"), style: "cancel" },
|
||||||
{
|
{
|
||||||
@ -75,6 +127,12 @@ export default function GroupSettingsScreen() {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const saveDisabled =
|
||||||
|
!name.trim() ||
|
||||||
|
pending ||
|
||||||
|
(!isMainConversation &&
|
||||||
|
(selected.length < GROUP_MEMBER_MIN || selected.length > GROUP_MEMBER_MAX));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Stack.Screen options={{ title: t("Group settings") }} />
|
<Stack.Screen options={{ title: t("Group settings") }} />
|
||||||
@ -97,34 +155,27 @@ export default function GroupSettingsScreen() {
|
|||||||
fontSize: 16,
|
fontSize: 16,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<Text style={{ color: tokens.mutedForeground, fontSize: 14, marginTop: 20 }}>
|
{!isMainConversation ? (
|
||||||
{t("Members")}
|
<>
|
||||||
</Text>
|
<Text style={{ color: tokens.mutedForeground, fontSize: 14, marginTop: 20 }}>
|
||||||
<BotMemberPicker
|
{t("Members")}
|
||||||
bots={bots}
|
</Text>
|
||||||
selected={selected}
|
<BotMemberPicker
|
||||||
onChange={setSelected}
|
bots={bots}
|
||||||
disabled={pending}
|
selected={selected}
|
||||||
/>
|
onChange={setSelected}
|
||||||
|
disabled={pending}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
{error ? <Text style={{ color: tokens.destructive, marginTop: 12 }}>{error}</Text> : null}
|
{error ? <Text style={{ color: tokens.destructive, marginTop: 12 }}>{error}</Text> : null}
|
||||||
<Pressable
|
<Pressable
|
||||||
onPress={() => void save()}
|
onPress={() => void save()}
|
||||||
disabled={
|
disabled={saveDisabled}
|
||||||
!name.trim() ||
|
|
||||||
selected.length < GROUP_MEMBER_MIN ||
|
|
||||||
selected.length > GROUP_MEMBER_MAX ||
|
|
||||||
pending
|
|
||||||
}
|
|
||||||
style={{
|
style={{
|
||||||
marginTop: 24,
|
marginTop: 24,
|
||||||
backgroundColor: tokens.primary,
|
backgroundColor: tokens.primary,
|
||||||
opacity:
|
opacity: saveDisabled ? 0.5 : 1,
|
||||||
!name.trim() ||
|
|
||||||
selected.length < GROUP_MEMBER_MIN ||
|
|
||||||
selected.length > GROUP_MEMBER_MAX ||
|
|
||||||
pending
|
|
||||||
? 0.5
|
|
||||||
: 1,
|
|
||||||
borderRadius: 11,
|
borderRadius: 11,
|
||||||
padding: 14,
|
padding: 14,
|
||||||
alignItems: "center",
|
alignItems: "center",
|
||||||
@ -134,19 +185,35 @@ export default function GroupSettingsScreen() {
|
|||||||
{pending ? t("Saving…") : t("Save")}
|
{pending ? t("Saving…") : t("Save")}
|
||||||
</Text>
|
</Text>
|
||||||
</Pressable>
|
</Pressable>
|
||||||
<Pressable
|
{isProjectMain && projectSpace && canDeleteInboxSpace(projectSpace) ? (
|
||||||
onPress={remove}
|
<Pressable
|
||||||
style={{
|
onPress={remove}
|
||||||
marginTop: 16,
|
style={{
|
||||||
borderRadius: 11,
|
marginTop: 16,
|
||||||
borderWidth: 1,
|
borderRadius: 11,
|
||||||
borderColor: tokens.border,
|
borderWidth: 1,
|
||||||
padding: 14,
|
borderColor: tokens.border,
|
||||||
alignItems: "center",
|
padding: 14,
|
||||||
}}
|
alignItems: "center",
|
||||||
>
|
}}
|
||||||
<Text style={{ color: tokens.destructive, fontSize: 16 }}>{t("Delete group")}</Text>
|
>
|
||||||
</Pressable>
|
<Text style={{ color: tokens.destructive, fontSize: 16 }}>{t("Delete space")}</Text>
|
||||||
|
</Pressable>
|
||||||
|
) : !isMainConversation ? (
|
||||||
|
<Pressable
|
||||||
|
onPress={remove}
|
||||||
|
style={{
|
||||||
|
marginTop: 16,
|
||||||
|
borderRadius: 11,
|
||||||
|
borderWidth: 1,
|
||||||
|
borderColor: tokens.border,
|
||||||
|
padding: 14,
|
||||||
|
alignItems: "center",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Text style={{ color: tokens.destructive, fontSize: 16 }}>{t("Delete group")}</Text>
|
||||||
|
</Pressable>
|
||||||
|
) : null}
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import type { AutopilotRecent } from "@rakazo/contracts";
|
||||||
import {
|
import {
|
||||||
normalizeCreateBotProfile,
|
normalizeCreateBotProfile,
|
||||||
type RunActivityRow,
|
type RunActivityRow,
|
||||||
@ -5,6 +6,7 @@ import {
|
|||||||
type SpaceBot,
|
type SpaceBot,
|
||||||
type SpaceGroup,
|
type SpaceGroup,
|
||||||
} from "@rakazo/contracts";
|
} from "@rakazo/contracts";
|
||||||
|
import { projectLook, runChatTitle } from "@rakazo/core";
|
||||||
import { botColors } from "@rakazo/ui-tokens";
|
import { botColors } from "@rakazo/ui-tokens";
|
||||||
import { Redirect, useFocusEffect, useRouter } from "expo-router";
|
import { Redirect, useFocusEffect, useRouter } from "expo-router";
|
||||||
import { type ReactNode, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
import { type ReactNode, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||||
@ -43,9 +45,11 @@ import {
|
|||||||
rpc,
|
rpc,
|
||||||
selectedSpaceId,
|
selectedSpaceId,
|
||||||
selectInitialSpace,
|
selectInitialSpace,
|
||||||
|
selectLibrarySpace,
|
||||||
selectSpace,
|
selectSpace,
|
||||||
} from "../lib/api";
|
} from "../lib/api";
|
||||||
import { mobileTokens, resolveMobileAppearance } from "../lib/appearance";
|
import { mobileShellIconSizes, mobileTokens, resolveMobileAppearance } from "../lib/appearance";
|
||||||
|
import { autopilotSummary } from "../lib/autopilot";
|
||||||
import { allowFocusPrompt, scheduleFocusPrompt } from "../lib/focus-prompt";
|
import { allowFocusPrompt, scheduleFocusPrompt } from "../lib/focus-prompt";
|
||||||
import { t, useI18n } from "../lib/i18n";
|
import { t, useI18n } from "../lib/i18n";
|
||||||
import { botTag, filterBots, formatThreadTime, userInitials } from "../lib/inbox";
|
import { botTag, filterBots, formatThreadTime, userInitials } from "../lib/inbox";
|
||||||
@ -87,6 +91,7 @@ export default function Home() {
|
|||||||
const [botSections, setBotSections] = useState<MobileBotSection[]>([]);
|
const [botSections, setBotSections] = useState<MobileBotSection[]>([]);
|
||||||
const [spaces, setSpaces] = useState<MobileSpace[]>([]);
|
const [spaces, setSpaces] = useState<MobileSpace[]>([]);
|
||||||
const [me, setMe] = useState<MobileMe | null>(null);
|
const [me, setMe] = useState<MobileMe | null>(null);
|
||||||
|
const [autopilots, setAutopilots] = useState<AutopilotRecent[]>([]);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [ready, setReady] = useState(false);
|
const [ready, setReady] = useState(false);
|
||||||
const [hasSession, setHasSession] = useState(false);
|
const [hasSession, setHasSession] = useState(false);
|
||||||
@ -131,9 +136,10 @@ export default function Home() {
|
|||||||
const requestId = ++inboxRequestId.current;
|
const requestId = ++inboxRequestId.current;
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const [navigation, nextMe] = await Promise.all([
|
const [navigation, nextMe, recentAutopilots] = await Promise.all([
|
||||||
rpc<MobileSpaceNavigation>("spaces/list"),
|
rpc<MobileSpaceNavigation>("spaces/list"),
|
||||||
rpc<MobileMe>("me"),
|
rpc<MobileMe>("me"),
|
||||||
|
rpc<{ items: AutopilotRecent[] }>("autopilot/recent").catch(() => null),
|
||||||
]);
|
]);
|
||||||
if (requestId !== inboxRequestId.current) return;
|
if (requestId !== inboxRequestId.current) return;
|
||||||
if (!(await selectInitialSpace(nextMe.spaceId))) {
|
if (!(await selectInitialSpace(nextMe.spaceId))) {
|
||||||
@ -145,6 +151,7 @@ export default function Home() {
|
|||||||
setGroups(navigation.current.groups);
|
setGroups(navigation.current.groups);
|
||||||
setSpaces(navigation.spaces);
|
setSpaces(navigation.spaces);
|
||||||
setMe(nextMe);
|
setMe(nextMe);
|
||||||
|
if (recentAutopilots) setAutopilots(recentAutopilots.items);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (requestId !== inboxRequestId.current) return;
|
if (requestId !== inboxRequestId.current) return;
|
||||||
setError(err instanceof Error ? err.message : t("Could not load bots"));
|
setError(err instanceof Error ? err.message : t("Could not load bots"));
|
||||||
@ -405,6 +412,7 @@ export default function Home() {
|
|||||||
return;
|
return;
|
||||||
creatingBotRef.current = true;
|
creatingBotRef.current = true;
|
||||||
try {
|
try {
|
||||||
|
await selectLibrarySpace();
|
||||||
// Authoritative roster so a slow home fetch does not treat later bots as first.
|
// Authoritative roster so a slow home fetch does not treat later bots as first.
|
||||||
// A failed list is unknown — use the delayed path rather than assuming first.
|
// A failed list is unknown — use the delayed path rather than assuming first.
|
||||||
const existing = await rpc<MobileBot[]>("bots/list").catch(() => null);
|
const existing = await rpc<MobileBot[]>("bots/list").catch(() => null);
|
||||||
@ -456,7 +464,7 @@ export default function Home() {
|
|||||||
<NativeSymbol
|
<NativeSymbol
|
||||||
ios={activityMode ? "bell.fill" : "bell"}
|
ios={activityMode ? "bell.fill" : "bell"}
|
||||||
android={activityMode ? "notifications" : "notifications-outline"}
|
android={activityMode ? "notifications" : "notifications-outline"}
|
||||||
size={17}
|
size={mobileShellIconSizes.md}
|
||||||
color={activityMode ? tokens.primaryForeground : tokens.foreground}
|
color={activityMode ? tokens.primaryForeground : tokens.foreground}
|
||||||
/>
|
/>
|
||||||
</CircleButton>
|
</CircleButton>
|
||||||
@ -470,7 +478,7 @@ export default function Home() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<NativeSymbol ios="magnifyingglass" android="search" size={17} />
|
<NativeSymbol ios="magnifyingglass" android="search" size={mobileShellIconSizes.md} />
|
||||||
</CircleButton>
|
</CircleButton>
|
||||||
<CircleButton
|
<CircleButton
|
||||||
accessibilityLabel={t("Create")}
|
accessibilityLabel={t("Create")}
|
||||||
@ -484,7 +492,7 @@ export default function Home() {
|
|||||||
]);
|
]);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<NativeSymbol ios="plus" android="add" size={18} />
|
<NativeSymbol ios="plus" android="add" size={mobileShellIconSizes.lg} />
|
||||||
</CircleButton>
|
</CircleButton>
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
@ -543,11 +551,15 @@ export default function Home() {
|
|||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
ListHeaderComponent={
|
ListHeaderComponent={
|
||||||
activityMode &&
|
!searching && !query.trim() ? (
|
||||||
!searching &&
|
<>
|
||||||
!query.trim() &&
|
{autopilots.length > 0 ? (
|
||||||
(activity.active.length > 0 || activity.recent.length > 0) ? (
|
<AutopilotSection autopilots={autopilots} bots={bots} groups={groups} />
|
||||||
<ActivitySection activity={activity} bots={bots} />
|
) : null}
|
||||||
|
{activityMode && (activity.active.length > 0 || activity.recent.length > 0) ? (
|
||||||
|
<ActivitySection activity={activity} bots={bots} />
|
||||||
|
) : null}
|
||||||
|
</>
|
||||||
) : null
|
) : null
|
||||||
}
|
}
|
||||||
ListEmptyComponent={
|
ListEmptyComponent={
|
||||||
@ -585,10 +597,25 @@ export default function Home() {
|
|||||||
}}
|
}}
|
||||||
disabled={spaceBusy}
|
disabled={spaceBusy}
|
||||||
onPress={() => {
|
onPress={() => {
|
||||||
if (item.space) void chooseInboxSpace(item.space.id);
|
if (!item.space) return;
|
||||||
|
const targetSpace = item.space;
|
||||||
|
const main = !targetSpace.isDefault
|
||||||
|
? targetSpace.groups.find((group) => group.kind === "main")
|
||||||
|
: undefined;
|
||||||
|
if (main) {
|
||||||
|
void openMobileSpace(targetSpace.id, () =>
|
||||||
|
router.push({
|
||||||
|
pathname: "/group-thread",
|
||||||
|
params: { groupId: main.id, name: targetSpace.name },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void chooseInboxSpace(targetSpace.id);
|
||||||
}}
|
}}
|
||||||
style={({ pressed }) => [styles.spaceSelect, pressed && styles.rowPressed]}
|
style={({ pressed }) => [styles.spaceSelect, pressed && styles.rowPressed]}
|
||||||
>
|
>
|
||||||
|
<ProjectTile space={item.space} />
|
||||||
<Text style={styles.spaceTitle}>{item.title}</Text>
|
<Text style={styles.spaceTitle}>{item.title}</Text>
|
||||||
</Pressable>
|
</Pressable>
|
||||||
{canDeleteInboxSpace(item.space) ? (
|
{canDeleteInboxSpace(item.space) ? (
|
||||||
@ -601,7 +628,11 @@ export default function Home() {
|
|||||||
}}
|
}}
|
||||||
style={({ pressed }) => [styles.spaceActions, pressed && styles.rowPressed]}
|
style={({ pressed }) => [styles.spaceActions, pressed && styles.rowPressed]}
|
||||||
>
|
>
|
||||||
<NativeSymbol ios="ellipsis" android="ellipsis-horizontal" size={20} />
|
<NativeSymbol
|
||||||
|
ios="ellipsis"
|
||||||
|
android="ellipsis-horizontal"
|
||||||
|
size={mobileShellIconSizes.lg}
|
||||||
|
/>
|
||||||
</Pressable>
|
</Pressable>
|
||||||
) : null}
|
) : null}
|
||||||
</View>
|
</View>
|
||||||
@ -699,16 +730,17 @@ function ActivitySection({
|
|||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const botsById = useMemo(() => new Map(bots.map((bot) => [bot.id, bot])), [bots]);
|
const botsById = useMemo(() => new Map(bots.map((bot) => [bot.id, bot])), [bots]);
|
||||||
const openRun = (run: RunActivityRow) => {
|
const openRun = (run: RunActivityRow) =>
|
||||||
if (run.groupId) {
|
void openMobileSpace(run.spaceId, () => {
|
||||||
router.push({
|
if (run.groupId) {
|
||||||
pathname: "/group-thread",
|
router.push({
|
||||||
params: { groupId: run.groupId, name: run.groupName ?? t("Group") },
|
pathname: "/group-thread",
|
||||||
});
|
params: { groupId: run.groupId, name: run.groupName ?? t("Group") },
|
||||||
return;
|
});
|
||||||
}
|
return;
|
||||||
router.push({ pathname: "/thread", params: { botId: run.botId, name: run.botName } });
|
}
|
||||||
};
|
router.push({ pathname: "/thread", params: { botId: run.botId, name: run.botName } });
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={styles.activitySection}>
|
<View style={styles.activitySection}>
|
||||||
@ -744,6 +776,67 @@ function ActivitySection({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The night's work, like Today on web: each autopilot that runs, waits, or ended within a day. */
|
||||||
|
function AutopilotSection({
|
||||||
|
autopilots,
|
||||||
|
bots,
|
||||||
|
groups,
|
||||||
|
}: {
|
||||||
|
autopilots: AutopilotRecent[];
|
||||||
|
bots: MobileBot[];
|
||||||
|
groups: MobileGroup[];
|
||||||
|
}) {
|
||||||
|
const styles = useThemedStyles(createHomeStyles);
|
||||||
|
const { t } = useI18n();
|
||||||
|
const router = useRouter();
|
||||||
|
const botsById = useMemo(() => new Map(bots.map((bot) => [bot.id, bot])), [bots]);
|
||||||
|
const open = (autopilot: AutopilotRecent) =>
|
||||||
|
void openMobileSpace(autopilot.spaceId, () =>
|
||||||
|
router.push(
|
||||||
|
autopilot.groupId
|
||||||
|
? {
|
||||||
|
pathname: "/group-thread",
|
||||||
|
params: {
|
||||||
|
groupId: autopilot.groupId,
|
||||||
|
name: groups.find((group) => group.id === autopilot.groupId)?.name ?? t("Group"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
pathname: "/thread",
|
||||||
|
params: {
|
||||||
|
botId: autopilot.botId,
|
||||||
|
name: botsById.get(autopilot.botId)?.name ?? "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<View style={styles.activitySection} testID="home-autopilot">
|
||||||
|
<Text style={styles.sectionHeading}>{t("Autopilot")}</Text>
|
||||||
|
{autopilots.map((autopilot) => {
|
||||||
|
const { detail, trail } = autopilotSummary(autopilot);
|
||||||
|
return (
|
||||||
|
<ConversationRow
|
||||||
|
key={autopilot.id}
|
||||||
|
title={autopilot.goal}
|
||||||
|
preview={detail}
|
||||||
|
time={trail}
|
||||||
|
accessibilityLabel={`${autopilot.goal}, ${trail}`}
|
||||||
|
onPress={() => open(autopilot)}
|
||||||
|
avatar={
|
||||||
|
<BotAvatar
|
||||||
|
identity={autopilot.botId}
|
||||||
|
color={botsById.get(autopilot.botId)?.color ?? FALLBACK_COLOR}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function ActivityRow({
|
function ActivityRow({
|
||||||
run,
|
run,
|
||||||
bot,
|
bot,
|
||||||
@ -753,7 +846,7 @@ function ActivityRow({
|
|||||||
bot?: MobileBot;
|
bot?: MobileBot;
|
||||||
onPress: () => void;
|
onPress: () => void;
|
||||||
}) {
|
}) {
|
||||||
const title = run.groupName ? `${run.botName} · ${run.groupName}` : run.botName;
|
const title = runChatTitle(run);
|
||||||
const status = activityStatusLabel(run.status);
|
const status = activityStatusLabel(run.status);
|
||||||
const preview = run.promptSnippet ? `${run.promptSnippet} · ${status}` : status;
|
const preview = run.promptSnippet ? `${run.promptSnippet} · ${status}` : status;
|
||||||
return (
|
return (
|
||||||
@ -831,6 +924,16 @@ function ConversationRow({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function ProjectTile({ space }: { space: Pick<InboxSpace, "icon" | "color"> }) {
|
||||||
|
const styles = useThemedStyles(createHomeStyles);
|
||||||
|
const { icon, color } = projectLook(space);
|
||||||
|
return (
|
||||||
|
<View style={[styles.projectTile, { backgroundColor: color.hex }]}>
|
||||||
|
<NativeSymbol ios={icon.ios} android={icon.android as never} size={15} color={color.ink} />
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function CircleButton({
|
function CircleButton({
|
||||||
children,
|
children,
|
||||||
onPress,
|
onPress,
|
||||||
@ -988,9 +1091,9 @@ function createHomeStyles() {
|
|||||||
gap: 12,
|
gap: 12,
|
||||||
},
|
},
|
||||||
circleButton: {
|
circleButton: {
|
||||||
width: 40,
|
width: 44,
|
||||||
height: 40,
|
height: 44,
|
||||||
borderRadius: 20,
|
borderRadius: 22,
|
||||||
backgroundColor: native.fillPressed,
|
backgroundColor: native.fillPressed,
|
||||||
alignItems: "center",
|
alignItems: "center",
|
||||||
justifyContent: "center",
|
justifyContent: "center",
|
||||||
@ -1116,6 +1219,15 @@ function createHomeStyles() {
|
|||||||
spaceSelect: {
|
spaceSelect: {
|
||||||
flex: 1,
|
flex: 1,
|
||||||
minHeight: 44,
|
minHeight: 44,
|
||||||
|
flexDirection: "row",
|
||||||
|
alignItems: "center",
|
||||||
|
gap: 10,
|
||||||
|
},
|
||||||
|
projectTile: {
|
||||||
|
width: 28,
|
||||||
|
height: 28,
|
||||||
|
borderRadius: 8,
|
||||||
|
alignItems: "center",
|
||||||
justifyContent: "center",
|
justifyContent: "center",
|
||||||
},
|
},
|
||||||
spaceTitle: {
|
spaceTitle: {
|
||||||
|
|||||||
@ -5,7 +5,10 @@ import {
|
|||||||
CONNECTION_CATALOG_PAGE_SIZE,
|
CONNECTION_CATALOG_PAGE_SIZE,
|
||||||
EMPTY_PLUGIN_CATALOG_MESSAGE,
|
EMPTY_PLUGIN_CATALOG_MESSAGE,
|
||||||
filterConnectionCatalogItems,
|
filterConnectionCatalogItems,
|
||||||
|
GITHUB_MCP_SOURCE,
|
||||||
|
GITHUB_TOKEN_URL,
|
||||||
humanizeToolName,
|
humanizeToolName,
|
||||||
|
isGithubInstall,
|
||||||
} from "@rakazo/core";
|
} from "@rakazo/core";
|
||||||
import { useEffect, useMemo, useRef, useState } from "react";
|
import { useEffect, useMemo, useRef, useState } from "react";
|
||||||
import {
|
import {
|
||||||
@ -84,6 +87,11 @@ export default function Integrations() {
|
|||||||
const [query, setQuery] = useState("");
|
const [query, setQuery] = useState("");
|
||||||
const [visibleCount, setVisibleCount] = useState(CONNECTION_CATALOG_PAGE_SIZE);
|
const [visibleCount, setVisibleCount] = useState(CONNECTION_CATALOG_PAGE_SIZE);
|
||||||
const [sources, setSources] = useState<CapabilityInstall[]>([]);
|
const [sources, setSources] = useState<CapabilityInstall[]>([]);
|
||||||
|
const github = sources.find(isGithubInstall);
|
||||||
|
const customSources = sources.filter((source) => !isGithubInstall(source));
|
||||||
|
const [githubOpen, setGithubOpen] = useState(false);
|
||||||
|
const [githubToken, setGithubToken] = useState("");
|
||||||
|
const [githubError, setGithubError] = useState<string | null>(null);
|
||||||
const [sourceKind, setSourceKind] = useState<SourceKind | null>(null);
|
const [sourceKind, setSourceKind] = useState<SourceKind | null>(null);
|
||||||
const [advancedOpen, setAdvancedOpen] = useState(false);
|
const [advancedOpen, setAdvancedOpen] = useState(false);
|
||||||
const [name, setName] = useState("");
|
const [name, setName] = useState("");
|
||||||
@ -388,6 +396,27 @@ export default function Integrations() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function connectGithub() {
|
||||||
|
setPending("github");
|
||||||
|
setGithubError(null);
|
||||||
|
try {
|
||||||
|
await rpc("capabilities/install", {
|
||||||
|
kind: "mcp",
|
||||||
|
name: "GitHub",
|
||||||
|
source: GITHUB_MCP_SOURCE,
|
||||||
|
credential: githubToken.trim(),
|
||||||
|
config: { preset: "github" },
|
||||||
|
});
|
||||||
|
setGithubToken("");
|
||||||
|
setGithubOpen(false);
|
||||||
|
await refresh();
|
||||||
|
} catch (reason) {
|
||||||
|
setGithubError(reason instanceof Error ? reason.message : t("Could not connect GitHub"));
|
||||||
|
} finally {
|
||||||
|
setPending(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function removeSource(source: CapabilityInstall) {
|
async function removeSource(source: CapabilityInstall) {
|
||||||
setPending(source.id);
|
setPending(source.id);
|
||||||
setSourceError(null);
|
setSourceError(null);
|
||||||
@ -592,6 +621,63 @@ export default function Integrations() {
|
|||||||
renderDetail(detailItem)
|
renderDetail(detailItem)
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
|
<View testID="github-connect" style={styles.card}>
|
||||||
|
<View style={styles.detailTitleRow}>
|
||||||
|
<ConnectorLogo label="GitHub" styles={styles} />
|
||||||
|
<Text style={[styles.title, styles.grow]}>GitHub</Text>
|
||||||
|
{github ? (
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="button"
|
||||||
|
disabled={pending === github.id}
|
||||||
|
onPress={() => void removeSource(github)}
|
||||||
|
>
|
||||||
|
<Text style={styles.remove}>{t("Disconnect")}</Text>
|
||||||
|
</Pressable>
|
||||||
|
) : githubOpen ? null : (
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="button"
|
||||||
|
onPress={() => setGithubOpen(true)}
|
||||||
|
style={styles.smallButton}
|
||||||
|
>
|
||||||
|
<Text style={styles.buttonLabel}>{t("Connect")}</Text>
|
||||||
|
</Pressable>
|
||||||
|
)}
|
||||||
|
</View>
|
||||||
|
{!github && githubOpen ? (
|
||||||
|
<>
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="link"
|
||||||
|
onPress={() => void Linking.openURL(GITHUB_TOKEN_URL)}
|
||||||
|
style={styles.cardButton}
|
||||||
|
>
|
||||||
|
<Text style={styles.buttonLabel}>{t("Create token on GitHub")}</Text>
|
||||||
|
</Pressable>
|
||||||
|
<TextInput
|
||||||
|
value={githubToken}
|
||||||
|
onChangeText={setGithubToken}
|
||||||
|
accessibilityLabel={t("GitHub token")}
|
||||||
|
placeholder="ghp_…"
|
||||||
|
placeholderTextColor={native.tertiaryLabel}
|
||||||
|
autoCapitalize="none"
|
||||||
|
autoCorrect={false}
|
||||||
|
secureTextEntry
|
||||||
|
style={styles.input}
|
||||||
|
/>
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="button"
|
||||||
|
disabled={pending === "github" || !githubToken.trim()}
|
||||||
|
onPress={() => void connectGithub()}
|
||||||
|
style={styles.cardButton}
|
||||||
|
>
|
||||||
|
<Text style={styles.buttonLabel}>
|
||||||
|
{pending === "github" ? t("Connecting…") : t("Connect")}
|
||||||
|
</Text>
|
||||||
|
</Pressable>
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
|
{githubError ? <Text style={styles.error}>{githubError}</Text> : null}
|
||||||
|
</View>
|
||||||
|
|
||||||
{!catalogReady ? <ActivityIndicator color={native.fillPressed} /> : null}
|
{!catalogReady ? <ActivityIndicator color={native.fillPressed} /> : null}
|
||||||
|
|
||||||
{catalogReady && catalog.length === 0 ? (
|
{catalogReady && catalog.length === 0 ? (
|
||||||
@ -782,10 +868,10 @@ export default function Integrations() {
|
|||||||
) : null}
|
) : null}
|
||||||
|
|
||||||
<Text style={styles.section}>{t("Tool sources")}</Text>
|
<Text style={styles.section}>{t("Tool sources")}</Text>
|
||||||
{sources.length === 0 ? (
|
{customSources.length === 0 ? (
|
||||||
<Text style={styles.secondary}>{t("No custom sources installed.")}</Text>
|
<Text style={styles.secondary}>{t("No custom sources installed.")}</Text>
|
||||||
) : null}
|
) : null}
|
||||||
{sources.map((source) => (
|
{customSources.map((source) => (
|
||||||
<View key={source.id} style={styles.row}>
|
<View key={source.id} style={styles.row}>
|
||||||
<View style={styles.grow}>
|
<View style={styles.grow}>
|
||||||
<Text style={styles.title}>{source.name}</Text>
|
<Text style={styles.title}>{source.name}</Text>
|
||||||
|
|||||||
@ -931,7 +931,7 @@ export default function Models() {
|
|||||||
{selected.auth === "oauth" && !subscriptionSignIn ? (
|
{selected.auth === "oauth" && !subscriptionSignIn ? (
|
||||||
<Text style={styles.secondary}>
|
<Text style={styles.secondary}>
|
||||||
{t(
|
{t(
|
||||||
"This subscription sign-in is not available in Rakazo yet. Use a deployment credential or choose another provider.",
|
"This subscription sign-in is not available in evOo yet. Use a deployment credential or choose another provider.",
|
||||||
)}
|
)}
|
||||||
</Text>
|
</Text>
|
||||||
) : null}
|
) : null}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import { Stack, useRouter } from "expo-router";
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { Pressable, ScrollView, Text, TextInput } from "react-native";
|
import { Pressable, ScrollView, Text, TextInput } from "react-native";
|
||||||
import { ComputerModePicker } from "../components/computer-mode-picker";
|
import { ComputerModePicker } from "../components/computer-mode-picker";
|
||||||
import { type MobileBot, rpc } from "../lib/api";
|
import { type MobileBot, rpc, selectLibrarySpace } from "../lib/api";
|
||||||
import { allowFocusPrompt, scheduleFocusPrompt } from "../lib/focus-prompt";
|
import { allowFocusPrompt, scheduleFocusPrompt } from "../lib/focus-prompt";
|
||||||
import { useI18n } from "../lib/i18n";
|
import { useI18n } from "../lib/i18n";
|
||||||
import { useMobileTokens } from "../lib/native";
|
import { useMobileTokens } from "../lib/native";
|
||||||
@ -42,6 +42,7 @@ export default function NewBot() {
|
|||||||
setPending(true);
|
setPending(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
|
await selectLibrarySpace();
|
||||||
// Failed list is unknown — delay focus rather than treating the bot as first.
|
// Failed list is unknown — delay focus rather than treating the bot as first.
|
||||||
const existing = await rpc<MobileBot[]>("bots/list").catch(() => null);
|
const existing = await rpc<MobileBot[]>("bots/list").catch(() => null);
|
||||||
const isFirstBot = existing !== null && existing.length === 0;
|
const isFirstBot = existing !== null && existing.length === 0;
|
||||||
|
|||||||
@ -166,9 +166,9 @@ export default function SignIn() {
|
|||||||
{resetSent
|
{resetSent
|
||||||
? t("Check your email")
|
? t("Check your email")
|
||||||
: mode === "in"
|
: mode === "in"
|
||||||
? t("Sign in to Rakazo")
|
? t("Sign in to evOo")
|
||||||
: mode === "up"
|
: mode === "up"
|
||||||
? t("Sign up for Rakazo")
|
? t("Sign up for evOo")
|
||||||
: t("Reset your password")}
|
: t("Reset your password")}
|
||||||
</Text>
|
</Text>
|
||||||
{resetSent ? (
|
{resetSent ? (
|
||||||
@ -457,7 +457,7 @@ function ServerSheet({
|
|||||||
<Text
|
<Text
|
||||||
style={{ color: tokens.mutedForeground, marginTop: 28, fontSize: 15, lineHeight: 22 }}
|
style={{ color: tokens.mutedForeground, marginTop: 28, fontSize: 15, lineHeight: 22 }}
|
||||||
>
|
>
|
||||||
{t("Enter your Rakazo server address.")}
|
{t("Enter your evOo server address.")}
|
||||||
</Text>
|
</Text>
|
||||||
<TextInput
|
<TextInput
|
||||||
autoCapitalize="none"
|
autoCapitalize="none"
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -1,76 +1,51 @@
|
|||||||
|
import { voicesForLocale } from "@rakazo/core";
|
||||||
import { useFocusEffect } from "expo-router";
|
import { useFocusEffect } from "expo-router";
|
||||||
import { useCallback, useState } from "react";
|
import { useCallback, useState } from "react";
|
||||||
import {
|
import { ActivityIndicator, Pressable, ScrollView, StyleSheet, Text, View } from "react-native";
|
||||||
ActivityIndicator,
|
|
||||||
Pressable,
|
|
||||||
ScrollView,
|
|
||||||
StyleSheet,
|
|
||||||
Text,
|
|
||||||
TextInput,
|
|
||||||
View,
|
|
||||||
} from "react-native";
|
|
||||||
import { SafeAreaView } from "react-native-safe-area-context";
|
import { SafeAreaView } from "react-native-safe-area-context";
|
||||||
|
import { NativeSymbol } from "../components/native-symbol";
|
||||||
import { rpc } from "../lib/api";
|
import { rpc } from "../lib/api";
|
||||||
import { mobileTokens } from "../lib/appearance";
|
import { mobileTokens } from "../lib/appearance";
|
||||||
import { useI18n } from "../lib/i18n";
|
import { useI18n } from "../lib/i18n";
|
||||||
import { native, useThemedStyles } from "../lib/native";
|
import { native, useThemedStyles } from "../lib/native";
|
||||||
import { speakText } from "../lib/voice";
|
import {
|
||||||
|
alignVoiceWithLocale,
|
||||||
|
KOKORO_LOCAL_TOKEN,
|
||||||
|
LOCAL_VOICE_PROVIDER,
|
||||||
|
speakText,
|
||||||
|
} from "../lib/voice";
|
||||||
|
|
||||||
const KOKORO_LOCAL_TOKEN = "local-kokoro";
|
|
||||||
|
|
||||||
type VoiceCatalogEntry = {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
description: string;
|
|
||||||
transcribe: boolean;
|
|
||||||
};
|
|
||||||
type VoiceCredential = {
|
type VoiceCredential = {
|
||||||
id: string;
|
id: string;
|
||||||
provider: string;
|
provider: string;
|
||||||
voiceId: string;
|
voiceId: string;
|
||||||
};
|
};
|
||||||
type VoiceStatus = {
|
|
||||||
configured: boolean;
|
|
||||||
ready: boolean;
|
|
||||||
provider: string | null;
|
|
||||||
voiceId: string;
|
|
||||||
};
|
|
||||||
type VoiceInfo = { id: string; label: string; description?: string };
|
type VoiceInfo = { id: string; label: string; description?: string };
|
||||||
|
|
||||||
export default function VoiceSettings() {
|
export default function VoiceSettings() {
|
||||||
const styles = useThemedStyles(createVoiceStyles);
|
const styles = useThemedStyles(createVoiceStyles);
|
||||||
const { t } = useI18n();
|
const { locale, t } = useI18n();
|
||||||
const [catalog, setCatalog] = useState<VoiceCatalogEntry[]>([]);
|
const [connected, setConnected] = useState(false);
|
||||||
const [credentials, setCredentials] = useState<VoiceCredential[]>([]);
|
|
||||||
const [status, setStatus] = useState<VoiceStatus | null>(null);
|
|
||||||
const [voices, setVoices] = useState<VoiceInfo[]>([]);
|
const [voices, setVoices] = useState<VoiceInfo[]>([]);
|
||||||
const [provider, setProvider] = useState("");
|
|
||||||
const [apiKey, setApiKey] = useState("");
|
|
||||||
const [voiceId, setVoiceId] = useState("");
|
const [voiceId, setVoiceId] = useState("");
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [pending, setPending] = useState<"connect" | "disconnect" | "voice" | "test" | null>(null);
|
const [pending, setPending] = useState<"connect" | "disconnect" | "voice" | null>(null);
|
||||||
|
const [playing, setPlaying] = useState<string | null>(null);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [notice, setNotice] = useState<string | null>(null);
|
|
||||||
|
|
||||||
const load = useCallback(async (nextProvider?: string) => {
|
const load = useCallback(async () => {
|
||||||
const [nextCatalog, nextCredentials, nextStatus] = await Promise.all([
|
await alignVoiceWithLocale(locale).catch(() => undefined);
|
||||||
rpc<VoiceCatalogEntry[]>("voice/catalog"),
|
const credentials = await rpc<VoiceCredential[]>("voice/credentials");
|
||||||
rpc<VoiceCredential[]>("voice/credentials"),
|
const cred = credentials.find((entry) => entry.provider === LOCAL_VOICE_PROVIDER);
|
||||||
rpc<VoiceStatus>("voice/status"),
|
setConnected(Boolean(cred));
|
||||||
]);
|
|
||||||
const selected = nextProvider || nextStatus.provider || nextCatalog[0]?.id || "";
|
|
||||||
setCatalog(nextCatalog);
|
|
||||||
setCredentials(nextCredentials);
|
|
||||||
setStatus(nextStatus);
|
|
||||||
setProvider(selected);
|
|
||||||
const cred = nextCredentials.find((entry) => entry.provider === selected);
|
|
||||||
setVoiceId(cred?.voiceId ?? "");
|
setVoiceId(cred?.voiceId ?? "");
|
||||||
if (cred) {
|
if (cred) {
|
||||||
setVoices(await rpc<VoiceInfo[]>("voice/voices", { provider: selected }));
|
const listed = await rpc<VoiceInfo[]>("voice/voices", { provider: LOCAL_VOICE_PROVIDER });
|
||||||
|
setVoices(voicesForLocale(listed, locale));
|
||||||
} else {
|
} else {
|
||||||
setVoices([]);
|
setVoices([]);
|
||||||
}
|
}
|
||||||
}, []);
|
}, [locale]);
|
||||||
|
|
||||||
useFocusEffect(
|
useFocusEffect(
|
||||||
useCallback(() => {
|
useCallback(() => {
|
||||||
@ -83,24 +58,12 @@ export default function VoiceSettings() {
|
|||||||
}, [load]),
|
}, [load]),
|
||||||
);
|
);
|
||||||
|
|
||||||
const selected = catalog.find((entry) => entry.id === provider);
|
|
||||||
const isKokoro = selected?.id === "kokoro";
|
|
||||||
const credential = credentials.find((entry) => entry.provider === provider);
|
|
||||||
const canConnect = isKokoro || apiKey.trim().length >= 8;
|
|
||||||
|
|
||||||
async function connect() {
|
async function connect() {
|
||||||
if (!selected || !canConnect) return;
|
|
||||||
setPending("connect");
|
setPending("connect");
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
await rpc("voice/connect", {
|
await rpc("voice/connect", { provider: LOCAL_VOICE_PROVIDER, apiKey: KOKORO_LOCAL_TOKEN });
|
||||||
provider: selected.id,
|
await load();
|
||||||
apiKey: isKokoro ? KOKORO_LOCAL_TOKEN : apiKey.trim(),
|
|
||||||
voiceId: voiceId || undefined,
|
|
||||||
});
|
|
||||||
setApiKey("");
|
|
||||||
await load(selected.id);
|
|
||||||
setNotice(t("Connected {name}.", { name: selected.name }));
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err instanceof Error ? err.message : t("Could not connect"));
|
setError(err instanceof Error ? err.message : t("Could not connect"));
|
||||||
} finally {
|
} finally {
|
||||||
@ -109,14 +72,11 @@ export default function VoiceSettings() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function disconnect() {
|
async function disconnect() {
|
||||||
if (!credential) return;
|
|
||||||
setPending("disconnect");
|
setPending("disconnect");
|
||||||
setError(null);
|
setError(null);
|
||||||
setNotice(null);
|
|
||||||
try {
|
try {
|
||||||
await rpc("voice/disconnect", { provider: credential.provider });
|
await rpc("voice/disconnect", { provider: LOCAL_VOICE_PROVIDER });
|
||||||
setApiKey("");
|
await load();
|
||||||
await load(credential.provider);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err instanceof Error ? err.message : t("Could not disconnect"));
|
setError(err instanceof Error ? err.message : t("Could not disconnect"));
|
||||||
} finally {
|
} finally {
|
||||||
@ -127,173 +87,145 @@ export default function VoiceSettings() {
|
|||||||
async function chooseVoice(nextVoiceId: string) {
|
async function chooseVoice(nextVoiceId: string) {
|
||||||
setVoiceId(nextVoiceId);
|
setVoiceId(nextVoiceId);
|
||||||
setPending("voice");
|
setPending("voice");
|
||||||
|
setError(null);
|
||||||
try {
|
try {
|
||||||
await rpc("voice/setVoice", { voiceId: nextVoiceId, provider: selected?.id });
|
await rpc("voice/setVoice", { voiceId: nextVoiceId, provider: LOCAL_VOICE_PROVIDER });
|
||||||
await load(selected?.id);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err instanceof Error ? err.message : t("Could not save that voice"));
|
setError(err instanceof Error ? err.message : t("Could not save that voice"));
|
||||||
|
await load().catch(() => undefined);
|
||||||
} finally {
|
} finally {
|
||||||
setPending(null);
|
setPending(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function testVoice() {
|
async function playVoice(voice: VoiceInfo) {
|
||||||
setPending("test");
|
setPlaying(voice.id);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const ready = await speakText(t("Hi, this is how I'll sound when I read replies out loud."));
|
await speakText(t("Hi, this is how I'll sound when I read replies out loud."), {
|
||||||
if (!ready) {
|
voiceId: voice.id,
|
||||||
throw new Error(t("Connect a voice provider first."));
|
});
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err instanceof Error ? err.message : t("Could not play a sample"));
|
setError(err instanceof Error ? err.message : t("Could not play a sample"));
|
||||||
} finally {
|
} finally {
|
||||||
setPending(null);
|
setPlaying((current) => (current === voice.id ? null : current));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const busy = pending !== null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SafeAreaView edges={["bottom"]} style={styles.screen}>
|
<SafeAreaView edges={["bottom"]} style={styles.screen}>
|
||||||
<ScrollView contentContainerStyle={styles.content}>
|
<ScrollView contentContainerStyle={styles.content}>
|
||||||
{loading ? <ActivityIndicator color={native.secondaryLabel} /> : null}
|
{loading ? <ActivityIndicator color={native.secondaryLabel} /> : null}
|
||||||
{error ? <Text style={styles.error}>{error}</Text> : null}
|
{error ? <Text style={styles.error}>{error}</Text> : null}
|
||||||
{notice ? <Text style={styles.notice}>{notice}</Text> : null}
|
{!loading && !connected ? (
|
||||||
{catalog.map((entry) => {
|
<Pressable
|
||||||
const connected = credentials.some((cred) => cred.provider === entry.id);
|
disabled={busy}
|
||||||
return (
|
onPress={() => void connect()}
|
||||||
<Pressable
|
style={[styles.button, busy && styles.disabled]}
|
||||||
key={entry.id}
|
>
|
||||||
disabled={pending !== null}
|
<Text style={styles.buttonLabel}>{t("Connect")}</Text>
|
||||||
onPress={() => {
|
</Pressable>
|
||||||
setProvider(entry.id);
|
) : null}
|
||||||
setPending("voice");
|
{voices.length ? (
|
||||||
void load(entry.id)
|
<ScrollView
|
||||||
.catch((err: unknown) =>
|
horizontal
|
||||||
setError(
|
accessibilityRole="radiogroup"
|
||||||
err instanceof Error ? err.message : t("Could not load voice settings"),
|
showsHorizontalScrollIndicator={false}
|
||||||
),
|
snapToInterval={CARD_WIDTH + CARD_GAP}
|
||||||
)
|
decelerationRate="fast"
|
||||||
.finally(() => setPending(null));
|
style={styles.carousel}
|
||||||
}}
|
contentContainerStyle={styles.carouselContent}
|
||||||
style={[
|
>
|
||||||
styles.card,
|
{voices.map((voice) => {
|
||||||
provider === entry.id && styles.cardActive,
|
const active = voiceId === voice.id;
|
||||||
pending !== null && styles.disabled,
|
return (
|
||||||
]}
|
<Pressable
|
||||||
>
|
key={voice.id}
|
||||||
<Text style={styles.cardTitle}>{entry.name}</Text>
|
accessibilityRole="radio"
|
||||||
<Text style={styles.cardMeta}>
|
accessibilityState={{ checked: active, disabled: busy }}
|
||||||
{connected
|
disabled={busy}
|
||||||
? t("Connected")
|
onPress={() => void chooseVoice(voice.id)}
|
||||||
: entry.transcribe
|
style={[styles.card, active && styles.cardActive]}
|
||||||
? t("Speak + transcribe")
|
>
|
||||||
: t("Speak only")}
|
<View style={styles.cardHeader}>
|
||||||
</Text>
|
<Text numberOfLines={1} style={styles.cardTitle}>
|
||||||
</Pressable>
|
{voice.label}
|
||||||
);
|
</Text>
|
||||||
})}
|
<Pressable
|
||||||
{selected ? (
|
accessibilityRole="button"
|
||||||
<>
|
accessibilityLabel={t("Play {name}", { name: voice.label })}
|
||||||
{isKokoro ? (
|
disabled={playing !== null}
|
||||||
<Text style={styles.help}>
|
hitSlop={8}
|
||||||
{t("Local voice runs on your server. Tap Connect when it is ready.")}
|
onPress={() => void playVoice(voice)}
|
||||||
</Text>
|
style={styles.play}
|
||||||
) : (
|
>
|
||||||
<TextInput
|
{playing === voice.id ? (
|
||||||
accessibilityLabel={t("API key")}
|
<ActivityIndicator size="small" color={native.secondaryLabel} />
|
||||||
autoCapitalize="none"
|
) : (
|
||||||
autoComplete="off"
|
<NativeSymbol ios="play.fill" android="play" size={14} />
|
||||||
autoCorrect={false}
|
)}
|
||||||
importantForAutofill="no"
|
</Pressable>
|
||||||
value={apiKey}
|
</View>
|
||||||
onChangeText={setApiKey}
|
{voice.description ? (
|
||||||
placeholder={credential ? t("Paste a replacement key") : t("Paste your API key")}
|
<Text style={styles.cardMeta}>{voice.description}</Text>
|
||||||
placeholderTextColor={native.tertiaryLabel}
|
) : null}
|
||||||
secureTextEntry
|
</Pressable>
|
||||||
style={styles.input}
|
);
|
||||||
textContentType="none"
|
})}
|
||||||
/>
|
</ScrollView>
|
||||||
)}
|
) : null}
|
||||||
<Pressable
|
{connected ? (
|
||||||
disabled={pending !== null || !canConnect}
|
<Pressable
|
||||||
onPress={() => void connect()}
|
disabled={busy}
|
||||||
style={[styles.button, (pending !== null || !canConnect) && styles.disabled]}
|
onPress={() => void disconnect()}
|
||||||
>
|
style={[styles.secondary, busy && styles.disabled]}
|
||||||
<Text style={styles.buttonLabel}>
|
>
|
||||||
{credential ? (isKokoro ? t("Reconnect") : t("Replace key")) : t("Connect")}
|
<Text style={styles.secondaryLabel}>
|
||||||
</Text>
|
{pending === "disconnect" ? t("Disconnecting…") : t("Disconnect")}
|
||||||
</Pressable>
|
</Text>
|
||||||
{credential ? (
|
</Pressable>
|
||||||
<Pressable
|
|
||||||
disabled={pending !== null}
|
|
||||||
onPress={() => void disconnect()}
|
|
||||||
style={[styles.secondary, pending !== null && styles.disabled]}
|
|
||||||
>
|
|
||||||
<Text style={styles.secondaryLabel}>
|
|
||||||
{pending === "disconnect" ? t("Disconnecting…") : t("Disconnect")}
|
|
||||||
</Text>
|
|
||||||
</Pressable>
|
|
||||||
) : null}
|
|
||||||
{voices.length ? (
|
|
||||||
<View style={styles.voices}>
|
|
||||||
{voices.map((voice) => (
|
|
||||||
<Pressable
|
|
||||||
key={voice.id}
|
|
||||||
disabled={pending !== null}
|
|
||||||
onPress={() => void chooseVoice(voice.id)}
|
|
||||||
style={[styles.voiceRow, pending !== null && styles.disabled]}
|
|
||||||
>
|
|
||||||
<Text style={styles.voiceLabel}>{voice.label}</Text>
|
|
||||||
{voiceId === voice.id ? <Text style={styles.check}>✓</Text> : null}
|
|
||||||
</Pressable>
|
|
||||||
))}
|
|
||||||
</View>
|
|
||||||
) : null}
|
|
||||||
{status?.ready ? (
|
|
||||||
<Pressable
|
|
||||||
disabled={pending !== null}
|
|
||||||
onPress={() => void testVoice()}
|
|
||||||
style={styles.secondary}
|
|
||||||
>
|
|
||||||
<Text style={styles.secondaryLabel}>{t("Hear a sample")}</Text>
|
|
||||||
</Pressable>
|
|
||||||
) : null}
|
|
||||||
</>
|
|
||||||
) : null}
|
) : null}
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
</SafeAreaView>
|
</SafeAreaView>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const CARD_WIDTH = 160;
|
||||||
|
const CARD_GAP = 12;
|
||||||
|
|
||||||
function createVoiceStyles() {
|
function createVoiceStyles() {
|
||||||
const tokens = mobileTokens();
|
const tokens = mobileTokens();
|
||||||
return StyleSheet.create({
|
return StyleSheet.create({
|
||||||
screen: { flex: 1, backgroundColor: native.page },
|
screen: { flex: 1, backgroundColor: native.page },
|
||||||
content: { padding: 20, gap: 10 },
|
content: { paddingVertical: 20, gap: 10 },
|
||||||
error: { color: tokens.destructive, marginBottom: 8 },
|
error: { color: tokens.destructive, marginBottom: 8, marginHorizontal: 20 },
|
||||||
notice: { color: tokens.success, marginBottom: 8 },
|
carousel: { flexGrow: 0 },
|
||||||
|
carouselContent: { paddingHorizontal: 20, gap: CARD_GAP },
|
||||||
card: {
|
card: {
|
||||||
|
width: CARD_WIDTH,
|
||||||
borderRadius: 14,
|
borderRadius: 14,
|
||||||
borderWidth: 1,
|
borderWidth: 1,
|
||||||
borderColor: tokens.border,
|
borderColor: tokens.border,
|
||||||
padding: 14,
|
|
||||||
backgroundColor: tokens.card,
|
|
||||||
},
|
|
||||||
cardActive: { borderColor: tokens.ring, backgroundColor: tokens.muted },
|
|
||||||
cardTitle: { color: native.label, fontSize: 16 },
|
|
||||||
cardMeta: { color: native.tertiaryLabel, marginTop: 4, fontSize: 12 },
|
|
||||||
help: { marginTop: 8, color: native.secondaryLabel, fontSize: 14, lineHeight: 20 },
|
|
||||||
input: {
|
|
||||||
marginTop: 8,
|
|
||||||
borderRadius: 12,
|
|
||||||
borderWidth: 1,
|
|
||||||
borderColor: tokens.border,
|
|
||||||
color: native.label,
|
|
||||||
paddingHorizontal: 14,
|
paddingHorizontal: 14,
|
||||||
paddingVertical: 12,
|
paddingVertical: 12,
|
||||||
|
backgroundColor: tokens.card,
|
||||||
|
},
|
||||||
|
cardActive: { borderColor: tokens.primary, borderWidth: 2, backgroundColor: tokens.muted },
|
||||||
|
cardHeader: { flexDirection: "row", alignItems: "center", justifyContent: "space-between" },
|
||||||
|
cardTitle: { flexShrink: 1, color: native.label, fontSize: 16, fontWeight: "600" },
|
||||||
|
cardMeta: { color: native.tertiaryLabel, marginTop: 4, fontSize: 13 },
|
||||||
|
play: {
|
||||||
|
width: 30,
|
||||||
|
height: 30,
|
||||||
|
borderRadius: 15,
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "center",
|
||||||
|
backgroundColor: tokens.muted,
|
||||||
},
|
},
|
||||||
button: {
|
button: {
|
||||||
marginTop: 8,
|
marginHorizontal: 20,
|
||||||
backgroundColor: tokens.primary,
|
backgroundColor: tokens.primary,
|
||||||
borderRadius: 12,
|
borderRadius: 12,
|
||||||
paddingVertical: 12,
|
paddingVertical: 12,
|
||||||
@ -301,17 +233,6 @@ function createVoiceStyles() {
|
|||||||
},
|
},
|
||||||
disabled: { opacity: 0.4 },
|
disabled: { opacity: 0.4 },
|
||||||
buttonLabel: { color: tokens.primaryForeground, fontWeight: "600" },
|
buttonLabel: { color: tokens.primaryForeground, fontWeight: "600" },
|
||||||
voices: { marginTop: 12, borderRadius: 12, borderWidth: 1, borderColor: tokens.border },
|
|
||||||
voiceRow: {
|
|
||||||
flexDirection: "row",
|
|
||||||
justifyContent: "space-between",
|
|
||||||
paddingHorizontal: 14,
|
|
||||||
paddingVertical: 12,
|
|
||||||
borderBottomWidth: 1,
|
|
||||||
borderBottomColor: tokens.border,
|
|
||||||
},
|
|
||||||
voiceLabel: { color: native.label },
|
|
||||||
check: { color: tokens.success },
|
|
||||||
secondary: { marginTop: 16, alignItems: "center" },
|
secondary: { marginTop: 16, alignItems: "center" },
|
||||||
secondaryLabel: { color: native.secondaryLabel, fontSize: 15 },
|
secondaryLabel: { color: native.secondaryLabel, fontSize: 15 },
|
||||||
});
|
});
|
||||||
|
|||||||
183
apps/mobile/components/autopilot-line.tsx
Normal file
183
apps/mobile/components/autopilot-line.tsx
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
import type { AutopilotBlock } from "@rakazo/contracts";
|
||||||
|
import { formatAutopilotDuration } from "@rakazo/core";
|
||||||
|
import { useState } from "react";
|
||||||
|
import { Pressable, Text, View } from "react-native";
|
||||||
|
import { dateLocaleForUi, useI18n } from "../lib/i18n";
|
||||||
|
import { useMobileTokens } from "../lib/native";
|
||||||
|
import { NativeSymbol } from "./native-symbol";
|
||||||
|
|
||||||
|
export type AutopilotAction = "stop" | "resume";
|
||||||
|
|
||||||
|
const ICONS = {
|
||||||
|
"autopilot.started": { ios: "play.fill", android: "play" },
|
||||||
|
"autopilot.paused": { ios: "pause.fill", android: "pause" },
|
||||||
|
"autopilot.stopped": { ios: "stop.fill", android: "stop" },
|
||||||
|
"autopilot.finished": { ios: "checkmark.circle", android: "checkmark-circle-outline" },
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One quiet line when the autopilot starts, pauses, stops or finishes. The start line counts the
|
||||||
|
* steps and folds open to the latest checkpoints; a finished autopilot's result reads as a card.
|
||||||
|
*/
|
||||||
|
export function AutopilotLine({
|
||||||
|
block,
|
||||||
|
onAction,
|
||||||
|
}: {
|
||||||
|
block: AutopilotBlock;
|
||||||
|
onAction?: (action: AutopilotAction) => Promise<void>;
|
||||||
|
}) {
|
||||||
|
const tokens = useMobileTokens();
|
||||||
|
const { t, locale } = useI18n();
|
||||||
|
const [busy, setBusy] = useState(false);
|
||||||
|
const [failed, setFailed] = useState(false);
|
||||||
|
const [expanded, setExpanded] = useState(false);
|
||||||
|
const open = !block.ended && Boolean(onAction);
|
||||||
|
const muted = { color: tokens.mutedForeground, fontSize: 13.5 };
|
||||||
|
|
||||||
|
async function act(action: AutopilotAction) {
|
||||||
|
if (!onAction) return;
|
||||||
|
setBusy(true);
|
||||||
|
setFailed(false);
|
||||||
|
try {
|
||||||
|
await onAction(action);
|
||||||
|
} catch {
|
||||||
|
setFailed(true);
|
||||||
|
} finally {
|
||||||
|
setBusy(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const parts: string[] = [];
|
||||||
|
let label = t("Autopilot");
|
||||||
|
if (block.event === "autopilot.started") {
|
||||||
|
parts.push(block.goal);
|
||||||
|
if (block.until) {
|
||||||
|
const until = new Intl.DateTimeFormat(dateLocaleForUi(locale), {
|
||||||
|
hour: "numeric",
|
||||||
|
minute: "2-digit",
|
||||||
|
}).format(new Date(block.until));
|
||||||
|
parts.push(t("until {until}", { until }));
|
||||||
|
}
|
||||||
|
if (block.focus) parts.push(block.focus);
|
||||||
|
if (block.allowed?.length) {
|
||||||
|
parts.push(t("allowed: {allowed}", { allowed: block.allowed.join(", ") }));
|
||||||
|
}
|
||||||
|
} else if (block.event === "autopilot.paused") {
|
||||||
|
label = t("Autopilot paused");
|
||||||
|
if (block.text) parts.push(block.text);
|
||||||
|
} else {
|
||||||
|
label = block.event === "autopilot.stopped" ? t("Autopilot stopped") : t("Autopilot finished");
|
||||||
|
if (block.durationMs !== undefined) parts.push(formatAutopilotDuration(block.durationMs));
|
||||||
|
}
|
||||||
|
const icon = ICONS[block.event];
|
||||||
|
const checkpoints = block.event === "autopilot.started" ? (block.checkpoints ?? []) : [];
|
||||||
|
const latest = checkpoints.at(-1)?.text;
|
||||||
|
const time = new Intl.DateTimeFormat(dateLocaleForUi(locale), {
|
||||||
|
hour: "numeric",
|
||||||
|
minute: "2-digit",
|
||||||
|
});
|
||||||
|
const link = (text: string, action: AutopilotAction) => (
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="button"
|
||||||
|
hitSlop={10}
|
||||||
|
disabled={busy}
|
||||||
|
onPress={() => void act(action)}
|
||||||
|
style={{ opacity: busy ? 0.5 : 1 }}
|
||||||
|
>
|
||||||
|
<Text style={[muted, { fontWeight: "600" }]}>{text}</Text>
|
||||||
|
</Pressable>
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<View style={{ width: "100%", paddingVertical: 4, alignItems: "center", gap: 4 }}>
|
||||||
|
<View
|
||||||
|
style={{
|
||||||
|
flexDirection: "row",
|
||||||
|
flexWrap: "wrap",
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "center",
|
||||||
|
columnGap: 6,
|
||||||
|
rowGap: 2,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<NativeSymbol
|
||||||
|
ios={icon.ios}
|
||||||
|
android={icon.android}
|
||||||
|
size={13}
|
||||||
|
color={tokens.mutedForeground}
|
||||||
|
/>
|
||||||
|
<Text style={[muted, { color: tokens.foreground, fontWeight: "500" }]}>{label}</Text>
|
||||||
|
{parts.map((part, index) => (
|
||||||
|
<Text key={index} style={muted}>
|
||||||
|
· {part}
|
||||||
|
</Text>
|
||||||
|
))}
|
||||||
|
{block.steps && block.event === "autopilot.started" ? (
|
||||||
|
<>
|
||||||
|
<Text style={muted}>·</Text>
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="button"
|
||||||
|
accessibilityState={{ expanded }}
|
||||||
|
hitSlop={10}
|
||||||
|
onPress={() => setExpanded((value) => !value)}
|
||||||
|
>
|
||||||
|
<Text style={[muted, { fontWeight: "600" }]}>
|
||||||
|
{block.steps === 1 ? t("1 step") : t("{count} steps", { count: block.steps })}
|
||||||
|
</Text>
|
||||||
|
</Pressable>
|
||||||
|
{latest && !expanded ? (
|
||||||
|
<Text style={muted} numberOfLines={1}>
|
||||||
|
· {t("last: {latest}", { latest })}
|
||||||
|
</Text>
|
||||||
|
) : null}
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
|
{open && block.event === "autopilot.paused" ? (
|
||||||
|
<>
|
||||||
|
<Text style={muted}>·</Text>
|
||||||
|
{link(t("Continue"), "resume")}
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
|
{open && (block.event === "autopilot.started" || block.event === "autopilot.paused") ? (
|
||||||
|
<>
|
||||||
|
<Text style={muted}>·</Text>
|
||||||
|
{link(failed ? t("Try again") : t("Stop"), "stop")}
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
|
</View>
|
||||||
|
{expanded && checkpoints.length > 0 ? (
|
||||||
|
<View style={{ width: "100%", maxWidth: 420, gap: 4, paddingHorizontal: 16 }}>
|
||||||
|
{checkpoints.map((checkpoint, index) => (
|
||||||
|
<View key={`${checkpoint.at}-${index}`} style={{ flexDirection: "row", gap: 8 }}>
|
||||||
|
<Text
|
||||||
|
style={[muted, { width: 64, textAlign: "right", fontVariant: ["tabular-nums"] }]}
|
||||||
|
>
|
||||||
|
{time.format(new Date(checkpoint.at))}
|
||||||
|
</Text>
|
||||||
|
<Text style={{ flex: 1, color: tokens.foreground, fontSize: 13.5 }}>
|
||||||
|
{checkpoint.text}
|
||||||
|
</Text>
|
||||||
|
</View>
|
||||||
|
))}
|
||||||
|
</View>
|
||||||
|
) : null}
|
||||||
|
{block.event === "autopilot.finished" && block.text ? (
|
||||||
|
<View
|
||||||
|
style={{
|
||||||
|
width: "100%",
|
||||||
|
maxWidth: 520,
|
||||||
|
marginTop: 4,
|
||||||
|
borderRadius: 12,
|
||||||
|
borderWidth: 1,
|
||||||
|
borderColor: tokens.border,
|
||||||
|
backgroundColor: tokens.card,
|
||||||
|
paddingHorizontal: 14,
|
||||||
|
paddingVertical: 10,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Text style={{ color: tokens.foreground, fontSize: 14 }}>{block.text}</Text>
|
||||||
|
</View>
|
||||||
|
) : null}
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
}
|
||||||
71
apps/mobile/components/autopilot-time-picker.tsx
Normal file
71
apps/mobile/components/autopilot-time-picker.tsx
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import { DateTimePicker } from "@expo/ui/community/datetime-picker";
|
||||||
|
import { useState } from "react";
|
||||||
|
import { Modal, Platform, Pressable, Text, View } from "react-native";
|
||||||
|
import { autopilotTimeOfDay } from "../lib/autopilot";
|
||||||
|
import { useI18n } from "../lib/i18n";
|
||||||
|
import { useMobileTokens, useResolvedAppearance } from "../lib/native";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The native time picker for "Until a time". Android opens its clock dialog; iOS shows the wheel in
|
||||||
|
* a sheet with Start. Either way the caller gets "HH:MM" and unmounts the picker.
|
||||||
|
*/
|
||||||
|
export function AutopilotTimePicker({
|
||||||
|
onPick,
|
||||||
|
onClose,
|
||||||
|
}: {
|
||||||
|
onPick: (time: string) => void;
|
||||||
|
onClose: () => void;
|
||||||
|
}) {
|
||||||
|
const tokens = useMobileTokens();
|
||||||
|
const colorScheme = useResolvedAppearance();
|
||||||
|
const { t } = useI18n();
|
||||||
|
const [value, setValue] = useState(() => {
|
||||||
|
const morning = new Date();
|
||||||
|
morning.setHours(8, 0, 0, 0);
|
||||||
|
return morning;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (Platform.OS === "android") {
|
||||||
|
return (
|
||||||
|
<DateTimePicker
|
||||||
|
mode="time"
|
||||||
|
value={value}
|
||||||
|
onValueChange={(_event, date) => onPick(autopilotTimeOfDay(date))}
|
||||||
|
onDismiss={onClose}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal visible animationType="slide" presentationStyle="pageSheet" onRequestClose={onClose}>
|
||||||
|
<View style={{ flex: 1, backgroundColor: tokens.background, padding: 20, gap: 12 }}>
|
||||||
|
<View
|
||||||
|
style={{ flexDirection: "row", alignItems: "center", justifyContent: "space-between" }}
|
||||||
|
>
|
||||||
|
<Pressable accessibilityRole="button" onPress={onClose} hitSlop={12}>
|
||||||
|
<Text style={{ color: tokens.mutedForeground, fontSize: 17 }}>{t("Cancel")}</Text>
|
||||||
|
</Pressable>
|
||||||
|
<Text style={{ color: tokens.foreground, fontSize: 17, fontWeight: "600" }}>
|
||||||
|
{t("Until when?")}
|
||||||
|
</Text>
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="button"
|
||||||
|
onPress={() => onPick(autopilotTimeOfDay(value))}
|
||||||
|
hitSlop={12}
|
||||||
|
>
|
||||||
|
<Text style={{ color: tokens.foreground, fontSize: 17, fontWeight: "600" }}>
|
||||||
|
{t("Start")}
|
||||||
|
</Text>
|
||||||
|
</Pressable>
|
||||||
|
</View>
|
||||||
|
<DateTimePicker
|
||||||
|
mode="time"
|
||||||
|
display="spinner"
|
||||||
|
value={value}
|
||||||
|
themeVariant={colorScheme}
|
||||||
|
onValueChange={(_event, date) => setValue(date)}
|
||||||
|
/>
|
||||||
|
</View>
|
||||||
|
</Modal>
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -1,52 +0,0 @@
|
|||||||
import type { AvatarStyle, Me } from "@rakazo/contracts";
|
|
||||||
import { usePathname } from "expo-router";
|
|
||||||
import { createContext, type ReactNode, useContext, useEffect, useRef, useState } from "react";
|
|
||||||
import { rpc } from "../lib/api";
|
|
||||||
|
|
||||||
const AvatarStyleContext = createContext<{
|
|
||||||
avatarStyle: AvatarStyle;
|
|
||||||
updateAvatarStyle: (avatarStyle: AvatarStyle) => Promise<void>;
|
|
||||||
}>({
|
|
||||||
avatarStyle: "robot",
|
|
||||||
updateAvatarStyle: async () => undefined,
|
|
||||||
});
|
|
||||||
|
|
||||||
export function AvatarStyleProvider({ children }: { children: ReactNode }) {
|
|
||||||
const [avatarStyle, setAvatarStyle] = useState<AvatarStyle>("robot");
|
|
||||||
const pathname = usePathname();
|
|
||||||
const requestIdRef = useRef(0);
|
|
||||||
const updatePromiseRef = useRef<Promise<void> | null>(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const requestId = ++requestIdRef.current;
|
|
||||||
void rpc<Me>("me")
|
|
||||||
.then((me) => {
|
|
||||||
if (requestId !== requestIdRef.current) return;
|
|
||||||
setAvatarStyle(me.avatarStyle);
|
|
||||||
})
|
|
||||||
.catch(() => undefined);
|
|
||||||
}, [pathname]);
|
|
||||||
|
|
||||||
function updateAvatarStyle(next: AvatarStyle): Promise<void> {
|
|
||||||
if (updatePromiseRef.current) return updatePromiseRef.current;
|
|
||||||
const requestId = ++requestIdRef.current;
|
|
||||||
const update = rpc<Me>("preferences/update", { avatarStyle: next })
|
|
||||||
.then((me) => {
|
|
||||||
if (requestId !== requestIdRef.current) return;
|
|
||||||
setAvatarStyle(me.avatarStyle);
|
|
||||||
})
|
|
||||||
.finally(() => {
|
|
||||||
updatePromiseRef.current = null;
|
|
||||||
});
|
|
||||||
updatePromiseRef.current = update;
|
|
||||||
return update;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<AvatarStyleContext value={{ avatarStyle, updateAvatarStyle }}>{children}</AvatarStyleContext>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useAvatarStyle() {
|
|
||||||
return useContext(AvatarStyleContext);
|
|
||||||
}
|
|
||||||
@ -1,88 +1,76 @@
|
|||||||
import type { AvatarStyle } from "@rakazo/contracts";
|
import type { OoMoment } from "@rakazo/core";
|
||||||
import { ACTIVE_RUN_STATUSES, avatarIdentitySeed, organicAvatarPath } from "@rakazo/core";
|
import {
|
||||||
import { memo, useEffect } from "react";
|
ACTIVE_RUN_STATUSES,
|
||||||
import { View } from "react-native";
|
DEFAULT_OO_AVATAR,
|
||||||
import Animated, {
|
decodeOoAvatar,
|
||||||
cancelAnimation,
|
OO_SUCCESS_SECONDS,
|
||||||
Easing,
|
ooIdentityShapeIndex,
|
||||||
useAnimatedProps,
|
ooMomentAfterStatus,
|
||||||
useAnimatedStyle,
|
ooMomentForStatus,
|
||||||
useReducedMotion,
|
resolveOoColor,
|
||||||
useSharedValue,
|
} from "@rakazo/core";
|
||||||
withRepeat,
|
import { memo, useEffect, useMemo, useRef, useState } from "react";
|
||||||
withTiming,
|
import { Image, View } from "react-native";
|
||||||
} from "react-native-reanimated";
|
|
||||||
import Svg, { G, Path, Rect } from "react-native-svg";
|
|
||||||
import { workingAvatarDuration, workingAvatarFrame } from "../lib/avatar-motion";
|
|
||||||
import { useI18n } from "../lib/i18n";
|
import { useI18n } from "../lib/i18n";
|
||||||
import { useAvatarStyle } from "./avatar-style";
|
|
||||||
import { NativeSymbol } from "./native-symbol";
|
import { NativeSymbol } from "./native-symbol";
|
||||||
|
import { OoFigure } from "./oo-figure";
|
||||||
|
|
||||||
const AnimatedRect = Animated.createAnimatedComponent(Rect);
|
/** A short moment after a status change, such as joy when a run finishes. */
|
||||||
|
function useOoStatusMoment(status?: string): OoMoment | null {
|
||||||
|
const previous = useRef(status);
|
||||||
|
const [moment, setMoment] = useState<OoMoment | null>(null);
|
||||||
|
useEffect(() => {
|
||||||
|
const next = ooMomentAfterStatus(previous.current, status);
|
||||||
|
previous.current = status;
|
||||||
|
setMoment(next);
|
||||||
|
if (!next) return;
|
||||||
|
const timer = setTimeout(() => setMoment(null), OO_SUCCESS_SECONDS * 1000);
|
||||||
|
return () => clearTimeout(timer);
|
||||||
|
}, [status]);
|
||||||
|
return moment;
|
||||||
|
}
|
||||||
|
|
||||||
export const BotAvatar = memo(function BotAvatar({
|
export const BotAvatar = memo(function BotAvatar({
|
||||||
color,
|
color,
|
||||||
size = 54,
|
size = 54,
|
||||||
status,
|
status,
|
||||||
identity,
|
identity,
|
||||||
variant,
|
moment,
|
||||||
|
still,
|
||||||
muted = false,
|
muted = false,
|
||||||
}: {
|
}: {
|
||||||
color: string;
|
color: string;
|
||||||
size?: number;
|
size?: number;
|
||||||
status?: string;
|
status?: string;
|
||||||
identity?: string;
|
identity?: string;
|
||||||
variant?: AvatarStyle;
|
/** Explicit moment; wins over `status`. */
|
||||||
|
moment?: OoMoment;
|
||||||
|
still?: boolean;
|
||||||
muted?: boolean;
|
muted?: boolean;
|
||||||
}) {
|
}) {
|
||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
const isWorking = ACTIVE_RUN_STATUSES.some((activeStatus) => activeStatus === status);
|
const isWorking = ACTIVE_RUN_STATUSES.some((activeStatus) => activeStatus === status);
|
||||||
const { avatarStyle } = useAvatarStyle();
|
const picture = color.startsWith("data:image/") ? color : null;
|
||||||
const visorW = Math.round(size * 0.68);
|
const look = useMemo(() => decodeOoAvatar(color) ?? DEFAULT_OO_AVATAR, [color]);
|
||||||
const visorH = Math.round(size * 0.44);
|
const seed = identity || (picture ? "agent" : color) || "agent";
|
||||||
const eyeW = Math.max(3, Math.round(size * 0.11));
|
const body = resolveOoColor(seed, look.color).hex;
|
||||||
const eyeH = Math.max(4, Math.round(size * 0.17));
|
const statusMoment = useOoStatusMoment(status);
|
||||||
const gap = Math.max(3, Math.round(size * 0.11));
|
|
||||||
return (
|
return (
|
||||||
<View style={{ width: size, height: size }}>
|
<View style={{ width: size, height: size }}>
|
||||||
{(variant ?? avatarStyle) === "organic" ? (
|
{picture ? (
|
||||||
<OrganicAvatar color={color} identity={identity} size={size} isWorking={isWorking} />
|
<Image
|
||||||
|
source={{ uri: picture }}
|
||||||
|
style={{ width: size, height: size, borderRadius: size / 2 }}
|
||||||
|
/>
|
||||||
) : (
|
) : (
|
||||||
<View
|
<OoFigure
|
||||||
style={{
|
avatar={look}
|
||||||
width: size,
|
color={body}
|
||||||
height: size,
|
fallbackShape={look.shape ?? ooIdentityShapeIndex(seed)}
|
||||||
borderRadius: size / 2,
|
moment={moment ?? statusMoment ?? ooMomentForStatus(status)}
|
||||||
backgroundColor: color,
|
size={size}
|
||||||
alignItems: "center",
|
still={still}
|
||||||
justifyContent: "center",
|
/>
|
||||||
}}
|
|
||||||
>
|
|
||||||
<View
|
|
||||||
style={{
|
|
||||||
width: visorW,
|
|
||||||
height: visorH,
|
|
||||||
borderRadius: Math.round(visorH * 0.52),
|
|
||||||
backgroundColor: "#0C0C0E",
|
|
||||||
flexDirection: "row",
|
|
||||||
alignItems: "center",
|
|
||||||
justifyContent: "center",
|
|
||||||
gap,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{[0, 1].map((eye) => (
|
|
||||||
<View
|
|
||||||
key={eye}
|
|
||||||
style={{
|
|
||||||
width: eyeW,
|
|
||||||
height: eyeH,
|
|
||||||
borderRadius: Math.max(2, Math.round(eyeW * 0.6)),
|
|
||||||
backgroundColor: "#fff",
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
</View>
|
|
||||||
</View>
|
|
||||||
)}
|
)}
|
||||||
{isWorking ? (
|
{isWorking ? (
|
||||||
<View
|
<View
|
||||||
@ -128,81 +116,3 @@ export const BotAvatar = memo(function BotAvatar({
|
|||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
function OrganicAvatar({
|
|
||||||
color,
|
|
||||||
identity,
|
|
||||||
size,
|
|
||||||
isWorking,
|
|
||||||
}: {
|
|
||||||
color: string;
|
|
||||||
identity?: string;
|
|
||||||
size: number;
|
|
||||||
isWorking: boolean;
|
|
||||||
}) {
|
|
||||||
const seed = avatarIdentitySeed(identity || color || "#8B5CF6");
|
|
||||||
const progress = useSharedValue(0);
|
|
||||||
const reducedMotion = useReducedMotion();
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
cancelAnimation(progress);
|
|
||||||
progress.value = 0;
|
|
||||||
if (isWorking && !reducedMotion) {
|
|
||||||
progress.value = withRepeat(
|
|
||||||
withTiming(1, {
|
|
||||||
duration: workingAvatarDuration(seed),
|
|
||||||
easing: Easing.linear,
|
|
||||||
}),
|
|
||||||
-1,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return () => cancelAnimation(progress);
|
|
||||||
}, [isWorking, progress, reducedMotion, seed]);
|
|
||||||
|
|
||||||
const bodyStyle = useAnimatedStyle(() => {
|
|
||||||
const frame = workingAvatarFrame(seed, progress.value);
|
|
||||||
return {
|
|
||||||
transform: [
|
|
||||||
{ translateX: (frame.translationX * size) / 120 },
|
|
||||||
{ translateY: (frame.translationY * size) / 120 },
|
|
||||||
{ rotate: `${frame.rotation}deg` },
|
|
||||||
{ scaleX: frame.scaleX },
|
|
||||||
{ scaleY: frame.scaleY },
|
|
||||||
],
|
|
||||||
};
|
|
||||||
});
|
|
||||||
const leftEyeProps = useAnimatedProps(() => {
|
|
||||||
const frame = workingAvatarFrame(seed, progress.value);
|
|
||||||
return { x: -14 + frame.eyeOffsetX, y: -12 + frame.eyeOffsetY };
|
|
||||||
});
|
|
||||||
const rightEyeProps = useAnimatedProps(() => {
|
|
||||||
const frame = workingAvatarFrame(seed, progress.value);
|
|
||||||
return { x: 7 + frame.eyeOffsetX, y: -12 + frame.eyeOffsetY };
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
|
||||||
<View style={{ width: size, height: size }}>
|
|
||||||
<Animated.View style={[{ width: size, height: size }, bodyStyle]}>
|
|
||||||
<Svg width={size} height={size} viewBox="-60 -60 120 120">
|
|
||||||
<Path d={organicAvatarPath(seed)} fill={color} />
|
|
||||||
<G transform={`rotate(${(seed % 9) - 4})`}>
|
|
||||||
<AnimatedRect
|
|
||||||
animatedProps={leftEyeProps}
|
|
||||||
width={7}
|
|
||||||
height={24}
|
|
||||||
rx={3.5}
|
|
||||||
fill="#101014"
|
|
||||||
/>
|
|
||||||
<AnimatedRect
|
|
||||||
animatedProps={rightEyeProps}
|
|
||||||
width={7}
|
|
||||||
height={24}
|
|
||||||
rx={3.5}
|
|
||||||
fill="#101014"
|
|
||||||
/>
|
|
||||||
</G>
|
|
||||||
</Svg>
|
|
||||||
</Animated.View>
|
|
||||||
</View>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|||||||
173
apps/mobile/components/cooking-mode.tsx
Normal file
173
apps/mobile/components/cooking-mode.tsx
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
import type { CardAct, CardSection } from "@rakazo/contracts";
|
||||||
|
import { cardTimerRemaining, formatCardClock } from "@rakazo/core";
|
||||||
|
import { useKeepAwake } from "expo-keep-awake";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { Modal, Pressable, SafeAreaView, StyleSheet, Text, View } from "react-native";
|
||||||
|
import { mobileTokens } from "../lib/appearance";
|
||||||
|
import { useI18n } from "../lib/i18n";
|
||||||
|
import { NativeSymbol } from "./native-symbol";
|
||||||
|
|
||||||
|
type Steps = Extract<CardSection, { type: "steps" }>;
|
||||||
|
|
||||||
|
/** The current time, ticking every second while `active`. */
|
||||||
|
export function useNow(active: boolean): number {
|
||||||
|
const [now, setNow] = useState(() => Date.now());
|
||||||
|
useEffect(() => {
|
||||||
|
if (!active) return;
|
||||||
|
setNow(Date.now());
|
||||||
|
const timer = setInterval(() => setNow(Date.now()), 1000);
|
||||||
|
return () => clearInterval(timer);
|
||||||
|
}, [active]);
|
||||||
|
return now;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A card's steps one at a time in full screen, large enough to read from the stove, with the
|
||||||
|
* screen kept on. The step and any timer live on the card, so the web and a reload agree.
|
||||||
|
*/
|
||||||
|
export function CookingMode({
|
||||||
|
title,
|
||||||
|
steps,
|
||||||
|
now,
|
||||||
|
busy,
|
||||||
|
onAct,
|
||||||
|
onClose,
|
||||||
|
}: {
|
||||||
|
title: string;
|
||||||
|
steps: Steps;
|
||||||
|
now: number;
|
||||||
|
busy: boolean;
|
||||||
|
onAct: (act: CardAct) => Promise<void>;
|
||||||
|
onClose: () => void;
|
||||||
|
}) {
|
||||||
|
useKeepAwake();
|
||||||
|
const tokens = mobileTokens();
|
||||||
|
const { t } = useI18n();
|
||||||
|
const index = Math.min(steps.current ?? 0, steps.steps.length - 1);
|
||||||
|
const step = steps.steps[index];
|
||||||
|
const last = index === steps.steps.length - 1;
|
||||||
|
const remaining = cardTimerRemaining(step?.timerEndsAt, now);
|
||||||
|
const running = remaining !== null && remaining > 0;
|
||||||
|
const go = (next: number) => void onAct({ type: "step", sectionId: steps.id, index: next });
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal animationType="slide" presentationStyle="fullScreen" onRequestClose={onClose}>
|
||||||
|
<SafeAreaView style={[styles.screen, { backgroundColor: tokens.background }]}>
|
||||||
|
<View style={styles.top}>
|
||||||
|
<Text numberOfLines={1} style={[styles.title, { color: tokens.mutedForeground }]}>
|
||||||
|
{title}
|
||||||
|
</Text>
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="button"
|
||||||
|
accessibilityLabel={t("Close")}
|
||||||
|
onPress={onClose}
|
||||||
|
hitSlop={12}
|
||||||
|
>
|
||||||
|
<NativeSymbol ios="xmark" android="close" size={20} />
|
||||||
|
</Pressable>
|
||||||
|
</View>
|
||||||
|
<View style={styles.middle}>
|
||||||
|
<Text style={[styles.counter, { color: tokens.mutedForeground }]}>
|
||||||
|
{t("Step {position} of {total}", {
|
||||||
|
position: index + 1,
|
||||||
|
total: steps.steps.length,
|
||||||
|
})}
|
||||||
|
</Text>
|
||||||
|
<Text style={[styles.step, { color: tokens.foreground }]}>{step?.text}</Text>
|
||||||
|
{step?.timerSeconds ? (
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="button"
|
||||||
|
accessibilityLabel={running ? t("Stop timer") : t("Start timer")}
|
||||||
|
disabled={busy}
|
||||||
|
onPress={() =>
|
||||||
|
void onAct({
|
||||||
|
type: "timer",
|
||||||
|
sectionId: steps.id,
|
||||||
|
stepId: step.id,
|
||||||
|
running: !running,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
style={[
|
||||||
|
styles.timer,
|
||||||
|
{
|
||||||
|
borderColor: running ? tokens.primary : tokens.border,
|
||||||
|
backgroundColor: running ? tokens.primary : "transparent",
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<NativeSymbol
|
||||||
|
ios={running ? "pause.fill" : "play.fill"}
|
||||||
|
android={running ? "pause" : "play"}
|
||||||
|
size={20}
|
||||||
|
color={running ? tokens.primaryForeground : tokens.foreground}
|
||||||
|
/>
|
||||||
|
<Text
|
||||||
|
style={[
|
||||||
|
styles.clock,
|
||||||
|
{ color: running ? tokens.primaryForeground : tokens.foreground },
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
{formatCardClock(remaining ?? step.timerSeconds)}
|
||||||
|
</Text>
|
||||||
|
</Pressable>
|
||||||
|
) : null}
|
||||||
|
</View>
|
||||||
|
<View style={styles.bottom}>
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="button"
|
||||||
|
disabled={busy || index === 0}
|
||||||
|
onPress={() => go(index - 1)}
|
||||||
|
style={[styles.nav, { borderColor: tokens.border, opacity: index === 0 ? 0.4 : 1 }]}
|
||||||
|
>
|
||||||
|
<Text style={[styles.navText, { color: tokens.foreground }]}>{t("Back")}</Text>
|
||||||
|
</Pressable>
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="button"
|
||||||
|
disabled={busy && !last}
|
||||||
|
onPress={() => (last ? onClose() : go(index + 1))}
|
||||||
|
style={[styles.nav, { borderColor: tokens.primary, backgroundColor: tokens.primary }]}
|
||||||
|
>
|
||||||
|
<Text style={[styles.navText, { color: tokens.primaryForeground }]}>
|
||||||
|
{last ? t("Done") : t("Next")}
|
||||||
|
</Text>
|
||||||
|
</Pressable>
|
||||||
|
</View>
|
||||||
|
</SafeAreaView>
|
||||||
|
</Modal>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const styles = StyleSheet.create({
|
||||||
|
screen: { flex: 1, paddingHorizontal: 24 },
|
||||||
|
top: {
|
||||||
|
flexDirection: "row",
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
gap: 12,
|
||||||
|
paddingTop: 16,
|
||||||
|
},
|
||||||
|
title: { flex: 1, fontSize: 15, fontWeight: "600" },
|
||||||
|
middle: { flex: 1, justifyContent: "center", gap: 28 },
|
||||||
|
counter: { fontSize: 15 },
|
||||||
|
step: { fontSize: 30, lineHeight: 40, fontWeight: "600" },
|
||||||
|
timer: {
|
||||||
|
alignSelf: "flex-start",
|
||||||
|
flexDirection: "row",
|
||||||
|
alignItems: "center",
|
||||||
|
gap: 10,
|
||||||
|
borderWidth: 1,
|
||||||
|
borderRadius: 16,
|
||||||
|
paddingHorizontal: 18,
|
||||||
|
paddingVertical: 12,
|
||||||
|
},
|
||||||
|
clock: { fontSize: 26, fontWeight: "600", fontVariant: ["tabular-nums"] },
|
||||||
|
bottom: { flexDirection: "row", justifyContent: "space-between", gap: 12, paddingBottom: 16 },
|
||||||
|
nav: {
|
||||||
|
flex: 1,
|
||||||
|
alignItems: "center",
|
||||||
|
borderWidth: 1,
|
||||||
|
borderRadius: 14,
|
||||||
|
paddingVertical: 14,
|
||||||
|
},
|
||||||
|
navText: { fontSize: 16, fontWeight: "600" },
|
||||||
|
});
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user